@mswjs/interceptors 0.41.4 → 0.41.6
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-C2KXXdBy.cjs → XMLHttpRequest-DNly-bu0.cjs} +9 -21
- package/lib/browser/XMLHttpRequest-DNly-bu0.cjs.map +1 -0
- package/lib/browser/{XMLHttpRequest-Bbvn_AjX.mjs → XMLHttpRequest-DaQ73vVt.mjs} +9 -21
- package/lib/{node/XMLHttpRequest-BBbs-p9i.mjs.map → browser/XMLHttpRequest-DaQ73vVt.mjs.map} +1 -1
- package/lib/browser/{createRequestId-Cs4oXfa1.cjs → createRequestId-DOf8Ktjs.cjs} +1 -1
- package/lib/browser/{createRequestId-Cs4oXfa1.cjs.map → createRequestId-DOf8Ktjs.cjs.map} +1 -1
- package/lib/browser/{createRequestId-DQcIlohW.mjs → createRequestId-DYCsFHOi.mjs} +1 -1
- package/lib/browser/{createRequestId-DQcIlohW.mjs.map → createRequestId-DYCsFHOi.mjs.map} +1 -1
- package/lib/browser/{fetch-D4es-Apw.mjs → fetch-BS6cxWzo.mjs} +12 -20
- package/lib/browser/fetch-BS6cxWzo.mjs.map +1 -0
- package/lib/browser/{fetch-CJYWVliE.cjs → fetch-BmrYPMQ0.cjs} +12 -20
- package/lib/browser/fetch-BmrYPMQ0.cjs.map +1 -0
- package/lib/browser/{getRawRequest-DaxX6uMy.cjs → getRawRequest-B6GjFKAr.cjs} +22 -11
- package/lib/browser/getRawRequest-B6GjFKAr.cjs.map +1 -0
- package/lib/browser/{getRawRequest-Df640uw8.mjs → getRawRequest-BP--nUwn.mjs} +23 -6
- package/lib/browser/getRawRequest-BP--nUwn.mjs.map +1 -0
- package/lib/browser/{glossary-BdLS4k1H.d.cts → glossary-1lhZx6Aw.d.cts} +2 -3
- package/lib/browser/{glossary-DYwOrogs.d.mts → glossary-nkHWcwvY.d.mts} +2 -3
- package/lib/browser/{handleRequest--veikAod.cjs → handleRequest-BIte-8l8.cjs} +3 -3
- package/lib/browser/{handleRequest--veikAod.cjs.map → handleRequest-BIte-8l8.cjs.map} +1 -1
- package/lib/browser/{handleRequest-Dq1a55b4.mjs → handleRequest-CnDA8MVO.mjs} +3 -3
- package/lib/browser/{handleRequest-Dq1a55b4.mjs.map → handleRequest-CnDA8MVO.mjs.map} +1 -1
- package/lib/browser/hasConfigurableGlobal-B39W0thh.mjs +98 -0
- package/lib/browser/hasConfigurableGlobal-B39W0thh.mjs.map +1 -0
- package/lib/browser/hasConfigurableGlobal-B5o6PHq3.cjs +115 -0
- package/lib/browser/hasConfigurableGlobal-B5o6PHq3.cjs.map +1 -0
- package/lib/browser/index.cjs +2 -3
- package/lib/browser/index.d.cts +10 -2
- package/lib/browser/index.d.mts +10 -2
- package/lib/browser/index.mjs +3 -3
- package/lib/browser/interceptors/WebSocket/index.cjs +6 -10
- package/lib/browser/interceptors/WebSocket/index.cjs.map +1 -1
- package/lib/browser/interceptors/WebSocket/index.mjs +6 -10
- package/lib/browser/interceptors/WebSocket/index.mjs.map +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.cjs +5 -4
- package/lib/browser/interceptors/XMLHttpRequest/index.d.cts +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.d.mts +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +5 -4
- package/lib/browser/interceptors/fetch/index.cjs +5 -4
- package/lib/browser/interceptors/fetch/index.d.cts +1 -1
- package/lib/browser/interceptors/fetch/index.d.mts +1 -1
- package/lib/browser/interceptors/fetch/index.mjs +5 -4
- package/lib/browser/presets/browser.cjs +6 -5
- package/lib/browser/presets/browser.cjs.map +1 -1
- package/lib/browser/presets/browser.mjs +6 -5
- package/lib/browser/presets/browser.mjs.map +1 -1
- package/lib/node/{BatchInterceptor-BxgAXyLw.mjs → BatchInterceptor-0prNZ0wK.mjs} +2 -2
- package/lib/node/{BatchInterceptor-BxgAXyLw.mjs.map → BatchInterceptor-0prNZ0wK.mjs.map} +1 -1
- package/lib/node/{BatchInterceptor-7u9fqQIe.cjs → BatchInterceptor-Bo8JunSK.cjs} +2 -2
- package/lib/node/{BatchInterceptor-7u9fqQIe.cjs.map → BatchInterceptor-Bo8JunSK.cjs.map} +1 -1
- package/lib/node/{BatchInterceptor-D7mXzHcQ.d.mts → BatchInterceptor-KKF8Q47I.d.cts} +2 -2
- package/lib/node/{BatchInterceptor-D_YqR8qU.d.cts → BatchInterceptor-rSr-NH_H.d.mts} +2 -2
- package/lib/node/{ClientRequest-Cl3Ky7Tw.mjs → ClientRequest-BquJl6T-.mjs} +5 -5
- package/lib/node/{ClientRequest-Cl3Ky7Tw.mjs.map → ClientRequest-BquJl6T-.mjs.map} +1 -1
- package/lib/node/{ClientRequest-CMSXChAM.cjs → ClientRequest-OY5NnrOh.cjs} +5 -5
- package/lib/node/{ClientRequest-CMSXChAM.cjs.map → ClientRequest-OY5NnrOh.cjs.map} +1 -1
- package/lib/node/{Interceptor-DJ2akVWI.d.cts → Interceptor-BsZ21ue0.d.cts} +2 -3
- package/lib/node/{Interceptor-DEazpLJd.d.mts → Interceptor-soD-WpQO.d.mts} +2 -3
- package/lib/node/RemoteHttpInterceptor.cjs +10 -9
- package/lib/node/RemoteHttpInterceptor.cjs.map +1 -1
- package/lib/node/RemoteHttpInterceptor.d.cts +3 -3
- package/lib/node/RemoteHttpInterceptor.d.mts +3 -3
- package/lib/node/RemoteHttpInterceptor.mjs +10 -9
- package/lib/node/RemoteHttpInterceptor.mjs.map +1 -1
- package/lib/node/{XMLHttpRequest-Dj3xJgke.cjs → XMLHttpRequest-C1KTngEF.cjs} +10 -23
- package/lib/node/XMLHttpRequest-C1KTngEF.cjs.map +1 -0
- package/lib/node/{XMLHttpRequest-BBbs-p9i.mjs → XMLHttpRequest-Cj07Pos7.mjs} +10 -23
- package/lib/{browser/XMLHttpRequest-Bbvn_AjX.mjs.map → node/XMLHttpRequest-Cj07Pos7.mjs.map} +1 -1
- package/lib/node/{bufferUtils-_8XfKIfX.mjs → bufferUtils-DxPxwff_.mjs} +1 -1
- package/lib/node/{bufferUtils-_8XfKIfX.mjs.map → bufferUtils-DxPxwff_.mjs.map} +1 -1
- package/lib/node/{bufferUtils-DiCTqG-7.cjs → bufferUtils-S5_-2eN4.cjs} +1 -1
- package/lib/node/{bufferUtils-DiCTqG-7.cjs.map → bufferUtils-S5_-2eN4.cjs.map} +1 -1
- package/lib/node/{fetch-BoCFyaYx.cjs → fetch-B9UiBg-M.cjs} +12 -21
- package/lib/node/fetch-B9UiBg-M.cjs.map +1 -0
- package/lib/node/{fetch-DmiwAmpu.mjs → fetch-UxWd1aQX.mjs} +12 -21
- package/lib/node/fetch-UxWd1aQX.mjs.map +1 -0
- package/lib/node/{fetchUtils-DE_q7tpr.cjs → fetchUtils-BAlG765A.cjs} +22 -1
- package/lib/node/{fetchUtils-DE_q7tpr.cjs.map → fetchUtils-BAlG765A.cjs.map} +1 -1
- package/lib/node/{fetchUtils-BTrdMHGp.mjs → fetchUtils-Bq0Mdmkv.mjs} +22 -1
- package/lib/node/{fetchUtils-BTrdMHGp.mjs.map → fetchUtils-Bq0Mdmkv.mjs.map} +1 -1
- package/lib/node/{getRawRequest-BavnMWh_.cjs → getRawRequest-B6znU6il.cjs} +1 -1
- package/lib/node/{getRawRequest-BavnMWh_.cjs.map → getRawRequest-B6znU6il.cjs.map} +1 -1
- package/lib/node/{getRawRequest-DnwmXyOW.mjs → getRawRequest-C2-1urzA.mjs} +1 -1
- package/lib/node/{getRawRequest-DnwmXyOW.mjs.map → getRawRequest-C2-1urzA.mjs.map} +1 -1
- package/lib/node/{handleRequest-pLIe3KKo.cjs → handleRequest-DUPo40Df.cjs} +2 -2
- package/lib/node/{handleRequest-pLIe3KKo.cjs.map → handleRequest-DUPo40Df.cjs.map} +1 -1
- package/lib/node/{handleRequest-CDabRyt1.mjs → handleRequest-u6jpjgrr.mjs} +2 -2
- package/lib/node/{handleRequest-CDabRyt1.mjs.map → handleRequest-u6jpjgrr.mjs.map} +1 -1
- package/lib/node/hasConfigurableGlobal-Befl_QJD.cjs +97 -0
- package/lib/node/hasConfigurableGlobal-Befl_QJD.cjs.map +1 -0
- package/lib/node/hasConfigurableGlobal-jN_-v3gp.mjs +85 -0
- package/lib/node/hasConfigurableGlobal-jN_-v3gp.mjs.map +1 -0
- package/lib/node/{index-BMbJ8FXL.d.cts → index-BJ5E5aW-.d.cts} +2 -2
- package/lib/node/{index-C0YAQ36w.d.mts → index-DfoF45ih.d.mts} +2 -2
- package/lib/node/index.cjs +4 -6
- package/lib/node/index.cjs.map +1 -1
- package/lib/node/index.d.cts +11 -3
- package/lib/node/index.d.mts +11 -3
- package/lib/node/index.mjs +5 -6
- package/lib/node/index.mjs.map +1 -1
- package/lib/node/interceptors/ClientRequest/index.cjs +4 -4
- package/lib/node/interceptors/ClientRequest/index.d.cts +1 -1
- package/lib/node/interceptors/ClientRequest/index.d.mts +2 -2
- package/lib/node/interceptors/ClientRequest/index.mjs +4 -4
- package/lib/node/interceptors/XMLHttpRequest/index.cjs +5 -4
- package/lib/node/interceptors/XMLHttpRequest/index.d.cts +1 -1
- package/lib/node/interceptors/XMLHttpRequest/index.d.mts +1 -1
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +5 -4
- package/lib/node/interceptors/fetch/index.cjs +4 -3
- package/lib/node/interceptors/fetch/index.d.cts +1 -1
- package/lib/node/interceptors/fetch/index.d.mts +1 -1
- package/lib/node/interceptors/fetch/index.mjs +4 -3
- package/lib/node/{node-dKdAf3tC.cjs → node-DIKcnzhK.cjs} +2 -2
- package/lib/node/{node-dKdAf3tC.cjs.map → node-DIKcnzhK.cjs.map} +1 -1
- package/lib/node/{node-DwCc6iuP.mjs → node-lsdNwZEW.mjs} +2 -2
- package/lib/node/{node-DwCc6iuP.mjs.map → node-lsdNwZEW.mjs.map} +1 -1
- package/lib/node/presets/node.cjs +8 -7
- package/lib/node/presets/node.cjs.map +1 -1
- package/lib/node/presets/node.d.cts +1 -1
- package/lib/node/presets/node.d.mts +2 -2
- package/lib/node/presets/node.mjs +8 -7
- package/lib/node/presets/node.mjs.map +1 -1
- package/lib/node/utils/node/index.cjs +1 -1
- package/lib/node/utils/node/index.mjs +1 -1
- package/package.json +1 -1
- package/src/RemoteHttpInterceptor.ts +1 -1
- package/src/glossary.ts +3 -5
- package/src/interceptors/WebSocket/index.ts +8 -15
- package/src/interceptors/XMLHttpRequest/index.ts +12 -32
- package/src/interceptors/fetch/index.ts +10 -26
- package/src/utils/fetchUtils.test.ts +25 -1
- package/src/utils/fetchUtils.ts +28 -0
- package/src/utils/globalUtils.test.ts +144 -0
- package/src/utils/globalsRegistry.ts +107 -0
- package/src/utils/hasConfigurableGlobal.test.ts +11 -0
- package/src/utils/hasConfigurableGlobal.ts +9 -3
- package/lib/browser/XMLHttpRequest-C2KXXdBy.cjs.map +0 -1
- package/lib/browser/fetch-CJYWVliE.cjs.map +0 -1
- package/lib/browser/fetch-D4es-Apw.mjs.map +0 -1
- package/lib/browser/getRawRequest-DaxX6uMy.cjs.map +0 -1
- package/lib/browser/getRawRequest-Df640uw8.mjs.map +0 -1
- package/lib/browser/hasConfigurableGlobal-BvCTG97d.cjs +0 -45
- package/lib/browser/hasConfigurableGlobal-BvCTG97d.cjs.map +0 -1
- package/lib/browser/hasConfigurableGlobal-npXitu1-.mjs +0 -33
- package/lib/browser/hasConfigurableGlobal-npXitu1-.mjs.map +0 -1
- package/lib/node/XMLHttpRequest-Dj3xJgke.cjs.map +0 -1
- package/lib/node/fetch-BoCFyaYx.cjs.map +0 -1
- package/lib/node/fetch-DmiwAmpu.mjs.map +0 -1
- package/lib/node/glossary-BLKRyLBd.cjs +0 -12
- package/lib/node/glossary-BLKRyLBd.cjs.map +0 -1
- package/lib/node/glossary-glQBRnVD.mjs +0 -6
- package/lib/node/glossary-glQBRnVD.mjs.map +0 -1
- package/lib/node/hasConfigurableGlobal-C97fWuaA.cjs +0 -26
- package/lib/node/hasConfigurableGlobal-C97fWuaA.cjs.map +0 -1
- package/lib/node/hasConfigurableGlobal-DBJA0vjm.mjs +0 -20
- package/lib/node/hasConfigurableGlobal-DBJA0vjm.mjs.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { o as RequestController, s as InterceptorError } from "./getRawRequest-
|
|
2
|
-
import {
|
|
1
|
+
import { o as RequestController, s as InterceptorError } from "./getRawRequest-BP--nUwn.mjs";
|
|
2
|
+
import { r as emitAsync } from "./hasConfigurableGlobal-B39W0thh.mjs";
|
|
3
3
|
import { DeferredPromise } from "@open-draft/deferred-promise";
|
|
4
4
|
import { until } from "@open-draft/until";
|
|
5
5
|
|
|
@@ -175,4 +175,4 @@ async function handleRequest(options) {
|
|
|
175
175
|
|
|
176
176
|
//#endregion
|
|
177
177
|
export { isResponseError as n, handleRequest as t };
|
|
178
|
-
//# sourceMappingURL=handleRequest-
|
|
178
|
+
//# sourceMappingURL=handleRequest-CnDA8MVO.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleRequest-Dq1a55b4.mjs","names":[],"sources":["../../src/utils/isObject.ts","../../src/utils/isPropertyAccessible.ts","../../src/utils/responseUtils.ts","../../src/utils/isNodeLikeError.ts","../../src/utils/handleRequest.ts"],"sourcesContent":["/**\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","/**\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 { 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-CnDA8MVO.mjs","names":[],"sources":["../../src/utils/isObject.ts","../../src/utils/isPropertyAccessible.ts","../../src/utils/responseUtils.ts","../../src/utils/isNodeLikeError.ts","../../src/utils/handleRequest.ts"],"sourcesContent":["/**\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","/**\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 { 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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"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { invariant } from "outvariant";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/emitAsync.ts
|
|
4
|
+
/**
|
|
5
|
+
* Emits an event on the given emitter but executes
|
|
6
|
+
* the listeners sequentially. This accounts for asynchronous
|
|
7
|
+
* listeners (e.g. those having "sleep" and handling the request).
|
|
8
|
+
*/
|
|
9
|
+
async function emitAsync(emitter, eventName, ...data) {
|
|
10
|
+
const listeners = emitter.listeners(eventName);
|
|
11
|
+
if (listeners.length === 0) return;
|
|
12
|
+
for (const listener of listeners) await listener.apply(emitter, data);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/utils/globalsRegistry.ts
|
|
17
|
+
var GlobalsRegistry = class {
|
|
18
|
+
#globals = /* @__PURE__ */ new Map();
|
|
19
|
+
replaceGlobal(key, nextValue) {
|
|
20
|
+
invariant(!this.#globals.has(key), `Failed to replace a global value at "${key}": already replaced.`);
|
|
21
|
+
const match = getDeepPropertyDescriptor(globalThis, key);
|
|
22
|
+
if (typeof match === "undefined") {
|
|
23
|
+
console.warn(`Failed to replace a global value at "${key}": not a global value.`);
|
|
24
|
+
return () => {};
|
|
25
|
+
}
|
|
26
|
+
Object.defineProperty(globalThis, key, {
|
|
27
|
+
value: nextValue,
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true
|
|
30
|
+
});
|
|
31
|
+
const restoreGlobal = () => {
|
|
32
|
+
if (!this.#globals.has(key)) return;
|
|
33
|
+
if (match.owner === globalThis) Object.defineProperty(match.owner, key, match.descriptor);
|
|
34
|
+
else
|
|
35
|
+
/**
|
|
36
|
+
* @todo Delete the proxy property set by the registry.
|
|
37
|
+
* If the owner isn't `globalThis`, the property is likely nested in the prototype.
|
|
38
|
+
* The registry does not meddle with those, they are left intact.
|
|
39
|
+
*/
|
|
40
|
+
Reflect.deleteProperty(globalThis, key);
|
|
41
|
+
this.#globals.delete(key);
|
|
42
|
+
};
|
|
43
|
+
this.#globals.set(key, restoreGlobal);
|
|
44
|
+
return restoreGlobal;
|
|
45
|
+
}
|
|
46
|
+
restoreAllGlobals() {
|
|
47
|
+
const errors = [];
|
|
48
|
+
for (const [, restoreGlobal] of this.#globals) try {
|
|
49
|
+
restoreGlobal();
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof Error) errors.push(error);
|
|
52
|
+
else throw error;
|
|
53
|
+
}
|
|
54
|
+
if (errors.length > 0) throw new AggregateError(errors, "FOO!");
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const globalsRegistry = new GlobalsRegistry();
|
|
58
|
+
/**
|
|
59
|
+
* Returns a property descriptor for the given property on the owner.
|
|
60
|
+
* Walks down the prototype chain if the property does not exist on the owner.
|
|
61
|
+
* Handy for getting a global property descriptor where `globalThis` is
|
|
62
|
+
* replaced with a controlled class (e.g. ServiceWorkerGlobalScope).
|
|
63
|
+
*/
|
|
64
|
+
function getDeepPropertyDescriptor(owner, key) {
|
|
65
|
+
let currentOwner = owner;
|
|
66
|
+
let descriptor;
|
|
67
|
+
while (currentOwner) {
|
|
68
|
+
descriptor = Object.getOwnPropertyDescriptor(currentOwner, key);
|
|
69
|
+
if (descriptor) return {
|
|
70
|
+
owner: currentOwner,
|
|
71
|
+
descriptor
|
|
72
|
+
};
|
|
73
|
+
currentOwner = Object.getPrototypeOf(currentOwner);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/utils/hasConfigurableGlobal.ts
|
|
79
|
+
/**
|
|
80
|
+
* Returns a boolean indicating whether the given global property
|
|
81
|
+
* is defined and is configurable.
|
|
82
|
+
*/
|
|
83
|
+
function hasConfigurableGlobal(propertyName) {
|
|
84
|
+
const match = getDeepPropertyDescriptor(globalThis, propertyName);
|
|
85
|
+
if (typeof match === "undefined") return false;
|
|
86
|
+
const { descriptor } = match;
|
|
87
|
+
if (typeof descriptor.get === "function" && typeof descriptor.get() === "undefined") return false;
|
|
88
|
+
if (typeof descriptor.get === "undefined" && descriptor.value == null) return false;
|
|
89
|
+
if (typeof descriptor.set === "undefined" && !descriptor.configurable) {
|
|
90
|
+
console.error(`[MSW] Failed to apply interceptor: the global \`${propertyName}\` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.`);
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
//#endregion
|
|
97
|
+
export { globalsRegistry as n, emitAsync as r, hasConfigurableGlobal as t };
|
|
98
|
+
//# sourceMappingURL=hasConfigurableGlobal-B39W0thh.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hasConfigurableGlobal-B39W0thh.mjs","names":["#globals","errors: Array<Error>","currentOwner: Owner | null","descriptor: PropertyDescriptor | undefined"],"sources":["../../src/utils/emitAsync.ts","../../src/utils/globalsRegistry.ts","../../src/utils/hasConfigurableGlobal.ts"],"sourcesContent":["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","import { invariant } from 'outvariant'\n\nclass GlobalsRegistry {\n #globals = new Map<keyof typeof globalThis, () => void>()\n\n public replaceGlobal<K extends keyof typeof globalThis>(\n key: K,\n nextValue: (typeof globalThis)[K]\n ): () => void {\n invariant(\n !this.#globals.has(key),\n `Failed to replace a global value at \"${key}\": already replaced.`\n )\n\n const match = getDeepPropertyDescriptor(globalThis, key)\n\n if (typeof match === 'undefined') {\n console.warn(\n `Failed to replace a global value at \"${key}\": not a global value.`\n )\n return () => {}\n }\n\n Object.defineProperty(globalThis, key, {\n value: nextValue,\n enumerable: true,\n configurable: true,\n })\n\n const restoreGlobal = () => {\n if (!this.#globals.has(key)) {\n return\n }\n\n if (match.owner === globalThis) {\n Object.defineProperty(match.owner, key, match.descriptor)\n } else {\n /**\n * @todo Delete the proxy property set by the registry.\n * If the owner isn't `globalThis`, the property is likely nested in the prototype.\n * The registry does not meddle with those, they are left intact.\n */\n Reflect.deleteProperty(globalThis, key)\n }\n\n this.#globals.delete(key)\n }\n\n this.#globals.set(key, restoreGlobal)\n\n return restoreGlobal\n }\n\n public restoreAllGlobals(): void {\n const errors: Array<Error> = []\n\n for (const [, restoreGlobal] of this.#globals) {\n try {\n restoreGlobal()\n } catch (error) {\n if (error instanceof Error) {\n errors.push(error)\n } else {\n throw error\n }\n }\n }\n\n if (errors.length > 0) {\n throw new AggregateError(errors, 'FOO!')\n }\n }\n}\n\nexport const globalsRegistry = new GlobalsRegistry()\n\ninterface DeepDescriptorMatch {\n owner: object\n descriptor: PropertyDescriptor\n}\n\n/**\n * Returns a property descriptor for the given property on the owner.\n * Walks down the prototype chain if the property does not exist on the owner.\n * Handy for getting a global property descriptor where `globalThis` is\n * replaced with a controlled class (e.g. ServiceWorkerGlobalScope).\n */\nexport function getDeepPropertyDescriptor<Owner extends object>(\n owner: Owner,\n key: keyof Owner\n): DeepDescriptorMatch | undefined {\n let currentOwner: Owner | null = owner\n let descriptor: PropertyDescriptor | undefined\n\n while (currentOwner) {\n descriptor = Object.getOwnPropertyDescriptor(currentOwner, key)\n\n if (descriptor) {\n return {\n owner: currentOwner,\n descriptor,\n }\n }\n\n currentOwner = Object.getPrototypeOf(currentOwner)\n }\n}\n","import { getDeepPropertyDescriptor } from './globalsRegistry'\n\n/**\n * Returns a boolean indicating whether the given global property\n * is defined and is configurable.\n */\nexport function hasConfigurableGlobal(\n propertyName: keyof typeof globalThis\n): boolean {\n const match = getDeepPropertyDescriptor(globalThis, propertyName)\n\n // The property is not set at all.\n if (typeof match === 'undefined') {\n return false\n }\n\n const { descriptor } = match\n\n // The property is set to a getter that returns undefined.\n if (\n typeof descriptor.get === 'function' &&\n typeof descriptor.get() === 'undefined'\n ) {\n return false\n }\n\n // The property is set to a value equal to undefined.\n if (typeof descriptor.get === 'undefined' && descriptor.value == null) {\n return false\n }\n\n if (typeof descriptor.set === 'undefined' && !descriptor.configurable) {\n console.error(\n `[MSW] Failed to apply interceptor: the global \\`${propertyName}\\` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.`\n )\n return false\n }\n\n return true\n}\n"],"mappings":";;;;;;;;AAOA,eAAsB,UAIpB,SACA,WACA,GAAG,MACY;CACf,MAAM,YAAY,QAAQ,UAAU,UAAU;AAE9C,KAAI,UAAU,WAAW,EACvB;AAGF,MAAK,MAAM,YAAY,UACrB,OAAM,SAAS,MAAM,SAAS,KAAK;;;;;ACpBvC,IAAM,kBAAN,MAAsB;CACpB,2BAAW,IAAI,KAA0C;CAEzD,AAAO,cACL,KACA,WACY;AACZ,YACE,CAAC,MAAKA,QAAS,IAAI,IAAI,EACvB,wCAAwC,IAAI,sBAC7C;EAED,MAAM,QAAQ,0BAA0B,YAAY,IAAI;AAExD,MAAI,OAAO,UAAU,aAAa;AAChC,WAAQ,KACN,wCAAwC,IAAI,wBAC7C;AACD,gBAAa;;AAGf,SAAO,eAAe,YAAY,KAAK;GACrC,OAAO;GACP,YAAY;GACZ,cAAc;GACf,CAAC;EAEF,MAAM,sBAAsB;AAC1B,OAAI,CAAC,MAAKA,QAAS,IAAI,IAAI,CACzB;AAGF,OAAI,MAAM,UAAU,WAClB,QAAO,eAAe,MAAM,OAAO,KAAK,MAAM,WAAW;;;;;;;AAOzD,WAAQ,eAAe,YAAY,IAAI;AAGzC,SAAKA,QAAS,OAAO,IAAI;;AAG3B,QAAKA,QAAS,IAAI,KAAK,cAAc;AAErC,SAAO;;CAGT,AAAO,oBAA0B;EAC/B,MAAMC,SAAuB,EAAE;AAE/B,OAAK,MAAM,GAAG,kBAAkB,MAAKD,QACnC,KAAI;AACF,kBAAe;WACR,OAAO;AACd,OAAI,iBAAiB,MACnB,QAAO,KAAK,MAAM;OAElB,OAAM;;AAKZ,MAAI,OAAO,SAAS,EAClB,OAAM,IAAI,eAAe,QAAQ,OAAO;;;AAK9C,MAAa,kBAAkB,IAAI,iBAAiB;;;;;;;AAapD,SAAgB,0BACd,OACA,KACiC;CACjC,IAAIE,eAA6B;CACjC,IAAIC;AAEJ,QAAO,cAAc;AACnB,eAAa,OAAO,yBAAyB,cAAc,IAAI;AAE/D,MAAI,WACF,QAAO;GACL,OAAO;GACP;GACD;AAGH,iBAAe,OAAO,eAAe,aAAa;;;;;;;;;;AClGtD,SAAgB,sBACd,cACS;CACT,MAAM,QAAQ,0BAA0B,YAAY,aAAa;AAGjE,KAAI,OAAO,UAAU,YACnB,QAAO;CAGT,MAAM,EAAE,eAAe;AAGvB,KACE,OAAO,WAAW,QAAQ,cAC1B,OAAO,WAAW,KAAK,KAAK,YAE5B,QAAO;AAIT,KAAI,OAAO,WAAW,QAAQ,eAAe,WAAW,SAAS,KAC/D,QAAO;AAGT,KAAI,OAAO,WAAW,QAAQ,eAAe,CAAC,WAAW,cAAc;AACrE,UAAQ,MACN,mDAAmD,aAAa,oKACjE;AACD,SAAO;;AAGT,QAAO"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
let outvariant = require("outvariant");
|
|
2
|
+
|
|
3
|
+
//#region src/utils/emitAsync.ts
|
|
4
|
+
/**
|
|
5
|
+
* Emits an event on the given emitter but executes
|
|
6
|
+
* the listeners sequentially. This accounts for asynchronous
|
|
7
|
+
* listeners (e.g. those having "sleep" and handling the request).
|
|
8
|
+
*/
|
|
9
|
+
async function emitAsync(emitter, eventName, ...data) {
|
|
10
|
+
const listeners = emitter.listeners(eventName);
|
|
11
|
+
if (listeners.length === 0) return;
|
|
12
|
+
for (const listener of listeners) await listener.apply(emitter, data);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/utils/globalsRegistry.ts
|
|
17
|
+
var GlobalsRegistry = class {
|
|
18
|
+
#globals = /* @__PURE__ */ new Map();
|
|
19
|
+
replaceGlobal(key, nextValue) {
|
|
20
|
+
(0, outvariant.invariant)(!this.#globals.has(key), `Failed to replace a global value at "${key}": already replaced.`);
|
|
21
|
+
const match = getDeepPropertyDescriptor(globalThis, key);
|
|
22
|
+
if (typeof match === "undefined") {
|
|
23
|
+
console.warn(`Failed to replace a global value at "${key}": not a global value.`);
|
|
24
|
+
return () => {};
|
|
25
|
+
}
|
|
26
|
+
Object.defineProperty(globalThis, key, {
|
|
27
|
+
value: nextValue,
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true
|
|
30
|
+
});
|
|
31
|
+
const restoreGlobal = () => {
|
|
32
|
+
if (!this.#globals.has(key)) return;
|
|
33
|
+
if (match.owner === globalThis) Object.defineProperty(match.owner, key, match.descriptor);
|
|
34
|
+
else
|
|
35
|
+
/**
|
|
36
|
+
* @todo Delete the proxy property set by the registry.
|
|
37
|
+
* If the owner isn't `globalThis`, the property is likely nested in the prototype.
|
|
38
|
+
* The registry does not meddle with those, they are left intact.
|
|
39
|
+
*/
|
|
40
|
+
Reflect.deleteProperty(globalThis, key);
|
|
41
|
+
this.#globals.delete(key);
|
|
42
|
+
};
|
|
43
|
+
this.#globals.set(key, restoreGlobal);
|
|
44
|
+
return restoreGlobal;
|
|
45
|
+
}
|
|
46
|
+
restoreAllGlobals() {
|
|
47
|
+
const errors = [];
|
|
48
|
+
for (const [, restoreGlobal] of this.#globals) try {
|
|
49
|
+
restoreGlobal();
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof Error) errors.push(error);
|
|
52
|
+
else throw error;
|
|
53
|
+
}
|
|
54
|
+
if (errors.length > 0) throw new AggregateError(errors, "FOO!");
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const globalsRegistry = new GlobalsRegistry();
|
|
58
|
+
/**
|
|
59
|
+
* Returns a property descriptor for the given property on the owner.
|
|
60
|
+
* Walks down the prototype chain if the property does not exist on the owner.
|
|
61
|
+
* Handy for getting a global property descriptor where `globalThis` is
|
|
62
|
+
* replaced with a controlled class (e.g. ServiceWorkerGlobalScope).
|
|
63
|
+
*/
|
|
64
|
+
function getDeepPropertyDescriptor(owner, key) {
|
|
65
|
+
let currentOwner = owner;
|
|
66
|
+
let descriptor;
|
|
67
|
+
while (currentOwner) {
|
|
68
|
+
descriptor = Object.getOwnPropertyDescriptor(currentOwner, key);
|
|
69
|
+
if (descriptor) return {
|
|
70
|
+
owner: currentOwner,
|
|
71
|
+
descriptor
|
|
72
|
+
};
|
|
73
|
+
currentOwner = Object.getPrototypeOf(currentOwner);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/utils/hasConfigurableGlobal.ts
|
|
79
|
+
/**
|
|
80
|
+
* Returns a boolean indicating whether the given global property
|
|
81
|
+
* is defined and is configurable.
|
|
82
|
+
*/
|
|
83
|
+
function hasConfigurableGlobal(propertyName) {
|
|
84
|
+
const match = getDeepPropertyDescriptor(globalThis, propertyName);
|
|
85
|
+
if (typeof match === "undefined") return false;
|
|
86
|
+
const { descriptor } = match;
|
|
87
|
+
if (typeof descriptor.get === "function" && typeof descriptor.get() === "undefined") return false;
|
|
88
|
+
if (typeof descriptor.get === "undefined" && descriptor.value == null) return false;
|
|
89
|
+
if (typeof descriptor.set === "undefined" && !descriptor.configurable) {
|
|
90
|
+
console.error(`[MSW] Failed to apply interceptor: the global \`${propertyName}\` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.`);
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
//#endregion
|
|
97
|
+
Object.defineProperty(exports, 'emitAsync', {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
get: function () {
|
|
100
|
+
return emitAsync;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
Object.defineProperty(exports, 'globalsRegistry', {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
get: function () {
|
|
106
|
+
return globalsRegistry;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
Object.defineProperty(exports, 'hasConfigurableGlobal', {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
get: function () {
|
|
112
|
+
return hasConfigurableGlobal;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
//# sourceMappingURL=hasConfigurableGlobal-B5o6PHq3.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hasConfigurableGlobal-B5o6PHq3.cjs","names":["#globals","errors: Array<Error>","currentOwner: Owner | null","descriptor: PropertyDescriptor | undefined"],"sources":["../../src/utils/emitAsync.ts","../../src/utils/globalsRegistry.ts","../../src/utils/hasConfigurableGlobal.ts"],"sourcesContent":["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","import { invariant } from 'outvariant'\n\nclass GlobalsRegistry {\n #globals = new Map<keyof typeof globalThis, () => void>()\n\n public replaceGlobal<K extends keyof typeof globalThis>(\n key: K,\n nextValue: (typeof globalThis)[K]\n ): () => void {\n invariant(\n !this.#globals.has(key),\n `Failed to replace a global value at \"${key}\": already replaced.`\n )\n\n const match = getDeepPropertyDescriptor(globalThis, key)\n\n if (typeof match === 'undefined') {\n console.warn(\n `Failed to replace a global value at \"${key}\": not a global value.`\n )\n return () => {}\n }\n\n Object.defineProperty(globalThis, key, {\n value: nextValue,\n enumerable: true,\n configurable: true,\n })\n\n const restoreGlobal = () => {\n if (!this.#globals.has(key)) {\n return\n }\n\n if (match.owner === globalThis) {\n Object.defineProperty(match.owner, key, match.descriptor)\n } else {\n /**\n * @todo Delete the proxy property set by the registry.\n * If the owner isn't `globalThis`, the property is likely nested in the prototype.\n * The registry does not meddle with those, they are left intact.\n */\n Reflect.deleteProperty(globalThis, key)\n }\n\n this.#globals.delete(key)\n }\n\n this.#globals.set(key, restoreGlobal)\n\n return restoreGlobal\n }\n\n public restoreAllGlobals(): void {\n const errors: Array<Error> = []\n\n for (const [, restoreGlobal] of this.#globals) {\n try {\n restoreGlobal()\n } catch (error) {\n if (error instanceof Error) {\n errors.push(error)\n } else {\n throw error\n }\n }\n }\n\n if (errors.length > 0) {\n throw new AggregateError(errors, 'FOO!')\n }\n }\n}\n\nexport const globalsRegistry = new GlobalsRegistry()\n\ninterface DeepDescriptorMatch {\n owner: object\n descriptor: PropertyDescriptor\n}\n\n/**\n * Returns a property descriptor for the given property on the owner.\n * Walks down the prototype chain if the property does not exist on the owner.\n * Handy for getting a global property descriptor where `globalThis` is\n * replaced with a controlled class (e.g. ServiceWorkerGlobalScope).\n */\nexport function getDeepPropertyDescriptor<Owner extends object>(\n owner: Owner,\n key: keyof Owner\n): DeepDescriptorMatch | undefined {\n let currentOwner: Owner | null = owner\n let descriptor: PropertyDescriptor | undefined\n\n while (currentOwner) {\n descriptor = Object.getOwnPropertyDescriptor(currentOwner, key)\n\n if (descriptor) {\n return {\n owner: currentOwner,\n descriptor,\n }\n }\n\n currentOwner = Object.getPrototypeOf(currentOwner)\n }\n}\n","import { getDeepPropertyDescriptor } from './globalsRegistry'\n\n/**\n * Returns a boolean indicating whether the given global property\n * is defined and is configurable.\n */\nexport function hasConfigurableGlobal(\n propertyName: keyof typeof globalThis\n): boolean {\n const match = getDeepPropertyDescriptor(globalThis, propertyName)\n\n // The property is not set at all.\n if (typeof match === 'undefined') {\n return false\n }\n\n const { descriptor } = match\n\n // The property is set to a getter that returns undefined.\n if (\n typeof descriptor.get === 'function' &&\n typeof descriptor.get() === 'undefined'\n ) {\n return false\n }\n\n // The property is set to a value equal to undefined.\n if (typeof descriptor.get === 'undefined' && descriptor.value == null) {\n return false\n }\n\n if (typeof descriptor.set === 'undefined' && !descriptor.configurable) {\n console.error(\n `[MSW] Failed to apply interceptor: the global \\`${propertyName}\\` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.`\n )\n return false\n }\n\n return true\n}\n"],"mappings":";;;;;;;;AAOA,eAAsB,UAIpB,SACA,WACA,GAAG,MACY;CACf,MAAM,YAAY,QAAQ,UAAU,UAAU;AAE9C,KAAI,UAAU,WAAW,EACvB;AAGF,MAAK,MAAM,YAAY,UACrB,OAAM,SAAS,MAAM,SAAS,KAAK;;;;;ACpBvC,IAAM,kBAAN,MAAsB;CACpB,2BAAW,IAAI,KAA0C;CAEzD,AAAO,cACL,KACA,WACY;AACZ,4BACE,CAAC,MAAKA,QAAS,IAAI,IAAI,EACvB,wCAAwC,IAAI,sBAC7C;EAED,MAAM,QAAQ,0BAA0B,YAAY,IAAI;AAExD,MAAI,OAAO,UAAU,aAAa;AAChC,WAAQ,KACN,wCAAwC,IAAI,wBAC7C;AACD,gBAAa;;AAGf,SAAO,eAAe,YAAY,KAAK;GACrC,OAAO;GACP,YAAY;GACZ,cAAc;GACf,CAAC;EAEF,MAAM,sBAAsB;AAC1B,OAAI,CAAC,MAAKA,QAAS,IAAI,IAAI,CACzB;AAGF,OAAI,MAAM,UAAU,WAClB,QAAO,eAAe,MAAM,OAAO,KAAK,MAAM,WAAW;;;;;;;AAOzD,WAAQ,eAAe,YAAY,IAAI;AAGzC,SAAKA,QAAS,OAAO,IAAI;;AAG3B,QAAKA,QAAS,IAAI,KAAK,cAAc;AAErC,SAAO;;CAGT,AAAO,oBAA0B;EAC/B,MAAMC,SAAuB,EAAE;AAE/B,OAAK,MAAM,GAAG,kBAAkB,MAAKD,QACnC,KAAI;AACF,kBAAe;WACR,OAAO;AACd,OAAI,iBAAiB,MACnB,QAAO,KAAK,MAAM;OAElB,OAAM;;AAKZ,MAAI,OAAO,SAAS,EAClB,OAAM,IAAI,eAAe,QAAQ,OAAO;;;AAK9C,MAAa,kBAAkB,IAAI,iBAAiB;;;;;;;AAapD,SAAgB,0BACd,OACA,KACiC;CACjC,IAAIE,eAA6B;CACjC,IAAIC;AAEJ,QAAO,cAAc;AACnB,eAAa,OAAO,yBAAyB,cAAc,IAAI;AAE/D,MAAI,WACF,QAAO;GACL,OAAO;GACP;GACD;AAGH,iBAAe,OAAO,eAAe,aAAa;;;;;;;;;;AClGtD,SAAgB,sBACd,cACS;CACT,MAAM,QAAQ,0BAA0B,YAAY,aAAa;AAGjE,KAAI,OAAO,UAAU,YACnB,QAAO;CAGT,MAAM,EAAE,eAAe;AAGvB,KACE,OAAO,WAAW,QAAQ,cAC1B,OAAO,WAAW,KAAK,KAAK,YAE5B,QAAO;AAIT,KAAI,OAAO,WAAW,QAAQ,eAAe,WAAW,SAAS,KAC/D,QAAO;AAGT,KAAI,OAAO,WAAW,QAAQ,eAAe,CAAC,WAAW,cAAc;AACrE,UAAQ,MACN,mDAAmD,aAAa,oKACjE;AACD,SAAO;;AAGT,QAAO"}
|
package/lib/browser/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
1
|
+
const require_createRequestId = require('./createRequestId-DOf8Ktjs.cjs');
|
|
2
|
+
const require_getRawRequest = require('./getRawRequest-B6GjFKAr.cjs');
|
|
3
3
|
const require_bufferUtils = require('./bufferUtils-Uc0eRItL.cjs');
|
|
4
4
|
const require_resolveWebSocketUrl = require('./resolveWebSocketUrl-6K6EgqsA.cjs');
|
|
5
5
|
|
|
@@ -56,7 +56,6 @@ exports.BatchInterceptor = BatchInterceptor;
|
|
|
56
56
|
exports.FetchRequest = require_getRawRequest.FetchRequest;
|
|
57
57
|
exports.FetchResponse = require_getRawRequest.FetchResponse;
|
|
58
58
|
exports.INTERNAL_REQUEST_ID_HEADER_NAME = require_createRequestId.INTERNAL_REQUEST_ID_HEADER_NAME;
|
|
59
|
-
exports.IS_PATCHED_MODULE = require_getRawRequest.IS_PATCHED_MODULE;
|
|
60
59
|
exports.Interceptor = require_createRequestId.Interceptor;
|
|
61
60
|
exports.InterceptorReadyState = require_createRequestId.InterceptorReadyState;
|
|
62
61
|
exports.RequestController = require_getRawRequest.RequestController;
|
package/lib/browser/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as RequestControllerSource, n as RequestCredentials, r as RequestController, t as HttpRequestEventMap } from "./glossary-1lhZx6Aw.cjs";
|
|
2
2
|
import { a as InterceptorReadyState, c as getGlobalSymbol, i as InterceptorEventMap, n as INTERNAL_REQUEST_ID_HEADER_NAME, o as InterceptorSubscription, r as Interceptor, s as deleteGlobalSymbol, t as ExtractEventNames } from "./Interceptor-Deczogc8.cjs";
|
|
3
3
|
import { EventMap, Listener } from "strict-event-emitter";
|
|
4
4
|
|
|
@@ -84,6 +84,14 @@ declare class FetchResponse extends Response {
|
|
|
84
84
|
* Parses the given raw HTTP headers into a Fetch API `Headers` instance.
|
|
85
85
|
*/
|
|
86
86
|
static parseRawHeaders(rawHeaders: Array<string>): Headers;
|
|
87
|
+
/**
|
|
88
|
+
* Safely clones the given `Response`.
|
|
89
|
+
* Coerces response clone exceptions into 500 mocked responses.
|
|
90
|
+
* Handy in the environments that introduce arbitrary response
|
|
91
|
+
* cloning restrictions, like "101 Switching Protocols" cloning
|
|
92
|
+
* in "miniflare".
|
|
93
|
+
*/
|
|
94
|
+
static clone(response: Response): Response;
|
|
87
95
|
constructor(body?: BodyInit | null, init?: FetchResponseInit);
|
|
88
96
|
}
|
|
89
97
|
//#endregion
|
|
@@ -111,5 +119,5 @@ declare function getRawRequest(request: Request): unknown | undefined;
|
|
|
111
119
|
*/
|
|
112
120
|
declare function resolveWebSocketUrl(url: string | URL): string;
|
|
113
121
|
//#endregion
|
|
114
|
-
export { BatchInterceptor, BatchInterceptorOptions, ExtractEventMapType, ExtractEventNames, FetchRequest, FetchResponse, HttpRequestEventMap, INTERNAL_REQUEST_ID_HEADER_NAME,
|
|
122
|
+
export { BatchInterceptor, BatchInterceptorOptions, ExtractEventMapType, ExtractEventNames, FetchRequest, FetchResponse, HttpRequestEventMap, INTERNAL_REQUEST_ID_HEADER_NAME, Interceptor, InterceptorEventMap, InterceptorReadyState, InterceptorSubscription, RequestController, type RequestControllerSource, RequestCredentials, createRequestId, decodeBuffer, deleteGlobalSymbol, encodeBuffer, getCleanUrl, getGlobalSymbol, getRawRequest, resolveWebSocketUrl };
|
|
115
123
|
//# sourceMappingURL=index.d.cts.map
|
package/lib/browser/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as RequestControllerSource, n as RequestCredentials, r as RequestController, t as HttpRequestEventMap } from "./glossary-nkHWcwvY.mjs";
|
|
2
2
|
import { a as InterceptorReadyState, c as getGlobalSymbol, i as InterceptorEventMap, n as INTERNAL_REQUEST_ID_HEADER_NAME, o as InterceptorSubscription, r as Interceptor, s as deleteGlobalSymbol, t as ExtractEventNames } from "./Interceptor-gqKgs-aF.mjs";
|
|
3
3
|
import { EventMap, Listener } from "strict-event-emitter";
|
|
4
4
|
|
|
@@ -84,6 +84,14 @@ declare class FetchResponse extends Response {
|
|
|
84
84
|
* Parses the given raw HTTP headers into a Fetch API `Headers` instance.
|
|
85
85
|
*/
|
|
86
86
|
static parseRawHeaders(rawHeaders: Array<string>): Headers;
|
|
87
|
+
/**
|
|
88
|
+
* Safely clones the given `Response`.
|
|
89
|
+
* Coerces response clone exceptions into 500 mocked responses.
|
|
90
|
+
* Handy in the environments that introduce arbitrary response
|
|
91
|
+
* cloning restrictions, like "101 Switching Protocols" cloning
|
|
92
|
+
* in "miniflare".
|
|
93
|
+
*/
|
|
94
|
+
static clone(response: Response): Response;
|
|
87
95
|
constructor(body?: BodyInit | null, init?: FetchResponseInit);
|
|
88
96
|
}
|
|
89
97
|
//#endregion
|
|
@@ -111,5 +119,5 @@ declare function getRawRequest(request: Request): unknown | undefined;
|
|
|
111
119
|
*/
|
|
112
120
|
declare function resolveWebSocketUrl(url: string | URL): string;
|
|
113
121
|
//#endregion
|
|
114
|
-
export { BatchInterceptor, BatchInterceptorOptions, ExtractEventMapType, ExtractEventNames, FetchRequest, FetchResponse, HttpRequestEventMap, INTERNAL_REQUEST_ID_HEADER_NAME,
|
|
122
|
+
export { BatchInterceptor, BatchInterceptorOptions, ExtractEventMapType, ExtractEventNames, FetchRequest, FetchResponse, HttpRequestEventMap, INTERNAL_REQUEST_ID_HEADER_NAME, Interceptor, InterceptorEventMap, InterceptorReadyState, InterceptorSubscription, RequestController, type RequestControllerSource, RequestCredentials, createRequestId, decodeBuffer, deleteGlobalSymbol, encodeBuffer, getCleanUrl, getGlobalSymbol, getRawRequest, resolveWebSocketUrl };
|
|
115
123
|
//# sourceMappingURL=index.d.mts.map
|
package/lib/browser/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { a as deleteGlobalSymbol, i as InterceptorReadyState, n as INTERNAL_REQUEST_ID_HEADER_NAME, o as getGlobalSymbol, r as Interceptor, t as createRequestId } from "./createRequestId-DYCsFHOi.mjs";
|
|
2
|
+
import { i as FetchResponse, o as RequestController, r as FetchRequest, t as getRawRequest } from "./getRawRequest-BP--nUwn.mjs";
|
|
3
3
|
import { n as encodeBuffer, t as decodeBuffer } from "./bufferUtils-BiiO6HZv.mjs";
|
|
4
4
|
import { t as resolveWebSocketUrl } from "./resolveWebSocketUrl-C83-x9iE.mjs";
|
|
5
5
|
|
|
@@ -52,5 +52,5 @@ function getCleanUrl(url, isAbsolute = true) {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
//#endregion
|
|
55
|
-
export { BatchInterceptor, FetchRequest, FetchResponse, INTERNAL_REQUEST_ID_HEADER_NAME,
|
|
55
|
+
export { BatchInterceptor, FetchRequest, FetchResponse, INTERNAL_REQUEST_ID_HEADER_NAME, Interceptor, InterceptorReadyState, RequestController, createRequestId, decodeBuffer, deleteGlobalSymbol, encodeBuffer, getCleanUrl, getGlobalSymbol, getRawRequest, resolveWebSocketUrl };
|
|
56
56
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const require_createRequestId = require('../../createRequestId-
|
|
1
|
+
const require_createRequestId = require('../../createRequestId-DOf8Ktjs.cjs');
|
|
2
2
|
const require_resolveWebSocketUrl = require('../../resolveWebSocketUrl-6K6EgqsA.cjs');
|
|
3
|
-
const require_hasConfigurableGlobal = require('../../hasConfigurableGlobal-
|
|
3
|
+
const require_hasConfigurableGlobal = require('../../hasConfigurableGlobal-B5o6PHq3.cjs');
|
|
4
4
|
let _open_draft_deferred_promise = require("@open-draft/deferred-promise");
|
|
5
5
|
let outvariant = require("outvariant");
|
|
6
6
|
|
|
@@ -558,7 +558,7 @@ var WebSocketInterceptor = class WebSocketInterceptor extends require_createRequ
|
|
|
558
558
|
return require_hasConfigurableGlobal.hasConfigurableGlobal("WebSocket");
|
|
559
559
|
}
|
|
560
560
|
setup() {
|
|
561
|
-
const
|
|
561
|
+
const logger = this.logger.extend("setup");
|
|
562
562
|
const WebSocketProxy = new Proxy(globalThis.WebSocket, { construct: (target, args, newTarget) => {
|
|
563
563
|
const [url, protocols] = args;
|
|
564
564
|
const createConnection = () => {
|
|
@@ -600,13 +600,9 @@ var WebSocketInterceptor = class WebSocketInterceptor extends require_createRequ
|
|
|
600
600
|
});
|
|
601
601
|
return socket;
|
|
602
602
|
} });
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
});
|
|
607
|
-
this.subscriptions.push(() => {
|
|
608
|
-
Object.defineProperty(globalThis, "WebSocket", originalWebSocketDescriptor);
|
|
609
|
-
});
|
|
603
|
+
logger.info("patching global WebSocket...");
|
|
604
|
+
this.subscriptions.push(require_hasConfigurableGlobal.globalsRegistry.replaceGlobal("WebSocket", WebSocketProxy));
|
|
605
|
+
logger.info("global WebSocket patched!", globalThis.WebSocket.name);
|
|
610
606
|
}
|
|
611
607
|
};
|
|
612
608
|
|