From 2345bd0e47d810a3b9251ad8f522936ec95e58e5 Mon Sep 17 00:00:00 2001 From: Roman Reiss Date: Sat, 23 May 2015 07:48:13 +0200 Subject: [PATCH] net: return this from setNoDelay and setKeepAlive Modifies the Socket.setNoDelay and Socket.setKeepAlive methods to return the Socket instance instead of undefined, to allow for chaining. --- doc/api/net.markdown | 4 ++++ lib/net.js | 8 ++++++-- test/parallel/test-net-persistent-keepalive.js | 3 ++- test/parallel/test-net-persistent-nodelay.js | 3 ++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/doc/api/net.markdown b/doc/api/net.markdown index 46fc46fde4fdb2..c4631bef1897db 100644 --- a/doc/api/net.markdown +++ b/doc/api/net.markdown @@ -464,6 +464,8 @@ algorithm, they buffer data before sending it off. Setting `true` for `noDelay` will immediately fire off data each time `socket.write()` is called. `noDelay` defaults to `true`. +Returns `socket`. + ### socket.setKeepAlive([enable][, initialDelay]) Enable/disable keep-alive functionality, and optionally set the initial @@ -475,6 +477,8 @@ data packet received and the first keepalive probe. Setting 0 for initialDelay will leave the value unchanged from the default (or previous) setting. Defaults to `0`. +Returns `socket`. + ### socket.address() Returns the bound address, the address family name and port of the diff --git a/lib/net.js b/lib/net.js index 8412bcd38b86df..0c5930bca94066 100644 --- a/lib/net.js +++ b/lib/net.js @@ -324,23 +324,27 @@ Socket.prototype.setNoDelay = function(enable) { if (!this._handle) { this.once('connect', enable ? this.setNoDelay : this.setNoDelay.bind(this, enable)); - return; + return this; } // backwards compatibility: assume true when `enable` is omitted if (this._handle.setNoDelay) this._handle.setNoDelay(enable === undefined ? true : !!enable); + + return this; }; Socket.prototype.setKeepAlive = function(setting, msecs) { if (!this._handle) { this.once('connect', this.setKeepAlive.bind(this, setting, msecs)); - return; + return this; } if (this._handle.setKeepAlive) this._handle.setKeepAlive(setting, ~~(msecs / 1000)); + + return this; }; diff --git a/test/parallel/test-net-persistent-keepalive.js b/test/parallel/test-net-persistent-keepalive.js index adf7d7a4fa81d8..a54833a8f56371 100644 --- a/test/parallel/test-net-persistent-keepalive.js +++ b/test/parallel/test-net-persistent-keepalive.js @@ -18,7 +18,8 @@ echoServer.on('listening', function() { var clientConnection = new net.Socket(); // send a keepalive packet after 1000 ms // and make sure it persists - clientConnection.setKeepAlive(true, 400); + var s = clientConnection.setKeepAlive(true, 400); + assert.ok(s instanceof net.Socket); clientConnection.connect(common.PORT); clientConnection.setTimeout(0); diff --git a/test/parallel/test-net-persistent-nodelay.js b/test/parallel/test-net-persistent-nodelay.js index 8ed5925f11a659..e120305b3a62eb 100644 --- a/test/parallel/test-net-persistent-nodelay.js +++ b/test/parallel/test-net-persistent-nodelay.js @@ -24,7 +24,8 @@ echoServer.on('listening', function() { // setNoDelay before the handle is created // there is probably a better way to test this - sock1.setNoDelay(); + var s = sock1.setNoDelay(); + assert.ok(s instanceof net.Socket); sock1.connect(common.PORT); sock1.on('end', function() { assert.equal(callCount, 1);