Skip to content

Commit 77730ab

Browse files
committed
fixup: webpack
1 parent 147f5fc commit 77730ab

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

packages/devtools-proxy-support/src/proxy-agent.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,23 +214,32 @@ export class ProxyAgent extends Agent {
214214
}
215215
}
216216

217+
declare const __webpack_require__: unknown;
218+
217219
// Work around https://github.com/TooTallNate/proxy-agents/pull/329
218220
// While the proxy-agent package implementation in this file,
219221
// and in the original, properly check whether an 'upgrade' header
220222
// is present and set to 'websocket', the pac-proxy-agent performs
221223
// a similar 'CONNECT vs regular HTTP proxy' selection and doesn't
222224
// account for this. We monkey-patch in this behavior ourselves.
223225
function installPacHttpsHack() {
224-
const pacProxyAgentPath = require.resolve('pac-proxy-agent');
225-
const pacRequire = createRequire(pacProxyAgentPath);
226-
const { HttpProxyAgent } = pacRequire(
227-
'http-proxy-agent'
228-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
229-
) as typeof import('http-proxy-agent');
230-
const { HttpsProxyAgent } = pacRequire(
231-
'https-proxy-agent'
232-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
233-
) as typeof import('https-proxy-agent');
226+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
227+
let HttpProxyAgent: typeof import('http-proxy-agent').HttpProxyAgent;
228+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
229+
let HttpsProxyAgent: typeof import('https-proxy-agent').HttpsProxyAgent;
230+
if (typeof __webpack_require__ === 'undefined') {
231+
const pacProxyAgentPath = require.resolve('pac-proxy-agent');
232+
const pacRequire = createRequire(pacProxyAgentPath);
233+
HttpProxyAgent = pacRequire('http-proxy-agent').HttpProxyAgent;
234+
HttpsProxyAgent = pacRequire('https-proxy-agent').HttpsProxyAgent;
235+
} else {
236+
// No such thing as require.resolve() in webpack, just need to assume
237+
// that everything is hoisted :(
238+
// eslint-disable-next-line @typescript-eslint/no-var-requires
239+
HttpProxyAgent = require('http-proxy-agent').HttpProxyAgent;
240+
// eslint-disable-next-line @typescript-eslint/no-var-requires
241+
HttpsProxyAgent = require('https-proxy-agent').HttpsProxyAgent;
242+
}
234243

235244
const kCompanionHttpsProxyAgent = Symbol('kCompanionHttpsProxyAgent');
236245
// eslint-disable-next-line @typescript-eslint/unbound-method

0 commit comments

Comments
 (0)