fs: fix missing 'error' event in (Read|Write)Stream#destroy

fs.ReadStream / fs.WriteStream destroy([error]) function
should emit 'error' event if `error` is set.

PR-URL: https://github.com/nodejs/node/pull/19735
Fixes: https://github.com/nodejs/node/issues/19727
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Kohei Hiraga 2018-04-02 01:58:16 +09:00 committed by Ruben Bridgewater
parent 9c06770443
commit 6e2d5af0e4
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 21 additions and 1 deletions

View File

@ -2128,7 +2128,7 @@ ReadStream.prototype._destroy = function(err, cb) {
return;
}
closeFsStream(this, cb);
closeFsStream(this, cb, err);
this.fd = null;
};

View File

@ -0,0 +1,20 @@
'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);
}));
}