From cf24177abaf30c6d261abb19ad6af2d88a247e52 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Wed, 7 Jun 2017 12:48:35 -0700 Subject: [PATCH] net: return this from destroy() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/13530 Reviewed-By: Refael Ackermann Reviewed-By: Colin Ihrig Reviewed-By: Tobias Nießen Reviewed-By: Jeremiah Senkpiel Reviewed-By: Michael Dawson Reviewed-By: Gibson Fahnestock Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- doc/api/net.md | 2 ++ doc/api/stream.md | 4 +++- lib/internal/streams/destroy.js | 4 +++- test/parallel/test-net-socket-destroy-send.js | 3 ++- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/doc/api/net.md b/doc/api/net.md index 6eb661b43af90c..86c9159bcfbcab 100644 --- a/doc/api/net.md +++ b/doc/api/net.md @@ -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). diff --git a/doc/api/stream.md b/doc/api/stream.md index ca37243c90e62d..ff1b0c78fbcc29 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -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 diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 37e7d4bc364299..86a22633f2fe78 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -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 @@ -39,6 +39,8 @@ function destroy(err, cb) { cb(err); } }); + + return this; } function undestroy() { diff --git a/test/parallel/test-net-socket-destroy-send.js b/test/parallel/test-net-socket-destroy-send.js index 309e41f6c94e6d..6dc4b9566a3900 100644 --- a/test/parallel/test-net-socket-destroy-send.js +++ b/test/parallel/test-net-socket-destroy-send.js @@ -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'); }));