msw 2.0.11 → 2.0.13
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/index.d.mts +104 -0
- package/lib/browser/index.d.ts +1 -1
- package/lib/browser/index.js +1 -0
- package/lib/browser/index.js.map +1 -0
- package/lib/browser/index.mjs +1 -0
- package/lib/browser/index.mjs.map +1 -0
- package/lib/core/{GraphQLHandler-907fc607.d.ts → GraphQLHandler-SHlRCcy4.d.ts} +9 -2
- package/lib/core/GraphQLHandler-zlmUDnN6.d.mts +97 -0
- package/lib/core/HttpResponse.d.mts +2 -0
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/HttpResponse.js +1 -0
- package/lib/core/HttpResponse.js.map +1 -0
- package/lib/core/HttpResponse.mjs +1 -0
- package/lib/core/HttpResponse.mjs.map +1 -0
- package/lib/core/{RequestHandler-50ddea0c.d.ts → RequestHandler-rBDJQrEf.d.ts} +3 -1
- package/lib/core/RequestHandler-vxZdj6Tw.d.mts +179 -0
- package/lib/core/SetupApi.d.mts +26 -0
- package/lib/core/SetupApi.d.ts +1 -1
- package/lib/core/SetupApi.js +1 -0
- package/lib/core/SetupApi.js.map +1 -0
- package/lib/core/SetupApi.mjs +1 -0
- package/lib/core/SetupApi.mjs.map +1 -0
- package/lib/core/bypass.d.mts +16 -0
- package/lib/core/bypass.d.ts +1 -1
- package/lib/core/bypass.js +1 -0
- package/lib/core/bypass.js.map +1 -0
- package/lib/core/bypass.mjs +1 -0
- package/lib/core/bypass.mjs.map +1 -0
- package/lib/core/delay.d.mts +18 -0
- package/lib/core/delay.d.ts +1 -1
- package/lib/core/delay.js +1 -0
- package/lib/core/delay.js.map +1 -0
- package/lib/core/delay.mjs +1 -0
- package/lib/core/delay.mjs.map +1 -0
- package/lib/core/graphql.d.mts +109 -0
- package/lib/core/graphql.d.ts +3 -3
- package/lib/core/graphql.js +1 -0
- package/lib/core/graphql.js.map +1 -0
- package/lib/core/graphql.mjs +1 -0
- package/lib/core/graphql.mjs.map +1 -0
- package/lib/core/handlers/GraphQLHandler.d.mts +5 -0
- package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
- package/lib/core/handlers/GraphQLHandler.js +28 -8
- package/lib/core/handlers/GraphQLHandler.js.map +1 -0
- package/lib/core/handlers/GraphQLHandler.mjs +28 -8
- package/lib/core/handlers/GraphQLHandler.mjs.map +1 -0
- package/lib/core/handlers/HttpHandler.d.mts +62 -0
- package/lib/core/handlers/HttpHandler.d.ts +2 -2
- package/lib/core/handlers/HttpHandler.js +1 -0
- package/lib/core/handlers/HttpHandler.js.map +1 -0
- package/lib/core/handlers/HttpHandler.mjs +1 -0
- package/lib/core/handlers/HttpHandler.mjs.map +1 -0
- package/lib/core/handlers/RequestHandler.d.mts +2 -0
- package/lib/core/handlers/RequestHandler.d.ts +1 -1
- package/lib/core/handlers/RequestHandler.js +19 -4
- package/lib/core/handlers/RequestHandler.js.map +1 -0
- package/lib/core/handlers/RequestHandler.mjs +19 -4
- package/lib/core/handlers/RequestHandler.mjs.map +1 -0
- package/lib/core/http.d.mts +26 -0
- package/lib/core/http.d.ts +1 -1
- package/lib/core/http.js +1 -0
- package/lib/core/http.js.map +1 -0
- package/lib/core/http.mjs +1 -0
- package/lib/core/http.mjs.map +1 -0
- package/lib/core/index.d.mts +18 -0
- package/lib/core/index.d.ts +2 -2
- package/lib/core/index.js +1 -0
- package/lib/core/index.js.map +1 -0
- package/lib/core/index.mjs +1 -0
- package/lib/core/index.mjs.map +1 -0
- package/lib/core/passthrough.d.mts +17 -0
- package/lib/core/passthrough.js +1 -0
- package/lib/core/passthrough.js.map +1 -0
- package/lib/core/passthrough.mjs +1 -0
- package/lib/core/passthrough.mjs.map +1 -0
- package/lib/core/sharedOptions.d.mts +66 -0
- package/lib/core/sharedOptions.d.ts +2 -2
- package/lib/core/sharedOptions.js +1 -0
- package/lib/core/sharedOptions.js.map +1 -0
- package/lib/core/sharedOptions.mjs +1 -0
- package/lib/core/sharedOptions.mjs.map +1 -0
- package/lib/core/typeUtils.d.mts +7 -0
- package/lib/core/typeUtils.d.ts +1 -1
- package/lib/core/typeUtils.js +1 -0
- package/lib/core/typeUtils.js.map +1 -0
- package/lib/core/typeUtils.mjs +1 -0
- package/lib/core/typeUtils.mjs.map +1 -0
- package/lib/core/utils/HttpResponse/decorators.d.mts +12 -0
- package/lib/core/utils/HttpResponse/decorators.d.ts +2 -2
- package/lib/core/utils/HttpResponse/decorators.js +1 -0
- package/lib/core/utils/HttpResponse/decorators.js.map +1 -0
- package/lib/core/utils/HttpResponse/decorators.mjs +1 -0
- package/lib/core/utils/HttpResponse/decorators.mjs.map +1 -0
- package/lib/core/utils/getResponse.d.mts +2 -0
- package/lib/core/utils/getResponse.d.ts +1 -1
- package/lib/core/utils/getResponse.js +1 -0
- package/lib/core/utils/getResponse.js.map +1 -0
- package/lib/core/utils/getResponse.mjs +1 -0
- package/lib/core/utils/getResponse.mjs.map +1 -0
- package/lib/core/utils/handleRequest.d.mts +37 -0
- package/lib/core/utils/handleRequest.d.ts +2 -2
- package/lib/core/utils/handleRequest.js +1 -0
- package/lib/core/utils/handleRequest.js.map +1 -0
- package/lib/core/utils/handleRequest.mjs +1 -0
- package/lib/core/utils/handleRequest.mjs.map +1 -0
- package/lib/core/utils/internal/Disposable.d.mts +7 -0
- package/lib/core/utils/internal/Disposable.d.ts +1 -1
- package/lib/core/utils/internal/Disposable.js +1 -0
- package/lib/core/utils/internal/Disposable.js.map +1 -0
- package/lib/core/utils/internal/Disposable.mjs +1 -0
- package/lib/core/utils/internal/Disposable.mjs.map +1 -0
- package/lib/core/utils/internal/checkGlobals.d.mts +3 -0
- package/lib/core/utils/internal/checkGlobals.js +1 -0
- package/lib/core/utils/internal/checkGlobals.js.map +1 -0
- package/lib/core/utils/internal/checkGlobals.mjs +1 -0
- package/lib/core/utils/internal/checkGlobals.mjs.map +1 -0
- package/lib/core/utils/internal/devUtils.d.mts +19 -0
- package/lib/core/utils/internal/devUtils.js +1 -0
- package/lib/core/utils/internal/devUtils.js.map +1 -0
- package/lib/core/utils/internal/devUtils.mjs +1 -0
- package/lib/core/utils/internal/devUtils.mjs.map +1 -0
- package/lib/core/utils/internal/getCallFrame.d.mts +6 -0
- package/lib/core/utils/internal/getCallFrame.js +1 -0
- package/lib/core/utils/internal/getCallFrame.js.map +1 -0
- package/lib/core/utils/internal/getCallFrame.mjs +1 -0
- package/lib/core/utils/internal/getCallFrame.mjs.map +1 -0
- package/lib/core/utils/internal/isIterable.d.mts +6 -0
- package/lib/core/utils/internal/isIterable.js +1 -0
- package/lib/core/utils/internal/isIterable.js.map +1 -0
- package/lib/core/utils/internal/isIterable.mjs +1 -0
- package/lib/core/utils/internal/isIterable.mjs.map +1 -0
- package/lib/core/utils/internal/isObject.d.mts +6 -0
- package/lib/core/utils/internal/isObject.js +1 -0
- package/lib/core/utils/internal/isObject.js.map +1 -0
- package/lib/core/utils/internal/isObject.mjs +1 -0
- package/lib/core/utils/internal/isObject.mjs.map +1 -0
- package/lib/core/utils/internal/isStringEqual.d.mts +6 -0
- package/lib/core/utils/internal/isStringEqual.js +1 -0
- package/lib/core/utils/internal/isStringEqual.js.map +1 -0
- package/lib/core/utils/internal/isStringEqual.mjs +1 -0
- package/lib/core/utils/internal/isStringEqual.mjs.map +1 -0
- package/lib/core/utils/internal/jsonParse.d.mts +7 -0
- package/lib/core/utils/internal/jsonParse.js +1 -0
- package/lib/core/utils/internal/jsonParse.js.map +1 -0
- package/lib/core/utils/internal/jsonParse.mjs +1 -0
- package/lib/core/utils/internal/jsonParse.mjs.map +1 -0
- package/lib/core/utils/internal/mergeRight.d.mts +7 -0
- package/lib/core/utils/internal/mergeRight.js +1 -0
- package/lib/core/utils/internal/mergeRight.js.map +1 -0
- package/lib/core/utils/internal/mergeRight.mjs +1 -0
- package/lib/core/utils/internal/mergeRight.mjs.map +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.d.mts +5 -0
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
- package/lib/core/utils/internal/parseGraphQLRequest.js +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.mjs +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +1 -0
- package/lib/core/utils/internal/parseMultipartData.d.mts +10 -0
- package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
- package/lib/core/utils/internal/parseMultipartData.js +1 -0
- package/lib/core/utils/internal/parseMultipartData.js.map +1 -0
- package/lib/core/utils/internal/parseMultipartData.mjs +1 -0
- package/lib/core/utils/internal/parseMultipartData.mjs.map +1 -0
- package/lib/core/utils/internal/pipeEvents.d.mts +8 -0
- package/lib/core/utils/internal/pipeEvents.js +1 -0
- package/lib/core/utils/internal/pipeEvents.js.map +1 -0
- package/lib/core/utils/internal/pipeEvents.mjs +1 -0
- package/lib/core/utils/internal/pipeEvents.mjs.map +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.d.mts +8 -0
- package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.js +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.js.map +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.mjs +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.mjs.map +1 -0
- package/lib/core/utils/internal/toReadonlyArray.d.mts +6 -0
- package/lib/core/utils/internal/toReadonlyArray.js +1 -0
- package/lib/core/utils/internal/toReadonlyArray.js.map +1 -0
- package/lib/core/utils/internal/toReadonlyArray.mjs +1 -0
- package/lib/core/utils/internal/toReadonlyArray.mjs.map +1 -0
- package/lib/core/utils/internal/tryCatch.d.mts +3 -0
- package/lib/core/utils/internal/tryCatch.js +1 -0
- package/lib/core/utils/internal/tryCatch.js.map +1 -0
- package/lib/core/utils/internal/tryCatch.mjs +1 -0
- package/lib/core/utils/internal/tryCatch.mjs.map +1 -0
- package/lib/core/utils/internal/uuidv4.d.mts +3 -0
- package/lib/core/utils/internal/uuidv4.js +1 -0
- package/lib/core/utils/internal/uuidv4.js.map +1 -0
- package/lib/core/utils/internal/uuidv4.mjs +1 -0
- package/lib/core/utils/internal/uuidv4.mjs.map +1 -0
- package/lib/core/utils/logging/getStatusCodeColor.d.mts +11 -0
- package/lib/core/utils/logging/getStatusCodeColor.js +1 -0
- package/lib/core/utils/logging/getStatusCodeColor.js.map +1 -0
- package/lib/core/utils/logging/getStatusCodeColor.mjs +1 -0
- package/lib/core/utils/logging/getStatusCodeColor.mjs.map +1 -0
- package/lib/core/utils/logging/getTimestamp.d.mts +6 -0
- package/lib/core/utils/logging/getTimestamp.js +1 -0
- package/lib/core/utils/logging/getTimestamp.js.map +1 -0
- package/lib/core/utils/logging/getTimestamp.mjs +1 -0
- package/lib/core/utils/logging/getTimestamp.mjs.map +1 -0
- package/lib/core/utils/logging/serializeRequest.d.mts +12 -0
- package/lib/core/utils/logging/serializeRequest.d.ts +1 -1
- package/lib/core/utils/logging/serializeRequest.js +1 -0
- package/lib/core/utils/logging/serializeRequest.js.map +1 -0
- package/lib/core/utils/logging/serializeRequest.mjs +1 -0
- package/lib/core/utils/logging/serializeRequest.mjs.map +1 -0
- package/lib/core/utils/logging/serializeResponse.d.mts +9 -0
- package/lib/core/utils/logging/serializeResponse.d.ts +1 -1
- package/lib/core/utils/logging/serializeResponse.js +1 -0
- package/lib/core/utils/logging/serializeResponse.js.map +1 -0
- package/lib/core/utils/logging/serializeResponse.mjs +1 -0
- package/lib/core/utils/logging/serializeResponse.mjs.map +1 -0
- package/lib/core/utils/matching/matchRequestUrl.d.mts +19 -0
- package/lib/core/utils/matching/matchRequestUrl.d.ts +1 -1
- package/lib/core/utils/matching/matchRequestUrl.js +1 -0
- package/lib/core/utils/matching/matchRequestUrl.js.map +1 -0
- package/lib/core/utils/matching/matchRequestUrl.mjs +1 -0
- package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -0
- package/lib/core/utils/matching/normalizePath.d.mts +12 -0
- package/lib/core/utils/matching/normalizePath.js +1 -0
- package/lib/core/utils/matching/normalizePath.js.map +1 -0
- package/lib/core/utils/matching/normalizePath.mjs +1 -0
- package/lib/core/utils/matching/normalizePath.mjs.map +1 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.d.mts +7 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.js +1 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.js.map +1 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.mjs +1 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.mjs.map +1 -0
- package/lib/core/utils/request/getRequestCookies.d.mts +8 -0
- package/lib/core/utils/request/getRequestCookies.js +1 -0
- package/lib/core/utils/request/getRequestCookies.js.map +1 -0
- package/lib/core/utils/request/getRequestCookies.mjs +1 -0
- package/lib/core/utils/request/getRequestCookies.mjs.map +1 -0
- package/lib/core/utils/request/onUnhandledRequest.d.mts +12 -0
- package/lib/core/utils/request/onUnhandledRequest.d.ts +2 -2
- package/lib/core/utils/request/onUnhandledRequest.js +1 -0
- package/lib/core/utils/request/onUnhandledRequest.js.map +1 -0
- package/lib/core/utils/request/onUnhandledRequest.mjs +1 -0
- package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -0
- package/lib/core/utils/request/readResponseCookies.d.mts +3 -0
- package/lib/core/utils/request/readResponseCookies.js +1 -0
- package/lib/core/utils/request/readResponseCookies.js.map +1 -0
- package/lib/core/utils/request/readResponseCookies.mjs +1 -0
- package/lib/core/utils/request/readResponseCookies.mjs.map +1 -0
- package/lib/core/utils/toResponseInit.d.mts +3 -0
- package/lib/core/utils/toResponseInit.js +1 -0
- package/lib/core/utils/toResponseInit.js.map +1 -0
- package/lib/core/utils/toResponseInit.mjs +1 -0
- package/lib/core/utils/toResponseInit.mjs.map +1 -0
- package/lib/core/utils/url/cleanUrl.d.mts +7 -0
- package/lib/core/utils/url/cleanUrl.js +1 -0
- package/lib/core/utils/url/cleanUrl.js.map +1 -0
- package/lib/core/utils/url/cleanUrl.mjs +1 -0
- package/lib/core/utils/url/cleanUrl.mjs.map +1 -0
- package/lib/core/utils/url/getAbsoluteUrl.d.mts +6 -0
- package/lib/core/utils/url/getAbsoluteUrl.js +1 -0
- package/lib/core/utils/url/getAbsoluteUrl.js.map +1 -0
- package/lib/core/utils/url/getAbsoluteUrl.mjs +1 -0
- package/lib/core/utils/url/getAbsoluteUrl.mjs.map +1 -0
- package/lib/core/utils/url/isAbsoluteUrl.d.mts +6 -0
- package/lib/core/utils/url/isAbsoluteUrl.js +1 -0
- package/lib/core/utils/url/isAbsoluteUrl.js.map +1 -0
- package/lib/core/utils/url/isAbsoluteUrl.mjs +1 -0
- package/lib/core/utils/url/isAbsoluteUrl.mjs.map +1 -0
- package/lib/iife/index.js +59 -25
- package/lib/iife/index.js.map +1 -0
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.d.mts +75 -0
- package/lib/native/index.d.ts +0 -1
- package/lib/native/index.js +1 -3
- package/lib/native/index.js.map +1 -0
- package/lib/native/index.mjs +1 -3
- package/lib/native/index.mjs.map +1 -0
- package/lib/node/index.d.mts +75 -0
- package/lib/node/index.d.ts +1 -2
- package/lib/node/index.js +2 -35
- package/lib/node/index.js.map +1 -0
- package/lib/node/index.mjs +2 -35
- package/lib/node/index.mjs.map +1 -0
- package/package.json +6 -5
- package/src/browser/global.browser.d.ts +1 -0
- package/src/browser/index.ts +3 -0
- package/src/browser/setupWorker/glossary.ts +259 -0
- package/src/browser/setupWorker/setupWorker.node.test.ts +10 -0
- package/src/browser/setupWorker/setupWorker.ts +199 -0
- package/src/browser/setupWorker/start/createFallbackRequestListener.ts +67 -0
- package/src/browser/setupWorker/start/createFallbackStart.ts +21 -0
- package/src/browser/setupWorker/start/createRequestListener.ts +127 -0
- package/src/browser/setupWorker/start/createResponseListener.ts +58 -0
- package/src/browser/setupWorker/start/createStartHandler.ts +143 -0
- package/src/browser/setupWorker/start/utils/createMessageChannel.ts +32 -0
- package/src/browser/setupWorker/start/utils/enableMocking.ts +32 -0
- package/src/browser/setupWorker/start/utils/getWorkerByRegistration.ts +25 -0
- package/src/browser/setupWorker/start/utils/getWorkerInstance.ts +94 -0
- package/src/browser/setupWorker/start/utils/prepareStartHandler.test.ts +59 -0
- package/src/browser/setupWorker/start/utils/prepareStartHandler.ts +44 -0
- package/src/browser/setupWorker/start/utils/printStartMessage.test.ts +84 -0
- package/src/browser/setupWorker/start/utils/printStartMessage.ts +40 -0
- package/src/browser/setupWorker/start/utils/validateWorkerScope.ts +18 -0
- package/src/browser/setupWorker/stop/createFallbackStop.ts +11 -0
- package/src/browser/setupWorker/stop/createStop.ts +29 -0
- package/src/browser/setupWorker/stop/utils/printStopMessage.test.ts +26 -0
- package/src/browser/setupWorker/stop/utils/printStopMessage.ts +12 -0
- package/src/browser/tsconfig.browser.build.json +6 -0
- package/src/browser/tsconfig.browser.json +9 -0
- package/src/browser/utils/deferNetworkRequestsUntil.test.ts +48 -0
- package/src/browser/utils/deferNetworkRequestsUntil.ts +29 -0
- package/src/browser/utils/getAbsoluteWorkerUrl.test.ts +31 -0
- package/src/browser/utils/getAbsoluteWorkerUrl.ts +7 -0
- package/src/browser/utils/parseWorkerRequest.ts +15 -0
- package/src/browser/utils/pruneGetRequestBody.test.ts +53 -0
- package/src/browser/utils/pruneGetRequestBody.ts +21 -0
- package/src/browser/utils/requestIntegrityCheck.ts +23 -0
- package/src/browser/utils/supportsReadableStreamTransfer.ts +17 -0
- package/src/core/HttpResponse.test.ts +200 -0
- package/src/core/HttpResponse.ts +134 -0
- package/src/core/SetupApi.ts +95 -0
- package/src/core/bypass.test.ts +47 -0
- package/src/core/bypass.ts +36 -0
- package/src/core/delay.ts +70 -0
- package/src/core/graphql.test.ts +11 -0
- package/src/core/graphql.ts +138 -0
- package/src/core/handlers/GraphQLHandler.test.ts +820 -0
- package/src/core/handlers/GraphQLHandler.ts +263 -0
- package/src/core/handlers/HttpHandler.test.ts +218 -0
- package/src/core/handlers/HttpHandler.ts +169 -0
- package/src/core/handlers/RequestHandler.ts +338 -0
- package/src/core/http.test.ts +15 -0
- package/src/core/http.ts +51 -0
- package/src/core/index.ts +56 -0
- package/src/core/passthrough.test.ts +13 -0
- package/src/core/passthrough.ts +23 -0
- package/src/core/sharedOptions.ts +66 -0
- package/src/core/typeUtils.ts +20 -0
- package/src/core/utils/HttpResponse/decorators.ts +56 -0
- package/src/core/utils/getResponse.ts +55 -0
- package/src/core/utils/handleRequest.test.ts +554 -0
- package/src/core/utils/handleRequest.ts +132 -0
- package/src/core/utils/internal/Disposable.ts +9 -0
- package/src/core/utils/internal/checkGlobals.ts +17 -0
- package/src/core/utils/internal/devUtils.ts +31 -0
- package/src/core/utils/internal/getCallFrame.test.ts +154 -0
- package/src/core/utils/internal/getCallFrame.ts +35 -0
- package/src/core/utils/internal/isIterable.test.ts +23 -0
- package/src/core/utils/internal/isIterable.ts +12 -0
- package/src/core/utils/internal/isObject.test.ts +20 -0
- package/src/core/utils/internal/isObject.ts +6 -0
- package/src/core/utils/internal/isStringEqual.test.ts +45 -0
- package/src/core/utils/internal/isStringEqual.ts +6 -0
- package/src/core/utils/internal/jsonParse.test.ts +13 -0
- package/src/core/utils/internal/jsonParse.ts +13 -0
- package/src/core/utils/internal/mergeRight.test.ts +43 -0
- package/src/core/utils/internal/mergeRight.ts +27 -0
- package/src/core/utils/internal/parseGraphQLRequest.test.ts +99 -0
- package/src/core/utils/internal/parseGraphQLRequest.ts +205 -0
- package/src/core/utils/internal/parseMultipartData.test.ts +76 -0
- package/src/core/utils/internal/parseMultipartData.ts +104 -0
- package/src/core/utils/internal/pipeEvents.test.ts +14 -0
- package/src/core/utils/internal/pipeEvents.ts +25 -0
- package/src/core/utils/internal/requestHandlerUtils.ts +21 -0
- package/src/core/utils/internal/toReadonlyArray.test.ts +30 -0
- package/src/core/utils/internal/toReadonlyArray.ts +8 -0
- package/src/core/utils/internal/tryCatch.test.ts +29 -0
- package/src/core/utils/internal/tryCatch.ts +11 -0
- package/src/core/utils/internal/uuidv4.ts +3 -0
- package/src/core/utils/logging/getStatusCodeColor.test.ts +22 -0
- package/src/core/utils/logging/getStatusCodeColor.ts +20 -0
- package/src/core/utils/logging/getTimestamp.test.ts +18 -0
- package/src/core/utils/logging/getTimestamp.ts +12 -0
- package/src/core/utils/logging/serializeRequest.test.ts +23 -0
- package/src/core/utils/logging/serializeRequest.ts +23 -0
- package/src/core/utils/logging/serializeResponse.test.ts +77 -0
- package/src/core/utils/logging/serializeResponse.ts +31 -0
- package/src/core/utils/matching/matchRequestUrl.test.ts +114 -0
- package/src/core/utils/matching/matchRequestUrl.ts +73 -0
- package/src/core/utils/matching/normalizePath.node.test.ts +44 -0
- package/src/core/utils/matching/normalizePath.test.ts +50 -0
- package/src/core/utils/matching/normalizePath.ts +21 -0
- package/src/core/utils/request/getPublicUrlFromRequest.test.ts +26 -0
- package/src/core/utils/request/getPublicUrlFromRequest.ts +15 -0
- package/src/core/utils/request/getRequestCookies.node.test.ts +29 -0
- package/src/core/utils/request/getRequestCookies.test.ts +64 -0
- package/src/core/utils/request/getRequestCookies.ts +75 -0
- package/src/core/utils/request/onUnhandledRequest.test.ts +215 -0
- package/src/core/utils/request/onUnhandledRequest.ts +247 -0
- package/src/core/utils/request/readResponseCookies.ts +9 -0
- package/src/core/utils/toResponseInit.ts +7 -0
- package/src/core/utils/url/cleanUrl.test.ts +17 -0
- package/src/core/utils/url/cleanUrl.ts +12 -0
- package/src/core/utils/url/getAbsoluteUrl.node.test.ts +19 -0
- package/src/core/utils/url/getAbsoluteUrl.test.ts +29 -0
- package/src/core/utils/url/getAbsoluteUrl.ts +26 -0
- package/src/core/utils/url/isAbsoluteUrl.test.ts +32 -0
- package/src/core/utils/url/isAbsoluteUrl.ts +6 -0
- package/src/iife/index.ts +2 -0
- package/src/mockServiceWorker.js +287 -0
- package/src/native/index.ts +17 -0
- package/src/node/SetupServerApi.ts +113 -0
- package/src/node/glossary.ts +62 -0
- package/src/node/index.ts +3 -0
- package/src/node/setupServer.ts +21 -0
- package/src/node/utils/isNodeExceptionLike.ts +14 -0
- package/src/tsconfig.core.build.json +6 -0
- package/src/tsconfig.node.build.json +6 -0
- package/src/tsconfig.node.json +8 -0
- package/src/tsconfig.src.json +15 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ServiceWorkerIncomingEventsMap,
|
|
3
|
+
SetupWorkerInternalContext,
|
|
4
|
+
} from '../glossary'
|
|
5
|
+
import { ServiceWorkerMessage } from './utils/createMessageChannel'
|
|
6
|
+
import { isResponseWithoutBody } from '@mswjs/interceptors'
|
|
7
|
+
|
|
8
|
+
export function createResponseListener(context: SetupWorkerInternalContext) {
|
|
9
|
+
return (
|
|
10
|
+
_: MessageEvent,
|
|
11
|
+
message: ServiceWorkerMessage<
|
|
12
|
+
'RESPONSE',
|
|
13
|
+
ServiceWorkerIncomingEventsMap['RESPONSE']
|
|
14
|
+
>,
|
|
15
|
+
) => {
|
|
16
|
+
const { payload: responseJson } = message
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* CORS requests with `mode: "no-cors"` result in "opaque" responses.
|
|
20
|
+
* That kind of responses cannot be manipulated in JavaScript due
|
|
21
|
+
* to the security considerations.
|
|
22
|
+
* @see https://fetch.spec.whatwg.org/#concept-filtered-response-opaque
|
|
23
|
+
* @see https://github.com/mswjs/msw/issues/529
|
|
24
|
+
*/
|
|
25
|
+
if (responseJson.type?.includes('opaque')) {
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const response =
|
|
30
|
+
responseJson.status === 0
|
|
31
|
+
? Response.error()
|
|
32
|
+
: new Response(
|
|
33
|
+
/**
|
|
34
|
+
* Responses may be streams here, but when we create a response object
|
|
35
|
+
* with null-body status codes, like 204, 205, 304 Response will
|
|
36
|
+
* throw when passed a non-null body, so ensure it's null here
|
|
37
|
+
* for those codes
|
|
38
|
+
*/
|
|
39
|
+
isResponseWithoutBody(responseJson.status)
|
|
40
|
+
? null
|
|
41
|
+
: responseJson.body,
|
|
42
|
+
responseJson,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
context.emitter.emit(
|
|
46
|
+
responseJson.isMockedResponse ? 'response:mocked' : 'response:bypass',
|
|
47
|
+
{
|
|
48
|
+
response,
|
|
49
|
+
/**
|
|
50
|
+
* @todo @fixme In this context, we don't know anything about
|
|
51
|
+
* the request.
|
|
52
|
+
*/
|
|
53
|
+
request: null as any,
|
|
54
|
+
requestId: responseJson.requestId,
|
|
55
|
+
},
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { until } from '@open-draft/until'
|
|
2
|
+
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
3
|
+
import { getWorkerInstance } from './utils/getWorkerInstance'
|
|
4
|
+
import { enableMocking } from './utils/enableMocking'
|
|
5
|
+
import { SetupWorkerInternalContext, StartHandler } from '../glossary'
|
|
6
|
+
import { createRequestListener } from './createRequestListener'
|
|
7
|
+
import { requestIntegrityCheck } from '../../utils/requestIntegrityCheck'
|
|
8
|
+
import { deferNetworkRequestsUntil } from '../../utils/deferNetworkRequestsUntil'
|
|
9
|
+
import { createResponseListener } from './createResponseListener'
|
|
10
|
+
import { validateWorkerScope } from './utils/validateWorkerScope'
|
|
11
|
+
|
|
12
|
+
export const createStartHandler = (
|
|
13
|
+
context: SetupWorkerInternalContext,
|
|
14
|
+
): StartHandler => {
|
|
15
|
+
return function start(options, customOptions) {
|
|
16
|
+
const startWorkerInstance = async () => {
|
|
17
|
+
// Remove all previously existing event listeners.
|
|
18
|
+
// This way none of the listeners persists between Fast refresh
|
|
19
|
+
// of the application's code.
|
|
20
|
+
context.events.removeAllListeners()
|
|
21
|
+
|
|
22
|
+
// Handle requests signaled by the worker.
|
|
23
|
+
context.workerChannel.on(
|
|
24
|
+
'REQUEST',
|
|
25
|
+
createRequestListener(context, options),
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
// Handle responses signaled by the worker.
|
|
29
|
+
context.workerChannel.on('RESPONSE', createResponseListener(context))
|
|
30
|
+
|
|
31
|
+
const instance = await getWorkerInstance(
|
|
32
|
+
options.serviceWorker.url,
|
|
33
|
+
options.serviceWorker.options,
|
|
34
|
+
options.findWorker,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
const [worker, registration] = instance
|
|
38
|
+
|
|
39
|
+
if (!worker) {
|
|
40
|
+
const missingWorkerMessage = customOptions?.findWorker
|
|
41
|
+
? devUtils.formatMessage(
|
|
42
|
+
`Failed to locate the Service Worker registration using a custom "findWorker" predicate.
|
|
43
|
+
|
|
44
|
+
Please ensure that the custom predicate properly locates the Service Worker registration at "%s".
|
|
45
|
+
More details: https://mswjs.io/docs/api/setup-worker/start#findworker
|
|
46
|
+
`,
|
|
47
|
+
options.serviceWorker.url,
|
|
48
|
+
)
|
|
49
|
+
: devUtils.formatMessage(
|
|
50
|
+
`Failed to locate the Service Worker registration.
|
|
51
|
+
|
|
52
|
+
This most likely means that the worker script URL "%s" cannot resolve against the actual public hostname (%s). This may happen if your application runs behind a proxy, or has a dynamic hostname.
|
|
53
|
+
|
|
54
|
+
Please consider using a custom "serviceWorker.url" option to point to the actual worker script location, or a custom "findWorker" option to resolve the Service Worker registration manually. More details: https://mswjs.io/docs/api/setup-worker/start`,
|
|
55
|
+
options.serviceWorker.url,
|
|
56
|
+
location.host,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
throw new Error(missingWorkerMessage)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
context.worker = worker
|
|
63
|
+
context.registration = registration
|
|
64
|
+
|
|
65
|
+
context.events.addListener(window, 'beforeunload', () => {
|
|
66
|
+
if (worker.state !== 'redundant') {
|
|
67
|
+
// Notify the Service Worker that this client has closed.
|
|
68
|
+
// Internally, it's similar to disabling the mocking, only
|
|
69
|
+
// client close event has a handler that self-terminates
|
|
70
|
+
// the Service Worker when there are no open clients.
|
|
71
|
+
context.workerChannel.send('CLIENT_CLOSED')
|
|
72
|
+
}
|
|
73
|
+
// Make sure we're always clearing the interval - there are reports that not doing this can
|
|
74
|
+
// cause memory leaks in headless browser environments.
|
|
75
|
+
window.clearInterval(context.keepAliveInterval)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
// Check if the active Service Worker is the latest published one
|
|
79
|
+
const integrityCheckResult = await until(() =>
|
|
80
|
+
requestIntegrityCheck(context, worker),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
if (integrityCheckResult.error) {
|
|
84
|
+
devUtils.error(`\
|
|
85
|
+
Detected outdated Service Worker: ${integrityCheckResult.error.message}
|
|
86
|
+
|
|
87
|
+
The mocking is still enabled, but it's highly recommended that you update your Service Worker by running:
|
|
88
|
+
|
|
89
|
+
$ npx msw init <PUBLIC_DIR>
|
|
90
|
+
|
|
91
|
+
This is necessary to ensure that the Service Worker is in sync with the library to guarantee its stability.
|
|
92
|
+
If this message still persists after updating, please report an issue: https://github.com/open-draft/msw/issues\
|
|
93
|
+
`)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
context.keepAliveInterval = window.setInterval(
|
|
97
|
+
() => context.workerChannel.send('KEEPALIVE_REQUEST'),
|
|
98
|
+
5000,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
// Warn the user when loading the page that lies outside
|
|
102
|
+
// of the worker's scope.
|
|
103
|
+
validateWorkerScope(registration, context.startOptions)
|
|
104
|
+
|
|
105
|
+
return registration
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const workerRegistration = startWorkerInstance().then(
|
|
109
|
+
async (registration) => {
|
|
110
|
+
const pendingInstance = registration.installing || registration.waiting
|
|
111
|
+
|
|
112
|
+
// Wait until the worker is activated.
|
|
113
|
+
// Assume the worker is already activated if there's no pending registration
|
|
114
|
+
// (i.e. when reloading the page after a successful activation).
|
|
115
|
+
if (pendingInstance) {
|
|
116
|
+
await new Promise<void>((resolve) => {
|
|
117
|
+
pendingInstance.addEventListener('statechange', () => {
|
|
118
|
+
if (pendingInstance.state === 'activated') {
|
|
119
|
+
return resolve()
|
|
120
|
+
}
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Print the activation message only after the worker has been activated.
|
|
126
|
+
await enableMocking(context, options).catch((error) => {
|
|
127
|
+
throw new Error(`Failed to enable mocking: ${error?.message}`)
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
return registration
|
|
131
|
+
},
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
// Defer any network requests until the Service Worker instance is ready.
|
|
135
|
+
// This prevents a race condition between the Service Worker registration
|
|
136
|
+
// and application's runtime requests (i.e. requests on mount).
|
|
137
|
+
if (options.waitUntilReady) {
|
|
138
|
+
deferNetworkRequestsUntil(workerRegistration)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return workerRegistration
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
StringifiedResponse,
|
|
3
|
+
ServiceWorkerIncomingEventsMap,
|
|
4
|
+
} from '../../glossary'
|
|
5
|
+
|
|
6
|
+
export interface ServiceWorkerMessage<
|
|
7
|
+
EventType extends keyof ServiceWorkerIncomingEventsMap,
|
|
8
|
+
EventPayload,
|
|
9
|
+
> {
|
|
10
|
+
type: EventType
|
|
11
|
+
payload: EventPayload
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface WorkerChannelEventsMap {
|
|
15
|
+
MOCK_RESPONSE: [
|
|
16
|
+
data: StringifiedResponse,
|
|
17
|
+
transfer?: [ReadableStream<Uint8Array>],
|
|
18
|
+
]
|
|
19
|
+
NOT_FOUND: []
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class WorkerChannel {
|
|
23
|
+
constructor(private readonly port: MessagePort) {}
|
|
24
|
+
|
|
25
|
+
public postMessage<Event extends keyof WorkerChannelEventsMap>(
|
|
26
|
+
event: Event,
|
|
27
|
+
...rest: WorkerChannelEventsMap[Event]
|
|
28
|
+
): void {
|
|
29
|
+
const [data, transfer] = rest
|
|
30
|
+
this.port.postMessage({ type: event, data }, { transfer })
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
2
|
+
import { StartOptions, SetupWorkerInternalContext } from '../../glossary'
|
|
3
|
+
import { printStartMessage } from './printStartMessage'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Signals the worker to enable the interception of requests.
|
|
7
|
+
*/
|
|
8
|
+
export async function enableMocking(
|
|
9
|
+
context: SetupWorkerInternalContext,
|
|
10
|
+
options: StartOptions,
|
|
11
|
+
) {
|
|
12
|
+
context.workerChannel.send('MOCK_ACTIVATE')
|
|
13
|
+
await context.events.once('MOCKING_ENABLED')
|
|
14
|
+
|
|
15
|
+
// Warn the developer on multiple "worker.start()" calls.
|
|
16
|
+
// While this will not affect the worker in any way,
|
|
17
|
+
// it likely indicates an issue with the developer's code.
|
|
18
|
+
if (context.isMockingEnabled) {
|
|
19
|
+
devUtils.warn(
|
|
20
|
+
`Found a redundant "worker.start()" call. Note that starting the worker while mocking is already enabled will have no effect. Consider removing this "worker.start()" call.`,
|
|
21
|
+
)
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
context.isMockingEnabled = true
|
|
26
|
+
|
|
27
|
+
printStartMessage({
|
|
28
|
+
quiet: options.quiet,
|
|
29
|
+
workerScope: context.registration?.scope,
|
|
30
|
+
workerUrl: context.worker?.scriptURL,
|
|
31
|
+
})
|
|
32
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { FindWorker } from '../../glossary'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Attempts to resolve a Service Worker instance from a given registration,
|
|
5
|
+
* regardless of its state (active, installing, waiting).
|
|
6
|
+
*/
|
|
7
|
+
export function getWorkerByRegistration(
|
|
8
|
+
registration: ServiceWorkerRegistration,
|
|
9
|
+
absoluteWorkerUrl: string,
|
|
10
|
+
findWorker: FindWorker,
|
|
11
|
+
): ServiceWorker | null {
|
|
12
|
+
const allStates = [
|
|
13
|
+
registration.active,
|
|
14
|
+
registration.installing,
|
|
15
|
+
registration.waiting,
|
|
16
|
+
]
|
|
17
|
+
const relevantStates = allStates.filter((state): state is ServiceWorker => {
|
|
18
|
+
return state != null
|
|
19
|
+
})
|
|
20
|
+
const worker = relevantStates.find((worker) => {
|
|
21
|
+
return findWorker(worker.scriptURL, absoluteWorkerUrl)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
return worker || null
|
|
25
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { until } from '@open-draft/until'
|
|
2
|
+
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
3
|
+
import { getAbsoluteWorkerUrl } from '../../../utils/getAbsoluteWorkerUrl'
|
|
4
|
+
import { getWorkerByRegistration } from './getWorkerByRegistration'
|
|
5
|
+
import { ServiceWorkerInstanceTuple, FindWorker } from '../../glossary'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns an active Service Worker instance.
|
|
9
|
+
* When not found, registers a new Service Worker.
|
|
10
|
+
*/
|
|
11
|
+
export const getWorkerInstance = async (
|
|
12
|
+
url: string,
|
|
13
|
+
options: RegistrationOptions = {},
|
|
14
|
+
findWorker: FindWorker,
|
|
15
|
+
): Promise<ServiceWorkerInstanceTuple> => {
|
|
16
|
+
// Resolve the absolute Service Worker URL.
|
|
17
|
+
const absoluteWorkerUrl = getAbsoluteWorkerUrl(url)
|
|
18
|
+
|
|
19
|
+
const mockRegistrations = await navigator.serviceWorker
|
|
20
|
+
.getRegistrations()
|
|
21
|
+
.then((registrations) =>
|
|
22
|
+
registrations.filter((registration) =>
|
|
23
|
+
getWorkerByRegistration(registration, absoluteWorkerUrl, findWorker),
|
|
24
|
+
),
|
|
25
|
+
)
|
|
26
|
+
if (!navigator.serviceWorker.controller && mockRegistrations.length > 0) {
|
|
27
|
+
// Reload the page when it has associated workers, but no active controller.
|
|
28
|
+
// The absence of a controller can mean either:
|
|
29
|
+
// - page has no Service Worker associated with it
|
|
30
|
+
// - page has been hard-reloaded and its workers won't be used until the next reload.
|
|
31
|
+
// Since we've checked that there are registrations associated with this page,
|
|
32
|
+
// at this point we are sure it's hard reload that falls into this clause.
|
|
33
|
+
location.reload()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const [existingRegistration] = mockRegistrations
|
|
37
|
+
|
|
38
|
+
if (existingRegistration) {
|
|
39
|
+
// When the Service Worker is registered, update it and return the reference.
|
|
40
|
+
return existingRegistration.update().then(() => {
|
|
41
|
+
return [
|
|
42
|
+
getWorkerByRegistration(
|
|
43
|
+
existingRegistration,
|
|
44
|
+
absoluteWorkerUrl,
|
|
45
|
+
findWorker,
|
|
46
|
+
),
|
|
47
|
+
existingRegistration,
|
|
48
|
+
]
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// When the Service Worker wasn't found, register it anew and return the reference.
|
|
53
|
+
const registrationResult = await until<Error, ServiceWorkerInstanceTuple>(
|
|
54
|
+
async () => {
|
|
55
|
+
const registration = await navigator.serviceWorker.register(url, options)
|
|
56
|
+
return [
|
|
57
|
+
// Compare existing worker registration by its worker URL,
|
|
58
|
+
// to prevent irrelevant workers to resolve here (such as Codesandbox worker).
|
|
59
|
+
getWorkerByRegistration(registration, absoluteWorkerUrl, findWorker),
|
|
60
|
+
registration,
|
|
61
|
+
]
|
|
62
|
+
},
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
// Handle Service Worker registration errors.
|
|
66
|
+
if (registrationResult.error) {
|
|
67
|
+
const isWorkerMissing = registrationResult.error.message.includes('(404)')
|
|
68
|
+
|
|
69
|
+
// Produce a custom error message when given a non-existing Service Worker url.
|
|
70
|
+
// Suggest developers to check their setup.
|
|
71
|
+
if (isWorkerMissing) {
|
|
72
|
+
const scopeUrl = new URL(options?.scope || '/', location.href)
|
|
73
|
+
|
|
74
|
+
throw new Error(
|
|
75
|
+
devUtils.formatMessage(`\
|
|
76
|
+
Failed to register a Service Worker for scope ('${scopeUrl.href}') with script ('${absoluteWorkerUrl}'): Service Worker script does not exist at the given path.
|
|
77
|
+
|
|
78
|
+
Did you forget to run "npx msw init <PUBLIC_DIR>"?
|
|
79
|
+
|
|
80
|
+
Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/init`),
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Fallback error message for any other registration errors.
|
|
85
|
+
throw new Error(
|
|
86
|
+
devUtils.formatMessage(
|
|
87
|
+
'Failed to register the Service Worker:\n\n%s',
|
|
88
|
+
registrationResult.error.message,
|
|
89
|
+
),
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return registrationResult.data
|
|
94
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { SetupWorkerInternalContext, StartOptions } from '../../glossary'
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_START_OPTIONS,
|
|
4
|
+
resolveStartOptions,
|
|
5
|
+
prepareStartHandler,
|
|
6
|
+
} from './prepareStartHandler'
|
|
7
|
+
|
|
8
|
+
describe('resolveStartOptions', () => {
|
|
9
|
+
test('returns default options given no custom start options', () => {
|
|
10
|
+
expect(resolveStartOptions()).toEqual(DEFAULT_START_OPTIONS)
|
|
11
|
+
expect(resolveStartOptions(undefined)).toEqual(DEFAULT_START_OPTIONS)
|
|
12
|
+
expect(resolveStartOptions({})).toEqual(DEFAULT_START_OPTIONS)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
test('deeply merges the default and custom start options', () => {
|
|
16
|
+
expect(
|
|
17
|
+
resolveStartOptions({
|
|
18
|
+
quiet: true,
|
|
19
|
+
serviceWorker: {
|
|
20
|
+
url: './custom.js',
|
|
21
|
+
},
|
|
22
|
+
}),
|
|
23
|
+
).toEqual({
|
|
24
|
+
...DEFAULT_START_OPTIONS,
|
|
25
|
+
quiet: true,
|
|
26
|
+
serviceWorker: {
|
|
27
|
+
url: './custom.js',
|
|
28
|
+
options: null,
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
describe('prepareStartHandler', () => {
|
|
35
|
+
test('exposes resolved start options to the generated star handler', () => {
|
|
36
|
+
const createStartHandler = vi.fn()
|
|
37
|
+
const context: SetupWorkerInternalContext = {} as any
|
|
38
|
+
const startHandler = prepareStartHandler(createStartHandler, context)
|
|
39
|
+
expect(startHandler).toBeInstanceOf(Function)
|
|
40
|
+
|
|
41
|
+
const initialOptions: StartOptions = {
|
|
42
|
+
quiet: true,
|
|
43
|
+
serviceWorker: {
|
|
44
|
+
url: './custom.js',
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
const resolvedOptions = resolveStartOptions(initialOptions)
|
|
48
|
+
startHandler(initialOptions)
|
|
49
|
+
|
|
50
|
+
// Calls the handler creator with both resolved and initial options.
|
|
51
|
+
expect(createStartHandler).toHaveBeenCalledWith(
|
|
52
|
+
resolvedOptions,
|
|
53
|
+
initialOptions,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
// Sets the resolved options on the internal context.
|
|
57
|
+
expect(context).toHaveProperty('startOptions', resolvedOptions)
|
|
58
|
+
})
|
|
59
|
+
})
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { RequiredDeep } from '~/core/typeUtils'
|
|
2
|
+
import { mergeRight } from '~/core/utils/internal/mergeRight'
|
|
3
|
+
import {
|
|
4
|
+
SetupWorker,
|
|
5
|
+
SetupWorkerInternalContext,
|
|
6
|
+
StartHandler,
|
|
7
|
+
StartOptions,
|
|
8
|
+
} from '../../glossary'
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_START_OPTIONS: RequiredDeep<StartOptions> = {
|
|
11
|
+
serviceWorker: {
|
|
12
|
+
url: '/mockServiceWorker.js',
|
|
13
|
+
options: null as any,
|
|
14
|
+
},
|
|
15
|
+
quiet: false,
|
|
16
|
+
waitUntilReady: true,
|
|
17
|
+
onUnhandledRequest: 'warn',
|
|
18
|
+
findWorker(scriptURL, mockServiceWorkerUrl) {
|
|
19
|
+
return scriptURL === mockServiceWorkerUrl
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Returns resolved worker start options, merging the default options
|
|
25
|
+
* with the given custom options.
|
|
26
|
+
*/
|
|
27
|
+
export function resolveStartOptions(
|
|
28
|
+
initialOptions?: StartOptions,
|
|
29
|
+
): RequiredDeep<StartOptions> {
|
|
30
|
+
return mergeRight(
|
|
31
|
+
DEFAULT_START_OPTIONS,
|
|
32
|
+
initialOptions || {},
|
|
33
|
+
) as RequiredDeep<StartOptions>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function prepareStartHandler(
|
|
37
|
+
handler: StartHandler,
|
|
38
|
+
context: SetupWorkerInternalContext,
|
|
39
|
+
): SetupWorker['start'] {
|
|
40
|
+
return (initialOptions) => {
|
|
41
|
+
context.startOptions = resolveStartOptions(initialOptions)
|
|
42
|
+
return handler(context.startOptions, initialOptions || {})
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { printStartMessage } from './printStartMessage'
|
|
2
|
+
|
|
3
|
+
beforeEach(() => {
|
|
4
|
+
vi.spyOn(console, 'groupCollapsed').mockImplementation(() => void 0)
|
|
5
|
+
vi.spyOn(console, 'log').mockImplementation(() => void 0)
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
vi.restoreAllMocks()
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
test('prints out a default start message into console', () => {
|
|
13
|
+
printStartMessage({
|
|
14
|
+
workerScope: 'http://localhost:3000/',
|
|
15
|
+
workerUrl: 'http://localhost:3000/worker.js',
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
expect(console.groupCollapsed).toHaveBeenCalledWith(
|
|
19
|
+
'%c[MSW] Mocking enabled.',
|
|
20
|
+
expect.anything(),
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
// Includes a link to the documentation.
|
|
24
|
+
expect(console.log).toHaveBeenCalledWith(
|
|
25
|
+
'%cDocumentation: %chttps://mswjs.io/docs',
|
|
26
|
+
expect.anything(),
|
|
27
|
+
expect.anything(),
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
// Includes a link to the GitHub issues page.
|
|
31
|
+
expect(console.log).toHaveBeenCalledWith(
|
|
32
|
+
'Found an issue? https://github.com/mswjs/msw/issues',
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
// Includes service worker scope.
|
|
36
|
+
expect(console.log).toHaveBeenCalledWith(
|
|
37
|
+
'Worker scope:',
|
|
38
|
+
'http://localhost:3000/',
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
// Includes service worker script location.
|
|
42
|
+
expect(console.log).toHaveBeenCalledWith(
|
|
43
|
+
'Worker script URL:',
|
|
44
|
+
'http://localhost:3000/worker.js',
|
|
45
|
+
)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('supports printing a custom start message', () => {
|
|
49
|
+
printStartMessage({ message: 'Custom start message' })
|
|
50
|
+
|
|
51
|
+
expect(console.groupCollapsed).toHaveBeenCalledWith(
|
|
52
|
+
'%c[MSW] Custom start message',
|
|
53
|
+
expect.anything(),
|
|
54
|
+
)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('does not print any messages when log level is quiet', () => {
|
|
58
|
+
printStartMessage({ quiet: true })
|
|
59
|
+
|
|
60
|
+
expect(console.groupCollapsed).not.toHaveBeenCalled()
|
|
61
|
+
expect(console.log).not.toHaveBeenCalled()
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('prints a worker scope in the start message', () => {
|
|
65
|
+
printStartMessage({
|
|
66
|
+
workerScope: 'http://localhost:3000/user',
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
expect(console.log).toHaveBeenCalledWith(
|
|
70
|
+
'Worker scope:',
|
|
71
|
+
'http://localhost:3000/user',
|
|
72
|
+
)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('prints a worker script url in the start message', () => {
|
|
76
|
+
printStartMessage({
|
|
77
|
+
workerUrl: 'http://localhost:3000/mockServiceWorker.js',
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
expect(console.log).toHaveBeenCalledWith(
|
|
81
|
+
'Worker script URL:',
|
|
82
|
+
'http://localhost:3000/mockServiceWorker.js',
|
|
83
|
+
)
|
|
84
|
+
})
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
2
|
+
|
|
3
|
+
export interface PrintStartMessageArgs {
|
|
4
|
+
quiet?: boolean
|
|
5
|
+
message?: string
|
|
6
|
+
workerUrl?: string
|
|
7
|
+
workerScope?: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Prints a worker activation message in the browser's console.
|
|
12
|
+
*/
|
|
13
|
+
export function printStartMessage(args: PrintStartMessageArgs = {}) {
|
|
14
|
+
if (args.quiet) {
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const message = args.message || 'Mocking enabled.'
|
|
19
|
+
|
|
20
|
+
console.groupCollapsed(
|
|
21
|
+
`%c${devUtils.formatMessage(message)}`,
|
|
22
|
+
'color:orangered;font-weight:bold;',
|
|
23
|
+
)
|
|
24
|
+
console.log(
|
|
25
|
+
'%cDocumentation: %chttps://mswjs.io/docs',
|
|
26
|
+
'font-weight:bold',
|
|
27
|
+
'font-weight:normal',
|
|
28
|
+
)
|
|
29
|
+
console.log('Found an issue? https://github.com/mswjs/msw/issues')
|
|
30
|
+
|
|
31
|
+
if (args.workerUrl) {
|
|
32
|
+
console.log('Worker script URL:', args.workerUrl)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (args.workerScope) {
|
|
36
|
+
console.log('Worker scope:', args.workerScope)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
console.groupEnd()
|
|
40
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
2
|
+
import { StartOptions } from '../../glossary'
|
|
3
|
+
|
|
4
|
+
export function validateWorkerScope(
|
|
5
|
+
registration: ServiceWorkerRegistration,
|
|
6
|
+
options?: StartOptions,
|
|
7
|
+
): void {
|
|
8
|
+
if (!options?.quiet && !location.href.startsWith(registration.scope)) {
|
|
9
|
+
devUtils.warn(
|
|
10
|
+
`\
|
|
11
|
+
Cannot intercept requests on this page because it's outside of the worker's scope ("${registration.scope}"). If you wish to mock API requests on this page, you must resolve this scope issue.
|
|
12
|
+
|
|
13
|
+
- (Recommended) Register the worker at the root level ("/") of your application.
|
|
14
|
+
- Set the "Service-Worker-Allowed" response header to allow out-of-scope workers.\
|
|
15
|
+
`,
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SetupWorkerInternalContext, StopHandler } from '../glossary'
|
|
2
|
+
import { printStopMessage } from './utils/printStopMessage'
|
|
3
|
+
|
|
4
|
+
export function createFallbackStop(
|
|
5
|
+
context: SetupWorkerInternalContext,
|
|
6
|
+
): StopHandler {
|
|
7
|
+
return function stop() {
|
|
8
|
+
context.fallbackInterceptor?.dispose()
|
|
9
|
+
printStopMessage({ quiet: context.startOptions?.quiet })
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
2
|
+
import { SetupWorkerInternalContext, StopHandler } from '../glossary'
|
|
3
|
+
import { printStopMessage } from './utils/printStopMessage'
|
|
4
|
+
|
|
5
|
+
export const createStop = (
|
|
6
|
+
context: SetupWorkerInternalContext,
|
|
7
|
+
): StopHandler => {
|
|
8
|
+
return function stop() {
|
|
9
|
+
// Warn developers calling "worker.stop()" more times than necessary.
|
|
10
|
+
// This likely indicates a mistake in their code.
|
|
11
|
+
if (!context.isMockingEnabled) {
|
|
12
|
+
devUtils.warn(
|
|
13
|
+
'Found a redundant "worker.stop()" call. Note that stopping the worker while mocking already stopped has no effect. Consider removing this "worker.stop()" call.',
|
|
14
|
+
)
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Signal the Service Worker to disable mocking for this client.
|
|
20
|
+
* Use this an an explicit way to stop the mocking, while preserving
|
|
21
|
+
* the worker-client relation. Does not affect the worker's lifecycle.
|
|
22
|
+
*/
|
|
23
|
+
context.workerChannel.send('MOCK_DEACTIVATE')
|
|
24
|
+
context.isMockingEnabled = false
|
|
25
|
+
window.clearInterval(context.keepAliveInterval)
|
|
26
|
+
|
|
27
|
+
printStopMessage({ quiet: context.startOptions?.quiet })
|
|
28
|
+
}
|
|
29
|
+
}
|