🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

msw

Package Overview
Dependencies
Maintainers
1
Versions
337
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

msw - npm Package Compare versions

Comparing version
2.14.2
to
2.14.3
+14
-10
lib/core/experimental/sources/interceptor-source.js

@@ -75,12 +75,16 @@ "use strict";

queueMicrotask(() => {
httpFrame.events.emit(
new import_http_frame.ResponseEvent(
isMockedResponse ? "response:mocked" : "response:bypass",
{
requestId,
request,
response
}
)
);
try {
httpFrame.events.emit(
new import_http_frame.ResponseEvent(
isMockedResponse ? "response:mocked" : "response:bypass",
{
requestId,
request,
response
}
)
);
} finally {
httpFrame.events.removeAllListeners();
}
});

@@ -87,0 +91,0 @@ }

@@ -1,1 +0,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 httpFrame.events.emit(\n new ResponseEvent(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n requestId,\n request,\n response,\n },\n ),\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;AAKxB,SAAK,aACF,GAAG,WAAW,KAAK,eAAe,KAAK,IAAI,CAAQ,EACnD,GAAG,YAAY,KAAK,gBAAgB,KAAK,IAAI,CAAQ,EACrD,GAAG,cAAc,KAAK,2BAA2B,KAAK,IAAI,CAAQ;AAAA,EACvE;AAAA,EAEO,UAAgB;AACrB,UAAM,QAAQ;AACd,SAAK,aAAa,QAAQ;AAM1B,SAAK,QAAQ,MAAM;AAAA,EACrB;AAAA,EAEA,MAAM,eAAe;AAAA,IACnB;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAAqD;AACnD,UAAM,YAAY,IAAI,4BAA4B;AAAA,MAChD,IAAI;AAAA,MACJ;AAAA,MACA;AAAA,IACF,CAAC;AAED,SAAK,QAAQ,IA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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":[]}
{"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;AAKxB,SAAK,aACF,GAAG,WAAW,KAAK,eAAe,KAAK,IAAI,CAAQ,EACnD,GAAG,YAAY,KAAK,gBAAgB,KAAK,IAAI,CAAQ,EACrD,GAAG,cAAc,KAAK,2BAA2B,KAAK,IAAI,CAAQ;AAAA,EACvE;AAAA,EAEO,UAAgB;AACrB,UAAM,QAAQ;AACd,SAAK,aAAa,QAAQ;AAM1B,SAAK,QAAQ,MAAM;AAAA,EACrB;AAAA,EAEA,MAAM,eAAe;AAAA,IACnB;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAAqD;AACnD,UAAM,YAAY,IAAI,4BAA4B;AAAA,MAChD,IAAI;AAAA,MACJ;AAAA,MACA;AAAA,IACF,CAAC;AAED,SAAK,QAAQ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AY,MAA+C;AACzD,UAAM,EAAE,YAAY,KAAK,WAAW,CAAC;AAAA,EACvC;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":[]}

@@ -52,12 +52,16 @@ import { BatchInterceptor } from "@mswjs/interceptors";

queueMicrotask(() => {
httpFrame.events.emit(
new ResponseEvent(
isMockedResponse ? "response:mocked" : "response:bypass",
{
requestId,
request,
response
}
)
);
try {
httpFrame.events.emit(
new ResponseEvent(
isMockedResponse ? "response:mocked" : "response:bypass",
{
requestId,
request,
response
}
)
);
} finally {
httpFrame.events.removeAllListeners();
}
});

@@ -64,0 +68,0 @@ }

