@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
@@ -0,0 +1,1061 @@
1
+ import { _ as waitUntilAls, d as withSpan, h as revalidationAls, m as requestContextAls, v as isDebug } from "./tracing-C8V-YGsP.js";
2
+ import { n as fromSchema } from "./schema-bridge-Cxu4l-7p.js";
3
+ import { t as _setGetSearchParamsFn } from "./define-B-Q_UMOD.js";
4
+ import { n as _setGetSegmentParamsFn } from "./define-CfBPoJb0.js";
5
+ import { n as assertValidCookieName, r as assertValidCookieValue, t as mergePreservedSearchParams } from "./merge-search-params-BphMdht_.js";
6
+ import { n as _registerServerCookieImpl, t as _registerFromSchema } from "./define-cookie-BjpIt4UC.js";
7
+ import { t as getCacheHandler } from "./handler-store-B-lqaGyh.js";
8
+ //#region src/server/cookie-parsing.ts
9
+ /**
10
+ * Parse a Cookie header string into a Map of name → value pairs.
11
+ * Follows RFC 6265 §4.2.1: cookies are semicolon-separated key=value pairs.
12
+ *
13
+ * Values are auto-decoded with `decodeURIComponent` so they round-trip
14
+ * losslessly with `getCookies().set()` (which auto-encodes). Malformed
15
+ * `%`-escapes from third-party cookies fall back to the raw byte sequence
16
+ * — the parser must be total over arbitrary inbound headers, including
17
+ * non-conforming values from other servers, browser extensions, etc.
18
+ */
19
+ function parseCookieHeader(header) {
20
+ const map = /* @__PURE__ */ new Map();
21
+ if (!header) return map;
22
+ for (const pair of header.split(";")) {
23
+ const eqIndex = pair.indexOf("=");
24
+ if (eqIndex === -1) continue;
25
+ const name = pair.slice(0, eqIndex).trim();
26
+ const value = pair.slice(eqIndex + 1).trim();
27
+ if (name) map.set(name, safeDecodeCookieValue(value));
28
+ }
29
+ return map;
30
+ }
31
+ /**
32
+ * Decode a single cookie value with `decodeURIComponent`, falling back to
33
+ * the raw byte sequence if the input contains a malformed `%`-escape.
34
+ *
35
+ * Used by both `parseCookieHeader` (incoming Cookie: header) and the
36
+ * `setRaw` forwarding path (outgoing Set-Cookie from upstream services).
37
+ * Total — never throws.
38
+ */
39
+ function safeDecodeCookieValue(raw) {
40
+ try {
41
+ return decodeURIComponent(raw);
42
+ } catch {
43
+ return raw;
44
+ }
45
+ }
46
+ /** Serialize a CookieEntry into a Set-Cookie header value. */
47
+ function serializeCookieEntry(entry) {
48
+ const parts = [`${entry.name}=${entry.value}`];
49
+ const opts = entry.options;
50
+ if (opts.domain) parts.push(`Domain=${opts.domain}`);
51
+ if (opts.path) parts.push(`Path=${opts.path}`);
52
+ if (opts.expires) parts.push(`Expires=${opts.expires.toUTCString()}`);
53
+ if (opts.maxAge !== void 0) parts.push(`Max-Age=${opts.maxAge}`);
54
+ if (opts.httpOnly) parts.push("HttpOnly");
55
+ if (opts.secure) parts.push("Secure");
56
+ if (opts.sameSite) parts.push(`SameSite=${opts.sameSite.charAt(0).toUpperCase()}${opts.sameSite.slice(1)}`);
57
+ if (opts.partitioned) parts.push("Partitioned");
58
+ return parts.join("; ");
59
+ }
60
+ /**
61
+ * Parse a raw `Set-Cookie` header string into name, value, and options.
62
+ * Handles all standard attributes: Path, Domain, Max-Age, Expires,
63
+ * SameSite, Secure, HttpOnly, Partitioned.
64
+ *
65
+ * Does NOT apply DEFAULT_COOKIE_OPTIONS — the caller decides whether
66
+ * to merge defaults (e.g. `set()` does, but `setRaw()` should preserve
67
+ * the original header's intent).
68
+ */
69
+ function parseSetCookie(header) {
70
+ const segments = header.split(";");
71
+ const nameValue = segments[0];
72
+ const eqIdx = nameValue.indexOf("=");
73
+ if (eqIdx <= 0) return null;
74
+ const name = nameValue.slice(0, eqIdx).trim();
75
+ const value = nameValue.slice(eqIdx + 1).trim();
76
+ const options = {};
77
+ for (let i = 1; i < segments.length; i++) {
78
+ const seg = segments[i].trim();
79
+ if (!seg) continue;
80
+ const [attrName, ...rest] = seg.split("=");
81
+ const key = attrName.trim().toLowerCase();
82
+ const val = rest.join("=").trim();
83
+ switch (key) {
84
+ case "path":
85
+ options.path = val || "/";
86
+ break;
87
+ case "domain":
88
+ options.domain = val;
89
+ break;
90
+ case "max-age":
91
+ options.maxAge = Number(val);
92
+ break;
93
+ case "expires":
94
+ options.expires = new Date(val);
95
+ break;
96
+ case "samesite":
97
+ options.sameSite = val.toLowerCase();
98
+ break;
99
+ case "secure":
100
+ options.secure = true;
101
+ break;
102
+ case "httponly":
103
+ options.httpOnly = true;
104
+ break;
105
+ case "partitioned":
106
+ options.partitioned = true;
107
+ break;
108
+ }
109
+ }
110
+ return {
111
+ name,
112
+ value,
113
+ options
114
+ };
115
+ }
116
+ //#endregion
117
+ //#region src/server/cookie-context.ts
118
+ /**
119
+ * Cookie Context — per-request cookie API and on-the-wire helpers.
120
+ *
121
+ * Split out of `request-context.ts` (TIM-853) so the cookie subsystem
122
+ * — encoding contract, options grammar, parser, serializer, RYW map,
123
+ * and rerender seed — lives in one file. The headers/scope/params APIs
124
+ * stay in `request-context.ts` and call into this module via the
125
+ * exported helpers.
126
+ *
127
+ * See design/29-cookies.md for the encoding contract and read-your-own-
128
+ * writes semantics. See ONGOING_SECURITY.md H-3 (TIM-868) for the
129
+ * smuggling primitive that the encoding contract closes.
130
+ */
131
+ /**
132
+ * Returns a cookie accessor for the current request.
133
+ *
134
+ * Available in middleware, access checks, server components, and server actions.
135
+ * Throws if called outside a request context (security principle #2: no global fallback).
136
+ *
137
+ * Read methods (.get, .has, .getAll) are always available and reflect
138
+ * read-your-own-writes from .set() calls in the same request.
139
+ *
140
+ * Mutation methods (.set, .delete, .clear) are only available in mutable
141
+ * contexts (middleware.ts, server actions, route.ts handlers). Calling them
142
+ * in read-only contexts (access.ts, server components) throws.
143
+ *
144
+ * This is the escape hatch for direct cookie jar operations. For typed
145
+ * cookie access, use `defineCookie()` instead.
146
+ *
147
+ * See design/29-cookies.md
148
+ */
149
+ function getCookieJar() {
150
+ const store = requestContextAls.getStore();
151
+ if (!store) throw new Error("[timber] getCookieJar() called outside of a request context. It can only be used in middleware, access checks, server components, and server actions.");
152
+ if (!store.parsedCookies) store.parsedCookies = parseCookieHeader(store.cookieHeader);
153
+ const map = store.parsedCookies;
154
+ return {
155
+ get(name) {
156
+ return map.get(name);
157
+ },
158
+ has(name) {
159
+ return map.has(name);
160
+ },
161
+ getAll() {
162
+ return Array.from(map.entries()).map(([name, value]) => ({
163
+ name,
164
+ value
165
+ }));
166
+ },
167
+ get size() {
168
+ return map.size;
169
+ },
170
+ set(name, value, options) {
171
+ assertMutable(store, "set");
172
+ assertValidCookieName(name);
173
+ if (typeof value !== "string") throw new Error(`[timber] getCookieJar().set(${JSON.stringify(name)}, …): value must be a string, got ${typeof value}.\n To store a JSON-serializable value, use defineCookie + jsonCookieCodec:\n\n import { defineCookie, jsonCookieCodec } from '@timber-js/app/cookies';\n\n export const ${name}Cookie = defineCookie(${JSON.stringify(name)}, {\n codec: jsonCookieCodec(),\n });\n\n ${name}Cookie.set(value);\n\n See design/29-cookies.md §"Typed Cookies with Schema Validation".`);
174
+ const raw = options?.raw === true;
175
+ const wireValue = raw ? value : encodeURIComponent(value);
176
+ if (raw) assertValidCookieValue(name, wireValue);
177
+ if (store.flushed) {
178
+ if (isDebug()) console.warn(`[timber] warn: getCookieJar().set('${name}') called after response headers were committed.\n The cookie will NOT be sent. Move cookie mutations to middleware.ts, a server action,\n or a route.ts handler.`);
179
+ return;
180
+ }
181
+ const { raw: _raw, ...attributeOptions } = options ?? {};
182
+ const opts = {
183
+ ...DEFAULT_COOKIE_OPTIONS,
184
+ ...attributeOptions
185
+ };
186
+ store.cookieJar.set(name, {
187
+ name,
188
+ value: wireValue,
189
+ options: opts
190
+ });
191
+ map.set(name, raw ? wireValue : value);
192
+ },
193
+ setFromHeaders(headers) {
194
+ assertMutable(store, "setFromHeaders");
195
+ if (store.flushed) {
196
+ console.warn("[timber] warn: getCookieJar().setFromHeaders() called after response headers were committed.\n The cookies will NOT be sent. Move cookie mutations to middleware.ts, a server action,\n or a route.ts handler.");
197
+ return;
198
+ }
199
+ for (const raw of headers.getSetCookie()) {
200
+ const parsed = parseSetCookie(raw);
201
+ if (parsed) setRaw(store, map, parsed.name, parsed.value, parsed.options);
202
+ }
203
+ },
204
+ delete(name, options) {
205
+ assertMutable(store, "delete");
206
+ assertValidCookieName(name);
207
+ if (store.flushed) {
208
+ if (isDebug()) console.warn(`[timber] warn: getCookieJar().delete('${name}') called after response headers were committed.\n The cookie will NOT be deleted. Move cookie mutations to middleware.ts, a server action,\n or a route.ts handler.`);
209
+ return;
210
+ }
211
+ const opts = {
212
+ ...DEFAULT_COOKIE_OPTIONS,
213
+ ...options,
214
+ maxAge: 0,
215
+ expires: /* @__PURE__ */ new Date(0)
216
+ };
217
+ store.cookieJar.set(name, {
218
+ name,
219
+ value: "",
220
+ options: opts
221
+ });
222
+ map.delete(name);
223
+ },
224
+ clear() {
225
+ assertMutable(store, "clear");
226
+ if (store.flushed) return;
227
+ for (const name of Array.from(map.keys())) store.cookieJar.set(name, {
228
+ name,
229
+ value: "",
230
+ options: {
231
+ ...DEFAULT_COOKIE_OPTIONS,
232
+ maxAge: 0,
233
+ expires: /* @__PURE__ */ new Date(0)
234
+ }
235
+ });
236
+ map.clear();
237
+ },
238
+ toString() {
239
+ return Array.from(map.entries()).map(([name, value]) => `${name}=${encodeURIComponent(value)}`).join("; ");
240
+ }
241
+ };
242
+ }
243
+ /**
244
+ * Returns the value of a single cookie, or undefined if absent.
245
+ *
246
+ * @internal — not part of the public API. Use `defineCookie().get()` or `getCookieJar().get()` instead.
247
+ */
248
+ function getCookie(name) {
249
+ return getCookieJar().get(name);
250
+ }
251
+ var DEFAULT_COOKIE_OPTIONS = {
252
+ path: "/",
253
+ httpOnly: true,
254
+ secure: true,
255
+ sameSite: "lax"
256
+ };
257
+ /**
258
+ * Per-request seed map of cookie name → value, registered out-of-band so the
259
+ * pipeline's `runWithRequestContext` call can pick it up without changing
260
+ * the function's calling convention. Stored in a WeakMap keyed by the
261
+ * synthetic Request object built for the rerender path, so the seed lives
262
+ * exactly as long as the request and is collected with it.
263
+ *
264
+ * The seed exists to eliminate the parse/serialize round-trip on the no-JS
265
+ * form-rerender path — see ONGOING_SECURITY.md H-3 (TIM-868). The action's
266
+ * post-mutation RYW snapshot is threaded directly into the rerender scope's
267
+ * `parsedCookies` map, bypassing `parseCookieHeader` entirely.
268
+ */
269
+ var seededRequestCookies = /* @__PURE__ */ new WeakMap();
270
+ /**
271
+ * Pop the seed (if any) for `req` and return it. Called from
272
+ * `runWithRequestContext` exactly once per request — the seed is consumed
273
+ * eagerly so it cannot leak into a hypothetical future re-use of the same
274
+ * Request reference.
275
+ *
276
+ * @internal — framework use only.
277
+ */
278
+ function consumeSeededCookies(req) {
279
+ const seed = seededRequestCookies.get(req);
280
+ if (seed) seededRequestCookies.delete(req);
281
+ return seed;
282
+ }
283
+ /**
284
+ * Collect all Set-Cookie headers from the cookie jar.
285
+ * Called by the framework at flush time to apply cookies to the response.
286
+ *
287
+ * Returns an array of serialized Set-Cookie header values.
288
+ */
289
+ function getSetCookieHeaders() {
290
+ const store = requestContextAls.getStore();
291
+ if (!store) return [];
292
+ return Array.from(store.cookieJar.values()).map(serializeCookieEntry);
293
+ }
294
+ /** Throw if cookie mutation is attempted in a read-only context. */
295
+ function assertMutable(store, method) {
296
+ if (!store.mutableContext) throw new Error(`[timber] getCookieJar().${method}() cannot be called in this context.\n Set cookies in middleware.ts, server actions, or route.ts handlers.`);
297
+ }
298
+ /**
299
+ * Write a cookie to the jar WITHOUT merging DEFAULT_COOKIE_OPTIONS.
300
+ * Used by setFromHeaders to preserve the original header's attributes exactly.
301
+ *
302
+ * For deletion cookies (maxAge=0), the jar entry is still created so the
303
+ * Set-Cookie header is emitted, but the cookie is NOT added to the read map
304
+ * (it would be misleading — the cookie is being deleted).
305
+ */
306
+ function setRaw(store, readMap, name, value, options) {
307
+ assertValidCookieName(name);
308
+ assertValidCookieValue(name, value);
309
+ store.cookieJar.set(name, {
310
+ name,
311
+ value,
312
+ options
313
+ });
314
+ if (options.maxAge === 0) readMap.delete(name);
315
+ else readMap.set(name, safeDecodeCookieValue(value));
316
+ }
317
+ //#endregion
318
+ //#region src/server/request-context.ts
319
+ /**
320
+ * Request Context — per-request ALS store for headers, search params,
321
+ * segment params, and request scope lifecycle.
322
+ *
323
+ * Follows the same pattern as tracing.ts: a module-level AsyncLocalStorage
324
+ * instance, public accessor functions that throw outside request scope,
325
+ * and a framework-internal `runWithRequestContext()` to establish scope.
326
+ *
327
+ * Cookie state lives in `cookie-context.ts` (split out in TIM-853). The
328
+ * scope set up here owns the cookie jar / parsedCookies fields on the
329
+ * store, but the cookie API and helpers are in the cookie module.
330
+ *
331
+ * See design/04-authorization.md §"AccessContext does not include cookies or headers"
332
+ * and design/11-platform.md §"AsyncLocalStorage".
333
+ */
334
+ /**
335
+ * Returns a read-only view of the current request's headers.
336
+ *
337
+ * Available in middleware, access checks, server components, and server actions.
338
+ * Throws if called outside a request context (security principle #2: no global fallback).
339
+ */
340
+ function getHeaders() {
341
+ const store = requestContextAls.getStore();
342
+ if (!store) throw new Error("[timber] getHeaders() called outside of a request context. It can only be used in middleware, access checks, server components, and server actions.");
343
+ return store.headers;
344
+ }
345
+ /**
346
+ * Returns the value of a single request header, or undefined if absent.
347
+ *
348
+ * Thin wrapper over `getHeaders().get(name)` for the common case where
349
+ * you need exactly one header.
350
+ *
351
+ * @internal — not part of the public API. Use `getHeaders().get(name)` instead.
352
+ */
353
+ function getHeader(name) {
354
+ return getHeaders().get(name) ?? void 0;
355
+ }
356
+ /**
357
+ * Returns the current request's raw URLSearchParams.
358
+ *
359
+ * @internal — not part of the public API. Use `defineSearchParams().get()` instead.
360
+ */
361
+ function getSearchParams() {
362
+ const store = requestContextAls.getStore();
363
+ if (!store) throw new Error("[timber] getSearchParams() called outside of a request context. It can only be used in middleware, access checks, server components, and server actions.");
364
+ return store.searchParams;
365
+ }
366
+ _setGetSearchParamsFn(getSearchParams);
367
+ _setGetSegmentParamsFn(getSegmentParams);
368
+ _registerServerCookieImpl({ getCookieJar });
369
+ _registerFromSchema(fromSchema);
370
+ /**
371
+ * Returns the current request's coerced segment params.
372
+ *
373
+ * @internal — not part of the public API. Use `defineSegmentParams().get()` instead.
374
+ */
375
+ function getSegmentParams() {
376
+ const store = requestContextAls.getStore();
377
+ if (!store) throw new Error("[timber] getSegmentParams() called outside of a request context. It can only be used in middleware, access checks, server components, and server actions.");
378
+ if (!store.segmentParams) throw new Error("[timber] getSegmentParams() called before route matching completed. Segment params are not available until after the route is matched.");
379
+ return store.segmentParams;
380
+ }
381
+ /**
382
+ * Set the segment params promise on the current request context.
383
+ * Called by the pipeline after route matching and param coercion.
384
+ *
385
+ * @internal — framework use only
386
+ */
387
+ function setSegmentParams(params) {
388
+ const store = requestContextAls.getStore();
389
+ if (!store) throw new Error("[timber] setSegmentParams() called outside of a request context.");
390
+ store.segmentParams = params;
391
+ }
392
+ /**
393
+ * Returns the raw search string from the current request URL (e.g. "?foo=bar").
394
+ * Synchronous — safe for use in `redirect()` which throws synchronously.
395
+ *
396
+ * Returns empty string if called outside a request context (non-throwing for
397
+ * use in redirect's optional preserveSearchParams path).
398
+ *
399
+ * @internal — used by redirect() for preserveSearchParams support.
400
+ */
401
+ function getRequestSearchString() {
402
+ return requestContextAls.getStore()?.searchString ?? "";
403
+ }
404
+ /**
405
+ * Run a callback within a request context. Used by the pipeline to establish
406
+ * per-request ALS scope so that `getHeaders()` and `getCookies()` work.
407
+ *
408
+ * If the request was previously registered via `seedRequestCookies`, the
409
+ * resulting context's `parsedCookies` map is initialized from the seed and
410
+ * the raw `cookieHeader` is left empty — `parseCookieHeader` is never called
411
+ * for that request. This is the no-JS form-rerender path. See TIM-868.
412
+ *
413
+ * @param req - The incoming Request object.
414
+ * @param fn - The function to run within the request context.
415
+ */
416
+ function runWithRequestContext(req, fn) {
417
+ const originalCopy = new Headers(req.headers);
418
+ const parsedUrl = new URL(req.url);
419
+ const seed = consumeSeededCookies(req);
420
+ const store = {
421
+ headers: freezeHeaders(req.headers),
422
+ originalHeaders: originalCopy,
423
+ cookieHeader: seed ? "" : req.headers.get("cookie") ?? "",
424
+ parsedCookies: seed,
425
+ searchParams: parsedUrl.searchParams,
426
+ searchString: parsedUrl.search,
427
+ cookieJar: /* @__PURE__ */ new Map(),
428
+ flushed: false,
429
+ mutableContext: false
430
+ };
431
+ return requestContextAls.run(store, fn);
432
+ }
433
+ /**
434
+ * Enable cookie mutation for the current context. Called by the framework
435
+ * when entering middleware.ts, server actions, or route.ts handlers.
436
+ *
437
+ * See design/29-cookies.md §"Context Tracking"
438
+ */
439
+ function setMutableCookieContext(mutable) {
440
+ const store = requestContextAls.getStore();
441
+ if (store) store.mutableContext = mutable;
442
+ }
443
+ /**
444
+ * Mark the response as flushed (headers committed). After this point,
445
+ * cookie mutations log a warning instead of throwing.
446
+ *
447
+ * See design/29-cookies.md §"Streaming Constraint: Post-Flush Cookie Warning"
448
+ */
449
+ function markResponseFlushed() {
450
+ const store = requestContextAls.getStore();
451
+ if (store) store.flushed = true;
452
+ }
453
+ /**
454
+ * Apply middleware-injected request headers to the current request context.
455
+ *
456
+ * Called by the pipeline after middleware.ts runs. Merges overlay headers
457
+ * on top of the original request headers so downstream code (access.ts,
458
+ * server components, server actions) sees them via `getHeaders()`.
459
+ *
460
+ * The original request headers are never mutated — a new frozen Headers
461
+ * object is created with the overlay applied on top.
462
+ *
463
+ * See design/07-routing.md §"Request Header Injection"
464
+ */
465
+ function applyRequestHeaderOverlay(overlay) {
466
+ const store = requestContextAls.getStore();
467
+ if (!store) throw new Error("[timber] applyRequestHeaderOverlay() called outside of a request context.");
468
+ let hasOverlay = false;
469
+ overlay.forEach(() => {
470
+ hasOverlay = true;
471
+ });
472
+ if (!hasOverlay) return;
473
+ const merged = new Headers(store.originalHeaders);
474
+ overlay.forEach((value, key) => {
475
+ merged.set(key, value);
476
+ });
477
+ store.headers = freezeHeaders(merged);
478
+ }
479
+ var MUTATING_METHODS = new Set([
480
+ "set",
481
+ "append",
482
+ "delete"
483
+ ]);
484
+ /**
485
+ * Wrap a Headers object in a Proxy that throws on mutating methods.
486
+ * Object.freeze doesn't work on Headers (native internal slots), so we
487
+ * intercept property access and reject set/append/delete at runtime.
488
+ *
489
+ * Read methods (get, has, entries, etc.) must be bound to the underlying
490
+ * Headers instance because they access private #headersList slots.
491
+ */
492
+ function freezeHeaders(source) {
493
+ const copy = new Headers(source);
494
+ return new Proxy(copy, { get(target, prop) {
495
+ if (typeof prop === "string" && MUTATING_METHODS.has(prop)) return () => {
496
+ throw new Error(`[timber] getHeaders() returns a read-only Headers object. Calling .${prop}() is not allowed. Use ctx.requestHeaders in middleware to inject headers for downstream components.`);
497
+ };
498
+ const value = Reflect.get(target, prop);
499
+ if (typeof value === "function") return value.bind(target);
500
+ return value;
501
+ } });
502
+ }
503
+ //#endregion
504
+ //#region src/server/waituntil-bridge.ts
505
+ /**
506
+ * Per-request waitUntil bridge — ALS bridge for platform adapters.
507
+ *
508
+ * The generated entry point (Nitro, Cloudflare) wraps the handler with
509
+ * `runWithWaitUntil`, binding the platform's lifecycle extension function
510
+ * (e.g., h3's `event.waitUntil()` or CF's `ctx.waitUntil()`) for the
511
+ * request duration. The `waitUntil()` primitive reads from this ALS to
512
+ * dispatch background work to the correct platform API.
513
+ *
514
+ * Design doc: design/11-platform.md §"waitUntil()"
515
+ */
516
+ /**
517
+ * Get the current request's waitUntil function, if available.
518
+ *
519
+ * Returns undefined when no platform adapter has installed a waitUntil
520
+ * handler for the current request (e.g., on platforms that don't support
521
+ * lifecycle extension, or outside a request context).
522
+ */
523
+ function getWaitUntil() {
524
+ return waitUntilAls.getStore();
525
+ }
526
+ //#endregion
527
+ //#region src/server/primitives.ts
528
+ /**
529
+ * Check if a value is JSON-serializable without data loss.
530
+ * Returns a description of the first non-serializable value found, or null if OK.
531
+ *
532
+ * @internal Exported for testing only.
533
+ */
534
+ function findNonSerializable(value, path = "data") {
535
+ if (value === null || value === void 0) return null;
536
+ switch (typeof value) {
537
+ case "string":
538
+ case "number":
539
+ case "boolean": return null;
540
+ case "bigint": return `${path} contains a BigInt — BigInt throws in JSON.stringify`;
541
+ case "function": return `${path} is a function — functions are not JSON-serializable`;
542
+ case "symbol": return `${path} is a symbol — symbols are not JSON-serializable`;
543
+ case "object": break;
544
+ default: return `${path} has unsupported type "${typeof value}"`;
545
+ }
546
+ if (value instanceof Date) return `${path} is a Date — Dates silently coerce to strings in JSON.stringify`;
547
+ if (value instanceof Map) return `${path} is a Map — Maps serialize as {} in JSON.stringify (data loss)`;
548
+ if (value instanceof Set) return `${path} is a Set — Sets serialize as {} in JSON.stringify (data loss)`;
549
+ if (value instanceof RegExp) return `${path} is a RegExp — RegExps serialize as {} in JSON.stringify`;
550
+ if (value instanceof Error) return `${path} is an Error — Errors serialize as {} in JSON.stringify`;
551
+ if (Array.isArray(value)) {
552
+ for (let i = 0; i < value.length; i++) {
553
+ const result = findNonSerializable(value[i], `${path}[${i}]`);
554
+ if (result) return result;
555
+ }
556
+ return null;
557
+ }
558
+ const proto = Object.getPrototypeOf(value);
559
+ if (proto === null) return `${path} is a null-prototype object — React Flight rejects null prototypes`;
560
+ if (proto !== Object.prototype) return `${path} is a ${value.constructor?.name ?? "unknown"} instance — class instances may lose data in JSON.stringify`;
561
+ for (const key of Object.keys(value)) {
562
+ const result = findNonSerializable(value[key], `${path}.${key}`);
563
+ if (result) return result;
564
+ }
565
+ return null;
566
+ }
567
+ /**
568
+ * Emit a dev-mode warning if data is not JSON-serializable.
569
+ * No-op in production.
570
+ */
571
+ function warnIfNotSerializable(data, callerName) {
572
+ if (!isDebug()) return;
573
+ if (data === void 0) return;
574
+ const issue = findNonSerializable(data);
575
+ if (issue) console.warn(`[timber] ${callerName}: ${issue}. Data passed to deny() or RenderError must be JSON-serializable because the post-flush path uses JSON.stringify, not React Flight.`);
576
+ }
577
+ /**
578
+ * Render-phase signal thrown by `deny()`. Caught by the framework to produce
579
+ * the correct HTTP status code (segment context) or graceful degradation (slot context).
580
+ */
581
+ var DenySignal = class extends Error {
582
+ status;
583
+ data;
584
+ constructor(status, data) {
585
+ super(`Access denied with status ${status}`);
586
+ this.name = "DenySignal";
587
+ this.status = status;
588
+ this.data = data;
589
+ }
590
+ /**
591
+ * Extract the file that called deny() from the stack trace.
592
+ * Returns a short path (e.g. "app/auth/access.ts") or undefined if
593
+ * the stack can't be parsed. Dev-only — used for dev log output.
594
+ */
595
+ get sourceFile() {
596
+ if (!this.stack) return void 0;
597
+ const frames = this.stack.split("\n");
598
+ for (let i = 2; i < frames.length; i++) {
599
+ const frame = frames[i];
600
+ if (!frame) continue;
601
+ if (frame.includes("primitives.ts") || frame.includes("node_modules")) continue;
602
+ const match = frame.match(/\(([^)]+?)(?::\d+:\d+)\)/) ?? frame.match(/at\s+([^\s]+?)(?::\d+:\d+)/);
603
+ if (match?.[1]) {
604
+ const full = match[1];
605
+ const appIdx = full.indexOf("/app/");
606
+ return appIdx >= 0 ? full.slice(appIdx + 1) : full;
607
+ }
608
+ }
609
+ }
610
+ };
611
+ /**
612
+ * Universal denial/error primitive. Throws a `DenySignal` that the framework catches.
613
+ *
614
+ * - In segment context (outside Suspense): produces HTTP status code
615
+ * - In slot context: graceful degradation → denied.tsx → default.tsx → null
616
+ * - Inside Suspense (hold window): promoted to pre-flush behavior
617
+ * - Inside Suspense (after flush): error boundary + noindex meta
618
+ *
619
+ * Supports both positional and object signatures:
620
+ * ```ts
621
+ * deny() // 403 (default)
622
+ * deny(404) // 404
623
+ * deny(503, { retry: true }) // 503 with data
624
+ * deny({ status: 503, message: 'Maintenance' }) // object form
625
+ * ```
626
+ *
627
+ * Accepts any 4xx or 5xx status code. This replaces the need for
628
+ * `throw new RenderError(...)` in user code — RenderError is now an
629
+ * internal pipeline detail.
630
+ *
631
+ * @param statusOrOptions - Status code (number) or options object. Default: 403.
632
+ * @param data - Optional JSON-serializable data (positional form only).
633
+ */
634
+ function deny(statusOrOptions, data) {
635
+ let status;
636
+ let resolvedData;
637
+ if (typeof statusOrOptions === "object" && statusOrOptions !== null) {
638
+ status = statusOrOptions.status ?? 403;
639
+ resolvedData = statusOrOptions.data;
640
+ } else {
641
+ status = statusOrOptions ?? 403;
642
+ resolvedData = data;
643
+ }
644
+ if (status < 400 || status > 599) throw new Error(`deny() requires a 4xx or 5xx status code, got ${status}.`);
645
+ warnIfNotSerializable(resolvedData, "deny()");
646
+ throw new DenySignal(status, resolvedData);
647
+ }
648
+ /**
649
+ * Next.js redirect type discriminator.
650
+ *
651
+ * Provided for API compatibility with libraries that import `RedirectType`
652
+ * from `next/navigation`. In timber, `redirect()` always uses `replace`
653
+ * semantics (no history entry for the redirect itself).
654
+ */
655
+ var RedirectType = {
656
+ push: "push",
657
+ replace: "replace"
658
+ };
659
+ /**
660
+ * Render-phase signal thrown by `redirect()` and `redirectExternal()`.
661
+ * Caught by the framework to produce a 3xx response or client-side navigation.
662
+ */
663
+ var RedirectSignal = class extends Error {
664
+ location;
665
+ status;
666
+ constructor(location, status) {
667
+ super(`Redirect to ${location}`);
668
+ this.name = "RedirectSignal";
669
+ this.location = location;
670
+ this.status = status;
671
+ }
672
+ };
673
+ /** Pattern matching absolute URLs: http(s):// or protocol-relative // */
674
+ var ABSOLUTE_URL_RE = /^(?:[a-zA-Z][a-zA-Z\d+\-.]*:|\/\/)/;
675
+ /**
676
+ * Redirect to a relative path. Rejects absolute and protocol-relative URLs.
677
+ * Use `redirectExternal()` for external redirects with an allow-list.
678
+ *
679
+ * @param path - Relative path (e.g. '/login', 'settings', '/login?returnTo=/dash')
680
+ * @param statusOrOptions - HTTP status code (3xx, default 302) or options object.
681
+ *
682
+ * @example
683
+ * // Simple redirect
684
+ * redirect('/login');
685
+ *
686
+ * // With status code
687
+ * redirect('/login', 301);
688
+ *
689
+ * // With preserved search params
690
+ * redirect(`/docs/${version}/${slug}`, { preserveSearchParams: ['foo'] });
691
+ */
692
+ function redirect(path, statusOrOptions) {
693
+ let status;
694
+ let preserveSearchParams;
695
+ if (typeof statusOrOptions === "number") status = statusOrOptions;
696
+ else if (statusOrOptions) {
697
+ status = statusOrOptions.status ?? (statusOrOptions.permanent ? 308 : 302);
698
+ preserveSearchParams = statusOrOptions.preserveSearchParams;
699
+ } else status = 302;
700
+ if (status < 300 || status > 399) throw new Error(`redirect() requires a 3xx status code, got ${status}.`);
701
+ if (ABSOLUTE_URL_RE.test(path)) throw new Error(`redirect() only accepts relative URLs. Got absolute URL: "${path}". Use redirectExternal(url, allowList) for external redirects.`);
702
+ let resolvedPath = path;
703
+ if (preserveSearchParams) resolvedPath = mergePreservedSearchParams(path, getRequestSearchString(), preserveSearchParams);
704
+ throw new RedirectSignal(resolvedPath, status);
705
+ }
706
+ /**
707
+ * Redirect to an external URL. The hostname must be in the provided allow-list.
708
+ *
709
+ * @param url - Absolute URL to redirect to.
710
+ * @param allowList - Array of allowed hostnames (e.g. ['example.com', 'auth.example.com']).
711
+ * @param status - HTTP redirect status code (3xx). Defaults to 302.
712
+ */
713
+ function redirectExternal(url, allowList, status = 302) {
714
+ if (status < 300 || status > 399) throw new Error(`redirectExternal() requires a 3xx status code, got ${status}.`);
715
+ let hostname;
716
+ try {
717
+ hostname = new URL(url).hostname;
718
+ } catch {
719
+ throw new Error(`redirectExternal() received an invalid URL: "${url}"`);
720
+ }
721
+ if (!allowList.includes(hostname)) throw new Error(`redirectExternal() target "${hostname}" is not in the allow-list. Allowed: [${allowList.join(", ")}]`);
722
+ throw new RedirectSignal(url, status);
723
+ }
724
+ /**
725
+ * Typed throw for render-phase errors that carry structured context to error boundaries.
726
+ *
727
+ * The `digest` (code + data) is serialized into the RSC stream separately from the
728
+ * Error instance — only the digest crosses the RSC → client boundary.
729
+ *
730
+ * @example
731
+ * ```ts
732
+ * throw new RenderError('PRODUCT_NOT_FOUND', {
733
+ * title: 'Product not found',
734
+ * resourceId: params.id,
735
+ * })
736
+ * ```
737
+ */
738
+ var RenderError = class extends Error {
739
+ code;
740
+ digest;
741
+ status;
742
+ constructor(code, data, options) {
743
+ super(`RenderError: ${code}`);
744
+ this.name = "RenderError";
745
+ this.code = code;
746
+ this.digest = {
747
+ code,
748
+ data
749
+ };
750
+ warnIfNotSerializable(data, "RenderError");
751
+ const status = options?.status ?? 500;
752
+ if (status < 400 || status > 599) throw new Error(`RenderError status must be 4xx or 5xx, got ${status}.`);
753
+ this.status = status;
754
+ }
755
+ };
756
+ var _waitUntilWarned = false;
757
+ /**
758
+ * Register a promise to be kept alive after the response is sent.
759
+ * Maps to `ctx.waitUntil()` on Cloudflare Workers and similar platforms.
760
+ *
761
+ * In production, the platform adapter installs a per-request waitUntil
762
+ * function via ALS (see waituntil-bridge.ts). This function checks the
763
+ * ALS bridge first, then falls back to the legacy adapter argument.
764
+ *
765
+ * If neither is available, a warning is logged once and the promise is
766
+ * left to resolve (or reject) without being tracked.
767
+ *
768
+ * @param promise - The background work to keep alive.
769
+ * @param adapter - Optional legacy adapter (prefer ALS bridge in production).
770
+ */
771
+ function waitUntil(promise, adapter) {
772
+ const alsFn = getWaitUntil();
773
+ if (alsFn) {
774
+ alsFn(promise);
775
+ return;
776
+ }
777
+ if (adapter && typeof adapter.waitUntil === "function") {
778
+ adapter.waitUntil(promise);
779
+ return;
780
+ }
781
+ if (!_waitUntilWarned) {
782
+ _waitUntilWarned = true;
783
+ console.warn("[timber] waitUntil() is not supported by the current adapter. Background work will not be tracked. This warning is shown once.");
784
+ }
785
+ }
786
+ //#endregion
787
+ //#region src/server/form-data.ts
788
+ /**
789
+ * FormData preprocessing — schema-agnostic conversion of FormData to typed objects.
790
+ *
791
+ * FormData is all strings. Schema validation expects typed values. This module
792
+ * bridges the gap with intelligent coercion that runs *before* schema validation.
793
+ *
794
+ * Inspired by zod-form-data, but schema-agnostic — works with any Standard Schema
795
+ * library (Zod, Valibot, ArkType).
796
+ *
797
+ * See design/08-forms-and-actions.md §"parseFormData() and coerce helpers"
798
+ */
799
+ /**
800
+ * Convert FormData into a plain object with intelligent coercion.
801
+ *
802
+ * Handles:
803
+ * - **Duplicate keys → arrays**: `tags=js&tags=ts` → `{ tags: ["js", "ts"] }`
804
+ * - **Nested dot-paths**: `user.name=Alice` → `{ user: { name: "Alice" } }`
805
+ * - **Empty strings → undefined**: Enables `.optional()` semantics in schemas
806
+ * - **Empty Files → undefined**: File inputs with no selection become `undefined`
807
+ * - **Strips `$ACTION_*` fields**: React's internal hidden fields are excluded
808
+ */
809
+ function parseFormData(formData) {
810
+ const flat = {};
811
+ for (const key of new Set(formData.keys())) {
812
+ if (key.startsWith("$ACTION_")) continue;
813
+ const processed = formData.getAll(key).map(normalizeValue);
814
+ if (processed.length === 1) flat[key] = processed[0];
815
+ else flat[key] = processed.filter((v) => v !== void 0);
816
+ }
817
+ return expandDotPaths(flat);
818
+ }
819
+ /**
820
+ * Normalize a single FormData entry value.
821
+ * - Empty strings → undefined (enables .optional() semantics)
822
+ * - Empty File objects (no selection) → undefined
823
+ * - Everything else passes through as-is
824
+ */
825
+ function normalizeValue(value) {
826
+ if (typeof value === "string") return value === "" ? void 0 : value;
827
+ if (value instanceof File && value.size === 0 && value.name === "") return;
828
+ return value;
829
+ }
830
+ /**
831
+ * Expand dot-notation keys into nested objects.
832
+ * `{ "user.name": "Alice", "user.age": "30" }` → `{ user: { name: "Alice", age: "30" } }`
833
+ *
834
+ * Keys without dots are left as-is. Bracket notation (e.g. `items[0]`) is NOT
835
+ * supported — use dot notation (`items.0`) instead.
836
+ */
837
+ function expandDotPaths(flat) {
838
+ const result = {};
839
+ let hasDotPaths = false;
840
+ for (const key of Object.keys(flat)) if (key.includes(".")) {
841
+ hasDotPaths = true;
842
+ break;
843
+ }
844
+ if (!hasDotPaths) return flat;
845
+ for (const [key, value] of Object.entries(flat)) {
846
+ if (!key.includes(".")) {
847
+ result[key] = value;
848
+ continue;
849
+ }
850
+ const parts = key.split(".");
851
+ let current = result;
852
+ for (let i = 0; i < parts.length - 1; i++) {
853
+ const part = parts[i];
854
+ if (current[part] === void 0 || current[part] === null) current[part] = {};
855
+ if (typeof current[part] !== "object" || current[part] instanceof File) current[part] = {};
856
+ current = current[part];
857
+ }
858
+ current[parts[parts.length - 1]] = value;
859
+ }
860
+ return result;
861
+ }
862
+ /**
863
+ * Schema-agnostic coercion primitives for common FormData patterns.
864
+ *
865
+ * These are plain transform functions — they compose with any schema library's
866
+ * `transform`/`preprocess` pipeline:
867
+ *
868
+ * ```ts
869
+ * // Zod
870
+ * z.preprocess(coerce.number, z.number())
871
+ * // Valibot
872
+ * v.pipe(v.unknown(), v.transform(coerce.number), v.number())
873
+ * ```
874
+ */
875
+ var coerce = {
876
+ number(value) {
877
+ if (value === void 0 || value === null || value === "") return void 0;
878
+ if (typeof value === "number") return value;
879
+ if (typeof value !== "string") return void 0;
880
+ const num = Number(value);
881
+ if (Number.isNaN(num)) return void 0;
882
+ return num;
883
+ },
884
+ checkbox(value) {
885
+ if (value === void 0 || value === null || value === "") return false;
886
+ if (typeof value === "boolean") return value;
887
+ return typeof value === "string" && value.length > 0;
888
+ },
889
+ json(value) {
890
+ if (value === void 0 || value === null || value === "") return void 0;
891
+ if (typeof value !== "string") return value;
892
+ try {
893
+ return JSON.parse(value);
894
+ } catch {
895
+ return;
896
+ }
897
+ },
898
+ date(value) {
899
+ if (value === void 0 || value === null || value === "") return void 0;
900
+ if (value instanceof Date) return value;
901
+ if (typeof value !== "string") return void 0;
902
+ const date = new Date(value);
903
+ if (Number.isNaN(date.getTime())) return void 0;
904
+ const ymdMatch = value.match(/^(\d{4})-(\d{2})-(\d{2})/);
905
+ if (ymdMatch) {
906
+ const inputYear = Number(ymdMatch[1]);
907
+ const inputMonth = Number(ymdMatch[2]);
908
+ const inputDay = Number(ymdMatch[3]);
909
+ const isUTC = value.length === 10 || value.endsWith("Z");
910
+ const parsedYear = isUTC ? date.getUTCFullYear() : date.getFullYear();
911
+ const parsedMonth = isUTC ? date.getUTCMonth() + 1 : date.getMonth() + 1;
912
+ const parsedDay = isUTC ? date.getUTCDate() : date.getDate();
913
+ if (inputYear !== parsedYear || inputMonth !== parsedMonth || inputDay !== parsedDay) return;
914
+ }
915
+ return date;
916
+ },
917
+ file(options) {
918
+ return (value) => {
919
+ if (value === void 0 || value === null || value === "") return void 0;
920
+ if (!(value instanceof File)) return void 0;
921
+ if (value.size === 0 && value.name === "") return void 0;
922
+ if (options?.maxSize !== void 0 && value.size > options.maxSize) return;
923
+ if (options?.accept !== void 0 && !options.accept.includes(value.type)) return;
924
+ return value;
925
+ };
926
+ }
927
+ };
928
+ //#endregion
929
+ //#region src/server/actions.ts
930
+ /**
931
+ * Server action primitives: revalidatePath, revalidateTag, and the action handler.
932
+ *
933
+ * - revalidatePath(path) re-renders the route at that path and returns the RSC
934
+ * flight payload for inline reconciliation.
935
+ * - revalidateTag(tag) invalidates timber.cache entries by tag.
936
+ *
937
+ * Both are callable from anywhere on the server — actions, API routes, handlers.
938
+ *
939
+ * The action handler processes incoming action requests, validates CSRF,
940
+ * enforces body limits, executes the action, and returns the response
941
+ * (with piggybacked RSC payload if revalidatePath was called).
942
+ *
943
+ * See design/08-forms-and-actions.md
944
+ */
945
+ /**
946
+ * Get the current revalidation state. Throws if called outside an action context.
947
+ * @internal
948
+ */
949
+ function getRevalidationState() {
950
+ const state = revalidationAls.getStore();
951
+ if (!state) throw new Error("revalidatePath/revalidateTag called outside of a server action context. These functions can only be called during action execution.");
952
+ return state;
953
+ }
954
+ /**
955
+ * Re-render the route at `path` and include the RSC flight payload in the
956
+ * action response. The client reconciles inline — no separate fetch needed.
957
+ *
958
+ * Can be called from server actions, API routes, or any server-side context.
959
+ *
960
+ * @param path - The path to re-render (e.g. '/dashboard', '/todos').
961
+ */
962
+ function revalidatePath(path) {
963
+ const state = getRevalidationState();
964
+ if (!state.paths.includes(path)) state.paths.push(path);
965
+ }
966
+ /**
967
+ * Invalidate all timber.cache entries tagged with `tag`.
968
+ * Does not return a payload — the next request for an invalidated entry re-executes.
969
+ *
970
+ * @param tag - The cache tag to invalidate (e.g. 'products', 'user:123').
971
+ */
972
+ function revalidateTag(tag) {
973
+ const state = getRevalidationState();
974
+ if (!state.tags.includes(tag)) state.tags.push(tag);
975
+ }
976
+ /**
977
+ * Execute a server action and process revalidation.
978
+ *
979
+ * 1. Sets up revalidation state
980
+ * 2. Calls the action function
981
+ * 3. Processes revalidateTag calls (invalidates cache entries)
982
+ * 4. Processes revalidatePath calls (re-renders and captures RSC payload)
983
+ * 5. Returns the action result + optional RSC payload
984
+ *
985
+ * @param actionFn - The server action function to execute.
986
+ * @param args - Arguments to pass to the action.
987
+ * @param config - Handler configuration (cache handler, renderer).
988
+ */
989
+ async function executeAction(actionFn, args, config = {}, spanMeta) {
990
+ const state = {
991
+ paths: [],
992
+ tags: []
993
+ };
994
+ let actionResult;
995
+ let redirectTo;
996
+ let redirectStatus;
997
+ await revalidationAls.run(state, async () => {
998
+ try {
999
+ actionResult = await withSpan("timber.action", {
1000
+ ...spanMeta?.actionFile ? { "timber.action_file": spanMeta.actionFile } : {},
1001
+ ...spanMeta?.actionName ? { "timber.action_name": spanMeta.actionName } : {}
1002
+ }, () => actionFn(...args));
1003
+ } catch (error) {
1004
+ if (error instanceof RedirectSignal) {
1005
+ redirectTo = error.location;
1006
+ redirectStatus = error.status;
1007
+ } else throw error;
1008
+ }
1009
+ });
1010
+ if (state.tags.length > 0) {
1011
+ const handler = getCacheHandler();
1012
+ await Promise.all(state.tags.map((tag) => handler.invalidate({ tag })));
1013
+ }
1014
+ let revalidation;
1015
+ if (state.paths.length > 0 && config.renderer) {
1016
+ const path = state.paths[0];
1017
+ try {
1018
+ revalidation = await config.renderer(path);
1019
+ } catch (renderError) {
1020
+ if (renderError instanceof RedirectSignal) {
1021
+ redirectTo = renderError.location;
1022
+ redirectStatus = renderError.status;
1023
+ } else console.error("[timber] revalidatePath render failed:", renderError);
1024
+ }
1025
+ }
1026
+ return {
1027
+ actionResult,
1028
+ revalidation,
1029
+ ...redirectTo ? {
1030
+ redirectTo,
1031
+ redirectStatus
1032
+ } : {}
1033
+ };
1034
+ }
1035
+ /**
1036
+ * Build an HTTP Response for a no-JS form submission.
1037
+ * Standard POST → 302 redirect pattern.
1038
+ *
1039
+ * @param redirectPath - Where to redirect after the action executes.
1040
+ */
1041
+ function buildNoJsResponse(redirectPath, status = 302) {
1042
+ return new Response(null, {
1043
+ status,
1044
+ headers: { Location: redirectPath }
1045
+ });
1046
+ }
1047
+ /**
1048
+ * Detect whether the incoming request is an RSC action request (with JS)
1049
+ * or a plain HTML form POST (no JS).
1050
+ *
1051
+ * RSC action requests use Accept: text/x-component or Content-Type: text/x-component.
1052
+ */
1053
+ function isRscActionRequest(req) {
1054
+ const accept = req.headers.get("Accept") ?? "";
1055
+ const contentType = req.headers.get("Content-Type") ?? "";
1056
+ return accept.includes("text/x-component") || contentType.includes("text/x-component");
1057
+ }
1058
+ //#endregion
1059
+ export { setMutableCookieContext as C, getSetCookieHeaders as D, getCookieJar as E, runWithRequestContext as S, getCookie as T, getHeader as _, revalidateTag as a, getSegmentParams as b, DenySignal as c, RenderError as d, deny as f, applyRequestHeaderOverlay as g, waitUntil as h, revalidatePath as i, RedirectSignal as l, redirectExternal as m, executeAction as n, coerce as o, redirect as p, isRscActionRequest as r, parseFormData as s, buildNoJsResponse as t, RedirectType as u, getHeaders as v, setSegmentParams as w, markResponseFlushed as x, getSearchParams as y };
1060
+
1061
+ //# sourceMappingURL=actions-CQ8Z8VGL.js.map