@mostra/runtime 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +39 -2
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +236 -0
- package/dist/bin.js.map +1 -0
- package/dist/builder/child/main.d.ts +1 -0
- package/dist/builder/child/main.js +1408 -0
- package/dist/builder/child/main.js.map +1 -0
- package/dist/child-xrHmytks.js +49 -0
- package/dist/child-xrHmytks.js.map +1 -0
- package/dist/deps/child/install.d.ts +1 -0
- package/dist/deps/child/install.js +33 -0
- package/dist/deps/child/install.js.map +1 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/instance-D7_UVdoN.js +15752 -0
- package/dist/instance-D7_UVdoN.js.map +1 -0
- package/dist/types-NjNy7gXX.js +57 -0
- package/dist/types-NjNy7gXX.js.map +1 -0
- package/package.json +87 -3
- package/src/actions/index.ts +622 -0
- package/src/addressing/index.ts +281 -0
- package/src/bin.ts +240 -0
- package/src/builder/child/dev.ts +383 -0
- package/src/builder/child/fields.ts +363 -0
- package/src/builder/child/main.ts +47 -0
- package/src/builder/child/plugins.ts +195 -0
- package/src/builder/child/scan.ts +459 -0
- package/src/builder/child/spa.ts +348 -0
- package/src/builder/coverage.ts +72 -0
- package/src/builder/index.ts +406 -0
- package/src/builder/types.ts +180 -0
- package/src/deps/child/install.ts +30 -0
- package/src/deps/index.ts +918 -0
- package/src/deps/installer.ts +321 -0
- package/src/deps/lock.ts +82 -0
- package/src/deps/scan.ts +266 -0
- package/src/deps/specifier.ts +64 -0
- package/src/deps/warm.ts +57 -0
- package/src/dev/index.ts +518 -0
- package/src/faults.ts +65 -0
- package/src/gate.ts +55 -0
- package/src/gateway/actions.ts +268 -0
- package/src/gateway/browser.ts +650 -0
- package/src/gateway/capabilities.ts +739 -0
- package/src/gateway/controls.ts +91 -0
- package/src/gateway/dev.ts +119 -0
- package/src/gateway/http.ts +229 -0
- package/src/gateway/index.ts +1209 -0
- package/src/gateway/integrity.ts +117 -0
- package/src/gateway/listener.ts +217 -0
- package/src/gateway/routes.ts +197 -0
- package/src/gateway/serve.ts +138 -0
- package/src/gc/index.ts +451 -0
- package/src/home/acquire.ts +276 -0
- package/src/home/index.ts +16 -0
- package/src/home/paths.ts +192 -0
- package/src/home/platform.ts +34 -0
- package/src/home/record.ts +76 -0
- package/src/home/settings.ts +113 -0
- package/src/index.ts +30 -0
- package/src/instance/index.ts +942 -0
- package/src/interaction/ephemeral.ts +300 -0
- package/src/interaction/events.ts +355 -0
- package/src/interaction/fields.ts +392 -0
- package/src/interaction/index.ts +1009 -0
- package/src/interaction/outbox.ts +493 -0
- package/src/interaction/policy.ts +26 -0
- package/src/interaction/results.ts +341 -0
- package/src/interaction/runs.ts +313 -0
- package/src/interaction/signals.ts +120 -0
- package/src/interaction/submissions.ts +585 -0
- package/src/interaction/subscriptions.ts +258 -0
- package/src/interaction/viewers.ts +173 -0
- package/src/interaction/wait.ts +201 -0
- package/src/ledger/index.ts +442 -0
- package/src/ledger/migrations.ts +583 -0
- package/src/ledger/requests.ts +105 -0
- package/src/ledger/types.ts +28 -0
- package/src/live/index.ts +573 -0
- package/src/log.ts +316 -0
- package/src/processes/child.ts +67 -0
- package/src/processes/environment.ts +64 -0
- package/src/processes/identity.ts +191 -0
- package/src/processes/index.ts +19 -0
- package/src/processes/manager.ts +543 -0
- package/src/protocol-server/client.ts +94 -0
- package/src/protocol-server/index.ts +91 -0
- package/src/protocol-server/server.ts +509 -0
- package/src/protocol-server/streams.ts +81 -0
- package/src/publish/checks.ts +92 -0
- package/src/publish/digest.ts +87 -0
- package/src/publish/index.ts +23 -0
- package/src/publish/manifest.ts +409 -0
- package/src/publish/methods.ts +96 -0
- package/src/publish/publisher.ts +913 -0
- package/src/publish/source.ts +272 -0
- package/src/publish/tar.ts +110 -0
- package/src/registry/index.ts +481 -0
- package/src/registry/store.ts +136 -0
- package/src/secrets/index.ts +223 -0
- package/src/service/idle.ts +64 -0
- package/src/service/index.ts +3 -0
- package/src/service/unit.ts +250 -0
- package/src/verify/fields.ts +125 -0
- package/src/verify/index.ts +913 -0
- package/src/verify/regression.ts +157 -0
- package/src/verify/runner.ts +230 -0
- package/src/verify/sandbox.ts +407 -0
- package/src/verify/typecheck.ts +371 -0
- package/src/workers/index.ts +17 -0
- package/src/workers/launcher.ts +260 -0
- package/src/workers/pool.ts +708 -0
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The builder (ARCHITECTURE §1, §3; ROUND-3 §7): the internal `RenderMode` contract with SPA as
|
|
3
|
+
* its implementation (D19 seam 5), and the supervisor's side of the builder child. The supervisor
|
|
4
|
+
* never loads the bundler: it starts a child per build through the process manager, which runs
|
|
5
|
+
* `child/main.ts`, and turns a crash or a timeout into `MOSTRA_BUILD_CRASHED` or
|
|
6
|
+
* `MOSTRA_BUILD_TIMEOUT` (FAILURE_MODEL §2, §9).
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const spa = createSpaRenderMode({ processes, log });
|
|
10
|
+
* const result = await spa.build({ stagingDir, entry: "index.tsx", entryHasDefault: true, manifest, nodeModules, … });
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
15
|
+
import { dirname, join } from "node:path";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
import { createDiagnostic, RpcError } from "@mostra/protocol";
|
|
18
|
+
import type { Faults } from "../faults.ts";
|
|
19
|
+
import type { Environment, Log } from "../log.ts";
|
|
20
|
+
import type { ProcessManager } from "../processes/index.ts";
|
|
21
|
+
import type { FieldCoverage } from "./coverage.ts";
|
|
22
|
+
import {
|
|
23
|
+
BUILD_METHOD,
|
|
24
|
+
type BuildRequest,
|
|
25
|
+
type BuildResponse,
|
|
26
|
+
DEV_CHANGED_METHOD,
|
|
27
|
+
DEV_METHOD,
|
|
28
|
+
DEV_RELOAD_METHOD,
|
|
29
|
+
DEV_SERVER_NOTIFICATION,
|
|
30
|
+
DEV_UPDATE_NOTIFICATION,
|
|
31
|
+
type DevChange,
|
|
32
|
+
type DevReady,
|
|
33
|
+
type DevRequest,
|
|
34
|
+
type DevServerBuild,
|
|
35
|
+
STAGING_FILES,
|
|
36
|
+
} from "./types.ts";
|
|
37
|
+
|
|
38
|
+
export { coverageMatcher, type FieldCoverage, type FieldEntry } from "./coverage.ts";
|
|
39
|
+
export {
|
|
40
|
+
type BuildRequest,
|
|
41
|
+
type BuildResponse,
|
|
42
|
+
DEV_SECRET_HEADER,
|
|
43
|
+
type DevChange,
|
|
44
|
+
type DevReady,
|
|
45
|
+
type DevRequest,
|
|
46
|
+
type DevServerBuild,
|
|
47
|
+
LIVE_DECLARATION,
|
|
48
|
+
type VersionRegistry,
|
|
49
|
+
} from "./types.ts";
|
|
50
|
+
|
|
51
|
+
/** How long one build may take (FAILURE_MODEL §9). */
|
|
52
|
+
export const BUILD_TIMEOUT_MS = 180_000;
|
|
53
|
+
|
|
54
|
+
/** Packages whose versions define the SPA build; they are part of `buildConfigDigest` (VR-01). */
|
|
55
|
+
const BUILD_PACKAGES = [
|
|
56
|
+
"vite",
|
|
57
|
+
"@vitejs/plugin-react",
|
|
58
|
+
"@mdx-js/mdx",
|
|
59
|
+
"remark-frontmatter",
|
|
60
|
+
"remark-gfm",
|
|
61
|
+
"@tailwindcss/vite",
|
|
62
|
+
"oxc-parser",
|
|
63
|
+
] as const;
|
|
64
|
+
|
|
65
|
+
/** Bumped whenever the SPA build's settings change, so older versions aren't reused. */
|
|
66
|
+
const SPA_CONFIG_REVISION = 7;
|
|
67
|
+
|
|
68
|
+
/** What a render mode can deliver for a manifest (ROUND-3 §7). */
|
|
69
|
+
export interface RenderCapabilities {
|
|
70
|
+
readonly staticExport: boolean;
|
|
71
|
+
readonly mcpApp: boolean;
|
|
72
|
+
readonly needsWorker: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The internal render-mode contract (ROUND-3 §7, D19 seam 5). SPA is v1's only mode; an `rsc`
|
|
77
|
+
* mode implements the same interface later. `dev` arrives with `mostra dev` (WP-10).
|
|
78
|
+
*/
|
|
79
|
+
export interface RenderMode {
|
|
80
|
+
readonly id: "spa";
|
|
81
|
+
/** The settings that define the build, hashed into `buildConfigDigest`. */
|
|
82
|
+
readonly buildConfig: Readonly<Record<string, unknown>>;
|
|
83
|
+
capabilities(input: { readonly hasServerCode: boolean }): RenderCapabilities;
|
|
84
|
+
/**
|
|
85
|
+
* Builds the snapshot in `request.stagingDir` into `request.outDir`, resolving packages from
|
|
86
|
+
* `request.nodeModules`: the warm layout or the artifact's lock layout (D95).
|
|
87
|
+
*/
|
|
88
|
+
build(request: BuildRequest): Promise<BuildResponse>;
|
|
89
|
+
/** The server bundle a worker loads from a version directory, when the version has one. */
|
|
90
|
+
serverEntry(versionDir: string): string | undefined;
|
|
91
|
+
/**
|
|
92
|
+
* `mostra dev` (VR-08, D140 item 12): a dev server in a builder child of its own, over the dev
|
|
93
|
+
* root's mirror of the source, until {@link DevChild.stop}. Throws `MOSTRA_BUILD_CRASHED` when
|
|
94
|
+
* the child can't start it.
|
|
95
|
+
*/
|
|
96
|
+
dev(request: DevRequest, events: DevEvents): Promise<DevChild>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** What a dev child tells the supervisor as the source changes. */
|
|
100
|
+
export interface DevEvents {
|
|
101
|
+
/** The server bundle was built again, or failed to build; `files` changed. */
|
|
102
|
+
server(build: DevServerBuild, files: readonly string[]): void;
|
|
103
|
+
/** Client modules of `files` were updated in place. */
|
|
104
|
+
update(files: readonly string[]): void;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** A running dev child. */
|
|
108
|
+
export interface DevChild {
|
|
109
|
+
readonly ready: DevReady;
|
|
110
|
+
/** Tells every page to load again. */
|
|
111
|
+
reload(): Promise<void>;
|
|
112
|
+
/** These files of the mirror changed; resolves once the child acted on them. */
|
|
113
|
+
changed(changes: readonly DevChange[]): Promise<void>;
|
|
114
|
+
/** Resolves when the child ended, for whatever reason. */
|
|
115
|
+
readonly exited: Promise<{ readonly stderrTail: readonly string[] }>;
|
|
116
|
+
stop(): Promise<void>;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The directory of `@mostra/runtime`'s package.json. */
|
|
120
|
+
function runtimeRoot(): string {
|
|
121
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
122
|
+
for (;;) {
|
|
123
|
+
const manifest = join(dir, "package.json");
|
|
124
|
+
if (existsSync(manifest)) {
|
|
125
|
+
const { name } = JSON.parse(readFileSync(manifest, "utf8")) as { name?: string };
|
|
126
|
+
if (name === "@mostra/runtime") return dir;
|
|
127
|
+
}
|
|
128
|
+
const parent = dirname(dir);
|
|
129
|
+
if (parent === dir) throw new Error("Couldn't find @mostra/runtime's package.json.");
|
|
130
|
+
dir = parent;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Node's lookup of an installed package's directory, from `from` upwards. */
|
|
135
|
+
function packageDir(name: string, from: string): string | undefined {
|
|
136
|
+
let dir = from;
|
|
137
|
+
for (;;) {
|
|
138
|
+
const candidate = join(dir, "node_modules", name);
|
|
139
|
+
if (existsSync(join(candidate, "package.json"))) return realpathSync(candidate);
|
|
140
|
+
const parent = dirname(dir);
|
|
141
|
+
if (parent === dir) return undefined;
|
|
142
|
+
dir = parent;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function versionOf(dir: string): string {
|
|
147
|
+
return (JSON.parse(readFileSync(join(dir, "package.json"), "utf8")) as { version: string })
|
|
148
|
+
.version;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The builder child's entry: the compiled one beside the compiled supervisor, else the source. */
|
|
152
|
+
function childEntry(root: string): string {
|
|
153
|
+
return fileURLToPath(import.meta.url).endsWith(".ts")
|
|
154
|
+
? join(root, "src/builder/child/main.ts")
|
|
155
|
+
: join(root, "dist/builder/child/main.js");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Options for {@link createSpaRenderMode}. */
|
|
159
|
+
export interface SpaRenderModeOptions {
|
|
160
|
+
readonly processes: ProcessManager;
|
|
161
|
+
readonly log: Log;
|
|
162
|
+
/** Default {@link BUILD_TIMEOUT_MS}. */
|
|
163
|
+
readonly timeoutMs?: number;
|
|
164
|
+
/** The environment the builder's minimal environment takes `PATH` and `HOME` from. */
|
|
165
|
+
readonly env?: Environment;
|
|
166
|
+
/** Test fault points: `builder:build` with `hang` stops each build's child before it builds. */
|
|
167
|
+
readonly faults?: Faults;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** The SPA render mode (D19): every build in its own builder child. */
|
|
171
|
+
export function createSpaRenderMode(options: SpaRenderModeOptions): RenderMode {
|
|
172
|
+
const root = runtimeRoot();
|
|
173
|
+
const buildPackages: Record<string, string> = {};
|
|
174
|
+
for (const name of BUILD_PACKAGES) {
|
|
175
|
+
const dir = packageDir(name, root);
|
|
176
|
+
if (!dir) throw new Error(`The runtime is missing its build dependency ${name}.`);
|
|
177
|
+
buildPackages[name] = versionOf(dir);
|
|
178
|
+
}
|
|
179
|
+
const buildConfig = {
|
|
180
|
+
mode: "spa",
|
|
181
|
+
revision: SPA_CONFIG_REVISION,
|
|
182
|
+
base: "/",
|
|
183
|
+
sourcemap: true,
|
|
184
|
+
packages: buildPackages,
|
|
185
|
+
stagingFiles: STAGING_FILES,
|
|
186
|
+
};
|
|
187
|
+
const timeoutMs = options.timeoutMs ?? BUILD_TIMEOUT_MS;
|
|
188
|
+
const env = options.env ?? process.env;
|
|
189
|
+
// The builder gets only what it needs: no secrets, nothing that could reach a bundle.
|
|
190
|
+
const childEnv: Environment = {
|
|
191
|
+
PATH: env["PATH"] ?? "/usr/bin:/bin",
|
|
192
|
+
HOME: env["HOME"] ?? "/",
|
|
193
|
+
NODE_ENV: "production",
|
|
194
|
+
...(env["TMPDIR"] ? { TMPDIR: env["TMPDIR"] } : {}),
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
id: "spa",
|
|
199
|
+
buildConfig,
|
|
200
|
+
capabilities: ({ hasServerCode }) => ({
|
|
201
|
+
staticExport: !hasServerCode,
|
|
202
|
+
mcpApp: true,
|
|
203
|
+
needsWorker: hasServerCode,
|
|
204
|
+
}),
|
|
205
|
+
serverEntry(versionDir) {
|
|
206
|
+
const entry = spaServerEntry(versionDir);
|
|
207
|
+
return entry === undefined ? undefined : join(versionDir, entry);
|
|
208
|
+
},
|
|
209
|
+
async dev(request, events) {
|
|
210
|
+
const child = await options.processes.spawn({
|
|
211
|
+
kind: "builder",
|
|
212
|
+
ownerRef: request.root,
|
|
213
|
+
command: process.execPath,
|
|
214
|
+
args: [childEntry(root)],
|
|
215
|
+
cwd: request.root,
|
|
216
|
+
// A dev server builds for development; nothing else of the supervisor's environment.
|
|
217
|
+
env: { ...childEnv, NODE_ENV: "development" },
|
|
218
|
+
ipc: {
|
|
219
|
+
onNotification: (method, params) => {
|
|
220
|
+
const body = (params ?? {}) as { build?: DevServerBuild; files?: string[] };
|
|
221
|
+
if (method === DEV_SERVER_NOTIFICATION && body.build)
|
|
222
|
+
events.server(body.build, body.files ?? []);
|
|
223
|
+
if (method === DEV_UPDATE_NOTIFICATION) events.update(body.files ?? []);
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
const outcome = await Promise.race([
|
|
228
|
+
child
|
|
229
|
+
.request(DEV_METHOD, request as unknown as Record<string, unknown>, {
|
|
230
|
+
timeoutMs: timeoutMs + 1000,
|
|
231
|
+
})
|
|
232
|
+
.then(
|
|
233
|
+
(ready) => ({ kind: "ready" as const, ready: ready as DevReady }),
|
|
234
|
+
(error: unknown) => ({ kind: "error" as const, error }),
|
|
235
|
+
),
|
|
236
|
+
child.exited.then((exit) => ({ kind: "exit" as const, exit })),
|
|
237
|
+
]);
|
|
238
|
+
if (outcome.kind !== "ready") {
|
|
239
|
+
await child.stop(2000);
|
|
240
|
+
const why =
|
|
241
|
+
outcome.kind === "error"
|
|
242
|
+
? ((outcome.error as Error).message ?? String(outcome.error))
|
|
243
|
+
: `it exited (${outcome.exit.signal ?? `code ${outcome.exit.code}`})${outcome.exit.stderrTail.length > 0 ? `\n${outcome.exit.stderrTail.slice(-10).join("\n")}` : ""}`;
|
|
244
|
+
throw new RpcError(
|
|
245
|
+
createDiagnostic("MOSTRA_BUILD_CRASHED", {
|
|
246
|
+
message: `The dev server couldn't start: ${why}`,
|
|
247
|
+
}),
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
ready: outcome.ready,
|
|
252
|
+
reload: async () => {
|
|
253
|
+
await child.request(DEV_RELOAD_METHOD, {}, { timeoutMs: 5000 });
|
|
254
|
+
},
|
|
255
|
+
changed: async (changes) => {
|
|
256
|
+
await child.request(
|
|
257
|
+
DEV_CHANGED_METHOD,
|
|
258
|
+
{ changes: changes as unknown as Record<string, unknown>[] },
|
|
259
|
+
{ timeoutMs: timeoutMs + 1000 },
|
|
260
|
+
);
|
|
261
|
+
},
|
|
262
|
+
exited: child.exited,
|
|
263
|
+
stop: async () => {
|
|
264
|
+
await child.stop(3000);
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
},
|
|
268
|
+
async build(request) {
|
|
269
|
+
const child = await options.processes.spawn({
|
|
270
|
+
kind: "builder",
|
|
271
|
+
ownerRef: request.stagingDir,
|
|
272
|
+
command: process.execPath,
|
|
273
|
+
args: [childEntry(root)],
|
|
274
|
+
cwd: request.stagingDir,
|
|
275
|
+
env: childEnv,
|
|
276
|
+
ipc: {},
|
|
277
|
+
});
|
|
278
|
+
// A builder that hangs (issue #18): told to, so it never answers and the limit ends it. A
|
|
279
|
+
// signal from outside raced a fast build; the child itself can't.
|
|
280
|
+
const wire: BuildRequest = options.faults?.hangs("builder:build")
|
|
281
|
+
? { ...request, hang: true }
|
|
282
|
+
: request;
|
|
283
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
284
|
+
try {
|
|
285
|
+
const outcome = await Promise.race([
|
|
286
|
+
child
|
|
287
|
+
.request(BUILD_METHOD, wire as unknown as Record<string, unknown>, {
|
|
288
|
+
timeoutMs: timeoutMs + 1000,
|
|
289
|
+
})
|
|
290
|
+
.then(
|
|
291
|
+
(result) => ({ kind: "result" as const, result: result as BuildResponse }),
|
|
292
|
+
(error: unknown) => ({ kind: "error" as const, error }),
|
|
293
|
+
),
|
|
294
|
+
child.exited.then((exit) => ({ kind: "exit" as const, exit })),
|
|
295
|
+
new Promise<{ kind: "timeout" }>((done) => {
|
|
296
|
+
timer = setTimeout(() => done({ kind: "timeout" }), timeoutMs);
|
|
297
|
+
}),
|
|
298
|
+
]);
|
|
299
|
+
if (outcome.kind === "result") return outcome.result;
|
|
300
|
+
if (outcome.kind === "timeout") {
|
|
301
|
+
return failed(
|
|
302
|
+
"MOSTRA_BUILD_TIMEOUT",
|
|
303
|
+
`The build took longer than ${Math.round(timeoutMs / 1000)} s and was stopped.`,
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
let exit: Awaited<typeof child.exited> | undefined =
|
|
307
|
+
outcome.kind === "exit" ? outcome.exit : undefined;
|
|
308
|
+
if (outcome.kind === "error") {
|
|
309
|
+
// A crash closes the IPC channel first; give the exit a moment to arrive.
|
|
310
|
+
exit = await Promise.race([
|
|
311
|
+
child.exited,
|
|
312
|
+
new Promise<undefined>((done) => setTimeout(() => done(undefined), 1000)),
|
|
313
|
+
]);
|
|
314
|
+
if (exit === undefined) {
|
|
315
|
+
options.log.error("builder.failed", { error: outcome.error });
|
|
316
|
+
return failed(
|
|
317
|
+
"MOSTRA_BUILD_CRASHED",
|
|
318
|
+
`The builder failed unexpectedly: ${(outcome.error as Error).message ?? String(outcome.error)}`,
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (exit === undefined) throw new Error("unreachable");
|
|
323
|
+
const tail = exit.stderrTail.slice(-10).join("\n");
|
|
324
|
+
options.log.warn("builder.crashed", { code: exit.code, signal: exit.signal });
|
|
325
|
+
return failed(
|
|
326
|
+
"MOSTRA_BUILD_CRASHED",
|
|
327
|
+
`The builder exited (${exit.signal ?? `code ${exit.code}`}) before it finished.${tail ? `\n${tail}` : ""}`,
|
|
328
|
+
);
|
|
329
|
+
} finally {
|
|
330
|
+
if (timer) clearTimeout(timer);
|
|
331
|
+
await child.stop(2000);
|
|
332
|
+
}
|
|
333
|
+
},
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function failed(
|
|
338
|
+
code: "MOSTRA_BUILD_TIMEOUT" | "MOSTRA_BUILD_CRASHED",
|
|
339
|
+
message: string,
|
|
340
|
+
): BuildResponse {
|
|
341
|
+
return { ok: false, diagnostics: [createDiagnostic(code, { message })] };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* The SPA mode's server bundle of a built version, relative to it (`server/entry.mjs`), or
|
|
346
|
+
* `undefined` for a version without server code, which never gets a worker (RT-05).
|
|
347
|
+
*/
|
|
348
|
+
function spaServerEntry(versionDir: string): string | undefined {
|
|
349
|
+
return existsSync(join(versionDir, "server", "entry.mjs")) ? "server/entry.mjs" : undefined;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** The IDs of the server functions a version's `registry.json` lists, empty when it has none. */
|
|
353
|
+
export function readServerFunctions(versionDir: string): ReadonlySet<string> {
|
|
354
|
+
try {
|
|
355
|
+
const registry = JSON.parse(readFileSync(join(versionDir, "registry.json"), "utf8")) as {
|
|
356
|
+
readonly actions?: unknown;
|
|
357
|
+
};
|
|
358
|
+
if (!Array.isArray(registry.actions)) return new Set();
|
|
359
|
+
return new Set(
|
|
360
|
+
registry.actions
|
|
361
|
+
.map((entry) => (entry as { id?: unknown } | null)?.id)
|
|
362
|
+
.filter((id): id is string => typeof id === "string"),
|
|
363
|
+
);
|
|
364
|
+
} catch {
|
|
365
|
+
return new Set();
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** The live sources a version's `registry.json` lists (D56), empty when it has none. */
|
|
370
|
+
export function readLiveSources(
|
|
371
|
+
versionDir: string,
|
|
372
|
+
): readonly { readonly key: string; readonly always?: boolean }[] {
|
|
373
|
+
try {
|
|
374
|
+
const registry = JSON.parse(readFileSync(join(versionDir, "registry.json"), "utf8")) as {
|
|
375
|
+
readonly liveSources?: unknown;
|
|
376
|
+
};
|
|
377
|
+
if (!Array.isArray(registry.liveSources)) return [];
|
|
378
|
+
return registry.liveSources.filter(
|
|
379
|
+
(entry): entry is { key: string; always?: boolean } =>
|
|
380
|
+
typeof entry === "object" &&
|
|
381
|
+
entry !== null &&
|
|
382
|
+
typeof (entry as { key?: unknown }).key === "string",
|
|
383
|
+
);
|
|
384
|
+
} catch {
|
|
385
|
+
return [];
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* The field coverage a published version recorded in its `registry.json` (D109), or `undefined`
|
|
391
|
+
* for a version built before WP-07 or one whose registry can't be read: such a version covers
|
|
392
|
+
* every field ID, so nothing is orphaned by a guess.
|
|
393
|
+
*/
|
|
394
|
+
export function readFieldCoverage(versionDir: string): FieldCoverage | undefined {
|
|
395
|
+
try {
|
|
396
|
+
const registry = JSON.parse(readFileSync(join(versionDir, "registry.json"), "utf8")) as {
|
|
397
|
+
readonly fields?: FieldCoverage;
|
|
398
|
+
};
|
|
399
|
+
const fields = registry.fields;
|
|
400
|
+
if (fields === undefined || typeof fields.open !== "boolean" || !Array.isArray(fields.entries))
|
|
401
|
+
return undefined;
|
|
402
|
+
return fields;
|
|
403
|
+
} catch {
|
|
404
|
+
return undefined;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The builder's types, shared by the supervisor side (`index.ts`) and the builder child
|
|
3
|
+
* (`child/`). Nothing here imports the bundler, so the supervisor can use them.
|
|
4
|
+
*/
|
|
5
|
+
import type { ArtifactManifest, Diagnostic } from "@mostra/protocol";
|
|
6
|
+
import type { FieldCoverage } from "./coverage.ts";
|
|
7
|
+
|
|
8
|
+
/** One `"use server"` export (D54, D55): what the client stub calls and the worker runs. */
|
|
9
|
+
export interface ServerFunctionEntry {
|
|
10
|
+
/** `<module>#<export>`, for example `actions.ts#sendReply`. */
|
|
11
|
+
readonly id: string;
|
|
12
|
+
/** The module's path inside the artifact. */
|
|
13
|
+
readonly module: string;
|
|
14
|
+
/** The export's name (`default` for a default export). */
|
|
15
|
+
readonly export: string;
|
|
16
|
+
/** Whether the function references `dryRun()` from `@mostra/server`, opting into `verify`'s smoke test (D55). */
|
|
17
|
+
readonly dryRun: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** One route of a `pages/` artifact (D57). */
|
|
21
|
+
export interface PageRoute {
|
|
22
|
+
/** The React Router path: `/`, `/settings`, `/reports/:id`. */
|
|
23
|
+
readonly path: string;
|
|
24
|
+
/** The page's file inside the artifact. */
|
|
25
|
+
readonly file: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* What a version contains, read by `verify`, the shell, the worker and `results` (INTERFACES §11).
|
|
30
|
+
* Written as `registry.json` in the version directory. Later packages add keys (fields, WP-07).
|
|
31
|
+
*/
|
|
32
|
+
export interface VersionRegistry {
|
|
33
|
+
readonly schemaVersion: 1;
|
|
34
|
+
/** The entry, inside the artifact. */
|
|
35
|
+
readonly entry: string;
|
|
36
|
+
/** Every `"use server"` export. */
|
|
37
|
+
readonly actions: readonly ServerFunctionEntry[];
|
|
38
|
+
/**
|
|
39
|
+
* The live sources `live.ts` exports (D56), each with `always` when its `defineLive` literal
|
|
40
|
+
* says it (WP-08); absent when the builder couldn't tell, and the worker reports it.
|
|
41
|
+
*/
|
|
42
|
+
readonly liveSources: readonly { readonly key: string; readonly always?: boolean }[];
|
|
43
|
+
/** The routes of `pages/`, empty for a single-page artifact. */
|
|
44
|
+
readonly pages: readonly PageRoute[];
|
|
45
|
+
/** The terminal sessions the manifest declares, by name. */
|
|
46
|
+
readonly terminals: readonly string[];
|
|
47
|
+
/** The web demos the manifest declares, by name. */
|
|
48
|
+
readonly demos: readonly string[];
|
|
49
|
+
/**
|
|
50
|
+
* The field IDs the version's code can use (D109). Absent in versions built before WP-07, which
|
|
51
|
+
* cover every field ID.
|
|
52
|
+
*/
|
|
53
|
+
readonly fields?: FieldCoverage;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** What the supervisor asks the builder child to build (`builder/build`). */
|
|
57
|
+
export interface BuildRequest {
|
|
58
|
+
/** The staging directory; the artifact's snapshot is in its `source/`. */
|
|
59
|
+
readonly stagingDir: string;
|
|
60
|
+
/** The entry, inside the snapshot. */
|
|
61
|
+
readonly entry: string;
|
|
62
|
+
/** Whether the entry has a default export: the page, or with `pages/` the layout. */
|
|
63
|
+
readonly entryHasDefault: boolean;
|
|
64
|
+
/** The artifact's manifest (with its title resolved). */
|
|
65
|
+
readonly manifest: ArtifactManifest;
|
|
66
|
+
/**
|
|
67
|
+
* The version being built (D89: known from the digests before the build). The page carries it
|
|
68
|
+
* (`<meta name="mostra-version">`), so a frame tells its shell which version it loaded (D98
|
|
69
|
+
* item 5). Same digests, same ID, so the build stays a function of its inputs.
|
|
70
|
+
*/
|
|
71
|
+
readonly versionId: string;
|
|
72
|
+
/** The `node_modules` the build links: the warm layout, or the artifact's lock layout (D95). */
|
|
73
|
+
readonly nodeModules: string;
|
|
74
|
+
/** Where the client build goes; a server build goes beside it in `server/`. */
|
|
75
|
+
readonly outDir: string;
|
|
76
|
+
/**
|
|
77
|
+
* Tests only (`MOSTRA_TEST_FAULTS=builder:build:hang`, issue #18): the child never answers,
|
|
78
|
+
* as a builder stuck in a plugin would, so the build limit or a kill ends it.
|
|
79
|
+
*/
|
|
80
|
+
readonly hang?: true;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The builder child's answer. */
|
|
84
|
+
export type BuildResponse =
|
|
85
|
+
| { readonly ok: true; readonly registry: VersionRegistry; readonly server: boolean }
|
|
86
|
+
| { readonly ok: false; readonly diagnostics: readonly Diagnostic[] };
|
|
87
|
+
|
|
88
|
+
/** The IPC method the supervisor calls on the builder child. */
|
|
89
|
+
export const BUILD_METHOD = "builder/build";
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* `mostra dev`'s child (D140 item 12): the supervisor asks the builder child for a Vite dev
|
|
93
|
+
* server over a mirror of the artifact's source, reached only through the dev sandbox's gateway.
|
|
94
|
+
*/
|
|
95
|
+
export const DEV_METHOD = "builder/dev";
|
|
96
|
+
/**
|
|
97
|
+
* Supervisor to child: these files of the mirror changed (`{ changes: DevChange[] }`). The child
|
|
98
|
+
* watches nothing itself: the supervisor, which keeps the mirror, says exactly what changed.
|
|
99
|
+
*/
|
|
100
|
+
export const DEV_CHANGED_METHOD = "builder/dev.changed";
|
|
101
|
+
|
|
102
|
+
/** One file of the mirror that changed, relative to its `source/`. */
|
|
103
|
+
export interface DevChange {
|
|
104
|
+
readonly file: string;
|
|
105
|
+
readonly kind: "add" | "change" | "unlink";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Supervisor to child: tell every page to load again (a new server bundle runs now). */
|
|
109
|
+
export const DEV_RELOAD_METHOD = "builder/dev.reload";
|
|
110
|
+
/** Child to supervisor: the server bundle was built again (or failed to build). */
|
|
111
|
+
export const DEV_SERVER_NOTIFICATION = "builder/dev.server";
|
|
112
|
+
/** Child to supervisor: the client modules of these files were updated in place (HMR). */
|
|
113
|
+
export const DEV_UPDATE_NOTIFICATION = "builder/dev.update";
|
|
114
|
+
|
|
115
|
+
/** What the dev child serves. */
|
|
116
|
+
export interface DevRequest {
|
|
117
|
+
/** The dev root: generated files, `node_modules`, and the mirror of the source in `source/`. */
|
|
118
|
+
readonly root: string;
|
|
119
|
+
readonly entry: string;
|
|
120
|
+
readonly entryHasDefault: boolean;
|
|
121
|
+
readonly manifest: ArtifactManifest;
|
|
122
|
+
/** The dev version's ID, in every page's `mostra-version` meta (D115). */
|
|
123
|
+
readonly versionId: string;
|
|
124
|
+
readonly nodeModules: string;
|
|
125
|
+
/** Where the server bundle and `registry.json` go: the dev version's directory. */
|
|
126
|
+
readonly outDir: string;
|
|
127
|
+
/** The secret every request from the gateway carries; nothing else reaches the dev server. */
|
|
128
|
+
readonly secret: string;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The header that carries {@link DevRequest.secret}. */
|
|
132
|
+
export const DEV_SECRET_HEADER = "x-mostra-dev-secret";
|
|
133
|
+
|
|
134
|
+
/** A server build of the dev child: what `registry.json` now holds, or why it failed. */
|
|
135
|
+
export type DevServerBuild =
|
|
136
|
+
| { readonly ok: true; readonly registry: VersionRegistry; readonly server: boolean }
|
|
137
|
+
| { readonly ok: false; readonly diagnostics: readonly Diagnostic[] };
|
|
138
|
+
|
|
139
|
+
/** The dev child's answer to {@link DEV_METHOD}: its loopback port and its first server build. */
|
|
140
|
+
export interface DevReady {
|
|
141
|
+
readonly port: number;
|
|
142
|
+
readonly build: DevServerBuild;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The files written into the staging root so the bundler's upward searches stop there instead of
|
|
147
|
+
* reaching directories above the home (VR-01): a `tsconfig.json` with the compile settings Mostra
|
|
148
|
+
* chooses (an artifact's own `tsconfig.json`, part of its source, still applies to its files),
|
|
149
|
+
* and a `package.json` that marks the build as ES modules. Their contents are part of
|
|
150
|
+
* `buildConfigDigest`.
|
|
151
|
+
*/
|
|
152
|
+
export const STAGING_FILES: Readonly<Record<string, string>> = {
|
|
153
|
+
"tsconfig.json": `${JSON.stringify(
|
|
154
|
+
{
|
|
155
|
+
compilerOptions: {
|
|
156
|
+
target: "ES2022",
|
|
157
|
+
useDefineForClassFields: true,
|
|
158
|
+
jsx: "react-jsx",
|
|
159
|
+
jsxImportSource: "react",
|
|
160
|
+
experimentalDecorators: false,
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
null,
|
|
164
|
+
2,
|
|
165
|
+
)}\n`,
|
|
166
|
+
"package.json": `${JSON.stringify({ private: true, type: "module" }, null, 2)}\n`,
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The declaration that types `useLive` from `live.ts` (D56): client code reads the source types
|
|
171
|
+
* through `typeof import`, never by importing server code at run time. Written beside the build's
|
|
172
|
+
* other generated files for the type check, never into the artifact's source.
|
|
173
|
+
*/
|
|
174
|
+
export const LIVE_DECLARATION: string = `// Generated by Mostra: the types of this artifact's live sources (D56).
|
|
175
|
+
import type { LiveValuesOf } from "@mostra/react";
|
|
176
|
+
|
|
177
|
+
declare module "@mostra/react" {
|
|
178
|
+
interface LiveSources extends LiveValuesOf<typeof import("../source/live")> {}
|
|
179
|
+
}
|
|
180
|
+
`;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The install child's entry (`dist/deps/child/install.js`, D95, DP-05): the process the process
|
|
3
|
+
* manager starts for every installer run, in the artifact's source directory with the artifact
|
|
4
|
+
* environment. It connects to its supervisor over IPC, so that when the supervisor exits or is
|
|
5
|
+
* killed, its whole process group ends with it (FAILURE_MODEL §2): pnpm, and every lifecycle
|
|
6
|
+
* script pnpm starts, run in that group. Its arguments are pnpm's entry and pnpm's arguments; pnpm
|
|
7
|
+
* runs as its child with the same output, and its exit code is the install child's.
|
|
8
|
+
*/
|
|
9
|
+
import { spawn } from "node:child_process";
|
|
10
|
+
import { connectToSupervisor } from "../../processes/index.ts";
|
|
11
|
+
|
|
12
|
+
connectToSupervisor();
|
|
13
|
+
|
|
14
|
+
const [entry, ...args] = process.argv.slice(2);
|
|
15
|
+
if (entry === undefined) {
|
|
16
|
+
console.error("The install child needs the installer's entry as its first argument.");
|
|
17
|
+
process.exit(2);
|
|
18
|
+
}
|
|
19
|
+
// Not detached: pnpm stays in this child's process group, which the supervisor and the disconnect
|
|
20
|
+
// rule both end as a whole.
|
|
21
|
+
const installer = spawn(process.execPath, [entry, ...args], {
|
|
22
|
+
stdio: ["ignore", "inherit", "inherit"],
|
|
23
|
+
});
|
|
24
|
+
installer.on("error", (error) => {
|
|
25
|
+
console.error(`The installer couldn't start: ${error.message}`);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
});
|
|
28
|
+
installer.on("exit", (code, signal) => {
|
|
29
|
+
process.exit(code ?? (signal ? 1 : 0));
|
|
30
|
+
});
|