@timber-js/app 0.2.0-alpha.166 → 0.2.0-alpha.167
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/dist/_chunks/{actions-CDPfMp_I.js → actions-TSxpXLHJ.js} +2 -2
- package/dist/_chunks/{actions-CDPfMp_I.js.map → actions-TSxpXLHJ.js.map} +1 -1
- package/dist/_chunks/{cache-api-DygSeKCB.js → cache-api-DzpQQOEx.js} +52 -48
- package/dist/_chunks/{cache-api-DygSeKCB.js.map → cache-api-DzpQQOEx.js.map} +1 -1
- package/dist/_chunks/{cli-schema-sync-EXGYPhI2.js → cli-schema-sync-wX-i90Og.js} +4 -2
- package/dist/_chunks/cli-schema-sync-wX-i90Og.js.map +1 -0
- package/dist/_chunks/cloudflare-AHoWYTYr.js +1188 -0
- package/dist/_chunks/cloudflare-AHoWYTYr.js.map +1 -0
- package/dist/_chunks/logger-t3uxAmbX.js.map +1 -1
- package/dist/_chunks/{walkers-DBVzXuWc.js → walkers-Cfwvl-UC.js} +2 -2
- package/dist/_chunks/{walkers-DBVzXuWc.js.map → walkers-Cfwvl-UC.js.map} +1 -1
- package/dist/adapters/cloudflare-dev.js +1 -1
- package/dist/adapters/cloudflare-kv-cache.js +1 -1
- package/dist/adapters/cloudflare.d.ts +12 -1
- package/dist/adapters/cloudflare.d.ts.map +1 -1
- package/dist/adapters/cloudflare.js +2 -461
- package/dist/adapters/types.d.ts +2 -0
- package/dist/adapters/types.d.ts.map +1 -1
- package/dist/cache/cache-api.d.ts +33 -11
- package/dist/cache/cache-api.d.ts.map +1 -1
- package/dist/cache/index.js +1 -1
- package/dist/cli.js +1 -1
- package/dist/client/browser-entry/post-hydration.d.ts.map +1 -1
- package/dist/client/history.d.ts +10 -0
- package/dist/client/history.d.ts.map +1 -1
- package/dist/client/internal.js +138 -114
- package/dist/client/internal.js.map +1 -1
- package/dist/client/router.d.ts.map +1 -1
- package/dist/index.js +36 -27
- package/dist/index.js.map +1 -1
- package/dist/plugins/adapter-build.d.ts.map +1 -1
- package/dist/plugins/cache.d.ts +8 -8
- package/dist/plugins/cache.d.ts.map +1 -1
- package/dist/routing/index.js +2 -2
- package/dist/server/als-registry.d.ts +7 -0
- package/dist/server/als-registry.d.ts.map +1 -1
- package/dist/server/deny-boundary.d.ts +3 -1
- package/dist/server/deny-boundary.d.ts.map +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/internal.js +6 -3
- package/dist/server/internal.js.map +1 -1
- package/dist/server/route-element-builder.d.ts +9 -0
- package/dist/server/route-element-builder.d.ts.map +1 -1
- package/dist/server/rsc-entry/render-route.d.ts.map +1 -1
- package/dist/server/rsc-entry/rsc-payload.d.ts.map +1 -1
- package/dist/server/rsc-entry/rsc-stream.d.ts +8 -0
- package/dist/server/rsc-entry/rsc-stream.d.ts.map +1 -1
- package/docs/api/32-api-cache.mdx +4 -4
- package/docs/learn/09-caching.mdx +5 -5
- package/docs/more/03-coming-from-nextjs.mdx +2 -2
- package/docs/more/50-ai-agent-instructions.mdx +5 -5
- package/package.json +3 -2
- package/src/adapters/cloudflare.ts +63 -25
- package/src/adapters/types.ts +2 -0
- package/src/cache/cache-api.ts +84 -84
- package/src/client/browser-entry/post-hydration.ts +16 -9
- package/src/client/history.ts +11 -0
- package/src/client/router.ts +212 -206
- package/src/plugins/adapter-build.ts +1 -0
- package/src/plugins/cache.ts +45 -30
- package/src/routing/scanner.ts +7 -0
- package/src/server/als-registry.ts +7 -0
- package/src/server/deny-boundary.ts +6 -3
- package/src/server/route-element-builder.ts +49 -8
- package/src/server/rsc-entry/render-route.ts +3 -1
- package/src/server/rsc-entry/rsc-payload.ts +21 -4
- package/src/server/rsc-entry/rsc-stream.ts +8 -0
- package/dist/_chunks/cli-schema-sync-EXGYPhI2.js.map +0 -1
- package/dist/adapters/cloudflare.js.map +0 -1
package/src/routing/scanner.ts
CHANGED
|
@@ -232,6 +232,13 @@ function scanSegmentFiles(dirPath: string, node: SegmentNode, extSet: Set<string
|
|
|
232
232
|
// See design/16-metadata.md §"Metadata Routes"
|
|
233
233
|
const metaInfo = classifyMetadataRoute(entry);
|
|
234
234
|
if (metaInfo) {
|
|
235
|
+
if (!metaInfo.nestable && node.segmentName !== '') {
|
|
236
|
+
throw new Error(
|
|
237
|
+
`Build error: '${name}' is a root-only metadata convention and must be in the app root directory.\n` +
|
|
238
|
+
` File: ${fullPath}\n` +
|
|
239
|
+
` Move this file to the app root (not a route group or nested segment).`
|
|
240
|
+
);
|
|
241
|
+
}
|
|
235
242
|
if (!node.metadataRoutes) {
|
|
236
243
|
node.metadataRoutes = {};
|
|
237
244
|
}
|
|
@@ -127,6 +127,13 @@ export interface RequestContextStore {
|
|
|
127
127
|
* See TIM-666.
|
|
128
128
|
*/
|
|
129
129
|
denyStatus?: number;
|
|
130
|
+
/**
|
|
131
|
+
* Callback fired when setDenyStatus() is called. Used by shellSettled
|
|
132
|
+
* to short-circuit when any component catches a DenySignal in-tree,
|
|
133
|
+
* regardless of which component caught it (AccessGate, TracedLayout,
|
|
134
|
+
* or PageDenyBoundary). See TIM-1208.
|
|
135
|
+
*/
|
|
136
|
+
onDenyStatus?: () => void;
|
|
130
137
|
/**
|
|
131
138
|
* Dev-only: getter for the current request's RSC debug components.
|
|
132
139
|
* Set by renderRoute() so onPipelineError can include component tree
|
|
@@ -262,6 +262,7 @@ export async function PageDenyBoundary({
|
|
|
262
262
|
Page,
|
|
263
263
|
route,
|
|
264
264
|
denyPages,
|
|
265
|
+
onSettled,
|
|
265
266
|
}: {
|
|
266
267
|
/** The page server component function. */
|
|
267
268
|
Page: (...args: unknown[]) => unknown;
|
|
@@ -269,10 +270,10 @@ export async function PageDenyBoundary({
|
|
|
269
270
|
route: string;
|
|
270
271
|
/** Deny page fallback chain from the segment chain. */
|
|
271
272
|
denyPages: DenyPageEntry[];
|
|
273
|
+
/** Called when the page component finishes (success or deny). See TIM-1045. */
|
|
274
|
+
onSettled?: () => void;
|
|
272
275
|
}): Promise<React.ReactElement> {
|
|
273
276
|
try {
|
|
274
|
-
// Call the page as an async function — same as React Flight does.
|
|
275
|
-
// Wrap in OTEL span for tracing (replaces the TracedPage wrapper).
|
|
276
277
|
const result = await withSpan('timber.page', { 'timber.route': route }, () => Page({}));
|
|
277
278
|
return result as React.ReactElement;
|
|
278
279
|
} catch (error: unknown) {
|
|
@@ -283,8 +284,9 @@ export async function PageDenyBoundary({
|
|
|
283
284
|
return denyElement;
|
|
284
285
|
}
|
|
285
286
|
}
|
|
286
|
-
// Non-deny errors (RedirectSignal, runtime errors) propagate normally.
|
|
287
287
|
throw error;
|
|
288
|
+
} finally {
|
|
289
|
+
onSettled?.();
|
|
288
290
|
}
|
|
289
291
|
}
|
|
290
292
|
|
|
@@ -299,6 +301,7 @@ export function setDenyStatus(status: number): void {
|
|
|
299
301
|
const store = requestContextAls.getStore();
|
|
300
302
|
if (store) {
|
|
301
303
|
store.denyStatus = status;
|
|
304
|
+
store.onDenyStatus?.();
|
|
302
305
|
}
|
|
303
306
|
}
|
|
304
307
|
|
|
@@ -34,6 +34,7 @@ function getDevNonce(): string {
|
|
|
34
34
|
}
|
|
35
35
|
import { DenySignal, RedirectSignal } from './primitives.js';
|
|
36
36
|
import { AccessGate } from './access-gate.js';
|
|
37
|
+
import { requestContextAls } from './als-registry.js';
|
|
37
38
|
import {
|
|
38
39
|
PageDenyBoundary,
|
|
39
40
|
buildDenyPageChain,
|
|
@@ -177,6 +178,15 @@ export interface RouteElementResult {
|
|
|
177
178
|
* See design/19-client-navigation.md §"X-Timber-State-Tree Header"
|
|
178
179
|
*/
|
|
179
180
|
skippedSegments: string[];
|
|
181
|
+
/**
|
|
182
|
+
* Resolves when every deny-capable shell component (PageDenyBoundary and
|
|
183
|
+
* each TracedLayout with a deny page chain) has finished executing, OR
|
|
184
|
+
* immediately when any of them catches a DenySignal in-tree (the HTTP
|
|
185
|
+
* status is known at that point; inner components may never run).
|
|
186
|
+
* Undefined when the route has no deny-capable server components.
|
|
187
|
+
* See TIM-1045 (pages), TIM-1208 (layouts).
|
|
188
|
+
*/
|
|
189
|
+
shellSettled?: Promise<void>;
|
|
180
190
|
}
|
|
181
191
|
|
|
182
192
|
// ─── Module Processing Helpers ─────────────────────────────────────────────
|
|
@@ -462,12 +472,24 @@ export async function buildRouteElement(
|
|
|
462
472
|
// Build element tree: page wrapped in layouts (innermost to outermost)
|
|
463
473
|
const h = createElement as (...args: unknown[]) => React.ReactElement;
|
|
464
474
|
|
|
465
|
-
//
|
|
466
|
-
//
|
|
467
|
-
//
|
|
468
|
-
//
|
|
469
|
-
//
|
|
470
|
-
//
|
|
475
|
+
// Shell settlement tracking (TIM-1045 pages, TIM-1208 layouts).
|
|
476
|
+
// ChildSegmentProvider is 'use client', so React Flight eagerly
|
|
477
|
+
// serializes both the layout and child content as independent subtrees —
|
|
478
|
+
// the page can settle before an outer layout. We need all deny-capable
|
|
479
|
+
// components to settle before committing the HTTP status.
|
|
480
|
+
//
|
|
481
|
+
// Each tracked component gets its own deferred. shellSettled resolves
|
|
482
|
+
// when ALL settle (Promise.all) OR when any in-tree deny fires
|
|
483
|
+
// (short-circuit via onDenyStatus — covers AccessGate, TracedLayout,
|
|
484
|
+
// and PageDenyBoundary without threading callbacks into each).
|
|
485
|
+
const shellPromises: Promise<void>[] = [];
|
|
486
|
+
let resolveShortCircuit: (() => void) | undefined;
|
|
487
|
+
const trackShellComponent = (): (() => void) => {
|
|
488
|
+
let resolve!: () => void;
|
|
489
|
+
shellPromises.push(new Promise<void>((r) => (resolve = r)));
|
|
490
|
+
return resolve;
|
|
491
|
+
};
|
|
492
|
+
|
|
471
493
|
// Build the page element.
|
|
472
494
|
// Server component pages are wrapped in PageDenyBoundary which calls
|
|
473
495
|
// them as async functions and catches DenySignal — rendering the deny
|
|
@@ -482,11 +504,12 @@ export async function buildRouteElement(
|
|
|
482
504
|
if (isClientReference(PageComponent)) {
|
|
483
505
|
element = h(PageComponent, {});
|
|
484
506
|
} else if (leafDenyPages && leafDenyPages.length > 0) {
|
|
485
|
-
|
|
507
|
+
const onSettled = trackShellComponent();
|
|
486
508
|
element = h(PageDenyBoundary, {
|
|
487
509
|
Page: PageComponent,
|
|
488
510
|
route: match.segments[leafIndex]?.urlPath ?? '/',
|
|
489
511
|
denyPages: leafDenyPages,
|
|
512
|
+
onSettled,
|
|
490
513
|
});
|
|
491
514
|
} else {
|
|
492
515
|
// Server component page WITHOUT deny page chain — trace only
|
|
@@ -694,6 +717,8 @@ export async function buildRouteElement(
|
|
|
694
717
|
// fallback path. See TIM-668, design/04-authorization.md.
|
|
695
718
|
const layoutComponentRef = layoutComponent;
|
|
696
719
|
const layoutDenyPages = denyPageChains.get(i);
|
|
720
|
+
const hasLayoutDenyChain = !!layoutDenyPages && layoutDenyPages.length > 0;
|
|
721
|
+
const onLayoutSettled = hasLayoutDenyChain ? trackShellComponent() : undefined;
|
|
697
722
|
const TracedLayout = async (props: Record<string, unknown>) => {
|
|
698
723
|
try {
|
|
699
724
|
return await withSpan('timber.layout', { 'timber.segment': segmentId }, () =>
|
|
@@ -707,8 +732,11 @@ export async function buildRouteElement(
|
|
|
707
732
|
return denyElement;
|
|
708
733
|
}
|
|
709
734
|
}
|
|
710
|
-
// Non-deny errors (RedirectSignal, runtime errors) propagate normally
|
|
735
|
+
// Non-deny errors (RedirectSignal, runtime errors) propagate normally
|
|
736
|
+
// to onError — signalDetected / renderError handling covers those.
|
|
711
737
|
throw error;
|
|
738
|
+
} finally {
|
|
739
|
+
onLayoutSettled?.();
|
|
712
740
|
}
|
|
713
741
|
};
|
|
714
742
|
layoutElement = h(TracedLayout, {
|
|
@@ -776,11 +804,24 @@ export async function buildRouteElement(
|
|
|
776
804
|
element = replaceOutermostSegmentOutlet(element, outermostSegmentProvider);
|
|
777
805
|
}
|
|
778
806
|
|
|
807
|
+
// Compose shellSettled: resolves when ALL deny-capable components settle
|
|
808
|
+
// (Promise.all) OR immediately when any component calls setDenyStatus()
|
|
809
|
+
// (short-circuit via onDenyStatus — covers AccessGate, TracedLayout, and
|
|
810
|
+
// PageDenyBoundary). See TIM-1045, TIM-1208.
|
|
811
|
+
let shellSettled: Promise<void> | undefined;
|
|
812
|
+
if (shellPromises.length > 0) {
|
|
813
|
+
const shortCircuit = new Promise<void>((r) => (resolveShortCircuit = r));
|
|
814
|
+
const store = requestContextAls.getStore();
|
|
815
|
+
if (store) store.onDenyStatus = () => resolveShortCircuit?.();
|
|
816
|
+
shellSettled = Promise.race([Promise.all(shellPromises).then(() => {}), shortCircuit]);
|
|
817
|
+
}
|
|
818
|
+
|
|
779
819
|
return {
|
|
780
820
|
element,
|
|
781
821
|
layoutComponents,
|
|
782
822
|
segments,
|
|
783
823
|
deferSuspenseFor,
|
|
784
824
|
skippedSegments,
|
|
825
|
+
shellSettled,
|
|
785
826
|
};
|
|
786
827
|
}
|
|
@@ -137,7 +137,8 @@ export async function renderRoute(
|
|
|
137
137
|
desc: 'build element tree',
|
|
138
138
|
});
|
|
139
139
|
|
|
140
|
-
const { element, layoutComponents, deferSuspenseFor, skippedSegments } =
|
|
140
|
+
const { element, layoutComponents, deferSuspenseFor, skippedSegments, shellSettled } =
|
|
141
|
+
routeResult;
|
|
141
142
|
|
|
142
143
|
// Build head HTML for injection into the SSR output.
|
|
143
144
|
// Collects CSS, fonts, and modulepreload from the build manifest for matched segments.
|
|
@@ -191,6 +192,7 @@ export async function renderRoute(
|
|
|
191
192
|
// Render to RSC Flight stream with signal tracking.
|
|
192
193
|
const _rscStart = performance.now();
|
|
193
194
|
const { rscStream, signals, getDebugComponents } = renderRscStream(element, req);
|
|
195
|
+
if (shellSettled) signals.shellSettled = shellSettled;
|
|
194
196
|
|
|
195
197
|
// Store the debug components getter in ALS so onPipelineError can
|
|
196
198
|
// include component tree context for render-phase errors (dev mode only).
|
|
@@ -88,11 +88,28 @@ export async function buildRscPayloadResponse(
|
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
// If data arrived first, still check signals — they may have fired
|
|
91
|
-
// concurrently.
|
|
92
|
-
//
|
|
91
|
+
// concurrently. Wait for all deny-capable shell components (layouts
|
|
92
|
+
// with deny chains + page) to settle so async deny() calls preceded
|
|
93
|
+
// by I/O are captured. See TIM-1045, TIM-1208.
|
|
93
94
|
if (first.type === 'data' && !signals.redirectSignal && !signals.denySignal) {
|
|
94
|
-
//
|
|
95
|
-
|
|
95
|
+
// Sync deny (AccessGate pre-pass replay) sets getDenyStatus() during
|
|
96
|
+
// renderToReadableStream — before the first chunk is read. Skip the
|
|
97
|
+
// wait entirely: components below the denied gate never execute.
|
|
98
|
+
const { getDenyStatus: earlyDenyCheck } = await import('../deny-boundary.js');
|
|
99
|
+
if (signals.shellSettled && !earlyDenyCheck()) {
|
|
100
|
+
// Wait for all deny-capable shell components to finish, or for an
|
|
101
|
+
// in-tree layout deny to short-circuit (setDenyStatus already
|
|
102
|
+
// called), or for a signal to escape to onError. TIM-1045, TIM-1208.
|
|
103
|
+
await Promise.race([signals.shellSettled, signalDetected]);
|
|
104
|
+
// Shell components' finally blocks resolve before React Flight's
|
|
105
|
+
// onError fires. Yield one macrotask so onError can set
|
|
106
|
+
// signals.redirectSignal / signals.renderError before we check.
|
|
107
|
+
await new Promise<void>((r) => setTimeout(r, 0));
|
|
108
|
+
} else if (!earlyDenyCheck()) {
|
|
109
|
+
// No deny-capable shell components (client component page, no deny
|
|
110
|
+
// chain) — brief yield to let in-flight microtask rejections complete.
|
|
111
|
+
await new Promise<void>((r) => setTimeout(r, 0));
|
|
112
|
+
}
|
|
96
113
|
}
|
|
97
114
|
|
|
98
115
|
// Detach the callback — no longer needed after this point.
|
|
@@ -53,6 +53,14 @@ export interface RenderSignals {
|
|
|
53
53
|
lastUnhandledError: unknown | null;
|
|
54
54
|
/** Callback fired when a redirect or deny signal is captured in onError. */
|
|
55
55
|
onSignal?: () => void;
|
|
56
|
+
/**
|
|
57
|
+
* Resolves when every deny-capable shell component (PageDenyBoundary +
|
|
58
|
+
* TracedLayouts with deny chains) has settled, OR immediately when any
|
|
59
|
+
* catches a DenySignal in-tree (status is known). Set by renderRoute
|
|
60
|
+
* after element-tree construction. Awaited by buildRscPayloadResponse
|
|
61
|
+
* before committing HTTP status. See TIM-1045, TIM-1208.
|
|
62
|
+
*/
|
|
63
|
+
shellSettled?: Promise<void>;
|
|
56
64
|
/** Callback fired when an unhandled error (non-signal) is captured. Used
|
|
57
65
|
* to halt Flight data injection into the inline stream. */
|
|
58
66
|
onUnhandledError?: () => void;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cli-schema-sync-EXGYPhI2.js","names":[],"sources":["../../src/routing/scanner.ts","../../src/cli-schema-sync.ts"],"sourcesContent":["/**\n * Route discovery scanner.\n *\n * Pure function: (appDir, config) → RouteTree\n *\n * Scans the app/ directory and builds a segment tree recognizing all\n * timber.js file conventions. Does NOT handle request matching — this\n * is discovery only.\n */\n\nimport { readdirSync, statSync } from 'node:fs';\nimport { join, extname, basename } from 'node:path';\nimport type {\n RouteTree,\n SegmentNode,\n SegmentType,\n RouteFile,\n ScannerConfig,\n InterceptionMarker,\n} from './types.js';\nimport { classifySegment } from './segment-classify.js';\nimport { DEFAULT_PAGE_EXTENSIONS } from './types.js';\nimport { classifyMetadataRoute, isDynamicMetadataExtension } from '../server/metadata-routes.js';\nimport { swallow } from '../server/logger.js';\nimport { ENCODED_SEPARATOR_RE, NULL_BYTE_RE } from '../server/canonicalize.js';\n\n/**\n * File convention names that use pageExtensions (can be .tsx, .ts, .jsx, .js, .mdx, etc.)\n */\nconst PAGE_EXT_CONVENTIONS = new Set(['page', 'layout', 'error', 'default', 'denied']);\n\n/**\n * Legacy compat status-code files.\n * Maps legacy file name → HTTP status code for the fallback chain.\n * See design/10-error-handling.md §\"Fallback Chain\".\n */\nconst LEGACY_STATUS_FILES: Record<string, number> = {\n 'not-found': 404,\n 'forbidden': 403,\n 'unauthorized': 401,\n};\n\n/**\n * File convention names that are always .ts/.tsx (never .mdx etc.)\n */\nconst FIXED_CONVENTIONS = new Set(['middleware', 'access', 'route', 'params']);\n\n/**\n * Status-code file patterns:\n * - Exact 3-digit codes: 401.tsx, 429.tsx, 503.tsx\n * - Category catch-alls: 4xx.tsx, 5xx.tsx\n */\nconst STATUS_CODE_PATTERN = /^(\\d{3}|[45]xx)$/;\n\n/**\n * Scan the app/ directory and build the route tree.\n *\n * @param appDir - Absolute path to the app/ directory\n * @param config - Scanner configuration\n * @returns The complete route tree\n */\nexport function scanRoutes(appDir: string, config: ScannerConfig = {}): RouteTree {\n const pageExtensions = config.pageExtensions ?? DEFAULT_PAGE_EXTENSIONS;\n const extSet = new Set(pageExtensions);\n\n const tree: RouteTree = {\n root: createSegmentNode('', 'static', '/'),\n };\n\n // Check for proxy.ts at app root\n const proxyFile = findFixedFile(appDir, 'proxy');\n if (proxyFile) {\n tree.proxy = proxyFile;\n }\n\n // Check for global-error.{tsx,ts,jsx,js} at app root.\n // Tier 2 error page — renders standalone (no layouts) when no segment-level\n // error file is found. See design/10-error-handling.md §\"Tier 2\".\n const globalErrorFile = findPageExtFile(appDir, 'global-error', extSet);\n if (globalErrorFile) {\n tree.globalError = globalErrorFile;\n }\n\n // Scan the root directory's files\n scanSegmentFiles(appDir, tree.root, extSet);\n\n // Scan children recursively\n scanChildren(appDir, tree.root, extSet);\n\n // Validate: detect route group collisions (different groups producing pages at the same URL)\n validateRouteGroupCollisions(tree.root);\n\n // Validate: detect duplicate param names in nested dynamic segments\n // e.g., /[id]/items/[id] — same param name in ancestor and descendant\n validateDuplicateParamNames(tree.root);\n\n // Validate: detect same param name used with different bracket forms\n // e.g., [id] in one route and [...id] in another\n validateUniqueBracketForms(tree.root);\n\n return tree;\n}\n\n/**\n * Create an empty segment node.\n */\nfunction createSegmentNode(\n segmentName: string,\n segmentType: SegmentType,\n urlPath: string,\n paramName?: string,\n interceptionMarker?: InterceptionMarker,\n interceptedSegmentName?: string\n): SegmentNode {\n return {\n segmentName,\n segmentType,\n urlPath,\n paramName,\n interceptionMarker,\n interceptedSegmentName,\n children: [],\n slots: {},\n };\n}\n\n/**\n * Compute the URL path for a child segment given its parent's URL path.\n * Route groups, slots, and intercepting routes do NOT add URL depth.\n */\nfunction computeUrlPath(parentUrlPath: string, dirName: string, segmentType: SegmentType): string {\n // Groups, slots, and intercepting routes don't add to URL path\n if (segmentType === 'group' || segmentType === 'slot' || segmentType === 'intercepting') {\n return parentUrlPath;\n }\n\n const parentPath = parentUrlPath === '/' ? '' : parentUrlPath;\n return `${parentPath}/${dirName}`;\n}\n\n/**\n * Scan a directory for file conventions and populate the segment node.\n */\nfunction scanSegmentFiles(dirPath: string, node: SegmentNode, extSet: Set<string>): void {\n let entries: string[];\n try {\n entries = readdirSync(dirPath);\n } catch (err) {\n swallow(err, `scanSegmentFiles: unreadable directory ${dirPath}`, { level: 'warn' });\n return;\n }\n\n for (const entry of entries) {\n const fullPath = join(dirPath, entry);\n\n // Skip directories — handled by scanChildren\n try {\n if (statSync(fullPath).isDirectory()) continue;\n } catch {\n continue;\n }\n\n const ext = extname(entry).slice(1); // remove leading dot\n const name = basename(entry, `.${ext}`);\n\n // Page-extension conventions (page, layout, error, default, denied)\n if (PAGE_EXT_CONVENTIONS.has(name) && extSet.has(ext)) {\n const key = name as 'page' | 'layout' | 'error' | 'default' | 'denied';\n const existing = node[key];\n if (existing) {\n throwDuplicateConvention(name, existing.filePath, fullPath);\n }\n node[key] = { filePath: fullPath, extension: ext };\n continue;\n }\n\n // Fixed conventions (middleware, access, route, params) — only\n // .ts/.tsx/.js/.jsx, never pageExtensions like .mdx. The regex is\n // anchored: mts/cts/mjs/cjs (and stray tail matches like `.foots`)\n // are NOT recognized. See design/07-routing.md §\"Page Extensions\".\n if (FIXED_CONVENTIONS.has(name) && /^[jt]sx?$/.test(ext)) {\n const key = name as 'middleware' | 'access' | 'route' | 'params';\n const existing = node[key];\n if (existing) {\n throwDuplicateConvention(name, existing.filePath, fullPath);\n }\n node[key] = { filePath: fullPath, extension: ext };\n continue;\n }\n\n // JSON status-code files (401.json, 4xx.json, 503.json, 5xx.json)\n // Recognized regardless of pageExtensions — .json is a data format, not a page extension.\n if (STATUS_CODE_PATTERN.test(name) && ext === 'json') {\n if (!node.jsonStatusFiles) {\n node.jsonStatusFiles = {};\n }\n node.jsonStatusFiles[name] = { filePath: fullPath, extension: ext };\n continue;\n }\n\n // Status-code files (401.tsx, 4xx.tsx, 503.tsx, 5xx.tsx)\n if (STATUS_CODE_PATTERN.test(name) && extSet.has(ext)) {\n if (!node.statusFiles) {\n node.statusFiles = {};\n }\n const existing = node.statusFiles[name];\n if (existing) {\n throwDuplicateConvention(name, existing.filePath, fullPath);\n }\n node.statusFiles[name] = { filePath: fullPath, extension: ext };\n continue;\n }\n\n // Legacy compat files (not-found.tsx, forbidden.tsx, unauthorized.tsx)\n if (name in LEGACY_STATUS_FILES && extSet.has(ext)) {\n if (!node.legacyStatusFiles) {\n node.legacyStatusFiles = {};\n }\n const existing = node.legacyStatusFiles[name];\n if (existing) {\n throwDuplicateConvention(name, existing.filePath, fullPath);\n }\n node.legacyStatusFiles[name] = { filePath: fullPath, extension: ext };\n continue;\n }\n\n // Metadata route files (sitemap.ts, robots.ts, icon.tsx, opengraph-image.tsx, etc.)\n // Both static (.xml, .txt, .png, .ico, etc.) and dynamic (.ts, .tsx) files are recognized.\n // When both exist for the same base name, dynamic takes precedence.\n // Two files of the SAME kind (static+static or dynamic+dynamic) are a\n // build error — picking one by readdir order is platform-dependent.\n // See design/16-metadata.md §\"Metadata Routes\"\n const metaInfo = classifyMetadataRoute(entry);\n if (metaInfo) {\n if (!node.metadataRoutes) {\n node.metadataRoutes = {};\n }\n const existing = node.metadataRoutes[name];\n if (existing) {\n const existingIsDynamic = isDynamicMetadataExtension(name, existing.extension);\n const newIsDynamic = isDynamicMetadataExtension(name, ext);\n if (existingIsDynamic === newIsDynamic) {\n throwDuplicateConvention(name, existing.filePath, fullPath);\n }\n // Dynamic > static precedence: overwrite only when the new file is dynamic.\n if (newIsDynamic) {\n node.metadataRoutes[name] = { filePath: fullPath, extension: ext };\n }\n } else {\n node.metadataRoutes[name] = { filePath: fullPath, extension: ext };\n }\n }\n }\n\n // Validate: route.ts + page.* is a hard build error\n if (node.route && node.page) {\n throw new Error(\n `Build error: route.ts and page.* cannot coexist in the same segment.\\n` +\n ` route.ts: ${node.route.filePath}\\n` +\n ` page: ${node.page.filePath}\\n` +\n `A URL is either an API endpoint or a rendered page, not both.`\n );\n }\n}\n\n/**\n * Throw a build error for two files claiming the same convention slot in\n * one segment (e.g. page.tsx + page.jsx). Which file readdir returns first\n * is platform-dependent (APFS sorts entries, ext4 does not) — silently\n * picking one would let macOS dev and Linux CI serve different\n * implementations of the same route.\n */\nfunction throwDuplicateConvention(\n convention: string,\n firstPath: string,\n secondPath: string\n): never {\n throw new Error(\n `Build error: duplicate '${convention}' files in the same route segment.\\n` +\n ` File 1: ${firstPath}\\n` +\n ` File 2: ${secondPath}\\n` +\n `Each segment may define '${convention}' at most once. Remove one of the files.`\n );\n}\n\n/**\n * Recursively scan child directories and build the segment tree.\n */\nfunction scanChildren(dirPath: string, parentNode: SegmentNode, extSet: Set<string>): void {\n let entries: string[];\n try {\n entries = readdirSync(dirPath);\n } catch (err) {\n swallow(err, `scanChildren: unreadable directory ${dirPath}`, { level: 'warn' });\n return;\n }\n\n for (const entry of entries) {\n const fullPath = join(dirPath, entry);\n\n try {\n if (!statSync(fullPath).isDirectory()) continue;\n } catch {\n continue;\n }\n\n // Reject directories with encoded path delimiters or null bytes.\n // These can cause route collisions when decoded at the URL boundary.\n // See design/13-security.md §\"Encoded separators rejected\" and §\"Null bytes rejected\".\n if (ENCODED_SEPARATOR_RE.test(entry)) {\n throw new Error(\n `Build error: directory name contains an encoded path delimiter (%2F or %5C).\\n` +\n ` Directory: ${fullPath}\\n` +\n `Encoded separators in directory names cause route collisions when decoded. ` +\n `Rename the directory to remove the encoded delimiter.`\n );\n }\n if (NULL_BYTE_RE.test(entry)) {\n throw new Error(\n `Build error: directory name contains an encoded null byte (%00).\\n` +\n ` Directory: ${fullPath}\\n` +\n `Encoded null bytes in directory names are not allowed. ` +\n `Rename the directory to remove the null byte encoding.`\n );\n }\n\n const { type, paramName, interceptionMarker, interceptedSegmentName } = classifySegment(entry);\n\n // Skip private folders — underscore-prefixed dirs are excluded from routing\n if (type === 'private') continue;\n\n const urlPath = computeUrlPath(parentNode.urlPath, entry, type);\n const childNode = createSegmentNode(\n entry,\n type,\n urlPath,\n paramName,\n interceptionMarker,\n interceptedSegmentName\n );\n\n // Scan this segment's files\n scanSegmentFiles(fullPath, childNode, extSet);\n\n // Recurse into subdirectories\n scanChildren(fullPath, childNode, extSet);\n\n // Attach to parent: slots go into slots record, everything else is a child\n if (type === 'slot') {\n const slotName = entry.slice(1); // remove @\n parentNode.slots[slotName] = childNode;\n } else {\n parentNode.children.push(childNode);\n }\n }\n}\n\n/**\n * Validate that route groups don't produce conflicting pages/routes at the same URL path.\n *\n * Two route groups like (auth)/login/page.tsx and (marketing)/login/page.tsx both claim\n * /login — the scanner must detect and reject this at build time.\n *\n * Each parallel slot is validated as its OWN collision space: a slot's pages\n * intentionally share URL paths with the parent tree (that's the point of\n * parallel routes), but two route groups INSIDE one slot claiming the same\n * URL — e.g. @side/(a)/x/page.tsx + @side/(b)/x/page.tsx — still collide.\n */\nfunction validateRouteGroupCollisions(root: SegmentNode): void {\n // Map from urlPath → { filePath, source } for the first page/route seen at that path\n collectRoutableLeaves(root, new Map(), '');\n}\n\n/**\n * Walk the segment tree and collect all routable leaves (page or route files),\n * throwing on collision. Each slot recurses with a FRESH collision map —\n * slots are parallel routes that intentionally share URL paths with their\n * parent tree, but collisions within a single slot are real errors.\n */\nfunction collectRoutableLeaves(\n node: SegmentNode,\n seen: Map<string, { filePath: string; segmentPath: string }>,\n segmentPath: string\n): void {\n // Intercepting routes are resolved by the slot resolver, not URL matching,\n // and their computed urlPaths intentionally mirror other routes (e.g. two\n // interceptors in one slot both produce /gallery/[id]). Skip them entirely.\n if (node.segmentType === 'intercepting') return;\n\n const currentPath = segmentPath\n ? `${segmentPath}/${node.segmentName}`\n : node.segmentName || '(root)';\n\n const routableFile = node.page ?? node.route;\n if (routableFile) {\n const existing = seen.get(node.urlPath);\n if (existing) {\n throw new Error(\n `Build error: route collision — multiple route groups produce a page/route at the same URL path.\\n` +\n ` URL path: ${node.urlPath}\\n` +\n ` File 1: ${existing.filePath} (via ${existing.segmentPath})\\n` +\n ` File 2: ${routableFile.filePath} (via ${currentPath})\\n` +\n `Each URL path must map to exactly one page or route handler. ` +\n `Rename or move one of the conflicting files.`\n );\n }\n seen.set(node.urlPath, { filePath: routableFile.filePath, segmentPath: currentPath });\n }\n\n // Recurse into children\n for (const child of node.children) {\n collectRoutableLeaves(child, seen, currentPath);\n }\n\n // Recurse into slots — each slot gets a fresh map (its own collision space)\n for (const slotNode of Object.values(node.slots)) {\n collectRoutableLeaves(slotNode, new Map(), currentPath);\n }\n}\n\n/**\n * Validate that no route chain contains duplicate dynamic param names.\n *\n * Example violation:\n * app/[id]/items/[id]/page.tsx — 'id' appears twice in the ancestor chain.\n *\n * Route groups are transparent — params accumulate through them.\n * Slots are independent — duplicate detection does NOT cross slot boundaries.\n *\n * See design/07-routing.md §\"Duplicate Param Name Detection\"\n */\nfunction validateDuplicateParamNames(root: SegmentNode): void {\n walkForDuplicateParams(root, new Map());\n}\n\n/**\n * Recursively walk the segment tree, tracking seen param names → segment paths.\n * Throws on the first duplicate found.\n */\nfunction walkForDuplicateParams(node: SegmentNode, seen: Map<string, string>): void {\n // If this node introduces a param name, check for duplicates\n if (node.paramName) {\n const existing = seen.get(node.paramName);\n if (existing) {\n throw new Error(\n `[timber] Duplicate param name '${node.paramName}' in route chain.\\n` +\n ` First defined at: ${existing}\\n` +\n ` Duplicate at: ${node.urlPath || '/'}\\n` +\n ` Rename one of the segments to avoid ambiguity.`\n );\n }\n // Add to seen for descendants (use a new Map to avoid polluting siblings)\n seen = new Map(seen);\n seen.set(node.paramName, node.urlPath || '/');\n }\n\n // Recurse into children (they inherit the accumulated params)\n for (const child of node.children) {\n walkForDuplicateParams(child, seen);\n }\n\n // Slots are independent parallel routes — start fresh param tracking\n // (a slot's params don't conflict with the main route's params)\n for (const slotNode of Object.values(node.slots)) {\n walkForDuplicateParams(slotNode, new Map(seen));\n }\n}\n\n/**\n * Validate that each param name uses a single bracket form across the entire route tree.\n *\n * Different bracket forms produce different types:\n * [id] → string\n * [...id] → string[]\n * [[...id]] → string[] | undefined\n *\n * If the same param name appears with multiple forms, the no-argument\n * getSegmentParams() / useSegmentParams() return type becomes ambiguous.\n *\n * Same name + same form across different routes is fine and expected.\n */\nfunction validateUniqueBracketForms(root: SegmentNode): void {\n const forms = new Map<string, { segmentType: SegmentType; urlPath: string }>();\n collectBracketForms(root, forms);\n}\n\nfunction collectBracketForms(\n node: SegmentNode,\n forms: Map<string, { segmentType: SegmentType; urlPath: string }>\n): void {\n if (node.paramName && node.segmentType !== 'static' && node.segmentType !== 'group') {\n const existing = forms.get(node.paramName);\n if (existing && existing.segmentType !== node.segmentType) {\n const bracketExamples: Record<string, string> = {\n 'dynamic': `[${node.paramName}]`,\n 'catch-all': `[...${node.paramName}]`,\n 'optional-catch-all': `[[...${node.paramName}]]`,\n };\n throw new Error(\n `[timber] Param name '${node.paramName}' uses multiple bracket forms.\\n` +\n ` ${bracketExamples[existing.segmentType] ?? existing.segmentType} at: ${existing.urlPath}\\n` +\n ` ${bracketExamples[node.segmentType] ?? node.segmentType} at: ${node.urlPath || '/'}\\n` +\n ` Each param name must use a single bracket form across all routes.\\n` +\n ` Rename one of the segments to fix this (e.g., [${node.paramName}] → [${node.paramName}Id]).`\n );\n }\n if (!existing) {\n forms.set(node.paramName, {\n segmentType: node.segmentType,\n urlPath: node.urlPath || '/',\n });\n }\n }\n\n for (const child of node.children) {\n collectBracketForms(child, forms);\n }\n for (const slotNode of Object.values(node.slots)) {\n collectBracketForms(slotNode, forms);\n }\n}\n\n/**\n * Find a fixed-extension file (proxy.ts) in a directory.\n */\nfunction findFixedFile(dirPath: string, name: string): RouteFile | undefined {\n for (const ext of ['ts', 'tsx']) {\n const fullPath = join(dirPath, `${name}.${ext}`);\n try {\n if (statSync(fullPath).isFile()) {\n return { filePath: fullPath, extension: ext };\n }\n } catch {\n // File doesn't exist\n }\n }\n return undefined;\n}\n\n/**\n * Find a file using the configured page extensions (tsx, ts, jsx, js, mdx, etc.).\n * Used for app-root conventions like global-error that aren't per-segment.\n */\nfunction findPageExtFile(\n dirPath: string,\n name: string,\n extSet: Set<string>\n): RouteFile | undefined {\n for (const ext of extSet) {\n const fullPath = join(dirPath, `${name}.${ext}`);\n try {\n if (statSync(fullPath).isFile()) {\n return { filePath: fullPath, extension: ext };\n }\n } catch {\n // File doesn't exist\n }\n }\n return undefined;\n}\n","/**\n * `timber schema sync` — scan filesystem for dynamic segments, append\n * missing entries with `codec.string` defaults, and remove stale entries\n * whose segments no longer exist in the filesystem.\n *\n * Design doc: design/41-global-params.md §Auto-Scaffolding\n */\n\nimport { readFileSync, writeFileSync, existsSync } from 'node:fs';\nimport { join } from 'node:path';\nimport { scanRoutes } from './routing/scanner.js';\nimport type { SegmentNode } from './routing/types.js';\nimport { resolveAppDir } from './plugin-context.js';\n\n/**\n * Collect all unique bracket-keyed dynamic segment names from the route tree.\n * Returns entries like '[artistSlug]', '[...slug]', '[[...topic]]'.\n */\nexport function collectDynamicSegments(appDir: string): string[] {\n const tree = scanRoutes(appDir);\n const segments = new Set<string>();\n walkSegments(tree.root, segments);\n return [...segments].sort();\n}\n\nfunction walkSegments(node: SegmentNode, segments: Set<string>): void {\n if (node.paramName) {\n // Reconstruct bracket key from segmentName (already has brackets)\n segments.add(node.segmentName);\n }\n for (const child of node.children) {\n walkSegments(child, segments);\n }\n for (const slot of Object.values(node.slots)) {\n walkSegments(slot, segments);\n }\n}\n\n/**\n * Replace comment bodies (and optionally string contents) with spaces,\n * preserving length and newlines so every index maps 1:1 to the original.\n * Lets the structural scanning below ignore `segmentParams` mentions and\n * braces inside comments/strings without a TS parser (fine pre-alpha,\n * see TIM-1066).\n *\n * With `maskStrings`, string contents are blanked too — except literal\n * 'segmentParams' strings, kept so a quoted property key stays findable.\n */\nfunction maskSource(content: string, maskStrings: boolean): string {\n const out = content.split('');\n const n = content.length;\n let i = 0;\n\n const blank = (from: number, to: number): void => {\n for (let j = from; j < to; j++) {\n if (content[j] !== '\\n') out[j] = ' ';\n }\n };\n\n while (i < n) {\n const ch = content[i];\n const next = content[i + 1];\n if (ch === '/' && next === '/') {\n const start = i;\n while (i < n && content[i] !== '\\n') i++;\n blank(start, i);\n } else if (ch === '/' && next === '*') {\n const start = i;\n i += 2;\n while (i < n && !(content[i] === '*' && content[i + 1] === '/')) i++;\n i = Math.min(i + 2, n);\n blank(start, i);\n } else if (ch === '\"' || ch === \"'\" || ch === '`') {\n const start = i;\n i++;\n while (i < n && content[i] !== ch) {\n if (content[i] === '\\\\') i++;\n i++;\n }\n const end = i; // at closing quote (or n if unterminated)\n if (maskStrings && content.slice(start + 1, end) !== 'segmentParams') {\n blank(start + 1, end);\n }\n i++;\n } else {\n i++;\n }\n }\n return out.join('');\n}\n\n/**\n * Locate the `segmentParams: { ... }` block in a schema file.\n * Returns the indices of the opening and matching closing brace, or null\n * if no segmentParams property is found. Comments and strings are masked\n * first, so a JSDoc example mentioning `segmentParams: { ... }` above the\n * real defineSchema call is not mistaken for the block, and braces inside\n * comments/strings don't skew the depth count. The search is anchored to\n * the `defineSchema(` call when present, so helper objects with their own\n * segmentParams property earlier in the file are skipped.\n */\nexport function findSegmentParamsBlock(\n content: string\n): { openBrace: number; closeBrace: number } | null {\n const masked = maskSource(content, true);\n\n const defineCall = /\\bdefineSchema\\s*\\(/.exec(masked);\n const keyRe = /(['\"]?)segmentParams\\1\\s*:\\s*\\{/g;\n keyRe.lastIndex = defineCall ? defineCall.index + defineCall[0].length : 0;\n const keyMatch = keyRe.exec(masked);\n if (!keyMatch) return null;\n\n const openBrace = keyMatch.index + keyMatch[0].length - 1;\n let depth = 1;\n let i = openBrace + 1;\n while (i < masked.length && depth > 0) {\n if (masked[i] === '{') depth++;\n else if (masked[i] === '}') depth--;\n i++;\n }\n if (depth > 0) return null;\n // i now points to just after the closing brace\n return { openBrace, closeBrace: i - 1 };\n}\n\n/**\n * Parse existing bracket keys from an app/schema.ts file.\n * Scoped to the segmentParams: { ... } block — bracket keys elsewhere in\n * the file (unrelated objects, comments above defineSchema) are ignored.\n * Comments inside the block are masked, so a commented-out entry does not\n * count as registered.\n *\n * The key charset matches the route classifier (segment-classify.ts):\n * any character except `]`, so hyphenated params like '[my-param]' parse.\n */\nexport function parseExistingSchemaKeys(content: string): string[] {\n const block = findSegmentParamsBlock(content);\n if (!block) return [];\n\n const blockContent = maskSource(content, false).slice(block.openBrace, block.closeBrace);\n const keys: string[] = [];\n // Match quoted bracket keys like '[artistSlug]', '[...slug]', '[[...topic]]'\n const re = /['\"](\\[{1,2}(?:\\.{3})?[^\\]]+\\]{1,2})['\"]\\s*:/g;\n let m: RegExpExecArray | null;\n while ((m = re.exec(blockContent)) !== null) {\n keys.push(m[1]);\n }\n return keys;\n}\n\n/** Schema file extensions to check. */\nconst SCHEMA_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx'];\n\n/**\n * Find the schema file path if it exists.\n */\nexport function findSchemaFile(appDir: string): string | undefined {\n for (const ext of SCHEMA_EXTENSIONS) {\n const p = join(appDir, `schema${ext}`);\n if (existsSync(p)) return p;\n }\n return undefined;\n}\n\nexport function defaultCodecForSegment(seg: string): string {\n if (seg.startsWith('[[...')) return 'codec.optionalCatchAll(codec.string)';\n if (seg.startsWith('[...')) return 'codec.stringArray';\n return 'codec.string';\n}\n\n/**\n * Generate the content for a new app/schema.ts with the given segment keys.\n */\nfunction generateNewSchema(segments: string[]): string {\n const lines: string[] = [];\n lines.push(\"import { defineSchema } from '@timber-js/app/params';\");\n lines.push(\"import { codec } from '@timber-js/app/codec';\");\n lines.push('');\n lines.push('export default defineSchema({');\n lines.push(' segmentParams: {');\n for (const seg of segments) {\n lines.push(` '${seg}': ${defaultCodecForSegment(seg)},`);\n }\n lines.push(' },');\n lines.push('});');\n lines.push('');\n return lines.join('\\n');\n}\n\n/**\n * Append missing segment entries to an existing schema file.\n * Inserts new entries before the closing `}` of segmentParams.\n */\nconst CODEC_IMPORT = \"import { codec } from '@timber-js/app/codec';\";\n\nfunction hasCodecImport(content: string): boolean {\n return /import\\s+\\{[^}]*\\bcodec\\b[^}]*\\}\\s+from\\s+['\"]@timber-js\\/app\\/codec['\"]/.test(content);\n}\n\nfunction ensureCodecImport(content: string): string {\n if (hasCodecImport(content)) return content;\n\n // Insert after the last existing import statement\n const lines = content.split('\\n');\n let lastImportIndex = -1;\n for (let i = 0; i < lines.length; i++) {\n if (/^\\s*import\\s/.test(lines[i])) lastImportIndex = i;\n }\n\n if (lastImportIndex === -1) {\n return CODEC_IMPORT + '\\n' + content;\n }\n\n lines.splice(lastImportIndex + 1, 0, CODEC_IMPORT);\n return lines.join('\\n');\n}\n\nfunction appendToSchema(content: string, newSegments: string[]): string {\n content = ensureCodecImport(content);\n\n const block = findSegmentParamsBlock(content);\n if (!block) {\n throw new Error(\n '[timber] Could not find segmentParams in schema file. ' +\n 'Expected a defineSchema({ segmentParams: { ... } }) call.'\n );\n }\n const { openBrace, closeBrace } = block;\n\n // Detect indentation from existing entries\n const existingLine = content.slice(openBrace, closeBrace).match(/^(\\s+)['\"]\\[/m);\n const indent = existingLine ? existingLine[1] : ' ';\n\n // Build insertion text\n const insertion = newSegments\n .map((seg) => `${indent}'${seg}': ${defaultCodecForSegment(seg)},`)\n .join('\\n');\n\n // Insert before the closing brace\n const before = content.slice(0, closeBrace);\n const after = content.slice(closeBrace);\n\n // Add newline if the content before doesn't end with one\n const separator = before.trimEnd().endsWith(',') ? '\\n' : '\\n';\n\n return before + separator + insertion + '\\n' + after;\n}\n\n/**\n * Remove entries from the segmentParams block whose keys match `staleSegments`.\n * Removes the entire entry including trailing comma/whitespace. Scoped to\n * the segmentParams block — identical keys in unrelated objects elsewhere\n * in the file are left untouched.\n */\nfunction removeFromSchema(content: string, staleSegments: string[]): string {\n const block = findSegmentParamsBlock(content);\n if (!block) return content;\n\n let blockContent = content.slice(block.openBrace, block.closeBrace);\n // Key matching and the entry walk run on a comment-masked copy (same\n // length, indices align) so commented-out entries are never removed.\n // Both copies are spliced in sync to keep indices valid.\n let blockMasked = maskSource(blockContent, false);\n\n for (const seg of staleSegments) {\n const escaped = seg.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n const keyRe = new RegExp(`^[ \\t]*['\"]${escaped}['\"][ \\t]*:`, 'gm');\n let m: RegExpExecArray | null;\n while ((m = keyRe.exec(blockMasked)) !== null) {\n const start = m.index;\n let i = m.index + m[0].length;\n let depth = 0;\n let inString: string | null = null;\n\n while (i < blockMasked.length) {\n const ch = blockMasked[i];\n if (inString) {\n if (ch === '\\\\') {\n i++;\n } else if (ch === inString) {\n inString = null;\n }\n } else if (ch === '\"' || ch === \"'\" || ch === '`') {\n inString = ch;\n } else if (ch === '(' || ch === '{' || ch === '[') {\n depth++;\n } else if (ch === ')' || ch === '}' || ch === ']') {\n depth--;\n } else if (depth === 0 && ch === ',') {\n i++;\n break;\n } else if (depth === 0 && (ch === '\\n' || ch === '}')) {\n break;\n }\n i++;\n }\n\n // Consume trailing newline\n if (i < blockMasked.length && blockMasked[i] === '\\n') i++;\n\n blockContent = blockContent.slice(0, start) + blockContent.slice(i);\n blockMasked = blockMasked.slice(0, start) + blockMasked.slice(i);\n keyRe.lastIndex = start;\n }\n }\n\n return content.slice(0, block.openBrace) + blockContent + content.slice(block.closeBrace);\n}\n\nexport interface SchemaSyncResult {\n /** Segments found in the filesystem. */\n filesystemSegments: string[];\n /** Segments already defined in app/schema.ts. */\n existingSegments: string[];\n /** Segments that were added. */\n addedSegments: string[];\n /** Segments that were removed (no longer in filesystem). */\n removedSegments: string[];\n /** Whether a new schema file was created. */\n created: boolean;\n}\n\n/**\n * Run the schema sync: scan filesystem, diff against schema, append missing.\n */\nexport function runSchemaSync(projectRoot: string): SchemaSyncResult {\n const appDir = resolveAppDir(projectRoot);\n\n const filesystemSegments = collectDynamicSegments(appDir);\n\n const schemaPath = findSchemaFile(appDir);\n\n if (filesystemSegments.length === 0 && !schemaPath) {\n return {\n filesystemSegments: [],\n existingSegments: [],\n addedSegments: [],\n removedSegments: [],\n created: false,\n };\n }\n\n if (!schemaPath) {\n // Create new schema file\n const newPath = join(appDir, 'schema.ts');\n writeFileSync(newPath, generateNewSchema(filesystemSegments), 'utf-8');\n return {\n filesystemSegments,\n existingSegments: [],\n addedSegments: filesystemSegments,\n removedSegments: [],\n created: true,\n };\n }\n\n // Read existing schema and diff\n const content = readFileSync(schemaPath, 'utf-8');\n const existingSegments = parseExistingSchemaKeys(content);\n const missing = filesystemSegments.filter((seg) => !existingSegments.includes(seg));\n const stale = existingSegments.filter((seg) => !filesystemSegments.includes(seg));\n\n if (missing.length === 0 && stale.length === 0) {\n return {\n filesystemSegments,\n existingSegments,\n addedSegments: [],\n removedSegments: [],\n created: false,\n };\n }\n\n let updated = content;\n if (stale.length > 0) {\n updated = removeFromSchema(updated, stale);\n }\n if (missing.length > 0) {\n updated = appendToSchema(updated, missing);\n }\n writeFileSync(schemaPath, updated, 'utf-8');\n\n return {\n filesystemSegments,\n existingSegments,\n addedSegments: missing,\n removedSegments: stale,\n created: false,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA6BA,IAAM,uCAAuB,IAAI,IAAI;CAAC;CAAQ;CAAU;CAAS;CAAW;AAAQ,CAAC;;;;;;AAOrF,IAAM,sBAA8C;CAClD,aAAa;CACb,aAAa;CACb,gBAAgB;AAClB;;;;AAKA,IAAM,oCAAoB,IAAI,IAAI;CAAC;CAAc;CAAU;CAAS;AAAQ,CAAC;;;;;;AAO7E,IAAM,sBAAsB;;;;;;;;AAS5B,SAAgB,WAAW,QAAgB,SAAwB,CAAC,GAAc;CAChF,MAAM,iBAAiB,OAAO,kBAAkB;CAChD,MAAM,SAAS,IAAI,IAAI,cAAc;CAErC,MAAM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M,WAAW,GAC3C,OAAO;EACL;EACA;EACA,eAAe,CAAC;EAChB,iBAAiB,CAAC;EAClB,SAAS;CACX;CAGF,IAAI,UAAU;CACd,IAAI,MAAM,SAAS,GACjB,UAAU,iBAAiB,SAAS,KAAK;CAE3C,IAAI,QAAQ,SAAS,GACnB,UAAU,eAAe,SAAS,OAAO;CAE3C,cAAc,YAAY,SAAS,OAAO;CAE1C,OAAO;EACL;EACA;EACA,eAAe;EACf,iBAAiB;EACjB,SAAS;CACX;AACF"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cloudflare.js","names":[],"sources":["../../src/adapters/cloudflare.ts"],"sourcesContent":["// Cloudflare Workers adapter\n//\n// Primary deployment target. Generates a Workers-compatible entry point\n// and wrangler.jsonc configuration. See design/11-platform.md §\"Cloudflare Workers\".\n\nimport { writeFile, readFile, cp, rm, readdir } from 'node:fs/promises';\nimport { execFile } from 'node:child_process';\nimport { join, relative } from 'node:path';\nimport { AsyncLocalStorage } from 'node:async_hooks';\nimport type { TimberPlatformAdapter, TimberConfig } from './types';\nimport { runSharedBuildSteps } from './build-output-helper.js';\n\n// ─── Bindings passthrough ─────────────────────────────────────────────────\n// ALS stores the env object per-request so server components and middleware\n// can access KV, D1, DO, R2, Queues, etc. via getCloudflareBindings().\n// No global fallback — if called outside a request, it throws.\n// See design/11-platform.md §\"Platform Target\" and design/25-production-deployments.md.\n//\n// The ALS is stored on globalThis via Symbol.for so it survives Vite's\n// module instance split in dev mode. The Vite host process (where\n// cloudflareDevBindings → runWithBindings runs) and the RSC module runner\n// (where getCloudflareBindings runs) load separate instances of this file,\n// each with their own module-level variables. globalThis + Symbol.for\n// ensures both share the same ALS — same pattern React uses for\n// Symbol.for('react.element').\n\nconst BINDINGS_ALS_KEY = Symbol.for('timber:cf-bindings-als');\n\nfunction getBindingsAls(): AsyncLocalStorage<Record<string, unknown>> {\n let als = (globalThis as any)[BINDINGS_ALS_KEY] as\n | AsyncLocalStorage<Record<string, unknown>>\n | undefined;\n if (!als) {\n als = new AsyncLocalStorage<Record<string, unknown>>();\n (globalThis as any)[BINDINGS_ALS_KEY] = als;\n }\n return als;\n}\n\n/**\n * Get Cloudflare Worker bindings for the current request.\n *\n * Returns the `env` object passed to the Worker's `fetch` handler,\n * giving direct access to KV, D1, Durable Objects, R2, Queues, and\n * any other bindings configured in `wrangler.jsonc`.\n *\n * Must be called within a request context (server component, middleware,\n * server action). Throws outside a request.\n *\n * @example\n * ```ts\n * import { getCloudflareBindings } from '@timber-js/app/adapters/cloudflare'\n *\n * export default async function Page() {\n * const { MY_KV, MY_DB } = getCloudflareBindings()\n * const data = await MY_KV.get('key')\n * return <div>{data}</div>\n * }\n * ```\n */\nexport function getCloudflareBindings<T = Record<string, unknown>>(): T {\n const env = getBindingsAls().getStore();\n if (!env) {\n throw new Error(\n 'getCloudflareBindings() called outside a Cloudflare Workers request context. ' +\n 'It can only be called from server components, middleware, or server actions ' +\n 'when running on the Cloudflare adapter.'\n );\n }\n return env as T;\n}\n\n/**\n * Run a function with Cloudflare bindings available via getCloudflareBindings().\n * @internal Used by cloudflareDevBindings() to wrap dev-server requests.\n */\nexport function runWithBindings<T>(env: Record<string, unknown>, fn: () => T): T {\n return getBindingsAls().run(env, fn);\n}\n\n// ─── Binding configuration types ──────────────────────────────────────────────\n// Declarative binding config that maps to wrangler.jsonc binding sections.\n// See design/35-cloudflare-primitives.md §\"Binding Declarations in Adapter Config\".\n\n/** KV namespace binding. `id` defaults to `''` (filled in by wrangler or dashboard). */\nexport interface CloudflareKVBinding {\n name: string;\n id?: string;\n}\n\n/** D1 database binding. `database_id` is required (from Cloudflare dashboard). */\nexport interface CloudflareD1Binding {\n name: string;\n database_id: string;\n}\n\n/** R2 bucket binding. `bucket_name` must match the bucket created in Cloudflare. */\nexport interface CloudflareR2Binding {\n name: string;\n bucket_name: string;\n}\n\n/** Queue producer binding. `queue` is the queue name in Cloudflare. */\nexport interface CloudflareQueueProducer {\n name: string;\n queue: string;\n}\n\n/** Queue consumer declaration. Attached to the worker, not a binding name. */\nexport interface CloudflareQueueConsumer {\n queue: string;\n max_batch_size?: number;\n max_retries?: number;\n dead_letter_queue?: string;\n}\n\n/** Durable Object binding. `script_name` is for external DO references. */\nexport interface CloudflareDurableObjectBinding {\n name: string;\n class_name: string;\n script_name?: string;\n}\n\n/**\n * Declarative Cloudflare bindings configuration.\n *\n * These are converted to the appropriate wrangler.jsonc sections\n * (`kv_namespaces`, `d1_databases`, `r2_buckets`, `queues`, `durable_objects`).\n * The `wrangler` escape hatch overrides any generated binding sections\n * if there's a conflict.\n *\n * @example\n * ```ts\n * cloudflare({\n * bindings: {\n * kv: [{ name: 'TIMBER_CACHE' }],\n * d1: [{ name: 'MY_DB', database_id: 'xxxx' }],\n * r2: [{ name: 'MY_BUCKET', bucket_name: 'my-bucket' }],\n * queues: {\n * producers: [{ name: 'EMAIL_QUEUE', queue: 'email-queue' }],\n * consumers: [{ queue: 'email-queue', max_batch_size: 10 }],\n * },\n * durableObjects: [{ name: 'MY_DO', class_name: 'MyDurableObject' }],\n * },\n * })\n * ```\n */\nexport interface CloudflareBindings {\n kv?: CloudflareKVBinding[];\n d1?: CloudflareD1Binding[];\n r2?: CloudflareR2Binding[];\n queues?: {\n producers?: CloudflareQueueProducer[];\n consumers?: CloudflareQueueConsumer[];\n };\n durableObjects?: CloudflareDurableObjectBinding[];\n}\n\n/** Options for the Cloudflare Workers adapter. */\nexport interface CloudflareAdapterOptions {\n /**\n * Cloudflare compatibility date.\n * @default Current date in YYYY-MM-DD format at build time.\n */\n compatibilityDate?: string;\n\n /**\n * Additional compatibility flags.\n * @default ['nodejs_compat']\n */\n compatibilityFlags?: string[];\n\n /**\n * Declarative Cloudflare bindings. Generates the appropriate\n * `kv_namespaces`, `d1_databases`, `r2_buckets`, `queues`, and\n * `durable_objects` sections in wrangler.jsonc.\n *\n * If both `bindings` and `wrangler` specify the same section,\n * `wrangler` wins (it's the escape hatch).\n */\n bindings?: CloudflareBindings;\n\n /**\n * Custom wrangler.jsonc fields to merge.\n * Overrides generated values (including bindings-generated sections).\n */\n wrangler?: Record<string, unknown>;\n\n /**\n * Path to a module that exports additional Worker handlers (queue, scheduled, email, etc.).\n * The module is imported in the generated `_worker.js` and its named exports are\n * spread into the default export alongside `fetch`.\n *\n * The module receives `(batch/event, env, ctx)` — standard Cloudflare handler signatures.\n * It does NOT have access to timber's request pipeline (no ALS, no getCloudflareBindings).\n * Use `env` directly for bindings.\n *\n * @example\n * ```ts\n * // timber.config.ts\n * adapter: cloudflare({ workerHandlers: './src/worker-handlers.ts' })\n *\n * // src/worker-handlers.ts\n * export async function queue(batch, env) { ... }\n * export async function scheduled(controller, env, ctx) { ... }\n * ```\n */\n workerHandlers?: string;\n}\n\n/**\n * Create a Cloudflare Workers adapter.\n *\n * @example\n * ```ts\n * import { cloudflare } from '@timber-js/app/adapters/cloudflare'\n *\n * export default {\n * output: 'server',\n * adapter: cloudflare(),\n * }\n * ```\n */\nexport function cloudflare(options: CloudflareAdapterOptions = {}): TimberPlatformAdapter {\n return {\n name: 'cloudflare',\n\n async buildOutput(config: TimberConfig, buildDir: string) {\n const outDir = join(buildDir, 'cloudflare');\n\n // Clean stale prebuilt artifacts from prior builds.\n await rm(join(outDir, 'prebuilt'), { recursive: true, force: true }).catch(() => {});\n await rm(join(outDir, 'static', 'prebuilt'), { recursive: true, force: true }).catch(\n () => {}\n );\n\n // Steps 1–5: shared across all adapters (mkdir, copy client/rsc/ssr,\n // write _headers, write manifest-init).\n await runSharedBuildSteps({\n config,\n buildDir,\n outDir,\n publicDirName: 'static',\n });\n\n // Compile optional worker handlers (queue, scheduled, etc.)\n // Uses Vite's build API to bundle the TypeScript source into ESM.\n let hasWorkerHandlers = false;\n if (options.workerHandlers) {\n const handlersEntry = join(process.cwd(), options.workerHandlers);\n const { build: viteBuild } = await import('vite');\n await viteBuild({\n configFile: false,\n logLevel: 'info',\n build: {\n lib: {\n entry: handlersEntry,\n formats: ['es'],\n fileName: () => '_worker-handlers.js',\n },\n outDir,\n emptyOutDir: false,\n minify: false,\n rollupOptions: {\n external: [/^node:/, /^cloudflare:/],\n },\n },\n });\n hasWorkerHandlers = true;\n }\n\n // Inline prebuilt flight payloads into the worker JS module (TIM-1162).\n // Data is read from buildDir/prebuilt/ and embedded as literals —\n // no external Text/Data module imports needed.\n const prebuiltData = await readPrebuiltData(buildDir);\n await writeFile(\n join(outDir, '_timber-prebuilt-source.js'),\n generatePrebuiltSourceModule(prebuiltData)\n );\n // Remove the copied prebuilt/ dir — data is inlined, files aren't needed.\n await rm(join(outDir, 'prebuilt'), { recursive: true, force: true }).catch(() => {});\n\n // Write the runtime init module — sets process.env.TIMBER_RUNTIME\n // BEFORE the RSC entry evaluates (ESM import order). Without this,\n // createRequestHandler's auto-wire for WorkersCachePurge would not\n // detect the Cloudflare runtime (TIM-1170).\n await writeFile(join(outDir, '_timber-runtime-init.js'), generateRuntimeInitModule());\n\n // Generate the Workers entry point\n const hasManifestInit = !!config.manifestInit;\n const workerEntry = generateWorkerEntry(outDir, outDir, hasManifestInit, hasWorkerHandlers);\n await writeFile(join(outDir, '_worker.js'), workerEntry);\n\n // Generate wrangler.jsonc\n const wranglerConfig = generateWranglerConfig(config, options);\n await writeFile(join(outDir, 'wrangler.jsonc'), JSON.stringify(wranglerConfig, null, 2));\n },\n\n async preview(_config: TimberConfig, buildDir: string) {\n const cmd = generatePreviewCommand(buildDir);\n await spawnPreviewProcess(cmd.command, cmd.args, cmd.cwd);\n },\n\n // No adapter-level waitUntil: on Workers there is no ambient\n // ExecutionContext outside a request, so support is provided\n // per-request by the generated _worker.js, which bridges\n // ctx.waitUntil() into the ALS via runWithWaitUntil().\n // See design/11-platform.md §\"waitUntil()\".\n };\n}\n\n// ─── Exported helpers (used by tests and build) ─────────────────────────────\n\n/** @internal Exported for testing. */\nexport function generateWorkerEntry(\n buildDir: string,\n outDir: string,\n hasManifestInit = false,\n hasWorkerHandlers = false\n): string {\n // The RSC entry is the main request handler — it exports the fetch handler as default.\n // The Vite RSC plugin outputs it to rsc/index.js.\n let rscEntryRelative = relative(outDir, join(buildDir, 'rsc', 'index.js'));\n // Ensure the import path starts with ./ for ESM compatibility\n if (!rscEntryRelative.startsWith('.')) {\n rscEntryRelative = './' + rscEntryRelative;\n }\n\n // Build manifest init must be imported before the RSC handler so that\n // globalThis.__TIMBER_BUILD_MANIFEST__ is set when the virtual module evaluates.\n // ESM guarantees imports are evaluated in order.\n const manifestImport = hasManifestInit ? \"import './_timber-manifest-init.js'\\n\" : '';\n\n // Optional additional Worker handlers (queue, scheduled, email, etc.)\n // Compiled by buildOutput via Vite's build API into _worker-handlers.js.\n // The module's named exports are spread into the default export alongside fetch.\n const handlersImport = hasWorkerHandlers\n ? \"import * as workerHandlers from './_worker-handlers.js'\\n\"\n : '';\n const handlersSpread = hasWorkerHandlers ? ' ...workerHandlers,\\n' : '';\n\n return `// Generated by @timber-js/app/adapters/cloudflare\n// Do not edit — this file is regenerated on each build.\n\nimport { AsyncLocalStorage } from 'node:async_hooks'\n// Side-effect imports: evaluated IN ORDER before the _worker.js body runs.\n// _timber-runtime-init.js sets TIMBER_RUNTIME before createRequestHandler()\n// runs (during the RSC entry's top-level await) so the auto-wire for\n// WorkersCachePurge can detect the Cloudflare runtime (TIM-1170).\n// _timber-prebuilt-source.js sets globalThis.__TIMBER_PREBUILT_SOURCE__\n// for cache.component payloads.\nimport './_timber-runtime-init.js'\nimport './_timber-prebuilt-source.js'\n${manifestImport}import handler, { runWithWaitUntil, runWithWorkersCachePurge } from '${rscEntryRelative}'\n${handlersImport}\n\n// Bind Cloudflare env to ALS so getCloudflareBindings() works at runtime.\n// Uses the same Symbol.for key as getCloudflareBindings() reads from.\nconst ALS_KEY = Symbol.for('timber:cf-bindings-als')\nlet bindingsAls = globalThis[ALS_KEY]\nif (!bindingsAls) {\n bindingsAls = new AsyncLocalStorage()\n globalThis[ALS_KEY] = bindingsAls\n}\n\n// Register timber's platform tracer so pipeline phase spans (timber.proxy,\n// timber.middleware, timber.render, timber.access, timber.action, …) appear\n// as native Cloudflare spans in the Observability dashboard, nested under\n// the auto-instrumented fetch/KV/D1 spans. Requires observability.traces\n// enabled in wrangler.jsonc (generated by default).\n//\n// Lazily imported and guarded: runtimes without the custom spans API\n// (older compatibility dates, older local workerd) fall back to OTEL-only\n// emission. withSpan() in the framework composes both channels.\nconst TRACER_KEY = Symbol.for('timber:platform-tracer')\nlet tracerInit\nfunction initPlatformTracer() {\n tracerInit ??= import('cloudflare:workers')\n .then(({ tracing }) => {\n if (tracing && typeof tracing.enterSpan === 'function') {\n globalThis[TRACER_KEY] ??= {\n enterSpan: (name, fn) => tracing.enterSpan(name, fn),\n }\n }\n })\n .catch(() => {})\n return tracerInit\n}\n\nexport default {\n${handlersSpread} async fetch(request, env, ctx) {\n // One-time on first request; resolved-promise await thereafter.\n await initPlatformTracer()\n\n // Edge-cache GET/HEAD responses via the Cache API. RSC navigation\n // requests (Accept: text/x-component) are excluded — they vary by\n // X-Timber-State-Tree which the cache key doesn't capture.\n const isRsc = request.headers.get('Accept')?.includes('text/x-component')\n const reqCc = request.headers.get('Cache-Control') || ''\n const bypassCache = reqCc.includes('no-cache') || reqCc.includes('max-age=0')\n || request.headers.has('Pragma')\n const cacheable = !isRsc && !bypassCache\n && (request.method === 'GET' || request.method === 'HEAD')\n const cache = cacheable && typeof caches !== 'undefined' ? caches.default : null\n if (cache) {\n const cached = await cache.match(request)\n if (cached) return cached\n }\n\n // Bridge ctx.cache into the per-request ALS so WorkersCachePurge can\n // call ctx.cache.purge({ tags }) during cache.invalidate/revalidateTag.\n // Workers Cache is available on all Cloudflare plans — no API token needed.\n const workersCacheCtx = ctx.cache && typeof ctx.cache.purge === 'function' ? ctx.cache : null\n const runHandler = () =>\n runWithWaitUntil((promise) => ctx.waitUntil(promise), () => handler(request))\n const response = await bindingsAls.run(env, () =>\n workersCacheCtx\n ? runWithWorkersCachePurge(workersCacheCtx, runHandler)\n : runHandler()\n )\n\n // Cache public responses at the edge. A response is edge-cacheable if\n // it has 'public' or 's-maxage' (a shared-cache TTL directive) and no\n // 'no-store' or 'private'. Cloudflare's cache.put() only accepts GET\n // requests and rejects 206, so only store GET 200s. HEAD requests\n // benefit from cache.match() which handles HEAD→GET matching automatically.\n // See design/25-production-deployments.md §\"CDN / Edge Cache\".\n if (cache && request.method === 'GET' && response.status === 200) {\n const cc = response.headers.get('Cache-Control')\n const vary = response.headers.get('Vary')\n if (cc && !cc.includes('no-store') && !cc.includes('private')\n && (cc.includes('public') || cc.includes('s-maxage'))\n && vary !== '*' && !response.headers.has('Set-Cookie')) {\n ctx.waitUntil(cache.put(request, response.clone()))\n }\n }\n\n return response\n }\n}\n`;\n}\n\n/** Build-time prebuilt data read from `.timber/dist/prebuilt/`. */\nexport interface PrebuiltModuleData {\n /** Raw manifest.json content. */\n manifestJson: string;\n /** Payload file paths and their raw bytes. */\n payloads: Array<{ file: string; bytes: Buffer }>;\n}\n\n/**\n * Discover and read prebuilt flight payload files from the build directory.\n * Returns null when no prebuilt artifacts exist (no cache.component usage).\n *\n * @internal Exported for testing.\n */\nexport async function readPrebuiltData(buildDir: string): Promise<PrebuiltModuleData | null> {\n const prebuiltDir = join(buildDir, 'prebuilt');\n let manifestJson: string;\n try {\n manifestJson = await readFile(join(prebuiltDir, 'manifest.json'), 'utf-8');\n } catch {\n return null;\n }\n const payloads: PrebuiltModuleData['payloads'] = [];\n try {\n const componentDirs = await readdir(prebuiltDir, { withFileTypes: true });\n for (const dir of componentDirs) {\n if (!dir.isDirectory()) continue;\n const files = await readdir(join(prebuiltDir, dir.name));\n for (const file of files) {\n if (file.endsWith('.flight')) {\n const filePath = `${dir.name}/${file}`;\n const bytes = await readFile(join(prebuiltDir, filePath));\n payloads.push({ file: filePath, bytes });\n }\n }\n }\n } catch {\n // No subdirectories — manifest exists but no payloads\n }\n return { manifestJson, payloads };\n}\n\n/**\n * Generate the prebuilt payload source module for Cloudflare Workers.\n *\n * Inlines manifest JSON and flight payload bytes directly into the module\n * so they're part of the worker's JS bundle — no external Text/Data module\n * imports needed. This avoids depending on wrangler's find_additional_modules\n * to discover non-JS files, which doesn't reliably resolve Text/Data imports.\n *\n * @internal Exported for testing.\n */\nexport function generatePrebuiltSourceModule(data: PrebuiltModuleData | null): string {\n if (!data) {\n return `// Generated by @timber-js/app/adapters/cloudflare\n// No prebuilt artifacts — all cache.component wrappers render dynamically.\nglobalThis.__TIMBER_PREBUILT_SOURCE__ = {\n async loadManifest() { return null },\n async readPayload() { return null },\n}\n`;\n }\n\n const entries: string[] = [];\n for (const { file, bytes } of data.payloads) {\n const b64 = bytes.toString('base64');\n entries.push(` ${JSON.stringify(file)}: ${JSON.stringify(b64)}`);\n }\n\n return `// Generated by @timber-js/app/adapters/cloudflare\n// Prebuilt payload source — inlines cache.component flight payloads\n// directly into the worker bundle (TIM-1162).\nconst _manifest = ${data.manifestJson.trim()}\n\nfunction _decode(b64) {\n const bin = atob(b64)\n const buf = new Uint8Array(bin.length)\n for (let i = 0; i < bin.length; i++) buf[i] = bin.charCodeAt(i)\n return buf\n}\n\nconst _payloads = {\n${entries.join(',\\n')}\n}\n\nglobalThis.__TIMBER_PREBUILT_SOURCE__ = {\n async loadManifest() { return _manifest },\n async readPayload(file) {\n const b64 = _payloads[file]\n return b64 ? _decode(b64) : null\n },\n}\n`;\n}\n\n/**\n * Generate the runtime init module that sets TIMBER_RUNTIME before the\n * RSC entry evaluates. Imported as a side-effect module in _worker.js.\n *\n * @internal Exported for testing.\n */\nexport function generateRuntimeInitModule(): string {\n return `// Generated by @timber-js/app/adapters/cloudflare\n// Sets TIMBER_RUNTIME before the RSC handler evaluates (TIM-1170).\n// Must be a separate module imported before rsc/index.js so that ESM\n// evaluation order guarantees the env var is set when\n// createRequestHandler() runs its auto-wire logic.\nglobalThis.process ??= { env: {} }\nprocess.env.TIMBER_RUNTIME = 'cloudflare'\n`;\n}\n\n/** Wrangler binding sections generated from `CloudflareBindings`. */\nexport interface WranglerBindingsConfig {\n kv_namespaces?: Array<{ binding: string; id: string }>;\n d1_databases?: Array<{ binding: string; database_id: string }>;\n r2_buckets?: Array<{ binding: string; bucket_name: string }>;\n queues?: {\n producers?: Array<{ binding: string; queue: string }>;\n consumers?: Array<Record<string, unknown>>;\n };\n durable_objects?: {\n bindings: Array<Record<string, string>>;\n };\n}\n\n/**\n * Convert declarative `CloudflareBindings` into wrangler.jsonc binding sections.\n *\n * Maps:\n * - `kv` → `kv_namespaces`\n * - `d1` → `d1_databases`\n * - `r2` → `r2_buckets`\n * - `queues` → `queues` (producers + consumers)\n * - `durableObjects` → `durable_objects`\n *\n * Empty arrays are omitted from the output.\n *\n * @internal Exported for testing.\n */\nexport function generateBindingsConfig(\n bindings: CloudflareBindings | undefined\n): WranglerBindingsConfig {\n if (!bindings) return {};\n\n const result: WranglerBindingsConfig = {};\n\n // KV namespaces\n if (bindings.kv && bindings.kv.length > 0) {\n result.kv_namespaces = bindings.kv.map((kv) => ({\n binding: kv.name,\n id: kv.id ?? '',\n }));\n }\n\n // D1 databases\n if (bindings.d1 && bindings.d1.length > 0) {\n result.d1_databases = bindings.d1.map((d1) => ({\n binding: d1.name,\n database_id: d1.database_id,\n }));\n }\n\n // R2 buckets\n if (bindings.r2 && bindings.r2.length > 0) {\n result.r2_buckets = bindings.r2.map((r2) => ({\n binding: r2.name,\n bucket_name: r2.bucket_name,\n }));\n }\n\n // Queues (producers and/or consumers)\n if (bindings.queues) {\n const queues: Record<string, unknown> = {};\n if (bindings.queues.producers && bindings.queues.producers.length > 0) {\n queues.producers = bindings.queues.producers.map((p) => ({\n binding: p.name,\n queue: p.queue,\n }));\n }\n if (bindings.queues.consumers && bindings.queues.consumers.length > 0) {\n queues.consumers = bindings.queues.consumers.map((c) => {\n const consumer: Record<string, unknown> = { queue: c.queue };\n if (c.max_batch_size !== undefined) consumer.max_batch_size = c.max_batch_size;\n if (c.max_retries !== undefined) consumer.max_retries = c.max_retries;\n if (c.dead_letter_queue !== undefined) consumer.dead_letter_queue = c.dead_letter_queue;\n return consumer;\n });\n }\n if (Object.keys(queues).length > 0) {\n result.queues = queues;\n }\n }\n\n // Durable Objects\n if (bindings.durableObjects && bindings.durableObjects.length > 0) {\n result.durable_objects = {\n bindings: bindings.durableObjects.map((dobj) => {\n const entry: Record<string, string> = {\n name: dobj.name,\n class_name: dobj.class_name,\n };\n if (dobj.script_name) entry.script_name = dobj.script_name;\n return entry;\n }),\n };\n }\n\n return result;\n}\n\n/**\n * One-level deep merge: for each key in `override`, if both the base and\n * override values are plain objects (not arrays, not null), merge their\n * keys with override winning on conflicts. Everything else (primitives,\n * arrays, null) is replaced outright — matching the intuitive behavior\n * where `kv_namespaces: [...]` replaces fully but\n * `durable_objects: { migrations }` merges with generated `bindings`.\n */\nfunction shallowDeepMerge(\n base: Record<string, unknown>,\n override: Record<string, unknown>\n): Record<string, unknown> {\n const result = { ...base };\n for (const key of Object.keys(override)) {\n const baseVal = result[key];\n const overVal = override[key];\n if (isPlainObject(baseVal) && isPlainObject(overVal)) {\n result[key] = { ...baseVal, ...overVal };\n } else {\n result[key] = overVal;\n }\n }\n return result;\n}\n\n/** True for `{}` literals — false for arrays, null, Date, etc. */\nfunction isPlainObject(val: unknown): val is Record<string, unknown> {\n return val !== null && typeof val === 'object' && !Array.isArray(val);\n}\n\n/** @internal Exported for testing. */\nexport function generateWranglerConfig(\n config: TimberConfig,\n options: CloudflareAdapterOptions\n): Record<string, unknown> {\n const compatDate = options.compatibilityDate ?? new Date().toISOString().slice(0, 10);\n\n const flags = options.compatibilityFlags ?? ['nodejs_compat'];\n\n const base: Record<string, unknown> = {\n name: 'timber-app',\n main: '_worker.js',\n compatibility_date: compatDate,\n compatibility_flags: flags,\n // The build output is already fully bundled by Vite — skip wrangler's\n // esbuild pass to avoid issues with top-level await and module format.\n no_bundle: true,\n find_additional_modules: true,\n rules: [{ type: 'ESModule', globs: ['**/*.js'] }],\n assets: {\n directory: './static',\n binding: 'ASSETS',\n },\n // Native trace destination — required for timber's pipeline spans\n // (emitted via tracing.enterSpan in the generated _worker.js) to appear\n // in the Cloudflare Observability dashboard. Gates traces only; does\n // not enable Workers Logs. Override via the `wrangler` escape hatch:\n // wrangler: { observability: { traces: { enabled: false } } }.\n observability: {\n traces: { enabled: true },\n },\n };\n\n // Layer 1: merge bindings-generated sections into base\n const bindingsConfig = generateBindingsConfig(options.bindings);\n const merged = { ...base, ...bindingsConfig };\n\n // Layer 2: wrangler escape hatch with deep merge for nested plain objects.\n // A shallow spread would replace entire sections like durable_objects and\n // queues — e.g. adding migrations via wrangler would drop the generated\n // durable_objects.bindings. Deep merge preserves generated keys while\n // letting wrangler override on actual key-level conflicts.\n if (options.wrangler) {\n return shallowDeepMerge(merged, options.wrangler);\n }\n\n return merged;\n}\n\n// ─── Preview ─────────────────────────────────────────────────────────────────\n\n/** Command descriptor for preview — testable without spawning a process. */\nexport interface PreviewCommand {\n command: string;\n args: string[];\n cwd: string;\n}\n\n/** @internal Exported for testing. */\nexport function generatePreviewCommand(buildDir: string): PreviewCommand {\n const cfDir = join(buildDir, 'cloudflare');\n return {\n command: 'wrangler',\n args: ['dev', '--local', '--config', join(cfDir, 'wrangler.jsonc')],\n cwd: cfDir,\n };\n}\n\n/**\n * Spawn a long-running preview process and pipe stdio to the parent.\n * Resolves when the process exits.\n */\nfunction spawnPreviewProcess(command: string, args: string[], cwd: string): Promise<void> {\n return new Promise<void>((resolve, reject) => {\n const child = execFile(command, args, { cwd }, (err) => {\n if (err) reject(err);\n else resolve();\n });\n child.stdout?.pipe(process.stdout);\n child.stderr?.pipe(process.stderr);\n });\n}\n\n// ─── Cloudflare Workers type stubs ───────────────────────────────────────────\n// Local type declarations so this file compiles without @cloudflare/workers-types.\n// These are NOT declared globally — that would clash with @cloudflare/workers-types\n// when users install it. Instead, they're module-scoped and used only within\n// this file's function signatures.\n\n/** @internal Minimal stub — use @cloudflare/workers-types for full types. */\nexport interface CfExecutionContext {\n waitUntil(promise: Promise<unknown>): void;\n passThroughOnException(): void;\n}\n"],"mappings":";;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