@mswjs/interceptors 0.41.2 → 0.41.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser/{XMLHttpRequest-BvxZV0WU.mjs → XMLHttpRequest-Bbvn_AjX.mjs} +4 -4
- package/lib/browser/XMLHttpRequest-Bbvn_AjX.mjs.map +1 -0
- package/lib/browser/{XMLHttpRequest-BACqefB-.cjs → XMLHttpRequest-C2KXXdBy.cjs} +4 -4
- package/lib/browser/XMLHttpRequest-C2KXXdBy.cjs.map +1 -0
- package/lib/browser/{fetch-U3v3Y4ap.cjs → fetch-CJYWVliE.cjs} +4 -11
- package/lib/browser/fetch-CJYWVliE.cjs.map +1 -0
- package/lib/browser/{fetch-DdKEdDOR.mjs → fetch-D4es-Apw.mjs} +4 -11
- package/lib/browser/fetch-D4es-Apw.mjs.map +1 -0
- package/lib/browser/{getRawRequest-zx8rUJL2.cjs → getRawRequest-DaxX6uMy.cjs} +83 -1
- package/lib/browser/getRawRequest-DaxX6uMy.cjs.map +1 -0
- package/lib/browser/{getRawRequest-BTaNLFr0.mjs → getRawRequest-Df640uw8.mjs} +78 -2
- package/lib/browser/getRawRequest-Df640uw8.mjs.map +1 -0
- package/lib/browser/{handleRequest-CvX2G-Lz.cjs → handleRequest--veikAod.cjs} +2 -2
- package/lib/browser/{handleRequest-CvX2G-Lz.cjs.map → handleRequest--veikAod.cjs.map} +1 -1
- package/lib/browser/{handleRequest-D7kpTI5U.mjs → handleRequest-Dq1a55b4.mjs} +2 -2
- package/lib/browser/{handleRequest-D7kpTI5U.mjs.map → handleRequest-Dq1a55b4.mjs.map} +1 -1
- package/lib/browser/index.cjs +2 -1
- package/lib/browser/index.d.cts +20 -1
- package/lib/browser/index.d.mts +20 -1
- package/lib/browser/index.mjs +2 -2
- package/lib/browser/interceptors/WebSocket/index.cjs.map +1 -1
- package/lib/browser/interceptors/WebSocket/index.d.cts +1 -1
- package/lib/browser/interceptors/WebSocket/index.d.mts +1 -1
- package/lib/browser/interceptors/WebSocket/index.mjs.map +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.cjs +3 -3
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +3 -3
- package/lib/browser/interceptors/fetch/index.cjs +3 -3
- package/lib/browser/interceptors/fetch/index.mjs +3 -3
- package/lib/browser/presets/browser.cjs +4 -4
- package/lib/browser/presets/browser.mjs +4 -4
- package/lib/node/{BatchInterceptor-3LnAnLTx.cjs → BatchInterceptor-7u9fqQIe.cjs} +2 -2
- package/lib/node/{BatchInterceptor-3LnAnLTx.cjs.map → BatchInterceptor-7u9fqQIe.cjs.map} +1 -1
- package/lib/node/{BatchInterceptor-DFaBPilf.mjs → BatchInterceptor-BxgAXyLw.mjs} +2 -2
- package/lib/node/{BatchInterceptor-DFaBPilf.mjs.map → BatchInterceptor-BxgAXyLw.mjs.map} +1 -1
- package/lib/node/{ClientRequest-2rDe54Ui.cjs → ClientRequest-CMSXChAM.cjs} +18 -4
- package/lib/node/ClientRequest-CMSXChAM.cjs.map +1 -0
- package/lib/node/{ClientRequest-Ca8Qykuv.mjs → ClientRequest-Cl3Ky7Tw.mjs} +18 -4
- package/lib/node/ClientRequest-Cl3Ky7Tw.mjs.map +1 -0
- package/lib/node/RemoteHttpInterceptor.cjs +7 -7
- package/lib/node/RemoteHttpInterceptor.cjs.map +1 -1
- package/lib/node/RemoteHttpInterceptor.mjs +7 -7
- package/lib/node/RemoteHttpInterceptor.mjs.map +1 -1
- package/lib/node/{XMLHttpRequest-C8dIZpds.mjs → XMLHttpRequest-BBbs-p9i.mjs} +4 -4
- package/lib/node/XMLHttpRequest-BBbs-p9i.mjs.map +1 -0
- package/lib/node/{XMLHttpRequest-B7kJdYYI.cjs → XMLHttpRequest-Dj3xJgke.cjs} +4 -4
- package/lib/node/XMLHttpRequest-Dj3xJgke.cjs.map +1 -0
- package/lib/node/{fetch-BmXpK10r.cjs → fetch-BoCFyaYx.cjs} +4 -11
- package/lib/node/fetch-BoCFyaYx.cjs.map +1 -0
- package/lib/node/{fetch-G1DVwDKG.mjs → fetch-DmiwAmpu.mjs} +4 -11
- package/lib/node/fetch-DmiwAmpu.mjs.map +1 -0
- package/lib/node/{fetchUtils-CoU35g3M.mjs → fetchUtils-BTrdMHGp.mjs} +78 -2
- package/lib/node/{fetchUtils-BaY5iWXw.cjs.map → fetchUtils-BTrdMHGp.mjs.map} +1 -1
- package/lib/node/{fetchUtils-BaY5iWXw.cjs → fetchUtils-DE_q7tpr.cjs} +83 -1
- package/lib/node/fetchUtils-DE_q7tpr.cjs.map +1 -0
- package/lib/node/{handleRequest-Y97UwBbF.mjs → handleRequest-CDabRyt1.mjs} +2 -2
- package/lib/node/{handleRequest-Y97UwBbF.mjs.map → handleRequest-CDabRyt1.mjs.map} +1 -1
- package/lib/node/{handleRequest-Bb7Y-XLw.cjs → handleRequest-pLIe3KKo.cjs} +2 -2
- package/lib/node/{handleRequest-Bb7Y-XLw.cjs.map → handleRequest-pLIe3KKo.cjs.map} +1 -1
- package/lib/node/index.cjs +3 -2
- package/lib/node/index.d.cts +20 -1
- package/lib/node/index.d.mts +20 -1
- package/lib/node/index.mjs +3 -3
- package/lib/node/interceptors/ClientRequest/index.cjs +3 -3
- package/lib/node/interceptors/ClientRequest/index.mjs +3 -3
- package/lib/node/interceptors/XMLHttpRequest/index.cjs +3 -3
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +3 -3
- package/lib/node/interceptors/fetch/index.cjs +3 -3
- package/lib/node/interceptors/fetch/index.mjs +3 -3
- package/lib/node/presets/node.cjs +5 -5
- package/lib/node/presets/node.mjs +5 -5
- package/package.json +1 -1
- package/src/RemoteHttpInterceptor.ts +2 -2
- package/src/index.ts +1 -1
- package/src/interceptors/ClientRequest/MockHttpSocket.ts +2 -2
- package/src/interceptors/ClientRequest/utils/recordRawHeaders.ts +18 -0
- package/src/interceptors/WebSocket/WebSocketOverride.ts +1 -1
- package/src/interceptors/XMLHttpRequest/XMLHttpRequestController.ts +2 -1
- package/src/interceptors/fetch/index.ts +2 -2
- package/src/utils/fetchUtils.test.ts +85 -0
- package/src/utils/fetchUtils.ts +139 -3
- package/lib/browser/XMLHttpRequest-BACqefB-.cjs.map +0 -1
- package/lib/browser/XMLHttpRequest-BvxZV0WU.mjs.map +0 -1
- package/lib/browser/fetch-DdKEdDOR.mjs.map +0 -1
- package/lib/browser/fetch-U3v3Y4ap.cjs.map +0 -1
- package/lib/browser/getRawRequest-BTaNLFr0.mjs.map +0 -1
- package/lib/browser/getRawRequest-zx8rUJL2.cjs.map +0 -1
- package/lib/node/ClientRequest-2rDe54Ui.cjs.map +0 -1
- package/lib/node/ClientRequest-Ca8Qykuv.mjs.map +0 -1
- package/lib/node/XMLHttpRequest-B7kJdYYI.cjs.map +0 -1
- package/lib/node/XMLHttpRequest-C8dIZpds.mjs.map +0 -1
- package/lib/node/fetch-BmXpK10r.cjs.map +0 -1
- package/lib/node/fetch-G1DVwDKG.mjs.map +0 -1
- package/lib/node/fetchUtils-CoU35g3M.mjs.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleRequest-Bb7Y-XLw.cjs","names":["InterceptorError","DeferredPromise","RequestController"],"sources":["../../src/utils/isPropertyAccessible.ts","../../src/utils/emitAsync.ts","../../src/utils/isObject.ts","../../src/utils/responseUtils.ts","../../src/utils/isNodeLikeError.ts","../../src/utils/handleRequest.ts"],"sourcesContent":["/**\n * A function that validates if property access is possible on an object\n * without throwing. It returns `true` if the property access is possible\n * and `false` otherwise.\n *\n * Environments like miniflare will throw on property access on certain objects\n * like Request and Response, for unimplemented properties.\n */\nexport function isPropertyAccessible<Obj extends Record<string, any>>(\n obj: Obj,\n key: keyof Obj\n) {\n try {\n obj[key]\n return true\n } catch {\n return false\n }\n}\n","import { Emitter, EventMap } from 'strict-event-emitter'\n\n/**\n * Emits an event on the given emitter but executes\n * the listeners sequentially. This accounts for asynchronous\n * listeners (e.g. those having \"sleep\" and handling the request).\n */\nexport async function emitAsync<\n Events extends EventMap,\n EventName extends keyof Events\n>(\n emitter: Emitter<Events>,\n eventName: EventName,\n ...data: Events[EventName]\n): Promise<void> {\n const listeners = emitter.listeners(eventName)\n\n if (listeners.length === 0) {\n return\n }\n\n for (const listener of listeners) {\n await listener.apply(emitter, data)\n }\n}\n","/**\n * Determines if a given value is an instance of object.\n */\nexport function isObject<T>(value: any, loose = false): value is T {\n return loose\n ? Object.prototype.toString.call(value).startsWith('[object ')\n : Object.prototype.toString.call(value) === '[object Object]'\n}\n","import { isObject } from './isObject'\nimport { isPropertyAccessible } from './isPropertyAccessible'\n\n/**\n * Creates a generic 500 Unhandled Exception response.\n */\nexport function createServerErrorResponse(body: unknown): Response {\n return new Response(\n JSON.stringify(\n body instanceof Error\n ? {\n name: body.name,\n message: body.message,\n stack: body.stack,\n }\n : body\n ),\n {\n status: 500,\n statusText: 'Unhandled Exception',\n headers: {\n 'Content-Type': 'application/json',\n },\n }\n )\n}\n\nexport type ResponseError = Response & { type: 'error' }\n\n/**\n * Check if the given response is a `Response.error()`.\n *\n * @note Some environments, like Miniflare (Cloudflare) do not\n * implement the \"Response.type\" property and throw on its access.\n * Safely check if we can access \"type\" on \"Response\" before continuing.\n * @see https://github.com/mswjs/msw/issues/1834\n */\nexport function isResponseError(response: unknown): response is ResponseError {\n return (\n response != null &&\n response instanceof Response &&\n isPropertyAccessible(response, 'type') &&\n response.type === 'error'\n )\n}\n\n/**\n * Check if the given value is a `Response` or a Response-like object.\n * This is different from `value instanceof Response` because it supports\n * custom `Response` constructors, like the one when using Undici directly.\n */\nexport function isResponseLike(value: unknown): value is Response {\n return (\n isObject<Record<string, any>>(value, true) &&\n isPropertyAccessible(value, 'status') &&\n isPropertyAccessible(value, 'statusText') &&\n isPropertyAccessible(value, 'bodyUsed')\n )\n}\n","export function isNodeLikeError(\n error: unknown\n): error is NodeJS.ErrnoException {\n if (error == null) {\n return false\n }\n\n if (!(error instanceof Error)) {\n return false\n }\n\n return 'code' in error && 'errno' in error\n}\n","import type { Emitter } from 'strict-event-emitter'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { until } from '@open-draft/until'\nimport type { HttpRequestEventMap } from '../glossary'\nimport { emitAsync } from './emitAsync'\nimport { RequestController } from '../RequestController'\nimport {\n createServerErrorResponse,\n isResponseError,\n isResponseLike,\n} from './responseUtils'\nimport { InterceptorError } from '../InterceptorError'\nimport { isNodeLikeError } from './isNodeLikeError'\nimport { isObject } from './isObject'\n\ninterface HandleRequestOptions {\n requestId: string\n request: Request\n emitter: Emitter<HttpRequestEventMap>\n controller: RequestController\n}\n\nexport async function handleRequest(\n options: HandleRequestOptions\n): Promise<void> {\n const handleResponse = async (\n response: Response | Error | Record<string, any>\n ) => {\n if (response instanceof Error) {\n await options.controller.errorWith(response)\n return true\n }\n\n // Handle \"Response.error()\" instances.\n if (isResponseError(response)) {\n await options.controller.respondWith(response)\n return true\n }\n\n /**\n * Handle normal responses or response-like objects.\n * @note This must come before the arbitrary object check\n * since Response instances are, in fact, objects.\n */\n if (isResponseLike(response)) {\n await options.controller.respondWith(response)\n return true\n }\n\n // Handle arbitrary objects provided to `.errorWith(reason)`.\n if (isObject(response)) {\n await options.controller.errorWith(response)\n return true\n }\n\n return false\n }\n\n const handleResponseError = async (error: unknown): Promise<boolean> => {\n // Forward the special interceptor error instances\n // to the developer. These must not be handled in any way.\n if (error instanceof InterceptorError) {\n throw result.error\n }\n\n // Support mocking Node.js-like errors.\n if (isNodeLikeError(error)) {\n await options.controller.errorWith(error)\n return true\n }\n\n // Handle thrown responses.\n if (error instanceof Response) {\n return await handleResponse(error)\n }\n\n return false\n }\n\n // Add the last \"request\" listener to check if the request\n // has been handled in any way. If it hasn't, resolve the\n // response promise with undefined.\n // options.emitter.once('request', async ({ requestId: pendingRequestId }) => {\n // if (\n // pendingRequestId === options.requestId &&\n // options.controller.readyState === RequestController.PENDING\n // ) {\n // await options.controller.passthrough()\n // }\n // })\n\n const requestAbortPromise = new DeferredPromise<void, unknown>()\n\n /**\n * @note `signal` is not always defined in React Native.\n */\n if (options.request.signal) {\n if (options.request.signal.aborted) {\n await options.controller.errorWith(options.request.signal.reason)\n return\n }\n\n options.request.signal.addEventListener(\n 'abort',\n () => {\n requestAbortPromise.reject(options.request.signal.reason)\n },\n { once: true }\n )\n }\n\n const result = await until(async () => {\n // Emit the \"request\" event and wait until all the listeners\n // for that event are finished (e.g. async listeners awaited).\n // By the end of this promise, the developer cannot affect the\n // request anymore.\n const requestListenersPromise = emitAsync(options.emitter, 'request', {\n requestId: options.requestId,\n request: options.request,\n controller: options.controller,\n })\n\n await Promise.race([\n // Short-circuit the request handling promise if the request gets aborted.\n requestAbortPromise,\n requestListenersPromise,\n options.controller.handled,\n ])\n })\n\n // Handle the request being aborted while waiting for the request listeners.\n if (requestAbortPromise.state === 'rejected') {\n await options.controller.errorWith(requestAbortPromise.rejectionReason)\n return\n }\n\n if (result.error) {\n // Handle the error during the request listener execution.\n // These can be thrown responses or request errors.\n if (await handleResponseError(result.error)) {\n return\n }\n\n // If the developer has added \"unhandledException\" listeners,\n // allow them to handle the error. They can translate it to a\n // mocked response, network error, or forward it as-is.\n if (options.emitter.listenerCount('unhandledException') > 0) {\n // Create a new request controller just for the unhandled exception case.\n // This is needed because the original controller might have been already\n // interacted with (e.g. \"respondWith\" or \"errorWith\" called on it).\n const unhandledExceptionController = new RequestController(\n options.request,\n {\n /**\n * @note Intentionally empty passthrough handle.\n * This controller is created within another controller and we only need\n * to know if `unhandledException` listeners handled the request.\n */\n passthrough() {},\n async respondWith(response) {\n await handleResponse(response)\n },\n async errorWith(reason) {\n /**\n * @note Handle the result of the unhandled controller\n * in the same way as the original request controller.\n * The exception here is that thrown errors within the\n * \"unhandledException\" event do NOT result in another\n * emit of the same event. They are forwarded as-is.\n */\n await options.controller.errorWith(reason)\n },\n }\n )\n\n await emitAsync(options.emitter, 'unhandledException', {\n error: result.error,\n request: options.request,\n requestId: options.requestId,\n controller: unhandledExceptionController,\n })\n\n // If all the \"unhandledException\" listeners have finished\n // but have not handled the request in any way, passthrough.\n if (\n unhandledExceptionController.readyState !== RequestController.PENDING\n ) {\n return\n }\n }\n\n // Otherwise, coerce unhandled exceptions to a 500 Internal Server Error response.\n await options.controller.respondWith(\n createServerErrorResponse(result.error)\n )\n return\n }\n\n // If the request hasn't been handled by this point, passthrough.\n if (options.controller.readyState === RequestController.PENDING) {\n return await options.controller.passthrough()\n }\n\n return options.controller.handled\n}\n"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"handleRequest-pLIe3KKo.cjs","names":["InterceptorError","DeferredPromise","RequestController"],"sources":["../../src/utils/isPropertyAccessible.ts","../../src/utils/emitAsync.ts","../../src/utils/isObject.ts","../../src/utils/responseUtils.ts","../../src/utils/isNodeLikeError.ts","../../src/utils/handleRequest.ts"],"sourcesContent":["/**\n * A function that validates if property access is possible on an object\n * without throwing. It returns `true` if the property access is possible\n * and `false` otherwise.\n *\n * Environments like miniflare will throw on property access on certain objects\n * like Request and Response, for unimplemented properties.\n */\nexport function isPropertyAccessible<Obj extends Record<string, any>>(\n obj: Obj,\n key: keyof Obj\n) {\n try {\n obj[key]\n return true\n } catch {\n return false\n }\n}\n","import { Emitter, EventMap } from 'strict-event-emitter'\n\n/**\n * Emits an event on the given emitter but executes\n * the listeners sequentially. This accounts for asynchronous\n * listeners (e.g. those having \"sleep\" and handling the request).\n */\nexport async function emitAsync<\n Events extends EventMap,\n EventName extends keyof Events\n>(\n emitter: Emitter<Events>,\n eventName: EventName,\n ...data: Events[EventName]\n): Promise<void> {\n const listeners = emitter.listeners(eventName)\n\n if (listeners.length === 0) {\n return\n }\n\n for (const listener of listeners) {\n await listener.apply(emitter, data)\n }\n}\n","/**\n * Determines if a given value is an instance of object.\n */\nexport function isObject<T>(value: any, loose = false): value is T {\n return loose\n ? Object.prototype.toString.call(value).startsWith('[object ')\n : Object.prototype.toString.call(value) === '[object Object]'\n}\n","import { isObject } from './isObject'\nimport { isPropertyAccessible } from './isPropertyAccessible'\n\n/**\n * Creates a generic 500 Unhandled Exception response.\n */\nexport function createServerErrorResponse(body: unknown): Response {\n return new Response(\n JSON.stringify(\n body instanceof Error\n ? {\n name: body.name,\n message: body.message,\n stack: body.stack,\n }\n : body\n ),\n {\n status: 500,\n statusText: 'Unhandled Exception',\n headers: {\n 'Content-Type': 'application/json',\n },\n }\n )\n}\n\nexport type ResponseError = Response & { type: 'error' }\n\n/**\n * Check if the given response is a `Response.error()`.\n *\n * @note Some environments, like Miniflare (Cloudflare) do not\n * implement the \"Response.type\" property and throw on its access.\n * Safely check if we can access \"type\" on \"Response\" before continuing.\n * @see https://github.com/mswjs/msw/issues/1834\n */\nexport function isResponseError(response: unknown): response is ResponseError {\n return (\n response != null &&\n response instanceof Response &&\n isPropertyAccessible(response, 'type') &&\n response.type === 'error'\n )\n}\n\n/**\n * Check if the given value is a `Response` or a Response-like object.\n * This is different from `value instanceof Response` because it supports\n * custom `Response` constructors, like the one when using Undici directly.\n */\nexport function isResponseLike(value: unknown): value is Response {\n return (\n isObject<Record<string, any>>(value, true) &&\n isPropertyAccessible(value, 'status') &&\n isPropertyAccessible(value, 'statusText') &&\n isPropertyAccessible(value, 'bodyUsed')\n )\n}\n","export function isNodeLikeError(\n error: unknown\n): error is NodeJS.ErrnoException {\n if (error == null) {\n return false\n }\n\n if (!(error instanceof Error)) {\n return false\n }\n\n return 'code' in error && 'errno' in error\n}\n","import type { Emitter } from 'strict-event-emitter'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { until } from '@open-draft/until'\nimport type { HttpRequestEventMap } from '../glossary'\nimport { emitAsync } from './emitAsync'\nimport { RequestController } from '../RequestController'\nimport {\n createServerErrorResponse,\n isResponseError,\n isResponseLike,\n} from './responseUtils'\nimport { InterceptorError } from '../InterceptorError'\nimport { isNodeLikeError } from './isNodeLikeError'\nimport { isObject } from './isObject'\n\ninterface HandleRequestOptions {\n requestId: string\n request: Request\n emitter: Emitter<HttpRequestEventMap>\n controller: RequestController\n}\n\nexport async function handleRequest(\n options: HandleRequestOptions\n): Promise<void> {\n const handleResponse = async (\n response: Response | Error | Record<string, any>\n ) => {\n if (response instanceof Error) {\n await options.controller.errorWith(response)\n return true\n }\n\n // Handle \"Response.error()\" instances.\n if (isResponseError(response)) {\n await options.controller.respondWith(response)\n return true\n }\n\n /**\n * Handle normal responses or response-like objects.\n * @note This must come before the arbitrary object check\n * since Response instances are, in fact, objects.\n */\n if (isResponseLike(response)) {\n await options.controller.respondWith(response)\n return true\n }\n\n // Handle arbitrary objects provided to `.errorWith(reason)`.\n if (isObject(response)) {\n await options.controller.errorWith(response)\n return true\n }\n\n return false\n }\n\n const handleResponseError = async (error: unknown): Promise<boolean> => {\n // Forward the special interceptor error instances\n // to the developer. These must not be handled in any way.\n if (error instanceof InterceptorError) {\n throw result.error\n }\n\n // Support mocking Node.js-like errors.\n if (isNodeLikeError(error)) {\n await options.controller.errorWith(error)\n return true\n }\n\n // Handle thrown responses.\n if (error instanceof Response) {\n return await handleResponse(error)\n }\n\n return false\n }\n\n // Add the last \"request\" listener to check if the request\n // has been handled in any way. If it hasn't, resolve the\n // response promise with undefined.\n // options.emitter.once('request', async ({ requestId: pendingRequestId }) => {\n // if (\n // pendingRequestId === options.requestId &&\n // options.controller.readyState === RequestController.PENDING\n // ) {\n // await options.controller.passthrough()\n // }\n // })\n\n const requestAbortPromise = new DeferredPromise<void, unknown>()\n\n /**\n * @note `signal` is not always defined in React Native.\n */\n if (options.request.signal) {\n if (options.request.signal.aborted) {\n await options.controller.errorWith(options.request.signal.reason)\n return\n }\n\n options.request.signal.addEventListener(\n 'abort',\n () => {\n requestAbortPromise.reject(options.request.signal.reason)\n },\n { once: true }\n )\n }\n\n const result = await until(async () => {\n // Emit the \"request\" event and wait until all the listeners\n // for that event are finished (e.g. async listeners awaited).\n // By the end of this promise, the developer cannot affect the\n // request anymore.\n const requestListenersPromise = emitAsync(options.emitter, 'request', {\n requestId: options.requestId,\n request: options.request,\n controller: options.controller,\n })\n\n await Promise.race([\n // Short-circuit the request handling promise if the request gets aborted.\n requestAbortPromise,\n requestListenersPromise,\n options.controller.handled,\n ])\n })\n\n // Handle the request being aborted while waiting for the request listeners.\n if (requestAbortPromise.state === 'rejected') {\n await options.controller.errorWith(requestAbortPromise.rejectionReason)\n return\n }\n\n if (result.error) {\n // Handle the error during the request listener execution.\n // These can be thrown responses or request errors.\n if (await handleResponseError(result.error)) {\n return\n }\n\n // If the developer has added \"unhandledException\" listeners,\n // allow them to handle the error. They can translate it to a\n // mocked response, network error, or forward it as-is.\n if (options.emitter.listenerCount('unhandledException') > 0) {\n // Create a new request controller just for the unhandled exception case.\n // This is needed because the original controller might have been already\n // interacted with (e.g. \"respondWith\" or \"errorWith\" called on it).\n const unhandledExceptionController = new RequestController(\n options.request,\n {\n /**\n * @note Intentionally empty passthrough handle.\n * This controller is created within another controller and we only need\n * to know if `unhandledException` listeners handled the request.\n */\n passthrough() {},\n async respondWith(response) {\n await handleResponse(response)\n },\n async errorWith(reason) {\n /**\n * @note Handle the result of the unhandled controller\n * in the same way as the original request controller.\n * The exception here is that thrown errors within the\n * \"unhandledException\" event do NOT result in another\n * emit of the same event. They are forwarded as-is.\n */\n await options.controller.errorWith(reason)\n },\n }\n )\n\n await emitAsync(options.emitter, 'unhandledException', {\n error: result.error,\n request: options.request,\n requestId: options.requestId,\n controller: unhandledExceptionController,\n })\n\n // If all the \"unhandledException\" listeners have finished\n // but have not handled the request in any way, passthrough.\n if (\n unhandledExceptionController.readyState !== RequestController.PENDING\n ) {\n return\n }\n }\n\n // Otherwise, coerce unhandled exceptions to a 500 Internal Server Error response.\n await options.controller.respondWith(\n createServerErrorResponse(result.error)\n )\n return\n }\n\n // If the request hasn't been handled by this point, passthrough.\n if (options.controller.readyState === RequestController.PENDING) {\n return await options.controller.passthrough()\n }\n\n return options.controller.handled\n}\n"],"mappings":"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"}
|
package/lib/node/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const require_glossary = require('./glossary-BLKRyLBd.cjs');
|
|
2
|
-
const require_fetchUtils = require('./fetchUtils-
|
|
3
|
-
const require_BatchInterceptor = require('./BatchInterceptor-
|
|
2
|
+
const require_fetchUtils = require('./fetchUtils-DE_q7tpr.cjs');
|
|
3
|
+
const require_BatchInterceptor = require('./BatchInterceptor-7u9fqQIe.cjs');
|
|
4
4
|
const require_bufferUtils = require('./bufferUtils-DiCTqG-7.cjs');
|
|
5
5
|
const require_getRawRequest = require('./getRawRequest-BavnMWh_.cjs');
|
|
6
6
|
|
|
@@ -37,6 +37,7 @@ function resolveWebSocketUrl(url) {
|
|
|
37
37
|
|
|
38
38
|
//#endregion
|
|
39
39
|
exports.BatchInterceptor = require_BatchInterceptor.BatchInterceptor;
|
|
40
|
+
exports.FetchRequest = require_fetchUtils.FetchRequest;
|
|
40
41
|
exports.FetchResponse = require_fetchUtils.FetchResponse;
|
|
41
42
|
exports.INTERNAL_REQUEST_ID_HEADER_NAME = require_fetchUtils.INTERNAL_REQUEST_ID_HEADER_NAME;
|
|
42
43
|
exports.IS_PATCHED_MODULE = require_glossary.IS_PATCHED_MODULE;
|
package/lib/node/index.d.cts
CHANGED
|
@@ -22,6 +22,25 @@ declare function encodeBuffer(text: string): Uint8Array;
|
|
|
22
22
|
declare function decodeBuffer(buffer: ArrayBuffer, encoding?: string): string;
|
|
23
23
|
//#endregion
|
|
24
24
|
//#region src/utils/fetchUtils.d.ts
|
|
25
|
+
interface FetchRequestInit extends Omit<RequestInit, 'mode'> {
|
|
26
|
+
mode?: RequestMode | 'websocket' | 'webtransport';
|
|
27
|
+
duplex?: 'half' | 'full';
|
|
28
|
+
}
|
|
29
|
+
declare class FetchRequest extends Request {
|
|
30
|
+
#private;
|
|
31
|
+
/**
|
|
32
|
+
* Check if the given request method is configurable.
|
|
33
|
+
* @see https://fetch.spec.whatwg.org/#methods
|
|
34
|
+
*/
|
|
35
|
+
static isConfigurableMethod(method: string): boolean;
|
|
36
|
+
static isMethodWithBody(method: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Check if the given request `mode` is configurable.
|
|
39
|
+
* @see https://fetch.spec.whatwg.org/#concept-request-mode
|
|
40
|
+
*/
|
|
41
|
+
static isConfigurableMode(mode: string): boolean;
|
|
42
|
+
constructor(input: URL | RequestInfo, init?: FetchRequestInit);
|
|
43
|
+
}
|
|
25
44
|
interface FetchResponseInit extends ResponseInit {
|
|
26
45
|
url?: string;
|
|
27
46
|
}
|
|
@@ -71,5 +90,5 @@ declare function getRawRequest(request: Request): unknown | undefined;
|
|
|
71
90
|
*/
|
|
72
91
|
declare function resolveWebSocketUrl(url: string | URL): string;
|
|
73
92
|
//#endregion
|
|
74
|
-
export { BatchInterceptor, BatchInterceptorOptions, ExtractEventMapType, ExtractEventNames, FetchResponse, HttpRequestEventMap, INTERNAL_REQUEST_ID_HEADER_NAME, IS_PATCHED_MODULE, Interceptor, InterceptorEventMap, InterceptorReadyState, InterceptorSubscription, RequestController, type RequestControllerSource, RequestCredentials, createRequestId, decodeBuffer, deleteGlobalSymbol, encodeBuffer, getCleanUrl, getGlobalSymbol, getRawRequest, resolveWebSocketUrl };
|
|
93
|
+
export { BatchInterceptor, BatchInterceptorOptions, ExtractEventMapType, ExtractEventNames, FetchRequest, FetchResponse, HttpRequestEventMap, INTERNAL_REQUEST_ID_HEADER_NAME, IS_PATCHED_MODULE, Interceptor, InterceptorEventMap, InterceptorReadyState, InterceptorSubscription, RequestController, type RequestControllerSource, RequestCredentials, createRequestId, decodeBuffer, deleteGlobalSymbol, encodeBuffer, getCleanUrl, getGlobalSymbol, getRawRequest, resolveWebSocketUrl };
|
|
75
94
|
//# sourceMappingURL=index.d.cts.map
|
package/lib/node/index.d.mts
CHANGED
|
@@ -22,6 +22,25 @@ declare function encodeBuffer(text: string): Uint8Array;
|
|
|
22
22
|
declare function decodeBuffer(buffer: ArrayBuffer, encoding?: string): string;
|
|
23
23
|
//#endregion
|
|
24
24
|
//#region src/utils/fetchUtils.d.ts
|
|
25
|
+
interface FetchRequestInit extends Omit<RequestInit, 'mode'> {
|
|
26
|
+
mode?: RequestMode | 'websocket' | 'webtransport';
|
|
27
|
+
duplex?: 'half' | 'full';
|
|
28
|
+
}
|
|
29
|
+
declare class FetchRequest extends Request {
|
|
30
|
+
#private;
|
|
31
|
+
/**
|
|
32
|
+
* Check if the given request method is configurable.
|
|
33
|
+
* @see https://fetch.spec.whatwg.org/#methods
|
|
34
|
+
*/
|
|
35
|
+
static isConfigurableMethod(method: string): boolean;
|
|
36
|
+
static isMethodWithBody(method: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Check if the given request `mode` is configurable.
|
|
39
|
+
* @see https://fetch.spec.whatwg.org/#concept-request-mode
|
|
40
|
+
*/
|
|
41
|
+
static isConfigurableMode(mode: string): boolean;
|
|
42
|
+
constructor(input: URL | RequestInfo, init?: FetchRequestInit);
|
|
43
|
+
}
|
|
25
44
|
interface FetchResponseInit extends ResponseInit {
|
|
26
45
|
url?: string;
|
|
27
46
|
}
|
|
@@ -71,5 +90,5 @@ declare function getRawRequest(request: Request): unknown | undefined;
|
|
|
71
90
|
*/
|
|
72
91
|
declare function resolveWebSocketUrl(url: string | URL): string;
|
|
73
92
|
//#endregion
|
|
74
|
-
export { BatchInterceptor, BatchInterceptorOptions, ExtractEventMapType, ExtractEventNames, FetchResponse, HttpRequestEventMap, INTERNAL_REQUEST_ID_HEADER_NAME, IS_PATCHED_MODULE, Interceptor, InterceptorEventMap, InterceptorReadyState, InterceptorSubscription, RequestController, type RequestControllerSource, RequestCredentials, createRequestId, decodeBuffer, deleteGlobalSymbol, encodeBuffer, getCleanUrl, getGlobalSymbol, getRawRequest, resolveWebSocketUrl };
|
|
93
|
+
export { BatchInterceptor, BatchInterceptorOptions, ExtractEventMapType, ExtractEventNames, FetchRequest, FetchResponse, HttpRequestEventMap, INTERNAL_REQUEST_ID_HEADER_NAME, IS_PATCHED_MODULE, Interceptor, InterceptorEventMap, InterceptorReadyState, InterceptorSubscription, RequestController, type RequestControllerSource, RequestCredentials, createRequestId, decodeBuffer, deleteGlobalSymbol, encodeBuffer, getCleanUrl, getGlobalSymbol, getRawRequest, resolveWebSocketUrl };
|
|
75
94
|
//# sourceMappingURL=index.d.mts.map
|
package/lib/node/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { t as IS_PATCHED_MODULE } from "./glossary-glQBRnVD.mjs";
|
|
2
|
-
import { c as
|
|
3
|
-
import { t as BatchInterceptor } from "./BatchInterceptor-
|
|
2
|
+
import { a as RequestController, c as Interceptor, d as getGlobalSymbol, i as createRequestId, l as InterceptorReadyState, n as FetchResponse, s as INTERNAL_REQUEST_ID_HEADER_NAME, t as FetchRequest, u as deleteGlobalSymbol } from "./fetchUtils-BTrdMHGp.mjs";
|
|
3
|
+
import { t as BatchInterceptor } from "./BatchInterceptor-BxgAXyLw.mjs";
|
|
4
4
|
import { n as encodeBuffer, t as decodeBuffer } from "./bufferUtils-_8XfKIfX.mjs";
|
|
5
5
|
import { t as getRawRequest } from "./getRawRequest-DnwmXyOW.mjs";
|
|
6
6
|
|
|
@@ -36,5 +36,5 @@ function resolveWebSocketUrl(url) {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
//#endregion
|
|
39
|
-
export { BatchInterceptor, FetchResponse, INTERNAL_REQUEST_ID_HEADER_NAME, IS_PATCHED_MODULE, Interceptor, InterceptorReadyState, RequestController, createRequestId, decodeBuffer, deleteGlobalSymbol, encodeBuffer, getCleanUrl, getGlobalSymbol, getRawRequest, resolveWebSocketUrl };
|
|
39
|
+
export { BatchInterceptor, FetchRequest, FetchResponse, INTERNAL_REQUEST_ID_HEADER_NAME, IS_PATCHED_MODULE, Interceptor, InterceptorReadyState, RequestController, createRequestId, decodeBuffer, deleteGlobalSymbol, encodeBuffer, getCleanUrl, getGlobalSymbol, getRawRequest, resolveWebSocketUrl };
|
|
40
40
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
require('../../fetchUtils-
|
|
2
|
-
const require_ClientRequest = require('../../ClientRequest-
|
|
3
|
-
require('../../handleRequest-
|
|
1
|
+
require('../../fetchUtils-DE_q7tpr.cjs');
|
|
2
|
+
const require_ClientRequest = require('../../ClientRequest-CMSXChAM.cjs');
|
|
3
|
+
require('../../handleRequest-pLIe3KKo.cjs');
|
|
4
4
|
require('../../node-dKdAf3tC.cjs');
|
|
5
5
|
|
|
6
6
|
exports.ClientRequestInterceptor = require_ClientRequest.ClientRequestInterceptor;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import "../../fetchUtils-
|
|
2
|
-
import { t as ClientRequestInterceptor } from "../../ClientRequest-
|
|
3
|
-
import "../../handleRequest-
|
|
1
|
+
import "../../fetchUtils-BTrdMHGp.mjs";
|
|
2
|
+
import { t as ClientRequestInterceptor } from "../../ClientRequest-Cl3Ky7Tw.mjs";
|
|
3
|
+
import "../../handleRequest-CDabRyt1.mjs";
|
|
4
4
|
import "../../node-DwCc6iuP.mjs";
|
|
5
5
|
|
|
6
6
|
export { ClientRequestInterceptor };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
require('../../fetchUtils-
|
|
1
|
+
require('../../fetchUtils-DE_q7tpr.cjs');
|
|
2
2
|
require('../../bufferUtils-DiCTqG-7.cjs');
|
|
3
|
-
require('../../handleRequest-
|
|
4
|
-
const require_XMLHttpRequest = require('../../XMLHttpRequest-
|
|
3
|
+
require('../../handleRequest-pLIe3KKo.cjs');
|
|
4
|
+
const require_XMLHttpRequest = require('../../XMLHttpRequest-Dj3xJgke.cjs');
|
|
5
5
|
|
|
6
6
|
exports.XMLHttpRequestInterceptor = require_XMLHttpRequest.XMLHttpRequestInterceptor;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import "../../fetchUtils-
|
|
1
|
+
import "../../fetchUtils-BTrdMHGp.mjs";
|
|
2
2
|
import "../../bufferUtils-_8XfKIfX.mjs";
|
|
3
|
-
import "../../handleRequest-
|
|
4
|
-
import { t as XMLHttpRequestInterceptor } from "../../XMLHttpRequest-
|
|
3
|
+
import "../../handleRequest-CDabRyt1.mjs";
|
|
4
|
+
import { t as XMLHttpRequestInterceptor } from "../../XMLHttpRequest-BBbs-p9i.mjs";
|
|
5
5
|
|
|
6
6
|
export { XMLHttpRequestInterceptor };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
require('../../fetchUtils-
|
|
2
|
-
require('../../handleRequest-
|
|
3
|
-
const require_fetch = require('../../fetch-
|
|
1
|
+
require('../../fetchUtils-DE_q7tpr.cjs');
|
|
2
|
+
require('../../handleRequest-pLIe3KKo.cjs');
|
|
3
|
+
const require_fetch = require('../../fetch-BoCFyaYx.cjs');
|
|
4
4
|
|
|
5
5
|
exports.FetchInterceptor = require_fetch.FetchInterceptor;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import "../../fetchUtils-
|
|
2
|
-
import "../../handleRequest-
|
|
3
|
-
import { t as FetchInterceptor } from "../../fetch-
|
|
1
|
+
import "../../fetchUtils-BTrdMHGp.mjs";
|
|
2
|
+
import "../../handleRequest-CDabRyt1.mjs";
|
|
3
|
+
import { t as FetchInterceptor } from "../../fetch-DmiwAmpu.mjs";
|
|
4
4
|
|
|
5
5
|
export { FetchInterceptor };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
require('../fetchUtils-
|
|
1
|
+
require('../fetchUtils-DE_q7tpr.cjs');
|
|
2
2
|
require('../bufferUtils-DiCTqG-7.cjs');
|
|
3
|
-
const require_ClientRequest = require('../ClientRequest-
|
|
4
|
-
require('../handleRequest-
|
|
3
|
+
const require_ClientRequest = require('../ClientRequest-CMSXChAM.cjs');
|
|
4
|
+
require('../handleRequest-pLIe3KKo.cjs');
|
|
5
5
|
require('../node-dKdAf3tC.cjs');
|
|
6
|
-
const require_XMLHttpRequest = require('../XMLHttpRequest-
|
|
7
|
-
const require_fetch = require('../fetch-
|
|
6
|
+
const require_XMLHttpRequest = require('../XMLHttpRequest-Dj3xJgke.cjs');
|
|
7
|
+
const require_fetch = require('../fetch-BoCFyaYx.cjs');
|
|
8
8
|
|
|
9
9
|
//#region src/presets/node.ts
|
|
10
10
|
/**
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import "../fetchUtils-
|
|
1
|
+
import "../fetchUtils-BTrdMHGp.mjs";
|
|
2
2
|
import "../bufferUtils-_8XfKIfX.mjs";
|
|
3
|
-
import { t as ClientRequestInterceptor } from "../ClientRequest-
|
|
4
|
-
import "../handleRequest-
|
|
3
|
+
import { t as ClientRequestInterceptor } from "../ClientRequest-Cl3Ky7Tw.mjs";
|
|
4
|
+
import "../handleRequest-CDabRyt1.mjs";
|
|
5
5
|
import "../node-DwCc6iuP.mjs";
|
|
6
|
-
import { t as XMLHttpRequestInterceptor } from "../XMLHttpRequest-
|
|
7
|
-
import { t as FetchInterceptor } from "../fetch-
|
|
6
|
+
import { t as XMLHttpRequestInterceptor } from "../XMLHttpRequest-BBbs-p9i.mjs";
|
|
7
|
+
import { t as FetchInterceptor } from "../fetch-DmiwAmpu.mjs";
|
|
8
8
|
|
|
9
9
|
//#region src/presets/node.ts
|
|
10
10
|
/**
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"type": "commonjs",
|
|
3
3
|
"name": "@mswjs/interceptors",
|
|
4
4
|
"description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.",
|
|
5
|
-
"version": "0.41.
|
|
5
|
+
"version": "0.41.4",
|
|
6
6
|
"main": "./lib/node/index.cjs",
|
|
7
7
|
"module": "./lib/node/index.mjs",
|
|
8
8
|
"types": "./lib/node/index.d.cts",
|
|
@@ -7,7 +7,7 @@ import { XMLHttpRequestInterceptor } from './interceptors/XMLHttpRequest'
|
|
|
7
7
|
import { FetchInterceptor } from './interceptors/fetch'
|
|
8
8
|
import { handleRequest } from './utils/handleRequest'
|
|
9
9
|
import { RequestController } from './RequestController'
|
|
10
|
-
import { FetchResponse } from './utils/fetchUtils'
|
|
10
|
+
import { FetchRequest, FetchResponse } from './utils/fetchUtils'
|
|
11
11
|
import { isResponseError } from './utils/responseUtils'
|
|
12
12
|
|
|
13
13
|
export interface SerializedRequest {
|
|
@@ -172,7 +172,7 @@ export class RemoteHttpResolver extends Interceptor<HttpRequestEventMap> {
|
|
|
172
172
|
|
|
173
173
|
logger.info('parsed intercepted request', requestJson)
|
|
174
174
|
|
|
175
|
-
const request = new
|
|
175
|
+
const request = new FetchRequest(requestJson.url, {
|
|
176
176
|
method: requestJson.method,
|
|
177
177
|
headers: new Headers(requestJson.headers),
|
|
178
178
|
credentials: requestJson.credentials,
|
package/src/index.ts
CHANGED
|
@@ -10,6 +10,6 @@ export {
|
|
|
10
10
|
export { createRequestId } from './createRequestId'
|
|
11
11
|
export { getCleanUrl } from './utils/getCleanUrl'
|
|
12
12
|
export { encodeBuffer, decodeBuffer } from './utils/bufferUtils'
|
|
13
|
-
export { FetchResponse } from './utils/fetchUtils'
|
|
13
|
+
export { FetchRequest, FetchResponse } from './utils/fetchUtils'
|
|
14
14
|
export { getRawRequest } from './getRawRequest'
|
|
15
15
|
export { resolveWebSocketUrl } from './utils/resolveWebSocketUrl'
|
|
@@ -15,7 +15,7 @@ import { isPropertyAccessible } from '../../utils/isPropertyAccessible'
|
|
|
15
15
|
import { baseUrlFromConnectionOptions } from '../Socket/utils/baseUrlFromConnectionOptions'
|
|
16
16
|
import { createRequestId } from '../../createRequestId'
|
|
17
17
|
import { getRawFetchHeaders } from './utils/recordRawHeaders'
|
|
18
|
-
import { FetchResponse } from '../../utils/fetchUtils'
|
|
18
|
+
import { FetchRequest, FetchResponse } from '../../utils/fetchUtils'
|
|
19
19
|
import { setRawRequest } from '../../getRawRequest'
|
|
20
20
|
import { setRawRequestBodyStream } from '../../utils/node'
|
|
21
21
|
import { freeParser } from './utils/parserUtils'
|
|
@@ -576,7 +576,7 @@ export class MockHttpSocket extends MockSocket {
|
|
|
576
576
|
})
|
|
577
577
|
|
|
578
578
|
const requestId = createRequestId()
|
|
579
|
-
this.request = new
|
|
579
|
+
this.request = new FetchRequest(url, {
|
|
580
580
|
method,
|
|
581
581
|
headers,
|
|
582
582
|
credentials: 'same-origin',
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { FetchRequest, FetchResponse } from '../../../utils/fetchUtils'
|
|
2
|
+
|
|
1
3
|
type HeaderTuple = [string, string]
|
|
2
4
|
type RawHeaders = Array<HeaderTuple>
|
|
3
5
|
type SetHeaderBehavior = 'set' | 'append'
|
|
@@ -92,6 +94,11 @@ export function recordRawFetchHeaders() {
|
|
|
92
94
|
globalThis.Request = OriginalRequest
|
|
93
95
|
globalThis.Response = OriginalResponse
|
|
94
96
|
|
|
97
|
+
Object.setPrototypeOf(FetchRequest, OriginalRequest)
|
|
98
|
+
Object.setPrototypeOf(FetchRequest.prototype, OriginalRequest.prototype)
|
|
99
|
+
Object.setPrototypeOf(FetchResponse, OriginalResponse)
|
|
100
|
+
Object.setPrototypeOf(FetchResponse.prototype, OriginalResponse.prototype)
|
|
101
|
+
|
|
95
102
|
Reflect.deleteProperty(Headers, kRestorePatches)
|
|
96
103
|
},
|
|
97
104
|
enumerable: false,
|
|
@@ -222,6 +229,17 @@ export function recordRawFetchHeaders() {
|
|
|
222
229
|
},
|
|
223
230
|
}),
|
|
224
231
|
})
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Re-parent FetchRequest/FetchResponse so their `super()` calls go
|
|
235
|
+
* through the proxied globalThis.Request/Response above. Without this,
|
|
236
|
+
* FetchRequest extends the statically-captured (original) Request,
|
|
237
|
+
* bypassing the construct proxy that records raw headers.
|
|
238
|
+
*/
|
|
239
|
+
Object.setPrototypeOf(FetchRequest, globalThis.Request)
|
|
240
|
+
Object.setPrototypeOf(FetchRequest.prototype, globalThis.Request.prototype)
|
|
241
|
+
Object.setPrototypeOf(FetchResponse, globalThis.Response)
|
|
242
|
+
Object.setPrototypeOf(FetchResponse.prototype, globalThis.Response.prototype)
|
|
225
243
|
}
|
|
226
244
|
|
|
227
245
|
export function restoreHeadersPrototype() {
|
|
@@ -30,7 +30,7 @@ export class WebSocketOverride extends EventTarget implements WebSocket {
|
|
|
30
30
|
public protocol: string
|
|
31
31
|
public extensions: string
|
|
32
32
|
public binaryType: BinaryType
|
|
33
|
-
public readyState:
|
|
33
|
+
public readyState: WebSocket['readyState']
|
|
34
34
|
public bufferedAmount: number
|
|
35
35
|
|
|
36
36
|
private _onopen: WebSocketEventListener | null = null
|
|
@@ -16,6 +16,7 @@ import { INTERNAL_REQUEST_ID_HEADER_NAME } from '../../Interceptor'
|
|
|
16
16
|
import { createRequestId } from '../../createRequestId'
|
|
17
17
|
import { getBodyByteLength } from './utils/getBodyByteLength'
|
|
18
18
|
import { setRawRequest } from '../../getRawRequest'
|
|
19
|
+
import { FetchRequest } from '../../utils/fetchUtils'
|
|
19
20
|
|
|
20
21
|
const kIsRequestHandled = Symbol('kIsRequestHandled')
|
|
21
22
|
const IS_NODE = isNodeProcess()
|
|
@@ -671,7 +672,7 @@ export class XMLHttpRequestController {
|
|
|
671
672
|
const resolvedBody =
|
|
672
673
|
body instanceof Document ? body.documentElement.innerText : body
|
|
673
674
|
|
|
674
|
-
const fetchRequest = new
|
|
675
|
+
const fetchRequest = new FetchRequest(this.url.href, {
|
|
675
676
|
method: this.method,
|
|
676
677
|
headers: this.requestHeaders,
|
|
677
678
|
/**
|
|
@@ -12,7 +12,7 @@ import { createNetworkError } from './utils/createNetworkError'
|
|
|
12
12
|
import { followFetchRedirect } from './utils/followRedirect'
|
|
13
13
|
import { decompressResponse } from './utils/decompression'
|
|
14
14
|
import { hasConfigurableGlobal } from '../../utils/hasConfigurableGlobal'
|
|
15
|
-
import { FetchResponse } from '../../utils/fetchUtils'
|
|
15
|
+
import { FetchRequest, FetchResponse } from '../../utils/fetchUtils'
|
|
16
16
|
import { setRawRequest } from '../../getRawRequest'
|
|
17
17
|
import { isResponseError } from '../../utils/responseUtils'
|
|
18
18
|
|
|
@@ -51,7 +51,7 @@ export class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
|
51
51
|
? new URL(input, location.href)
|
|
52
52
|
: input
|
|
53
53
|
|
|
54
|
-
const request = new
|
|
54
|
+
const request = new FetchRequest(resolvedInput, init)
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
57
|
* @note Set the raw request only if a Request instance was provided to fetch.
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { FetchRequest } from './fetchUtils'
|
|
3
|
+
|
|
4
|
+
describe('FetchRequest', () => {
|
|
5
|
+
const URL = 'https://example.com/'
|
|
6
|
+
|
|
7
|
+
it('passes instanceof check with the regular Request', () => {
|
|
8
|
+
expect(new FetchRequest(URL)).toBeInstanceOf(Request)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('creates a request with a non-configurable method', () => {
|
|
12
|
+
expect
|
|
13
|
+
.soft(new FetchRequest(URL, { method: 'CONNECT' }))
|
|
14
|
+
.toHaveProperty('method', 'CONNECT')
|
|
15
|
+
|
|
16
|
+
expect
|
|
17
|
+
.soft(new FetchRequest(URL, { method: 'TRACE' }))
|
|
18
|
+
.toHaveProperty('method', 'TRACE')
|
|
19
|
+
expect
|
|
20
|
+
.soft(new FetchRequest(new FetchRequest(URL, { method: 'TRACE' })))
|
|
21
|
+
.toHaveProperty('method', 'TRACE')
|
|
22
|
+
|
|
23
|
+
expect
|
|
24
|
+
.soft(new FetchRequest(URL, { method: 'TRACK' }))
|
|
25
|
+
.toHaveProperty('method', 'TRACK')
|
|
26
|
+
expect
|
|
27
|
+
.soft(new FetchRequest(new FetchRequest(URL, { method: 'TRACK' })))
|
|
28
|
+
.toHaveProperty('method', 'TRACK')
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('creates a request with a non-configurable mode', () => {
|
|
32
|
+
expect
|
|
33
|
+
.soft(new FetchRequest(URL, { mode: 'navigate' }))
|
|
34
|
+
.toHaveProperty('mode', 'navigate')
|
|
35
|
+
expect
|
|
36
|
+
.soft(new FetchRequest(new FetchRequest(URL, { mode: 'navigate' })))
|
|
37
|
+
.toHaveProperty('mode', 'navigate')
|
|
38
|
+
|
|
39
|
+
expect
|
|
40
|
+
.soft(new FetchRequest(URL, { mode: 'websocket' }))
|
|
41
|
+
.toHaveProperty('mode', 'websocket')
|
|
42
|
+
expect
|
|
43
|
+
.soft(new FetchRequest(new FetchRequest(URL, { mode: 'websocket' })))
|
|
44
|
+
.toHaveProperty('mode', 'websocket')
|
|
45
|
+
|
|
46
|
+
expect
|
|
47
|
+
.soft(new FetchRequest(URL, { mode: 'webtransport' }))
|
|
48
|
+
.toHaveProperty('mode', 'webtransport')
|
|
49
|
+
expect
|
|
50
|
+
.soft(new FetchRequest(new FetchRequest(URL, { mode: 'webtransport' })))
|
|
51
|
+
.toHaveProperty('mode', 'webtransport')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('ignores body for the requests without a body', () => {
|
|
55
|
+
expect
|
|
56
|
+
.soft(new FetchRequest(URL, { body: null }))
|
|
57
|
+
.toHaveProperty('body', null)
|
|
58
|
+
expect
|
|
59
|
+
.soft(new FetchRequest(URL, { body: undefined }))
|
|
60
|
+
.toHaveProperty('body', null)
|
|
61
|
+
expect
|
|
62
|
+
.soft(new FetchRequest(URL, { body: 'hello' }))
|
|
63
|
+
.toHaveProperty('body', null)
|
|
64
|
+
|
|
65
|
+
expect
|
|
66
|
+
.soft(new FetchRequest(URL, { method: 'GET', body: 'hello' }))
|
|
67
|
+
.toHaveProperty('body', null)
|
|
68
|
+
expect
|
|
69
|
+
.soft(new FetchRequest(URL, { method: 'HEAD', body: 'hello' }))
|
|
70
|
+
.toHaveProperty('body', null)
|
|
71
|
+
expect
|
|
72
|
+
.soft(new FetchRequest(URL, { method: 'CONNECT', body: 'hello' }))
|
|
73
|
+
.toHaveProperty('body', null)
|
|
74
|
+
expect
|
|
75
|
+
.soft(new FetchRequest(URL, { method: 'TRACE', body: 'hello' }))
|
|
76
|
+
.toHaveProperty('body', null)
|
|
77
|
+
expect
|
|
78
|
+
.soft(new FetchRequest(URL, { method: 'TRACK', body: 'hello' }))
|
|
79
|
+
.toHaveProperty('body', null)
|
|
80
|
+
|
|
81
|
+
expect
|
|
82
|
+
.soft(new FetchRequest(URL, { method: 'POST', body: 'hello' }))
|
|
83
|
+
.toHaveProperty('body', expect.any(ReadableStream))
|
|
84
|
+
})
|
|
85
|
+
})
|