brustjs 0.1.24-alpha → 0.1.26-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.
@@ -0,0 +1,61 @@
1
+ import { __scope } from './request-context.ts'
2
+
3
+ export interface CookieOptions {
4
+ maxAge?: number
5
+ expires?: Date
6
+ path?: string
7
+ domain?: string
8
+ secure?: boolean
9
+ httpOnly?: boolean
10
+ sameSite?: 'Strict' | 'Lax' | 'None'
11
+ }
12
+
13
+ // RFC 6265 cookie-name is a token: no control chars, whitespace, or separators.
14
+ const COOKIE_NAME = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/
15
+
16
+ /** Serialize a single Set-Cookie value. The value is URL-encoded; attributes
17
+ * are appended in a stable order. Mirrors the standard cookie attribute names.
18
+ *
19
+ * Hardening: the name is validated as an RFC 6265 token, and the final line is
20
+ * asserted CRLF-free — so a stray `\r\n` in a name/path/domain (which are NOT
21
+ * URL-encoded, unlike the value) can't smuggle an extra response header. */
22
+ export function serializeCookie(name: string, value: string, opts: CookieOptions = {}): string {
23
+ if (!COOKIE_NAME.test(name)) {
24
+ throw new Error(`invalid cookie name ${JSON.stringify(name)} (must be an RFC 6265 token)`)
25
+ }
26
+ let out = `${name}=${encodeURIComponent(value)}`
27
+ if (opts.maxAge !== undefined) out += `; Max-Age=${opts.maxAge}`
28
+ if (opts.expires !== undefined) out += `; Expires=${opts.expires.toUTCString()}`
29
+ if (opts.path !== undefined) out += `; Path=${opts.path}`
30
+ if (opts.domain !== undefined) out += `; Domain=${opts.domain}`
31
+ if (opts.secure) out += '; Secure'
32
+ if (opts.httpOnly) out += '; HttpOnly'
33
+ if (opts.sameSite !== undefined) out += `; SameSite=${opts.sameSite}`
34
+ if (/[\r\n]/.test(out)) {
35
+ throw new Error('cookie contains CR/LF — refusing to emit (header-injection guard)')
36
+ }
37
+ return out
38
+ }
39
+
40
+ /** Per-request cookie helper. `get` reads the incoming request cookies; `set`
41
+ * and `delete` stage a Set-Cookie onto the active request scope, flushed onto
42
+ * the response by routes.ts. Outside a request scope, `set`/`delete` are no-ops
43
+ * (dev-warn under BRUST_DEV). */
44
+ export const cookies = {
45
+ get(name: string): string | undefined {
46
+ return __scope()?.reqCookies[name]
47
+ },
48
+ set(name: string, value: string, opts?: CookieOptions): void {
49
+ const s = __scope()
50
+ if (!s) {
51
+ if (process.env.BRUST_DEV === '1') {
52
+ console.warn(`[brust] cookies.set('${name}') outside a request scope — no-op`)
53
+ }
54
+ return
55
+ }
56
+ s.setCookies.push(serializeCookie(name, value, opts))
57
+ },
58
+ delete(name: string, opts?: Pick<CookieOptions, 'path' | 'domain'>): void {
59
+ cookies.set(name, '', { ...opts, maxAge: 0 })
60
+ },
61
+ }
@@ -46,6 +46,10 @@ export interface EndpointOptions {
46
46
  middleware?: Middleware[]
47
47
  /** Build-time MCP tool description (read by the manifest extractor). */
48
48
  description?: string
49
+ /** Declared domain errors, keyed by code. Each value is a StandardSchema for
50
+ * the error's `data` payload. TYPE-ONLY: flows into the treaty client's typed
51
+ * error union; the runtime ignores it (handlers throw `ActionError`). */
52
+ errors?: Record<string, StandardSchemaV1>
49
53
  }
