Skip to content

Commit 1dff218

Browse files
vhainitaloacasas
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 52f0092 commit 1dff218

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
@@ -899,24 +899,18 @@ function connect(self, address, port, addressType, localAddress, localPort) {
899899
}
900900

901901

902-
Socket.prototype.connect = function(options, cb) {
902+
Socket.prototype.connect = function() {
903+
const args = new Array(arguments.length);
904+
for (var i = 0; i < arguments.length; i++)
905+
args[i] = arguments[i];
906+
// TODO(joyeecheung): use destructuring when V8 is fast enough
907+
const normalized = normalizeArgs(args);
908+
const options = normalized[0];
909+
const cb = normalized[1];
910+
903911
if (this.write !== Socket.prototype.write)
904912
this.write = Socket.prototype.write;
905913

906-
if (options === null || typeof options !== 'object') {
907-
// Old API:
908-
// connect(port[, host][, cb])
909-
// connect(path[, cb]);
910-
const args = new Array(arguments.length);
911-
for (var i = 0; i < arguments.length; i++)
912-
args[i] = arguments[i];
913-
const normalized = normalizeArgs(args);
914-
const normalizedOptions = normalized[0];
915-
const normalizedCb = normalized[1];
916-
return Socket.prototype.connect.call(this,
917-
normalizedOptions, normalizedCb);
918-
}
919-
920914
if (this.destroyed) {
921915
this._readableState.reading = false;
922916
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)