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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/README.md +66 -6
  2. package/native/index.d.ts +87 -5
  3. package/native/index.js +61 -54
  4. package/package.json +22 -9
  5. package/src/actions/client.ts +22 -5
  6. package/src/actions/load.ts +2 -1
  7. package/src/actions/prefix.ts +27 -3
  8. package/src/actions-bridge.ts +76 -19
  9. package/src/browser.ts +3 -1
  10. package/src/build/actions-only.ts +86 -0
  11. package/src/build/build-eval.ts +319 -0
  12. package/src/build/build-icons.ts +69 -0
  13. package/src/build/bundle.ts +278 -43
  14. package/src/build/cf-assets.ts +83 -0
  15. package/src/build/cf-core.ts +90 -0
  16. package/src/build/cf-entry.ts +76 -0
  17. package/src/build/cf-wrangler.ts +169 -0
  18. package/src/build/cloudflare.ts +317 -0
  19. package/src/build/compile.ts +72 -4
  20. package/src/build/config.ts +274 -15
  21. package/src/build/css.ts +275 -0
  22. package/src/build/features.ts +110 -0
  23. package/src/build/index.ts +223 -20
  24. package/src/build/manifest.ts +59 -8
  25. package/src/build/pkg-src.ts +383 -0
  26. package/src/build/scan.ts +61 -20
  27. package/src/build/server-only.ts +39 -0
  28. package/src/build/ssg-render.ts +65 -0
  29. package/src/build/ssg.ts +146 -0
  30. package/src/build/static-worker.ts +91 -0
  31. package/src/build/static.ts +258 -0
  32. package/src/build/tailwind.ts +117 -0
  33. package/src/build/tsconfig-paths.ts +67 -0
  34. package/src/build-time.ts +10 -0
  35. package/src/cache-adapter/bridge.ts +91 -0
  36. package/src/cache-adapter/conformance.ts +226 -0
  37. package/src/cache-adapter/index.ts +10 -0
  38. package/src/cache-adapter/load.ts +144 -0
  39. package/src/cache-adapter/memory.ts +229 -0
  40. package/src/cache-adapter/runtime.ts +124 -0
  41. package/src/cache-adapter/types.ts +96 -0
  42. package/src/cache-adapter/workers.ts +118 -0
  43. package/src/cache.ts +4 -1
  44. package/src/cf.ts +19 -0
  45. package/src/cli.ts +50 -2
  46. package/src/config.ts +127 -25
  47. package/src/css-modules.d.ts +8 -0
  48. package/src/dev/bun.ts +365 -0
  49. package/src/dev/client.ts +92 -0
  50. package/src/dev/devtools-data.ts +23 -0
  51. package/src/dev/devtools.ts +356 -0
  52. package/src/dev/diag.ts +30 -0
  53. package/src/dev/proxy.ts +236 -0
  54. package/src/dev/tui.ts +144 -0
  55. package/src/dev/watch.ts +81 -0
  56. package/src/dev.ts +93 -0
  57. package/src/handlers.ts +282 -0
  58. package/src/index.ts +3 -1
  59. package/src/native.ts +49 -2
  60. package/src/routes.ts +20 -1
  61. package/src/run.ts +48 -11
  62. package/src/server-handlers.ts +32 -0
  63. package/src/worker.ts +129 -245
