Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions lib/_stream_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,7 @@ function Transform(options) {
this._readableState.sync = false;

this.once('prefinish', function() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there is no need in prefinish after this change

if (util.isFunction(this._flush))
this._flush(function(er) {
done(stream, er);
});
else
done(stream);
done(stream);
});
}

Expand Down
9 changes: 8 additions & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,14 @@ function needFinish(stream, state) {
function prefinish(stream, state) {
if (!state.prefinished) {
state.prefinished = true;
stream.emit('prefinish');
if (util.isFunction(stream._flush)) {
stream._flush(function (err) {
if (err) return stream.emit('error', err)
stream.emit('prefinish')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emitting prefinish in the callback doesn't defer finish

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, you're right.

})
} else {
stream.emit('prefinish');
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/simple/test-stream2-writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,25 @@ test('finish is emitted if last chunk is empty', function(t) {
w.write(Buffer(1));
w.end(Buffer(0));
});

test('write flush', function(t) {
var w = new W();
var writtenData = [];
w._write = function(chunk, e, cb){
writtenData.push(chunk.toString());
cb();
}
w._flush = function(cb){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also try an a async implemention of _flush and see if it still completes
before the 'finish' event is fired:

w._flush = function(cb){
 process.nextTick(function () {
  writtenData.push('flushed');
   cb();
 }); 
};

See related discussion here:
#7612

writtenData.push('flushed');
cb();
}

w.write('start');
w.end();

w.on('finish', function() {
t.equal(writtenData[0], 'start');
t.equal(writtenData[1], 'flushed');
t.end();
});
});