monitor-transfer-dsh 0.0.0-stage → 0.1.2

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.
Files changed (367) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +51 -2
  3. package/cordis.patch.yml +4 -0
  4. package/lib/client.js +381 -0
  5. package/lib/index.js +267 -0
  6. package/package.json +67 -3
  7. package/skill/SKILL.md +48 -0
  8. package/skill/tools/node_modules/.package-lock.json +163 -0
  9. package/skill/tools/node_modules/pg/LICENSE +21 -0
  10. package/skill/tools/node_modules/pg/README.md +96 -0
  11. package/skill/tools/node_modules/pg/esm/index.mjs +20 -0
  12. package/skill/tools/node_modules/pg/lib/client.js +846 -0
  13. package/skill/tools/node_modules/pg/lib/connection-parameters.js +184 -0
  14. package/skill/tools/node_modules/pg/lib/connection.js +250 -0
  15. package/skill/tools/node_modules/pg/lib/crypto/cert-signatures.js +122 -0
  16. package/skill/tools/node_modules/pg/lib/crypto/sasl.js +262 -0
  17. package/skill/tools/node_modules/pg/lib/crypto/utils.js +89 -0
  18. package/skill/tools/node_modules/pg/lib/defaults.js +94 -0
  19. package/skill/tools/node_modules/pg/lib/index.js +73 -0
  20. package/skill/tools/node_modules/pg/lib/native/client.js +428 -0
  21. package/skill/tools/node_modules/pg/lib/native/index.js +2 -0
  22. package/skill/tools/node_modules/pg/lib/native/query.js +167 -0
  23. package/skill/tools/node_modules/pg/lib/query.js +263 -0
  24. package/skill/tools/node_modules/pg/lib/result.js +109 -0
  25. package/skill/tools/node_modules/pg/lib/stream.js +83 -0
  26. package/skill/tools/node_modules/pg/lib/type-overrides.js +35 -0
  27. package/skill/tools/node_modules/pg/lib/utils.js +225 -0
  28. package/skill/tools/node_modules/pg/package.json +76 -0
  29. package/skill/tools/node_modules/pg-cloudflare/LICENSE +21 -0
  30. package/skill/tools/node_modules/pg-cloudflare/README.md +112 -0
  31. package/skill/tools/node_modules/pg-cloudflare/dist/empty.d.ts +2 -0
  32. package/skill/tools/node_modules/pg-cloudflare/dist/empty.js +6 -0
  33. package/skill/tools/node_modules/pg-cloudflare/dist/empty.js.map +1 -0
  34. package/skill/tools/node_modules/pg-cloudflare/dist/index.d.ts +29 -0
  35. package/skill/tools/node_modules/pg-cloudflare/dist/index.js +171 -0
  36. package/skill/tools/node_modules/pg-cloudflare/dist/index.js.map +1 -0
  37. package/skill/tools/node_modules/pg-cloudflare/esm/index.mjs +3 -0
  38. package/skill/tools/node_modules/pg-cloudflare/package.json +39 -0
  39. package/skill/tools/node_modules/pg-cloudflare/src/empty.ts +3 -0
  40. package/skill/tools/node_modules/pg-cloudflare/src/index.ts +187 -0
  41. package/skill/tools/node_modules/pg-cloudflare/src/types.d.ts +25 -0
  42. package/skill/tools/node_modules/pg-connection-string/LICENSE +21 -0
  43. package/skill/tools/node_modules/pg-connection-string/README.md +105 -0
  44. package/skill/tools/node_modules/pg-connection-string/esm/index.mjs +8 -0
  45. package/skill/tools/node_modules/pg-connection-string/index.d.ts +37 -0
  46. package/skill/tools/node_modules/pg-connection-string/index.js +237 -0
  47. package/skill/tools/node_modules/pg-connection-string/package.json +51 -0
  48. package/skill/tools/node_modules/pg-int8/LICENSE +13 -0
  49. package/skill/tools/node_modules/pg-int8/README.md +16 -0
  50. package/skill/tools/node_modules/pg-int8/index.js +100 -0
  51. package/skill/tools/node_modules/pg-int8/package.json +24 -0
  52. package/skill/tools/node_modules/pg-pool/LICENSE +21 -0
  53. package/skill/tools/node_modules/pg-pool/README.md +357 -0
  54. package/skill/tools/node_modules/pg-pool/esm/index.mjs +5 -0
  55. package/skill/tools/node_modules/pg-pool/index.js +517 -0
  56. package/skill/tools/node_modules/pg-pool/package.json +51 -0
  57. package/skill/tools/node_modules/pg-protocol/LICENSE +21 -0
  58. package/skill/tools/node_modules/pg-protocol/README.md +3 -0
  59. package/skill/tools/node_modules/pg-protocol/dist/b.d.ts +1 -0
  60. package/skill/tools/node_modules/pg-protocol/dist/b.js +23 -0
  61. package/skill/tools/node_modules/pg-protocol/dist/b.js.map +1 -0
  62. package/skill/tools/node_modules/pg-protocol/dist/buffer-reader.d.ts +14 -0
  63. package/skill/tools/node_modules/pg-protocol/dist/buffer-reader.js +55 -0
  64. package/skill/tools/node_modules/pg-protocol/dist/buffer-reader.js.map +1 -0
  65. package/skill/tools/node_modules/pg-protocol/dist/buffer-writer.d.ts +21 -0
  66. package/skill/tools/node_modules/pg-protocol/dist/buffer-writer.js +113 -0
  67. package/skill/tools/node_modules/pg-protocol/dist/buffer-writer.js.map +1 -0
  68. package/skill/tools/node_modules/pg-protocol/dist/inbound-parser.test.d.ts +1 -0
  69. package/skill/tools/node_modules/pg-protocol/dist/inbound-parser.test.js +538 -0
  70. package/skill/tools/node_modules/pg-protocol/dist/inbound-parser.test.js.map +1 -0
  71. package/skill/tools/node_modules/pg-protocol/dist/index.d.ts +5 -0
  72. package/skill/tools/node_modules/pg-protocol/dist/index.js +15 -0
  73. package/skill/tools/node_modules/pg-protocol/dist/index.js.map +1 -0
  74. package/skill/tools/node_modules/pg-protocol/dist/messages.d.ts +161 -0
  75. package/skill/tools/node_modules/pg-protocol/dist/messages.js +160 -0
  76. package/skill/tools/node_modules/pg-protocol/dist/messages.js.map +1 -0
  77. package/skill/tools/node_modules/pg-protocol/dist/outbound-serializer.test.d.ts +1 -0
  78. package/skill/tools/node_modules/pg-protocol/dist/outbound-serializer.test.js +325 -0
  79. package/skill/tools/node_modules/pg-protocol/dist/outbound-serializer.test.js.map +1 -0
  80. package/skill/tools/node_modules/pg-protocol/dist/parser.d.ts +22 -0
  81. package/skill/tools/node_modules/pg-protocol/dist/parser.js +325 -0
  82. package/skill/tools/node_modules/pg-protocol/dist/parser.js.map +1 -0
  83. package/skill/tools/node_modules/pg-protocol/dist/serializer.d.ts +42 -0
  84. package/skill/tools/node_modules/pg-protocol/dist/serializer.js +198 -0
  85. package/skill/tools/node_modules/pg-protocol/dist/serializer.js.map +1 -0
  86. package/skill/tools/node_modules/pg-protocol/esm/index.js +11 -0
  87. package/skill/tools/node_modules/pg-protocol/package.json +44 -0
  88. package/skill/tools/node_modules/pg-protocol/src/b.ts +25 -0
  89. package/skill/tools/node_modules/pg-protocol/src/buffer-reader.ts +58 -0
  90. package/skill/tools/node_modules/pg-protocol/src/buffer-writer.ts +120 -0
  91. package/skill/tools/node_modules/pg-protocol/src/inbound-parser.test.ts +585 -0
  92. package/skill/tools/node_modules/pg-protocol/src/index.ts +11 -0
  93. package/skill/tools/node_modules/pg-protocol/src/messages.ts +262 -0
  94. package/skill/tools/node_modules/pg-protocol/src/outbound-serializer.test.ts +356 -0
  95. package/skill/tools/node_modules/pg-protocol/src/parser.ts +414 -0
  96. package/skill/tools/node_modules/pg-protocol/src/serializer.ts +286 -0
  97. package/skill/tools/node_modules/pg-protocol/src/testing/buffer-list.ts +67 -0
  98. package/skill/tools/node_modules/pg-protocol/src/testing/test-buffers.ts +166 -0
  99. package/skill/tools/node_modules/pg-types/.travis.yml +7 -0
  100. package/skill/tools/node_modules/pg-types/Makefile +14 -0
  101. package/skill/tools/node_modules/pg-types/README.md +75 -0
  102. package/skill/tools/node_modules/pg-types/index.d.ts +137 -0
  103. package/skill/tools/node_modules/pg-types/index.js +47 -0
  104. package/skill/tools/node_modules/pg-types/index.test-d.ts +21 -0
  105. package/skill/tools/node_modules/pg-types/lib/arrayParser.js +11 -0
  106. package/skill/tools/node_modules/pg-types/lib/binaryParsers.js +257 -0
  107. package/skill/tools/node_modules/pg-types/lib/builtins.js +73 -0
  108. package/skill/tools/node_modules/pg-types/lib/textParsers.js +215 -0
  109. package/skill/tools/node_modules/pg-types/package.json +42 -0
  110. package/skill/tools/node_modules/pg-types/test/index.js +24 -0
  111. package/skill/tools/node_modules/pg-types/test/types.js +597 -0
  112. package/skill/tools/node_modules/pgpass/README.md +74 -0
  113. package/skill/tools/node_modules/pgpass/lib/helper.js +233 -0
  114. package/skill/tools/node_modules/pgpass/lib/index.js +23 -0
  115. package/skill/tools/node_modules/pgpass/package.json +41 -0
  116. package/skill/tools/node_modules/postgres-array/index.d.ts +4 -0
  117. package/skill/tools/node_modules/postgres-array/index.js +97 -0
  118. package/skill/tools/node_modules/postgres-array/license +21 -0
  119. package/skill/tools/node_modules/postgres-array/package.json +35 -0
  120. package/skill/tools/node_modules/postgres-array/readme.md +43 -0
  121. package/skill/tools/node_modules/postgres-bytea/index.js +33 -0
  122. package/skill/tools/node_modules/postgres-bytea/license +21 -0
  123. package/skill/tools/node_modules/postgres-bytea/package.json +34 -0
  124. package/skill/tools/node_modules/postgres-bytea/readme.md +34 -0
  125. package/skill/tools/node_modules/postgres-date/index.js +116 -0
  126. package/skill/tools/node_modules/postgres-date/license +21 -0
  127. package/skill/tools/node_modules/postgres-date/package.json +33 -0
  128. package/skill/tools/node_modules/postgres-date/readme.md +49 -0
  129. package/skill/tools/node_modules/postgres-interval/index.d.ts +20 -0
  130. package/skill/tools/node_modules/postgres-interval/index.js +125 -0
  131. package/skill/tools/node_modules/postgres-interval/license +21 -0
  132. package/skill/tools/node_modules/postgres-interval/package.json +36 -0
  133. package/skill/tools/node_modules/postgres-interval/readme.md +48 -0
  134. package/skill/tools/node_modules/split2/LICENSE +13 -0
  135. package/skill/tools/node_modules/split2/README.md +85 -0
  136. package/skill/tools/node_modules/split2/bench.js +27 -0
  137. package/skill/tools/node_modules/split2/index.js +141 -0
  138. package/skill/tools/node_modules/split2/package.json +39 -0
  139. package/skill/tools/node_modules/split2/test.js +409 -0
  140. package/skill/tools/node_modules/undici/LICENSE +21 -0
  141. package/skill/tools/node_modules/undici/README.md +828 -0
  142. package/skill/tools/node_modules/undici/docs/docs/api/Agent.md +291 -0
  143. package/skill/tools/node_modules/undici/docs/docs/api/BalancedPool.md +297 -0
  144. package/skill/tools/node_modules/undici/docs/docs/api/CacheStorage.md +188 -0
  145. package/skill/tools/node_modules/undici/docs/docs/api/CacheStore.md +375 -0
  146. package/skill/tools/node_modules/undici/docs/docs/api/Client.md +538 -0
  147. package/skill/tools/node_modules/undici/docs/docs/api/ClientStats.md +93 -0
  148. package/skill/tools/node_modules/undici/docs/docs/api/Connector.md +198 -0
  149. package/skill/tools/node_modules/undici/docs/docs/api/ContentType.md +97 -0
  150. package/skill/tools/node_modules/undici/docs/docs/api/Cookies.md +179 -0
  151. package/skill/tools/node_modules/undici/docs/docs/api/Debug.md +78 -0
  152. package/skill/tools/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +518 -0
  153. package/skill/tools/node_modules/undici/docs/docs/api/Dispatcher.md +842 -0
  154. package/skill/tools/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +171 -0
  155. package/skill/tools/node_modules/undici/docs/docs/api/Errors.md +624 -0
  156. package/skill/tools/node_modules/undici/docs/docs/api/EventSource.md +358 -0
  157. package/skill/tools/node_modules/undici/docs/docs/api/Fetch.md +721 -0
  158. package/skill/tools/node_modules/undici/docs/docs/api/GlobalInstallation.md +121 -0
  159. package/skill/tools/node_modules/undici/docs/docs/api/H2CClient.md +386 -0
  160. package/skill/tools/node_modules/undici/docs/docs/api/Interceptors.md +439 -0
  161. package/skill/tools/node_modules/undici/docs/docs/api/MockAgent.md +622 -0
  162. package/skill/tools/node_modules/undici/docs/docs/api/MockCallHistory.md +371 -0
  163. package/skill/tools/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +206 -0
  164. package/skill/tools/node_modules/undici/docs/docs/api/MockClient.md +199 -0
  165. package/skill/tools/node_modules/undici/docs/docs/api/MockErrors.md +63 -0
  166. package/skill/tools/node_modules/undici/docs/docs/api/MockPool.md +618 -0
  167. package/skill/tools/node_modules/undici/docs/docs/api/Pool.md +278 -0
  168. package/skill/tools/node_modules/undici/docs/docs/api/PoolStats.md +141 -0
  169. package/skill/tools/node_modules/undici/docs/docs/api/ProxyAgent.md +248 -0
  170. package/skill/tools/node_modules/undici/docs/docs/api/RedirectHandler.md +279 -0
  171. package/skill/tools/node_modules/undici/docs/docs/api/RetryAgent.md +136 -0
  172. package/skill/tools/node_modules/undici/docs/docs/api/RetryHandler.md +200 -0
  173. package/skill/tools/node_modules/undici/docs/docs/api/RoundRobinPool.md +239 -0
  174. package/skill/tools/node_modules/undici/docs/docs/api/SnapshotAgent.md +361 -0
  175. package/skill/tools/node_modules/undici/docs/docs/api/Socks5ProxyAgent.md +256 -0
  176. package/skill/tools/node_modules/undici/docs/docs/api/Util.md +82 -0
  177. package/skill/tools/node_modules/undici/docs/docs/api/WebSocket.md +536 -0
  178. package/skill/tools/node_modules/undici/docs/docs/api/api-lifecycle.md +159 -0
  179. package/skill/tools/node_modules/undici/docs/docs/best-practices/client-certificate.md +66 -0
  180. package/skill/tools/node_modules/undici/docs/docs/best-practices/crawling.md +60 -0
  181. package/skill/tools/node_modules/undici/docs/docs/best-practices/migrating-from-v7-to-v8.md +233 -0
  182. package/skill/tools/node_modules/undici/docs/docs/best-practices/mocking-request.md +192 -0
  183. package/skill/tools/node_modules/undici/docs/docs/best-practices/proxy.md +129 -0
  184. package/skill/tools/node_modules/undici/docs/docs/best-practices/undici-vs-builtin-fetch.md +226 -0
  185. package/skill/tools/node_modules/undici/docs/docs/best-practices/writing-tests.md +65 -0
  186. package/skill/tools/node_modules/undici/docs/docs/getting-started.md +281 -0
  187. package/skill/tools/node_modules/undici/docs/docs/index.md +793 -0
  188. package/skill/tools/node_modules/undici/docs/docs/site.json +125 -0
  189. package/skill/tools/node_modules/undici/docs/docs/type-map.json +79 -0
  190. package/skill/tools/node_modules/undici/index-fetch.js +65 -0
  191. package/skill/tools/node_modules/undici/index.d.ts +3 -0
  192. package/skill/tools/node_modules/undici/index.js +236 -0
  193. package/skill/tools/node_modules/undici/lib/api/abort-signal.js +59 -0
  194. package/skill/tools/node_modules/undici/lib/api/api-connect.js +112 -0
  195. package/skill/tools/node_modules/undici/lib/api/api-pipeline.js +265 -0
  196. package/skill/tools/node_modules/undici/lib/api/api-request.js +244 -0
  197. package/skill/tools/node_modules/undici/lib/api/api-stream.js +270 -0
  198. package/skill/tools/node_modules/undici/lib/api/api-upgrade.js +121 -0
  199. package/skill/tools/node_modules/undici/lib/api/index.js +7 -0
  200. package/skill/tools/node_modules/undici/lib/api/readable.js +615 -0
  201. package/skill/tools/node_modules/undici/lib/cache/memory-cache-store.js +314 -0
  202. package/skill/tools/node_modules/undici/lib/cache/sqlite-cache-store.js +593 -0
  203. package/skill/tools/node_modules/undici/lib/core/connect.js +198 -0
  204. package/skill/tools/node_modules/undici/lib/core/constants.js +131 -0
  205. package/skill/tools/node_modules/undici/lib/core/diagnostics.js +227 -0
  206. package/skill/tools/node_modules/undici/lib/core/errors.js +534 -0
  207. package/skill/tools/node_modules/undici/lib/core/request.js +582 -0
  208. package/skill/tools/node_modules/undici/lib/core/socks5-client.js +422 -0
  209. package/skill/tools/node_modules/undici/lib/core/socks5-utils.js +212 -0
  210. package/skill/tools/node_modules/undici/lib/core/symbols.js +81 -0
  211. package/skill/tools/node_modules/undici/lib/core/tree.js +160 -0
  212. package/skill/tools/node_modules/undici/lib/core/util.js +1035 -0
  213. package/skill/tools/node_modules/undici/lib/dispatcher/agent.js +181 -0
  214. package/skill/tools/node_modules/undici/lib/dispatcher/balanced-pool.js +217 -0
  215. package/skill/tools/node_modules/undici/lib/dispatcher/client-h1.js +1846 -0
  216. package/skill/tools/node_modules/undici/lib/dispatcher/client-h2.js +1989 -0
  217. package/skill/tools/node_modules/undici/lib/dispatcher/client.js +749 -0
  218. package/skill/tools/node_modules/undici/lib/dispatcher/dispatcher-base.js +198 -0
  219. package/skill/tools/node_modules/undici/lib/dispatcher/dispatcher.js +58 -0
  220. package/skill/tools/node_modules/undici/lib/dispatcher/dispatcher1-wrapper.js +127 -0
  221. package/skill/tools/node_modules/undici/lib/dispatcher/env-http-proxy-agent.js +190 -0
  222. package/skill/tools/node_modules/undici/lib/dispatcher/fixed-queue.js +135 -0
  223. package/skill/tools/node_modules/undici/lib/dispatcher/h2c-client.js +51 -0
  224. package/skill/tools/node_modules/undici/lib/dispatcher/parser-h1.js +1065 -0
  225. package/skill/tools/node_modules/undici/lib/dispatcher/pool-base.js +347 -0
  226. package/skill/tools/node_modules/undici/lib/dispatcher/pool.js +229 -0
  227. package/skill/tools/node_modules/undici/lib/dispatcher/proxy-agent.js +418 -0
  228. package/skill/tools/node_modules/undici/lib/dispatcher/retry-agent.js +48 -0
  229. package/skill/tools/node_modules/undici/lib/dispatcher/round-robin-pool.js +169 -0
  230. package/skill/tools/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +350 -0
  231. package/skill/tools/node_modules/undici/lib/encoding/index.js +33 -0
  232. package/skill/tools/node_modules/undici/lib/global.js +82 -0
  233. package/skill/tools/node_modules/undici/lib/handler/cache-handler.js +1222 -0
  234. package/skill/tools/node_modules/undici/lib/handler/cache-revalidation-handler.js +134 -0
  235. package/skill/tools/node_modules/undici/lib/handler/decorator-handler.js +76 -0
  236. package/skill/tools/node_modules/undici/lib/handler/deduplication-handler.js +540 -0
  237. package/skill/tools/node_modules/undici/lib/handler/redirect-handler.js +284 -0
  238. package/skill/tools/node_modules/undici/lib/handler/retry-handler.js +686 -0
  239. package/skill/tools/node_modules/undici/lib/interceptor/cache.js +771 -0
  240. package/skill/tools/node_modules/undici/lib/interceptor/decompress.js +574 -0
  241. package/skill/tools/node_modules/undici/lib/interceptor/deduplicate.js +118 -0
  242. package/skill/tools/node_modules/undici/lib/interceptor/dns.js +593 -0
  243. package/skill/tools/node_modules/undici/lib/interceptor/dump.js +99 -0
  244. package/skill/tools/node_modules/undici/lib/interceptor/redirect.js +21 -0
  245. package/skill/tools/node_modules/undici/lib/interceptor/response-error.js +139 -0
  246. package/skill/tools/node_modules/undici/lib/interceptor/retry.js +19 -0
  247. package/skill/tools/node_modules/undici/lib/llhttp/constants.d.ts +195 -0
  248. package/skill/tools/node_modules/undici/lib/llhttp/constants.js +531 -0
  249. package/skill/tools/node_modules/undici/lib/llhttp/llhttp-wasm.js +15 -0
  250. package/skill/tools/node_modules/undici/lib/llhttp/llhttp_simd-wasm.js +15 -0
  251. package/skill/tools/node_modules/undici/lib/llhttp/utils.d.ts +2 -0
  252. package/skill/tools/node_modules/undici/lib/llhttp/utils.js +12 -0
  253. package/skill/tools/node_modules/undici/lib/milo/LICENSE.md +7 -0
  254. package/skill/tools/node_modules/undici/lib/milo/no-simd.js +1002 -0
  255. package/skill/tools/node_modules/undici/lib/milo/simd.js +1002 -0
  256. package/skill/tools/node_modules/undici/lib/mock/mock-agent.js +255 -0
  257. package/skill/tools/node_modules/undici/lib/mock/mock-call-history.js +248 -0
  258. package/skill/tools/node_modules/undici/lib/mock/mock-client.js +68 -0
  259. package/skill/tools/node_modules/undici/lib/mock/mock-errors.js +29 -0
  260. package/skill/tools/node_modules/undici/lib/mock/mock-interceptor.js +227 -0
  261. package/skill/tools/node_modules/undici/lib/mock/mock-pool.js +68 -0
  262. package/skill/tools/node_modules/undici/lib/mock/mock-symbols.js +32 -0
  263. package/skill/tools/node_modules/undici/lib/mock/mock-utils.js +730 -0
  264. package/skill/tools/node_modules/undici/lib/mock/pending-interceptors-formatter.js +43 -0
  265. package/skill/tools/node_modules/undici/lib/mock/snapshot-agent.js +371 -0
  266. package/skill/tools/node_modules/undici/lib/mock/snapshot-recorder.js +624 -0
  267. package/skill/tools/node_modules/undici/lib/mock/snapshot-utils.js +159 -0
  268. package/skill/tools/node_modules/undici/lib/util/cache.js +764 -0
  269. package/skill/tools/node_modules/undici/lib/util/date.js +670 -0
  270. package/skill/tools/node_modules/undici/lib/util/runtime-features.js +93 -0
  271. package/skill/tools/node_modules/undici/lib/util/stats.js +32 -0
  272. package/skill/tools/node_modules/undici/lib/util/timers.js +425 -0
  273. package/skill/tools/node_modules/undici/lib/web/cache/cache.js +871 -0
  274. package/skill/tools/node_modules/undici/lib/web/cache/cachestorage.js +158 -0
  275. package/skill/tools/node_modules/undici/lib/web/cache/util.js +45 -0
  276. package/skill/tools/node_modules/undici/lib/web/cookies/constants.js +12 -0
  277. package/skill/tools/node_modules/undici/lib/web/cookies/index.js +211 -0
  278. package/skill/tools/node_modules/undici/lib/web/cookies/parse.js +318 -0
  279. package/skill/tools/node_modules/undici/lib/web/cookies/util.js +371 -0
  280. package/skill/tools/node_modules/undici/lib/web/eventsource/eventsource-stream.js +575 -0
  281. package/skill/tools/node_modules/undici/lib/web/eventsource/eventsource.js +547 -0
  282. package/skill/tools/node_modules/undici/lib/web/eventsource/util.js +60 -0
  283. package/skill/tools/node_modules/undici/lib/web/fetch/LICENSE +21 -0
  284. package/skill/tools/node_modules/undici/lib/web/fetch/body.js +583 -0
  285. package/skill/tools/node_modules/undici/lib/web/fetch/constants.js +131 -0
  286. package/skill/tools/node_modules/undici/lib/web/fetch/data-url.js +596 -0
  287. package/skill/tools/node_modules/undici/lib/web/fetch/formdata-parser.js +594 -0
  288. package/skill/tools/node_modules/undici/lib/web/fetch/formdata.js +272 -0
  289. package/skill/tools/node_modules/undici/lib/web/fetch/global.js +40 -0
  290. package/skill/tools/node_modules/undici/lib/web/fetch/headers.js +722 -0
  291. package/skill/tools/node_modules/undici/lib/web/fetch/index.js +2451 -0
  292. package/skill/tools/node_modules/undici/lib/web/fetch/request.js +1117 -0
  293. package/skill/tools/node_modules/undici/lib/web/fetch/response.js +638 -0
  294. package/skill/tools/node_modules/undici/lib/web/fetch/util.js +1530 -0
  295. package/skill/tools/node_modules/undici/lib/web/infra/index.js +244 -0
  296. package/skill/tools/node_modules/undici/lib/web/subresource-integrity/Readme.md +9 -0
  297. package/skill/tools/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +307 -0
  298. package/skill/tools/node_modules/undici/lib/web/webidl/index.js +992 -0
  299. package/skill/tools/node_modules/undici/lib/web/websocket/connection.js +350 -0
  300. package/skill/tools/node_modules/undici/lib/web/websocket/constants.js +126 -0
  301. package/skill/tools/node_modules/undici/lib/web/websocket/events.js +348 -0
  302. package/skill/tools/node_modules/undici/lib/web/websocket/frame.js +127 -0
  303. package/skill/tools/node_modules/undici/lib/web/websocket/permessage-deflate.js +105 -0
  304. package/skill/tools/node_modules/undici/lib/web/websocket/receiver.js +530 -0
  305. package/skill/tools/node_modules/undici/lib/web/websocket/sender.js +109 -0
  306. package/skill/tools/node_modules/undici/lib/web/websocket/stream/websocketerror.js +107 -0
  307. package/skill/tools/node_modules/undici/lib/web/websocket/stream/websocketstream.js +511 -0
  308. package/skill/tools/node_modules/undici/lib/web/websocket/util.js +347 -0
  309. package/skill/tools/node_modules/undici/lib/web/websocket/websocket.js +783 -0
  310. package/skill/tools/node_modules/undici/package.json +154 -0
  311. package/skill/tools/node_modules/undici/scripts/strip-comments.js +10 -0
  312. package/skill/tools/node_modules/undici/types/README.md +6 -0
  313. package/skill/tools/node_modules/undici/types/agent.d.ts +30 -0
  314. package/skill/tools/node_modules/undici/types/api.d.ts +43 -0
  315. package/skill/tools/node_modules/undici/types/balanced-pool.d.ts +30 -0
  316. package/skill/tools/node_modules/undici/types/cache-interceptor.d.ts +189 -0
  317. package/skill/tools/node_modules/undici/types/cache.d.ts +36 -0
  318. package/skill/tools/node_modules/undici/types/client-stats.d.ts +15 -0
  319. package/skill/tools/node_modules/undici/types/client.d.ts +190 -0
  320. package/skill/tools/node_modules/undici/types/connector.d.ts +37 -0
  321. package/skill/tools/node_modules/undici/types/content-type.d.ts +21 -0
  322. package/skill/tools/node_modules/undici/types/cookies.d.ts +30 -0
  323. package/skill/tools/node_modules/undici/types/diagnostics-channel.d.ts +75 -0
  324. package/skill/tools/node_modules/undici/types/dispatcher.d.ts +253 -0
  325. package/skill/tools/node_modules/undici/types/dispatcher1-wrapper.d.ts +7 -0
  326. package/skill/tools/node_modules/undici/types/env-http-proxy-agent.d.ts +22 -0
  327. package/skill/tools/node_modules/undici/types/errors.d.ts +202 -0
  328. package/skill/tools/node_modules/undici/types/eventsource.d.ts +66 -0
  329. package/skill/tools/node_modules/undici/types/fetch.d.ts +235 -0
  330. package/skill/tools/node_modules/undici/types/formdata.d.ts +108 -0
  331. package/skill/tools/node_modules/undici/types/global-dispatcher.d.ts +9 -0
  332. package/skill/tools/node_modules/undici/types/global-origin.d.ts +7 -0
  333. package/skill/tools/node_modules/undici/types/h2c-client.d.ts +73 -0
  334. package/skill/tools/node_modules/undici/types/handlers.d.ts +16 -0
  335. package/skill/tools/node_modules/undici/types/header.d.ts +165 -0
  336. package/skill/tools/node_modules/undici/types/index.d.ts +93 -0
  337. package/skill/tools/node_modules/undici/types/interceptors.d.ts +87 -0
  338. package/skill/tools/node_modules/undici/types/mock-agent.d.ts +69 -0
  339. package/skill/tools/node_modules/undici/types/mock-call-history.d.ts +111 -0
  340. package/skill/tools/node_modules/undici/types/mock-client.d.ts +27 -0
  341. package/skill/tools/node_modules/undici/types/mock-errors.d.ts +12 -0
  342. package/skill/tools/node_modules/undici/types/mock-interceptor.d.ts +98 -0
  343. package/skill/tools/node_modules/undici/types/mock-pool.d.ts +27 -0
  344. package/skill/tools/node_modules/undici/types/patch.d.ts +29 -0
  345. package/skill/tools/node_modules/undici/types/pool-stats.d.ts +19 -0
  346. package/skill/tools/node_modules/undici/types/pool.d.ts +39 -0
  347. package/skill/tools/node_modules/undici/types/proxy-agent.d.ts +36 -0
  348. package/skill/tools/node_modules/undici/types/readable.d.ts +68 -0
  349. package/skill/tools/node_modules/undici/types/retry-agent.d.ts +8 -0
  350. package/skill/tools/node_modules/undici/types/retry-handler.d.ts +125 -0
  351. package/skill/tools/node_modules/undici/types/round-robin-pool.d.ts +39 -0
  352. package/skill/tools/node_modules/undici/types/snapshot-agent.d.ts +113 -0
  353. package/skill/tools/node_modules/undici/types/socks5-proxy-agent.d.ts +27 -0
  354. package/skill/tools/node_modules/undici/types/util.d.ts +18 -0
  355. package/skill/tools/node_modules/undici/types/utility.d.ts +7 -0
  356. package/skill/tools/node_modules/undici/types/webidl.d.ts +352 -0
  357. package/skill/tools/node_modules/undici/types/websocket.d.ts +188 -0
  358. package/skill/tools/node_modules/xtend/.jshintrc +30 -0
  359. package/skill/tools/node_modules/xtend/LICENSE +20 -0
  360. package/skill/tools/node_modules/xtend/README.md +32 -0
  361. package/skill/tools/node_modules/xtend/immutable.js +19 -0
  362. package/skill/tools/node_modules/xtend/mutable.js +17 -0
  363. package/skill/tools/node_modules/xtend/package.json +55 -0
  364. package/skill/tools/node_modules/xtend/test.js +103 -0
  365. package/skill/tools/package-lock.json +172 -0
  366. package/skill/tools/package.json +9 -0
  367. package/skill/tools/transfer_core.js +414 -0
