msw 2.14.4 → 2.14.5
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/core/experimental/sources/interceptor-source.js +9 -0
- package/lib/core/experimental/sources/interceptor-source.js.map +1 -1
- package/lib/core/experimental/sources/interceptor-source.mjs +9 -0
- package/lib/core/experimental/sources/interceptor-source.mjs.map +1 -1
- package/lib/iife/index.js +9 -0
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/package.json +2 -2
- package/src/core/experimental/sources/interceptor-source.ts +14 -0
|
@@ -126,6 +126,15 @@ class InterceptorHttpNetworkFrame extends import_http_frame.HttpNetworkFrame {
|
|
|
126
126
|
class InterceptorWebSocketNetworkFrame extends import_websocket_frame.WebSocketNetworkFrame {
|
|
127
127
|
constructor(args) {
|
|
128
128
|
super({ connection: args.connection });
|
|
129
|
+
args.connection.client.addEventListener(
|
|
130
|
+
"close",
|
|
131
|
+
() => {
|
|
132
|
+
this.events.removeAllListeners();
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
once: true
|
|
136
|
+
}
|
|
137
|
+
);
|
|
129
138
|
}
|
|
130
139
|
errorWith(reason) {
|
|
131
140
|
if (reason instanceof Error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/core/experimental/sources/interceptor-source.ts"],"sourcesContent":["import type { Interceptor, RequestController } from '@mswjs/interceptors'\nimport { BatchInterceptor, type HttpRequestEventMap } from '@mswjs/interceptors'\nimport type {\n WebSocketConnectionData,\n WebSocketEventMap,\n} from '@mswjs/interceptors/WebSocket'\nimport { NetworkSource } from './network-source'\nimport { InternalError } from '../../utils/internal/devUtils'\nimport { HttpNetworkFrame, ResponseEvent } from '../frames/http-frame'\nimport { WebSocketNetworkFrame } from '../frames/websocket-frame'\nimport { deleteRequestPassthroughHeader } from '../request-utils'\n\nexport interface InterceptorSourceOptions {\n interceptors: Array<Interceptor<HttpRequestEventMap | WebSocketEventMap>>\n}\n\n/**\n * Create a network source from the given list of interceptors.\n */\nexport class InterceptorSource extends NetworkSource {\n #interceptor: BatchInterceptor<\n InterceptorSourceOptions['interceptors'],\n HttpRequestEventMap | WebSocketEventMap\n >\n\n #frames: Map<string, HttpNetworkFrame>\n\n constructor(options: InterceptorSourceOptions) {\n super()\n\n this.#interceptor = new BatchInterceptor({\n name: 'interceptor-source',\n interceptors: options.interceptors,\n })\n this.#frames = new Map()\n }\n\n public enable(): void {\n this.#interceptor.apply()\n\n /**\n * @todo @fixme BatchInterceptor infers event types but not listener types.\n */\n this.#interceptor\n .on('request', this.#handleRequest.bind(this) as any)\n .on('response', this.#handleResponse.bind(this) as any)\n .on('connection', this.#handleWebSocketConnection.bind(this) as any)\n }\n\n public disable(): void {\n super.disable()\n this.#interceptor.dispose()\n\n /**\n * @todo We can also abort any pending frames here, given we implement\n * the `NetworkFrame.abort()` method.\n */\n this.#frames.clear()\n }\n\n async #handleRequest({\n requestId,\n request,\n controller,\n }: HttpRequestEventMap['request'][0]): Promise<void> {\n const httpFrame = new InterceptorHttpNetworkFrame({\n id: requestId,\n request,\n controller,\n })\n\n this.#frames.set(requestId, httpFrame)\n await this.queue(httpFrame)\n }\n\n async #handleResponse({\n requestId,\n request,\n response,\n isMockedResponse,\n }: HttpRequestEventMap['response'][0]): Promise<void> {\n const httpFrame = this.#frames.get(requestId)\n this.#frames.delete(requestId)\n\n if (httpFrame == null) {\n return\n }\n\n queueMicrotask(() => {\n try {\n httpFrame.events.emit(\n new ResponseEvent(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n requestId,\n request,\n response,\n },\n ),\n )\n } finally {\n /**\n * @note Remove any listeners from this frame.\n * Past this point, it won't emit anything. The removal is crucial\n * to prevent \"rettime\" from keeping the abort cleanup listeners internally.\n * @see https://github.com/mswjs/msw/issues/2735\n */\n httpFrame.events.removeAllListeners()\n }\n })\n }\n\n async #handleWebSocketConnection(\n connection: WebSocketEventMap['connection'][0],\n ): Promise<void> {\n await this.queue(\n new InterceptorWebSocketNetworkFrame({\n connection,\n }),\n )\n }\n}\n\nclass InterceptorHttpNetworkFrame extends HttpNetworkFrame {\n #controller: RequestController\n\n constructor(options: {\n id: string\n request: Request\n controller: RequestController\n }) {\n super({\n id: options.id,\n request: options.request,\n })\n\n this.#controller = options.controller\n }\n\n public passthrough(): void {\n deleteRequestPassthroughHeader(this.data.request)\n }\n\n public respondWith(response?: Response): void {\n if (response) {\n this.#controller.respondWith(response)\n }\n }\n\n public errorWith(reason?: unknown): void {\n if (reason instanceof Response) {\n return this.respondWith(reason)\n }\n\n if (reason instanceof InternalError) {\n this.#controller.errorWith(reason)\n }\n\n throw reason\n }\n}\n\nclass InterceptorWebSocketNetworkFrame extends WebSocketNetworkFrame {\n constructor(args: { connection: WebSocketConnectionData }) {\n super({ connection: args.connection })\n }\n\n public errorWith(reason?: unknown): void {\n if (reason instanceof Error) {\n const { client } = this.data.connection\n\n /**\n * Use `client.errorWith(reason)` in the future.\n * @see https://github.com/mswjs/interceptors/issues/747\n */\n const errorEvent = new Event('error')\n\n Object.defineProperty(errorEvent, 'cause', {\n enumerable: true,\n configurable: false,\n value: reason,\n })\n\n client.socket.dispatchEvent(errorEvent)\n }\n }\n\n public passthrough() {\n this.data.connection.server.connect()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,0BAA2D;AAK3D,4BAA8B;AAC9B,sBAA8B;AAC9B,wBAAgD;AAChD,6BAAsC;AACtC,2BAA+C;AASxC,MAAM,0BAA0B,oCAAc;AAAA,EACnD;AAAA,EAKA;AAAA,EAEA,YAAY,SAAmC;AAC7C,UAAM;AAEN,SAAK,eAAe,IAAI,qCAAiB;AAAA,MACvC,MAAM;AAAA,MACN,cAAc,QAAQ;AAAA,IACxB,CAAC;AACD,SAAK,UAAU,oBAAI,IAAI;AAAA,EACzB;AAAA,EAEO,SAAe;AACpB,SAAK,aAAa,MAAM;A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AAQ;AAAA,IACnB,CAAC;AAED,SAAK,cAAc,QAAQ;AAAA,EAC7B;AAAA,EAEO,cAAoB;AACzB,6DAA+B,KAAK,KAAK,OAAO;AAAA,EAClD;AAAA,EAEO,YAAY,UAA2B;AAC5C,QAAI,UAAU;AACZ,WAAK,YAAY,YAAY,QAAQ;AAAA,IACvC;AAAA,EACF;AAAA,EAEO,UAAU,QAAwB;AACvC,QAAI,kBAAkB,UAAU;AAC9B,aAAO,KAAK,YAAY,MAAM;AAAA,IAChC;AAEA,QAAI,kBAAkB,+BAAe;AACnC,WAAK,YAAY,UAAU,MAAM;AAAA,IACnC;AAEA,UAAM;AAAA,EACR;AACF;AAEA,MAAM,yCAAyC,6CAAsB;AAAA,EACnE,YAAY,MAA+C;AACzD,UAAM,EAAE,YAAY,KAAK,WAAW,CAAC;AAAA,
|
|
1
|
+
{"version":3,"sources":["../../../../src/core/experimental/sources/interceptor-source.ts"],"sourcesContent":["import type { Interceptor, RequestController } from '@mswjs/interceptors'\nimport { BatchInterceptor, type HttpRequestEventMap } from '@mswjs/interceptors'\nimport type {\n WebSocketConnectionData,\n WebSocketEventMap,\n} from '@mswjs/interceptors/WebSocket'\nimport { NetworkSource } from './network-source'\nimport { InternalError } from '../../utils/internal/devUtils'\nimport { HttpNetworkFrame, ResponseEvent } from '../frames/http-frame'\nimport { WebSocketNetworkFrame } from '../frames/websocket-frame'\nimport { deleteRequestPassthroughHeader } from '../request-utils'\n\nexport interface InterceptorSourceOptions {\n interceptors: Array<Interceptor<HttpRequestEventMap | WebSocketEventMap>>\n}\n\n/**\n * Create a network source from the given list of interceptors.\n */\nexport class InterceptorSource extends NetworkSource {\n #interceptor: BatchInterceptor<\n InterceptorSourceOptions['interceptors'],\n HttpRequestEventMap | WebSocketEventMap\n >\n\n #frames: Map<string, HttpNetworkFrame>\n\n constructor(options: InterceptorSourceOptions) {\n super()\n\n this.#interceptor = new BatchInterceptor({\n name: 'interceptor-source',\n interceptors: options.interceptors,\n })\n this.#frames = new Map()\n }\n\n public enable(): void {\n this.#interceptor.apply()\n\n /**\n * @todo @fixme BatchInterceptor infers event types but not listener types.\n */\n this.#interceptor\n .on('request', this.#handleRequest.bind(this) as any)\n .on('response', this.#handleResponse.bind(this) as any)\n .on('connection', this.#handleWebSocketConnection.bind(this) as any)\n }\n\n public disable(): void {\n super.disable()\n this.#interceptor.dispose()\n\n /**\n * @todo We can also abort any pending frames here, given we implement\n * the `NetworkFrame.abort()` method.\n */\n this.#frames.clear()\n }\n\n async #handleRequest({\n requestId,\n request,\n controller,\n }: HttpRequestEventMap['request'][0]): Promise<void> {\n const httpFrame = new InterceptorHttpNetworkFrame({\n id: requestId,\n request,\n controller,\n })\n\n this.#frames.set(requestId, httpFrame)\n await this.queue(httpFrame)\n }\n\n async #handleResponse({\n requestId,\n request,\n response,\n isMockedResponse,\n }: HttpRequestEventMap['response'][0]): Promise<void> {\n const httpFrame = this.#frames.get(requestId)\n this.#frames.delete(requestId)\n\n if (httpFrame == null) {\n return\n }\n\n queueMicrotask(() => {\n try {\n httpFrame.events.emit(\n new ResponseEvent(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n requestId,\n request,\n response,\n },\n ),\n )\n } finally {\n /**\n * @note Remove any listeners from this frame.\n * Past this point, it won't emit anything. The removal is crucial\n * to prevent \"rettime\" from keeping the abort cleanup listeners internally.\n * @see https://github.com/mswjs/msw/issues/2735\n */\n httpFrame.events.removeAllListeners()\n }\n })\n }\n\n async #handleWebSocketConnection(\n connection: WebSocketEventMap['connection'][0],\n ): Promise<void> {\n await this.queue(\n new InterceptorWebSocketNetworkFrame({\n connection,\n }),\n )\n }\n}\n\nclass InterceptorHttpNetworkFrame extends HttpNetworkFrame {\n #controller: RequestController\n\n constructor(options: {\n id: string\n request: Request\n controller: RequestController\n }) {\n super({\n id: options.id,\n request: options.request,\n })\n\n this.#controller = options.controller\n }\n\n public passthrough(): void {\n deleteRequestPassthroughHeader(this.data.request)\n }\n\n public respondWith(response?: Response): void {\n if (response) {\n this.#controller.respondWith(response)\n }\n }\n\n public errorWith(reason?: unknown): void {\n if (reason instanceof Response) {\n return this.respondWith(reason)\n }\n\n if (reason instanceof InternalError) {\n this.#controller.errorWith(reason)\n }\n\n throw reason\n }\n}\n\nclass InterceptorWebSocketNetworkFrame extends WebSocketNetworkFrame {\n constructor(args: { connection: WebSocketConnectionData }) {\n super({ connection: args.connection })\n\n /**\n * @note Provide a similar frame listener cleanup as for HTTP.\n * When the client connection closes, the handler can no longer be used.\n */\n args.connection.client.addEventListener(\n 'close',\n () => {\n this.events.removeAllListeners()\n },\n {\n once: true,\n },\n )\n }\n\n public errorWith(reason?: unknown): void {\n if (reason instanceof Error) {\n const { client } = this.data.connection\n\n /**\n * Use `client.errorWith(reason)` in the future.\n * @see https://github.com/mswjs/interceptors/issues/747\n */\n const errorEvent = new Event('error')\n\n Object.defineProperty(errorEvent, 'cause', {\n enumerable: true,\n configurable: false,\n value: reason,\n })\n\n client.socket.dispatchEvent(errorEvent)\n }\n }\n\n public passthrough() {\n this.data.connection.server.connect()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,0BAA2D;AAK3D,4BAA8B;AAC9B,sBAA8B;AAC9B,wBAAgD;AAChD,6BAAsC;AACtC,2BAA+C;AASxC,MAAM,0BAA0B,oCAAc;AAAA,EACnD;AAAA,EAKA;AAAA,EAEA,YAAY,SAAmC;AAC7C,UAAM;AAEN,SAAK,eAAe,IAAI,qCAAiB;AAAA,MACvC,MAAM;AAAA,MACN,cAAc,QAAQ;AAAA,IACxB,CAAC;AACD,SAAK,UAAU,oBAAI,IAAI;AAAA,EACzB;AAAA,EAEO,SAAe;AACpB,SAAK,aAAa,MAAM;AAKxB,SAAK,aACF,GAAG,WAAW,KAAK,eAAe,KAAK,IAAI,CAAQ,EACnD,GAAG,YAAY,KAAK,gBAAgB,KAAK,IAAI,CAAQ,EACrD,GAAG,cAAc,KAAK,2BAA2B,KAAK,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AAC5C,QAAI,UAAU;AACZ,WAAK,YAAY,YAAY,QAAQ;AAAA,IACvC;AAAA,EACF;AAAA,EAEO,UAAU,QAAwB;AACvC,QAAI,kBAAkB,UAAU;AAC9B,aAAO,KAAK,YAAY,MAAM;AAAA,IAChC;AAEA,QAAI,kBAAkB,+BAAe;AACnC,WAAK,YAAY,UAAU,MAAM;AAAA,IACnC;AAEA,UAAM;AAAA,EACR;AACF;AAEA,MAAM,yCAAyC,6CAAsB;AAAA,EACnE,YAAY,MAA+C;AACzD,UAAM,EAAE,YAAY,KAAK,WAAW,CAAC;AAMrC,SAAK,WAAW,OAAO;AAAA,MACrB;AAAA,MACA,MAAM;AACJ,aAAK,OAAO,mBAAmB;AAAA,MACjC;AAAA,MACA;AAAA,QACE,MAAM;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAAA,EAEO,UAAU,QAAwB;AACvC,QAAI,kBAAkB,OAAO;AAC3B,YAAM,EAAE,OAAO,IAAI,KAAK,KAAK;AAM7B,YAAM,aAAa,IAAI,MAAM,OAAO;AAEpC,aAAO,eAAe,YAAY,SAAS;AAAA,QACzC,YAAY;AAAA,QACZ,cAAc;AAAA,QACd,OAAO;AAAA,MACT,CAAC;AAED,aAAO,OAAO,cAAc,UAAU;AAAA,IACxC;AAAA,EACF;AAAA,EAEO,cAAc;AACnB,SAAK,KAAK,WAAW,OAAO,QAAQ;AAAA,EACtC;AACF;","names":[]}
|
|
@@ -103,6 +103,15 @@ class InterceptorHttpNetworkFrame extends HttpNetworkFrame {
|
|
|
103
103
|
class InterceptorWebSocketNetworkFrame extends WebSocketNetworkFrame {
|
|
104
104
|
constructor(args) {
|
|
105
105
|
super({ connection: args.connection });
|
|
106
|
+
args.connection.client.addEventListener(
|
|
107
|
+
"close",
|
|
108
|
+
() => {
|
|
109
|
+
this.events.removeAllListeners();
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
once: true
|
|
113
|
+
}
|
|
114
|
+
);
|
|
106
115
|
}
|
|
107
116
|
errorWith(reason) {
|
|
108
117
|
if (reason instanceof Error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/core/experimental/sources/interceptor-source.ts"],"sourcesContent":["import type { Interceptor, RequestController } from '@mswjs/interceptors'\nimport { BatchInterceptor, type HttpRequestEventMap } from '@mswjs/interceptors'\nimport type {\n WebSocketConnectionData,\n WebSocketEventMap,\n} from '@mswjs/interceptors/WebSocket'\nimport { NetworkSource } from './network-source'\nimport { InternalError } from '../../utils/internal/devUtils'\nimport { HttpNetworkFrame, ResponseEvent } from '../frames/http-frame'\nimport { WebSocketNetworkFrame } from '../frames/websocket-frame'\nimport { deleteRequestPassthroughHeader } from '../request-utils'\n\nexport interface InterceptorSourceOptions {\n interceptors: Array<Interceptor<HttpRequestEventMap | WebSocketEventMap>>\n}\n\n/**\n * Create a network source from the given list of interceptors.\n */\nexport class InterceptorSource extends NetworkSource {\n #interceptor: BatchInterceptor<\n InterceptorSourceOptions['interceptors'],\n HttpRequestEventMap | WebSocketEventMap\n >\n\n #frames: Map<string, HttpNetworkFrame>\n\n constructor(options: InterceptorSourceOptions) {\n super()\n\n this.#interceptor = new BatchInterceptor({\n name: 'interceptor-source',\n interceptors: options.interceptors,\n })\n this.#frames = new Map()\n }\n\n public enable(): void {\n this.#interceptor.apply()\n\n /**\n * @todo @fixme BatchInterceptor infers event types but not listener types.\n */\n this.#interceptor\n .on('request', this.#handleRequest.bind(this) as any)\n .on('response', this.#handleResponse.bind(this) as any)\n .on('connection', this.#handleWebSocketConnection.bind(this) as any)\n }\n\n public disable(): void {\n super.disable()\n this.#interceptor.dispose()\n\n /**\n * @todo We can also abort any pending frames here, given we implement\n * the `NetworkFrame.abort()` method.\n */\n this.#frames.clear()\n }\n\n async #handleRequest({\n requestId,\n request,\n controller,\n }: HttpRequestEventMap['request'][0]): Promise<void> {\n const httpFrame = new InterceptorHttpNetworkFrame({\n id: requestId,\n request,\n controller,\n })\n\n this.#frames.set(requestId, httpFrame)\n await this.queue(httpFrame)\n }\n\n async #handleResponse({\n requestId,\n request,\n response,\n isMockedResponse,\n }: HttpRequestEventMap['response'][0]): Promise<void> {\n const httpFrame = this.#frames.get(requestId)\n this.#frames.delete(requestId)\n\n if (httpFrame == null) {\n return\n }\n\n queueMicrotask(() => {\n try {\n httpFrame.events.emit(\n new ResponseEvent(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n requestId,\n request,\n response,\n },\n ),\n )\n } finally {\n /**\n * @note Remove any listeners from this frame.\n * Past this point, it won't emit anything. The removal is crucial\n * to prevent \"rettime\" from keeping the abort cleanup listeners internally.\n * @see https://github.com/mswjs/msw/issues/2735\n */\n httpFrame.events.removeAllListeners()\n }\n })\n }\n\n async #handleWebSocketConnection(\n connection: WebSocketEventMap['connection'][0],\n ): Promise<void> {\n await this.queue(\n new InterceptorWebSocketNetworkFrame({\n connection,\n }),\n )\n }\n}\n\nclass InterceptorHttpNetworkFrame extends HttpNetworkFrame {\n #controller: RequestController\n\n constructor(options: {\n id: string\n request: Request\n controller: RequestController\n }) {\n super({\n id: options.id,\n request: options.request,\n })\n\n this.#controller = options.controller\n }\n\n public passthrough(): void {\n deleteRequestPassthroughHeader(this.data.request)\n }\n\n public respondWith(response?: Response): void {\n if (response) {\n this.#controller.respondWith(response)\n }\n }\n\n public errorWith(reason?: unknown): void {\n if (reason instanceof Response) {\n return this.respondWith(reason)\n }\n\n if (reason instanceof InternalError) {\n this.#controller.errorWith(reason)\n }\n\n throw reason\n }\n}\n\nclass InterceptorWebSocketNetworkFrame extends WebSocketNetworkFrame {\n constructor(args: { connection: WebSocketConnectionData }) {\n super({ connection: args.connection })\n }\n\n public errorWith(reason?: unknown): void {\n if (reason instanceof Error) {\n const { client } = this.data.connection\n\n /**\n * Use `client.errorWith(reason)` in the future.\n * @see https://github.com/mswjs/interceptors/issues/747\n */\n const errorEvent = new Event('error')\n\n Object.defineProperty(errorEvent, 'cause', {\n enumerable: true,\n configurable: false,\n value: reason,\n })\n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ACnB,UAAI;AACF,kBAAU,OAAO;AAAA,UACf,IAAI;AAAA,YACF,mBAAmB,oBAAoB;AAAA,YACvC;AAAA,cACE;AAAA,cACA;AAAA,cACA;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,MACF,UAAE;AAOA,kBAAU,OAAO,mBAAmB;AAAA,MACtC;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,2BACJ,YACe;AACf,UAAM,KAAK;AAAA,MACT,IAAI,iCAAiC;AAAA,QACnC;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAEA,MAAM,oCAAoC,iBAAiB;AAAA,EACzD;AAAA,EAEA,YAAY,SAIT;AACD,UAAM;AAAA,MACJ,IAAI,QAAQ;AAAA,MACZ,SAAS,QAAQ;AAAA,IACnB,CAAC;AAED,SAAK,cAAc,QAAQ;AAAA,EAC7B;AAAA,EAEO,cAAoB;AACzB,mCAA+B,KAAK,KAAK,OAAO;AAAA,EAClD;AAAA,EAEO,YAAY,UAA2B;AAC5C,QAAI,UAAU;AACZ,WAAK,YAAY,YAAY,QAAQ;AAAA,IACvC;AAAA,EACF;AAAA,EAEO,UAAU,QAAwB;AACvC,QAAI,kBAAkB,UAAU;AAC9B,aAAO,KAAK,YAAY,MAAM;AAAA,IAChC;AAEA,QAAI,kBAAkB,eAAe;AACnC,WAAK,YAAY,UAAU,MAAM;AAAA,IACnC;AAEA,UAAM;AAAA,EACR;AACF;AAEA,MAAM,yCAAyC,sBAAsB;AAAA,EACnE,YAAY,MAA+C;AACzD,UAAM,EAAE,YAAY,KAAK,WAAW,CAAC;AAAA,
|
|
1
|
+
{"version":3,"sources":["../../../../src/core/experimental/sources/interceptor-source.ts"],"sourcesContent":["import type { Interceptor, RequestController } from '@mswjs/interceptors'\nimport { BatchInterceptor, type HttpRequestEventMap } from '@mswjs/interceptors'\nimport type {\n WebSocketConnectionData,\n WebSocketEventMap,\n} from '@mswjs/interceptors/WebSocket'\nimport { NetworkSource } from './network-source'\nimport { InternalError } from '../../utils/internal/devUtils'\nimport { HttpNetworkFrame, ResponseEvent } from '../frames/http-frame'\nimport { WebSocketNetworkFrame } from '../frames/websocket-frame'\nimport { deleteRequestPassthroughHeader } from '../request-utils'\n\nexport interface InterceptorSourceOptions {\n interceptors: Array<Interceptor<HttpRequestEventMap | WebSocketEventMap>>\n}\n\n/**\n * Create a network source from the given list of interceptors.\n */\nexport class InterceptorSource extends NetworkSource {\n #interceptor: BatchInterceptor<\n InterceptorSourceOptions['interceptors'],\n HttpRequestEventMap | WebSocketEventMap\n >\n\n #frames: Map<string, HttpNetworkFrame>\n\n constructor(options: InterceptorSourceOptions) {\n super()\n\n this.#interceptor = new BatchInterceptor({\n name: 'interceptor-source',\n interceptors: options.interceptors,\n })\n this.#frames = new Map()\n }\n\n public enable(): void {\n this.#interceptor.apply()\n\n /**\n * @todo @fixme BatchInterceptor infers event types but not listener types.\n */\n this.#interceptor\n .on('request', this.#handleRequest.bind(this) as any)\n .on('response', this.#handleResponse.bind(this) as any)\n .on('connection', this.#handleWebSocketConnection.bind(this) as any)\n }\n\n public disable(): void {\n super.disable()\n this.#interceptor.dispose()\n\n /**\n * @todo We can also abort any pending frames here, given we implement\n * the `NetworkFrame.abort()` method.\n */\n this.#frames.clear()\n }\n\n async #handleRequest({\n requestId,\n request,\n controller,\n }: HttpRequestEventMap['request'][0]): Promise<void> {\n const httpFrame = new InterceptorHttpNetworkFrame({\n id: requestId,\n request,\n controller,\n })\n\n this.#frames.set(requestId, httpFrame)\n await this.queue(httpFrame)\n }\n\n async #handleResponse({\n requestId,\n request,\n response,\n isMockedResponse,\n }: HttpRequestEventMap['response'][0]): Promise<void> {\n const httpFrame = this.#frames.get(requestId)\n this.#frames.delete(requestId)\n\n if (httpFrame == null) {\n return\n }\n\n queueMicrotask(() => {\n try {\n httpFrame.events.emit(\n new ResponseEvent(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n requestId,\n request,\n response,\n },\n ),\n )\n } finally {\n /**\n * @note Remove any listeners from this frame.\n * Past this point, it won't emit anything. The removal is crucial\n * to prevent \"rettime\" from keeping the abort cleanup listeners internally.\n * @see https://github.com/mswjs/msw/issues/2735\n */\n httpFrame.events.removeAllListeners()\n }\n })\n }\n\n async #handleWebSocketConnection(\n connection: WebSocketEventMap['connection'][0],\n ): Promise<void> {\n await this.queue(\n new InterceptorWebSocketNetworkFrame({\n connection,\n }),\n )\n }\n}\n\nclass InterceptorHttpNetworkFrame extends HttpNetworkFrame {\n #controller: RequestController\n\n constructor(options: {\n id: string\n request: Request\n controller: RequestController\n }) {\n super({\n id: options.id,\n request: options.request,\n })\n\n this.#controller = options.controller\n }\n\n public passthrough(): void {\n deleteRequestPassthroughHeader(this.data.request)\n }\n\n public respondWith(response?: Response): void {\n if (response) {\n this.#controller.respondWith(response)\n }\n }\n\n public errorWith(reason?: unknown): void {\n if (reason instanceof Response) {\n return this.respondWith(reason)\n }\n\n if (reason instanceof InternalError) {\n this.#controller.errorWith(reason)\n }\n\n throw reason\n }\n}\n\nclass InterceptorWebSocketNetworkFrame extends WebSocketNetworkFrame {\n constructor(args: { connection: WebSocketConnectionData }) {\n super({ connection: args.connection })\n\n /**\n * @note Provide a similar frame listener cleanup as for HTTP.\n * When the client connection closes, the handler can no longer be used.\n */\n args.connection.client.addEventListener(\n 'close',\n () => {\n this.events.removeAllListeners()\n },\n {\n once: true,\n },\n )\n }\n\n public errorWith(reason?: unknown): void {\n if (reason instanceof Error) {\n const { client } = this.data.connection\n\n /**\n * Use `client.errorWith(reason)` in the future.\n * @see https://github.com/mswjs/interceptors/issues/747\n */\n const errorEvent = new Event('error')\n\n Object.defineProperty(errorEvent, 'cause', {\n enumerable: true,\n configurable: false,\n value: reason,\n })\n\n client.socket.dispatchEvent(errorEvent)\n }\n }\n\n public passthrough() {\n this.data.connection.server.connect()\n }\n}\n"],"mappings":"AACA,SAAS,wBAAkD;AAK3D,SAAS,qBAAqB;AAC9B,SAAS,qBAAqB;AAC9B,SAAS,k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OAAO,cAAc,UAAU;AAAA,IACxC;AAAA,EACF;AAAA,EAEO,cAAc;AACnB,SAAK,KAAK,WAAW,OAAO,QAAQ;AAAA,EACtC;AACF;","names":[]}
|
package/lib/iife/index.js
CHANGED
|
@@ -23116,6 +23116,15 @@ Read more: https://mswjs.io/docs/websocket`;
|
|
|
23116
23116
|
var InterceptorWebSocketNetworkFrame = class extends WebSocketNetworkFrame {
|
|
23117
23117
|
constructor(args) {
|
|
23118
23118
|
super({ connection: args.connection });
|
|
23119
|
+
args.connection.client.addEventListener(
|
|
23120
|
+
"close",
|
|
23121
|
+
() => {
|
|
23122
|
+
this.events.removeAllListeners();
|
|
23123
|
+
},
|
|
23124
|
+
{
|
|
23125
|
+
once: true
|
|
23126
|
+
}
|
|
23127
|
+
);
|
|
23119
23128
|
}
|
|
23120
23129
|
errorWith(reason) {
|
|
23121
23130
|
if (reason instanceof Error) {
|