@brust/core 2.0.0-alpha.2 → 2.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +64 -6
  2. package/native/index.d.ts +65 -2
  3. package/native/index.js +59 -54
  4. package/package.json +16 -8
  5. package/src/actions/client.ts +22 -5
  6. package/src/actions/load.ts +2 -1
  7. package/src/actions/prefix.ts +24 -2
  8. package/src/actions-bridge.ts +29 -11
  9. package/src/browser.ts +3 -1
  10. package/src/build/actions-only.ts +86 -0
  11. package/src/build/build-eval.ts +199 -0
  12. package/src/build/build-icons.ts +69 -0
  13. package/src/build/bundle.ts +48 -12
  14. package/src/build/cf-assets.ts +83 -0
  15. package/src/build/cf-core.ts +90 -0
  16. package/src/build/cf-entry.ts +76 -0
  17. package/src/build/cf-wrangler.ts +169 -0
  18. package/src/build/cloudflare.ts +315 -0
  19. package/src/build/compile.ts +41 -4
  20. package/src/build/config.ts +255 -10
  21. package/src/build/css.ts +286 -0
  22. package/src/build/index.ts +199 -16
  23. package/src/build/manifest.ts +41 -4
  24. package/src/build/scan.ts +59 -20
  25. package/src/build/ssg-render.ts +65 -0
  26. package/src/build/ssg.ts +146 -0
  27. package/src/build/static-worker.ts +91 -0
  28. package/src/build/static.ts +258 -0
  29. package/src/build-time.ts +10 -0
  30. package/src/cache-adapter/bridge.ts +91 -0
  31. package/src/cache-adapter/conformance.ts +226 -0
  32. package/src/cache-adapter/index.ts +10 -0
  33. package/src/cache-adapter/load.ts +144 -0
  34. package/src/cache-adapter/memory.ts +229 -0
  35. package/src/cache-adapter/runtime.ts +124 -0
  36. package/src/cache-adapter/types.ts +96 -0
  37. package/src/cache-adapter/workers.ts +118 -0
  38. package/src/cache.ts +4 -1
  39. package/src/cf.ts +19 -0
  40. package/src/cli.ts +50 -2
  41. package/src/config.ts +127 -25
  42. package/src/css-modules.d.ts +8 -0
  43. package/src/dev/bun.ts +365 -0
  44. package/src/dev/client.ts +92 -0
  45. package/src/dev/devtools-data.ts +23 -0
  46. package/src/dev/devtools.ts +356 -0
  47. package/src/dev/diag.ts +30 -0
  48. package/src/dev/proxy.ts +236 -0
  49. package/src/dev/tui.ts +144 -0
  50. package/src/dev/watch.ts +81 -0
  51. package/src/dev.ts +93 -0
  52. package/src/handlers.ts +282 -0
  53. package/src/index.ts +3 -1
  54. package/src/native.ts +38 -1
  55. package/src/routes.ts +20 -1
  56. package/src/run.ts +46 -9
  57. package/src/server-handlers.ts +32 -0
  58. package/src/worker.ts +125 -245
package/src/worker.ts CHANGED
@@ -1,274 +1,91 @@
1
1
  // The Bun side of contract 7 (`crates/brust-server/src/protocol.rs`): requests arrive as inline
2
2
  // JSON, responses go into this worker's SharedArrayBuffer slot (SAB rule, `dispatch.rs`). A
3
3
  // handler NEVER rejects across the tsfn: every failure becomes `{error}` / `{id, error}`.
4
- import { join, resolve } from 'node:path'
4
+ import { appendFileSync } from 'node:fs'
5
+ import { dirname, join, resolve } from 'node:path'
5
6
  import { workerData } from 'node:worker_threads'
6
- import { runFetch, setActionsApp, setActionsLimits } from './actions-bridge'
7
+ import { type HeapIo, runFetch, setActionsApp, setActionsLimits } from './actions-bridge'
7
8
  import { DEFAULT_MAX_RESPONSE_BYTES } from './actions/prefix'