@@ -0,0 +1,1222 @@
1
+ 'use strict'
2
+
3
+ const util = require('../core/util')
4
+ const { kRedirectCacheControl } = require('../core/symbols')
5
+ const {
6
+ parseCacheControlHeader,
7
+ hasInvalidCacheControlDirective,
8
+ parseVaryHeader,
9
+ hasVaryStar,
10
+ isInvalidOrWildcardVaryHeader,
11
+ isEtagUsable
12
+ } = require('../util/cache')
13
+
14
+ /**
15
+ * A suppressed redirect hop recorded by the redirect handler. Carried on
16
+ * {@link kRedirectCacheControl} and inspected by the cache handler before the
17
+ * final response is stored under the original request key.
18
+ *
19
+ * @typedef {object} RedirectHop
20
+ * @property {number} statusCode The hop's response status code (e.g. 302).
21
+ * @property {import('../../types/header.d.ts').IncomingHttpHeaders} headers The hop's response headers.
22
+ * @property {number} receivedAt The time the hop's response headers were received.
23
+ */
24
+ const { parseHttpDate } = require('../util/date.js')
25
+
26
+ function noop () {}
27
+
28
+ class CacheController {
29
+ #blocked = false
30
+ #downstreamPaused = false
31
+
32
+ constructor (target) {
33
+ this.target = target
34
+ }
35
+
36
+ block () {
37
+ this.#blocked = true
38
+ }
39
+
40
+ release () {
41
+ this.#blocked = false
42
+ if (!this.#downstreamPaused) {
43
+ this.target.resume()
44
+ }
45
+ }
46
+
47
+ pause () {
48
+ this.#downstreamPaused = true
49
+ this.target.pause()
50
+ }
51
+
52
+ resume () {
53
+ this.#downstreamPaused = false
54
+ if (!this.#blocked) {
55
+ this.target.resume()
56
+ }
57
+ }
58
+
59
+ abort (reason) {
60
+ this.target.abort(reason)
61
+ }
62
+
63
+ get paused () { return this.#downstreamPaused }
64
+ get aborted () { return this.target.aborted }
65
+ get reason () { return this.target.reason }
66
+ get rawHeaders () { return this.target.rawHeaders }
67
+ set rawHeaders (value) { this.target.rawHeaders = value }
68
+ get rawTrailers () { return this.target.rawTrailers }
69
+ set rawTrailers (value) { this.target.rawTrailers = value }
70
+ }
71
+
72
+ /**
73
+ * @param {string | URL} a
74
+ * @param {string | URL} b
75
+ * @returns {boolean}
76
+ */
77
+ function sameOrigin (a, b) {
78
+ try {
79
+ return new URL(String(a)).origin === new URL(String(b)).origin
80
+ } catch {
81
+ return String(a) === String(b)
82
+ }
83
+ }
84
+
85
+ // Status codes that we can use some heuristics on to cache
86
+ const HEURISTICALLY_CACHEABLE_STATUS_CODES = [
87
+ 200, 203, 204, 206, 300, 301, 308, 404, 405, 410, 414, 501
88
+ ]
89
+
90
+ // Status codes which semantic is not handled by the cache
91
+ // https://datatracker.ietf.org/doc/html/rfc9111#section-3
92
+ // This list should not grow beyond 206 unless the RFC is updated
93
+ // by a newer one including more. Please introduce another list if
94
+ // implementing caching of responses with the 'must-understand' directive.
95
+ const NOT_UNDERSTOOD_STATUS_CODES = [
96
+ 206
97
+ ]
98
+
99
+ const MAX_RESPONSE_AGE = 2147483647000
100
+
101
+ // Retention for revalidation-only entries (zero freshness lifetime but a
102
+ // validator present); each successful revalidation re-stores the entry.
103
+ const REVALIDATION_ONLY_RETENTION = 86400000 // 24 hours
104
+
105
+ function trimOWS (value) {
106
+ return value.replace(/^[\t ]+|[\t ]+$/g, '')
107
+ }
108
+
109
+ function arrayIncludes (array, value) {
110
+ for (let i = 0; i < array.length; i++) {
111
+ if (array[i] === value) {
112
+ return true
113
+ }
114
+ }
115
+
116
+ return false
117
+ }
118
+
119
+ function appendVaryValues (target, varyValue) {
120
+ if (Array.isArray(varyValue)) {
121
+ for (let i = 0; i < varyValue.length; i++) {
122
+ target.push(varyValue[i])
123
+ }
124
+ } else if (varyValue != null) {
125
+ target.push(varyValue)
126
+ }
127
+ }
128
+
129
+ function appendConnectionHeaderTokens (headersToRemove, connectionHeader) {
130
+ const values = Array.isArray(connectionHeader) ? connectionHeader : [connectionHeader]
131
+
132
+ for (let i = 0; i < values.length; i++) {
133
+ const tokens = values[i].split(',')
134
+ for (let j = 0; j < tokens.length; j++) {
135
+ headersToRemove.push(trimOWS(tokens[j]).toLowerCase())
136
+ }
137
+ }
138
+ }
139
+
140
+ function getSameOriginPath (cacheKey, location) {
141
+ if (typeof location !== 'string') {
142
+ return undefined
143
+ }
144
+
145
+ let originUrl
146
+ let requestUrl
147
+ let locationUrl
148
+ try {
149
+ originUrl = new URL(cacheKey.origin)
150
+ requestUrl = new URL(cacheKey.path, originUrl)
151
+ locationUrl = new URL(location, requestUrl)
152
+ } catch {
153
+ return undefined
154
+ }
155
+
156
+ if (locationUrl.origin !== originUrl.origin) {
157
+ return undefined
158
+ }
159
+
160
+ return locationUrl.pathname + locationUrl.search
161
+ }
162
+
163
+ function deleteCachedUri (store, cacheKey, path) {
164
+ deleteCachedValue(store, {
165
+ ...cacheKey,
166
+ path
167
+ })
168
+
169
+ for (let i = 0; i < util.safeHTTPMethods.length; i++) {
170
+ const method = util.safeHTTPMethods[i]
171
+ if (method !== cacheKey.method) {
172
+ deleteCachedValue(store, {
173
+ ...cacheKey,
174
+ method,
175
+ path
176
+ })
177
+ }
178
+ }
179
+ }
180
+
181
+ function deleteLocationTargets (store, cacheKey, headerValue) {
182
+ if (headerValue === undefined) {
183
+ return
184
+ }
185
+
186
+ const values = Array.isArray(headerValue) ? headerValue : [headerValue]
187
+ for (let i = 0; i < values.length; i++) {
188
+ const path = getSameOriginPath(cacheKey, values[i])
189
+ if (path !== undefined) {
190
+ deleteCachedUri(store, cacheKey, path)
191
+ }
192
+ }
193
+ }
194
+
195
+ function invalidateUnsafeRequest (store, cacheKey, resHeaders) {
196
+ deleteCachedUri(store, cacheKey, cacheKey.path)
197
+ deleteLocationTargets(store, cacheKey, resHeaders.location)
198
+ deleteLocationTargets(store, cacheKey, resHeaders['content-location'])
199
+ }
200
+
201
+ /**
202
+ * Whether the original request carried a credential that is not reflected in
203
+ * the cache key. The memory/sqlite stores key an entry on origin, method, path
204
+ * and `vary` only, so a credentialed request that is redirected to a resource
205
+ * that varies per principal must not be stored under the original (alias) key:
206
+ * a later caller would be served the first principal's body. The set mirrors the
207
+ * request credentials the redirect handler strips across origins (`authorization`,
208
+ * `cookie`, `proxy-authorization`).
209
+ *
210
+ * @param {import('../../types/header.d.ts').IncomingHttpHeaders | undefined} headers
211
+ * @returns {boolean}
212
+ */
213
+ function requestCarriesCredentials (headers) {
214
+ return headers != null && (
215
+ Object.hasOwn(headers, 'authorization') ||
216
+ Object.hasOwn(headers, 'cookie') ||
217
+ Object.hasOwn(headers, 'proxy-authorization')
218
+ )
219
+ }
220
+ /**
221
+ * @typedef {import('../../types/dispatcher.d.ts').default.DispatchHandler} DispatchHandler
222
+ *
223
+ * @implements {DispatchHandler}
224
+ */
225
+ class CacheHandler {
226
+ /**
227
+ * @type {import('../../types/cache-interceptor.d.ts').default.CacheKey}
228
+ */
229
+ #cacheKey
230
+
231
+ /**
232
+ * @type {import('../../types/cache-interceptor.d.ts').default.CacheHandlerOptions['type']}
233
+ */
234
+ #cacheType
235
+
236
+ /**
237
+ * @type {number | undefined}
238
+ */
239
+ #cacheByDefault
240
+
241
+ /**
242
+ * @type {import('../../types/cache-interceptor.d.ts').default.CacheStore}
243
+ */
244
+ #store
245
+
246
+ /**
247
+ * @type {import('../../types/dispatcher.d.ts').default.DispatchHandler}
248
+ */
249
+ #handler
250
+
251
+ /**
252
+ * @type {CacheController | undefined}
253
+ */
254
+ #downstreamController
255
+
256
+ /**
257
+ * @type {import('node:stream').Writable | undefined}
258
+ */
259
+ #writeStream
260
+
261
+ /**
262
+ * The URLs visited while following redirects, shared by reference from the
263
+ * redirect handler through the request context. Carries the suppressed hops'
264
+ * status/headers under {@link kRedirectCacheControl}.
265
+ * @type {URL[] | undefined}
266
+ */
267
+ #history
268
+
269
+ /**
270
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheHandlerOptions} opts
271
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} cacheKey
272
+ * @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler
273
+ */
274
+ constructor ({ store, type, cacheByDefault }, cacheKey, handler) {
275
+ this.#store = store
276
+ this.#cacheType = type
277
+ this.#cacheByDefault = cacheByDefault
278
+ this.#cacheKey = cacheKey
279
+ this.#handler = handler
280
+ }
281
+
282
+ onRequestStart (controller, context) {
283
+ this.#writeStream?.destroy()
284
+ this.#writeStream = undefined
285
+ this.#downstreamController = new CacheController(controller)
286
+ // The redirect handler threads the list of visited URLs through the
287
+ // handler-chain context. Keep a reference to it (it is mutated as redirects
288
+ // are followed) so onResponseStart can tell which request actually produced
289
+ // the response. Unlike the request options object, this reference is not
290
+ // broken by an interceptor that copies the options between the cache and
291
+ // the redirect.
292
+ this.#history = context?.history
293
+ this.#handler.onRequestStart?.(this.#downstreamController, context)
294
+ }
295
+
296
+ onBodySent (chunk) {
297
+ this.#handler.onBodySent?.(chunk)
298
+ }
299
+
300
+ onRequestSent () {
301
+ this.#handler.onRequestSent?.()
302
+ }
303
+
304
+ onRequestUpgrade (_controller, statusCode, headers, socket) {
305
+ this.#handler.onRequestUpgrade?.(this.#downstreamController, statusCode, headers, socket)
306
+ }
307
+
308
+ /**
309
+ * @param {import('../../types/dispatcher.d.ts').default.DispatchController} controller
310
+ * @param {number} statusCode
311
+ * @param {import('../../types/header.d.ts').IncomingHttpHeaders} resHeaders
312
+ * @param {string} statusMessage
313
+ */
314
+ onResponseStart (
315
+ controller,
316
+ statusCode,
317
+ resHeaders,
318
+ statusMessage
319
+ ) {
320
+ const downstreamOnHeaders = () =>
321
+ this.#handler.onResponseStart?.(
322
+ this.#downstreamController,
323
+ statusCode,
324
+ resHeaders,
325
+ statusMessage
326
+ )
327
+ const handler = this
328
+
329
+ if (
330
+ !arrayIncludes(util.safeHTTPMethods, this.#cacheKey.method) &&
331
+ statusCode >= 200 &&
332
+ statusCode <= 399
333
+ ) {
334
+ // Successful response to an unsafe method, delete it from cache
335
+ // https://www.rfc-editor.org/rfc/rfc9111.html#name-invalidating-stored-response
336
+ invalidateUnsafeRequest(this.#store, this.#cacheKey, resHeaders)
337
+ return downstreamOnHeaders()
338
+ }
339
+
340
+ // A credential-bearing request that was redirected must not have the final
341
+ // hop's response stored under the original request's key. The cache key
342
+ // does not include the credential, and the redirect target may vary per
343
+ // principal (e.g. `/me` -> `/profile/{user}`), so replaying the body under
344
+ // the alias key would leak one principal's data to another — or to an
345
+ // unauthenticated caller — from a shared cache. The final hop's permissive
346
+ // cache-control (e.g. `public`) does not authorize this: the credential was
347
+ // presented to the original hop, not the resource that actually produced
348
+ // the body. Fail closed. A history with more than one entry means at least
349
+ // one redirect was actually followed (a single non-redirected hop records
350
+ // exactly one entry), so a plain revalidation is not mistaken for a redirect.
351
+ if (
352
+ this.#history && this.#history.length > 1 &&
353
+ requestCarriesCredentials(this.#cacheKey.headers)
354
+ ) {
355
+ return downstreamOnHeaders()
356
+ }
357
+
358
+ // A cross-origin redirect can change the origin that actually produced the
359
+ // final response. Keying that response on the original (trusted) origin
360
+ // would let a redirect target control an entry for a URL the application
361
+ // trusts. Every hop in the redirect chain must stay on the cache key's
362
+ // origin. An intermediate cross-origin hop can bounce back to the trusted
363
+ // origin (or to any cacheable asset there) even when the final response
364
+ // comes from the trusted origin, so checking only the final URL is not
365
+ // enough.
366
+ if (this.#history && this.#history.length > 0) {
367
+ for (const hopUrl of this.#history) {
368
+ if (!sameOrigin(hopUrl.origin, this.#cacheKey.origin)) {
369
+ return downstreamOnHeaders()
370
+ }
371
+ }
372
+ }
373
+
374
+ // A redirect hop that is not forwarded can still carry a storage policy
375
+ // (for example `no-store`, `private` for a shared cache, `no-cache`,
376
+ // `must-revalidate`, zero freshness, or `Vary: *`) that the final response
377
+ // body does not inherit. Honor it, otherwise a redirect that must not be
378
+ // stored (or that selects a private/authenticated target) could be
379
+ // laundered into a cacheable final response under the original key.
380
+ const now = Date.now()
381
+ const redirectHopInfo = this.#history?.[kRedirectCacheControl]
382
+ const redirectPolicy = redirectHopInfo?.length > 0
383
+ ? getRedirectPolicy(
384
+ this.#cacheType,
385
+ this.#cacheKey.method,
386
+ redirectHopInfo,
387
+ this.#cacheKey.headers,
388
+ this.#cacheByDefault,
389
+ now
390
+ )
391
+ : undefined
392
+ if (redirectHopInfo?.length > 0 && redirectPolicy === undefined) {
393
+ return downstreamOnHeaders()
394
+ }
395
+
396
+ const cacheControlHeader = resHeaders['cache-control']
397
+ const cacheControlDirectives = cacheControlHeader ? parseCacheControlHeader(cacheControlHeader) : {}
398
+
399
+ if (revalidationResponseDisallowsCachedReuse(this.#cacheType, resHeaders, cacheControlDirectives)) {
400
+ deleteCachedValue(this.#store, this.#cacheKey)
401
+ return downstreamOnHeaders()
402
+ }
403
+
404
+ const heuristicallyCacheable = resHeaders['last-modified'] && arrayIncludes(HEURISTICALLY_CACHEABLE_STATUS_CODES, statusCode)
405
+ if (
406
+ !cacheControlHeader &&
407
+ !resHeaders['expires'] &&
408
+ !heuristicallyCacheable &&
409
+ !this.#cacheByDefault
410
+ ) {
411
+ if (statusCode === 304 && resHeaders.vary && isInvalidOrWildcardVaryHeader(resHeaders.vary)) {
412
+ deleteCachedValue(this.#store, this.#cacheKey)
413
+ }
414
+
415
+ // Don't have anything to tell us this response is cachable and we're not
416
+ // caching by default
417
+ return downstreamOnHeaders()
418
+ }
419
+
420
+ if (!canCacheResponse(this.#cacheType, this.#cacheKey.method, statusCode, resHeaders, cacheControlDirectives, this.#cacheKey.headers)) {
421
+ if (statusCode === 304 && (cacheControlHeader || revalidationResponseDisallowsCachedReuse(this.#cacheType, resHeaders, cacheControlDirectives))) {
422
+ deleteCachedValue(this.#store, this.#cacheKey)
423
+ }
424
+
425
+ return downstreamOnHeaders()
426
+ }
427
+
428
+ const resAge = Object.hasOwn(resHeaders, 'age') ? getAge(resHeaders.age) : undefined
429
+ if (resAge !== undefined && resAge >= MAX_RESPONSE_AGE) {
430
+ // Response considered stale
431
+ deleteCachedValueIfNotModified(statusCode, this.#store, this.#cacheKey)
432
+ return downstreamOnHeaders()
433
+ }
434
+
435
+ const resDate = Object.hasOwn(resHeaders, 'date') ? getDate(resHeaders.date) : undefined
436
+ if (resDate === null) {
437
+ deleteCachedValueIfNotModified(statusCode, this.#store, this.#cacheKey)
438
+ return downstreamOnHeaders()
439
+ }
440
+
441
+ const apparentAge = resDate ? Math.max(0, now - resDate.getTime()) : 0
442
+ const currentAge = Math.max(apparentAge, resAge ?? 0)
443
+
444
+ const hasValidator =
445
+ (typeof resHeaders.etag === 'string' && isEtagUsable(resHeaders.etag)) ||
446
+ typeof resHeaders['last-modified'] === 'string'
447
+
448
+ const staleAt =
449
+ determineStaleAt(this.#cacheType, now, resAge, resHeaders, resDate, cacheControlDirectives, hasValidator) ??
450
+ this.#cacheByDefault
451
+ // Zero freshness lifetime but a validator: stale from the start, yet still
452
+ // storable since each reuse is preceded by a revalidation request.
453
+ // https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.2.4
454
+ const revalidationOnly = staleAt === 0 && hasValidator
455
+ if (staleAt === undefined || (currentAge >= staleAt && !revalidationOnly)) {
456
+ if (cacheControlHeader || staleAt !== undefined) {
457
+ deleteCachedValueIfNotModified(statusCode, this.#store, this.#cacheKey)
458
+ }
459
+
460
+ return downstreamOnHeaders()
461
+ }
462
+
463
+ const baseTime = now - currentAge
464
+ let absoluteStaleAt = staleAt + baseTime
465
+ if (redirectPolicy !== undefined) {
466
+ absoluteStaleAt = Math.min(absoluteStaleAt, redirectPolicy.staleAt)
467
+ }
468
+ if (now >= absoluteStaleAt && !revalidationOnly) {
469
+ // Response is already stale
470
+ deleteCachedValueIfNotModified(statusCode, this.#store, this.#cacheKey)
471
+ return downstreamOnHeaders()
472
+ }
473
+
474
+ // Union the `Vary` of every suppressed redirect hop with the final
475
+ // response's `Vary`. A 3xx hop that negotiates its target on `Authorization`
476
+ // / `Cookie` (or any other header) must make the stored entry vary on those
477
+ // headers too, or an authenticated user's private response would be served
478
+ // to unrelated callers.
479
+ let varyDirectives
480
+ const combinedVary = []
481
+ if (redirectHopInfo) {
482
+ for (const hop of redirectHopInfo) {
483
+ appendVaryValues(combinedVary, hop?.headers?.vary)
484
+ }
485
+ }
486
+ appendVaryValues(combinedVary, resHeaders.vary)
487
+ if (combinedVary.length > 0) {
488
+ for (const varyHeader of combinedVary) {
489
+ if (hasVaryStar(varyHeader)) {
490
+ // `Vary: *` means the response can never be reused (RFC 9111 §4.1)
491
+ return downstreamOnHeaders()
492
+ }
493
+ }
494
+ if (this.#cacheKey.headers) {
495
+ varyDirectives = parseVaryHeader(combinedVary, this.#cacheKey.headers)
496
+ if (!varyDirectives) {
497
+ // Parse error
498
+ return downstreamOnHeaders()
499
+ }
500
+ }
501
+ }
502
+
503
+ // When the entry was folded from a redirect chain, the final response's
504
+ // stale-serving directives (RFC 5861) must not outlive the hops' freshness.
505
+ // A hop that grants no `stale-while-revalidate` / `stale-if-error` caps
506
+ // stale serving at its freshness, so the final response cannot launder a
507
+ // stale-serving window past the hop-bounded `staleAt`. Drop those directives
508
+ // (and keep the `proxy-revalidate` the hop policy may require) so the entry
509
+ // revalidates at `staleAt` instead of being served stale for the final
510
+ // response's window.
511
+ let effectiveCacheControlDirectives = cacheControlDirectives
512
+ if (redirectPolicy !== undefined) {
513
+ effectiveCacheControlDirectives = { ...cacheControlDirectives }
514
+ delete effectiveCacheControlDirectives['stale-while-revalidate']
515
+ delete effectiveCacheControlDirectives['stale-if-error']
516
+ if (redirectPolicy.requiresRevalidation === true) {
517
+ effectiveCacheControlDirectives['proxy-revalidate'] = true
518
+ }
519
+ }
520
+
521
+ const cachedAt = baseTime
522
+ const deleteAt = determineDeleteAt(baseTime, now, effectiveCacheControlDirectives, absoluteStaleAt)
523
+ const strippedHeaders = stripNecessaryHeaders(resHeaders, cacheControlDirectives)
524
+
525
+ /**
526
+ * @type {import('../../types/cache-interceptor.d.ts').default.CacheValue}
527
+ */
528
+ const value = {
529
+ statusCode,
530
+ statusMessage,
531
+ headers: strippedHeaders,
532
+ vary: varyDirectives,
533
+ cacheControlDirectives: effectiveCacheControlDirectives,
534
+ cachedAt,
535
+ staleAt: absoluteStaleAt,
536
+ deleteAt
537
+ }
538
+
539
+ // Not modified, re-use the cached value
540
+ // https://www.rfc-editor.org/rfc/rfc9111.html#name-handling-304-not-modified
541
+ if (statusCode === 304) {
542
+ let done = false
543
+ const finish304 = () => {
544
+ if (done) {
545
+ return
546
+ }
547
+ done = true
548
+ this.#downstreamController.release()
549
+ }
550
+
551
+ // The cached body below is replayed to the downstream handler from later
552
+ // stream ticks (or an iterator loop), outside the dispatcher's own error
553
+ // handling, so a throwing downstream handler must be converged to a
554
+ // per-request error instead of escaping as an uncaught exception.
555
+ const failReplay = (err) => {
556
+ if (done) {
557
+ return
558
+ }
559
+ done = true
560
+ // Abort the revalidation request: this releases the paused origin
561
+ // response (freeing the connection) and routes the error through
562
+ // CacheHandler.onResponseError, which destroys the cache write stream
563
+ // and forwards the error downstream exactly once.
564
+ controller.abort(err)
565
+ }
566
+
567
+ const handle304 = (cachedValue) => {
568
+ if (!cachedValue) {
569
+ // Do not create a new cache entry, as a 304 won't have a body - so cannot be cached.
570
+ downstreamOnHeaders()
571
+ finish304()
572
+ return
573
+ }
574
+
575
+ // Re-use the cached value: statuscode, statusmessage, headers and body
576
+ value.statusCode = cachedValue.statusCode
577
+ value.statusMessage = cachedValue.statusMessage
578
+ value.etag = cachedValue.etag
579
+ value.vary = varyDirectives ?? cachedValue.vary
580
+ value.headers = { ...cachedValue.headers, ...strippedHeaders }
581
+
582
+ downstreamOnHeaders()
583
+
584
+ this.#writeStream = this.#store.createWriteStream(this.#cacheKey, value)
585
+
586
+ if (!this.#writeStream || !cachedValue?.body) {
587
+ finish304()
588
+ return
589
+ }
590
+
591
+ if (typeof cachedValue.body.values === 'function') {
592
+ const bodyIterator = cachedValue.body.values()
593
+
594
+ const streamCachedBody = () => {
595
+ if (done) {
596
+ return
597
+ }
598
+ try {
599
+ for (const chunk of bodyIterator) {
600
+ const full = this.#writeStream?.write(chunk) === false
601
+ this.#handler.onResponseData?.(this.#downstreamController, chunk)
602
+ // when stream is full stop writing until we get a 'drain' event
603
+ if (full) {
604
+ return
605
+ }
606
+ }
607
+ finish304()
608
+ } catch (err) {
609
+ failReplay(err)
610
+ }
611
+ }
612
+
613
+ this.#writeStream
614
+ .on('error', function () {
615
+ handler.#writeStream = undefined
616
+ handler.#store.delete(handler.#cacheKey)
617
+ // Keep replaying downstream without the store
618
+ streamCachedBody()
619
+ })
620
+ .on('drain', () => {
621
+ streamCachedBody()
622
+ })
623
+ .on('close', function () {
624
+ if (handler.#writeStream === this) {
625
+ handler.#writeStream = undefined
626
+ // Keep replaying downstream without the store
627
+ streamCachedBody()
628
+ }
629
+ })
630
+
631
+ streamCachedBody()
632
+ } else if (typeof cachedValue.body.on === 'function') {
633
+ // Readable stream body (e.g. from async/remote cache stores)
634
+ cachedValue.body
635
+ .on('data', (chunk) => {
636
+ try {
637
+ this.#writeStream?.write(chunk)
638
+ this.#handler.onResponseData?.(this.#downstreamController, chunk)
639
+ } catch (err) {
640
+ cachedValue.body.destroy()
641
+ failReplay(err)
642
+ }
643
+ })
644
+ .on('end', () => {
645
+ this.#writeStream?.end()
646
+ finish304()
647
+ })
648
+ .on('error', () => {
649
+ this.#writeStream = undefined
650
+ this.#store.delete(this.#cacheKey)
651
+ finish304()
652
+ })
653
+
654
+ this.#writeStream
655
+ .on('error', function () {
656
+ handler.#writeStream = undefined
657
+ handler.#store.delete(handler.#cacheKey)
658
+ })
659
+ .on('close', function () {
660
+ if (handler.#writeStream === this) {
661
+ handler.#writeStream = undefined
662
+ }
663
+ })
664
+ } else {
665
+ finish304()
666
+ }
667
+ }
668
+
669
+ /**
670
+ * @type {import('../../types/cache-interceptor.d.ts').default.CacheValue}
671
+ */
672
+ const result = this.#store.get(this.#cacheKey)
673
+ // A 304 ends before the cached body is replayed. Keep the origin
674
+ // response paused until replay is complete.
675
+ this.#downstreamController.block()
676
+ controller.pause()
677
+ if (result && typeof result.then === 'function') {
678
+ result.then(handle304, () => handle304(undefined)).catch((err) => {
679
+ controller.abort(err)
680
+ })
681
+ } else {
682
+ handle304(result)
683
+ }
684
+ } else {
685
+ if (typeof resHeaders.etag === 'string' && isEtagUsable(resHeaders.etag)) {
686
+ value.etag = resHeaders.etag
687
+ }
688
+
689
+ this.#writeStream = this.#store.createWriteStream(this.#cacheKey, value)
690
+
691
+ if (!this.#writeStream) {
692
+ return downstreamOnHeaders()
693
+ }
694
+
695
+ this.#writeStream
696
+ .on('drain', () => controller.resume())
697
+ .on('error', function () {
698
+ // TODO (fix): Make error somehow observable?
699
+ handler.#writeStream = undefined
700
+
701
+ // Delete the value in case the cache store is holding onto state from
702
+ // the call to createWriteStream
703
+ handler.#store.delete(handler.#cacheKey)
704
+ })
705
+ .on('close', function () {
706
+ if (handler.#writeStream === this) {
707
+ handler.#writeStream = undefined
708
+ }
709
+
710
+ // TODO (fix): Should we resume even if was paused downstream?
711
+ controller.resume()
712
+ })
713
+
714
+ downstreamOnHeaders()
715
+ }
716
+ }
717
+
718
+ onResponseData (controller, chunk) {
719
+ if (this.#writeStream?.write(chunk) === false) {
720
+ controller.pause()
721
+ }
722
+
723
+ this.#handler.onResponseData?.(this.#downstreamController, chunk)
724
+ }
725
+
726
+ onResponseEnd (controller, trailers) {
727
+ this.#writeStream?.end()
728
+ this.#handler.onResponseEnd?.(this.#downstreamController, trailers)
729
+ }
730
+
731
+ onResponseError (controller, err) {
732
+ this.#writeStream?.destroy(err)
733
+ this.#writeStream = undefined
734
+ this.#handler.onResponseError?.(this.#downstreamController, err)
735
+ }
736
+ }
737
+
738
+ /**
739
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheStore} store
740
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} cacheKey
741
+ */
742
+ function deleteCachedValue (store, cacheKey) {
743
+ try {
744
+ store.delete(cacheKey)?.catch?.(noop)
745
+ } catch {
746
+ // Fail silently
747
+ }
748
+ }
749
+
750
+ function deleteCachedValueIfNotModified (statusCode, store, cacheKey) {
751
+ if (statusCode === 304) {
752
+ deleteCachedValue(store, cacheKey)
753
+ }
754
+ }
755
+
756
+ /**
757
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
758
+ * @param {import('../../types/header.d.ts').IncomingHttpHeaders} resHeaders
759
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} cacheControlDirectives
760
+ * @returns {boolean}
761
+ */
762
+ function revalidationResponseDisallowsCachedReuse (cacheType, resHeaders, cacheControlDirectives) {
763
+ return cacheControlDirectives['no-store'] === true ||
764
+ (cacheType === 'shared' && (
765
+ cacheControlDirectives.private === true ||
766
+ Object.hasOwn(resHeaders, 'set-cookie')
767
+ )) ||
768
+ (resHeaders.vary ? isInvalidOrWildcardVaryHeader(resHeaders.vary) : false)
769
+ }
770
+
771
+ /**
772
+ * @see https://www.rfc-editor.org/rfc/rfc9111.html#name-storing-responses-to-authen
773
+ *
774
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
775
+ * @param {string} method
776
+ * @param {number} statusCode
777
+ * @param {import('../../types/header.d.ts').IncomingHttpHeaders} resHeaders
778
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} cacheControlDirectives
779
+ * @param {import('../../types/header.d.ts').IncomingHttpHeaders} [reqHeaders]
780
+ */
781
+ function canCacheResponse (cacheType, method, statusCode, resHeaders, cacheControlDirectives, reqHeaders) {
782
+ if (!arrayIncludes(util.safeHTTPMethods, method)) {
783
+ return false
784
+ }
785
+ // Status code must be final and understood.
786
+ if (statusCode < 200 || arrayIncludes(NOT_UNDERSTOOD_STATUS_CODES, statusCode)) {
787
+ return false
788
+ }
789
+ // Responses with neither status codes that are heuristically cacheable, nor "explicit enough" caching
790
+ // directives, are not cacheable. "Explicit enough": see https://www.rfc-editor.org/rfc/rfc9111.html#section-3
791
+ if (!arrayIncludes(HEURISTICALLY_CACHEABLE_STATUS_CODES, statusCode) && !resHeaders['expires'] &&
792
+ !cacheControlDirectives.public &&
793
+ cacheControlDirectives['max-age'] === undefined &&
794
+ // RFC 9111: a private response directive, if the cache is not shared
795
+ !(cacheControlDirectives.private && cacheType === 'private') &&
796
+ !(cacheControlDirectives['s-maxage'] !== undefined && cacheType === 'shared')
797
+ ) {
798
+ return false
799
+ }
800
+
801
+ if (cacheControlDirectives['no-store']) {
802
+ return false
803
+ }
804
+
805
+ if (cacheType === 'shared' && (
806
+ cacheControlDirectives.private === true ||
807
+ Object.hasOwn(resHeaders, 'set-cookie')
808
+ )) {
809
+ return false
810
+ }
811
+
812
+ // https://www.rfc-editor.org/rfc/rfc9111.html#section-4.1-5
813
+ if (resHeaders.vary && hasVaryStar(resHeaders.vary)) {
814
+ return false
815
+ }
816
+
817
+ // https://www.rfc-editor.org/rfc/rfc9111.html#name-storing-responses-to-authen
818
+ if (reqHeaders != null && Object.hasOwn(reqHeaders, 'authorization')) {
819
+ if (
820
+ !cacheControlDirectives.public &&
821
+ !cacheControlDirectives['s-maxage'] &&
822
+ !cacheControlDirectives['must-revalidate']
823
+ ) {
824
+ return false
825
+ }
826
+
827
+ if (typeof reqHeaders.authorization !== 'string') {
828
+ return false
829
+ }
830
+
831
+ if (
832
+ Array.isArray(cacheControlDirectives['no-cache']) &&
833
+ arrayIncludes(cacheControlDirectives['no-cache'], 'authorization')
834
+ ) {
835
+ return false
836
+ }
837
+
838
+ if (
839
+ Array.isArray(cacheControlDirectives['private']) &&
840
+ arrayIncludes(cacheControlDirectives['private'], 'authorization')
841
+ ) {
842
+ return false
843
+ }
844
+ }
845
+
846
+ return true
847
+ }
848
+
849
+ /**
850
+ * Determine the absolute freshness deadline imposed by all suppressed redirect
851
+ * hops. Returning `undefined` means at least one hop cannot safely be folded
852
+ * into the final response under the original request key.
853
+ *
854
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
855
+ * @param {string} method
856
+ * @param {RedirectHop[]} redirectHops
857
+ * @param {import('../../types/header.d.ts').IncomingHttpHeaders} reqHeaders
858
+ * @param {number | undefined} cacheByDefault
859
+ * @param {number} now
860
+ * @returns {{ staleAt: number, requiresRevalidation: boolean } | undefined}
861
+ */
862
+ function getRedirectPolicy (cacheType, method, redirectHops, reqHeaders, cacheByDefault, now) {
863
+ let staleAt = Infinity
864
+ let requiresRevalidation = false
865
+
866
+ for (const hop of redirectHops) {
867
+ const hopPolicy = getRedirectHopPolicy(cacheType, method, hop, reqHeaders, cacheByDefault)
868
+ if (hopPolicy === undefined || hopPolicy.staleAt <= now) {
869
+ return undefined
870
+ }
871
+
872
+ staleAt = Math.min(staleAt, hopPolicy.staleAt)
873
+ requiresRevalidation ||= hopPolicy.requiresRevalidation
874
+ }
875
+
876
+ return { staleAt, requiresRevalidation }
877
+ }
878
+
879
+ /**
880
+ * Calculate one redirect hop's absolute freshness deadline using the same age,
881
+ * date, expiration, invalid-directive, and heuristic rules as a final response.
882
+ *
883
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
884
+ * @param {string} method
885
+ * @param {RedirectHop} hop
886
+ * @param {import('../../types/header.d.ts').IncomingHttpHeaders} reqHeaders
887
+ * @param {number | undefined} cacheByDefault
888
+ * @returns {{ staleAt: number, requiresRevalidation: boolean } | undefined}
889
+ */
890
+ function getRedirectHopPolicy (cacheType, method, hop, reqHeaders, cacheByDefault) {
891
+ if (!Number.isInteger(hop?.statusCode) || !Number.isFinite(hop?.receivedAt)) {
892
+ return undefined
893
+ }
894
+
895
+ const hopHeaders = hop.headers ?? {}
896
+ const cacheControlHeader = hopHeaders['cache-control']
897
+ const hopCacheControl = cacheControlHeader
898
+ ? parseCacheControlHeader(cacheControlHeader)
899
+ : {}
900
+
901
+ // https://www.rfc-editor.org/rfc/rfc9111.html#section-4.1-5
902
+ if (hopHeaders.vary && hasVaryStar(hopHeaders.vary)) {
903
+ return undefined
904
+ }
905
+
906
+ if (hopCacheControl['no-store'] === true) {
907
+ return undefined
908
+ }
909
+
910
+ // A qualified `private` directive still marks the named fields as private.
911
+ // Folding a redirect that says `private="location"` would preserve the
912
+ // private target choice in the final body even though a shared cache is not
913
+ // allowed to store that Location field.
914
+ if (cacheType === 'shared' && Object.hasOwn(hopCacheControl, 'private')) {
915
+ return undefined
916
+ }
917
+
918
+ // These directives require the hop to be revalidated before reuse. Since the
919
+ // cache stores only the final response, it cannot independently validate the
920
+ // suppressed redirect response.
921
+ if (
922
+ Object.hasOwn(hopCacheControl, 'no-cache') ||
923
+ hopCacheControl['must-revalidate'] === true ||
924
+ (cacheType === 'shared' && hopCacheControl['proxy-revalidate'] === true)
925
+ ) {
926
+ return undefined
927
+ }
928
+
929
+ const heuristicallyCacheable =
930
+ hopHeaders['last-modified'] &&
931
+ arrayIncludes(HEURISTICALLY_CACHEABLE_STATUS_CODES, hop.statusCode)
932
+ if (
933
+ !cacheControlHeader &&
934
+ !hopHeaders.expires &&
935
+ !heuristicallyCacheable &&
936
+ !cacheByDefault
937
+ ) {
938
+ return undefined
939
+ }
940
+
941
+ if (!canCacheResponse(cacheType, method, hop.statusCode, hopHeaders, hopCacheControl, reqHeaders)) {
942
+ return undefined
943
+ }
944
+
945
+ const age = Object.hasOwn(hopHeaders, 'age') ? getAge(hopHeaders.age) : undefined
946
+ if (age !== undefined && age >= MAX_RESPONSE_AGE) {
947
+ return undefined
948
+ }
949
+
950
+ const responseDate = Object.hasOwn(hopHeaders, 'date') ? getDate(hopHeaders.date) : undefined
951
+ if (responseDate === null) {
952
+ return undefined
953
+ }
954
+
955
+ const apparentAge = responseDate ? Math.max(0, hop.receivedAt - responseDate.getTime()) : 0
956
+ const currentAge = Math.max(apparentAge, age ?? 0)
957
+ const hasValidator =
958
+ (typeof hopHeaders.etag === 'string' && isEtagUsable(hopHeaders.etag)) ||
959
+ typeof hopHeaders['last-modified'] === 'string'
960
+ const freshnessLifetime =
961
+ determineStaleAt(
962
+ cacheType,
963
+ hop.receivedAt,
964
+ age,
965
+ hopHeaders,
966
+ responseDate,
967
+ hopCacheControl,
968
+ hasValidator
969
+ ) ?? cacheByDefault
970
+
971
+ if (freshnessLifetime === undefined) {
972
+ return undefined
973
+ }
974
+
975
+ return {
976
+ staleAt: hop.receivedAt - currentAge + freshnessLifetime,
977
+ // RFC 9111 §5.2.2.10: s-maxage implies proxy-revalidate for shared caches.
978
+ requiresRevalidation: cacheType === 'shared' && Object.hasOwn(hopCacheControl, 's-maxage')
979
+ }
980
+ }
981
+
982
+ /**
983
+ * @param {string | string[]} dateHeader
984
+ * @returns {Date | null | undefined}
985
+ */
986
+ function getDate (dateHeader) {
987
+ let dateValue = dateHeader
988
+ if (Array.isArray(dateValue)) {
989
+ if (dateValue.length !== 1) {
990
+ return null
991
+ }
992
+
993
+ dateValue = dateValue[0]
994
+ }
995
+
996
+ if (typeof dateValue !== 'string') {
997
+ return null
998
+ }
999
+
1000
+ return parseHttpDate(dateValue)
1001
+ }
1002
+
1003
+ /**
1004
+ * @param {string | string[]} ageHeader
1005
+ * @returns {number | undefined}
1006
+ */
1007
+ function getAge (ageHeader) {
1008
+ let ageValue = ageHeader
1009
+ if (Array.isArray(ageValue)) {
1010
+ if (ageValue.length !== 1) {
1011
+ return MAX_RESPONSE_AGE
1012
+ }
1013
+
1014
+ ageValue = ageValue[0]
1015
+ }
1016
+
1017
+ if (typeof ageValue !== 'string' || !/^[\t ]*[0-9]+[\t ]*$/.test(ageValue)) {
1018
+ return MAX_RESPONSE_AGE
1019
+ }
1020
+
1021
+ const age = BigInt(ageValue.replace(/^[\t ]+|[\t ]+$/g, ''))
1022
+ if (age >= BigInt(MAX_RESPONSE_AGE / 1000)) {
1023
+ return MAX_RESPONSE_AGE
1024
+ }
1025
+
1026
+ return Number(age) * 1000
1027
+ }
1028
+
1029
+ /**
1030
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheOptions['type']} cacheType
1031
+ * @param {number} now
1032
+ * @param {number | undefined} age
1033
+ * @param {import('../../types/header.d.ts').IncomingHttpHeaders} resHeaders
1034
+ * @param {Date | undefined} responseDate
1035
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} cacheControlDirectives
1036
+ * @param {boolean} hasValidator whether the response has a validator (etag or
1037
+ * last-modified) that revalidation requests can be made with
1038
+ *
1039
+ * @returns {number | undefined} time that the value is stale at in seconds or undefined if it shouldn't be cached
1040
+ */
1041
+ function determineStaleAt (cacheType, now, age, resHeaders, responseDate, cacheControlDirectives, hasValidator) {
1042
+ if (cacheType === 'shared') {
1043
+ // Prioritize s-maxage since we're a shared cache
1044
+ // s-maxage > max-age > Expire
1045
+ // https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.2.10-3
1046
+ if (hasInvalidCacheControlDirective(cacheControlDirectives, 's-maxage')) {
1047
+ return 0
1048
+ }
1049
+
1050
+ const sMaxAge = cacheControlDirectives['s-maxage']
1051
+ if (sMaxAge !== undefined) {
1052
+ if (sMaxAge > 0) {
1053
+ return sMaxAge * 1000
1054
+ }
1055
+
1056
+ // Immediately stale, but storable if we can revalidate it before reuse.
1057
+ return 0
1058
+ }
1059
+ }
1060
+
1061
+ if (hasInvalidCacheControlDirective(cacheControlDirectives, 'max-age')) {
1062
+ return 0
1063
+ }
1064
+
1065
+ const maxAge = cacheControlDirectives['max-age']
1066
+ if (maxAge !== undefined) {
1067
+ if (maxAge > 0) {
1068
+ return maxAge * 1000
1069
+ }
1070
+
1071
+ // Immediately stale, but storable if we can revalidate it before reuse.
1072
+ return 0
1073
+ }
1074
+
1075
+ if (Object.hasOwn(resHeaders, 'expires')) {
1076
+ // https://www.rfc-editor.org/rfc/rfc9111.html#section-5.3
1077
+ if (typeof resHeaders.expires !== 'string') {
1078
+ return 0
1079
+ }
1080
+
1081
+ const expiresDate = parseHttpDate(resHeaders.expires)
1082
+ if (!expiresDate) {
1083
+ return 0
1084
+ }
1085
+
1086
+ if (now >= expiresDate.getTime()) {
1087
+ return 0
1088
+ }
1089
+
1090
+ if (responseDate) {
1091
+ if (responseDate >= expiresDate) {
1092
+ return 0
1093
+ }
1094
+
1095
+ const freshnessLifetime = expiresDate.getTime() - responseDate.getTime()
1096
+ if (age !== undefined && age >= freshnessLifetime) {
1097
+ return 0
1098
+ }
1099
+
1100
+ return freshnessLifetime
1101
+ }
1102
+
1103
+ return expiresDate.getTime() - now
1104
+ }
1105
+
1106
+ if (typeof resHeaders['last-modified'] === 'string') {
1107
+ // https://www.rfc-editor.org/rfc/rfc9111.html#name-calculating-heuristic-fresh
1108
+ const lastModified = parseHttpDate(resHeaders['last-modified'])
1109
+ if (lastModified) {
1110
+ if (lastModified.getTime() >= now) {
1111
+ return undefined
1112
+ }
1113
+
1114
+ const responseAge = now - lastModified.getTime()
1115
+
1116
+ return responseAge * 0.1
1117
+ }
1118
+ }
1119
+
1120
+ if (cacheControlDirectives.immutable) {
1121
+ // https://www.rfc-editor.org/rfc/rfc8246.html#section-2.2
1122
+ return 31536000000
1123
+ }
1124
+
1125
+ if (cacheControlDirectives['no-cache'] === true && hasValidator) {
1126
+ // No freshness source, but a validator lets us revalidate before reuse.
1127
+ // https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.2.4
1128
+ return 0
1129
+ }
1130
+
1131
+ return undefined
1132
+ }
1133
+
1134
+ /**
1135
+ * @param {number} baseTime
1136
+ * @param {number} cachedAt
1137
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} cacheControlDirectives
1138
+ * @param {number} staleAt
1139
+ */
1140
+ function determineDeleteAt (baseTime, cachedAt, cacheControlDirectives, staleAt) {
1141
+ let staleWhileRevalidate = -Infinity
1142
+ let staleIfError = -Infinity
1143
+ let immutable = -Infinity
1144
+
1145
+ if (cacheControlDirectives['stale-while-revalidate']) {
1146
+ staleWhileRevalidate = staleAt + (cacheControlDirectives['stale-while-revalidate'] * 1000)
1147
+ }
1148
+
1149
+ if (cacheControlDirectives['stale-if-error']) {
1150
+ staleIfError = staleAt + (cacheControlDirectives['stale-if-error'] * 1000)
1151
+ }
1152
+
1153
+ if (cacheControlDirectives.immutable && staleWhileRevalidate === -Infinity && staleIfError === -Infinity) {
1154
+ immutable = cachedAt + 31536000000
1155
+ }
1156
+
1157
+ // When no stale directives or immutable flag, add a revalidation buffer
1158
+ // equal to the freshness lifetime so the entry survives past staleAt long
1159
+ // enough to be revalidated instead of silently disappearing.
1160
+ //
1161
+ // Response Date headers only have second precision, so baseTime can trail the
1162
+ // actual cache insertion time by up to ~1s. Pad the buffer by that bounded
1163
+ // skew so short-lived entries do not disappear exactly when they should be
1164
+ // revalidated.
1165
+ if (staleWhileRevalidate === -Infinity && staleIfError === -Infinity && immutable === -Infinity) {
1166
+ const freshnessLifetime = staleAt - baseTime
1167
+ if (freshnessLifetime <= 0) {
1168
+ // Revalidation-only entry: no freshness lifetime to size the buffer on,
1169
+ // so retain it for a bounded window instead.
1170
+ return cachedAt + REVALIDATION_ONLY_RETENTION
1171
+ }
1172
+ const datePrecisionPadding = Math.min(Math.max(cachedAt - baseTime, 0), 1000)
1173
+ return staleAt + freshnessLifetime + datePrecisionPadding
1174
+ }
1175
+
1176
+ return Math.max(staleAt, staleWhileRevalidate, staleIfError, immutable)
1177
+ }
1178
+
1179
+ /**
1180
+ * Strips headers required to be removed in cached responses
1181
+ * @param {import('../../types/header.d.ts').IncomingHttpHeaders} resHeaders
1182
+ * @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} cacheControlDirectives
1183
+ * @returns {Record<string, string | string []>}
1184
+ */
1185
+ function stripNecessaryHeaders (resHeaders, cacheControlDirectives) {
1186
+ const headersToRemove = [
1187
+ 'connection',
1188
+ 'proxy-authenticate',
1189
+ 'proxy-authentication-info',
1190
+ 'proxy-authorization',
1191
+ 'proxy-connection',
1192
+ 'te',
1193
+ 'transfer-encoding',
1194
+ 'upgrade',
1195
+ // We'll add age back when serving it
1196
+ 'age'
1197
+ ]
1198
+
1199
+ if (resHeaders['connection']) {
1200
+ appendConnectionHeaderTokens(headersToRemove, resHeaders['connection'])
1201
+ }
1202
+
1203
+ if (Array.isArray(cacheControlDirectives['no-cache'])) {
1204
+ headersToRemove.push(...cacheControlDirectives['no-cache'])
1205
+ }
1206
+
1207
+ if (Array.isArray(cacheControlDirectives['private'])) {
1208
+ headersToRemove.push(...cacheControlDirectives['private'])
1209
+ }
1210
+
1211
+ let strippedHeaders
1212
+ for (const headerName of headersToRemove) {
1213
+ if (Object.hasOwn(resHeaders, headerName)) {
1214
+ strippedHeaders ??= { ...resHeaders }
1215
+ delete strippedHeaders[headerName]
1216
+ }
1217
+ }
1218
+
1219
+ return strippedHeaders ?? resHeaders
1220
+ }
1221
+
1222
+ module.exports = CacheHandler