@mswjs/interceptors 0.41.9 → 0.42.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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-Bk5YX1AM.js +720 -0
- package/lib/browser/create-request-id-Bk5YX1AM.js.map +1 -0
- package/lib/browser/{getRawRequest-B1BqgWG6.mjs → fetch-utils-CUOrwEQf.js} +123 -52
- package/lib/browser/fetch-utils-CUOrwEQf.js.map +1 -0
- package/lib/browser/handle-request-CqVvBhcw.js +167 -0
- package/lib/browser/handle-request-CqVvBhcw.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/{XMLHttpRequest-DrZAf3w6.mjs → web-BcKSQghf.js} +327 -255
- package/lib/browser/web-BcKSQghf.js.map +1 -0
- package/lib/browser/web-CdcYFjgm.js +248 -0
- package/lib/browser/web-CdcYFjgm.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,1990 @@
|
|
|
1
|
+
import { a as createLogger, i as Interceptor, o as formatRequest, r as toBuffer } from "./buffer-utils-BvPY1Tc-.js";
|
|
2
|
+
import { a as isResponseError, c as isObject, d as createRequestId, f as RequestController, l as getRawFetchHeaders, n as FetchResponse, o as isResponseLike, p as InterceptorError, r as createServerErrorResponse, s as kErrorResponse, t as FetchRequest, u as recordRawFetchHeaders } from "./fetch-utils-Dw1PsmtX.js";
|
|
3
|
+
import { i as unwrapPendingData, n as SocketController, r as kRawSocket, t as SocketInterceptor } from "./net-DtMnyEeg.js";
|
|
4
|
+
import { TypedEvent } from "rettime";
|
|
5
|
+
import { invariant } from "outvariant";
|
|
6
|
+
import { IncomingMessage, METHODS, STATUS_CODES, ServerResponse } from "node:http";
|
|
7
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
8
|
+
import net from "node:net";
|
|
9
|
+
import tls from "node:tls";
|
|
10
|
+
import { Readable } from "node:stream";
|
|
11
|
+
import fs from "node:fs";
|
|
12
|
+
import { until } from "@open-draft/until";
|
|
13
|
+
//#region \0rolldown/runtime.js
|
|
14
|
+
var __create = Object.create;
|
|
15
|
+
var __defProp = Object.defineProperty;
|
|
16
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
17
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
18
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
19
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
20
|
+
var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
23
|
+
key = keys[i];
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
25
|
+
get: ((k) => from[k]).bind(null, key),
|
|
26
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return to;
|
|
30
|
+
};
|
|
31
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
32
|
+
value: mod,
|
|
33
|
+
enumerable: true
|
|
34
|
+
}) : target, mod));
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/request-context.ts
|
|
37
|
+
const requestContext = new AsyncLocalStorage();
|
|
38
|
+
function runInRequestContext(callback, logger) {
|
|
39
|
+
/**
|
|
40
|
+
* @note Never shadow an existing request context. Nested calls
|
|
41
|
+
* (e.g. a patched entry point re-entered synchronously, or a request
|
|
42
|
+
* made within the fetch/XMLHttpRequest interceptor context) must run
|
|
43
|
+
* within the parent context so the sockets they create capture it.
|
|
44
|
+
*/
|
|
45
|
+
if (requestContext.getStore()) return callback();
|
|
46
|
+
/**
|
|
47
|
+
* @note The initiator is the callback's return value (e.g. the
|
|
48
|
+
* "ClientRequest" instance), so it cannot be known before running
|
|
49
|
+
* the callback. The context is mutated in place once the callback
|
|
50
|
+
* returns; readers hold the context object by reference and sample
|
|
51
|
+
* "initiator" only after the request has been written.
|
|
52
|
+
*/
|
|
53
|
+
const context = {
|
|
54
|
+
initiator: void 0,
|
|
55
|
+
logger
|
|
56
|
+
};
|
|
57
|
+
return requestContext.run(context, () => {
|
|
58
|
+
const initiator = callback();
|
|
59
|
+
context.initiator = initiator;
|
|
60
|
+
return initiator;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/interceptors/http/forward-events.ts
|
|
65
|
+
const logger = createLogger("http-request");
|
|
66
|
+
function forwardHttpEvents(options) {
|
|
67
|
+
const controller = new AbortController();
|
|
68
|
+
const { source, emitter, predicate, responsePredicate } = options;
|
|
69
|
+
source.on("request", async (event) => {
|
|
70
|
+
if (predicate(event.initiator)) {
|
|
71
|
+
logger.verbose("forwarding \"request\" event %o", { requestId: event.requestId });
|
|
72
|
+
await emitter.emitAsPromise(event);
|
|
73
|
+
}
|
|
74
|
+
}, { signal: controller.signal });
|
|
75
|
+
const responseListener = async (event) => {
|
|
76
|
+
if (predicate(event.initiator) && (responsePredicate == null || responsePredicate(event))) {
|
|
77
|
+
logger.verbose("forwarding \"response\" event %o", {
|
|
78
|
+
requestId: event.requestId,
|
|
79
|
+
responseType: event.responseType
|
|
80
|
+
});
|
|
81
|
+
await emitter.emitAsPromise(event);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const unhandledExceptionListener = async (event) => {
|
|
85
|
+
if (predicate(event.initiator)) {
|
|
86
|
+
logger.verbose("forwarding \"unhandledException\" event %o", { requestId: event.requestId });
|
|
87
|
+
await emitter.emitAsPromise(event);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const addResponseListener = () => {
|
|
91
|
+
if (!source.listeners("response").includes(responseListener)) source.on("response", responseListener, { signal: controller.signal });
|
|
92
|
+
};
|
|
93
|
+
const addUnhandledExceptionListener = () => {
|
|
94
|
+
if (!source.listeners("unhandledException").includes(unhandledExceptionListener)) source.on("unhandledException", unhandledExceptionListener, { signal: controller.signal });
|
|
95
|
+
};
|
|
96
|
+
if (emitter.listenerCount("response") > 0) addResponseListener();
|
|
97
|
+
if (emitter.listenerCount("unhandledException") > 0) addUnhandledExceptionListener();
|
|
98
|
+
emitter.hooks.on("newListener", (type) => {
|
|
99
|
+
if (type === "response") addResponseListener();
|
|
100
|
+
if (type === "unhandledException") addUnhandledExceptionListener();
|
|
101
|
+
}, {
|
|
102
|
+
signal: controller.signal,
|
|
103
|
+
persist: true
|
|
104
|
+
});
|
|
105
|
+
emitter.hooks.on("removeListener", (type) => {
|
|
106
|
+
if (type === "response" && emitter.listenerCount("response") === 0) source.removeListener("response", responseListener);
|
|
107
|
+
if (type === "unhandledException" && emitter.listenerCount("unhandledException") === 0) source.removeListener("unhandledException", unhandledExceptionListener);
|
|
108
|
+
}, {
|
|
109
|
+
signal: controller.signal,
|
|
110
|
+
persist: true
|
|
111
|
+
});
|
|
112
|
+
return () => {
|
|
113
|
+
controller.abort();
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/events/http.ts
|
|
118
|
+
var HttpRequestEvent = class extends TypedEvent {
|
|
119
|
+
constructor(data) {
|
|
120
|
+
super(...["request", {}]);
|
|
121
|
+
this.request = data.request;
|
|
122
|
+
this.requestId = data.requestId;
|
|
123
|
+
this.initiator = data.initiator;
|
|
124
|
+
this.controller = data.controller;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
var HttpResponseEvent = class extends TypedEvent {
|
|
128
|
+
constructor(data) {
|
|
129
|
+
super(...["response", {}]);
|
|
130
|
+
this.response = data.response;
|
|
131
|
+
this.responseType = data.responseType;
|
|
132
|
+
this.request = data.request;
|
|
133
|
+
this.requestId = data.requestId;
|
|
134
|
+
this.initiator = data.initiator;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
var UnhandledHttpException = class extends TypedEvent {
|
|
138
|
+
constructor(data) {
|
|
139
|
+
super(...["unhandledException", {}]);
|
|
140
|
+
this.error = data.error;
|
|
141
|
+
this.request = data.request;
|
|
142
|
+
this.requestId = data.requestId;
|
|
143
|
+
this.initiator = data.initiator;
|
|
144
|
+
this.controller = data.controller;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/interceptors/net/utils/connection-options-to-url.ts
|
|
149
|
+
/**
|
|
150
|
+
* Creates a `URL` instance out of the `net.connect()` options.
|
|
151
|
+
* @note This implies that the passed connection is an HTTP connection.
|
|
152
|
+
*/
|
|
153
|
+
function connectionOptionsToUrl(options, socket) {
|
|
154
|
+
const isIPv6 = net.isIPv6(options.host || "");
|
|
155
|
+
const protocol = socket instanceof tls.TLSSocket ? "https:" : getProtocolByConnectionOptions(options);
|
|
156
|
+
const host = options.host || "localhost";
|
|
157
|
+
const url = new URL(`${protocol}//${isIPv6 ? `[${host}]` : host}`);
|
|
158
|
+
if (options.path) url.pathname = options.path;
|
|
159
|
+
if (options.port) url.port = options.port.toString();
|
|
160
|
+
if (options.auth) {
|
|
161
|
+
const [username, password] = options.auth.split(":");
|
|
162
|
+
/**
|
|
163
|
+
* Authentication options are provided as plain values.
|
|
164
|
+
* Encode them to form a valid URL.
|
|
165
|
+
* @see https://github.com/nodejs/node/blob/f3adc11e37b8bfaaa026ea85c1cf22e3a0e29ae9/lib/internal/url.js#L1452
|
|
166
|
+
*/
|
|
167
|
+
url.username = encodeURIComponent(username);
|
|
168
|
+
url.password = encodeURIComponent(password);
|
|
169
|
+
}
|
|
170
|
+
return url;
|
|
171
|
+
}
|
|
172
|
+
function getProtocolByConnectionOptions(options) {
|
|
173
|
+
if (options.protocol) return options.protocol;
|
|
174
|
+
if (options.port === 443) return "https:";
|
|
175
|
+
return "http:";
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/interceptors/http/http-parser/index.ts
|
|
179
|
+
var import_constants = /* @__PURE__ */ __toESM((/* @__PURE__ */ __commonJSMin(((exports) => {
|
|
180
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
181
|
+
exports.SPECIAL_HEADERS = exports.MINOR = exports.MAJOR = exports.QDTEXT = exports.CONNECTION_TOKEN_CHARS = exports.RELAXED_HEADER_CHARS = exports.HEADER_CHARS = exports.HTAB_SP_VCHAR_OBS_TEXT = exports.SP = exports.HTAB = exports.TOKEN = exports.HEX = exports.URL_CHAR = exports.USERINFO_CHARS = exports.MARK = exports.ALPHANUM = exports.DIGIT = exports.HEX_MAP = exports.NUM_MAP = exports.ALPHA = exports.METHODS = exports.METHODS_HTTP = exports.METHODS_HTTP2 = exports.METHODS_HTTP1 = exports.METHODS_RTSP = exports.METHODS_RAOP = exports.METHODS_AIRPLAY = exports.METHODS_ICECAST = exports.METHODS_NON_STANDARD = exports.METHODS_CALDAV = exports.METHODS_UPNP = exports.METHODS_SUBVERSION = exports.METHODS_WEBDAV = exports.METHODS_BASIC_HTTP = exports.METHODS_HTTP1_HEAD = exports.HEADER_STATE = exports.FINISH = exports.STATUSES = exports.LENIENT_FLAGS = exports.FLAGS = exports.TYPE = exports.ERROR = void 0;
|
|
182
|
+
exports.ERROR = {
|
|
183
|
+
OK: 0,
|
|
184
|
+
INTERNAL: 1,
|
|
185
|
+
STRICT: 2,
|
|
186
|
+
CR_EXPECTED: 25,
|
|
187
|
+
LF_EXPECTED: 3,
|
|
188
|
+
UNEXPECTED_CONTENT_LENGTH: 4,
|
|
189
|
+
UNEXPECTED_SPACE: 30,
|
|
190
|
+
CLOSED_CONNECTION: 5,
|
|
191
|
+
INVALID_METHOD: 6,
|
|
192
|
+
INVALID_URL: 7,
|
|
193
|
+
INVALID_CONSTANT: 8,
|
|
194
|
+
INVALID_VERSION: 9,
|
|
195
|
+
INVALID_HEADER_TOKEN: 10,
|
|
196
|
+
INVALID_CONTENT_LENGTH: 11,
|
|
197
|
+
INVALID_CHUNK_SIZE: 12,
|
|
198
|
+
INVALID_STATUS: 13,
|
|
199
|
+
INVALID_EOF_STATE: 14,
|
|
200
|
+
INVALID_TRANSFER_ENCODING: 15,
|
|
201
|
+
CB_MESSAGE_BEGIN: 16,
|
|
202
|
+
CB_HEADERS_COMPLETE: 17,
|
|
203
|
+
CB_MESSAGE_COMPLETE: 18,
|
|
204
|
+
CB_CHUNK_HEADER: 19,
|
|
205
|
+
CB_CHUNK_COMPLETE: 20,
|
|
206
|
+
PAUSED: 21,
|
|
207
|
+
PAUSED_UPGRADE: 22,
|
|
208
|
+
PAUSED_H2_UPGRADE: 23,
|
|
209
|
+
USER: 24,
|
|
210
|
+
CB_URL_COMPLETE: 26,
|
|
211
|
+
CB_STATUS_COMPLETE: 27,
|
|
212
|
+
CB_METHOD_COMPLETE: 32,
|
|
213
|
+
CB_VERSION_COMPLETE: 33,
|
|
214
|
+
CB_HEADER_FIELD_COMPLETE: 28,
|
|
215
|
+
CB_HEADER_VALUE_COMPLETE: 29,
|
|
216
|
+
CB_CHUNK_EXTENSION_NAME_COMPLETE: 34,
|
|
217
|
+
CB_CHUNK_EXTENSION_VALUE_COMPLETE: 35,
|
|
218
|
+
CB_RESET: 31,
|
|
219
|
+
CB_PROTOCOL_COMPLETE: 38
|
|
220
|
+
};
|
|
221
|
+
exports.TYPE = {
|
|
222
|
+
BOTH: 0,
|
|
223
|
+
REQUEST: 1,
|
|
224
|
+
RESPONSE: 2
|
|
225
|
+
};
|
|
226
|
+
exports.FLAGS = {
|
|
227
|
+
CONNECTION_KEEP_ALIVE: 1,
|
|
228
|
+
CONNECTION_CLOSE: 2,
|
|
229
|
+
CONNECTION_UPGRADE: 4,
|
|
230
|
+
CHUNKED: 8,
|
|
231
|
+
UPGRADE: 16,
|
|
232
|
+
CONTENT_LENGTH: 32,
|
|
233
|
+
SKIPBODY: 64,
|
|
234
|
+
TRAILING: 128,
|
|
235
|
+
TRANSFER_ENCODING: 512
|
|
236
|
+
};
|
|
237
|
+
exports.LENIENT_FLAGS = {
|
|
238
|
+
HEADERS: 1,
|
|
239
|
+
CHUNKED_LENGTH: 2,
|
|
240
|
+
KEEP_ALIVE: 4,
|
|
241
|
+
TRANSFER_ENCODING: 8,
|
|
242
|
+
VERSION: 16,
|
|
243
|
+
DATA_AFTER_CLOSE: 32,
|
|
244
|
+
OPTIONAL_LF_AFTER_CR: 64,
|
|
245
|
+
OPTIONAL_CRLF_AFTER_CHUNK: 128,
|
|
246
|
+
OPTIONAL_CR_BEFORE_LF: 256,
|
|
247
|
+
SPACES_AFTER_CHUNK_SIZE: 512,
|
|
248
|
+
HEADER_VALUE_RELAXED: 1024
|
|
249
|
+
};
|
|
250
|
+
exports.STATUSES = {
|
|
251
|
+
CONTINUE: 100,
|
|
252
|
+
SWITCHING_PROTOCOLS: 101,
|
|
253
|
+
PROCESSING: 102,
|
|
254
|
+
EARLY_HINTS: 103,
|
|
255
|
+
RESPONSE_IS_STALE: 110,
|
|
256
|
+
REVALIDATION_FAILED: 111,
|
|
257
|
+
DISCONNECTED_OPERATION: 112,
|
|
258
|
+
HEURISTIC_EXPIRATION: 113,
|
|
259
|
+
MISCELLANEOUS_WARNING: 199,
|
|
260
|
+
OK: 200,
|
|
261
|
+
CREATED: 201,
|
|
262
|
+
ACCEPTED: 202,
|
|
263
|
+
NON_AUTHORITATIVE_INFORMATION: 203,
|
|
264
|
+
NO_CONTENT: 204,
|
|
265
|
+
RESET_CONTENT: 205,
|
|
266
|
+
PARTIAL_CONTENT: 206,
|
|
267
|
+
MULTI_STATUS: 207,
|
|
268
|
+
ALREADY_REPORTED: 208,
|
|
269
|
+
TRANSFORMATION_APPLIED: 214,
|
|
270
|
+
IM_USED: 226,
|
|
271
|
+
MISCELLANEOUS_PERSISTENT_WARNING: 299,
|
|
272
|
+
MULTIPLE_CHOICES: 300,
|
|
273
|
+
MOVED_PERMANENTLY: 301,
|
|
274
|
+
FOUND: 302,
|
|
275
|
+
SEE_OTHER: 303,
|
|
276
|
+
NOT_MODIFIED: 304,
|
|
277
|
+
USE_PROXY: 305,
|
|
278
|
+
SWITCH_PROXY: 306,
|
|
279
|
+
TEMPORARY_REDIRECT: 307,
|
|
280
|
+
PERMANENT_REDIRECT: 308,
|
|
281
|
+
BAD_REQUEST: 400,
|
|
282
|
+
UNAUTHORIZED: 401,
|
|
283
|
+
PAYMENT_REQUIRED: 402,
|
|
284
|
+
FORBIDDEN: 403,
|
|
285
|
+
NOT_FOUND: 404,
|
|
286
|
+
METHOD_NOT_ALLOWED: 405,
|
|
287
|
+
NOT_ACCEPTABLE: 406,
|
|
288
|
+
PROXY_AUTHENTICATION_REQUIRED: 407,
|
|
289
|
+
REQUEST_TIMEOUT: 408,
|
|
290
|
+
CONFLICT: 409,
|
|
291
|
+
GONE: 410,
|
|
292
|
+
LENGTH_REQUIRED: 411,
|
|
293
|
+
PRECONDITION_FAILED: 412,
|
|
294
|
+
PAYLOAD_TOO_LARGE: 413,
|
|
295
|
+
URI_TOO_LONG: 414,
|
|
296
|
+
UNSUPPORTED_MEDIA_TYPE: 415,
|
|
297
|
+
RANGE_NOT_SATISFIABLE: 416,
|
|
298
|
+
EXPECTATION_FAILED: 417,
|
|
299
|
+
IM_A_TEAPOT: 418,
|
|
300
|
+
PAGE_EXPIRED: 419,
|
|
301
|
+
ENHANCE_YOUR_CALM: 420,
|
|
302
|
+
MISDIRECTED_REQUEST: 421,
|
|
303
|
+
UNPROCESSABLE_ENTITY: 422,
|
|
304
|
+
LOCKED: 423,
|
|
305
|
+
FAILED_DEPENDENCY: 424,
|
|
306
|
+
TOO_EARLY: 425,
|
|
307
|
+
UPGRADE_REQUIRED: 426,
|
|
308
|
+
PRECONDITION_REQUIRED: 428,
|
|
309
|
+
TOO_MANY_REQUESTS: 429,
|
|
310
|
+
REQUEST_HEADER_FIELDS_TOO_LARGE_UNOFFICIAL: 430,
|
|
311
|
+
REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
|
|
312
|
+
LOGIN_TIMEOUT: 440,
|
|
313
|
+
NO_RESPONSE: 444,
|
|
314
|
+
RETRY_WITH: 449,
|
|
315
|
+
BLOCKED_BY_PARENTAL_CONTROL: 450,
|
|
316
|
+
UNAVAILABLE_FOR_LEGAL_REASONS: 451,
|
|
317
|
+
CLIENT_CLOSED_LOAD_BALANCED_REQUEST: 460,
|
|
318
|
+
INVALID_X_FORWARDED_FOR: 463,
|
|
319
|
+
REQUEST_HEADER_TOO_LARGE: 494,
|
|
320
|
+
SSL_CERTIFICATE_ERROR: 495,
|
|
321
|
+
SSL_CERTIFICATE_REQUIRED: 496,
|
|
322
|
+
HTTP_REQUEST_SENT_TO_HTTPS_PORT: 497,
|
|
323
|
+
INVALID_TOKEN: 498,
|
|
324
|
+
CLIENT_CLOSED_REQUEST: 499,
|
|
325
|
+
INTERNAL_SERVER_ERROR: 500,
|
|
326
|
+
NOT_IMPLEMENTED: 501,
|
|
327
|
+
BAD_GATEWAY: 502,
|
|
328
|
+
SERVICE_UNAVAILABLE: 503,
|
|
329
|
+
GATEWAY_TIMEOUT: 504,
|
|
330
|
+
HTTP_VERSION_NOT_SUPPORTED: 505,
|
|
331
|
+
VARIANT_ALSO_NEGOTIATES: 506,
|
|
332
|
+
INSUFFICIENT_STORAGE: 507,
|
|
333
|
+
LOOP_DETECTED: 508,
|
|
334
|
+
BANDWIDTH_LIMIT_EXCEEDED: 509,
|
|
335
|
+
NOT_EXTENDED: 510,
|
|
336
|
+
NETWORK_AUTHENTICATION_REQUIRED: 511,
|
|
337
|
+
WEB_SERVER_UNKNOWN_ERROR: 520,
|
|
338
|
+
WEB_SERVER_IS_DOWN: 521,
|
|
339
|
+
CONNECTION_TIMEOUT: 522,
|
|
340
|
+
ORIGIN_IS_UNREACHABLE: 523,
|
|
341
|
+
TIMEOUT_OCCURED: 524,
|
|
342
|
+
SSL_HANDSHAKE_FAILED: 525,
|
|
343
|
+
INVALID_SSL_CERTIFICATE: 526,
|
|
344
|
+
RAILGUN_ERROR: 527,
|
|
345
|
+
SITE_IS_OVERLOADED: 529,
|
|
346
|
+
SITE_IS_FROZEN: 530,
|
|
347
|
+
IDENTITY_PROVIDER_AUTHENTICATION_ERROR: 561,
|
|
348
|
+
NETWORK_READ_TIMEOUT: 598,
|
|
349
|
+
NETWORK_CONNECT_TIMEOUT: 599
|
|
350
|
+
};
|
|
351
|
+
exports.FINISH = {
|
|
352
|
+
SAFE: 0,
|
|
353
|
+
SAFE_WITH_CB: 1,
|
|
354
|
+
UNSAFE: 2
|
|
355
|
+
};
|
|
356
|
+
exports.HEADER_STATE = {
|
|
357
|
+
GENERAL: 0,
|
|
358
|
+
CONNECTION: 1,
|
|
359
|
+
CONTENT_LENGTH: 2,
|
|
360
|
+
TRANSFER_ENCODING: 3,
|
|
361
|
+
UPGRADE: 4,
|
|
362
|
+
CONNECTION_KEEP_ALIVE: 5,
|
|
363
|
+
CONNECTION_CLOSE: 6,
|
|
364
|
+
CONNECTION_UPGRADE: 7,
|
|
365
|
+
TRANSFER_ENCODING_CHUNKED: 8
|
|
366
|
+
};
|
|
367
|
+
exports.METHODS_HTTP1_HEAD = { HEAD: 2 };
|
|
368
|
+
/**
|
|
369
|
+
* HTTP methods as defined by RFC-9110 and other specifications.
|
|
370
|
+
* @see https://httpwg.org/specs/rfc9110.html#method.definitions
|
|
371
|
+
*/
|
|
372
|
+
exports.METHODS_BASIC_HTTP = {
|
|
373
|
+
DELETE: 0,
|
|
374
|
+
GET: 1,
|
|
375
|
+
...exports.METHODS_HTTP1_HEAD,
|
|
376
|
+
POST: 3,
|
|
377
|
+
PUT: 4,
|
|
378
|
+
CONNECT: 5,
|
|
379
|
+
OPTIONS: 6,
|
|
380
|
+
TRACE: 7,
|
|
381
|
+
/**
|
|
382
|
+
* @see https://www.rfc-editor.org/rfc/rfc5789.html
|
|
383
|
+
*/
|
|
384
|
+
PATCH: 28,
|
|
385
|
+
LINK: 31,
|
|
386
|
+
UNLINK: 32
|
|
387
|
+
};
|
|
388
|
+
exports.METHODS_WEBDAV = {
|
|
389
|
+
COPY: 8,
|
|
390
|
+
LOCK: 9,
|
|
391
|
+
MKCOL: 10,
|
|
392
|
+
MOVE: 11,
|
|
393
|
+
PROPFIND: 12,
|
|
394
|
+
PROPPATCH: 13,
|
|
395
|
+
SEARCH: 14,
|
|
396
|
+
UNLOCK: 15,
|
|
397
|
+
BIND: 16,
|
|
398
|
+
REBIND: 17,
|
|
399
|
+
UNBIND: 18,
|
|
400
|
+
ACL: 19
|
|
401
|
+
};
|
|
402
|
+
exports.METHODS_SUBVERSION = {
|
|
403
|
+
REPORT: 20,
|
|
404
|
+
MKACTIVITY: 21,
|
|
405
|
+
CHECKOUT: 22,
|
|
406
|
+
MERGE: 23
|
|
407
|
+
};
|
|
408
|
+
exports.METHODS_UPNP = {
|
|
409
|
+
"M-SEARCH": 24,
|
|
410
|
+
NOTIFY: 25,
|
|
411
|
+
SUBSCRIBE: 26,
|
|
412
|
+
UNSUBSCRIBE: 27
|
|
413
|
+
};
|
|
414
|
+
exports.METHODS_CALDAV = { MKCALENDAR: 30 };
|
|
415
|
+
exports.METHODS_NON_STANDARD = {
|
|
416
|
+
/**
|
|
417
|
+
* Not defined in any RFC but commonly used
|
|
418
|
+
*/
|
|
419
|
+
PURGE: 29,
|
|
420
|
+
QUERY: 46
|
|
421
|
+
};
|
|
422
|
+
exports.METHODS_ICECAST = { SOURCE: 33 };
|
|
423
|
+
exports.METHODS_AIRPLAY = {
|
|
424
|
+
GET: 1,
|
|
425
|
+
POST: 3
|
|
426
|
+
};
|
|
427
|
+
exports.METHODS_RAOP = { FLUSH: 45 };
|
|
428
|
+
exports.METHODS_RTSP = {
|
|
429
|
+
OPTIONS: exports.METHODS_BASIC_HTTP.OPTIONS,
|
|
430
|
+
DESCRIBE: 35,
|
|
431
|
+
ANNOUNCE: 36,
|
|
432
|
+
SETUP: 37,
|
|
433
|
+
PLAY: 38,
|
|
434
|
+
PAUSE: 39,
|
|
435
|
+
TEARDOWN: 40,
|
|
436
|
+
GET_PARAMETER: 41,
|
|
437
|
+
SET_PARAMETER: 42,
|
|
438
|
+
REDIRECT: 43,
|
|
439
|
+
RECORD: 44,
|
|
440
|
+
...exports.METHODS_AIRPLAY,
|
|
441
|
+
...exports.METHODS_RAOP
|
|
442
|
+
};
|
|
443
|
+
exports.METHODS_HTTP1 = {
|
|
444
|
+
...exports.METHODS_BASIC_HTTP,
|
|
445
|
+
...exports.METHODS_WEBDAV,
|
|
446
|
+
...exports.METHODS_SUBVERSION,
|
|
447
|
+
...exports.METHODS_UPNP,
|
|
448
|
+
...exports.METHODS_CALDAV,
|
|
449
|
+
...exports.METHODS_NON_STANDARD,
|
|
450
|
+
...exports.METHODS_ICECAST
|
|
451
|
+
};
|
|
452
|
+
exports.METHODS_HTTP2 = {
|
|
453
|
+
/**
|
|
454
|
+
* RFC-9113, section 11.6
|
|
455
|
+
* @see https://www.rfc-editor.org/rfc/rfc9113.html#preface
|
|
456
|
+
*/
|
|
457
|
+
PRI: 34 };
|
|
458
|
+
exports.METHODS_HTTP = {
|
|
459
|
+
...exports.METHODS_HTTP1,
|
|
460
|
+
...exports.METHODS_HTTP2
|
|
461
|
+
};
|
|
462
|
+
exports.METHODS = {
|
|
463
|
+
...exports.METHODS_HTTP1,
|
|
464
|
+
...exports.METHODS_HTTP2,
|
|
465
|
+
...exports.METHODS_RTSP
|
|
466
|
+
};
|
|
467
|
+
exports.ALPHA = [
|
|
468
|
+
"A",
|
|
469
|
+
"a",
|
|
470
|
+
"B",
|
|
471
|
+
"b",
|
|
472
|
+
"C",
|
|
473
|
+
"c",
|
|
474
|
+
"D",
|
|
475
|
+
"d",
|
|
476
|
+
"E",
|
|
477
|
+
"e",
|
|
478
|
+
"F",
|
|
479
|
+
"f",
|
|
480
|
+
"G",
|
|
481
|
+
"g",
|
|
482
|
+
"H",
|
|
483
|
+
"h",
|
|
484
|
+
"I",
|
|
485
|
+
"i",
|
|
486
|
+
"J",
|
|
487
|
+
"j",
|
|
488
|
+
"K",
|
|
489
|
+
"k",
|
|
490
|
+
"L",
|
|
491
|
+
"l",
|
|
492
|
+
"M",
|
|
493
|
+
"m",
|
|
494
|
+
"N",
|
|
495
|
+
"n",
|
|
496
|
+
"O",
|
|
497
|
+
"o",
|
|
498
|
+
"P",
|
|
499
|
+
"p",
|
|
500
|
+
"Q",
|
|
501
|
+
"q",
|
|
502
|
+
"R",
|
|
503
|
+
"r",
|
|
504
|
+
"S",
|
|
505
|
+
"s",
|
|
506
|
+
"T",
|
|
507
|
+
"t",
|
|
508
|
+
"U",
|
|
509
|
+
"u",
|
|
510
|
+
"V",
|
|
511
|
+
"v",
|
|
512
|
+
"W",
|
|
513
|
+
"w",
|
|
514
|
+
"X",
|
|
515
|
+
"x",
|
|
516
|
+
"Y",
|
|
517
|
+
"y",
|
|
518
|
+
"Z",
|
|
519
|
+
"z"
|
|
520
|
+
];
|
|
521
|
+
exports.NUM_MAP = {
|
|
522
|
+
0: 0,
|
|
523
|
+
1: 1,
|
|
524
|
+
2: 2,
|
|
525
|
+
3: 3,
|
|
526
|
+
4: 4,
|
|
527
|
+
5: 5,
|
|
528
|
+
6: 6,
|
|
529
|
+
7: 7,
|
|
530
|
+
8: 8,
|
|
531
|
+
9: 9
|
|
532
|
+
};
|
|
533
|
+
exports.HEX_MAP = {
|
|
534
|
+
0: 0,
|
|
535
|
+
1: 1,
|
|
536
|
+
2: 2,
|
|
537
|
+
3: 3,
|
|
538
|
+
4: 4,
|
|
539
|
+
5: 5,
|
|
540
|
+
6: 6,
|
|
541
|
+
7: 7,
|
|
542
|
+
8: 8,
|
|
543
|
+
9: 9,
|
|
544
|
+
A: 10,
|
|
545
|
+
B: 11,
|
|
546
|
+
C: 12,
|
|
547
|
+
D: 13,
|
|
548
|
+
E: 14,
|
|
549
|
+
F: 15,
|
|
550
|
+
a: 10,
|
|
551
|
+
b: 11,
|
|
552
|
+
c: 12,
|
|
553
|
+
d: 13,
|
|
554
|
+
e: 14,
|
|
555
|
+
f: 15
|
|
556
|
+
};
|
|
557
|
+
exports.DIGIT = [
|
|
558
|
+
"0",
|
|
559
|
+
"1",
|
|
560
|
+
"2",
|
|
561
|
+
"3",
|
|
562
|
+
"4",
|
|
563
|
+
"5",
|
|
564
|
+
"6",
|
|
565
|
+
"7",
|
|
566
|
+
"8",
|
|
567
|
+
"9"
|
|
568
|
+
];
|
|
569
|
+
exports.ALPHANUM = [...exports.ALPHA, ...exports.DIGIT];
|
|
570
|
+
exports.MARK = [
|
|
571
|
+
"-",
|
|
572
|
+
"_",
|
|
573
|
+
".",
|
|
574
|
+
"!",
|
|
575
|
+
"~",
|
|
576
|
+
"*",
|
|
577
|
+
"'",
|
|
578
|
+
"(",
|
|
579
|
+
")"
|
|
580
|
+
];
|
|
581
|
+
exports.USERINFO_CHARS = [
|
|
582
|
+
...exports.ALPHANUM,
|
|
583
|
+
...exports.MARK,
|
|
584
|
+
"%",
|
|
585
|
+
";",
|
|
586
|
+
":",
|
|
587
|
+
"&",
|
|
588
|
+
"=",
|
|
589
|
+
"+",
|
|
590
|
+
"$",
|
|
591
|
+
","
|
|
592
|
+
];
|
|
593
|
+
exports.URL_CHAR = [
|
|
594
|
+
"!",
|
|
595
|
+
"\"",
|
|
596
|
+
"$",
|
|
597
|
+
"%",
|
|
598
|
+
"&",
|
|
599
|
+
"'",
|
|
600
|
+
"(",
|
|
601
|
+
")",
|
|
602
|
+
"*",
|
|
603
|
+
"+",
|
|
604
|
+
",",
|
|
605
|
+
"-",
|
|
606
|
+
".",
|
|
607
|
+
"/",
|
|
608
|
+
":",
|
|
609
|
+
";",
|
|
610
|
+
"<",
|
|
611
|
+
"=",
|
|
612
|
+
">",
|
|
613
|
+
"@",
|
|
614
|
+
"[",
|
|
615
|
+
"\\",
|
|
616
|
+
"]",
|
|
617
|
+
"^",
|
|
618
|
+
"_",
|
|
619
|
+
"`",
|
|
620
|
+
"{",
|
|
621
|
+
"|",
|
|
622
|
+
"}",
|
|
623
|
+
"~",
|
|
624
|
+
...exports.ALPHANUM
|
|
625
|
+
];
|
|
626
|
+
exports.HEX = [
|
|
627
|
+
...exports.DIGIT,
|
|
628
|
+
"a",
|
|
629
|
+
"b",
|
|
630
|
+
"c",
|
|
631
|
+
"d",
|
|
632
|
+
"e",
|
|
633
|
+
"f",
|
|
634
|
+
"A",
|
|
635
|
+
"B",
|
|
636
|
+
"C",
|
|
637
|
+
"D",
|
|
638
|
+
"E",
|
|
639
|
+
"F"
|
|
640
|
+
];
|
|
641
|
+
exports.TOKEN = [
|
|
642
|
+
"!",
|
|
643
|
+
"#",
|
|
644
|
+
"$",
|
|
645
|
+
"%",
|
|
646
|
+
"&",
|
|
647
|
+
"'",
|
|
648
|
+
"*",
|
|
649
|
+
"+",
|
|
650
|
+
"-",
|
|
651
|
+
".",
|
|
652
|
+
"^",
|
|
653
|
+
"_",
|
|
654
|
+
"`",
|
|
655
|
+
"|",
|
|
656
|
+
"~",
|
|
657
|
+
...exports.ALPHANUM
|
|
658
|
+
];
|
|
659
|
+
exports.HTAB = [" "];
|
|
660
|
+
exports.SP = [" "];
|
|
661
|
+
const VCHAR = [
|
|
662
|
+
33,
|
|
663
|
+
34,
|
|
664
|
+
35,
|
|
665
|
+
36,
|
|
666
|
+
37,
|
|
667
|
+
38,
|
|
668
|
+
39,
|
|
669
|
+
40,
|
|
670
|
+
41,
|
|
671
|
+
42,
|
|
672
|
+
43,
|
|
673
|
+
44,
|
|
674
|
+
45,
|
|
675
|
+
46,
|
|
676
|
+
47,
|
|
677
|
+
48,
|
|
678
|
+
49,
|
|
679
|
+
50,
|
|
680
|
+
51,
|
|
681
|
+
52,
|
|
682
|
+
53,
|
|
683
|
+
54,
|
|
684
|
+
55,
|
|
685
|
+
56,
|
|
686
|
+
57,
|
|
687
|
+
58,
|
|
688
|
+
59,
|
|
689
|
+
60,
|
|
690
|
+
61,
|
|
691
|
+
62,
|
|
692
|
+
63,
|
|
693
|
+
64,
|
|
694
|
+
65,
|
|
695
|
+
66,
|
|
696
|
+
67,
|
|
697
|
+
68,
|
|
698
|
+
69,
|
|
699
|
+
70,
|
|
700
|
+
71,
|
|
701
|
+
72,
|
|
702
|
+
73,
|
|
703
|
+
74,
|
|
704
|
+
75,
|
|
705
|
+
76,
|
|
706
|
+
77,
|
|
707
|
+
78,
|
|
708
|
+
79,
|
|
709
|
+
80,
|
|
710
|
+
81,
|
|
711
|
+
82,
|
|
712
|
+
83,
|
|
713
|
+
84,
|
|
714
|
+
85,
|
|
715
|
+
86,
|
|
716
|
+
87,
|
|
717
|
+
88,
|
|
718
|
+
89,
|
|
719
|
+
90,
|
|
720
|
+
91,
|
|
721
|
+
92,
|
|
722
|
+
93,
|
|
723
|
+
94,
|
|
724
|
+
95,
|
|
725
|
+
96,
|
|
726
|
+
97,
|
|
727
|
+
98,
|
|
728
|
+
99,
|
|
729
|
+
100,
|
|
730
|
+
101,
|
|
731
|
+
102,
|
|
732
|
+
103,
|
|
733
|
+
104,
|
|
734
|
+
105,
|
|
735
|
+
106,
|
|
736
|
+
107,
|
|
737
|
+
108,
|
|
738
|
+
109,
|
|
739
|
+
110,
|
|
740
|
+
111,
|
|
741
|
+
112,
|
|
742
|
+
113,
|
|
743
|
+
114,
|
|
744
|
+
115,
|
|
745
|
+
116,
|
|
746
|
+
117,
|
|
747
|
+
118,
|
|
748
|
+
119,
|
|
749
|
+
120,
|
|
750
|
+
121,
|
|
751
|
+
122,
|
|
752
|
+
123,
|
|
753
|
+
124,
|
|
754
|
+
125,
|
|
755
|
+
126
|
|
756
|
+
];
|
|
757
|
+
const OBS_TEXT = [
|
|
758
|
+
128,
|
|
759
|
+
129,
|
|
760
|
+
130,
|
|
761
|
+
131,
|
|
762
|
+
132,
|
|
763
|
+
133,
|
|
764
|
+
134,
|
|
765
|
+
135,
|
|
766
|
+
136,
|
|
767
|
+
137,
|
|
768
|
+
138,
|
|
769
|
+
139,
|
|
770
|
+
140,
|
|
771
|
+
141,
|
|
772
|
+
142,
|
|
773
|
+
143,
|
|
774
|
+
144,
|
|
775
|
+
145,
|
|
776
|
+
146,
|
|
777
|
+
147,
|
|
778
|
+
148,
|
|
779
|
+
149,
|
|
780
|
+
150,
|
|
781
|
+
151,
|
|
782
|
+
152,
|
|
783
|
+
153,
|
|
784
|
+
154,
|
|
785
|
+
155,
|
|
786
|
+
156,
|
|
787
|
+
157,
|
|
788
|
+
158,
|
|
789
|
+
159,
|
|
790
|
+
160,
|
|
791
|
+
161,
|
|
792
|
+
162,
|
|
793
|
+
163,
|
|
794
|
+
164,
|
|
795
|
+
165,
|
|
796
|
+
166,
|
|
797
|
+
167,
|
|
798
|
+
168,
|
|
799
|
+
169,
|
|
800
|
+
170,
|
|
801
|
+
171,
|
|
802
|
+
172,
|
|
803
|
+
173,
|
|
804
|
+
174,
|
|
805
|
+
175,
|
|
806
|
+
176,
|
|
807
|
+
177,
|
|
808
|
+
178,
|
|
809
|
+
179,
|
|
810
|
+
180,
|
|
811
|
+
181,
|
|
812
|
+
182,
|
|
813
|
+
183,
|
|
814
|
+
184,
|
|
815
|
+
185,
|
|
816
|
+
186,
|
|
817
|
+
187,
|
|
818
|
+
188,
|
|
819
|
+
189,
|
|
820
|
+
190,
|
|
821
|
+
191,
|
|
822
|
+
192,
|
|
823
|
+
193,
|
|
824
|
+
194,
|
|
825
|
+
195,
|
|
826
|
+
196,
|
|
827
|
+
197,
|
|
828
|
+
198,
|
|
829
|
+
199,
|
|
830
|
+
200,
|
|
831
|
+
201,
|
|
832
|
+
202,
|
|
833
|
+
203,
|
|
834
|
+
204,
|
|
835
|
+
205,
|
|
836
|
+
206,
|
|
837
|
+
207,
|
|
838
|
+
208,
|
|
839
|
+
209,
|
|
840
|
+
210,
|
|
841
|
+
211,
|
|
842
|
+
212,
|
|
843
|
+
213,
|
|
844
|
+
214,
|
|
845
|
+
215,
|
|
846
|
+
216,
|
|
847
|
+
217,
|
|
848
|
+
218,
|
|
849
|
+
219,
|
|
850
|
+
220,
|
|
851
|
+
221,
|
|
852
|
+
222,
|
|
853
|
+
223,
|
|
854
|
+
224,
|
|
855
|
+
225,
|
|
856
|
+
226,
|
|
857
|
+
227,
|
|
858
|
+
228,
|
|
859
|
+
229,
|
|
860
|
+
230,
|
|
861
|
+
231,
|
|
862
|
+
232,
|
|
863
|
+
233,
|
|
864
|
+
234,
|
|
865
|
+
235,
|
|
866
|
+
236,
|
|
867
|
+
237,
|
|
868
|
+
238,
|
|
869
|
+
239,
|
|
870
|
+
240,
|
|
871
|
+
241,
|
|
872
|
+
242,
|
|
873
|
+
243,
|
|
874
|
+
244,
|
|
875
|
+
245,
|
|
876
|
+
246,
|
|
877
|
+
247,
|
|
878
|
+
248,
|
|
879
|
+
249,
|
|
880
|
+
250,
|
|
881
|
+
251,
|
|
882
|
+
252,
|
|
883
|
+
253,
|
|
884
|
+
254,
|
|
885
|
+
255
|
|
886
|
+
];
|
|
887
|
+
exports.HTAB_SP_VCHAR_OBS_TEXT = [
|
|
888
|
+
...exports.HTAB,
|
|
889
|
+
...exports.SP,
|
|
890
|
+
...VCHAR,
|
|
891
|
+
...OBS_TEXT
|
|
892
|
+
];
|
|
893
|
+
exports.HEADER_CHARS = exports.HTAB_SP_VCHAR_OBS_TEXT;
|
|
894
|
+
exports.RELAXED_HEADER_CHARS = [...[
|
|
895
|
+
1,
|
|
896
|
+
2,
|
|
897
|
+
3,
|
|
898
|
+
4,
|
|
899
|
+
5,
|
|
900
|
+
6,
|
|
901
|
+
7,
|
|
902
|
+
8,
|
|
903
|
+
11,
|
|
904
|
+
12,
|
|
905
|
+
14,
|
|
906
|
+
15,
|
|
907
|
+
16,
|
|
908
|
+
17,
|
|
909
|
+
18,
|
|
910
|
+
19,
|
|
911
|
+
20,
|
|
912
|
+
21,
|
|
913
|
+
22,
|
|
914
|
+
23,
|
|
915
|
+
24,
|
|
916
|
+
25,
|
|
917
|
+
26,
|
|
918
|
+
27,
|
|
919
|
+
28,
|
|
920
|
+
29,
|
|
921
|
+
30,
|
|
922
|
+
31,
|
|
923
|
+
127
|
|
924
|
+
], ...exports.HEADER_CHARS];
|
|
925
|
+
exports.CONNECTION_TOKEN_CHARS = [
|
|
926
|
+
...exports.HTAB,
|
|
927
|
+
...exports.SP,
|
|
928
|
+
33,
|
|
929
|
+
34,
|
|
930
|
+
35,
|
|
931
|
+
36,
|
|
932
|
+
37,
|
|
933
|
+
38,
|
|
934
|
+
39,
|
|
935
|
+
40,
|
|
936
|
+
41,
|
|
937
|
+
42,
|
|
938
|
+
43,
|
|
939
|
+
45,
|
|
940
|
+
46,
|
|
941
|
+
47,
|
|
942
|
+
48,
|
|
943
|
+
49,
|
|
944
|
+
50,
|
|
945
|
+
51,
|
|
946
|
+
52,
|
|
947
|
+
53,
|
|
948
|
+
54,
|
|
949
|
+
55,
|
|
950
|
+
56,
|
|
951
|
+
57,
|
|
952
|
+
58,
|
|
953
|
+
59,
|
|
954
|
+
60,
|
|
955
|
+
61,
|
|
956
|
+
62,
|
|
957
|
+
63,
|
|
958
|
+
64,
|
|
959
|
+
65,
|
|
960
|
+
66,
|
|
961
|
+
67,
|
|
962
|
+
68,
|
|
963
|
+
69,
|
|
964
|
+
70,
|
|
965
|
+
71,
|
|
966
|
+
72,
|
|
967
|
+
73,
|
|
968
|
+
74,
|
|
969
|
+
75,
|
|
970
|
+
76,
|
|
971
|
+
77,
|
|
972
|
+
78,
|
|
973
|
+
79,
|
|
974
|
+
80,
|
|
975
|
+
81,
|
|
976
|
+
82,
|
|
977
|
+
83,
|
|
978
|
+
84,
|
|
979
|
+
85,
|
|
980
|
+
86,
|
|
981
|
+
87,
|
|
982
|
+
88,
|
|
983
|
+
89,
|
|
984
|
+
90,
|
|
985
|
+
91,
|
|
986
|
+
92,
|
|
987
|
+
93,
|
|
988
|
+
94,
|
|
989
|
+
95,
|
|
990
|
+
96,
|
|
991
|
+
97,
|
|
992
|
+
98,
|
|
993
|
+
99,
|
|
994
|
+
100,
|
|
995
|
+
101,
|
|
996
|
+
102,
|
|
997
|
+
103,
|
|
998
|
+
104,
|
|
999
|
+
105,
|
|
1000
|
+
106,
|
|
1001
|
+
107,
|
|
1002
|
+
108,
|
|
1003
|
+
109,
|
|
1004
|
+
110,
|
|
1005
|
+
111,
|
|
1006
|
+
112,
|
|
1007
|
+
113,
|
|
1008
|
+
114,
|
|
1009
|
+
115,
|
|
1010
|
+
116,
|
|
1011
|
+
117,
|
|
1012
|
+
118,
|
|
1013
|
+
119,
|
|
1014
|
+
120,
|
|
1015
|
+
121,
|
|
1016
|
+
122,
|
|
1017
|
+
123,
|
|
1018
|
+
124,
|
|
1019
|
+
125,
|
|
1020
|
+
126,
|
|
1021
|
+
...OBS_TEXT
|
|
1022
|
+
];
|
|
1023
|
+
exports.QDTEXT = [
|
|
1024
|
+
...exports.HTAB,
|
|
1025
|
+
...exports.SP,
|
|
1026
|
+
33,
|
|
1027
|
+
35,
|
|
1028
|
+
36,
|
|
1029
|
+
37,
|
|
1030
|
+
38,
|
|
1031
|
+
39,
|
|
1032
|
+
40,
|
|
1033
|
+
41,
|
|
1034
|
+
42,
|
|
1035
|
+
43,
|
|
1036
|
+
44,
|
|
1037
|
+
45,
|
|
1038
|
+
46,
|
|
1039
|
+
47,
|
|
1040
|
+
48,
|
|
1041
|
+
49,
|
|
1042
|
+
50,
|
|
1043
|
+
51,
|
|
1044
|
+
52,
|
|
1045
|
+
53,
|
|
1046
|
+
54,
|
|
1047
|
+
55,
|
|
1048
|
+
56,
|
|
1049
|
+
57,
|
|
1050
|
+
58,
|
|
1051
|
+
59,
|
|
1052
|
+
60,
|
|
1053
|
+
61,
|
|
1054
|
+
62,
|
|
1055
|
+
63,
|
|
1056
|
+
64,
|
|
1057
|
+
65,
|
|
1058
|
+
66,
|
|
1059
|
+
67,
|
|
1060
|
+
68,
|
|
1061
|
+
69,
|
|
1062
|
+
70,
|
|
1063
|
+
71,
|
|
1064
|
+
72,
|
|
1065
|
+
73,
|
|
1066
|
+
74,
|
|
1067
|
+
75,
|
|
1068
|
+
76,
|
|
1069
|
+
77,
|
|
1070
|
+
78,
|
|
1071
|
+
79,
|
|
1072
|
+
80,
|
|
1073
|
+
81,
|
|
1074
|
+
82,
|
|
1075
|
+
83,
|
|
1076
|
+
84,
|
|
1077
|
+
85,
|
|
1078
|
+
86,
|
|
1079
|
+
87,
|
|
1080
|
+
88,
|
|
1081
|
+
89,
|
|
1082
|
+
90,
|
|
1083
|
+
91,
|
|
1084
|
+
93,
|
|
1085
|
+
94,
|
|
1086
|
+
95,
|
|
1087
|
+
96,
|
|
1088
|
+
97,
|
|
1089
|
+
98,
|
|
1090
|
+
99,
|
|
1091
|
+
100,
|
|
1092
|
+
101,
|
|
1093
|
+
102,
|
|
1094
|
+
103,
|
|
1095
|
+
104,
|
|
1096
|
+
105,
|
|
1097
|
+
106,
|
|
1098
|
+
107,
|
|
1099
|
+
108,
|
|
1100
|
+
109,
|
|
1101
|
+
110,
|
|
1102
|
+
111,
|
|
1103
|
+
112,
|
|
1104
|
+
113,
|
|
1105
|
+
114,
|
|
1106
|
+
115,
|
|
1107
|
+
116,
|
|
1108
|
+
117,
|
|
1109
|
+
118,
|
|
1110
|
+
119,
|
|
1111
|
+
120,
|
|
1112
|
+
121,
|
|
1113
|
+
122,
|
|
1114
|
+
123,
|
|
1115
|
+
124,
|
|
1116
|
+
125,
|
|
1117
|
+
126,
|
|
1118
|
+
...OBS_TEXT
|
|
1119
|
+
];
|
|
1120
|
+
exports.MAJOR = exports.NUM_MAP;
|
|
1121
|
+
exports.MINOR = exports.MAJOR;
|
|
1122
|
+
exports.SPECIAL_HEADERS = {
|
|
1123
|
+
"connection": exports.HEADER_STATE.CONNECTION,
|
|
1124
|
+
"content-length": exports.HEADER_STATE.CONTENT_LENGTH,
|
|
1125
|
+
"proxy-connection": exports.HEADER_STATE.CONNECTION,
|
|
1126
|
+
"transfer-encoding": exports.HEADER_STATE.TRANSFER_ENCODING,
|
|
1127
|
+
"upgrade": exports.HEADER_STATE.UPGRADE
|
|
1128
|
+
};
|
|
1129
|
+
exports.default = {
|
|
1130
|
+
ERROR: exports.ERROR,
|
|
1131
|
+
TYPE: exports.TYPE,
|
|
1132
|
+
FLAGS: exports.FLAGS,
|
|
1133
|
+
LENIENT_FLAGS: exports.LENIENT_FLAGS,
|
|
1134
|
+
STATUSES: exports.STATUSES,
|
|
1135
|
+
FINISH: exports.FINISH,
|
|
1136
|
+
HEADER_STATE: exports.HEADER_STATE,
|
|
1137
|
+
ALPHA: exports.ALPHA,
|
|
1138
|
+
NUM_MAP: exports.NUM_MAP,
|
|
1139
|
+
HEX_MAP: exports.HEX_MAP,
|
|
1140
|
+
DIGIT: exports.DIGIT,
|
|
1141
|
+
ALPHANUM: exports.ALPHANUM,
|
|
1142
|
+
MARK: exports.MARK,
|
|
1143
|
+
USERINFO_CHARS: exports.USERINFO_CHARS,
|
|
1144
|
+
URL_CHAR: exports.URL_CHAR,
|
|
1145
|
+
HEX: exports.HEX,
|
|
1146
|
+
TOKEN: exports.TOKEN,
|
|
1147
|
+
HEADER_CHARS: exports.HEADER_CHARS,
|
|
1148
|
+
RELAXED_HEADER_CHARS: exports.RELAXED_HEADER_CHARS,
|
|
1149
|
+
CONNECTION_TOKEN_CHARS: exports.CONNECTION_TOKEN_CHARS,
|
|
1150
|
+
QDTEXT: exports.QDTEXT,
|
|
1151
|
+
HTAB_SP_VCHAR_OBS_TEXT: exports.HTAB_SP_VCHAR_OBS_TEXT,
|
|
1152
|
+
MAJOR: exports.MAJOR,
|
|
1153
|
+
MINOR: exports.MINOR,
|
|
1154
|
+
SPECIAL_HEADERS: exports.SPECIAL_HEADERS,
|
|
1155
|
+
METHODS: exports.METHODS,
|
|
1156
|
+
METHODS_HTTP: exports.METHODS_HTTP,
|
|
1157
|
+
METHODS_HTTP1_HEAD: exports.METHODS_HTTP1_HEAD,
|
|
1158
|
+
METHODS_HTTP1: exports.METHODS_HTTP1,
|
|
1159
|
+
METHODS_HTTP2: exports.METHODS_HTTP2,
|
|
1160
|
+
METHODS_ICECAST: exports.METHODS_ICECAST,
|
|
1161
|
+
METHODS_RTSP: exports.METHODS_RTSP
|
|
1162
|
+
};
|
|
1163
|
+
})))(), 1);
|
|
1164
|
+
const KIND_REQUEST = import_constants.TYPE.REQUEST;
|
|
1165
|
+
import_constants.TYPE.RESPONSE;
|
|
1166
|
+
const kPointer = Symbol("kPtr");
|
|
1167
|
+
const kUrl = Symbol("kUrl");
|
|
1168
|
+
const kStatusMessage = Symbol("kStatusMessage");
|
|
1169
|
+
const kHeadersFields = Symbol("kHeadersFields");
|
|
1170
|
+
const kHeadersValues = Symbol("kHeadersValues");
|
|
1171
|
+
const kLastHeaderCallback = Symbol("kLastHeaderCallback");
|
|
1172
|
+
const kCallbacks = Symbol("kCallbacks");
|
|
1173
|
+
const kType = Symbol("kType");
|
|
1174
|
+
const HEADER_CB_NONE = 0;
|
|
1175
|
+
const HEADER_CB_FIELD = 1;
|
|
1176
|
+
const HEADER_CB_VALUE = 2;
|
|
1177
|
+
const parsersMap = /* @__PURE__ */ new Map();
|
|
1178
|
+
const methodNames = Object.fromEntries(Object.entries(import_constants.METHODS).map(([name, num]) => [num, name]));
|
|
1179
|
+
function readStringFrom(pointer, length) {
|
|
1180
|
+
return Buffer.from(llhttp_memory.buffer, pointer, length).toString("latin1");
|
|
1181
|
+
}
|
|
1182
|
+
/**
|
|
1183
|
+
* @note Reference the base URL through a variable. Bundlers (e.g. Vite)
|
|
1184
|
+
* statically rewrite the `new URL('...', import.meta.url)` pattern into
|
|
1185
|
+
* an asset URL resolved against the served origin, which breaks reading
|
|
1186
|
+
* the WASM binary from the file system in DOM-like test environments.
|
|
1187
|
+
*/
|
|
1188
|
+
const wasmBaseUrl = import.meta.url;
|
|
1189
|
+
const llhttpModule = new WebAssembly.Module(fs.readFileSync(new URL("./llhttp/llhttp.wasm", wasmBaseUrl)));
|
|
1190
|
+
const llhttpInstance = new WebAssembly.Instance(llhttpModule, { env: {
|
|
1191
|
+
wasm_on_message_begin(parserPointer) {
|
|
1192
|
+
const parser = parsersMap.get(parserPointer);
|
|
1193
|
+
parser[kUrl] = "";
|
|
1194
|
+
parser[kStatusMessage] = "";
|
|
1195
|
+
parser[kHeadersFields] = [];
|
|
1196
|
+
parser[kHeadersValues] = [];
|
|
1197
|
+
parser[kLastHeaderCallback] = HEADER_CB_NONE;
|
|
1198
|
+
return parser[kCallbacks].onMessageBegin?.() ?? 0;
|
|
1199
|
+
},
|
|
1200
|
+
wasm_on_url(parserPointer, at, length) {
|
|
1201
|
+
parsersMap.get(parserPointer)[kUrl] = readStringFrom(at, length);
|
|
1202
|
+
return 0;
|
|
1203
|
+
},
|
|
1204
|
+
wasm_on_status(parserPointer, at, length) {
|
|
1205
|
+
parsersMap.get(parserPointer)[kStatusMessage] = readStringFrom(at, length);
|
|
1206
|
+
return 0;
|
|
1207
|
+
},
|
|
1208
|
+
wasm_on_header_field(parserPointer, at, length) {
|
|
1209
|
+
const parser = parsersMap.get(parserPointer);
|
|
1210
|
+
const chunk = readStringFrom(at, length);
|
|
1211
|
+
const fields = parser[kHeadersFields];
|
|
1212
|
+
if (parser[kLastHeaderCallback] === HEADER_CB_FIELD) fields[fields.length - 1] += chunk;
|
|
1213
|
+
else {
|
|
1214
|
+
fields.push(chunk);
|
|
1215
|
+
parser[kLastHeaderCallback] = HEADER_CB_FIELD;
|
|
1216
|
+
}
|
|
1217
|
+
return 0;
|
|
1218
|
+
},
|
|
1219
|
+
wasm_on_header_value(parserPointer, at, length) {
|
|
1220
|
+
const parser = parsersMap.get(parserPointer);
|
|
1221
|
+
const chunk = readStringFrom(at, length);
|
|
1222
|
+
const values = parser[kHeadersValues];
|
|
1223
|
+
if (parser[kLastHeaderCallback] === HEADER_CB_VALUE) values[values.length - 1] += chunk;
|
|
1224
|
+
else {
|
|
1225
|
+
values.push(chunk);
|
|
1226
|
+
parser[kLastHeaderCallback] = HEADER_CB_VALUE;
|
|
1227
|
+
}
|
|
1228
|
+
return 0;
|
|
1229
|
+
},
|
|
1230
|
+
wasm_on_headers_complete(parserPointer, statusCode, rawUpgrade, rawShouldKeepAlive) {
|
|
1231
|
+
const parser = parsersMap.get(parserPointer);
|
|
1232
|
+
const versionMajor = llhttp_get_version_major(parserPointer);
|
|
1233
|
+
const versionMinor = llhttp_get_version_minor(parserPointer);
|
|
1234
|
+
const rawHeaders = [];
|
|
1235
|
+
const upgrade = rawUpgrade === 1;
|
|
1236
|
+
const shouldKeepAlive = rawShouldKeepAlive === 1;
|
|
1237
|
+
for (let c = 0; c < parser[kHeadersFields].length; c++) rawHeaders.push(parser[kHeadersFields][c], parser[kHeadersValues][c]);
|
|
1238
|
+
if (parser[kType] === KIND_REQUEST) {
|
|
1239
|
+
const method = methodNames[llhttp_get_method(parserPointer)];
|
|
1240
|
+
const url = parser[kUrl];
|
|
1241
|
+
return parser[kCallbacks].onHeadersComplete?.({
|
|
1242
|
+
versionMajor,
|
|
1243
|
+
versionMinor,
|
|
1244
|
+
rawHeaders,
|
|
1245
|
+
method,
|
|
1246
|
+
url,
|
|
1247
|
+
upgrade,
|
|
1248
|
+
shouldKeepAlive
|
|
1249
|
+
}) ?? 0;
|
|
1250
|
+
} else {
|
|
1251
|
+
const statusCode = llhttp_get_status_code(parserPointer);
|
|
1252
|
+
const statusMessage = parser[kStatusMessage];
|
|
1253
|
+
return parser[kCallbacks].onHeadersComplete?.({
|
|
1254
|
+
versionMajor,
|
|
1255
|
+
versionMinor,
|
|
1256
|
+
rawHeaders,
|
|
1257
|
+
statusCode,
|
|
1258
|
+
statusMessage,
|
|
1259
|
+
upgrade,
|
|
1260
|
+
shouldKeepAlive
|
|
1261
|
+
}) ?? 0;
|
|
1262
|
+
}
|
|
1263
|
+
},
|
|
1264
|
+
wasm_on_body(parserPointer, at, length) {
|
|
1265
|
+
const parser = parsersMap.get(parserPointer);
|
|
1266
|
+
const body = Buffer.from(new Uint8Array(llhttp_memory.buffer, at, length));
|
|
1267
|
+
return parser[kCallbacks].onBody?.(body) ?? 0;
|
|
1268
|
+
},
|
|
1269
|
+
wasm_on_message_complete(parserPointer) {
|
|
1270
|
+
return parsersMap.get(parserPointer)[kCallbacks].onMessageComplete?.() ?? 0;
|
|
1271
|
+
}
|
|
1272
|
+
} });
|
|
1273
|
+
const llhttp_memory = llhttpInstance.exports.memory;
|
|
1274
|
+
const llhttp_alloc = llhttpInstance.exports.llhttp_alloc;
|
|
1275
|
+
const llhttp_malloc = llhttpInstance.exports.malloc;
|
|
1276
|
+
const llhttp_execute = llhttpInstance.exports.llhttp_execute;
|
|
1277
|
+
llhttpInstance.exports.llhttp_get_type;
|
|
1278
|
+
llhttpInstance.exports.llhttp_get_upgrade;
|
|
1279
|
+
llhttpInstance.exports.llhttp_should_keep_alive;
|
|
1280
|
+
const llhttp_get_method = llhttpInstance.exports.llhttp_get_method;
|
|
1281
|
+
const llhttp_get_status_code = llhttpInstance.exports.llhttp_get_status_code;
|
|
1282
|
+
const llhttp_get_version_minor = llhttpInstance.exports.llhttp_get_http_minor;
|
|
1283
|
+
const llhttp_get_version_major = llhttpInstance.exports.llhttp_get_http_major;
|
|
1284
|
+
const llhttp_get_error_reason = llhttpInstance.exports.llhttp_get_error_reason;
|
|
1285
|
+
const llhttp_get_error_pos = llhttpInstance.exports.llhttp_get_error_pos;
|
|
1286
|
+
const llhttp_free = llhttpInstance.exports.free;
|
|
1287
|
+
const initialize = llhttpInstance.exports._initialize;
|
|
1288
|
+
initialize();
|
|
1289
|
+
var HttpParser = class {
|
|
1290
|
+
constructor(type, callbacks) {
|
|
1291
|
+
this[kUrl] = "";
|
|
1292
|
+
this[kStatusMessage] = "";
|
|
1293
|
+
this[kHeadersFields] = [];
|
|
1294
|
+
this[kHeadersValues] = [];
|
|
1295
|
+
this[kLastHeaderCallback] = HEADER_CB_NONE;
|
|
1296
|
+
this[kType] = type;
|
|
1297
|
+
this[kCallbacks] = callbacks;
|
|
1298
|
+
const parserPointer = llhttp_alloc(type);
|
|
1299
|
+
if (parserPointer === 0) throw new Error("Failed to allocate llhttp parser");
|
|
1300
|
+
this[kPointer] = parserPointer;
|
|
1301
|
+
parsersMap.set(this[kPointer], this);
|
|
1302
|
+
}
|
|
1303
|
+
destroy() {
|
|
1304
|
+
if (this[kPointer] === 0) return;
|
|
1305
|
+
parsersMap.delete(this[kPointer]);
|
|
1306
|
+
llhttp_free(this[kPointer]);
|
|
1307
|
+
this[kPointer] = 0;
|
|
1308
|
+
}
|
|
1309
|
+
execute(data) {
|
|
1310
|
+
const pointer = llhttp_malloc(data.byteLength);
|
|
1311
|
+
if (pointer === 0) throw new Error("Failed to allocate llhttp input buffer");
|
|
1312
|
+
let ret;
|
|
1313
|
+
try {
|
|
1314
|
+
new Uint8Array(llhttp_memory.buffer).set(data, pointer);
|
|
1315
|
+
ret = llhttp_execute(this[kPointer], pointer, data.byteLength);
|
|
1316
|
+
} catch (error) {
|
|
1317
|
+
llhttp_free(pointer);
|
|
1318
|
+
throw error;
|
|
1319
|
+
}
|
|
1320
|
+
if (ret === import_constants.ERROR.PAUSED_UPGRADE) {
|
|
1321
|
+
const consumed = llhttp_get_error_pos(this[kPointer]) - pointer;
|
|
1322
|
+
llhttp_free(pointer);
|
|
1323
|
+
return data.subarray(consumed);
|
|
1324
|
+
}
|
|
1325
|
+
llhttp_free(pointer);
|
|
1326
|
+
this.#checkError(ret);
|
|
1327
|
+
return null;
|
|
1328
|
+
}
|
|
1329
|
+
#checkError(errorCode) {
|
|
1330
|
+
if (errorCode === import_constants.ERROR.OK) return;
|
|
1331
|
+
const errorPointer = llhttp_get_error_reason(this[kPointer]);
|
|
1332
|
+
const length = new Uint8Array(llhttp_memory.buffer).indexOf(0, errorPointer) - errorPointer;
|
|
1333
|
+
throw new Error(readStringFrom(errorPointer, length));
|
|
1334
|
+
}
|
|
1335
|
+
};
|
|
1336
|
+
//#endregion
|
|
1337
|
+
//#region src/interceptors/http/http-parser.ts
|
|
1338
|
+
var HttpRequestParser = class extends HttpParser {
|
|
1339
|
+
#requestBodyStream;
|
|
1340
|
+
constructor(options) {
|
|
1341
|
+
super(1, {
|
|
1342
|
+
onHeadersComplete: ({ rawHeaders, method, url: path }) => {
|
|
1343
|
+
/**
|
|
1344
|
+
* @note When the socket is reused, "connectionOptions" will point
|
|
1345
|
+
* to the "net.connect()" call options that established the connection,
|
|
1346
|
+
* which may differ from the description of the current request (e.g. method).
|
|
1347
|
+
* Rely on the HTTPParser supplying us with the correct "rawMethod" number.
|
|
1348
|
+
*/
|
|
1349
|
+
const finalMethod = (method || options.connectionOptions.method || "GET").toUpperCase();
|
|
1350
|
+
const url = new URL(path || "", options.connectionOptions.url);
|
|
1351
|
+
const headers = FetchResponse.parseRawHeaders([...rawHeaders]);
|
|
1352
|
+
if (url.username || url.password) {
|
|
1353
|
+
if (!headers.has("authorization")) {
|
|
1354
|
+
const credentials = Buffer.from(`${url.username}:${url.password}`).toString("base64");
|
|
1355
|
+
headers.set("authorization", `Basic ${credentials}`);
|
|
1356
|
+
}
|
|
1357
|
+
url.username = "";
|
|
1358
|
+
url.password = "";
|
|
1359
|
+
}
|
|
1360
|
+
this.#requestBodyStream = new Readable({
|
|
1361
|
+
/**
|
|
1362
|
+
* @note Provide the `read()` method so a `Readable` could be
|
|
1363
|
+
* used as the actual request body (the stream calls "read()").
|
|
1364
|
+
*/
|
|
1365
|
+
read: () => {} });
|
|
1366
|
+
/**
|
|
1367
|
+
* @note Expose an abort controller for the parsed request so the
|
|
1368
|
+
* consumer can abort it (e.g. when the client destroys the
|
|
1369
|
+
* connection before the request is handled).
|
|
1370
|
+
*/
|
|
1371
|
+
const abortController = new AbortController();
|
|
1372
|
+
const request = new FetchRequest(url, {
|
|
1373
|
+
method: finalMethod,
|
|
1374
|
+
headers,
|
|
1375
|
+
credentials: "same-origin",
|
|
1376
|
+
body: Readable.toWeb(this.#requestBodyStream),
|
|
1377
|
+
signal: abortController.signal
|
|
1378
|
+
});
|
|
1379
|
+
options.onRequest(request, abortController);
|
|
1380
|
+
},
|
|
1381
|
+
onBody: (chunk) => {
|
|
1382
|
+
invariant(this.#requestBodyStream, "Failed to write to a request stream: stream does not exist. This is likely an issue with the library. Please report it on GitHub.");
|
|
1383
|
+
this.#requestBodyStream.push(chunk);
|
|
1384
|
+
},
|
|
1385
|
+
onMessageComplete: () => {
|
|
1386
|
+
this.#requestBodyStream?.push(null);
|
|
1387
|
+
}
|
|
1388
|
+
});
|
|
1389
|
+
}
|
|
1390
|
+
free() {
|
|
1391
|
+
this.destroy();
|
|
1392
|
+
this.#requestBodyStream?.destroy();
|
|
1393
|
+
this.#requestBodyStream = void 0;
|
|
1394
|
+
}
|
|
1395
|
+
};
|
|
1396
|
+
var HttpResponseParser = class extends HttpParser {
|
|
1397
|
+
#responseBodyStream;
|
|
1398
|
+
constructor(options) {
|
|
1399
|
+
super(2, {
|
|
1400
|
+
onHeadersComplete: ({ rawHeaders, statusCode: status, statusMessage: statusText }) => {
|
|
1401
|
+
const headers = FetchResponse.parseRawHeaders([...rawHeaders]);
|
|
1402
|
+
const response = new FetchResponse(FetchResponse.isResponseWithBody(status) ? Readable.toWeb(this.#responseBodyStream = new Readable({ read() {} })) : null, {
|
|
1403
|
+
status,
|
|
1404
|
+
statusText,
|
|
1405
|
+
headers
|
|
1406
|
+
});
|
|
1407
|
+
options.onResponse(response);
|
|
1408
|
+
},
|
|
1409
|
+
onBody: (chunk) => {
|
|
1410
|
+
invariant(this.#responseBodyStream, "Failed to read from a response stream: stream does not exist. This is likely an issue with the library. Please report it on GitHub.");
|
|
1411
|
+
this.#responseBodyStream.push(chunk);
|
|
1412
|
+
},
|
|
1413
|
+
onMessageComplete: () => {
|
|
1414
|
+
this.#responseBodyStream?.push(null);
|
|
1415
|
+
}
|
|
1416
|
+
});
|
|
1417
|
+
}
|
|
1418
|
+
free() {
|
|
1419
|
+
this.destroy();
|
|
1420
|
+
this.#responseBodyStream = null;
|
|
1421
|
+
}
|
|
1422
|
+
};
|
|
1423
|
+
//#endregion
|
|
1424
|
+
//#region src/utils/is-node-like-error.ts
|
|
1425
|
+
function isNodeLikeError(error) {
|
|
1426
|
+
if (error == null) return false;
|
|
1427
|
+
if (!(error instanceof Error)) return false;
|
|
1428
|
+
return "code" in error && "errno" in error;
|
|
1429
|
+
}
|
|
1430
|
+
//#endregion
|
|
1431
|
+
//#region src/utils/handle-request.ts
|
|
1432
|
+
async function handleRequest(options) {
|
|
1433
|
+
if (options.logger?.isEnabled("default")) formatRequest(options.request).then((message) => {
|
|
1434
|
+
options.logger?.info("[%s] %s", options.requestId, message);
|
|
1435
|
+
});
|
|
1436
|
+
const handleResponse = async (response) => {
|
|
1437
|
+
if (response instanceof Error) {
|
|
1438
|
+
await options.controller.errorWith(response);
|
|
1439
|
+
return true;
|
|
1440
|
+
}
|
|
1441
|
+
if (isResponseError(response)) {
|
|
1442
|
+
await options.controller.respondWith(response);
|
|
1443
|
+
return true;
|
|
1444
|
+
}
|
|
1445
|
+
/**
|
|
1446
|
+
* Handle normal responses or response-like objects.
|
|
1447
|
+
* @note This must come before the arbitrary object check
|
|
1448
|
+
* since Response instances are, in fact, objects.
|
|
1449
|
+
*/
|
|
1450
|
+
if (isResponseLike(response)) {
|
|
1451
|
+
await options.controller.respondWith(response);
|
|
1452
|
+
return true;
|
|
1453
|
+
}
|
|
1454
|
+
if (isObject(response)) {
|
|
1455
|
+
await options.controller.errorWith(response);
|
|
1456
|
+
return true;
|
|
1457
|
+
}
|
|
1458
|
+
return false;
|
|
1459
|
+
};
|
|
1460
|
+
const handleResponseError = async (error) => {
|
|
1461
|
+
if (error instanceof InterceptorError) throw resultError;
|
|
1462
|
+
if (isNodeLikeError(error)) {
|
|
1463
|
+
await options.controller.errorWith(error);
|
|
1464
|
+
return true;
|
|
1465
|
+
}
|
|
1466
|
+
if (error instanceof Response) return await handleResponse(error);
|
|
1467
|
+
return false;
|
|
1468
|
+
};
|
|
1469
|
+
const requestAbortPromise = Promise.withResolvers();
|
|
1470
|
+
let requestAbortReason;
|
|
1471
|
+
let isRequestAborted = false;
|
|
1472
|
+
const onAbort = () => {
|
|
1473
|
+
isRequestAborted = true;
|
|
1474
|
+
requestAbortReason = options.request.signal?.reason;
|
|
1475
|
+
requestAbortPromise.reject(requestAbortReason);
|
|
1476
|
+
};
|
|
1477
|
+
/**
|
|
1478
|
+
* @note `signal` is not always defined in React Native.
|
|
1479
|
+
*/
|
|
1480
|
+
if (options.request.signal) {
|
|
1481
|
+
if (options.request.signal.aborted) {
|
|
1482
|
+
await options.controller.errorWith(options.request.signal.reason);
|
|
1483
|
+
return;
|
|
1484
|
+
}
|
|
1485
|
+
options.request.signal.addEventListener("abort", onAbort, { once: true });
|
|
1486
|
+
}
|
|
1487
|
+
const [resultError] = await until(async () => {
|
|
1488
|
+
const requestEvent = new HttpRequestEvent({
|
|
1489
|
+
initiator: options.initiator,
|
|
1490
|
+
requestId: options.requestId,
|
|
1491
|
+
request: options.request,
|
|
1492
|
+
controller: options.controller
|
|
1493
|
+
});
|
|
1494
|
+
const requestListenersPromise = options.emitter.emitAsPromise(requestEvent);
|
|
1495
|
+
await Promise.race([
|
|
1496
|
+
requestAbortPromise.promise,
|
|
1497
|
+
requestListenersPromise,
|
|
1498
|
+
options.controller.handled
|
|
1499
|
+
]);
|
|
1500
|
+
/**
|
|
1501
|
+
* @note If the "request" listener has replaced the request instance,
|
|
1502
|
+
* propagate that mutation back to the underlying insterceptor.
|
|
1503
|
+
* This happens with XMLHttpRequest that replaces request instances
|
|
1504
|
+
* to correctly reflect the "withCredentials" option on the Fetch API request.
|
|
1505
|
+
*/
|
|
1506
|
+
if (requestEvent.request !== options.request) options.request = requestEvent.request;
|
|
1507
|
+
});
|
|
1508
|
+
options.request.signal?.removeEventListener("abort", onAbort);
|
|
1509
|
+
if (isRequestAborted) {
|
|
1510
|
+
await options.controller.errorWith(requestAbortReason);
|
|
1511
|
+
return;
|
|
1512
|
+
}
|
|
1513
|
+
if (resultError) {
|
|
1514
|
+
if (await handleResponseError(resultError)) return;
|
|
1515
|
+
if (options.emitter.listenerCount("unhandledException") > 0) {
|
|
1516
|
+
const unhandledExceptionController = new RequestController(options.request, {
|
|
1517
|
+
/**
|
|
1518
|
+
* @note Intentionally empty passthrough handle.
|
|
1519
|
+
* This controller is created within another controller and we only need
|
|
1520
|
+
* to know if `unhandledException` listeners handled the request.
|
|
1521
|
+
*/
|
|
1522
|
+
passthrough() {},
|
|
1523
|
+
async respondWith(response) {
|
|
1524
|
+
await handleResponse(response);
|
|
1525
|
+
},
|
|
1526
|
+
async errorWith(reason) {
|
|
1527
|
+
/**
|
|
1528
|
+
* @note Handle the result of the unhandled controller
|
|
1529
|
+
* in the same way as the original request controller.
|
|
1530
|
+
* The exception here is that thrown errors within the
|
|
1531
|
+
* "unhandledException" event do NOT result in another
|
|
1532
|
+
* emit of the same event. They are forwarded as-is.
|
|
1533
|
+
*/
|
|
1534
|
+
await options.controller.errorWith(reason);
|
|
1535
|
+
}
|
|
1536
|
+
});
|
|
1537
|
+
await options.emitter.emitAsPromise(new UnhandledHttpException({
|
|
1538
|
+
initiator: options.initiator,
|
|
1539
|
+
error: resultError,
|
|
1540
|
+
request: options.request,
|
|
1541
|
+
requestId: options.requestId,
|
|
1542
|
+
controller: unhandledExceptionController
|
|
1543
|
+
}));
|
|
1544
|
+
if (unhandledExceptionController.readyState !== RequestController.PENDING) return;
|
|
1545
|
+
}
|
|
1546
|
+
await options.controller.respondWith(createServerErrorResponse(resultError));
|
|
1547
|
+
return;
|
|
1548
|
+
}
|
|
1549
|
+
if (options.controller.readyState === RequestController.PENDING) return await options.controller.passthrough();
|
|
1550
|
+
return options.controller.handled;
|
|
1551
|
+
}
|
|
1552
|
+
//#endregion
|
|
1553
|
+
//#region src/interceptors/http/source.ts
|
|
1554
|
+
const httpLogger = createLogger("http-request");
|
|
1555
|
+
/**
|
|
1556
|
+
* Interceptor for HTTP requests in Node.js.
|
|
1557
|
+
* Routes socket connections through an HTTP parser.
|
|
1558
|
+
*/
|
|
1559
|
+
var NodeHttpRequestSource = class extends Interceptor {
|
|
1560
|
+
static {
|
|
1561
|
+
this.symbol = Symbol.for("node-http-request-source");
|
|
1562
|
+
}
|
|
1563
|
+
predicate() {
|
|
1564
|
+
return true;
|
|
1565
|
+
}
|
|
1566
|
+
setup() {
|
|
1567
|
+
const socketInterceptor = Interceptor.singleton(SocketInterceptor);
|
|
1568
|
+
socketInterceptor.apply(this);
|
|
1569
|
+
this.subscriptions.push(() => {
|
|
1570
|
+
socketInterceptor.dispose(this);
|
|
1571
|
+
});
|
|
1572
|
+
/**
|
|
1573
|
+
* @note Record the raw values provided to Headers set/append
|
|
1574
|
+
* in order to support "IncomingMessage.prototype.rawHeaders".
|
|
1575
|
+
* This is meant for the headers in mocked responses.
|
|
1576
|
+
*/
|
|
1577
|
+
this.subscriptions.push(recordRawFetchHeaders());
|
|
1578
|
+
const controller = new AbortController();
|
|
1579
|
+
this.subscriptions.push(() => controller.abort());
|
|
1580
|
+
socketInterceptor.on("connection", ({ connectionOptions, socket, controller: socketController }) => {
|
|
1581
|
+
let isHttpConnection;
|
|
1582
|
+
let requestParser;
|
|
1583
|
+
let tunnelUrl;
|
|
1584
|
+
let abortPendingRequest;
|
|
1585
|
+
/**
|
|
1586
|
+
* @note Capture the request context of the connection itself.
|
|
1587
|
+
* The socket is created synchronously within the request async
|
|
1588
|
+
* context (e.g. inside the patched `http.request()`), but the
|
|
1589
|
+
* first data may reach the socket from a foreign context (e.g.
|
|
1590
|
+
* a form-data stream piped into the request), where sampling
|
|
1591
|
+
* the request context yields nothing.
|
|
1592
|
+
*/
|
|
1593
|
+
const connectionRequestContext = requestContext.getStore();
|
|
1594
|
+
/**
|
|
1595
|
+
* @note The client destroys the socket synchronously (e.g. Undici
|
|
1596
|
+
* on request abort) but the socket teardown events ("error",
|
|
1597
|
+
* "close") are emitted asynchronously, after the consumer has
|
|
1598
|
+
* already observed the rejected request promise. Hook into the
|
|
1599
|
+
* destroy itself so the pending request is aborted before any
|
|
1600
|
+
* of its listeners can resume.
|
|
1601
|
+
*/
|
|
1602
|
+
const rawSocket = socketController[kRawSocket];
|
|
1603
|
+
const realSocketDestroy = rawSocket._destroy.bind(rawSocket);
|
|
1604
|
+
rawSocket._destroy = (error, callback) => {
|
|
1605
|
+
abortPendingRequest?.();
|
|
1606
|
+
realSocketDestroy(error, callback);
|
|
1607
|
+
};
|
|
1608
|
+
/**
|
|
1609
|
+
* @note Only inspect the first sent packet to determine the protocol.
|
|
1610
|
+
* A single socket cannot be used for different protocols.
|
|
1611
|
+
*/
|
|
1612
|
+
socket.on("data", (chunk) => {
|
|
1613
|
+
if (isHttpConnection === false) return;
|
|
1614
|
+
/**
|
|
1615
|
+
* @note A mocked "CONNECT" request has established a tunnel.
|
|
1616
|
+
* The data that follows belongs to a new exchange addressed to
|
|
1617
|
+
* the tunnel target. The parser stopped at the tunnel boundary
|
|
1618
|
+
* (HTTP upgrade semantics), so tear it down and detect the
|
|
1619
|
+
* tunneled protocol anew, like on a fresh connection.
|
|
1620
|
+
*/
|
|
1621
|
+
if (tunnelUrl && requestParser) {
|
|
1622
|
+
requestParser.free();
|
|
1623
|
+
requestParser = void 0;
|
|
1624
|
+
isHttpConnection = void 0;
|
|
1625
|
+
socketController.reset();
|
|
1626
|
+
}
|
|
1627
|
+
if (requestParser) {
|
|
1628
|
+
requestParser.execute(toBuffer(chunk));
|
|
1629
|
+
return;
|
|
1630
|
+
}
|
|
1631
|
+
const httpMessage = chunk.toString();
|
|
1632
|
+
const httpMethod = httpMessage.split(" ")[0] || "";
|
|
1633
|
+
if (!METHODS.includes(httpMethod.toUpperCase())) {
|
|
1634
|
+
isHttpConnection = false;
|
|
1635
|
+
return;
|
|
1636
|
+
}
|
|
1637
|
+
isHttpConnection = true;
|
|
1638
|
+
const baseUrl = tunnelUrl ?? connectionOptionsToUrl(connectionOptions, socket);
|
|
1639
|
+
httpLogger.verbose("handling http message %o", {
|
|
1640
|
+
httpMessage,
|
|
1641
|
+
httpMethod,
|
|
1642
|
+
baseUrl
|
|
1643
|
+
});
|
|
1644
|
+
const requestContextValue = requestContext.getStore() ?? connectionRequestContext;
|
|
1645
|
+
const initiator = requestContextValue?.initiator || socket;
|
|
1646
|
+
requestParser = new HttpRequestParser({
|
|
1647
|
+
connectionOptions: {
|
|
1648
|
+
method: httpMethod,
|
|
1649
|
+
url: baseUrl
|
|
1650
|
+
},
|
|
1651
|
+
onRequest: async (parsedRequest, requestAbortController) => {
|
|
1652
|
+
const request = requestContextValue?.transformRequest?.(parsedRequest) ?? parsedRequest;
|
|
1653
|
+
/**
|
|
1654
|
+
* @note A subsequent request arriving on a kept-alive socket
|
|
1655
|
+
* that has already been handled (passed through or mocked).
|
|
1656
|
+
* Clients like Undici reuse sockets without emitting the
|
|
1657
|
+
* "free" event, so reset the controller here, at the HTTP
|
|
1658
|
+
* message boundary, to handle the new request from the
|
|
1659
|
+
* pending state again.
|
|
1660
|
+
*/
|
|
1661
|
+
if (socketController["readyState"] !== SocketController.PENDING) socketController.reset();
|
|
1662
|
+
const requestId = createRequestId();
|
|
1663
|
+
const requestLogger = requestContextValue?.logger ?? httpLogger;
|
|
1664
|
+
httpLogger.verbose("received a parsed HTTP request %o", {
|
|
1665
|
+
method: request.method,
|
|
1666
|
+
url: request.url
|
|
1667
|
+
});
|
|
1668
|
+
const requestController = new RequestController(request, {
|
|
1669
|
+
respondWith: async (rawResponse) => {
|
|
1670
|
+
httpLogger.verbose("respondWith() %o", {
|
|
1671
|
+
status: rawResponse.status,
|
|
1672
|
+
statusText: rawResponse.statusText,
|
|
1673
|
+
hasBody: rawResponse.body != null
|
|
1674
|
+
});
|
|
1675
|
+
/**
|
|
1676
|
+
* @note The client may destroy the socket (e.g. on request
|
|
1677
|
+
* abort) moments before a response arrives. A destroyed
|
|
1678
|
+
* socket cannot be claimed and has no one reading it.
|
|
1679
|
+
*/
|
|
1680
|
+
if (socket.destroyed) return;
|
|
1681
|
+
socketController.claim();
|
|
1682
|
+
const response = FetchResponse.from(rawResponse, { url: request.url });
|
|
1683
|
+
/**
|
|
1684
|
+
* @note A successful mocked response to a "CONNECT"
|
|
1685
|
+
* request establishes a tunnel to the requested authority
|
|
1686
|
+
* (e.g. "127.0.0.1:80"). The exchange that follows on this
|
|
1687
|
+
* socket is addressed to that authority, not to the proxy.
|
|
1688
|
+
*/
|
|
1689
|
+
if (request.method === "CONNECT" && response.ok) tunnelUrl = new URL(`http://${request.url}`);
|
|
1690
|
+
/**
|
|
1691
|
+
* @note Clone the response before "respondWith" because it will
|
|
1692
|
+
* consume its body. This way, we can have a readable response copy
|
|
1693
|
+
* for the "response" event below.
|
|
1694
|
+
*/
|
|
1695
|
+
const responseClone = isResponseError(response) ? null : response.clone();
|
|
1696
|
+
const respond = () => {
|
|
1697
|
+
return this.respondWith({
|
|
1698
|
+
socket: socketController[kRawSocket],
|
|
1699
|
+
request: context.request,
|
|
1700
|
+
response
|
|
1701
|
+
});
|
|
1702
|
+
};
|
|
1703
|
+
if (responseClone) await this.emitter.emitAsPromise(new HttpResponseEvent({
|
|
1704
|
+
initiator,
|
|
1705
|
+
requestId,
|
|
1706
|
+
request: context.request,
|
|
1707
|
+
response: responseClone,
|
|
1708
|
+
responseType: "mock"
|
|
1709
|
+
}));
|
|
1710
|
+
if (socket.connecting) socket.once("connect", respond);
|
|
1711
|
+
else
|
|
1712
|
+
/**
|
|
1713
|
+
* @note Reused sockets stay connected between requests and will not
|
|
1714
|
+
* emit "connect" anymore. If that's the case, respond immediately.
|
|
1715
|
+
*/
|
|
1716
|
+
await respond();
|
|
1717
|
+
},
|
|
1718
|
+
errorWith: (reason) => {
|
|
1719
|
+
if (reason instanceof Error) socket.destroy(reason);
|
|
1720
|
+
},
|
|
1721
|
+
passthrough: () => {
|
|
1722
|
+
const realSocket = socketController.passthrough(this.#modifyHttpHeaders(context.request));
|
|
1723
|
+
if (this.emitter.listenerCount("response") > 0) {
|
|
1724
|
+
httpLogger.verbose("found \"response\" listener, corking socket reads");
|
|
1725
|
+
/**
|
|
1726
|
+
* Suspend the delivery of the original response to the client
|
|
1727
|
+
* until the "response" event listeners settle. This guarantees
|
|
1728
|
+
* that the request promise (e.g. `await fetch()`) does not
|
|
1729
|
+
* resolve before the listeners are done. The real socket keeps
|
|
1730
|
+
* emitting data for the response parser meanwhile.
|
|
1731
|
+
*/
|
|
1732
|
+
socketController.corkReads();
|
|
1733
|
+
const responseParser = new HttpResponseParser({ onResponse: async (response) => {
|
|
1734
|
+
httpLogger.verbose("HTTP response parser parsed: %d %s", response.status, response.statusText);
|
|
1735
|
+
if (isResponseError(response)) {
|
|
1736
|
+
httpLogger.verbose("response is an error response, uncorking socket reads...");
|
|
1737
|
+
socketController.uncorkReads();
|
|
1738
|
+
return;
|
|
1739
|
+
}
|
|
1740
|
+
FetchResponse.setUrl(request.url, response);
|
|
1741
|
+
try {
|
|
1742
|
+
httpLogger.verbose("emitting \"response\" event");
|
|
1743
|
+
await this.emitter.emitAsPromise(new HttpResponseEvent({
|
|
1744
|
+
initiator,
|
|
1745
|
+
requestId,
|
|
1746
|
+
request: context.request,
|
|
1747
|
+
response,
|
|
1748
|
+
responseType: "original"
|
|
1749
|
+
}));
|
|
1750
|
+
} finally {
|
|
1751
|
+
httpLogger.verbose("uncorking socket reads");
|
|
1752
|
+
socketController.uncorkReads();
|
|
1753
|
+
/**
|
|
1754
|
+
* @note Informational responses other than
|
|
1755
|
+
* "101 Switching Protocols" are followed by a final
|
|
1756
|
+
* response on the same connection. Keep gating that
|
|
1757
|
+
* final response on the "response" event listeners.
|
|
1758
|
+
*/
|
|
1759
|
+
if (response.status < 200 && response.status !== 101) socketController.corkReads();
|
|
1760
|
+
}
|
|
1761
|
+
} });
|
|
1762
|
+
realSocket.on("data", (chunk) => responseParser.execute(chunk)).on("close", () => responseParser.free());
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
}, {
|
|
1766
|
+
logger: requestLogger,
|
|
1767
|
+
requestId
|
|
1768
|
+
});
|
|
1769
|
+
invariant(socketController["readyState"] === SocketController.PENDING, "CANNOT HANDLE ALREADY HANDLED REQUEST", request.method, request.url, socketController["readyState"]);
|
|
1770
|
+
/**
|
|
1771
|
+
* @note Create a request resolution context.
|
|
1772
|
+
* This is so modifications to the "request" in upstream interceptors
|
|
1773
|
+
* are correctly picked up by the underlying HTTP interceptor.
|
|
1774
|
+
*/
|
|
1775
|
+
const context = {
|
|
1776
|
+
initiator,
|
|
1777
|
+
requestId,
|
|
1778
|
+
request,
|
|
1779
|
+
controller: requestController,
|
|
1780
|
+
emitter: this.emitter,
|
|
1781
|
+
logger: requestLogger
|
|
1782
|
+
};
|
|
1783
|
+
/**
|
|
1784
|
+
* @note The client destroying the socket while the request
|
|
1785
|
+
* is still pending means the request was aborted (e.g. via
|
|
1786
|
+
* `AbortController`). Abort the parsed request so its
|
|
1787
|
+
* handling settles and late interactions with the request
|
|
1788
|
+
* controller become controlled errors.
|
|
1789
|
+
*/
|
|
1790
|
+
abortPendingRequest = () => {
|
|
1791
|
+
if (requestController.readyState === RequestController.PENDING) requestAbortController.abort();
|
|
1792
|
+
};
|
|
1793
|
+
try {
|
|
1794
|
+
await handleRequest(context);
|
|
1795
|
+
} finally {
|
|
1796
|
+
abortPendingRequest = void 0;
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
});
|
|
1800
|
+
requestParser.execute(toBuffer(chunk));
|
|
1801
|
+
});
|
|
1802
|
+
socket.on("close", () => requestParser?.free());
|
|
1803
|
+
}, { signal: controller.signal });
|
|
1804
|
+
}
|
|
1805
|
+
async respondWith(args) {
|
|
1806
|
+
const { socket, request, response } = args;
|
|
1807
|
+
if (socket.destroyed) return;
|
|
1808
|
+
if (isResponseError(response)) {
|
|
1809
|
+
/**
|
|
1810
|
+
* @note Reference the error response on the socket error so the
|
|
1811
|
+
* client-side interceptors (e.g. fetch) can surface it to the
|
|
1812
|
+
* consumer as the reason behind the failed request. Keep the
|
|
1813
|
+
* reference non-enumerable so the error remains observably
|
|
1814
|
+
* identical for the clients that expose it as-is.
|
|
1815
|
+
*/
|
|
1816
|
+
socket.destroy(Object.defineProperty(/* @__PURE__ */ new TypeError("Network error"), kErrorResponse, {
|
|
1817
|
+
value: response,
|
|
1818
|
+
enumerable: false
|
|
1819
|
+
}));
|
|
1820
|
+
return;
|
|
1821
|
+
}
|
|
1822
|
+
invariant(!socket.connecting, "Failed to mock a response for \"%s %s\": socket has not connected", request.method, request.url);
|
|
1823
|
+
/**
|
|
1824
|
+
* Use native server response handling in Node.js.
|
|
1825
|
+
* @see https://github.com/nodejs/node/blob/13eb80f3b718452213e0fc449702aefbbfe4110f/lib/_http_server.js#L202
|
|
1826
|
+
*/
|
|
1827
|
+
const incomingMessage = new IncomingMessage(socket);
|
|
1828
|
+
/**
|
|
1829
|
+
* @note Describe the request method so the response body is
|
|
1830
|
+
* handled appropriately (e.g. "HEAD" responses must not write
|
|
1831
|
+
* a body). The HTTP version is deliberately left unset: with it,
|
|
1832
|
+
* `ServerResponse` frames bodies of unknown length as chunked,
|
|
1833
|
+
* polluting the mocked response headers with "Transfer-Encoding"
|
|
1834
|
+
* the mock never specified.
|
|
1835
|
+
*/
|
|
1836
|
+
incomingMessage.method = request.method;
|
|
1837
|
+
const serverResponse = new ServerResponse(incomingMessage);
|
|
1838
|
+
const responseSocket = new net.Socket();
|
|
1839
|
+
responseSocket._writeGeneric = (writev, data, encoding, callback) => {
|
|
1840
|
+
unwrapPendingData(data, (chunk, encoding) => {
|
|
1841
|
+
socket.push(toBuffer(chunk), encoding);
|
|
1842
|
+
});
|
|
1843
|
+
callback?.();
|
|
1844
|
+
};
|
|
1845
|
+
responseSocket._destroy = (error, callback) => {
|
|
1846
|
+
/**
|
|
1847
|
+
* Only destroy the socket on stream errors.
|
|
1848
|
+
* On a clean end, the socket is already signaled via `socket.push(null)`
|
|
1849
|
+
* in the main response flow. Destroying it here prematurely would prevent
|
|
1850
|
+
* the client from processing the response (e.g. calling `response.destroy()`).
|
|
1851
|
+
* @see https://github.com/mswjs/interceptors/issues/738
|
|
1852
|
+
*/
|
|
1853
|
+
if (error) socket.destroy();
|
|
1854
|
+
callback(null);
|
|
1855
|
+
};
|
|
1856
|
+
responseSocket.on("drain", () => serverResponse.emit("drain"));
|
|
1857
|
+
serverResponse.assignSocket(responseSocket);
|
|
1858
|
+
serverResponse.removeHeader("connection");
|
|
1859
|
+
serverResponse.removeHeader("date");
|
|
1860
|
+
const rawResponseHeaders = getRawFetchHeaders(response.headers);
|
|
1861
|
+
serverResponse.writeHead(response.status, response.statusText || STATUS_CODES[response.status], rawResponseHeaders);
|
|
1862
|
+
/**
|
|
1863
|
+
* @note Override the socket's `_destroy` before writing the response body.
|
|
1864
|
+
* The underlying TCP handle (from `socket.connect()`) makes `_destroy` async
|
|
1865
|
+
* (`_handle.close()` callback), which delays the 'error' event. Since the real
|
|
1866
|
+
* TCP connection is irrelevant for mocked responses, take the synchronous path
|
|
1867
|
+
* so that user-initiated `response.destroy(error)` emits the error promptly.
|
|
1868
|
+
* This must happen before `serverResponse.end()` because the HTTP parser may
|
|
1869
|
+
* fire the 'response' event synchronously during `socket.push()`.
|
|
1870
|
+
*/
|
|
1871
|
+
socket._destroy = function(error, callback) {
|
|
1872
|
+
if (error)
|
|
1873
|
+
/**
|
|
1874
|
+
* Emit the error event as a microtask instead of relying on the default
|
|
1875
|
+
* `process.nextTick(emitErrorNT)` from `callback(error)`. This is necessary
|
|
1876
|
+
* because `respondWith` runs inside a microtask (from `await reader.read()`).
|
|
1877
|
+
* A resolved promise continuation (from toWebResponse) is queued as
|
|
1878
|
+
* another microtask during the same phase. Since microtasks are drained before
|
|
1879
|
+
* nextTick, the test's `await` would resolve before the error event fires.
|
|
1880
|
+
* Using `queueMicrotask` ensures the error event is emitted within the current
|
|
1881
|
+
* microtask phase, before other queued microtasks.
|
|
1882
|
+
*/
|
|
1883
|
+
queueMicrotask(() => this.emit("error", error));
|
|
1884
|
+
callback(null);
|
|
1885
|
+
/**
|
|
1886
|
+
* @note `net.Socket` is constructed with `emitClose: false`, so Node's
|
|
1887
|
+
* stream destroy machinery does not emit `'close'` automatically; the
|
|
1888
|
+
* stock `net.Socket._destroy` only emits it via `_handle.close()`.
|
|
1889
|
+
* Since this override replaces `_destroy`, emit `'close'` here so the
|
|
1890
|
+
* mocked socket completes its lifecycle (otherwise consumers waiting
|
|
1891
|
+
* on `'close'`, like `http.ClientRequest`, hang).
|
|
1892
|
+
*/
|
|
1893
|
+
process.nextTick(() => this.emit("close", error != null));
|
|
1894
|
+
};
|
|
1895
|
+
if (response.body) {
|
|
1896
|
+
const reader = response.body.getReader();
|
|
1897
|
+
try {
|
|
1898
|
+
while (true) {
|
|
1899
|
+
const { done, value } = await reader.read();
|
|
1900
|
+
if (done) {
|
|
1901
|
+
serverResponse.end();
|
|
1902
|
+
break;
|
|
1903
|
+
}
|
|
1904
|
+
if (!serverResponse.write(value)) await new Promise((resolve) => {
|
|
1905
|
+
serverResponse.once("drain", resolve);
|
|
1906
|
+
});
|
|
1907
|
+
}
|
|
1908
|
+
} catch {
|
|
1909
|
+
/**
|
|
1910
|
+
* @note Delay the socket destruction to allow the event loop
|
|
1911
|
+
* to flush already-pushed response data (headers + body chunks)
|
|
1912
|
+
* through the HTTP parser. Without this, the socket is destroyed
|
|
1913
|
+
* on the same tick as `socket.push(data)` and the client never
|
|
1914
|
+
* reads the response.
|
|
1915
|
+
*/
|
|
1916
|
+
await new Promise((resolve) => process.nextTick(resolve));
|
|
1917
|
+
socket.destroy();
|
|
1918
|
+
return;
|
|
1919
|
+
}
|
|
1920
|
+
} else serverResponse.end();
|
|
1921
|
+
/**
|
|
1922
|
+
* @note Self-delimiting responses (chunked, explicit "Content-Length",
|
|
1923
|
+
* or bodiless by definition) must NOT signal the end-of-stream.
|
|
1924
|
+
* The client parser completes them from their framing alone, and
|
|
1925
|
+
* ending the socket would kill the kept-alive connection that
|
|
1926
|
+
* agents pool and reuse for subsequent requests.
|
|
1927
|
+
*/
|
|
1928
|
+
const isSelfDelimitingResponse = request.method === "HEAD" || response.headers.has("content-length") || response.headers.has("transfer-encoding") || !FetchResponse.isResponseWithBody(response.status);
|
|
1929
|
+
if (request.method !== "CONNECT" && !isSelfDelimitingResponse) {
|
|
1930
|
+
/**
|
|
1931
|
+
* @note Defer the end-of-stream signal so the HTTP parser has a chance
|
|
1932
|
+
* to process already-pushed response data and fire the 'response' event
|
|
1933
|
+
* before the socket is ended. Without this, the parser marks the response
|
|
1934
|
+
* as "complete" before the client can interact with it (e.g. `response.destroy()`).
|
|
1935
|
+
*/
|
|
1936
|
+
await new Promise((resolve) => process.nextTick(resolve));
|
|
1937
|
+
socket.push(null);
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
#modifyHttpHeaders(request) {
|
|
1941
|
+
const transformRequestMessage = (httpMessage, encoding) => {
|
|
1942
|
+
/**
|
|
1943
|
+
* @note Socket can write a buffer (e.g. uploaded file) even before
|
|
1944
|
+
* it writes the HTTP message. Bypass those cases.
|
|
1945
|
+
*/
|
|
1946
|
+
if (encoding === "buffer") return httpMessage;
|
|
1947
|
+
const parts = httpMessage.toString(encoding).split("\r\n");
|
|
1948
|
+
const headersEndIndex = parts.findIndex((field) => field === "");
|
|
1949
|
+
const httpMessageHeaderPairs = parts.slice(1, headersEndIndex);
|
|
1950
|
+
const httpMessageRawHeaders = httpMessageHeaderPairs.map((line) => {
|
|
1951
|
+
const separatorIndex = line.indexOf(": ");
|
|
1952
|
+
return [line.slice(0, separatorIndex), line.slice(separatorIndex + 2)];
|
|
1953
|
+
});
|
|
1954
|
+
const requestRawHeaders = getRawFetchHeaders(request.headers);
|
|
1955
|
+
if (httpMessageRawHeaders.length === requestRawHeaders.length && httpMessageRawHeaders.every((tuple, index) => {
|
|
1956
|
+
const requestTuple = requestRawHeaders[index];
|
|
1957
|
+
return tuple[0] === requestTuple[0] && tuple[1] === requestTuple[1];
|
|
1958
|
+
})) return httpMessage;
|
|
1959
|
+
const httpMessageHeaders = FetchResponse.parseRawHeaders(httpMessageHeaderPairs.flatMap((header) => header.split(": ")));
|
|
1960
|
+
const visitedHeaders = /* @__PURE__ */ new Set();
|
|
1961
|
+
for (const [headerName] of requestRawHeaders) {
|
|
1962
|
+
const normalizedHeaderName = headerName.toLowerCase();
|
|
1963
|
+
if (visitedHeaders.has(normalizedHeaderName)) continue;
|
|
1964
|
+
visitedHeaders.add(normalizedHeaderName);
|
|
1965
|
+
/**
|
|
1966
|
+
* @note Forbidden Fetch headers (e.g. Host, Origin, Connection)
|
|
1967
|
+
* are stripped from `request.headers` but remain in the raw
|
|
1968
|
+
* headers list. Skip them so the original values from the
|
|
1969
|
+
* outgoing HTTP message are preserved.
|
|
1970
|
+
*/
|
|
1971
|
+
const headerValue = request.headers.get(headerName);
|
|
1972
|
+
if (headerValue === null) continue;
|
|
1973
|
+
httpMessageHeaders.set(headerName, headerValue);
|
|
1974
|
+
}
|
|
1975
|
+
visitedHeaders.clear();
|
|
1976
|
+
const httpMessageHeadersString = Array.from(httpMessageHeaders).map(([name, value]) => `${name}: ${value}`).join("\r\n");
|
|
1977
|
+
parts.splice(1, headersEndIndex - 1, httpMessageHeadersString);
|
|
1978
|
+
return parts.join("\r\n");
|
|
1979
|
+
};
|
|
1980
|
+
return (pendingData, encoding, callback) => {
|
|
1981
|
+
if (Array.isArray(pendingData)) pendingData[0].chunk = transformRequestMessage(pendingData[0].chunk, pendingData[0].encoding);
|
|
1982
|
+
else pendingData = transformRequestMessage(pendingData, encoding);
|
|
1983
|
+
callback(pendingData);
|
|
1984
|
+
};
|
|
1985
|
+
}
|
|
1986
|
+
};
|
|
1987
|
+
//#endregion
|
|
1988
|
+
export { requestContext as a, forwardHttpEvents as i, handleRequest as n, runInRequestContext as o, HttpResponseEvent as r, NodeHttpRequestSource as t };
|
|
1989
|
+
|
|
1990
|
+
//# sourceMappingURL=source-lP0yyEtA.js.map
|