Skip to content

Commit

Permalink
net: return this from destroy()
Browse files Browse the repository at this point in the history
PR-URL: #13530
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gibson Fahnestock <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
sam-github authored and addaleax committed Jul 11, 2017
1 parent e2d3254 commit 69f806c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,8 @@ callback.
added: v0.1.90
-->

* Returns: {net.Socket}

Ensures that no more I/O activity happens on this socket. Only necessary in
case of errors (parse error or so).

Expand Down
4 changes: 3 additions & 1 deletion doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,10 @@ A Writable stream in object mode will always ignore the `encoding` argument.
added: v8.0.0
-->

* Returns: `this`

Destroy the stream, and emit the passed error. After this call, the
writible stream has ended. Implementors should not override this method,
writable stream has ended. Implementors should not override this method,
but instead implement [`writable._destroy`][writable-_destroy].

### Readable Streams
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function destroy(err, cb) {
(!this._writableState || !this._writableState.errorEmitted)) {
process.nextTick(emitErrorNT, this, err);
}
return;
return this;
}

// we set destroyed to true before firing error callbacks in order
Expand All @@ -39,6 +39,8 @@ function destroy(err, cb) {
cb(err);
}
});

return this;
}

function undestroy() {
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-net-socket-destroy-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ server.listen(0, common.mustCall(function() {
const conn = net.createConnection(port);

conn.on('connect', common.mustCall(function() {
conn.destroy();
// Test destroy returns this, even on multiple calls when it short-circuits.
assert.strictEqual(conn, conn.destroy().destroy());
conn.on('error', common.mustCall(function(err) {
assert.strictEqual(err.message, 'This socket is closed');
}));
Expand Down

0 comments on commit 69f806c

Please sign in to comment.