From df1ba9734811b53713b6940e9becb7a9ea139fa6 Mon Sep 17 00:00:00 2001 From: Giorgos Ntemiris Date: Mon, 19 Aug 2019 21:03:21 +0200 Subject: [PATCH 1/4] fs: allow passing true to emitClose option Allow passing true for emitClose option for fs streams. Fixes: https://github.com/nodejs/node/issues/29177 --- lib/internal/fs/streams.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index dfff08dbbd1d2a..deb79850f4924c 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -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); @@ -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); From 93c1f17ec2861d602b263b9450d19b127873d9fa Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 21 Aug 2019 22:01:38 -0700 Subject: [PATCH 2/4] test: add `emitClose: true` tests for fs streams --- .../test-fs-stream-destroy-emit-error.js | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-fs-stream-destroy-emit-error.js b/test/parallel/test-fs-stream-destroy-emit-error.js index c0405ce5f154f0..c1db9547a8a158 100644 --- a/test/parallel/test-fs-stream-destroy-emit-error.js +++ b/test/parallel/test-fs-stream-destroy-emit-error.js @@ -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'); From 3bb7bf4fd91b23684b8554ba9e0acd06446787a1 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 21 Aug 2019 22:13:56 -0700 Subject: [PATCH 3/4] doc: add emitClose option for fs streams --- doc/api/fs.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/doc/api/fs.md b/doc/api/fs.md index d71f8afd82b878..233178cbc37f98 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1504,6 +1504,9 @@ fs.copyFileSync('source.txt', 'destination.txt', COPYFILE_EXCL);