0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00

fs: acknowledge signal option in filehandle.createReadStream()

PR-URL: https://github.com/nodejs/node/pull/55148
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Livia Medeiros 2024-10-03 15:32:36 +09:00 committed by GitHub
parent 6b9413e41a
commit 36ca010bef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 73 additions and 0 deletions

View File

@ -267,6 +267,7 @@ added: v16.11.0
* `start` {integer}
* `end` {integer} **Default:** `Infinity`
* `highWaterMark` {integer} **Default:** `64 * 1024`
* `signal` {AbortSignal|undefined} **Default:** `undefined`
* Returns: {fs.ReadStream}
Unlike the 16 KiB default `highWaterMark` for a {stream.Readable}, the stream

View File

@ -80,3 +80,75 @@ fs.promises.open(file, 'r').then((handle) => {
assert.strictEqual(output, input);
}));
}).then(common.mustCall());
// AbortSignal option test
fs.promises.open(file, 'r').then((handle) => {
const controller = new AbortController();
const { signal } = controller;
const stream = handle.createReadStream({ signal });
stream.on('data', common.mustNotCall());
stream.on('end', common.mustNotCall());
stream.on('error', common.mustCall((err) => {
assert.strictEqual(err.name, 'AbortError');
}));
stream.on('close', common.mustCall(() => {
handle.close();
}));
controller.abort();
}).then(common.mustCall());
// Already-aborted signal test
fs.promises.open(file, 'r').then((handle) => {
const signal = AbortSignal.abort();
const stream = handle.createReadStream({ signal });
stream.on('data', common.mustNotCall());
stream.on('end', common.mustNotCall());
stream.on('error', common.mustCall((err) => {
assert.strictEqual(err.name, 'AbortError');
}));
stream.on('close', common.mustCall(() => {
handle.close();
}));
}).then(common.mustCall());
// Invalid signal type test
fs.promises.open(file, 'r').then((handle) => {
for (const signal of [1, {}, [], '', null, NaN, 1n, () => {}, Symbol(), false, true]) {
assert.throws(() => {
handle.createReadStream({ signal });
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
}
return handle.close();
}).then(common.mustCall());
// Custom abort reason test
fs.promises.open(file, 'r').then((handle) => {
const controller = new AbortController();
const { signal } = controller;
const reason = new Error('some silly abort reason');
const stream = handle.createReadStream({ signal });
stream.on('data', common.mustNotCall());
stream.on('end', common.mustNotCall());
stream.on('error', common.mustCall((err) => {
assert.strictEqual(err.name, 'AbortError');
assert.strictEqual(err.cause, reason);
}));
stream.on('close', common.mustCall(() => {
handle.close();
}));
controller.abort(reason);
}).then(common.mustCall());