@rangojs/router 0.0.0-experimental.ea6d5eec → 0.0.0-experimental.ea9f40f2

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