@timber-js/app 0.2.0-alpha.98 → 0.2.0-alpha.99

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 (322) hide show
  1. package/LICENSE +8 -0
  2. package/dist/_chunks/actions-CQ8Z8VGL.js +1061 -0
  3. package/dist/_chunks/actions-CQ8Z8VGL.js.map +1 -0
  4. package/dist/_chunks/build-output-helper-DXnW0qjz.js +61 -0
  5. package/dist/_chunks/build-output-helper-DXnW0qjz.js.map +1 -0
  6. package/dist/_chunks/{define-Itxvcd7F.js → define-B-Q_UMOD.js} +19 -23
  7. package/dist/_chunks/define-B-Q_UMOD.js.map +1 -0
  8. package/dist/_chunks/{define-C77ScO0m.js → define-CfBPoJb0.js} +24 -7
  9. package/dist/_chunks/define-CfBPoJb0.js.map +1 -0
  10. package/dist/_chunks/define-cookie-BjpIt4UC.js +194 -0
  11. package/dist/_chunks/define-cookie-BjpIt4UC.js.map +1 -0
  12. package/dist/_chunks/{format-CYBGxKtc.js → format-Bcn-Iv1x.js} +1 -1
  13. package/dist/_chunks/{format-CYBGxKtc.js.map → format-Bcn-Iv1x.js.map} +1 -1
  14. package/dist/_chunks/handler-store-B-lqaGyh.js +54 -0
  15. package/dist/_chunks/handler-store-B-lqaGyh.js.map +1 -0
  16. package/dist/_chunks/logger-0m8MsKdc.js +291 -0
  17. package/dist/_chunks/logger-0m8MsKdc.js.map +1 -0
  18. package/dist/_chunks/merge-search-params-BphMdht_.js +122 -0
  19. package/dist/_chunks/merge-search-params-BphMdht_.js.map +1 -0
  20. package/dist/_chunks/navigation-root-BCYczjml.js +96 -0
  21. package/dist/_chunks/navigation-root-BCYczjml.js.map +1 -0
  22. package/dist/_chunks/registry-I2ss-lvy.js +20 -0
  23. package/dist/_chunks/registry-I2ss-lvy.js.map +1 -0
  24. package/dist/_chunks/router-ref-h3-UaCQv.js +28 -0
  25. package/dist/_chunks/router-ref-h3-UaCQv.js.map +1 -0
  26. package/dist/_chunks/{schema-bridge-C3xl_vfb.js → schema-bridge-Cxu4l-7p.js} +1 -1
  27. package/dist/_chunks/{schema-bridge-C3xl_vfb.js.map → schema-bridge-Cxu4l-7p.js.map} +1 -1
  28. package/dist/_chunks/{segment-context-fHFLF1PE.js → segment-context-Dx_OizxD.js} +1 -1
  29. package/dist/_chunks/{segment-context-fHFLF1PE.js.map → segment-context-Dx_OizxD.js.map} +1 -1
  30. package/dist/_chunks/{router-ref-C8OCm7g7.js → ssr-data-B4CdH7rE.js} +2 -26
  31. package/dist/_chunks/ssr-data-B4CdH7rE.js.map +1 -0
  32. package/dist/_chunks/{stale-reload-BX5gL1r-.js → stale-reload-Bab885FO.js} +1 -1
  33. package/dist/_chunks/{stale-reload-BX5gL1r-.js.map → stale-reload-Bab885FO.js.map} +1 -1
  34. package/dist/_chunks/tracing-C8V-YGsP.js +329 -0
  35. package/dist/_chunks/tracing-C8V-YGsP.js.map +1 -0
  36. package/dist/_chunks/{use-query-states-BiV5GJgm.js → use-query-states-B2XTqxDR.js} +3 -19
  37. package/dist/_chunks/use-query-states-B2XTqxDR.js.map +1 -0
  38. package/dist/_chunks/{use-params-IOPu7E8t.js → use-segment-params-BkpKAQ7D.js} +9 -95
  39. package/dist/_chunks/use-segment-params-BkpKAQ7D.js.map +1 -0
  40. package/dist/_chunks/{walkers-VOXgavMF.js → walkers-Tg0Alwcg.js} +6 -3
  41. package/dist/_chunks/walkers-Tg0Alwcg.js.map +1 -0
  42. package/dist/_chunks/{dev-warnings-DpGRGoDi.js → warnings-Cg47l5sk.js} +3 -3
  43. package/dist/_chunks/warnings-Cg47l5sk.js.map +1 -0
  44. package/dist/adapters/build-output-helper.d.ts +28 -0
  45. package/dist/adapters/build-output-helper.d.ts.map +1 -0
  46. package/dist/adapters/cloudflare.d.ts.map +1 -1
  47. package/dist/adapters/cloudflare.js +8 -28
  48. package/dist/adapters/cloudflare.js.map +1 -1
  49. package/dist/adapters/nitro.d.ts.map +1 -1
  50. package/dist/adapters/nitro.js +8 -26
  51. package/dist/adapters/nitro.js.map +1 -1
  52. package/dist/adapters/shared.d.ts +16 -0
  53. package/dist/adapters/shared.d.ts.map +1 -0
  54. package/dist/cache/index.js +9 -2
  55. package/dist/cache/index.js.map +1 -1
  56. package/dist/cache/timber-cache.d.ts.map +1 -1
  57. package/dist/client/error-boundary.js +2 -1
  58. package/dist/client/error-boundary.js.map +1 -1
  59. package/dist/client/form.d.ts +10 -24
  60. package/dist/client/form.d.ts.map +1 -1
  61. package/dist/client/index.d.ts +1 -5
  62. package/dist/client/index.d.ts.map +1 -1
  63. package/dist/client/index.js +40 -90
  64. package/dist/client/index.js.map +1 -1
  65. package/dist/client/internal.d.ts +2 -1
  66. package/dist/client/internal.d.ts.map +1 -1
  67. package/dist/client/internal.js +81 -7
  68. package/dist/client/internal.js.map +1 -1
  69. package/dist/client/rsc-fetch.d.ts.map +1 -1
  70. package/dist/client/state.d.ts +1 -1
  71. package/dist/client/use-cookie.d.ts +8 -0
  72. package/dist/client/use-cookie.d.ts.map +1 -1
  73. package/dist/client/{use-params.d.ts → use-segment-params.d.ts} +1 -1
  74. package/dist/client/use-segment-params.d.ts.map +1 -0
  75. package/dist/codec.d.ts +1 -1
  76. package/dist/codec.d.ts.map +1 -1
  77. package/dist/codec.js +2 -2
  78. package/dist/config-types.d.ts +28 -0
  79. package/dist/config-types.d.ts.map +1 -1
  80. package/dist/cookies/define-cookie.d.ts +87 -35
  81. package/dist/cookies/define-cookie.d.ts.map +1 -1
  82. package/dist/cookies/index.d.ts +2 -1
  83. package/dist/cookies/index.d.ts.map +1 -1
  84. package/dist/cookies/index.js +48 -2
  85. package/dist/cookies/index.js.map +1 -0
  86. package/dist/cookies/json-cookie.d.ts +64 -0
  87. package/dist/cookies/json-cookie.d.ts.map +1 -0
  88. package/dist/cookies/validation.d.ts +46 -0
  89. package/dist/cookies/validation.d.ts.map +1 -0
  90. package/dist/{plugins/dev-404-page.d.ts → dev-tools/404-page.d.ts} +1 -1
  91. package/dist/dev-tools/404-page.d.ts.map +1 -0
  92. package/dist/{plugins/dev-browser-logs.d.ts → dev-tools/browser-logs.d.ts} +1 -1
  93. package/dist/dev-tools/browser-logs.d.ts.map +1 -0
  94. package/dist/{plugins/dev-error-page.d.ts → dev-tools/error-page.d.ts} +2 -2
  95. package/dist/dev-tools/error-page.d.ts.map +1 -0
  96. package/dist/{server/dev-holding-server.d.ts → dev-tools/holding-server.d.ts} +1 -1
  97. package/dist/dev-tools/holding-server.d.ts.map +1 -0
  98. package/dist/dev-tools/index.d.ts +31 -0
  99. package/dist/dev-tools/index.d.ts.map +1 -0
  100. package/dist/{server/dev-span-processor.d.ts → dev-tools/instrumentation.d.ts} +26 -6
  101. package/dist/dev-tools/instrumentation.d.ts.map +1 -0
  102. package/dist/{server/dev-logger.d.ts → dev-tools/logger.d.ts} +1 -1
  103. package/dist/dev-tools/logger.d.ts.map +1 -0
  104. package/dist/{plugins/dev-logs.d.ts → dev-tools/logs.d.ts} +1 -1
  105. package/dist/dev-tools/logs.d.ts.map +1 -0
  106. package/dist/{plugins/dev-error-overlay.d.ts → dev-tools/overlay.d.ts} +3 -12
  107. package/dist/dev-tools/overlay.d.ts.map +1 -0
  108. package/dist/dev-tools/stack-classifier.d.ts +34 -0
  109. package/dist/dev-tools/stack-classifier.d.ts.map +1 -0
  110. package/dist/{plugins/dev-terminal-error.d.ts → dev-tools/terminal.d.ts} +2 -2
  111. package/dist/dev-tools/terminal.d.ts.map +1 -0
  112. package/dist/{server/dev-warnings.d.ts → dev-tools/warnings.d.ts} +1 -1
  113. package/dist/dev-tools/warnings.d.ts.map +1 -0
  114. package/dist/index.d.ts +1 -0
  115. package/dist/index.d.ts.map +1 -1
  116. package/dist/index.js +97 -72
  117. package/dist/index.js.map +1 -1
  118. package/dist/plugin-context.d.ts +1 -1
  119. package/dist/plugin-context.d.ts.map +1 -1
  120. package/dist/plugins/adapter-build.d.ts.map +1 -1
  121. package/dist/routing/convention-lint.d.ts.map +1 -1
  122. package/dist/routing/index.js +1 -1
  123. package/dist/routing/scanner.d.ts.map +1 -1
  124. package/dist/routing/status-file-lint.d.ts.map +1 -1
  125. package/dist/search-params/define.d.ts +25 -7
  126. package/dist/search-params/define.d.ts.map +1 -1
  127. package/dist/search-params/index.js +5 -3
  128. package/dist/search-params/index.js.map +1 -1
  129. package/dist/search-params/wrappers.d.ts +2 -2
  130. package/dist/search-params/wrappers.d.ts.map +1 -1
  131. package/dist/segment-params/define.d.ts +23 -6
  132. package/dist/segment-params/define.d.ts.map +1 -1
  133. package/dist/segment-params/index.js +1 -1
  134. package/dist/server/access-gate.d.ts +4 -3
  135. package/dist/server/access-gate.d.ts.map +1 -1
  136. package/dist/server/action-handler.d.ts +15 -6
  137. package/dist/server/action-handler.d.ts.map +1 -1
  138. package/dist/server/als-registry.d.ts +5 -5
  139. package/dist/server/als-registry.d.ts.map +1 -1
  140. package/dist/server/asset-headers.d.ts +1 -15
  141. package/dist/server/asset-headers.d.ts.map +1 -1
  142. package/dist/server/cookie-context.d.ts +170 -0
  143. package/dist/server/cookie-context.d.ts.map +1 -0
  144. package/dist/server/cookie-parsing.d.ts +51 -0
  145. package/dist/server/cookie-parsing.d.ts.map +1 -0
  146. package/dist/server/deny-boundary.d.ts +90 -0
  147. package/dist/server/deny-boundary.d.ts.map +1 -0
  148. package/dist/server/deny-renderer.d.ts.map +1 -1
  149. package/dist/server/early-hints-sender.d.ts.map +1 -1
  150. package/dist/server/index.d.ts +5 -4
  151. package/dist/server/index.d.ts.map +1 -1
  152. package/dist/server/index.js +4 -149
  153. package/dist/server/index.js.map +1 -1
  154. package/dist/server/internal.d.ts +6 -4
  155. package/dist/server/internal.d.ts.map +1 -1
  156. package/dist/server/internal.js +261 -408
  157. package/dist/server/internal.js.map +1 -1
  158. package/dist/server/logger.d.ts +14 -0
  159. package/dist/server/logger.d.ts.map +1 -1
  160. package/dist/server/middleware-runner.d.ts +17 -0
  161. package/dist/server/middleware-runner.d.ts.map +1 -1
  162. package/dist/server/param-coercion.d.ts +26 -0
  163. package/dist/server/param-coercion.d.ts.map +1 -0
  164. package/dist/server/pipeline-helpers.d.ts +14 -7
  165. package/dist/server/pipeline-helpers.d.ts.map +1 -1
  166. package/dist/server/pipeline-outcome.d.ts +49 -0
  167. package/dist/server/pipeline-outcome.d.ts.map +1 -0
  168. package/dist/server/pipeline-phases.d.ts +4 -49
  169. package/dist/server/pipeline-phases.d.ts.map +1 -1
  170. package/dist/server/pipeline.d.ts +0 -2
  171. package/dist/server/pipeline.d.ts.map +1 -1
  172. package/dist/server/request-context.d.ts +22 -159
  173. package/dist/server/request-context.d.ts.map +1 -1
  174. package/dist/server/route-element-builder.d.ts.map +1 -1
  175. package/dist/server/rsc-entry/action-middleware-runner.d.ts +66 -0
  176. package/dist/server/rsc-entry/action-middleware-runner.d.ts.map +1 -0
  177. package/dist/server/rsc-entry/helpers.d.ts +1 -1
  178. package/dist/server/rsc-entry/helpers.d.ts.map +1 -1
  179. package/dist/server/rsc-entry/index.d.ts.map +1 -1
  180. package/dist/server/rsc-entry/render-route.d.ts +50 -0
  181. package/dist/server/rsc-entry/render-route.d.ts.map +1 -0
  182. package/dist/server/rsc-entry/wrap-action-dispatch.d.ts +59 -14
  183. package/dist/server/rsc-entry/wrap-action-dispatch.d.ts.map +1 -1
  184. package/dist/server/state-tree-diff.d.ts.map +1 -1
  185. package/dist/server/tracing.d.ts +1 -1
  186. package/dist/server/tracing.d.ts.map +1 -1
  187. package/dist/server/tree-builder.d.ts +45 -16
  188. package/dist/server/tree-builder.d.ts.map +1 -1
  189. package/dist/server/types.d.ts +48 -0
  190. package/dist/server/types.d.ts.map +1 -1
  191. package/dist/server/utils/escape-html.d.ts +14 -0
  192. package/dist/server/utils/escape-html.d.ts.map +1 -0
  193. package/dist/shims/headers.d.ts +2 -2
  194. package/dist/shims/headers.d.ts.map +1 -1
  195. package/dist/shims/navigation-client.d.ts +3 -1
  196. package/dist/shims/navigation-client.d.ts.map +1 -1
  197. package/dist/shims/navigation.d.ts +9 -4
  198. package/dist/shims/navigation.d.ts.map +1 -1
  199. package/package.json +6 -7
  200. package/src/adapters/build-output-helper.ts +77 -0
  201. package/src/adapters/cloudflare.ts +10 -50
  202. package/src/adapters/nitro.ts +11 -45
  203. package/src/adapters/shared.ts +40 -0
  204. package/src/cache/timber-cache.ts +3 -2
  205. package/src/cli.ts +0 -0
  206. package/src/client/form.tsx +17 -25
  207. package/src/client/index.ts +16 -9
  208. package/src/client/internal.ts +3 -2
  209. package/src/client/router.ts +1 -1
  210. package/src/client/rsc-fetch.ts +15 -0
  211. package/src/client/state.ts +2 -2
  212. package/src/client/use-cookie.ts +29 -0
  213. package/src/codec.ts +3 -7
  214. package/src/config-types.ts +28 -0
  215. package/src/cookies/define-cookie.ts +271 -78
  216. package/src/cookies/index.ts +11 -8
  217. package/src/cookies/json-cookie.ts +105 -0
  218. package/src/cookies/validation.ts +134 -0
  219. package/src/{plugins/dev-404-page.ts → dev-tools/404-page.ts} +2 -7
  220. package/src/{plugins/dev-error-page.ts → dev-tools/error-page.ts} +5 -32
  221. package/src/dev-tools/index.ts +90 -0
  222. package/src/dev-tools/instrumentation.ts +176 -0
  223. package/src/{plugins/dev-logs.ts → dev-tools/logs.ts} +2 -2
  224. package/src/{plugins/dev-error-overlay.ts → dev-tools/overlay.ts} +5 -23
  225. package/src/dev-tools/stack-classifier.ts +75 -0
  226. package/src/{plugins/dev-terminal-error.ts → dev-tools/terminal.ts} +4 -38
  227. package/src/{server/dev-warnings.ts → dev-tools/warnings.ts} +1 -1
  228. package/src/index.ts +11 -3
  229. package/src/plugin-context.ts +1 -1
  230. package/src/plugins/adapter-build.ts +3 -1
  231. package/src/plugins/dev-server.ts +3 -3
  232. package/src/plugins/shims.ts +1 -1
  233. package/src/plugins/static-build.ts +1 -1
  234. package/src/routing/convention-lint.ts +5 -4
  235. package/src/routing/scanner.ts +5 -2
  236. package/src/routing/status-file-lint.ts +4 -2
  237. package/src/search-params/define.ts +71 -15
  238. package/src/search-params/wrappers.ts +9 -2
  239. package/src/segment-params/define.ts +66 -13
  240. package/src/server/access-gate.tsx +9 -8
  241. package/src/server/action-handler.ts +28 -38
  242. package/src/server/als-registry.ts +5 -5
  243. package/src/server/asset-headers.ts +8 -34
  244. package/src/server/cookie-context.ts +468 -0
  245. package/src/server/cookie-parsing.ts +135 -0
  246. package/src/server/{deny-page-resolver.ts → deny-boundary.ts} +78 -14
  247. package/src/server/deny-renderer.ts +2 -7
  248. package/src/server/early-hints-sender.ts +3 -2
  249. package/src/server/fallback-error.ts +1 -1
  250. package/src/server/index.ts +13 -14
  251. package/src/server/internal.ts +10 -3
  252. package/src/server/logger.ts +23 -0
  253. package/src/server/middleware-runner.ts +44 -0
  254. package/src/server/param-coercion.ts +76 -0
  255. package/src/server/pipeline-helpers.ts +37 -13
  256. package/src/server/pipeline-outcome.ts +167 -0
  257. package/src/server/pipeline-phases.ts +27 -209
  258. package/src/server/pipeline.ts +2 -9
  259. package/src/server/request-context.ts +46 -451
  260. package/src/server/route-element-builder.ts +7 -3
  261. package/src/server/rsc-entry/action-middleware-runner.ts +167 -0
  262. package/src/server/rsc-entry/error-renderer.ts +1 -1
  263. package/src/server/rsc-entry/helpers.ts +2 -7
  264. package/src/server/rsc-entry/index.ts +34 -273
  265. package/src/server/rsc-entry/render-route.ts +304 -0
  266. package/src/server/rsc-entry/rsc-payload.ts +1 -1
  267. package/src/server/rsc-entry/ssr-renderer.ts +2 -2
  268. package/src/server/rsc-entry/wrap-action-dispatch.ts +316 -23
  269. package/src/server/ssr-entry.ts +1 -1
  270. package/src/server/state-tree-diff.ts +4 -1
  271. package/src/server/tracing.ts +3 -3
  272. package/src/server/tree-builder.ts +128 -52
  273. package/src/server/types.ts +52 -0
  274. package/src/server/utils/escape-html.ts +20 -0
  275. package/src/shims/headers.ts +3 -3
  276. package/src/shims/navigation-client.ts +4 -3
  277. package/src/shims/navigation.ts +9 -7
  278. package/dist/_chunks/actions-DLnUaR65.js +0 -421
  279. package/dist/_chunks/actions-DLnUaR65.js.map +0 -1
  280. package/dist/_chunks/als-registry-HS0LGUl2.js +0 -41
  281. package/dist/_chunks/als-registry-HS0LGUl2.js.map +0 -1
  282. package/dist/_chunks/debug-ECi_61pb.js +0 -108
  283. package/dist/_chunks/debug-ECi_61pb.js.map +0 -1
  284. package/dist/_chunks/define-C77ScO0m.js.map +0 -1
  285. package/dist/_chunks/define-Itxvcd7F.js.map +0 -1
  286. package/dist/_chunks/define-cookie-BowvzoP0.js +0 -94
  287. package/dist/_chunks/define-cookie-BowvzoP0.js.map +0 -1
  288. package/dist/_chunks/dev-warnings-DpGRGoDi.js.map +0 -1
  289. package/dist/_chunks/merge-search-params-Cm_KIWDX.js +0 -41
  290. package/dist/_chunks/merge-search-params-Cm_KIWDX.js.map +0 -1
  291. package/dist/_chunks/request-context-CK5tZqIP.js +0 -478
  292. package/dist/_chunks/request-context-CK5tZqIP.js.map +0 -1
  293. package/dist/_chunks/router-ref-C8OCm7g7.js.map +0 -1
  294. package/dist/_chunks/tracing-CCYbKn5n.js +0 -238
  295. package/dist/_chunks/tracing-CCYbKn5n.js.map +0 -1
  296. package/dist/_chunks/use-params-IOPu7E8t.js.map +0 -1
  297. package/dist/_chunks/use-query-states-BiV5GJgm.js.map +0 -1
  298. package/dist/_chunks/walkers-VOXgavMF.js.map +0 -1
  299. package/dist/client/use-params.d.ts.map +0 -1
  300. package/dist/plugins/dev-404-page.d.ts.map +0 -1
  301. package/dist/plugins/dev-browser-logs.d.ts.map +0 -1
  302. package/dist/plugins/dev-error-overlay.d.ts.map +0 -1
  303. package/dist/plugins/dev-error-page.d.ts.map +0 -1
  304. package/dist/plugins/dev-logs.d.ts.map +0 -1
  305. package/dist/plugins/dev-terminal-error.d.ts.map +0 -1
  306. package/dist/server/deny-page-resolver.d.ts +0 -52
  307. package/dist/server/deny-page-resolver.d.ts.map +0 -1
  308. package/dist/server/dev-fetch-instrumentation.d.ts +0 -22
  309. package/dist/server/dev-fetch-instrumentation.d.ts.map +0 -1
  310. package/dist/server/dev-holding-server.d.ts.map +0 -1
  311. package/dist/server/dev-logger.d.ts.map +0 -1
  312. package/dist/server/dev-span-processor.d.ts.map +0 -1
  313. package/dist/server/dev-warnings.d.ts.map +0 -1
  314. package/dist/server/page-deny-boundary.d.ts +0 -31
  315. package/dist/server/page-deny-boundary.d.ts.map +0 -1
  316. package/src/server/dev-fetch-instrumentation.ts +0 -96
  317. package/src/server/dev-span-processor.ts +0 -78
  318. package/src/server/page-deny-boundary.tsx +0 -56
  319. /package/src/client/{use-params.ts → use-segment-params.ts} +0 -0
  320. /package/src/{plugins/dev-browser-logs.ts → dev-tools/browser-logs.ts} +0 -0
  321. /package/src/{server/dev-holding-server.ts → dev-tools/holding-server.ts} +0 -0
  322. /package/src/{server/dev-logger.ts → dev-tools/logger.ts} +0 -0
