Skip to content

Commit 1dff218

Browse files
vhainItalo A. Casas
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 <vieuxtech@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com>
1 parent 52f0092 commit 1dff218

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

‎lib/net.js‎

Lines changed: 9 additions & 15 deletions
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+
constargs=newArray(arguments.length);
904+
for(vari=0;i<arguments.length;i++)
905+
args[i]=arguments[i];
906+
// TODO(joyeecheung): use destructuring when V8 is fast enough
907+
constnormalized=normalizeArgs(args);
908+
constoptions=normalized[0];
909+
constcb=normalized[1];
910+
903911
if(this.write!==Socket.prototype.write)
904912
this.write=Socket.prototype.write;
905913

906-
if(options===null||typeofoptions!=='object'){
907-
// Old API:
908-
// connect(port[, host][, cb])
909-
// connect(path[, cb]);
910-
constargs=newArray(arguments.length);
911-
for(vari=0;i<arguments.length;i++)
912-
args[i]=arguments[i];
913-
constnormalized=normalizeArgs(args);
914-
constnormalizedOptions=normalized[0];
915-
constnormalizedCb=normalized[1];
916-
returnSocket.prototype.connect.call(this,
917-
normalizedOptions,normalizedCb);
918-
}
919-
920914
if(this.destroyed){
921915
this._readableState.reading=false;
922916
this._readableState.ended=false;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
4+
// This test ensures that socket.connect can be called without callback
5+
// which is optional.
6+
7+
constnet=require('net');
8+
9+
constserver=net.createServer(common.mustCall(function(conn){
10+
conn.end();
11+
server.close();
12+
})).listen(0,common.mustCall(function(){
13+
constclient=newnet.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)