Skip to content

Commit c6cbbf9

Browse files
vhainjoyeecheung
authored andcommitted
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: #11761 PR-URL: #11762 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent efec14a commit c6cbbf9

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

lib/net.js

+9-15
Original file line numberDiff line numberDiff line change
@@ -920,24 +920,18 @@ function connect(self, address, port, addressType, localAddress, localPort) {
920920
}
921921

922922

923-
Socket.prototype.connect = function(options, cb) {
923+
Socket.prototype.connect = function() {
924+
const args = new Array(arguments.length);
925+
for (var i = 0; i < arguments.length; i++)
926+
args[i] = arguments[i];
927+
// TODO(joyeecheung): use destructuring when V8 is fast enough
928+
const normalized = normalizeArgs(args);
929+
const options = normalized[0];
930+
const cb = normalized[1];
931+
924932
if (this.write !== Socket.prototype.write)
925933
this.write = Socket.prototype.write;
926934

927-
if (options === null || typeof options !== 'object') {
928-
// Old API:
929-
// connect(port[, host][, cb])
930-
// connect(path[, cb]);
931-
const args = new Array(arguments.length);
932-
for (var i = 0; i < arguments.length; i++)
933-
args[i] = arguments[i];
934-
const normalized = normalizeArgs(args);
935-
const normalizedOptions = normalized[0];
936-
const normalizedCb = normalized[1];
937-
return Socket.prototype.connect.call(this,
938-
normalizedOptions, normalizedCb);
939-
}
940-
941935
if (this.destroyed) {
942936
this._readableState.reading = false;
943937
this._readableState.ended = false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// This test ensures that socket.connect can be called without callback
5+
// which is optional.
6+
7+
const net = require('net');
8+
9+
const server = net.createServer(common.mustCall(function(conn) {
10+
conn.end();
11+
server.close();
12+
})).listen(0, common.mustCall(function() {
13+
const client = new net.Socket();
14+
15+
client.on('connect', common.mustCall(function() {
16+
client.end();
17+
}));
18+
19+
client.connect(server.address());
20+
}));

0 commit comments

Comments
 (0)