50
54
  export interface EndpointDef {
51
55
  method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD'
@@ -72,8 +76,19 @@ type QueryOf<O> = O extends { query: infer S }
72
76
  ? InferOutput<S>
73
77
  : unknown
74
78
  : Record<string, string>
79
+ /** Discriminated error union derived from `opts.errors`. Each declared code maps
80
+ * to `{ code; message; data }` where `data` is the schema's inferred output. */
81
+ type ErrorOf<O> = O extends { errors: infer E }
82
+ ? {
83
+ [K in keyof E & string]: {
84
+ code: K
85
+ message: string
86
+ data: E[K] extends StandardSchemaV1 ? InferOutput<E[K]> : unknown
87
+ }
88
+ }[keyof E & string]
89
+ : never
75
90
 
76
- export type EndpointEntry = { input: unknown; output: unknown }
91
+ export type EndpointEntry = { input: unknown; output: unknown; error: unknown }
77
92
  export type EndpointMap = Record<string, Partial<Record<EndpointDef['method'], EndpointEntry>>>
78
93
 
79
94
  export function isValidEndpointPath(p: string): boolean {
@@ -88,32 +103,44 @@ export interface ActionsBuilder<Acc extends EndpointMap = {}> {
88
103
  path: P,
89
104
  handler: Handler<BodyOf<O>, Params<P>, QueryOf<O>, R>,
90
105
  opts?: O,
91
- ): ActionsBuilder<Acc & { [K in P]: { GET: { input: QueryOf<O>; output: Awaited<R> } } }>
106
+ ): ActionsBuilder<
107
+ Acc & { [K in P]: { GET: { input: QueryOf<O>; output: Awaited<R>; error: ErrorOf<O> } } }
108
+ >
92
109
  post<P extends string, O extends EndpointOptions, R>(
93
110
  path: P,
94
111
  handler: Handler<BodyOf<O>, Params<P>, QueryOf<O>, R>,
95
112
  opts?: O,
96
- ): ActionsBuilder<Acc & { [K in P]: { POST: { input: BodyOf<O>; output: Awaited<R> } } }>
113
+ ): ActionsBuilder<
114
+ Acc & { [K in P]: { POST: { input: BodyOf<O>; output: Awaited<R>; error: ErrorOf<O> } } }
115
+ >
97
116
  put<P extends string, O extends EndpointOptions, R>(
98
117
  path: P,
99
118
  handler: Handler<BodyOf<O>, Params<P>, QueryOf<O>, R>,
100
119
  opts?: O,
101
- ): ActionsBuilder<Acc & { [K in P]: { PUT: { input: BodyOf<O>; output: Awaited<R> } } }>
120
+ ): ActionsBuilder<
121
+ Acc & { [K in P]: { PUT: { input: BodyOf<O>; output: Awaited<R>; error: ErrorOf<O> } } }
122
+ >
102
123
  patch<P extends string, O extends EndpointOptions, R>(
103
124
  path: P,
104
125
  handler: Handler<BodyOf<O>, Params<P>, QueryOf<O>, R>,
105
126
  opts?: O,
106
- ): ActionsBuilder<Acc & { [K in P]: { PATCH: { input: BodyOf<O>; output: Awaited<R> } } }>
127
+ ): ActionsBuilder<
128
+ Acc & { [K in P]: { PATCH: { input: BodyOf<O>; output: Awaited<R>; error: ErrorOf<O> } } }
129
+ >
107
130
  delete<P extends string, O extends EndpointOptions, R>(
108
131
  path: P,
109
132
  handler: Handler<BodyOf<O>, Params<P>, QueryOf<O>, R>,
110
133
  opts?: O,
111
- ): ActionsBuilder<Acc & { [K in P]: { DELETE: { input: BodyOf<O>; output: Awaited<R> } } }>
134
+ ): ActionsBuilder<
135
+ Acc & { [K in P]: { DELETE: { input: BodyOf<O>; output: Awaited<R>; error: ErrorOf<O> } } }
136
+ >
112
137
  head<P extends string, O extends EndpointOptions, R>(
113
138
  path: P,
114
139
  handler: Handler<BodyOf<O>, Params<P>, QueryOf<O>, R>,
115
140
  opts?: O,
116
- ): ActionsBuilder<Acc & { [K in P]: { HEAD: { input: QueryOf<O>; output: Awaited<R> } } }>
141
+ ): ActionsBuilder<
142
+ Acc & { [K in P]: { HEAD: { input: QueryOf<O>; output: Awaited<R>; error: ErrorOf<O> } } }
143
+ >
117
144
  }
118
145
 
