Skip to content

Commit ba45367

Browse files
ronagTrott
authored andcommittedOct 13, 2019
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: expressjs/multer#238 PR-URL: #29930 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 6f81401 commit ba45367

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
 

‎lib/internal/fs/streams.js

+6
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ Object.setPrototypeOf(WriteStream.prototype, Writable.prototype);
284284
Object.setPrototypeOf(WriteStream, Writable);
285285

286286
WriteStream.prototype._final = function(callback) {
287+
if (typeof this.fd !== 'number') {
288+
return this.once('open', function() {
289+
this._final(callback);
290+
});
291+
}
292+
287293
if (this.autoClose) {
288294
this.destroy();
289295
}

‎test/parallel/test-fs-write-stream-end.js

+14
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,17 @@ tmpdir.refresh();
4444
assert.strictEqual(content, 'a\n');
4545
}));
4646
}
47+
48+
{
49+
const file = path.join(tmpdir.path, 'write-end-test2.txt');
50+
const stream = fs.createWriteStream(file);
51+
stream.end();
52+
53+
let calledOpen = false;
54+
stream.on('open', () => {
55+
calledOpen = true;
56+
});
57+
stream.on('finish', common.mustCall(() => {
58+
assert.strictEqual(calledOpen, true);
59+
}));
60+
}

0 commit comments

Comments
 (0)