From 08b7bf2f7031b9183a20a6e9ccbcbfdc17a4dcec Mon Sep 17 00:00:00 2001 From: Kohei Hiraga Date: Mon, 2 Apr 2018 01:58:16 +0900 Subject: [PATCH 1/2] fs: fix missing 'error' event in ReadStream/WriteSteam destroy(err) fs.ReadStream / fs.WriteStrem destroy([error]) function should emit 'error' event if [error] is set. Fixes: https://github.com/nodejs/node/issues/19727 --- lib/fs.js | 2 +- .../test-fs-stream-destroy-emit-error.js | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-fs-stream-destroy-emit-error.js diff --git a/lib/fs.js b/lib/fs.js index 2de5f8dd6e205b..653039cd37c442 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2125,7 +2125,7 @@ ReadStream.prototype._destroy = function(err, cb) { return; } - closeFsStream(this, cb); + closeFsStream(this, cb, err); this.fd = null; }; diff --git a/test/parallel/test-fs-stream-destroy-emit-error.js b/test/parallel/test-fs-stream-destroy-emit-error.js new file mode 100644 index 00000000000000..e91ee5343fb3df --- /dev/null +++ b/test/parallel/test-fs-stream-destroy-emit-error.js @@ -0,0 +1,41 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); + +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +test(fs.createReadStream(__filename)); +test(fs.createWriteStream(`${tmpdir.path}/dummy`)); + +function test(stream) { + const err = new Error('DESTROYED'); + stream.on('open', function() { + stream.destroy(err); + }); + stream.on('error', common.mustCall(function(err_) { + assert.strictEqual(err_, err); + })); +} From c90e2fe5ffaf1f7f5a75a747d09aed77a5c0b661 Mon Sep 17 00:00:00 2001 From: Kohei Hiraga Date: Mon, 2 Apr 2018 08:50:33 +0900 Subject: [PATCH 2/2] test: remove the copyright boilerplate --- .../test-fs-stream-destroy-emit-error.js | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/test/parallel/test-fs-stream-destroy-emit-error.js b/test/parallel/test-fs-stream-destroy-emit-error.js index e91ee5343fb3df..c0405ce5f154f0 100644 --- a/test/parallel/test-fs-stream-destroy-emit-error.js +++ b/test/parallel/test-fs-stream-destroy-emit-error.js @@ -1,24 +1,3 @@ -// Copyright Joyent, Inc. and other Node contributors. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to permit -// persons to whom the Software is furnished to do so, subject to the -// following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN -// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE -// USE OR OTHER DEALINGS IN THE SOFTWARE. - 'use strict'; const common = require('../common'); const assert = require('assert');