brustjs 0.1.43-alpha → 0.1.45-alpha

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/README.md CHANGED
@@ -88,8 +88,9 @@ Click either one — both update. [See it live](https://brust.assetsart.com/) ·
88
88
 
89
89
  - **Native rendering** — pages compile ahead of time and are served as plain
90
90
  HTML: no hydration pass, no framework runtime in the response. One compiled
91
- page sustains **84,119 req/s** versus 28,938 for the same page through a
92
- JavaScript pipeline — about 2. ([bench/RESULTS.md](./bench/RESULTS.md)).
91
+ page sustains **106,374 req/s** versus 44,448 for the same page through a
92
+ JavaScript pipeline — about 2.4×, on an M1 Pro (10c)
93
+ ([bench/RESULTS.md](./bench/RESULTS.md)).
93
94
  → [Rendering](https://brust.assetsart.com/docs/rendering)
94
95
  - **React islands** — hydrate one component, not the page. React 19 streaming
95
96
  SSR with auto-Suspense; SSR islands can opt into **ISR caching**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brustjs",
3
- "version": "0.1.43-alpha",
3
+ "version": "0.1.45-alpha",
4
4
  "description": "Bun + Rust SSR framework — React on the server, Rust everywhere else (napi cdylib + per-worker SharedArrayBuffer).",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -41,12 +41,12 @@
41
41
  "typescript": "^6.0.3"
42
42
  },
43
43
  "optionalDependencies": {
44
- "brustjs-darwin-x64": "0.1.43-alpha",
45
- "brustjs-darwin-arm64": "0.1.43-alpha",
46
- "brustjs-linux-x64-gnu": "0.1.43-alpha",
47
- "brustjs-linux-arm64-gnu": "0.1.43-alpha",
48
- "brustjs-linux-x64-musl": "0.1.43-alpha",
49
- "brustjs-linux-arm64-musl": "0.1.43-alpha"
44
+ "brustjs-darwin-x64": "0.1.45-alpha",
45
+ "brustjs-darwin-arm64": "0.1.45-alpha",
46
+ "brustjs-linux-x64-gnu": "0.1.45-alpha",
47
+ "brustjs-linux-arm64-gnu": "0.1.45-alpha",
48
+ "brustjs-linux-x64-musl": "0.1.45-alpha",
49
+ "brustjs-linux-arm64-musl": "0.1.45-alpha"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "react": "^19.2.6",
package/runtime/cache.ts CHANGED
@@ -5,14 +5,28 @@ import * as native from './index.js'
5
5
  export interface InvalidateArgs {
6
6
  key?: string
7
7
  tags?: string[]
8
+ /** L1 response-cache: evict all entries for this request path (any prefix /
9
+ * query variant). Independent of `tags`. */
10
+ path?: string
11
+ /** HTTP method for the `path` eviction (defaults to GET in Rust). */
12
+ method?: string
8
13
  }
9
14
 
10
15
  export const cache = {
11
- /** Evict by exact key and/or by tag group. Both optional; both may be given.
12
- * `invalidate({})` forwards `(undefined, undefined)` a deliberate no-op on
13
- * the Rust side (neither the key nor the tags branch fires). The `?.` guards
14
- * against a stale addon built before this export existed (degrades to no-op). */
16
+ /** Evict across all three caches: islands, L2 page cache, and L1 response
17
+ * cache. `key`/`tags` hit islands + L2; the L1 response cache is reached by
18
+ * `tags` (the ROUTE must declare static `cache.tags` for its L1 entries to
19
+ * carry them) and by `path` (evicts every L1 entry for that request path,
20
+ * any prefix/query variant). All fields optional; `invalidate({})` is a
21
+ * deliberate no-op. The `?.` guards against a stale addon built before a
22
+ * given binding existed (degrades to no-op). */
15
23
  invalidate(args: InvalidateArgs): void {
16
24
  ;(native as any).islandCacheInvalidate?.(args.key, args.tags)
25
+ // Fan out to the L2 page cache (same key/tag semantics). `?.` keeps a
26
+ // stale addon (built before page-cache bindings existed) a no-op.
27
+ ;(native as any).pageCacheInvalidate?.(args.key, args.tags)
28
+ // Fan out to the L1 response cache by tag (route must declare cache.tags)
29
+ // and/or by path. `?.` keeps a stale addon a no-op.
30
+ ;(native as any).responseCacheInvalidate?.(args.tags, args.path, args.method)
17
31
  },
18
32
  }
package/runtime/config.ts CHANGED
@@ -10,8 +10,10 @@ export interface BrustConfig {
10
10
  port: number
11
11
  /** Bun Worker count for render dispatch. Default `availableParallelism()`. */
12
12
  workers: number
13
- /** Cache capacity (entries). Undefined → Rust default of 1000. */
13
+ /** L1 response-cache capacity (entries). Undefined → Rust default of 1000. */
14
14
  cacheMaxEntries?: number
15
+ /** L2 page-cache capacity (entries). Undefined → Rust default of 1000. */
16
+ cachePageMaxEntries?: number
15
17
  }
16
18
 
17
19
  /** Caller-supplied fallbacks (e.g. `brust.run({ address, port })`) applied
@@ -77,7 +79,13 @@ export async function loadConfig(
77
79
  const port = fromEnv.port ?? fromToml.port ?? defaults.port ?? DEFAULT_PORT
78
80
  const workers = fromEnv.workers ?? fromToml.workers ?? defaultWorkers()
79
81
 
80
- return { host, port, workers, cacheMaxEntries: fromToml.cacheMaxEntries }
82
+ return {
83
+ host,
84
+ port,
85
+ workers,
86
+ cacheMaxEntries: fromToml.cacheMaxEntries,
87
+ cachePageMaxEntries: fromToml.cachePageMaxEntries,
88
+ }
81
89
  }
82
90
 
83
91
  function extractFromToml(parsed: unknown, file: string): Partial<BrustConfig> {
@@ -146,6 +154,20 @@ function extractFromToml(parsed: unknown, file: string): Partial<BrustConfig> {
146
154
  }
147
155
  out.cacheMaxEntries = maxEntries
148
156
  }
157
+ const pageMaxEntries = (cache as Record<string, unknown>).page_max_entries
158
+ if (pageMaxEntries !== undefined) {
159
+ if (
160
+ typeof pageMaxEntries !== 'number' ||
161
+ !Number.isInteger(pageMaxEntries) ||
162
+ pageMaxEntries < 1
163
+ ) {
164
+ throw new BrustConfigError(
165
+ `${file}: cache.page_max_entries must be a positive integer (got ${JSON.stringify(pageMaxEntries)})`,
166
+ file,
167
+ )
168
+ }
169
+ out.cachePageMaxEntries = pageMaxEntries
170
+ }
149
171
  }
150
172
 
151
173
  return out
@@ -19,7 +19,7 @@ export interface CachedIslandJs {
19
19
 
20
20
  export declare function compileJsx(source: string, path: string, componentSources?: Record<string, string> | undefined | null, lucideIcons?: Record<string, string> | undefined | null, directiveNames?: Record<string, string> | undefined | null): NapiCompiledJsx
21
21
 
22
- export declare function configureCache(maxEntries: number): NapiResult<undefined>
22
+ export declare function configureCache(maxEntries: number, pageMaxEntries: number): NapiResult<undefined>
23
23
 
24
24
  export declare function configureCssDir(path: string): NapiResult<undefined>
25
25
 
@@ -235,6 +235,14 @@ export declare function napiWsSend(connId: bigint, data: Buffer, isBinary: boole
235
235
  */
236
236
  export declare function napiWsSignalOpen(connId: bigint, status: number, body: Buffer, contentType: string, subprotocol: string): NapiResult<undefined>
237
237
 
238
+ export declare function pageCacheClear(): void
239
+
240
+ export declare function pageCacheGet(key: string): Buffer | null
241
+
242
+ export declare function pageCacheInvalidate(key?: string | undefined | null, tags?: Array<string> | undefined | null): void
243
+
244
+ export declare function pageCacheSet(key: string, tags: Array<string>, ttlMs: number | undefined | null, payload: Buffer): void
245
+
238
246
  /**
239
247
  * Register action endpoints. Replaces any previous router state.
240
248
  * Returns the number of endpoints registered.
@@ -256,6 +264,8 @@ export declare function registerRoutes(configs: Array<string>): NapiResult<numbe
256
264
  */
257
265
  export declare function resetWorkerPool(): number
258
266
 
267
+ export declare function responseCacheInvalidate(tags?: Array<string> | undefined | null, path?: string | undefined | null, method?: string | undefined | null): void
268
+
259
269
  export interface ServeOptions {
260
270
  /**
261
271
  * Host to bind on — a hostname (e.g. `localhost`, resolved here) or a
@@ -320,8 +330,9 @@ export interface ServeTuning {
320
330
  workerThreads?: number
321
331
  /**
322
332
  * Number of render slots per Bun worker (concurrent in-flight renders per
323
- * isolate). Default 1 (single in-flight render per worker byte-identical
324
- * to the pre-multi-slot behaviour). The COUNT reaches `register_renderer`
333
+ * isolate). Default `min(cores, 16)` (set `BRUST_RENDER_SLOTS` to exceed 16
334
+ * cores); slots > 1 only speed renders that await during render and are
335
+ * byte-identical to slots = 1. The COUNT reaches `register_renderer`
325
336
  * via the `BRUST_RENDER_SLOTS` worker env var set in `runtime/index.ts`; it
326
337
  * is NOT consumed by the Rust `Tuning` struct here (the pool learns it from
327
338
  * `dispatch.slot_count()`). Carried on `ServeTuning` only so the JS layer
package/runtime/index.js CHANGED
@@ -77,8 +77,8 @@ function requireNative() {
77
77
  try {
78
78
  const binding = require('brustjs-android-arm64')
79
79
  const bindingPackageVersion = require('brustjs-android-arm64/package.json').version
80
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
80
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
82
82
  }
83
83
  return binding
84
84
  } catch (e) {
@@ -93,8 +93,8 @@ function requireNative() {
93
93
  try {
94
94
  const binding = require('brustjs-android-arm-eabi')
95
95
  const bindingPackageVersion = require('brustjs-android-arm-eabi/package.json').version
96
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
96
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
98
98
  }
99
99
  return binding
100
100
  } catch (e) {
@@ -114,8 +114,8 @@ function requireNative() {
114
114
  try {
115
115
  const binding = require('brustjs-win32-x64-gnu')
116
116
  const bindingPackageVersion = require('brustjs-win32-x64-gnu/package.json').version
117
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
117
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
119
119
  }
120
120
  return binding
121
121
  } catch (e) {
@@ -130,8 +130,8 @@ function requireNative() {
130
130
  try {
131
131
  const binding = require('brustjs-win32-x64-msvc')
132
132
  const bindingPackageVersion = require('brustjs-win32-x64-msvc/package.json').version
133
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
133
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
135
135
  }
136
136
  return binding
137
137
  } catch (e) {
@@ -147,8 +147,8 @@ function requireNative() {
147
147
  try {
148
148
  const binding = require('brustjs-win32-ia32-msvc')
149
149
  const bindingPackageVersion = require('brustjs-win32-ia32-msvc/package.json').version
150
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
150
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
152
152
  }
153
153
  return binding
154
154
  } catch (e) {
@@ -163,8 +163,8 @@ function requireNative() {
163
163
  try {
164
164
  const binding = require('brustjs-win32-arm64-msvc')
165
165
  const bindingPackageVersion = require('brustjs-win32-arm64-msvc/package.json').version
166
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
166
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
168
168
  }
169
169
  return binding
170
170
  } catch (e) {
@@ -182,8 +182,8 @@ function requireNative() {
182
182
  try {
183
183
  const binding = require('brustjs-darwin-universal')
184
184
  const bindingPackageVersion = require('brustjs-darwin-universal/package.json').version
185
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
185
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
187
187
  }
188
188
  return binding
189
189
  } catch (e) {
@@ -198,8 +198,8 @@ function requireNative() {
198
198
  try {
199
199
  const binding = require('brustjs-darwin-x64')
200
200
  const bindingPackageVersion = require('brustjs-darwin-x64/package.json').version
201
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
201
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
203
203
  }
204
204
  return binding
205
205
  } catch (e) {
@@ -214,8 +214,8 @@ function requireNative() {
214
214
  try {
215
215
  const binding = require('brustjs-darwin-arm64')
216
216
  const bindingPackageVersion = require('brustjs-darwin-arm64/package.json').version
217
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
217
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
219
219
  }
220
220
  return binding
221
221
  } catch (e) {
@@ -234,8 +234,8 @@ function requireNative() {
234
234
  try {
235
235
  const binding = require('brustjs-freebsd-x64')
236
236
  const bindingPackageVersion = require('brustjs-freebsd-x64/package.json').version
237
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
237
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
239
239
  }
240
240
  return binding
241
241
  } catch (e) {
@@ -250,8 +250,8 @@ function requireNative() {
250
250
  try {
251
251
  const binding = require('brustjs-freebsd-arm64')
252
252
  const bindingPackageVersion = require('brustjs-freebsd-arm64/package.json').version
253
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
253
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
255
255
  }
256
256
  return binding
257
257
  } catch (e) {
@@ -271,8 +271,8 @@ function requireNative() {
271
271
  try {
272
272
  const binding = require('brustjs-linux-x64-musl')
273
273
  const bindingPackageVersion = require('brustjs-linux-x64-musl/package.json').version
274
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
274
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
276
276
  }
277
277
  return binding
278
278
  } catch (e) {
@@ -287,8 +287,8 @@ function requireNative() {
287
287
  try {
288
288
  const binding = require('brustjs-linux-x64-gnu')
289
289
  const bindingPackageVersion = require('brustjs-linux-x64-gnu/package.json').version
290
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
290
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
292
292
  }
293
293
  return binding
294
294
  } catch (e) {
@@ -305,8 +305,8 @@ function requireNative() {
305
305
  try {
306
306
  const binding = require('brustjs-linux-arm64-musl')
307
307
  const bindingPackageVersion = require('brustjs-linux-arm64-musl/package.json').version
308
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
308
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
310
310
  }
311
311
  return binding
312
312
  } catch (e) {
@@ -321,8 +321,8 @@ function requireNative() {
321
321
  try {
322
322
  const binding = require('brustjs-linux-arm64-gnu')
323
323
  const bindingPackageVersion = require('brustjs-linux-arm64-gnu/package.json').version
324
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
324
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
326
326
  }
327
327
  return binding
328
328
  } catch (e) {
@@ -339,8 +339,8 @@ function requireNative() {
339
339
  try {
340
340
  const binding = require('brustjs-linux-arm-musleabihf')
341
341
  const bindingPackageVersion = require('brustjs-linux-arm-musleabihf/package.json').version
342
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
342
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
344
344
  }
345
345
  return binding
346
346
  } catch (e) {
@@ -355,8 +355,8 @@ function requireNative() {
355
355
  try {
356
356
  const binding = require('brustjs-linux-arm-gnueabihf')
357
357
  const bindingPackageVersion = require('brustjs-linux-arm-gnueabihf/package.json').version
358
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
358
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
360
360
  }
361
361
  return binding
362
362
  } catch (e) {
@@ -373,8 +373,8 @@ function requireNative() {
373
373
  try {
374
374
  const binding = require('brustjs-linux-loong64-musl')
375
375
  const bindingPackageVersion = require('brustjs-linux-loong64-musl/package.json').version
376
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
376
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
378
378
  }
379
379
  return binding
380
380
  } catch (e) {
@@ -389,8 +389,8 @@ function requireNative() {
389
389
  try {
390
390
  const binding = require('brustjs-linux-loong64-gnu')
391
391
  const bindingPackageVersion = require('brustjs-linux-loong64-gnu/package.json').version
392
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
392
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
394
394
  }
395
395
  return binding
396
396
  } catch (e) {
@@ -407,8 +407,8 @@ function requireNative() {
407
407
  try {
408
408
  const binding = require('brustjs-linux-riscv64-musl')
409
409
  const bindingPackageVersion = require('brustjs-linux-riscv64-musl/package.json').version
410
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
410
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
412
412
  }
413
413
  return binding
414
414
  } catch (e) {
@@ -423,8 +423,8 @@ function requireNative() {
423
423
  try {
424
424
  const binding = require('brustjs-linux-riscv64-gnu')
425
425
  const bindingPackageVersion = require('brustjs-linux-riscv64-gnu/package.json').version
426
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
426
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
428
428
  }
429
429
  return binding
430
430
  } catch (e) {
@@ -440,8 +440,8 @@ function requireNative() {
440
440
  try {
441
441
  const binding = require('brustjs-linux-ppc64-gnu')
442
442
  const bindingPackageVersion = require('brustjs-linux-ppc64-gnu/package.json').version
443
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
443
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
445
445
  }
446
446
  return binding
447
447
  } catch (e) {
@@ -456,8 +456,8 @@ function requireNative() {
456
456
  try {
457
457
  const binding = require('brustjs-linux-s390x-gnu')
458
458
  const bindingPackageVersion = require('brustjs-linux-s390x-gnu/package.json').version
459
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
459
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
461
461
  }
462
462
  return binding
463
463
  } catch (e) {
@@ -476,8 +476,8 @@ function requireNative() {
476
476
  try {
477
477
  const binding = require('brustjs-openharmony-arm64')
478
478
  const bindingPackageVersion = require('brustjs-openharmony-arm64/package.json').version
479
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
479
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
481
481
  }
482
482
  return binding
483
483
  } catch (e) {
@@ -492,8 +492,8 @@ function requireNative() {
492
492
  try {
493
493
  const binding = require('brustjs-openharmony-x64')
494
494
  const bindingPackageVersion = require('brustjs-openharmony-x64/package.json').version
495
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
495
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
497
497
  }
498
498
  return binding
499
499
  } catch (e) {
@@ -508,8 +508,8 @@ function requireNative() {
508
508
  try {
509
509
  const binding = require('brustjs-openharmony-arm')
510
510
  const bindingPackageVersion = require('brustjs-openharmony-arm/package.json').version
511
- if (bindingPackageVersion !== '0.1.43-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
- throw new Error(`Native binding package version mismatch, expected 0.1.43-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
511
+ if (bindingPackageVersion !== '0.1.45-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
+ throw new Error(`Native binding package version mismatch, expected 0.1.45-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
513
513
  }
514
514
  return binding
515
515
  } catch (e) {
@@ -605,10 +605,15 @@ module.exports.napiWsClose = nativeBinding.napiWsClose
605
605
  module.exports.napiWsRegisterHandlers = nativeBinding.napiWsRegisterHandlers
606
606
  module.exports.napiWsSend = nativeBinding.napiWsSend
607
607
  module.exports.napiWsSignalOpen = nativeBinding.napiWsSignalOpen
608
+ module.exports.pageCacheClear = nativeBinding.pageCacheClear
609
+ module.exports.pageCacheGet = nativeBinding.pageCacheGet
610
+ module.exports.pageCacheInvalidate = nativeBinding.pageCacheInvalidate
611
+ module.exports.pageCacheSet = nativeBinding.pageCacheSet
608
612
  module.exports.registerActions = nativeBinding.registerActions
609
613
  module.exports.registerRenderer = nativeBinding.registerRenderer
610
614
  module.exports.registerRoutes = nativeBinding.registerRoutes
611
615
  module.exports.resetWorkerPool = nativeBinding.resetWorkerPool
616
+ module.exports.responseCacheInvalidate = nativeBinding.responseCacheInvalidate
612
617
  module.exports.untilReady = nativeBinding.untilReady
613
618
  module.exports.untilShutdown = nativeBinding.untilShutdown
614
619
  module.exports.workerId = nativeBinding.workerId
package/runtime/index.ts CHANGED
@@ -1,9 +1,26 @@
1
+ import os from 'node:os'
1
2
  import * as native from './index.js'
2
3
  import type { EndpointDef } from './define-actions.ts'
3
4
  import { loadConfig } from './config.ts'
4
5
  import { configureCssEnabled, configureCssHrefsForRoute } from './css.ts'
5
6
  import { configureJinjaDir } from './islands/native-render.ts'
6
7
 
8
+ /** Default render slots per Bun worker: one per CPU, capped at 16. Above 16
9
+ * cores, set `BRUST_RENDER_SLOTS` explicitly to go higher. Slots > 1 only speed
10
+ * up renders that `await` DURING render (e.g. Suspense fetching async data);
11
+ * CPU-bound / native-jinja / cache-hit routes serialize on the worker's single
12
+ * JS isolate and are unaffected. Each slot costs one SAB region (`sabBytes`), so
13
+ * the cap bounds memory. slots > 1 is byte-identical to slots = 1. */
14
+ function defaultRenderSlots(): number {
15
+ let cores: number
16
+ try {
17
+ cores = os.availableParallelism()
18
+ } catch {
19
+ cores = os.cpus().length
20
+ }
21
+ return Math.min(Math.max(cores, 1), 16)
22
+ }
23
+
7
24
  export interface ServeOptions {
8
25
  /** Host/address to bind on. A hostname (e.g. `localhost`, resolved Rust-side)
9
26
  * or a literal IP such as `0.0.0.0` / `127.0.0.1`. */
@@ -54,9 +71,12 @@ export interface ServeOptions {
54
71
  * one-per-core. Default `min(availableParallelism, 4)` (fallback 2). */
55
72
  workerThreads?: number
56
73
  /** Render slots per Bun worker — concurrent in-flight renders per isolate.
57
- * Default 1 (byte-identical to single in-flight render per worker). The
58
- * count is propagated to each worker via the `BRUST_RENDER_SLOTS` env var
59
- * and scales the per-worker SAB so each slot keeps the single-slot capacity. */
74
+ * Default `min(cores, 16)` (set `BRUST_RENDER_SLOTS` to go higher on >16-core
75
+ * hosts). Only speeds renders that `await` during render (e.g. Suspense with
76
+ * async data); CPU-bound/native/cache routes serialize on the one isolate and
77
+ * are unaffected. The count is propagated to each worker via the
78
+ * `BRUST_RENDER_SLOTS` env var and scales the per-worker SAB (one region per
79
+ * slot, so the cap bounds memory). slots > 1 is byte-identical to slots = 1. */
60
80
  renderSlots?: number
61
81
  }
62
82
  }
@@ -144,7 +164,7 @@ export const brust = {
144
164
  // per-app wiring), else 1 — byte-identical single in-flight render.
145
165
  const renderSlots = Math.max(
146
166
  1,
147
- opts.tuning?.renderSlots ?? (Number(process.env.BRUST_RENDER_SLOTS) || 1),
167
+ opts.tuning?.renderSlots ?? (Number(process.env.BRUST_RENDER_SLOTS) || defaultRenderSlots()),
148
168
  )
149
169
  const baseEnv = { ...process.env, BRUST_RENDER_SLOTS: String(renderSlots) }
150
170
  const workersArr: Worker[] = []
@@ -190,7 +210,25 @@ export const brust = {
190
210
  const configs = routes.map((r) =>
191
211
  JSON.stringify({
192
212
  path: r.fullPath,
193
- cache: r.cache ?? null,
213
+ // Rust-safe projection: the L2 `key` FUNCTION and `key_ttl_seconds`
214
+ // stay TS-side (the worker reads them off the FlatRoute). Only the
215
+ // L1 directives cross napi. `bypass` passes through as `true` or the
216
+ // string expr — serde's untagged BypassSpec handles both. `false` and
217
+ // absent both normalize to null → None (never bypass), so an explicitly
218
+ // disabled bypass doesn't ride a cross-language `false` contract. An
219
+ // EMPTY-STRING expr deliberately passes through: Expr::parse("") fails
220
+ // route install, surfacing the misconfiguration loudly at boot instead
221
+ // of silently never bypassing.
222
+ cache: r.cache
223
+ ? {
224
+ ttl_seconds: r.cache.ttl_seconds,
225
+ prefix: r.cache.prefix ?? null,
226
+ bypass: r.cache.bypass === false ? null : (r.cache.bypass ?? null),
227
+ // Static L1 invalidation tags carried into each L1 entry so
228
+ // `cache.invalidate({ tags })` can reach the response cache.
229
+ tags: r.cache.tags ?? null,
230
+ }
231
+ : null,
194
232
  nativeTemplate: r.nativeTemplate ?? null,
195
233
  }),
196
234
  )
@@ -229,11 +267,12 @@ export const brust = {
229
267
  registerWsPaths(paths: string[]): void {
230
268
  ;(native as any).napiRegisterWsPaths(paths)
231
269
  },
232
- /** Set the response cache capacity (entries). Default is 1000.
233
- * Safe to call at any time; if shrinking below current size, excess
234
- * LRU entries are evicted. */
235
- configureCache(opts: { maxEntries: number }): void {
236
- ;(native as any).configureCache(opts.maxEntries)
270
+ /** Set the L1 response-cache and L2 page-cache capacities (entries).
271
+ * Default is 1000 each. Rust reconstructs both caches at the given
272
+ * capacities (moka fixes capacity at construction), so call this once at
273
+ * boot before serving begins. */
274
+ configureCache(opts: { maxEntries: number; pageMaxEntries: number }): void {
275
+ ;(native as any).configureCache(opts.maxEntries, opts.pageMaxEntries)
237
276
  },
238
277
  /** Tell Rust where to read `/_brust/islands/<file>` from. Called once at
239
278
  * boot after buildIslands() emits chunks. Path must be absolute. */
@@ -365,12 +404,19 @@ export const brust = {
365
404
  const endpoints: EndpointDef[] = opts.actions?.endpoints ?? []
366
405
 
367
406
  if (!isWorker) {
368
- const { host, port, workers, cacheMaxEntries } = await loadConfig(process.cwd(), {
369
- host: opts.address,
370
- port: opts.port,
371
- })
407
+ const { host, port, workers, cacheMaxEntries, cachePageMaxEntries } = await loadConfig(
408
+ process.cwd(),
409
+ {
410
+ host: opts.address,
411
+ port: opts.port,
412
+ },
413
+ )
372
414
  console.log(`[brust] main: spawning ${workers} worker threads`)
373
- if (cacheMaxEntries !== undefined) this.configureCache({ maxEntries: cacheMaxEntries })
415
+ if (cacheMaxEntries !== undefined || cachePageMaxEntries !== undefined)
416
+ this.configureCache({
417
+ maxEntries: cacheMaxEntries ?? 1000,
418
+ pageMaxEntries: cachePageMaxEntries ?? 1000,
419
+ })
374
420
 
375
421
  // md routes present? (leaf carries `__mdSource`, attached by mdRoutes()).
376
422
  // Checked inline — no md-module import — so md-free apps never load the
@@ -883,7 +929,13 @@ export const brust = {
883
929
  // Render slots for this worker (set in serve() via the worker env). The
884
930
  // SAB scales with the slot count so each slot's disjoint sub-region keeps
885
931
  // the single-slot capacity; at K=1 this is byte-identical to before.
886
- const renderSlots = Math.max(1, Number(process.env.BRUST_RENDER_SLOTS ?? 1) || 1)
932
+ // Normally main propagates the computed count via the worker env; the
933
+ // defaultRenderSlots() fallback keeps an in-process boot (env unset) in
934
+ // sync with the main-branch default above.
935
+ const renderSlots = Math.max(
936
+ 1,
937
+ Number(process.env.BRUST_RENDER_SLOTS) || defaultRenderSlots(),
938
+ )
887
939
  const sab = new SharedArrayBuffer((opts.sabBytes ?? 256 * 1024) * renderSlots)
888
940
  const view = new Uint8Array(sab)
889
941
 
@@ -1,4 +1,7 @@
1
1
  import { Marked, type Tokens } from 'marked'
2
+ // Heading slugger shared with the public `brustjs/routes` export so a consumer's
3
+ // anchors can't drift from the ids we stamp here (FRAMEWORK-GAPS G1).
4
+ import { slugifyHeading as slugify } from './slug.ts'
2
5
 
3
6
  // ---------------------------------------------------------------------------
4
7
  // Types
@@ -397,14 +400,6 @@ function scanBalancedBraces(text: string, start: number): number {
397
400
  // Markdown → HTML (marked, GFM, heading ids, shiki fences)
398
401
  // ---------------------------------------------------------------------------
399
402
 
400
- function slugify(text: string): string {
401
- return text
402
- .toLowerCase()
403
- .trim()
404
- .replace(/\s+/g, '-')
405
- .replace(/[^\w-]/g, '')
406
- }
407
-
408
403
  async function renderMarkdown(source: string): Promise<string> {
409
404
  // Fresh instance per page: heading-id dedupe state is page-local.
410
405
  const slugCounts = new Map<string, number>()
@@ -60,6 +60,13 @@ export interface MdRoutesOptions {
60
60
  layout?: ComponentType<any>
61
61
  /** Component-tag registry for `<Name />` tags inside the md body. */
62
62
  components?: Record<string, ComponentType<any>>
63
+ /** Loader for the LAYOUT parent route — runs before each md leaf renders and
64
+ * its data merges into the leaf's context top-down (so it feeds the layout
65
+ * chrome: sidebar, pager, …). Only applies when `layout` is set. Return keys
66
+ * that DON'T collide with the leaf's `__md` head metadata, which must win.
67
+ * Without this, the layout loader had to be attached by mutating the returned
68
+ * route node (`tree.loader = …`) — undocumented surface (was FRAMEWORK-GAPS G2). */
69
+ loader?: Route['loader']
63
70
  }
64
71
 
65
72
  /** One frozen-manifest entry (everything route construction needs per page).
@@ -270,7 +277,15 @@ export function mdRoutes(contentDir: string, opts: MdRoutesOptions = {}): Route[
270
277
  })
271
278
 
272
279
  if (opts.layout === undefined) return leaves
273
- return [{ path: basePath, native: true, Component: opts.layout, children: leaves }]
280
+ const layoutRoute: Route = {
281
+ path: basePath,
282
+ native: true,
283
+ Component: opts.layout,
284
+ children: leaves,
285
+ }
286
+ // First-class layout loader (was a post-hoc `tree.loader = …` mutation).
287
+ if (opts.loader !== undefined) layoutRoute.loader = opts.loader
288
+ return [layoutRoute]
274
289
  }
275
290
 
276
291
  /** Strip the mount base off a full url path (index page → `''`, which
@@ -0,0 +1,16 @@
1
+ // Heading-anchor slugger — the SINGLE source of truth for the ids brust stamps
2
+ // on markdown `<h2>/<h3>`. render.ts uses it when emitting heading ids, and it
3
+ // is re-exported on `brustjs/routes` so a consumer (e.g. a search-index
4
+ // generator) can compute the SAME anchors without copying this logic — which
5
+ // is exactly the parity that used to drift (FRAMEWORK-GAPS G1).
6
+ //
7
+ // `\w` is ASCII-only: non-ascii heading text strips to nothing (matches marked
8
+ // GFM's behavior as wired here). Per-page duplicate-id disambiguation
9
+ // (`-2`, `-3`, …) is the caller's concern — render.ts keeps a page-local count.
10
+ export function slugifyHeading(text: string): string {
11
+ return text
12
+ .toLowerCase()
13
+ .trim()
14
+ .replace(/\s+/g, '-')
15
+ .replace(/[^\w-]/g, '')
16
+ }
package/runtime/routes.ts CHANGED
@@ -31,7 +31,7 @@ import { validate } from './standard-schema.ts'
31
31
  // surface — apps spread `...mdRoutes('content/docs', …)` into defineRoutes and
32
32
  // render sidebars from `mdNav(...)`. Re-exported here so user code never
33
33
  // imports runtime-internal paths.
34
- export { mdNav, mdRoutes } from './md/routes.ts'
34
+ export { mdNav, mdRoutes, mdUrlPath } from './md/routes.ts'
35
35
  export type {
36
36
  MdNavGroup,
37
37
  MdNavItem,
@@ -39,6 +39,12 @@ export type {
39
39
  MdRoutesOptions,
40
40
  MdRouteSource,
41
41
  } from './md/routes.ts'
42
+ // Lower-level md building blocks — the directory scanner and the heading
43
+ // slugger — so consumers (search indexers, sitemaps) build on the SAME
44
+ // scan/slug brust uses internally instead of copying it (FRAMEWORK-GAPS G1).
45
+ export { scanMdDir } from './md/scan.ts'
46
+ export type { MdFile } from './md/scan.ts'
47
+ export { slugifyHeading } from './md/slug.ts'
42
48
 
43
49
  // S2 + B3 — unified per-request scope. The request scope (B3 cookies/context) is
44
50
  // the OUTERMOST layer, then the request-scoped loader cache/dedupe (S2: one Map
@@ -150,11 +156,37 @@ export interface ErrorBoundaryProps {
150
156
  error: Error
151
157
  }
152
158
 
153
- export interface RouteCacheConfig {
154
- /** Time-to-live in seconds. */
159
+ /** L2 programmatic cache-key result. The `key` function builds the COMPLETE
160
+ * cache key (you concat url + query + DB-derived data yourself). */
161
+ export interface CacheKeyResult {
162
+ /** The COMPLETE L2 cache key. */
163
+ key: string
164
+ /** Tag groups for `cache.invalidate({ tags })`. */
165
+ tags?: string[]
166
+ /** Seconds; overrides `key_ttl_seconds` / `ttl_seconds`. */
167
+ ttl?: number
168
+ }
169
+
170
+ export interface RouteCacheConfig<Params = Record<string, string>> {
171
+ /** Base TTL in seconds (L1; L2 fallback). */
155
172
  ttl_seconds: number
156
- /** Request headers that affect content. Each becomes part of the cache key. */
157
- vary?: string[]
173
+ /** L1 declarative key prefix expression (evaluated in Rust, zero-Bun on hit). */
174
+ prefix?: string
175
+ /** Route to L2 when truthy: a key-expression (conditional) or `true` (always).
176
+ * Absent / `false` ⇒ L1 only. */
177
+ bypass?: string | boolean
178
+ /** L2 programmatic key (runs in the worker). Returns the COMPLETE key. */
179
+ key?: (ctx: {
180
+ req: BrustRequest
181
+ url: URL
182
+ params: Params
183
+ }) => CacheKeyResult | Promise<CacheKeyResult>
184
+ /** Static L2 TTL (seconds); `CacheKeyResult.ttl` overrides per-entry. */
185
+ key_ttl_seconds?: number
186
+ /** Static L1 invalidation tags. L1 entries cached for this route carry these
187
+ * tags so `cache.invalidate({ tags })` evicts them (L1 is no longer
188
+ * TTL-only). Route-level + static — not per-request. */
189
+ tags?: string[]
158
190
  }
159
191
 
160
192
  /** Shape returned by a middleware or by the terminal `next()` (loader + render).
@@ -415,10 +447,8 @@ function validateRoute(r: Route, basePath: string): void {
415
447
  if (r.children !== undefined) {
416
448
  assertNativeSubtree(r.children, where)
417
449
  }
418
- if (r.cache !== undefined) {
419
- throw new Error(`Route ${where}: 'native: true' cannot coexist with 'cache' (deferred)`)
420
- }
421
- // loader + middleware are EXPLICITLY allowed.
450
+ // `cache` is now allowed on native routes — they are the primary L1
451
+ // (zero-Bun) target. loader + middleware are EXPLICITLY allowed.
422
452
  }
423
453
  if (r.sse) {
424
454
  const where = r.path ?? '(no path)'
@@ -447,6 +477,14 @@ function validateRoute(r: Route, basePath: string): void {
447
477
  throw new Error(`Route ${where}: 'websocket' cannot have nested children`)
448
478
  }
449
479
  }
480
+ // L2 (cache.key) is only reachable via a truthy `bypass`. A key with no
481
+ // bypass is dead config — warn so it isn't silently ignored.
482
+ if (r.cache?.key && r.cache.bypass === undefined) {
483
+ const where = r.path ?? '(no path)'
484
+ console.warn(
485
+ `[brust] route ${where}: cache.key has no cache.bypass — L2 is unreachable (bypass never routes to it)`,
486
+ )
487
+ }
450
488
  }
451
489
 
452
490
  /** T2 — recursively assert every node in a native subtree is also `native: true`.
@@ -566,6 +604,9 @@ export type RouteCall =
566
604
  path: string
567
605
  params: Record<string, string>
568
606
  req: BrustRequest
607
+ /** Set by Rust when the route's `cache.bypass` matched — routes this
608
+ * request to the L2 programmatic cache (worker `cache.key`). */
609
+ bypassed?: boolean
569
610
  }
570
611
  | {
571
612
  kind: 'navigation'
@@ -633,6 +674,51 @@ export interface MakeRendererOptions {
633
674
  slots?: number
634
675
  }
635
676
 
677
+ /** May a framed single-chunk payload (`[meta_len: u16 BE][meta JSON][body]`)
678
+ * enter the L2 page cache? Only a plain 200 without Set-Cookie. The status
679
+ * gate is load-bearing: a jinja render failure comes back as a framed 500
680
+ * through the SAME napiRenderJinja success return as a real render, so without
681
+ * it one flaky render would poison the L2 key for its full TTL. A Set-Cookie
682
+ * response is per-client (a cached one would leak a session). Short/truncated/
683
+ * malformed payloads → NOT cacheable (fail closed). */
684
+ function payloadCacheable(payload: Uint8Array): boolean {
685
+ if (payload.length < 2) return false
686
+ const metaLen = (payload[0] << 8) | payload[1]
687
+ if (payload.length < 2 + metaLen) return false
688
+ try {
689
+ const meta = JSON.parse(new TextDecoder().decode(payload.subarray(2, 2 + metaLen)))
690
+ if (meta?.status !== 200) return false
691
+ const headers = (meta?.headers ?? {}) as Record<string, unknown>
692
+ return !Object.keys(headers).some((h) => h.toLowerCase() === 'set-cookie')
693
+ } catch {
694
+ return false
695
+ }
696
+ }
697
+
698
+ // Return a copy of a framed `[meta_len:u16 BE][meta JSON][body]` payload with
699
+ // `X-Brust-Cache: HIT` added to the meta headers — stamped on an L2 replay so a
700
+ // cache hit is observable on the wire (mirrors the L1 hit header). On any parse
701
+ // failure the original payload is returned unchanged (never corrupt a response).
702
+ function withCacheHitHeader(payload: Uint8Array): Uint8Array {
703
+ if (payload.length < 2) return payload
704
+ const metaLen = (payload[0] << 8) | payload[1]
705
+ if (payload.length < 2 + metaLen) return payload
706
+ try {
707
+ const meta = JSON.parse(new TextDecoder().decode(payload.subarray(2, 2 + metaLen)))
708
+ meta.headers = { ...(meta.headers ?? {}), 'X-Brust-Cache': 'HIT' }
709
+ const metaBytes = new TextEncoder().encode(JSON.stringify(meta))
710
+ const body = payload.subarray(2 + metaLen)
711
+ const out = new Uint8Array(2 + metaBytes.length + body.length)
712
+ out[0] = (metaBytes.length >> 8) & 0xff
713
+ out[1] = metaBytes.length & 0xff
714
+ out.set(metaBytes, 2)
715
+ out.set(body, 2 + metaBytes.length)
716
+ return out
717
+ } catch {
718
+ return payload
719
+ }
720
+ }
721
+
636
722
  export function makeRenderer(
637
723
  routes: FlatRoute[],
638
724
  view: Uint8Array,
@@ -751,6 +837,78 @@ export function makeRenderer(
751
837
  })
752
838
  }
753
839
 
840
+ // L2 programmatic page cache (Task 10). Only on a bypassed request whose
841
+ // route declares a `cache.key` function. Runs AFTER middleware (which may
842
+ // set cookies / auth context the key depends on) and BEFORE render.
843
+ // HIT → write the cached framed payload into the SAB slot, return its
844
+ // length — the exact fast-lane shape Rust reads from a native
845
+ // render, so the loader+render are skipped entirely.
846
+ // MISS → render normally, then capture slotView[0..len] and pageCacheSet.
847
+ // Streaming/Suspense responses never reach the `len > 0` fast-lane return
848
+ // below (renderBranchStreaming returns 0), so they are never L2-cached.
849
+ // L2 capture/replay rides the NATIVE SAB fast-lane (a framed payload in
850
+ // slotView + a length return). React routes emit via the chunk channel and
851
+ // return 0, so engaging L2 there would return a non-zero length into the
852
+ // streaming path = protocol corruption. Gate L2 to native routes; a React
853
+ // route with `bypass` still skips L1 and renders fresh (no L2).
854
+ const cc = flat.cache
855
+ const wantL2 =
856
+ call.bypassed === true && typeof cc?.key === 'function' && flat.nativeTemplate !== undefined
857
+ let l2Key: CacheKeyResult | undefined
858
+ if (wantL2 && cc) {
859
+ const url = new URL(call.req.url, 'http://internal') // req.url is a path+query
860
+ try {
861
+ l2Key = await cc.key!({ req: call.req, url, params: call.params ?? {} })
862
+ } catch (err) {
863
+ // A throwing/rejecting key() must not crash the worker — fall back to
864
+ // a fresh render. l2Key stays undefined → replay skipped, store no-op.
865
+ console.error('[brust] cache.key threw; bypassing L2 (rendering fresh):', err)
866
+ }
867
+ if (l2Key) {
868
+ const cached = (native as any).pageCacheGet?.(l2Key.key) as Buffer | null | undefined
869
+ if (cached && cached.length > 0) {
870
+ // REPLAY — stamp the framed meta with X-Brust-Cache: HIT, then write
871
+ // it into the slot and return its length (fast lane). Prefer the
872
+ // stamped payload; if the extra header tips it past the slot, fall
873
+ // back to the verbatim bytes; only a too-large verbatim payload
874
+ // falls through to a fresh render (never serve truncated bytes).
875
+ const stamped = withCacheHitHeader(cached)
876
+ const out =
877
+ stamped.length <= slotView.length
878
+ ? stamped
879
+ : cached.length <= slotView.length
880
+ ? cached
881
+ : null
882
+ if (out) {
883
+ slotView.set(out, 0)
884
+ return out.length
885
+ }
886
+ }
887
+ }
888
+ }
889
+ // Capture-and-store on an L2 miss. Wraps the native success returns; a
890
+ // no-op when not bypassed or no key. Only single-chunk (len > 0) bytes
891
+ // that fit the slot and carry no Set-Cookie are stored.
892
+ const maybeStoreL2 = (len: number): number => {
893
+ if (wantL2 && cc && l2Key && len > 0 && len <= slotView.length) {
894
+ const payload = slotView.subarray(0, len).slice() // copy out of the SAB
895
+ if (payloadCacheable(payload)) {
896
+ const ttlSec = l2Key.ttl ?? cc.key_ttl_seconds ?? cc.ttl_seconds
897
+ try {
898
+ ;(native as any).pageCacheSet?.(
899
+ l2Key.key,
900
+ l2Key.tags ?? [],
901
+ Math.round(ttlSec * 1000),
902
+ payload,
903
+ )
904
+ } catch (err) {
905
+ console.error('[brust] pageCacheSet failed (response served, not cached):', err)
906
+ }
907
+ }
908
+ }
909
+ return len
910
+ }
911
+
754
912
  // Sub-project J — native: true branch. Runs the leaf's loader (if any),
755
913
  // JSON-encodes the result into the SAB, then invokes napiRenderJinja
756
914
  // which performs the minijinja render Rust-side and emits a 200 chunk
@@ -890,12 +1048,14 @@ export function makeRenderer(
890
1048
  }
891
1049
  slotView.set(finalBytes, 0)
892
1050
  try {
893
- return (native as any).napiRenderJinja(
894
- Number(workerId),
895
- slot,
896
- finalBytes.length,
897
- flat.nativeTemplate,
898
- renderStatus,
1051
+ return maybeStoreL2(
1052
+ (native as any).napiRenderJinja(
1053
+ Number(workerId),
1054
+ slot,
1055
+ finalBytes.length,
1056
+ flat.nativeTemplate,
1057
+ renderStatus,
1058
+ ),
899
1059
  )
900
1060
  } catch (err) {
901
1061
  console.error(`[brust] napiRenderJinja failed for "${flat.nativeTemplate}":`, err)
@@ -920,12 +1080,14 @@ export function makeRenderer(
920
1080
  // writes the framed response into the SAB, and returns its length
921
1081
  // directly (no Promise round-trip). Return it up to the tsfn; Rust's
922
1082
  // fast-lane arm reads the SAB directly (no chunk channel).
923
- return (native as any).napiRenderJinja(
924
- Number(workerId),
925
- slot,
926
- dataBytes.length,
927
- flat.nativeTemplate,
928
- renderStatus,
1083
+ return maybeStoreL2(
1084
+ (native as any).napiRenderJinja(
1085
+ Number(workerId),
1086
+ slot,
1087
+ dataBytes.length,
1088
+ flat.nativeTemplate,
1089
+ renderStatus,
1090
+ ),
929
1091
  )
930
1092
  } catch (err) {
931
1093
  console.error(`[brust] napiRenderJinja failed for "${flat.nativeTemplate}":`, err)