@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.
- package/LICENSE +8 -0
- package/dist/_chunks/actions-CQ8Z8VGL.js +1061 -0
- package/dist/_chunks/actions-CQ8Z8VGL.js.map +1 -0
- package/dist/_chunks/build-output-helper-DXnW0qjz.js +61 -0
- package/dist/_chunks/build-output-helper-DXnW0qjz.js.map +1 -0
- package/dist/_chunks/{define-Itxvcd7F.js → define-B-Q_UMOD.js} +19 -23
- package/dist/_chunks/define-B-Q_UMOD.js.map +1 -0
- package/dist/_chunks/{define-C77ScO0m.js → define-CfBPoJb0.js} +24 -7
- package/dist/_chunks/define-CfBPoJb0.js.map +1 -0
- package/dist/_chunks/define-cookie-BjpIt4UC.js +194 -0
- package/dist/_chunks/define-cookie-BjpIt4UC.js.map +1 -0
- package/dist/_chunks/{format-CYBGxKtc.js → format-Bcn-Iv1x.js} +1 -1
- package/dist/_chunks/{format-CYBGxKtc.js.map → format-Bcn-Iv1x.js.map} +1 -1
- package/dist/_chunks/handler-store-B-lqaGyh.js +54 -0
- package/dist/_chunks/handler-store-B-lqaGyh.js.map +1 -0
- package/dist/_chunks/logger-0m8MsKdc.js +291 -0
- package/dist/_chunks/logger-0m8MsKdc.js.map +1 -0
- package/dist/_chunks/merge-search-params-BphMdht_.js +122 -0
- package/dist/_chunks/merge-search-params-BphMdht_.js.map +1 -0
- package/dist/_chunks/navigation-root-BCYczjml.js +96 -0
- package/dist/_chunks/navigation-root-BCYczjml.js.map +1 -0
- package/dist/_chunks/registry-I2ss-lvy.js +20 -0
- package/dist/_chunks/registry-I2ss-lvy.js.map +1 -0
- package/dist/_chunks/router-ref-h3-UaCQv.js +28 -0
- package/dist/_chunks/router-ref-h3-UaCQv.js.map +1 -0
- package/dist/_chunks/{schema-bridge-C3xl_vfb.js → schema-bridge-Cxu4l-7p.js} +1 -1
- package/dist/_chunks/{schema-bridge-C3xl_vfb.js.map → schema-bridge-Cxu4l-7p.js.map} +1 -1
- package/dist/_chunks/{segment-context-fHFLF1PE.js → segment-context-Dx_OizxD.js} +1 -1
- package/dist/_chunks/{segment-context-fHFLF1PE.js.map → segment-context-Dx_OizxD.js.map} +1 -1
- package/dist/_chunks/{router-ref-C8OCm7g7.js → ssr-data-B4CdH7rE.js} +2 -26
- package/dist/_chunks/ssr-data-B4CdH7rE.js.map +1 -0
- package/dist/_chunks/{stale-reload-BX5gL1r-.js → stale-reload-Bab885FO.js} +1 -1
- package/dist/_chunks/{stale-reload-BX5gL1r-.js.map → stale-reload-Bab885FO.js.map} +1 -1
- package/dist/_chunks/tracing-C8V-YGsP.js +329 -0
- package/dist/_chunks/tracing-C8V-YGsP.js.map +1 -0
- package/dist/_chunks/{use-query-states-BiV5GJgm.js → use-query-states-B2XTqxDR.js} +3 -19
- package/dist/_chunks/use-query-states-B2XTqxDR.js.map +1 -0
- package/dist/_chunks/{use-params-IOPu7E8t.js → use-segment-params-BkpKAQ7D.js} +9 -95
- package/dist/_chunks/use-segment-params-BkpKAQ7D.js.map +1 -0
- package/dist/_chunks/{walkers-VOXgavMF.js → walkers-Tg0Alwcg.js} +6 -3
- package/dist/_chunks/walkers-Tg0Alwcg.js.map +1 -0
- package/dist/_chunks/{dev-warnings-DpGRGoDi.js → warnings-Cg47l5sk.js} +3 -3
- package/dist/_chunks/warnings-Cg47l5sk.js.map +1 -0
- package/dist/adapters/build-output-helper.d.ts +28 -0
- package/dist/adapters/build-output-helper.d.ts.map +1 -0
- package/dist/adapters/cloudflare.d.ts.map +1 -1
- package/dist/adapters/cloudflare.js +8 -28
- package/dist/adapters/cloudflare.js.map +1 -1
- package/dist/adapters/nitro.d.ts.map +1 -1
- package/dist/adapters/nitro.js +8 -26
- package/dist/adapters/nitro.js.map +1 -1
- package/dist/adapters/shared.d.ts +16 -0
- package/dist/adapters/shared.d.ts.map +1 -0
- package/dist/cache/index.js +9 -2
- package/dist/cache/index.js.map +1 -1
- package/dist/cache/timber-cache.d.ts.map +1 -1
- package/dist/client/error-boundary.js +2 -1
- package/dist/client/error-boundary.js.map +1 -1
- package/dist/client/form.d.ts +10 -24
- package/dist/client/form.d.ts.map +1 -1
- package/dist/client/index.d.ts +1 -5
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +40 -90
- package/dist/client/index.js.map +1 -1
- package/dist/client/internal.d.ts +2 -1
- package/dist/client/internal.d.ts.map +1 -1
- package/dist/client/internal.js +81 -7
- package/dist/client/internal.js.map +1 -1
- package/dist/client/rsc-fetch.d.ts.map +1 -1
- package/dist/client/state.d.ts +1 -1
- package/dist/client/use-cookie.d.ts +8 -0
- package/dist/client/use-cookie.d.ts.map +1 -1
- package/dist/client/{use-params.d.ts → use-segment-params.d.ts} +1 -1
- package/dist/client/use-segment-params.d.ts.map +1 -0
- package/dist/codec.d.ts +1 -1
- package/dist/codec.d.ts.map +1 -1
- package/dist/codec.js +2 -2
- package/dist/config-types.d.ts +28 -0
- package/dist/config-types.d.ts.map +1 -1
- package/dist/cookies/define-cookie.d.ts +87 -35
- package/dist/cookies/define-cookie.d.ts.map +1 -1
- package/dist/cookies/index.d.ts +2 -1
- package/dist/cookies/index.d.ts.map +1 -1
- package/dist/cookies/index.js +48 -2
- package/dist/cookies/index.js.map +1 -0
- package/dist/cookies/json-cookie.d.ts +64 -0
- package/dist/cookies/json-cookie.d.ts.map +1 -0
- package/dist/cookies/validation.d.ts +46 -0
- package/dist/cookies/validation.d.ts.map +1 -0
- package/dist/{plugins/dev-404-page.d.ts → dev-tools/404-page.d.ts} +1 -1
- package/dist/dev-tools/404-page.d.ts.map +1 -0
- package/dist/{plugins/dev-browser-logs.d.ts → dev-tools/browser-logs.d.ts} +1 -1
- package/dist/dev-tools/browser-logs.d.ts.map +1 -0
- package/dist/{plugins/dev-error-page.d.ts → dev-tools/error-page.d.ts} +2 -2
- package/dist/dev-tools/error-page.d.ts.map +1 -0
- package/dist/{server/dev-holding-server.d.ts → dev-tools/holding-server.d.ts} +1 -1
- package/dist/dev-tools/holding-server.d.ts.map +1 -0
- package/dist/dev-tools/index.d.ts +31 -0
- package/dist/dev-tools/index.d.ts.map +1 -0
- package/dist/{server/dev-span-processor.d.ts → dev-tools/instrumentation.d.ts} +26 -6
- package/dist/dev-tools/instrumentation.d.ts.map +1 -0
- package/dist/{server/dev-logger.d.ts → dev-tools/logger.d.ts} +1 -1
- package/dist/dev-tools/logger.d.ts.map +1 -0
- package/dist/{plugins/dev-logs.d.ts → dev-tools/logs.d.ts} +1 -1
- package/dist/dev-tools/logs.d.ts.map +1 -0
- package/dist/{plugins/dev-error-overlay.d.ts → dev-tools/overlay.d.ts} +3 -12
- package/dist/dev-tools/overlay.d.ts.map +1 -0
- package/dist/dev-tools/stack-classifier.d.ts +34 -0
- package/dist/dev-tools/stack-classifier.d.ts.map +1 -0
- package/dist/{plugins/dev-terminal-error.d.ts → dev-tools/terminal.d.ts} +2 -2
- package/dist/dev-tools/terminal.d.ts.map +1 -0
- package/dist/{server/dev-warnings.d.ts → dev-tools/warnings.d.ts} +1 -1
- package/dist/dev-tools/warnings.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +97 -72
- package/dist/index.js.map +1 -1
- package/dist/plugin-context.d.ts +1 -1
- package/dist/plugin-context.d.ts.map +1 -1
- package/dist/plugins/adapter-build.d.ts.map +1 -1
- package/dist/routing/convention-lint.d.ts.map +1 -1
- package/dist/routing/index.js +1 -1
- package/dist/routing/scanner.d.ts.map +1 -1
- package/dist/routing/status-file-lint.d.ts.map +1 -1
- package/dist/search-params/define.d.ts +25 -7
- package/dist/search-params/define.d.ts.map +1 -1
- package/dist/search-params/index.js +5 -3
- package/dist/search-params/index.js.map +1 -1
- package/dist/search-params/wrappers.d.ts +2 -2
- package/dist/search-params/wrappers.d.ts.map +1 -1
- package/dist/segment-params/define.d.ts +23 -6
- package/dist/segment-params/define.d.ts.map +1 -1
- package/dist/segment-params/index.js +1 -1
- package/dist/server/access-gate.d.ts +4 -3
- package/dist/server/access-gate.d.ts.map +1 -1
- package/dist/server/action-handler.d.ts +15 -6
- package/dist/server/action-handler.d.ts.map +1 -1
- package/dist/server/als-registry.d.ts +5 -5
- package/dist/server/als-registry.d.ts.map +1 -1
- package/dist/server/asset-headers.d.ts +1 -15
- package/dist/server/asset-headers.d.ts.map +1 -1
- package/dist/server/cookie-context.d.ts +170 -0
- package/dist/server/cookie-context.d.ts.map +1 -0
- package/dist/server/cookie-parsing.d.ts +51 -0
- package/dist/server/cookie-parsing.d.ts.map +1 -0
- package/dist/server/deny-boundary.d.ts +90 -0
- package/dist/server/deny-boundary.d.ts.map +1 -0
- package/dist/server/deny-renderer.d.ts.map +1 -1
- package/dist/server/early-hints-sender.d.ts.map +1 -1
- package/dist/server/index.d.ts +5 -4
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +4 -149
- package/dist/server/index.js.map +1 -1
- package/dist/server/internal.d.ts +6 -4
- package/dist/server/internal.d.ts.map +1 -1
- package/dist/server/internal.js +261 -408
- package/dist/server/internal.js.map +1 -1
- package/dist/server/logger.d.ts +14 -0
- package/dist/server/logger.d.ts.map +1 -1
- package/dist/server/middleware-runner.d.ts +17 -0
- package/dist/server/middleware-runner.d.ts.map +1 -1
- package/dist/server/param-coercion.d.ts +26 -0
- package/dist/server/param-coercion.d.ts.map +1 -0
- package/dist/server/pipeline-helpers.d.ts +14 -7
- package/dist/server/pipeline-helpers.d.ts.map +1 -1
- package/dist/server/pipeline-outcome.d.ts +49 -0
- package/dist/server/pipeline-outcome.d.ts.map +1 -0
- package/dist/server/pipeline-phases.d.ts +4 -49
- package/dist/server/pipeline-phases.d.ts.map +1 -1
- package/dist/server/pipeline.d.ts +0 -2
- package/dist/server/pipeline.d.ts.map +1 -1
- package/dist/server/request-context.d.ts +22 -159
- package/dist/server/request-context.d.ts.map +1 -1
- package/dist/server/route-element-builder.d.ts.map +1 -1
- package/dist/server/rsc-entry/action-middleware-runner.d.ts +66 -0
- package/dist/server/rsc-entry/action-middleware-runner.d.ts.map +1 -0
- package/dist/server/rsc-entry/helpers.d.ts +1 -1
- package/dist/server/rsc-entry/helpers.d.ts.map +1 -1
- package/dist/server/rsc-entry/index.d.ts.map +1 -1
- package/dist/server/rsc-entry/render-route.d.ts +50 -0
- package/dist/server/rsc-entry/render-route.d.ts.map +1 -0
- package/dist/server/rsc-entry/wrap-action-dispatch.d.ts +59 -14
- package/dist/server/rsc-entry/wrap-action-dispatch.d.ts.map +1 -1
- package/dist/server/state-tree-diff.d.ts.map +1 -1
- package/dist/server/tracing.d.ts +1 -1
- package/dist/server/tracing.d.ts.map +1 -1
- package/dist/server/tree-builder.d.ts +45 -16
- package/dist/server/tree-builder.d.ts.map +1 -1
- package/dist/server/types.d.ts +48 -0
- package/dist/server/types.d.ts.map +1 -1
- package/dist/server/utils/escape-html.d.ts +14 -0
- package/dist/server/utils/escape-html.d.ts.map +1 -0
- package/dist/shims/headers.d.ts +2 -2
- package/dist/shims/headers.d.ts.map +1 -1
- package/dist/shims/navigation-client.d.ts +3 -1
- package/dist/shims/navigation-client.d.ts.map +1 -1
- package/dist/shims/navigation.d.ts +9 -4
- package/dist/shims/navigation.d.ts.map +1 -1
- package/package.json +6 -7
- package/src/adapters/build-output-helper.ts +77 -0
- package/src/adapters/cloudflare.ts +10 -50
- package/src/adapters/nitro.ts +11 -45
- package/src/adapters/shared.ts +40 -0
- package/src/cache/timber-cache.ts +3 -2
- package/src/cli.ts +0 -0
- package/src/client/form.tsx +17 -25
- package/src/client/index.ts +16 -9
- package/src/client/internal.ts +3 -2
- package/src/client/router.ts +1 -1
- package/src/client/rsc-fetch.ts +15 -0
- package/src/client/state.ts +2 -2
- package/src/client/use-cookie.ts +29 -0
- package/src/codec.ts +3 -7
- package/src/config-types.ts +28 -0
- package/src/cookies/define-cookie.ts +271 -78
- package/src/cookies/index.ts +11 -8
- package/src/cookies/json-cookie.ts +105 -0
- package/src/cookies/validation.ts +134 -0
- package/src/{plugins/dev-404-page.ts → dev-tools/404-page.ts} +2 -7
- package/src/{plugins/dev-error-page.ts → dev-tools/error-page.ts} +5 -32
- package/src/dev-tools/index.ts +90 -0
- package/src/dev-tools/instrumentation.ts +176 -0
- package/src/{plugins/dev-logs.ts → dev-tools/logs.ts} +2 -2
- package/src/{plugins/dev-error-overlay.ts → dev-tools/overlay.ts} +5 -23
- package/src/dev-tools/stack-classifier.ts +75 -0
- package/src/{plugins/dev-terminal-error.ts → dev-tools/terminal.ts} +4 -38
- package/src/{server/dev-warnings.ts → dev-tools/warnings.ts} +1 -1
- package/src/index.ts +11 -3
- package/src/plugin-context.ts +1 -1
- package/src/plugins/adapter-build.ts +3 -1
- package/src/plugins/dev-server.ts +3 -3
- package/src/plugins/shims.ts +1 -1
- package/src/plugins/static-build.ts +1 -1
- package/src/routing/convention-lint.ts +5 -4
- package/src/routing/scanner.ts +5 -2
- package/src/routing/status-file-lint.ts +4 -2
- package/src/search-params/define.ts +71 -15
- package/src/search-params/wrappers.ts +9 -2
- package/src/segment-params/define.ts +66 -13
- package/src/server/access-gate.tsx +9 -8
- package/src/server/action-handler.ts +28 -38
- package/src/server/als-registry.ts +5 -5
- package/src/server/asset-headers.ts +8 -34
- package/src/server/cookie-context.ts +468 -0
- package/src/server/cookie-parsing.ts +135 -0
- package/src/server/{deny-page-resolver.ts → deny-boundary.ts} +78 -14
- package/src/server/deny-renderer.ts +2 -7
- package/src/server/early-hints-sender.ts +3 -2
- package/src/server/fallback-error.ts +1 -1
- package/src/server/index.ts +13 -14
- package/src/server/internal.ts +10 -3
- package/src/server/logger.ts +23 -0
- package/src/server/middleware-runner.ts +44 -0
- package/src/server/param-coercion.ts +76 -0
- package/src/server/pipeline-helpers.ts +37 -13
- package/src/server/pipeline-outcome.ts +167 -0
- package/src/server/pipeline-phases.ts +27 -209
- package/src/server/pipeline.ts +2 -9
- package/src/server/request-context.ts +46 -451
- package/src/server/route-element-builder.ts +7 -3
- package/src/server/rsc-entry/action-middleware-runner.ts +167 -0
- package/src/server/rsc-entry/error-renderer.ts +1 -1
- package/src/server/rsc-entry/helpers.ts +2 -7
- package/src/server/rsc-entry/index.ts +34 -273
- package/src/server/rsc-entry/render-route.ts +304 -0
- package/src/server/rsc-entry/rsc-payload.ts +1 -1
- package/src/server/rsc-entry/ssr-renderer.ts +2 -2
- package/src/server/rsc-entry/wrap-action-dispatch.ts +316 -23
- package/src/server/ssr-entry.ts +1 -1
- package/src/server/state-tree-diff.ts +4 -1
- package/src/server/tracing.ts +3 -3
- package/src/server/tree-builder.ts +128 -52
- package/src/server/types.ts +52 -0
- package/src/server/utils/escape-html.ts +20 -0
- package/src/shims/headers.ts +3 -3
- package/src/shims/navigation-client.ts +4 -3
- package/src/shims/navigation.ts +9 -7
- package/dist/_chunks/actions-DLnUaR65.js +0 -421
- package/dist/_chunks/actions-DLnUaR65.js.map +0 -1
- package/dist/_chunks/als-registry-HS0LGUl2.js +0 -41
- package/dist/_chunks/als-registry-HS0LGUl2.js.map +0 -1
- package/dist/_chunks/debug-ECi_61pb.js +0 -108
- package/dist/_chunks/debug-ECi_61pb.js.map +0 -1
- package/dist/_chunks/define-C77ScO0m.js.map +0 -1
- package/dist/_chunks/define-Itxvcd7F.js.map +0 -1
- package/dist/_chunks/define-cookie-BowvzoP0.js +0 -94
- package/dist/_chunks/define-cookie-BowvzoP0.js.map +0 -1
- package/dist/_chunks/dev-warnings-DpGRGoDi.js.map +0 -1
- package/dist/_chunks/merge-search-params-Cm_KIWDX.js +0 -41
- package/dist/_chunks/merge-search-params-Cm_KIWDX.js.map +0 -1
- package/dist/_chunks/request-context-CK5tZqIP.js +0 -478
- package/dist/_chunks/request-context-CK5tZqIP.js.map +0 -1
- package/dist/_chunks/router-ref-C8OCm7g7.js.map +0 -1
- package/dist/_chunks/tracing-CCYbKn5n.js +0 -238
- package/dist/_chunks/tracing-CCYbKn5n.js.map +0 -1
- package/dist/_chunks/use-params-IOPu7E8t.js.map +0 -1
- package/dist/_chunks/use-query-states-BiV5GJgm.js.map +0 -1
- package/dist/_chunks/walkers-VOXgavMF.js.map +0 -1
- package/dist/client/use-params.d.ts.map +0 -1
- package/dist/plugins/dev-404-page.d.ts.map +0 -1
- package/dist/plugins/dev-browser-logs.d.ts.map +0 -1
- package/dist/plugins/dev-error-overlay.d.ts.map +0 -1
- package/dist/plugins/dev-error-page.d.ts.map +0 -1
- package/dist/plugins/dev-logs.d.ts.map +0 -1
- package/dist/plugins/dev-terminal-error.d.ts.map +0 -1
- package/dist/server/deny-page-resolver.d.ts +0 -52
- package/dist/server/deny-page-resolver.d.ts.map +0 -1
- package/dist/server/dev-fetch-instrumentation.d.ts +0 -22
- package/dist/server/dev-fetch-instrumentation.d.ts.map +0 -1
- package/dist/server/dev-holding-server.d.ts.map +0 -1
- package/dist/server/dev-logger.d.ts.map +0 -1
- package/dist/server/dev-span-processor.d.ts.map +0 -1
- package/dist/server/dev-warnings.d.ts.map +0 -1
- package/dist/server/page-deny-boundary.d.ts +0 -31
- package/dist/server/page-deny-boundary.d.ts.map +0 -1
- package/src/server/dev-fetch-instrumentation.ts +0 -96
- package/src/server/dev-span-processor.ts +0 -78
- package/src/server/page-deny-boundary.tsx +0 -56
- /package/src/client/{use-params.ts → use-segment-params.ts} +0 -0
- /package/src/{plugins/dev-browser-logs.ts → dev-tools/browser-logs.ts} +0 -0
- /package/src/{server/dev-holding-server.ts → dev-tools/holding-server.ts} +0 -0
- /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
|
-
|
|
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
|
-
* - `
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
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
|
-
|
|
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
|
|
212
|
-
// can
|
|
213
|
-
|
|
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
|
-
|
|
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 +
|
|
431
|
-
//
|
|
432
|
-
return { rerender: actionResult, setCookieHeaders: [],
|
|
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
|
-
*
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
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('./
|
|
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
|
+
}
|