@depup/mswjs__interceptors 0.41.3-depup.1 → 0.42.0-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -10
- package/changes.json +3 -12
- package/lib/browser/{bufferUtils-BiiO6HZv.mjs → buffer-utils-DJj7YzLG.js} +3 -3
- package/lib/browser/buffer-utils-DJj7YzLG.js.map +1 -0
- package/lib/browser/create-request-id-DlEd4GOA.js +184 -0
- package/lib/browser/create-request-id-DlEd4GOA.js.map +1 -0
- package/lib/browser/fetch-utils-zxA_SD66.js +406 -0
- package/lib/browser/fetch-utils-zxA_SD66.js.map +1 -0
- package/lib/browser/handle-request-CIOa9O-N.js +167 -0
- package/lib/browser/handle-request-CIOa9O-N.js.map +1 -0
- package/lib/browser/has-configurable-global-Cew-dYqk.js +94 -0
- package/lib/browser/has-configurable-global-Cew-dYqk.js.map +1 -0
- package/lib/browser/http-CB9rQ7i_.d.ts +105 -0
- package/lib/browser/index.d.ts +120 -0
- package/lib/browser/index.js +130 -0
- package/lib/browser/index.js.map +1 -0
- package/lib/browser/interceptor-dtJZ_9QT.d.ts +50 -0
- package/lib/browser/interceptors/WebSocket/index.d.ts +2 -0
- package/lib/browser/interceptors/WebSocket/{index.mjs → index.js} +41 -36
- package/lib/browser/interceptors/WebSocket/index.js.map +1 -0
- package/lib/browser/interceptors/XMLHttpRequest/web.d.ts +11 -0
- package/lib/browser/interceptors/XMLHttpRequest/web.js +2 -0
- package/lib/browser/interceptors/fetch/web.d.ts +14 -0
- package/lib/browser/interceptors/fetch/web.js +2 -0
- package/lib/browser/presets/browser.d.ts +11 -0
- package/lib/browser/presets/browser.js +12 -0
- package/lib/browser/presets/browser.js.map +1 -0
- package/lib/browser/{resolveWebSocketUrl-C83-x9iE.mjs → resolve-web-socket-url-CSvNPLGi.js} +3 -3
- package/lib/browser/resolve-web-socket-url-CSvNPLGi.js.map +1 -0
- package/lib/browser/web-BjVDkiX0.js +248 -0
- package/lib/browser/web-BjVDkiX0.js.map +1 -0
- package/lib/{node/XMLHttpRequest-C8dIZpds.mjs → browser/web-C2oKsT3Q.js} +334 -273
- package/lib/browser/web-C2oKsT3Q.js.map +1 -0
- package/lib/browser/{interceptors/WebSocket/index.d.mts → websocket-CC0nB0md.d.ts} +41 -33
- package/lib/node/batch-interceptor-ByEZVih3.js +119 -0
- package/lib/node/batch-interceptor-ByEZVih3.js.map +1 -0
- package/lib/node/batch-interceptor-CPjFK2td.d.ts +28 -0
- package/lib/node/buffer-utils-BvPY1Tc-.js +187 -0
- package/lib/node/buffer-utils-BvPY1Tc-.js.map +1 -0
- package/lib/node/fetch-utils-Dw1PsmtX.js +568 -0
- package/lib/node/fetch-utils-Dw1PsmtX.js.map +1 -0
- package/lib/node/{hasConfigurableGlobal-DBJA0vjm.mjs → has-configurable-global-CJCh_g1I.js} +7 -5
- package/lib/node/has-configurable-global-CJCh_g1I.js.map +1 -0
- package/lib/node/http-CB9rQ7i_.d.ts +105 -0
- package/lib/{browser/interceptors/WebSocket/index.d.cts → node/index.d.ts} +120 -60
- package/lib/node/{index.mjs → index.js} +8 -12
- package/lib/node/index.js.map +1 -0
- package/lib/node/interceptor-dtJZ_9QT.d.ts +50 -0
- package/lib/node/interceptors/ClientRequest/index.d.ts +14 -0
- package/lib/node/interceptors/ClientRequest/index.js +67 -0
- package/lib/node/interceptors/ClientRequest/index.js.map +1 -0
- package/lib/node/interceptors/XMLHttpRequest/node.d.ts +12 -0
- package/lib/node/interceptors/XMLHttpRequest/node.js +74 -0
- package/lib/node/interceptors/XMLHttpRequest/node.js.map +1 -0
- package/lib/node/interceptors/fetch/node.d.ts +18 -0
- package/lib/node/interceptors/fetch/node.js +83 -0
- package/lib/node/interceptors/fetch/node.js.map +1 -0
- package/lib/node/interceptors/http/index.d.ts +14 -0
- package/lib/node/interceptors/http/index.js +32 -0
- package/lib/node/interceptors/http/index.js.map +1 -0
- package/lib/node/interceptors/net/index.d.ts +203 -0
- package/lib/node/interceptors/net/index.js +2 -0
- package/lib/node/llhttp/constants.cjs +485 -0
- package/lib/node/llhttp/constants.d.cts +957 -0
- package/lib/node/llhttp/llhttp.wasm +0 -0
- package/lib/node/net-DtMnyEeg.js +1388 -0
- package/lib/node/net-DtMnyEeg.js.map +1 -0
- package/lib/node/presets/node.d.ts +12 -0
- package/lib/node/presets/node.js +17 -0
- package/lib/node/presets/node.js.map +1 -0
- package/lib/node/{RemoteHttpInterceptor.d.cts → remote-http-interceptor.d.ts} +12 -11
- package/lib/node/{RemoteHttpInterceptor.mjs → remote-http-interceptor.js} +40 -34
- package/lib/node/remote-http-interceptor.js.map +1 -0
- package/lib/node/source-lP0yyEtA.js +1990 -0
- package/lib/node/source-lP0yyEtA.js.map +1 -0
- package/package.json +84 -131
- package/src/batch-interceptor.test.ts +475 -0
- package/src/batch-interceptor.ts +244 -0
- package/src/{createRequestId.test.ts → create-request-id.test.ts} +1 -2
- package/src/disposable.ts +13 -0
- package/src/events/http.ts +92 -0
- package/src/events/websocket.ts +47 -0
- package/src/glossary.ts +1 -29
- package/src/index.ts +18 -10
- package/src/interceptor.test.ts +279 -0
- package/src/interceptor.ts +139 -0
- package/src/interceptors/ClientRequest/index.test.ts +21 -39
- package/src/interceptors/ClientRequest/index.ts +65 -180
- package/src/interceptors/ClientRequest/utils/{recordRawHeaders.test.ts → record-raw-headers.test.ts} +34 -2
- package/src/interceptors/ClientRequest/utils/{recordRawHeaders.ts → record-raw-headers.ts} +65 -5
- package/src/interceptors/WebSocket/index.ts +38 -62
- package/src/interceptors/WebSocket/utils/{bindEvent.test.ts → bind-event.test.ts} +1 -2
- package/src/interceptors/WebSocket/utils/events.test.ts +0 -1
- package/src/interceptors/WebSocket/{WebSocketClassTransport.ts → web-socket-class-transport.ts} +3 -3
- package/src/interceptors/WebSocket/{WebSocketClientConnection.ts → web-socket-client-connection.ts} +4 -4
- package/src/interceptors/WebSocket/{WebSocketOverride.ts → web-socket-override.ts} +6 -7
- package/src/interceptors/WebSocket/{WebSocketServerConnection.ts → web-socket-server-connection.ts} +4 -4
- package/src/interceptors/WebSocket/{WebSocketTransport.ts → web-socket-transport.ts} +1 -1
- package/src/interceptors/XMLHttpRequest/node.ts +104 -0
- package/src/interceptors/XMLHttpRequest/polyfills/{ProgressEventPolyfill.ts → progress-event-polyfill.ts} +1 -1
- package/src/interceptors/XMLHttpRequest/utils/{concateArrayBuffer.test.ts → concat-array-buffer.test.ts} +1 -2
- package/src/interceptors/XMLHttpRequest/utils/{concatArrayBuffer.ts → concat-array-buffer.ts} +1 -1
- package/src/interceptors/XMLHttpRequest/utils/{createEvent.test.ts → create-event.test.ts} +3 -4
- package/src/interceptors/XMLHttpRequest/utils/{createEvent.ts → create-event.ts} +2 -2
- package/src/interceptors/XMLHttpRequest/utils/{createResponse.ts → create-response.ts} +1 -1
- package/src/interceptors/XMLHttpRequest/utils/{getBodyByteLength.test.ts → get-body-byte-length.test.ts} +1 -2
- package/src/interceptors/XMLHttpRequest/web.ts +34 -0
- package/src/interceptors/XMLHttpRequest/{XMLHttpRequestController.ts → xml-http-request-controller.ts} +364 -204
- package/src/interceptors/XMLHttpRequest/{XMLHttpRequestProxy.ts → xml-http-request-proxy.ts} +28 -25
- package/src/interceptors/fetch/node.ts +103 -0
- package/src/interceptors/fetch/utils/{followRedirect.ts → follow-redirect.ts} +1 -1
- package/src/interceptors/fetch/web.ts +217 -0
- package/src/interceptors/http/forward-events.test.ts +86 -0
- package/src/interceptors/http/forward-events.ts +139 -0
- package/src/interceptors/http/http-parser/index.ts +310 -0
- package/src/interceptors/http/http-parser/llhttp/constants.cjs +485 -0
- package/src/interceptors/http/http-parser/llhttp/constants.d.cts +957 -0
- package/src/interceptors/http/http-parser/llhttp/llhttp.wasm +0 -0
- package/src/interceptors/http/http-parser.ts +138 -0
- package/src/interceptors/http/index.ts +34 -0
- package/src/interceptors/http/source.ts +688 -0
- package/src/interceptors/net/index.ts +368 -0
- package/src/interceptors/net/socket-controller.ts +1476 -0
- package/src/interceptors/net/utils/address-info.ts +54 -0
- package/src/interceptors/net/utils/connection-options-to-url.ts +56 -0
- package/src/interceptors/net/utils/flush-writes.ts +57 -0
- package/src/interceptors/net/utils/get-tls-connect-options.ts +23 -0
- package/src/interceptors/net/utils/normalize-net-connect-args.test.ts +233 -0
- package/src/interceptors/net/utils/normalize-net-connect-args.ts +121 -0
- package/src/interceptors/net/utils/normalize-tls-connect-args.ts +50 -0
- package/src/presets/browser.ts +4 -4
- package/src/presets/node.ts +4 -4
- package/src/{RemoteHttpInterceptor.ts → remote-http-interceptor.ts} +57 -44
- package/src/request-context.ts +40 -0
- package/src/{RequestController.test.ts → request-controller.test.ts} +37 -3
- package/src/{RequestController.ts → request-controller.ts} +33 -11
- package/src/utils/{bufferUtils.test.ts → buffer-utils.test.ts} +1 -2
- package/src/utils/buffer-utils.ts +37 -0
- package/src/utils/{cloneObject.test.ts → clone-object.test.ts} +1 -2
- package/src/utils/{cloneObject.ts → clone-object.ts} +8 -8
- package/src/utils/{createProxy.test.ts → create-proxy.test.ts} +6 -4
- package/src/utils/{createProxy.ts → create-proxy.ts} +1 -1
- package/src/utils/fetch-utils.test.ts +131 -0
- package/src/utils/fetch-utils.ts +353 -0
- package/src/utils/{findPropertySource.test.ts → find-property-source.test.ts} +1 -2
- package/src/utils/{getCleanUrl.test.ts → get-clean-url.test.ts} +1 -2
- package/src/utils/{getUrlByRequestOptions.test.ts → get-url-by-request-options.test.ts} +4 -8
- package/src/utils/{getUrlByRequestOptions.ts → get-url-by-request-options.ts} +16 -16
- package/src/utils/{getValueBySymbol.test.ts → get-value-by-symbol.test.ts} +1 -2
- package/src/utils/{handleRequest.ts → handle-request.ts} +66 -47
- package/src/utils/{hasConfigurableGlobal.test.ts → has-configurable-global.test.ts} +12 -2
- package/src/utils/{hasConfigurableGlobal.ts → has-configurable-global.ts} +6 -2
- package/src/utils/{isObject.test.ts → is-object.test.ts} +1 -2
- package/src/utils/logger.test.ts +154 -0
- package/src/utils/logger.ts +154 -0
- package/src/utils/{parseJson.test.ts → parse-json.test.ts} +1 -2
- package/src/utils/patches-registry.test.ts +240 -0
- package/src/utils/patches-registry.ts +134 -0
- package/src/utils/{responseUtils.ts → response-utils.ts} +25 -2
- package/ClientRequest/package.json +0 -11
- package/RemoteHttpInterceptor/package.json +0 -11
- package/WebSocket/package.json +0 -12
- package/XMLHttpRequest/package.json +0 -12
- package/fetch/package.json +0 -12
- package/lib/browser/Interceptor-Deczogc8.d.cts +0 -65
- package/lib/browser/Interceptor-gqKgs-aF.d.mts +0 -65
- package/lib/browser/XMLHttpRequest-BACqefB-.cjs +0 -761
- package/lib/browser/XMLHttpRequest-BACqefB-.cjs.map +0 -1
- package/lib/browser/XMLHttpRequest-BvxZV0WU.mjs +0 -756
- package/lib/browser/XMLHttpRequest-BvxZV0WU.mjs.map +0 -1
- package/lib/browser/bufferUtils-BiiO6HZv.mjs.map +0 -1
- package/lib/browser/bufferUtils-Uc0eRItL.cjs +0 -38
- package/lib/browser/bufferUtils-Uc0eRItL.cjs.map +0 -1
- package/lib/browser/createRequestId-Cs4oXfa1.cjs +0 -205
- package/lib/browser/createRequestId-Cs4oXfa1.cjs.map +0 -1
- package/lib/browser/createRequestId-DQcIlohW.mjs +0 -170
- package/lib/browser/createRequestId-DQcIlohW.mjs.map +0 -1
- package/lib/browser/fetch-DdKEdDOR.mjs +0 -248
- package/lib/browser/fetch-DdKEdDOR.mjs.map +0 -1
- package/lib/browser/fetch-U3v3Y4ap.cjs +0 -253
- package/lib/browser/fetch-U3v3Y4ap.cjs.map +0 -1
- package/lib/browser/getRawRequest-BTaNLFr0.mjs +0 -218
- package/lib/browser/getRawRequest-BTaNLFr0.mjs.map +0 -1
- package/lib/browser/getRawRequest-zx8rUJL2.cjs +0 -259
- package/lib/browser/getRawRequest-zx8rUJL2.cjs.map +0 -1
- package/lib/browser/glossary-BdLS4k1H.d.cts +0 -70
- package/lib/browser/glossary-DYwOrogs.d.mts +0 -70
- package/lib/browser/handleRequest-CvX2G-Lz.cjs +0 -189
- package/lib/browser/handleRequest-CvX2G-Lz.cjs.map +0 -1
- package/lib/browser/handleRequest-D7kpTI5U.mjs +0 -178
- package/lib/browser/handleRequest-D7kpTI5U.mjs.map +0 -1
- package/lib/browser/hasConfigurableGlobal-BvCTG97d.cjs +0 -45
- package/lib/browser/hasConfigurableGlobal-BvCTG97d.cjs.map +0 -1
- package/lib/browser/hasConfigurableGlobal-npXitu1-.mjs +0 -33
- package/lib/browser/hasConfigurableGlobal-npXitu1-.mjs.map +0 -1
- package/lib/browser/index.cjs +0 -70
- package/lib/browser/index.cjs.map +0 -1
- package/lib/browser/index.d.cts +0 -96
- package/lib/browser/index.d.mts +0 -96
- package/lib/browser/index.mjs +0 -56
- package/lib/browser/index.mjs.map +0 -1
- package/lib/browser/interceptors/WebSocket/index.cjs +0 -622
- package/lib/browser/interceptors/WebSocket/index.cjs.map +0 -1
- package/lib/browser/interceptors/WebSocket/index.mjs.map +0 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.cjs +0 -7
- package/lib/browser/interceptors/XMLHttpRequest/index.d.cts +0 -15
- package/lib/browser/interceptors/XMLHttpRequest/index.d.mts +0 -15
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +0 -7
- package/lib/browser/interceptors/fetch/index.cjs +0 -6
- package/lib/browser/interceptors/fetch/index.d.cts +0 -13
- package/lib/browser/interceptors/fetch/index.d.mts +0 -13
- package/lib/browser/interceptors/fetch/index.mjs +0 -6
- package/lib/browser/presets/browser.cjs +0 -17
- package/lib/browser/presets/browser.cjs.map +0 -1
- package/lib/browser/presets/browser.d.cts +0 -12
- package/lib/browser/presets/browser.d.mts +0 -14
- package/lib/browser/presets/browser.mjs +0 -17
- package/lib/browser/presets/browser.mjs.map +0 -1
- package/lib/browser/resolveWebSocketUrl-6K6EgqsA.cjs +0 -31
- package/lib/browser/resolveWebSocketUrl-6K6EgqsA.cjs.map +0 -1
- package/lib/browser/resolveWebSocketUrl-C83-x9iE.mjs.map +0 -1
- package/lib/node/BatchInterceptor-3LnAnLTx.cjs +0 -49
- package/lib/node/BatchInterceptor-3LnAnLTx.cjs.map +0 -1
- package/lib/node/BatchInterceptor-D7mXzHcQ.d.mts +0 -26
- package/lib/node/BatchInterceptor-DFaBPilf.mjs +0 -44
- package/lib/node/BatchInterceptor-DFaBPilf.mjs.map +0 -1
- package/lib/node/BatchInterceptor-D_YqR8qU.d.cts +0 -26
- package/lib/node/ClientRequest-2rDe54Ui.cjs +0 -1043
- package/lib/node/ClientRequest-2rDe54Ui.cjs.map +0 -1
- package/lib/node/ClientRequest-Ca8Qykuv.mjs +0 -1034
- package/lib/node/ClientRequest-Ca8Qykuv.mjs.map +0 -1
- package/lib/node/Interceptor-DEazpLJd.d.mts +0 -133
- package/lib/node/Interceptor-DJ2akVWI.d.cts +0 -133
- package/lib/node/RemoteHttpInterceptor.cjs +0 -154
- package/lib/node/RemoteHttpInterceptor.cjs.map +0 -1
- package/lib/node/RemoteHttpInterceptor.d.mts +0 -39
- package/lib/node/RemoteHttpInterceptor.mjs.map +0 -1
- package/lib/node/XMLHttpRequest-B7kJdYYI.cjs +0 -763
- package/lib/node/XMLHttpRequest-B7kJdYYI.cjs.map +0 -1
- package/lib/node/XMLHttpRequest-C8dIZpds.mjs.map +0 -1
- package/lib/node/bufferUtils-DiCTqG-7.cjs +0 -38
- package/lib/node/bufferUtils-DiCTqG-7.cjs.map +0 -1
- package/lib/node/bufferUtils-_8XfKIfX.mjs +0 -20
- package/lib/node/bufferUtils-_8XfKIfX.mjs.map +0 -1
- package/lib/node/chunk-CbDLau6x.cjs +0 -34
- package/lib/node/fetch-BmXpK10r.cjs +0 -272
- package/lib/node/fetch-BmXpK10r.cjs.map +0 -1
- package/lib/node/fetch-G1DVwDKG.mjs +0 -265
- package/lib/node/fetch-G1DVwDKG.mjs.map +0 -1
- package/lib/node/fetchUtils-BaY5iWXw.cjs +0 -419
- package/lib/node/fetchUtils-BaY5iWXw.cjs.map +0 -1
- package/lib/node/fetchUtils-CoU35g3M.mjs +0 -359
- package/lib/node/fetchUtils-CoU35g3M.mjs.map +0 -1
- package/lib/node/getRawRequest-BavnMWh_.cjs +0 -36
- package/lib/node/getRawRequest-BavnMWh_.cjs.map +0 -1
- package/lib/node/getRawRequest-DnwmXyOW.mjs +0 -24
- package/lib/node/getRawRequest-DnwmXyOW.mjs.map +0 -1
- package/lib/node/glossary-BLKRyLBd.cjs +0 -12
- package/lib/node/glossary-BLKRyLBd.cjs.map +0 -1
- package/lib/node/glossary-glQBRnVD.mjs +0 -6
- package/lib/node/glossary-glQBRnVD.mjs.map +0 -1
- package/lib/node/handleRequest-Bb7Y-XLw.cjs +0 -220
- package/lib/node/handleRequest-Bb7Y-XLw.cjs.map +0 -1
- package/lib/node/handleRequest-Y97UwBbF.mjs +0 -190
- package/lib/node/handleRequest-Y97UwBbF.mjs.map +0 -1
- package/lib/node/hasConfigurableGlobal-C97fWuaA.cjs +0 -26
- package/lib/node/hasConfigurableGlobal-C97fWuaA.cjs.map +0 -1
- package/lib/node/hasConfigurableGlobal-DBJA0vjm.mjs.map +0 -1
- package/lib/node/index-BMbJ8FXL.d.cts +0 -113
- package/lib/node/index-C0YAQ36w.d.mts +0 -113
- package/lib/node/index.cjs +0 -54
- package/lib/node/index.cjs.map +0 -1
- package/lib/node/index.d.cts +0 -75
- package/lib/node/index.d.mts +0 -75
- package/lib/node/index.mjs.map +0 -1
- package/lib/node/interceptors/ClientRequest/index.cjs +0 -6
- package/lib/node/interceptors/ClientRequest/index.d.cts +0 -2
- package/lib/node/interceptors/ClientRequest/index.d.mts +0 -3
- package/lib/node/interceptors/ClientRequest/index.mjs +0 -6
- package/lib/node/interceptors/XMLHttpRequest/index.cjs +0 -6
- package/lib/node/interceptors/XMLHttpRequest/index.d.cts +0 -14
- package/lib/node/interceptors/XMLHttpRequest/index.d.mts +0 -14
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +0 -6
- package/lib/node/interceptors/fetch/index.cjs +0 -5
- package/lib/node/interceptors/fetch/index.d.cts +0 -12
- package/lib/node/interceptors/fetch/index.d.mts +0 -12
- package/lib/node/interceptors/fetch/index.mjs +0 -5
- package/lib/node/node-DwCc6iuP.mjs +0 -27
- package/lib/node/node-DwCc6iuP.mjs.map +0 -1
- package/lib/node/node-dKdAf3tC.cjs +0 -39
- package/lib/node/node-dKdAf3tC.cjs.map +0 -1
- package/lib/node/presets/node.cjs +0 -22
- package/lib/node/presets/node.cjs.map +0 -1
- package/lib/node/presets/node.d.cts +0 -13
- package/lib/node/presets/node.d.mts +0 -15
- package/lib/node/presets/node.mjs +0 -22
- package/lib/node/presets/node.mjs.map +0 -1
- package/lib/node/utils/node/index.cjs +0 -4
- package/lib/node/utils/node/index.d.cts +0 -16
- package/lib/node/utils/node/index.d.mts +0 -16
- package/lib/node/utils/node/index.mjs +0 -3
- package/presets/browser/package.json +0 -5
- package/presets/node/package.json +0 -11
- package/src/BatchInterceptor.test.ts +0 -255
- package/src/BatchInterceptor.ts +0 -95
- package/src/Interceptor.test.ts +0 -205
- package/src/Interceptor.ts +0 -249
- package/src/getRawRequest.ts +0 -21
- package/src/interceptors/ClientRequest/MockHttpSocket.ts +0 -724
- package/src/interceptors/ClientRequest/agents.ts +0 -110
- package/src/interceptors/ClientRequest/utils/getIncomingMessageBody.test.ts +0 -54
- package/src/interceptors/ClientRequest/utils/getIncomingMessageBody.ts +0 -45
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.test.ts +0 -427
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts +0 -268
- package/src/interceptors/ClientRequest/utils/parserUtils.ts +0 -48
- package/src/interceptors/Socket/MockSocket.test.ts +0 -264
- package/src/interceptors/Socket/MockSocket.ts +0 -58
- package/src/interceptors/Socket/utils/baseUrlFromConnectionOptions.ts +0 -26
- package/src/interceptors/Socket/utils/normalizeSocketWriteArgs.test.ts +0 -52
- package/src/interceptors/Socket/utils/normalizeSocketWriteArgs.ts +0 -33
- package/src/interceptors/XMLHttpRequest/index.ts +0 -61
- package/src/interceptors/fetch/index.ts +0 -214
- package/src/utils/bufferUtils.ts +0 -22
- package/src/utils/canParseUrl.ts +0 -13
- package/src/utils/emitAsync.ts +0 -25
- package/src/utils/fetchUtils.ts +0 -119
- package/src/utils/nextTick.ts +0 -11
- package/src/utils/node/index.ts +0 -39
- /package/src/{createRequestId.ts → create-request-id.ts} +0 -0
- /package/src/{InterceptorError.ts → interceptor-error.ts} +0 -0
- /package/src/interceptors/WebSocket/utils/{bindEvent.ts → bind-event.ts} +0 -0
- /package/src/interceptors/XMLHttpRequest/polyfills/{EventPolyfill.ts → event-polyfill.ts} +0 -0
- /package/src/interceptors/XMLHttpRequest/utils/{getBodyByteLength.ts → get-body-byte-length.ts} +0 -0
- /package/src/interceptors/XMLHttpRequest/utils/{isDomParserSupportedType.ts → is-dom-parser-supported-type.ts} +0 -0
- /package/src/interceptors/fetch/utils/{createNetworkError.ts → create-network-error.ts} +0 -0
- /package/src/utils/{findPropertySource.ts → find-property-source.ts} +0 -0
- /package/src/utils/{getCleanUrl.ts → get-clean-url.ts} +0 -0
- /package/src/utils/{getValueBySymbol.ts → get-value-by-symbol.ts} +0 -0
- /package/src/utils/{isNodeLikeError.ts → is-node-like-error.ts} +0 -0
- /package/src/utils/{isObject.ts → is-object.ts} +0 -0
- /package/src/utils/{isPropertyAccessible.ts → is-property-accessible.ts} +0 -0
- /package/src/utils/{parseJson.ts → parse-json.ts} +0 -0
- /package/src/utils/{resolveWebSocketUrl.ts → resolve-web-socket-url.ts} +0 -0
package/README.md
CHANGED
|
@@ -13,17 +13,10 @@ npm install @depup/mswjs__interceptors
|
|
|
13
13
|
|
|
14
14
|
| Field | Value |
|
|
15
15
|
|-------|-------|
|
|
16
|
-
| Original | [@mswjs/interceptors](https://www.npmjs.com/package/@mswjs/interceptors) @ 0.
|
|
17
|
-
| Processed | 2026-
|
|
16
|
+
| Original | [@mswjs/interceptors](https://www.npmjs.com/package/@mswjs/interceptors) @ 0.42.0 |
|
|
17
|
+
| Processed | 2026-07-22 |
|
|
18
18
|
| Smoke test | passed |
|
|
19
|
-
| Deps updated |
|
|
20
|
-
|
|
21
|
-
## Dependency Changes
|
|
22
|
-
|
|
23
|
-
| Dependency | From | To |
|
|
24
|
-
|------------|------|-----|
|
|
25
|
-
| @open-draft/deferred-promise | ^2.2.0 | ^3.0.0 |
|
|
26
|
-
| @open-draft/until | ^2.0.0 | ^3.0.1 |
|
|
19
|
+
| Deps updated | 0 |
|
|
27
20
|
|
|
28
21
|
---
|
|
29
22
|
|
package/changes.json
CHANGED
|
@@ -1,14 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"bumped": {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
"to": "^3.0.0"
|
|
6
|
-
},
|
|
7
|
-
"@open-draft/until": {
|
|
8
|
-
"from": "^2.0.0",
|
|
9
|
-
"to": "^3.0.1"
|
|
10
|
-
}
|
|
11
|
-
},
|
|
12
|
-
"timestamp": "2026-03-24T20:22:00.437Z",
|
|
13
|
-
"totalUpdated": 2
|
|
2
|
+
"bumped": {},
|
|
3
|
+
"timestamp": "2026-07-22T16:31:58.647Z",
|
|
4
|
+
"totalUpdated": 0
|
|
14
5
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//#region src/utils/
|
|
1
|
+
//#region src/utils/buffer-utils.ts
|
|
2
2
|
const encoder = new TextEncoder();
|
|
3
3
|
function encodeBuffer(text) {
|
|
4
4
|
return encoder.encode(text);
|
|
@@ -14,7 +14,7 @@ function decodeBuffer(buffer, encoding) {
|
|
|
14
14
|
function toArrayBuffer(array) {
|
|
15
15
|
return array.buffer.slice(array.byteOffset, array.byteOffset + array.byteLength);
|
|
16
16
|
}
|
|
17
|
-
|
|
18
17
|
//#endregion
|
|
19
18
|
export { encodeBuffer as n, toArrayBuffer as r, decodeBuffer as t };
|
|
20
|
-
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=buffer-utils-DJj7YzLG.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buffer-utils-DJj7YzLG.js","names":[],"sources":["../../src/utils/buffer-utils.ts"],"sourcesContent":["const encoder = new TextEncoder()\n\nexport function encodeBuffer(text: string) {\n return encoder.encode(text)\n}\n\nexport function decodeBuffer(buffer: AllowSharedBufferSource, encoding?: string): string {\n const decoder = new TextDecoder(encoding)\n return decoder.decode(buffer)\n}\n\n/**\n * Create an `ArrayBuffer` from the given `Uint8Array`.\n * Takes the byte offset into account to produce the right buffer\n * in the case when the buffer is bigger than the data view.\n */\nexport function toArrayBuffer(array: Uint8Array<ArrayBuffer>): ArrayBuffer {\n return array.buffer.slice(\n array.byteOffset,\n array.byteOffset + array.byteLength\n )\n}\n\nexport function toBuffer(\n data: string | Buffer | Uint8Array<ArrayBufferLike>,\n encoding?: BufferEncoding\n): Buffer {\n if (Buffer.isBuffer(data)) {\n return data\n }\n\n if (data instanceof Uint8Array) {\n return Buffer.from(data.buffer)\n }\n\n return Buffer.from(data, encoding)\n}\n"],"mappings":";AAAA,MAAM,UAAU,IAAI,YAAY;AAEhC,SAAgB,aAAa,MAAc;CACzC,OAAO,QAAQ,OAAO,IAAI;AAC5B;AAEA,SAAgB,aAAa,QAAiC,UAA2B;CAEvF,OAAO,IADa,YAAY,QACnB,CAAC,CAAC,OAAO,MAAM;AAC9B;;;;;;AAOA,SAAgB,cAAc,OAA6C;CACzE,OAAO,MAAM,OAAO,MAClB,MAAM,YACN,MAAM,aAAa,MAAM,UAC3B;AACF"}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { Emitter } from "rettime";
|
|
2
|
+
import debug from "debug";
|
|
3
|
+
//#region src/disposable.ts
|
|
4
|
+
var Disposable = class {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.subscriptions = [];
|
|
7
|
+
}
|
|
8
|
+
dispose() {
|
|
9
|
+
let subscription;
|
|
10
|
+
while (subscription = this.subscriptions.pop()) subscription();
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/utils/logger.ts
|
|
15
|
+
const LOG_TIMESTAMP_REGEXP = /\d{2}:\d{2}:\d{2}\.\d{3}/;
|
|
16
|
+
function normalizeNamespace(namespace) {
|
|
17
|
+
return namespace.split(":").map((segment) => {
|
|
18
|
+
return segment.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[^a-zA-Z0-9]+/g, "-").replace(/^-|-$/g, "").toLowerCase();
|
|
19
|
+
}).filter(Boolean).join(":");
|
|
20
|
+
}
|
|
21
|
+
function getTimestamp() {
|
|
22
|
+
return (/* @__PURE__ */ new Date()).toISOString().slice(11, 23);
|
|
23
|
+
}
|
|
24
|
+
async function readBody(message) {
|
|
25
|
+
if (message.body == null) return null;
|
|
26
|
+
try {
|
|
27
|
+
return await message.clone().text();
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function formatHeaders(headers) {
|
|
33
|
+
return Array.from(headers.entries()).map(([name, value]) => {
|
|
34
|
+
return `${name}: ${value}`;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
async function formatHttpMessage(startLine, message) {
|
|
38
|
+
const lines = [startLine, ...formatHeaders(message.headers)];
|
|
39
|
+
const body = await readBody(message);
|
|
40
|
+
lines.push("", body ?? "");
|
|
41
|
+
return lines.join("\n");
|
|
42
|
+
}
|
|
43
|
+
async function formatRequest(request) {
|
|
44
|
+
return formatHttpMessage(`${request.method} ${request.url}`, request);
|
|
45
|
+
}
|
|
46
|
+
async function formatResponse(response) {
|
|
47
|
+
const statusText = response.statusText ? ` ${response.statusText}` : "";
|
|
48
|
+
return formatHttpMessage(`HTTP ${response.status}${statusText}`, response);
|
|
49
|
+
}
|
|
50
|
+
function formatLogArguments(arguments_) {
|
|
51
|
+
const message = arguments_[0];
|
|
52
|
+
if (typeof message === "string") {
|
|
53
|
+
const messageWithoutDebugTimestamp = message.replace(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z /, "");
|
|
54
|
+
const timestampMatch = messageWithoutDebugTimestamp.match(LOG_TIMESTAMP_REGEXP);
|
|
55
|
+
if (!timestampMatch || timestampMatch.index === void 0) {
|
|
56
|
+
arguments_[0] = messageWithoutDebugTimestamp;
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const messagePrefix = messageWithoutDebugTimestamp.slice(0, timestampMatch.index).trim();
|
|
60
|
+
const messageBody = messageWithoutDebugTimestamp.slice(timestampMatch.index + timestampMatch[0].length).trimStart();
|
|
61
|
+
arguments_[0] = `${timestampMatch[0]} ${messagePrefix} ${messageBody}`;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function useConciseTimestamp(logger) {
|
|
65
|
+
logger.log = (...arguments_) => {
|
|
66
|
+
formatLogArguments(arguments_);
|
|
67
|
+
debug.log(...arguments_);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function isVerboseLoggingEnabled() {
|
|
71
|
+
if (typeof process !== "undefined" && process.env.DEBUG_LEVEL === "verbose") return true;
|
|
72
|
+
/**
|
|
73
|
+
* @note Consult the localStorage only in browser-like environments.
|
|
74
|
+
* In Node.js 26+, reading "globalThis.localStorage" without the
|
|
75
|
+
* "--localstorage-file" flag set emits an experimental warning
|
|
76
|
+
* (a try/catch cannot suppress it). Node.js consumers control the
|
|
77
|
+
* log level via the "DEBUG_LEVEL" environment variable above.
|
|
78
|
+
*/
|
|
79
|
+
if (typeof document === "undefined") return false;
|
|
80
|
+
try {
|
|
81
|
+
return globalThis.localStorage?.getItem("debugLevel") === "verbose";
|
|
82
|
+
} catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function createLogger(namespace) {
|
|
87
|
+
const logger = debug(`interceptors:${normalizeNamespace(namespace)}`);
|
|
88
|
+
Reflect.set(logger, "useColors", true);
|
|
89
|
+
useConciseTimestamp(logger);
|
|
90
|
+
return {
|
|
91
|
+
info(message, ...positionals) {
|
|
92
|
+
logger(`${getTimestamp()} ${message}`, ...positionals);
|
|
93
|
+
},
|
|
94
|
+
verbose(message, ...positionals) {
|
|
95
|
+
if (!isVerboseLoggingEnabled()) return;
|
|
96
|
+
logger(`${getTimestamp()} ${message}`, ...positionals);
|
|
97
|
+
},
|
|
98
|
+
isEnabled(level) {
|
|
99
|
+
return logger.enabled && (level === "default" || isVerboseLoggingEnabled());
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/interceptor.ts
|
|
105
|
+
const interceptorsRegistry = globalThis.__MSW_INTERCEPTORS_REGISTRY ??= /* @__PURE__ */ new Map();
|
|
106
|
+
var Interceptor = class extends Disposable {
|
|
107
|
+
#owners;
|
|
108
|
+
static singleton(InterceptorClass) {
|
|
109
|
+
const symbol = InterceptorClass.symbol;
|
|
110
|
+
const existing = interceptorsRegistry.get(symbol);
|
|
111
|
+
if (existing instanceof InterceptorClass) return existing;
|
|
112
|
+
const newInstance = new InterceptorClass();
|
|
113
|
+
interceptorsRegistry.set(symbol, newInstance);
|
|
114
|
+
return newInstance;
|
|
115
|
+
}
|
|
116
|
+
constructor() {
|
|
117
|
+
super();
|
|
118
|
+
this.on = (type, listener, options) => {
|
|
119
|
+
return this.emitter.on(type, listener, options);
|
|
120
|
+
};
|
|
121
|
+
this.once = (type, listener, options) => {
|
|
122
|
+
return this.emitter.once(type, listener, options);
|
|
123
|
+
};
|
|
124
|
+
this.listeners = (type) => {
|
|
125
|
+
return this.emitter.listeners(type);
|
|
126
|
+
};
|
|
127
|
+
this.listenerCount = (type) => {
|
|
128
|
+
return this.emitter.listenerCount(type);
|
|
129
|
+
};
|
|
130
|
+
this.removeListener = (type, listener) => {
|
|
131
|
+
return this.emitter.removeListener(type, listener);
|
|
132
|
+
};
|
|
133
|
+
this.removeAllListeners = (type) => {
|
|
134
|
+
this.logger.info("removeAllListeners %o", { eventType: type ?? "*" });
|
|
135
|
+
return this.emitter.removeAllListeners(type);
|
|
136
|
+
};
|
|
137
|
+
this.#owners = /* @__PURE__ */ new Set();
|
|
138
|
+
this.readyState = "INACTIVE";
|
|
139
|
+
this.emitter = new Emitter();
|
|
140
|
+
this.logger = createLogger(this.#getLoggerNamespace());
|
|
141
|
+
}
|
|
142
|
+
apply(owner = this) {
|
|
143
|
+
if (this.#owners.has(owner)) return;
|
|
144
|
+
if (this.readyState !== "ACTIVE" && !this.predicate()) return;
|
|
145
|
+
this.#owners.add(owner);
|
|
146
|
+
if (this.readyState === "ACTIVE") return;
|
|
147
|
+
try {
|
|
148
|
+
this.setup();
|
|
149
|
+
this.readyState = "ACTIVE";
|
|
150
|
+
this.logger.info("apply");
|
|
151
|
+
} catch (error) {
|
|
152
|
+
this.dispose(owner);
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
dispose(owner = this) {
|
|
157
|
+
if (!this.#owners.delete(owner)) return;
|
|
158
|
+
if (this.#owners.size > 0) return;
|
|
159
|
+
super.dispose();
|
|
160
|
+
this.emitter.removeAllListeners();
|
|
161
|
+
this.readyState = "DISPOSED";
|
|
162
|
+
this.logger.info("disable");
|
|
163
|
+
}
|
|
164
|
+
#getLoggerNamespace() {
|
|
165
|
+
const symbolDescription = this.constructor.symbol?.description;
|
|
166
|
+
if (symbolDescription) return symbolDescription.replace(/-interceptor$/, "");
|
|
167
|
+
return this.constructor.name.replace(/Interceptor$/, "");
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/create-request-id.ts
|
|
172
|
+
/**
|
|
173
|
+
* Generate a random ID string to represent a request.
|
|
174
|
+
* @example
|
|
175
|
+
* createRequestId()
|
|
176
|
+
* // "f774b6c9c600f"
|
|
177
|
+
*/
|
|
178
|
+
function createRequestId() {
|
|
179
|
+
return Math.random().toString(16).slice(2);
|
|
180
|
+
}
|
|
181
|
+
//#endregion
|
|
182
|
+
export { formatResponse as a, formatRequest as i, Interceptor as n, createLogger as r, createRequestId as t };
|
|
183
|
+
|
|
184
|
+
//# sourceMappingURL=create-request-id-DlEd4GOA.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-request-id-DlEd4GOA.js","names":["#owners","#getLoggerNamespace"],"sources":["../../src/disposable.ts","../../src/utils/logger.ts","../../src/interceptor.ts","../../src/create-request-id.ts"],"sourcesContent":["export type DisposableSubscription = () => void\n\nexport class Disposable {\n protected subscriptions: Array<DisposableSubscription> = []\n\n public dispose() {\n let subscription: DisposableSubscription | undefined\n\n while ((subscription = this.subscriptions.pop())) {\n subscription()\n }\n }\n}\n","import debug from 'debug'\n\nexport type LogLevel = 'default' | 'verbose'\n\nexport interface Logger {\n info(message: string, ...positionals: Array<unknown>): void\n verbose(message: string, ...positionals: Array<unknown>): void\n isEnabled(level: LogLevel): boolean\n}\n\nconst LOG_TIMESTAMP_REGEXP = /\\d{2}:\\d{2}:\\d{2}\\.\\d{3}/\n\nfunction normalizeNamespace(namespace: string): string {\n return namespace\n .split(':')\n .map((segment) => {\n return segment\n .replace(/([a-z0-9])([A-Z])/g, '$1-$2')\n .replace(/[^a-zA-Z0-9]+/g, '-')\n .replace(/^-|-$/g, '')\n .toLowerCase()\n })\n .filter(Boolean)\n .join(':')\n}\n\nfunction getTimestamp(): string {\n return new Date().toISOString().slice(11, 23)\n}\n\nasync function readBody(message: Request | Response): Promise<string | null> {\n if (message.body == null) {\n return null\n }\n\n try {\n return await message.clone().text()\n } catch {\n return null\n }\n}\n\nfunction formatHeaders(headers: Headers): Array<string> {\n return Array.from(headers.entries()).map(([name, value]) => {\n return `${name}: ${value}`\n })\n}\n\nasync function formatHttpMessage(\n startLine: string,\n message: Request | Response\n): Promise<string> {\n const lines = [startLine, ...formatHeaders(message.headers)]\n const body = await readBody(message)\n\n lines.push('', body ?? '')\n\n return lines.join('\\n')\n}\n\nexport async function formatRequest(request: Request): Promise<string> {\n return formatHttpMessage(`${request.method} ${request.url}`, request)\n}\n\nexport async function formatResponse(response: Response): Promise<string> {\n const statusText = response.statusText ? ` ${response.statusText}` : ''\n return formatHttpMessage(\n `HTTP ${response.status}${statusText}`,\n response\n )\n}\n\nfunction formatLogArguments(arguments_: Array<unknown>): void {\n const message = arguments_[0]\n\n if (typeof message === 'string') {\n const messageWithoutDebugTimestamp = message.replace(\n /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z /,\n ''\n )\n const timestampMatch = messageWithoutDebugTimestamp.match(\n LOG_TIMESTAMP_REGEXP\n )\n\n if (!timestampMatch || timestampMatch.index === undefined) {\n arguments_[0] = messageWithoutDebugTimestamp\n return\n }\n\n const messagePrefix = messageWithoutDebugTimestamp\n .slice(0, timestampMatch.index)\n .trim()\n const messageBody = messageWithoutDebugTimestamp\n .slice(timestampMatch.index + timestampMatch[0].length)\n .trimStart()\n\n arguments_[0] = `${timestampMatch[0]} ${messagePrefix} ${messageBody}`\n }\n}\n\nfunction useConciseTimestamp(logger: debug.Debugger): void {\n logger.log = (...arguments_) => {\n formatLogArguments(arguments_)\n debug.log(...arguments_)\n }\n}\n\nfunction isVerboseLoggingEnabled(): boolean {\n if (typeof process !== 'undefined' && process.env.DEBUG_LEVEL === 'verbose') {\n return true\n }\n\n /**\n * @note Consult the localStorage only in browser-like environments.\n * In Node.js 26+, reading \"globalThis.localStorage\" without the\n * \"--localstorage-file\" flag set emits an experimental warning\n * (a try/catch cannot suppress it). Node.js consumers control the\n * log level via the \"DEBUG_LEVEL\" environment variable above.\n */\n if (typeof document === 'undefined') {\n return false\n }\n\n try {\n return globalThis.localStorage?.getItem('debugLevel') === 'verbose'\n } catch {\n return false\n }\n}\n\nexport function createLogger(namespace: string): Logger {\n const normalizedNamespace = normalizeNamespace(namespace)\n const logger = debug(`interceptors:${normalizedNamespace}`)\n Reflect.set(logger, 'useColors', true)\n useConciseTimestamp(logger)\n\n return {\n info(message, ...positionals) {\n logger(`${getTimestamp()} ${message}`, ...positionals)\n },\n verbose(message, ...positionals) {\n if (!isVerboseLoggingEnabled()) {\n return\n }\n\n logger(`${getTimestamp()} ${message}`, ...positionals)\n },\n isEnabled(level) {\n return (\n logger.enabled && (level === 'default' || isVerboseLoggingEnabled())\n )\n },\n }\n}\n","import { Emitter, type DefaultEventMap } from 'rettime'\nimport { Disposable } from './disposable'\nimport { createLogger, type Logger } from './utils/logger'\n\nexport enum InterceptorReadyState {\n INACTIVE = 'INACTIVE',\n ACTIVE = 'ACTIVE',\n DISPOSED = 'DISPOSED',\n}\n\ndeclare global {\n var __MSW_INTERCEPTORS_REGISTRY: Map<symbol, Interceptor<any>> | undefined\n}\n\nconst interceptorsRegistry = (globalThis.__MSW_INTERCEPTORS_REGISTRY ??=\n new Map<symbol, Interceptor<any>>())\n\nexport abstract class Interceptor<\n Events extends DefaultEventMap,\n> extends Disposable {\n declare ['constructor']: typeof Interceptor\n\n protected emitter: Emitter<Events>\n protected readonly logger: Logger\n\n public readyState: InterceptorReadyState\n\n static readonly symbol: symbol\n\n #owners: Set<object>\n\n static singleton<T extends Interceptor<any>>(\n InterceptorClass: (new () => T) & { symbol: symbol }\n ): T {\n const symbol = InterceptorClass.symbol\n const existing = interceptorsRegistry.get(symbol)\n\n if (existing instanceof InterceptorClass) {\n return existing\n }\n\n const newInstance = new InterceptorClass()\n interceptorsRegistry.set(symbol, newInstance)\n return newInstance\n }\n\n constructor() {\n super()\n\n this.#owners = new Set()\n this.readyState = InterceptorReadyState.INACTIVE\n this.emitter = new Emitter()\n this.logger = createLogger(this.#getLoggerNamespace())\n }\n\n protected abstract predicate(): boolean\n protected abstract setup(): void\n\n public apply(owner: object = this): void {\n if (this.#owners.has(owner)) {\n return\n }\n\n if (\n this.readyState !== InterceptorReadyState.ACTIVE &&\n !this.predicate()\n ) {\n return\n }\n\n this.#owners.add(owner)\n\n if (this.readyState === InterceptorReadyState.ACTIVE) {\n return\n }\n\n try {\n this.setup()\n this.readyState = InterceptorReadyState.ACTIVE\n this.logger.info('apply')\n } catch (error) {\n this.dispose(owner)\n throw error\n }\n }\n\n public dispose(owner: object = this): void {\n if (!this.#owners.delete(owner)) {\n return\n }\n\n if (this.#owners.size > 0) {\n return\n }\n\n super.dispose()\n this.emitter.removeAllListeners()\n this.readyState = InterceptorReadyState.DISPOSED\n this.logger.info('disable')\n }\n\n public on: Emitter<Events>['on'] = (type, listener, options) => {\n return this.emitter.on(type, listener, options)\n }\n\n public once: Emitter<Events>['once'] = (type, listener, options) => {\n return this.emitter.once(type, listener, options)\n }\n\n public listeners: Emitter<Events>['listeners'] = (type) => {\n return this.emitter.listeners(type)\n }\n\n public listenerCount: Emitter<Events>['listenerCount'] = (type) => {\n return this.emitter.listenerCount(type)\n }\n\n public removeListener: Emitter<Events>['removeListener'] = (\n type,\n listener\n ) => {\n return this.emitter.removeListener(type, listener)\n }\n\n public removeAllListeners: Emitter<Events>['removeAllListeners'] = (type) => {\n this.logger.info('removeAllListeners %o', { eventType: type ?? '*' })\n return this.emitter.removeAllListeners(type)\n }\n\n #getLoggerNamespace(): string {\n const symbolDescription = this.constructor.symbol?.description\n\n if (symbolDescription) {\n return symbolDescription.replace(/-interceptor$/, '')\n }\n\n return this.constructor.name.replace(/Interceptor$/, '')\n }\n}\n","/**\n * Generate a random ID string to represent a request.\n * @example\n * createRequestId()\n * // \"f774b6c9c600f\"\n */\nexport function createRequestId(): string {\n return Math.random().toString(16).slice(2)\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import { a as formatResponse } from "./create-request-id-DlEd4GOA.js";
|
|
2
|
+
import { invariant } from "outvariant";
|
|
3
|
+
//#region src/interceptor-error.ts
|
|
4
|
+
var InterceptorError = class InterceptorError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "InterceptorError";
|
|
8
|
+
Object.setPrototypeOf(this, InterceptorError.prototype);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/request-controller.ts
|
|
13
|
+
var RequestController = class RequestController {
|
|
14
|
+
static {
|
|
15
|
+
this.PENDING = 0;
|
|
16
|
+
}
|
|
17
|
+
static {
|
|
18
|
+
this.PASSTHROUGH = 1;
|
|
19
|
+
}
|
|
20
|
+
static {
|
|
21
|
+
this.RESPONSE = 2;
|
|
22
|
+
}
|
|
23
|
+
static {
|
|
24
|
+
this.ERROR = 3;
|
|
25
|
+
}
|
|
26
|
+
#handled;
|
|
27
|
+
constructor(request, source, options) {
|
|
28
|
+
this.request = request;
|
|
29
|
+
this.source = source;
|
|
30
|
+
this.options = options;
|
|
31
|
+
this.readyState = RequestController.PENDING;
|
|
32
|
+
this.#handled = Promise.withResolvers();
|
|
33
|
+
this.handled = this.#handled.promise;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Perform this request as-is.
|
|
37
|
+
*/
|
|
38
|
+
async passthrough() {
|
|
39
|
+
invariant.as(InterceptorError, this.readyState === RequestController.PENDING, "Failed to passthrough the \"%s %s\" request: the request has already been handled", this.request.method, this.request.url);
|
|
40
|
+
this.readyState = RequestController.PASSTHROUGH;
|
|
41
|
+
if (this.options) this.options.logger.info("[%s] passthrough", this.options.requestId);
|
|
42
|
+
await this.source.passthrough();
|
|
43
|
+
this.#handled.resolve();
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Respond to this request with the given `Response` instance.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* controller.respondWith(new Response())
|
|
50
|
+
* controller.respondWith(Response.json({ id }))
|
|
51
|
+
* controller.respondWith(Response.error())
|
|
52
|
+
*/
|
|
53
|
+
respondWith(response) {
|
|
54
|
+
invariant.as(InterceptorError, this.readyState === RequestController.PENDING, "Failed to respond to the \"%s %s\" request with \"%d %s\": the request has already been handled (%d)", this.request.method, this.request.url, response.status, response.statusText || "OK", this.readyState);
|
|
55
|
+
this.readyState = RequestController.RESPONSE;
|
|
56
|
+
if (this.options?.logger.isEnabled("default")) {
|
|
57
|
+
const { logger, requestId } = this.options;
|
|
58
|
+
formatResponse(response).then((message) => {
|
|
59
|
+
logger.info("[%s] mocked %s", requestId, message);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
this.#handled.resolve();
|
|
63
|
+
/**
|
|
64
|
+
* @note Although `source.respondWith()` is potentially asynchronous,
|
|
65
|
+
* do NOT await it for backward-compatibility. Awaiting it will short-circuit
|
|
66
|
+
* the request listener invocation as soon as a listener responds to a request.
|
|
67
|
+
* Ideally, that's what we want, but that's not what we promise the user.
|
|
68
|
+
*/
|
|
69
|
+
this.source.respondWith(response);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Error this request with the given reason.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* controller.errorWith()
|
|
76
|
+
* controller.errorWith(new Error('Oops!'))
|
|
77
|
+
* controller.errorWith({ message: 'Oops!'})
|
|
78
|
+
*/
|
|
79
|
+
errorWith(reason) {
|
|
80
|
+
invariant.as(InterceptorError, this.readyState === RequestController.PENDING, "Failed to error the \"%s %s\" request with \"%s\": the request has already been handled (%d)", this.request.method, this.request.url, reason?.toString(), this.readyState);
|
|
81
|
+
this.readyState = RequestController.ERROR;
|
|
82
|
+
if (this.options) this.options.logger.info("[%s] error %o", this.options.requestId, reason);
|
|
83
|
+
this.source.errorWith(reason);
|
|
84
|
+
this.#handled.resolve();
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/interceptors/ClientRequest/utils/record-raw-headers.ts
|
|
89
|
+
const kRawHeaders = Symbol("kRawHeaders");
|
|
90
|
+
/**
|
|
91
|
+
* Define the raw headers symbol on the given `Headers` instance.
|
|
92
|
+
* If the symbol already exists, it gets overridden.
|
|
93
|
+
*/
|
|
94
|
+
function defineRawHeadersSymbol(headers, rawHeaders) {
|
|
95
|
+
Object.defineProperty(headers, kRawHeaders, {
|
|
96
|
+
value: rawHeaders,
|
|
97
|
+
enumerable: false,
|
|
98
|
+
configurable: true
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function getRawFetchHeaders(headers) {
|
|
102
|
+
if (!Reflect.has(headers, kRawHeaders)) return Array.from(headers.entries());
|
|
103
|
+
const rawHeaders = Reflect.get(headers, kRawHeaders);
|
|
104
|
+
return rawHeaders.length > 0 ? rawHeaders : Array.from(headers.entries());
|
|
105
|
+
}
|
|
106
|
+
function copyRawHeaders(source, destination) {
|
|
107
|
+
const rawHeaders = [...getRawFetchHeaders(source)];
|
|
108
|
+
if (rawHeaders.length === 0) return;
|
|
109
|
+
/**
|
|
110
|
+
* @note Add headers from trhe destination that raw headers from the source
|
|
111
|
+
* don't have. Undici automatically appends a "Content-Type" header for responses
|
|
112
|
+
* and, for some reason, that change is not recorded. This preserves it.
|
|
113
|
+
*/
|
|
114
|
+
for (const [name, value] of destination) if (rawHeaders.every((header) => header[0].toLowerCase() !== name.toLowerCase())) rawHeaders.push([name, value]);
|
|
115
|
+
defineRawHeadersSymbol(destination, rawHeaders);
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/utils/get-value-by-symbol.ts
|
|
119
|
+
/**
|
|
120
|
+
* Returns the value behind the symbol with the given name.
|
|
121
|
+
*/
|
|
122
|
+
function getValueBySymbol(symbolName, source) {
|
|
123
|
+
const symbol = Object.getOwnPropertySymbols(source).find((symbol) => {
|
|
124
|
+
return symbol.description === symbolName;
|
|
125
|
+
});
|
|
126
|
+
if (symbol) return Reflect.get(source, symbol);
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/utils/is-object.ts
|
|
130
|
+
/**
|
|
131
|
+
* Determines if a given value is an instance of object.
|
|
132
|
+
*/
|
|
133
|
+
function isObject(value, loose = false) {
|
|
134
|
+
return loose ? Object.prototype.toString.call(value).startsWith("[object ") : Object.prototype.toString.call(value) === "[object Object]";
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/utils/is-property-accessible.ts
|
|
138
|
+
/**
|
|
139
|
+
* A function that validates if property access is possible on an object
|
|
140
|
+
* without throwing. It returns `true` if the property access is possible
|
|
141
|
+
* and `false` otherwise.
|
|
142
|
+
*
|
|
143
|
+
* Environments like miniflare will throw on property access on certain objects
|
|
144
|
+
* like Request and Response, for unimplemented properties.
|
|
145
|
+
*/
|
|
146
|
+
function isPropertyAccessible(obj, key) {
|
|
147
|
+
try {
|
|
148
|
+
obj[key];
|
|
149
|
+
return true;
|
|
150
|
+
} catch {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/utils/response-utils.ts
|
|
156
|
+
/**
|
|
157
|
+
* Creates a generic 500 Unhandled Exception response.
|
|
158
|
+
*/
|
|
159
|
+
function createServerErrorResponse(body) {
|
|
160
|
+
return new Response(JSON.stringify(body instanceof Error ? {
|
|
161
|
+
name: body.name,
|
|
162
|
+
message: body.message,
|
|
163
|
+
stack: body.stack
|
|
164
|
+
} : body), {
|
|
165
|
+
status: 500,
|
|
166
|
+
statusText: "Unhandled Exception",
|
|
167
|
+
headers: { "Content-Type": "application/json" }
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Check if the given response is a `Response.error()`.
|
|
172
|
+
*
|
|
173
|
+
* @note Some environments, like Miniflare (Cloudflare) do not
|
|
174
|
+
* implement the "Response.type" property and throw on its access.
|
|
175
|
+
* Safely check if we can access "type" on "Response" before continuing.
|
|
176
|
+
* @see https://github.com/mswjs/msw/issues/1834
|
|
177
|
+
*/
|
|
178
|
+
function isResponseError(response) {
|
|
179
|
+
return response != null && response instanceof Response && isPropertyAccessible(response, "type") && response.type === "error";
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Check if the given value is a `Response` or a Response-like object.
|
|
183
|
+
* This is different from `value instanceof Response` because it supports
|
|
184
|
+
* custom `Response` constructors, like the one when using Undici directly.
|
|
185
|
+
*/
|
|
186
|
+
function isResponseLike(value) {
|
|
187
|
+
return isObject(value, true) && isPropertyAccessible(value, "status") && isPropertyAccessible(value, "statusText") && isPropertyAccessible(value, "bodyUsed");
|
|
188
|
+
}
|
|
189
|
+
//#endregion
|
|
190
|
+
//#region src/utils/fetch-utils.ts
|
|
191
|
+
var FetchRequest = class FetchRequest extends Request {
|
|
192
|
+
static #resolveProperty(input, init = {}, key) {
|
|
193
|
+
return init[key] ?? (input instanceof Request ? input[key] : void 0);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Check if the given request method is configurable.
|
|
197
|
+
* @see https://fetch.spec.whatwg.org/#methods
|
|
198
|
+
*/
|
|
199
|
+
static isConfigurableMethod(method) {
|
|
200
|
+
return method !== "CONNECT" && method !== "TRACE" && method !== "TRACK";
|
|
201
|
+
}
|
|
202
|
+
static isMethodWithBody(method) {
|
|
203
|
+
return method !== "HEAD" && method !== "GET" && FetchRequest.isConfigurableMethod(method);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Check if the given request `mode` is configurable.
|
|
207
|
+
* @see https://fetch.spec.whatwg.org/#concept-request-mode
|
|
208
|
+
*/
|
|
209
|
+
static isConfigurableMode(mode) {
|
|
210
|
+
return mode !== "navigate" && mode !== "websocket" && mode !== "webtransport";
|
|
211
|
+
}
|
|
212
|
+
constructor(input, init) {
|
|
213
|
+
const method = FetchRequest.#resolveProperty(input, init, "method") || "GET";
|
|
214
|
+
const safeMethod = FetchRequest.isConfigurableMethod(method) ? method : "GET";
|
|
215
|
+
const hasExplicitBody = init != null && "body" in init;
|
|
216
|
+
/**
|
|
217
|
+
* Only include `body` in the super init when it needs to be overridden.
|
|
218
|
+
* When `input` is a Request and no explicit body is in `init`, let the
|
|
219
|
+
* Request constructor handle body transfer naturally so it properly
|
|
220
|
+
* marks the original request's body as consumed (bodyUsed = true).
|
|
221
|
+
*/
|
|
222
|
+
const bodyInit = !FetchRequest.isMethodWithBody(method) ? { body: void 0 } : hasExplicitBody ? { body: init.body } : {};
|
|
223
|
+
const mode = FetchRequest.#resolveProperty(input, init, "mode") ?? void 0;
|
|
224
|
+
const safeMode = FetchRequest.isConfigurableMode(mode) ? mode : void 0;
|
|
225
|
+
super(input, {
|
|
226
|
+
...init || {},
|
|
227
|
+
method: safeMethod,
|
|
228
|
+
mode: safeMode,
|
|
229
|
+
duplex: init?.duplex ?? (FetchRequest.isMethodWithBody(method) ? "half" : void 0),
|
|
230
|
+
...bodyInit
|
|
231
|
+
});
|
|
232
|
+
if (method !== safeMethod) this.#setInternalProperty("method", method);
|
|
233
|
+
if (method === "CONNECT") {
|
|
234
|
+
const url = new URL(input instanceof Request ? input.url : input);
|
|
235
|
+
let authority;
|
|
236
|
+
/**
|
|
237
|
+
* @note Node.js has a bug parsing raw CONNECT requests URLs like
|
|
238
|
+
* "http://127.0.0.1:1337/localhost:80". It would treat "localhost:" as a protocol.
|
|
239
|
+
*/
|
|
240
|
+
if (url.protocol === "localhost:") authority = url.href;
|
|
241
|
+
else authority = url.pathname.replace(/^\/+/, "");
|
|
242
|
+
/**
|
|
243
|
+
* @note Define "url" as a getter because Undici uses their own
|
|
244
|
+
* logic to resolve the "request.url" property. Simply reassigning
|
|
245
|
+
* its value doesn't do anything. This is a destructive action
|
|
246
|
+
* but it's safe because "CONNECT" requests are forbidden per fetch.
|
|
247
|
+
*/
|
|
248
|
+
Object.defineProperty(this, "url", {
|
|
249
|
+
get: () => authority,
|
|
250
|
+
enumerable: true,
|
|
251
|
+
configurable: true
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
if (mode != null && mode !== safeMode) this.#setInternalProperty("mode", mode);
|
|
255
|
+
}
|
|
256
|
+
#setInternalProperty(key, value) {
|
|
257
|
+
const internalState = getValueBySymbol("state", this);
|
|
258
|
+
if (internalState) Reflect.set(internalState, key, value);
|
|
259
|
+
else Object.defineProperty(this, key, {
|
|
260
|
+
value,
|
|
261
|
+
enumerable: true,
|
|
262
|
+
configurable: true,
|
|
263
|
+
writable: false
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
const kStatus = Symbol("kStatus");
|
|
268
|
+
const kUrl = Symbol("kUrl");
|
|
269
|
+
var FetchResponse = class FetchResponse extends Response {
|
|
270
|
+
static from(response, init) {
|
|
271
|
+
if (response instanceof FetchResponse) return response;
|
|
272
|
+
if (isResponseError(response)) return response;
|
|
273
|
+
const fetchResponse = new FetchResponse(response.body, {
|
|
274
|
+
url: init?.url ?? response.url,
|
|
275
|
+
status: init?.status || response.status,
|
|
276
|
+
statusText: init?.statusText ?? response.statusText,
|
|
277
|
+
headers: init?.headers ?? response.headers
|
|
278
|
+
});
|
|
279
|
+
copyRawHeaders(response.headers, fetchResponse.headers);
|
|
280
|
+
return fetchResponse;
|
|
281
|
+
}
|
|
282
|
+
static {
|
|
283
|
+
this.STATUS_CODES_WITHOUT_BODY = [
|
|
284
|
+
101,
|
|
285
|
+
103,
|
|
286
|
+
204,
|
|
287
|
+
205,
|
|
288
|
+
304
|
|
289
|
+
];
|
|
290
|
+
}
|
|
291
|
+
static {
|
|
292
|
+
this.STATUS_CODES_WITH_REDIRECT = [
|
|
293
|
+
301,
|
|
294
|
+
302,
|
|
295
|
+
303,
|
|
296
|
+
307,
|
|
297
|
+
308
|
|
298
|
+
];
|
|
299
|
+
}
|
|
300
|
+
static isConfigurableStatusCode(status) {
|
|
301
|
+
return status >= 200 && status <= 599;
|
|
302
|
+
}
|
|
303
|
+
static isRedirectResponse(status) {
|
|
304
|
+
return FetchResponse.STATUS_CODES_WITH_REDIRECT.includes(status);
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Returns a boolean indicating whether the given response status
|
|
308
|
+
* code represents a response that can have a body.
|
|
309
|
+
*/
|
|
310
|
+
static isResponseWithBody(status) {
|
|
311
|
+
return !FetchResponse.STATUS_CODES_WITHOUT_BODY.includes(status);
|
|
312
|
+
}
|
|
313
|
+
static setStatus(status, response) {
|
|
314
|
+
/**
|
|
315
|
+
* @note Undici keeps an internal "Symbol(state)" that holds
|
|
316
|
+
* the actual value of response status. Update that in Node.js.
|
|
317
|
+
*/
|
|
318
|
+
const internalState = getValueBySymbol("state", response);
|
|
319
|
+
if (internalState) internalState.status = status;
|
|
320
|
+
else Object.defineProperty(response, "status", {
|
|
321
|
+
value: status,
|
|
322
|
+
enumerable: true,
|
|
323
|
+
configurable: true,
|
|
324
|
+
writable: false
|
|
325
|
+
});
|
|
326
|
+
Object.defineProperty(response, kStatus, {
|
|
327
|
+
value: status,
|
|
328
|
+
enumerable: false
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
static setUrl(url, response) {
|
|
332
|
+
if (!url || url === "about:" || !URL.canParse(url)) return;
|
|
333
|
+
const state = getValueBySymbol("state", response);
|
|
334
|
+
if (state) state.urlList.push(new URL(url));
|
|
335
|
+
else Object.defineProperty(response, "url", {
|
|
336
|
+
value: url,
|
|
337
|
+
enumerable: true,
|
|
338
|
+
configurable: true,
|
|
339
|
+
writable: false
|
|
340
|
+
});
|
|
341
|
+
Object.defineProperty(response, kUrl, {
|
|
342
|
+
value: url,
|
|
343
|
+
enumerable: false
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Parses the given raw HTTP headers into a Fetch API `Headers` instance.
|
|
348
|
+
*/
|
|
349
|
+
static parseRawHeaders(rawHeaders) {
|
|
350
|
+
const headers = new Headers();
|
|
351
|
+
for (let line = 0; line < rawHeaders.length; line += 2) headers.append(rawHeaders[line], rawHeaders[line + 1]);
|
|
352
|
+
return headers;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Safely clones the given `Response`.
|
|
356
|
+
* Coerces response clone exceptions into 500 mocked responses.
|
|
357
|
+
* Handy in the environments that introduce arbitrary response
|
|
358
|
+
* cloning restrictions, like "101 Switching Protocols" cloning
|
|
359
|
+
* in "miniflare".
|
|
360
|
+
*/
|
|
361
|
+
static clone(response) {
|
|
362
|
+
try {
|
|
363
|
+
return response.clone();
|
|
364
|
+
} catch (error) {
|
|
365
|
+
return Response.json(error instanceof Error ? {
|
|
366
|
+
name: error.name,
|
|
367
|
+
message: error.message,
|
|
368
|
+
stack: error.stack
|
|
369
|
+
} : {}, {
|
|
370
|
+
status: 500,
|
|
371
|
+
statusText: "Unclonable Response"
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
#status;
|
|
376
|
+
#url;
|
|
377
|
+
constructor(body, init = {}) {
|
|
378
|
+
const status = init.status ?? 200;
|
|
379
|
+
const safeStatus = FetchResponse.isConfigurableStatusCode(status) ? status : 200;
|
|
380
|
+
const finalBody = FetchResponse.isResponseWithBody(status) ? body : null;
|
|
381
|
+
super(finalBody, {
|
|
382
|
+
status: safeStatus,
|
|
383
|
+
statusText: init.statusText,
|
|
384
|
+
headers: init.headers
|
|
385
|
+
});
|
|
386
|
+
/**
|
|
387
|
+
* Since Node.js v24, Undici stores the Response state in an inaccessible field "#state".
|
|
388
|
+
* Forward the modified status/URL to the cloned response manually.
|
|
389
|
+
* @see https://github.com/nodejs/undici/blob/f734c87280e626c75f59aad55b65eb6a89cef392/lib/web/fetch/response.js#L242
|
|
390
|
+
*/
|
|
391
|
+
if (status !== safeStatus) FetchResponse.setStatus(status, this);
|
|
392
|
+
FetchResponse.setUrl(init.url, this);
|
|
393
|
+
}
|
|
394
|
+
clone() {
|
|
395
|
+
const clonedResponse = super.clone();
|
|
396
|
+
const customStatus = Reflect.get(this, kStatus);
|
|
397
|
+
if (customStatus) FetchResponse.setStatus(customStatus, clonedResponse);
|
|
398
|
+
const customUrl = Reflect.get(this, kUrl);
|
|
399
|
+
if (customUrl) FetchResponse.setUrl(customUrl, clonedResponse);
|
|
400
|
+
return clonedResponse;
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
//#endregion
|
|
404
|
+
export { isResponseLike as a, RequestController as c, isResponseError as i, InterceptorError as l, FetchResponse as n, isObject as o, createServerErrorResponse as r, copyRawHeaders as s, FetchRequest as t };
|
|
405
|
+
|
|
406
|
+
//# sourceMappingURL=fetch-utils-zxA_SD66.js.map
|