From b9f1711b3e37ce43fbbe0cb63ff420e55798da2d Mon Sep 17 00:00:00 2001 From: Juwan Yoo Date: Wed, 8 Mar 2017 19:36:15 -0800 Subject: [PATCH] net: allow missing callback for Socket.connect Arguments of Socket.prototype.connect should be also normalized, causing error when called without callback. Changed Socket.prototype.connect's code same as net.connect and added test. Fixes: https://github.com/nodejs/node/issues/11761 --- lib/net.js | 24 +++++++------------ .../test-net-socket-connect-without-cb.js | 20 ++++++++++++++++ 2 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 test/parallel/test-net-socket-connect-without-cb.js diff --git a/lib/net.js b/lib/net.js index 64e50e907719d5..3d94913ccf33bb 100644 --- a/lib/net.js +++ b/lib/net.js @@ -899,24 +899,18 @@ function connect(self, address, port, addressType, localAddress, localPort) { } -Socket.prototype.connect = function(options, cb) { +Socket.prototype.connect = function() { + const args = new Array(arguments.length); + for (var i = 0; i < arguments.length; i++) + args[i] = arguments[i]; + // TODO(joyeecheung): use destructuring when V8 is fast enough + const normalized = normalizeArgs(args); + const options = normalized[0]; + const cb = normalized[1]; + if (this.write !== Socket.prototype.write) this.write = Socket.prototype.write; - if (options === null || typeof options !== 'object') { - // Old API: - // connect(port[, host][, cb]) - // connect(path[, cb]); - const args = new Array(arguments.length); - for (var i = 0; i < arguments.length; i++) - args[i] = arguments[i]; - const normalized = normalizeArgs(args); - const normalizedOptions = normalized[0]; - const normalizedCb = normalized[1]; - return Socket.prototype.connect.call(this, - normalizedOptions, normalizedCb); - } - if (this.destroyed) { this._readableState.reading = false; this._readableState.ended = false; diff --git a/test/parallel/test-net-socket-connect-without-cb.js b/test/parallel/test-net-socket-connect-without-cb.js new file mode 100644 index 00000000000000..468b29a3a486d7 --- /dev/null +++ b/test/parallel/test-net-socket-connect-without-cb.js @@ -0,0 +1,20 @@ +'use strict'; +const common = require('../common'); + +// This test ensures that socket.connect can be called without callback +// which is optional. + +const net = require('net'); + +const server = net.createServer(common.mustCall(function(conn) { + conn.end(); + server.close(); +})).listen(0, common.mustCall(function() { + const client = new net.Socket(); + + client.on('connect', common.mustCall(function() { + client.end(); + })); + + client.connect(server.address()); +}));