@brust/core 2.0.0-alpha.1 → 2.0.0-alpha.10

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 (63) hide show
  1. package/README.md +66 -6
  2. package/native/index.d.ts +87 -5
  3. package/native/index.js +61 -54
  4. package/package.json +22 -9
  5. package/src/actions/client.ts +22 -5
  6. package/src/actions/load.ts +2 -1
  7. package/src/actions/prefix.ts +27 -3
  8. package/src/actions-bridge.ts +76 -19
  9. package/src/browser.ts +3 -1
  10. package/src/build/actions-only.ts +86 -0
  11. package/src/build/build-eval.ts +319 -0
  12. package/src/build/build-icons.ts +69 -0
  13. package/src/build/bundle.ts +278 -43
  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 +317 -0
  19. package/src/build/compile.ts +72 -4
  20. package/src/build/config.ts +274 -15
  21. package/src/build/css.ts +275 -0
  22. package/src/build/features.ts +110 -0
  23. package/src/build/index.ts +223 -20
  24. package/src/build/manifest.ts +59 -8
  25. package/src/build/pkg-src.ts +383 -0
  26. package/src/build/scan.ts +61 -20
  27. package/src/build/server-only.ts +39 -0
  28. package/src/build/ssg-render.ts +65 -0
  29. package/src/build/ssg.ts +146 -0
  30. package/src/build/static-worker.ts +91 -0
  31. package/src/build/static.ts +258 -0
  32. package/src/build/tailwind.ts +117 -0
  33. package/src/build/tsconfig-paths.ts +67 -0
  34. package/src/build-time.ts +10 -0
  35. package/src/cache-adapter/bridge.ts +91 -0
  36. package/src/cache-adapter/conformance.ts +226 -0
  37. package/src/cache-adapter/index.ts +10 -0
  38. package/src/cache-adapter/load.ts +144 -0
  39. package/src/cache-adapter/memory.ts +229 -0
  40. package/src/cache-adapter/runtime.ts +124 -0
  41. package/src/cache-adapter/types.ts +96 -0
  42. package/src/cache-adapter/workers.ts +118 -0
  43. package/src/cache.ts +4 -1
  44. package/src/cf.ts +19 -0
  45. package/src/cli.ts +50 -2
  46. package/src/config.ts +127 -25
  47. package/src/css-modules.d.ts +8 -0
  48. package/src/dev/bun.ts +365 -0
  49. package/src/dev/client.ts +92 -0
  50. package/src/dev/devtools-data.ts +23 -0
  51. package/src/dev/devtools.ts +356 -0
  52. package/src/dev/diag.ts +30 -0
  53. package/src/dev/proxy.ts +236 -0
  54. package/src/dev/tui.ts +144 -0
  55. package/src/dev/watch.ts +81 -0
  56. package/src/dev.ts +93 -0
  57. package/src/handlers.ts +282 -0
  58. package/src/index.ts +3 -1
  59. package/src/native.ts +49 -2
  60. package/src/routes.ts +20 -1
  61. package/src/run.ts +48 -11
  62. package/src/server-handlers.ts +32 -0
  63. package/src/worker.ts +129 -245