@@ -22,12 +22,8 @@ import {
22
22
 
23
23
  import { validateCsrf, type CsrfConfig } from './csrf.js';
24
24
  import { executeAction, type RevalidateRenderer } from './actions.js';
25
- import {
26
- runWithRequestContext,
27
- setMutableCookieContext,
28
- getSetCookieHeaders,
29
- getCookiesForSsr,
30
- } from './request-context.js';
25
+ import { runWithRequestContext, setMutableCookieContext } from './request-context.js';
26
+ import { getSetCookieHeaders, getCookiesForSsr } from './cookie-context.js';
31
27
  import { handleActionError } from './action-client.js';
32
28
  import { enforceBodyLimits, enforceFieldLimit, type BodyLimitsConfig } from './body-limits.js';
33
29
  import { parseFormData } from './form-data.js';
@@ -72,6 +68,12 @@ const RSC_CONTENT_TYPE = 'text/x-component';
72
68
  * - With JS: POST with `x-rsc-action` header (client callServer dispatch)
73
69
  * - Without JS: POST with form data containing `$ACTION_REF` or `$ACTION_KEY`
74
70
  * (React's progressive enhancement hidden fields)
71
+ *
72
+ * **Important:** This function returns true for ANY POST with a form
73
+ * Content-Type, including non-action POSTs to route.ts API handlers.
74
+ * The caller (wrap-action-dispatch.ts) MUST check the matched route type
75
+ * before entering the action path — route.ts matches skip action detection
76
+ * entirely so their body is not pre-parsed. See TIM-870.
75
77
  */
76
78
  export function isActionRequest(req: Request): boolean {
77
79
  if (req.method !== 'POST') return false;
@@ -90,25 +92,6 @@ export function isActionRequest(req: Request): boolean {
90
92
 
91
93
  // ─── Handler ──────────────────────────────────────────────────────────────
92
94
 
93
- /**
94
- * Serialize a `Map<string, string>` of cookie name → value as a `Cookie:`
95
- * request header value. Format: `name1=value1; name2=value2`. Matches the
96
- * format `parseCookieHeader` in `request-context.ts` reads with, so the
97
- * rerender pipeline can parse it back into the same RYW state.
98
- *
99
- * Used to thread the post-action cookie state from the action's ALS scope
100
- * into the rerender pipeline's fresh ALS scope. Cookies marked for deletion
101
- * are already absent from `getCookiesForSsr()`'s map, so they naturally drop
102
- * out of the synthesized header. See TIM-837.
103
- */
104
- function serializeCookieMapAsHeader(cookies: Map<string, string>): string {
105
- const parts: string[] = [];
106
- for (const [name, value] of cookies) {
107
- parts.push(`${name}=${value}`);
108
- }
109
- return parts.join('; ');
110
- }
111
-
112
95
  /**
113
96
  * Signal from handleFormAction to re-render the page with flash data instead of redirecting.
114
97
  *
@@ -120,16 +103,19 @@ function serializeCookieMapAsHeader(cookies: Map<string, string>): string {
120
103
  * final HTML response. Without this, cookies set inside the action are
121
104
  * silently dropped from the response. See TIM-836 (LOCAL-740).
122
105
  *
123
- * - `cookieHeader`: the post-action read-your-own-writes view of the
124
- * cookie jar, serialized as a `Cookie:` request header. The rerender
125
- * pipeline uses this to clone the request before re-rendering, so the
126
- * page server components see the action's cookie writes immediately
127
- * instead of the stale pre-action state. See TIM-837.
106
+ * - `cookies`: the post-action read-your-own-writes view of the cookie
107
+ * jar, as the same `Map<string, string>` shape used by the request
108
+ * context's `parsedCookies`. The rerender dispatcher seeds this map
109
+ * directly into the rerender request context via `seedRequestCookies`,
110
+ * bypassing any string round-trip through a `Cookie:` header. The
111
+ * direct-Map seed eliminates the value-smuggling primitive that the
112
+ * previous `cookieHeader: string` shape carried — see
113
+ * ONGOING_SECURITY.md H-3 (TIM-868) and TIM-837.
128
114
  */
129
115
  export interface FormRerender {
130
116
  rerender: FormFlashData;
131
117
  setCookieHeaders: string[];
132
- cookieHeader: string;
118
+ cookies: Map<string, string>;
133
119
  }
134
120
 
135
121
  /**
@@ -208,9 +194,13 @@ export async function handleActionRequest(
208
194
  }
209
195
  } else if (result && 'rerender' in result) {
210
196
  result.setCookieHeaders = getSetCookieHeaders();
211
- // Snapshot the post-action RYW cookie state so the rerender pipeline
212
- // can re-parse it into a fresh ALS scope. See TIM-837.
213
- result.cookieHeader = serializeCookieMapAsHeader(getCookiesForSsr());
197
+ // Snapshot the post-action RYW cookie state as a Map so the rerender
198
+ // dispatcher can seed it directly into the rerender request context
199
+ // via `seedRequestCookies`, with no string round-trip. See TIM-837
200
+ // and ONGOING_SECURITY.md H-3 (TIM-868). `getCookiesForSsr` already
201
+ // returns a defensive copy, so the rerender scope cannot mutate the
202
+ // snapshot through this reference.
203
+ result.cookies = getCookiesForSsr();
214
204
  }
215
205
  return result;
216
206
  });
@@ -397,7 +387,7 @@ async function handleFormAction(
397
387
  },
398
388
  // Filled in by handleActionRequest before the ALS scope exits.
399
389
  setCookieHeaders: [],
400
- cookieHeader: '',
390
+ cookies: new Map(),
401
391
  };
402
392
  }
403
393
 
@@ -427,7 +417,7 @@ async function handleFormAction(
427
417
  );
428
418
  }
429
419
 
430
- // setCookieHeaders + cookieHeader are filled in by handleActionRequest
431
- // before the ALS scope exits.
432
- return { rerender: actionResult, setCookieHeaders: [], cookieHeader: '' };
420
+ // setCookieHeaders + cookies are filled in by handleActionRequest before
421
+ // the ALS scope exits.
422
+ return { rerender: actionResult, setCookieHeaders: [], cookies: new Map() };
433
423
  }
@@ -40,25 +40,25 @@ export interface RequestContextStore {
40
40
  /** Original (pre-overlay) frozen headers, kept for overlay merging. */
41
41
  originalHeaders: Headers;
42
42
  /**
43
- * Promise resolving to the raw URLSearchParams for the current request.
43
+ * Raw URLSearchParams for the current request.
44
44
  * To get typed parsed params, import a search params definition and
45
45
  * call `.parse(searchParams())`.
46
46
  */
47
- searchParamsPromise: Promise<URLSearchParams>;
47
+ searchParams: URLSearchParams;
48
48
  /**
49
49
  * Raw search string from the request URL (e.g. "?foo=bar&baz=1").
50
50
  * Available synchronously for use in `redirect()` with `preserveSearchParams`.
51
51
  */
52
52
  searchString: string;
53
53
  /**
54
- * Promise resolving to the coerced segment params for the current request.
54
+ * Coerced segment params for the current request.
55
55
  * Set by the pipeline after route matching and param coercion, before
56
56
  * middleware and rendering. Pages and layouts read params via
57
57
  * `getSegmentParams()` instead of receiving them as a prop.
58
58
  *
59
59
  * See design/07-routing.md §"params.ts — Convention File for Typed Params"
60
60
  */
61
- segmentParamsPromise?: Promise<Record<string, string | string[]>>;
61
+ segmentParams?: Record<string, string | string[]>;
62
62
  /** Outgoing Set-Cookie entries (name → serialized value + options). Last write wins. */
63
63
  cookieJar: Map<string, CookieEntry>;
64
64
  /** Whether the response has flushed (headers committed). */
@@ -84,7 +84,7 @@ export interface RequestContextStore {
84
84
  export interface CookieEntry {
85
85
  name: string;
86
86
  value: string;
87
- options: import('./request-context.js').CookieOptions;
87
+ options: import('./cookie-context.js').CookieOptions;
88
88
  }
89
89
 
90
90
  // ─── Tracing ──────────────────────────────────────────────────────────────
@@ -15,6 +15,14 @@
15
15
  * Design docs: 18-build-system.md, 06-caching.md
16
16
  */
17
17
 
18
+ import { IMMUTABLE_CACHE, STATIC_CACHE } from '../adapters/shared.js';
19
+
20
+ // Re-export cache constants and generateHeadersFile from the shared adapter
21
+ // module. The canonical definitions live in adapters/shared.ts because
22
+ // adapters are loaded by Node before Vite's resolver is available — they
23
+ // can only import from within their own directory.
24
+ export { IMMUTABLE_CACHE, STATIC_CACHE, generateHeadersFile } from '../adapters/shared.js';
25
+
18
26
  /**
19
27
  * Regex matching Vite-hashed asset filenames.
20
28
  *
@@ -30,18 +38,6 @@
30
38
  */
31
39
  const HASHED_ASSET_RE = /[-.][\da-f]{8,}\.\w+$/;
32
40
 
33
- /** One year in seconds (365 days). */
34
- const ONE_YEAR = 31_536_000;
35
-
36
- /** One hour in seconds. */
37
- const ONE_HOUR = 3_600;
38
-
39
- /** Cache-Control value for hashed (immutable) assets. */
40
- export const IMMUTABLE_CACHE = `public, max-age=${ONE_YEAR}, immutable`;
41
-
42
- /** Cache-Control value for unhashed static assets. */
43
- export const STATIC_CACHE = `public, max-age=${ONE_HOUR}, must-revalidate`;
44
-
45
41
  /**
46
42
  * Check if a URL path looks like a hashed asset.
47
43
  */
@@ -57,25 +53,3 @@ export function isHashedAsset(pathname: string): boolean {
57
53
  export function getAssetCacheControl(pathname: string): string {
58
54
  return isHashedAsset(pathname) ? IMMUTABLE_CACHE : STATIC_CACHE;
59
55
  }
60
-
61
- /**
62
- * Generate a `_headers` file for static asset cache control.
63
- *
64
- * The `_headers` file is a platform convention supported by Cloudflare Workers
65
- * Static Assets, Cloudflare Pages, and Netlify. It maps URL patterns to
66
- * HTTP response headers.
67
- *
68
- * Vite places all hashed chunks under `/assets/` — these get immutable caching.
69
- * Everything else (favicon.ico, robots.txt, etc.) gets a shorter cache.
70
- */
71
- export function generateHeadersFile(): string {
72
- return `# Auto-generated by @timber-js/app — static asset cache headers.
73
- # See design/25-production-deployments.md §"CDN / Edge Cache"
74
-
75
- /assets/*
76
- Cache-Control: ${IMMUTABLE_CACHE}
77
-
78
- /*
79
- Cache-Control: ${STATIC_CACHE}
80
- `;
81
- }
@@ -0,0 +1,468 @@
1
+ /**
2
+ * Cookie Context — per-request cookie API and on-the-wire helpers.
3
+ *
4
+ * Split out of `request-context.ts` (TIM-853) so the cookie subsystem
5
+ * — encoding contract, options grammar, parser, serializer, RYW map,
6
+ * and rerender seed — lives in one file. The headers/scope/params APIs
7
+ * stay in `request-context.ts` and call into this module via the
8
+ * exported helpers.
9
+ *
10
+ * See design/29-cookies.md for the encoding contract and read-your-own-
11
+ * writes semantics. See ONGOING_SECURITY.md H-3 (TIM-868) for the
12
+ * smuggling primitive that the encoding contract closes.
13
+ */
14
+
15
+ import { requestContextAls, type RequestContextStore } from './als-registry.js';
16
+ import { isDebug } from './debug.js';
17
+ import { assertValidCookieName, assertValidCookieValue } from '../cookies/validation.js';
18
+ import {
19
+ parseCookieHeader,
20
+ parseSetCookie,
21
+ safeDecodeCookieValue,
22
+ serializeCookieEntry,
23
+ } from './cookie-parsing.js';
24
+
25
+ // Re-export the validators so framework-internal consumers and tests can
26
+ // import the canonical implementation from the same module that hosts
27
+ // `getCookies()`. The pure shared module lives in `cookies/validation.ts`
28
+ // so the client `useCookie` hook can use the same checks without pulling
29
+ // in the server ALS code.
30
+ export { assertValidCookieName, assertValidCookieValue };
31
+
32
+ // ─── Public API ───────────────────────────────────────────────────────────
33
+
34
+ /**
35
+ * Returns a cookie accessor for the current request.
36
+ *
37
+ * Available in middleware, access checks, server components, and server actions.
38
+ * Throws if called outside a request context (security principle #2: no global fallback).
39
+ *
40
+ * Read methods (.get, .has, .getAll) are always available and reflect
41
+ * read-your-own-writes from .set() calls in the same request.
42
+ *
43
+ * Mutation methods (.set, .delete, .clear) are only available in mutable
44
+ * contexts (middleware.ts, server actions, route.ts handlers). Calling them
45
+ * in read-only contexts (access.ts, server components) throws.
46
+ *
47
+ * This is the escape hatch for direct cookie jar operations. For typed
48
+ * cookie access, use `defineCookie()` instead.
49
+ *
50
+ * See design/29-cookies.md
51
+ */
52
+ export function getCookieJar(): RequestCookies {
53
+ const store = requestContextAls.getStore();
54
+ if (!store) {
55
+ throw new Error(
56
+ '[timber] getCookieJar() called outside of a request context. ' +
57
+ 'It can only be used in middleware, access checks, server components, and server actions.'
58
+ );
59
+ }
60
+
61
+ // Parse cookies lazily on first access
62
+ if (!store.parsedCookies) {
63
+ store.parsedCookies = parseCookieHeader(store.cookieHeader);
64
+ }
65
+
66
+ const map = store.parsedCookies;
67
+ return {
68
+ get(name: string): string | undefined {
69
+ return map.get(name);
70
+ },
71
+ has(name: string): boolean {
72
+ return map.has(name);
73
+ },
74
+ getAll(): Array<{ name: string; value: string }> {
75
+ return Array.from(map.entries()).map(([name, value]) => ({ name, value }));
76
+ },
77
+ get size(): number {
78
+ return map.size;
79
+ },
80
+
81
+ set(name: string, value: string, options?: SetCookieOptions): void {
82
+ assertMutable(store, 'set');
83
+ // Validate the name first — names cannot be URL-encoded (RFC 7230
84
+ // token grammar is strict), so a bad name is always a bug.
85
+ assertValidCookieName(name);
86
+ // Type guard with a guiding error. The single most common mistake
87
+ // is passing a non-string (object, number, Date) and expecting the
88
+ // framework to JSON-encode. Point developers at jsonCookieCodec.
89
+ if (typeof value !== 'string') {
90
+ throw new Error(
91
+ `[timber] getCookieJar().set(${JSON.stringify(name)}, …): value must be a string, got ${typeof value}.\n` +
92
+ ` To store a JSON-serializable value, use defineCookie + jsonCookieCodec:\n` +
93
+ `\n` +
94
+ ` import { defineCookie, jsonCookieCodec } from '@timber-js/app/cookies';\n` +
95
+ `\n` +
96
+ ` export const ${name}Cookie = defineCookie(${JSON.stringify(name)}, {\n` +
97
+ ` codec: jsonCookieCodec(),\n` +
98
+ ` });\n` +
99
+ `\n` +
100
+ ` ${name}Cookie.set(value);\n` +
101
+ `\n` +
102
+ ` See design/29-cookies.md §"Typed Cookies with Schema Validation".`
103
+ );
104
+ }
105
+ // Encode the value so the on-the-wire bytes always satisfy
106
+ // RFC 6265 §4.1.1 cookie-octet. encodeURIComponent's output is a
107
+ // strict subset of cookie-octet (only `A-Z a-z 0-9 ! ' ( ) * - . _
108
+ // ~ %`), so the encoded form can never carry the H-3 smuggling
109
+ // primitive — `;` becomes `%3B`, CR/LF become `%0D`/`%0A`, etc.
110
+ // The round-trip is lossless: parseCookieHeader auto-decodes on
111
+ // read, so `cookies().get(name)` returns exactly `value`. See
112
+ // ONGOING_SECURITY.md H-3 (TIM-868) and design/29-cookies.md
113
+ // §"Encoding Contract".
114
+ //
115
+ // The `{ raw: true }` opt-out skips the encoder for callers who
116
+ // need exact byte control (e.g. forwarding pre-encoded cookies
117
+ // from an upstream service). The opt-out goes through the strict
118
+ // cookie-octet validator instead — the smuggling primitive cannot
119
+ // sneak in via the escape hatch.
120
+ const raw = options?.raw === true;
121
+ const wireValue = raw ? value : encodeURIComponent(value);
122
+ if (raw) {
123
+ assertValidCookieValue(name, wireValue);
124
+ }
125
+ if (store.flushed) {
126
+ if (isDebug()) {
127
+ console.warn(
128
+ `[timber] warn: getCookieJar().set('${name}') called after response headers were committed.\n` +
129
+ ` The cookie will NOT be sent. Move cookie mutations to middleware.ts, a server action,\n` +
130
+ ` or a route.ts handler.`
131
+ );
132
+ }
133
+ return;
134
+ }
135
+ // Strip the framework-only `raw` flag before persisting — it is
136
+ // not an HTTP cookie attribute and must not leak into the jar.
137
+ const { raw: _raw, ...attributeOptions } = options ?? {};
138
+ void _raw;
139
+ const opts = { ...DEFAULT_COOKIE_OPTIONS, ...attributeOptions };
140
+ store.cookieJar.set(name, { name, value: wireValue, options: opts });
141
+ // Read-your-own-writes: store the DECODED logical value so that
142
+ // subsequent `cookies().get(name)` in the same request returns
143
+ // exactly what the developer wrote — never the encoded form.
144
+ // For `{ raw: true }`, the wire form IS the logical form.
145
+ map.set(name, raw ? wireValue : value);
146
+ },
147
+
148
+ setFromHeaders(headers: Headers): void {
149
+ assertMutable(store, 'setFromHeaders');
150
+ if (store.flushed) {
151
+ console.warn(
152
+ `[timber] warn: getCookieJar().setFromHeaders() called after response headers were committed.\n` +
153
+ ` The cookies will NOT be sent. Move cookie mutations to middleware.ts, a server action,\n` +
154
+ ` or a route.ts handler.`
155
+ );
156
+ return;
157
+ }
158
+ // Headers.getSetCookie() returns individual Set-Cookie strings,
159
+ // avoiding the fragile comma-splitting that raw .get() requires.
160
+ for (const raw of headers.getSetCookie()) {
161
+ const parsed = parseSetCookie(raw);
162
+ if (parsed) {
163
+ // Use setRaw to preserve the original header's attributes without
164
+ // merging DEFAULT_COOKIE_OPTIONS (parseSetCookie intentionally
165
+ // does not apply defaults — see its doc comment).
166
+ setRaw(store, map, parsed.name, parsed.value, parsed.options);
167
+ }
168
+ }
169
+ },
170
+
171
+ delete(name: string, options?: Pick<CookieOptions, 'path' | 'domain'>): void {
172
+ assertMutable(store, 'delete');
173
+ // Validate the name even though delete writes an empty value — a
174
+ // smuggled name (`;` or CR/LF) would corrupt the Set-Cookie header
175
+ // we emit. See ONGOING_SECURITY.md H-3 (TIM-868).
176
+ assertValidCookieName(name);
177
+ if (store.flushed) {
178
+ if (isDebug()) {
179
+ console.warn(
180
+ `[timber] warn: getCookieJar().delete('${name}') called after response headers were committed.\n` +
181
+ ` The cookie will NOT be deleted. Move cookie mutations to middleware.ts, a server action,\n` +
182
+ ` or a route.ts handler.`
183
+ );
184
+ }
185
+ return;
186
+ }
187
+ const opts: CookieOptions = {
188
+ ...DEFAULT_COOKIE_OPTIONS,
189
+ ...options,
190
+ maxAge: 0,
191
+ expires: new Date(0),
192
+ };
193
+ store.cookieJar.set(name, { name, value: '', options: opts });
194
+ // Remove from read view
195
+ map.delete(name);
196
+ },
197
+
198
+ clear(): void {
199
+ assertMutable(store, 'clear');
200
+ if (store.flushed) return;
201
+ // Delete every incoming cookie
202
+ for (const name of Array.from(map.keys())) {
203
+ store.cookieJar.set(name, {
204
+ name,
205
+ value: '',
206
+ options: { ...DEFAULT_COOKIE_OPTIONS, maxAge: 0, expires: new Date(0) },
207
+ });
208
+ }
209
+ map.clear();
210
+ },
211
+
212
+ toString(): string {
213
+ // Re-encode values when serializing as a Cookie header — the
214
+ // RYW map holds decoded logical values, but a Cookie header has
215
+ // to satisfy `cookie-octet`. Mirror the auto-encode contract on
216
+ // `set()` so toString() round-trips losslessly with parseCookieHeader.
217
+ return Array.from(map.entries())
218
+ .map(([name, value]) => `${name}=${encodeURIComponent(value)}`)
219
+ .join('; ');
220
+ },
221
+ };
222
+ }
223
+
224
+ /**
225
+ * Returns the value of a single cookie, or undefined if absent.
226
+ *
227
+ * @internal — not part of the public API. Use `defineCookie().get()` or `getCookieJar().get()` instead.
228
+ */
229
+ export function getCookie(name: string): string | undefined {
230
+ const jar = getCookieJar();
231
+ return jar.get(name);
232
+ }
233
+
234
+ // ─── Types ────────────────────────────────────────────────────────────────
235
+
236
+ /**
237
+ * Per-call options for `getCookies().set()`. Extends the persistent
238
+ * `CookieOptions` (HTTP cookie attributes) with framework-only flags
239
+ * that are NOT serialized into the Set-Cookie header.
240
+ *
241
+ * The `raw` flag is the escape hatch for the auto-encoding contract.
242
+ * See design/29-cookies.md §"Encoding Contract".
243
+ */
244
+ export interface SetCookieOptions extends CookieOptions {
245
+ /**
246
+ * Skip the framework's `encodeURIComponent` pass and store the value
247
+ * verbatim. The value is then validated against the strict RFC 6265
248
+ * §4.1.1 `cookie-octet` grammar — the H-3 smuggling primitive cannot
249
+ * sneak in via this opt-out.
250
+ *
251
+ * Use this when forwarding a cookie value that is already in its
252
+ * intended on-the-wire form (e.g. mirroring an upstream service's
253
+ * Set-Cookie). Default: `false` (auto-encode).
254
+ */
255
+ raw?: boolean;
256
+ }
257
+
258
+ /** Options for setting a cookie. See design/29-cookies.md. */
259
+ export interface CookieOptions {
260
+ /** Domain scope. Default: omitted (current domain only). */
261
+ domain?: string;
262
+ /** URL path scope. Default: '/'. */
263
+ path?: string;
264
+ /** Expiration date. Mutually exclusive with maxAge. */
265
+ expires?: Date;
266
+ /** Max age in seconds. Mutually exclusive with expires. */
267
+ maxAge?: number;
268
+ /** Prevent client-side JS access. Default: true. */
269
+ httpOnly?: boolean;
270
+ /** Only send over HTTPS. Default: true. */
271
+ secure?: boolean;
272
+ /** Cross-site request policy. Default: 'lax'. */
273
+ sameSite?: 'strict' | 'lax' | 'none';
274
+ /** Partitioned (CHIPS) — isolate cookie per top-level site. Default: false. */
275
+ partitioned?: boolean;
276
+ }
277
+
278
+ /**
279
+ * Cookie accessor returned by `getCookies()`.
280
+ *
281
+ * Read methods are always available. Mutation methods throw in read-only
282
+ * contexts (access.ts, server components).
283
+ */
284
+ export interface RequestCookies {
285
+ /** Get a cookie value by name. Returns undefined if not present. */
286
+ get(name: string): string | undefined;
287
+ /** Check if a cookie exists. */
288
+ has(name: string): boolean;
289
+ /** Get all cookies as an array of { name, value } pairs. */
290
+ getAll(): Array<{ name: string; value: string }>;
291
+ /** Number of cookies. */
292
+ readonly size: number;
293
+ /**
294
+ * Set a cookie. Only available in mutable contexts (middleware, actions,
295
+ * route handlers).
296
+ *
297
+ * The value is auto-encoded with `encodeURIComponent` so the on-the-wire
298
+ * bytes always satisfy RFC 6265 §4.1.1 cookie-octet — `cookies().get()`
299
+ * returns the same logical value the developer wrote. Pass `{ raw: true }`
300
+ * to skip the encoder; the raw path validates against the strict
301
+ * cookie-octet grammar instead. See design/29-cookies.md §"Encoding
302
+ * Contract" and ONGOING_SECURITY.md H-3 (TIM-868).
303
+ */
304
+ set(name: string, value: string, options?: SetCookieOptions): void;
305
+ /**
306
+ * Copy all `Set-Cookie` headers from a `Headers` object.
307
+ * Parses each header and forwards name, value, and all attributes
308
+ * (path, domain, max-age, expires, sameSite, secure, httpOnly, partitioned).
309
+ *
310
+ * Useful when forwarding cookies from an internal `fetch()` or auth handler:
311
+ * ```ts
312
+ * const response = await auth.handler(req);
313
+ * getCookies().then(c => c.setFromHeaders(response.headers));
314
+ * ```
315
+ */
316
+ setFromHeaders(headers: Headers): void;
317
+ /** Delete a cookie. Only available in mutable contexts. */
318
+ delete(name: string, options?: Pick<CookieOptions, 'path' | 'domain'>): void;
319
+ /** Delete all cookies. Only available in mutable contexts. */
320
+ clear(): void;
321
+ /** Serialize cookies as a Cookie header string. */
322
+ toString(): string;
323
+ }
324
+
325
+ const DEFAULT_COOKIE_OPTIONS: CookieOptions = {
326
+ path: '/',
327
+ httpOnly: true,
328
+ secure: true,
329
+ sameSite: 'lax',
330
+ };
331
+
332
+ // ─── Framework-Internal Helpers ───────────────────────────────────────────
333
+
334
+ /**
335
+ * Per-request seed map of cookie name → value, registered out-of-band so the
336
+ * pipeline's `runWithRequestContext` call can pick it up without changing
337
+ * the function's calling convention. Stored in a WeakMap keyed by the
338
+ * synthetic Request object built for the rerender path, so the seed lives
339
+ * exactly as long as the request and is collected with it.
340
+ *
341
+ * The seed exists to eliminate the parse/serialize round-trip on the no-JS
342
+ * form-rerender path — see ONGOING_SECURITY.md H-3 (TIM-868). The action's
343
+ * post-mutation RYW snapshot is threaded directly into the rerender scope's
344
+ * `parsedCookies` map, bypassing `parseCookieHeader` entirely.
345
+ */
346
+ const seededRequestCookies = new WeakMap<Request, Map<string, string>>();
347
+
348
+ /**
349
+ * Register a pre-parsed cookie map to use as the request context seed for
350
+ * the next `runWithRequestContext(req, …)` call with this exact `req`.
351
+ *
352
+ * Used by the no-JS form-rerender dispatcher in
353
+ * `rsc-entry/wrap-action-dispatch.ts` to thread the action's post-mutation
354
+ * cookie state into the rerender scope without serializing back through a
355
+ * `Cookie:` header. See TIM-868 / TIM-837.
356
+ *
357
+ * @internal — framework use only.
358
+ */
359
+ export function seedRequestCookies(req: Request, cookies: Map<string, string>): void {
360
+ // Defensive copy: callers must not be able to mutate the rerender scope's
361
+ // map after seeding, and the rerender scope must not mutate the snapshot
362
+ // we hand back to other observers.
363
+ seededRequestCookies.set(req, new Map(cookies));
364
+ }
365
+
366
+ /**
367
+ * Pop the seed (if any) for `req` and return it. Called from
368
+ * `runWithRequestContext` exactly once per request — the seed is consumed
369
+ * eagerly so it cannot leak into a hypothetical future re-use of the same
370
+ * Request reference.
371
+ *
372
+ * @internal — framework use only.
373
+ */
374
+ export function consumeSeededCookies(req: Request): Map<string, string> | undefined {
375
+ const seed = seededRequestCookies.get(req);
376
+ if (seed) seededRequestCookies.delete(req);
377
+ return seed;
378
+ }
379
+
380
+ /**
381
+ * Build a Map of cookie name → value reflecting the current request's
382
+ * read-your-own-writes state. Includes incoming cookies plus any
383
+ * mutations from getCookies().set() / getCookies().delete() in the same request.
384
+ *
385
+ * Used by SSR renderers to populate NavContext.cookies so that
386
+ * useCookie()'s server snapshot matches the actual response state.
387
+ *
388
+ * See design/29-cookies.md §"Read-Your-Own-Writes"
389
+ * See design/triage/TIM-441-cookie-api-triage.md §4
390
+ */
391
+ export function getCookiesForSsr(): Map<string, string> {
392
+ const store = requestContextAls.getStore();
393
+ if (!store) {
394
+ throw new Error('[timber] getCookiesForSsr() called outside of a request context.');
395
+ }
396
+
397
+ // Trigger lazy parsing if not yet done
398
+ if (!store.parsedCookies) {
399
+ store.parsedCookies = parseCookieHeader(store.cookieHeader);
400
+ }
401
+
402
+ // The parsedCookies map already reflects read-your-own-writes:
403
+ // - getCookies().set() updates the map via map.set(name, value)
404
+ // - getCookies().delete() removes from the map via map.delete(name)
405
+ // Return a copy so callers can't mutate the internal map.
406
+ return new Map(store.parsedCookies);
407
+ }
408
+
409
+ /**
410
+ * Collect all Set-Cookie headers from the cookie jar.
411
+ * Called by the framework at flush time to apply cookies to the response.
412
+ *
413
+ * Returns an array of serialized Set-Cookie header values.
414
+ */
415
+ export function getSetCookieHeaders(): string[] {
416
+ const store = requestContextAls.getStore();
417
+ if (!store) return [];
418
+ return Array.from(store.cookieJar.values()).map(serializeCookieEntry);
419
+ }
420
+
421
+ // ─── Cookie Helpers ───────────────────────────────────────────────────────
422
+
423
+ /** Throw if cookie mutation is attempted in a read-only context. */
424
+ function assertMutable(store: RequestContextStore, method: string): void {
425
+ if (!store.mutableContext) {
426
+ throw new Error(
427
+ `[timber] getCookieJar().${method}() cannot be called in this context.\n` +
428
+ ` Set cookies in middleware.ts, server actions, or route.ts handlers.`
429
+ );
430
+ }
431
+ }
432
+
433
+ /**
434
+ * Write a cookie to the jar WITHOUT merging DEFAULT_COOKIE_OPTIONS.
435
+ * Used by setFromHeaders to preserve the original header's attributes exactly.
436
+ *
437
+ * For deletion cookies (maxAge=0), the jar entry is still created so the
438
+ * Set-Cookie header is emitted, but the cookie is NOT added to the read map
439
+ * (it would be misleading — the cookie is being deleted).
440
+ */
441
+ function setRaw(
442
+ store: RequestContextStore,
443
+ readMap: Map<string, string>,
444
+ name: string,
445
+ value: string,
446
+ options: CookieOptions
447
+ ): void {
448
+ // setRaw is the forwarding path for upstream Set-Cookie headers
449
+ // (`getCookies().setFromHeaders(response.headers)`). The value comes
450
+ // out of `parseSetCookie` in its on-the-wire form — already encoded
451
+ // by whoever produced it — so we re-emit it verbatim into our jar.
452
+ // We DO validate against the strict cookie-octet grammar, both as
453
+ // defense-in-depth against a malicious upstream and to keep the
454
+ // ONGOING_SECURITY.md H-3 invariant intact at every entry point into
455
+ // the cookie jar / RYW map.
456
+ assertValidCookieName(name);
457
+ assertValidCookieValue(name, value);
458
+ store.cookieJar.set(name, { name, value, options });
459
+ // Deletion cookies (Max-Age=0) should not appear in the read map.
460
+ if (options.maxAge === 0) {
461
+ readMap.delete(name);
462
+ } else {
463
+ // Decode for the RYW map so consumers see the same logical bytes
464
+ // they would see if the upstream cookie had arrived on the next
465
+ // request. Mirrors `parseCookieHeader`'s auto-decode.
466
+ readMap.set(name, safeDecodeCookieValue(value));
467
+ }
468
+ }