@ultimat3/cli 30.8.0 → 31.0.0
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/CLAUDE.md +75 -64
- package/README.md +4 -1
- package/package.json +30 -29
- package/src/admin-registration.ts +1 -1
- package/src/affected.ts +5 -1
- package/src/app-boundaries.ts +88 -9
- package/src/app-load-async.ts +133 -0
- package/src/app-load-batch.ts +203 -0
- package/src/app-load-prebuilt.ts +147 -0
- package/src/app-load-register.ts +175 -0
- package/src/app-load.ts +143 -175
- package/src/app-loader-imports.ts +31 -0
- package/src/app-root.ts +13 -0
- package/src/app-runtime.ts +5 -1
- package/src/boundary-findings.ts +12 -0
- package/src/browser-launcher-port.ts +3 -7
- package/src/browser-launcher.ts +1 -1
- package/src/budgets-scan.ts +8 -0
- package/src/budgets.ts +10 -1
- package/src/build-registry-background.ts +59 -0
- package/src/build-registry.ts +207 -0
- package/src/cdp-shot-clock.ts +3 -11
- package/src/cdp-shot-driver.ts +3 -2
- package/src/cdp-shot-element.ts +3 -2
- package/src/cdp-shot-page.ts +2 -2
- package/src/cdp-shot-watch.ts +4 -3
- package/src/cmd-auth.ts +3 -3
- package/src/cmd-db-recount.ts +15 -0
- package/src/cmd-drill.ts +10 -6
- package/src/cmd-exercise.ts +2 -1
- package/src/cmd-generate-spec.ts +6 -0
- package/src/cmd-generate.ts +22 -4
- package/src/cmd-mail.ts +1 -1
- package/src/cmd-routes.ts +2 -2
- package/src/cmd-secrets-spec.ts +4 -1
- package/src/cmd-shot.ts +14 -4
- package/src/cmd-verify-spec.ts +21 -1
- package/src/cmd-verify.ts +16 -4
- package/src/coverage-source.ts +4 -1
- package/src/db-diagnostics.ts +8 -1
- package/src/dev-boot.ts +29 -15
- package/src/dev-dashboard-invalidations.ts +43 -0
- package/src/dev-dashboard.ts +13 -16
- package/src/dev-n-plus-one.ts +7 -3
- package/src/dev-rebuild.ts +2 -1
- package/src/dev-route-table.ts +1 -1
- package/src/dev-traces.ts +16 -15
- package/src/document-options.ts +12 -1
- package/src/drift-store.ts +55 -0
- package/src/error-catalog.ts +8 -3
- package/src/error-codes-boundary.ts +32 -0
- package/src/error-codes.ts +23 -19
- package/src/errors.ts +17 -7
- package/src/exercise-database.ts +13 -1
- package/src/exercise-findings.ts +36 -18
- package/src/exercise-services.ts +84 -0
- package/src/exercise-subject.ts +38 -13
- package/src/failure-capture-boot.ts +9 -5
- package/src/framework-schema.ts +5 -5
- package/src/generate-exercise.ts +1 -1
- package/src/generate-files.ts +27 -7
- package/src/generate-kinds.ts +5 -0
- package/src/generate-refresh.ts +53 -0
- package/src/generate-workspace.ts +169 -0
- package/src/generate-write.ts +8 -2
- package/src/hold.ts +19 -8
- package/src/image-prepare.ts +6 -2
- package/src/index.ts +5 -20
- package/src/island-bundle.ts +7 -3
- package/src/island-capture.ts +2 -2
- package/src/island-harness.ts +1 -1
- package/src/job-registration.ts +43 -6
- package/src/jobs-driver.ts +4 -5
- package/src/mcp-errors.ts +18 -4
- package/src/mcp-host.ts +5 -1
- package/src/mcp-ui-interact.ts +3 -0
- package/src/messages-gate.ts +4 -0
- package/src/new-shapes-generate.ts +1 -1
- package/src/otlp-export.ts +94 -29
- package/src/output.ts +3 -7
- package/src/page-sync.ts +3 -1
- package/src/role-load.ts +17 -11
- package/src/role-start.ts +5 -4
- package/src/role-sync.ts +7 -8
- package/src/route-islands-preload.ts +46 -0
- package/src/route-islands.ts +6 -2
- package/src/runtime-admin.ts +5 -1
- package/src/runtime-bindings.ts +25 -19
- package/src/runtime-boot-ports.ts +66 -0
- package/src/runtime-bus.ts +29 -5
- package/src/runtime-cache-bus.ts +53 -0
- package/src/runtime-cache.ts +43 -13
- package/src/runtime-isr-outcome.ts +5 -4
- package/src/runtime-jobs.ts +2 -2
- package/src/runtime-keys.ts +37 -0
- package/src/runtime-overrides.ts +148 -42
- package/src/runtime-port-bindings-queue.ts +36 -0
- package/src/runtime-port-bindings.ts +117 -0
- package/src/runtime-ports.ts +164 -0
- package/src/runtime-purge.ts +41 -28
- package/src/runtime-push.ts +7 -3
- package/src/runtime-queue.ts +70 -55
- package/src/runtime-realtime.ts +5 -5
- package/src/runtime-render-document.ts +4 -1
- package/src/runtime-services.ts +110 -158
- package/src/runtime-storage-boot.ts +111 -0
- package/src/runtime-storage-upload.ts +9 -7
- package/src/runtime-storage.ts +7 -1
- package/src/serve-boot.ts +8 -4
- package/src/serve-entry.ts +3 -0
- package/src/serve-env.ts +47 -14
- package/src/serve-prebuilt-paths.ts +3 -0
- package/src/serve-prebuilt.ts +15 -4
- package/src/serve.ts +2 -2
- package/src/static-cards.ts +3 -2
- package/src/static-report.ts +2 -2
- package/src/style-bundle.ts +1 -1
- package/src/sync-authenticator.ts +17 -18
- package/src/templates/action-tests.ts +4 -4
- package/src/templates/backfill.ts +5 -8
- package/src/templates/entity.ts +3 -0
- package/src/templates/github/ci.yml.ts +3 -1
- package/src/templates/github/dependabot.yml.ts +73 -0
- package/src/templates/job.ts +13 -22
- package/src/templates/naming.ts +20 -1
- package/src/templates/pinned/docker-compose.yml +1 -1
- package/src/templates/query.ts +7 -8
- package/src/templates/route.ts +5 -3
- package/src/templates/scaffold-api.ts +1 -1
- package/src/templates/scaffold-auth.ts +5 -3
- package/src/templates/scaffold-claude-commands.ts +6 -3
- package/src/templates/scaffold-claude.ts +4 -4
- package/src/templates/scaffold-container-compose.ts +2 -4
- package/src/templates/scaffold-domain-package.ts +1 -1
- package/src/templates/scaffold-entries.ts +6 -1
- package/src/templates/scaffold-helm-fences.ts +3 -2
- package/src/templates/scaffold-helm.ts +35 -6
- package/src/templates/scaffold-repo.ts +47 -135
- package/src/templates/scaffold-runtime-ports.ts +62 -0
- package/src/templates/scaffold-verify-floor.ts +97 -0
- package/src/templates/shapes/overlay.ts +1 -1
- package/src/templates/sign-in-oidc.ts +5 -3
- package/src/templates/sign-in-shared.ts +28 -10
- package/src/templates/workspace.ts +75 -0
- package/src/test-ignores.ts +5 -2
- package/src/test-select.ts +4 -1
- package/src/upgrade-gate.ts +30 -5
- package/src/upgrade-report.ts +7 -1
- package/src/verify-changed-facts.ts +53 -0
- package/src/verify-changed-scope.ts +161 -0
- package/src/verify-changed.ts +174 -0
- package/src/verify-checks.ts +31 -19
- package/src/verify-drift-owner.ts +30 -0
- package/src/verify-e2e.ts +2 -1
- package/src/verify-factories.ts +86 -0
- package/src/verify-run.ts +13 -11
- package/src/verify-step.ts +13 -0
- package/src/verify-tests.ts +58 -6
- package/src/write-line.ts +21 -6
- package/wiki/Actions.md +2 -2
- package/wiki/Adapters.md +400 -0
- package/wiki/Admin-Dashboard.md +2 -1
- package/wiki/Agents.md +9 -9
- package/wiki/Auth.md +78 -2
- package/wiki/Building-Your-Own-Base.md +1 -1
- package/wiki/CLI-Reference.md +34 -7
- package/wiki/Caching-And-Invalidation.md +17 -5
- package/wiki/Compared.md +8 -2
- package/wiki/Configuration.md +14 -14
- package/wiki/Deployment.md +28 -1
- package/wiki/Entities-And-Migrations.md +48 -2
- package/wiki/FAQ.md +8 -2
- package/wiki/Feature-Flags.md +1 -1
- package/wiki/Home.md +3 -2
- package/wiki/Jobs-And-Workflows.md +107 -6
- package/wiki/Known-Gaps.md +4 -3
- package/wiki/MCP-And-AI.md +18 -4
- package/wiki/Mail.md +14 -5
- package/wiki/Migrations-And-Backfills.md +31 -4
- package/wiki/Money.md +2 -0
- package/wiki/N-Plus-One-Detection.md +24 -22
- package/wiki/Notify.md +28 -4
- package/wiki/Observability.md +121 -15
- package/wiki/Queries-And-Live-Queries.md +2 -2
- package/wiki/Realtime.md +9 -4
- package/wiki/Routes-And-Render-Modes.md +5 -0
- package/wiki/Scraping.md +57 -11
- package/wiki/Shapes.md +2 -2
- package/wiki/Stability.md +7 -5
- package/wiki/Storage-And-Uploads.md +56 -4
- package/wiki/Testing.md +69 -56
- package/wiki/Troubleshooting.md +1 -1
- package/wiki/Tutorial-01-First-App.md +1 -1
- package/wiki/Tutorial-04-Jobs-And-Realtime.md +1 -1
- package/wiki/Tutorial-05-Deploy-Free.md +1 -1
- package/wiki/Tutorial-06-Growing-Up.md +8 -8
- package/wiki/Upgrading.md +129 -4
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Which app modules evaluate ASYNCHRONOUSLY — a top-level `await` in the module or in anything of
|
|
2
|
+
// the app's it imports. In one entry module such a module suspends and its later siblings run
|
|
3
|
+
// meanwhile, so the batched import (`app-load-batch.ts`) imports each on its own: the serial
|
|
4
|
+
// scan's evaluation order is the guarantee, and these are the modules that would break it.
|
|
5
|
+
|
|
6
|
+
// why: Bun ships no path API; the walk resolves imports to absolute paths and names them back.
|
|
7
|
+
import { basename, resolve } from 'node:path';
|
|
8
|
+
import { resolvedImports } from './module-imports';
|
|
9
|
+
import { hasPathSegment, isPathUnder } from './path-segments';
|
|
10
|
+
import { isTest } from './source-files';
|
|
11
|
+
|
|
12
|
+
/** Every source the app could import: the scan's globs are not the whole of what modules import. */
|
|
13
|
+
const APP_SOURCE = '**/*.{ts,tsx,mts,js,jsx,mjs}';
|
|
14
|
+
|
|
15
|
+
const transpilers = {
|
|
16
|
+
ts: new Bun.Transpiler({ loader: 'ts' }),
|
|
17
|
+
tsx: new Bun.Transpiler({ loader: 'tsx' }),
|
|
18
|
+
js: new Bun.Transpiler({ loader: 'js' }),
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The module statements, removed or turned into the declarations they export, so what remains
|
|
23
|
+
* is a statement list a class static block can hold. Read off Bun's own printed output, whose
|
|
24
|
+
* shape is fixed: one statement per `import`/`export`, double-quoted specifiers.
|
|
25
|
+
*/
|
|
26
|
+
function withoutModuleSyntax(printed: string): string {
|
|
27
|
+
return printed
|
|
28
|
+
.replace(/^import\s[\s\S]*?\sfrom\s*"[^"]*";/gm, '')
|
|
29
|
+
.replace(/^import\s*"[^"]*";/gm, '')
|
|
30
|
+
.replace(/^export\s*\*[^;]*;/gm, '')
|
|
31
|
+
.replace(/^export\s*\{[\s\S]*?\}(?:\s*from\s*"[^"]*")?;/gm, '')
|
|
32
|
+
.replace(/^export\s+default\s+/gm, 'void ')
|
|
33
|
+
.replace(/^export\s+/gm, '')
|
|
34
|
+
.replace(/\bimport\.meta\b/g, '({})');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Whether `source` awaits at its top level. Parsed, never pattern-matched: the statements go into a
|
|
39
|
+
* class static block, where `await` is reserved — `await x`, `await (x)` and `for await` refuse
|
|
40
|
+
* there exactly as they run at a module's top level, while every `await` inside an async function
|
|
41
|
+
* still parses. A source the parser refuses for any other reason is answered `true`: importing it
|
|
42
|
+
* alone costs a little speed, importing it in a batch could cost the order.
|
|
43
|
+
*/
|
|
44
|
+
export function hasTopLevelAwait(path: string, source: string): boolean {
|
|
45
|
+
if (!source.includes('await')) return false;
|
|
46
|
+
const loader = /x$/.test(path) ? transpilers.tsx : transpilers.ts;
|
|
47
|
+
try {
|
|
48
|
+
const statements = withoutModuleSyntax(loader.transformSync(source));
|
|
49
|
+
transpilers.js.transformSync(`class __XTopLevel { static {\n${statements}\n} }`);
|
|
50
|
+
return false;
|
|
51
|
+
} catch {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const inApp = (root: string, path: string): boolean =>
|
|
57
|
+
isPathUnder(root, path) && !hasPathSegment(path, 'node_modules');
|
|
58
|
+
|
|
59
|
+
/** Every app source, read, and the ones that await at their top level. */
|
|
60
|
+
async function readSources(root: string): Promise<{
|
|
61
|
+
readonly sources: ReadonlyMap<string, string>;
|
|
62
|
+
readonly awaiting: readonly string[];
|
|
63
|
+
}> {
|
|
64
|
+
const sources = new Map<string, string>();
|
|
65
|
+
const awaiting: string[] = [];
|
|
66
|
+
const paths: string[] = [];
|
|
67
|
+
for await (const path of new Bun.Glob(APP_SOURCE).scan({ cwd: root, absolute: true })) {
|
|
68
|
+
// A test is imported by no module: `bun test` loads it, never the scan.
|
|
69
|
+
if (!hasPathSegment(path, 'node_modules') && !isTest(path)) paths.push(path);
|
|
70
|
+
}
|
|
71
|
+
// Bounded: an app of 12k files opened 12k descriptors at once otherwise.
|
|
72
|
+
for (let start = 0; start < paths.length; start += 64) {
|
|
73
|
+
const batch = paths.slice(start, start + 64);
|
|
74
|
+
const texts = await Promise.all(batch.map((path) => Bun.file(path).text()));
|
|
75
|
+
for (const [index, path] of batch.entries()) {
|
|
76
|
+
const text = texts[index] as string;
|
|
77
|
+
sources.set(path, text);
|
|
78
|
+
if (hasTopLevelAwait(path, text)) awaiting.push(path);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { sources, awaiting };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Whether any source could import `path`: a specifier ending in its file name, with or without the
|
|
86
|
+
* extension. A filter, never the answer — it only spares the import walk for the awaiting modules
|
|
87
|
+
* nothing names, which are most of them: `server.ts`, `prerender.ts`, `bin/` scripts.
|
|
88
|
+
*/
|
|
89
|
+
function named(path: string, sources: ReadonlyMap<string, string>): boolean {
|
|
90
|
+
const file = basename(path);
|
|
91
|
+
const stem = file.replace(/\.[cm]?[jt]sx?$/, '');
|
|
92
|
+
const escaped = (text: string): string => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
93
|
+
const specifier = new RegExp(`[/'"](?:${escaped(stem)}|${escaped(file)})['"]`);
|
|
94
|
+
for (const [other, text] of sources) {
|
|
95
|
+
if (other !== path && specifier.test(text)) return true;
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The modules of `modules` (absolute) whose evaluation can suspend: one that awaits at its top
|
|
102
|
+
* level, or reaches one through the app's own imports. The framework and `node_modules` are not
|
|
103
|
+
* walked — the CLI has evaluated the framework before any scan, so it is no longer async. The
|
|
104
|
+
* common answer, no importable top-level await in the app, costs one read of each source.
|
|
105
|
+
*/
|
|
106
|
+
export async function asyncModules(
|
|
107
|
+
root: string,
|
|
108
|
+
modules: readonly string[],
|
|
109
|
+
): Promise<ReadonlySet<string>> {
|
|
110
|
+
const base = resolve(root);
|
|
111
|
+
const { sources, awaiting } = await readSources(base);
|
|
112
|
+
// The common answer: nothing the app could import awaits at its top level, and no walk runs.
|
|
113
|
+
if (!awaiting.some((path) => modules.includes(path) || named(path, sources))) return new Set();
|
|
114
|
+
const answered = new Map<string, boolean>();
|
|
115
|
+
const reaches = (path: string, visiting: Set<string>): boolean => {
|
|
116
|
+
const known = answered.get(path);
|
|
117
|
+
if (known !== undefined) return known;
|
|
118
|
+
// A cycle is answered by the rest of the walk: the module on the stack is still deciding.
|
|
119
|
+
if (visiting.has(path)) return false;
|
|
120
|
+
visiting.add(path);
|
|
121
|
+
const source = sources.get(path);
|
|
122
|
+
const awaits =
|
|
123
|
+
source !== undefined &&
|
|
124
|
+
(hasTopLevelAwait(path, source) ||
|
|
125
|
+
resolvedImports(path, source, 'evaluated')
|
|
126
|
+
.filter((target) => inApp(base, target))
|
|
127
|
+
.some((target) => reaches(target, visiting)));
|
|
128
|
+
visiting.delete(path);
|
|
129
|
+
answered.set(path, awaits);
|
|
130
|
+
return awaits;
|
|
131
|
+
};
|
|
132
|
+
return new Set(modules.filter((path) => reaches(path, new Set())));
|
|
133
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// Importing many app modules at once, with the serial loop's guarantee: each module is evaluated,
|
|
2
|
+
// then handed to `onEvaluated`, before the next one starts — so a module reads every earlier one
|
|
3
|
+
// REGISTERED. A batch is an entry that imports one hook module per app module (`batchSources`);
|
|
4
|
+
// a module that can suspend (top-level await) is a batch of its own (`app-load-async.ts`).
|
|
5
|
+
|
|
6
|
+
// why: Bun ships no temp directory, no path API and no recursive delete of its own.
|
|
7
|
+
import { mkdir, rm } from 'node:fs/promises';
|
|
8
|
+
// why: as above — the batch lives in the platform temp root, which only node:os names.
|
|
9
|
+
import { tmpdir } from 'node:os';
|
|
10
|
+
// why: Bun exposes no path-join primitive.
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Modules per entry. A bound, not a tuning knob: the loader holds a batch's whole graph — every
|
|
15
|
+
* source, every transpiled module — before it evaluates the first, so an unbounded entry over a
|
|
16
|
+
* 12k-module app held all of it at once. Measured on a 5,000-module fixture (2026-11-09): 64 →
|
|
17
|
+
* 1.04 s, 256 → 0.87 s, 1,000 → 0.84 s, serial → 1.36 s. The hooks cost their own module each:
|
|
18
|
+
* re-measured 2026-11-10 (median of 5 cold boots), serial 2.19 s, hooked batches 1.39 s, and the
|
|
19
|
+
* order-breaking hookless entry 0.90 s.
|
|
20
|
+
*/
|
|
21
|
+
export const BATCH_SIZE = 256;
|
|
22
|
+
|
|
23
|
+
/** One module's import: the namespace it evaluated to, or what it threw. */
|
|
24
|
+
export type Imported =
|
|
25
|
+
| { readonly ok: true; readonly module: Record<string, unknown> }
|
|
26
|
+
| { readonly ok: false; readonly error: unknown };
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Called once per module that evaluated, by its index in scan order, before the next module in
|
|
30
|
+
* that order is evaluated. The scan registers here: what a later module reads at its top level —
|
|
31
|
+
* `defineAppMcp({ include: 'exposed' })` reads the action registry — is what the serial scan left.
|
|
32
|
+
*/
|
|
33
|
+
export type OnEvaluated = (index: number, module: Record<string, unknown>) => void;
|
|
34
|
+
|
|
35
|
+
/** The global a hook module reports through: one well-known symbol, owned by `receiving`. */
|
|
36
|
+
const HOOK = 'ultimate.scan.evaluated';
|
|
37
|
+
|
|
38
|
+
/** The file one hook module is written to, beside its entry: named by the module's scan index. */
|
|
39
|
+
export const hookFile = (index: number): string => `m${String(index)}.ts`;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* One batch's sources: a hook per module — it imports the module and reports it — and an entry
|
|
43
|
+
* that imports the hooks in order. ES evaluation walks the entry's imports depth-first, so hook N
|
|
44
|
+
* runs after module N and before module N+1: a static graph the loader fetches concurrently, with
|
|
45
|
+
* the serial loop's interleaving of evaluate and register. Never the modules themselves in one
|
|
46
|
+
* entry: static imports evaluate every one before the entry's first statement runs.
|
|
47
|
+
*/
|
|
48
|
+
export function batchSources(
|
|
49
|
+
specifiers: readonly string[],
|
|
50
|
+
first: number,
|
|
51
|
+
): { readonly entry: string; readonly hooks: readonly (readonly [string, string])[] } {
|
|
52
|
+
const hooks = specifiers.map((specifier, offset): readonly [string, string] => [
|
|
53
|
+
hookFile(first + offset),
|
|
54
|
+
`import * as m from ${JSON.stringify(specifier)};\n` +
|
|
55
|
+
`globalThis[Symbol.for(${JSON.stringify(HOOK)})]?.(${String(first + offset)}, m);\n`,
|
|
56
|
+
]);
|
|
57
|
+
const entry = hooks.map(([name]) => `import ${JSON.stringify(`./${name}`)};`).join('\n');
|
|
58
|
+
return { entry: `${entry}\n`, hooks };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Runs `load` with `receive` installed as the hooks' target, or answers `undefined` without
|
|
63
|
+
* running it when another load holds the slot: two at once would hand one's modules to the other.
|
|
64
|
+
* A receiver that throws is contained — a throw would fail the hook, and the batch with it.
|
|
65
|
+
*/
|
|
66
|
+
export async function receiving<T>(
|
|
67
|
+
receive: OnEvaluated,
|
|
68
|
+
load: () => Promise<T>,
|
|
69
|
+
): Promise<T | undefined> {
|
|
70
|
+
const slot = Symbol.for(HOOK);
|
|
71
|
+
if (Reflect.get(globalThis, slot) !== undefined) return undefined;
|
|
72
|
+
const contained: OnEvaluated = (index, module) => {
|
|
73
|
+
try {
|
|
74
|
+
receive(index, module);
|
|
75
|
+
} catch {
|
|
76
|
+
// `registerModule` answers a finding, never a throw; anything else is not the batch's.
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
Reflect.set(globalThis, slot, contained);
|
|
80
|
+
try {
|
|
81
|
+
return await load();
|
|
82
|
+
} finally {
|
|
83
|
+
Reflect.deleteProperty(globalThis, slot);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* A directory no other batch has used — in this process or any copy of this module in it. Bun's
|
|
89
|
+
* module cache is keyed by path and outlives a module instance: `bun test --parallel` evaluates
|
|
90
|
+
* this file afresh per test file in one worker, and a per-instance counter named the same
|
|
91
|
+
* directory twice, so the second load imported the first one's cached hooks and was handed the
|
|
92
|
+
* first app's modules (2026-11-10, `app-load-batch.test.ts`).
|
|
93
|
+
*/
|
|
94
|
+
export const uniqueBatchName = (): string => `x-scan-${crypto.randomUUID()}`;
|
|
95
|
+
|
|
96
|
+
/** The serial import, for one module: what the batch falls back to. */
|
|
97
|
+
async function importOne(absolute: string): Promise<Imported> {
|
|
98
|
+
try {
|
|
99
|
+
return { ok: true, module: (await import(absolute)) as Record<string, unknown> };
|
|
100
|
+
} catch (error) {
|
|
101
|
+
return { ok: false, error };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A batch's answers, filled by the hooks as they run and by the serial fallback after. */
|
|
106
|
+
type Slots = (Imported | undefined)[];
|
|
107
|
+
|
|
108
|
+
/** Imports `absolute` serially unless a hook already answered it, and reports a first evaluation. */
|
|
109
|
+
async function fillSerially(
|
|
110
|
+
batch: readonly string[],
|
|
111
|
+
first: number,
|
|
112
|
+
slots: Slots,
|
|
113
|
+
onEvaluated: OnEvaluated,
|
|
114
|
+
): Promise<void> {
|
|
115
|
+
for (const [offset, absolute] of batch.entries()) {
|
|
116
|
+
if (slots[offset] !== undefined) continue;
|
|
117
|
+
const one = await importOne(absolute);
|
|
118
|
+
slots[offset] = one;
|
|
119
|
+
if (one.ok) onEvaluated(first + offset, one.module);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* One batch. Any failure in it — a module that throws, one that cannot link — fails the entry:
|
|
125
|
+
* every module before the failing one has evaluated and been reported, and the rest are imported
|
|
126
|
+
* module by module — the failing one throws its own error again, every one after it evaluates in
|
|
127
|
+
* its serial turn. A temp root that cannot be written (a read-only container) is the serial loop
|
|
128
|
+
* too: slower, never wrong.
|
|
129
|
+
*/
|
|
130
|
+
async function importBatch(
|
|
131
|
+
batch: readonly string[],
|
|
132
|
+
first: number,
|
|
133
|
+
onEvaluated: OnEvaluated,
|
|
134
|
+
): Promise<readonly Imported[]> {
|
|
135
|
+
const slots: Slots = batch.map(() => undefined);
|
|
136
|
+
if (batch.length > 1) {
|
|
137
|
+
const dir = join(tmpdir(), uniqueBatchName());
|
|
138
|
+
const { entry, hooks } = batchSources(
|
|
139
|
+
batch.map((absolute) => Bun.pathToFileURL(absolute).href),
|
|
140
|
+
first,
|
|
141
|
+
);
|
|
142
|
+
const receive: OnEvaluated = (index, module) => {
|
|
143
|
+
slots[index - first] = { ok: true, module };
|
|
144
|
+
onEvaluated(index, module);
|
|
145
|
+
};
|
|
146
|
+
try {
|
|
147
|
+
await mkdir(dir, { recursive: true });
|
|
148
|
+
await Promise.all(hooks.map(([name, source]) => Bun.write(join(dir, name), source)));
|
|
149
|
+
await Bun.write(join(dir, 'entry.ts'), entry);
|
|
150
|
+
await receiving(receive, () => import(join(dir, 'entry.ts')));
|
|
151
|
+
} catch {
|
|
152
|
+
// Answered below, module by module, with each module's own error.
|
|
153
|
+
} finally {
|
|
154
|
+
await rm(dir, { recursive: true, force: true });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
await fillSerially(batch, first, slots, onEvaluated);
|
|
158
|
+
return slots as Imported[];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* `absolutes` cut into entry-sized batches, in order. A module in `alone` — one whose evaluation can
|
|
163
|
+
* suspend — ends the batch before it and is a batch by itself: an entry module waits for it before
|
|
164
|
+
* anything after it starts. The scan and the prebuilt registry both cut here, so both keep order.
|
|
165
|
+
*/
|
|
166
|
+
export function planBatches(
|
|
167
|
+
absolutes: readonly string[],
|
|
168
|
+
alone: ReadonlySet<string> = new Set(),
|
|
169
|
+
): readonly (readonly string[])[] {
|
|
170
|
+
const batches: string[][] = [];
|
|
171
|
+
let current: string[] = [];
|
|
172
|
+
const flush = (): void => {
|
|
173
|
+
if (current.length > 0) batches.push(current);
|
|
174
|
+
current = [];
|
|
175
|
+
};
|
|
176
|
+
for (const absolute of absolutes) {
|
|
177
|
+
if (alone.has(absolute)) {
|
|
178
|
+
flush();
|
|
179
|
+
batches.push([absolute]);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
current.push(absolute);
|
|
183
|
+
if (current.length === BATCH_SIZE) flush();
|
|
184
|
+
}
|
|
185
|
+
flush();
|
|
186
|
+
return batches;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Every module of `absolutes`, imported in `planBatches`' batches and answered in the same order;
|
|
191
|
+
* `onEvaluated` hears each module that evaluated, in that order, before the next one evaluates.
|
|
192
|
+
*/
|
|
193
|
+
export async function importInOrder(
|
|
194
|
+
absolutes: readonly string[],
|
|
195
|
+
alone: ReadonlySet<string> = new Set(),
|
|
196
|
+
onEvaluated: OnEvaluated = () => undefined,
|
|
197
|
+
): Promise<readonly Imported[]> {
|
|
198
|
+
const imported: Imported[] = [];
|
|
199
|
+
for (const batch of planBatches(absolutes, alone)) {
|
|
200
|
+
imported.push(...(await importBatch(batch, imported.length, onEvaluated)));
|
|
201
|
+
}
|
|
202
|
+
return imported;
|
|
203
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// A serving role's import of the app from the image's prebuilt registry (`build-registry.ts`):
|
|
2
|
+
// the chunks `x build --target prebuilt` wrote — every module's, or the background roles' subset —
|
|
3
|
+
// imported in order and registered, with no import scan and no document-graph walk. `undefined`
|
|
4
|
+
// whenever the registry cannot be trusted to be the app, so the caller scans instead.
|
|
5
|
+
|
|
6
|
+
// why: Bun ships no path-join primitive.
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import { logger, UltimateError } from '@ultimat3/core';
|
|
9
|
+
import type { AppScan } from './app-load';
|
|
10
|
+
import { prepareAppImport, scannedModulePaths } from './app-load';
|
|
11
|
+
import type { OnEvaluated } from './app-load-batch';
|
|
12
|
+
import { receiving } from './app-load-batch';
|
|
13
|
+
import { registerModule } from './app-load-register';
|
|
14
|
+
import type { RegistryChunks } from './build-registry';
|
|
15
|
+
import { moduleStamp, readPrebuiltRegistry, registryDir } from './build-registry';
|
|
16
|
+
import type { Finding } from './output';
|
|
17
|
+
import { posixRelative } from './posix-path';
|
|
18
|
+
import type { RoleLoad } from './role-load';
|
|
19
|
+
import { PREBUILT_COMMAND } from './serve-prebuilt-paths';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Logged, never thrown: a stale registry is answered by the scan, which imports what is on disk.
|
|
23
|
+
* In an image it cannot happen — the registry is written after the source is copied in — so it
|
|
24
|
+
* says the Dockerfile copies something after the prebuilt step.
|
|
25
|
+
*/
|
|
26
|
+
export class PrebuiltRegistryStaleError extends UltimateError {
|
|
27
|
+
constructor(input: {
|
|
28
|
+
readonly added: readonly string[];
|
|
29
|
+
readonly removed: readonly string[];
|
|
30
|
+
/** The same modules, and one at least was rewritten since the build (a background boot's check). */
|
|
31
|
+
readonly changed?: boolean;
|
|
32
|
+
}) {
|
|
33
|
+
const first = [...input.added, ...input.removed].slice(0, 3).join(', ');
|
|
34
|
+
const what = input.changed
|
|
35
|
+
? 'lists the modules on disk, and one or more of them changed content since it was written (which of them reach a component decides what a worker imports)'
|
|
36
|
+
: `lists ${String(input.removed.length)} module(s) not on disk and misses ${String(input.added.length)} that are (${first})`;
|
|
37
|
+
super({
|
|
38
|
+
code: 'X_PREBUILT_REGISTRY_STALE',
|
|
39
|
+
cause: `the prebuilt module registry ${what}, so this boot scanned the app instead of importing the registry`,
|
|
40
|
+
fix: `edit docker/Dockerfile: move RUN ${PREBUILT_COMMAND} after the last COPY in the runtime stage, then rebuild the image`,
|
|
41
|
+
meta: { added: input.added, removed: input.removed, changed: input.changed === true },
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The module list `files` against the one on disk, as what each lacks. */
|
|
47
|
+
function difference(
|
|
48
|
+
listed: readonly string[],
|
|
49
|
+
onDisk: readonly string[],
|
|
50
|
+
): { readonly added: readonly string[]; readonly removed: readonly string[] } {
|
|
51
|
+
const held = new Set(listed);
|
|
52
|
+
const present = new Set(onDisk);
|
|
53
|
+
return {
|
|
54
|
+
added: onDisk.filter((file) => !held.has(file)),
|
|
55
|
+
removed: listed.filter((file) => !present.has(file)),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Every chunk imported in order, each module reported to `receive` as it evaluates — or `false`
|
|
61
|
+
* when one would not import, or another load held the hooks' slot.
|
|
62
|
+
*/
|
|
63
|
+
async function importChunks(
|
|
64
|
+
dir: string,
|
|
65
|
+
chunks: readonly string[],
|
|
66
|
+
receive: OnEvaluated,
|
|
67
|
+
): Promise<boolean> {
|
|
68
|
+
const imported = await receiving(receive, async () => {
|
|
69
|
+
for (const chunk of chunks) {
|
|
70
|
+
try {
|
|
71
|
+
await import(join(dir, chunk));
|
|
72
|
+
} catch {
|
|
73
|
+
// The scan imports module by module and reports the one that fails, with its own cause.
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return true;
|
|
78
|
+
});
|
|
79
|
+
return imported === true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Logs the registry stale and answers `undefined`: the caller scans. */
|
|
83
|
+
function stale(input: ConstructorParameters<typeof PrebuiltRegistryStaleError>[0]): undefined {
|
|
84
|
+
const error = new PrebuiltRegistryStaleError(input);
|
|
85
|
+
logger.warn(error.format(), { code: error.code, ...error.meta });
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The app, imported from the prebuilt registry and registered in its order — every module for a
|
|
91
|
+
* role that imports `everything`, the background subset for `worker` and `scheduler` — or
|
|
92
|
+
* `undefined`: no registry, one this Bun did not write, one whose module list is not the disk's
|
|
93
|
+
* (logged, `X_PREBUILT_REGISTRY_STALE`), or a chunk that would not import. The list is checked
|
|
94
|
+
* against the scan's own glob, which reads directory entries and imports nothing: a module added
|
|
95
|
+
* after the build is a route or an action the registry would silently not serve. A background
|
|
96
|
+
* boot also checks every module's stamp: its subset is decided by what the modules import.
|
|
97
|
+
*/
|
|
98
|
+
export async function loadPrebuiltApp(
|
|
99
|
+
root: string,
|
|
100
|
+
load: RoleLoad = 'everything',
|
|
101
|
+
): Promise<AppScan | undefined> {
|
|
102
|
+
const registry = await readPrebuiltRegistry(root);
|
|
103
|
+
if (registry === undefined) return undefined;
|
|
104
|
+
const scanned = await scannedModulePaths(root);
|
|
105
|
+
const onDisk = scanned.map((absolute) => posixRelative(root, absolute));
|
|
106
|
+
if (onDisk.join('\n') !== registry.files.join('\n')) {
|
|
107
|
+
return stale(difference(registry.files, onDisk));
|
|
108
|
+
}
|
|
109
|
+
if (load === 'background') {
|
|
110
|
+
const stamp = await moduleStamp(root, registry.files);
|
|
111
|
+
if (stamp !== registry.background.stamp)
|
|
112
|
+
return stale({ added: [], removed: [], changed: true });
|
|
113
|
+
}
|
|
114
|
+
// The glob's own spelling of each path: registration is keyed by it, and a later full load (the
|
|
115
|
+
// background boot's fallback) must find these modules registered under the same key.
|
|
116
|
+
const absoluteOf = new Map(onDisk.map((file, index) => [file, scanned[index] as string]));
|
|
117
|
+
const set = load === 'background' ? registry.background : registry;
|
|
118
|
+
const absolutes = set.files.map((file) => absoluteOf.get(file));
|
|
119
|
+
if (absolutes.some((absolute) => absolute === undefined)) return undefined;
|
|
120
|
+
return importRegistered(root, set, absolutes as string[]);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** `set`'s chunks imported and each module registered as it evaluates, in the set's order. */
|
|
124
|
+
async function importRegistered(
|
|
125
|
+
root: string,
|
|
126
|
+
set: RegistryChunks,
|
|
127
|
+
absolutes: readonly string[],
|
|
128
|
+
): Promise<AppScan | undefined> {
|
|
129
|
+
prepareAppImport(root);
|
|
130
|
+
// Registered as each module evaluates, by its hook: a later module reads every earlier one
|
|
131
|
+
// registered, as the scan leaves them (`app-load-batch.ts`).
|
|
132
|
+
const findings: (Finding | undefined)[] = [];
|
|
133
|
+
let evaluated = 0;
|
|
134
|
+
const register: OnEvaluated = (index, module) => {
|
|
135
|
+
const absolute = absolutes[index];
|
|
136
|
+
if (absolute === undefined) return;
|
|
137
|
+
evaluated += 1;
|
|
138
|
+
const at = { absolute, file: set.files[index] as string, track: false };
|
|
139
|
+
findings[index] = registerModule(module, at);
|
|
140
|
+
};
|
|
141
|
+
const imported = await importChunks(registryDir(root), set.chunks, register);
|
|
142
|
+
if (!imported || evaluated !== absolutes.length) return undefined;
|
|
143
|
+
return {
|
|
144
|
+
files: [...set.files].sort(),
|
|
145
|
+
findings: findings.filter((finding): finding is Finding => finding !== undefined),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// Registering one imported app module, once per process: its route, its actions, its queries, and
|
|
2
|
+
// — for a process that rescans — its pin in the reload graph. The scan (`app-load.ts`) and the
|
|
3
|
+
// prebuilt registry's boot (`app-load-prebuilt.ts`) both register through here, so a module
|
|
4
|
+
// registers the same way whichever of them imported it.
|
|
5
|
+
|
|
6
|
+
import { isAction, isMutator } from '@ultimat3/action';
|
|
7
|
+
import { registerActions } from '@ultimat3/action/loader';
|
|
8
|
+
import { getEntity } from '@ultimat3/entity';
|
|
9
|
+
import { isJobHandle, isTaskHandle } from '@ultimat3/jobs';
|
|
10
|
+
import { isQuery } from '@ultimat3/query';
|
|
11
|
+
import { registerQueries } from '@ultimat3/query/loader';
|
|
12
|
+
import type { RouteConfig } from '@ultimat3/render';
|
|
13
|
+
import { isRouteConfig, pageComponentOf, registerRoute } from '@ultimat3/render';
|
|
14
|
+
import { pinModule, resetReloadGraph } from './app-reload-graph';
|
|
15
|
+
import { API_INDEX } from './app-root';
|
|
16
|
+
import type { Finding } from './output';
|
|
17
|
+
import { findingFrom } from './output';
|
|
18
|
+
|
|
19
|
+
// A module is imported and registered once per PROCESS: `import()` caches, and a registry rejects
|
|
20
|
+
// a second registration of a name. So a rescan refreshes the facts DERIVED from the registries —
|
|
21
|
+
// the manifest and its build id — and, for exactly one kind of module, the registration itself. A
|
|
22
|
+
// rescan first evicts every changed module, and everything that imports it, from Bun's registry
|
|
23
|
+
// (`app-reload-graph.ts`), so the scan's own `import()` evaluates the new chain; a ROUTE module
|
|
24
|
+
// that comes back as a different instance has its entry replaced. A module that defines a
|
|
25
|
+
// primitive (an action, a query, an entity, a job, a task) is pinned instead — its exports are held
|
|
26
|
+
// by every importer and a second instance would be a duplicate name in its registry — so those
|
|
27
|
+
// edits still need a restart. Until 2026-09-27 only the route module itself was re-imported, under
|
|
28
|
+
// `?x-reload=<hash>`, and every component it imports stayed the cached one: `x dev` logged
|
|
29
|
+
// "reloaded" and served the old component.
|
|
30
|
+
const registered = new Set<string>();
|
|
31
|
+
// A registration failure is sticky: the file is never retried, so the finding is replayed.
|
|
32
|
+
const failures = new Map<string, Finding>();
|
|
33
|
+
// Route modules only: the module instance each one registered from. A rescan that imports the same
|
|
34
|
+
// instance registers nothing; a different one (the file or something it imports was evicted) is
|
|
35
|
+
// the page the author saved.
|
|
36
|
+
const routeModules = new Map<string, Record<string, unknown>>();
|
|
37
|
+
|
|
38
|
+
/** Test seam, and what `x dev` would call if it ever restarted the registries in-process. */
|
|
39
|
+
export function resetAppLoad(): void {
|
|
40
|
+
registered.clear();
|
|
41
|
+
failures.clear();
|
|
42
|
+
routeModules.clear();
|
|
43
|
+
resetReloadGraph();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Whether any module has registered in this process: a scan after one is a rescan. */
|
|
47
|
+
export const registeredAny = (): boolean => registered.size > 0;
|
|
48
|
+
|
|
49
|
+
/** Whether `absolute` has never been through `registerModule` — registered or failed. */
|
|
50
|
+
export const isFirstPass = (absolute: string): boolean =>
|
|
51
|
+
!registered.has(absolute) && !failures.has(absolute);
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Whether `value` is an entity the registry holds — the entry, or the `core` an `entity()` module
|
|
55
|
+
* exports. Asked of the registry by the value's own name, so it is O(1) and never stale: a scan
|
|
56
|
+
* registers module by module while later modules are still evaluating and registering entities,
|
|
57
|
+
* and a set built once (as until 2026-11-09) missed every entity declared after it.
|
|
58
|
+
*/
|
|
59
|
+
export function isRegisteredEntity(value: unknown): boolean {
|
|
60
|
+
if (typeof value !== 'object' || value === null) return false;
|
|
61
|
+
const named: unknown = Reflect.get(value, '$name') ?? Reflect.get(value, 'name');
|
|
62
|
+
if (typeof named !== 'string') return false;
|
|
63
|
+
const entry = getEntity(named);
|
|
64
|
+
return entry !== undefined && (entry === value || entry.core === value);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** One module's registration: what `registerModule` needs beyond the module itself. */
|
|
68
|
+
export interface ModuleRegistration {
|
|
69
|
+
readonly absolute: string;
|
|
70
|
+
/** App-root-relative, POSIX. */
|
|
71
|
+
readonly file: string;
|
|
72
|
+
/** A process that rescans: pins primitive-defining modules, re-registers changed routes. */
|
|
73
|
+
readonly track: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Registers a module once; every later call replays whatever the first one reported — except for
|
|
78
|
+
* a route module, which a later call re-registers when the import handed back a new instance.
|
|
79
|
+
*/
|
|
80
|
+
export function registerModule(
|
|
81
|
+
module: Record<string, unknown>,
|
|
82
|
+
at: ModuleRegistration,
|
|
83
|
+
): Finding | undefined {
|
|
84
|
+
const { absolute, file, track } = at;
|
|
85
|
+
const previous = failures.get(absolute);
|
|
86
|
+
if (previous !== undefined) return previous;
|
|
87
|
+
if (registered.has(absolute)) return track ? reloadRoute(absolute, file, module) : undefined;
|
|
88
|
+
registered.add(absolute);
|
|
89
|
+
try {
|
|
90
|
+
const config = module['config'];
|
|
91
|
+
const route = isRouteConfig(config) ? config : undefined;
|
|
92
|
+
if (route !== undefined) {
|
|
93
|
+
registerRouteModule(file, module, route);
|
|
94
|
+
routeModules.set(absolute, module);
|
|
95
|
+
}
|
|
96
|
+
// A pin is the reload graph's: it stops an eviction a process that never rescans never runs,
|
|
97
|
+
// and deciding it walks every export of every module against the entity registry.
|
|
98
|
+
if (track && (definesPrimitive(module, route !== undefined) || file === API_INDEX)) {
|
|
99
|
+
pinModule(absolute);
|
|
100
|
+
}
|
|
101
|
+
registerActions(module);
|
|
102
|
+
registerQueries(module);
|
|
103
|
+
return undefined;
|
|
104
|
+
} catch (error) {
|
|
105
|
+
const finding: Finding = { ...findingFrom(error), at: file };
|
|
106
|
+
failures.set(absolute, finding);
|
|
107
|
+
return finding;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* A module whose instance must be the one every importer and every registry holds. A route module
|
|
113
|
+
* may export an action beside its page — it has always been re-imported, and its actions stayed
|
|
114
|
+
* registered from the first instance — so only an entity, a job or a task pins a route.
|
|
115
|
+
*/
|
|
116
|
+
function definesPrimitive(module: Record<string, unknown>, route: boolean): boolean {
|
|
117
|
+
const defines = (value: unknown): boolean =>
|
|
118
|
+
isJobHandle(value) ||
|
|
119
|
+
isTaskHandle(value) ||
|
|
120
|
+
(!route && (isAction(value) || isQuery(value) || isMutator(value))) ||
|
|
121
|
+
isRegisteredEntity(value);
|
|
122
|
+
return Object.values(module).some((value) => {
|
|
123
|
+
// A brand check reads a property, and an export may be a proxy whose every read throws until
|
|
124
|
+
// the env is set — examples/dummy's typed client throws X_ENV_MISSING without APP_URL. A value
|
|
125
|
+
// that cannot be asked is not a primitive: every primitive is a plain branded object.
|
|
126
|
+
try {
|
|
127
|
+
return defines(value);
|
|
128
|
+
} catch {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The route half of a registration, and the whole of a re-registration. */
|
|
135
|
+
function registerRouteModule(
|
|
136
|
+
file: string,
|
|
137
|
+
module: Record<string, unknown>,
|
|
138
|
+
config: RouteConfig,
|
|
139
|
+
): void {
|
|
140
|
+
// The page component comes from the same module as its config, resolved by render's own
|
|
141
|
+
// rule — the CLI does not decide which export is a page any more than it decides what a
|
|
142
|
+
// route is. A module with no component registers without one, and renders a bare shell.
|
|
143
|
+
const component = pageComponentOf(module);
|
|
144
|
+
registerRoute({
|
|
145
|
+
file,
|
|
146
|
+
config,
|
|
147
|
+
...(component === undefined ? {} : { component }),
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* A registered route module, on a rescan: re-registered when the import handed back a different
|
|
153
|
+
* instance — the file or something it imports was evicted — and left alone otherwise. A save that
|
|
154
|
+
* will not import never reaches here (the scan reports it, not sticky), so the last page that did
|
|
155
|
+
* import is the one served meanwhile.
|
|
156
|
+
*/
|
|
157
|
+
function reloadRoute(
|
|
158
|
+
absolute: string,
|
|
159
|
+
file: string,
|
|
160
|
+
module: Record<string, unknown>,
|
|
161
|
+
): Finding | undefined {
|
|
162
|
+
const held = routeModules.get(absolute);
|
|
163
|
+
if (held === undefined || held === module) return undefined;
|
|
164
|
+
try {
|
|
165
|
+
const config = module['config'];
|
|
166
|
+
// A file that stopped being a route is not un-registered — the table has no verb for it, and a
|
|
167
|
+
// deleted file needs a restart either way. Its instance is recorded so the same save is not
|
|
168
|
+
// re-registered on every later tick.
|
|
169
|
+
if (isRouteConfig(config)) registerRouteModule(file, module, config);
|
|
170
|
+
routeModules.set(absolute, module);
|
|
171
|
+
return undefined;
|
|
172
|
+
} catch (error) {
|
|
173
|
+
return { ...findingFrom(error), at: file };
|
|
174
|
+
}
|
|
175
|
+
}
|