Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: allow passing true to emitClose option #29212

Closed
wants to merge 4 commits into from
Closed
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
16 changes: 16 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,9 @@ fs.copyFileSync('source.txt', 'destination.txt', COPYFILE_EXCL);
<!-- YAML
added: v0.1.31
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/29212
description: Enable `emitClose` option.
- version: v11.0.0
pr-url: https://github.com/nodejs/node/pull/19898
description: Impose new restrictions on `start` and `end`, throwing
Expand All @@ -1529,6 +1532,7 @@ changes:
* `fd` {integer} **Default:** `null`
* `mode` {integer} **Default:** `0o666`
* `autoClose` {boolean} **Default:** `true`
* `emitClose` {boolean} **Default:** `false`
* `start` {integer}
* `end` {integer} **Default:** `Infinity`
* `highWaterMark` {integer} **Default:** `64 * 1024`
Expand All @@ -1555,6 +1559,10 @@ If `fd` points to a character device that only supports blocking reads
available. This can prevent the process from exiting and the stream from
closing naturally.

By default, the stream will not emit a `'close'` event after it has been
destroyed. This is the opposite of the default for other `Readable` streams.
Set the `emitClose` option to `true` to change this behavior.

```js
const fs = require('fs');
// Create a stream from some character device.
Expand Down Expand Up @@ -1592,6 +1600,9 @@ If `options` is a string, then it specifies the encoding.
<!-- YAML
added: v0.1.31
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/29212
description: Enable `emitClose` option.
- version: v7.6.0
pr-url: https://github.com/nodejs/node/pull/10739
description: The `path` parameter can be a WHATWG `URL` object using
Expand All @@ -1615,6 +1626,7 @@ changes:
* `fd` {integer} **Default:** `null`
* `mode` {integer} **Default:** `0o666`
* `autoClose` {boolean} **Default:** `true`
* `emitClose` {boolean} **Default:** `false`
* `start` {integer}
* Returns: {fs.WriteStream} See [Writable Stream][].

Expand All @@ -1631,6 +1643,10 @@ then the file descriptor won't be closed, even if there's an error.
It is the application's responsibility to close it and make sure there's no
file descriptor leak.

By default, the stream will not emit a `'close'` event after it has been
destroyed. This is the opposite of the default for other `Writable` streams.
Set the `emitClose` option to `true` to change this behavior.

Like [`ReadStream`][], if `fd` is specified, [`WriteStream`][] will ignore the
`path` argument and will use the specified file descriptor. This means that no
`'open'` event will be emitted. `fd` should be blocking; non-blocking `fd`s
Expand Down
8 changes: 6 additions & 2 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ function ReadStream(path, options) {
options.highWaterMark = 64 * 1024;

// For backwards compat do not emit close on destroy.
options.emitClose = false;
if (options.emitClose === undefined) {
options.emitClose = false;
}

Readable.call(this, options);

Expand Down Expand Up @@ -241,7 +243,9 @@ function WriteStream(path, options) {
options = copyObject(getOptions(options, {}));

// For backwards compat do not emit close on destroy.
options.emitClose = false;
if (options.emitClose === undefined) {
options.emitClose = false;
}

Writable.call(this, options);

Expand Down
27 changes: 25 additions & 2 deletions test/parallel/test-fs-stream-destroy-emit-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,31 @@ const fs = require('fs');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

test(fs.createReadStream(__filename));
test(fs.createWriteStream(`${tmpdir.path}/dummy`));
{
const stream = fs.createReadStream(__filename);
stream.on('close', common.mustNotCall());
test(stream);
}

{
const stream = fs.createWriteStream(`${tmpdir.path}/dummy`);
stream.on('close', common.mustNotCall());
test(stream);
}

{
const stream = fs.createReadStream(__filename, { emitClose: true });
stream.on('close', common.mustCall());
test(stream);
}

{
const stream = fs.createWriteStream(`${tmpdir.path}/dummy2`,
{ emitClose: true });
stream.on('close', common.mustCall());
test(stream);
}


function test(stream) {
const err = new Error('DESTROYED');
Expand Down