@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,371 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The type check (D138 item 8, as D139 amends it): TypeScript 7 `tsc --noEmit` over an artifact's snapshot, in a
|
|
3
|
+
* child the process manager starts and records, beside the build and inside its 180 s. Imports
|
|
4
|
+
* resolve as the build resolves them: the same `node_modules` layout (the warm set or the lock
|
|
5
|
+
* layout), `@/*` at the snapshot's root, `@mostra/react`'s and `@mostra/server`'s types and the
|
|
6
|
+
* generated live-source declaration. It judges what the build compiles: the build's compile
|
|
7
|
+
* settings first, the artifact's own `tsconfig.json` over them, then the options Mostra forces;
|
|
8
|
+
* imports are declared by Vite's own client types. MDX isn't checked.
|
|
9
|
+
*
|
|
10
|
+
* The check runs in a directory of its own (a copy of the snapshot with the layout linked beside
|
|
11
|
+
* it), so it never races the builder, which links its own `node_modules` into the staging root.
|
|
12
|
+
*/
|
|
13
|
+
import { createHash } from "node:crypto";
|
|
14
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
15
|
+
import { cp, mkdir, rm, symlink, writeFile } from "node:fs/promises";
|
|
16
|
+
import { createRequire } from "node:module";
|
|
17
|
+
import { dirname, join } from "node:path";
|
|
18
|
+
import { createDiagnostic, type Diagnostic } from "@mostra/protocol";
|
|
19
|
+
import { LIVE_DECLARATION } from "../builder/index.ts";
|
|
20
|
+
import { runtimeRoot } from "../deps/index.ts";
|
|
21
|
+
import type { Environment, Log } from "../log.ts";
|
|
22
|
+
import type { ProcessManager } from "../processes/index.ts";
|
|
23
|
+
|
|
24
|
+
/** How long the check may take: the build's own limit, which it runs beside (FAILURE_MODEL §9). */
|
|
25
|
+
export const TYPECHECK_TIMEOUT_MS = 180_000;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* How the build compiles, which every check starts from, under an artifact's own `tsconfig.json`
|
|
29
|
+
* too: its options override these (Opus review finding 2: without them a `tsconfig.json` that
|
|
30
|
+
* sets only `strict` failed every JSX file the build compiles).
|
|
31
|
+
*/
|
|
32
|
+
const BUILD_OPTIONS = {
|
|
33
|
+
target: "ES2022",
|
|
34
|
+
lib: ["ES2023", "DOM", "DOM.Iterable"],
|
|
35
|
+
jsx: "react-jsx",
|
|
36
|
+
module: "ESNext",
|
|
37
|
+
isolatedModules: true,
|
|
38
|
+
allowImportingTsExtensions: true,
|
|
39
|
+
resolveJsonModule: true,
|
|
40
|
+
allowJs: true,
|
|
41
|
+
checkJs: false,
|
|
42
|
+
// Server code runs in Node (D18): its modules see Node's types.
|
|
43
|
+
types: ["node"],
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** What an artifact without a `tsconfig.json` is checked with besides (D138 item 8). */
|
|
47
|
+
const DEFAULT_OPTIONS = {
|
|
48
|
+
strict: true,
|
|
49
|
+
noUnusedLocals: false,
|
|
50
|
+
noUnusedParameters: false,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The options Mostra's check always sets, over an artifact's own `tsconfig.json` too. `baseUrl`
|
|
55
|
+
* is cleared: the build doesn't use it and TypeScript 7 refuses it (TS5102), so a configuration
|
|
56
|
+
* copied from a TypeScript 5 project (shadcn's Vite setup has one) checks like the build builds.
|
|
57
|
+
*/
|
|
58
|
+
const FORCED_OPTIONS = {
|
|
59
|
+
noEmit: true,
|
|
60
|
+
skipLibCheck: true,
|
|
61
|
+
// The build is a bundler: it resolves and emits modules as one, whatever `module` says.
|
|
62
|
+
module: "ESNext",
|
|
63
|
+
moduleResolution: "bundler",
|
|
64
|
+
baseUrl: null,
|
|
65
|
+
paths: { "@/*": ["./source/*"] },
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** Vite's own declarations of what imports are (assets, CSS modules, `?raw`, `import.meta`). */
|
|
69
|
+
function viteClientTypes(): { readonly path: string; readonly version: string } | undefined {
|
|
70
|
+
try {
|
|
71
|
+
const require = createRequire(import.meta.url);
|
|
72
|
+
const manifest = require.resolve("vite/package.json");
|
|
73
|
+
const version = (JSON.parse(readFileSync(manifest, "utf8")) as { version: string }).version;
|
|
74
|
+
const path = join(dirname(manifest), "client.d.ts");
|
|
75
|
+
return existsSync(path) ? { path, version } : undefined;
|
|
76
|
+
} catch {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* What the files a build bundles are, for the checker: Vite's own client declarations for the
|
|
83
|
+
* pinned release (every asset type, CSS modules as class maps, `?raw`, `?url`, `?inline`,
|
|
84
|
+
* `?worker`, `import.meta.env`, `.glob` and `.hot`; Opus review finding 2), and MDX, which
|
|
85
|
+
* Mostra's build adds.
|
|
86
|
+
*/
|
|
87
|
+
const VITE_CLIENT = viteClientTypes();
|
|
88
|
+
const AMBIENT = `// Written by Mostra for verify's type check (D138): what the build's imports are.
|
|
89
|
+
${VITE_CLIENT ? `/// <reference path=${JSON.stringify(VITE_CLIENT.path)} />\n` : ""}declare module "*.mdx" {
|
|
90
|
+
const Content: (props: Record<string, unknown>) => unknown;
|
|
91
|
+
export default Content;
|
|
92
|
+
export const frontmatter: Record<string, unknown>;
|
|
93
|
+
}
|
|
94
|
+
`;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The check configuration's revision: its text, so a change to what verify checks never reuses a
|
|
98
|
+
* verdict made under the old rules (D138 items 5 and 8).
|
|
99
|
+
*/
|
|
100
|
+
export const TYPECHECK_REVISION: string = createHash("sha256")
|
|
101
|
+
.update(
|
|
102
|
+
JSON.stringify({
|
|
103
|
+
BUILD_OPTIONS,
|
|
104
|
+
DEFAULT_OPTIONS,
|
|
105
|
+
FORCED_OPTIONS,
|
|
106
|
+
// The declarations' text without this machine's path to them.
|
|
107
|
+
AMBIENT: AMBIENT.replace(VITE_CLIENT?.path ?? "\u0000", "vite/client.d.ts"),
|
|
108
|
+
vite: VITE_CLIENT?.version ?? null,
|
|
109
|
+
revision: 2,
|
|
110
|
+
}),
|
|
111
|
+
)
|
|
112
|
+
.digest("hex");
|
|
113
|
+
|
|
114
|
+
/** Where TypeScript 7's `tsc` is. */
|
|
115
|
+
function tscPath(): string {
|
|
116
|
+
const require = createRequire(import.meta.url);
|
|
117
|
+
return join(dirname(require.resolve("typescript/package.json")), "bin", "tsc");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** One line of `tsc --pretty false`: `file(line,col): error TS1234: message`. */
|
|
121
|
+
const LINE = /^(.+?)\((\d+),(\d+)\): error (TS\d+): (.*)$/;
|
|
122
|
+
/** A diagnostic without a file: `error TS5023: Unknown compiler option …`. */
|
|
123
|
+
const GLOBAL = /^error (TS\d+): (.*)$/;
|
|
124
|
+
/** The configuration files of a check: Mostra's, which extends the artifact's own. */
|
|
125
|
+
const CONFIG_FILES = new Set([
|
|
126
|
+
"tsconfig.verify.json",
|
|
127
|
+
".mostra-check/build.json",
|
|
128
|
+
"source/tsconfig.json",
|
|
129
|
+
]);
|
|
130
|
+
|
|
131
|
+
/** What the check found. */
|
|
132
|
+
export interface TypecheckOutcome {
|
|
133
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Turns `tsc`'s output into diagnostics: errors in the artifact's own files only, TS7016 as an
|
|
138
|
+
* untyped package (a warning), and a TS2307 for an import `alreadyReported` names dropped.
|
|
139
|
+
*/
|
|
140
|
+
export function parseTscOutput(
|
|
141
|
+
lines: readonly string[],
|
|
142
|
+
alreadyReported: (specifier: string) => boolean,
|
|
143
|
+
/** Whether the artifact has its own `tsconfig.json`, which Mostra's configuration extends. */
|
|
144
|
+
ownConfig = false,
|
|
145
|
+
): Diagnostic[] {
|
|
146
|
+
const found: { file: string; line: number; column: number; code: string; text: string }[] = [];
|
|
147
|
+
for (const line of lines) {
|
|
148
|
+
const match = LINE.exec(line);
|
|
149
|
+
const global = match ? undefined : GLOBAL.exec(line);
|
|
150
|
+
if (global) {
|
|
151
|
+
found.push({
|
|
152
|
+
file: "",
|
|
153
|
+
line: 1,
|
|
154
|
+
column: 1,
|
|
155
|
+
code: global[1] as string,
|
|
156
|
+
text: global[2] as string,
|
|
157
|
+
});
|
|
158
|
+
} else if (match) {
|
|
159
|
+
found.push({
|
|
160
|
+
file: match[1] as string,
|
|
161
|
+
line: Number(match[2]),
|
|
162
|
+
column: Number(match[3]),
|
|
163
|
+
code: match[4] as string,
|
|
164
|
+
text: match[5] as string,
|
|
165
|
+
});
|
|
166
|
+
} else if (/^\s+\S/.test(line) && found.length > 0) {
|
|
167
|
+
const last = found[found.length - 1] as (typeof found)[number];
|
|
168
|
+
if (last.text.length < 1500) last.text = `${last.text}\n${line.trim()}`;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const diagnostics: Diagnostic[] = [];
|
|
172
|
+
for (const entry of found) {
|
|
173
|
+
const file = entry.file.replace(/\\/g, "/");
|
|
174
|
+
// A configuration the compiler refuses means the source was never checked: an error, never
|
|
175
|
+
// a pass (it is the artifact's `tsconfig.json` against how Mostra builds, D138 item 8).
|
|
176
|
+
if (file === "" || CONFIG_FILES.has(file)) {
|
|
177
|
+
diagnostics.push(
|
|
178
|
+
createDiagnostic("MOSTRA_TYPE_ERROR", {
|
|
179
|
+
message: `The type check couldn't run with the artifact's tsconfig.json and Mostra's build settings (noEmit, skipLibCheck, moduleResolution "bundler", the @/* path): ${entry.code}: ${entry.text}`,
|
|
180
|
+
// The compiler reports options it inherits against the file that extends them, so an
|
|
181
|
+
// artifact with its own configuration is pointed at it.
|
|
182
|
+
...(file === "source/tsconfig.json"
|
|
183
|
+
? { location: { file: "tsconfig.json", line: entry.line, column: entry.column } }
|
|
184
|
+
: ownConfig
|
|
185
|
+
? { location: { file: "tsconfig.json", line: 1, column: 1 } }
|
|
186
|
+
: {}),
|
|
187
|
+
}),
|
|
188
|
+
);
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (!file.startsWith("source/") || file.includes("/node_modules/")) continue;
|
|
192
|
+
const location = { file: file.slice("source/".length), line: entry.line, column: entry.column };
|
|
193
|
+
if (entry.code === "TS7016") {
|
|
194
|
+
diagnostics.push(
|
|
195
|
+
createDiagnostic("MOSTRA_TYPE_UNTYPED_PACKAGE", {
|
|
196
|
+
message: `${entry.text} (${entry.code})`,
|
|
197
|
+
location,
|
|
198
|
+
}),
|
|
199
|
+
);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (entry.code === "TS2307") {
|
|
203
|
+
const specifier = /'([^']+)'/.exec(entry.text)?.[1];
|
|
204
|
+
if (specifier !== undefined && alreadyReported(specifier)) continue;
|
|
205
|
+
}
|
|
206
|
+
diagnostics.push(
|
|
207
|
+
createDiagnostic("MOSTRA_TYPE_ERROR", {
|
|
208
|
+
message: `${entry.code}: ${entry.text}`,
|
|
209
|
+
location,
|
|
210
|
+
}),
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
return diagnostics;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Where `@mostra/server`'s declarations are: the worker provides the package, so no layout has it (D129). */
|
|
217
|
+
function serverTypes(): string | undefined {
|
|
218
|
+
const root = runtimeRoot();
|
|
219
|
+
for (const candidate of [
|
|
220
|
+
join(root, "..", "server", "dist", "index.d.ts"),
|
|
221
|
+
join(root, "..", "server", "src", "index.ts"),
|
|
222
|
+
]) {
|
|
223
|
+
if (existsSync(candidate)) return candidate;
|
|
224
|
+
}
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Runs the type check of the snapshot in `sourceDir` against `nodeModules`. */
|
|
229
|
+
export async function typecheck(options: {
|
|
230
|
+
readonly processes: ProcessManager;
|
|
231
|
+
readonly log: Log;
|
|
232
|
+
readonly env: Environment;
|
|
233
|
+
/** The snapshot (staging's `source/`). */
|
|
234
|
+
readonly sourceDir: string;
|
|
235
|
+
/** A fresh directory the check may use, removed afterwards. */
|
|
236
|
+
readonly workDir: string;
|
|
237
|
+
/** The layout the build links: the warm set or the artifact's lock layout. */
|
|
238
|
+
readonly nodeModules: string;
|
|
239
|
+
/** Whether the artifact has `live.ts` (D56). */
|
|
240
|
+
readonly hasLive: boolean;
|
|
241
|
+
/** Whether a TS2307 for this import repeats what the build or dependency stage said. */
|
|
242
|
+
readonly alreadyReported: (specifier: string) => boolean;
|
|
243
|
+
readonly timeoutMs?: number;
|
|
244
|
+
}): Promise<TypecheckOutcome> {
|
|
245
|
+
const { workDir } = options;
|
|
246
|
+
await rm(workDir, { recursive: true, force: true });
|
|
247
|
+
await mkdir(join(workDir, ".mostra-check"), { recursive: true });
|
|
248
|
+
try {
|
|
249
|
+
await cp(options.sourceDir, join(workDir, "source"), { recursive: true });
|
|
250
|
+
await symlink(options.nodeModules, join(workDir, "node_modules"), "dir");
|
|
251
|
+
await writeFile(join(workDir, ".mostra-check", "assets.d.ts"), AMBIENT);
|
|
252
|
+
if (options.hasLive)
|
|
253
|
+
await writeFile(join(workDir, ".mostra-check", "mostra-env.d.ts"), LIVE_DECLARATION);
|
|
254
|
+
const own = existsSync(join(workDir, "source", "tsconfig.json"));
|
|
255
|
+
const server = serverTypes();
|
|
256
|
+
const forced = {
|
|
257
|
+
...FORCED_OPTIONS,
|
|
258
|
+
paths: { ...FORCED_OPTIONS.paths, ...(server ? { "@mostra/server": [server] } : {}) },
|
|
259
|
+
};
|
|
260
|
+
// The build's options first, the artifact's own configuration over them, Mostra's forced
|
|
261
|
+
// ones last (an `extends` array applies in order).
|
|
262
|
+
await writeFile(
|
|
263
|
+
join(workDir, ".mostra-check", "build.json"),
|
|
264
|
+
`${JSON.stringify({ compilerOptions: own ? BUILD_OPTIONS : { ...BUILD_OPTIONS, ...DEFAULT_OPTIONS } }, null, 2)}\n`,
|
|
265
|
+
);
|
|
266
|
+
const config = {
|
|
267
|
+
extends: own
|
|
268
|
+
? ["./.mostra-check/build.json", "./source/tsconfig.json"]
|
|
269
|
+
: "./.mostra-check/build.json",
|
|
270
|
+
compilerOptions: forced,
|
|
271
|
+
include: ["source/**/*.ts", "source/**/*.tsx", ".mostra-check/*.d.ts"],
|
|
272
|
+
exclude: ["source/**/node_modules"],
|
|
273
|
+
};
|
|
274
|
+
await writeFile(join(workDir, "tsconfig.verify.json"), `${JSON.stringify(config, null, 2)}\n`);
|
|
275
|
+
const lines: string[] = [];
|
|
276
|
+
const child = await options.processes.spawn({
|
|
277
|
+
kind: "typecheck",
|
|
278
|
+
ownerRef: workDir,
|
|
279
|
+
command: process.execPath,
|
|
280
|
+
args: [tscPath(), "-p", "tsconfig.verify.json", "--noEmit", "--pretty", "false"],
|
|
281
|
+
cwd: workDir,
|
|
282
|
+
env: {
|
|
283
|
+
PATH: options.env["PATH"] ?? "/usr/bin:/bin",
|
|
284
|
+
HOME: options.env["HOME"] ?? "/",
|
|
285
|
+
...(options.env["TMPDIR"] ? { TMPDIR: options.env["TMPDIR"] } : {}),
|
|
286
|
+
},
|
|
287
|
+
onStdout: (line) => {
|
|
288
|
+
if (lines.length < 5000) lines.push(line);
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
const timeoutMs = options.timeoutMs ?? TYPECHECK_TIMEOUT_MS;
|
|
292
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
293
|
+
const exit = await Promise.race([
|
|
294
|
+
child.exited,
|
|
295
|
+
new Promise<"timeout">((done) => {
|
|
296
|
+
timer = setTimeout(() => done("timeout"), timeoutMs);
|
|
297
|
+
}),
|
|
298
|
+
]);
|
|
299
|
+
if (timer) clearTimeout(timer);
|
|
300
|
+
if (exit === "timeout") {
|
|
301
|
+
await child.stop(2000);
|
|
302
|
+
options.log.warn("verify.typecheck_timeout", { timeoutMs });
|
|
303
|
+
return {
|
|
304
|
+
diagnostics: [
|
|
305
|
+
createDiagnostic("MOSTRA_VERIFY_TIMEOUT", {
|
|
306
|
+
message: `The type check took longer than ${Math.round(timeoutMs / 1000)} s and was stopped.`,
|
|
307
|
+
}),
|
|
308
|
+
],
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
const diagnostics = parseTscOutput(lines, options.alreadyReported, own);
|
|
312
|
+
return { diagnostics: [...diagnostics, ...unexplainedExit(exit, lines, diagnostics)] };
|
|
313
|
+
} finally {
|
|
314
|
+
await rm(workDir, { recursive: true, force: true });
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** Whether the build's diagnostics already name a module the type check couldn't find. */
|
|
319
|
+
export function reportedByBuild(diagnostics: readonly Diagnostic[], specifier: string): boolean {
|
|
320
|
+
return diagnostics.some(
|
|
321
|
+
(diagnostic) =>
|
|
322
|
+
diagnostic.message.includes(`"${specifier}"`) ||
|
|
323
|
+
diagnostic.message.includes(`'${specifier}'`) ||
|
|
324
|
+
diagnostic.message.includes(`\`${specifier}\``),
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* What a `tsc` exit says beyond the diagnostics it printed: a signal is a crash of the checker
|
|
330
|
+
* (never cached), and a failing exit with no error found means the source wasn't checked, which
|
|
331
|
+
* fails closed (an error) instead of passing.
|
|
332
|
+
*/
|
|
333
|
+
export function unexplainedExit(
|
|
334
|
+
exit: {
|
|
335
|
+
readonly code: number | null;
|
|
336
|
+
readonly signal: string | null;
|
|
337
|
+
readonly stderrTail: readonly string[];
|
|
338
|
+
},
|
|
339
|
+
lines: readonly string[],
|
|
340
|
+
found: readonly Diagnostic[],
|
|
341
|
+
): Diagnostic[] {
|
|
342
|
+
const tail = [...lines, ...exit.stderrTail]
|
|
343
|
+
.filter((line) => line.trim() !== "")
|
|
344
|
+
.slice(-6)
|
|
345
|
+
.join("\n");
|
|
346
|
+
if (exit.signal !== null) {
|
|
347
|
+
return [
|
|
348
|
+
createDiagnostic("MOSTRA_VERIFY_CRASHED", {
|
|
349
|
+
message: `The type checker stopped (${exit.signal}) before it finished.${tail ? `\n${tail}` : ""}`,
|
|
350
|
+
}),
|
|
351
|
+
];
|
|
352
|
+
}
|
|
353
|
+
// An exit is explained by any error `tsc` printed in its own format, whatever the check made of
|
|
354
|
+
// it: TS7016 becomes the warning MOSTRA_TYPE_UNTYPED_PACKAGE, a TS2307 the build already
|
|
355
|
+
// reported is dropped, and errors outside the artifact's own files are dropped (D138 item 8).
|
|
356
|
+
// Requiring an error-severity diagnostic instead failed every artifact that imports an untyped
|
|
357
|
+
// package (found by WP-11's bundle smoke test, an on-demand `ms`).
|
|
358
|
+
const printedErrors = lines.some((line) => LINE.test(line) || GLOBAL.test(line));
|
|
359
|
+
if (
|
|
360
|
+
exit.code !== 0 &&
|
|
361
|
+
!printedErrors &&
|
|
362
|
+
!found.some((diagnostic) => diagnostic.severity === "error")
|
|
363
|
+
) {
|
|
364
|
+
return [
|
|
365
|
+
createDiagnostic("MOSTRA_TYPE_ERROR", {
|
|
366
|
+
message: `The type check exited with code ${exit.code} without checking the source${tail ? `:\n${tail}` : "."}`,
|
|
367
|
+
}),
|
|
368
|
+
];
|
|
369
|
+
}
|
|
370
|
+
return [];
|
|
371
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// The workers module (RT-03, RT-04, D62, D124): the launcher seam and the pool that runs one worker
|
|
2
|
+
// per active artifact version with server code.
|
|
3
|
+
export {
|
|
4
|
+
localLauncher,
|
|
5
|
+
locateWorker,
|
|
6
|
+
socketLauncher,
|
|
7
|
+
type WorkerLauncher,
|
|
8
|
+
} from "./launcher.ts";
|
|
9
|
+
export {
|
|
10
|
+
createWorkerPool,
|
|
11
|
+
type WorkerBridge,
|
|
12
|
+
type WorkerCrash,
|
|
13
|
+
type WorkerHandle,
|
|
14
|
+
type WorkerPool,
|
|
15
|
+
type WorkerVersion,
|
|
16
|
+
workerLimits,
|
|
17
|
+
} from "./pool.ts";
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Launching artifact workers (D124): the `WorkerLauncher` seam that becomes the remote server's
|
|
3
|
+
* `SandboxPort`. A launch names the version by reference (its ID, output digest and a locator),
|
|
4
|
+
* never by a directory path, and the channel it returns carries only serializable messages, so a
|
|
5
|
+
* launcher whose worker runs as a separate process over a socket, or on another machine, fits
|
|
6
|
+
* without a change. Secrets never travel in `env`: a worker asks for them by name (D44).
|
|
7
|
+
*/
|
|
8
|
+
import { connect } from "node:net";
|
|
9
|
+
import { resolve } from "node:path";
|
|
10
|
+
import type { Duplex } from "node:stream";
|
|
11
|
+
import {
|
|
12
|
+
encodeLine,
|
|
13
|
+
type JsonRpcMessage,
|
|
14
|
+
LineSplitter,
|
|
15
|
+
type WorkerLimits,
|
|
16
|
+
type WorkerVersionRef,
|
|
17
|
+
} from "@mostra/protocol";
|
|
18
|
+
import type { Environment } from "../log.ts";
|
|
19
|
+
import type { ProcessManager } from "../processes/index.ts";
|
|
20
|
+
|
|
21
|
+
/** How a worker ended. */
|
|
22
|
+
export interface WorkerExit {
|
|
23
|
+
readonly code: number | null;
|
|
24
|
+
readonly signal: string | null;
|
|
25
|
+
/** Its last stderr lines, redacted. */
|
|
26
|
+
readonly stderrTail: readonly string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** What a launcher starts. */
|
|
30
|
+
export interface WorkerLaunchSpec {
|
|
31
|
+
readonly artifactId: string;
|
|
32
|
+
readonly version: WorkerVersionRef;
|
|
33
|
+
/** The worker's environment: never a secret (D124). */
|
|
34
|
+
readonly env: Environment;
|
|
35
|
+
readonly limits: WorkerLimits;
|
|
36
|
+
/**
|
|
37
|
+
* The artifact's source directory on the owner's machine, when it still exists: the working
|
|
38
|
+
* directory of server code and the root of `watch` globs. A launcher elsewhere ignores it.
|
|
39
|
+
*/
|
|
40
|
+
readonly sourceDir?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A started worker's connection: JSON-RPC messages both ways, and how it ends. */
|
|
44
|
+
export interface WorkerChannel {
|
|
45
|
+
/** Sends one message to the worker. Does nothing once it has ended. */
|
|
46
|
+
send(message: JsonRpcMessage): void;
|
|
47
|
+
/** Receives every message from the worker; set once, before the first message is read. */
|
|
48
|
+
onMessage(listener: (message: unknown) => void): void;
|
|
49
|
+
/** Resolves once the worker has ended, for whatever reason. */
|
|
50
|
+
readonly ended: Promise<WorkerExit>;
|
|
51
|
+
/** Ends the worker now (SIGKILL locally). */
|
|
52
|
+
kill(): void;
|
|
53
|
+
/** The worker's process, when it runs on this machine. */
|
|
54
|
+
readonly pid?: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Starts workers (D124). */
|
|
58
|
+
export interface WorkerLauncher {
|
|
59
|
+
launch(spec: WorkerLaunchSpec): Promise<WorkerChannel>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Reads newline-delimited JSON from `stream` into `deliver`. */
|
|
63
|
+
function readLines(stream: Duplex, deliver: (message: unknown) => void): void {
|
|
64
|
+
const splitter = new LineSplitter();
|
|
65
|
+
stream.setEncoding("utf8");
|
|
66
|
+
stream.on("data", (chunk: string) => {
|
|
67
|
+
let lines: string[];
|
|
68
|
+
try {
|
|
69
|
+
lines = splitter.push(chunk);
|
|
70
|
+
} catch {
|
|
71
|
+
stream.destroy();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
for (const line of lines) {
|
|
75
|
+
if (line.trim() === "") continue;
|
|
76
|
+
try {
|
|
77
|
+
deliver(JSON.parse(line));
|
|
78
|
+
} catch {
|
|
79
|
+
// A worker that writes garbage is answered by nothing; its calls time out or it is killed.
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Options for {@link localLauncher}. */
|
|
86
|
+
export interface LocalLauncherOptions {
|
|
87
|
+
readonly processes: ProcessManager;
|
|
88
|
+
/** The home's real path, which `home:` locators resolve inside. */
|
|
89
|
+
readonly home: string;
|
|
90
|
+
/** The worker's process entry (`@mostra/worker`'s `dist/main.js`). */
|
|
91
|
+
readonly workerEntry: string;
|
|
92
|
+
/** Extra Node flags, such as `--conditions` in tests. */
|
|
93
|
+
readonly nodeArgs?: readonly string[];
|
|
94
|
+
/**
|
|
95
|
+
* The `node_modules` the version's packages resolve from (D135), made ready before the worker
|
|
96
|
+
* starts; without it, the server bundle's package imports have nothing to resolve from.
|
|
97
|
+
*/
|
|
98
|
+
readonly modules?: (target: {
|
|
99
|
+
readonly artifactId: string;
|
|
100
|
+
readonly versionDir: string;
|
|
101
|
+
readonly sourceDir?: string;
|
|
102
|
+
}) => Promise<string>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The local launcher: resolves a `home:` locator to the version's directory in this home and
|
|
107
|
+
* forks the worker as a child of the process manager, in its own process group, with its channel
|
|
108
|
+
* as fd 4 and the heap limit as a V8 flag.
|
|
109
|
+
*/
|
|
110
|
+
export function localLauncher(options: LocalLauncherOptions): WorkerLauncher {
|
|
111
|
+
return {
|
|
112
|
+
async launch(spec) {
|
|
113
|
+
if (!spec.version.locator.startsWith("home:")) {
|
|
114
|
+
throw new Error(`The local launcher can't reach ${spec.version.locator}.`);
|
|
115
|
+
}
|
|
116
|
+
const versionDir = resolve(options.home, spec.version.locator.slice("home:".length));
|
|
117
|
+
// A published version, a build a verify run made and serves from its sandbox (D138), or a
|
|
118
|
+
// `mostra dev` session's server bundle (D140 item 12).
|
|
119
|
+
const roots = [
|
|
120
|
+
resolve(options.home, "versions"),
|
|
121
|
+
resolve(options.home, "cache", "builds"),
|
|
122
|
+
resolve(options.home, "cache", "dev"),
|
|
123
|
+
];
|
|
124
|
+
if (!roots.some((root) => versionDir.startsWith(`${root}/`))) {
|
|
125
|
+
throw new Error(
|
|
126
|
+
"A home: locator names a version directory, a verify build or a dev session of the home.",
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
const modules = await options.modules?.({
|
|
130
|
+
artifactId: spec.artifactId,
|
|
131
|
+
versionDir,
|
|
132
|
+
...(spec.sourceDir ? { sourceDir: spec.sourceDir } : {}),
|
|
133
|
+
});
|
|
134
|
+
const child = await options.processes.spawn({
|
|
135
|
+
kind: "worker",
|
|
136
|
+
ownerRef: spec.version.versionId,
|
|
137
|
+
command: process.execPath,
|
|
138
|
+
args: [
|
|
139
|
+
`--max-old-space-size=${spec.limits.heapMb}`,
|
|
140
|
+
...(options.nodeArgs ?? []),
|
|
141
|
+
options.workerEntry,
|
|
142
|
+
"--channel",
|
|
143
|
+
"fd:4",
|
|
144
|
+
"--version-dir",
|
|
145
|
+
versionDir,
|
|
146
|
+
...(spec.sourceDir ? ["--source-dir", spec.sourceDir] : []),
|
|
147
|
+
...(modules ? ["--modules", modules] : []),
|
|
148
|
+
],
|
|
149
|
+
cwd: spec.sourceDir ?? versionDir,
|
|
150
|
+
env: spec.env,
|
|
151
|
+
channel: true,
|
|
152
|
+
});
|
|
153
|
+
const stream = child.channel;
|
|
154
|
+
if (!stream) throw new Error("The worker started without its channel.");
|
|
155
|
+
let ended = false;
|
|
156
|
+
void child.exited.then(() => {
|
|
157
|
+
ended = true;
|
|
158
|
+
});
|
|
159
|
+
return {
|
|
160
|
+
pid: child.pid,
|
|
161
|
+
send(message) {
|
|
162
|
+
if (ended || stream.destroyed) return;
|
|
163
|
+
stream.write(encodeLine(message));
|
|
164
|
+
},
|
|
165
|
+
onMessage(listener) {
|
|
166
|
+
readLines(stream, listener);
|
|
167
|
+
},
|
|
168
|
+
ended: child.exited.then((exit) => ({
|
|
169
|
+
code: exit.code,
|
|
170
|
+
signal: exit.signal,
|
|
171
|
+
stderrTail: exit.stderrTail,
|
|
172
|
+
})),
|
|
173
|
+
kill() {
|
|
174
|
+
void child.stop(0);
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The line a worker host sends after its worker ended, before it closes the connection. */
|
|
182
|
+
export interface WorkerHostExitLine {
|
|
183
|
+
readonly mostraWorkerExit: WorkerExit;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* A launcher whose workers run in a separately started worker host, reached over a Unix socket
|
|
188
|
+
* (D124's proof that the seam fits a worker elsewhere). Each launch opens one connection and sends
|
|
189
|
+
* the launch spec as its first line, without the source directory (the host has its own files);
|
|
190
|
+
* the host starts the worker, passes messages through, and ends the connection with a
|
|
191
|
+
* {@link WorkerHostExitLine} once the worker ended. Closing the connection ends the worker.
|
|
192
|
+
*/
|
|
193
|
+
export function socketLauncher(socketPath: string): WorkerLauncher {
|
|
194
|
+
return {
|
|
195
|
+
launch(spec) {
|
|
196
|
+
return new Promise<WorkerChannel>((resolveLaunch, rejectLaunch) => {
|
|
197
|
+
const socket = connect(socketPath);
|
|
198
|
+
let exit: WorkerExit | undefined;
|
|
199
|
+
let listener: ((message: unknown) => void) | undefined;
|
|
200
|
+
let resolveEnded!: (exit: WorkerExit) => void;
|
|
201
|
+
const ended = new Promise<WorkerExit>((done) => {
|
|
202
|
+
resolveEnded = done;
|
|
203
|
+
});
|
|
204
|
+
socket.once("error", rejectLaunch);
|
|
205
|
+
socket.once("connect", () => {
|
|
206
|
+
socket.off("error", rejectLaunch);
|
|
207
|
+
socket.on("error", () => socket.destroy());
|
|
208
|
+
const { sourceDir: _local, ...remote } = spec;
|
|
209
|
+
socket.write(`${JSON.stringify({ launch: remote })}\n`);
|
|
210
|
+
readLines(socket, (message) => {
|
|
211
|
+
const line = message as Partial<WorkerHostExitLine>;
|
|
212
|
+
if (line.mostraWorkerExit) exit = line.mostraWorkerExit;
|
|
213
|
+
else listener?.(message);
|
|
214
|
+
});
|
|
215
|
+
socket.once("close", () =>
|
|
216
|
+
resolveEnded(
|
|
217
|
+
exit ?? {
|
|
218
|
+
code: null,
|
|
219
|
+
signal: "SIGKILL",
|
|
220
|
+
stderrTail: ["The worker host closed the connection."],
|
|
221
|
+
},
|
|
222
|
+
),
|
|
223
|
+
);
|
|
224
|
+
resolveLaunch({
|
|
225
|
+
send(message) {
|
|
226
|
+
if (!socket.destroyed) socket.write(encodeLine(message));
|
|
227
|
+
},
|
|
228
|
+
onMessage(next) {
|
|
229
|
+
listener = next;
|
|
230
|
+
},
|
|
231
|
+
ended,
|
|
232
|
+
kill() {
|
|
233
|
+
socket.destroy();
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Where the worker's process entry is: `MOSTRA_WORKER_ENTRY` when set, else `@mostra/worker`
|
|
244
|
+
* beside this runtime (`worker/dist/main.js`; its source, run with the source condition, when
|
|
245
|
+
* this runtime runs from source too).
|
|
246
|
+
*/
|
|
247
|
+
export function locateWorker(
|
|
248
|
+
env: Environment,
|
|
249
|
+
runtimeRoot: string,
|
|
250
|
+
): { readonly entry: string; readonly nodeArgs: readonly string[] } {
|
|
251
|
+
const override = env["MOSTRA_WORKER_ENTRY"];
|
|
252
|
+
if (override) return { entry: override, nodeArgs: [] };
|
|
253
|
+
const fromSource = import.meta.url.endsWith(".ts");
|
|
254
|
+
return fromSource
|
|
255
|
+
? {
|
|
256
|
+
entry: resolve(runtimeRoot, "..", "worker", "src", "main.ts"),
|
|
257
|
+
nodeArgs: ["--conditions=@mostra/source"],
|
|
258
|
+
}
|
|
259
|
+
: { entry: resolve(runtimeRoot, "..", "worker", "dist", "main.js"), nodeArgs: [] };
|
|
260
|
+
}
|