Browse Source

Fix net.Socket.connect argument parsing

Fixes #1251.
v0.7.4-release
koichik 14 years ago
parent
commit
c60cdcda4e
  1. 13
      lib/net_legacy.js
  2. 12
      lib/net_uv.js
  3. 20
      test/simple/test-net-create-connection.js

13
lib/net_legacy.js

@ -700,9 +700,14 @@ Socket.prototype.connect = function() {
self._connecting = true; // set false in doConnect
self.writable = true;
var lastArg = arguments[arguments.length - 1];
if (typeof lastArg == 'function') {
self.addListener('connect', lastArg);
var host;
if (typeof arguments[1] === 'function') {
self.on('connect', arguments[1]);
} else {
host = arguments[1];
if (typeof arguments[2] === 'function') {
self.on('connect', arguments[2]);
}
}
var port = toPort(arguments[0]);
@ -715,7 +720,7 @@ Socket.prototype.connect = function() {
doConnect(self, arguments[0]);
} else {
// TCP
require('dns').lookup(arguments[1], function(err, ip, addressType) {
require('dns').lookup(host, function(err, ip, addressType) {
if (err) {
self.emit('error', err);
} else {

12
lib/net_uv.js

@ -24,9 +24,9 @@ exports.createServer = function() {
};
exports.connect = exports.createConnection = function(port, host /* [cb] */ ) {
exports.connect = exports.createConnection = function(port /* [host], [cb] */) {
var s = new Socket();
s.connect(port, host, arguments[2]);
s.connect(port, arguments[1], arguments[2]);
return s;
};
@ -340,7 +340,7 @@ function connectip(self, port, ip) {
}
Socket.prototype.connect = function(port, host /* [cb] */) {
Socket.prototype.connect = function(port /* [host], [cb] */) {
var self = this;
if (this.destroyed) {
@ -348,9 +348,15 @@ Socket.prototype.connect = function(port, host /* [cb] */) {
initSocketHandle(this);
}
var host;
if (typeof arguments[1] === 'function') {
self.on('connect', arguments[1]);
} else {
host = arguments[1];
if (typeof arguments[2] === 'function') {
self.on('connect', arguments[2]);
}
}
timers.active(this);

20
test/simple/test-net-create-connection.js

@ -24,19 +24,27 @@ var assert = require('assert');
var net = require('net');
var tcpPort = common.PORT;
var connectHappened = false;
var clientConnected = 0;
var serverConnected = 0;
var server = net.createServer(function(socket) {
server.close();
socket.end();
if (++serverConnected === 4) {
server.close();
}
});
server.listen(tcpPort, 'localhost', function() {
var client = net.createConnection(tcpPort, 'localhost', function() {
connectHappened = true;
});
function cb() {
++clientConnected;
}
net.createConnection(tcpPort).on('connect', cb);
net.createConnection(tcpPort, 'localhost').on('connect', cb);
net.createConnection(tcpPort, cb);
net.createConnection(tcpPort, 'localhost', cb);
});
process.on('exit', function () {
assert.ok(connectHappened);
assert.equal(clientConnected, 4);
});

Loading…
Cancel
Save