8
- import { planJobs, registerWorker } from './native'
9
- import { installServerResolver, type StoreSnapshot } from './store/core-adapter'
10
- import { collectSnapshot, getServerInstance, runInStoreContext, touchStores } from './store/server-context'
11
- import { type FlatRoute, flattenRoutes, isHttpErrorTrigger, isVerdict, type LoaderReq, type Route, type Verdict } from './routes'
9
+ import { type CacheBridgeDeps, runCache } from './cache-adapter/bridge'
10
+ import type { CacheManifest } from './cache-adapter/load'
11
+ import { type Handlers, type JobsModule, type ManifestLike, makeHandlers, type SsgRecord, toJson } from './handlers'
12
+ import { cacheClear, cacheL2Count, cacheReplay, planJobs, planJobsBuf, putFetchReply, registerWorker, takeFetchBody } from './native'
13
+ import { flattenRoutes, type Route } from './routes'
12
14
 
13
- installServerResolver(getServerInstance)
15
+ export * from './handlers'
14
16
 
15
- // ---- wire shapes (protocol.rs, camelCase) ----
16
-
17
- export interface LoaderRequest {
18
- routeId: string
19
- params: Record<string, string>
20
- path: string
21
- req: LoaderReq
22
- /** M3-P P6: the chain has jobs — plan them in this call (`planJobs`). Optional to honour. */
23
- plan?: boolean
24
- }
25
-
26
- export type LoaderResponse =
27
- | { ok: true; data: Record<string, unknown>; headers?: Record<string, string>; store?: StoreSnapshot | null }
28
- | { verdict: 'notFound'; data: unknown; store?: StoreSnapshot | null }
29
- | { verdict: 'redirect'; location: string; status: number }
30
- | { verdict: 'httpError'; status: number; body: string }
31
- | { error: string }
32
-
33
- export type JobKind = 'precompute' | 'ssr'
34
-
35
- export interface JobCall {
36
- /** Opaque, echoed back: `<componentId>/<jobId>[/<row>]` for a chain job,
37
- * `<parentId>/<childId>_<k>/<jobId>[/<row>]` for an inlined child instance (pipeline.rs `plan_one`). */
38
- id: string
39
- componentId: string
40
- kind: JobKind
41
- // biome-ignore lint/suspicious/noExplicitAny: job inputs are arbitrary JSON
42
- inputs: any
43
- /** ssr: the react component to render (D6). */
44
- target?: string
45
- row?: number
46
- }
47
-
48
- export interface JobsRequest {
49
- jobs: JobCall[]
50
- }
51
-
52
- export type JobResult = { id: string; value: unknown } | { id: string; error: string }
17
+ // ---- SAB slot writer ----
53
18
 
54
- export interface JobsResponse {
55
- results: JobResult[]
56
- }
19
+ const enc = new TextEncoder()
57
20
 
58
- /** The default export of `<dist>/<manifest.jobs_module>` (built by Task 6): component id → job fns. */
59
- // biome-ignore lint/suspicious/noExplicitAny: props are whatever the component takes
60
- export type JobsModule = Record<string, { precompute?: (props: any) => unknown; ssr?: (props: any) => string | Promise<string> }>
21
+ /** L10: the default `maxLoaderResponseBytes` (config.ts keeps the range). */
22
+ export const DEFAULT_MAX_LOADER_RESPONSE_BYTES = 8_388_608
61
23
 
