From 44de1fc7de122988d59bc785f01d87f0d766e6ea Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 5 May 2017 05:42:32 -0400 Subject: [PATCH 1/2] Revert "net: don't normalize twice in Socket#connect()" This reverts commit bb06add4d78535f10d1dc8f0c8c3210fecb96c8a. This commit was causing failures in the async-listener module, which monkey patches Socket.prototype.connect(). --- lib/net.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/net.js b/lib/net.js index d50b3729d968a9..38b6c284343ae3 100644 --- a/lib/net.js +++ b/lib/net.js @@ -95,7 +95,7 @@ function connect() { socket.setTimeout(options.timeout); } - return realConnect.call(socket, options, cb); + return Socket.prototype.connect.call(socket, options, cb); } @@ -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; @@ -967,7 +963,7 @@ function realConnect(options, cb) { lookupAndConnect(this, options); } return this; -} +}; function lookupAndConnect(self, options) { From 33918ed5a0cd2029c9dbfef00659b8c43787c4c9 Mon Sep 17 00:00:00 2001 From: Thomas Watson Date: Fri, 5 May 2017 14:49:45 +0200 Subject: [PATCH 2/2] test: test net.connect monkey patching This is important for people who monkey-patches `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. --- .../test-net-connect-call-socket-connect.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/parallel/test-net-connect-call-socket-connect.js diff --git a/test/parallel/test-net-connect-call-socket-connect.js b/test/parallel/test-net-connect-call-socket-connect.js new file mode 100644 index 00000000000000..4c8ac94efcc3a6 --- /dev/null +++ b/test/parallel/test-net-connect-call-socket-connect.js @@ -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(); + })); +}));