@mswjs/interceptors 0.41.9 → 0.42.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 +274 -228
- 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/{getRawRequest-B1BqgWG6.mjs → fetch-utils-zxA_SD66.js} +123 -52
- 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/{node/hasConfigurableGlobal-BiTmog1u.mjs → browser/has-configurable-global-Cew-dYqk.js} +5 -7
- 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.cts → index.d.ts} +33 -38
- 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} +39 -30
- 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/browser/{XMLHttpRequest-DrZAf3w6.mjs → web-C2oKsT3Q.js} +327 -255
- package/lib/browser/web-C2oKsT3Q.js.map +1 -0
- package/lib/browser/{interceptors/WebSocket/index.d.cts → 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/has-configurable-global-CJCh_g1I.js +22 -0
- 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.mts → node/index.d.ts} +120 -60
- package/lib/node/{index.mjs → index.js} +8 -11
- 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} +38 -33
- 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 +79 -118
- 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 -27
- 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 +64 -179
- package/src/interceptors/ClientRequest/utils/{recordRawHeaders.test.ts → record-raw-headers.test.ts} +1 -2
- package/src/interceptors/ClientRequest/utils/{recordRawHeaders.ts → record-raw-headers.ts} +29 -2
- package/src/interceptors/WebSocket/index.ts +33 -50
- 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} +363 -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} +55 -42
- 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/{fetchUtils.test.ts → fetch-utils.test.ts} +1 -1
- package/src/utils/{fetchUtils.ts → fetch-utils.ts} +28 -3
- 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} +61 -41
- package/src/utils/{hasConfigurableGlobal.test.ts → has-configurable-global.test.ts} +1 -2
- package/src/utils/{hasConfigurableGlobal.ts → has-configurable-global.ts} +2 -4
- 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/{patchesRegistry.test.ts → patches-registry.test.ts} +1 -1
- package/src/utils/{patchesRegistry.ts → patches-registry.ts} +1 -1
- 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-DrZAf3w6.mjs.map +0 -1
- package/lib/browser/XMLHttpRequest-gXuzzX1g.cjs +0 -751
- package/lib/browser/XMLHttpRequest-gXuzzX1g.cjs.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-DOf8Ktjs.cjs +0 -205
- package/lib/browser/createRequestId-DOf8Ktjs.cjs.map +0 -1
- package/lib/browser/createRequestId-DYCsFHOi.mjs +0 -170
- package/lib/browser/createRequestId-DYCsFHOi.mjs.map +0 -1
- package/lib/browser/fetch-5IMPqr9e.mjs +0 -236
- package/lib/browser/fetch-5IMPqr9e.mjs.map +0 -1
- package/lib/browser/fetch-Bt9rMY9c.cjs +0 -241
- package/lib/browser/fetch-Bt9rMY9c.cjs.map +0 -1
- package/lib/browser/getRawRequest-B1BqgWG6.mjs.map +0 -1
- package/lib/browser/getRawRequest-DdfaiPVH.cjs +0 -376
- package/lib/browser/getRawRequest-DdfaiPVH.cjs.map +0 -1
- package/lib/browser/glossary-1lhZx6Aw.d.cts +0 -69
- package/lib/browser/glossary-nkHWcwvY.d.mts +0 -69
- package/lib/browser/handleRequest-CJim54xG.cjs +0 -191
- package/lib/browser/handleRequest-CJim54xG.cjs.map +0 -1
- package/lib/browser/handleRequest-CPPgsG7s.mjs +0 -180
- package/lib/browser/handleRequest-CPPgsG7s.mjs.map +0 -1
- package/lib/browser/hasConfigurableGlobal-C8zq1MCg.mjs +0 -109
- package/lib/browser/hasConfigurableGlobal-C8zq1MCg.mjs.map +0 -1
- package/lib/browser/hasConfigurableGlobal-CS7adEvV.cjs +0 -126
- package/lib/browser/hasConfigurableGlobal-CS7adEvV.cjs.map +0 -1
- package/lib/browser/index.cjs +0 -70
- package/lib/browser/index.cjs.map +0 -1
- package/lib/browser/index.d.mts +0 -125
- package/lib/browser/index.mjs +0 -56
- package/lib/browser/index.mjs.map +0 -1
- package/lib/browser/interceptors/WebSocket/index.cjs +0 -618
- 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 -8
- 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 -8
- package/lib/browser/interceptors/fetch/index.cjs +0 -7
- 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 -7
- package/lib/browser/presets/browser.cjs +0 -18
- 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 -18
- 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-BGhH8z1z.cjs +0 -49
- package/lib/node/BatchInterceptor-BGhH8z1z.cjs.map +0 -1
- package/lib/node/BatchInterceptor-C4-RX5rg.mjs +0 -44
- package/lib/node/BatchInterceptor-C4-RX5rg.mjs.map +0 -1
- package/lib/node/BatchInterceptor-KKF8Q47I.d.cts +0 -26
- package/lib/node/BatchInterceptor-rSr-NH_H.d.mts +0 -26
- package/lib/node/ClientRequest-2loAH7xM.cjs +0 -1058
- package/lib/node/ClientRequest-2loAH7xM.cjs.map +0 -1
- package/lib/node/ClientRequest-DGhBX21V.mjs +0 -1049
- package/lib/node/ClientRequest-DGhBX21V.mjs.map +0 -1
- package/lib/node/Interceptor-BsZ21ue0.d.cts +0 -132
- package/lib/node/Interceptor-soD-WpQO.d.mts +0 -132
- package/lib/node/RemoteHttpInterceptor.cjs +0 -155
- 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-BtDNZipW.cjs +0 -752
- package/lib/node/XMLHttpRequest-BtDNZipW.cjs.map +0 -1
- package/lib/node/XMLHttpRequest-Dw6Wm-UU.mjs +0 -746
- package/lib/node/XMLHttpRequest-Dw6Wm-UU.mjs.map +0 -1
- package/lib/node/bufferUtils-DxPxwff_.mjs +0 -20
- package/lib/node/bufferUtils-DxPxwff_.mjs.map +0 -1
- package/lib/node/bufferUtils-S5_-2eN4.cjs +0 -38
- package/lib/node/bufferUtils-S5_-2eN4.cjs.map +0 -1
- package/lib/node/chunk-CbDLau6x.cjs +0 -34
- package/lib/node/fetch-CaC3rGEu.mjs +0 -252
- package/lib/node/fetch-CaC3rGEu.mjs.map +0 -1
- package/lib/node/fetch-D2ClLNN-.cjs +0 -259
- package/lib/node/fetch-D2ClLNN-.cjs.map +0 -1
- package/lib/node/fetchUtils-BKJ1XmiO.mjs +0 -480
- package/lib/node/fetchUtils-BKJ1XmiO.mjs.map +0 -1
- package/lib/node/fetchUtils-umV5xXBy.cjs +0 -546
- package/lib/node/fetchUtils-umV5xXBy.cjs.map +0 -1
- package/lib/node/getRawRequest-B6znU6il.cjs +0 -36
- package/lib/node/getRawRequest-B6znU6il.cjs.map +0 -1
- package/lib/node/getRawRequest-C2-1urzA.mjs +0 -24
- package/lib/node/getRawRequest-C2-1urzA.mjs.map +0 -1
- package/lib/node/handleRequest-C-cKYjIh.cjs +0 -222
- package/lib/node/handleRequest-C-cKYjIh.cjs.map +0 -1
- package/lib/node/handleRequest-FIQv5pwH.mjs +0 -192
- package/lib/node/handleRequest-FIQv5pwH.mjs.map +0 -1
- package/lib/node/hasConfigurableGlobal-BiTmog1u.mjs.map +0 -1
- package/lib/node/hasConfigurableGlobal-MjY06_Ok.cjs +0 -108
- package/lib/node/hasConfigurableGlobal-MjY06_Ok.cjs.map +0 -1
- package/lib/node/index-BJ5E5aW-.d.cts +0 -113
- package/lib/node/index-DfoF45ih.d.mts +0 -113
- package/lib/node/index.cjs +0 -53
- package/lib/node/index.cjs.map +0 -1
- package/lib/node/index.d.cts +0 -104
- package/lib/node/index.d.mts +0 -104
- 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 -7
- 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 -7
- package/lib/node/interceptors/fetch/index.cjs +0 -6
- 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 -6
- package/lib/node/node-DIKcnzhK.cjs +0 -39
- package/lib/node/node-DIKcnzhK.cjs.map +0 -1
- package/lib/node/node-lsdNwZEW.mjs +0 -27
- package/lib/node/node-lsdNwZEW.mjs.map +0 -1
- package/lib/node/presets/node.cjs +0 -23
- 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 -23
- 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 -96
- 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 -40
- package/src/interceptors/fetch/index.ts +0 -203
- package/src/utils/bufferUtils.ts +0 -22
- package/src/utils/canParseUrl.ts +0 -13
- package/src/utils/emitAsync.ts +0 -25
- 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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../../src/interceptors/ClientRequest/index.ts"],"sourcesContent":["import http from 'node:http'\nimport https from 'node:https'\nimport { runInRequestContext } from '#/src/request-context'\nimport { patchesRegistry } from '#/src/utils/patches-registry'\nimport { forwardHttpEvents } from '#/src/interceptors/http/forward-events'\nimport { NodeHttpRequestSource } from '#/src/interceptors/http/source'\nimport { Interceptor } from '../../interceptor'\nimport { HttpRequestEventMap } from '#/src/events/http'\n\n/**\n * Interceptor for HTTP requests in Node.js made via `http.ClientRequest`.\n */\nexport class ClientRequestInterceptor extends Interceptor<HttpRequestEventMap> {\n static symbol = Symbol.for('client-request-interceptor')\n\n protected predicate(): boolean {\n return true\n }\n\n protected setup(): void {\n const requestSource = Interceptor.singleton(NodeHttpRequestSource)\n const requestLogger = this.logger\n requestSource.apply(this)\n this.subscriptions.push(() => {\n requestSource.dispose(this)\n })\n\n this.subscriptions.push(\n forwardHttpEvents({\n source: requestSource,\n emitter: this.emitter,\n predicate: (initiator) => {\n return initiator instanceof http.ClientRequest\n },\n })\n )\n\n this.subscriptions.push(\n patchesRegistry.applyPatch(http, 'ClientRequest', (ClientRequest) => {\n return new Proxy(ClientRequest, {\n construct(target, args, newTarget) {\n return runInRequestContext(() => {\n return Reflect.construct(target, args, newTarget)\n }, requestLogger)\n },\n })\n }),\n patchesRegistry.applyPatch(http, 'get', (httpGet) => {\n return function mockHttpGet(...args) {\n return runInRequestContext(() => {\n return httpGet(...(args as [any, any]))\n }, requestLogger)\n }\n }),\n patchesRegistry.applyPatch(http, 'request', (httpRequest) => {\n return function mockHttpRequest(...args) {\n return runInRequestContext(() => {\n return httpRequest(...(args as [any, any]))\n }, requestLogger)\n }\n }),\n patchesRegistry.applyPatch(https, 'get', (httpsGet) => {\n return function mockHttpsGet(...args) {\n return runInRequestContext(() => {\n return httpsGet(...(args as [any, any]))\n }, requestLogger)\n }\n }),\n patchesRegistry.applyPatch(https, 'request', (httpsRequest) => {\n return function mockHttpsRequest(...args) {\n return runInRequestContext(() => {\n return httpsRequest(...(args as [any, any]))\n }, requestLogger)\n }\n })\n )\n }\n}\n"],"mappings":";;;;;;;;;AAYA,IAAa,2BAAb,cAA8C,YAAiC;;EAC7D,KAAA,SAAA,OAAO,IAAI,4BAA4B;;CAEvD,YAA+B;EAC7B,OAAO;CACT;CAEA,QAAwB;EACtB,MAAM,gBAAgB,YAAY,UAAU,qBAAqB;EACjE,MAAM,gBAAgB,KAAK;EAC3B,cAAc,MAAM,IAAI;EACxB,KAAK,cAAc,WAAW;GAC5B,cAAc,QAAQ,IAAI;EAC5B,CAAC;EAED,KAAK,cAAc,KACjB,kBAAkB;GAChB,QAAQ;GACR,SAAS,KAAK;GACd,YAAY,cAAc;IACxB,OAAO,qBAAqB,KAAK;GACnC;EACF,CAAC,CACH;EAEA,KAAK,cAAc,KACjB,gBAAgB,WAAW,MAAM,kBAAkB,kBAAkB;GACnE,OAAO,IAAI,MAAM,eAAe,EAC9B,UAAU,QAAQ,MAAM,WAAW;IACjC,OAAO,0BAA0B;KAC/B,OAAO,QAAQ,UAAU,QAAQ,MAAM,SAAS;IAClD,GAAG,aAAa;GAClB,EACF,CAAC;EACH,CAAC,GACD,gBAAgB,WAAW,MAAM,QAAQ,YAAY;GACnD,OAAO,SAAS,YAAY,GAAG,MAAM;IACnC,OAAO,0BAA0B;KAC/B,OAAO,QAAQ,GAAI,IAAmB;IACxC,GAAG,aAAa;GAClB;EACF,CAAC,GACD,gBAAgB,WAAW,MAAM,YAAY,gBAAgB;GAC3D,OAAO,SAAS,gBAAgB,GAAG,MAAM;IACvC,OAAO,0BAA0B;KAC/B,OAAO,YAAY,GAAI,IAAmB;IAC5C,GAAG,aAAa;GAClB;EACF,CAAC,GACD,gBAAgB,WAAW,OAAO,QAAQ,aAAa;GACrD,OAAO,SAAS,aAAa,GAAG,MAAM;IACpC,OAAO,0BAA0B;KAC/B,OAAO,SAAS,GAAI,IAAmB;IACzC,GAAG,aAAa;GAClB;EACF,CAAC,GACD,gBAAgB,WAAW,OAAO,YAAY,iBAAiB;GAC7D,OAAO,SAAS,iBAAiB,GAAG,MAAM;IACxC,OAAO,0BAA0B;KAC/B,OAAO,aAAa,GAAI,IAAmB;IAC7C,GAAG,aAAa;GAClB;EACF,CAAC,CACH;CACF;AACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { t as Interceptor } from "../../interceptor-dtJZ_9QT.js";
|
|
2
|
+
import { n as HttpRequestEventMap } from "../../http-CB9rQ7i_.js";
|
|
3
|
+
//#region src/interceptors/XMLHttpRequest/node.d.ts
|
|
4
|
+
declare class XMLHttpRequestInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
5
|
+
#private;
|
|
6
|
+
static symbol: symbol;
|
|
7
|
+
protected predicate(): boolean;
|
|
8
|
+
protected setup(): void;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { XMLHttpRequestInterceptor };
|
|
12
|
+
//# sourceMappingURL=node.d.ts.map
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { a as requestContext, i as forwardHttpEvents, t as NodeHttpRequestSource } from "../../source-lP0yyEtA.js";
|
|
2
|
+
import { a as createLogger, i as Interceptor } from "../../buffer-utils-BvPY1Tc-.js";
|
|
3
|
+
import { t as FetchRequest } from "../../fetch-utils-Dw1PsmtX.js";
|
|
4
|
+
import { o as patchesRegistry } from "../../net-DtMnyEeg.js";
|
|
5
|
+
import { t as hasConfigurableGlobal } from "../../has-configurable-global-CJCh_g1I.js";
|
|
6
|
+
//#region src/interceptors/XMLHttpRequest/node.ts
|
|
7
|
+
const logger = createLogger("xhr");
|
|
8
|
+
var XMLHttpRequestInterceptor = class extends Interceptor {
|
|
9
|
+
static {
|
|
10
|
+
this.symbol = Symbol.for("xhr-interceptor");
|
|
11
|
+
}
|
|
12
|
+
predicate() {
|
|
13
|
+
return hasConfigurableGlobal("XMLHttpRequest");
|
|
14
|
+
}
|
|
15
|
+
setup() {
|
|
16
|
+
const requestSource = Interceptor.singleton(NodeHttpRequestSource);
|
|
17
|
+
requestSource.apply(this);
|
|
18
|
+
this.subscriptions.push(() => {
|
|
19
|
+
requestSource.dispose(this);
|
|
20
|
+
});
|
|
21
|
+
this.subscriptions.push(forwardHttpEvents({
|
|
22
|
+
source: requestSource,
|
|
23
|
+
emitter: this.emitter,
|
|
24
|
+
predicate: (initiator) => {
|
|
25
|
+
return initiator instanceof XMLHttpRequest;
|
|
26
|
+
}
|
|
27
|
+
}));
|
|
28
|
+
logger.verbose("patching global \"XMLHttpRequest\"...");
|
|
29
|
+
const prepareRequest = this.#transformRequest.bind(this);
|
|
30
|
+
const requestLogger = this.logger;
|
|
31
|
+
this.subscriptions.push(patchesRegistry.applyPatch(globalThis, "XMLHttpRequest", (realXMLHttpRequest) => {
|
|
32
|
+
return new Proxy(realXMLHttpRequest, { construct(target, args, newTarget) {
|
|
33
|
+
const xmlHttpRequest = Reflect.construct(target, args, newTarget);
|
|
34
|
+
/**
|
|
35
|
+
* @note Scope the request context to the `send()` call.
|
|
36
|
+
* XHR in JSDOM is implemented via `http`/`https`, and the
|
|
37
|
+
* underlying `http.ClientRequest` is created within `send()`,
|
|
38
|
+
* so the initiator cascading keeps working. Binding the
|
|
39
|
+
* context to the caller's scope instead (e.g. `enterWith`)
|
|
40
|
+
* would attribute unrelated requests performed after this
|
|
41
|
+
* XMLHttpRequest to it.
|
|
42
|
+
*/
|
|
43
|
+
const realSend = xmlHttpRequest.send;
|
|
44
|
+
xmlHttpRequest.send = (...sendArgs) => {
|
|
45
|
+
return requestContext.run({
|
|
46
|
+
initiator: xmlHttpRequest,
|
|
47
|
+
logger: requestLogger,
|
|
48
|
+
transformRequest: (request) => {
|
|
49
|
+
return prepareRequest(request, xmlHttpRequest);
|
|
50
|
+
}
|
|
51
|
+
}, () => realSend.apply(xmlHttpRequest, sendArgs));
|
|
52
|
+
};
|
|
53
|
+
return xmlHttpRequest;
|
|
54
|
+
} });
|
|
55
|
+
}));
|
|
56
|
+
logger.verbose("global \"XMLHttpRequest\" patched!");
|
|
57
|
+
}
|
|
58
|
+
#transformRequest(request, initiator) {
|
|
59
|
+
const expectedCredentials = initiator.withCredentials ? "include" : "same-origin";
|
|
60
|
+
if (request.credentials === expectedCredentials) return request;
|
|
61
|
+
return new FetchRequest(request.url, {
|
|
62
|
+
...request,
|
|
63
|
+
method: request.method,
|
|
64
|
+
headers: request.headers,
|
|
65
|
+
credentials: expectedCredentials,
|
|
66
|
+
body: request.body,
|
|
67
|
+
signal: request.signal
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
//#endregion
|
|
72
|
+
export { XMLHttpRequestInterceptor };
|
|
73
|
+
|
|
74
|
+
//# sourceMappingURL=node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.js","names":["#transformRequest"],"sources":["../../../../src/interceptors/XMLHttpRequest/node.ts"],"sourcesContent":["import { requestContext } from '#/src/request-context'\nimport { hasConfigurableGlobal } from '#/src/utils/has-configurable-global'\nimport { Interceptor } from '#/src/interceptor'\nimport { forwardHttpEvents } from '#/src/interceptors/http/forward-events'\nimport { NodeHttpRequestSource } from '#/src/interceptors/http/source'\nimport { patchesRegistry } from '#/src/utils/patches-registry'\nimport { FetchRequest } from '#/src/utils/fetch-utils'\nimport { HttpRequestEventMap } from '#/src/events/http'\nimport { createLogger } from '#/src/utils/logger'\n\nconst logger = createLogger('xhr')\n\nexport class XMLHttpRequestInterceptor extends Interceptor<HttpRequestEventMap> {\n static symbol = Symbol.for('xhr-interceptor')\n\n protected predicate() {\n return hasConfigurableGlobal('XMLHttpRequest')\n }\n\n protected setup(): void {\n const requestSource = Interceptor.singleton(NodeHttpRequestSource)\n requestSource.apply(this)\n this.subscriptions.push(() => {\n requestSource.dispose(this)\n })\n\n this.subscriptions.push(\n forwardHttpEvents({\n source: requestSource,\n emitter: this.emitter,\n predicate: (initiator) => {\n return initiator instanceof XMLHttpRequest\n },\n })\n )\n\n logger.verbose('patching global \"XMLHttpRequest\"...')\n\n const prepareRequest = this.#transformRequest.bind(this)\n const requestLogger = this.logger\n\n this.subscriptions.push(\n patchesRegistry.applyPatch(\n globalThis,\n 'XMLHttpRequest',\n (realXMLHttpRequest) => {\n return new Proxy(realXMLHttpRequest, {\n construct(target, args, newTarget) {\n const xmlHttpRequest = Reflect.construct(target, args, newTarget)\n\n /**\n * @note Scope the request context to the `send()` call.\n * XHR in JSDOM is implemented via `http`/`https`, and the\n * underlying `http.ClientRequest` is created within `send()`,\n * so the initiator cascading keeps working. Binding the\n * context to the caller's scope instead (e.g. `enterWith`)\n * would attribute unrelated requests performed after this\n * XMLHttpRequest to it.\n */\n const realSend = xmlHttpRequest.send\n xmlHttpRequest.send = (\n ...sendArgs: Parameters<XMLHttpRequest['send']>\n ) => {\n return requestContext.run(\n {\n initiator: xmlHttpRequest,\n logger: requestLogger,\n transformRequest: (request) => {\n return prepareRequest(request, xmlHttpRequest)\n },\n },\n () => realSend.apply(xmlHttpRequest, sendArgs)\n )\n }\n\n return xmlHttpRequest\n },\n })\n }\n )\n )\n\n logger.verbose('global \"XMLHttpRequest\" patched!')\n }\n\n #transformRequest(request: Request, initiator: XMLHttpRequest): Request {\n const expectedCredentials = initiator.withCredentials\n ? 'include'\n : 'same-origin'\n\n if (request.credentials === expectedCredentials) {\n return request\n }\n\n return new FetchRequest(request.url, {\n ...request,\n method: request.method,\n headers: request.headers,\n credentials: expectedCredentials,\n body: request.body,\n signal: request.signal,\n })\n }\n}\n"],"mappings":";;;;;;AAUA,MAAM,SAAS,aAAa,KAAK;AAEjC,IAAa,4BAAb,cAA+C,YAAiC;;EAC9D,KAAA,SAAA,OAAO,IAAI,iBAAiB;;CAE5C,YAAsB;EACpB,OAAO,sBAAsB,gBAAgB;CAC/C;CAEA,QAAwB;EACtB,MAAM,gBAAgB,YAAY,UAAU,qBAAqB;EACjE,cAAc,MAAM,IAAI;EACxB,KAAK,cAAc,WAAW;GAC5B,cAAc,QAAQ,IAAI;EAC5B,CAAC;EAED,KAAK,cAAc,KACjB,kBAAkB;GAChB,QAAQ;GACR,SAAS,KAAK;GACd,YAAY,cAAc;IACxB,OAAO,qBAAqB;GAC9B;EACF,CAAC,CACH;EAEA,OAAO,QAAQ,uCAAqC;EAEpD,MAAM,iBAAiB,KAAKA,kBAAkB,KAAK,IAAI;EACvD,MAAM,gBAAgB,KAAK;EAE3B,KAAK,cAAc,KACjB,gBAAgB,WACd,YACA,mBACC,uBAAuB;GACtB,OAAO,IAAI,MAAM,oBAAoB,EACnC,UAAU,QAAQ,MAAM,WAAW;IACjC,MAAM,iBAAiB,QAAQ,UAAU,QAAQ,MAAM,SAAS;;;;;;;;;;IAWhE,MAAM,WAAW,eAAe;IAChC,eAAe,QACb,GAAG,aACA;KACH,OAAO,eAAe,IACpB;MACE,WAAW;MACX,QAAQ;MACR,mBAAmB,YAAY;OAC7B,OAAO,eAAe,SAAS,cAAc;MAC/C;KACF,SACM,SAAS,MAAM,gBAAgB,QAAQ,CAC/C;IACF;IAEA,OAAO;GACT,EACF,CAAC;EACH,CACF,CACF;EAEA,OAAO,QAAQ,oCAAkC;CACnD;CAEA,kBAAkB,SAAkB,WAAoC;EACtE,MAAM,sBAAsB,UAAU,kBAClC,YACA;EAEJ,IAAI,QAAQ,gBAAgB,qBAC1B,OAAO;EAGT,OAAO,IAAI,aAAa,QAAQ,KAAK;GACnC,GAAG;GACH,QAAQ,QAAQ;GAChB,SAAS,QAAQ;GACjB,aAAa;GACb,MAAM,QAAQ;GACd,QAAQ,QAAQ;EAClB,CAAC;CACH;AACF"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { t as Interceptor } from "../../interceptor-dtJZ_9QT.js";
|
|
2
|
+
import { n as HttpRequestEventMap } from "../../http-CB9rQ7i_.js";
|
|
3
|
+
//#region src/interceptors/fetch/node.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Interceptor for `fetch` requests in Node.js.
|
|
6
|
+
* @note This interceptor only affects requests performed via
|
|
7
|
+
* the global `fetch` function. To intercept fetch requests performed
|
|
8
|
+
* by other means (e.g. direct `request()` from Undici) use the
|
|
9
|
+
* `HttpRequestInterceptor` instead.
|
|
10
|
+
*/
|
|
11
|
+
declare class FetchInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
12
|
+
static symbol: symbol;
|
|
13
|
+
protected predicate(): boolean;
|
|
14
|
+
protected setup(): void;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { FetchInterceptor };
|
|
18
|
+
//# sourceMappingURL=node.d.ts.map
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { a as requestContext, i as forwardHttpEvents, t as NodeHttpRequestSource } from "../../source-lP0yyEtA.js";
|
|
2
|
+
import { i as Interceptor } from "../../buffer-utils-BvPY1Tc-.js";
|
|
3
|
+
import { i as getErrorResponse } from "../../fetch-utils-Dw1PsmtX.js";
|
|
4
|
+
import { o as patchesRegistry } from "../../net-DtMnyEeg.js";
|
|
5
|
+
import { t as hasConfigurableGlobal } from "../../has-configurable-global-CJCh_g1I.js";
|
|
6
|
+
//#region src/interceptors/fetch/node.ts
|
|
7
|
+
/**
|
|
8
|
+
* Interceptor for `fetch` requests in Node.js.
|
|
9
|
+
* @note This interceptor only affects requests performed via
|
|
10
|
+
* the global `fetch` function. To intercept fetch requests performed
|
|
11
|
+
* by other means (e.g. direct `request()` from Undici) use the
|
|
12
|
+
* `HttpRequestInterceptor` instead.
|
|
13
|
+
*/
|
|
14
|
+
var FetchInterceptor = class extends Interceptor {
|
|
15
|
+
static {
|
|
16
|
+
this.symbol = Symbol.for("fetch-interceptor");
|
|
17
|
+
}
|
|
18
|
+
predicate() {
|
|
19
|
+
return hasConfigurableGlobal("fetch");
|
|
20
|
+
}
|
|
21
|
+
setup() {
|
|
22
|
+
const requestSource = Interceptor.singleton(NodeHttpRequestSource);
|
|
23
|
+
requestSource.apply(this);
|
|
24
|
+
this.subscriptions.push(() => {
|
|
25
|
+
requestSource.dispose(this);
|
|
26
|
+
});
|
|
27
|
+
this.subscriptions.push(forwardHttpEvents({
|
|
28
|
+
source: requestSource,
|
|
29
|
+
emitter: this.emitter,
|
|
30
|
+
predicate: (initiator) => {
|
|
31
|
+
return initiator instanceof Request;
|
|
32
|
+
},
|
|
33
|
+
responsePredicate: (event) => {
|
|
34
|
+
/**
|
|
35
|
+
* @note Fetch clients never observe informational responses (1xx).
|
|
36
|
+
* Undici treats them as a network error, failing the request,
|
|
37
|
+
* so do not forward their "response" events to fetch consumers.
|
|
38
|
+
*/
|
|
39
|
+
return event.response.status >= 200;
|
|
40
|
+
}
|
|
41
|
+
}));
|
|
42
|
+
this.subscriptions.push(patchesRegistry.applyPatch(globalThis, "fetch", (realFetch) => {
|
|
43
|
+
return (input, init) => {
|
|
44
|
+
/**
|
|
45
|
+
* Resolve potentially relative request URL against the present `location`.
|
|
46
|
+
* This is mainly for native `fetch` in browser-like environments.
|
|
47
|
+
* @see https://github.com/mswjs/msw/issues/1625
|
|
48
|
+
*/
|
|
49
|
+
const resolvedInput = typeof input === "string" && typeof location !== "undefined" && !URL.canParse(input) ? new URL(input, location.href) : input;
|
|
50
|
+
const request = new Request(resolvedInput, init);
|
|
51
|
+
/**
|
|
52
|
+
* @note Scope the request context to the `fetch` call itself.
|
|
53
|
+
* Using `enterWith` here would rebind the store for the caller's
|
|
54
|
+
* entire asynchronous scope, attributing unrelated requests
|
|
55
|
+
* performed after this `fetch` to it.
|
|
56
|
+
*/
|
|
57
|
+
return requestContext.run({
|
|
58
|
+
initiator: request,
|
|
59
|
+
logger: this.logger
|
|
60
|
+
}, () => {
|
|
61
|
+
return realFetch(request).catch((error) => {
|
|
62
|
+
/**
|
|
63
|
+
* @note A mocked `Response.error()` destroys the socket with
|
|
64
|
+
* an internal error, which Undici reports as the cause of its
|
|
65
|
+
* "fetch failed" rejection. Surface the error response as the
|
|
66
|
+
* rejection cause instead, so the consumer can tell a mocked
|
|
67
|
+
* network error apart from an actual connectivity issue.
|
|
68
|
+
*/
|
|
69
|
+
if (error instanceof TypeError) {
|
|
70
|
+
const errorResponse = getErrorResponse(error.cause);
|
|
71
|
+
if (errorResponse) error.cause = errorResponse;
|
|
72
|
+
}
|
|
73
|
+
throw error;
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
}));
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
//#endregion
|
|
81
|
+
export { FetchInterceptor };
|
|
82
|
+
|
|
83
|
+
//# sourceMappingURL=node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.js","names":[],"sources":["../../../../src/interceptors/fetch/node.ts"],"sourcesContent":["import { hasConfigurableGlobal } from '#/src/utils/has-configurable-global'\nimport { getErrorResponse } from '#/src/utils/response-utils'\nimport { requestContext } from '#/src/request-context'\nimport { patchesRegistry } from '#/src/utils/patches-registry'\nimport { forwardHttpEvents } from '#/src/interceptors/http/forward-events'\nimport { NodeHttpRequestSource } from '#/src/interceptors/http/source'\nimport { HttpRequestEventMap } from '#/src/events/http'\nimport { Interceptor } from '../../interceptor'\n\n/**\n * Interceptor for `fetch` requests in Node.js.\n * @note This interceptor only affects requests performed via\n * the global `fetch` function. To intercept fetch requests performed\n * by other means (e.g. direct `request()` from Undici) use the\n * `HttpRequestInterceptor` instead.\n */\nexport class FetchInterceptor extends Interceptor<HttpRequestEventMap> {\n static symbol = Symbol.for('fetch-interceptor')\n\n protected predicate() {\n return hasConfigurableGlobal('fetch')\n }\n\n protected setup(): void {\n const requestSource = Interceptor.singleton(NodeHttpRequestSource)\n requestSource.apply(this)\n\n this.subscriptions.push(() => {\n requestSource.dispose(this)\n })\n\n this.subscriptions.push(\n forwardHttpEvents({\n source: requestSource,\n emitter: this.emitter,\n predicate: (initiator) => {\n return initiator instanceof Request\n },\n responsePredicate: (event) => {\n /**\n * @note Fetch clients never observe informational responses (1xx).\n * Undici treats them as a network error, failing the request,\n * so do not forward their \"response\" events to fetch consumers.\n */\n return event.response.status >= 200\n },\n })\n )\n\n this.subscriptions.push(\n patchesRegistry.applyPatch(globalThis, 'fetch', (realFetch) => {\n return (input, init) => {\n /**\n * Resolve potentially relative request URL against the present `location`.\n * This is mainly for native `fetch` in browser-like environments.\n * @see https://github.com/mswjs/msw/issues/1625\n */\n const resolvedInput =\n typeof input === 'string' &&\n typeof location !== 'undefined' &&\n !URL.canParse(input)\n ? new URL(input, location.href)\n : input\n\n const request = new Request(resolvedInput, init)\n\n /**\n * @note Scope the request context to the `fetch` call itself.\n * Using `enterWith` here would rebind the store for the caller's\n * entire asynchronous scope, attributing unrelated requests\n * performed after this `fetch` to it.\n */\n return requestContext.run(\n {\n initiator: request,\n logger: this.logger,\n },\n () => {\n return realFetch(request).catch((error: unknown) => {\n /**\n * @note A mocked `Response.error()` destroys the socket with\n * an internal error, which Undici reports as the cause of its\n * \"fetch failed\" rejection. Surface the error response as the\n * rejection cause instead, so the consumer can tell a mocked\n * network error apart from an actual connectivity issue.\n */\n if (error instanceof TypeError) {\n const errorResponse = getErrorResponse(error.cause)\n\n if (errorResponse) {\n error.cause = errorResponse\n }\n }\n\n throw error\n })\n }\n )\n }\n })\n )\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAgBA,IAAa,mBAAb,cAAsC,YAAiC;;EACrD,KAAA,SAAA,OAAO,IAAI,mBAAmB;;CAE9C,YAAsB;EACpB,OAAO,sBAAsB,OAAO;CACtC;CAEA,QAAwB;EACtB,MAAM,gBAAgB,YAAY,UAAU,qBAAqB;EACjE,cAAc,MAAM,IAAI;EAExB,KAAK,cAAc,WAAW;GAC5B,cAAc,QAAQ,IAAI;EAC5B,CAAC;EAED,KAAK,cAAc,KACjB,kBAAkB;GAChB,QAAQ;GACR,SAAS,KAAK;GACd,YAAY,cAAc;IACxB,OAAO,qBAAqB;GAC9B;GACA,oBAAoB,UAAU;;;;;;IAM5B,OAAO,MAAM,SAAS,UAAU;GAClC;EACF,CAAC,CACH;EAEA,KAAK,cAAc,KACjB,gBAAgB,WAAW,YAAY,UAAU,cAAc;GAC7D,QAAQ,OAAO,SAAS;;;;;;IAMtB,MAAM,gBACJ,OAAO,UAAU,YACjB,OAAO,aAAa,eACpB,CAAC,IAAI,SAAS,KAAK,IACf,IAAI,IAAI,OAAO,SAAS,IAAI,IAC5B;IAEN,MAAM,UAAU,IAAI,QAAQ,eAAe,IAAI;;;;;;;IAQ/C,OAAO,eAAe,IACpB;KACE,WAAW;KACX,QAAQ,KAAK;IACf,SACM;KACJ,OAAO,UAAU,OAAO,CAAC,CAAC,OAAO,UAAmB;;;;;;;;MAQlD,IAAI,iBAAiB,WAAW;OAC9B,MAAM,gBAAgB,iBAAiB,MAAM,KAAK;OAElD,IAAI,eACF,MAAM,QAAQ;MAElB;MAEA,MAAM;KACR,CAAC;IACH,CACF;GACF;EACF,CAAC,CACH;CACF;AACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { t as Interceptor } from "../../interceptor-dtJZ_9QT.js";
|
|
2
|
+
import { n as HttpRequestEventMap } from "../../http-CB9rQ7i_.js";
|
|
3
|
+
//#region src/interceptors/http/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Interceptor for all HTTP requests in Node.js.
|
|
6
|
+
*/
|
|
7
|
+
declare class HttpRequestInterceptor extends Interceptor<HttpRequestEventMap> {
|
|
8
|
+
static symbol: symbol;
|
|
9
|
+
protected predicate(): boolean;
|
|
10
|
+
protected setup(): void;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { HttpRequestInterceptor };
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { i as forwardHttpEvents, t as NodeHttpRequestSource } from "../../source-lP0yyEtA.js";
|
|
2
|
+
import { i as Interceptor } from "../../buffer-utils-BvPY1Tc-.js";
|
|
3
|
+
//#region src/interceptors/http/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* Interceptor for all HTTP requests in Node.js.
|
|
6
|
+
*/
|
|
7
|
+
var HttpRequestInterceptor = class extends Interceptor {
|
|
8
|
+
static {
|
|
9
|
+
this.symbol = Symbol.for("http-request-interceptor");
|
|
10
|
+
}
|
|
11
|
+
predicate() {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
setup() {
|
|
15
|
+
const requestSource = Interceptor.singleton(NodeHttpRequestSource);
|
|
16
|
+
requestSource.apply(this);
|
|
17
|
+
this.subscriptions.push(() => {
|
|
18
|
+
requestSource.dispose(this);
|
|
19
|
+
});
|
|
20
|
+
this.subscriptions.push(forwardHttpEvents({
|
|
21
|
+
source: requestSource,
|
|
22
|
+
emitter: this.emitter,
|
|
23
|
+
predicate: () => {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
//#endregion
|
|
30
|
+
export { HttpRequestInterceptor };
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../../src/interceptors/http/index.ts"],"sourcesContent":["import { Interceptor } from '#/src/interceptor'\nimport { type HttpRequestEventMap } from '#/src/events/http'\nimport { forwardHttpEvents } from './forward-events'\nimport { NodeHttpRequestSource } from './source'\n\n/**\n * Interceptor for all HTTP requests in Node.js.\n */\nexport class HttpRequestInterceptor extends Interceptor<HttpRequestEventMap> {\n static symbol = Symbol.for('http-request-interceptor')\n\n protected predicate(): boolean {\n return true\n }\n\n protected setup(): void {\n const requestSource = Interceptor.singleton(NodeHttpRequestSource)\n requestSource.apply(this)\n\n this.subscriptions.push(() => {\n requestSource.dispose(this)\n })\n\n this.subscriptions.push(\n forwardHttpEvents({\n source: requestSource,\n emitter: this.emitter,\n predicate: () => {\n return true\n },\n })\n )\n }\n}\n"],"mappings":";;;;;;AAQA,IAAa,yBAAb,cAA4C,YAAiC;;EAC3D,KAAA,SAAA,OAAO,IAAI,0BAA0B;;CAErD,YAA+B;EAC7B,OAAO;CACT;CAEA,QAAwB;EACtB,MAAM,gBAAgB,YAAY,UAAU,qBAAqB;EACjE,cAAc,MAAM,IAAI;EAExB,KAAK,cAAc,WAAW;GAC5B,cAAc,QAAQ,IAAI;EAC5B,CAAC;EAED,KAAK,cAAc,KACjB,kBAAkB;GAChB,QAAQ;GACR,SAAS,KAAK;GACd,iBAAiB;IACf,OAAO;GACT;EACF,CAAC,CACH;CACF;AACF"}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { t as Interceptor } from "../../interceptor-dtJZ_9QT.js";
|
|
2
|
+
import { TypedEvent } from "rettime";
|
|
3
|
+
import http from "node:http";
|
|
4
|
+
import net from "node:net";
|
|
5
|
+
import tls from "node:tls";
|
|
6
|
+
//#region src/interceptors/net/utils/normalize-net-connect-args.d.ts
|
|
7
|
+
interface NetworkConnectionOptions {
|
|
8
|
+
/**
|
|
9
|
+
* @note The port may be a string when a URL is (incorrectly) passed
|
|
10
|
+
* to `net.connect()`. Node.js reads URLs as plain options objects.
|
|
11
|
+
*/
|
|
12
|
+
port?: number | string | null;
|
|
13
|
+
path: string | null;
|
|
14
|
+
host?: string | null;
|
|
15
|
+
protocol?: string | null;
|
|
16
|
+
auth?: string | null;
|
|
17
|
+
family?: number | null;
|
|
18
|
+
hints?: number | null;
|
|
19
|
+
session?: Buffer;
|
|
20
|
+
localAddress?: string | null;
|
|
21
|
+
localPort?: number | null;
|
|
22
|
+
timeout?: number;
|
|
23
|
+
lookup?: net.LookupFunction;
|
|
24
|
+
allowHalfOpen?: boolean | null;
|
|
25
|
+
noDelay?: boolean | null;
|
|
26
|
+
keepAlive?: boolean | null;
|
|
27
|
+
keepAliveInitialDelay?: number | null;
|
|
28
|
+
autoSelectFamily?: boolean | null;
|
|
29
|
+
autoSelectFamilyAttemptTimeout?: number | null;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/interceptors/net/utils/normalize-tls-connect-args.d.ts
|
|
33
|
+
type TlsConnectionOptions = tls.ConnectionOptions & NetworkConnectionOptions;
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/interceptors/net/socket-controller.d.ts
|
|
36
|
+
declare const kRawSocket: unique symbol;
|
|
37
|
+
declare const kPatched: unique symbol;
|
|
38
|
+
type OperationStatus = 0 | 1;
|
|
39
|
+
declare module 'node:net' {
|
|
40
|
+
interface Socket {
|
|
41
|
+
[kPatched]?: boolean;
|
|
42
|
+
_httpMessage?: object | null;
|
|
43
|
+
_pendingData: string | Buffer | Array<{
|
|
44
|
+
chunk: string | Buffer;
|
|
45
|
+
encoding?: BufferEncoding;
|
|
46
|
+
callback?: (error?: Error | null) => void;
|
|
47
|
+
}> | null;
|
|
48
|
+
_pendingEncoding: BufferEncoding | '';
|
|
49
|
+
_bytesDispatched: number;
|
|
50
|
+
_writeGeneric(writev: boolean, data: NonNullable<net.Socket['_pendingData']>, encoding: BufferEncoding, callback?: (error?: Error | null) => void): void;
|
|
51
|
+
_handle: TcpHandle;
|
|
52
|
+
_start: () => void;
|
|
53
|
+
_unrefTimer: () => void;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
declare module 'node:tls' {
|
|
57
|
+
interface TLSSocket {
|
|
58
|
+
_handle: TcpHandle & {
|
|
59
|
+
start: () => void;
|
|
60
|
+
onhandshakedone: () => void;
|
|
61
|
+
onnewsession: (sessionId: unknown, session: Buffer) => void;
|
|
62
|
+
getSession: () => Buffer;
|
|
63
|
+
isSessionReused: () => boolean;
|
|
64
|
+
getServername: () => string;
|
|
65
|
+
getALPNNegotiatedProtocol: () => string | false;
|
|
66
|
+
getCipher: () => {
|
|
67
|
+
name: string;
|
|
68
|
+
standardName: string;
|
|
69
|
+
version: string;
|
|
70
|
+
};
|
|
71
|
+
getEphemeralKeyInfo: () => tls.EphemeralKeyInfo;
|
|
72
|
+
verifyError: () => void;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
interface TcpHandle {
|
|
77
|
+
open: (fd: unknown) => OperationStatus;
|
|
78
|
+
connect: (request: TcpWrap, address: string, port: number) => void;
|
|
79
|
+
connect6: (request: TcpWrap, address: string, port: number) => void;
|
|
80
|
+
listen: (backlog: number) => OperationStatus;
|
|
81
|
+
onconnection?: () => void;
|
|
82
|
+
getpeername?: (addressInfo: ReturnType<net.Socket['address']>) => OperationStatus;
|
|
83
|
+
getsockname?: (addressInfo: ReturnType<net.Socket['address']>) => OperationStatus;
|
|
84
|
+
reading: boolean;
|
|
85
|
+
onread: () => void;
|
|
86
|
+
readStart: () => void;
|
|
87
|
+
readStop: () => void;
|
|
88
|
+
bytesRead: number;
|
|
89
|
+
bytesWritten: number;
|
|
90
|
+
ref?: () => void;
|
|
91
|
+
unref?: () => void;
|
|
92
|
+
hasRef?: () => boolean;
|
|
93
|
+
fchmod: (mode: number) => void;
|
|
94
|
+
setBlocking: (blocking: boolean) => OperationStatus;
|
|
95
|
+
setNoDelay?: (noDelay: boolean) => void;
|
|
96
|
+
setKeepAlive?: (keepAlive: boolean, initialDelay: number) => void;
|
|
97
|
+
setTypeOfService?: (tos: number) => OperationStatus;
|
|
98
|
+
shutdown: (reqest: unknown) => OperationStatus;
|
|
99
|
+
close: () => void;
|
|
100
|
+
_parent?: TcpHandle;
|
|
101
|
+
}
|
|
102
|
+
interface TcpWrap {
|
|
103
|
+
oncomplete: (status: OperationStatus, owner: TcpHandle, request: TcpWrap, readable?: boolean, writable?: boolean) => void;
|
|
104
|
+
}
|
|
105
|
+
declare abstract class SocketController {
|
|
106
|
+
static PENDING: 0;
|
|
107
|
+
static CLAIMED: 1;
|
|
108
|
+
static PASSTHROUGH: 2;
|
|
109
|
+
readyState: typeof SocketController.PENDING | typeof SocketController.CLAIMED | typeof SocketController.PASSTHROUGH;
|
|
110
|
+
private [kRawSocket];
|
|
111
|
+
constructor(socket: net.Socket);
|
|
112
|
+
/**
|
|
113
|
+
* Claim this socket. Once claimed, the connection attempt succeeds
|
|
114
|
+
* regardless of the requested host and the interceptor becomes the
|
|
115
|
+
* mocked server for this connection.
|
|
116
|
+
*/
|
|
117
|
+
claim(): void;
|
|
118
|
+
/**
|
|
119
|
+
* Establish this socket connection as-is.
|
|
120
|
+
*/
|
|
121
|
+
passthrough(): void;
|
|
122
|
+
}
|
|
123
|
+
type FlushPendingDataFunction = (data: NonNullable<net.Socket['_pendingData']>, encoding: BufferEncoding | undefined, callback: (data: NonNullable<net.Socket['_pendingData']>) => void) => void;
|
|
124
|
+
declare class TcpSocketController extends SocketController {
|
|
125
|
+
#private;
|
|
126
|
+
protected readonly socket: net.Socket;
|
|
127
|
+
protected readonly createConnection: () => net.Socket;
|
|
128
|
+
serverSocket: net.Socket;
|
|
129
|
+
protected pendingConnection: PromiseWithResolvers<[TcpWrap, TcpHandle]>;
|
|
130
|
+
constructor(socket: net.Socket, createConnection: () => net.Socket, connectionOptions?: NetworkConnectionOptions);
|
|
131
|
+
/**
|
|
132
|
+
* Reset this controller to the pending state so the next exchange
|
|
133
|
+
* on this socket can be handled anew. This is meant for kept-alive
|
|
134
|
+
* sockets that are reused for multiple exchanges by clients that
|
|
135
|
+
* don't emit the "free" event on the socket (e.g. Undici).
|
|
136
|
+
*/
|
|
137
|
+
reset(): void;
|
|
138
|
+
protected emulateConnect(): void;
|
|
139
|
+
/**
|
|
140
|
+
* Suspend forwarding of the passthrough socket events ("data", "end", "close")
|
|
141
|
+
* to the client socket. The events are buffered in order until `uncorkReads()`
|
|
142
|
+
* is called. This allows the consumer to delay the delivery of the original
|
|
143
|
+
* response to the client (e.g. until its own asynchronous logic settles).
|
|
144
|
+
*
|
|
145
|
+
* @note Pausing the client socket is not enough to delay the delivery.
|
|
146
|
+
* Consumers like Undici read the pushed data from a paused socket
|
|
147
|
+
* directly via `socket.read()`, which is unaffected by `socket.pause()`.
|
|
148
|
+
*/
|
|
149
|
+
corkReads(): void;
|
|
150
|
+
/**
|
|
151
|
+
* Resume forwarding of the passthrough socket events to the client socket,
|
|
152
|
+
* replaying any events buffered while the reads were corked.
|
|
153
|
+
*/
|
|
154
|
+
uncorkReads(): void;
|
|
155
|
+
claim(): void;
|
|
156
|
+
passthrough(flushPendingData?: FlushPendingDataFunction): net.Socket;
|
|
157
|
+
}
|
|
158
|
+
declare class TlsSocketController extends TcpSocketController {
|
|
159
|
+
#private;
|
|
160
|
+
protected readonly socket: tls.TLSSocket;
|
|
161
|
+
protected readonly createConnection: () => tls.TLSSocket;
|
|
162
|
+
constructor(socket: tls.TLSSocket, createConnection: () => tls.TLSSocket, tlsConnectionOptions?: TlsConnectionOptions);
|
|
163
|
+
protected emulateConnect(): void;
|
|
164
|
+
claim(): void;
|
|
165
|
+
passthrough(flushPendingData?: FlushPendingDataFunction): tls.TLSSocket;
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/interceptors/net/index.d.ts
|
|
169
|
+
declare module 'node:http' {
|
|
170
|
+
interface Agent {
|
|
171
|
+
/**
|
|
172
|
+
* @note An undocumented method backing every agent-driven request
|
|
173
|
+
* (see "#stopReusingUnpatchedSockets").
|
|
174
|
+
*/
|
|
175
|
+
addRequest?: (request: http.ClientRequest, ...args: Array<unknown>) => void;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
interface SocketConnectionEventData {
|
|
179
|
+
socket: net.Socket | tls.TLSSocket;
|
|
180
|
+
connectionOptions: NetworkConnectionOptions;
|
|
181
|
+
controller: TcpSocketController | TlsSocketController;
|
|
182
|
+
}
|
|
183
|
+
declare class SocketConnectionEvent<DataType extends SocketConnectionEventData = SocketConnectionEventData> extends TypedEvent<DataType, void, 'connection'> {
|
|
184
|
+
socket: net.Socket | tls.TLSSocket;
|
|
185
|
+
connectionOptions: NetworkConnectionOptions;
|
|
186
|
+
controller: TcpSocketController | TlsSocketController;
|
|
187
|
+
constructor(data: DataType);
|
|
188
|
+
}
|
|
189
|
+
type SocketEventMap = {
|
|
190
|
+
connection: SocketConnectionEvent;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* Interceptor for `net.Socket` connections.
|
|
194
|
+
*/
|
|
195
|
+
declare class SocketInterceptor extends Interceptor<SocketEventMap> {
|
|
196
|
+
#private;
|
|
197
|
+
static symbol: symbol;
|
|
198
|
+
protected predicate(): boolean;
|
|
199
|
+
protected setup(): void;
|
|
200
|
+
}
|
|
201
|
+
//#endregion
|
|
202
|
+
export { SocketInterceptor };
|
|
203
|
+
//# sourceMappingURL=index.d.ts.map
|