diff --git a/lib/utils/request-interceptor.ts b/lib/utils/request-interceptor.ts index c85ba01cd7..5125e7b486 100644 --- a/lib/utils/request-interceptor.ts +++ b/lib/utils/request-interceptor.ts @@ -1,32 +1,19 @@ import { setupServer } from 'msw/node'; -import { http, passthrough } from 'msw'; +import { http } from 'msw'; import logger from '@/utils/logger'; import { config } from '@/config'; -import { fetch, Headers, FormData, ProxyAgent, Request, RequestInfo, RequestInit, Response } from 'undici'; +import { fetch, Headers, FormData, Request, Response } from 'undici'; import proxy from '@/utils/proxy'; -import type nodehttp from 'node:http'; - -class ExtendedRequest extends Request { - public dispatcher: ProxyAgent | undefined; - public agent: nodehttp.ClientRequestArgs['agent']; - - constructor(input: RequestInfo, init?: RequestInit & nodehttp.ClientRequestArgs) { - super(input, init); - this.dispatcher = init?.dispatcher; // fetch - this.agent = init?.agent; // http - } -} Object.defineProperties(globalThis, { fetch: { value: fetch, writable: true }, Headers: { value: Headers }, FormData: { value: FormData }, - Request: { value: ExtendedRequest }, + Request: { value: Request }, Response: { value: Response }, }); -const handler = (request: ExtendedRequest) => { - request.headers.set('debug', '1'); +const handler = (request: globalThis.Request, options: NonNullable[1]>) => { // ua if (!request.headers.get('user-agent')) { request.headers.set('user-agent', config.ua); @@ -48,7 +35,7 @@ const handler = (request: ExtendedRequest) => { } // proxy - if (!request.dispatcher && !request.agent && proxy.dispatcher) { + if (!options.dispatcher && proxy.dispatcher) { const proxyRegex = new RegExp(proxy.proxyObj.url_regex); let urlHandler; try { @@ -59,22 +46,20 @@ const handler = (request: ExtendedRequest) => { if (proxyRegex.test(request.url) && request.url.startsWith('http') && !(urlHandler && urlHandler.host === proxy.proxyUrlHandler?.host)) { // fetch - request.dispatcher = proxy.dispatcher; - - // http - request.agent = proxy.agent || undefined; - if (proxy.proxyObj.auth) { - request.headers.set('Proxy-Authorization', `Basic ${proxy.proxyObj.auth}`); - } + options.dispatcher = proxy.dispatcher; } } }; const server = setupServer( + // @ts-expect-error http.all('*', ({ request }) => { logger.debug(`Outgoing request: ${request.method} ${request.url}`); - handler(request); - return passthrough(); + const requestClone = request.clone(); + const options = {}; + handler(requestClone, options); + // @ts-expect-error + return fetch(requestClone, options); }) ); server.listen(); diff --git a/package.json b/package.json index 77124348d0..a78cdc7947 100644 --- a/package.json +++ b/package.json @@ -167,7 +167,7 @@ "js-beautify": "1.15.1", "lint-staged": "15.2.2", "mockdate": "3.0.5", - "msw": "2.2.12", + "msw": "2.2.13", "nock": "14.0.0-beta.5", "prettier": "3.2.5", "remark-parse": "11.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9756e122f5..2d1a2462e5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -353,8 +353,8 @@ devDependencies: specifier: 3.0.5 version: 3.0.5 msw: - specifier: 2.2.12 - version: 2.2.12(typescript@5.4.3) + specifier: 2.2.13 + version: 2.2.13(typescript@5.4.3) nock: specifier: 14.0.0-beta.5 version: 14.0.0-beta.5 @@ -7187,8 +7187,8 @@ packages: resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} dev: false - /msw@2.2.12(typescript@5.4.3): - resolution: {integrity: sha512-thmjEipUvr+XGawcNOh4T8+T2TN3qKWPfF0Ewx6ub1HIATXBsjOhnjV6Wx2BmmFThKEtYYC+RygrvkpG4DzoZw==} + /msw@2.2.13(typescript@5.4.3): + resolution: {integrity: sha512-ljFf1xZsU0b4zv1l7xzEmC6OZA6yD06hcx0H+dc8V0VypaP3HGYJa1rMLjQbBWl32ptGhcfwcPCWDB1wjmsftw==} engines: {node: '>=18'} hasBin: true requiresBuild: true