Skip to content

Commit

Permalink
fixup: webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Aug 13, 2024
1 parent 147f5fc commit 77730ab
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions packages/devtools-proxy-support/src/proxy-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,23 +214,32 @@ export class ProxyAgent extends Agent {
}
}

declare const __webpack_require__: unknown;

// Work around https://github.com/TooTallNate/proxy-agents/pull/329
// While the proxy-agent package implementation in this file,
// and in the original, properly check whether an 'upgrade' header
// is present and set to 'websocket', the pac-proxy-agent performs
// a similar 'CONNECT vs regular HTTP proxy' selection and doesn't
// account for this. We monkey-patch in this behavior ourselves.
function installPacHttpsHack() {
const pacProxyAgentPath = require.resolve('pac-proxy-agent');
const pacRequire = createRequire(pacProxyAgentPath);
const { HttpProxyAgent } = pacRequire(
'http-proxy-agent'
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
) as typeof import('http-proxy-agent');
const { HttpsProxyAgent } = pacRequire(
'https-proxy-agent'
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
) as typeof import('https-proxy-agent');
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
let HttpProxyAgent: typeof import('http-proxy-agent').HttpProxyAgent;
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
let HttpsProxyAgent: typeof import('https-proxy-agent').HttpsProxyAgent;
if (typeof __webpack_require__ === 'undefined') {
const pacProxyAgentPath = require.resolve('pac-proxy-agent');
const pacRequire = createRequire(pacProxyAgentPath);
HttpProxyAgent = pacRequire('http-proxy-agent').HttpProxyAgent;
HttpsProxyAgent = pacRequire('https-proxy-agent').HttpsProxyAgent;
} else {
// No such thing as require.resolve() in webpack, just need to assume
// that everything is hoisted :(
// eslint-disable-next-line @typescript-eslint/no-var-requires
HttpProxyAgent = require('http-proxy-agent').HttpProxyAgent;
// eslint-disable-next-line @typescript-eslint/no-var-requires
HttpsProxyAgent = require('https-proxy-agent').HttpsProxyAgent;
}

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

0 comments on commit 77730ab

Please sign in to comment.