package/src/native.ts CHANGED
@@ -22,6 +22,8 @@ export interface StartOptions {
22
22
  workers: number
23
23
  claimTimeoutMs?: number
24
24
  callTimeoutMs?: number
25
+ /** L10: the biggest loader/jobs reply that may cross the heap when it does not fit the slot; absent = 8388608. */
26
+ maxLoaderResponseBytes?: number
25
27
  /** tokio I/O threads; absent = one per core. */
26
28
  workerThreads?: number
27
29
  l1Capacity?: number
@@ -31,6 +33,20 @@ export interface StartOptions {
31
33
  cors?: CorsOptions
32
34
  /** The actions subtree (M3-F A3); absent = no actions. Boot error `actions: …` when invalid. */
33
35
  actions?: ActionsOptions
36
+ /** L16: what the page tells the browser `client()` (base URL, credentials, a non-default prefix); absent = from `actions` alone. */
37
+ actionsClient?: ActionsClientOptions
38
+ /** The cache adapter (`manifest.cache`); absent = none (byte-identical server). */
39
+ l2?: L2Options
40
+ /** `brust dev` only (L14): a request carrying this token (`x-brust-dev-token`) is traced; absent in production. */
41
+ devToken?: string
42
+ }
43
+ /** napi `NapiL2Options`; defaults 50 ms, 1048576 bytes. */
44
+ export interface L2Options {
45
+ adapter: string
46
+ getTimeoutMs?: number
47
+ maxBodyBytes?: number
48
+ namespace: string
49
+ buildId: string
34
50
  }
35
51
  /** napi `NapiCorsOptions` (camelCase keys; snake_case keys are silently dropped). */
36
52
  export interface CorsOptions {
@@ -41,12 +57,22 @@ export interface CorsOptions {
41
57
  credentials?: boolean
42
58
  maxAgeSeconds?: number
43
59
  }
44
- /** napi `NapiActionsOptions`; defaults `/_brust/action`, 262144. */
60
+ /** napi `NapiActionsClient`. */
61
+ export interface ActionsClientOptions {
62
+ prefix?: string
63
+ baseUrl?: string
64
+ credentials?: 'same-origin' | 'include' | 'omit'
65
+ }
66
+ /** napi `NapiActionsOptions`; defaults `/_brust/action`, 1048576, 8388608. */
45
67
  export interface ActionsOptions {
46
68
  prefix?: string
47
69
  maxBodyBytes?: number
70
+ /** F82: response body cap (bytes). Default 8388608. */
71
+ maxResponseBytes?: number
72
+ /** F87: cross-site mutating requests. Default `reject`. */
73
+ crossSite?: 'reject' | 'allow'
48
74
  }
49
- export type WorkerFn = (kind: 'loader' | 'jobs' | 'fetch', requestJson: string, slot: number) => Promise<number>
75
+ export type WorkerFn = (kind: 'loader' | 'jobs' | 'fetch' | 'cache', requestJson: string, slot: number) => Promise<number>
50
76
  export interface NativeBinding {
51
77
  startServer(opts: StartOptions): void
52
78
  registerWorker(buf: Uint8Array, slots: number, f: WorkerFn): number
@@ -57,6 +83,20 @@ export interface NativeBinding {
57
83
  localAddr(): string
58
84
  /** M3-P P6 — see crates/brust-napi/src/server.rs `plan_jobs`. Never throws. */
59
85
  planJobs(slot: number, len: number): string
86
+ /** L10 — `planJobs` for a loader response too large for the slot: the bytes are handed over directly. Never throws. */
87
+ planJobsBuf(slot: number, buf: Uint8Array): string
88
+ /** F82 — the heap request body of the in-flight `fetch` call, once; null if none. Never throws. */
89
+ takeFetchBody(slot: number): Uint8Array | null
90
+ /** F82 — hand Rust a response body too large for the slot; false = declined. Never throws. */
91
+ putFetchReply(slot: number, body: Uint8Array): boolean
92
+ /** cache adapter R10 — a foreign broadcast event: tier 1 + the floor at `seq`; never re-published. */
93
+ cacheReplay(args: InvalidateArgs, seq: number | null): InvalidateResult
94
+ /** cache adapter R9 — bump `stats.l2[name]`; false = unknown name / no adapter / no server. Never throws. */
95
+ cacheL2Count(name: string): boolean
96
+ /** cache adapter R10 — drop both tier-1 caches (a broadcast gap); the L1 entries removed. */
97
+ cacheClear(): number
98
+ /** Static output (gaps round R9.4): the request reads of a cache key expression; throws when it does not parse. */
99
+ cacheExprReads(src: string): string[]
60
100
  }
61
101
 
62
102
  let binding: NativeBinding | null = null
@@ -83,3 +123,10 @@ export const cacheInvalidate = (args: InvalidateArgs): InvalidateResult => nativ
83
123
  export const cacheStats = (): string => native().cacheStats()
84
124
  export const localAddr = (): string => native().localAddr()
85
125
  export const planJobs = (slot: number, len: number): string => native().planJobs(slot, len)
126
+ export const planJobsBuf = (slot: number, buf: Uint8Array): string => native().planJobsBuf(slot, buf)
127
+ export const takeFetchBody = (slot: number): Uint8Array | null => native().takeFetchBody(slot)
128
+ export const putFetchReply = (slot: number, body: Uint8Array): boolean => native().putFetchReply(slot, body)
129
+ export const cacheReplay = (args: InvalidateArgs, seq: number | null): InvalidateResult => native().cacheReplay(args, seq)
130
+ export const cacheL2Count = (name: string): boolean => native().cacheL2Count(name)
131
+ export const cacheClear = (): number => native().cacheClear()
132
+ export const cacheExprReads = (src: string): string[] => native().cacheExprReads(src)
package/src/routes.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference path="./css-modules.d.ts" />
1
2
  // Route tree, ids and loader verdicts (spec S4, S7 step 4). M2 routes carry ONLY
2
3
  // `path/Component/loader/cache/children`; every other 0.1.x field fails loudly (M3).
3
4
 
@@ -29,15 +30,21 @@ export interface LoaderCtx<P = Record<string, string>> {
29
30
  req: LoaderReq
30
31
  }
31
32
 
33
+ /** `brust build --target static` (gaps round R9.2): `params` lists the values of a dynamic route's
34
+ * `{name}` segments, one record per page; `false` leaves the route out of the static output. The
35
+ * server and Cloudflare targets ignore it. */
36
+ export type RouteSsgConfig<P = Record<string, string>> = { params?: () => P[] | Promise<P[]> } | false
37
+
32
38
  export interface Route<P = Record<string, string>, D = unknown> {
33
39
  path?: string
34
40
  Component?: ComponentType<any>
35
41
  loader?: (ctx: LoaderCtx<P>) => Promise<D> | D
36
42
  cache?: RouteCacheConfig
43
+ ssg?: RouteSsgConfig<P>
37
44
  children?: Route<any, any>[]
38
45
  }
39
46
 
40
- export const ALLOWED_ROUTE_FIELDS = ['path', 'Component', 'loader', 'cache', 'children'] as const
47
+ export const ALLOWED_ROUTE_FIELDS = ['path', 'Component', 'loader', 'cache', 'ssg', 'children'] as const
41
48
  const ALLOWED_CACHE_FIELDS = ['ttl_seconds', 'prefix', 'bypass', 'tags'] as const
42
49
  const M3_CACHE_FIELDS = ['key', 'key_ttl_seconds'] as const
43
50
 
@@ -61,6 +68,18 @@ function validate(node: Route, where: string): void {
61
68
  if (node.path !== undefined && typeof node.path !== 'string') throw new BrustRouteError(`route ${where}: path must be a string`)
62
69
  if (node.loader !== undefined && typeof node.loader !== 'function')
63
70
  throw new BrustRouteError(`route ${where}: loader must be a function`)
71
+ if (node.ssg !== undefined) {
72
+ const ssg = node.ssg as unknown
73
+ if (ssg !== false && (typeof ssg !== 'object' || ssg === null || Array.isArray(ssg)))
74
+ throw new BrustRouteError(`route ${where}: ssg must be false or { params }`)
75
+ if (ssg !== false) {
76
+ for (const k of Object.keys(ssg as object))
77
+ if (k !== 'params') throw new BrustRouteError(`route ${where}: unknown ssg field ssg.${k} (allowed: params)`)
78
+ const params = (ssg as { params?: unknown }).params
79
+ if (params !== undefined && typeof params !== 'function') throw new BrustRouteError(`route ${where}: ssg.params must be a function`)
80
+ }
81
+ if (node.children !== undefined) throw new BrustRouteError(`route ${where}: ssg on a layout route is not supported (put it on each leaf)`)
82
+ }
64
83
  if (node.cache !== undefined) {
65
84
  if (typeof node.cache !== 'object' || node.cache === null) throw new BrustRouteError(`route ${where}: cache must be an object`)
66
85
  for (const k of Object.keys(node.cache)) {
package/src/run.ts CHANGED
@@ -8,38 +8,59 @@
8
8
  // does not end it, so every exit is an explicit `process.exit`.
9
9
  import { existsSync, readFileSync } from 'node:fs'
10
10
  import { join, resolve } from 'node:path'
11
- import { type BrustConfig, loadConfig } from './config'
12
- import { beginDrain, type StartOptions, startServer, untilReady } from './native'
13
- import { SLOT_BYTES } from './worker'
11
+ import { type BrustConfig, type ConfigOverrides, loadConfig } from './config'
12
+ import { beginDrain, localAddr, type StartOptions, startServer, untilReady } from './native'
13
+ import { DEFAULT_MAX_LOADER_RESPONSE_BYTES } from './worker'
14
14
 
15
15
  export interface RunOptions {
16
16
  distDir?: string
17
17
  /** The routes module the workers import (`export const routes` or default). */
18
18
  entry?: string
19
19
  /** Overrides below env (CLI flags): see config.ts (incl. `bootTimeoutMs`, default 30000). */
20
- config?: Partial<BrustConfig>
20
+ config?: ConfigOverrides
21
+ /** Static output (gaps round L9): runs once every worker is ready, with the bound `host:port`;
22
+ * then the server drains and the process exits with the returned code. */
23
+ onReady?: (addr: string) => Promise<number>
21
24
  }
22
25
 
23
26
  /** The manifest fields `run` needs (build/manifest.ts owns the full shape). */
24
27
  export interface RunManifest {
25
- actions?: { entry: string; prefix: string; maxBodyBytes: number } | null
28
+ actions?: { entry: string; prefix: string; maxBodyBytes: number; maxResponseBytes?: number; crossSite?: 'reject' | 'allow' } | null
29
+ /** L16 R16.3: `brust build --actions-only`: no page routes and no routes entry. */
30
+ actions_only?: boolean
31
+ /** L16 R16.4b: a page-only build; the actions app is deployed elsewhere (`[actions] base_url`). */
32
+ actions_remote?: { prefix: string } | null
33
+ /** `[cache]`: the server's L2 settings (the workers load the adapter itself). */
34
+ cache?: { adapter: string; getTimeoutMs: number; maxBodyBytes: number; namespace: string; buildId: string } | null
26
35
  }
27
36
 
28
- /** Pure: the napi `StartOptions`. No actions/cors → exactly the pre-actions keys. */
29
- export function startOptions(cfg: BrustConfig, distDir: string, m: RunManifest, version: string): StartOptions {
37
+ /** Pure: the napi `StartOptions`. No actions/cors/cache → exactly the pre-actions keys. */
38
+ export function startOptions(cfg: BrustConfig, distDir: string, m: RunManifest, version: string, devToken?: string): StartOptions {
30
39
  return {
31
40
  host: cfg.host,
32
41
  port: cfg.port,
33
42
  distDir,
34
43
  workers: cfg.workers,
35
44
  callTimeoutMs: cfg.callTimeoutMs,
45
+ ...(cfg.maxLoaderResponseBytes !== undefined && cfg.maxLoaderResponseBytes !== DEFAULT_MAX_LOADER_RESPONSE_BYTES ? { maxLoaderResponseBytes: cfg.maxLoaderResponseBytes } : {}),
36
46
  workerThreads: cfg.ioThreads,
37
47
  generator: `brust/${version}`,
38
- ...(m.actions ? { actions: { prefix: m.actions.prefix, maxBodyBytes: m.actions.maxBodyBytes } } : {}),
48
+ ...(m.actions ? { actions: { prefix: m.actions.prefix, maxBodyBytes: m.actions.maxBodyBytes, ...(m.actions.maxResponseBytes !== undefined ? { maxResponseBytes: m.actions.maxResponseBytes } : {}), ...(m.actions.crossSite ? { crossSite: m.actions.crossSite } : {}) } } : {}),
49
+ ...(actionsClient(cfg, m) ? { actionsClient: actionsClient(cfg, m) } : {}),
39
50
  ...(cfg.cors ? { cors: cfg.cors } : {}),
51
+ ...(devToken ? { devToken } : {}),
52
+ ...(m.cache ? { l2: { adapter: m.cache.adapter, getTimeoutMs: m.cache.getTimeoutMs, maxBodyBytes: m.cache.maxBodyBytes, namespace: m.cache.namespace, buildId: m.cache.buildId } } : {}),
40
53
  }
41
54
  }
42
55
 
56
+ /** L16: what the page tells the browser `client()` (R16.1/R16.2); absent while it would only repeat the prefix script. */
57
+ function actionsClient(cfg: BrustConfig, m: RunManifest): NonNullable<StartOptions['actionsClient']> | undefined {
58
+ const credentials = cfg.actionsCredentials !== undefined && cfg.actionsCredentials !== 'same-origin' ? cfg.actionsCredentials : undefined
59
+ if (cfg.actionsBaseUrl === undefined && credentials === undefined && !m.actions_remote) return undefined
60
+ const prefix = m.actions?.prefix ?? m.actions_remote?.prefix
61
+ return { ...(prefix ? { prefix } : {}), ...(cfg.actionsBaseUrl !== undefined ? { baseUrl: cfg.actionsBaseUrl } : {}), ...(credentials ? { credentials } : {}) }
62
+ }
63
+
43
64
  /** Every worker's response buffer, kept reachable until the process exits (see run()). */
44
65
  const workerBuffers: SharedArrayBuffer[] = []
45
66
 
@@ -60,7 +81,8 @@ export async function run(opts: RunOptions = {}): Promise<void> {
60
81
  fail(`bad manifest ${manifest}: ${(e as Error).message}`)
61
82
  }
62
83
  const entry = resolve(process.env.BRUST_APP_ENTRY || opts.entry || 'routes.tsx')
63
- if (!existsSync(entry)) fail(`routes entry ${entry} not found`)
84
+ // R16.3: an actions-only dist has no pages, so no routes module either.
85
+ if (!m.actions_only && !existsSync(entry)) fail(`routes entry ${entry} not found`)
64
86
 
65
87
  let cfg: BrustConfig
66
88
  try {
@@ -69,9 +91,11 @@ export async function run(opts: RunOptions = {}): Promise<void> {
69
91
  fail((e as Error).message)
70
92
  }
71
93
 
94
+ if (m.actions_remote && cfg.actionsBaseUrl === undefined)
95
+ fail('this build has no local actions app: set [actions] base_url (or BRUST_ACTIONS_BASE_URL) to the deployment that serves them')
72
96
  const version: string = (await Bun.file(join(import.meta.dir, '../package.json')).json()).version
73
97
  try {
74
- startServer(startOptions(cfg, distDir, m, version))
98
+ startServer(startOptions(cfg, distDir, m, version, process.env.BRUST_DEV_TOKEN || undefined))
75
99
  } catch (e) {
76
100
  fail((e as Error).message)
77
101
  }
@@ -84,8 +108,12 @@ export async function run(opts: RunOptions = {}): Promise<void> {
84
108
  ...process.env,
85
109
  NODE_ENV: process.env.NODE_ENV,
86
110
  BRUST_RENDER_SLOTS: String(cfg.renderSlots),
111
+ BRUST_SLOT_BYTES: String(cfg.slotBytes),
112
+ BRUST_MAX_LOADER_RESPONSE_BYTES: String(cfg.maxLoaderResponseBytes),
87
113
  BRUST_DIST_DIR: distDir,
88
114
  BRUST_APP_ENTRY: entry,
115
+ // One cache-adapter origin per process: its workers' published events are not replayed here.
116
+ BRUST_CACHE_ORIGIN: crypto.randomUUID(),
89
117
  }
90
118
 
91
119
  let ready = false
@@ -110,7 +138,7 @@ export async function run(opts: RunOptions = {}): Promise<void> {
110
138
  for (let i = 0; i < cfg.workers; i++) {
111
139
  // The response buffer is owned HERE, for the process lifetime: the Rust dispatcher keeps a raw
112
140
  // pointer into it, so it must not be freed with the worker (crates/brust-napi dispatch.rs).
113
- const sab = new SharedArrayBuffer(SLOT_BYTES * cfg.renderSlots)
141
+ const sab = new SharedArrayBuffer(cfg.slotBytes * cfg.renderSlots)
114
142
  workerBuffers.push(sab)
115
143
  const w = new Worker(url, { env: { ...env, BRUST_WORKER_ID: String(i) }, workerData: { sab } } as WorkerOptions)
116
144
  // No respawn in M2: a worker that fails to boot or dies later takes the process down loudly
@@ -153,5 +181,14 @@ export async function run(opts: RunOptions = {}): Promise<void> {
153
181
  }
154
182
  ready = true
155
183
  console.log(`[brust] ready (${cfg.workers} worker${cfg.workers === 1 ? '' : 's'})`)
184
+ if (opts.onReady) {
185
+ let code = 1
186
+ try {
187
+ code = await opts.onReady(localAddr())
188
+ } catch (e) {
189
+ console.error(`[brust] ${(e as Error).stack ?? String(e)}`)
190
+ }
191
+ if (!draining) drainThenExit(code)
192
+ }
156
193
  await new Promise<never>(() => {})
157
194
  }
@@ -0,0 +1,32 @@
1
+ // What the generated Cloudflare entry (`brust-app.js`) imports: the host-agnostic handler code and the
2
+ // store-scoped action fetch. No Bun / worker_threads here; `actions-bridge`'s napi import is stubbed by the
3
+ // CF bundle (M4-CF R8).
4
+ import { type CfCacheBridge, type CfInstallExt, installCache } from './cache-adapter/workers'
5
+ import type { InvalidateArgs, InvalidateResult } from './native'
6
+
7
+ export { fetchInScope, noActions } from './actions-bridge'
8
+ export { jsonCall, makeHandlers } from './handlers'
9
+ export { flattenRoutes } from './routes'
10
+
11
+ /** What the wasm side hands over at start (`#[event(start]` -> `install({ invalidate, l2Count })`, ruling 19753649). */
12
+ export interface CfRuntime {
13
+ invalidate(args: InvalidateArgs): InvalidateResult
14
+ /** Bumps a `stats.l2` counter (cache adapters R9). */
15
+ l2Count?(name: string): boolean
16
+ }
17
+ export type { CfCacheBridge, CfInstallExt }
18
+
19
+ let rt: CfRuntime | null = null
20
+
21
+ /** Called by the wasm glue once at Worker start; the CF bundle's napi stub forwards `cacheInvalidate` to it.
22
+ * With a cache adapter (`ext`, from the generated entry) it answers the adapter bridge (R11, R14). */
23
+ export function install(runtime: CfRuntime, ext?: CfInstallExt): { cache: CfCacheBridge | null } {
24
+ rt = runtime
25
+ return { cache: ext ? installCache(runtime, ext) : null }
26
+ }
27
+
28
+ /** The installed runtime; throws before `install` (a request cannot arrive earlier). */
29
+ export function installedRuntime(): CfRuntime {
30
+ if (!rt) throw new Error('brust: the Cloudflare runtime is not installed (install(rt) was not called at start)')
31
+ return rt
32
+ }