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: fix destroy(err, cb) regression #13156

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
5 changes: 4 additions & 1 deletion lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ function destroy(err, cb) {
this._writableState.destroyed;
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO If the cb should stay undocumented don't put it in the signature, extract it from arguments.
My IDE (WebStorm) will intellisense it's presence.

Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO we shouldn't use arguments ever unless we really need to.

Copy link
Member Author

Choose a reason for hiding this comment

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

this was done in the previous PR, I would prefer to discuss that in another issue, if you want to fire a PR (I would like to get this in ASAP, keeping it only on the regression).

Copy link
Contributor

Choose a reason for hiding this comment

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

Not feeling strongly about this... Just FYI.


if (readableDestroyed || writableDestroyed) {
if (err && (!this._writableState || !this._writableState.errorEmitted)) {
if (cb) {
cb(err);
} else if (err &&
(!this._writableState || !this._writableState.errorEmitted)) {
process.nextTick(emitErrorNT, this, err);
}
return;
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-net-socket-destroy-send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const common = require('../common');
const net = require('net');
const assert = require('assert');

const server = net.createServer();
server.listen(0, common.mustCall(function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally I like it that if a function is used it's a strong indicator that you need this bound.

Copy link
Member Author

@mcollina mcollina May 22, 2017

Choose a reason for hiding this comment

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

This is just a plain old ES5 function. I tend to say the contrary: I use () => {} as a strong indicator that I need this to stay the same.

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool.

const port = server.address().port;
const conn = net.createConnection(port);

conn.on('connect', common.mustCall(function() {
conn.destroy();
conn.on('error', common.mustCall(function(err) {
assert.strictEqual(err.message, 'This socket is closed');
}));
conn.write(Buffer.from('kaboom'), common.mustCall(function(err) {
assert.strictEqual(err.message, 'This socket is closed');
}));
server.close();
}));
}));
14 changes: 14 additions & 0 deletions test/parallel/test-stream-readable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,17 @@ const { inherits } = require('util');

new MyReadable();
}

{
// destroy and destroy callback
const read = new Readable({
read() {}
});
read.resume();

const expected = new Error('kaboom');

read.destroy(expected, common.mustCall(function(err) {
assert.strictEqual(expected, err);
}));
}
15 changes: 15 additions & 0 deletions test/parallel/test-stream-writable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,18 @@ const { inherits } = require('util');

new MyWritable();
}

{
// destroy and destroy callback
const write = new Writable({
write(chunk, enc, cb) { cb(); }
});

write.destroy();

const expected = new Error('kaboom');

write.destroy(expected, common.mustCall(function(err) {
assert.strictEqual(expected, err);
}));
}