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 <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
sam-github authored and rvagg committed Jun 29, 2017
1 parent 4e5fdc5 commit 0379174
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 0379174

Please sign in to comment.