Skip to content

Commit 88d8e94

Browse files
committed
Ensure superagent is intercepted in Node < 10
1 parent d8a0aeb commit 88d8e94

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

overrides/path/prepend.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ function fixModule(requestedName, filename, loadedModule) {
7474
// Disable built-in proxy support, to let global-agent/tunnel take precedence
7575
fixedModule = loadedModule.defaults({ proxy: false });
7676
fixedModule.INTERCEPTED_BY_HTTPTOOLKIT = true;
77+
} else if (
78+
requestedName === 'superagent' &&
79+
!global.GLOBAL_AGENT && // Works automatically with global-agent
80+
!loadedModule.INTERCEPTED_BY_HTTPTOOLKIT // Make this idempotent
81+
) {
82+
loadedModule.INTERCEPTED_BY_HTTPTOOLKIT = true;
83+
84+
// Global tunnel doesn't successfully reconfigure superagent.
85+
// To fix it, we forcibly override the agent property on every request.
86+
const originalRequestMethod = loadedModule.Request.prototype.request;
87+
fixedModule.Request.prototype.request = function () {
88+
if (this.url.indexOf('https:') === 0) {
89+
this._agent = require('https').globalAgent;
90+
} else {
91+
this._agent = require('http').globalAgent;
92+
}
93+
return originalRequestMethod.apply(this, arguments);
94+
};
7795
} else if (requestedName === 'stripe' && !loadedModule.INTERCEPTED_BY_HTTPTOOLKIT) {
7896
fixedModule = Object.assign(function () {
7997
const result = loadedModule.apply(this, arguments);

0 commit comments

Comments
 (0)