@xrkseek/harness 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node_modules/@xrkseek/core-tools/dist/index.d.ts +1 -0
- package/node_modules/@xrkseek/core-tools/dist/index.d.ts.map +1 -1
- package/node_modules/@xrkseek/core-tools/dist/index.js +1 -0
- package/node_modules/@xrkseek/core-tools/dist/index.js.map +1 -1
- package/node_modules/@xrkseek/core-tools/dist/settings-tools.d.ts +37 -0
- package/node_modules/@xrkseek/core-tools/dist/settings-tools.d.ts.map +1 -0
- package/node_modules/@xrkseek/core-tools/dist/settings-tools.js +161 -0
- package/node_modules/@xrkseek/core-tools/dist/settings-tools.js.map +1 -0
- package/node_modules/@xrkseek/core-tools/dist/std-tools.d.ts +4 -1
- package/node_modules/@xrkseek/core-tools/dist/std-tools.d.ts.map +1 -1
- package/node_modules/@xrkseek/core-tools/dist/std-tools.js +6 -1
- package/node_modules/@xrkseek/core-tools/dist/std-tools.js.map +1 -1
- package/node_modules/@xrkseek/exec-fs/dist/read-image.d.ts.map +1 -1
- package/node_modules/@xrkseek/exec-fs/dist/read-image.js +20 -2
- package/node_modules/@xrkseek/exec-fs/dist/read-image.js.map +1 -1
- package/node_modules/@xrkseek/preset-harness/dist/tsconfig.tsbuildinfo +1 -1
- package/node_modules/@xrkseek/preset-minimal/dist/tsconfig.tsbuildinfo +1 -1
- package/node_modules/@xrkseek/preset-server/dist/tsconfig.tsbuildinfo +1 -1
- package/node_modules/@xrkseek/server-config/dist/index.d.ts +2 -2
- package/node_modules/@xrkseek/server-config/dist/index.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-config/dist/index.js +3 -2
- package/node_modules/@xrkseek/server-config/dist/index.js.map +1 -1
- package/node_modules/@xrkseek/server-config/dist/mcp-servers.d.ts +12 -1
- package/node_modules/@xrkseek/server-config/dist/mcp-servers.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-config/dist/mcp-servers.js +56 -1
- package/node_modules/@xrkseek/server-config/dist/mcp-servers.js.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/adapt/wire-event.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/adapt/wire-event.js +17 -1
- package/node_modules/@xrkseek/server-face/dist/adapt/wire-event.js.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/context.d.ts +8 -0
- package/node_modules/@xrkseek/server-face/dist/context.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/dispatch.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/dispatch.js +4 -1
- package/node_modules/@xrkseek/server-face/dist/dispatch.js.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/face-schema.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/face-schema.js +2 -0
- package/node_modules/@xrkseek/server-face/dist/face-schema.js.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/handlers/host.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/handlers/host.js +2 -0
- package/node_modules/@xrkseek/server-face/dist/handlers/host.js.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/handlers/remotes.d.ts +6 -0
- package/node_modules/@xrkseek/server-face/dist/handlers/remotes.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/handlers/remotes.js +89 -1
- package/node_modules/@xrkseek/server-face/dist/handlers/remotes.js.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/handlers/session.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/handlers/session.js +11 -112
- package/node_modules/@xrkseek/server-face/dist/handlers/session.js.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/index.d.ts +2 -1
- package/node_modules/@xrkseek/server-face/dist/index.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/index.js +2 -1
- package/node_modules/@xrkseek/server-face/dist/index.js.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/plugin-inventory.d.ts +22 -3
- package/node_modules/@xrkseek/server-face/dist/plugin-inventory.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/plugin-inventory.js +117 -6
- package/node_modules/@xrkseek/server-face/dist/plugin-inventory.js.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/presets-catalog.d.ts +9 -7
- package/node_modules/@xrkseek/server-face/dist/presets-catalog.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/presets-catalog.js +7 -17
- package/node_modules/@xrkseek/server-face/dist/presets-catalog.js.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/runtime.d.ts +5 -0
- package/node_modules/@xrkseek/server-face/dist/runtime.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/runtime.js +5 -0
- package/node_modules/@xrkseek/server-face/dist/runtime.js.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/select-session-model.d.ts +15 -0
- package/node_modules/@xrkseek/server-face/dist/select-session-model.d.ts.map +1 -0
- package/node_modules/@xrkseek/server-face/dist/select-session-model.js +117 -0
- package/node_modules/@xrkseek/server-face/dist/select-session-model.js.map +1 -0
- package/node_modules/@xrkseek/server-face/dist/settings-agent-tools.d.ts +7 -0
- package/node_modules/@xrkseek/server-face/dist/settings-agent-tools.d.ts.map +1 -0
- package/node_modules/@xrkseek/server-face/dist/settings-agent-tools.js +91 -0
- package/node_modules/@xrkseek/server-face/dist/settings-agent-tools.js.map +1 -0
- package/node_modules/@xrkseek/server-face/dist/settings-credentials.d.ts +7 -1
- package/node_modules/@xrkseek/server-face/dist/settings-credentials.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/settings-credentials.js +85 -26
- package/node_modules/@xrkseek/server-face/dist/settings-credentials.js.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/slash.d.ts +4 -3
- package/node_modules/@xrkseek/server-face/dist/slash.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-face/dist/slash.js +191 -20
- package/node_modules/@xrkseek/server-face/dist/slash.js.map +1 -1
- package/node_modules/@xrkseek/server-host/dist/http-proxy.d.ts +11 -0
- package/node_modules/@xrkseek/server-host/dist/http-proxy.d.ts.map +1 -0
- package/node_modules/@xrkseek/server-host/dist/http-proxy.js +42 -0
- package/node_modules/@xrkseek/server-host/dist/http-proxy.js.map +1 -0
- package/node_modules/@xrkseek/server-host/dist/index.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-host/dist/index.js +17 -1
- package/node_modules/@xrkseek/server-host/dist/index.js.map +1 -1
- package/node_modules/@xrkseek/server-host/dist/mcp-wire.d.ts +4 -3
- package/node_modules/@xrkseek/server-host/dist/mcp-wire.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-host/dist/mcp-wire.js +14 -10
- package/node_modules/@xrkseek/server-host/dist/mcp-wire.js.map +1 -1
- package/node_modules/@xrkseek/server-host/package.json +57 -56
- package/node_modules/@xrkseek/server-http/dist/index.d.ts +1 -0
- package/node_modules/@xrkseek/server-http/dist/index.d.ts.map +1 -1
- package/node_modules/@xrkseek/server-http/dist/index.js +1 -0
- package/node_modules/@xrkseek/server-http/dist/index.js.map +1 -1
- package/node_modules/undici/LICENSE +21 -0
- package/node_modules/undici/README.md +741 -0
- package/node_modules/undici/docs/docs/api/Agent.md +84 -0
- package/node_modules/undici/docs/docs/api/BalancedPool.md +99 -0
- package/node_modules/undici/docs/docs/api/CacheStorage.md +30 -0
- package/node_modules/undici/docs/docs/api/CacheStore.md +164 -0
- package/node_modules/undici/docs/docs/api/Client.md +288 -0
- package/node_modules/undici/docs/docs/api/ClientStats.md +27 -0
- package/node_modules/undici/docs/docs/api/Connector.md +115 -0
- package/node_modules/undici/docs/docs/api/ContentType.md +57 -0
- package/node_modules/undici/docs/docs/api/Cookies.md +128 -0
- package/node_modules/undici/docs/docs/api/Debug.md +62 -0
- package/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +315 -0
- package/node_modules/undici/docs/docs/api/Dispatcher.md +1392 -0
- package/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +159 -0
- package/node_modules/undici/docs/docs/api/Errors.md +49 -0
- package/node_modules/undici/docs/docs/api/EventSource.md +45 -0
- package/node_modules/undici/docs/docs/api/Fetch.md +60 -0
- package/node_modules/undici/docs/docs/api/GlobalInstallation.md +139 -0
- package/node_modules/undici/docs/docs/api/H2CClient.md +263 -0
- package/node_modules/undici/docs/docs/api/MockAgent.md +603 -0
- package/node_modules/undici/docs/docs/api/MockCallHistory.md +197 -0
- package/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +43 -0
- package/node_modules/undici/docs/docs/api/MockClient.md +81 -0
- package/node_modules/undici/docs/docs/api/MockErrors.md +12 -0
- package/node_modules/undici/docs/docs/api/MockPool.md +555 -0
- package/node_modules/undici/docs/docs/api/Pool.md +84 -0
- package/node_modules/undici/docs/docs/api/PoolStats.md +35 -0
- package/node_modules/undici/docs/docs/api/ProxyAgent.md +229 -0
- package/node_modules/undici/docs/docs/api/RedirectHandler.md +93 -0
- package/node_modules/undici/docs/docs/api/RetryAgent.md +50 -0
- package/node_modules/undici/docs/docs/api/RetryHandler.md +118 -0
- package/node_modules/undici/docs/docs/api/RoundRobinPool.md +145 -0
- package/node_modules/undici/docs/docs/api/SnapshotAgent.md +616 -0
- package/node_modules/undici/docs/docs/api/Socks5ProxyAgent.md +275 -0
- package/node_modules/undici/docs/docs/api/Util.md +25 -0
- package/node_modules/undici/docs/docs/api/WebSocket.md +141 -0
- package/node_modules/undici/docs/docs/api/api-lifecycle.md +91 -0
- package/node_modules/undici/docs/docs/best-practices/client-certificate.md +64 -0
- package/node_modules/undici/docs/docs/best-practices/crawling.md +58 -0
- package/node_modules/undici/docs/docs/best-practices/mocking-request.md +190 -0
- package/node_modules/undici/docs/docs/best-practices/proxy.md +127 -0
- package/node_modules/undici/docs/docs/best-practices/undici-vs-builtin-fetch.md +224 -0
- package/node_modules/undici/docs/docs/best-practices/writing-tests.md +63 -0
- package/node_modules/undici/index-fetch.js +65 -0
- package/node_modules/undici/index.d.ts +3 -0
- package/node_modules/undici/index.js +234 -0
- package/node_modules/undici/lib/api/abort-signal.js +59 -0
- package/node_modules/undici/lib/api/api-connect.js +110 -0
- package/node_modules/undici/lib/api/api-pipeline.js +252 -0
- package/node_modules/undici/lib/api/api-request.js +214 -0
- package/node_modules/undici/lib/api/api-stream.js +209 -0
- package/node_modules/undici/lib/api/api-upgrade.js +111 -0
- package/node_modules/undici/lib/api/index.js +7 -0
- package/node_modules/undici/lib/api/readable.js +580 -0
- package/node_modules/undici/lib/cache/memory-cache-store.js +279 -0
- package/node_modules/undici/lib/cache/sqlite-cache-store.js +467 -0
- package/node_modules/undici/lib/core/connect.js +153 -0
- package/node_modules/undici/lib/core/constants.js +143 -0
- package/node_modules/undici/lib/core/diagnostics.js +227 -0
- package/node_modules/undici/lib/core/errors.js +477 -0
- package/node_modules/undici/lib/core/request.js +464 -0
- package/node_modules/undici/lib/core/socks5-client.js +422 -0
- package/node_modules/undici/lib/core/socks5-utils.js +212 -0
- package/node_modules/undici/lib/core/symbols.js +75 -0
- package/node_modules/undici/lib/core/tree.js +160 -0
- package/node_modules/undici/lib/core/util.js +992 -0
- package/node_modules/undici/lib/dispatcher/agent.js +158 -0
- package/node_modules/undici/lib/dispatcher/balanced-pool.js +221 -0
- package/node_modules/undici/lib/dispatcher/client-h1.js +1739 -0
- package/node_modules/undici/lib/dispatcher/client-h2.js +1051 -0
- package/node_modules/undici/lib/dispatcher/client.js +668 -0
- package/node_modules/undici/lib/dispatcher/dispatcher-base.js +184 -0
- package/node_modules/undici/lib/dispatcher/dispatcher.js +48 -0
- package/node_modules/undici/lib/dispatcher/env-http-proxy-agent.js +146 -0
- package/node_modules/undici/lib/dispatcher/fixed-queue.js +135 -0
- package/node_modules/undici/lib/dispatcher/h2c-client.js +51 -0
- package/node_modules/undici/lib/dispatcher/pool-base.js +214 -0
- package/node_modules/undici/lib/dispatcher/pool.js +118 -0
- package/node_modules/undici/lib/dispatcher/proxy-agent.js +319 -0
- package/node_modules/undici/lib/dispatcher/retry-agent.js +35 -0
- package/node_modules/undici/lib/dispatcher/round-robin-pool.js +137 -0
- package/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +260 -0
- package/node_modules/undici/lib/encoding/index.js +33 -0
- package/node_modules/undici/lib/global.js +59 -0
- package/node_modules/undici/lib/handler/cache-handler.js +776 -0
- package/node_modules/undici/lib/handler/cache-revalidation-handler.js +124 -0
- package/node_modules/undici/lib/handler/decorator-handler.js +67 -0
- package/node_modules/undici/lib/handler/deduplication-handler.js +460 -0
- package/node_modules/undici/lib/handler/redirect-handler.js +238 -0
- package/node_modules/undici/lib/handler/retry-handler.js +453 -0
- package/node_modules/undici/lib/handler/unwrap-handler.js +106 -0
- package/node_modules/undici/lib/handler/wrap-handler.js +105 -0
- package/node_modules/undici/lib/interceptor/cache.js +621 -0
- package/node_modules/undici/lib/interceptor/decompress.js +391 -0
- package/node_modules/undici/lib/interceptor/deduplicate.js +117 -0
- package/node_modules/undici/lib/interceptor/dns.js +571 -0
- package/node_modules/undici/lib/interceptor/dump.js +99 -0
- package/node_modules/undici/lib/interceptor/redirect.js +21 -0
- package/node_modules/undici/lib/interceptor/response-error.js +95 -0
- package/node_modules/undici/lib/interceptor/retry.js +19 -0
- package/node_modules/undici/lib/llhttp/.gitkeep +0 -0
- package/node_modules/undici/lib/llhttp/constants.d.ts +195 -0
- package/node_modules/undici/lib/llhttp/constants.js +531 -0
- package/node_modules/undici/lib/llhttp/llhttp-wasm.js +15 -0
- package/node_modules/undici/lib/llhttp/llhttp_simd-wasm.js +15 -0
- package/node_modules/undici/lib/llhttp/utils.d.ts +2 -0
- package/node_modules/undici/lib/llhttp/utils.js +12 -0
- package/node_modules/undici/lib/mock/mock-agent.js +232 -0
- package/node_modules/undici/lib/mock/mock-call-history.js +248 -0
- package/node_modules/undici/lib/mock/mock-client.js +68 -0
- package/node_modules/undici/lib/mock/mock-errors.js +29 -0
- package/node_modules/undici/lib/mock/mock-interceptor.js +209 -0
- package/node_modules/undici/lib/mock/mock-pool.js +68 -0
- package/node_modules/undici/lib/mock/mock-symbols.js +32 -0
- package/node_modules/undici/lib/mock/mock-utils.js +486 -0
- package/node_modules/undici/lib/mock/pending-interceptors-formatter.js +43 -0
- package/node_modules/undici/lib/mock/snapshot-agent.js +353 -0
- package/node_modules/undici/lib/mock/snapshot-recorder.js +588 -0
- package/node_modules/undici/lib/mock/snapshot-utils.js +158 -0
- package/node_modules/undici/lib/util/cache.js +676 -0
- package/node_modules/undici/lib/util/date.js +670 -0
- package/node_modules/undici/lib/util/promise.js +28 -0
- package/node_modules/undici/lib/util/runtime-features.js +124 -0
- package/node_modules/undici/lib/util/stats.js +32 -0
- package/node_modules/undici/lib/util/timers.js +425 -0
- package/node_modules/undici/lib/web/cache/cache.js +864 -0
- package/node_modules/undici/lib/web/cache/cachestorage.js +152 -0
- package/node_modules/undici/lib/web/cache/util.js +45 -0
- package/node_modules/undici/lib/web/cookies/constants.js +12 -0
- package/node_modules/undici/lib/web/cookies/index.js +199 -0
- package/node_modules/undici/lib/web/cookies/parse.js +314 -0
- package/node_modules/undici/lib/web/cookies/util.js +352 -0
- package/node_modules/undici/lib/web/eventsource/eventsource-stream.js +494 -0
- package/node_modules/undici/lib/web/eventsource/eventsource.js +501 -0
- package/node_modules/undici/lib/web/eventsource/util.js +29 -0
- package/node_modules/undici/lib/web/fetch/LICENSE +21 -0
- package/node_modules/undici/lib/web/fetch/body.js +509 -0
- package/node_modules/undici/lib/web/fetch/constants.js +131 -0
- package/node_modules/undici/lib/web/fetch/data-url.js +596 -0
- package/node_modules/undici/lib/web/fetch/formdata-parser.js +586 -0
- package/node_modules/undici/lib/web/fetch/formdata.js +259 -0
- package/node_modules/undici/lib/web/fetch/global.js +40 -0
- package/node_modules/undici/lib/web/fetch/headers.js +719 -0
- package/node_modules/undici/lib/web/fetch/index.js +2413 -0
- package/node_modules/undici/lib/web/fetch/request.js +1115 -0
- package/node_modules/undici/lib/web/fetch/response.js +641 -0
- package/node_modules/undici/lib/web/fetch/util.js +1522 -0
- package/node_modules/undici/lib/web/infra/index.js +229 -0
- package/node_modules/undici/lib/web/subresource-integrity/Readme.md +9 -0
- package/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +307 -0
- package/node_modules/undici/lib/web/webidl/index.js +1006 -0
- package/node_modules/undici/lib/web/websocket/connection.js +329 -0
- package/node_modules/undici/lib/web/websocket/constants.js +126 -0
- package/node_modules/undici/lib/web/websocket/events.js +331 -0
- package/node_modules/undici/lib/web/websocket/frame.js +133 -0
- package/node_modules/undici/lib/web/websocket/permessage-deflate.js +105 -0
- package/node_modules/undici/lib/web/websocket/receiver.js +507 -0
- package/node_modules/undici/lib/web/websocket/sender.js +109 -0
- package/node_modules/undici/lib/web/websocket/stream/websocketerror.js +104 -0
- package/node_modules/undici/lib/web/websocket/stream/websocketstream.js +496 -0
- package/node_modules/undici/lib/web/websocket/util.js +347 -0
- package/node_modules/undici/lib/web/websocket/websocket.js +758 -0
- package/node_modules/undici/package.json +152 -0
- package/node_modules/undici/scripts/strip-comments.js +10 -0
- package/node_modules/undici/types/README.md +6 -0
- package/node_modules/undici/types/agent.d.ts +32 -0
- package/node_modules/undici/types/api.d.ts +43 -0
- package/node_modules/undici/types/balanced-pool.d.ts +30 -0
- package/node_modules/undici/types/cache-interceptor.d.ts +179 -0
- package/node_modules/undici/types/cache.d.ts +36 -0
- package/node_modules/undici/types/client-stats.d.ts +15 -0
- package/node_modules/undici/types/client.d.ts +139 -0
- package/node_modules/undici/types/connector.d.ts +36 -0
- package/node_modules/undici/types/content-type.d.ts +21 -0
- package/node_modules/undici/types/cookies.d.ts +30 -0
- package/node_modules/undici/types/diagnostics-channel.d.ts +74 -0
- package/node_modules/undici/types/dispatcher.d.ts +275 -0
- package/node_modules/undici/types/env-http-proxy-agent.d.ts +22 -0
- package/node_modules/undici/types/errors.d.ts +177 -0
- package/node_modules/undici/types/eventsource.d.ts +66 -0
- package/node_modules/undici/types/fetch.d.ts +231 -0
- package/node_modules/undici/types/formdata.d.ts +114 -0
- package/node_modules/undici/types/global-dispatcher.d.ts +9 -0
- package/node_modules/undici/types/global-origin.d.ts +7 -0
- package/node_modules/undici/types/h2c-client.d.ts +73 -0
- package/node_modules/undici/types/handlers.d.ts +14 -0
- package/node_modules/undici/types/header.d.ts +160 -0
- package/node_modules/undici/types/index.d.ts +91 -0
- package/node_modules/undici/types/interceptors.d.ts +82 -0
- package/node_modules/undici/types/mock-agent.d.ts +68 -0
- package/node_modules/undici/types/mock-call-history.d.ts +111 -0
- package/node_modules/undici/types/mock-client.d.ts +27 -0
- package/node_modules/undici/types/mock-errors.d.ts +12 -0
- package/node_modules/undici/types/mock-interceptor.d.ts +94 -0
- package/node_modules/undici/types/mock-pool.d.ts +27 -0
- package/node_modules/undici/types/patch.d.ts +29 -0
- package/node_modules/undici/types/pool-stats.d.ts +19 -0
- package/node_modules/undici/types/pool.d.ts +41 -0
- package/node_modules/undici/types/proxy-agent.d.ts +29 -0
- package/node_modules/undici/types/readable.d.ts +68 -0
- package/node_modules/undici/types/retry-agent.d.ts +8 -0
- package/node_modules/undici/types/retry-handler.d.ts +125 -0
- package/node_modules/undici/types/round-robin-pool.d.ts +41 -0
- package/node_modules/undici/types/snapshot-agent.d.ts +109 -0
- package/node_modules/undici/types/socks5-proxy-agent.d.ts +25 -0
- package/node_modules/undici/types/util.d.ts +18 -0
- package/node_modules/undici/types/utility.d.ts +7 -0
- package/node_modules/undici/types/webidl.d.ts +347 -0
- package/node_modules/undici/types/websocket.d.ts +188 -0
- package/package.json +3 -1
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const assert = require('node:assert')
|
|
4
|
+
const { Readable } = require('node:stream')
|
|
5
|
+
const util = require('../core/util')
|
|
6
|
+
const CacheHandler = require('../handler/cache-handler')
|
|
7
|
+
const MemoryCacheStore = require('../cache/memory-cache-store')
|
|
8
|
+
const CacheRevalidationHandler = require('../handler/cache-revalidation-handler')
|
|
9
|
+
const { assertCacheStore, assertCacheMethods, makeCacheKey, normalizeHeaders, parseCacheControlHeader, isInvalidOrWildcardVaryHeader } = require('../util/cache.js')
|
|
10
|
+
const { AbortError } = require('../core/errors.js')
|
|
11
|
+
const { parseHttpDate } = require('../util/date.js')
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {(string | RegExp)[] | undefined} origins
|
|
15
|
+
* @param {string} name
|
|
16
|
+
*/
|
|
17
|
+
function assertCacheOrigins (origins, name) {
|
|
18
|
+
if (origins === undefined) return
|
|
19
|
+
if (!Array.isArray(origins)) {
|
|
20
|
+
throw new TypeError(`expected ${name} to be an array or undefined, got ${typeof origins}`)
|
|
21
|
+
}
|
|
22
|
+
for (let i = 0; i < origins.length; i++) {
|
|
23
|
+
const origin = origins[i]
|
|
24
|
+
if (typeof origin !== 'string' && !(origin instanceof RegExp)) {
|
|
25
|
+
throw new TypeError(`expected ${name}[${i}] to be a string or RegExp, got ${typeof origin}`)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const nop = () => {}
|
|
31
|
+
|
|
32
|
+
function trimOWS (value) {
|
|
33
|
+
return value.replace(/^[\t ]+|[\t ]+$/g, '')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function arrayIncludes (array, value) {
|
|
37
|
+
for (let i = 0; i < array.length; i++) {
|
|
38
|
+
if (array[i] === value) {
|
|
39
|
+
return true
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return false
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function hasPragmaNoCache (headers) {
|
|
47
|
+
const pragma = headers?.pragma
|
|
48
|
+
if (!pragma) {
|
|
49
|
+
return false
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const values = Array.isArray(pragma) ? pragma : [pragma]
|
|
53
|
+
for (let i = 0; i < values.length; i++) {
|
|
54
|
+
const value = values[i]
|
|
55
|
+
if (typeof value !== 'string') {
|
|
56
|
+
continue
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const directives = value.split(',')
|
|
60
|
+
for (let j = 0; j < directives.length; j++) {
|
|
61
|
+
if (trimOWS(directives[j]).toLowerCase() === 'no-cache') {
|
|
62
|
+
return true
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return false
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @typedef {(options: import('../../types/dispatcher.d.ts').default.DispatchOptions, handler: import('../../types/dispatcher.d.ts').default.DispatchHandler) => void} DispatchFn
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
|
|
76
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives | undefined} cacheControlDirectives
|
|
77
|
+
* @param {import('../../types/dispatcher.d.ts').default.RequestOptions} opts
|
|
78
|
+
* @returns {boolean}
|
|
79
|
+
*/
|
|
80
|
+
function needsRevalidation (result, cacheControlDirectives, { headers = {} }) {
|
|
81
|
+
// Always revalidate requests with the no-cache request directive.
|
|
82
|
+
if (cacheControlDirectives?.['no-cache']) {
|
|
83
|
+
return true
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Always revalidate requests with unqualified no-cache response directive.
|
|
87
|
+
if (result.cacheControlDirectives?.['no-cache'] && !Array.isArray(result.cacheControlDirectives['no-cache'])) {
|
|
88
|
+
return true
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Always revalidate requests with conditional headers.
|
|
92
|
+
if (headers['if-modified-since'] || headers['if-none-match']) {
|
|
93
|
+
return true
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return false
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
|
|
101
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
|
|
102
|
+
* @returns {boolean}
|
|
103
|
+
*/
|
|
104
|
+
function staleResponseRequiresRevalidation (result, cacheType) {
|
|
105
|
+
return result.cacheControlDirectives?.['must-revalidate'] === true ||
|
|
106
|
+
(cacheType === 'shared' && (
|
|
107
|
+
result.cacheControlDirectives?.['proxy-revalidate'] === true ||
|
|
108
|
+
// https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.2.10
|
|
109
|
+
// s-maxage implies proxy-revalidate for shared caches.
|
|
110
|
+
result.cacheControlDirectives?.['s-maxage'] !== undefined
|
|
111
|
+
))
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
|
|
116
|
+
* @param {import('../../types/header.d.ts').IncomingHttpHeaders} headers
|
|
117
|
+
* @returns {boolean}
|
|
118
|
+
*/
|
|
119
|
+
function revalidationResponseDisallowsCachedReuse (cacheType, headers) {
|
|
120
|
+
if (
|
|
121
|
+
(headers.vary && isInvalidOrWildcardVaryHeader(headers.vary)) ||
|
|
122
|
+
(cacheType === 'shared' && Object.hasOwn(headers, 'set-cookie'))
|
|
123
|
+
) {
|
|
124
|
+
return true
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const cacheControl = headers['cache-control']
|
|
128
|
+
if (!cacheControl) {
|
|
129
|
+
return false
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const cacheControlDirectives = parseCacheControlHeader(cacheControl)
|
|
133
|
+
return cacheControlDirectives['no-store'] === true ||
|
|
134
|
+
(cacheType === 'shared' && cacheControlDirectives.private === true)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function revalidationResponseUpdatesCacheControl (headers) {
|
|
138
|
+
return headers['cache-control'] !== undefined
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function deleteCachedValue (store, cacheKey) {
|
|
142
|
+
try {
|
|
143
|
+
store.delete(cacheKey)?.catch?.(nop)
|
|
144
|
+
} catch {
|
|
145
|
+
// Fail silently
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function getUsableLastModified (headers) {
|
|
150
|
+
const lastModified = headers?.['last-modified']
|
|
151
|
+
if (typeof lastModified === 'string' && parseHttpDate(lastModified)) {
|
|
152
|
+
return lastModified
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function makeRevalidationHeaders (opts, result) {
|
|
157
|
+
const headers = {
|
|
158
|
+
...opts.headers,
|
|
159
|
+
'if-modified-since': getUsableLastModified(result.headers) ?? new Date(result.cachedAt).toUTCString()
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (result.etag) {
|
|
163
|
+
headers['if-none-match'] = result.etag
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (result.vary) {
|
|
167
|
+
for (const key in result.vary) {
|
|
168
|
+
if (result.vary[key] != null) {
|
|
169
|
+
headers[key] = result.vary[key]
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return headers
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
|
|
179
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives | undefined} cacheControlDirectives
|
|
180
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
|
|
181
|
+
* @returns {boolean}
|
|
182
|
+
*/
|
|
183
|
+
function isStale (result, cacheControlDirectives, cacheType) {
|
|
184
|
+
const now = Date.now()
|
|
185
|
+
if (now > result.staleAt) {
|
|
186
|
+
// Response is stale
|
|
187
|
+
if (!staleResponseRequiresRevalidation(result, cacheType) && cacheControlDirectives?.['max-stale']) {
|
|
188
|
+
// There's a threshold where we can serve stale responses, let's see if
|
|
189
|
+
// we're in it
|
|
190
|
+
// https://www.rfc-editor.org/rfc/rfc9111.html#name-max-stale
|
|
191
|
+
const gracePeriod = result.staleAt + (cacheControlDirectives['max-stale'] * 1000)
|
|
192
|
+
return now > gracePeriod
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return true
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (cacheControlDirectives?.['min-fresh']) {
|
|
199
|
+
// https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.1.3
|
|
200
|
+
|
|
201
|
+
// At this point, staleAt is always > now
|
|
202
|
+
const timeLeftTillStale = result.staleAt - now
|
|
203
|
+
const threshold = cacheControlDirectives['min-fresh'] * 1000
|
|
204
|
+
|
|
205
|
+
return timeLeftTillStale <= threshold
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return false
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Check if we're within the stale-while-revalidate window for a stale response
|
|
213
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
|
|
214
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
|
|
215
|
+
* @returns {boolean}
|
|
216
|
+
*/
|
|
217
|
+
function withinStaleWhileRevalidateWindow (result, cacheType) {
|
|
218
|
+
const staleWhileRevalidate = result.cacheControlDirectives?.['stale-while-revalidate']
|
|
219
|
+
if (!staleWhileRevalidate || staleResponseRequiresRevalidation(result, cacheType)) {
|
|
220
|
+
return false
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const now = Date.now()
|
|
224
|
+
const staleWhileRevalidateExpiry = result.staleAt + (staleWhileRevalidate * 1000)
|
|
225
|
+
return now <= staleWhileRevalidateExpiry
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* @param {DispatchFn} dispatch
|
|
230
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheHandlerOptions} globalOpts
|
|
231
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} cacheKey
|
|
232
|
+
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler
|
|
233
|
+
* @param {import('../../types/dispatcher.d.ts').default.RequestOptions} opts
|
|
234
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives | undefined} reqCacheControl
|
|
235
|
+
*/
|
|
236
|
+
function handleUncachedResponse (
|
|
237
|
+
dispatch,
|
|
238
|
+
globalOpts,
|
|
239
|
+
cacheKey,
|
|
240
|
+
handler,
|
|
241
|
+
opts,
|
|
242
|
+
reqCacheControl
|
|
243
|
+
) {
|
|
244
|
+
if (reqCacheControl?.['only-if-cached']) {
|
|
245
|
+
let aborted = false
|
|
246
|
+
try {
|
|
247
|
+
if (typeof handler.onConnect === 'function') {
|
|
248
|
+
handler.onConnect(() => {
|
|
249
|
+
aborted = true
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
if (aborted) {
|
|
253
|
+
return
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (typeof handler.onHeaders === 'function') {
|
|
258
|
+
handler.onHeaders(504, [], nop, 'Gateway Timeout')
|
|
259
|
+
if (aborted) {
|
|
260
|
+
return
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (typeof handler.onComplete === 'function') {
|
|
265
|
+
handler.onComplete([])
|
|
266
|
+
}
|
|
267
|
+
} catch (err) {
|
|
268
|
+
if (typeof handler.onError === 'function') {
|
|
269
|
+
handler.onError(err)
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return true
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return dispatch(opts, new CacheHandler(globalOpts, cacheKey, handler))
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler
|
|
281
|
+
* @param {import('../../types/dispatcher.d.ts').default.RequestOptions} opts
|
|
282
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult} result
|
|
283
|
+
* @param {number} age
|
|
284
|
+
* @param {any} context
|
|
285
|
+
* @param {boolean} isStale
|
|
286
|
+
*/
|
|
287
|
+
function sendCachedValue (handler, opts, result, age, context, isStale) {
|
|
288
|
+
// TODO (perf): Readable.from path can be optimized...
|
|
289
|
+
const stream = util.isStream(result.body)
|
|
290
|
+
? result.body
|
|
291
|
+
: Readable.from(result.body ?? [])
|
|
292
|
+
|
|
293
|
+
assert(!stream.destroyed, 'stream should not be destroyed')
|
|
294
|
+
assert(!stream.readableDidRead, 'stream should not be readableDidRead')
|
|
295
|
+
|
|
296
|
+
const controller = {
|
|
297
|
+
resume () {
|
|
298
|
+
stream.resume()
|
|
299
|
+
},
|
|
300
|
+
pause () {
|
|
301
|
+
stream.pause()
|
|
302
|
+
},
|
|
303
|
+
get paused () {
|
|
304
|
+
return stream.isPaused()
|
|
305
|
+
},
|
|
306
|
+
get aborted () {
|
|
307
|
+
return stream.destroyed
|
|
308
|
+
},
|
|
309
|
+
get reason () {
|
|
310
|
+
return stream.errored
|
|
311
|
+
},
|
|
312
|
+
abort (reason) {
|
|
313
|
+
stream.destroy(reason ?? new AbortError())
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
stream
|
|
318
|
+
.on('error', function (err) {
|
|
319
|
+
if (!this.readableEnded) {
|
|
320
|
+
if (typeof handler.onResponseError === 'function') {
|
|
321
|
+
handler.onResponseError(controller, err)
|
|
322
|
+
} else {
|
|
323
|
+
throw err
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
})
|
|
327
|
+
.on('close', function () {
|
|
328
|
+
if (!this.errored) {
|
|
329
|
+
handler.onResponseEnd?.(controller, {})
|
|
330
|
+
}
|
|
331
|
+
})
|
|
332
|
+
|
|
333
|
+
handler.onRequestStart?.(controller, context)
|
|
334
|
+
|
|
335
|
+
if (stream.destroyed) {
|
|
336
|
+
return
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// Add the age header
|
|
340
|
+
// https://www.rfc-editor.org/rfc/rfc9111.html#name-age
|
|
341
|
+
const headers = { ...result.headers, age: String(age) }
|
|
342
|
+
|
|
343
|
+
if (isStale) {
|
|
344
|
+
// Add warning header
|
|
345
|
+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Warning
|
|
346
|
+
headers.warning = '110 - "response is stale"'
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
handler.onResponseStart?.(controller, result.statusCode, headers, result.statusMessage)
|
|
350
|
+
|
|
351
|
+
if (opts.method === 'HEAD') {
|
|
352
|
+
stream.destroy()
|
|
353
|
+
} else {
|
|
354
|
+
stream.on('data', function (chunk) {
|
|
355
|
+
handler.onResponseData?.(controller, chunk)
|
|
356
|
+
})
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* @param {DispatchFn} dispatch
|
|
362
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheHandlerOptions} globalOpts
|
|
363
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} cacheKey
|
|
364
|
+
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler
|
|
365
|
+
* @param {import('../../types/dispatcher.d.ts').default.RequestOptions} opts
|
|
366
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives | undefined} reqCacheControl
|
|
367
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.GetResult | undefined} result
|
|
368
|
+
*/
|
|
369
|
+
function handleResult (
|
|
370
|
+
dispatch,
|
|
371
|
+
globalOpts,
|
|
372
|
+
cacheKey,
|
|
373
|
+
handler,
|
|
374
|
+
opts,
|
|
375
|
+
reqCacheControl,
|
|
376
|
+
result
|
|
377
|
+
) {
|
|
378
|
+
if (!result) {
|
|
379
|
+
return handleUncachedResponse(dispatch, globalOpts, cacheKey, handler, opts, reqCacheControl)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Shared stores may outlive the Undici version that wrote them. Do not
|
|
383
|
+
// re-serve a Set-Cookie header from an existing shared-cache entry.
|
|
384
|
+
if (globalOpts.type === 'shared' && Object.hasOwn(result.headers, 'set-cookie')) {
|
|
385
|
+
if (util.isStream(result.body)) {
|
|
386
|
+
result.body.on('error', nop).destroy()
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
deleteCachedValue(globalOpts.store, cacheKey)
|
|
390
|
+
return handleUncachedResponse(dispatch, globalOpts, cacheKey, handler, opts, reqCacheControl)
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const now = Date.now()
|
|
394
|
+
if (now > result.deleteAt) {
|
|
395
|
+
// Response is expired, cache store shouldn't have given this to us
|
|
396
|
+
return dispatch(opts, new CacheHandler(globalOpts, cacheKey, handler))
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const age = Math.round((now - result.cachedAt) / 1000)
|
|
400
|
+
const requestMaxAgeExpired = reqCacheControl?.['max-age'] !== undefined && age >= reqCacheControl['max-age']
|
|
401
|
+
|
|
402
|
+
const stale = requestMaxAgeExpired || isStale(result, reqCacheControl, globalOpts.type)
|
|
403
|
+
const revalidate = requestMaxAgeExpired || needsRevalidation(result, reqCacheControl, opts)
|
|
404
|
+
|
|
405
|
+
// Check if the response is stale
|
|
406
|
+
if (stale || revalidate) {
|
|
407
|
+
if (util.isStream(opts.body) && util.bodyLength(opts.body) !== 0) {
|
|
408
|
+
// If body is a stream we can't revalidate...
|
|
409
|
+
// TODO (fix): This could be less strict...
|
|
410
|
+
return dispatch(opts, new CacheHandler(globalOpts, cacheKey, handler))
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// RFC 5861: If we're within stale-while-revalidate window, serve stale immediately
|
|
414
|
+
// and revalidate in background, unless immediate revalidation is necessary
|
|
415
|
+
if (!revalidate && withinStaleWhileRevalidateWindow(result, globalOpts.type)) {
|
|
416
|
+
// Serve stale response immediately
|
|
417
|
+
sendCachedValue(handler, opts, result, age, null, true)
|
|
418
|
+
|
|
419
|
+
// Start background revalidation (fire-and-forget)
|
|
420
|
+
queueMicrotask(() => {
|
|
421
|
+
const headers = makeRevalidationHeaders(opts, result)
|
|
422
|
+
|
|
423
|
+
// Background revalidation - update cache if we get new data
|
|
424
|
+
dispatch(
|
|
425
|
+
{
|
|
426
|
+
...opts,
|
|
427
|
+
headers
|
|
428
|
+
},
|
|
429
|
+
new CacheHandler(globalOpts, cacheKey, {
|
|
430
|
+
// Silent handler that just updates the cache
|
|
431
|
+
onRequestStart () {},
|
|
432
|
+
onRequestUpgrade () {},
|
|
433
|
+
onResponseStart () {},
|
|
434
|
+
onResponseData () {},
|
|
435
|
+
onResponseEnd () {},
|
|
436
|
+
onResponseError () {}
|
|
437
|
+
})
|
|
438
|
+
)
|
|
439
|
+
})
|
|
440
|
+
|
|
441
|
+
return true
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
let withinStaleIfErrorThreshold = false
|
|
445
|
+
if (!staleResponseRequiresRevalidation(result, globalOpts.type)) {
|
|
446
|
+
const staleIfErrorExpiry = result.cacheControlDirectives['stale-if-error'] ?? reqCacheControl?.['stale-if-error']
|
|
447
|
+
if (staleIfErrorExpiry) {
|
|
448
|
+
withinStaleIfErrorThreshold = now < (result.staleAt + (staleIfErrorExpiry * 1000))
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const headers = makeRevalidationHeaders(opts, result)
|
|
453
|
+
|
|
454
|
+
// We need to revalidate the response
|
|
455
|
+
return dispatch(
|
|
456
|
+
{
|
|
457
|
+
...opts,
|
|
458
|
+
headers
|
|
459
|
+
},
|
|
460
|
+
new CacheRevalidationHandler(
|
|
461
|
+
(success, context, statusCode, headers) => {
|
|
462
|
+
if (success) {
|
|
463
|
+
if (statusCode === 304) {
|
|
464
|
+
if (revalidationResponseDisallowsCachedReuse(globalOpts.type, headers)) {
|
|
465
|
+
if (util.isStream(result.body)) {
|
|
466
|
+
result.body.on('error', nop).destroy()
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
deleteCachedValue(globalOpts.store, cacheKey)
|
|
470
|
+
return dispatch(opts, new CacheHandler(globalOpts, cacheKey, handler))
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (revalidationResponseUpdatesCacheControl(headers)) {
|
|
474
|
+
deleteCachedValue(globalOpts.store, cacheKey)
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// TODO: successful revalidation should be considered fresh (not give stale warning).
|
|
479
|
+
sendCachedValue(handler, opts, result, age, context, stale)
|
|
480
|
+
} else if (util.isStream(result.body)) {
|
|
481
|
+
result.body.on('error', nop).destroy()
|
|
482
|
+
}
|
|
483
|
+
},
|
|
484
|
+
new CacheHandler(globalOpts, cacheKey, handler),
|
|
485
|
+
withinStaleIfErrorThreshold
|
|
486
|
+
)
|
|
487
|
+
)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// Dump request body.
|
|
491
|
+
if (util.isStream(opts.body)) {
|
|
492
|
+
opts.body.on('error', nop).destroy()
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
sendCachedValue(handler, opts, result, age, null, false)
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions} [opts]
|
|
500
|
+
* @returns {import('../../types/dispatcher.d.ts').default.DispatcherComposeInterceptor}
|
|
501
|
+
*/
|
|
502
|
+
module.exports = (opts = {}) => {
|
|
503
|
+
const {
|
|
504
|
+
store = new MemoryCacheStore(),
|
|
505
|
+
methods = ['GET'],
|
|
506
|
+
cacheByDefault = undefined,
|
|
507
|
+
type = 'shared',
|
|
508
|
+
origins = undefined
|
|
509
|
+
} = opts
|
|
510
|
+
|
|
511
|
+
if (typeof opts !== 'object' || opts === null) {
|
|
512
|
+
throw new TypeError(`expected type of opts to be an Object, got ${opts === null ? 'null' : typeof opts}`)
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
assertCacheStore(store, 'opts.store')
|
|
516
|
+
assertCacheMethods(methods, 'opts.methods')
|
|
517
|
+
assertCacheOrigins(origins, 'opts.origins')
|
|
518
|
+
|
|
519
|
+
if (typeof cacheByDefault !== 'undefined' && typeof cacheByDefault !== 'number') {
|
|
520
|
+
throw new TypeError(`expected opts.cacheByDefault to be number or undefined, got ${typeof cacheByDefault}`)
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
if (typeof type !== 'undefined' && type !== 'shared' && type !== 'private') {
|
|
524
|
+
throw new TypeError(`expected opts.type to be shared, private, or undefined, got ${typeof type}`)
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const globalOpts = {
|
|
528
|
+
store,
|
|
529
|
+
methods,
|
|
530
|
+
cacheByDefault,
|
|
531
|
+
type
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
const safeMethodsToNotCache = []
|
|
535
|
+
for (let i = 0; i < util.safeHTTPMethods.length; i++) {
|
|
536
|
+
const method = util.safeHTTPMethods[i]
|
|
537
|
+
if (!arrayIncludes(methods, method)) {
|
|
538
|
+
safeMethodsToNotCache.push(method)
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
return dispatch => {
|
|
543
|
+
return (opts, handler) => {
|
|
544
|
+
if (!opts.origin || arrayIncludes(safeMethodsToNotCache, opts.method)) {
|
|
545
|
+
// Not a method we want to cache or we don't have the origin, skip
|
|
546
|
+
return dispatch(opts, handler)
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// Check if origin is in whitelist
|
|
550
|
+
if (origins !== undefined) {
|
|
551
|
+
const requestOrigin = opts.origin.toString().toLowerCase()
|
|
552
|
+
let isAllowed = false
|
|
553
|
+
|
|
554
|
+
for (let i = 0; i < origins.length; i++) {
|
|
555
|
+
const allowed = origins[i]
|
|
556
|
+
if (typeof allowed === 'string') {
|
|
557
|
+
if (allowed.toLowerCase() === requestOrigin) {
|
|
558
|
+
isAllowed = true
|
|
559
|
+
break
|
|
560
|
+
}
|
|
561
|
+
} else if (allowed.test(requestOrigin)) {
|
|
562
|
+
isAllowed = true
|
|
563
|
+
break
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (!isAllowed) {
|
|
568
|
+
return dispatch(opts, handler)
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
opts = {
|
|
573
|
+
...opts,
|
|
574
|
+
headers: normalizeHeaders(opts)
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
const reqCacheControl = opts.headers?.['cache-control']
|
|
578
|
+
? parseCacheControlHeader(opts.headers['cache-control'])
|
|
579
|
+
: hasPragmaNoCache(opts.headers)
|
|
580
|
+
? { 'no-cache': true }
|
|
581
|
+
: undefined
|
|
582
|
+
|
|
583
|
+
if (reqCacheControl?.['no-store']) {
|
|
584
|
+
return dispatch(opts, handler)
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* @type {import('../../types/cache-interceptor.d.ts').default.CacheKey}
|
|
589
|
+
*/
|
|
590
|
+
const cacheKey = makeCacheKey(opts)
|
|
591
|
+
|
|
592
|
+
if (!arrayIncludes(util.safeHTTPMethods, opts.method)) {
|
|
593
|
+
return dispatch(opts, new CacheHandler(globalOpts, cacheKey, handler))
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const result = store.get(cacheKey)
|
|
597
|
+
|
|
598
|
+
if (result && typeof result.then === 'function') {
|
|
599
|
+
return result
|
|
600
|
+
.then(result => handleResult(dispatch,
|
|
601
|
+
globalOpts,
|
|
602
|
+
cacheKey,
|
|
603
|
+
handler,
|
|
604
|
+
opts,
|
|
605
|
+
reqCacheControl,
|
|
606
|
+
result
|
|
607
|
+
))
|
|
608
|
+
} else {
|
|
609
|
+
return handleResult(
|
|
610
|
+
dispatch,
|
|
611
|
+
globalOpts,
|
|
612
|
+
cacheKey,
|
|
613
|
+
handler,
|
|
614
|
+
opts,
|
|
615
|
+
reqCacheControl,
|
|
616
|
+
result
|
|
617
|
+
)
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|