diff options
author | Robert Nagy <ronagy@icloud.com> | 2019-10-11 13:12:18 +0300 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2019-10-18 10:09:12 +0300 |
commit | 8ebc94562cb570a8f2331675dc83c37fd9fe7417 (patch) | |
tree | 48d4061973c00b4206cdf0eda80b2bc84e4f7b32 | |
parent | b59209b1189249b970c56182c5b091b37ba7144c (diff) |
fs: do not emit 'finish' before 'open' on write empty file
'finish' could previously be emitted before the file has been
created when ending a write stream without having written any
data.
Refs: https://github.com/expressjs/multer/issues/238
PR-URL: https://github.com/nodejs/node/pull/29930
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
-rw-r--r-- | lib/internal/fs/streams.js | 6 | ||||
-rw-r--r-- | test/parallel/test-fs-write-stream-end.js | 14 |
2 files changed, 20 insertions, 0 deletions
diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index 110ad114930..210c1ad1ae1 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -272,6 +272,12 @@ Object.setPrototypeOf(WriteStream.prototype, Writable.prototype); Object.setPrototypeOf(WriteStream, Writable); WriteStream.prototype._final = function(callback) { + if (typeof this.fd !== 'number') { + return this.once('open', function() { + this._final(callback); + }); + } + if (this.autoClose) { this.destroy(); } diff --git a/test/parallel/test-fs-write-stream-end.js b/test/parallel/test-fs-write-stream-end.js index 36e7cb5504c..daaff6a2530 100644 --- a/test/parallel/test-fs-write-stream-end.js +++ b/test/parallel/test-fs-write-stream-end.js @@ -44,3 +44,17 @@ tmpdir.refresh(); assert.strictEqual(content, 'a\n'); })); } + +{ + const file = path.join(tmpdir.path, 'write-end-test2.txt'); + const stream = fs.createWriteStream(file); + stream.end(); + + let calledOpen = false; + stream.on('open', () => { + calledOpen = true; + }); + stream.on('finish', common.mustCall(() => { + assert.strictEqual(calledOpen, true); + })); +} |