Skip to content

Commit

Permalink
fix: wrap net.connect failed in node.js 8+ (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabakugaara authored and watson committed Dec 21, 2017
1 parent a2fde2d commit 0d62bcc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var shimmer = require('shimmer')

var v6plus = semver.gte(process.version, '6.0.0');
var v7plus = semver.gte(process.version, '7.0.0');
var v8plus = semver.gte(process.version, '8.0.0');

var net = require('net');

Expand Down Expand Up @@ -105,10 +106,18 @@ function patchOnRead(ctx) {

wrap(net.Socket.prototype, 'connect', function (original) {
return function () {
var args;
if (v8plus &&
Array.isArray(arguments[0]) &&
Object.getOwnPropertySymbols(arguments[0]).length > 0) {
// already normalized
args = arguments[0];
} else {
// From Node.js v7.0.0, net._normalizeConnectArgs have been renamed net._normalizeArgs
var args = v7plus
? net._normalizeArgs(arguments)
: net._normalizeConnectArgs(arguments);
args = v7plus
? net._normalizeArgs(arguments)
: net._normalizeConnectArgs(arguments);
}
if (args[1]) args[1] = wrapCallback(args[1]);
var result = original.apply(this, args);
patchOnRead(this);
Expand Down
2 changes: 1 addition & 1 deletion test/http-request.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ test('http.Agent socket reuse works', function(t){
})
];

if (nodeVersion[0] > 4 && nodeVersion[0] < 8) {
if (nodeVersion[0] > 4) {
firstImmediateChildren.push(make('ping #0 request'));
};

Expand Down

0 comments on commit 0d62bcc

Please sign in to comment.