@@ -0,0 +1,383 @@
1
+ // Package sources (shadcn S3 step 1, F153c, R34.61/R34.62): the compiler analyses a package component from the
2
+ // AUTHORED source its source map carries (`sourcesContent`), never from the published JS (G1, G2). This module runs
3
+ // no package code: sources are read, transpiled for the integrity check, and written, never imported; the compiler
4
+ // only parses them, and the build never evaluates a slot of a component compiled from them (L2). (The route scan
5
+ // imports the app graph, and with it the packages' published JS, as it always has: that is not new here.)
6
+ //
7
+ // For every bare import the app reaches (and, transitively, every bare import of a package source written here),
8
+ // the package's ESM entry is resolved, its `.map` read, and the `.ts`/`.tsx` sources written under
9
+ // `<app>/.brust/pkg-src/p-<hash>/` (the hash of the package's path: bun's store installs one copy per dependant; no
10
+ // text the package controls is ever part of a path). The compiler gets the resolution map (`PackageSources`), so it reads only what this build wrote:
11
+ // the directory is removed first, and a stale one is never consulted.
12
+ //
13
+ // Fail closed: a package with no ESM entry, no map, no source for its entry, a source outside the package, a
14
+ // relative import the map does not carry, a dynamic `import()` or `eval`, or a source whose literals and names are
15
+ // not all in the published JS (R34.62: the React build runs the dist, not the map; a containment check of literals and
16
+ // names, i.e. fidelity, not a security boundary: the author controls both files) is REFUSED, with the reason;
17
+ // the compiler then renders its components as React islands of the package export, exactly as before.
18
+ import { createHash } from 'node:crypto'
19
+ import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } from 'node:fs'
20
+ import { dirname, join, posix, relative, resolve, sep } from 'node:path'
21
+ import { BuildError } from './errors'
22
+
23
+ /** What the compiler reads (`AnalyzeOptions.packages`): per importing scope (`""` = the app, else the root-relative
24
+ * directory of a written package), each bare specifier → the root-relative path of its written entry source, or the
25
+ * reason it was refused. */
26
+ export interface PackageSources {
27
+ scopes: Record<string, Record<string, string>>
28
+ refused: Record<string, Record<string, string>>
29
+ /** Each written directory (root-relative) → the package's real directory: a bare import in an artifact compiled from
30
+ * a package source resolves from the package, as it does in a real install (not read by the compiler). */
31
+ dirs: Record<string, string>
32
+ }
33
+
34
+ /** The last build's written directories (absolute) → the package's real directory, for the bundler. */
35
+ const writtenDirs = new Map<string, string>()
36
+
37
+ /** The real directory of the package whose written source `file` (absolute) is, if it is one. */
38
+ export function packageDirOf(file: string): string | undefined {
39
+ for (const [dir, real] of writtenDirs) if (file.startsWith(dir + sep) || file.startsWith(`${dir}/`)) return real
40
+ return undefined
41
+ }
42
+
43
+ /** Where the sources go, relative to the app root. */
44
+ export const PKG_SRC_DIR = '.brust/pkg-src'
45
+
46
+ const lstatSyncOrNull = (p: string) => {
47
+ try {
48
+ return lstatSync(p)
49
+ } catch {
50
+ return null
51
+ }
52
+ }
53
+
54
+ /** Never followed: the React runtime itself, brust, and platform modules. */
55
+ const NEVER = (spec: string) =>
56
+ /^(react|react-dom|scheduler)(\/|$)/.test(spec) ||
57
+ /^(@brust\/|brust(\/|$)|brustjs(\/|$))/.test(spec) ||
58
+ /^(node|bun):/.test(spec) ||
59
+ // L23 inlines lucide icons from the package export; the icons pass owns them.
60
+ /^lucide-react(\/|$)/.test(spec)
61
+
62
+ const LIMITS = { packages: 256, bytesPerPackage: 4 * 1024 * 1024, totalBytes: 64 * 1024 * 1024 }
63
+
64
+ type Outcome = { ok: true; dir: string; entry: string; imports: string[] } | { ok: false; reason: string }
65
+
66
+ /** `@scope/name/sub` → [`@scope/name`, `sub`]. */
67
+ export function splitSpecifier(spec: string): [string, string] {
68
+ const parts = spec.split('/')
69
+ const n = spec.startsWith('@') ? 2 : 1
70
+ return [parts.slice(0, n).join('/'), parts.slice(n).join('/')]
71
+ }
72
+
73
+ /** The first `node_modules/<name>` from `from` upwards, as a real path. */
74
+ function findPackage(name: string, from: string): string | null {
75
+ for (let dir = from; ; ) {
76
+ const p = join(dir, 'node_modules', name)
77
+ if (existsSync(join(p, 'package.json'))) return realpathSync(p)
78
+ const up = dirname(dir)
79
+ if (up === dir) return null
80
+ dir = up
81
+ }
82
+ }
83
+
84
+ /** The package's ESM entry for `import` (exports `.` → `import`/`module`/`default`, then `module`). */
85
+ function esmEntry(pj: Record<string, unknown>): string | null {
86
+ const pick = (x: unknown): string | null => {
87
+ if (typeof x === 'string') return x
88
+ if (!x || typeof x !== 'object') return null
89
+ const o = x as Record<string, unknown>
90
+ for (const k of ['import', 'module', 'default']) if (k in o) return pick(o[k])
91
+ return null
92
+ }
93
+ const exp = pj.exports
94
+ const dot = typeof exp === 'string' ? exp : exp && typeof exp === 'object' ? (exp as Record<string, unknown>)['.'] : undefined
95
+ const viaExports = dot === undefined ? null : pick(dot)
96
+ if (viaExports) return viaExports
97
+ return typeof pj.module === 'string' ? pj.module : null
98
+ }
99
+
100
+ /** The source map of `file`: its `sourceMappingURL` (a relative file), else `<file>.map`. */
101
+ function mapOf(file: string): string | null {
102
+ const text = readFileSync(file, 'utf8')
103
+ const m = /\/\/# sourceMappingURL=([^\s'"]+)\s*$/.exec(text)
104
+ if (m && !m[1]!.startsWith('data:') && !/^[a-z]+:/i.test(m[1]!)) {
105
+ const p = join(dirname(file), m[1]!)
106
+ if (existsSync(p)) return p
107
+ }
108
+ return existsSync(`${file}.map`) ? `${file}.map` : null
109
+ }
110
+
111
+ // ---------------------------------------------------------------------------------------------------------------
112
+ // Integrity (R34.62): every string/number literal and every name of the transpiled sources occurs in the published
113
+ // file the map belongs to. Both sides go through the same transpiler first, so quoting and type syntax agree.
114
+
115
+ /** Names the JSX transform and esbuild add or rename (`undefined` prints as `void 0`); not evidence either way. */
116
+ const JSX_NAMES = new Set(['createElement', '__name', '__defProp', 'React', 'undefined'])
117
+ const JSX_RUNTIME = /^(?:jsx|jsxs|jsxDEV|Fragment)(?:_[\w$]+)?$/
118
+ /** Keywords are syntax, not names: esbuild prints `const` as `var` and drops `type`/`interface` with the types. */
119
+ const KEYWORDS = new Set(
120
+ 'await break case catch class const continue debugger default delete do else enum export extends false finally for function if implements import in instanceof interface let new null package private protected public return static super switch this throw true try typeof var void while with yield async of get set as from type declare readonly abstract namespace module satisfies keyof infer is unique asserts override accessor'.split(' '),
121
+ )
122
+
123
+ export interface Skeleton {
124
+ strings: Set<string>
125
+ names: Set<string>
126
+ }
127
+
128
+ /** Literals and names of transpiled JS (a small lexer: strings, templates, comments, numbers, identifiers; a regex
129
+ * literal may be read as names, which only makes a mismatch more likely: fail closed). */
130
+ export function skeleton(js: string): Skeleton {
131
+ const strings = new Set<string>()
132
+ const names = new Set<string>()
133
+ let i = 0
134
+ const n = js.length
135
+ const templateDepth: number[] = []
136
+ let braces = 0
137
+ const readTemplateChunk = () => {
138
+ // at the char after '`' or after the '}' that closes `${`
139
+ let s = ''
140
+ while (i < n) {
141
+ const c = js[i]!
142
+ if (c === '\\') {
143
+ s += js.slice(i, i + 2)
144
+ i += 2
145
+ } else if (c === '`') {
146
+ i++
147
+ strings.add(s)
148
+ return
149
+ } else if (c === '$' && js[i + 1] === '{') {
150
+ i += 2
151
+ strings.add(s)
152
+ templateDepth.push(braces)
153
+ braces++
154
+ return
155
+ } else {
156
+ s += c
157
+ i++
158
+ }
159
+ }
160
+ }
161
+ while (i < n) {
162
+ const c = js[i]!
163
+ if (c === '/' && js[i + 1] === '/') {
164
+ while (i < n && js[i] !== '\n') i++
165
+ } else if (c === '/' && js[i + 1] === '*') {
166
+ const end = js.indexOf('*/', i + 2)
167
+ i = end < 0 ? n : end + 2
168
+ } else if (c === '"' || c === "'") {
169
+ let s = ''
170
+ i++
171
+ while (i < n && js[i] !== c) {
172
+ if (js[i] === '\\') {
173
+ s += js.slice(i, i + 2)
174
+ i += 2
175
+ } else s += js[i++]
176
+ }
177
+ i++
178
+ strings.add(s)
179
+ } else if (c === '`') {
180
+ i++
181
+ readTemplateChunk()
182
+ } else if (c === '{') {
183
+ braces++
184
+ i++
185
+ } else if (c === '}') {
186
+ braces--
187
+ i++
188
+ if (templateDepth.length && templateDepth[templateDepth.length - 1] === braces) {
189
+ templateDepth.pop()
190
+ readTemplateChunk()
191
+ }
192
+ } else if (/[0-9]/.test(c) || (c === '.' && /[0-9]/.test(js[i + 1] ?? ''))) {
193
+ const m = /^(?:0[xX][0-9a-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|(?:\d[\d_]*\.?\d*(?:[eE][+-]?\d+)?|\.\d+(?:[eE][+-]?\d+)?))n?/.exec(js.slice(i))
194
+ const tok = m ? m[0] : c
195
+ const num = Number(tok.replace(/_/g, '').replace(/n$/, ''))
196
+ strings.add(Number.isNaN(num) ? tok : `#${num}`)
197
+ i += tok.length
198
+ } else if (/[A-Za-z_$]/.test(c)) {
199
+ const m = /^[A-Za-z_$][\w$]*/.exec(js.slice(i))!
200
+ names.add(m[0])
201
+ i += m[0].length
202
+ } else i++
203
+ }
204
+ return { strings, names }
205
+ }
206
+
207
+ const transpile = (code: string, loader: 'tsx' | 'ts' | 'js') =>
208
+ new Bun.Transpiler({ loader, target: 'browser', minifyWhitespace: false, deadCodeElimination: false, trimUnusedImports: false }).transformSync(code)
209
+
210
+ /** What of `sources` is not in `published`; empty = the sources are what the published file was built from. */
211
+ export function integrityGaps(sources: { path: string; text: string }[], published: string): string[] {
212
+ const pub = skeleton(transpile(published, 'js'))
213
+ const gaps: string[] = []
214
+ for (const s of sources) {
215
+ const loader = s.path.endsWith('.tsx') ? 'tsx' : 'ts'
216
+ const sk = skeleton(transpile(s.text, loader))
217
+ // Import specifiers are module names, which bundling replaces; they are checked as imports instead.
218
+ for (const im of new Bun.Transpiler({ loader }).scanImports(s.text)) sk.strings.delete(im.path)
219
+ for (const v of sk.strings) if (!pub.strings.has(v)) gaps.push(`${s.path}: literal ${JSON.stringify(v.startsWith('#') ? v.slice(1) : v).slice(0, 60)}`)
220
+ for (const v of sk.names) if (!JSX_NAMES.has(v) && !JSX_RUNTIME.test(v) && !KEYWORDS.has(v) && !pub.names.has(v)) gaps.push(`${s.path}: name \`${v}\``)
221
+ }
222
+ return gaps
223
+ }
224
+
225
+ // ---------------------------------------------------------------------------------------------------------------
226
+
227
+ /** Reads one package (real directory `dir`), checks it, and writes its sources under `outRoot`. */
228
+ function writePackage(appRoot: string, dir: string, budget: { bytes: number }): Outcome {
229
+ const pj = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')) as Record<string, unknown>
230
+ const entryRel = esmEntry(pj)
231
+ if (!entryRel) return { ok: false, reason: 'no ESM entry (exports `import` or `module`)' }
232
+ const entry = join(dir, entryRel)
233
+ if (!existsSync(entry) || !statSync(entry).isFile()) return { ok: false, reason: `the ESM entry ${entryRel} is missing` }
234
+ const mapFile = mapOf(entry)
235
+ if (!mapFile) return { ok: false, reason: 'no source map for the ESM entry' }
236
+ let map: { sources?: unknown; sourcesContent?: unknown }
237
+ try {
238
+ map = JSON.parse(readFileSync(mapFile, 'utf8'))
239
+ } catch {
240
+ return { ok: false, reason: 'the source map is not JSON' }
241
+ }
242
+ const sources = Array.isArray(map.sources) ? (map.sources as unknown[]) : []
243
+ const contents = Array.isArray(map.sourcesContent) ? (map.sourcesContent as unknown[]) : []
244
+ // Each source as a path inside the package; anything outside it, or not TS/TSX, is not ours to read.
245
+ const files: { path: string; text: string }[] = []
246
+ for (const [k, s] of sources.entries()) {
247
+ const text = contents[k]
248
+ if (typeof s !== 'string' || typeof text !== 'string') continue
249
+ const abs = posix.normalize(posix.join(relative(dir, dirname(mapFile)).split(sep).join('/'), s))
250
+ if (abs.startsWith('..') || posix.isAbsolute(abs) || /^[a-z]+:/i.test(s)) return { ok: false, reason: `the source ${s} lies outside the package` }
251
+ if (!/\.tsx?$/.test(abs)) continue
252
+ files.push({ path: abs, text })
253
+ }
254
+ if (files.length === 0) return { ok: false, reason: 'the source map carries no TypeScript sources (`sourcesContent`)' }
255
+ const bytes = files.reduce((a, f) => a + f.text.length, 0)
256
+ if (bytes > LIMITS.bytesPerPackage) return { ok: false, reason: `the sources are ${bytes} bytes (limit ${LIMITS.bytesPerPackage})` }
257
+ if (budget.bytes + bytes > LIMITS.totalBytes) return { ok: false, reason: 'the package-source budget of this build is spent' }
258
+ // Imports: relative ones must be in the map (the compiler would otherwise fail on them), bare ones are followed.
259
+ const have = new Set(files.map((f) => f.path))
260
+ const bare = new Set<string>()
261
+ const imported = new Set<string>()
262
+ for (const f of files) {
263
+ if (/\beval\s*\(|\bnew\s+Function\b/.test(f.text)) return { ok: false, reason: `${f.path} calls eval or new Function` }
264
+ let imports: { path: string; kind: string }[]
265
+ try {
266
+ imports = new Bun.Transpiler({ loader: f.path.endsWith('.tsx') ? 'tsx' : 'ts' }).scanImports(f.text)
267
+ } catch (e) {
268
+ return { ok: false, reason: `${f.path} does not parse: ${String(e instanceof Error ? e.message : e).split('\n')[0]}` }
269
+ }
270
+ for (const im of imports) {
271
+ // The JSX transform's own runtime import (the transpiler adds it), and React itself.
272
+ if (NEVER(im.path)) continue
273
+ if (im.kind === 'dynamic-import' || im.kind === 'require-call' || im.kind === 'require-resolve') return { ok: false, reason: `${f.path} has a ${im.kind} (${im.path})` }
274
+ if (im.path.startsWith('./') || im.path.startsWith('../')) {
275
+ const base = posix.normalize(posix.join(posix.dirname(f.path), im.path))
276
+ const hit = [base, `${base}.tsx`, `${base}.ts`, `${base}/index.tsx`, `${base}/index.ts`].find((c) => have.has(c))
277
+ if (hit) imported.add(hit)
278
+ else return { ok: false, reason: `${f.path} imports ${im.path}, which the source map does not carry` }
279
+ } else if (im.path.startsWith('/') || /^[a-z]+:/i.test(im.path)) return { ok: false, reason: `${f.path} imports ${im.path}` }
280
+ else bare.add(im.path)
281
+ }
282
+ }
283
+ // The entry source: the one `package.json#source` names, else the only source no other source imports.
284
+ const declared = typeof pj.source === 'string' ? posix.normalize(pj.source.replace(/^\.\//, '')) : null
285
+ const roots = files.filter((f) => !imported.has(f.path))
286
+ const entrySrc = files.find((f) => f.path === declared) ?? (roots.length === 1 ? roots[0] : undefined)
287
+ if (!entrySrc) return { ok: false, reason: `the source map has ${files.length} sources and none is the entry (no \`source\` field, ${roots.length} unimported)` }
288
+ // R34.62: the published file must be what these sources were built into.
289
+ let gaps: string[]
290
+ try {
291
+ gaps = integrityGaps(files, readFileSync(entry, 'utf8'))
292
+ } catch (e) {
293
+ return { ok: false, reason: `the integrity check could not read the sources or ${entryRel}: ${String(e instanceof Error ? e.message : e).split('\n')[0]}` }
294
+ }
295
+ if (gaps.length > 0) return { ok: false, reason: `the sources do not match the published ${entryRel} (${gaps.length} differences, first: ${gaps[0]})` }
296
+ // Keyed on where the package sits relative to the app (stable across machines), not its absolute path.
297
+ // Review F1 (security): no package-controlled text in the path. The directory is named by a hash of where the package
298
+ // sits relative to the app (stable across machines), and every target must resolve inside the directory this build
299
+ // owns before anything is created; a `name`/`version` of `../..` cannot move a write.
300
+ const hash = createHash('sha256').update(relative(appRoot, dir).split(sep).join('/')).digest('hex').slice(0, 16)
301
+ const outRel = posix.join(PKG_SRC_DIR, `p-${hash}`)
302
+ const own = resolve(appRoot, PKG_SRC_DIR) + sep
303
+ const targets = files.map((f) => ({ f, target: resolve(appRoot, outRel, f.path) }))
304
+ const escaped = targets.find((t) => !t.target.startsWith(own))
305
+ if (escaped) return { ok: false, reason: `the source ${escaped.f.path} would be written outside ${PKG_SRC_DIR}` }
306
+ for (const { f, target } of targets) {
307
+ mkdirSync(dirname(target), { recursive: true })
308
+ // The directories just made are this build's: a real path elsewhere means a link was planted in between.
309
+ if (!`${realpathSync(dirname(target))}${sep}`.startsWith(realpathSync(resolve(appRoot, PKG_SRC_DIR)) + sep))
310
+ return { ok: false, reason: `the source ${f.path} would be written outside ${PKG_SRC_DIR}` }
311
+ writeFileSync(target, f.text)
312
+ }
313
+ budget.bytes += bytes
314
+ return { ok: true, dir: outRel, entry: posix.join(outRel, entrySrc.path), imports: [...bare].sort() }
315
+ }
316
+
317
+ /**
318
+ * Writes the sources of every package `specs` reaches from the app (and of every package those sources import),
319
+ * and returns the resolution map for the compiler. The directory is rebuilt from nothing on every call.
320
+ */
321
+ export function preparePackageSources(appRoot: string, specs: Iterable<string>): PackageSources {
322
+ const root = realpathSync(appRoot)
323
+ // BEFORE anything is removed (Mellow N1, F163): neither `.brust` nor `.brust/pkg-src` may be a link out of the app, or
324
+ // the recursive remove below would delete whatever the link points at.
325
+ for (const p of [dirname(PKG_SRC_DIR), PKG_SRC_DIR].map((d) => join(root, d))) {
326
+ if (!lstatSyncOrNull(p)) continue
327
+ let real = '' // a dangling link resolves to nothing: refused as well
328
+ try {
329
+ real = realpathSync(p)
330
+ } catch {}
331
+ if (!`${real}${sep}`.startsWith(root + sep)) throw new BuildError('package-source', `${relative(root, p)} resolves outside the app root (a link?); remove it and build again`)
332
+ }
333
+ rmSync(join(root, PKG_SRC_DIR), { recursive: true, force: true })
334
+ mkdirSync(join(root, PKG_SRC_DIR), { recursive: true })
335
+ if (!realpathSync(join(root, PKG_SRC_DIR)).startsWith(root + sep)) throw new BuildError('package-source', `${PKG_SRC_DIR} resolves outside the app root (a link?); remove it and build again`)
336
+ const out: PackageSources = { scopes: {}, refused: {}, dirs: {} }
337
+ writtenDirs.clear()
338
+ const byDir = new Map<string, Outcome>()
339
+ const budget = { bytes: 0 }
340
+ // (scope, the real directory bare imports resolve from, specifier)
341
+ const queue: [string, string, string][] = [...new Set(specs)].sort().map((s) => ['', root, s])
342
+ const seen = new Set<string>()
343
+ while (queue.length > 0) {
344
+ const [scope, from, spec] = queue.shift()!
345
+ const key = `${scope}\0${spec}`
346
+ if (seen.has(key) || NEVER(spec)) continue
347
+ seen.add(key)
348
+ const refuse = (reason: string) => {
349
+ ;(out.refused[scope] ??= {})[spec] = reason
350
+ }
351
+ const [name, sub] = splitSpecifier(spec)
352
+ if (sub) {
353
+ refuse('a package subpath import is not followed')
354
+ continue
355
+ }
356
+ const dir = findPackage(name, from)
357
+ if (!dir) {
358
+ refuse('not installed')
359
+ continue
360
+ }
361
+ let o = byDir.get(dir)
362
+ if (!o) {
363
+ try {
364
+ o = byDir.size >= LIMITS.packages ? { ok: false, reason: `more than ${LIMITS.packages} packages` } : writePackage(root, dir, budget)
365
+ } catch (e) {
366
+ // An unreadable package.json or map is a refusal: a package never fails the build here.
367
+ o = { ok: false, reason: `unreadable: ${String(e instanceof Error ? e.message : e).split('\n')[0]}` }
368
+ }
369
+ byDir.set(dir, o)
370
+ // A refusal is reported where it matters: the compiler names it on the island it leaves (`external-component`).
371
+ if (o.ok) {
372
+ out.dirs[o.dir] = dir
373
+ writtenDirs.set(join(root, o.dir), dir)
374
+ for (const im of o.imports) queue.push([o.dir, dir, im])
375
+ }
376
+ }
377
+ if (o.ok) (out.scopes[scope] ??= {})[spec] = o.entry
378
+ else refuse(o.reason)
379
+ }
380
+ mkdirSync(join(root, PKG_SRC_DIR), { recursive: true })
381
+ writeFileSync(join(root, PKG_SRC_DIR, 'index.json'), `${JSON.stringify(out, null, 2)}\n`)
382
+ return out
383
+ }
package/src/build/scan.ts CHANGED
@@ -1,15 +1,19 @@
1
- // Route scan (plan T5): import the route entry, then map every route Component to the `.tsx`
2
- // file it is the default export of. The compiler only ever sees files, never functions.
3
- import { readFileSync } from 'node:fs'
4
- import { dirname } from 'node:path'
1
+ // Route scan (plan T5, gaps L7): import the route entry, then walk its static import graph (inside the app root,
2
+ // outside node_modules) and map every Component to the `.tsx` file it is the default export of. The compiler only ever sees files, never functions.
3
+ import { readFileSync, realpathSync } from 'node:fs'
4
+ import { dirname, resolve, sep } from 'node:path'
5
5
  import { BrustRouteError, type Route } from '../routes'
6
6
  import { BuildError } from './errors'
7
+ import { installServerOnlyRuntime } from './server-only'
7
8
 
8
- const DEFAULT_IMPORT = /^\s*import\s+([A-Za-z_$][\w$]*)\s*(?:,\s*\{[^}]*\})?\s+from\s+['"]([^'"]+)['"]/gm
9
-
10
- export async function scanRoutes(entryFile: string): Promise<{ routes: Route[]; componentFile: Map<Function, string> }> {
9
+ /** `componentIssue` explains a Component the walk saw but could not map (named-only, outside the root, in node_modules). */
10
+ export async function scanRoutes(
11
+ entryFile: string,
12
+ appRoot?: string,
13
+ ): Promise<{ routes: Route[]; componentFile: Map<Function, string>; componentIssue: Map<Function, string> }> {
11
14
  // biome-ignore lint/suspicious/noExplicitAny: an arbitrary user module
12
15
  let mod: any
16
+ installServerOnlyRuntime() // `server-only` resolves to an empty module: a loader may import server-only code
13
17
  try {
14
18
  mod = await import(entryFile)
15
19
  } catch (e) {
@@ -21,22 +25,59 @@ export async function scanRoutes(entryFile: string): Promise<{ routes: Route[];
21
25
  if (!Array.isArray(routes))
22
26
  throw new BuildError('route-entry', `${entryFile} exports neither \`routes\` nor a default route array`)
23
27
 
24
- const src = readFileSync(entryFile, 'utf8')
25
- const real = new Set(new Bun.Transpiler({ loader: 'tsx' }).scanImports(src).map((i) => i.path))
26
28
  const componentFile = new Map<Function, string>()
27
- for (const m of src.matchAll(DEFAULT_IMPORT)) {
28
- const spec = m[2]!
29
- if (!real.has(spec)) continue // inside a comment or string: not an import
30
- let file: string
29
+ const componentIssue = new Map<Function, string>()
30
+ const root = realpathSync(resolve(appRoot ?? dirname(entryFile)))
31
+ const transpiler = new Bun.Transpiler({ loader: 'tsx' })
32
+ const inRoot = (f: string) => f.startsWith(root + sep) && !f.split(sep).includes('node_modules')
33
+ const visited = new Set<string>()
34
+ // Post-order: a file's imports are mapped before the file itself, and the first file to claim a function
35
+ // keeps it, so the module that defines a Component beats a barrel that re-exports it.
36
+ const visit = async (file: string): Promise<void> => {
37
+ if (visited.has(file)) return
38
+ visited.add(file)
39
+ let src: string
31
40
  try {
32
- file = Bun.resolveSync(spec, dirname(entryFile))
41
+ src = readFileSync(file, 'utf8')
33
42
  } catch {
34
- continue // a bare specifier the route file never uses as a Component; checked below
43
+ return
44
+ }
45
+ const loader = file.endsWith('.tsx') ? transpiler : file.endsWith('.ts') ? new Bun.Transpiler({ loader: 'ts' }) : null
46
+ if (!loader) return
47
+ const deps: string[] = []
48
+ // Only static imports are followed: a dynamic import() is not part of the route table's static graph.
49
+ for (const imp of loader.scanImports(src)) {
50
+ if (imp.kind !== 'import-statement') continue
51
+ let dep: string
52
+ try {
53
+ dep = Bun.resolveSync(imp.path, dirname(file))
54
+ } catch {
55
+ continue // a bare specifier that does not resolve; a route using it fails in compileApp
56
+ }
57
+ if (!dep.endsWith('.tsx') && !dep.endsWith('.ts')) continue
58
+ if (inRoot(dep)) deps.push(dep)
59
+ else if (dep.endsWith('.tsx')) await note(dep, dep.split(sep).includes('node_modules') ? 'in node_modules' : `outside the app root ${root}`)
60
+ }
61
+ for (const dep of deps) await visit(dep)
62
+ if (!file.endsWith('.tsx')) return
63
+ const m = await import(file)
64
+ for (const [name, v] of Object.entries(m)) {
65
+ if (typeof v !== 'function') continue
66
+ if (name === 'default') {
67
+ if (!componentFile.has(v)) componentFile.set(v, file)
68
+ } else if (!componentIssue.has(v)) {
69
+ componentIssue.set(v, `${v.name || name} is only a named export of ${file} (export ${name}): export it as default`)
70
+ }
35
71
  }
36
- if (!file.endsWith('.tsx')) continue // not a component source; a route using it fails in compileApp
37
- const def = (await import(file)).default
38
- // Bun's module cache: the same function object the routes tree holds.
39
- if (typeof def === 'function') componentFile.set(def, file)
40
72
  }
41
- return { routes, componentFile }
73
+ const note = async (file: string, why: string) => {
74
+ try {
75
+ const def = (await import(file)).default
76
+ if (typeof def === 'function' && !componentIssue.has(def)) componentIssue.set(def, `${def.name || '(anonymous)'} lives in ${file}, which is ${why}`)
77
+ } catch {}
78
+ }
79
+ await visit(entryFile)
80
+ // A function reachable as a default somewhere is fine; only an unmapped one keeps its explanation.
81
+ for (const f of componentFile.keys()) componentIssue.delete(f)
82
+ return { routes, componentFile, componentIssue }
42
83
  }
@@ -0,0 +1,39 @@
1
+ // The `server-only` marker package (Next.js convention): a module that starts with `import 'server-only'` is server code.
2
+ // The published package throws when it is resolved without the `react-server` condition, which both lets server-only code
3
+ // into a browser bundle whenever the marker is stubbed (a bundler alias, an empty `node_modules/server-only`) and breaks a
4
+ // correct app whose route scan imports it under the default condition. brust does not rely on the throw:
5
+ // - a BROWSER build that reaches the specifier is refused (`server-only-in-client`, bundle.ts `isServerOnly`);
6
+ // - every SERVER side (route scan, worker import of the app, jobs / actions / cache-adapter / build-eval / Worker
7
+ // bundles) resolves it to an empty module, the same thing the `react-server` condition resolves to.
8
+ import type { BunPlugin } from 'bun'
9
+
10
+ const MARKER = /^server-only(\/.*)?$/
11
+ const NS = 'brust-server-only-empty'
12
+
13
+ /** `server-only` itself or one of its subpaths. */
14
+ export const isServerOnlyMarker = (spec: string): boolean => MARKER.test(spec)
15
+
16
+ /** A build plugin for a SERVER bundle: the marker becomes an empty module. */
17
+ export function serverOnlyEmptyPlugin(): BunPlugin {
18
+ return {
19
+ name: 'brust-server-only-empty',
20
+ setup(b) {
21
+ b.onResolve({ filter: MARKER }, (a) => ({ path: a.path, namespace: NS }))
22
+ b.onLoad({ filter: /.*/, namespace: NS }, () => ({ contents: 'export {}', loader: 'js' }))
23
+ },
24
+ }
25
+ }
26
+
27
+ let installed = false
28
+ /** For code the process imports directly (the build's route scan, the worker's import of the app entry): a virtual
29
+ * module, so a bare `import 'server-only'` never reaches the package. Idempotent; only the marker is touched. */
30
+ export function installServerOnlyRuntime(): void {
31
+ if (installed) return
32
+ installed = true
33
+ Bun.plugin({
34
+ name: 'brust-server-only-runtime',
35
+ setup(b) {
36
+ b.module('server-only', () => ({ exports: {}, loader: 'object' }))
37
+ },
38
+ })
39
+ }
@@ -0,0 +1,65 @@
1
+ // The child process `brust build --target static` renders in (gaps round L9): boots the real server
2
+ // on loopback with the normal workers (`run()`, so every byte is what `brust start` sends), fetches
3
+ // each planned path, writes the responses to `BRUST_SSG_OUT`, then drains. A child process, because
4
+ // the napi server is a per-process singleton. Input: `BRUST_SSG_PLAN` (a JSON list of paths).
5
+ import { appendFileSync, readFileSync, writeFileSync } from 'node:fs'
6
+ import { run } from '../run'
7
+
8
+ export interface Rendered {
9
+ path: string
10
+ status: number
11
+ headers: Record<string, string>
12
+ /** base64 */
13
+ body: string
14
+ }
15
+
16
+ /** A page that did not answer within the per-page limit (review N3). */
17
+ export class PageTimeout extends Error {
18
+ constructor(
19
+ readonly path: string,
20
+ readonly ms: number,
21
+ ) {
22
+ super(`GET ${path} did not answer within ${ms} ms`)
23
+ }
24
+ }
25
+
26
+ /** The seam an in-process host can replace later: GET each path from `base`, uncompressed, no redirect
27
+ * following, each within `pageMs` (`PageTimeout`). `started` hears each path before it is requested. */
28
+ export async function renderPaths(base: string, paths: string[], pageMs = 30_000, started: (path: string) => void = () => {}): Promise<Rendered[]> {
29
+ const out: Rendered[] = []
30
+ for (const path of paths) {
31
+ started(path)
32
+ const signal = AbortSignal.timeout(pageMs)
33
+ try {
34
+ const r = await fetch(base + path, { redirect: 'manual', headers: { 'accept-encoding': 'identity' }, signal })
35
+ out.push({ path, status: r.status, headers: Object.fromEntries(r.headers), body: Buffer.from(await r.arrayBuffer()).toString('base64') })
36
+ } catch (e) {
37
+ if (signal.aborted) throw new PageTimeout(path, pageMs)
38
+ throw e
39
+ }
40
+ }
41
+ return out
42
+ }
43
+
44
+ if (import.meta.main) {
45
+ const env = process.env
46
+ const paths = JSON.parse(readFileSync(env.BRUST_SSG_PLAN!, 'utf8')) as string[]
47
+ await run({
48
+ distDir: env.BRUST_DIST_DIR,
49
+ entry: env.BRUST_APP_ENTRY,
50
+ config: { host: '127.0.0.1', port: 0 },
51
+ onReady: async (addr) => {
52
+ const pageMs = Number(env.BRUST_SSG_PAGE_TIMEOUT_MS) || 30_000
53
+ const progress = env.BRUST_SSG_PROGRESS
54
+ try {
55
+ const pages = await renderPaths(`http://${addr}`, paths, pageMs, (p) => progress && appendFileSync(progress, `${p}\n`))
56
+ writeFileSync(env.BRUST_SSG_OUT!, JSON.stringify({ addr, pages }))
57
+ return 0
58
+ } catch (e) {
59
+ if (!(e instanceof PageTimeout)) throw e
60
+ writeFileSync(env.BRUST_SSG_OUT!, JSON.stringify({ addr, timeout: { path: e.path, ms: e.ms } }))
61
+ return 1
62
+ }
63
+ },
64
+ })
65
+ }