@@ -1,1 +0,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 httpFrame.events.emit(\n new ResponseEvent(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n requestId,\n request,\n response,\n },\n ),\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":"AACA,SAAS,wBAAkD;AAK3D,SAAS,qBAAqB;AAC9B,SAAS,qBAAqB;AAC9B,SAAS,kBAAkB,qBAAqB;AAChD,SAAS,6BAA6B;AACtC,SAAS,sCAAsC;AASxC,MAAM,0BAA0B,cAAc;AAAA,EACnD;AAAA,EAKA;AAAA,EAEA,YAAY,SAAmC;AAC7C,UAAM;AAEN,SAAK,eAAe,IAAI,iBAAiB;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,IAAI,CAAQ;AAAA,EACvE;AAAA,EAEO,UAAgB;AACrB,UAAM,QAAQ;AACd,SAAK,aAAa,QAAQ;AAM1B,SAAK,QAAQ,M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AI,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,EACvC;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":[]}
{"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":"AACA,SAAS,wBAAkD;AAK3D,SAAS,qBAAqB;AAC9B,SAAS,qBAAqB;AAC9B,SAAS,kBAAkB,qBAAqB;AAChD,SAAS,6BAA6B;AACtC,SAAS,sCAAsC;AASxC,MAAM,0BAA0B,cAAc;AAAA,EACnD;AAAA,EAKA;AAAA,EAEA,YAAY,SAAmC;AAC7C,UAAM;AAEN,SAAK,eAAe,IAAI,iBAAiB;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,IAAI,CAAQ;AAAA,EACvE;AAAA,EAEO,UAAgB;AACrB,UAAM,QAAQ;AACd,SAAK,aAAa,QAAQ;AAM1B,SAAK,QAAQ,MAAM;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AkB,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,EACvC;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":[]}

@@ -10,3 +10,3 @@ /* eslint-disable */

const PACKAGE_VERSION = '2.14.2'
const PACKAGE_VERSION = '2.14.3'
const INTEGRITY_CHECKSUM = '4db4a41e972cec1b64cc569c66952d82'

@@ -13,0 +13,0 @@ const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')

{
"name": "msw",
"version": "2.14.2",
"version": "2.14.3",
"description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",

@@ -236,3 +236,3 @@ "type": "commonjs",

"picocolors": "^1.1.1",
"rettime": "^0.11.7",
"rettime": "^0.11.11",
"statuses": "^2.0.2",

@@ -290,3 +290,3 @@ "strict-event-emitter": "^0.5.1",

"webpack-http-server": "^0.5.0",
"msw": "^2.14.2"
"msw": "^2.14.3"
},

@@ -318,3 +318,3 @@ "peerDependencies": {

"publint": "publint",
"test": "pnpm test:unit && pnpm test:node && pnpm test:browser && pnpm test:native",
"test": "pnpm test:unit && pnpm test:node && pnpm test:browser && pnpm test:native && pnpm test:memory",
"test:unit": "vitest",

@@ -328,2 +328,3 @@ "test:node": "vitest --config=./test/node/vitest.config.ts",

"test:ts": "vitest --config=./test/typings/vitest.config.ts",
"test:memory": "vitest --config=./test/memory/vitest.config.ts",
"release": "release publish",

@@ -330,0 +331,0 @@ "postinstall": "node -e \"import('./config/scripts/postinstall.js').catch(() => void 0)\"",

@@ -333,2 +333,3 @@ import { invariant } from 'outvariant'

const { request, response, isMockedResponse } = event.data
const frame = this.#frames.get(request.id)

@@ -344,6 +345,6 @@ /**

this.#frames.delete(request.id)
frame?.events.removeAllListeners()
return
}
const frame = this.#frames.get(request.id)
this.#frames.delete(request.id)

@@ -384,13 +385,17 @@

frame.events.emit(
new ResponseEvent(
isMockedResponse ? 'response:mocked' : 'response:bypass',
{
requestId: frame.data.id,
request: fetchRequest,
response: fetchResponse,
isMockedResponse,
},
),
)
try {
frame.events.emit(
new ResponseEvent(
isMockedResponse ? 'response:mocked' : 'response:bypass',
{
requestId: frame.data.id,
request: fetchRequest,
response: fetchResponse,
isMockedResponse,
},
),
)
} finally {
frame.events.removeAllListeners()
}
}

@@ -397,0 +402,0 @@

@@ -90,12 +90,22 @@ import type { Interceptor, RequestController } from '@mswjs/interceptors'

queueMicrotask(() => {
httpFrame.events.emit(
new ResponseEvent(
isMockedResponse ? 'response:mocked' : 'response:bypass',
{
requestId,
request,
response,
},
),
)
try {
httpFrame.events.emit(
new ResponseEvent(
isMockedResponse ? 'response:mocked' : 'response:bypass',
{
requestId,
request,
response,
},
),
)
} finally {
/**
* @note Remove any listeners from this frame.
* Past this point, it won't emit anything. The removal is crucial
* to prevent "rettime" from keeping the abort cleanup listeners internally.
* @see https://github.com/mswjs/msw/issues/2735
*/
httpFrame.events.removeAllListeners()
}
})

@@ -102,0 +112,0 @@ }

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display