Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 72 additions & 0 deletions 72 test/parallel/test-fs-read-stream-file-handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
LiviaMedeiros marked this conversation as resolved.
Show resolved Hide resolved
const controller = new AbortController();
const { signal } = controller;
const stream = handle.createReadStream({ signal });
LiviaMedeiros marked this conversation as resolved.
Show resolved Hide resolved

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();
LiviaMedeiros marked this conversation as resolved.
Show resolved Hide resolved
}).then(common.mustCall());
LiviaMedeiros marked this conversation as resolved.
Show resolved Hide resolved

// 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());
Morty Proxy This is a proxified and sanitized view of the page, visit original site.