Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Commit

Permalink
skip http.request patching if already done
Browse files Browse the repository at this point in the history
fixes #22
  • Loading branch information
vfernandestoptal committed Feb 17, 2019
1 parent c7ffe87 commit 9c295a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
34 changes: 19 additions & 15 deletions patch-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ const https = require('https');
*
* There is currently no PR attempting to move this property upstream.
*/
https.request = (function(request) {
return function(_options, cb) {
let options;
if (typeof _options === 'string') {
options = url.parse(_options);
} else {
options = Object.assign({}, _options);
}
if (null == options.port) {
options.port = 443;
}
options.secureEndpoint = true;
return request.call(https, options, cb);
};
})(https.request);
const patchMarker = "__agent_base_https_request_patched__";
if (!https.request[patchMarker]) {
https.request = (function(request) {
return function(_options, cb) {
let options;
if (typeof _options === 'string') {
options = url.parse(_options);
} else {
options = Object.assign({}, _options);
}
if (null == options.port) {
options.port = 443;
}
options.secureEndpoint = true;
return request.call(https, options, cb);
};
})(https.request);
https.request[patchMarker] = true;
}

/**
* This is needed for Node.js >= 9.0.0 to make sure `https.get()` uses the
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,17 @@ describe('"https" module', function() {
});
});

it('should not re-patch https.request', () => {
var patchModulePath = "../patch-core";
var patchedRequest = https.request;

delete require.cache[require.resolve(patchModulePath)];
require(patchModulePath);

assert.equal(patchedRequest, https.request);
assert.equal(true, https.request.__agent_base_https_request_patched__);
});

describe('PassthroughAgent', function() {
it('should pass through to `https.globalAgent`', function(done) {
// add HTTP server "request" listener
Expand Down

0 comments on commit 9c295a5

Please sign in to comment.