msw 2.7.4 → 2.7.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/index.js.map +1 -1
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/handlers/WebSocketHandler.js +4 -2
- package/lib/core/handlers/WebSocketHandler.js.map +1 -1
- package/lib/core/handlers/WebSocketHandler.mjs +4 -2
- package/lib/core/handlers/WebSocketHandler.mjs.map +1 -1
- package/lib/iife/index.js +4 -2
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.d.mts +0 -5
- package/lib/native/index.d.ts +0 -5
- package/lib/native/index.js.map +1 -1
- package/lib/native/index.mjs.map +1 -1
- package/lib/node/index.d.mts +0 -5
- package/lib/node/index.d.ts +0 -5
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs.map +1 -1
- package/package.json +5 -7
- package/src/browser/setupWorker/glossary.ts +3 -6
- package/src/browser/setupWorker/start/utils/printStartMessage.ts +2 -2
- package/src/core/handlers/WebSocketHandler.test.ts +160 -0
- package/src/core/handlers/WebSocketHandler.ts +11 -2
- package/src/node/SetupServerCommonApi.ts +1 -1
- package/src/node/utils/isNodeExceptionLike.ts +0 -14
package/lib/mockServiceWorker.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* - Please do NOT serve this file on production.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
const PACKAGE_VERSION = '2.7.
|
|
11
|
+
const PACKAGE_VERSION = '2.7.6'
|
|
12
12
|
const INTEGRITY_CHECKSUM = '00729d72e3b82faf54ca8b9621dbb96f'
|
|
13
13
|
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
|
|
14
14
|
const activeClientIds = new Set()
|
package/lib/native/index.d.mts
CHANGED
|
@@ -51,11 +51,6 @@ interface SetupServerCommon {
|
|
|
51
51
|
events: LifeCycleEventEmitter<LifeCycleEventsMap>;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
/**
|
|
55
|
-
* @note This API is extended by both "msw/node" and "msw/native"
|
|
56
|
-
* so be minding about the things you import!
|
|
57
|
-
*/
|
|
58
|
-
|
|
59
54
|
declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implements SetupServerCommon {
|
|
60
55
|
protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
|
|
61
56
|
private resolvedOptions;
|
package/lib/native/index.d.ts
CHANGED
|
@@ -51,11 +51,6 @@ interface SetupServerCommon {
|
|
|
51
51
|
events: LifeCycleEventEmitter<LifeCycleEventsMap>;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
/**
|
|
55
|
-
* @note This API is extended by both "msw/node" and "msw/native"
|
|
56
|
-
* so be minding about the things you import!
|
|
57
|
-
*/
|
|
58
|
-
|
|
59
54
|
declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implements SetupServerCommon {
|
|
60
55
|
protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
|
|
61
56
|
private resolvedOptions;
|
package/lib/native/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/native/index.ts","../../src/node/SetupServerCommonApi.ts"],"sourcesContent":["import { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerCommonApi } from '../node/SetupServerCommonApi'\n\n/**\n * Sets up a requests interception in React Native with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport function setupServer(\n ...handlers: Array<RequestHandler>\n): SetupServerCommonApi {\n // Provision request interception via patching the `XMLHttpRequest` class only\n // in React Native. There is no `http`/`https` modules in that environment.\n return new SetupServerCommonApi(\n [FetchInterceptor, XMLHttpRequestInterceptor],\n handlers,\n )\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\
|
|
1
|
+
{"version":3,"sources":["../../src/native/index.ts","../../src/node/SetupServerCommonApi.ts"],"sourcesContent":["import { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerCommonApi } from '../node/SetupServerCommonApi'\n\n/**\n * Sets up a requests interception in React Native with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport function setupServer(\n ...handlers: Array<RequestHandler>\n): SetupServerCommonApi {\n // Provision request interception via patching the `XMLHttpRequest` class only\n // in React Native. There is no `http`/`https` modules in that environment.\n return new SetupServerCommonApi(\n [FetchInterceptor, XMLHttpRequestInterceptor],\n handlers,\n )\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n\n // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\n })\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/ms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yB;AAAA,IAC5C;AAAA,EACF;AACF;","names":["request"]}
|
package/lib/native/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/native/index.ts","../../src/node/SetupServerCommonApi.ts"],"sourcesContent":["import { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerCommonApi } from '../node/SetupServerCommonApi'\n\n/**\n * Sets up a requests interception in React Native with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport function setupServer(\n ...handlers: Array<RequestHandler>\n): SetupServerCommonApi {\n // Provision request interception via patching the `XMLHttpRequest` class only\n // in React Native. There is no `http`/`https` modules in that environment.\n return new SetupServerCommonApi(\n [FetchInterceptor, XMLHttpRequestInterceptor],\n handlers,\n )\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\
|
|
1
|
+
{"version":3,"sources":["../../src/native/index.ts","../../src/node/SetupServerCommonApi.ts"],"sourcesContent":["import { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerCommonApi } from '../node/SetupServerCommonApi'\n\n/**\n * Sets up a requests interception in React Native with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport function setupServer(\n ...handlers: Array<RequestHandler>\n): SetupServerCommonApi {\n // Provision request interception via patching the `XMLHttpRequest` class only\n // in React Native. There is no `http`/`https` modules in that environment.\n return new SetupServerCommonApi(\n [FetchInterceptor, XMLHttpRequestInterceptor],\n handlers,\n )\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n\n // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\n })\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n"],"mappings":";AAAA,SAAS,wBAAwB;AACjC,SAAS,iCAAiC;;;ACI1C,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAEP,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;AAG9B,SAAS,kBAAkB;AAC3B,SAAS,eAAe,gBAAgB;AAExC,SAAS,4BAA4B;AACrC,SAAS,4BAA4B;AACrC,SAAS,qBAAqB;AAE9B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,uBAAN,cACG,SAEV;AAAA,EACqB;AAAA,EAIX;AAAA,EAER,YACE,cACA,UACA;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,cAAc,IAAI,iBAAiB;AAAA,MACtC,MAAM;AAAA,MACN,cAAc,aAAa,IAAI,CAAC,gBAAgB,IAAI,YAAY,CAAC;AAAA,IACnE,CAAC;AAED,SAAK,kBAAkB,CAAC;AAAA,EAC1B;AAAA;AAAA;AAAA;AAA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MAAM;AAAA,MAAC;AAAA,IAClC,CAAC;AAAA,EACH;AAAA,EAEO,OAAO,UAAkC,CAAC,GAAS;AACxD,SAAK,kBAAkB;AAAA,MACrB;AAAA,MACA;AAAA,IACF;AAKA,SAAK,YAAY,MAAM;AACvB,SAAK,KAAK;AACV,SAAK,cAAc,KAAK,MAAM,KAAK,YAAY,QAAQ,CAAC;AAGxD,yBAAqB,MAAM;AAC3B,SAAK,cAAc,KAAK,MAAM,qBAAqB,QAAQ,CAAC;AAK5D;AAAA,MACE,CAAC,sBAAsB,UAAU,sBAAsB,OAAO,EAAE;AAAA,QAC9D,KAAK,YAAY;AAAA,MACnB;AAAA,MACA,SAAS;AAAA,QACP;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA,EAEO,QAAc;AACnB,SAAK,QAAQ;AAAA,EACf;AACF;;;AD7JO,SAAS,eACX,UACmB;AAGtB,SAAO,IAAI;AAAA,IACT,CAAC,kBAAkB,yBAAyB;AAAA,IAC5C;AAAA,EACF;AACF;","names":["request"]}
|
package/lib/node/index.d.mts
CHANGED
|
@@ -62,11 +62,6 @@ interface SetupServer extends SetupServerCommon {
|
|
|
62
62
|
boundary<Args extends Array<any>, R>(callback: (...args: Args) => R): (...args: Args) => R;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
/**
|
|
66
|
-
* @note This API is extended by both "msw/node" and "msw/native"
|
|
67
|
-
* so be minding about the things you import!
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
65
|
declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implements SetupServerCommon {
|
|
71
66
|
protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
|
|
72
67
|
private resolvedOptions;
|
package/lib/node/index.d.ts
CHANGED
|
@@ -62,11 +62,6 @@ interface SetupServer extends SetupServerCommon {
|
|
|
62
62
|
boundary<Args extends Array<any>, R>(callback: (...args: Args) => R): (...args: Args) => R;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
/**
|
|
66
|
-
* @note This API is extended by both "msw/node" and "msw/native"
|
|
67
|
-
* so be minding about the things you import!
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
65
|
declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implements SetupServerCommon {
|
|
71
66
|
protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
|
|
72
67
|
private resolvedOptions;
|
package/lib/node/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/node/index.ts","../../src/node/SetupServerApi.ts","../../src/node/SetupServerCommonApi.ts","../../src/node/setupServer.ts"],"sourcesContent":["export type { SetupServer } from './glossary'\nexport { SetupServerApi } from './SetupServerApi'\nexport { setupServer } from './setupServer'\n","import { AsyncLocalStorage } from 'node:async_hooks'\nimport { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { HandlersController } from '~/core/SetupApi'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport type { SetupServer } from './glossary'\nimport { SetupServerCommonApi } from './SetupServerCommonApi'\n\nconst store = new AsyncLocalStorage<RequestHandlersContext>()\n\ntype RequestHandlersContext = {\n initialHandlers: Array<RequestHandler | WebSocketHandler>\n handlers: Array<RequestHandler | WebSocketHandler>\n}\n\n/**\n * A handlers controller that utilizes `AsyncLocalStorage` in Node.js\n * to prevent the request handlers list from being a shared state\n * across mutliple tests.\n */\nclass AsyncHandlersController implements HandlersController {\n private rootContext: RequestHandlersContext\n\n constructor(initialHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.rootContext = { initialHandlers, handlers: [] }\n }\n\n get context(): RequestHandlersContext {\n return store.getStore() || this.rootContext\n }\n\n public prepend(runtimeHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.context.handlers.unshift(...runtimeHandlers)\n }\n\n public reset(nextHandlers: Array<RequestHandler | WebSocketHandler>) {\n const context = this.context\n context.handlers = []\n context.initialHandlers =\n nextHandlers.length > 0 ? nextHandlers : context.initialHandlers\n }\n\n public currentHandlers(): Array<RequestHandler | WebSocketHandler> {\n const { initialHandlers, handlers } = this.context\n return handlers.concat(initialHandlers)\n }\n}\n\nexport class SetupServerApi\n extends SetupServerCommonApi\n implements SetupServer\n{\n constructor(handlers: Array<RequestHandler | WebSocketHandler>) {\n super(\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],\n handlers,\n )\n\n this.handlersController = new AsyncHandlersController(handlers)\n }\n\n public boundary<Args extends Array<any>, R>(\n callback: (...args: Args) => R,\n ): (...args: Args) => R {\n return (...args: Args): R => {\n return store.run<any, any>(\n {\n initialHandlers: this.handlersController.currentHandlers(),\n handlers: [],\n },\n callback,\n ...args,\n )\n }\n }\n\n public close(): void {\n super.close()\n store.disable()\n }\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nexport const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n\n // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\n })\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n","import type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { SetupServerApi } from './SetupServerApi'\n\n/**\n * Sets up a requests interception in Node.js with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport const setupServer = (\n ...handlers: Array<RequestHandler | WebSocketHandler>\n): SetupServerApi => {\n return new SetupServerApi(handlers)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,8BAAkC;AAClC,2BAAyC;AACzC,4BAA0C;AAC1C,mBAAiC;;;ACEjC,wBAA0B;AAC1B,0BAKO;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AAQ,iBAA2D;AACxE,SAAK,QAAQ,SAAS,QAAQ,GAAG,eAAe;AAAA,EAClD;AAAA,EAEO,MAAM,cAAwD;AACnE,UAAM,UAAU,KAAK;AACrB,YAAQ,WAAW,CAAC;AACpB,YAAQ,kBACN,aAAa,SAAS,IAAI,eAAe,QAAQ;AAAA,EACrD;AAAA,EAEO,kBAA4D;AACjE,UAAM,EAAE,iBAAiB,SAAS,IAAI,KAAK;AAC3C,WAAO,SAAS,OAAO,eAAe;AAAA,EACxC;AACF;AAEO,IAAM,iBAAN,cACG,qBAEV;AAAA,EACE,YAAY,UAAoD;AAC9D;AAAA,MACE,CAAC,+CAA0B,iDAA2B,6BAAgB;AAAA,MACtE;AAAA,IACF;AAEA,SAAK,qBAAqB,IAAI,wBAAwB,QAAQ;AAAA,EAChE;AAAA,EAEO,SACL,UACsB;AACtB,WAAO,IAAI,SAAkB;AAC3B,aAAO,MAAM;AAAA,QACX;AAAA,UACE,iBAAiB,KAAK,mBAAmB,gBAAgB;AAAA,UACzD,UAAU,CAAC;AAAA,QACb;AAAA,QACA;AAAA,QACA,GAAG;AAAA,MACL;AAAA,IACF;AAAA,EACF;AAAA,EAEO,QAAc;AACnB,UAAM,MAAM;AACZ,UAAM,QAAQ;AAAA,EAChB;AACF;;;AExEO,IAAM,cAAc,IACtB,aACgB;AACnB,SAAO,IAAI,eAAe,QAAQ;AACpC;","names":["request"]}
|
|
1
|
+
{"version":3,"sources":["../../src/node/index.ts","../../src/node/SetupServerApi.ts","../../src/node/SetupServerCommonApi.ts","../../src/node/setupServer.ts"],"sourcesContent":["export type { SetupServer } from './glossary'\nexport { SetupServerApi } from './SetupServerApi'\nexport { setupServer } from './setupServer'\n","import { AsyncLocalStorage } from 'node:async_hooks'\nimport { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { HandlersController } from '~/core/SetupApi'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport type { SetupServer } from './glossary'\nimport { SetupServerCommonApi } from './SetupServerCommonApi'\n\nconst store = new AsyncLocalStorage<RequestHandlersContext>()\n\ntype RequestHandlersContext = {\n initialHandlers: Array<RequestHandler | WebSocketHandler>\n handlers: Array<RequestHandler | WebSocketHandler>\n}\n\n/**\n * A handlers controller that utilizes `AsyncLocalStorage` in Node.js\n * to prevent the request handlers list from being a shared state\n * across mutliple tests.\n */\nclass AsyncHandlersController implements HandlersController {\n private rootContext: RequestHandlersContext\n\n constructor(initialHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.rootContext = { initialHandlers, handlers: [] }\n }\n\n get context(): RequestHandlersContext {\n return store.getStore() || this.rootContext\n }\n\n public prepend(runtimeHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.context.handlers.unshift(...runtimeHandlers)\n }\n\n public reset(nextHandlers: Array<RequestHandler | WebSocketHandler>) {\n const context = this.context\n context.handlers = []\n context.initialHandlers =\n nextHandlers.length > 0 ? nextHandlers : context.initialHandlers\n }\n\n public currentHandlers(): Array<RequestHandler | WebSocketHandler> {\n const { initialHandlers, handlers } = this.context\n return handlers.concat(initialHandlers)\n }\n}\n\nexport class SetupServerApi\n extends SetupServerCommonApi\n implements SetupServer\n{\n constructor(handlers: Array<RequestHandler | WebSocketHandler>) {\n super(\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],\n handlers,\n )\n\n this.handlersController = new AsyncHandlersController(handlers)\n }\n\n public boundary<Args extends Array<any>, R>(\n callback: (...args: Args) => R,\n ): (...args: Args) => R {\n return (...args: Args): R => {\n return store.run<any, any>(\n {\n initialHandlers: this.handlersController.currentHandlers(),\n handlers: [],\n },\n callback,\n ...args,\n )\n }\n }\n\n public close(): void {\n super.close()\n store.disable()\n }\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n\n // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\n })\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n","import type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { SetupServerApi } from './SetupServerApi'\n\n/**\n * Sets up a requests interception in Node.js with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport const setupServer = (\n ...handlers: Array<RequestHandler | WebSocketHandler>\n): SetupServerApi => {\n return new SetupServerApi(handlers)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,8BAAkC;AAClC,2BAAyC;AACzC,4BAA0C;AAC1C,mBAAiC;;;ACEjC,wBAA0B;AAC1B,0BAKO;AAEP,sBAAyB;AACzB,2BAA8B;AAG9B,wBAA2B;AAC3B,sBAAwC;AAExC,kCAAqC;AACrC,kCAAqC;AACrC,2BAA8B;AAE9B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,uBAAN,cACG,yBAEV;AAAA,EACqB;AAAA,EAIX;AAAA,EAER,YACE,cACA,UACA;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,cAAc,IAAI,qCAAiB;AAAA,MACtC,MAAM;AAAA,MACN,cAAc,aAAa,IAAI,CAAC,gBAAgB,IAAI,YAAY,CAAC;AAAA,IACnE,CAAC;AAED,SAAK,kBAAkB,CAAC;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA,EAKQ,OAAa;AACnB,SAAK,YAAY;AAAA,MACf;AAAA,MACA,OAAO,EAAE,SAAS,WAAW,WAAW,MAAM;AAC5C,cAAM,WAAW,UAAM;AAAA,UACrB;AAAA,UACA;AAAA,UACA,KAAK,mBACF,gBAAgB,EAChB,WAAO,oCAAc,gBAAgB,CAAC;AAAA,UACzC,KAAK;AAAA,UACL,KAAK;AAAA,UACL;AA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A,IACF;AAAA,EACF;AAAA,EAEO,QAAc;AACnB,UAAM,MAAM;AACZ,UAAM,QAAQ;AAAA,EAChB;AACF;;;AExEO,IAAM,cAAc,IACtB,aACgB;AACnB,SAAO,IAAI,eAAe,QAAQ;AACpC;","names":["request"]}
|
package/lib/node/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/node/SetupServerApi.ts","../../src/node/SetupServerCommonApi.ts","../../src/node/setupServer.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks'\nimport { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { HandlersController } from '~/core/SetupApi'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport type { SetupServer } from './glossary'\nimport { SetupServerCommonApi } from './SetupServerCommonApi'\n\nconst store = new AsyncLocalStorage<RequestHandlersContext>()\n\ntype RequestHandlersContext = {\n initialHandlers: Array<RequestHandler | WebSocketHandler>\n handlers: Array<RequestHandler | WebSocketHandler>\n}\n\n/**\n * A handlers controller that utilizes `AsyncLocalStorage` in Node.js\n * to prevent the request handlers list from being a shared state\n * across mutliple tests.\n */\nclass AsyncHandlersController implements HandlersController {\n private rootContext: RequestHandlersContext\n\n constructor(initialHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.rootContext = { initialHandlers, handlers: [] }\n }\n\n get context(): RequestHandlersContext {\n return store.getStore() || this.rootContext\n }\n\n public prepend(runtimeHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.context.handlers.unshift(...runtimeHandlers)\n }\n\n public reset(nextHandlers: Array<RequestHandler | WebSocketHandler>) {\n const context = this.context\n context.handlers = []\n context.initialHandlers =\n nextHandlers.length > 0 ? nextHandlers : context.initialHandlers\n }\n\n public currentHandlers(): Array<RequestHandler | WebSocketHandler> {\n const { initialHandlers, handlers } = this.context\n return handlers.concat(initialHandlers)\n }\n}\n\nexport class SetupServerApi\n extends SetupServerCommonApi\n implements SetupServer\n{\n constructor(handlers: Array<RequestHandler | WebSocketHandler>) {\n super(\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],\n handlers,\n )\n\n this.handlersController = new AsyncHandlersController(handlers)\n }\n\n public boundary<Args extends Array<any>, R>(\n callback: (...args: Args) => R,\n ): (...args: Args) => R {\n return (...args: Args): R => {\n return store.run<any, any>(\n {\n initialHandlers: this.handlersController.currentHandlers(),\n handlers: [],\n },\n callback,\n ...args,\n )\n }\n }\n\n public close(): void {\n super.close()\n store.disable()\n }\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nexport const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n\n // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\n })\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n","import type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { SetupServerApi } from './SetupServerApi'\n\n/**\n * Sets up a requests interception in Node.js with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport const setupServer = (\n ...handlers: Array<RequestHandler | WebSocketHandler>\n): SetupServerApi => {\n return new SetupServerApi(handlers)\n}\n"],"mappings":";AAAA,SAAS,yBAAyB;AAClC,SAAS,gCAAgC;AACzC,SAAS,iCAAiC;AAC1C,SAAS,wBAAwB;;;ACEjC,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAEP,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;AAG9B,SAAS,kBAAkB;AAC3B,SAAS,eAAe,gBAAgB;AAExC,SAAS,4BAA4B;AACrC,SAAS,4BAA4B;AACrC,SAAS,qBAAqB;AAEvB,IAAM,yBAAsD;AAAA,EACjE,oBAAoB;AACtB;AAEO,IAA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AAa,SAAS,IAAI,eAAe,QAAQ;AAAA,EACrD;AAAA,EAEO,kBAA4D;AACjE,UAAM,EAAE,iBAAiB,SAAS,IAAI,KAAK;AAC3C,WAAO,SAAS,OAAO,eAAe;AAAA,EACxC;AACF;AAEO,IAAM,iBAAN,cACG,qBAEV;AAAA,EACE,YAAY,UAAoD;AAC9D;AAAA,MACE,CAAC,0BAA0B,2BAA2B,gBAAgB;AAAA,MACtE;AAAA,IACF;AAEA,SAAK,qBAAqB,IAAI,wBAAwB,QAAQ;AAAA,EAChE;AAAA,EAEO,SACL,UACsB;AACtB,WAAO,IAAI,SAAkB;AAC3B,aAAO,MAAM;AAAA,QACX;AAAA,UACE,iBAAiB,KAAK,mBAAmB,gBAAgB;AAAA,UACzD,UAAU,CAAC;AAAA,QACb;AAAA,QACA;AAAA,QACA,GAAG;AAAA,MACL;AAAA,IACF;AAAA,EACF;AAAA,EAEO,QAAc;AACnB,UAAM,MAAM;AACZ,UAAM,QAAQ;AAAA,EAChB;AACF;;;AExEO,IAAM,cAAc,IACtB,aACgB;AACnB,SAAO,IAAI,eAAe,QAAQ;AACpC;","names":["request"]}
|
|
1
|
+
{"version":3,"sources":["../../src/node/SetupServerApi.ts","../../src/node/SetupServerCommonApi.ts","../../src/node/setupServer.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks'\nimport { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { HandlersController } from '~/core/SetupApi'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport type { SetupServer } from './glossary'\nimport { SetupServerCommonApi } from './SetupServerCommonApi'\n\nconst store = new AsyncLocalStorage<RequestHandlersContext>()\n\ntype RequestHandlersContext = {\n initialHandlers: Array<RequestHandler | WebSocketHandler>\n handlers: Array<RequestHandler | WebSocketHandler>\n}\n\n/**\n * A handlers controller that utilizes `AsyncLocalStorage` in Node.js\n * to prevent the request handlers list from being a shared state\n * across mutliple tests.\n */\nclass AsyncHandlersController implements HandlersController {\n private rootContext: RequestHandlersContext\n\n constructor(initialHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.rootContext = { initialHandlers, handlers: [] }\n }\n\n get context(): RequestHandlersContext {\n return store.getStore() || this.rootContext\n }\n\n public prepend(runtimeHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.context.handlers.unshift(...runtimeHandlers)\n }\n\n public reset(nextHandlers: Array<RequestHandler | WebSocketHandler>) {\n const context = this.context\n context.handlers = []\n context.initialHandlers =\n nextHandlers.length > 0 ? nextHandlers : context.initialHandlers\n }\n\n public currentHandlers(): Array<RequestHandler | WebSocketHandler> {\n const { initialHandlers, handlers } = this.context\n return handlers.concat(initialHandlers)\n }\n}\n\nexport class SetupServerApi\n extends SetupServerCommonApi\n implements SetupServer\n{\n constructor(handlers: Array<RequestHandler | WebSocketHandler>) {\n super(\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],\n handlers,\n )\n\n this.handlersController = new AsyncHandlersController(handlers)\n }\n\n public boundary<Args extends Array<any>, R>(\n callback: (...args: Args) => R,\n ): (...args: Args) => R {\n return (...args: Args): R => {\n return store.run<any, any>(\n {\n initialHandlers: this.handlersController.currentHandlers(),\n handlers: [],\n },\n callback,\n ...args,\n )\n }\n }\n\n public close(): void {\n super.close()\n store.disable()\n }\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n\n // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\n })\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n","import type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { SetupServerApi } from './SetupServerApi'\n\n/**\n * Sets up a requests interception in Node.js with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport const setupServer = (\n ...handlers: Array<RequestHandler | WebSocketHandler>\n): SetupServerApi => {\n return new SetupServerApi(handlers)\n}\n"],"mappings":";AAAA,SAAS,yBAAyB;AAClC,SAAS,gCAAgC;AACzC,SAAS,iCAAiC;AAC1C,SAAS,wBAAwB;;;ACEjC,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAEP,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;AAG9B,SAAS,kBAAkB;AAC3B,SAAS,eAAe,gBAAgB;AAExC,SAAS,4BAA4B;AACrC,SAAS,4BAA4B;AACrC,SAAS,qBAAqB;AAE9B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,uBAAN,cACG,SAEV;AAAA,EACqB;AAAA,EAIX;AAAA,EAER,YACE,cACA,UACA;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,cAAc,IAAI,iBAAiB;AAAA,MACtC,MAAM;AAAA,MACN,cAAc,aAAa,IAAI,CAAC,gBAAgB,IAAI,YAAY,CAAC;AAAA,IACnE,CAAC;AAED,SAAK,kBAAkB,CAAC;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA,EAKQ,OAAa;AACnB,SAAK,YAAY;AAAA,MACf;AAAA,MACA,OAAO,EAAE,SAAS,WAAW,WAAW,MAAM;AAC5C,cAAM,WAAW,MAAM;AAAA,UACrB;AAAA,UACA;AAAA,UACA,KAAK,mBACF,gBAAgB,EAChB,OAAO,cAAc,gBAAgB,CAAC;AAAA,UACzC,KAAK;AAAA,UACL,KAAK;AAAA,UACL;AAAA,YACE,sBAAsBA,UAAS;AAC7B,oBAAM,eAAeA,SAAQ,QAAQ,IAAI,QAAQ;AAOjD,kBAAI,cAAc;AAChB,sBAAM,mBAAmB,aAAa;AAAA,kBACpC;AAAA,kBACA;AAAA,gBACF;AAEA,oBAAI,kBAAkB;AACpB,kBAAAA,SAAQ,QAAQ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
|
|
1
1
|
{
|
|
2
2
|
"name": "msw",
|
|
3
|
-
"version": "2.7.
|
|
3
|
+
"version": "2.7.6",
|
|
4
4
|
"description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
|
|
5
5
|
"main": "./lib/core/index.js",
|
|
6
6
|
"module": "./lib/core/index.mjs",
|
|
@@ -145,10 +145,8 @@
|
|
|
145
145
|
"@ossjs/release": "^0.8.1",
|
|
146
146
|
"@playwright/test": "^1.48.0",
|
|
147
147
|
"@types/express": "^4.17.21",
|
|
148
|
-
"@types/fs-extra": "^11.0.4",
|
|
149
|
-
"@types/glob": "^8.1.0",
|
|
150
148
|
"@types/json-bigint": "^1.0.4",
|
|
151
|
-
"@types/node": "18.
|
|
149
|
+
"@types/node": "~18.19.28",
|
|
152
150
|
"@typescript-eslint/eslint-plugin": "^8.8.1",
|
|
153
151
|
"@typescript-eslint/parser": "^8.8.1",
|
|
154
152
|
"@web/dev-server": "^0.4.6",
|
|
@@ -165,18 +163,17 @@
|
|
|
165
163
|
"eslint-plugin-prettier": "^5.2.1",
|
|
166
164
|
"express": "^5.0.0",
|
|
167
165
|
"fastify": "^4.26.0",
|
|
168
|
-
"fs-extra": "^11.2.0",
|
|
169
166
|
"fs-teardown": "^0.3.0",
|
|
170
167
|
"glob": "^11.0.0",
|
|
171
168
|
"jsdom": "^25.0.1",
|
|
172
169
|
"json-bigint": "^1.0.0",
|
|
170
|
+
"knip": "^5.51.1",
|
|
173
171
|
"lint-staged": "^15.2.10",
|
|
174
172
|
"page-with": "^0.6.1",
|
|
175
173
|
"prettier": "^3.4.2",
|
|
176
174
|
"regenerator-runtime": "^0.14.1",
|
|
177
175
|
"rimraf": "^6.0.1",
|
|
178
176
|
"simple-git-hooks": "^2.9.0",
|
|
179
|
-
"ts-node": "^10.9.2",
|
|
180
177
|
"tsup": "^8.3.0",
|
|
181
178
|
"typescript": "^5.5.2",
|
|
182
179
|
"undici": "^6.20.0",
|
|
@@ -221,6 +218,7 @@
|
|
|
221
218
|
"test:e2e": "vitest run --config=./test/e2e/vitest.config.mts",
|
|
222
219
|
"test:ts": "vitest --typecheck --config=./test/typings/vitest.config.mts",
|
|
223
220
|
"release": "release publish",
|
|
224
|
-
"postinstall": "node -e \"try{require('./config/scripts/postinstall')}catch(e){}\""
|
|
221
|
+
"postinstall": "node -e \"try{require('./config/scripts/postinstall')}catch(e){}\"",
|
|
222
|
+
"knip": "knip"
|
|
225
223
|
}
|
|
226
224
|
}
|
|
@@ -7,12 +7,9 @@ import {
|
|
|
7
7
|
import { ServiceWorkerMessage } from './start/utils/createMessageChannel'
|
|
8
8
|
import { RequestHandler } from '~/core/handlers/RequestHandler'
|
|
9
9
|
import type { HttpRequestEventMap, Interceptor } from '@mswjs/interceptors'
|
|
10
|
-
import type { Path } from '~/core/utils/matching/matchRequestUrl'
|
|
11
10
|
import type { RequiredDeep } from '~/core/typeUtils'
|
|
12
11
|
import type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'
|
|
13
12
|
|
|
14
|
-
export type ResolvedPath = Path | URL
|
|
15
|
-
|
|
16
13
|
type RequestWithoutMethods = Omit<
|
|
17
14
|
Request,
|
|
18
15
|
| 'text'
|
|
@@ -39,7 +36,7 @@ export interface ServiceWorkerIncomingRequest extends RequestWithoutMethods {
|
|
|
39
36
|
body?: ArrayBuffer | null
|
|
40
37
|
}
|
|
41
38
|
|
|
42
|
-
|
|
39
|
+
type ServiceWorkerIncomingResponse = Pick<
|
|
43
40
|
Response,
|
|
44
41
|
'type' | 'ok' | 'status' | 'statusText' | 'body' | 'headers' | 'redirected'
|
|
45
42
|
> & {
|
|
@@ -70,7 +67,7 @@ export interface ServiceWorkerIncomingEventsMap {
|
|
|
70
67
|
* Map of the events that can be sent to the Service Worker
|
|
71
68
|
* from any execution context.
|
|
72
69
|
*/
|
|
73
|
-
|
|
70
|
+
type ServiceWorkerOutgoingEventTypes =
|
|
74
71
|
| 'MOCK_ACTIVATE'
|
|
75
72
|
| 'MOCK_DEACTIVATE'
|
|
76
73
|
| 'INTEGRITY_CHECK_REQUEST'
|
|
@@ -81,7 +78,7 @@ export interface StringifiedResponse extends ResponseInit {
|
|
|
81
78
|
body: string | ArrayBuffer | ReadableStream<Uint8Array> | null
|
|
82
79
|
}
|
|
83
80
|
|
|
84
|
-
|
|
81
|
+
interface StrictEventListener<EventType extends Event> {
|
|
85
82
|
(event: EventType): void
|
|
86
83
|
}
|
|
87
84
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { ServiceWorkerIncomingEventsMap } from '
|
|
1
|
+
import type { ServiceWorkerIncomingEventsMap } from '../../glossary'
|
|
2
2
|
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
interface PrintStartMessageArgs {
|
|
5
5
|
quiet?: boolean
|
|
6
6
|
message?: string
|
|
7
7
|
workerUrl?: string
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import type { WebSocketConnectionData } from '@mswjs/interceptors/WebSocket'
|
|
2
|
+
import { WebSocketHandler } from './WebSocketHandler'
|
|
3
|
+
|
|
4
|
+
describe('parse', () => {
|
|
5
|
+
it('matches an exact url', () => {
|
|
6
|
+
expect(
|
|
7
|
+
new WebSocketHandler('ws://localhost:3000').parse({
|
|
8
|
+
event: new MessageEvent('connection', {
|
|
9
|
+
data: {
|
|
10
|
+
client: {
|
|
11
|
+
url: new URL('ws://localhost:3000'),
|
|
12
|
+
},
|
|
13
|
+
} as WebSocketConnectionData,
|
|
14
|
+
}),
|
|
15
|
+
}),
|
|
16
|
+
).toEqual({
|
|
17
|
+
match: {
|
|
18
|
+
matches: true,
|
|
19
|
+
params: {},
|
|
20
|
+
},
|
|
21
|
+
})
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('ignores trailing slash', () => {
|
|
25
|
+
expect(
|
|
26
|
+
new WebSocketHandler('ws://localhost:3000').parse({
|
|
27
|
+
event: new MessageEvent('connection', {
|
|
28
|
+
data: {
|
|
29
|
+
client: {
|
|
30
|
+
url: new URL('ws://localhost:3000/'),
|
|
31
|
+
},
|
|
32
|
+
} as WebSocketConnectionData,
|
|
33
|
+
}),
|
|
34
|
+
}),
|
|
35
|
+
).toEqual({
|
|
36
|
+
match: {
|
|
37
|
+
matches: true,
|
|
38
|
+
params: {},
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
expect(
|
|
43
|
+
new WebSocketHandler('ws://localhost:3000/').parse({
|
|
44
|
+
event: new MessageEvent('connection', {
|
|
45
|
+
data: {
|
|
46
|
+
client: {
|
|
47
|
+
url: new URL('ws://localhost:3000'),
|
|
48
|
+
},
|
|
49
|
+
} as WebSocketConnectionData,
|
|
50
|
+
}),
|
|
51
|
+
}),
|
|
52
|
+
).toEqual({
|
|
53
|
+
match: {
|
|
54
|
+
matches: true,
|
|
55
|
+
params: {},
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('supports path parameters', () => {
|
|
61
|
+
expect(
|
|
62
|
+
new WebSocketHandler('ws://localhost:3000/:serviceName').parse({
|
|
63
|
+
event: new MessageEvent('connection', {
|
|
64
|
+
data: {
|
|
65
|
+
client: {
|
|
66
|
+
url: new URL('ws://localhost:3000/auth'),
|
|
67
|
+
},
|
|
68
|
+
} as WebSocketConnectionData,
|
|
69
|
+
}),
|
|
70
|
+
}),
|
|
71
|
+
).toEqual({
|
|
72
|
+
match: {
|
|
73
|
+
matches: true,
|
|
74
|
+
params: {
|
|
75
|
+
serviceName: 'auth',
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('ignores "/socket.io/" prefix in the client url', () => {
|
|
82
|
+
expect(
|
|
83
|
+
new WebSocketHandler('ws://localhost:3000').parse({
|
|
84
|
+
event: new MessageEvent('connection', {
|
|
85
|
+
data: {
|
|
86
|
+
client: {
|
|
87
|
+
url: new URL(
|
|
88
|
+
'ws://localhost:3000/socket.io/?EIO=4&transport=websocket',
|
|
89
|
+
),
|
|
90
|
+
},
|
|
91
|
+
} as WebSocketConnectionData,
|
|
92
|
+
}),
|
|
93
|
+
}),
|
|
94
|
+
).toEqual({
|
|
95
|
+
match: {
|
|
96
|
+
matches: true,
|
|
97
|
+
params: {},
|
|
98
|
+
},
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
expect(
|
|
102
|
+
new WebSocketHandler('ws://localhost:3000/non-matching').parse({
|
|
103
|
+
event: new MessageEvent('connection', {
|
|
104
|
+
data: {
|
|
105
|
+
client: {
|
|
106
|
+
url: new URL(
|
|
107
|
+
'ws://localhost:3000/socket.io/?EIO=4&transport=websocket',
|
|
108
|
+
),
|
|
109
|
+
},
|
|
110
|
+
} as WebSocketConnectionData,
|
|
111
|
+
}),
|
|
112
|
+
}),
|
|
113
|
+
).toEqual({
|
|
114
|
+
match: {
|
|
115
|
+
matches: false,
|
|
116
|
+
params: {},
|
|
117
|
+
},
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('preserves non-prefix "/socket.io/" path segment', () => {
|
|
122
|
+
/**
|
|
123
|
+
* @note It is highly unlikely but we still shouldn't modify the
|
|
124
|
+
* WebSocket client URL if it contains a user-defined "socket.io" segment.
|
|
125
|
+
*/
|
|
126
|
+
expect(
|
|
127
|
+
new WebSocketHandler('ws://localhost:3000/clients/socket.io/123').parse({
|
|
128
|
+
event: new MessageEvent('connection', {
|
|
129
|
+
data: {
|
|
130
|
+
client: {
|
|
131
|
+
url: new URL('ws://localhost:3000/clients/socket.io/123'),
|
|
132
|
+
},
|
|
133
|
+
} as WebSocketConnectionData,
|
|
134
|
+
}),
|
|
135
|
+
}),
|
|
136
|
+
).toEqual({
|
|
137
|
+
match: {
|
|
138
|
+
matches: true,
|
|
139
|
+
params: {},
|
|
140
|
+
},
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
expect(
|
|
144
|
+
new WebSocketHandler('ws://localhost:3000').parse({
|
|
145
|
+
event: new MessageEvent('connection', {
|
|
146
|
+
data: {
|
|
147
|
+
client: {
|
|
148
|
+
url: new URL('ws://localhost:3000/clients/socket.io/123'),
|
|
149
|
+
},
|
|
150
|
+
} as WebSocketConnectionData,
|
|
151
|
+
}),
|
|
152
|
+
}),
|
|
153
|
+
).toEqual({
|
|
154
|
+
match: {
|
|
155
|
+
matches: false,
|
|
156
|
+
params: {},
|
|
157
|
+
},
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
})
|
|
@@ -47,8 +47,17 @@ export class WebSocketHandler {
|
|
|
47
47
|
public parse(args: {
|
|
48
48
|
event: MessageEvent<WebSocketConnectionData>
|
|
49
49
|
}): WebSocketHandlerParsedResult {
|
|
50
|
-
const connection = args.event
|
|
51
|
-
const
|
|
50
|
+
const { data: connection } = args.event
|
|
51
|
+
const { url: clientUrl } = connection.client
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @note Remove the Socket.IO path prefix from the WebSocket
|
|
55
|
+
* client URL. This is an exception to keep the users from
|
|
56
|
+
* including the implementation details in their handlers.
|
|
57
|
+
*/
|
|
58
|
+
clientUrl.pathname = clientUrl.pathname.replace(/^\/socket.io\//, '/')
|
|
59
|
+
|
|
60
|
+
const match = matchRequestUrl(clientUrl, this.url)
|
|
52
61
|
|
|
53
62
|
return {
|
|
54
63
|
match,
|
|
@@ -22,7 +22,7 @@ import { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'
|
|
|
22
22
|
import { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'
|
|
23
23
|
import { isHandlerKind } from '~/core/utils/internal/isHandlerKind'
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {
|
|
26
26
|
onUnhandledRequest: 'warn',
|
|
27
27
|
}
|
|
28
28
|
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Determines if the given value is shaped like a Node.js exception.
|
|
3
|
-
* Node.js exceptions have additional information, like
|
|
4
|
-
* the `code` and `errno` properties.
|
|
5
|
-
*
|
|
6
|
-
* In some environments, particularly jsdom/jest these may not
|
|
7
|
-
* instances of `Error` or its subclasses, despite being similar
|
|
8
|
-
* to them.
|
|
9
|
-
*/
|
|
10
|
-
export function isNodeExceptionLike(
|
|
11
|
-
error: unknown,
|
|
12
|
-
): error is NodeJS.ErrnoException {
|
|
13
|
-
return !!error && typeof error === 'object' && 'code' in error
|
|
14
|
-
}
|