msw 2.12.13 → 2.13.0
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 +29 -19
- package/lib/browser/index.d.ts +29 -19
- package/lib/browser/index.js +1763 -1321
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +1769 -1321
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/{HttpResponse-Dj6ibgFJ.d.ts → HttpResponse-CksOMVAa.d.ts} +5 -5
- package/lib/core/{HttpResponse-Be4eT3x6.d.mts → HttpResponse-DlRR1D-f.d.mts} +5 -5
- package/lib/core/HttpResponse.d.mts +1 -1
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/experimental/compat.d.mts +17 -0
- package/lib/core/experimental/compat.d.ts +17 -0
- package/lib/core/experimental/compat.js +54 -0
- package/lib/core/experimental/compat.js.map +1 -0
- package/lib/core/experimental/compat.mjs +36 -0
- package/lib/core/experimental/compat.mjs.map +1 -0
- package/lib/core/experimental/define-network.d.mts +75 -0
- package/lib/core/experimental/define-network.d.ts +75 -0
- package/lib/core/experimental/define-network.js +124 -0
- package/lib/core/experimental/define-network.js.map +1 -0
- package/lib/core/experimental/define-network.mjs +107 -0
- package/lib/core/experimental/define-network.mjs.map +1 -0
- package/lib/core/experimental/frames/http-frame.d.mts +77 -0
- package/lib/core/experimental/frames/http-frame.d.ts +77 -0
- package/lib/core/experimental/frames/http-frame.js +194 -0
- package/lib/core/experimental/frames/http-frame.js.map +1 -0
- package/lib/core/experimental/frames/http-frame.mjs +176 -0
- package/lib/core/experimental/frames/http-frame.mjs.map +1 -0
- package/lib/core/experimental/frames/network-frame.d.mts +12 -0
- package/lib/core/experimental/frames/network-frame.d.ts +12 -0
- package/lib/core/{handlers/common.js → experimental/frames/network-frame.js} +19 -3
- package/lib/core/experimental/frames/network-frame.js.map +1 -0
- package/lib/core/experimental/frames/network-frame.mjs +13 -0
- package/lib/core/experimental/frames/network-frame.mjs.map +1 -0
- package/lib/core/experimental/frames/websocket-frame.d.mts +55 -0
- package/lib/core/experimental/frames/websocket-frame.d.ts +55 -0
- package/lib/core/experimental/frames/websocket-frame.js +129 -0
- package/lib/core/experimental/frames/websocket-frame.js.map +1 -0
- package/lib/core/experimental/frames/websocket-frame.mjs +116 -0
- package/lib/core/experimental/frames/websocket-frame.mjs.map +1 -0
- package/lib/core/experimental/handlers-controller.d.mts +35 -0
- package/lib/core/experimental/handlers-controller.d.ts +35 -0
- package/lib/core/experimental/handlers-controller.js +121 -0
- package/lib/core/experimental/handlers-controller.js.map +1 -0
- package/lib/core/experimental/handlers-controller.mjs +101 -0
- package/lib/core/experimental/handlers-controller.mjs.map +1 -0
- package/lib/core/experimental/index.d.mts +17 -0
- package/lib/core/experimental/index.d.ts +17 -0
- package/lib/core/experimental/index.js +36 -0
- package/lib/core/experimental/index.js.map +1 -0
- package/lib/core/experimental/index.mjs +20 -0
- package/lib/core/experimental/index.mjs.map +1 -0
- package/lib/core/experimental/on-unhandled-frame.d.mts +12 -0
- package/lib/core/experimental/on-unhandled-frame.d.ts +12 -0
- package/lib/core/experimental/on-unhandled-frame.js +90 -0
- package/lib/core/experimental/on-unhandled-frame.js.map +1 -0
- package/lib/core/experimental/on-unhandled-frame.mjs +70 -0
- package/lib/core/experimental/on-unhandled-frame.mjs.map +1 -0
- package/lib/core/experimental/request-utils.d.mts +12 -0
- package/lib/core/experimental/request-utils.d.ts +12 -0
- package/lib/core/experimental/request-utils.js +50 -0
- package/lib/core/experimental/request-utils.js.map +1 -0
- package/lib/core/experimental/request-utils.mjs +30 -0
- package/lib/core/experimental/request-utils.mjs.map +1 -0
- package/lib/core/experimental/setup-api.d.mts +33 -0
- package/lib/core/experimental/setup-api.d.ts +33 -0
- package/lib/core/experimental/setup-api.js +61 -0
- package/lib/core/experimental/setup-api.js.map +1 -0
- package/lib/core/experimental/setup-api.mjs +43 -0
- package/lib/core/experimental/setup-api.mjs.map +1 -0
- package/lib/core/experimental/sources/interceptor-source.d.mts +28 -0
- package/lib/core/experimental/sources/interceptor-source.d.ts +28 -0
- package/lib/core/experimental/sources/interceptor-source.js +142 -0
- package/lib/core/experimental/sources/interceptor-source.js.map +1 -0
- package/lib/core/experimental/sources/interceptor-source.mjs +124 -0
- package/lib/core/experimental/sources/interceptor-source.mjs.map +1 -0
- package/lib/core/experimental/sources/network-source.d.mts +31 -0
- package/lib/core/experimental/sources/network-source.d.ts +31 -0
- package/lib/core/experimental/sources/network-source.js +50 -0
- package/lib/core/experimental/sources/network-source.js.map +1 -0
- package/lib/core/experimental/sources/network-source.mjs +30 -0
- package/lib/core/experimental/sources/network-source.mjs.map +1 -0
- package/lib/core/getResponse.d.mts +1 -1
- package/lib/core/getResponse.d.ts +1 -1
- package/lib/core/graphql.d.mts +1 -1
- package/lib/core/graphql.d.ts +1 -1
- package/lib/core/handlers/GraphQLHandler.d.mts +1 -1
- package/lib/core/handlers/GraphQLHandler.d.ts +1 -1
- package/lib/core/handlers/HttpHandler.d.mts +1 -1
- package/lib/core/handlers/HttpHandler.d.ts +1 -1
- package/lib/core/handlers/RequestHandler.d.mts +1 -1
- package/lib/core/handlers/RequestHandler.d.ts +1 -1
- package/lib/core/handlers/RequestHandler.js +5 -6
- package/lib/core/handlers/RequestHandler.js.map +1 -1
- package/lib/core/handlers/RequestHandler.mjs +5 -6
- package/lib/core/handlers/RequestHandler.mjs.map +1 -1
- package/lib/core/handlers/WebSocketHandler.d.mts +8 -4
- package/lib/core/handlers/WebSocketHandler.d.ts +8 -4
- package/lib/core/handlers/WebSocketHandler.js +18 -5
- package/lib/core/handlers/WebSocketHandler.js.map +1 -1
- package/lib/core/handlers/WebSocketHandler.mjs +18 -5
- package/lib/core/handlers/WebSocketHandler.mjs.map +1 -1
- package/lib/core/http.d.mts +1 -1
- package/lib/core/http.d.ts +1 -1
- package/lib/core/index.d.mts +7 -12
- package/lib/core/index.d.ts +7 -12
- package/lib/core/index.js +2 -2
- package/lib/core/index.js.map +1 -1
- package/lib/core/index.mjs +1 -1
- package/lib/core/index.mjs.map +1 -1
- package/lib/core/network-frame-B7A0ggXE.d.mts +56 -0
- package/lib/core/network-frame-usYiHS0K.d.ts +56 -0
- package/lib/core/passthrough.d.mts +1 -1
- package/lib/core/passthrough.d.ts +1 -1
- package/lib/core/sharedOptions.d.mts +6 -2
- package/lib/core/sharedOptions.d.ts +6 -2
- package/lib/core/sharedOptions.js.map +1 -1
- package/lib/core/sse.d.mts +1 -1
- package/lib/core/sse.d.ts +1 -1
- package/lib/core/sse.js.map +1 -1
- package/lib/core/sse.mjs.map +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
- package/lib/core/utils/cookieStore.js.map +1 -1
- package/lib/core/utils/cookieStore.mjs.map +1 -1
- package/lib/core/utils/executeHandlers.d.mts +1 -1
- package/lib/core/utils/executeHandlers.d.ts +1 -1
- package/lib/core/utils/handleRequest.d.mts +2 -1
- package/lib/core/utils/handleRequest.d.ts +2 -1
- package/lib/core/utils/internal/isHandlerKind.d.mts +3 -3
- package/lib/core/utils/internal/isHandlerKind.d.ts +3 -3
- package/lib/core/utils/internal/isHandlerKind.js +2 -1
- package/lib/core/utils/internal/isHandlerKind.js.map +1 -1
- package/lib/core/utils/internal/isHandlerKind.mjs +2 -1
- package/lib/core/utils/internal/isHandlerKind.mjs.map +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.mts +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +1 -1
- package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
- package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/core/utils/matching/matchRequestUrl.js +1 -1
- package/lib/core/utils/matching/matchRequestUrl.js.map +1 -1
- package/lib/core/utils/matching/matchRequestUrl.mjs +1 -1
- package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -1
- package/lib/core/utils/request/onUnhandledRequest.d.mts +2 -2
- package/lib/core/utils/request/onUnhandledRequest.d.ts +2 -2
- package/lib/core/utils/request/onUnhandledRequest.js.map +1 -1
- package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -1
- package/lib/core/ws/handleWebSocketEvent.d.mts +1 -1
- package/lib/core/ws/handleWebSocketEvent.d.ts +1 -1
- package/lib/core/ws/handleWebSocketEvent.js +1 -1
- package/lib/core/ws/handleWebSocketEvent.js.map +1 -1
- package/lib/core/ws/handleWebSocketEvent.mjs +1 -1
- package/lib/core/ws/handleWebSocketEvent.mjs.map +1 -1
- package/lib/core/ws/utils/attachWebSocketLogger.d.mts +1 -1
- package/lib/core/ws/utils/attachWebSocketLogger.d.ts +1 -1
- package/lib/core/ws/utils/attachWebSocketLogger.js +39 -10
- package/lib/core/ws/utils/attachWebSocketLogger.js.map +1 -1
- package/lib/core/ws/utils/attachWebSocketLogger.mjs +39 -10
- package/lib/core/ws/utils/attachWebSocketLogger.mjs.map +1 -1
- package/lib/core/ws.d.mts +3 -3
- package/lib/core/ws.d.ts +3 -3
- package/lib/core/ws.js.map +1 -1
- package/lib/core/ws.mjs.map +1 -1
- package/lib/iife/index.js +2022 -1433
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.d.mts +21 -29
- package/lib/native/index.d.ts +21 -29
- package/lib/native/index.js +48 -116
- package/lib/native/index.js.map +1 -1
- package/lib/native/index.mjs +51 -118
- package/lib/native/index.mjs.map +1 -1
- package/lib/node/index.d.mts +55 -33
- package/lib/node/index.d.ts +55 -33
- package/lib/node/index.js +152 -154
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +156 -156
- package/lib/node/index.mjs.map +1 -1
- package/package.json +10 -2
- package/src/browser/{setupWorker/glossary.ts → glossary.ts} +16 -33
- package/src/browser/index.ts +2 -3
- package/src/browser/{setupWorker/setupWorker.node.test.ts → setup-worker.node.test.ts} +2 -4
- package/src/browser/setup-worker.ts +148 -0
- package/src/browser/sources/fallback-http-source.ts +56 -0
- package/src/browser/sources/service-worker-source.ts +455 -0
- package/src/browser/tsconfig.browser.json +7 -2
- package/src/browser/utils/deserializeRequest.ts +1 -1
- package/src/browser/{setupWorker/start/utils/getWorkerByRegistration.ts → utils/get-worker-by-registration.ts} +3 -1
- package/src/browser/{setupWorker/start/utils/getWorkerInstance.ts → utils/get-worker-instance.ts} +4 -4
- package/src/browser/utils/pruneGetRequestBody.test.ts +1 -3
- package/src/browser/utils/pruneGetRequestBody.ts +1 -1
- package/src/browser/utils/validate-worker-scope.ts +19 -0
- package/src/browser/utils/workerChannel.ts +2 -2
- package/src/core/experimental/compat.ts +50 -0
- package/src/core/experimental/define-network.test.ts +124 -0
- package/src/core/experimental/define-network.ts +215 -0
- package/src/core/experimental/frames/http-frame.test.ts +360 -0
- package/src/core/experimental/frames/http-frame.ts +271 -0
- package/src/core/experimental/frames/network-frame.ts +64 -0
- package/src/core/experimental/frames/websocket-frame.test.ts +280 -0
- package/src/core/experimental/frames/websocket-frame.ts +188 -0
- package/src/core/experimental/handlers-controller.test.ts +198 -0
- package/src/core/experimental/handlers-controller.ts +145 -0
- package/src/core/experimental/index.ts +16 -0
- package/src/core/experimental/on-unhandled-frame.test.ts +360 -0
- package/src/core/experimental/on-unhandled-frame.ts +110 -0
- package/src/core/experimental/request-utils.test.ts +70 -0
- package/src/core/experimental/request-utils.ts +39 -0
- package/src/core/experimental/setup-api.ts +59 -0
- package/src/core/experimental/sources/interceptor-source.ts +185 -0
- package/src/core/experimental/sources/network-source.test.ts +74 -0
- package/src/core/experimental/sources/network-source.ts +56 -0
- package/src/core/handlers/RequestHandler.ts +9 -10
- package/src/core/handlers/WebSocketHandler.ts +27 -11
- package/src/core/index.ts +3 -7
- package/src/core/sharedOptions.ts +9 -4
- package/src/core/sse.ts +1 -1
- package/src/core/utils/cookieStore.ts +2 -1
- package/src/core/utils/internal/isHandlerKind.test.ts +20 -22
- package/src/core/utils/internal/isHandlerKind.ts +5 -9
- package/src/core/utils/matching/matchRequestUrl.test.ts +87 -3
- package/src/core/utils/matching/matchRequestUrl.ts +2 -2
- package/src/core/utils/request/onUnhandledRequest.ts +2 -2
- package/src/core/ws/WebSocketClientManager.test.ts +2 -10
- package/src/core/ws/handleWebSocketEvent.ts +5 -1
- package/src/core/ws/utils/attachWebSocketLogger.ts +43 -11
- package/src/core/ws.test.ts +1 -3
- package/src/core/ws.ts +6 -6
- package/src/iife/index.ts +1 -1
- package/src/native/index.ts +34 -11
- package/src/node/async-handlers-controller.test.ts +50 -0
- package/src/node/async-handlers-controller.ts +69 -0
- package/src/node/glossary.ts +19 -18
- package/src/node/index.ts +6 -2
- package/src/node/setup-server-common.ts +100 -0
- package/src/node/setup-server.ts +91 -0
- package/src/tsconfig.core.json +8 -0
- package/src/tsconfig.node.json +8 -3
- package/src/tsconfig.src.json +0 -2
- package/src/tsconfig.worker.json +2 -1
- package/lib/core/SetupApi.d.mts +0 -44
- package/lib/core/SetupApi.d.ts +0 -44
- package/lib/core/SetupApi.js +0 -112
- package/lib/core/SetupApi.js.map +0 -1
- package/lib/core/SetupApi.mjs +0 -92
- package/lib/core/SetupApi.mjs.map +0 -1
- package/lib/core/handlers/common.d.mts +0 -3
- package/lib/core/handlers/common.d.ts +0 -3
- package/lib/core/handlers/common.js.map +0 -1
- package/lib/core/handlers/common.mjs +0 -1
- package/lib/core/handlers/common.mjs.map +0 -1
- package/src/browser/setupWorker/setupWorker.ts +0 -184
- package/src/browser/setupWorker/start/createFallbackRequestListener.ts +0 -71
- package/src/browser/setupWorker/start/createRequestListener.ts +0 -138
- package/src/browser/setupWorker/start/createResponseListener.ts +0 -57
- package/src/browser/setupWorker/start/createStartHandler.ts +0 -137
- package/src/browser/setupWorker/start/utils/enableMocking.ts +0 -30
- package/src/browser/setupWorker/start/utils/prepareStartHandler.test.ts +0 -59
- package/src/browser/setupWorker/start/utils/prepareStartHandler.ts +0 -44
- package/src/browser/setupWorker/start/utils/printStartMessage.test.ts +0 -84
- package/src/browser/setupWorker/start/utils/printStartMessage.ts +0 -51
- package/src/browser/setupWorker/start/utils/validateWorkerScope.ts +0 -18
- package/src/browser/setupWorker/stop/utils/printStopMessage.test.ts +0 -26
- package/src/browser/setupWorker/stop/utils/printStopMessage.ts +0 -13
- package/src/browser/utils/checkWorkerIntegrity.ts +0 -42
- package/src/core/SetupApi.ts +0 -127
- package/src/core/handlers/common.ts +0 -1
- package/src/node/SetupServerApi.ts +0 -87
- package/src/node/SetupServerCommonApi.ts +0 -169
- package/src/node/setupServer.ts +0 -15
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/core/handlers/common.ts"],"sourcesContent":["export type HandlerKind = 'RequestHandler' | 'EventHandler'\n"],"mappings":";;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=common.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
import { invariant } from 'outvariant'
|
|
2
|
-
import { isNodeProcess } from 'is-node-process'
|
|
3
|
-
import { DeferredPromise } from '@open-draft/deferred-promise'
|
|
4
|
-
import type {
|
|
5
|
-
SetupWorkerInternalContext,
|
|
6
|
-
StartReturnType,
|
|
7
|
-
StartOptions,
|
|
8
|
-
SetupWorker,
|
|
9
|
-
} from './glossary'
|
|
10
|
-
import { RequestHandler } from '~/core/handlers/RequestHandler'
|
|
11
|
-
import { DEFAULT_START_OPTIONS } from './start/utils/prepareStartHandler'
|
|
12
|
-
import { createStartHandler } from './start/createStartHandler'
|
|
13
|
-
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
14
|
-
import { SetupApi } from '~/core/SetupApi'
|
|
15
|
-
import { mergeRight } from '~/core/utils/internal/mergeRight'
|
|
16
|
-
import type { LifeCycleEventsMap } from '~/core/sharedOptions'
|
|
17
|
-
import type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'
|
|
18
|
-
import { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'
|
|
19
|
-
import { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'
|
|
20
|
-
import { attachWebSocketLogger } from '~/core/ws/utils/attachWebSocketLogger'
|
|
21
|
-
import { WorkerChannel } from '../utils/workerChannel'
|
|
22
|
-
import { createFallbackRequestListener } from './start/createFallbackRequestListener'
|
|
23
|
-
import { printStartMessage } from './start/utils/printStartMessage'
|
|
24
|
-
import { printStopMessage } from './stop/utils/printStopMessage'
|
|
25
|
-
import { supportsServiceWorker } from '../utils/supports'
|
|
26
|
-
|
|
27
|
-
export class SetupWorkerApi
|
|
28
|
-
extends SetupApi<LifeCycleEventsMap>
|
|
29
|
-
implements SetupWorker
|
|
30
|
-
{
|
|
31
|
-
private context: SetupWorkerInternalContext
|
|
32
|
-
|
|
33
|
-
constructor(...handlers: Array<RequestHandler | WebSocketHandler>) {
|
|
34
|
-
super(...handlers)
|
|
35
|
-
|
|
36
|
-
invariant(
|
|
37
|
-
!isNodeProcess(),
|
|
38
|
-
devUtils.formatMessage(
|
|
39
|
-
'Failed to execute `setupWorker` in a non-browser environment. Consider using `setupServer` for Node.js environment instead.',
|
|
40
|
-
),
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
this.context = this.createWorkerContext()
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
private createWorkerContext(): SetupWorkerInternalContext {
|
|
47
|
-
const workerPromise = new DeferredPromise<ServiceWorker>()
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
// Mocking is not considered enabled until the worker
|
|
51
|
-
// signals back the successful activation event.
|
|
52
|
-
isMockingEnabled: false,
|
|
53
|
-
startOptions: null as any,
|
|
54
|
-
workerPromise,
|
|
55
|
-
registration: undefined,
|
|
56
|
-
getRequestHandlers: () => {
|
|
57
|
-
return this.handlersController.currentHandlers()
|
|
58
|
-
},
|
|
59
|
-
emitter: this.emitter,
|
|
60
|
-
workerChannel: new WorkerChannel({
|
|
61
|
-
worker: workerPromise,
|
|
62
|
-
}),
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public async start(options: StartOptions = {}): StartReturnType {
|
|
67
|
-
if ('waitUntilReady' in options) {
|
|
68
|
-
devUtils.warn(
|
|
69
|
-
'The "waitUntilReady" option has been deprecated. Please remove it from this "worker.start()" call. Follow the recommended Browser integration (https://mswjs.io/docs/integrations/browser) to eliminate any race conditions between the Service Worker registration and any requests made by your application on initial render.',
|
|
70
|
-
)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Warn the developer on multiple "worker.start()" calls.
|
|
74
|
-
// While this will not affect the worker in any way,
|
|
75
|
-
// it likely indicates an issue with the developer's code.
|
|
76
|
-
if (this.context.isMockingEnabled) {
|
|
77
|
-
devUtils.warn(
|
|
78
|
-
`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.`,
|
|
79
|
-
)
|
|
80
|
-
return this.context.registration
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
this.context.workerStoppedAt = undefined
|
|
84
|
-
|
|
85
|
-
this.context.startOptions = mergeRight(
|
|
86
|
-
DEFAULT_START_OPTIONS,
|
|
87
|
-
options,
|
|
88
|
-
) as SetupWorkerInternalContext['startOptions']
|
|
89
|
-
|
|
90
|
-
// Enable the WebSocket interception.
|
|
91
|
-
handleWebSocketEvent({
|
|
92
|
-
getUnhandledRequestStrategy: () => {
|
|
93
|
-
return this.context.startOptions.onUnhandledRequest
|
|
94
|
-
},
|
|
95
|
-
getHandlers: () => {
|
|
96
|
-
return this.handlersController.currentHandlers()
|
|
97
|
-
},
|
|
98
|
-
onMockedConnection: (connection) => {
|
|
99
|
-
if (!this.context.startOptions.quiet) {
|
|
100
|
-
// Attach the logger for mocked connections since
|
|
101
|
-
// those won't be visible in the browser's devtools.
|
|
102
|
-
attachWebSocketLogger(connection)
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
onPassthroughConnection() {},
|
|
106
|
-
})
|
|
107
|
-
webSocketInterceptor.apply()
|
|
108
|
-
|
|
109
|
-
this.subscriptions.push(() => {
|
|
110
|
-
webSocketInterceptor.dispose()
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
// Use a fallback interception algorithm in the environments
|
|
114
|
-
// where the Service Worker API isn't supported.
|
|
115
|
-
if (!supportsServiceWorker()) {
|
|
116
|
-
const fallbackInterceptor = createFallbackRequestListener(
|
|
117
|
-
this.context,
|
|
118
|
-
this.context.startOptions,
|
|
119
|
-
)
|
|
120
|
-
|
|
121
|
-
this.subscriptions.push(() => {
|
|
122
|
-
fallbackInterceptor.dispose()
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
this.context.isMockingEnabled = true
|
|
126
|
-
|
|
127
|
-
printStartMessage({
|
|
128
|
-
message: 'Mocking enabled (fallback mode).',
|
|
129
|
-
quiet: this.context.startOptions.quiet,
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
return undefined
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const startHandler = createStartHandler(this.context)
|
|
136
|
-
const registration = await startHandler(this.context.startOptions, options)
|
|
137
|
-
|
|
138
|
-
this.context.isMockingEnabled = true
|
|
139
|
-
|
|
140
|
-
return registration
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
public stop(): void {
|
|
144
|
-
super.dispose()
|
|
145
|
-
|
|
146
|
-
if (!this.context.isMockingEnabled) {
|
|
147
|
-
devUtils.warn(
|
|
148
|
-
'Found a redundant "worker.stop()" call. Notice that stopping the worker after it has already been stopped has no effect. Consider removing this "worker.stop()" call.',
|
|
149
|
-
)
|
|
150
|
-
return
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
this.context.isMockingEnabled = false
|
|
154
|
-
this.context.workerStoppedAt = Date.now()
|
|
155
|
-
this.context.emitter.removeAllListeners()
|
|
156
|
-
|
|
157
|
-
if (supportsServiceWorker()) {
|
|
158
|
-
this.context.workerChannel.removeAllListeners('RESPONSE')
|
|
159
|
-
window.clearInterval(this.context.keepAliveInterval)
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Post the internal stop message on the window
|
|
163
|
-
// to let any logic know when the worker has stopped.
|
|
164
|
-
// E.g. the WebSocket client manager needs this to know
|
|
165
|
-
// when to clear its in-memory clients list.
|
|
166
|
-
window.postMessage({ type: 'msw/worker:stop' })
|
|
167
|
-
|
|
168
|
-
printStopMessage({
|
|
169
|
-
quiet: this.context.startOptions?.quiet,
|
|
170
|
-
})
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Sets up a requests interception in the browser with the given request handlers.
|
|
176
|
-
* @param {RequestHandler[]} handlers List of request handlers.
|
|
177
|
-
*
|
|
178
|
-
* @see {@link https://mswjs.io/docs/api/setup-worker `setupWorker()` API reference}
|
|
179
|
-
*/
|
|
180
|
-
export function setupWorker(
|
|
181
|
-
...handlers: Array<RequestHandler | WebSocketHandler>
|
|
182
|
-
): SetupWorker {
|
|
183
|
-
return new SetupWorkerApi(...handlers)
|
|
184
|
-
}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Interceptor,
|
|
3
|
-
BatchInterceptor,
|
|
4
|
-
HttpRequestEventMap,
|
|
5
|
-
} from '@mswjs/interceptors'
|
|
6
|
-
import { FetchInterceptor } from '@mswjs/interceptors/fetch'
|
|
7
|
-
import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'
|
|
8
|
-
import { SetupWorkerInternalContext, StartOptions } from '../glossary'
|
|
9
|
-
import type { RequiredDeep } from '~/core/typeUtils'
|
|
10
|
-
import { handleRequest } from '~/core/utils/handleRequest'
|
|
11
|
-
import { isHandlerKind } from '~/core/utils/internal/isHandlerKind'
|
|
12
|
-
|
|
13
|
-
export function createFallbackRequestListener(
|
|
14
|
-
context: SetupWorkerInternalContext,
|
|
15
|
-
options: RequiredDeep<StartOptions>,
|
|
16
|
-
): Interceptor<HttpRequestEventMap> {
|
|
17
|
-
const interceptor = new BatchInterceptor({
|
|
18
|
-
name: 'fallback',
|
|
19
|
-
interceptors: [new FetchInterceptor(), new XMLHttpRequestInterceptor()],
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
interceptor.on('request', async ({ request, requestId, controller }) => {
|
|
23
|
-
const requestCloneForLogs = request.clone()
|
|
24
|
-
|
|
25
|
-
const response = await handleRequest(
|
|
26
|
-
request,
|
|
27
|
-
requestId,
|
|
28
|
-
context.getRequestHandlers().filter(isHandlerKind('RequestHandler')),
|
|
29
|
-
options,
|
|
30
|
-
context.emitter,
|
|
31
|
-
{
|
|
32
|
-
resolutionContext: {
|
|
33
|
-
quiet: options.quiet,
|
|
34
|
-
},
|
|
35
|
-
onMockedResponse(_, { handler, parsedResult }) {
|
|
36
|
-
if (!options.quiet) {
|
|
37
|
-
context.emitter.once('response:mocked', ({ response }) => {
|
|
38
|
-
handler.log({
|
|
39
|
-
request: requestCloneForLogs,
|
|
40
|
-
response,
|
|
41
|
-
parsedResult,
|
|
42
|
-
})
|
|
43
|
-
})
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
if (response) {
|
|
50
|
-
controller.respondWith(response)
|
|
51
|
-
}
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
interceptor.on(
|
|
55
|
-
'response',
|
|
56
|
-
({ response, isMockedResponse, request, requestId }) => {
|
|
57
|
-
context.emitter.emit(
|
|
58
|
-
isMockedResponse ? 'response:mocked' : 'response:bypass',
|
|
59
|
-
{
|
|
60
|
-
response,
|
|
61
|
-
request,
|
|
62
|
-
requestId,
|
|
63
|
-
},
|
|
64
|
-
)
|
|
65
|
-
},
|
|
66
|
-
)
|
|
67
|
-
|
|
68
|
-
interceptor.apply()
|
|
69
|
-
|
|
70
|
-
return interceptor
|
|
71
|
-
}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { Emitter } from 'rettime'
|
|
2
|
-
import { StartOptions, SetupWorkerInternalContext } from '../glossary'
|
|
3
|
-
import { deserializeRequest } from '../../utils/deserializeRequest'
|
|
4
|
-
import { supportsReadableStreamTransfer } from '../../utils/supports'
|
|
5
|
-
import { RequestHandler } from '~/core/handlers/RequestHandler'
|
|
6
|
-
import { handleRequest } from '~/core/utils/handleRequest'
|
|
7
|
-
import { RequiredDeep } from '~/core/typeUtils'
|
|
8
|
-
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
9
|
-
import { toResponseInit } from '~/core/utils/toResponseInit'
|
|
10
|
-
import { isHandlerKind } from '~/core/utils/internal/isHandlerKind'
|
|
11
|
-
|
|
12
|
-
const SUPPORTS_READABLE_STREAM_TRANSFER = supportsReadableStreamTransfer()
|
|
13
|
-
|
|
14
|
-
export const createRequestListener = (
|
|
15
|
-
context: SetupWorkerInternalContext,
|
|
16
|
-
options: RequiredDeep<StartOptions>,
|
|
17
|
-
): Emitter.ListenerType<typeof context.workerChannel, 'REQUEST'> => {
|
|
18
|
-
return async (event) => {
|
|
19
|
-
// Treat any incoming requests from the worker as passthrough
|
|
20
|
-
// if `worker.stop()` has been called for this client.
|
|
21
|
-
if (
|
|
22
|
-
!context.isMockingEnabled &&
|
|
23
|
-
context.workerStoppedAt &&
|
|
24
|
-
event.data.interceptedAt > context.workerStoppedAt
|
|
25
|
-
) {
|
|
26
|
-
event.postMessage('PASSTHROUGH')
|
|
27
|
-
return
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const requestId = event.data.id
|
|
31
|
-
const request = deserializeRequest(event.data)
|
|
32
|
-
const requestCloneForLogs = request.clone()
|
|
33
|
-
|
|
34
|
-
// Make this the first request clone before the
|
|
35
|
-
// request resolution pipeline even starts.
|
|
36
|
-
// Store the clone in cache so the first matching
|
|
37
|
-
// request handler would skip the cloning phase.
|
|
38
|
-
const requestClone = request.clone()
|
|
39
|
-
RequestHandler.cache.set(request, requestClone)
|
|
40
|
-
|
|
41
|
-
try {
|
|
42
|
-
await handleRequest(
|
|
43
|
-
request,
|
|
44
|
-
requestId,
|
|
45
|
-
context.getRequestHandlers().filter(isHandlerKind('RequestHandler')),
|
|
46
|
-
options,
|
|
47
|
-
context.emitter,
|
|
48
|
-
{
|
|
49
|
-
resolutionContext: {
|
|
50
|
-
quiet: options.quiet,
|
|
51
|
-
},
|
|
52
|
-
onPassthroughResponse() {
|
|
53
|
-
event.postMessage('PASSTHROUGH')
|
|
54
|
-
},
|
|
55
|
-
async onMockedResponse(response, { handler, parsedResult }) {
|
|
56
|
-
// Clone the mocked response so its body could be read
|
|
57
|
-
// to buffer to be sent to the worker and also in the
|
|
58
|
-
// ".log()" method of the request handler.
|
|
59
|
-
const responseClone = response.clone()
|
|
60
|
-
const responseCloneForLogs = response.clone()
|
|
61
|
-
const responseInit = toResponseInit(response)
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* @note Safari doesn't support transferring a "ReadableStream".
|
|
65
|
-
* Check that the browser supports that before sending it to the worker.
|
|
66
|
-
*/
|
|
67
|
-
if (SUPPORTS_READABLE_STREAM_TRANSFER) {
|
|
68
|
-
const responseStreamOrNull = response.body
|
|
69
|
-
|
|
70
|
-
event.postMessage(
|
|
71
|
-
'MOCK_RESPONSE',
|
|
72
|
-
{
|
|
73
|
-
...responseInit,
|
|
74
|
-
body: responseStreamOrNull,
|
|
75
|
-
},
|
|
76
|
-
responseStreamOrNull ? [responseStreamOrNull] : undefined,
|
|
77
|
-
)
|
|
78
|
-
} else {
|
|
79
|
-
/**
|
|
80
|
-
* @note If we are here, this means the current environment doesn't
|
|
81
|
-
* support "ReadableStream" as transferable. In that case,
|
|
82
|
-
* attempt to read the non-empty response body as ArrayBuffer, if it's not empty.
|
|
83
|
-
* @see https://github.com/mswjs/msw/issues/1827
|
|
84
|
-
*/
|
|
85
|
-
const responseBufferOrNull =
|
|
86
|
-
response.body === null
|
|
87
|
-
? null
|
|
88
|
-
: await responseClone.arrayBuffer()
|
|
89
|
-
|
|
90
|
-
event.postMessage('MOCK_RESPONSE', {
|
|
91
|
-
...responseInit,
|
|
92
|
-
body: responseBufferOrNull,
|
|
93
|
-
})
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (!options.quiet) {
|
|
97
|
-
context.emitter.once('response:mocked', () => {
|
|
98
|
-
handler.log({
|
|
99
|
-
request: requestCloneForLogs,
|
|
100
|
-
response: responseCloneForLogs,
|
|
101
|
-
parsedResult,
|
|
102
|
-
})
|
|
103
|
-
})
|
|
104
|
-
}
|
|
105
|
-
},
|
|
106
|
-
},
|
|
107
|
-
)
|
|
108
|
-
} catch (error) {
|
|
109
|
-
if (error instanceof Error) {
|
|
110
|
-
devUtils.error(
|
|
111
|
-
`Uncaught exception in the request handler for "%s %s":
|
|
112
|
-
|
|
113
|
-
%s
|
|
114
|
-
|
|
115
|
-
This exception has been gracefully handled as a 500 response, however, it's strongly recommended to resolve this error, as it indicates a mistake in your code. If you wish to mock an error response, please see this guide: https://mswjs.io/docs/http/mocking-responses/error-responses`,
|
|
116
|
-
request.method,
|
|
117
|
-
request.url,
|
|
118
|
-
error.stack ?? error,
|
|
119
|
-
)
|
|
120
|
-
|
|
121
|
-
// Treat all other exceptions in a request handler as unintended,
|
|
122
|
-
// alerting that there is a problem that needs fixing.
|
|
123
|
-
event.postMessage('MOCK_RESPONSE', {
|
|
124
|
-
status: 500,
|
|
125
|
-
statusText: 'Request Handler Error',
|
|
126
|
-
headers: {
|
|
127
|
-
'Content-Type': 'application/json',
|
|
128
|
-
},
|
|
129
|
-
body: JSON.stringify({
|
|
130
|
-
name: error.name,
|
|
131
|
-
message: error.message,
|
|
132
|
-
stack: error.stack,
|
|
133
|
-
}),
|
|
134
|
-
})
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { FetchResponse } from '@mswjs/interceptors'
|
|
2
|
-
import type { Emitter } from 'rettime'
|
|
3
|
-
import type { SetupWorkerInternalContext } from '../glossary'
|
|
4
|
-
import { deserializeRequest } from '../../utils/deserializeRequest'
|
|
5
|
-
|
|
6
|
-
export function createResponseListener(
|
|
7
|
-
context: SetupWorkerInternalContext,
|
|
8
|
-
): Emitter.ListenerType<typeof context.workerChannel, 'RESPONSE'> {
|
|
9
|
-
return (event) => {
|
|
10
|
-
const responseMessage = event.data
|
|
11
|
-
const request = deserializeRequest(responseMessage.request)
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* CORS requests with `mode: "no-cors"` result in "opaque" responses.
|
|
15
|
-
* That kind of responses cannot be manipulated in JavaScript due
|
|
16
|
-
* to the security considerations.
|
|
17
|
-
* @see https://fetch.spec.whatwg.org/#concept-filtered-response-opaque
|
|
18
|
-
* @see https://github.com/mswjs/msw/issues/529
|
|
19
|
-
*/
|
|
20
|
-
if (responseMessage.response.type?.includes('opaque')) {
|
|
21
|
-
return
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const response =
|
|
25
|
-
responseMessage.response.status === 0
|
|
26
|
-
? Response.error()
|
|
27
|
-
: new FetchResponse(
|
|
28
|
-
/**
|
|
29
|
-
* Responses may be streams here, but when we create a response object
|
|
30
|
-
* with null-body status codes, like 204, 205, 304 Response will
|
|
31
|
-
* throw when passed a non-null body, so ensure it's null here
|
|
32
|
-
* for those codes
|
|
33
|
-
*/
|
|
34
|
-
FetchResponse.isResponseWithBody(responseMessage.response.status)
|
|
35
|
-
? responseMessage.response.body
|
|
36
|
-
: null,
|
|
37
|
-
{
|
|
38
|
-
...responseMessage.response,
|
|
39
|
-
/**
|
|
40
|
-
* Set response URL if it's not set already.
|
|
41
|
-
* @see https://github.com/mswjs/msw/issues/2030
|
|
42
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/url
|
|
43
|
-
*/
|
|
44
|
-
url: request.url,
|
|
45
|
-
},
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
context.emitter.emit(
|
|
49
|
-
responseMessage.isMockedResponse ? 'response:mocked' : 'response:bypass',
|
|
50
|
-
{
|
|
51
|
-
requestId: responseMessage.request.id,
|
|
52
|
-
request,
|
|
53
|
-
response,
|
|
54
|
-
},
|
|
55
|
-
)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
2
|
-
import { getWorkerInstance } from './utils/getWorkerInstance'
|
|
3
|
-
import { enableMocking } from './utils/enableMocking'
|
|
4
|
-
import type { SetupWorkerInternalContext, StartHandler } from '../glossary'
|
|
5
|
-
import { createRequestListener } from './createRequestListener'
|
|
6
|
-
import { checkWorkerIntegrity } from '../../utils/checkWorkerIntegrity'
|
|
7
|
-
import { createResponseListener } from './createResponseListener'
|
|
8
|
-
import { validateWorkerScope } from './utils/validateWorkerScope'
|
|
9
|
-
import { DeferredPromise } from '@open-draft/deferred-promise'
|
|
10
|
-
|
|
11
|
-
export const createStartHandler = (
|
|
12
|
-
context: SetupWorkerInternalContext,
|
|
13
|
-
): StartHandler => {
|
|
14
|
-
return function start(options, customOptions) {
|
|
15
|
-
const startWorkerInstance = async () => {
|
|
16
|
-
// Remove all previously existing event listeners.
|
|
17
|
-
// This way none of the listeners persists between Fast refresh
|
|
18
|
-
// of the application's code.
|
|
19
|
-
context.workerChannel.removeAllListeners()
|
|
20
|
-
|
|
21
|
-
// Handle requests signaled by the worker.
|
|
22
|
-
context.workerChannel.on(
|
|
23
|
-
'REQUEST',
|
|
24
|
-
createRequestListener(context, options),
|
|
25
|
-
)
|
|
26
|
-
|
|
27
|
-
// Handle responses signaled by the worker.
|
|
28
|
-
context.workerChannel.on('RESPONSE', createResponseListener(context))
|
|
29
|
-
|
|
30
|
-
const instance = await getWorkerInstance(
|
|
31
|
-
options.serviceWorker.url,
|
|
32
|
-
options.serviceWorker.options,
|
|
33
|
-
options.findWorker,
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
const [worker, registration] = instance
|
|
37
|
-
|
|
38
|
-
if (!worker) {
|
|
39
|
-
const missingWorkerMessage = customOptions?.findWorker
|
|
40
|
-
? devUtils.formatMessage(
|
|
41
|
-
`Failed to locate the Service Worker registration using a custom "findWorker" predicate.
|
|
42
|
-
|
|
43
|
-
Please ensure that the custom predicate properly locates the Service Worker registration at "%s".
|
|
44
|
-
More details: https://mswjs.io/docs/api/setup-worker/start#findworker
|
|
45
|
-
`,
|
|
46
|
-
options.serviceWorker.url,
|
|
47
|
-
)
|
|
48
|
-
: devUtils.formatMessage(
|
|
49
|
-
`Failed to locate the Service Worker registration.
|
|
50
|
-
|
|
51
|
-
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.
|
|
52
|
-
|
|
53
|
-
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`,
|
|
54
|
-
options.serviceWorker.url,
|
|
55
|
-
location.host,
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
throw new Error(missingWorkerMessage)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
context.workerPromise.resolve(worker)
|
|
62
|
-
context.registration = registration
|
|
63
|
-
|
|
64
|
-
window.addEventListener('beforeunload', () => {
|
|
65
|
-
if (worker.state !== 'redundant') {
|
|
66
|
-
// Notify the Service Worker that this client has closed.
|
|
67
|
-
// Internally, it's similar to disabling the mocking, only
|
|
68
|
-
// client close event has a handler that self-terminates
|
|
69
|
-
// the Service Worker when there are no open clients.
|
|
70
|
-
context.workerChannel.postMessage('CLIENT_CLOSED')
|
|
71
|
-
}
|
|
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
|
-
// Notify others about this client disconnecting.
|
|
78
|
-
// E.g. this will purge the in-memory WebSocket clients since
|
|
79
|
-
// starting the worker again will assign them new IDs.
|
|
80
|
-
window.postMessage({ type: 'msw/worker:stop' })
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
// Check if the active Service Worker has been generated
|
|
84
|
-
// by the currently installed version of MSW.
|
|
85
|
-
await checkWorkerIntegrity(context).catch((error) => {
|
|
86
|
-
devUtils.error(
|
|
87
|
-
'Error while checking the worker script integrity. Please report this on GitHub (https://github.com/mswjs/msw/issues) and include the original error below.',
|
|
88
|
-
)
|
|
89
|
-
console.error(error)
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
context.keepAliveInterval = window.setInterval(
|
|
93
|
-
() => context.workerChannel.postMessage('KEEPALIVE_REQUEST'),
|
|
94
|
-
5000,
|
|
95
|
-
)
|
|
96
|
-
|
|
97
|
-
// Warn the user when loading the page that lies outside
|
|
98
|
-
// of the worker's scope.
|
|
99
|
-
validateWorkerScope(registration, context.startOptions)
|
|
100
|
-
|
|
101
|
-
return registration
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const workerRegistration = startWorkerInstance().then(
|
|
105
|
-
async (registration) => {
|
|
106
|
-
const pendingInstance = registration.installing || registration.waiting
|
|
107
|
-
|
|
108
|
-
if (pendingInstance) {
|
|
109
|
-
const activationPromise = new DeferredPromise<void>()
|
|
110
|
-
|
|
111
|
-
pendingInstance.addEventListener('statechange', () => {
|
|
112
|
-
if (pendingInstance.state === 'activated') {
|
|
113
|
-
activationPromise.resolve()
|
|
114
|
-
}
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
// Wait until the worker is activated.
|
|
118
|
-
// Assume the worker is already activated if there's no pending registration
|
|
119
|
-
// (i.e. when reloading the page after a successful activation).
|
|
120
|
-
await activationPromise
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Print the activation message only after the worker has been activated.
|
|
124
|
-
await enableMocking(context, options).catch((error) => {
|
|
125
|
-
devUtils.error(
|
|
126
|
-
'Failed to enable mocking. Please report this on GitHub (https://github.com/mswjs/msw/issues) and include the original error below.',
|
|
127
|
-
)
|
|
128
|
-
throw error
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
return registration
|
|
132
|
-
},
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
return workerRegistration
|
|
136
|
-
}
|
|
137
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { DeferredPromise } from '@open-draft/deferred-promise'
|
|
2
|
-
import type { StartOptions, SetupWorkerInternalContext } from '../../glossary'
|
|
3
|
-
import { printStartMessage } from './printStartMessage'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Signals the worker to enable the interception of requests.
|
|
7
|
-
*/
|
|
8
|
-
export function enableMocking(
|
|
9
|
-
context: SetupWorkerInternalContext,
|
|
10
|
-
options: StartOptions,
|
|
11
|
-
): Promise<boolean> {
|
|
12
|
-
const mockingEnabledPromise = new DeferredPromise<boolean>()
|
|
13
|
-
|
|
14
|
-
context.workerChannel.postMessage('MOCK_ACTIVATE')
|
|
15
|
-
context.workerChannel.once('MOCKING_ENABLED', async (event) => {
|
|
16
|
-
context.isMockingEnabled = true
|
|
17
|
-
const worker = await context.workerPromise
|
|
18
|
-
|
|
19
|
-
printStartMessage({
|
|
20
|
-
quiet: options.quiet,
|
|
21
|
-
workerScope: context.registration?.scope,
|
|
22
|
-
workerUrl: worker.scriptURL,
|
|
23
|
-
client: event.data.client,
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
mockingEnabledPromise.resolve(true)
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
return mockingEnabledPromise
|
|
30
|
-
}
|