@rangojs/router 0.0.0-experimental.14 → 0.0.0-experimental.141

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 (460) hide show
  1. package/AGENTS.md +17 -0
  2. package/README.md +432 -7
  3. package/dist/bin/rango.js +2073 -213
  4. package/dist/testing/vitest.js +82 -0
  5. package/dist/vite/index.js +7258 -2714
  6. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  7. package/package.json +140 -67
  8. package/skills/api-client/SKILL.md +211 -0
  9. package/skills/breadcrumbs/SKILL.md +329 -0
  10. package/skills/bundle-analysis/SKILL.md +159 -0
  11. package/skills/cache-guide/SKILL.md +487 -0
  12. package/skills/caching/SKILL.md +357 -25
  13. package/skills/comparison/SKILL.md +50 -0
  14. package/skills/comparison/agents/openai.yaml +4 -0
  15. package/skills/comparison/references/framework-comparison.md +837 -0
  16. package/skills/composability/SKILL.md +246 -0
  17. package/skills/css/SKILL.md +76 -0
  18. package/skills/debug-manifest/SKILL.md +16 -10
  19. package/skills/document-cache/SKILL.md +87 -62
  20. package/skills/fonts/SKILL.md +6 -4
  21. package/skills/handler-use/SKILL.md +364 -0
  22. package/skills/hooks/SKILL.md +557 -79
  23. package/skills/host-router/SKILL.md +320 -0
  24. package/skills/i18n/SKILL.md +276 -0
  25. package/skills/intercept/SKILL.md +207 -15
  26. package/skills/layout/SKILL.md +146 -6
  27. package/skills/links/SKILL.md +304 -25
  28. package/skills/loader/SKILL.md +616 -54
  29. package/skills/middleware/SKILL.md +217 -37
  30. package/skills/migrate-nextjs/SKILL.md +611 -0
  31. package/skills/migrate-react-router/SKILL.md +927 -0
  32. package/skills/mime-routes/SKILL.md +42 -11
  33. package/skills/observability/SKILL.md +194 -0
  34. package/skills/parallel/SKILL.md +284 -3
  35. package/skills/ppr/SKILL.md +293 -0
  36. package/skills/prerender/SKILL.md +437 -52
  37. package/skills/rango/SKILL.md +369 -22
  38. package/skills/react-compiler/SKILL.md +168 -0
  39. package/skills/response-routes/SKILL.md +263 -121
  40. package/skills/route/SKILL.md +350 -21
  41. package/skills/router-setup/SKILL.md +246 -33
  42. package/skills/scripts/SKILL.md +179 -0
  43. package/skills/server-actions/SKILL.md +775 -0
  44. package/skills/shell-manifest/SKILL.md +185 -0
  45. package/skills/streams-and-websockets/SKILL.md +283 -0
  46. package/skills/tailwind/SKILL.md +27 -3
  47. package/skills/testing/SKILL.md +126 -222
  48. package/skills/testing/bindings.md +103 -0
  49. package/skills/testing/cache-prerender.md +127 -0
  50. package/skills/testing/client-components.md +124 -0
  51. package/skills/testing/e2e-parity.md +125 -0
  52. package/skills/testing/flight.md +91 -0
  53. package/skills/testing/handles.md +131 -0
  54. package/skills/testing/loader.md +128 -0
  55. package/skills/testing/middleware.md +99 -0
  56. package/skills/testing/render-handler.md +122 -0
  57. package/skills/testing/response-routes.md +95 -0
  58. package/skills/testing/reverse-and-types.md +85 -0
  59. package/skills/testing/server-actions.md +107 -0
  60. package/skills/testing/server-tree.md +128 -0
  61. package/skills/testing/setup.md +123 -0
  62. package/skills/theme/SKILL.md +9 -8
  63. package/skills/typesafety/SKILL.md +532 -103
  64. package/skills/use-cache/SKILL.md +367 -0
  65. package/skills/vercel/SKILL.md +128 -0
  66. package/skills/view-transitions/SKILL.md +337 -0
  67. package/src/__augment-tests__/augment.ts +81 -0
  68. package/src/__augment-tests__/augmented.check.ts +116 -0
  69. package/src/__internal.ts +77 -44
  70. package/src/bin/rango.ts +312 -15
  71. package/src/browser/action-coordinator.ts +114 -0
  72. package/src/browser/action-fence.ts +47 -0
  73. package/src/browser/app-shell.ts +39 -0
  74. package/src/browser/app-version.ts +14 -0
  75. package/src/browser/connection-warmup.ts +134 -0
  76. package/src/browser/cookie-name.ts +140 -0
  77. package/src/browser/event-controller.ts +293 -202
  78. package/src/browser/history-state.ts +101 -0
  79. package/src/browser/index.ts +3 -3
  80. package/src/browser/intercept-utils.ts +52 -0
  81. package/src/browser/invalidate-client-cache.ts +52 -0
  82. package/src/browser/link-interceptor.ts +24 -4
  83. package/src/browser/logging.ts +11 -0
  84. package/src/browser/merge-segment-loaders.ts +20 -12
  85. package/src/browser/navigation-bridge.ts +385 -576
  86. package/src/browser/navigation-client.ts +245 -75
  87. package/src/browser/navigation-store-handle.ts +38 -0
  88. package/src/browser/navigation-store.ts +184 -118
  89. package/src/browser/navigation-transaction.ts +247 -0
  90. package/src/browser/network-error-handler.ts +88 -0
  91. package/src/browser/partial-update.ts +412 -364
  92. package/src/browser/prefetch/cache.ts +359 -0
  93. package/src/browser/prefetch/fetch.ts +452 -0
  94. package/src/browser/prefetch/observer.ts +65 -0
  95. package/src/browser/prefetch/policy.ts +48 -0
  96. package/src/browser/prefetch/queue.ts +209 -0
  97. package/src/browser/prefetch/resource-ready.ts +77 -0
  98. package/src/browser/rango-state.ts +194 -0
  99. package/src/browser/react/Link.tsx +275 -68
  100. package/src/browser/react/NavigationProvider.tsx +265 -109
  101. package/src/browser/react/ScrollRestoration.tsx +10 -6
  102. package/src/browser/react/context.ts +11 -0
  103. package/src/browser/react/filter-segment-order.ts +70 -0
  104. package/src/browser/react/index.ts +0 -48
  105. package/src/browser/react/location-state-shared.ts +272 -60
  106. package/src/browser/react/location-state.ts +90 -20
  107. package/src/browser/react/mount-context.ts +6 -1
  108. package/src/browser/react/nonce-context.ts +23 -0
  109. package/src/browser/react/shallow-equal.ts +27 -0
  110. package/src/browser/react/use-action.ts +35 -66
  111. package/src/browser/react/use-handle.ts +39 -126
  112. package/src/browser/react/use-href.tsx +8 -1
  113. package/src/browser/react/use-link-status.ts +39 -13
  114. package/src/browser/react/use-navigation.ts +53 -69
  115. package/src/browser/react/use-params.ts +75 -0
  116. package/src/browser/react/use-pathname.ts +47 -0
  117. package/src/browser/react/use-reverse.ts +106 -0
  118. package/src/browser/react/use-router.ts +98 -0
  119. package/src/browser/react/use-search-params.ts +51 -0
  120. package/src/browser/react/use-segments.ts +72 -99
  121. package/src/browser/response-adapter.ts +164 -0
  122. package/src/browser/rsc-router.tsx +300 -72
  123. package/src/browser/scroll-restoration.ts +138 -50
  124. package/src/browser/segment-reconciler.ts +243 -0
  125. package/src/browser/segment-structure-assert.ts +17 -1
  126. package/src/browser/server-action-bridge.ts +668 -613
  127. package/src/browser/types.ts +223 -51
  128. package/src/browser/validate-redirect-origin.ts +56 -0
  129. package/src/build/collect-fallback-refs.ts +107 -0
  130. package/src/build/generate-manifest.ts +252 -161
  131. package/src/build/generate-route-types.ts +41 -1038
  132. package/src/build/index.ts +12 -7
  133. package/src/build/prefix-tree-utils.ts +123 -0
  134. package/src/build/route-trie.ts +225 -42
  135. package/src/build/route-types/ast-helpers.ts +25 -0
  136. package/src/build/route-types/ast-route-extraction.ts +105 -0
  137. package/src/build/route-types/codegen.ts +113 -0
  138. package/src/build/route-types/include-resolution.ts +812 -0
  139. package/src/build/route-types/param-extraction.ts +51 -0
  140. package/src/build/route-types/per-module-writer.ts +144 -0
  141. package/src/build/route-types/router-processing.ts +695 -0
  142. package/src/build/route-types/scan-filter.ts +85 -0
  143. package/src/build/route-types/source-scan.ts +216 -0
  144. package/src/build/runtime-discovery.ts +223 -0
  145. package/src/cache/background-task.ts +34 -0
  146. package/src/cache/cache-error.ts +104 -0
  147. package/src/cache/cache-key-utils.ts +60 -0
  148. package/src/cache/cache-policy.ts +199 -0
  149. package/src/cache/cache-runtime.ts +525 -0
  150. package/src/cache/cache-scope.ts +298 -332
  151. package/src/cache/cache-tag.ts +103 -0
  152. package/src/cache/cf/cf-base64.ts +33 -0
  153. package/src/cache/cf/cf-cache-constants.ts +127 -0
  154. package/src/cache/cf/cf-cache-store.ts +2508 -158
  155. package/src/cache/cf/cf-cache-types.ts +349 -0
  156. package/src/cache/cf/cf-kv-utils.ts +46 -0
  157. package/src/cache/cf/cf-tag-marker-memo.ts +105 -0
  158. package/src/cache/cf/index.ts +17 -17
  159. package/src/cache/document-cache.ts +199 -92
  160. package/src/cache/handle-capture.ts +81 -0
  161. package/src/cache/handle-snapshot.ts +111 -0
  162. package/src/cache/index.ts +24 -35
  163. package/src/cache/memory-segment-store.ts +363 -30
  164. package/src/cache/profile-registry.ts +88 -0
  165. package/src/cache/read-through-swr.ts +178 -0
  166. package/src/cache/segment-codec.ts +248 -0
  167. package/src/cache/shell-snapshot.ts +368 -0
  168. package/src/cache/tag-invalidation.ts +230 -0
  169. package/src/cache/taint.ts +153 -0
  170. package/src/cache/types.ts +222 -211
  171. package/src/cache/vercel/index.ts +11 -0
  172. package/src/cache/vercel/vercel-cache-store.ts +1113 -0
  173. package/src/client.rsc.tsx +43 -21
  174. package/src/client.tsx +131 -347
  175. package/src/cloudflare/index.ts +11 -0
  176. package/src/cloudflare/tracing.ts +109 -0
  177. package/src/component-utils.ts +23 -4
  178. package/src/components/DefaultDocument.tsx +13 -3
  179. package/src/context-var.ts +168 -0
  180. package/src/debug.ts +19 -9
  181. package/src/decode-loader-results.ts +52 -0
  182. package/src/defer.ts +185 -0
  183. package/src/deps/ssr.ts +0 -1
  184. package/src/encode-kv.ts +49 -0
  185. package/src/errors.ts +106 -10
  186. package/src/escape-script.ts +52 -0
  187. package/src/handle.ts +110 -35
  188. package/src/handles/MetaTags.tsx +83 -59
  189. package/src/handles/Scripts.tsx +183 -0
  190. package/src/handles/breadcrumbs.ts +93 -0
  191. package/src/handles/deferred-resolution.ts +127 -0
  192. package/src/handles/is-thenable.ts +18 -0
  193. package/src/handles/meta.ts +44 -53
  194. package/src/handles/script.ts +244 -0
  195. package/src/host/cookie-handler.ts +20 -65
  196. package/src/host/errors.ts +21 -30
  197. package/src/host/index.ts +13 -9
  198. package/src/host/pattern-matcher.ts +50 -79
  199. package/src/host/router.ts +151 -121
  200. package/src/host/testing.ts +45 -32
  201. package/src/host/types.ts +52 -11
  202. package/src/host/utils.ts +2 -2
  203. package/src/href-client.ts +192 -57
  204. package/src/index.rsc.ts +173 -35
  205. package/src/index.ts +241 -73
  206. package/src/internal-debug.ts +9 -2
  207. package/src/loader-store.ts +500 -0
  208. package/src/loader.rsc.ts +31 -99
  209. package/src/loader.ts +30 -12
  210. package/src/missing-id-error.ts +68 -0
  211. package/src/outlet-context.ts +1 -1
  212. package/src/outlet-provider.tsx +41 -0
  213. package/src/prerender/param-hash.ts +16 -14
  214. package/src/prerender/store.ts +121 -21
  215. package/src/prerender.ts +460 -26
  216. package/src/redirect-origin.ts +100 -0
  217. package/src/regex-escape.ts +8 -0
  218. package/src/render-error-thrower.tsx +20 -0
  219. package/src/response-utils.ts +62 -0
  220. package/src/reverse.ts +198 -128
  221. package/src/root-error-boundary.tsx +42 -48
  222. package/src/route-content-wrapper.tsx +22 -77
  223. package/src/route-definition/dsl-helpers.ts +1116 -0
  224. package/src/route-definition/helper-factories.ts +88 -0
  225. package/src/route-definition/helpers-types.ts +505 -0
  226. package/src/route-definition/index.ts +54 -0
  227. package/src/route-definition/redirect.ts +134 -0
  228. package/src/route-definition/resolve-handler-use.ts +160 -0
  229. package/src/route-definition/use-item-types.ts +29 -0
  230. package/src/route-definition.ts +1 -1481
  231. package/src/route-map-builder.ts +82 -144
  232. package/src/route-name.ts +53 -0
  233. package/src/route-types.ts +71 -45
  234. package/src/router/basename.ts +14 -0
  235. package/src/router/content-negotiation.ts +263 -0
  236. package/src/router/debug-manifest.ts +72 -0
  237. package/src/router/error-handling.ts +54 -27
  238. package/src/router/find-match.ts +245 -0
  239. package/src/router/handler-context.ts +377 -125
  240. package/src/router/instrument.ts +350 -0
  241. package/src/router/intercept-resolution.ts +59 -28
  242. package/src/router/lazy-includes.ts +254 -0
  243. package/src/router/loader-resolution.ts +421 -157
  244. package/src/router/logging.ts +106 -6
  245. package/src/router/manifest.ts +131 -57
  246. package/src/router/match-api.ts +167 -246
  247. package/src/router/match-context.ts +4 -24
  248. package/src/router/match-handlers.ts +440 -0
  249. package/src/router/match-middleware/background-revalidation.ts +117 -93
  250. package/src/router/match-middleware/cache-lookup.ts +297 -150
  251. package/src/router/match-middleware/cache-store.ts +123 -51
  252. package/src/router/match-middleware/intercept-resolution.ts +44 -43
  253. package/src/router/match-middleware/segment-resolution.ts +64 -22
  254. package/src/router/match-pipelines.ts +11 -87
  255. package/src/router/match-result.ts +121 -50
  256. package/src/router/metrics.ts +219 -28
  257. package/src/router/middleware-types.ts +93 -0
  258. package/src/router/middleware.ts +505 -441
  259. package/src/router/navigation-snapshot.ts +133 -0
  260. package/src/router/params-util.ts +23 -0
  261. package/src/router/parse-pattern.ts +115 -0
  262. package/src/router/pattern-matching.ts +311 -142
  263. package/src/router/prefetch-cache-ttl.ts +51 -0
  264. package/src/router/prefetch-limits.ts +37 -0
  265. package/src/router/prerender-match.ts +547 -0
  266. package/src/router/preview-match.ts +102 -0
  267. package/src/router/request-classification.ts +278 -0
  268. package/src/router/revalidation.ts +203 -62
  269. package/src/router/route-snapshot.ts +246 -0
  270. package/src/router/router-context.ts +45 -48
  271. package/src/router/router-interfaces.ts +554 -0
  272. package/src/router/router-options.ts +779 -0
  273. package/src/router/router-registry.ts +21 -0
  274. package/src/router/segment-resolution/fresh.ts +772 -0
  275. package/src/router/segment-resolution/helpers.ts +348 -0
  276. package/src/router/segment-resolution/loader-cache.ts +250 -0
  277. package/src/router/segment-resolution/loader-mask.ts +44 -0
  278. package/src/router/segment-resolution/revalidation.ts +1331 -0
  279. package/src/router/segment-resolution/static-store.ts +81 -0
  280. package/src/router/segment-resolution/streamed-handler-telemetry.ts +52 -0
  281. package/src/router/segment-resolution/view-transition-default.ts +56 -0
  282. package/src/router/segment-resolution.ts +25 -1354
  283. package/src/router/segment-wrappers.ts +292 -0
  284. package/src/router/state-cookie-name.ts +33 -0
  285. package/src/router/substitute-pattern-params.ts +75 -0
  286. package/src/router/telemetry-otel.ts +261 -0
  287. package/src/router/telemetry.ts +377 -0
  288. package/src/router/timeout.ts +128 -0
  289. package/src/router/tracing.ts +206 -0
  290. package/src/router/trie-matching.ts +240 -61
  291. package/src/router/types.ts +23 -70
  292. package/src/router/url-params.ts +57 -0
  293. package/src/router.ts +781 -2378
  294. package/src/rsc/full-payload.ts +70 -0
  295. package/src/rsc/handler-context.ts +46 -0
  296. package/src/rsc/handler.ts +905 -1142
  297. package/src/rsc/helpers.ts +275 -19
  298. package/src/rsc/index.ts +2 -25
  299. package/src/rsc/json-route-result.ts +38 -0
  300. package/src/rsc/loader-fetch.ts +305 -0
  301. package/src/rsc/manifest-init.ts +77 -0
  302. package/src/rsc/nonce.ts +14 -0
  303. package/src/rsc/origin-guard.ts +155 -0
  304. package/src/rsc/progressive-enhancement.ts +502 -0
  305. package/src/rsc/redirect-guard.ts +99 -0
  306. package/src/rsc/response-cache-serve.ts +238 -0
  307. package/src/rsc/response-error.ts +104 -0
  308. package/src/rsc/response-route-handler.ts +257 -0
  309. package/src/rsc/rsc-rendering.ts +527 -0
  310. package/src/rsc/runtime-warnings.ts +55 -0
  311. package/src/rsc/server-action.ts +522 -0
  312. package/src/rsc/shell-capture.ts +897 -0
  313. package/src/rsc/shell-serve.ts +124 -0
  314. package/src/rsc/ssr-setup.ts +144 -0
  315. package/src/rsc/transition-gate.ts +89 -0
  316. package/src/rsc/types.ts +95 -12
  317. package/src/runtime-env.ts +18 -0
  318. package/src/search-params.ts +99 -82
  319. package/src/segment-content-promise.ts +67 -0
  320. package/src/segment-loader-promise.ts +149 -0
  321. package/src/segment-system.tsx +349 -134
  322. package/src/serialize.ts +243 -0
  323. package/src/server/context.ts +459 -85
  324. package/src/server/cookie-parse.ts +32 -0
  325. package/src/server/cookie-store.ts +310 -0
  326. package/src/server/fetchable-loader-store.ts +11 -6
  327. package/src/server/handle-store.ts +123 -42
  328. package/src/server/loader-registry.ts +51 -100
  329. package/src/server/request-context.ts +848 -157
  330. package/src/server.ts +15 -8
  331. package/src/ssr/index.tsx +443 -135
  332. package/src/ssr/ssr-root.tsx +228 -0
  333. package/src/static-handler.ts +45 -18
  334. package/src/testing/cache-status.ts +162 -0
  335. package/src/testing/collect-handle.ts +46 -0
  336. package/src/testing/dispatch.ts +701 -0
  337. package/src/testing/dom.entry.ts +22 -0
  338. package/src/testing/e2e/fixture.ts +188 -0
  339. package/src/testing/e2e/index.ts +128 -0
  340. package/src/testing/e2e/matchers.ts +35 -0
  341. package/src/testing/e2e/page-helpers.ts +272 -0
  342. package/src/testing/e2e/parity.ts +387 -0
  343. package/src/testing/e2e/server.ts +195 -0
  344. package/src/testing/flight-matchers.ts +97 -0
  345. package/src/testing/flight-normalize.ts +11 -0
  346. package/src/testing/flight-runtime.d.ts +57 -0
  347. package/src/testing/flight-tree.ts +682 -0
  348. package/src/testing/flight.entry.ts +52 -0
  349. package/src/testing/flight.ts +257 -0
  350. package/src/testing/generated-routes.ts +199 -0
  351. package/src/testing/index.ts +105 -0
  352. package/src/testing/internal/context.ts +371 -0
  353. package/src/testing/internal/flight-client-globals.ts +30 -0
  354. package/src/testing/internal/seed-vars.ts +54 -0
  355. package/src/testing/render-handler.ts +357 -0
  356. package/src/testing/render-route.tsx +584 -0
  357. package/src/testing/run-loader.ts +385 -0
  358. package/src/testing/run-middleware.ts +205 -0
  359. package/src/testing/run-transition-when.ts +164 -0
  360. package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
  361. package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
  362. package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
  363. package/src/testing/vitest-stubs/version.ts +5 -0
  364. package/src/testing/vitest.ts +305 -0
  365. package/src/theme/ThemeProvider.tsx +76 -98
  366. package/src/theme/ThemeScript.tsx +12 -14
  367. package/src/theme/constants.ts +57 -15
  368. package/src/theme/index.ts +3 -20
  369. package/src/theme/theme-context.ts +5 -35
  370. package/src/theme/theme-script.ts +43 -39
  371. package/src/theme/use-theme.ts +0 -3
  372. package/src/types/boundaries.ts +123 -0
  373. package/src/types/cache-types.ts +207 -0
  374. package/src/types/error-types.ts +132 -0
  375. package/src/types/global-namespace.ts +113 -0
  376. package/src/types/handler-context.ts +839 -0
  377. package/src/types/index.ts +81 -0
  378. package/src/types/loader-types.ts +212 -0
  379. package/src/types/request-scope.ts +112 -0
  380. package/src/types/route-config.ts +138 -0
  381. package/src/types/route-entry.ts +114 -0
  382. package/src/types/segments.ts +271 -0
  383. package/src/types.ts +1 -1795
  384. package/src/urls/include-helper.ts +162 -0
  385. package/src/urls/include-provider.ts +71 -0
  386. package/src/urls/index.ts +44 -0
  387. package/src/urls/path-helper-types.ts +413 -0
  388. package/src/urls/path-helper.ts +280 -0
  389. package/src/urls/pattern-types.ts +160 -0
  390. package/src/urls/response-types.ts +109 -0
  391. package/src/urls/type-extraction.ts +316 -0
  392. package/src/urls/urls-function.ts +80 -0
  393. package/src/urls.ts +1 -1341
  394. package/src/use-loader.tsx +406 -141
  395. package/src/vercel/index.ts +11 -0
  396. package/src/vercel/tracing.ts +88 -0
  397. package/src/vite/debug.ts +185 -0
  398. package/src/vite/discovery/bundle-postprocess.ts +182 -0
  399. package/src/vite/discovery/discover-routers.ts +389 -0
  400. package/src/vite/discovery/discovery-errors.ts +255 -0
  401. package/src/vite/discovery/gate-state.ts +171 -0
  402. package/src/vite/discovery/prerender-collection.ts +467 -0
  403. package/src/vite/discovery/route-types-writer.ts +214 -0
  404. package/src/vite/discovery/self-gen-tracking.ts +73 -0
  405. package/src/vite/discovery/state.ts +161 -0
  406. package/src/vite/discovery/virtual-module-codegen.ts +183 -0
  407. package/src/vite/index.ts +23 -2255
  408. package/src/vite/inject-client-debug.ts +36 -0
  409. package/src/vite/plugin-types.ts +303 -0
  410. package/src/vite/plugins/cjs-to-esm.ts +90 -0
  411. package/src/vite/plugins/client-ref-dedup.ts +120 -0
  412. package/src/vite/plugins/client-ref-hashing.ts +118 -0
  413. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  414. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  415. package/src/vite/plugins/cloudflare-protocol-stub.ts +194 -0
  416. package/src/vite/{expose-action-id.ts → plugins/expose-action-id.ts} +88 -110
  417. package/src/vite/{expose-id-utils.ts → plugins/expose-id-utils.ts} +89 -79
  418. package/src/vite/plugins/expose-ids/export-analysis.ts +363 -0
  419. package/src/vite/plugins/expose-ids/handler-transform.ts +130 -0
  420. package/src/vite/plugins/expose-ids/loader-transform.ts +64 -0
  421. package/src/vite/plugins/expose-ids/router-transform.ts +199 -0
  422. package/src/vite/plugins/expose-ids/types.ts +45 -0
  423. package/src/vite/plugins/expose-internal-ids.ts +805 -0
  424. package/src/vite/plugins/performance-tracks.ts +89 -0
  425. package/src/vite/plugins/refresh-cmd.ts +127 -0
  426. package/src/vite/plugins/use-cache-transform.ts +313 -0
  427. package/src/vite/plugins/vercel-output.ts +384 -0
  428. package/src/vite/plugins/version-injector.ts +94 -0
  429. package/src/vite/plugins/version-plugin.ts +263 -0
  430. package/src/vite/plugins/virtual-entries.ts +234 -0
  431. package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
  432. package/src/vite/rango.ts +560 -0
  433. package/src/vite/router-discovery.ts +1638 -0
  434. package/src/vite/{ast-handler-extract.ts → utils/ast-handler-extract.ts} +200 -37
  435. package/src/vite/utils/banner.ts +36 -0
  436. package/src/vite/utils/bundle-analysis.ts +132 -0
  437. package/src/vite/utils/client-chunks.ts +184 -0
  438. package/src/vite/utils/directive-prologue.ts +40 -0
  439. package/src/vite/utils/forward-user-plugins.ts +171 -0
  440. package/src/vite/utils/manifest-utils.ts +15 -0
  441. package/src/vite/utils/package-resolution.ts +89 -0
  442. package/src/vite/utils/prerender-utils.ts +249 -0
  443. package/src/vite/utils/shared-utils.ts +269 -0
  444. package/CLAUDE.md +0 -43
  445. package/dist/vite/index.named-routes.gen.ts +0 -103
  446. package/src/browser/lru-cache.ts +0 -69
  447. package/src/browser/react/use-client-cache.ts +0 -56
  448. package/src/browser/request-controller.ts +0 -164
  449. package/src/browser/shallow.ts +0 -35
  450. package/src/cache/memory-store.ts +0 -253
  451. package/src/handles/index.ts +0 -6
  452. package/src/href-context.ts +0 -33
  453. package/src/network-error-thrower.tsx +0 -21
  454. package/src/router.gen.ts +0 -6
  455. package/src/static-handler.gen.ts +0 -5
  456. package/src/urls.gen.ts +0 -8
  457. package/src/vite/expose-internal-ids.ts +0 -1167
  458. package/src/vite/package-resolution.ts +0 -125
  459. package/src/vite/virtual-entries.ts +0 -114
  460. /package/src/vite/{version.d.ts → plugins/version.d.ts} +0 -0
