Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: call end(cb) callback after destroy #29859

Closed
wants to merge 1 commit into from
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
14 changes: 13 additions & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,20 @@ Writable.prototype.end = function(chunk, encoding, cb) {
encoding = null;
}

if (chunk !== null && chunk !== undefined)
if (chunk !== null && chunk !== undefined) {
this.write(chunk, encoding);
} else if (state.destroyed) {
// TODO(ronag): Condition is for backwards compat.
if (state.errorEmitted || state.finished) {
const err = new ERR_STREAM_DESTROYED('end');
if (typeof cb === 'function') {
process.nextTick(cb, err);
}
// TODO(ronag): Disabled for backwards compat.
// errorOrDestroy(this, err, true);
return;
}
}

// .end() fully uncorks
if (state.corked) {
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-stream-writable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,33 @@ const assert = require('assert');
}));
write.uncork();
}

{
// Call end(cb) after error & destroy

const write = new Writable({
write(chunk, enc, cb) { cb(new Error('asd')); }
});
write.on('error', common.mustCall(() => {
write.destroy();
write.end(common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED');
}));
}));
write.write('asd');
}

{
// Call end(cb) after finish & destroy

const write = new Writable({
write(chunk, enc, cb) { cb(); }
});
write.on('finish', common.mustCall(() => {
write.destroy();
write.end(common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED');
}));
}));
write.end();
}