62
- /** The slice of `manifest.json` the worker reads: ssr job `literals` (S6 amendment). */
63
- export interface ManifestLike {
64
- routes?: { id: string; chain: string[] }[]
65
- components: Record<
66
- string,
67
- {
68
- jobs: { id: string; kind?: string; target?: string; literals?: Record<string, unknown> }[]
69
- children?: { id: string }[]
70
- stores?: string[]
71
- }
72
- >
73
- jobs_module?: string
74
- /** `brust build` (M3-F A3): the actions entry, relative to dist. Absent/null = no actions. */
75
- actions?: { entry: string; prefix: string; maxBodyBytes: number; maxResponseBytes?: number } | null
24
+ /** L10: a reply too large for the slot crosses by heap: `HeapIo.putReply` hands Rust the bytes, and the slot holds
25
+ * the 5-byte marker `[0x00][u32 LE n]` (JSON never starts with 0x00). `cap` = `maxLoaderResponseBytes`. */
26
+ export const HEAP_MARKER_BYTES = 5
27
+ export interface HeapReply {
28
+ io: HeapIo
29
+ /** `planJobsBuf` (napi): plan a loader response handed over directly. */
30
+ planBuf: (slot: number, buf: Uint8Array) => string
31
+ /** Default: the worker's `maxLoaderResponseBytes` (env `BRUST_MAX_LOADER_RESPONSE_BYTES`, 8 MiB). */
32
+ cap?: number
76
33
  }
77
-
78
- // ---- SAB slot writer ----
79
-
80
- const enc = new TextEncoder()
34
+ const nativeHeap: HeapReply = { io: cacheHeapIo(), planBuf: (slot, buf) => planJobsBuf(slot, buf) }
35
+ let loaderCap = DEFAULT_MAX_LOADER_RESPONSE_BYTES
81
36
 
82
37
  /** Writes `json` as UTF-8 at the start of `slot`'s sub-region `[slot*sub, slot*sub+sub)`,
83
38
  * `sub = floor(view.byteLength / slots)`; returns the byte length (> 0, ≤ sub). ONE copy: the
84
39
  * encoder writes straight into the slot (M3-P P1); it stops at the last whole code point that
85
40
  * fits, so `read < json.length` means "did not fit" and the neighbouring slot is never touched.
86
- * A response that does not fit is replaced by a small `{"error":"response too large: <n> > <sub>"}`
87
- * (`n` = its exact UTF-8 length, computed only on this path). */
88
- export function writeSlot(view: Uint8Array, slot: number, slots: number, json: string): number {
41
+ * A response that does not fit goes by heap when `io` and `cap` are given (L10): encoded once, handed to
42
+ * Rust with `io.putReply`, and the slot holds the 5-byte marker. Over `cap` (or declined), or with no `io`, it is
43
+ * replaced by a small `{"error":"response too large: <n> > <limit>"}` (`n` = its exact UTF-8 length, computed
44
+ * only on this path). */
45
+ export function writeSlot(view: Uint8Array, slot: number, slots: number, json: string, io?: HeapIo, cap?: number): number {
89
46
  const sub = Math.floor(view.byteLength / Math.max(1, slots))
90
47
  const dst = view.subarray(slot * sub, slot * sub + sub)
91
48
  const r = enc.encodeInto(json, dst)
92
49
  if (r.read === json.length) return r.written
93
- const err = JSON.stringify({ error: `response too large: ${Buffer.byteLength(json)} > ${sub}` })
50
+ let why = `response too large: ${Buffer.byteLength(json)} > ${sub}`
51
+ if (io && cap !== undefined && sub >= HEAP_MARKER_BYTES) {
52
+ const bytes = enc.encode(json)
53
+ const n = heapMarker(dst, slot, bytes, io, cap)
54
+ if (n > 0) return n
55
+ // Name the real reason: over the cap, or a put Rust declined while under it (idle slot, a second put).
56
+ why = bytes.length > cap ? `response too large: ${bytes.length} > ${cap} (maxLoaderResponseBytes)` : `heap reply declined: ${bytes.length} bytes did not fit the ${sub}-byte slot and Rust refused the handover`
57
+ }
58
+ const err = JSON.stringify({ error: why })
94
59
  const e = enc.encodeInto(err, dst)
95
60
  if (e.read === err.length) return e.written
96
61
  // Not even the message fits: the first `sub` bytes of a fixed one (ASCII, so byte = char).
97
62
  return enc.encodeInto('{"error":"too large"}', dst).written
98
63
  }
99
64
 
65
+ /** Hand `bytes` to Rust and write the marker at `dst`; 0 when over `cap` or Rust declined. */
66
+ function heapMarker(dst: Uint8Array, slot: number, bytes: Uint8Array, io: HeapIo, cap: number): number {
67
+ if (bytes.length > cap || !io.putReply(slot, bytes)) return 0
68
+ dst[0] = 0
69
+ new DataView(dst.buffer, dst.byteOffset + 1, 4).setUint32(0, bytes.length, true)
70
+ return HEAP_MARKER_BYTES
71
+ }
72
+
100
73
  /** `planJobs` (napi): the misses' JobsRequest JSON, '' (every job hit) or PLAN_DECLINED. */
101
74
  export type PlanJobs = (slot: number, len: number) => string
102
75
  /** `planJobs` declined: the loader response already in the slot is the answer (Rust plans after the call). */
103
76
  export const PLAN_DECLINED = '!'
104
77
  const PLANNED = { planned: true } as const
105
78
 
106
- function toJson(out: unknown): string {
107
- try {
108
- return JSON.stringify(out)
109
- } catch (e) {
110
- return JSON.stringify({ error: `response not serialisable: ${String(e)}` })
111
- }
112
- }
113
-
114
- // ---- literals (S6 amendment: the worker merges an ssr job's `literals` over `call.inputs`) ----
115
-
116
- /** `"<componentId>/<jobId>"` → that job record's `literals` (only records that carry some). */
117
- export function literalsIndex(manifest: ManifestLike | undefined): Map<string, Record<string, unknown>> {
118
- const out = new Map<string, Record<string, unknown>>()
119
- for (const [cid, c] of Object.entries(manifest?.components ?? {}))
120
- for (const j of c.jobs ?? []) if (j.literals && Object.keys(j.literals).length > 0) out.set(`${cid}/${j.id}`, j.literals)
121
- return out
122
- }
123
-
124
- /** The manifest job id of a call: the segment after `componentId` in a chain id
125
- * (`<cid>/<jobId>[/<row>]`), else the third segment of a child-instance id
126
- * (`<parentId>/<cid>_<k>/<jobId>[/<row>]`). */
127
- export function jobIdOf(call: Pick<JobCall, 'id' | 'componentId'>): string | undefined {
128
- const parts = call.id.split('/')
129
- return parts[0] === call.componentId ? parts[1] : parts[2]
130
- }
131
-
132
- // ---- touched stores (M3-F D4) ----
133
-
134
- /** route id -> sorted unique union of `stores` over the route's chain, walking each component's
135
- * `children[].id` and `jobs[].target` transitively. Routes with an empty union are absent. */
136
- export function routeStoresIndex(manifest: ManifestLike | undefined): Map<string, string[]> {
137
- const out = new Map<string, string[]>()
138
- const comps = manifest?.components ?? {}
139
- for (const r of manifest?.routes ?? []) {
140
- const acc = new Set<string>()
141
- const seen = new Set<string>()
142
- const walk = (id: string) => {
143
- if (seen.has(id)) return
144
- seen.add(id)
145
- const c = comps[id]
146
- if (!c) return
147
- for (const p of c.stores ?? []) acc.add(p)
148
- for (const k of c.children ?? []) walk(k.id)
149
- for (const j of c.jobs ?? []) if (j.target) walk(j.target)
150
- }
151
- for (const id of r.chain ?? []) walk(id)
152
- if (acc.size > 0) out.set(r.id, [...acc].sort())
153
- }
154
- return out
155
- }
156
-
157
- /** R1: `store` is assigned only when a store was touched; absent otherwise. */
158
- function withStore<T extends LoaderResponse>(r: T): T {
159
- const s = collectSnapshot()
160
- if (s) (r as { store?: StoreSnapshot | null }).store = s
161
- return r
162
- }
163
-
164
- // ---- handlers ----
165
-
166
- /** `parent`: the data the chain merged before the verdict. A notFound renders the route's
167
- * template (layouts included), so the layouts' data stays under the verdict's own keys. */
168
- function verdictJson(v: Verdict, parent: Record<string, unknown>): LoaderResponse {
169
- if (v.status === 404) {
170
- const own = v.data ?? {}
171
- return { verdict: 'notFound', data: typeof own === 'object' && !Array.isArray(own) ? { ...parent, ...own } : own }
172
- }
173
- return { verdict: 'redirect', location: v.headers?.Location ?? '/', status: v.status }
174
- }
175
-
176
- export interface Handlers {
177
- loader(req: LoaderRequest): Promise<LoaderResponse>
178
- jobs(req: JobsRequest): Promise<JobsResponse>
179
- }
180
-
181
- /** `leaves` from `flattenRoutes` (ids must match the manifest's route ids); `jobs` the jobs
182
- * module; `manifest` supplies ssr `literals` (omit → no merge). */
183
- export function makeHandlers(opts: {
184
- leaves: FlatRoute[]
185
- jobs: JobsModule
186
- manifest?: ManifestLike
187
- /** `jobs.js` `stores` table (R2): C3 path -> module namespace. */
188
- stores?: Record<string, object>
189
- }): Handlers {
190
- const byId = new Map(opts.leaves.map((l) => [l.id, l]))
191
- const literals = literalsIndex(opts.manifest)
192
- const jobs = opts.jobs
193
- const storeIdx = routeStoresIndex(opts.manifest)
194
- const storeTable = opts.stores ?? {}
195
-
196
- async function runJob(call: JobCall): Promise<JobResult> {
197
- const owner = call.kind === 'ssr' ? (call.target ?? call.componentId) : call.componentId // D6
198
- const fn = jobs[owner]?.[call.kind]
199
- if (typeof fn !== 'function') return { id: call.id, error: `no ${call.kind} job for component ${owner}` }
200
- try {
201
- let props = call.inputs
202
- if (call.kind === 'ssr') {
203
- const jid = jobIdOf(call)
204
- const lit = jid === undefined ? undefined : literals.get(`${call.componentId}/${jid}`)
205
- if (lit) props = { ...(props ?? {}), ...lit }
206
- // Precompute runs outside any scope (D8); ssr runs in a scope seeded from `__store`, which props never see.
207
- const seed = (props?.__store ?? null) as StoreSnapshot | null
208
- if (props && '__store' in props) {
209
- const { __store: _, ...rest } = props
210
- props = rest
211
- }
212
- const p = props
213
- return { id: call.id, value: await runInStoreContext(() => fn(p), seed) }
214
- }
215
- return { id: call.id, value: await fn(props) }
216
- } catch (e) {
217
- return { id: call.id, error: String(e) }
218
- }
219
- }
220
-
221
- return {
222
- // 0.1.x `runNativeChainLoaders`: root → leaf, flat merge (later keys win), first verdict stops
223
- // (a notFound keeps what the chain merged so far: verdictJson).
224
- async loader(req) {
225
- const leaf = byId.get(req.routeId)
226
- if (!leaf) return { error: `unknown routeId ${req.routeId}` }
227
- // One object for the whole chain: each level's keys are assigned INTO it (later keys win,
228
- // first-seen key order — what `{...merged, ...r}` produced) instead of re-spreading every
229
- // key once per level (M3-P P1). `merged` is fresh per call; a loader's own object is never
230
- // written to. Null prototype: with no `__proto__` setter to hit, an own "__proto__" key in a
231
- // loader's data is assigned as data, as the spread copied it.
232
- return runInStoreContext(async () => {
233
- const merged: Record<string, unknown> = Object.create(null)
234
- try {
235
- const touch = storeIdx.get(req.routeId)
236
- if (touch) touchStores(touch, storeTable)
237
- for (const node of leaf.chain) {
238
- if (!node.loader) continue
239
- let r: unknown
240
- try {
241
- r = await node.loader({ params: req.params, path: req.path, req: req.req })
242
- } catch (e) {
243
- if (isHttpErrorTrigger(e)) return { verdict: 'httpError', status: e.status, body: e.body } as LoaderResponse
244
- throw e
245
- }
246
- if (isVerdict(r)) {
247
- const v = verdictJson(r, merged)
248
- return 'verdict' in v && v.verdict === 'notFound' ? withStore(v) : v
249
- }
250
- if (r && typeof r === 'object') Object.assign(merged, r)
251
- }
252
- return withStore({ ok: true, data: merged } as LoaderResponse)
253
- } catch (e) {
254
- return { error: String(e) } as LoaderResponse
255
- }
256
- })
257
- },
258
- // One request carries every job of the page; a per-job failure keeps the other results.
259
- async jobs(req) {
260
- return { results: await Promise.all((req.jobs ?? []).map(runJob)) }
261
- },
262
- }
263
- }
264
-
265
79
  /** The tsfn callback for `registerWorker`. Never rejects: a parse error, an unknown kind or a
266
80
  * throwing handler is written to the slot as `{error}`. A loader request with `plan: true` whose
267
81
  * chain answered `ok` is planned in the same call (`plan`, the napi `planJobs` by default — the
268
- * addon is touched only then). */
269
- export function makeDispatch(h: Handlers, view: Uint8Array, slots: number, plan: PlanJobs = planJobs) {
82
+ * addon is touched only then). A loader/jobs/planned reply that does not fit the slot crosses by heap
83
+ * (`heap`, the napi addon by default; L10). */
84
+ export function makeDispatch(h: Handlers, view: Uint8Array, slots: number, plan: PlanJobs = planJobs, heap: HeapReply = nativeHeap) {
85
+ const cap = () => heap.cap ?? loaderCap
270
86
  return async (kind: string, requestJson: string, slot: number): Promise<number> => {
271
87
  if (kind === 'fetch') return runFetch(requestJson, view, slot, slots)
88
+ if (kind === 'cache') return runCache(requestJson, view, slot, slots, cacheHeapIo(), cacheDeps)
272
89
  let out: unknown
273
90
  try {
274
91
  const req = JSON.parse(requestJson)
@@ -278,9 +95,20 @@ export function makeDispatch(h: Handlers, view: Uint8Array, slots: number, plan:
278
95
  // One round trip (M3-P P6): the ctx goes to Rust through this slot, synchronously, and only
279
96
  // the jobs Rust found missing run here; the final answer replaces it in the same slot.
280
97
  const json = toJson(out)
281
- const n = writeSlot(view, slot, slots, json)
282
- const misses = plan(slot, n)
283
- if (misses === PLAN_DECLINED) return n
98
+ const sub = Math.floor(view.byteLength / Math.max(1, slots))
99
+ const dst = view.subarray(slot * sub, slot * sub + sub)
100
+ const w = enc.encodeInto(json, dst)
101
+ let misses: string
102
+ if (w.read === json.length) {
103
+ misses = plan(slot, w.written)
104
+ if (misses === PLAN_DECLINED) return w.written
105
+ } else {
106
+ // L10: too large for the slot — plan from the bytes directly; a declined plan hands them over as the answer.
107
+ const bytes = enc.encode(json)
108
+ if (bytes.length > cap()) return writeSlot(view, slot, slots, json, heap.io, cap())
109
+ misses = heap.planBuf(slot, bytes)
110
+ if (misses === PLAN_DECLINED) return writeSlot(view, slot, slots, json, heap.io, cap())
111
+ }
284
112
  out = misses === '' ? PLANNED : { planned: true, results: (await h.jobs(JSON.parse(misses))).results }
285
113
  }
286
114
  } else if (kind === 'jobs') out = await h.jobs(req)
@@ -288,16 +116,51 @@ export function makeDispatch(h: Handlers, view: Uint8Array, slots: number, plan:
288
116
  } catch (e) {
289
117
  out = { error: String(e) }
290
118
  }
291
- return writeSlot(view, slot, slots, toJson(out))
119
+ return writeSlot(view, slot, slots, toJson(out), heap.io, cap())
292
120
  }
293
121
  }
294
122
 
123
+ // ---- cache adapter (the `cache` call kind) ----
124
+
125
+ function cacheHeapIo(): HeapIo {
126
+ return { takeBody: (slot) => takeFetchBody(slot), putReply: (slot, body) => putFetchReply(slot, body) }
127
+ }
128
+ const count = (name: string) => void cacheL2Count(name)
129
+ let cacheDeps: CacheBridgeDeps = { store: undefined, getTimeoutMs: 50, maxBodyBytes: 1_048_576, count }
130
+
131
+ /** The `cache` call's store (tests; `startWorker` sets it from `manifest.cache`). */
132
+ export function setCacheBridge(d: CacheBridgeDeps): void {
133
+ cacheDeps = d
134
+ }
135
+
136
+ /** Load `manifest.cache`'s adapter in this worker (R8): a custom module from dist, a built-in from the
137
+ * app root; `init`, then `cache.invalidate` publishes through it. Worker 0 also subscribes after
138
+ * registering (R10). */
139
+ async function startCacheAdapter(m: CacheManifest, dist: string, appDir: string, workerId: number) {
140
+ const { loadAdapter } = await import('./cache-adapter/load')
141
+ const { setCacheRuntime } = await import('./cache-adapter/runtime')
142
+ const adapter = await loadAdapter(m, 'bun', (spec, builtin) => import(builtin ? Bun.resolveSync(spec, appDir) : join(dist, spec)), process.env)
143
+ await adapter.init?.({ namespace: m.namespace, buildId: m.buildId, runtime: 'bun', workerId })
144
+ const origin = process.env.BRUST_CACHE_ORIGIN || crypto.randomUUID()
145
+ setCacheRuntime({ adapter, origin, defer: (p) => void p.catch(() => {}), count, replay: (args, seq) => void cacheReplay(args, seq) })
146
+ setCacheBridge({ store: adapter.store, getTimeoutMs: m.getTimeoutMs, maxBodyBytes: m.maxBodyBytes, count })
147
+ return { adapter, origin }
148
+ }
149
+
150
+ // ---- static output (gaps round R9.5) ----
151
+
152
+ /** Appends each record as one JSON line to `file` (every worker of the render appends to it). */
153
+ function ssgLog(file: string): (r: SsgRecord) => void {
154
+ return (r) => appendFileSync(file, `${JSON.stringify(r)}\n`)
155
+ }
156
+
295
157
  // ---- worker entry ----
296
158
 
159
+ /** The default response slot (`slotBytes`, config.ts: 64 KiB..16 MiB). */
297
160
  export const SLOT_BYTES = 256 * 1024
298
161
 
299
162
  export interface StartWorkerOptions {
300
- /** The response buffer to register (`SLOT_BYTES * slots` bytes). `run()` allocates it on the
163
+ /** The response buffer to register (`slotBytes * slots` bytes). `run()` allocates it on the
301
164
  * MAIN thread and keeps it for the process lifetime, passing it as `workerData.sab`: the Rust
302
165
  * dispatcher holds a raw pointer into it until the process exits, so its backing store must not
303
166
  * die with this worker (`crates/brust-napi/src/dispatch.rs` BufPtr). Absent (tests, a worker
@@ -316,11 +179,18 @@ export async function startWorker(opts: StartWorkerOptions = {}): Promise<number
316
179
  const env = process.env
317
180
  const slots = Math.max(1, Number.parseInt(env.BRUST_RENDER_SLOTS ?? '1', 10) || 1)
318
181
  const dist = resolve(env.BRUST_DIST_DIR ?? 'dist')
319
- const entry = env.BRUST_APP_ENTRY
182
+ // L16 R16.3: an actions-only dist has no routes module (so no BRUST_APP_ENTRY either).
183
+ const manifestFile = join(dist, 'manifest.json')
184
+ const hasManifest = await Bun.file(manifestFile).exists()
185
+ const actionsOnly = hasManifest && !env.BRUST_APP_ENTRY && ((await Bun.file(manifestFile).json()) as { actions_only?: boolean }).actions_only === true
186
+ const entry = env.BRUST_APP_ENTRY ?? (actionsOnly ? join(dist, 'index.js') : undefined)
320
187
  if (!entry) throw new Error('BRUST_APP_ENTRY is not set')
321
- const manifest = (await Bun.file(join(dist, 'manifest.json')).json()) as ManifestLike
322
- const app = await import(resolve(entry))
323
- const routes = (app.routes ?? app.default) as Route[] | undefined
188
+ const manifest = (await Bun.file(manifestFile).json()) as ManifestLike & { cache?: CacheManifest | null; actions_only?: boolean }
189
+ let routes: Route[] | undefined = []
190
+ if (!manifest.actions_only) {
191
+ const app = await import(resolve(entry))
192
+ routes = (app.routes ?? app.default) as Route[] | undefined
193
+ }
324
194
  if (!Array.isArray(routes)) throw new Error(`${entry} exports no routes (export const routes or default)`)
325
195
  const jobsMod = await import(join(dist, manifest.jobs_module ?? 'jobs.js'))
326
196
  const jobs = (jobsMod.default ?? jobsMod) as JobsModule
@@ -330,15 +200,25 @@ export async function startWorker(opts: StartWorkerOptions = {}): Promise<number
330
200
  setActionsApp(await loadActionsApp(join(dist, manifest.actions.entry), manifest.actions.prefix))
331
201
  setActionsLimits({ maxResponseBytes: manifest.actions.maxResponseBytes ?? DEFAULT_MAX_RESPONSE_BYTES })
332
202
  }
333
- sab = opts.sab ?? new SharedArrayBuffer(SLOT_BYTES * slots)
203
+ loaderCap = Number.parseInt(env.BRUST_MAX_LOADER_RESPONSE_BYTES ?? '', 10) || DEFAULT_MAX_LOADER_RESPONSE_BYTES
204
+ const slotBytes = Number.parseInt(env.BRUST_SLOT_BYTES ?? '', 10) || SLOT_BYTES
205
+ const workerId = Number.parseInt(env.BRUST_WORKER_ID ?? '0', 10) || 0
206
+ const l2 = manifest.cache ? await startCacheAdapter(manifest.cache, dist, dirname(resolve(entry)), workerId) : null
207
+ sab = opts.sab ?? new SharedArrayBuffer(slotBytes * slots)
334
208
  view = new Uint8Array(sab)
335
209
  const handlers = makeHandlers({
336
210
  leaves: flattenRoutes(routes).leaves,
337
211
  jobs,
338
212
  manifest,
339
213
  stores: (jobsMod.stores ?? {}) as Record<string, object>,
214
+ ...(env.BRUST_SSG === '1' && env.BRUST_SSG_LOG ? { ssg: ssgLog(env.BRUST_SSG_LOG) } : {}),
340
215
  })
341
- return registerWorker(view, slots, makeDispatch(handlers, view, slots))
216
+ const id = registerWorker(view, slots, makeDispatch(handlers, view, slots))
217
+ if (l2 && env.BRUST_WORKER_ID === '0') {
218
+ const { startSubscriber } = await import('./cache-adapter/runtime')
219
+ await startSubscriber(l2.adapter, l2.origin, { replay: (args, seq) => void cacheReplay(args, seq), clear: () => void cacheClear(), count })
220
+ }
221
+ return id
342
222
  }
343
223
 
344
224
  // Auto-start only inside a Bun `Worker` spawned by `run()` (BRUST_WORKER_ID set). Not