119
146
  export function defineActions(): ActionsBuilder {
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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
80
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
96
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
117
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
133
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
150
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
166
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
185
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
201
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
217
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
237
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
253
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
274
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
290
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
308
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
324
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
342
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
358
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
376
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
392
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
410
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
426
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
443
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
459
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
479
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
495
+ if (bindingPackageVersion !== '0.1.26-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.26-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.24-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.24-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
511
+ if (bindingPackageVersion !== '0.1.26-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.26-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
513
513
  }
514
514
  return binding
515
515
  } catch (e) {
package/runtime/index.ts CHANGED
@@ -781,8 +781,17 @@ export type { BrustPageProps, HeadEntry } from './islands/brust-page.tsx'
781
781
  export { defineStore, signal, computed, effect, batch } from './store/index.ts'
782
782
  export type { StoreHandle, Snapshot, Signal, Computed } from './store/index.ts'
783
783
 
784
+ // S2 — request-scoped loader cache/dedupe helpers. `runInRequestCache` (the
785
+ // scope opener) is owned by the runtime route handler and intentionally NOT
786
+ // exported.
787
+ export { dedupe, cachedFetch } from './loader-cache.ts'
788
+
784
789
  export { buildIslands } from './islands/build.ts'
785
790
  export type { IslandsBuildResult, BuildIslandsOptions } from './islands/build.ts'
786
791
 
787
792
  export { cache } from './cache.ts'
788
793
  export type { InvalidateArgs } from './cache.ts'
794
+
795
+ export { getRequestContext } from './request-context.ts'
796
+ export { cookies } from './cookies.ts'
797
+ export type { CookieOptions } from './cookies.ts'
@@ -16,6 +16,8 @@
16
16
  import { createRoot, hydrateRoot, type Root } from 'react-dom/client'
17
17
  import { createElement } from 'react'
18
18
  import { applyStoreSnapshot } from '../store/client-hydrate.ts'
19
+ import { __navStart, __navCommit, __navError, __navInit } from '../navigation/store.ts'
20
+ import { installActiveNav } from '../navigation/active-nav.ts'
19
21
 
20
22
  // Track React roots created by hydrateOne so we can unmount them before
21
23
  // removing their DOM in swapMainContent. Without this, removing the DOM
@@ -205,11 +207,12 @@ export function isInternalLink(a: HTMLAnchorElement, event: MouseEvent): boolean
205
207
 
206
208
  let inFlight: AbortController | null = null
207
209
 
208
- async function navigate(url: URL, push: boolean): Promise<void> {
210
+ export async function navigate(url: URL, push: boolean): Promise<void> {
209
211
  inFlight?.abort()
210
212
  const ac = new AbortController()
211
213
  inFlight = ac
212
214
  try {
215
+ __navStart(url.pathname, url.search)
213
216
  const resp = await fetch(`/_brust/page${url.pathname}${url.search}`, {
214
217
  signal: ac.signal,
215
218
  headers: { Accept: 'application/json' },
@@ -229,8 +232,10 @@ async function navigate(url: URL, push: boolean): Promise<void> {
229
232
  if (push) history.pushState({}, '', url.href)
230
233
  window.scrollTo(0, 0)
231
234
  hydrateMarkersIn(main as HTMLElement)
235
+ __navCommit(url.pathname, url.search)
232
236
  } catch (err) {
233
237
  if ((err as Error).name === 'AbortError') return
238
+ __navError(url.pathname, err)
234
239
  console.warn('[brust] SPA navigation failed, falling back to full reload:', err)
235
240
  location.href = url.href
236
241
  } finally {
@@ -260,10 +265,14 @@ function installInterceptor(): void {
260
265
  if (typeof document !== 'undefined') {
261
266
  if (document.readyState === 'loading') {
262
267
  document.addEventListener('DOMContentLoaded', () => {
268
+ __navInit(location.pathname, location.search)
269
+ installActiveNav()
263
270
  hydrateMarkersIn(document.body)
264
271
  installInterceptor()
265
272
  })
266
273
  } else {
274
+ __navInit(location.pathname, location.search)
275
+ installActiveNav()
267
276
  hydrateMarkersIn(document.body)
268
277
  installInterceptor()
269
278
  }
@@ -13,8 +13,11 @@ export interface IslandProps<P> {
13
13
  * component (no anonymous default export). */
14
14
  component: ComponentType<P>
15
15
  /** Props passed to the component on both server and client. Must be
16
- * JSON-serializable (no functions, classes, DOM nodes, etc.). */
17
- props: P
16
+ * JSON-serializable (no functions, classes, DOM nodes, etc.). Optional — a
17
+ * propless island (e.g. one that reads only global/client state) may omit it;
18
+ * it defaults to `{}`. On native routes, omitting `props` lowers to an empty
19
+ * props_path the renderer fills with `{}`. */
20
+ props?: P
18
21
  /** When to hydrate. Default 'load'. */
19
22
  hydrate?: HydrateTrigger
20
23
  /** Native routes only: render this island server-side (renderToString during
@@ -74,7 +77,8 @@ export function Island<P extends object>({
74
77
  'export or a minified/inlined function.',
75
78
  )
76
79
  }
77
- const propsJson = JSON.stringify(props)
80
+ const resolvedProps = (props ?? {}) as P
81
+ const propsJson = JSON.stringify(resolvedProps)
78
82
  return createElement(
79
83
  'div',
80
84
  {
@@ -82,6 +86,6 @@ export function Island<P extends object>({
82
86
  'data-brust-props': propsJson,
83
87
  'data-brust-hydrate': hydrate,
84
88
  },
85
- createElement(Component, props),
89
+ createElement(Component, resolvedProps),
86
90
  )
87
91
  }
@@ -149,7 +149,9 @@ export async function resolveIslandContext(
149
149
  ): Promise<Record<string, string>> {
150
150
  const out: Record<string, string> = {}
151
151
  for (const entry of manifest) {
152
- const props = pathInto(data, entry.propsPath)
152
+ // Empty propsPath = a propless island → `{}`. (pathInto('') returns the whole
153
+ // context, which is NOT what an absent `props` attr means.)
154
+ const props = entry.propsPath === '' ? {} : pathInto(data, entry.propsPath)
153
155
  // `?? null` handles undefined props; the `?? 'null'` belt-and-braces covers
154
156
  // the case where JSON.stringify itself returns undefined (e.g. a function
155
157
  // value), so entityEncode never receives undefined. Hoisted ABOVE the ssr
@@ -0,0 +1,42 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks'
2
+
3
+ const cacheCtx = new AsyncLocalStorage<Map<string, Promise<unknown>>>()
4
+
5
+ export function runInRequestCache<T>(fn: () => T): T {
6
+ return cacheCtx.run(new Map(), fn)
7
+ }
8
+
9
+ /** Request-scoped memoize: share the in-flight promise + cache result for the
10
+ * scope's lifetime. Outside a scope → passthrough. Reject → guarded delete
11
+ * (identity-checked) so a stale catch can't evict a newer entry.
12
+ *
13
+ * NOTE: the reject cleanup runs one microtask after rejection, so a caller that
14
+ * dedupes the SAME key within that gap receives the about-to-reject promise (and
15
+ * thus the rejection) — acceptable: the result is one shared failure, not a hang. */
16
+ export function dedupe<T>(key: string, fn: () => Promise<T>): Promise<T> {
17
+ const map = cacheCtx.getStore()
18
+ if (!map) return fn()
19
+ const existing = map.get(key) as Promise<T> | undefined
20
+ if (existing) return existing
21
+ const p = fn()
22
+ map.set(key, p)
23
+ p.catch(() => {
24
+ if (map.get(key) === p) map.delete(key)
25
+ })
26
+ return p
27
+ }
28
+
29
+ /** Idempotent (GET/HEAD) fetch deduped per request; non-idempotent → bypass.
30
+ * Returns a fresh clone every call (the stored Response is never exposed).
31
+ *
32
+ * NOTE: the cache key is `method + url` ONLY — it does NOT include `init`
33
+ * headers/body. Two `cachedFetch(sameUrl, {headers:…})` calls with DIFFERENT
34
+ * headers in one request share the FIRST call's response. Intended for plain
35
+ * idempotent GETs (the common loader case); if a caller varies `init` per call
36
+ * on the same URL, use `fetch` directly. */
37
+ export function cachedFetch(url: string, init?: RequestInit): Promise<Response> {
38
+ const method = (init?.method ?? 'GET').toUpperCase()
39
+ if (method !== 'GET' && method !== 'HEAD') return fetch(url, init)
40
+ // `dedupe` infers T = Response from the fetch thunk, so `r` is already Response.
41
+ return dedupe(`${method} ${url}`, () => fetch(url, init)).then((r) => r.clone())
42
+ }