Skip to content

Commit

Permalink
fs: don't emit 'finish' before 'open' on write empty file
Browse files Browse the repository at this point in the history
'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
  • Loading branch information
ronag committed Oct 11, 2019
1 parent 9f873b3 commit f341a47
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-fs-write-stream-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
}

0 comments on commit f341a47

Please sign in to comment.