Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "net: don't normalize twice in Socket#connect()" #12852

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function connect() {
socket.setTimeout(options.timeout);
}

return realConnect.call(socket, options, cb);
return Socket.prototype.connect.call(socket, options, cb);
}


Expand Down Expand Up @@ -919,14 +919,10 @@ Socket.prototype.connect = function() {
for (var i = 0; i < arguments.length; i++)
args[i] = arguments[i];
// TODO(joyeecheung): use destructuring when V8 is fast enough
var normalized = normalizeArgs(args);
var options = normalized[0];
var cb = normalized[1];
return realConnect.call(this, options, cb);
};
const normalized = normalizeArgs(args);
const options = normalized[0];
const cb = normalized[1];


function realConnect(options, cb) {
if (this.write !== Socket.prototype.write)
this.write = Socket.prototype.write;

Expand Down Expand Up @@ -967,7 +963,7 @@ function realConnect(options, cb) {
lookupAndConnect(this, options);
}
return this;
}
};


function lookupAndConnect(self, options) {
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-net-connect-call-socket-connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

// This test checks that calling `net.connect` internally calls
// `Socket.prototype.connect`.
//
// This is important for people who monkey-patch `Socket.prototype.connect`
// since it's not possible to monkey-patch `net.connect` directly (as the core
// `connect` function is called internally in Node instead of calling the
// `exports.connect` function).
//
// Monkey-patching of `Socket.prototype.connect` is done by - among others -
// most APM vendors, the async-listener module and the
// continuation-local-storage module.
//
// See https://github.com/nodejs/node/pull/12852 for details.

const common = require('../common');
const net = require('net');
const Socket = net.Socket;

// monkey patch Socket.prototype.connect to check that it's called
const orig = Socket.prototype.connect;
Socket.prototype.connect = common.mustCall(function() {
return orig.apply(this, arguments);
});

const server = net.createServer();

server.listen(common.mustCall(function() {
const port = server.address().port;
const client = net.connect({port}, common.mustCall(function() {
client.end();
}));
client.on('end', common.mustCall(function() {
server.close();
}));
}));