msw 2.6.0 → 2.6.1
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 +4 -6
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +4 -6
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/{GraphQLHandler-Doool6Q_.d.mts → GraphQLHandler-B6uni-E_.d.mts} +1 -1
- package/lib/core/{GraphQLHandler-udzgBRPf.d.ts → GraphQLHandler-Cjm7JNGi.d.ts} +1 -1
- package/lib/core/{HttpResponse-BLGmJolh.d.mts → HttpResponse-63H9vVoL.d.mts} +2 -1
- package/lib/core/{HttpResponse-Cgbkdkje.d.ts → HttpResponse-DzhqZzTK.d.ts} +2 -1
- package/lib/core/HttpResponse.d.mts +1 -1
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/SetupApi.d.mts +1 -1
- package/lib/core/SetupApi.d.ts +1 -1
- package/lib/core/getResponse.d.mts +1 -1
- package/lib/core/getResponse.d.ts +1 -1
- package/lib/core/graphql.d.mts +2 -2
- package/lib/core/graphql.d.ts +2 -2
- package/lib/core/handlers/GraphQLHandler.d.mts +2 -2
- package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
- 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 +2 -0
- package/lib/core/handlers/RequestHandler.js.map +1 -1
- package/lib/core/handlers/RequestHandler.mjs +2 -0
- package/lib/core/handlers/RequestHandler.mjs.map +1 -1
- package/lib/core/handlers/WebSocketHandler.d.mts +1 -0
- package/lib/core/handlers/WebSocketHandler.d.ts +1 -0
- package/lib/core/handlers/WebSocketHandler.js +2 -0
- package/lib/core/handlers/WebSocketHandler.js.map +1 -1
- package/lib/core/handlers/WebSocketHandler.mjs +2 -0
- package/lib/core/handlers/WebSocketHandler.mjs.map +1 -1
- package/lib/core/handlers/common.d.mts +3 -0
- package/lib/core/handlers/common.d.ts +3 -0
- package/lib/core/handlers/common.js +17 -0
- package/lib/core/handlers/common.js.map +1 -0
- package/lib/core/handlers/common.mjs +1 -0
- package/lib/core/handlers/common.mjs.map +1 -0
- package/lib/core/http.d.mts +1 -1
- package/lib/core/http.d.ts +1 -1
- package/lib/core/index.d.mts +2 -2
- package/lib/core/index.d.ts +2 -2
- package/lib/core/passthrough.d.mts +1 -1
- package/lib/core/passthrough.d.ts +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/executeHandlers.d.mts +1 -1
- package/lib/core/utils/executeHandlers.d.ts +1 -1
- package/lib/core/utils/executeHandlers.js +0 -4
- package/lib/core/utils/executeHandlers.js.map +1 -1
- package/lib/core/utils/executeHandlers.mjs +0 -6
- package/lib/core/utils/executeHandlers.mjs.map +1 -1
- package/lib/core/utils/handleRequest.d.mts +1 -1
- package/lib/core/utils/handleRequest.d.ts +1 -1
- package/lib/core/utils/internal/isHandlerKind.d.mts +17 -0
- package/lib/core/utils/internal/isHandlerKind.d.ts +17 -0
- package/lib/core/utils/internal/isHandlerKind.js +29 -0
- package/lib/core/utils/internal/isHandlerKind.js.map +1 -0
- package/lib/core/utils/internal/isHandlerKind.mjs +9 -0
- package/lib/core/utils/internal/isHandlerKind.mjs.map +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.d.mts +2 -2
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
- 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/ws/handleWebSocketEvent.d.mts +1 -1
- package/lib/core/ws/handleWebSocketEvent.d.ts +1 -1
- package/lib/core/ws/handleWebSocketEvent.js +2 -1
- package/lib/core/ws/handleWebSocketEvent.js.map +1 -1
- package/lib/core/ws/handleWebSocketEvent.mjs +3 -2
- package/lib/core/ws/handleWebSocketEvent.mjs.map +1 -1
- package/lib/iife/index.js +14 -8
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.js +2 -5
- package/lib/native/index.js.map +1 -1
- package/lib/native/index.mjs +2 -5
- package/lib/native/index.mjs.map +1 -1
- package/lib/node/index.js +2 -5
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +2 -5
- package/lib/node/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/browser/setupWorker/start/createFallbackRequestListener.ts +2 -1
- package/src/browser/setupWorker/start/createRequestListener.ts +2 -7
- package/src/core/handlers/RequestHandler.ts +4 -0
- package/src/core/handlers/WebSocketHandler.ts +4 -0
- package/src/core/handlers/common.ts +1 -0
- package/src/core/utils/executeHandlers.ts +1 -5
- package/src/core/utils/internal/isHandlerKind.test.ts +64 -0
- package/src/core/utils/internal/isHandlerKind.ts +21 -0
- package/src/core/ws/handleWebSocketEvent.ts +2 -1
- package/src/node/SetupServerCommonApi.ts +4 -8
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.6.
|
|
11
|
+
const PACKAGE_VERSION = '2.6.1'
|
|
12
12
|
const INTEGRITY_CHECKSUM = '07a8241b182f8a246a7cd39894799a9e'
|
|
13
13
|
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
|
|
14
14
|
const activeClientIds = new Set()
|
package/lib/native/index.js
CHANGED
|
@@ -31,12 +31,11 @@ var import_outvariant = require("outvariant");
|
|
|
31
31
|
var import_interceptors = require("@mswjs/interceptors");
|
|
32
32
|
var import_SetupApi = require("../core/SetupApi.js");
|
|
33
33
|
var import_handleRequest = require("../core/utils/handleRequest.js");
|
|
34
|
-
var import_HttpHandler = require("../core/handlers/HttpHandler.js");
|
|
35
|
-
var import_GraphQLHandler = require("../core/handlers/GraphQLHandler.js");
|
|
36
34
|
var import_mergeRight = require("../core/utils/internal/mergeRight.js");
|
|
37
35
|
var import_devUtils = require("../core/utils/internal/devUtils.js");
|
|
38
36
|
var import_handleWebSocketEvent = require("../core/ws/handleWebSocketEvent.js");
|
|
39
37
|
var import_webSocketInterceptor = require("../core/ws/webSocketInterceptor.js");
|
|
38
|
+
var import_isHandlerKind = require("../core/utils/internal/isHandlerKind.js");
|
|
40
39
|
var DEFAULT_LISTEN_OPTIONS = {
|
|
41
40
|
onUnhandledRequest: "warn"
|
|
42
41
|
};
|
|
@@ -62,9 +61,7 @@ var SetupServerCommonApi = class extends import_SetupApi.SetupApi {
|
|
|
62
61
|
const response = await (0, import_handleRequest.handleRequest)(
|
|
63
62
|
request,
|
|
64
63
|
requestId,
|
|
65
|
-
this.handlersController.currentHandlers().filter((
|
|
66
|
-
return handler instanceof import_HttpHandler.HttpHandler || handler instanceof import_GraphQLHandler.GraphQLHandler;
|
|
67
|
-
}),
|
|
64
|
+
this.handlersController.currentHandlers().filter((0, import_isHandlerKind.isHandlerKind)("RequestHandler")),
|
|
68
65
|
this.resolvedOptions,
|
|
69
66
|
this.emitter
|
|
70
67
|
);
|
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
|
|
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\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 this.init()\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\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 this.interceptor.apply()\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;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAiC;AACjC,4BAA0C;;;ACI1C,wBAA0B;AAC1B,0BAKO;AAEP,sBAAyB;AACzB,2BAA8B;AAG9B,wBAA2B;AAC3B,sBAAwC;AAExC,kCAAqC;AACrC,kCAAqC;AACrC,2BAA8B;AAEvB,IAAM,yBAAsD;AAAA,EACjE,oBAAoB;AACtB;AAEO,IAAM,uBAAN,cACG,yBAEV;AAAA,EACqB;AAAA,EAIX;AAAA,EAER,YACE,cACA,UACA;AACA,UAAM,GAAG,QA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IACF;AAGA,SAAK,YAAY,MAAM;AACvB,SAAK,cAAc,KAAK,MAAM,KAAK,YAAY,QAAQ,CAAC;AAGxD,qDAAqB,MAAM;AAC3B,SAAK,cAAc,KAAK,MAAM,iDAAqB,QAAQ,CAAC;AAK5D;AAAA,MACE,CAAC,0CAAsB,UAAU,0CAAsB,OAAO,EAAE;AAAA,QAC9D,KAAK,YAAY;AAAA,MACnB;AAAA,MACA,yBAAS;AAAA,QACP;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA,EAEO,QAAc;AACnB,SAAK,QAAQ;AAAA,EACf;AACF;;;ADrIO,SAAS,eACX,UACmB;AAGtB,SAAO,IAAI;AAAA,IACT,CAAC,+BAAkB,+CAAyB;AAAA,IAC5C;AAAA,EACF;AACF;","names":[]}
|
package/lib/native/index.mjs
CHANGED
|
@@ -10,12 +10,11 @@ import {
|
|
|
10
10
|
} from "@mswjs/interceptors";
|
|
11
11
|
import { SetupApi } from '../core/SetupApi.mjs';
|
|
12
12
|
import { handleRequest } from '../core/utils/handleRequest.mjs';
|
|
13
|
-
import { HttpHandler } from '../core/handlers/HttpHandler.mjs';
|
|
14
|
-
import { GraphQLHandler } from '../core/handlers/GraphQLHandler.mjs';
|
|
15
13
|
import { mergeRight } from '../core/utils/internal/mergeRight.mjs';
|
|
16
14
|
import { InternalError, devUtils } from '../core/utils/internal/devUtils.mjs';
|
|
17
15
|
import { handleWebSocketEvent } from '../core/ws/handleWebSocketEvent.mjs';
|
|
18
16
|
import { webSocketInterceptor } from '../core/ws/webSocketInterceptor.mjs';
|
|
17
|
+
import { isHandlerKind } from '../core/utils/internal/isHandlerKind.mjs';
|
|
19
18
|
var DEFAULT_LISTEN_OPTIONS = {
|
|
20
19
|
onUnhandledRequest: "warn"
|
|
21
20
|
};
|
|
@@ -41,9 +40,7 @@ var SetupServerCommonApi = class extends SetupApi {
|
|
|
41
40
|
const response = await handleRequest(
|
|
42
41
|
request,
|
|
43
42
|
requestId,
|
|
44
|
-
this.handlersController.currentHandlers().filter((
|
|
45
|
-
return handler instanceof HttpHandler || handler instanceof GraphQLHandler;
|
|
46
|
-
}),
|
|
43
|
+
this.handlersController.currentHandlers().filter(isHandlerKind("RequestHandler")),
|
|
47
44
|
this.resolvedOptions,
|
|
48
45
|
this.emitter
|
|
49
46
|
);
|
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
|
|
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\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 this.init()\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\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 this.interceptor.apply()\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;AAEvB,IAAM,yBAAsD;AAAA,EACjE,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;AAExB,SAAK,KAAK;AAAA,EACZ;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,QACP;AAEA,YAAI,UAAU;AACZ,qBAAW,YAAY,QAAQ;AAAA,QACjC;AAEA;AAAA,MACF;AAAA,IACF;AAEA,SAAK,YAAY,GAAG,sBAAsB,CAAC,EAAE,MAAM,MAAM;AACvD,UAAI,iBAAiB,eAAe;AAClC,cAAM;AAAA,MACR;AAAA,IACF,CAAC;AAED,SAAK,YAAY;AAAA,MACf;AAAA,MACA,CAAC,EAAE,UAAU,kBAAkB,SAAS,UAAU,MAAM;AACtD,aAAK,QAAQ;AAAA,UACX,mBAAmB,oBAAoB;AAAA,UACvC;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAIA,yBAAqB;AAAA,MACnB,6BAA6B,MAAM;AACjC,eAAO,KAAK,gBAAgB;AAAA,MAC9B;AAAA,MACA,aAAa,MAAM;AACjB,eAAO,KAAK,mBAAmB,gBAAgB;AAAA,MACjD;AAAA,MACA,oBAAoB,MAAM;AAAA,MAAC;AAAA,MAC3B,yBAAyB,MAAM;AAAA,MAAC;AAAA,IAClC,CAAC;AAAA,EACH;AAAA,EAEO,OAAO,UAAkC,CAAC,GAAS;AACxD,SAAK,kBAAkB;AAAA,MACrB;AAAA,MACA;AAAA,IACF;AAGA,SAAK,YAAY,MAAM;AACvB,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;;;ADrIO,SAAS,eACX,UACmB;AAGtB,SAAO,IAAI;AAAA,IACT,CAAC,kBAAkB,yBAAyB;AAAA,IAC5C;AAAA,EACF;AACF;","names":[]}
|
package/lib/node/index.js
CHANGED
|
@@ -36,12 +36,11 @@ var import_outvariant = require("outvariant");
|
|
|
36
36
|
var import_interceptors = require("@mswjs/interceptors");
|
|
37
37
|
var import_SetupApi = require("../core/SetupApi.js");
|
|
38
38
|
var import_handleRequest = require("../core/utils/handleRequest.js");
|
|
39
|
-
var import_HttpHandler = require("../core/handlers/HttpHandler.js");
|
|
40
|
-
var import_GraphQLHandler = require("../core/handlers/GraphQLHandler.js");
|
|
41
39
|
var import_mergeRight = require("../core/utils/internal/mergeRight.js");
|
|
42
40
|
var import_devUtils = require("../core/utils/internal/devUtils.js");
|
|
43
41
|
var import_handleWebSocketEvent = require("../core/ws/handleWebSocketEvent.js");
|
|
44
42
|
var import_webSocketInterceptor = require("../core/ws/webSocketInterceptor.js");
|
|
43
|
+
var import_isHandlerKind = require("../core/utils/internal/isHandlerKind.js");
|
|
45
44
|
var DEFAULT_LISTEN_OPTIONS = {
|
|
46
45
|
onUnhandledRequest: "warn"
|
|
47
46
|
};
|
|
@@ -67,9 +66,7 @@ var SetupServerCommonApi = class extends import_SetupApi.SetupApi {
|
|
|
67
66
|
const response = await (0, import_handleRequest.handleRequest)(
|
|
68
67
|
request,
|
|
69
68
|
requestId,
|
|
70
|
-
this.handlersController.currentHandlers().filter((
|
|
71
|
-
return handler instanceof import_HttpHandler.HttpHandler || handler instanceof import_GraphQLHandler.GraphQLHandler;
|
|
72
|
-
}),
|
|
69
|
+
this.handlersController.currentHandlers().filter((0, import_isHandlerKind.isHandlerKind)("RequestHandler")),
|
|
73
70
|
this.resolvedOptions,
|
|
74
71
|
this.emitter
|
|
75
72
|
);
|
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 { HttpHandler } from '~/core/handlers/HttpHandler'\nimport { GraphQLHandler } from '~/core/handlers/GraphQLHandler'\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'\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 this.init()\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.currentHandlers().filter((handler) => {\n return (\n handler instanceof HttpHandler ||\n handler instanceof GraphQLHandler\n )\n }),\n this.resolvedOptions,\n this.emitter,\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 this.interceptor.apply()\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;AAE9B,yBAA4B;AAC5B,4BAA+B;AAE/B,wBAA2B;AAC3B,sBAAwC;AAExC,kCAAqC;AACrC,kCAAqC;AAE9B,IAAM,yBAAsD;AAAA,EACjE,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;AAExB,SAAK,KAAK;AAAA,EACZ;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,mBAAmB,gBAAgB,EAAE,OAAO,CAAC,YAAY;AAC5D,mBACE,mBAAmB,kCACnB,mBAAmB;AAAA,UAEvB,CAAC;AAAA,UACD,KAAK;AAAA,UACL,KAAK;AAAA,QACP;AAEA,YAAI,UAAU;AACZ,qBAAW,YAAY,QAAQ;AAAA,QACjC;AAEA;AAAA,MACF;AAAA,IACF;AAEA,SAAK,YAAY,GAAG,sBAAsB,CAAC,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M,SAAS,KAAK,KAAK;AAAA,EAClC;AAAA,EAEO,QAAQ,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":[]}
|
|
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 this.init()\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\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 this.interceptor.apply()\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;AAEvB,IAAM,yBAAsD;AAAA,EACjE,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;AAExB,SAAK,KAAK;AAAA,EACZ;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,QACP;AAEA,YAAI,UAAU;AACZ,qBAAW,YAAY,QAAQ;AAAA,QACjC;AAEA;AAAA,MACF;AAAA,IACF;AAEA,SAAK,YAAY,GAAG,sBAAsB,CAAC,EAAE,MAAM,MAAM;AACvD,UAAI,iBAAiB,+BAAe;AAClC,cAAM;AAAA,MACR;AAAA,IACF,CAAC;AAED,SAAK,YAAY;AAAA,MACf;AAAA,MACA,CAAC,EAAE,UAAU,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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":[]}
|
package/lib/node/index.mjs
CHANGED
|
@@ -12,12 +12,11 @@ import {
|
|
|
12
12
|
} from "@mswjs/interceptors";
|
|
13
13
|
import { SetupApi } from '../core/SetupApi.mjs';
|
|
14
14
|
import { handleRequest } from '../core/utils/handleRequest.mjs';
|
|
15
|
-
import { HttpHandler } from '../core/handlers/HttpHandler.mjs';
|
|
16
|
-
import { GraphQLHandler } from '../core/handlers/GraphQLHandler.mjs';
|
|
17
15
|
import { mergeRight } from '../core/utils/internal/mergeRight.mjs';
|
|
18
16
|
import { InternalError, devUtils } from '../core/utils/internal/devUtils.mjs';
|
|
19
17
|
import { handleWebSocketEvent } from '../core/ws/handleWebSocketEvent.mjs';
|
|
20
18
|
import { webSocketInterceptor } from '../core/ws/webSocketInterceptor.mjs';
|
|
19
|
+
import { isHandlerKind } from '../core/utils/internal/isHandlerKind.mjs';
|
|
21
20
|
var DEFAULT_LISTEN_OPTIONS = {
|
|
22
21
|
onUnhandledRequest: "warn"
|
|
23
22
|
};
|
|
@@ -43,9 +42,7 @@ var SetupServerCommonApi = class extends SetupApi {
|
|
|
43
42
|
const response = await handleRequest(
|
|
44
43
|
request,
|
|
45
44
|
requestId,
|
|
46
|
-
this.handlersController.currentHandlers().filter((
|
|
47
|
-
return handler instanceof HttpHandler || handler instanceof GraphQLHandler;
|
|
48
|
-
}),
|
|
45
|
+
this.handlersController.currentHandlers().filter(isHandlerKind("RequestHandler")),
|
|
49
46
|
this.resolvedOptions,
|
|
50
47
|
this.emitter
|
|
51
48
|
);
|
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 { HttpHandler } from '~/core/handlers/HttpHandler'\nimport { GraphQLHandler } from '~/core/handlers/GraphQLHandler'\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'\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 this.init()\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.currentHandlers().filter((handler) => {\n return (\n handler instanceof HttpHandler ||\n handler instanceof GraphQLHandler\n )\n }),\n this.resolvedOptions,\n this.emitter,\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 this.interceptor.apply()\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;AAE9B,SAAS,mBAAmB;AAC5B,SAAS,sBAAsB;AAE/B,SAAS,kBAAkB;AAC3B,SAAS,eAAe,gBAAgB;AAExC,SAAS,4BAA4B;AAC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ACE,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":[]}
|
|
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 this.init()\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\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 this.interceptor.apply()\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,IAAM,uBAAN,cACG,SAEV;AAAA,EACqB;AAAA,EAIX;AAAA,EAER,YACE,cAC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ACX;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":[]}
|
package/package.json
CHANGED
|
@@ -8,6 +8,7 @@ import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'
|
|
|
8
8
|
import { SetupWorkerInternalContext, StartOptions } from '../glossary'
|
|
9
9
|
import type { RequiredDeep } from '~/core/typeUtils'
|
|
10
10
|
import { handleRequest } from '~/core/utils/handleRequest'
|
|
11
|
+
import { isHandlerKind } from '~/core/utils/internal/isHandlerKind'
|
|
11
12
|
|
|
12
13
|
export function createFallbackRequestListener(
|
|
13
14
|
context: SetupWorkerInternalContext,
|
|
@@ -24,7 +25,7 @@ export function createFallbackRequestListener(
|
|
|
24
25
|
const response = await handleRequest(
|
|
25
26
|
request,
|
|
26
27
|
requestId,
|
|
27
|
-
context.getRequestHandlers(),
|
|
28
|
+
context.getRequestHandlers().filter(isHandlerKind('RequestHandler')),
|
|
28
29
|
options,
|
|
29
30
|
context.emitter,
|
|
30
31
|
{
|
|
@@ -9,12 +9,11 @@ import {
|
|
|
9
9
|
} from './utils/createMessageChannel'
|
|
10
10
|
import { parseWorkerRequest } from '../../utils/parseWorkerRequest'
|
|
11
11
|
import { RequestHandler } from '~/core/handlers/RequestHandler'
|
|
12
|
-
import { HttpHandler } from '~/core/handlers/HttpHandler'
|
|
13
|
-
import { GraphQLHandler } from '~/core/handlers/GraphQLHandler'
|
|
14
12
|
import { handleRequest } from '~/core/utils/handleRequest'
|
|
15
13
|
import { RequiredDeep } from '~/core/typeUtils'
|
|
16
14
|
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
17
15
|
import { toResponseInit } from '~/core/utils/toResponseInit'
|
|
16
|
+
import { isHandlerKind } from '~/core/utils/internal/isHandlerKind'
|
|
18
17
|
|
|
19
18
|
export const createRequestListener = (
|
|
20
19
|
context: SetupWorkerInternalContext,
|
|
@@ -45,11 +44,7 @@ export const createRequestListener = (
|
|
|
45
44
|
await handleRequest(
|
|
46
45
|
request,
|
|
47
46
|
requestId,
|
|
48
|
-
context.getRequestHandlers().filter((
|
|
49
|
-
return (
|
|
50
|
-
handler instanceof HttpHandler || handler instanceof GraphQLHandler
|
|
51
|
-
)
|
|
52
|
-
}),
|
|
47
|
+
context.getRequestHandlers().filter(isHandlerKind('RequestHandler')),
|
|
53
48
|
options,
|
|
54
49
|
context.emitter,
|
|
55
50
|
{
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
import type { ResponseResolutionContext } from '../utils/executeHandlers'
|
|
8
8
|
import type { MaybePromise } from '../typeUtils'
|
|
9
9
|
import { StrictRequest, StrictResponse } from '..//HttpResponse'
|
|
10
|
+
import type { HandlerKind } from './common'
|
|
10
11
|
|
|
11
12
|
export type DefaultRequestMultipartBody = Record<
|
|
12
13
|
string,
|
|
@@ -117,6 +118,8 @@ export abstract class RequestHandler<
|
|
|
117
118
|
StrictRequest<DefaultBodyType>
|
|
118
119
|
>()
|
|
119
120
|
|
|
121
|
+
private readonly __kind: HandlerKind
|
|
122
|
+
|
|
120
123
|
public info: HandlerInfo & RequestHandlerInternalInfo
|
|
121
124
|
/**
|
|
122
125
|
* Indicates whether this request handler has been used
|
|
@@ -151,6 +154,7 @@ export abstract class RequestHandler<
|
|
|
151
154
|
}
|
|
152
155
|
|
|
153
156
|
this.isUsed = false
|
|
157
|
+
this.__kind = 'RequestHandler'
|
|
154
158
|
}
|
|
155
159
|
|
|
156
160
|
/**
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
matchRequestUrl,
|
|
9
9
|
} from '../utils/matching/matchRequestUrl'
|
|
10
10
|
import { getCallFrame } from '../utils/internal/getCallFrame'
|
|
11
|
+
import type { HandlerKind } from './common'
|
|
11
12
|
|
|
12
13
|
type WebSocketHandlerParsedResult = {
|
|
13
14
|
match: Match
|
|
@@ -28,6 +29,8 @@ const kStopPropagationPatched = Symbol('kStopPropagationPatched')
|
|
|
28
29
|
const KOnStopPropagation = Symbol('KOnStopPropagation')
|
|
29
30
|
|
|
30
31
|
export class WebSocketHandler {
|
|
32
|
+
private readonly __kind: HandlerKind
|
|
33
|
+
|
|
31
34
|
public id: string
|
|
32
35
|
public callFrame?: string
|
|
33
36
|
|
|
@@ -38,6 +41,7 @@ export class WebSocketHandler {
|
|
|
38
41
|
|
|
39
42
|
this[kEmitter] = new Emitter()
|
|
40
43
|
this.callFrame = getCallFrame(new Error())
|
|
44
|
+
this.__kind = 'EventHandler'
|
|
41
45
|
}
|
|
42
46
|
|
|
43
47
|
public parse(args: {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type HandlerKind = 'RequestHandler' | 'EventHandler'
|
|
@@ -18,7 +18,7 @@ export interface ResponseResolutionContext {
|
|
|
18
18
|
* Returns the execution result object containing any matching request
|
|
19
19
|
* handler and any mocked response it returned.
|
|
20
20
|
*/
|
|
21
|
-
export const executeHandlers = async <Handlers extends Array<
|
|
21
|
+
export const executeHandlers = async <Handlers extends Array<RequestHandler>>({
|
|
22
22
|
request,
|
|
23
23
|
requestId,
|
|
24
24
|
handlers,
|
|
@@ -33,10 +33,6 @@ export const executeHandlers = async <Handlers extends Array<unknown>>({
|
|
|
33
33
|
let result: RequestHandlerExecutionResult<any> | null = null
|
|
34
34
|
|
|
35
35
|
for (const handler of handlers) {
|
|
36
|
-
if (!(handler instanceof RequestHandler)) {
|
|
37
|
-
continue
|
|
38
|
-
}
|
|
39
|
-
|
|
40
36
|
result = await handler.run({ request, requestId, resolutionContext })
|
|
41
37
|
|
|
42
38
|
// If the handler produces some result for this request,
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { GraphQLHandler } from '../../handlers/GraphQLHandler'
|
|
2
|
+
import { HttpHandler } from '../../handlers/HttpHandler'
|
|
3
|
+
import { RequestHandler } from '../../handlers/RequestHandler'
|
|
4
|
+
import { WebSocketHandler } from '../../handlers/WebSocketHandler'
|
|
5
|
+
import { isHandlerKind } from './isHandlerKind'
|
|
6
|
+
|
|
7
|
+
it('returns true if expected a request handler and given a request handler', () => {
|
|
8
|
+
expect(
|
|
9
|
+
isHandlerKind('RequestHandler')(new HttpHandler('*', '*', () => {})),
|
|
10
|
+
).toBe(true)
|
|
11
|
+
|
|
12
|
+
expect(
|
|
13
|
+
isHandlerKind('RequestHandler')(
|
|
14
|
+
new GraphQLHandler('all', '*', '*', () => {}),
|
|
15
|
+
),
|
|
16
|
+
).toBe(true)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('returns true if expected a request handler and given a custom request handler', () => {
|
|
20
|
+
class MyHandler extends RequestHandler {
|
|
21
|
+
constructor() {
|
|
22
|
+
super({ info: { header: '*' }, resolver: () => {} })
|
|
23
|
+
}
|
|
24
|
+
predicate = () => false
|
|
25
|
+
log() {}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
expect(isHandlerKind('RequestHandler')(new MyHandler())).toBe(true)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('returns false if expected a request handler but given event handler', () => {
|
|
32
|
+
expect(isHandlerKind('RequestHandler')(new WebSocketHandler('*'))).toBe(false)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('returns false if expected a request handler but given arbitrary object', () => {
|
|
36
|
+
expect(isHandlerKind('RequestHandler')(undefined)).toBe(false)
|
|
37
|
+
expect(isHandlerKind('RequestHandler')(null)).toBe(false)
|
|
38
|
+
expect(isHandlerKind('RequestHandler')({})).toBe(false)
|
|
39
|
+
expect(isHandlerKind('RequestHandler')([])).toBe(false)
|
|
40
|
+
expect(isHandlerKind('RequestHandler')(123)).toBe(false)
|
|
41
|
+
expect(isHandlerKind('RequestHandler')('hello')).toBe(false)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('returns true if expected an event handler and given an event handler', () => {
|
|
45
|
+
expect(isHandlerKind('EventHandler')(new WebSocketHandler('*'))).toBe(true)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('returns true if expected an event handler and given a custom event handler', () => {
|
|
49
|
+
class MyEventHandler extends WebSocketHandler {
|
|
50
|
+
constructor() {
|
|
51
|
+
super('*')
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
expect(isHandlerKind('EventHandler')(new MyEventHandler())).toBe(true)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('returns false if expected an event handler but given arbitrary object', () => {
|
|
58
|
+
expect(isHandlerKind('EventHandler')(undefined)).toBe(false)
|
|
59
|
+
expect(isHandlerKind('EventHandler')(null)).toBe(false)
|
|
60
|
+
expect(isHandlerKind('EventHandler')({})).toBe(false)
|
|
61
|
+
expect(isHandlerKind('EventHandler')([])).toBe(false)
|
|
62
|
+
expect(isHandlerKind('EventHandler')(123)).toBe(false)
|
|
63
|
+
expect(isHandlerKind('EventHandler')('hello')).toBe(false)
|
|
64
|
+
})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { HandlerKind } from '../../handlers/common'
|
|
2
|
+
import type { RequestHandler } from '../../handlers/RequestHandler'
|
|
3
|
+
import type { WebSocketHandler } from '../../handlers/WebSocketHandler'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A filter function that ensures that the provided argument
|
|
7
|
+
* is a handler of the given kind. This helps differentiate
|
|
8
|
+
* between different kinds of handlers, e.g. request and event handlers.
|
|
9
|
+
*/
|
|
10
|
+
export function isHandlerKind<K extends HandlerKind>(kind: K) {
|
|
11
|
+
return (
|
|
12
|
+
input: unknown,
|
|
13
|
+
): input is K extends 'EventHandler' ? WebSocketHandler : RequestHandler => {
|
|
14
|
+
return (
|
|
15
|
+
input != null &&
|
|
16
|
+
typeof input === 'object' &&
|
|
17
|
+
'__kind' in input &&
|
|
18
|
+
input.__kind === kind
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
onUnhandledRequest,
|
|
7
7
|
UnhandledRequestStrategy,
|
|
8
8
|
} from '../utils/request/onUnhandledRequest'
|
|
9
|
+
import { isHandlerKind } from '../utils/internal/isHandlerKind'
|
|
9
10
|
|
|
10
11
|
interface HandleWebSocketEventOptions {
|
|
11
12
|
getUnhandledRequestStrategy: () => UnhandledRequestStrategy
|
|
@@ -30,7 +31,7 @@ export function handleWebSocketEvent(options: HandleWebSocketEventOptions) {
|
|
|
30
31
|
|
|
31
32
|
for (const handler of handlers) {
|
|
32
33
|
if (
|
|
33
|
-
handler
|
|
34
|
+
isHandlerKind('EventHandler')(handler) &&
|
|
34
35
|
handler.predicate({
|
|
35
36
|
event: connectionEvent,
|
|
36
37
|
parsedResult: handler.parse({
|
|
@@ -14,14 +14,13 @@ import type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'
|
|
|
14
14
|
import { SetupApi } from '~/core/SetupApi'
|
|
15
15
|
import { handleRequest } from '~/core/utils/handleRequest'
|
|
16
16
|
import type { RequestHandler } from '~/core/handlers/RequestHandler'
|
|
17
|
-
import { HttpHandler } from '~/core/handlers/HttpHandler'
|
|
18
|
-
import { GraphQLHandler } from '~/core/handlers/GraphQLHandler'
|
|
19
17
|
import type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'
|
|
20
18
|
import { mergeRight } from '~/core/utils/internal/mergeRight'
|
|
21
19
|
import { InternalError, devUtils } from '~/core/utils/internal/devUtils'
|
|
22
20
|
import type { SetupServerCommon } from './glossary'
|
|
23
21
|
import { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'
|
|
24
22
|
import { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'
|
|
23
|
+
import { isHandlerKind } from '~/core/utils/internal/isHandlerKind'
|
|
25
24
|
|
|
26
25
|
export const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {
|
|
27
26
|
onUnhandledRequest: 'warn',
|
|
@@ -63,12 +62,9 @@ export class SetupServerCommonApi
|
|
|
63
62
|
const response = await handleRequest(
|
|
64
63
|
request,
|
|
65
64
|
requestId,
|
|
66
|
-
this.handlersController
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
handler instanceof GraphQLHandler
|
|
70
|
-
)
|
|
71
|
-
}),
|
|
65
|
+
this.handlersController
|
|
66
|
+
.currentHandlers()
|
|
67
|
+
.filter(isHandlerKind('RequestHandler')),
|
|
72
68
|
this.resolvedOptions,
|
|
73
69
|
this.emitter,
|
|
74
70
|
)
|