@@ -1,4 +1,4 @@
1
- /// <reference path="../../vite/version.d.ts" />
1
+ /// <reference path="../../vite/plugins/version.d.ts" />
2
2
 
3
3
  // Extend CacheStorage with Cloudflare's default cache property
4
4
  declare global {
@@ -10,14 +10,21 @@ declare global {
10
10
  /**
11
11
  * Cloudflare Edge Cache Store
12
12
  *
13
- * Production cache store using Cloudflare's Cache API.
14
- * Handles SWR atomically - get() checks staleness and marks REVALIDATING in one operation.
13
+ * Production cache store using Cloudflare's Cache API (L1) with optional
14
+ * KV persistence (L2).
15
+ *
16
+ * L1 (Cache API): Per-colo, fast, ephemeral. Handles SWR atomically.
17
+ * L2 (KV): Global, persistent, ~50ms reads. Auto-warms cold colos.
18
+ *
19
+ * Read flow: L1 hit → serve | L1 miss → L2 hit → serve + promote to L1 | both miss → render
20
+ * Write flow: L1 write + L2 write (both via waitUntil)
15
21
  *
16
22
  * Features:
17
23
  * - Extended TTL for SWR window (max-age = ttl + swr)
18
24
  * - Staleness via x-edge-cache-stale-at header
19
- * - Atomic REVALIDATING status for thundering herd prevention
25
+ * - Atomic REVALIDATING status for thundering herd prevention (L1 only)
20
26
  * - Non-blocking writes via waitUntil
27
+ * - KV L2 for cross-colo cache persistence
21
28
  */
22
29
 
23
30
  import type {
@@ -25,121 +32,213 @@ import type {
25
32
  CachedEntryData,
26
33
  CacheDefaults,
27
34
  CacheGetResult,
35
+ CacheItemResult,
36
+ CacheItemOptions,
37
+ ShellCacheEntry,
28
38
  } from "../types.js";
29
39
  import {
30
- getRequestContext,
40
+ _getRequestContext,
31
41
  type RequestContext,
32
42
  } from "../../server/request-context.js";
33
43
  import { VERSION } from "@rangojs/router:version";
44
+ import {
45
+ isPerClientSignalHeader,
46
+ stripPerClientSignals,
47
+ } from "../../browser/cookie-name.js";
48
+ import {
49
+ resolveTtl,
50
+ resolveSwrWindow,
51
+ DEFAULT_FUNCTION_TTL,
52
+ } from "../cache-policy.js";
53
+ import { reportCacheError, reportingAsync } from "../cache-error.js";
54
+ import type { CacheErrorCategory } from "../cache-error.js";
55
+ import { bufferToBase64, base64ToBuffer } from "./cf-base64.js";
56
+ import {
57
+ KV_MAX_KEY_BYTES,
58
+ KV_MIN_EXPIRATION_TTL,
59
+ kvKeyByteLength,
60
+ remainingCacheControl,
61
+ } from "./cf-kv-utils.js";
62
+ import {
63
+ TAG_MARKER_CACHE_PREFIX,
64
+ TAG_MARKER_ABSENT,
65
+ getTagMarkerMemo,
66
+ getTagMarkerInflight,
67
+ } from "./cf-tag-marker-memo.js";
34
68
 
35
69
  // ============================================================================
36
70
  // Constants
37
71
  // ============================================================================
72
+ //
73
+ // Header names, KV prefixes, and timeout/interval defaults live in
74
+ // cf-cache-constants.ts so collaborator modules can share them without a
75
+ // circular import back to this class. They are re-exported below so existing
76
+ // import paths (`../cf-cache-store`, `./cf-cache-store.js`) still resolve.
77
+ import {
78
+ CACHE_STALE_AT_HEADER,
79
+ CACHE_STATUS_HEADER,
80
+ CACHE_TAGS_HEADER,
81
+ CACHE_TAGGED_AT_HEADER,
82
+ TAG_MARKER_PREFIX,
83
+ CACHE_REVALIDATING_AT_HEADER,
84
+ CACHE_EXPIRES_AT_HEADER,
85
+ CACHE_ORIG_CC_HEADER,
86
+ MAX_REVALIDATION_INTERVAL,
87
+ EDGE_LOOKUP_TIMEOUT_MS,
88
+ EDGE_READ_TIMEOUT_MS,
89
+ KV_READ_TIMEOUT_MS,
90
+ } from "./cf-cache-constants.js";
38
91
 
39
- /** Header storing timestamp when entry becomes stale */
40
- export const CACHE_STALE_AT_HEADER = "x-edge-cache-stale-at";
92
+ // Re-export the public constants so consumers/tests importing them from
93
+ // cf-cache-store keep working after the move.
94
+ export {
95
+ CACHE_STALE_AT_HEADER,
96
+ CACHE_STATUS_HEADER,
97
+ CACHE_TAGS_HEADER,
98
+ CACHE_TAGGED_AT_HEADER,
99
+ TAG_MARKER_PREFIX,
100
+ CACHE_REVALIDATING_AT_HEADER,
101
+ MAX_REVALIDATION_INTERVAL,
102
+ EDGE_LOOKUP_TIMEOUT_MS,
103
+ EDGE_READ_TIMEOUT_MS,
104
+ KV_READ_TIMEOUT_MS,
105
+ };
41
106
 
42
- /** Header storing cache status: HIT | REVALIDATING */
43
- export const CACHE_STATUS_HEADER = "x-edge-cache-status";
107
+ // The tag-marker prefix/sentinel and per-request memo helpers (with their
108
+ // module-singleton WeakMaps) live in cf-tag-marker-memo.ts; imported above.
44
109
 
45
110
  /**
46
- * Maximum age in seconds for REVALIDATING status before allowing new revalidation.
47
- * After this period, a stale entry in REVALIDATING status will trigger revalidation again.
48
- * @internal
111
+ * Per-request memo of the derived cache-key base URL.
112
+ *
113
+ * deriveBaseUrl() is a pure function of the live request URL, but keyToRequest
114
+ * calls it on EVERY cache operation (each segment/item get/set/delete, each
115
+ * KV->L1 promote, each tag-marker read), so a page composed of many cached
116
+ * entries re-parses the same request.url and re-runs the host validation tens
117
+ * of times. Keying by the request-context object collapses that to one derive
118
+ * per request. Keyed by ctx alone (not by store) because the derived value
119
+ * depends only on the request URL, not on which store asked.
120
+ */
121
+ const derivedBaseUrlMemo = new WeakMap<object, string>();
122
+
123
+ // Pure KV helpers (key byte-length limits, expirationTtl floor, stale-path
124
+ // Cache-Control recompute) live in cf-kv-utils.ts; imported above.
125
+
126
+ /**
127
+ * Stores (by namespace) already warned about tag machinery configured without a
128
+ * KV namespace, so the warning fires once per process rather than per request
129
+ * (CFCacheStore is constructed per request).
130
+ */
131
+ const warnedNoKvReadInvalidation = new Set<string>();
132
+
133
+ /**
134
+ * Stores (by namespace) already warned about a tagInvalidationTtl below KV's
135
+ * expirationTtl floor, so the floor warning fires once per process rather than
136
+ * once per request (CFCacheStore is constructed per request).
49
137
  */
50
- export const MAX_REVALIDATION_INTERVAL = 30;
138
+ const warnedTagInvalidationTtlFloor = new Set<string>();
51
139
 
52
140
  // ============================================================================
53
141
  // Types
54
142
  // ============================================================================
143
+ //
144
+ // The shared public types (KVNamespace, CFCacheReadDebugEvent, CFCacheDebug,
145
+ // CFCacheStoreOptions) live in cf-cache-types.ts; imported and re-exported below
146
+ // so existing import paths still resolve. The private KV envelope interfaces
147
+ // stay here with the methods that read/write them.
148
+ import type {
149
+ KVNamespace,
150
+ CFCacheReadDebugEvent,
151
+ CFCacheDebug,
152
+ CFCacheStoreOptions,
153
+ } from "./cf-cache-types.js";
154
+ export type {
155
+ KVNamespace,
156
+ CFCacheReadDebugEvent,
157
+ CFCacheDebug,
158
+ CFCacheStoreOptions,
159
+ };
55
160
 
56
161
  /**
57
- * Cloudflare Workers ExecutionContext (subset we need)
162
+ * KV envelope for segment cache entries.
163
+ * @internal
58
164
  */
59
- export interface ExecutionContext {
60
- waitUntil(promise: Promise<any>): void;
61
- passThroughOnException(): void;
165
+ interface KVSegmentEnvelope {
166
+ /** Cached segment data */
167
+ d: CachedEntryData;
168
+ /** When entry becomes stale (ms epoch) */
169
+ s: number;
170
+ /** When entry hard-expires (ms epoch) */
171
+ e: number;
62
172
  }
63
173
 
64
- export interface CFCacheStoreOptions<TEnv = unknown> {
65
- /**
66
- * Cache namespace. If not provided, uses caches.default (recommended).
67
- * Only set this if you need isolated cache storage.
68
- */
69
- namespace?: string;
70
-
71
- /**
72
- * Base URL for cache keys.
73
- *
74
- * If not provided, derives from request hostname via requestContext:
75
- * - Production domains → uses `https://{hostname}/`
76
- * - Dev/preview (localhost, workers.dev, pages.dev) → uses internal fallback URL
77
- */
78
- baseUrl?: string;
79
-
80
- /** Default cache options */
81
- defaults?: CacheDefaults;
82
-
83
- /**
84
- * Cloudflare ExecutionContext for non-blocking cache writes.
85
- * Pass the `ctx` from your worker's fetch handler.
86
- *
87
- * @example
88
- * ```typescript
89
- * new CFCacheStore({ ctx: env.ctx })
90
- * ```
91
- */
92
- ctx: ExecutionContext;
93
-
94
- /**
95
- * Cache version string override. When this changes, all cached entries are
96
- * effectively invalidated (new keys won't match old entries).
97
- *
98
- * Defaults to the auto-generated VERSION from `rsc-router:version` virtual module.
99
- * Only set this if you need a custom versioning strategy.
100
- */
101
- version?: string;
174
+ /**
175
+ * KV envelope for function cache entries ("use cache").
176
+ * @internal
177
+ */
178
+ interface KVItemEnvelope {
179
+ /** RSC-serialized return value */
180
+ v: string;
181
+ /** RSC-encoded handle data (see handle-snapshot.ts encodeHandles) */
182
+ h?: string;
183
+ /** When entry becomes stale (ms epoch) */
184
+ s: number;
185
+ /** When entry hard-expires (ms epoch) */
186
+ e: number;
187
+ /** Cache tags (for distributed tag invalidation) */
188
+ t?: string[];
189
+ /** Timestamp when tags were attached (ms epoch) */
190
+ ta?: number;
191
+ }
102
192
 
103
- /**
104
- * Custom key generator applied to all cache operations.
105
- * Receives the full RequestContext (including env) and the default-generated key.
106
- * Return value becomes the final cache key (unless route overrides with `key` option).
107
- *
108
- * @example Using headers for user segmentation
109
- * ```typescript
110
- * keyGenerator: (ctx, defaultKey) => {
111
- * const segment = ctx.request.headers.get('x-user-segment') || 'default';
112
- * return `${segment}:${defaultKey}`;
113
- * }
114
- * ```
115
- *
116
- * @example Using env bindings for multi-region
117
- * ```typescript
118
- * keyGenerator: (ctx, defaultKey) => {
119
- * const region = ctx.env.REGION || 'us';
120
- * return `${region}:${defaultKey}`;
121
- * }
122
- * ```
123
- *
124
- * @example Using cookies for locale-aware caching
125
- * ```typescript
126
- * keyGenerator: (ctx, defaultKey) => {
127
- * const locale = ctx.cookie('locale') || 'en';
128
- * return `${locale}:${defaultKey}`;
129
- * }
130
- * ```
131
- */
132
- keyGenerator?: (
133
- ctx: RequestContext<TEnv>,
134
- defaultKey: string,
135
- ) => string | Promise<string>;
193
+ /**
194
+ * KV envelope for PPR shell cache entries.
195
+ * @internal
196
+ */
197
+ interface KVShellEnvelope {
198
+ /** base64-encoded prelude bytes */
199
+ p: string;
200
+ /** postponed state JSON, or null (DATA variant — no holes) */
201
+ po: string | null;
202
+ /** React.version captured at prerender time */
203
+ rv: string;
204
+ /** createdAt (ms epoch) */
205
+ c: number;
206
+ /** When entry becomes stale (ms epoch) */
207
+ s: number;
208
+ /** When entry hard-expires (ms epoch) */
209
+ e: number;
210
+ /** Cache tags (for distributed tag invalidation) */
211
+ t?: string[];
212
+ /** Timestamp when tags were attached (ms epoch) */
213
+ ta?: number;
214
+ /** initialTheme the capture render was built with (resume theme fidelity) */
215
+ i?: string;
216
+ /** Capture data snapshot: recorded cache-store hits/writes for HIT parity */
217
+ sn?: import("../types.js").ShellSnapshotRecord[];
136
218
  }
137
219
 
138
220
  /**
139
- * Cache status values for the x-edge-cache-status header.
221
+ * KV envelope for document cache entries.
140
222
  * @internal
141
223
  */
142
- export type CacheStatus = "HIT" | "REVALIDATING";
224
+ interface KVResponseEnvelope {
225
+ /** Response body as base64-encoded string (safe for binary payloads) */
226
+ b: string;
227
+ /** HTTP status code */
228
+ st: number;
229
+ /** HTTP status text */
230
+ stx: string;
231
+ /** Serialized headers as key-value pairs (client-facing; no internal headers) */
232
+ hd: [string, string][];
233
+ /** When entry becomes stale (ms epoch) */
234
+ s: number;
235
+ /** When entry hard-expires (ms epoch) */
236
+ e: number;
237
+ /** Cache tags (for distributed tag invalidation) */
238
+ t?: string[];
239
+ /** Timestamp when tags were attached (ms epoch) */
240
+ ta?: number;
241
+ }
143
242
 
144
243
  // ============================================================================
145
244
  // CFCacheStore Implementation
@@ -153,9 +252,17 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
153
252
  ) => string | Promise<string>;
154
253
 
155
254
  private readonly namespace?: string;
156
- private readonly baseUrl: string;
255
+ private readonly explicitBaseUrl?: string;
157
256
  private readonly waitUntil?: (fn: () => Promise<void>) => void;
158
257
  private readonly version?: string;
258
+ private readonly edgeLookupTimeoutMs: number;
259
+ private readonly edgeReadTimeoutMs: number;
260
+ private readonly kvReadTimeoutMs: number;
261
+ private readonly debug?: (event: CFCacheReadDebugEvent) => void;
262
+ private readonly kv?: KVNamespace;
263
+ private readonly onRevalidateTag?: (tags: string[]) => Promise<void>;
264
+ private readonly tagInvalidationTtl?: number;
265
+ private readonly tagCacheTtl: number;
159
266
 
160
267
  constructor(options: CFCacheStoreOptions<TEnv>) {
161
268
  if (!options.ctx) {
@@ -167,45 +274,192 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
167
274
  }
168
275
 
169
276
  this.namespace = options.namespace;
170
- this.baseUrl = options.baseUrl ?? this.deriveBaseUrl();
277
+ // Base URL is resolved lazily per cache operation (see resolveBaseUrl).
278
+ // The store is constructed before the per-request context ALS is entered
279
+ // (the cache factory runs ahead of runWithRequestContext in the handler),
280
+ // so deriving the host here would always miss the request and fall back to
281
+ // the internal host. Only the explicit override can be captured eagerly.
282
+ this.explicitBaseUrl = options.baseUrl;
171
283
  this.defaults = options.defaults;
172
284
  this.version = options.version ?? VERSION;
285
+ // Coalesce only finite numbers to the override; a non-finite value (NaN from
286
+ // `Number(env.UNSET)`, or Infinity) would otherwise sail past `?? DEFAULT`
287
+ // (which only replaces null/undefined) into setTimeout, where NaN/Infinity
288
+ // are spec-coerced to ~1ms and silently turn the budget into a near-100%
289
+ // false-miss on that tier. A genuine finite 0 or negative still passes
290
+ // through and disables the budget per the documented `<= 0` contract.
291
+ const finiteBudget = (
292
+ value: number | undefined,
293
+ fallback: number,
294
+ ): number =>
295
+ typeof value === "number" && Number.isFinite(value) ? value : fallback;
296
+ this.edgeLookupTimeoutMs = finiteBudget(
297
+ options.edgeLookupTimeoutMs,
298
+ EDGE_LOOKUP_TIMEOUT_MS,
299
+ );
300
+ this.edgeReadTimeoutMs = finiteBudget(
301
+ options.edgeReadTimeoutMs,
302
+ EDGE_READ_TIMEOUT_MS,
303
+ );
304
+ this.kvReadTimeoutMs = finiteBudget(
305
+ options.kvReadTimeoutMs,
306
+ KV_READ_TIMEOUT_MS,
307
+ );
308
+ this.debug =
309
+ options.debug === true
310
+ ? (event) =>
311
+ console.log(`[CFCacheStore:debug] ${JSON.stringify(event)}`)
312
+ : typeof options.debug === "function"
313
+ ? options.debug
314
+ : undefined;
173
315
  this.keyGenerator = options.keyGenerator;
174
316
  this.waitUntil = (fn) => options.ctx.waitUntil(fn());
317
+ this.kv = options.kv;
318
+ this.onRevalidateTag = options.onRevalidateTag;
319
+ // tagInvalidationTtl feeds KV's expirationTtl, which CF rejects below
320
+ // KV_MIN_EXPIRATION_TTL (60s) -- a too-small finite value would make EVERY
321
+ // marker write throw and break ALL invalidation. Floor it (and warn once);
322
+ // a non-finite/non-positive value falls back to the no-expiry default
323
+ // (markers persist) rather than silently sailing a NaN into expirationTtl.
324
+ this.tagInvalidationTtl = this.sanitizeTagInvalidationTtl(
325
+ options.tagInvalidationTtl,
326
+ );
327
+ // tagCacheTtl gates the L1 marker cache via `> 0`. A non-finite value (NaN
328
+ // from `Number(env.UNSET)`) is not null/undefined, so `?? 0` would let it
329
+ // through and silently disable the cache while reading as "configured".
330
+ // finiteBudget coerces non-finite/null/undefined to 0; the `> 0` guard then
331
+ // collapses a finite non-positive value to the documented 0 = disabled.
332
+ const tagCacheTtl = finiteBudget(options.tagCacheTtl, 0);
333
+ this.tagCacheTtl = tagCacheTtl > 0 ? tagCacheTtl : 0;
334
+
335
+ // Read-side tag invalidation requires KV: isGloballyInvalidated() compares an
336
+ // entry's taggedAt against the per-tag KV marker and short-circuits to "not
337
+ // invalidated" when no KV namespace is configured. A consumer who wires the
338
+ // tag machinery (tagCacheTtl for L1 markers, or onRevalidateTag for CDN purge)
339
+ // but omits kv gets only the purge fired - marker writes are skipped without
340
+ // kv - yet every tagged read still serves stale data with no other signal.
341
+ // Surface that misconfiguration.
342
+ if (!this.kv && (this.tagCacheTtl > 0 || this.onRevalidateTag)) {
343
+ const id = this.namespace ?? "default";
344
+ if (!warnedNoKvReadInvalidation.has(id)) {
345
+ warnedNoKvReadInvalidation.add(id);
346
+ console.warn(
347
+ `[CFCacheStore] tagCacheTtl/onRevalidateTag is configured without a KV ` +
348
+ `namespace, so tag invalidation has NO read-side effect: tagged reads ` +
349
+ `are never treated as invalidated and serve stale data. Configure ` +
350
+ `{ kv } for distributed tag invalidation.`,
351
+ );
352
+ }
353
+ }
354
+ }
355
+
356
+ /**
357
+ * Validate a consumer-supplied tagInvalidationTtl against CF KV's expirationTtl
358
+ * floor. A finite value below KV_MIN_EXPIRATION_TTL is raised to it (with a
359
+ * one-time warning) so invalidation keeps working instead of every marker
360
+ * write throwing; a non-finite or non-positive value returns undefined (the
361
+ * no-expiry default). The warning still notes the sizing rule: the TTL must
362
+ * exceed the largest entry TTL+SWR or invalidated entries can resurrect.
363
+ * @internal
364
+ */
365
+ private sanitizeTagInvalidationTtl(
366
+ value: number | undefined,
367
+ ): number | undefined {
368
+ if (value == null) return undefined;
369
+ if (!Number.isFinite(value) || value <= 0) return undefined;
370
+ if (value < KV_MIN_EXPIRATION_TTL) {
371
+ const id = this.namespace ?? "default";
372
+ if (!warnedTagInvalidationTtlFloor.has(id)) {
373
+ warnedTagInvalidationTtlFloor.add(id);
374
+ console.warn(
375
+ `[CFCacheStore] tagInvalidationTtl ${value} is below Cloudflare KV's ` +
376
+ `${KV_MIN_EXPIRATION_TTL}s expirationTtl floor; raising to ` +
377
+ `${KV_MIN_EXPIRATION_TTL}. It must still exceed your largest entry ` +
378
+ `TTL+SWR or invalidated entries can resurrect when the marker expires.`,
379
+ );
380
+ }
381
+ return KV_MIN_EXPIRATION_TTL;
382
+ }
383
+ return value;
384
+ }
385
+
386
+ /**
387
+ * Emit a debug event if `debug` is enabled. Swallows sink errors so a faulty
388
+ * debug callback can never break a cache read.
389
+ * @internal
390
+ */
391
+ private emitDebug(event: CFCacheReadDebugEvent): void {
392
+ if (!this.debug) return;
393
+ try {
394
+ this.debug(event);
395
+ } catch {
396
+ // A broken debug sink must not affect the request.
397
+ }
398
+ }
399
+
400
+ /**
401
+ * Resolve the cache-key base URL for the current cache operation.
402
+ * Prefers an explicit `baseUrl` option; otherwise derives it from the live
403
+ * request. Called per operation (from keyToRequest), which runs inside the
404
+ * request-context ALS, so deriveBaseUrl sees the request and can use the
405
+ * production host instead of the internal fallback.
406
+ * @internal
407
+ */
408
+ private resolveBaseUrl(): string {
409
+ return this.explicitBaseUrl ?? this.deriveBaseUrl();
175
410
  }
176
411
 
177
412
  /**
178
413
  * Derive base URL from request hostname via requestContext.
179
- * Uses internal fallback for dev/preview environments.
414
+ * Uses internal fallback for dev/preview environments and untrusted hostnames.
415
+ * Must run inside the request context (invoked lazily via resolveBaseUrl).
180
416
  * @internal
181
417
  */
182
418
  private deriveBaseUrl(): string {
183
- const fallback = "https://rsc-cache.internal.com/";
419
+ const fallback = "https://rsc-dummy-host-1.com/";
184
420
 
185
- const ctx = getRequestContext();
421
+ const ctx = _getRequestContext();
186
422
  if (!ctx?.request) {
187
423
  return fallback;
188
424
  }
189
425
 
190
- try {
191
- const url = new URL(ctx.request.url);
192
- const hostname = url.hostname;
426
+ // The result is deterministic per request, but keyToRequest calls this on
427
+ // every cache operation; memoize per request context (see derivedBaseUrlMemo).
428
+ const memoized = derivedBaseUrlMemo.get(ctx);
429
+ if (memoized !== undefined) {
430
+ return memoized;
431
+ }
193
432
 
194
- // Use fallback for dev/preview environments
195
- if (
196
- hostname === "localhost" ||
197
- hostname === "127.0.0.1" ||
198
- hostname.endsWith(".workers.dev") ||
199
- hostname.endsWith(".pages.dev")
200
- ) {
433
+ const derived = ((): string => {
434
+ try {
435
+ const url = new URL(ctx.request.url);
436
+ const hostname = url.hostname;
437
+
438
+ // Use fallback for dev/preview environments
439
+ if (
440
+ hostname === "localhost" ||
441
+ hostname === "127.0.0.1" ||
442
+ hostname.endsWith(".workers.dev") ||
443
+ hostname.endsWith(".pages.dev")
444
+ ) {
445
+ return fallback;
446
+ }
447
+
448
+ // Validate hostname: must be a valid domain (alphanumeric, hyphens, dots)
449
+ // to prevent host header injection into cache keys
450
+ if (!/^[a-zA-Z0-9.-]+$/.test(hostname) || hostname.length > 253) {
451
+ return fallback;
452
+ }
453
+
454
+ // Use actual hostname for production
455
+ return `https://${hostname}/`;
456
+ } catch {
201
457
  return fallback;
202
458
  }
459
+ })();
203
460
 
204
- // Use actual hostname for production
205
- return `https://${hostname}/`;
206
- } catch {
207
- return fallback;
208
- }
461
+ derivedBaseUrlMemo.set(ctx, derived);
462
+ return derived;
209
463
  }
210
464
 
211
465
  /**
@@ -219,6 +473,297 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
219
473
  return caches.default;
220
474
  }
221
475
 
476
+ /**
477
+ * Race an async cache read against a latency budget. Shared by all three read
478
+ * tiers (L1 match, L1 body, L2/KV) so the timeout policy lives in one place:
479
+ * on timeout it returns `{ value: undefined, timedOut: true }` and logs
480
+ * `${label} exceeded ${budgetMs}ms; treating as miss`; the abandoned read is
481
+ * left to settle in the background (late rejection swallowed) rather than
482
+ * aborted, since the underlying CF primitives expose no cancellation. A budget
483
+ * <= 0 disables the bound and awaits the read directly. `read` is a thunk so
484
+ * the disabled path and the raced path start the read identically.
485
+ * @internal
486
+ */
487
+ private async readWithTimeout<T>(
488
+ read: () => Promise<T>,
489
+ budgetMs: number,
490
+ label: string,
491
+ ): Promise<{ value: T | undefined; timedOut: boolean }> {
492
+ if (budgetMs <= 0) return { value: await read(), timedOut: false };
493
+
494
+ let timer: ReturnType<typeof setTimeout> | undefined;
495
+ const timeout = new Promise<{ timedOut: true }>((resolve) => {
496
+ timer = setTimeout(() => resolve({ timedOut: true }), budgetMs);
497
+ });
498
+ try {
499
+ const readPromise = read();
500
+ // The losing branch keeps running; ensure a late rejection can't surface
501
+ // as an unhandled rejection once we've stopped awaiting it.
502
+ readPromise.catch(() => {});
503
+ const result = await Promise.race([
504
+ readPromise.then((value) => ({ timedOut: false as const, value })),
505
+ timeout,
506
+ ]);
507
+ if (result.timedOut) {
508
+ console.warn(
509
+ `[CFCacheStore] ${label} exceeded ${budgetMs}ms; treating as miss`,
510
+ );
511
+ return { value: undefined, timedOut: true };
512
+ }
513
+ return { value: result.value, timedOut: false };
514
+ } finally {
515
+ if (timer) clearTimeout(timer);
516
+ }
517
+ }
518
+
519
+ /**
520
+ * Read from the L1 edge cache under the edgeLookupTimeoutMs budget. A `match`
521
+ * slower than the budget is abandoned and reported as a miss
522
+ * (`{ response: undefined, timedOut: true }`) so a degraded colo cannot stall
523
+ * the request; callers fall through to their normal miss path (L2/KV or
524
+ * render). The `timedOut` flag lets callers distinguish an abandoned slow
525
+ * match from a genuine miss for debug reporting; `error` is set when the
526
+ * `match` itself rejected (a transient L1 infra error) so the caller can
527
+ * report it as cache-read while still degrading to L2/KV -- distinct from a
528
+ * genuine miss (no entry), which sets neither flag.
529
+ * @internal
530
+ */
531
+ private async matchWithTimeout(
532
+ cache: Cache,
533
+ request: Request,
534
+ ): Promise<{
535
+ response: Response | undefined;
536
+ timedOut: boolean;
537
+ error?: unknown;
538
+ }> {
539
+ let matchError: unknown;
540
+ const { value, timedOut } = await this.readWithTimeout(
541
+ // A fast match rejection is caught at the thunk and reported as a miss
542
+ // (response undefined), so the caller falls through to L2/KV rather than
543
+ // escaping to the outer catch -- symmetric with the body-read thunk. The
544
+ // error is captured (not swallowed) so the caller can surface it via
545
+ // onError as a cache-read degradation.
546
+ () =>
547
+ cache.match(request).catch((e) => {
548
+ matchError = e;
549
+ return undefined;
550
+ }),
551
+ this.edgeLookupTimeoutMs,
552
+ "edge cache lookup",
553
+ );
554
+ return { response: value, timedOut, error: matchError };
555
+ }
556
+
557
+ /**
558
+ * Read and JSON-parse a matched L1 Response's body under the edgeReadTimeoutMs
559
+ * budget. CF resolves `match()` with a lazily-streamed body, so the latency
560
+ * tail surfaces here -- after matchWithTimeout has already passed -- not in the
561
+ * match itself. On timeout `undefined` is returned so the caller falls through
562
+ * to L2/KV or render.
563
+ * @internal
564
+ */
565
+ private async readJsonWithTimeout<T>(
566
+ response: Response,
567
+ ): Promise<{ value: T | undefined; errored: boolean; error?: unknown }> {
568
+ // A FAST json() rejection (a corrupt body, or a foreign 200 non-JSON
569
+ // response that collided on this key) is caught at the thunk and turned into
570
+ // a miss, so the caller falls through to L2/KV exactly like a body-timeout
571
+ // -- instead of escaping to get()/getItem()'s outer catch, which returns
572
+ // null WITHOUT ever consulting KV. The catch lives here, not in
573
+ // readWithTimeout, so the L2/KV tier keeps propagating a genuine kv.get
574
+ // rejection to its own error sink. The `errored` flag lets the caller emit a
575
+ // distinct "body-error" debug outcome rather than masquerading as a timeout.
576
+ // On a TIMEOUT the json() promise is still pending, so the catch has not
577
+ // fired: errored stays false and the outcome is correctly a body-timeout. A
578
+ // late rejection after the timeout only mutates the closure flag, which the
579
+ // already-returned object no longer reads.
580
+ let errored = false;
581
+ let error: unknown;
582
+ const { value } = await this.readWithTimeout<T | undefined>(
583
+ () =>
584
+ (response.json() as Promise<T>).catch((e) => {
585
+ errored = true;
586
+ error = e;
587
+ return undefined;
588
+ }),
589
+ this.edgeReadTimeoutMs,
590
+ "edge cache body read",
591
+ );
592
+ return { value, errored, error };
593
+ }
594
+
595
+ /**
596
+ * Self-heal a corrupt L1 entry, then return the fall-through result. Reports
597
+ * the corruption as cache-corrupt (so an onError consumer sees it distinctly
598
+ * from a transient outage), runs the caller's L2/KV fall-through, and evicts
599
+ * the faulty per-colo entry ONLY when that fall-through found no good copy.
600
+ *
601
+ * The conditional evict is the load-bearing detail: when KV DOES serve a copy,
602
+ * kvGet* has already scheduled a same-key promote (`cache.put`); an eager
603
+ * `cache.delete` here would race that put with no CF Cache API ordering
604
+ * guarantee and could clobber the freshly-restored entry. So in that case we
605
+ * lean on #558's heal-by-overwrite (the non-suppressed fall-through promotes /
606
+ * a fresh render re-`set`s over the bad entry) and skip the delete. Only when
607
+ * this request's fall-through found no copy (=== null) is the eager evict
608
+ * scheduled -- useful then, since nothing else will overwrite the poison entry.
609
+ * A null fall-through can also be a KV-read TIMEOUT rather than a genuine miss:
610
+ * a concurrent request that read KV successfully may be promoting the same key,
611
+ * and this evict could race it. That is benign -- the worst case is one wasted
612
+ * colo-local promote, never a wrong served value, and the next read self-heals
613
+ * -- so we accept it rather than suppressing the evict on a timeout (which
614
+ * would strand the poison entry when KV really is empty). The evict is
615
+ * non-blocking (waitUntil) so it never adds latency to the degraded read.
616
+ * @internal
617
+ */
618
+ private async healCorruptL1<T>(
619
+ cache: Cache,
620
+ request: Request,
621
+ error: unknown,
622
+ label: string,
623
+ fallThrough: () => Promise<T | null>,
624
+ ): Promise<T | null> {
625
+ reportCacheError(
626
+ error ?? new Error("corrupt/partial L1 body"),
627
+ "cache-corrupt",
628
+ `[CFCacheStore] ${label}: corrupt L1 body`,
629
+ );
630
+ const result = await fallThrough();
631
+ if (result === null) {
632
+ const evict = (): Promise<void> =>
633
+ reportingAsync(
634
+ () => cache.delete(request),
635
+ "cache-delete",
636
+ `[CFCacheStore] ${label}: evict corrupt L1`,
637
+ );
638
+ if (this.waitUntil) this.waitUntil(evict);
639
+ else void evict();
640
+ }
641
+ return result;
642
+ }
643
+
644
+ /**
645
+ * Re-put a stale L1 entry marked REVALIDATING, so concurrent requests serve it
646
+ * without each triggering a revalidation. Shared by get()/getItem().
647
+ *
648
+ * The write is NON-BLOCKING (waitUntil) and best-effort by design:
649
+ * - It runs in waitUntil, so it never adds the put latency to the served stale
650
+ * read and a put failure can never turn that good read into a miss. The put
651
+ * is still initiated synchronously (this.waitUntil invokes its callback
652
+ * immediately), so concurrent readers see the marker land at the same time an
653
+ * awaited write would -- awaiting only blocks the current request.
654
+ * - The background revalidation's fresh set() is gated behind a full re-render,
655
+ * so it lands well after this put; a stale-clobbers-fresh race would require
656
+ * this single put to be slower than that entire render+set, and self-heals
657
+ * within MAX_REVALIDATION_INTERVAL.
658
+ *
659
+ * Cache-Control is recomputed to the REMAINING ttl from the stored hard-expiry
660
+ * deadline (see remainingCacheControl), not copied from the original
661
+ * full-window header -- copying it would restart CF retention on every re-arm
662
+ * and pin a perpetually-failing entry past hard-expiry. A legacy/tampered entry
663
+ * without a valid deadline floors to max-age=1 and self-heals via KV.
664
+ * @internal
665
+ */
666
+ private markRevalidating(
667
+ cache: Cache,
668
+ request: Request,
669
+ sourceHeaders: Headers,
670
+ status: number,
671
+ body: string,
672
+ ): void {
673
+ const reputNow = Date.now();
674
+ const headers = new Headers(sourceHeaders);
675
+ headers.set(CACHE_STATUS_HEADER, "REVALIDATING");
676
+ headers.set(CACHE_REVALIDATING_AT_HEADER, String(reputNow));
677
+ headers.set("Cache-Control", remainingCacheControl(headers, reputNow));
678
+ const markerResponse = new Response(body, { status, headers });
679
+ const write = async (): Promise<void> => {
680
+ try {
681
+ await cache.put(request, markerResponse);
682
+ } catch {
683
+ // Best-effort: a failed marker write must not affect the served read;
684
+ // the entry simply re-arms on the next stale read.
685
+ }
686
+ };
687
+ if (this.waitUntil) this.waitUntil(write);
688
+ else void write();
689
+ }
690
+
691
+ /**
692
+ * Document-tier counterpart of markRevalidating for getResponse's herd guard.
693
+ * The segment/item tiers JSON-parse the body, so they re-put with a string
694
+ * body; document bodies are streamed verbatim, so we re-put with a CLONED
695
+ * response body (`response.clone()`) supplied by the caller -- the original
696
+ * body still streams to the client while the marker carries the clone. Same
697
+ * REVALIDATING status header, same revalidating-at stamp, same
698
+ * remainingCacheControl re-put math as markRevalidating, so the document tier
699
+ * suppresses concurrent revalidation for the identical MAX_REVALIDATION_INTERVAL
700
+ * window the segment tier does. Best-effort and non-blocking: a failed marker
701
+ * write must not affect the served stale read.
702
+ * @internal
703
+ */
704
+ private markResponseRevalidating(
705
+ cache: Cache,
706
+ request: Request,
707
+ clonedResponse: Response,
708
+ ): void {
709
+ const reputNow = Date.now();
710
+ const headers = new Headers(clonedResponse.headers);
711
+ headers.set(CACHE_STATUS_HEADER, "REVALIDATING");
712
+ headers.set(CACHE_REVALIDATING_AT_HEADER, String(reputNow));
713
+ headers.set("Cache-Control", remainingCacheControl(headers, reputNow));
714
+ const markerResponse = new Response(clonedResponse.body, {
715
+ status: clonedResponse.status,
716
+ statusText: clonedResponse.statusText,
717
+ headers,
718
+ });
719
+ const write = async (): Promise<void> => {
720
+ try {
721
+ await cache.put(request, markerResponse);
722
+ } catch {
723
+ // Best-effort: see markRevalidating.
724
+ }
725
+ };
726
+ if (this.waitUntil) this.waitUntil(write);
727
+ else void write();
728
+ }
729
+
730
+ // ============================================================================
731
+ // Segment Cache Methods
732
+ // ============================================================================
733
+
734
+ /**
735
+ * Guard the segment tier against a `keyGenerator` that returns a key colliding
736
+ * with a reserved tag-marker namespace: `__tag__/` (the KV marker key) or
737
+ * `__tagmarker__/` (the L1 Cache API marker request). The item/doc tiers are
738
+ * internally prefixed (`fn:`/`doc:`) so only the bare segment key can collide;
739
+ * a collision would let a segment write clobber - or a segment read/delete
740
+ * evict - a live tag marker, silently breaking invalidation. Report loudly
741
+ * (so a misconfigured keyGenerator surfaces immediately) and treat the segment
742
+ * operation as a miss/no-op rather than corrupting the marker namespace.
743
+ * @internal
744
+ */
745
+ private isReservedSegmentKey(
746
+ key: string,
747
+ category: CacheErrorCategory,
748
+ ): boolean {
749
+ const reserved = key.startsWith(TAG_MARKER_PREFIX)
750
+ ? TAG_MARKER_PREFIX
751
+ : key.startsWith(TAG_MARKER_CACHE_PREFIX)
752
+ ? TAG_MARKER_CACHE_PREFIX
753
+ : null;
754
+ if (!reserved) return false;
755
+ reportCacheError(
756
+ new Error(
757
+ `segment key "${key}" collides with the reserved "${reserved}" ` +
758
+ `tag-marker namespace; the operation is ignored. Fix the store ` +
759
+ `keyGenerator so it does not produce keys with this prefix.`,
760
+ ),
761
+ category,
762
+ "[CFCacheStore] reserved key",
763
+ );
764
+ return true;
765
+ }
766
+
222
767
  /**
223
768
  * Get cached entry data by key.
224
769
  *
@@ -227,51 +772,223 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
227
772
  * - If already REVALIDATING (and recent), returns shouldRevalidate: false
228
773
  * - If fresh, returns shouldRevalidate: false
229
774
  *
230
- * The atomic mark prevents thundering herd - only first request triggers revalidation.
775
+ * On L1 miss, falls back to KV (L2) if configured.
776
+ * KV hits are promoted to L1 in the background.
231
777
  */
232
778
  async get(key: string): Promise<CacheGetResult | null> {
779
+ if (this.isReservedSegmentKey(key, "cache-read")) return null;
233
780
  try {
234
781
  const cache = await this.getCache();
235
782
  const request = this.keyToRequest(key);
236
- const response = await cache.match(request);
783
+ const matchStart = Date.now();
784
+ const {
785
+ response,
786
+ timedOut,
787
+ error: matchError,
788
+ } = await this.matchWithTimeout(cache, request);
789
+ const matchMs = Date.now() - matchStart;
237
790
 
238
791
  if (!response) {
792
+ // A transient L1 match error (matchError set) is reported as cache-read
793
+ // but, like a genuine miss or an abandoned slow match (timedOut), still
794
+ // degrades to L2/KV rather than failing the read.
795
+ if (matchError)
796
+ reportCacheError(
797
+ matchError,
798
+ "cache-read",
799
+ "[CFCacheStore] get L1 match",
800
+ );
801
+ if (this.debug)
802
+ this.emitDebug({
803
+ op: "get",
804
+ key,
805
+ // A match REJECTION (matchError) is distinct from a genuine absence:
806
+ // surface it as match-error so debug agrees with the cache-read
807
+ // already routed to onError, instead of masquerading as l1-miss.
808
+ outcome: matchError
809
+ ? "match-error"
810
+ : timedOut
811
+ ? "match-timeout"
812
+ : "l1-miss",
813
+ matchMs,
814
+ });
815
+ return this.kvGetSegment(key);
816
+ }
817
+
818
+ // A non-200 entry (a cached error response, or a foreign response that
819
+ // landed on this key) is not valid segment data; treat it as a miss
820
+ // rather than JSON-parsing garbage and serving it as a hit.
821
+ if (response.status !== 200) {
822
+ if (this.debug)
823
+ this.emitDebug({
824
+ op: "get",
825
+ key,
826
+ outcome: "non-200",
827
+ status: response.status,
828
+ matchMs,
829
+ });
830
+ // Degraded fall-through: suppress revalidation so a broken L1 entry hit
831
+ // concurrently serves KV-stale, not a herd. See kvGetSegment.
832
+ return this.kvGetSegment(key, { suppressRevalidate: true });
833
+ }
834
+
835
+ // Tag invalidation: an entry whose tags were invalidated after it was
836
+ // cached is treated as a miss, so the next render re-populates it. We
837
+ // return null (re-render locally) rather than falling through to KV. In
838
+ // the common case the L1 entry and its KV twin were written together with
839
+ // the same taggedAt, so kvGetSegment's own tag check would miss too and a
840
+ // fall-through is pure cost. The tiers CAN diverge -- another colo may have
841
+ // already re-rendered and written a fresher KV envelope -- in which case a
842
+ // fall-through could serve that copy instead of re-rendering here.
843
+ // Capturing that cross-colo optimization is a deferred follow-up, not a
844
+ // correctness gap: this colo's next read after its own re-render self-heals.
845
+ const tagInfo = this.readTagInfo(response.headers);
846
+ // Measure the marker-resolution tail (memo -> L1 marker cache -> KV) only
847
+ // when debug is on, so the hot path pays nothing. It is the serial read
848
+ // that sits between matchMs and bodyReadMs for a tagged entry.
849
+ const markerStart = this.debug ? Date.now() : 0;
850
+ const invalidated = await this.isGloballyInvalidated(
851
+ tagInfo.tags,
852
+ tagInfo.taggedAt,
853
+ );
854
+ const markerMs = this.debug ? Date.now() - markerStart : undefined;
855
+ if (invalidated) {
856
+ if (this.debug)
857
+ this.emitDebug({
858
+ op: "get",
859
+ key,
860
+ outcome: "tag-invalidated",
861
+ status: response.status,
862
+ matchMs,
863
+ markerMs,
864
+ });
239
865
  return null;
240
866
  }
241
867
 
242
868
  // Read status headers
243
869
  const status = response.headers.get(CACHE_STATUS_HEADER);
244
- const age = Number(response.headers.get("age") ?? "0");
245
870
  const staleAt = Number(
246
871
  response.headers.get(CACHE_STALE_AT_HEADER) ?? "0",
247
872
  );
873
+ const revalidatingAt = Number(
874
+ response.headers.get(CACHE_REVALIDATING_AT_HEADER) ?? "0",
875
+ );
248
876
 
249
- const isStale = staleAt > 0 && Date.now() > staleAt;
877
+ const now = Date.now();
878
+ const isStale = staleAt > 0 && now > staleAt;
879
+ // Recency comes from our explicit revalidating-at stamp, not CF's `Age`
880
+ // header (see CACHE_REVALIDATING_AT_HEADER). An absent/zero stamp counts
881
+ // as "not recent" so a dropped revalidation re-arms instead of pinning.
250
882
  const isRevalidating =
251
- status === "REVALIDATING" && age < MAX_REVALIDATION_INTERVAL;
883
+ status === "REVALIDATING" &&
884
+ revalidatingAt > 0 &&
885
+ now - revalidatingAt < MAX_REVALIDATION_INTERVAL * 1000;
886
+
887
+ // Single emitter for the post-header L1 outcomes. Undefined (so the event
888
+ // object is never allocated) when debug is off; the informational-only
889
+ // `age` header is read lazily inside for the same reason.
890
+ const debugRead = this.debug
891
+ ? (
892
+ outcome: CFCacheReadDebugEvent["outcome"],
893
+ bodyReadMs: number,
894
+ shouldRevalidate?: boolean,
895
+ ) =>
896
+ this.emitDebug({
897
+ op: "get",
898
+ key,
899
+ outcome,
900
+ status: response.status,
901
+ cacheStatus: status,
902
+ staleAt,
903
+ revalidatingAt,
904
+ ageHeader: response.headers.get("age"),
905
+ isStale,
906
+ isRevalidating,
907
+ shouldRevalidate,
908
+ matchMs,
909
+ markerMs,
910
+ bodyReadMs,
911
+ })
912
+ : undefined;
252
913
 
253
914
  // Case 1: Fresh or already being revalidated - just return data
254
915
  if (!isStale || isRevalidating) {
255
- const data = (await response.json()) as CachedEntryData;
916
+ const bodyStart = Date.now();
917
+ const {
918
+ value: data,
919
+ errored,
920
+ error,
921
+ } = await this.readJsonWithTimeout<CachedEntryData>(response);
922
+ const bodyReadMs = Date.now() - bodyStart;
923
+ if (data === undefined) {
924
+ debugRead?.(errored ? "body-error" : "body-timeout", bodyReadMs);
925
+ // A body-ERROR (corrupt/foreign body) self-heals via healCorruptL1:
926
+ // report cache-corrupt, fall through to L2/KV (which overwrites the
927
+ // bad entry), and evict only if KV had no good copy to promote. A
928
+ // body-TIMEOUT is a degraded read of a likely-valid entry: leave it
929
+ // intact and suppress revalidation so a stalling colo cannot herd.
930
+ if (errored)
931
+ return this.healCorruptL1(cache, request, error, "get", () =>
932
+ this.kvGetSegment(key, { suppressRevalidate: false }),
933
+ );
934
+ return this.kvGetSegment(key, { suppressRevalidate: true });
935
+ }
936
+ debugRead?.(
937
+ isRevalidating ? "l1-revalidating-guarded" : "l1-fresh",
938
+ bodyReadMs,
939
+ false,
940
+ );
256
941
  return { data, shouldRevalidate: false };
257
942
  }
258
943
 
259
- // Case 2: Stale and needs revalidation - atomically mark REVALIDATING
260
- const [b1, b2] = response.body!.tee();
261
-
262
- const headers = new Headers(response.headers);
263
- headers.set(CACHE_STATUS_HEADER, "REVALIDATING");
944
+ // Case 2: Stale and needs revalidation.
945
+ // Read the body under the edge-read budget BEFORE writing the REVALIDATING
946
+ // marker. CF can resolve match() fast but stall the body stream; the prior
947
+ // approach teed the stream and awaited cache.put(b1) first, which blocked
948
+ // on that same stalled stream so the read budget could never fire on a
949
+ // stale hit. Reading first bounds the stall and lets us skip marking an
950
+ // entry we could not even read.
951
+ const bodyStart = Date.now();
952
+ const {
953
+ value: data,
954
+ errored,
955
+ error,
956
+ } = await this.readJsonWithTimeout<CachedEntryData>(response);
957
+ const bodyReadMs = Date.now() - bodyStart;
958
+ if (data === undefined) {
959
+ debugRead?.(errored ? "body-error" : "body-timeout", bodyReadMs);
960
+ // Heal + conditionally evict a body-error, suppress a body-timeout; see
961
+ // Case 1.
962
+ if (errored)
963
+ return this.healCorruptL1(
964
+ cache,
965
+ request,
966
+ error,
967
+ "get(revalidating)",
968
+ () => this.kvGetSegment(key, { suppressRevalidate: false }),
969
+ );
970
+ return this.kvGetSegment(key, { suppressRevalidate: true });
971
+ }
264
972
 
265
- // Blocking write - must complete before returning to prevent race
266
- await cache.put(
973
+ // Mark REVALIDATING so concurrent requests don't all revalidate, then
974
+ // return the stale data. The marker write is non-blocking and best-effort
975
+ // (see markRevalidating) -- it must not add latency to, or fail, the served
976
+ // stale read.
977
+ this.markRevalidating(
978
+ cache,
267
979
  request,
268
- new Response(b1, { status: response.status, headers }),
980
+ response.headers,
981
+ response.status,
982
+ JSON.stringify(data),
269
983
  );
270
984
 
271
- const data = (await new Response(b2).json()) as CachedEntryData;
985
+ debugRead?.("l1-stale-revalidate", bodyReadMs, true);
272
986
  return { data, shouldRevalidate: true };
273
987
  } catch (error) {
274
- console.error("[CFCacheStore] get failed:", error);
988
+ // reportCacheError logs and routes to onError (cache-read); the debug
989
+ // emit is the separate wrangler-tail signal. Keep both observability paths.
990
+ reportCacheError(error, "cache-read", "[CFCacheStore] get");
991
+ if (this.debug) this.emitDebug({ op: "get", key, outcome: "error" });
275
992
  return null;
276
993
  }
277
994
  }
@@ -279,6 +996,7 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
279
996
  /**
280
997
  * Store entry data with TTL and optional SWR window.
281
998
  * Uses waitUntil for non-blocking write when available.
999
+ * When KV is configured, also persists to L2.
282
1000
  */
283
1001
  async set(
284
1002
  key: string,
@@ -286,49 +1004,94 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
286
1004
  ttl: number,
287
1005
  swr?: number,
288
1006
  ): Promise<void> {
1007
+ if (this.isReservedSegmentKey(key, "cache-write")) return;
289
1008
  try {
290
1009
  const cache = await this.getCache();
291
1010
  const request = this.keyToRequest(key);
292
1011
 
293
1012
  // Extended TTL covers SWR window
294
- const swrWindow = swr ?? this.defaults?.swr ?? 0;
1013
+ const swrWindow = resolveSwrWindow(swr, this.defaults);
295
1014
  const totalTtl = ttl + swrWindow;
296
1015
  const staleAt = Date.now() + ttl * 1000;
297
1016
 
298
- const response = new Response(JSON.stringify(data), {
1017
+ // Stamp the tag timestamp at write time and carry it (with the tags)
1018
+ // into both the L1 body and the KV envelope so reads can run the
1019
+ // invalidation check.
1020
+ const taggedAt =
1021
+ Array.isArray(data.tags) && data.tags.length > 0
1022
+ ? Date.now()
1023
+ : undefined;
1024
+ const dataToStore: CachedEntryData = taggedAt
1025
+ ? { ...data, taggedAt }
1026
+ : data;
1027
+
1028
+ const body = JSON.stringify(dataToStore);
1029
+ const response = new Response(body, {
299
1030
  headers: {
300
1031
  "Content-Type": "application/json",
301
1032
  "Cache-Control": `public, max-age=${totalTtl}`,
302
1033
  [CACHE_STALE_AT_HEADER]: String(staleAt),
1034
+ // Absolute hard-expiry deadline so a stale-path re-put can recompute a
1035
+ // shrinking max-age instead of restarting retention (see
1036
+ // remainingCacheControl / CACHE_EXPIRES_AT_HEADER).
1037
+ [CACHE_EXPIRES_AT_HEADER]: String(staleAt + swrWindow * 1000),
303
1038
  [CACHE_STATUS_HEADER]: "HIT",
1039
+ ...this.tagHeaderEntries(dataToStore.tags, taggedAt),
304
1040
  },
305
1041
  });
306
1042
 
307
1043
  const putPromise = cache.put(request, response);
308
1044
 
309
1045
  if (this.waitUntil) {
310
- // Non-blocking write
311
- this.waitUntil(async () => {
312
- await putPromise;
313
- });
1046
+ // Non-blocking write. These store-level background tasks intentionally
1047
+ // omit the reportingAsync ctx argument: the store is a request-agnostic
1048
+ // singleton and this.waitUntil is the execution context's, not a single
1049
+ // request's, so a failure is reported console-loud only (it cannot be
1050
+ // attributed to one request's onError). The request-scoped tag verbs
1051
+ // (revalidateTag / stale-revalidation) DO thread their captured ctx.
1052
+ this.waitUntil(() =>
1053
+ reportingAsync(
1054
+ () => putPromise,
1055
+ "cache-write",
1056
+ "[CFCacheStore] L1 write",
1057
+ ),
1058
+ );
314
1059
  } else {
315
1060
  // Blocking fallback
316
1061
  await putPromise;
317
1062
  }
1063
+
1064
+ // L2: persist to KV
1065
+ this.kvSetSegment(key, dataToStore, staleAt, totalTtl, swrWindow);
318
1066
  } catch (error) {
319
- console.error("[CFCacheStore] set failed:", error);
1067
+ reportCacheError(error, "cache-write", "[CFCacheStore] set");
320
1068
  }
321
1069
  }
322
1070
 
323
1071
  /**
324
- * Delete a cached entry
1072
+ * Delete a cached entry from L1 and L2.
325
1073
  */
326
1074
  async delete(key: string): Promise<boolean> {
1075
+ if (this.isReservedSegmentKey(key, "cache-delete")) return false;
327
1076
  try {
328
1077
  const cache = await this.getCache();
329
- return await cache.delete(this.keyToRequest(key));
1078
+ const result = await cache.delete(this.keyToRequest(key));
1079
+
1080
+ // L2: delete from KV
1081
+ if (this.kv && this.waitUntil) {
1082
+ const kvKey = this.toKVKey(key);
1083
+ this.waitUntil(() =>
1084
+ reportingAsync(
1085
+ () => this.kv!.delete(kvKey),
1086
+ "cache-delete",
1087
+ "[CFCacheStore] delete L2",
1088
+ ),
1089
+ );
1090
+ }
1091
+
1092
+ return result;
330
1093
  } catch (error) {
331
- console.error("[CFCacheStore] delete failed:", error);
1094
+ reportCacheError(error, "cache-delete", "[CFCacheStore] delete");
332
1095
  return false;
333
1096
  }
334
1097
  }
@@ -340,6 +1103,7 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
340
1103
  /**
341
1104
  * Get a cached Response by key (for document-level caching).
342
1105
  * Returns the response and whether it should be revalidated (SWR).
1106
+ * Falls back to KV (L2) on L1 miss.
343
1107
  */
344
1108
  async getResponse(
345
1109
  key: string,
@@ -347,50 +1111,173 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
347
1111
  try {
348
1112
  const cache = await this.getCache();
349
1113
  const request = this.keyToRequest(`doc:${key}`);
350
- const response = await cache.match(request);
1114
+ // The document path is outside the debug surface (op is only get/getItem),
1115
+ // so the match-timeout flag is not surfaced as an event here -- though
1116
+ // matchWithTimeout still warns on a slow match. A miss or timeout falls
1117
+ // through to the KV document path and then render.
1118
+ const { response, error: matchError } = await this.matchWithTimeout(
1119
+ cache,
1120
+ request,
1121
+ );
351
1122
 
352
1123
  if (!response || response.status !== 200) {
1124
+ // A transient L1 match rejection (matchError set; only ever set when
1125
+ // response is undefined) is surfaced as cache-read before degrading to
1126
+ // L2/KV -- matching get()/getItem(). A genuine miss or a non-200 hit
1127
+ // carries no matchError and reports nothing.
1128
+ if (matchError)
1129
+ reportCacheError(
1130
+ matchError,
1131
+ "cache-read",
1132
+ "[CFCacheStore] getResponse L1 match",
1133
+ );
1134
+ return this.kvGetResponse(key);
1135
+ }
1136
+
1137
+ // Tag invalidation check (treat invalidated entry as a miss).
1138
+ const tagInfo = this.readTagInfo(response.headers);
1139
+ if (await this.isGloballyInvalidated(tagInfo.tags, tagInfo.taggedAt)) {
353
1140
  return null;
354
1141
  }
355
1142
 
356
1143
  // Check staleness
357
1144
  const staleAt = Number(response.headers.get(CACHE_STALE_AT_HEADER) || 0);
358
- const isStale = staleAt > 0 && Date.now() > staleAt;
1145
+ const now = Date.now();
1146
+ const isStale = staleAt > 0 && now > staleAt;
1147
+
1148
+ // Thundering-herd guard, mirroring the segment (get) and item (getItem)
1149
+ // tiers. Without it, every concurrent stale reader returned
1150
+ // shouldRevalidate=true and document-cache.ts scheduled a fresh render for
1151
+ // each one. Recency comes from our own revalidating-at stamp, not CF's Age
1152
+ // header (see CACHE_REVALIDATING_AT_HEADER); an absent/zero stamp counts as
1153
+ // "not recent" so a dropped revalidation re-arms instead of pinning.
1154
+ const status = response.headers.get(CACHE_STATUS_HEADER);
1155
+ const revalidatingAt = Number(
1156
+ response.headers.get(CACHE_REVALIDATING_AT_HEADER) ?? "0",
1157
+ );
1158
+ const isRevalidating =
1159
+ status === "REVALIDATING" &&
1160
+ revalidatingAt > 0 &&
1161
+ now - revalidatingAt < MAX_REVALIDATION_INTERVAL * 1000;
359
1162
 
1163
+ // L1 document bodies are streamed through verbatim - unlike the segment/
1164
+ // item tiers (which JSON-parse and so structurally detect corruption) and
1165
+ // the KV doc tier (validated in kvGetResponse, KV being the real partial-
1166
+ // read vector). Integrity here relies on the Cache API: cache.put stores a
1167
+ // response atomically or fails, so a truncated body is not served back. We
1168
+ // deliberately do NOT buffer+hash the body to re-verify it: that would
1169
+ // defeat streaming the document and add a full read to every cache hit.
1170
+
1171
+ if (isStale && !isRevalidating) {
1172
+ // First stale reader within the window: mark REVALIDATING (non-blocking,
1173
+ // best-effort) so concurrent readers below see the guard and suppress,
1174
+ // then return shouldRevalidate=true so this caller revalidates. Clone the
1175
+ // matched response for the marker since its original body must still
1176
+ // stream to the client.
1177
+ this.markResponseRevalidating(cache, request, response.clone());
1178
+ return {
1179
+ response: this.toClientResponse(response),
1180
+ shouldRevalidate: true,
1181
+ };
1182
+ }
1183
+
1184
+ // Fresh, or stale-but-already-REVALIDATING: serve without scheduling a
1185
+ // (re-)revalidation. A recent marker already has a render in flight.
360
1186
  return {
361
- response,
362
- shouldRevalidate: isStale,
1187
+ response: this.toClientResponse(response),
1188
+ shouldRevalidate: false,
363
1189
  };
364
1190
  } catch (error) {
365
- console.error("[CFCacheStore] getResponse failed:", error);
1191
+ reportCacheError(error, "cache-read", "[CFCacheStore] getResponse");
366
1192
  return null;
367
1193
  }
368
1194
  }
369
1195
 
1196
+ /**
1197
+ * Strip internal edge headers and restore the author's Cache-Control before a
1198
+ * cached document Response is served to a client. L1 entries carry the
1199
+ * internal staleness/status headers and a rewritten Cache-Control; none of
1200
+ * those should reach the browser or an upstream CDN.
1201
+ */
1202
+ private toClientResponse(response: Response): Response {
1203
+ const headers = new Headers(response.headers);
1204
+ const originalCacheControl = headers.get(CACHE_ORIG_CC_HEADER);
1205
+ if (originalCacheControl !== null) {
1206
+ headers.set("Cache-Control", originalCacheControl);
1207
+ } else {
1208
+ headers.delete("Cache-Control");
1209
+ }
1210
+ headers.delete(CACHE_ORIG_CC_HEADER);
1211
+ headers.delete(CACHE_STALE_AT_HEADER);
1212
+ headers.delete(CACHE_STATUS_HEADER);
1213
+ headers.delete(CACHE_TAGS_HEADER);
1214
+ headers.delete(CACHE_TAGGED_AT_HEADER);
1215
+ // Internal stale-path bookkeeping (hard-expiry deadline + REVALIDATING
1216
+ // stamp). Carried on doc L1 entries for the herd guard; never serve them.
1217
+ headers.delete(CACHE_EXPIRES_AT_HEADER);
1218
+ headers.delete(CACHE_REVALIDATING_AT_HEADER);
1219
+ // Finding #3 (read side): strip per-client signals a pre-fix or
1220
+ // pinned-version L1 entry may carry. See the read-side note in the design doc.
1221
+ stripPerClientSignals(headers);
1222
+ return new Response(response.body, {
1223
+ status: response.status,
1224
+ statusText: response.statusText,
1225
+ headers,
1226
+ });
1227
+ }
1228
+
370
1229
  /**
371
1230
  * Store a Response with TTL and optional SWR window (for document-level caching).
1231
+ * When KV is configured, also persists to L2.
372
1232
  */
373
1233
  async putResponse(
374
1234
  key: string,
375
1235
  response: Response,
376
1236
  ttl: number,
377
1237
  swr?: number,
1238
+ tags?: string[],
378
1239
  ): Promise<void> {
379
1240
  try {
380
1241
  const cache = await this.getCache();
381
1242
  const request = this.keyToRequest(`doc:${key}`);
382
1243
 
383
1244
  // Extended TTL covers SWR window
384
- const swrWindow = swr ?? this.defaults?.swr ?? 0;
1245
+ const swrWindow = resolveSwrWindow(swr, this.defaults);
385
1246
  const totalTtl = ttl + swrWindow;
386
1247
  const staleAt = Date.now() + ttl * 1000;
1248
+ const taggedAt =
1249
+ Array.isArray(tags) && tags.length > 0 ? Date.now() : undefined;
387
1250
 
388
- // Clone and add cache headers
1251
+ // Clone body for potential KV write before consuming it for L1
1252
+ const [l1Body, kvBody] = this.kv
1253
+ ? response.body
1254
+ ? response.body.tee()
1255
+ : [null, null]
1256
+ : [response.body, null];
1257
+
1258
+ // Clone and add cache headers. The author's Cache-Control is stashed and
1259
+ // replaced with a long max-age so the CF Cache API holds the entry across
1260
+ // the SWR window; getResponse restores the original before serving.
389
1261
  const headers = new Headers(response.headers);
1262
+ // Finding #3: never persist a per-client signal in the shared L1 entry
1263
+ // (the platform's Set-Cookie rejection is unverified and ignores the
1264
+ // directive anyway). See stripPerClientSignals.
1265
+ stripPerClientSignals(headers);
1266
+ const originalCacheControl = response.headers.get("Cache-Control");
1267
+ if (originalCacheControl !== null) {
1268
+ headers.set(CACHE_ORIG_CC_HEADER, originalCacheControl);
1269
+ }
390
1270
  headers.set("Cache-Control", `public, max-age=${totalTtl}`);
391
1271
  headers.set(CACHE_STALE_AT_HEADER, String(staleAt));
1272
+ // Absolute hard-expiry deadline so a stale-path REVALIDATING re-put can
1273
+ // recompute a shrinking max-age (remainingCacheControl) instead of
1274
+ // restarting retention. Mirrors set()/setItem(). Stripped by
1275
+ // toClientResponse before serving.
1276
+ headers.set(CACHE_EXPIRES_AT_HEADER, String(staleAt + swrWindow * 1000));
1277
+ // Internal tag headers (stripped by toClientResponse before serving).
1278
+ this.setTagHeaders(headers, tags, taggedAt);
392
1279
 
393
- const toCache = new Response(response.body, {
1280
+ const toCache = new Response(l1Body, {
394
1281
  status: response.status,
395
1282
  statusText: response.statusText,
396
1283
  headers,
@@ -400,29 +1287,1492 @@ export class CFCacheStore<TEnv = unknown> implements SegmentCacheStore<TEnv> {
400
1287
 
401
1288
  if (this.waitUntil) {
402
1289
  // Non-blocking write
403
- this.waitUntil(async () => {
404
- await putPromise;
405
- });
1290
+ this.waitUntil(() =>
1291
+ reportingAsync(
1292
+ () => putPromise,
1293
+ "cache-write",
1294
+ "[CFCacheStore] L1 write",
1295
+ ),
1296
+ );
406
1297
  } else {
407
1298
  // Blocking fallback
408
1299
  await putPromise;
409
1300
  }
1301
+
1302
+ // L2: persist to KV (KV requires expirationTtl >= 60s)
1303
+ if (this.kv && this.waitUntil && totalTtl >= 60) {
1304
+ const kvKey = this.toDocKVKey(key);
1305
+ // Finding #3: never persist a per-client signal in the KV envelope.
1306
+ const headersArray: [string, string][] = [];
1307
+ response.headers.forEach((v, k) => {
1308
+ if (isPerClientSignalHeader(k)) return;
1309
+ headersArray.push([k, v]);
1310
+ });
1311
+ // Read body as ArrayBuffer and encode to base64 to preserve binary payloads
1312
+ const bodyBuf = kvBody
1313
+ ? await new Response(kvBody).arrayBuffer()
1314
+ : new ArrayBuffer(0);
1315
+ const bodyBase64 = bufferToBase64(bodyBuf);
1316
+
1317
+ this.waitUntil(() =>
1318
+ reportingAsync(
1319
+ () => {
1320
+ const envelope: KVResponseEnvelope = {
1321
+ b: bodyBase64,
1322
+ st: response.status,
1323
+ stx: response.statusText,
1324
+ hd: headersArray,
1325
+ s: staleAt,
1326
+ e: staleAt + swrWindow * 1000,
1327
+ t: tags,
1328
+ ta: taggedAt,
1329
+ };
1330
+ return this.kv!.put(kvKey, JSON.stringify(envelope), {
1331
+ expirationTtl: totalTtl,
1332
+ });
1333
+ },
1334
+ "cache-write",
1335
+ "[CFCacheStore] kvPutResponse",
1336
+ ),
1337
+ );
1338
+ }
410
1339
  } catch (error) {
411
- console.error("[CFCacheStore] putResponse failed:", error);
1340
+ reportCacheError(error, "cache-write", "[CFCacheStore] putResponse");
412
1341
  }
413
1342
  }
414
1343
 
1344
+ // ============================================================================
1345
+ // Function Cache Methods (for "use cache" directive)
1346
+ // ============================================================================
1347
+
415
1348
  /**
416
- * Convert string key to Request object for CF Cache API.
417
- * Includes version in URL if specified (for cache invalidation on code changes).
418
- * @internal
1349
+ * Get a cached function result by key.
1350
+ * Follows the same SWR pattern as get() for segment caching.
1351
+ * Falls back to KV (L2) on L1 miss.
419
1352
  */
420
- private keyToRequest(key: string): Request {
421
- const encodedKey = encodeURIComponent(key);
422
- // Include version in URL path to invalidate cache when version changes
423
- const versionPath = this.version ? `v/${this.version}/` : "";
424
- return new Request(`${this.baseUrl}${versionPath}${encodedKey}`, {
425
- method: "GET",
426
- });
1353
+ async getItem(key: string): Promise<CacheItemResult | null> {
1354
+ try {
1355
+ const cache = await this.getCache();
1356
+ const request = this.keyToRequest(`fn:${key}`);
1357
+ const matchStart = Date.now();
1358
+ const {
1359
+ response,
1360
+ timedOut,
1361
+ error: matchError,
1362
+ } = await this.matchWithTimeout(cache, request);
1363
+ const matchMs = Date.now() - matchStart;
1364
+
1365
+ if (!response) {
1366
+ // Transient match error reported cache-read; still degrades to L2/KV.
1367
+ if (matchError)
1368
+ reportCacheError(
1369
+ matchError,
1370
+ "cache-read",
1371
+ "[CFCacheStore] getItem L1 match",
1372
+ );
1373
+ if (this.debug)
1374
+ this.emitDebug({
1375
+ op: "getItem",
1376
+ key,
1377
+ // match-error (rejection) vs l1-miss (absence); see get().
1378
+ outcome: matchError
1379
+ ? "match-error"
1380
+ : timedOut
1381
+ ? "match-timeout"
1382
+ : "l1-miss",
1383
+ matchMs,
1384
+ });
1385
+ return this.kvGetItem(key);
1386
+ }
1387
+
1388
+ // Non-200 entry is not a valid cached function result; treat as a miss.
1389
+ if (response.status !== 200) {
1390
+ if (this.debug)
1391
+ this.emitDebug({
1392
+ op: "getItem",
1393
+ key,
1394
+ outcome: "non-200",
1395
+ status: response.status,
1396
+ matchMs,
1397
+ });
1398
+ // Degraded fall-through: suppress revalidation so a broken L1 entry hit
1399
+ // concurrently serves KV-stale instead of spawning a herd (see get()).
1400
+ return this.kvGetItem(key, { suppressRevalidate: true });
1401
+ }
1402
+
1403
+ // Tag invalidation check (treat invalidated entry as a miss). Measure the
1404
+ // marker-resolution tail only under debug (see get()).
1405
+ const tagInfo = this.readTagInfo(response.headers);
1406
+ const markerStart = this.debug ? Date.now() : 0;
1407
+ const invalidated = await this.isGloballyInvalidated(
1408
+ tagInfo.tags,
1409
+ tagInfo.taggedAt,
1410
+ );
1411
+ const markerMs = this.debug ? Date.now() - markerStart : undefined;
1412
+ if (invalidated) {
1413
+ if (this.debug)
1414
+ this.emitDebug({
1415
+ op: "getItem",
1416
+ key,
1417
+ outcome: "tag-invalidated",
1418
+ status: response.status,
1419
+ matchMs,
1420
+ markerMs,
1421
+ });
1422
+ return null;
1423
+ }
1424
+
1425
+ const staleAt = Number(
1426
+ response.headers.get(CACHE_STALE_AT_HEADER) ?? "0",
1427
+ );
1428
+ const status = response.headers.get(CACHE_STATUS_HEADER);
1429
+ const revalidatingAt = Number(
1430
+ response.headers.get(CACHE_REVALIDATING_AT_HEADER) ?? "0",
1431
+ );
1432
+
1433
+ const now = Date.now();
1434
+ const isStale = staleAt > 0 && now > staleAt;
1435
+ // Recency from our explicit stamp, not CF's `Age` header (see get()).
1436
+ const isRevalidating =
1437
+ status === "REVALIDATING" &&
1438
+ revalidatingAt > 0 &&
1439
+ now - revalidatingAt < MAX_REVALIDATION_INTERVAL * 1000;
1440
+
1441
+ // Single emitter for the post-header L1 outcomes (see get()). Undefined
1442
+ // when debug is off, so the event object is never allocated on the hot
1443
+ // path; the informational-only `age` header is read lazily inside.
1444
+ const debugRead = this.debug
1445
+ ? (
1446
+ outcome: CFCacheReadDebugEvent["outcome"],
1447
+ bodyReadMs: number,
1448
+ shouldRevalidate?: boolean,
1449
+ ) =>
1450
+ this.emitDebug({
1451
+ op: "getItem",
1452
+ key,
1453
+ outcome,
1454
+ status: response.status,
1455
+ cacheStatus: status,
1456
+ staleAt,
1457
+ revalidatingAt,
1458
+ ageHeader: response.headers.get("age"),
1459
+ isStale,
1460
+ isRevalidating,
1461
+ shouldRevalidate,
1462
+ matchMs,
1463
+ markerMs,
1464
+ bodyReadMs,
1465
+ })
1466
+ : undefined;
1467
+
1468
+ const bodyStart = Date.now();
1469
+ const {
1470
+ value: data,
1471
+ errored,
1472
+ error,
1473
+ } = await this.readJsonWithTimeout<{
1474
+ value: string;
1475
+ handles?: string;
1476
+ }>(response);
1477
+ const bodyReadMs = Date.now() - bodyStart;
1478
+ if (data === undefined) {
1479
+ debugRead?.(errored ? "body-error" : "body-timeout", bodyReadMs);
1480
+ // Heal + conditionally evict a body-error, suppress a body-timeout; see
1481
+ // get().
1482
+ if (errored)
1483
+ return this.healCorruptL1(cache, request, error, "getItem", () =>
1484
+ this.kvGetItem(key, { suppressRevalidate: false }),
1485
+ );
1486
+ return this.kvGetItem(key, { suppressRevalidate: true });
1487
+ }
1488
+
1489
+ if (!isStale || isRevalidating) {
1490
+ debugRead?.(
1491
+ isRevalidating ? "l1-revalidating-guarded" : "l1-fresh",
1492
+ bodyReadMs,
1493
+ false,
1494
+ );
1495
+ return {
1496
+ value: data.value,
1497
+ handles: data.handles,
1498
+ shouldRevalidate: false,
1499
+ tags: tagInfo.tags,
1500
+ };
1501
+ }
1502
+
1503
+ // Stale and needs revalidation -- mark REVALIDATING (non-blocking,
1504
+ // best-effort, remaining-ttl) and return the stale value. See get() /
1505
+ // markRevalidating for the full rationale.
1506
+ this.markRevalidating(
1507
+ cache,
1508
+ request,
1509
+ response.headers,
1510
+ 200,
1511
+ JSON.stringify(data),
1512
+ );
1513
+
1514
+ debugRead?.("l1-stale-revalidate", bodyReadMs, true);
1515
+ return {
1516
+ value: data.value,
1517
+ handles: data.handles,
1518
+ shouldRevalidate: true,
1519
+ tags: tagInfo.tags,
1520
+ };
1521
+ } catch (error) {
1522
+ reportCacheError(error, "cache-read", "[CFCacheStore] getItem");
1523
+ if (this.debug) this.emitDebug({ op: "getItem", key, outcome: "error" });
1524
+ return null;
1525
+ }
1526
+ }
1527
+
1528
+ /**
1529
+ * Store a function result with TTL and optional SWR window.
1530
+ * When KV is configured, also persists to L2.
1531
+ */
1532
+ async setItem(
1533
+ key: string,
1534
+ value: string,
1535
+ options?: CacheItemOptions,
1536
+ ): Promise<void> {
1537
+ try {
1538
+ const cache = await this.getCache();
1539
+ const request = this.keyToRequest(`fn:${key}`);
1540
+
1541
+ const ttl = resolveTtl(options?.ttl, this.defaults, DEFAULT_FUNCTION_TTL);
1542
+ const swrWindow = resolveSwrWindow(options?.swr, this.defaults);
1543
+ const totalTtl = ttl + swrWindow;
1544
+ const staleAt = Date.now() + ttl * 1000;
1545
+
1546
+ const tags = options?.tags;
1547
+ const taggedAt =
1548
+ Array.isArray(tags) && tags.length > 0 ? Date.now() : undefined;
1549
+
1550
+ const body = JSON.stringify({ value, handles: options?.handles });
1551
+ const response = new Response(body, {
1552
+ headers: {
1553
+ "Content-Type": "application/json",
1554
+ "Cache-Control": `public, max-age=${totalTtl}`,
1555
+ [CACHE_STALE_AT_HEADER]: String(staleAt),
1556
+ // Absolute hard-expiry deadline; see set() / remainingCacheControl.
1557
+ [CACHE_EXPIRES_AT_HEADER]: String(staleAt + swrWindow * 1000),
1558
+ [CACHE_STATUS_HEADER]: "HIT",
1559
+ ...this.tagHeaderEntries(tags, taggedAt),
1560
+ },
1561
+ });
1562
+
1563
+ const putPromise = cache.put(request, response);
1564
+
1565
+ if (this.waitUntil) {
1566
+ this.waitUntil(() =>
1567
+ reportingAsync(
1568
+ () => putPromise,
1569
+ "cache-write",
1570
+ "[CFCacheStore] L1 write",
1571
+ ),
1572
+ );
1573
+ } else {
1574
+ await putPromise;
1575
+ }
1576
+
1577
+ // L2: persist to KV (KV requires expirationTtl >= 60s)
1578
+ if (this.kv && this.waitUntil && totalTtl >= 60) {
1579
+ const kvKey = this.toKVKey(`fn:${key}`);
1580
+ this.waitUntil(() =>
1581
+ reportingAsync(
1582
+ () => {
1583
+ const envelope: KVItemEnvelope = {
1584
+ v: value,
1585
+ h: options?.handles,
1586
+ s: staleAt,
1587
+ e: staleAt + swrWindow * 1000,
1588
+ t: tags,
1589
+ ta: taggedAt,
1590
+ };
1591
+ return this.kv!.put(kvKey, JSON.stringify(envelope), {
1592
+ expirationTtl: totalTtl,
1593
+ });
1594
+ },
1595
+ "cache-write",
1596
+ "[CFCacheStore] kvSetItem",
1597
+ ),
1598
+ );
1599
+ }
1600
+ } catch (error) {
1601
+ reportCacheError(error, "cache-write", "[CFCacheStore] setItem");
1602
+ }
1603
+ }
1604
+
1605
+ // ============================================================================
1606
+ // Shell Cache Methods (PPR shell resume) — KV-only in v1
1607
+ // ============================================================================
1608
+ //
1609
+ // Unlike the segment/item/document tiers, the shell family has NO Cache-API L1
1610
+ // tier: the prelude bytes + postponed blob are large and version-coupled, and a
1611
+ // per-colo L1 for them is a deliberate follow-up (see the PPR shell-resume
1612
+ // design doc). Shell entries live only in KV (the global tier), so the family
1613
+ // requires a configured KV namespace; without one, getShell/putShell no-op and
1614
+ // the shell-cache middleware fails open to a full HTML render. Tag invalidation
1615
+ // still applies: shell entries carry tags/taggedAt and are checked against the
1616
+ // same KV markers isGloballyInvalidated() reads for every other tier.
1617
+
1618
+ /**
1619
+ * Get a cached PPR shell entry by key from KV (no L1). Applies the KV read
1620
+ * budget, corrupt-entry eviction, hard-expiry, and tag invalidation exactly
1621
+ * like kvGetItem, minus the L1 promote. SWR is a plain staleness flag — KV has
1622
+ * no REVALIDATING herd guard, so the shell-cache middleware's module-level
1623
+ * in-flight set is the recapture stampede guard.
1624
+ */
1625
+ async getShell(
1626
+ key: string,
1627
+ ): Promise<{ entry: ShellCacheEntry; shouldRevalidate?: boolean } | null> {
1628
+ if (!this.kv) return null;
1629
+ try {
1630
+ const kvKey = this.toKVKey(`shell:${key}`);
1631
+ const { value: envelope, timedOut } =
1632
+ await this.kvGetOrEvict<KVShellEnvelope>(
1633
+ kvKey,
1634
+ (e) =>
1635
+ typeof e.p === "string" &&
1636
+ (e.po === null || typeof e.po === "string") &&
1637
+ typeof e.rv === "string" &&
1638
+ typeof e.e === "number" &&
1639
+ typeof e.s === "number",
1640
+ "getShell",
1641
+ );
1642
+ // A timeout, a missing key, or an already-evicted corrupt entry is a miss.
1643
+ if (timedOut || !envelope) return null;
1644
+
1645
+ const now = Date.now();
1646
+ if (now > envelope.e) return null;
1647
+
1648
+ if (await this.isGloballyInvalidated(envelope.t, envelope.ta)) {
1649
+ return null;
1650
+ }
1651
+
1652
+ const shouldRevalidate = envelope.s > 0 && now > envelope.s;
1653
+ return {
1654
+ entry: {
1655
+ prelude: envelope.p,
1656
+ postponed: envelope.po,
1657
+ reactVersion: envelope.rv,
1658
+ initialTheme: envelope.i,
1659
+ snapshot: envelope.sn,
1660
+ createdAt: envelope.c,
1661
+ },
1662
+ shouldRevalidate,
1663
+ };
1664
+ } catch (error) {
1665
+ reportCacheError(error, "cache-read", "[CFCacheStore] getShell");
1666
+ return null;
1667
+ }
1668
+ }
1669
+
1670
+ /**
1671
+ * Store a PPR shell entry in KV with TTL and optional SWR window. Non-blocking
1672
+ * (waitUntil) like the other KV writes. The tags/taggedAt ride in the envelope
1673
+ * so isGloballyInvalidated() can invalidate the shell via the shared KV markers.
1674
+ */
1675
+ async putShell(
1676
+ key: string,
1677
+ entry: ShellCacheEntry,
1678
+ ttlSeconds?: number,
1679
+ swrSeconds?: number,
1680
+ tags?: string[],
1681
+ ): Promise<void> {
1682
+ // KV-only tier: needs a KV namespace and waitUntil (writes are non-blocking).
1683
+ if (!this.kv || !this.waitUntil) return;
1684
+ try {
1685
+ const ttl = resolveTtl(ttlSeconds, this.defaults, DEFAULT_FUNCTION_TTL);
1686
+ const swrWindow = resolveSwrWindow(swrSeconds, this.defaults);
1687
+ const totalTtl = ttl + swrWindow;
1688
+ // KV requires expirationTtl >= 60s; skip a shorter-lived shell rather than
1689
+ // letting kv.put reject inside waitUntil (mirrors setItem/kvSetSegment).
1690
+ if (totalTtl < 60) return;
1691
+
1692
+ const staleAt = Date.now() + ttl * 1000;
1693
+ const taggedAt =
1694
+ Array.isArray(tags) && tags.length > 0 ? Date.now() : undefined;
1695
+
1696
+ const kvKey = this.toKVKey(`shell:${key}`);
1697
+ // A key over the KV limit makes kv.put reject deep inside waitUntil; report
1698
+ // and skip the doomed write (mirrors kvSetSegment).
1699
+ const kvKeyBytes = kvKeyByteLength(kvKey);
1700
+ if (kvKeyBytes > KV_MAX_KEY_BYTES) {
1701
+ reportCacheError(
1702
+ new Error(
1703
+ `shell cache key produces a ${kvKeyBytes}-byte KV key, over the ` +
1704
+ `${KV_MAX_KEY_BYTES}-byte limit; the shell was not persisted.`,
1705
+ ),
1706
+ "cache-write",
1707
+ "[CFCacheStore] putShell",
1708
+ );
1709
+ return;
1710
+ }
1711
+
1712
+ this.waitUntil(() =>
1713
+ reportingAsync(
1714
+ () => {
1715
+ const envelope: KVShellEnvelope = {
1716
+ p: entry.prelude,
1717
+ po: entry.postponed,
1718
+ rv: entry.reactVersion,
1719
+ c: entry.createdAt,
1720
+ s: staleAt,
1721
+ e: staleAt + swrWindow * 1000,
1722
+ t: tags,
1723
+ ta: taggedAt,
1724
+ i: entry.initialTheme,
1725
+ sn: entry.snapshot,
1726
+ };
1727
+ return this.kv!.put(kvKey, JSON.stringify(envelope), {
1728
+ expirationTtl: totalTtl,
1729
+ });
1730
+ },
1731
+ "cache-write",
1732
+ "[CFCacheStore] putShell",
1733
+ ),
1734
+ );
1735
+ } catch (error) {
1736
+ reportCacheError(error, "cache-write", "[CFCacheStore] putShell");
1737
+ }
1738
+ }
1739
+
1740
+ // ============================================================================
1741
+ // Key Helpers
1742
+ // ============================================================================
1743
+
1744
+ /**
1745
+ * Convert string key to Request object for CF Cache API.
1746
+ * Includes version in URL if specified (for cache invalidation on code changes).
1747
+ * @internal
1748
+ */
1749
+ private keyToRequest(key: string): Request {
1750
+ const encodedKey = encodeURIComponent(key);
1751
+ // Include version in URL path to invalidate cache when version changes
1752
+ const versionPath = this.version ? `v/${this.version}/` : "";
1753
+ return new Request(`${this.resolveBaseUrl()}${versionPath}${encodedKey}`, {
1754
+ method: "GET",
1755
+ });
1756
+ }
1757
+
1758
+ /**
1759
+ * Convert string key to KV key string.
1760
+ * Uses same version prefix as Cache API for consistent invalidation.
1761
+ * @internal
1762
+ */
1763
+ private toKVKey(key: string): string {
1764
+ const versionPath = this.version ? `v/${this.version}/` : "";
1765
+ return `${versionPath}${key}`;
1766
+ }
1767
+
1768
+ /**
1769
+ * Host token for the current request, used to namespace the document KV key.
1770
+ * Derived from the same resolveBaseUrl() that namespaces the L1 (Cache API)
1771
+ * tier, so a doc entry's KV twin lands under the identical host bucket.
1772
+ * Falls back to "_" if the base URL cannot be parsed (it always carries a
1773
+ * trailing-slash origin, so parsing succeeds in practice).
1774
+ * @internal
1775
+ */
1776
+ private docKVHost(): string {
1777
+ try {
1778
+ return new URL(this.resolveBaseUrl()).host || "_";
1779
+ } catch {
1780
+ return "_";
1781
+ }
1782
+ }
1783
+
1784
+ /**
1785
+ * Convert a document key to its host-namespaced KV key. The L1 tier already
1786
+ * namespaces document entries by host via keyToRequest/resolveBaseUrl, but the
1787
+ * KV fallback keyed only on `doc:{key}`, so two hosts serving the same path
1788
+ * could collide on the KV tier (one host serving another's cached document).
1789
+ * Prefixing the host closes that cross-host collision. Deterministic per
1790
+ * (host, key). Segment/fn/tag-marker KV keys keep toKVKey unchanged: tag
1791
+ * markers are intentionally global (invalidation must cross hosts), and the
1792
+ * document tier is the one with a request-host context here.
1793
+ * @internal
1794
+ */
1795
+ private toDocKVKey(key: string): string {
1796
+ return this.toKVKey(`h/${this.docKVHost()}/doc:${key}`);
1797
+ }
1798
+
1799
+ /**
1800
+ * Best-effort delete of a single KV key, reporting (not swallowing) a delete
1801
+ * failure as cache-delete. Used by the corrupt-entry self-heal paths.
1802
+ * @internal
1803
+ */
1804
+ private async evictKvKey(kvKey: string, label: string): Promise<void> {
1805
+ try {
1806
+ await this.kv!.delete(kvKey);
1807
+ } catch (error) {
1808
+ reportCacheError(
1809
+ error,
1810
+ "cache-delete",
1811
+ `[CFCacheStore] ${label}: evict failed`,
1812
+ );
1813
+ }
1814
+ }
1815
+
1816
+ /**
1817
+ * Schedule a corrupt-entry KV eviction as a NON-BLOCKING background task
1818
+ * (waitUntil) instead of awaiting it on the request path. The corrupt read has
1819
+ * already resolved to a miss; awaiting an unbounded kv.delete here would re-add
1820
+ * exactly the multi-second stall the read budgets exist to prevent when the KV
1821
+ * namespace is degraded. evictKvKey never rejects (it reports its own failure),
1822
+ * so the fire-and-forget fallback is safe when no waitUntil is available.
1823
+ * @internal
1824
+ */
1825
+ private scheduleKvEvict(kvKey: string, label: string): void {
1826
+ const evict = (): Promise<void> => this.evictKvKey(kvKey, label);
1827
+ if (this.waitUntil) this.waitUntil(evict);
1828
+ else void evict();
1829
+ }
1830
+
1831
+ /**
1832
+ * KV-get a JSON envelope, EVICTING the key only when it is genuinely corrupt.
1833
+ *
1834
+ * Reads as { type: "text" }, NOT { type: "json" }, on purpose: the "json" form
1835
+ * fuses the network read and the JSON parse, so a transient KV outage (5xx/429/
1836
+ * network blip) is indistinguishable from a malformed body and would delete a
1837
+ * still-good cross-colo entry - a self-inflicted miss storm. Reading text lets a
1838
+ * transient read error propagate to the caller's outer catch (reported
1839
+ * cache-read, the entry left intact); only a JSON.parse failure on a body that
1840
+ * WAS successfully read - or an envelope that parses but fails `validate`
1841
+ * (fields missing from a truncated write) - is true corruption that evicts +
1842
+ * reports cache-corrupt. A MISSING key (kv.get -> null) is a normal miss.
1843
+ * @internal
1844
+ */
1845
+ private async kvGetOrEvict<T>(
1846
+ kvKey: string,
1847
+ validate: (envelope: T) => boolean,
1848
+ label: string,
1849
+ ): Promise<{ value: T | null; timedOut: boolean }> {
1850
+ // Bound the read with the KV latency budget (inherited from #558) so a
1851
+ // degraded namespace cannot pin the request. readWithTimeout reports
1852
+ // timedOut on budget expiry; a transient read REJECTION (5xx/429/network)
1853
+ // instead propagates out to the caller's outer catch (reported cache-read,
1854
+ // the entry left intact) -- deliberately NOT caught as corruption.
1855
+ const { value: raw, timedOut } = await this.readWithTimeout<unknown>(
1856
+ () => this.kv!.get(kvKey, { type: "text" }),
1857
+ this.kvReadTimeoutMs,
1858
+ "KV read",
1859
+ );
1860
+ if (timedOut) return { value: null, timedOut: true };
1861
+ if (raw == null) return { value: null, timedOut: false }; // missing = miss
1862
+
1863
+ // Real CF KV with { type: "text" } returns a string: parse + structurally
1864
+ // validate it; a parse/validate failure on a successfully-read body is the
1865
+ // only true corruption (evict + cache-corrupt). A KV binding that already
1866
+ // returns a parsed object (some shims/tests) is used as-is.
1867
+ let envelope: T;
1868
+ if (typeof raw === "string") {
1869
+ try {
1870
+ envelope = JSON.parse(raw) as T;
1871
+ } catch (error) {
1872
+ reportCacheError(
1873
+ error,
1874
+ "cache-corrupt",
1875
+ `[CFCacheStore] ${label}: corrupt JSON in KV, evicting`,
1876
+ );
1877
+ this.scheduleKvEvict(kvKey, label);
1878
+ return { value: null, timedOut: false };
1879
+ }
1880
+ } else {
1881
+ envelope = raw as T;
1882
+ }
1883
+
1884
+ // A body that parses to null or a primitive ('null', '42', 'true', '"x"')
1885
+ // is not a valid envelope. Guard it BEFORE validate(): the property-reading
1886
+ // validators throw on a null/primitive rather than returning false, which
1887
+ // would escape to the caller's outer catch as a transient cache-read and
1888
+ // leave the bad key un-evicted (re-failing every read until its KV TTL). The
1889
+ // typeof check short-circuits validate() so it only ever runs on an object.
1890
+ if (
1891
+ envelope == null ||
1892
+ typeof envelope !== "object" ||
1893
+ !validate(envelope)
1894
+ ) {
1895
+ reportCacheError(
1896
+ new Error("malformed/partial KV envelope"),
1897
+ "cache-corrupt",
1898
+ `[CFCacheStore] ${label}: malformed envelope, evicting`,
1899
+ );
1900
+ this.scheduleKvEvict(kvKey, label);
1901
+ return { value: null, timedOut: false };
1902
+ }
1903
+ return { value: envelope, timedOut: false };
1904
+ }
1905
+
1906
+ // ============================================================================
1907
+ // Tag Invalidation (single-store: markers live in this.kv)
1908
+ // ============================================================================
1909
+
1910
+ /** KV key for a tag's invalidation marker. */
1911
+ private tagMarkerKey(tag: string): string {
1912
+ return this.toKVKey(`${TAG_MARKER_PREFIX}${tag}`);
1913
+ }
1914
+
1915
+ /**
1916
+ * Header entries carrying an entry's tags (JSON-encoded, comma-safe) and the
1917
+ * timestamp they were attached. Returns an empty object when there are no
1918
+ * tags so untagged entries stay header-free and skip the invalidation check.
1919
+ */
1920
+ private tagHeaderEntries(
1921
+ tags: string[] | undefined,
1922
+ taggedAt: number | undefined,
1923
+ ): Record<string, string> {
1924
+ if (!Array.isArray(tags) || tags.length === 0 || !taggedAt) return {};
1925
+ return {
1926
+ // encodeURIComponent so the value is pure ASCII: HTTP header values are
1927
+ // ByteStrings, but JSON.stringify leaves codepoints > U+00FF (emoji/CJK)
1928
+ // verbatim, which makes new Response({ headers }) throw and the outer
1929
+ // try/catch silently drop the whole entry from cache. Decoded in
1930
+ // readTagInfo. The L1 marker Cache-Tag path encodes for the same reason.
1931
+ [CACHE_TAGS_HEADER]: encodeURIComponent(JSON.stringify(tags)),
1932
+ [CACHE_TAGGED_AT_HEADER]: String(taggedAt),
1933
+ };
1934
+ }
1935
+
1936
+ /**
1937
+ * Merge the internal tag headers onto an existing Headers instance. The
1938
+ * from-scratch paths spread tagHeaderEntries() into an object-literal init;
1939
+ * the document put/promote paths build a Headers first, so they .set() each
1940
+ * entry instead.
1941
+ */
1942
+ private setTagHeaders(
1943
+ headers: Headers,
1944
+ tags: string[] | undefined,
1945
+ taggedAt: number | undefined,
1946
+ ): void {
1947
+ for (const [name, value] of Object.entries(
1948
+ this.tagHeaderEntries(tags, taggedAt),
1949
+ )) {
1950
+ headers.set(name, value);
1951
+ }
1952
+ }
1953
+
1954
+ /** Read an entry's tags/taggedAt back from its headers. */
1955
+ private readTagInfo(headers: Headers): {
1956
+ tags?: string[];
1957
+ taggedAt?: number;
1958
+ } {
1959
+ const rawTags = headers.get(CACHE_TAGS_HEADER);
1960
+ const rawTaggedAt = headers.get(CACHE_TAGGED_AT_HEADER);
1961
+ if (!rawTags || !rawTaggedAt) return {};
1962
+ try {
1963
+ const taggedAt = Number(rawTaggedAt);
1964
+ // A corrupt/non-numeric tagged-at header yields NaN. isGloballyInvalidated
1965
+ // short-circuits on a falsy taggedAt (NaN is falsy), so returning
1966
+ // { taggedAt: NaN } would make the entry permanently NON-invalidatable -
1967
+ // a revalidateTag could never evict it. Treat a non-finite stamp the same
1968
+ // as the missing-header case (untagged): drop both tags and taggedAt so the
1969
+ // entry is re-rendered/re-tagged rather than silently un-invalidatable.
1970
+ if (!Number.isFinite(taggedAt)) return {};
1971
+ return {
1972
+ tags: JSON.parse(decodeURIComponent(rawTags)) as string[],
1973
+ taggedAt,
1974
+ };
1975
+ } catch {
1976
+ return {};
1977
+ }
1978
+ }
1979
+
1980
+ /**
1981
+ * Whether an entry tagged at `taggedAt` with `tags` has been invalidated since.
1982
+ * Reads the per-tag invalidation markers from KV and returns true if any tag's
1983
+ * latest invalidation is at or after taggedAt (>= so a same-millisecond
1984
+ * invalidate wins, favouring freshness over staleness). Fails open: KV errors
1985
+ * never turn a hit into a wrongful miss-storm beyond this single read.
1986
+ */
1987
+ private async isGloballyInvalidated(
1988
+ tags: string[] | undefined,
1989
+ taggedAt: number | undefined,
1990
+ ): Promise<boolean> {
1991
+ // Array.isArray (not just truthiness): a non-array tags value - direct store
1992
+ // misuse like setItem(k, v, { tags: "products" }), or a skewed KV envelope -
1993
+ // must fail safe to "not invalidated" rather than throwing `.map` on every
1994
+ // read (which the outer catch would mis-report as a transient cache-read).
1995
+ if (!this.kv || !Array.isArray(tags) || tags.length === 0 || !taggedAt)
1996
+ return false;
1997
+ const ctx = _getRequestContext();
1998
+ const memo = ctx ? getTagMarkerMemo(ctx, this) : undefined;
1999
+ const inflight = ctx ? getTagMarkerInflight(ctx, this) : undefined;
2000
+ try {
2001
+ const markers = await Promise.all(
2002
+ tags.map((tag) => this.readTagMarker(tag, memo, inflight)),
2003
+ );
2004
+ for (const marker of markers) {
2005
+ if (marker != null && marker >= taggedAt) return true;
2006
+ }
2007
+ return false;
2008
+ } catch (error) {
2009
+ reportCacheError(
2010
+ error,
2011
+ "cache-read",
2012
+ "[CFCacheStore] tag invalidation check",
2013
+ );
2014
+ return false;
2015
+ }
2016
+ }
2017
+
2018
+ /** Synthetic Cache API request for a tag's L1-cached invalidation marker. */
2019
+ private tagMarkerRequest(tag: string): Request {
2020
+ return this.keyToRequest(`${TAG_MARKER_CACHE_PREFIX}${tag}`);
2021
+ }
2022
+
2023
+ /**
2024
+ * Read a tag's latest invalidation timestamp (or null if never invalidated)
2025
+ * through the cascade: per-request memo -> per-colo L1 cache (only when
2026
+ * tagCacheTtl > 0) -> KV (the global truth). The memo is always consulted
2027
+ * first so it stays authoritative within a request (read-your-own-writes),
2028
+ * and every KV/L1 result is written back into the memo. A Cache API miss
2029
+ * always falls through to KV; absence is represented by a cached sentinel,
2030
+ * never by a miss.
2031
+ *
2032
+ * Concurrent reads of the same tag within a request share one in-flight read
2033
+ * (the resolved-value memo only collapses sequential reads; parallel segment
2034
+ * loading would otherwise issue one KV read per concurrent reader).
2035
+ * @internal
2036
+ */
2037
+ private async readTagMarker(
2038
+ tag: string,
2039
+ memo: Map<string, number | null> | undefined,
2040
+ inflight: Map<string, Promise<number | null>> | undefined,
2041
+ ): Promise<number | null> {
2042
+ if (memo && memo.has(tag)) return memo.get(tag) ?? null;
2043
+
2044
+ // Collapse concurrent (not-yet-resolved) reads of this tag onto one promise.
2045
+ if (inflight) {
2046
+ const pending = inflight.get(tag);
2047
+ if (pending) return pending;
2048
+ const read = this.fetchTagMarker(tag, memo);
2049
+ inflight.set(tag, read);
2050
+ try {
2051
+ return await read;
2052
+ } finally {
2053
+ // Resolved values now live in the memo; drop the in-flight entry.
2054
+ inflight.delete(tag);
2055
+ }
2056
+ }
2057
+
2058
+ return this.fetchTagMarker(tag, memo);
2059
+ }
2060
+
2061
+ /**
2062
+ * Uncached body of readTagMarker: L1 (per-colo Cache API, opt-in via
2063
+ * tagCacheTtl) -> KV. Writes the resolved value back into the memo.
2064
+ * @internal
2065
+ */
2066
+ private async fetchTagMarker(
2067
+ tag: string,
2068
+ memo: Map<string, number | null> | undefined,
2069
+ ): Promise<number | null> {
2070
+ // Write the resolved marker into the memo WITHOUT clobbering a value a
2071
+ // concurrent invalidateTags() wrote during our await. The router resolves
2072
+ // sibling slots in parallel, so a slot's updateTag() can land the
2073
+ // authoritative invalidatedAt into the memo while this read is still in
2074
+ // flight; overwriting it with our (pre-invalidation) read result would break
2075
+ // read-your-own-writes for the rest of the request. If the tag was memoized
2076
+ // mid-read, that value wins and is returned. Without a memo, the read result
2077
+ // stands as-is.
2078
+ const memoize = (read: number | null): number | null => {
2079
+ if (memo && memo.has(tag)) return memo.get(tag) ?? null;
2080
+ memo?.set(tag, read);
2081
+ return read;
2082
+ };
2083
+
2084
+ // L1 (per-colo) marker cache - opt-in via tagCacheTtl. Bounded by the same
2085
+ // edge budgets as data reads (inherited from #558) so a degraded colo cannot
2086
+ // stall a tagged read; a miss, timeout, or error all fall through to KV.
2087
+ if (this.tagCacheTtl > 0) {
2088
+ try {
2089
+ const cache = await this.getCache();
2090
+ const { response: hit, error: matchError } =
2091
+ await this.matchWithTimeout(cache, this.tagMarkerRequest(tag));
2092
+ // A transient match REJECTION is captured (not thrown) by
2093
+ // matchWithTimeout; surface it as cache-read like the data read paths
2094
+ // before falling through to KV, rather than silently dropping it.
2095
+ if (matchError)
2096
+ reportCacheError(
2097
+ matchError,
2098
+ "cache-read",
2099
+ "[CFCacheStore] tag marker L1 match",
2100
+ );
2101
+ if (hit) {
2102
+ const { value: body } = await this.readWithTimeout(
2103
+ () => hit.text(),
2104
+ this.edgeReadTimeoutMs,
2105
+ "tag marker L1 body read",
2106
+ );
2107
+ if (body !== undefined) {
2108
+ const value = body === TAG_MARKER_ABSENT ? null : Number(body);
2109
+ return memoize(value);
2110
+ }
2111
+ }
2112
+ } catch {
2113
+ // Fall through to KV on any L1 read error.
2114
+ }
2115
+ }
2116
+
2117
+ // KV (global truth), bounded by the KV budget. On TIMEOUT fail OPEN: treat
2118
+ // the marker as absent (-> entry not invalidated -> served) so a degraded
2119
+ // namespace cannot pin every tagged read behind a slow global lookup. A
2120
+ // transient REJECTION instead propagates to isGloballyInvalidated's catch
2121
+ // (reported cache-read), which also fails open. Either way one slow tag
2122
+ // never amplifies into a per-segment stall.
2123
+ const { value: raw, timedOut } = await this.readWithTimeout<string | null>(
2124
+ () => this.kv!.get(this.tagMarkerKey(tag), { type: "text" }),
2125
+ this.kvReadTimeoutMs,
2126
+ "tag marker KV read",
2127
+ );
2128
+ if (timedOut) {
2129
+ // Memoize the fail-open result so the rest of this request is consistent
2130
+ // (and does not re-pay the timeout per segment sharing the tag).
2131
+ return memoize(null);
2132
+ }
2133
+ const value = raw != null ? Number(raw) : null;
2134
+ const resolved = memoize(value);
2135
+
2136
+ // Populate L1 for subsequent reads in this colo (non-blocking). Use the
2137
+ // resolved (memo-aware) value so a marker invalidated mid-read is not
2138
+ // re-cached stale into this colo's L1.
2139
+ if (this.tagCacheTtl > 0) {
2140
+ const put = () => this.putTagMarkerL1(tag, resolved);
2141
+ if (this.waitUntil) this.waitUntil(put);
2142
+ else void put();
2143
+ }
2144
+ return resolved;
2145
+ }
2146
+
2147
+ /**
2148
+ * Cloudflare Cache-Tags written on a tag's L1 marker entry, namespaced per
2149
+ * store so purges never collide with other Cache-Tags in the zone. Three
2150
+ * tiers, broad to specific:
2151
+ * rg:{ns} - everything this store cached (deploy/nuclear reset)
2152
+ * rg:{ns}:lk - all tag-lookup markers
2153
+ * rg:{ns}:lk:{tag} - this tag's lookup (the normal updateTag purge target)
2154
+ * The tag value is encodeURIComponent'd so commas/spaces can't corrupt the
2155
+ * comma-delimited Cache-Tag header.
2156
+ * @internal
2157
+ */
2158
+ private lookupCacheTags(tag: string): string[] {
2159
+ const ns = this.namespace ?? "default";
2160
+ return [`rg:${ns}`, `rg:${ns}:lk`, this.lookupPurgeTag(tag)];
2161
+ }
2162
+
2163
+ /** The specific Cache-Tag a consumer purges to evict tag `tag`'s lookup. */
2164
+ private lookupPurgeTag(tag: string): string {
2165
+ const ns = this.namespace ?? "default";
2166
+ return `rg:${ns}:lk:${encodeURIComponent(tag)}`;
2167
+ }
2168
+
2169
+ /**
2170
+ * Write a tag marker value into the per-colo L1 Cache API with tagCacheTtl.
2171
+ * `null` is stored as the TAG_MARKER_ABSENT sentinel so "no marker yet" is
2172
+ * cacheable (most tags are never invalidated - that is where the read savings
2173
+ * come from). The entry also carries a namespaced Cache-Tag so an external
2174
+ * purge-by-tag (via onRevalidateTag) can evict it across colos promptly,
2175
+ * rather than waiting out tagCacheTtl. Best-effort.
2176
+ * @internal
2177
+ */
2178
+ private async putTagMarkerL1(
2179
+ tag: string,
2180
+ value: number | null,
2181
+ opts?: { critical?: boolean },
2182
+ ): Promise<void> {
2183
+ if (this.tagCacheTtl <= 0) return;
2184
+ try {
2185
+ const cache = await this.getCache();
2186
+ const body = value != null ? String(value) : TAG_MARKER_ABSENT;
2187
+ await cache.put(
2188
+ this.tagMarkerRequest(tag),
2189
+ new Response(body, {
2190
+ headers: {
2191
+ "Cache-Control": `public, max-age=${this.tagCacheTtl}`,
2192
+ "Cache-Tag": this.lookupCacheTags(tag).join(","),
2193
+ },
2194
+ }),
2195
+ );
2196
+ } catch (error) {
2197
+ // The read-path populate is best-effort: a failed populate just means the
2198
+ // next read consults KV. The invalidation WRITE-THROUGH (critical) is not
2199
+ // - silently swallowing it would leave this colo's stale marker (often the
2200
+ // ABSENT sentinel) authoritative for tagCacheTtl while updateTag reports
2201
+ // success. Surface it, and best-effort delete the L1 marker so the next
2202
+ // read re-reads KV, which already holds the fresh marker (written before
2203
+ // this write-through in invalidateTags).
2204
+ if (opts?.critical) {
2205
+ reportCacheError(
2206
+ error,
2207
+ "cache-invalidate",
2208
+ "[CFCacheStore] tag marker L1 write-through",
2209
+ );
2210
+ await reportingAsync(
2211
+ async () => {
2212
+ const cache = await this.getCache();
2213
+ await cache.delete(this.tagMarkerRequest(tag));
2214
+ },
2215
+ "cache-delete",
2216
+ "[CFCacheStore] tag marker L1 evict after failed write-through",
2217
+ );
2218
+ }
2219
+ }
2220
+ }
2221
+
2222
+ /**
2223
+ * Invalidate every entry tagged with any of `tags`. Receives the whole batch
2224
+ * from one updateTag()/revalidateTag() call so the eager-purge hook fires
2225
+ * ONCE (one CDN purge request, not one per tag). For each tag: records the KV
2226
+ * marker (the durable cross-colo truth that reads compare taggedAt against),
2227
+ * writes the fresh marker straight into this colo's L1 (write-through, NOT
2228
+ * delete - a delete would let the next read re-read a not-yet-converged KV
2229
+ * value and re-arm the stale window), and memoizes it for same-request
2230
+ * read-your-own-writes. Finally fires onRevalidateTag with the namespaced
2231
+ * lookup Cache-Tags so a consumer purge evicts the cached lookups in other
2232
+ * colos promptly (otherwise they converge within tagCacheTtl).
2233
+ *
2234
+ * Durable-write integrity: the in-memory write-through (memo + L1) for a tag
2235
+ * runs ONLY after that tag's KV marker write is confirmed. If any KV write
2236
+ * fails (transient error, or an over-512-byte key), this rejects with the
2237
+ * failed tags so an awaiting updateTag() surfaces the failure instead of
2238
+ * silently reporting success while other requests/colos serve stale data. The
2239
+ * eager purge still fires for the whole batch first (it is additive).
2240
+ */
2241
+ async invalidateTags(tags: string[]): Promise<void> {
2242
+ if (tags.length === 0) return;
2243
+ const invalidatedAt = Date.now();
2244
+ const ctx = _getRequestContext();
2245
+ const memo = ctx ? getTagMarkerMemo(ctx, this) : undefined;
2246
+
2247
+ if (!this.kv && !this.onRevalidateTag) {
2248
+ console.warn(
2249
+ `[CFCacheStore] invalidateTags had no effect: configure a KV namespace ` +
2250
+ `for distributed invalidation, or an onRevalidateTag hook.`,
2251
+ );
2252
+ }
2253
+
2254
+ const failedTags = new Set<string>();
2255
+ const errors: unknown[] = [];
2256
+ if (this.kv) {
2257
+ await Promise.all(
2258
+ tags.map(async (tag) => {
2259
+ const markerKey = this.tagMarkerKey(tag);
2260
+ const markerKeyBytes = kvKeyByteLength(markerKey);
2261
+ if (markerKeyBytes > KV_MAX_KEY_BYTES) {
2262
+ failedTags.add(tag);
2263
+ errors.push(
2264
+ new Error(
2265
+ `tag "${tag}" produces a ${markerKeyBytes}-byte KV ` +
2266
+ `marker key, over the ${KV_MAX_KEY_BYTES}-byte limit`,
2267
+ ),
2268
+ );
2269
+ return;
2270
+ }
2271
+ try {
2272
+ await this.kv!.put(markerKey, String(invalidatedAt), {
2273
+ ...(this.tagInvalidationTtl
2274
+ ? { expirationTtl: this.tagInvalidationTtl }
2275
+ : {}),
2276
+ });
2277
+ } catch (error) {
2278
+ failedTags.add(tag);
2279
+ errors.push(error);
2280
+ }
2281
+ }),
2282
+ );
2283
+ }
2284
+
2285
+ // Write-through memo + L1 only for tags with a confirmed durable marker, and
2286
+ // only when KV is configured. Markers are read exclusively through
2287
+ // isGloballyInvalidated(), which short-circuits to "not invalidated" when
2288
+ // !this.kv; writing memo/L1 markers without KV would be dead state no read
2289
+ // path ever consults. The onRevalidateTag purge below still fires regardless
2290
+ // (it is additive and external to the marker cascade). The memo write is
2291
+ // synchronous (read-your-own-writes); the L1 Cache API writes are
2292
+ // independent, so fan them out in parallel rather than awaiting each.
2293
+ if (this.kv) {
2294
+ const l1Writes: Promise<void>[] = [];
2295
+ for (const tag of tags) {
2296
+ if (failedTags.has(tag)) continue;
2297
+ memo?.set(tag, invalidatedAt);
2298
+ if (this.tagCacheTtl > 0) {
2299
+ l1Writes.push(
2300
+ this.putTagMarkerL1(tag, invalidatedAt, { critical: true }),
2301
+ );
2302
+ }
2303
+ }
2304
+ if (l1Writes.length > 0) await Promise.all(l1Writes);
2305
+ }
2306
+
2307
+ // One batched eager purge of the lookup markers for the whole call. Fired
2308
+ // regardless of KV write outcome (it is additive and uses pure string ops).
2309
+ if (this.onRevalidateTag) {
2310
+ try {
2311
+ await this.onRevalidateTag(tags.map((tag) => this.lookupPurgeTag(tag)));
2312
+ } catch (error) {
2313
+ reportCacheError(
2314
+ error,
2315
+ "cache-invalidate",
2316
+ "[CFCacheStore] onRevalidateTag hook",
2317
+ );
2318
+ }
2319
+ }
2320
+
2321
+ if (failedTags.size > 0) {
2322
+ const err = new Error(
2323
+ `[CFCacheStore] ${failedTags.size}/${tags.length} tag marker write(s) ` +
2324
+ `failed: ${[...failedTags].join(", ")}. Those tags may still serve ` +
2325
+ `stale data across requests/colos; retry the invalidation.`,
2326
+ );
2327
+ (err as Error & { cause?: unknown }).cause = errors[0];
2328
+ throw err;
2329
+ }
2330
+ }
2331
+
2332
+ // ============================================================================
2333
+ // KV L2 Helpers
2334
+ // ============================================================================
2335
+
2336
+ /**
2337
+ * KV fallback for segment cache reads.
2338
+ * Returns null if KV is not configured, entry is missing, or expired.
2339
+ * Promotes hits to L1 via waitUntil.
2340
+ * @internal
2341
+ */
2342
+ private async kvGetSegment(
2343
+ key: string,
2344
+ opts?: { suppressRevalidate?: boolean },
2345
+ ): Promise<CacheGetResult | null> {
2346
+ if (!this.kv) return null;
2347
+
2348
+ try {
2349
+ const kvKey = this.toKVKey(key);
2350
+ const { value: envelope, timedOut } =
2351
+ await this.kvGetOrEvict<KVSegmentEnvelope>(
2352
+ kvKey,
2353
+ (e) =>
2354
+ typeof e.e === "number" && typeof e.s === "number" && e.d != null,
2355
+ "kvGetSegment",
2356
+ );
2357
+ if (timedOut) {
2358
+ // Abandoned slow KV read: no envelope, so no promote-to-L1. Distinct
2359
+ // from a genuine kv-miss so the degradation is visible on wrangler tail.
2360
+ if (this.debug)
2361
+ this.emitDebug({ op: "get", key, outcome: "kv-timeout" });
2362
+ return null;
2363
+ }
2364
+ if (!envelope) {
2365
+ // Missing key, or a corrupt entry already evicted + reported by
2366
+ // kvGetOrEvict. Either way a miss.
2367
+ if (this.debug) this.emitDebug({ op: "get", key, outcome: "kv-miss" });
2368
+ return null;
2369
+ }
2370
+
2371
+ const now = Date.now();
2372
+
2373
+ // Hard-expired — treat as miss
2374
+ if (now > envelope.e) {
2375
+ if (this.debug) this.emitDebug({ op: "get", key, outcome: "kv-miss" });
2376
+ return null;
2377
+ }
2378
+
2379
+ // Tag invalidation check (also covers the KV tier, not just L1).
2380
+ if (
2381
+ await this.isGloballyInvalidated(envelope.d.tags, envelope.d.taggedAt)
2382
+ ) {
2383
+ if (this.debug)
2384
+ this.emitDebug({ op: "get", key, outcome: "tag-invalidated" });
2385
+ return null;
2386
+ }
2387
+
2388
+ // When this is a degraded L1 fall-through (body-timeout / non-200), the
2389
+ // caller asks us to suppress revalidation: KV has no REVALIDATING herd
2390
+ // guard, so N concurrent degraded reads would otherwise each spawn a
2391
+ // render exactly when the colo is already struggling. We still serve the
2392
+ // stale data and still promote to L1; only the revalidation is withheld.
2393
+ const stale = now > envelope.s;
2394
+ const shouldRevalidate = stale && !opts?.suppressRevalidate;
2395
+
2396
+ // Promote to L1 in background
2397
+ this.promoteSegmentToL1(key, envelope);
2398
+
2399
+ if (this.debug)
2400
+ this.emitDebug({
2401
+ op: "get",
2402
+ key,
2403
+ outcome: !stale
2404
+ ? "kv-fresh"
2405
+ : opts?.suppressRevalidate
2406
+ ? "kv-stale-suppressed"
2407
+ : "kv-stale",
2408
+ shouldRevalidate,
2409
+ });
2410
+ return { data: envelope.d, shouldRevalidate };
2411
+ } catch (error) {
2412
+ reportCacheError(error, "cache-read", "[CFCacheStore] kvGetSegment");
2413
+ if (this.debug) this.emitDebug({ op: "get", key, outcome: "error" });
2414
+ return null;
2415
+ }
2416
+ }
2417
+
2418
+ /**
2419
+ * Write segment data to KV.
2420
+ * @internal
2421
+ */
2422
+ private kvSetSegment(
2423
+ key: string,
2424
+ data: CachedEntryData,
2425
+ staleAt: number,
2426
+ totalTtl: number,
2427
+ swrWindow: number,
2428
+ ): void {
2429
+ // KV requires expirationTtl >= 60s. Skip write for short-lived entries.
2430
+ if (!this.kv || !this.waitUntil || totalTtl < 60) return;
2431
+
2432
+ const kvKey = this.toKVKey(key);
2433
+
2434
+ // Reject an oversized data-segment KV key the same way tag-marker keys are
2435
+ // rejected in invalidateTags(). A key over KV_MAX_KEY_BYTES makes kv.put()
2436
+ // fail, so the segment silently never lands in L2 (KV) and every cold-colo
2437
+ // or TTL-expired read re-renders instead of serving stale. Segment keys can
2438
+ // grow with user-controlled inputs (e.g. a route's search params), so report
2439
+ // a clear, actionable error and skip the doomed write rather than letting it
2440
+ // reject deep inside waitUntil as an opaque cache-write failure.
2441
+ const kvKeyBytes = kvKeyByteLength(kvKey);
2442
+ if (kvKeyBytes > KV_MAX_KEY_BYTES) {
2443
+ reportCacheError(
2444
+ new Error(
2445
+ `cache segment key produces a ${kvKeyBytes}-byte KV key, over the ` +
2446
+ `${KV_MAX_KEY_BYTES}-byte limit; the segment was not persisted to KV (L2). ` +
2447
+ `Reduce the cache-key inputs (e.g. large search params on this route).`,
2448
+ ),
2449
+ "cache-write",
2450
+ "[CFCacheStore] kvSetSegment",
2451
+ );
2452
+ return;
2453
+ }
2454
+
2455
+ const expiresAt = staleAt + swrWindow * 1000;
2456
+
2457
+ this.waitUntil(() =>
2458
+ reportingAsync(
2459
+ () => {
2460
+ const envelope: KVSegmentEnvelope = {
2461
+ d: data,
2462
+ s: staleAt,
2463
+ e: expiresAt,
2464
+ };
2465
+ return this.kv!.put(kvKey, JSON.stringify(envelope), {
2466
+ expirationTtl: totalTtl,
2467
+ });
2468
+ },
2469
+ "cache-write",
2470
+ "[CFCacheStore] kvSetSegment",
2471
+ ),
2472
+ );
2473
+ }
2474
+
2475
+ /**
2476
+ * Promote segment data from KV to L1 Cache API.
2477
+ * @internal
2478
+ */
2479
+ private promoteSegmentToL1(key: string, envelope: KVSegmentEnvelope): void {
2480
+ if (!this.waitUntil) return;
2481
+
2482
+ this.waitUntil(() =>
2483
+ reportingAsync(
2484
+ async () => {
2485
+ const now = Date.now();
2486
+ const remainingTtl = Math.max(
2487
+ 1,
2488
+ Math.floor((envelope.e - now) / 1000),
2489
+ );
2490
+ const cache = await this.getCache();
2491
+ const request = this.keyToRequest(key);
2492
+
2493
+ const response = new Response(JSON.stringify(envelope.d), {
2494
+ headers: {
2495
+ "Content-Type": "application/json",
2496
+ "Cache-Control": `public, max-age=${remainingTtl}`,
2497
+ [CACHE_STALE_AT_HEADER]: String(envelope.s),
2498
+ // Carry the hard-expiry deadline so a promoted entry that later
2499
+ // goes stale re-puts with the correct remaining ttl (see set()).
2500
+ [CACHE_EXPIRES_AT_HEADER]: String(envelope.e),
2501
+ [CACHE_STATUS_HEADER]: "HIT",
2502
+ // Preserve tags across KV->L1 promotion so the promoted entry
2503
+ // stays tag-invalidatable.
2504
+ ...this.tagHeaderEntries(envelope.d.tags, envelope.d.taggedAt),
2505
+ },
2506
+ });
2507
+
2508
+ await cache.put(request, response);
2509
+ },
2510
+ "cache-write",
2511
+ "[CFCacheStore] promoteSegmentToL1",
2512
+ ),
2513
+ );
2514
+ }
2515
+
2516
+ /**
2517
+ * KV fallback for function cache reads.
2518
+ * @internal
2519
+ */
2520
+ private async kvGetItem(
2521
+ key: string,
2522
+ opts?: { suppressRevalidate?: boolean },
2523
+ ): Promise<CacheItemResult | null> {
2524
+ if (!this.kv) return null;
2525
+
2526
+ try {
2527
+ const kvKey = this.toKVKey(`fn:${key}`);
2528
+ const { value: envelope, timedOut } =
2529
+ await this.kvGetOrEvict<KVItemEnvelope>(
2530
+ kvKey,
2531
+ (e) =>
2532
+ typeof e.v === "string" &&
2533
+ typeof e.e === "number" &&
2534
+ typeof e.s === "number",
2535
+ "kvGetItem",
2536
+ );
2537
+ if (timedOut) {
2538
+ if (this.debug)
2539
+ this.emitDebug({ op: "getItem", key, outcome: "kv-timeout" });
2540
+ return null;
2541
+ }
2542
+ if (!envelope) {
2543
+ if (this.debug)
2544
+ this.emitDebug({ op: "getItem", key, outcome: "kv-miss" });
2545
+ return null;
2546
+ }
2547
+
2548
+ const now = Date.now();
2549
+
2550
+ if (now > envelope.e) {
2551
+ if (this.debug)
2552
+ this.emitDebug({ op: "getItem", key, outcome: "kv-miss" });
2553
+ return null;
2554
+ }
2555
+
2556
+ // Tag invalidation check (also covers the KV tier, not just L1).
2557
+ if (await this.isGloballyInvalidated(envelope.t, envelope.ta)) {
2558
+ if (this.debug)
2559
+ this.emitDebug({ op: "getItem", key, outcome: "tag-invalidated" });
2560
+ return null;
2561
+ }
2562
+
2563
+ // Degraded fall-through suppresses revalidation (no KV herd guard); see
2564
+ // kvGetSegment. Still serves stale and still promotes.
2565
+ const stale = now > envelope.s;
2566
+ const shouldRevalidate = stale && !opts?.suppressRevalidate;
2567
+
2568
+ // Promote to L1
2569
+ this.promoteItemToL1(key, envelope);
2570
+
2571
+ if (this.debug)
2572
+ this.emitDebug({
2573
+ op: "getItem",
2574
+ key,
2575
+ outcome: !stale
2576
+ ? "kv-fresh"
2577
+ : opts?.suppressRevalidate
2578
+ ? "kv-stale-suppressed"
2579
+ : "kv-stale",
2580
+ shouldRevalidate,
2581
+ });
2582
+ return {
2583
+ value: envelope.v,
2584
+ handles: envelope.h,
2585
+ shouldRevalidate,
2586
+ tags: envelope.t,
2587
+ };
2588
+ } catch (error) {
2589
+ reportCacheError(error, "cache-read", "[CFCacheStore] kvGetItem");
2590
+ if (this.debug) this.emitDebug({ op: "getItem", key, outcome: "error" });
2591
+ return null;
2592
+ }
2593
+ }
2594
+
2595
+ /**
2596
+ * Promote function cache data from KV to L1.
2597
+ * @internal
2598
+ */
2599
+ private promoteItemToL1(key: string, envelope: KVItemEnvelope): void {
2600
+ if (!this.waitUntil) return;
2601
+
2602
+ this.waitUntil(() =>
2603
+ reportingAsync(
2604
+ async () => {
2605
+ const now = Date.now();
2606
+ const remainingTtl = Math.max(
2607
+ 1,
2608
+ Math.floor((envelope.e - now) / 1000),
2609
+ );
2610
+ const cache = await this.getCache();
2611
+ const request = this.keyToRequest(`fn:${key}`);
2612
+
2613
+ const body = JSON.stringify({
2614
+ value: envelope.v,
2615
+ handles: envelope.h,
2616
+ });
2617
+ const response = new Response(body, {
2618
+ headers: {
2619
+ "Content-Type": "application/json",
2620
+ "Cache-Control": `public, max-age=${remainingTtl}`,
2621
+ [CACHE_STALE_AT_HEADER]: String(envelope.s),
2622
+ // Carry the hard-expiry deadline; see promoteSegmentToL1 / set().
2623
+ [CACHE_EXPIRES_AT_HEADER]: String(envelope.e),
2624
+ [CACHE_STATUS_HEADER]: "HIT",
2625
+ // Preserve tags across KV->L1 promotion (the item tier previously
2626
+ // dropped them, permanently disabling tag invalidation here).
2627
+ ...this.tagHeaderEntries(envelope.t, envelope.ta),
2628
+ },
2629
+ });
2630
+
2631
+ await cache.put(request, response);
2632
+ },
2633
+ "cache-write",
2634
+ "[CFCacheStore] promoteItemToL1",
2635
+ ),
2636
+ );
2637
+ }
2638
+
2639
+ /**
2640
+ * KV fallback for document cache reads.
2641
+ * @internal
2642
+ */
2643
+ private async kvGetResponse(
2644
+ key: string,
2645
+ ): Promise<{ response: Response; shouldRevalidate: boolean } | null> {
2646
+ if (!this.kv) return null;
2647
+
2648
+ try {
2649
+ const kvKey = this.toDocKVKey(key);
2650
+ // The document path is debug-silent (op is only get/getItem): a KV-read
2651
+ // timeout here is bounded for resilience parity (kvGetOrEvict applies the
2652
+ // budget) but emits no kv-timeout event, so its absence from the debug
2653
+ // stream is expected. A null envelope is a miss -- missing key, a budget
2654
+ // timeout, or a corrupt entry already evicted + reported by kvGetOrEvict.
2655
+ const { value: envelope } = await this.kvGetOrEvict<KVResponseEnvelope>(
2656
+ kvKey,
2657
+ (e) =>
2658
+ typeof e.b === "string" &&
2659
+ typeof e.st === "number" &&
2660
+ typeof e.e === "number" &&
2661
+ typeof e.s === "number" &&
2662
+ // stx is optional but, if present, must be a string (feeds Response).
2663
+ (e.stx === undefined || typeof e.stx === "string") &&
2664
+ // hd must be an array of [name, value] string tuples; a malformed
2665
+ // shape would otherwise throw in `new Headers(hd)`. Validate it here
2666
+ // so a faulty envelope is a fail-open MISS, never a thrown read.
2667
+ Array.isArray(e.hd) &&
2668
+ e.hd.every(
2669
+ (entry) =>
2670
+ Array.isArray(entry) &&
2671
+ entry.length === 2 &&
2672
+ typeof entry[0] === "string" &&
2673
+ typeof entry[1] === "string",
2674
+ ),
2675
+ "kvGetResponse",
2676
+ );
2677
+ if (!envelope) return null;
2678
+
2679
+ const now = Date.now();
2680
+
2681
+ if (now > envelope.e) return null;
2682
+
2683
+ // Tag invalidation check (also covers the KV tier, not just L1).
2684
+ if (await this.isGloballyInvalidated(envelope.t, envelope.ta)) {
2685
+ return null;
2686
+ }
2687
+
2688
+ const shouldRevalidate = now > envelope.s;
2689
+
2690
+ // Reconstruct Response: decode base64 -> binary, rebuild headers/status.
2691
+ // Corrupt/partial base64 throws in atob; malformed `hd` or an out-of-range
2692
+ // `st` throws in new Headers/new Response. Any of these is a faulty entry,
2693
+ // so evict it and miss rather than re-failing every read until TTL.
2694
+ let response: Response;
2695
+ try {
2696
+ // Finding #3 (read side): strip per-client signals a stale envelope may
2697
+ // carry. Inside the try so a malformed `hd` evicts (not throws through);
2698
+ // mutates `hd` in place so promoteResponseToL1 re-seeds from it too.
2699
+ envelope.hd = envelope.hd.filter(
2700
+ ([name]) => !isPerClientSignalHeader(name),
2701
+ );
2702
+ const bodyBuffer = base64ToBuffer(envelope.b);
2703
+ const headers = new Headers(envelope.hd);
2704
+ response = new Response(bodyBuffer, {
2705
+ status: envelope.st,
2706
+ statusText: envelope.stx,
2707
+ headers,
2708
+ });
2709
+ } catch (error) {
2710
+ reportCacheError(
2711
+ error,
2712
+ "cache-corrupt",
2713
+ "[CFCacheStore] kvGetResponse: corrupt response envelope, evicting",
2714
+ );
2715
+ this.scheduleKvEvict(kvKey, "kvGetResponse");
2716
+ return null;
2717
+ }
2718
+
2719
+ // Promote to L1
2720
+ this.promoteResponseToL1(key, envelope);
2721
+
2722
+ return { response, shouldRevalidate };
2723
+ } catch (error) {
2724
+ reportCacheError(error, "cache-read", "[CFCacheStore] kvGetResponse");
2725
+ return null;
2726
+ }
2727
+ }
2728
+
2729
+ /**
2730
+ * Promote document cache data from KV to L1.
2731
+ * @internal
2732
+ */
2733
+ private promoteResponseToL1(key: string, envelope: KVResponseEnvelope): void {
2734
+ if (!this.waitUntil) return;
2735
+
2736
+ this.waitUntil(() =>
2737
+ reportingAsync(
2738
+ async () => {
2739
+ const now = Date.now();
2740
+ const remainingTtl = Math.max(
2741
+ 1,
2742
+ Math.floor((envelope.e - now) / 1000),
2743
+ );
2744
+ const cache = await this.getCache();
2745
+ const request = this.keyToRequest(`doc:${key}`);
2746
+
2747
+ const headers = new Headers(envelope.hd);
2748
+ const originalCacheControl = headers.get("Cache-Control");
2749
+ if (originalCacheControl !== null) {
2750
+ headers.set(CACHE_ORIG_CC_HEADER, originalCacheControl);
2751
+ }
2752
+ headers.set("Cache-Control", `public, max-age=${remainingTtl}`);
2753
+ headers.set(CACHE_STALE_AT_HEADER, String(envelope.s));
2754
+ // Carry the hard-expiry deadline so the document herd guard's
2755
+ // markResponseRevalidating re-put can compute the remaining window
2756
+ // (matches promoteSegmentToL1/promoteItemToL1); without it a stale
2757
+ // re-put would floor to max-age=1 and churn the KV-promoted twin.
2758
+ headers.set(CACHE_EXPIRES_AT_HEADER, String(envelope.e));
2759
+ // Re-attach the internal tag headers (envelope.hd is client-facing
2760
+ // and intentionally excludes them) so the promoted entry stays
2761
+ // invalidatable.
2762
+ this.setTagHeaders(headers, envelope.t, envelope.ta);
2763
+
2764
+ const bodyBuffer = base64ToBuffer(envelope.b);
2765
+ const response = new Response(bodyBuffer, {
2766
+ status: envelope.st,
2767
+ statusText: envelope.stx,
2768
+ headers,
2769
+ });
2770
+
2771
+ await cache.put(request, response);
2772
+ },
2773
+ "cache-write",
2774
+ "[CFCacheStore] promoteResponseToL1",
2775
+ ),
2776
+ );
427
2777
  }
428
2778
  }