@mostra/runtime 0.0.0-placeholder → 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 +38 -1
- 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 -4
- 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,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The install child (D94, D95, DP-05): pnpm 10 run by the process manager in the artifact's
|
|
3
|
+
* context, never inside the supervisor. Every run is one child, `node pnpm.cjs …`, started in the
|
|
4
|
+
* artifact's source directory with the artifact environment, against the home's store, metadata
|
|
5
|
+
* cache and state, and stopped after 300 s (FAILURE_MODEL §9). Its NDJSON report becomes the
|
|
6
|
+
* outcome: success, or one diagnostic from the DEP area.
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
9
|
+
import { writeFile } from "node:fs/promises";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { createDiagnostic, type Diagnostic } from "@mostra/protocol";
|
|
13
|
+
import type { Environment, Log } from "../log.ts";
|
|
14
|
+
import { artifactEnvironment, type ProcessManager } from "../processes/index.ts";
|
|
15
|
+
import { runtimeRoot } from "./warm.ts";
|
|
16
|
+
|
|
17
|
+
/** How long one install may take (FAILURE_MODEL §9). */
|
|
18
|
+
export const INSTALL_TIMEOUT_MS = 300_000;
|
|
19
|
+
|
|
20
|
+
/** The release age of an unpinned resolution, in minutes (D95): one day. */
|
|
21
|
+
export const MINIMUM_RELEASE_AGE_MINUTES = 1440;
|
|
22
|
+
|
|
23
|
+
/** Where the installer keeps its state inside the home (DATA_MODEL §1). */
|
|
24
|
+
export interface InstallerDirs {
|
|
25
|
+
/** `deps/store`: the shared content-addressed store. */
|
|
26
|
+
readonly store: string;
|
|
27
|
+
/** `deps/cache`: registry metadata, read when offline. */
|
|
28
|
+
readonly cache: string;
|
|
29
|
+
/** `deps/state`: the installer's own state. */
|
|
30
|
+
readonly state: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** One installer run. */
|
|
34
|
+
export interface InstallRun {
|
|
35
|
+
/** The pnpm command and its own arguments, such as `["install", "--frozen-lockfile"]`. */
|
|
36
|
+
readonly command: readonly string[];
|
|
37
|
+
/** The project directory pnpm works on (`--dir`). */
|
|
38
|
+
readonly dir: string;
|
|
39
|
+
/** The artifact's source directory: the child's working directory (DP-05). */
|
|
40
|
+
readonly cwd: string;
|
|
41
|
+
/** Use only what the store and the metadata cache hold. */
|
|
42
|
+
readonly offline: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Resolution applies the release age (D95), exempting packages named with an exact version.
|
|
45
|
+
* `false` for an install of a lockfile already resolved: its pins are exact and were checked
|
|
46
|
+
* when they were chosen.
|
|
47
|
+
*/
|
|
48
|
+
readonly releaseAge: { readonly exempt: readonly string[] } | false;
|
|
49
|
+
/** What the child serves, for `instance/status` and reaping. */
|
|
50
|
+
readonly ownerRef: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** What a run ended with. */
|
|
54
|
+
export type InstallOutcome =
|
|
55
|
+
| { readonly ok: true }
|
|
56
|
+
| { readonly ok: false; readonly diagnostic: Diagnostic };
|
|
57
|
+
|
|
58
|
+
/** The installer of one instance. */
|
|
59
|
+
export interface Installer {
|
|
60
|
+
readonly version: string;
|
|
61
|
+
run(run: InstallRun): Promise<InstallOutcome>;
|
|
62
|
+
/**
|
|
63
|
+
* `pnpm store prune` (gc, D140 item 14): removes what no layout links from the store. `dir` is
|
|
64
|
+
* an empty scratch directory the prune works in, and its registry metadata cache: pnpm's prune
|
|
65
|
+
* empties the metadata cache it is given, and the home's (`deps/cache`) is what offline
|
|
66
|
+
* resolution reads (D95), so it never sees that one. A store command takes no network flag.
|
|
67
|
+
*/
|
|
68
|
+
prune(input: { readonly dir: string; readonly ownerRef: string }): Promise<InstallOutcome>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Connection failures: the registry can't be reached at all, so Mostra is offline (D95). */
|
|
72
|
+
const UNREACHABLE =
|
|
73
|
+
/\b(?:ECONNREFUSED|ENOTFOUND|EAI_AGAIN|ENETUNREACH|EHOSTUNREACH|ENETDOWN|EHOSTDOWN|ECONNRESET|ETIMEDOUT|ESOCKETTIMEDOUT)\b/;
|
|
74
|
+
|
|
75
|
+
/** The package a pnpm error names, from its record or its message. */
|
|
76
|
+
function packageNamed(record: Record<string, unknown>, message: string): string | undefined {
|
|
77
|
+
const fromRecord = (record["package"] as { name?: unknown } | undefined)?.name;
|
|
78
|
+
if (typeof fromRecord === "string") return fromRecord;
|
|
79
|
+
const fromSpec = (record["spec"] as { name?: unknown } | undefined)?.name;
|
|
80
|
+
if (typeof fromSpec === "string") return fromSpec;
|
|
81
|
+
return /(?:for|resolve|GET \S+?\/)(@?[^\s@/:]+(?:\/[^\s@/:]+)?)/.exec(message)?.[1];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The install child's entry: the compiled one beside the compiled supervisor, else the source. */
|
|
85
|
+
function childEntry(): string {
|
|
86
|
+
return fileURLToPath(import.meta.url).endsWith(".ts")
|
|
87
|
+
? join(runtimeRoot(), "src/deps/child/install.ts")
|
|
88
|
+
: join(runtimeRoot(), "dist/deps/child/install.js");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Finds pnpm 10 beside the runtime; anything else is refused (D94). */
|
|
92
|
+
function locatePnpm(): { entry: string; version: string } {
|
|
93
|
+
let dir = runtimeRoot();
|
|
94
|
+
for (;;) {
|
|
95
|
+
const candidate = join(dir, "node_modules", "pnpm");
|
|
96
|
+
if (existsSync(join(candidate, "package.json"))) {
|
|
97
|
+
const real = realpathSync(candidate);
|
|
98
|
+
const { version } = JSON.parse(readFileSync(join(real, "package.json"), "utf8")) as {
|
|
99
|
+
version: string;
|
|
100
|
+
};
|
|
101
|
+
if (!version.startsWith("10.")) {
|
|
102
|
+
throw new Error(`The runtime found pnpm ${version}; its installer must be pnpm 10 (D94).`);
|
|
103
|
+
}
|
|
104
|
+
return { entry: join(real, "dist", "pnpm.cjs"), version };
|
|
105
|
+
}
|
|
106
|
+
const parent = dirname(dir);
|
|
107
|
+
if (parent === dir) throw new Error("The runtime is missing its installer, pnpm 10.");
|
|
108
|
+
dir = parent;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The project settings of a run: the release age, and the packages named exactly (D95). */
|
|
113
|
+
export function workspaceSettings(releaseAge: InstallRun["releaseAge"]): string {
|
|
114
|
+
if (releaseAge === false) return "minimumReleaseAge: 0\n";
|
|
115
|
+
const lines = [`minimumReleaseAge: ${MINIMUM_RELEASE_AGE_MINUTES}`];
|
|
116
|
+
if (releaseAge.exempt.length > 0) {
|
|
117
|
+
lines.push("minimumReleaseAgeExclude:");
|
|
118
|
+
for (const name of [...new Set(releaseAge.exempt)].sort()) {
|
|
119
|
+
lines.push(` - ${JSON.stringify(name)}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return `${lines.join("\n")}\n`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Options for {@link createInstaller}. */
|
|
126
|
+
export interface InstallerOptions {
|
|
127
|
+
readonly processes: ProcessManager;
|
|
128
|
+
readonly log: Log;
|
|
129
|
+
readonly dirs: InstallerDirs;
|
|
130
|
+
/** The supervisor's environment; the child gets the artifact environment derived from it. */
|
|
131
|
+
readonly env: Environment;
|
|
132
|
+
/** Default {@link INSTALL_TIMEOUT_MS}. */
|
|
133
|
+
readonly timeoutMs?: number;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Creates the installer. */
|
|
137
|
+
export function createInstaller(options: InstallerOptions): Installer {
|
|
138
|
+
const pnpm = locatePnpm();
|
|
139
|
+
const entry = childEntry();
|
|
140
|
+
const timeoutMs = options.timeoutMs ?? INSTALL_TIMEOUT_MS;
|
|
141
|
+
// The artifact environment passes no CI variable, which would switch pnpm to frozen installs on
|
|
142
|
+
// its own; every run says what it wants.
|
|
143
|
+
const env = artifactEnvironment(options.env);
|
|
144
|
+
|
|
145
|
+
/** The arguments every pnpm run takes; `network` is absent for a store command. */
|
|
146
|
+
const argsFor = (spec: {
|
|
147
|
+
readonly command: readonly string[];
|
|
148
|
+
readonly dir: string;
|
|
149
|
+
readonly cacheDir: string;
|
|
150
|
+
readonly network?: "--offline" | "--prefer-offline";
|
|
151
|
+
}): string[] => [
|
|
152
|
+
// The install child connects to the supervisor and runs pnpm in its own process group.
|
|
153
|
+
entry,
|
|
154
|
+
pnpm.entry,
|
|
155
|
+
...spec.command,
|
|
156
|
+
"--dir",
|
|
157
|
+
spec.dir,
|
|
158
|
+
`--config.store-dir=${options.dirs.store}`,
|
|
159
|
+
`--config.cache-dir=${spec.cacheDir}`,
|
|
160
|
+
`--config.state-dir=${options.dirs.state}`,
|
|
161
|
+
// Settings a user's .npmrc could change, which these layouts depend on.
|
|
162
|
+
"--config.lockfile=true",
|
|
163
|
+
"--config.modules-dir=node_modules",
|
|
164
|
+
"--config.virtual-store-dir=node_modules/.pnpm",
|
|
165
|
+
"--config.shamefully-hoist=false",
|
|
166
|
+
"--config.enable-global-virtual-store=false",
|
|
167
|
+
"--reporter=ndjson",
|
|
168
|
+
...(spec.network ? [spec.network] : []),
|
|
169
|
+
"--config.update-notifier=false",
|
|
170
|
+
// Every on-demand package's lifecycle scripts run, here in the install child (D95).
|
|
171
|
+
"--config.dangerously-allow-all-builds=true",
|
|
172
|
+
"--config.ignore-scripts=false",
|
|
173
|
+
"--config.auto-install-peers=true",
|
|
174
|
+
"--config.strict-peer-dependencies=false",
|
|
175
|
+
"--config.node-linker=isolated",
|
|
176
|
+
"--config.fetch-retries=2",
|
|
177
|
+
"--config.fetch-retry-mintimeout=1000",
|
|
178
|
+
"--config.fetch-retry-maxtimeout=8000",
|
|
179
|
+
];
|
|
180
|
+
|
|
181
|
+
/** Runs pnpm in an install child, bounded by the install limit. */
|
|
182
|
+
const execute = async (run: {
|
|
183
|
+
readonly args: readonly string[];
|
|
184
|
+
readonly dir: string;
|
|
185
|
+
readonly cwd: string;
|
|
186
|
+
readonly releaseAge: InstallRun["releaseAge"];
|
|
187
|
+
readonly ownerRef: string;
|
|
188
|
+
}): Promise<InstallOutcome> => {
|
|
189
|
+
const args = [...run.args];
|
|
190
|
+
// pnpm 10 reads the release age's exemptions only from the project's pnpm-workspace.yaml, and
|
|
191
|
+
// reads that file only when the project is its own workspace root (VERIFIED_FACTS, WP-04);
|
|
192
|
+
// being the root also stops pnpm's upward search for a workspace above the home.
|
|
193
|
+
await writeFile(join(run.dir, "pnpm-workspace.yaml"), workspaceSettings(run.releaseAge));
|
|
194
|
+
const errors: { code: string; message: string; record: Record<string, unknown> }[] = [];
|
|
195
|
+
const scriptLines: string[] = [];
|
|
196
|
+
const child = await options.processes.spawn({
|
|
197
|
+
kind: "install",
|
|
198
|
+
ownerRef: run.ownerRef,
|
|
199
|
+
command: process.execPath,
|
|
200
|
+
args,
|
|
201
|
+
cwd: run.cwd,
|
|
202
|
+
env,
|
|
203
|
+
// The IPC channel is the child's lifeline: when the supervisor dies, it ends its group.
|
|
204
|
+
ipc: {},
|
|
205
|
+
onStdout: (line) => {
|
|
206
|
+
let record: Record<string, unknown>;
|
|
207
|
+
try {
|
|
208
|
+
record = JSON.parse(line) as Record<string, unknown>;
|
|
209
|
+
} catch {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (record["name"] === "pnpm:lifecycle" && typeof record["line"] === "string") {
|
|
213
|
+
scriptLines.push(record["line"]);
|
|
214
|
+
if (scriptLines.length > 20) scriptLines.shift();
|
|
215
|
+
}
|
|
216
|
+
if (record["level"] !== "error") return;
|
|
217
|
+
const err = (record["err"] ?? {}) as { code?: unknown; message?: unknown };
|
|
218
|
+
const code = String(record["code"] ?? err.code ?? "");
|
|
219
|
+
const message = String(err.message ?? record["message"] ?? "");
|
|
220
|
+
errors.push({ code, message, record });
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
224
|
+
const outcome = await Promise.race([
|
|
225
|
+
child.exited.then((exit) => ({ kind: "exit" as const, exit })),
|
|
226
|
+
new Promise<{ kind: "timeout" }>((done) => {
|
|
227
|
+
timer = setTimeout(() => done({ kind: "timeout" }), timeoutMs);
|
|
228
|
+
}),
|
|
229
|
+
]);
|
|
230
|
+
clearTimeout(timer);
|
|
231
|
+
if (outcome.kind === "timeout") {
|
|
232
|
+
await child.stop(2000);
|
|
233
|
+
return {
|
|
234
|
+
ok: false,
|
|
235
|
+
diagnostic: createDiagnostic("MOSTRA_DEP_TIMEOUT", {
|
|
236
|
+
message: `Installing packages took longer than ${Math.round(timeoutMs / 1000)} s and was stopped.`,
|
|
237
|
+
}),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
if (outcome.exit.code === 0) return { ok: true };
|
|
241
|
+
options.log.warn("deps.install_failed", {
|
|
242
|
+
code: outcome.exit.code,
|
|
243
|
+
signal: outcome.exit.signal,
|
|
244
|
+
errors: errors.map(({ code, message }) => ({ code, message })),
|
|
245
|
+
});
|
|
246
|
+
return { ok: false, diagnostic: diagnose(errors, scriptLines, outcome.exit.stderrTail) };
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
version: pnpm.version,
|
|
251
|
+
run: (run) =>
|
|
252
|
+
execute({
|
|
253
|
+
args: argsFor({
|
|
254
|
+
command: run.command,
|
|
255
|
+
dir: run.dir,
|
|
256
|
+
cacheDir: options.dirs.cache,
|
|
257
|
+
network: run.offline ? "--offline" : "--prefer-offline",
|
|
258
|
+
}),
|
|
259
|
+
dir: run.dir,
|
|
260
|
+
cwd: run.cwd,
|
|
261
|
+
releaseAge: run.releaseAge,
|
|
262
|
+
ownerRef: run.ownerRef,
|
|
263
|
+
}),
|
|
264
|
+
prune: (input) =>
|
|
265
|
+
execute({
|
|
266
|
+
args: argsFor({
|
|
267
|
+
command: ["store", "prune"],
|
|
268
|
+
dir: input.dir,
|
|
269
|
+
cacheDir: join(input.dir, "metadata-cache"),
|
|
270
|
+
}),
|
|
271
|
+
dir: input.dir,
|
|
272
|
+
cwd: input.dir,
|
|
273
|
+
releaseAge: false,
|
|
274
|
+
ownerRef: input.ownerRef,
|
|
275
|
+
}),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Turns the installer's errors into one diagnostic. */
|
|
280
|
+
function diagnose(
|
|
281
|
+
errors: readonly { code: string; message: string; record: Record<string, unknown> }[],
|
|
282
|
+
scriptLines: readonly string[],
|
|
283
|
+
stderr: readonly string[],
|
|
284
|
+
): Diagnostic {
|
|
285
|
+
const first = errors[0];
|
|
286
|
+
const output = [...scriptLines, ...stderr].slice(-12).join("\n");
|
|
287
|
+
if (!first) {
|
|
288
|
+
return createDiagnostic("MOSTRA_DEP_INSTALL_FAILED", {
|
|
289
|
+
message: `The package installer failed.${output ? `\n${output}` : ""}`,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
const named = packageNamed(first.record, first.message);
|
|
293
|
+
const subject = named ? `the package ${named}` : "a package";
|
|
294
|
+
if (first.code === "ERR_PNPM_NO_OFFLINE_META" || first.code === "ERR_PNPM_NO_OFFLINE_TARBALL") {
|
|
295
|
+
return createDiagnostic("MOSTRA_DEP_OFFLINE", {
|
|
296
|
+
message: `Mostra is offline and the home's package store doesn't hold ${subject}.`,
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
if (UNREACHABLE.test(first.message)) {
|
|
300
|
+
return createDiagnostic("MOSTRA_DEP_OFFLINE", {
|
|
301
|
+
message: `The package registry can't be reached, so ${subject} can't be installed: ${first.message}`,
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
if (
|
|
305
|
+
first.code === "ERR_PNPM_FETCH_404" ||
|
|
306
|
+
first.code === "ERR_PNPM_NO_MATCHING_VERSION" ||
|
|
307
|
+
first.code === "ERR_PNPM_NO_MATURE_MATCHING_VERSION"
|
|
308
|
+
) {
|
|
309
|
+
// pnpm reports a range whose only matches are too new either way (D95's release age).
|
|
310
|
+
const mature =
|
|
311
|
+
first.code === "ERR_PNPM_FETCH_404"
|
|
312
|
+
? ""
|
|
313
|
+
: " Without an exact version, only releases at least a day old match; a release less than a day old installs when named exactly.";
|
|
314
|
+
return createDiagnostic("MOSTRA_DEP_NOT_FOUND", {
|
|
315
|
+
message: `The registry has no match for ${subject}: ${first.message}.${mature}`,
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
return createDiagnostic("MOSTRA_DEP_INSTALL_FAILED", {
|
|
319
|
+
message: `Installing ${subject} failed: ${first.message}${output ? `\n${output}` : ""}`,
|
|
320
|
+
});
|
|
321
|
+
}
|
package/src/deps/lock.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lockfile (`lock.yaml`, D20, D95): what a version was built with, frozen at publish, and the
|
|
3
|
+
* form of every artifact's working lockfile. WP-03's form lists the warm set; an artifact with
|
|
4
|
+
* on-demand packages adds `packages` (each direct one at its exact version) and `pnpm` (the
|
|
5
|
+
* installer's lockfile, which pins the whole graph). `lockDigest` is the SHA-256 of the text, so
|
|
6
|
+
* any change to either changes `envDigest` and the version (DP-03).
|
|
7
|
+
*/
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { parse } from "yaml";
|
|
10
|
+
|
|
11
|
+
/** A lockfile's content. */
|
|
12
|
+
export interface Lock {
|
|
13
|
+
/** Every warm package and its version. */
|
|
14
|
+
readonly warm: Readonly<Record<string, string>>;
|
|
15
|
+
/** Each direct on-demand package and its exact version; empty without any. */
|
|
16
|
+
readonly packages: Readonly<Record<string, string>>;
|
|
17
|
+
/** The installer's lockfile (pnpm's `pnpm-lock.yaml`), present exactly when `packages` isn't empty. */
|
|
18
|
+
readonly pnpm?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const HEADER = "# Mostra lockfile: the packages this version was built with. Frozen at publish.";
|
|
22
|
+
|
|
23
|
+
const sorted = (record: Readonly<Record<string, string>>): [string, string][] =>
|
|
24
|
+
Object.entries(record).sort(([a], [b]) => (a < b ? -1 : 1));
|
|
25
|
+
|
|
26
|
+
/** The canonical text of `lock`. */
|
|
27
|
+
export function lockText(lock: Lock): string {
|
|
28
|
+
const lines = [HEADER, "lockfileVersion: 1", "warm:"];
|
|
29
|
+
for (const [name, version] of sorted(lock.warm)) {
|
|
30
|
+
lines.push(` ${JSON.stringify(name)}: ${JSON.stringify(version)}`);
|
|
31
|
+
}
|
|
32
|
+
const packages = sorted(lock.packages);
|
|
33
|
+
if (packages.length > 0) {
|
|
34
|
+
if (lock.pnpm === undefined)
|
|
35
|
+
throw new Error("A lockfile with packages needs its pnpm lockfile.");
|
|
36
|
+
lines.push("packages:");
|
|
37
|
+
for (const [name, version] of packages) {
|
|
38
|
+
lines.push(` ${JSON.stringify(name)}: ${JSON.stringify(version)}`);
|
|
39
|
+
}
|
|
40
|
+
lines.push("pnpm: |");
|
|
41
|
+
const pnpm = lock.pnpm.replace(/\s+$/, "");
|
|
42
|
+
for (const line of pnpm.split("\n")) lines.push(line === "" ? "" : ` ${line}`);
|
|
43
|
+
}
|
|
44
|
+
const text = `${lines.join("\n")}\n`;
|
|
45
|
+
// The block scalar must hold the installer's lockfile exactly; anything else is a bug here.
|
|
46
|
+
const back = parseLock(text);
|
|
47
|
+
if (packages.length > 0 && back.pnpm !== `${lock.pnpm?.replace(/\s+$/, "")}\n`) {
|
|
48
|
+
throw new Error("The pnpm lockfile doesn't round-trip through lock.yaml.");
|
|
49
|
+
}
|
|
50
|
+
return text;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Reads a lockfile's text. */
|
|
54
|
+
export function parseLock(text: string): Lock {
|
|
55
|
+
const data = (parse(text) ?? {}) as {
|
|
56
|
+
warm?: Record<string, string> | null;
|
|
57
|
+
packages?: Record<string, string> | null;
|
|
58
|
+
pnpm?: string;
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
warm: data.warm ?? {},
|
|
62
|
+
packages: data.packages ?? {},
|
|
63
|
+
...(typeof data.pnpm === "string" ? { pnpm: data.pnpm } : {}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The SHA-256 of a lockfile's text, as lowercase hex. */
|
|
68
|
+
export function lockDigest(text: string): string {
|
|
69
|
+
return createHash("sha256").update(text).digest("hex");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Every package, direct or transitive, an installer lockfile pins: `{ name, version }`. */
|
|
73
|
+
export function pinnedPackages(pnpm: string): { name: string; version: string }[] {
|
|
74
|
+
const data = (parse(pnpm) ?? {}) as { packages?: Record<string, unknown> | null };
|
|
75
|
+
const result: { name: string; version: string }[] = [];
|
|
76
|
+
for (const key of Object.keys(data.packages ?? {})) {
|
|
77
|
+
const at = key.lastIndexOf("@");
|
|
78
|
+
if (at <= 0) continue;
|
|
79
|
+
result.push({ name: key.slice(0, at), version: key.slice(at + 1) });
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
package/src/deps/scan.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The static import scan (D95): which npm packages an artifact's snapshot imports, found by
|
|
3
|
+
* parsing, never by running anything (RT-02). It reads every script (static, dynamic and
|
|
4
|
+
* `require` imports, `export … from` and `@jsxImportSource` pragmas), the ESM blocks of MDX files, and the `@import`,
|
|
5
|
+
* `@plugin` and `@reference` rules of CSS files. Type-only imports are skipped: the build erases
|
|
6
|
+
* them. What the scan misses (a computed `import()`, say) still meets the build, which refuses a
|
|
7
|
+
* package outside the warm set and the lockfile, and `deps/add` adds it explicitly.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
11
|
+
import { createRequire } from "node:module";
|
|
12
|
+
import { extname, join } from "node:path";
|
|
13
|
+
import type * as Oxc from "oxc-parser";
|
|
14
|
+
import { packageOf } from "./specifier.ts";
|
|
15
|
+
|
|
16
|
+
let oxc: typeof Oxc | undefined;
|
|
17
|
+
/** Loads the native parser on first use, so the platform gate answers first (RT-07). */
|
|
18
|
+
function parser(): typeof Oxc {
|
|
19
|
+
oxc ??= createRequire(import.meta.url)("oxc-parser") as typeof Oxc;
|
|
20
|
+
return oxc;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Where an import is, inside the artifact. */
|
|
24
|
+
export interface ImportSite {
|
|
25
|
+
readonly file: string;
|
|
26
|
+
readonly line: number;
|
|
27
|
+
readonly column: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const SCRIPT = new Set([".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"]);
|
|
31
|
+
|
|
32
|
+
interface Node {
|
|
33
|
+
readonly type: string;
|
|
34
|
+
readonly start: number;
|
|
35
|
+
readonly end: number;
|
|
36
|
+
readonly [key: string]: unknown;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function langOf(file: string): "ts" | "tsx" | "js" | "jsx" {
|
|
40
|
+
const ext = extname(file);
|
|
41
|
+
if (ext === ".tsx") return "tsx";
|
|
42
|
+
if (ext === ".jsx" || ext === ".mdx") return "jsx";
|
|
43
|
+
if (ext === ".ts" || ext === ".mts" || ext === ".cts") return "ts";
|
|
44
|
+
return "js";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function position(text: string, offset: number): { line: number; column: number } {
|
|
48
|
+
const before = text.slice(0, offset);
|
|
49
|
+
const newline = before.lastIndexOf("\n");
|
|
50
|
+
return { line: before.split("\n").length, column: offset - newline };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A string literal's value, or `undefined` for anything computed. */
|
|
54
|
+
function literal(source: string): string | undefined {
|
|
55
|
+
const quote = source[0];
|
|
56
|
+
if ((quote !== '"' && quote !== "'" && quote !== "`") || source.at(-1) !== quote)
|
|
57
|
+
return undefined;
|
|
58
|
+
const body = source.slice(1, -1);
|
|
59
|
+
if (quote === "`" && body.includes("${")) return undefined;
|
|
60
|
+
return body;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The specifiers a script imports, with their offsets in `text`. */
|
|
64
|
+
function scriptImports(file: string, text: string): { specifier: string; offset: number }[] {
|
|
65
|
+
const parsed = parser().parseSync(file, text, { lang: langOf(file), sourceType: "module" });
|
|
66
|
+
if (parsed.errors.length > 0 && !parsed.program) return [];
|
|
67
|
+
const found: { specifier: string; offset: number }[] = [];
|
|
68
|
+
const { module } = parsed;
|
|
69
|
+
for (const entry of module.staticImports) {
|
|
70
|
+
if (entry.entries.length > 0 && entry.entries.every((item) => item.isType)) continue;
|
|
71
|
+
found.push({ specifier: entry.moduleRequest.value, offset: entry.moduleRequest.start });
|
|
72
|
+
}
|
|
73
|
+
for (const entry of module.staticExports) {
|
|
74
|
+
for (const item of entry.entries) {
|
|
75
|
+
if (!item.moduleRequest || (item as { isType?: boolean }).isType) continue;
|
|
76
|
+
found.push({ specifier: item.moduleRequest.value, offset: item.moduleRequest.start });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const entry of module.dynamicImports) {
|
|
80
|
+
const value = literal(text.slice(entry.moduleRequest.start, entry.moduleRequest.end));
|
|
81
|
+
if (value !== undefined) found.push({ specifier: value, offset: entry.moduleRequest.start });
|
|
82
|
+
}
|
|
83
|
+
// A JSX pragma makes the compiled file import `<package>/jsx-runtime`.
|
|
84
|
+
for (const comment of parsed.comments) {
|
|
85
|
+
const pragma = /@jsxImportSource\s+(\S+)/.exec(comment.value);
|
|
86
|
+
if (pragma?.[1]) found.push({ specifier: pragma[1], offset: comment.start });
|
|
87
|
+
}
|
|
88
|
+
// `require("x")` in CommonJS files and in code that keeps it.
|
|
89
|
+
if (text.includes("require")) {
|
|
90
|
+
const visit = (node: unknown): void => {
|
|
91
|
+
if (Array.isArray(node)) {
|
|
92
|
+
for (const child of node) visit(child);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (node === null || typeof node !== "object") return;
|
|
96
|
+
const current = node as Node;
|
|
97
|
+
if (current.type === "CallExpression") {
|
|
98
|
+
const callee = current["callee"] as Node | undefined;
|
|
99
|
+
const [first] = (current["arguments"] as Node[] | undefined) ?? [];
|
|
100
|
+
if (
|
|
101
|
+
callee?.type === "Identifier" &&
|
|
102
|
+
(callee as unknown as { name: string }).name === "require" &&
|
|
103
|
+
first?.type === "Literal" &&
|
|
104
|
+
typeof (first as unknown as { value: unknown }).value === "string"
|
|
105
|
+
) {
|
|
106
|
+
found.push({
|
|
107
|
+
specifier: (first as unknown as { value: string }).value,
|
|
108
|
+
offset: first.start,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
for (const [key, value] of Object.entries(current)) {
|
|
113
|
+
if (key !== "type" && key !== "start" && key !== "end" && typeof value === "object") {
|
|
114
|
+
visit(value);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
visit(parsed.program);
|
|
119
|
+
}
|
|
120
|
+
return found;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The ESM blocks of an MDX file (MDX 3): paragraphs outside code fences and frontmatter whose
|
|
125
|
+
* first line starts with `import` or `export`, each running to the next blank line.
|
|
126
|
+
*/
|
|
127
|
+
function mdxImports(file: string, text: string): { specifier: string; offset: number }[] {
|
|
128
|
+
const lines = text.split("\n");
|
|
129
|
+
const offsets: number[] = [];
|
|
130
|
+
let at = 0;
|
|
131
|
+
for (const line of lines) {
|
|
132
|
+
offsets.push(at);
|
|
133
|
+
at += line.length + 1;
|
|
134
|
+
}
|
|
135
|
+
const found: { specifier: string; offset: number }[] = [];
|
|
136
|
+
let index = 0;
|
|
137
|
+
if (lines[0]?.trim() === "---") {
|
|
138
|
+
index = lines.findIndex((line, i) => i > 0 && line.trim() === "---") + 1;
|
|
139
|
+
if (index === 0) return found;
|
|
140
|
+
}
|
|
141
|
+
let fence: string | undefined;
|
|
142
|
+
while (index < lines.length) {
|
|
143
|
+
const line = lines[index] ?? "";
|
|
144
|
+
const marker = /^ {0,3}(`{3,}|~{3,})/.exec(line)?.[1];
|
|
145
|
+
if (fence !== undefined) {
|
|
146
|
+
if (marker !== undefined && marker[0] === fence[0] && marker.length >= fence.length) {
|
|
147
|
+
fence = undefined;
|
|
148
|
+
}
|
|
149
|
+
index++;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (marker !== undefined) {
|
|
153
|
+
fence = marker;
|
|
154
|
+
index++;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (/^(import|export)\b/.test(line)) {
|
|
158
|
+
let end = index;
|
|
159
|
+
while (end + 1 < lines.length && (lines[end + 1] ?? "").trim() !== "") end++;
|
|
160
|
+
const start = offsets[index] ?? 0;
|
|
161
|
+
const block = lines.slice(index, end + 1).join("\n");
|
|
162
|
+
try {
|
|
163
|
+
for (const entry of scriptImports(`${file}.jsx`, block)) {
|
|
164
|
+
found.push({ specifier: entry.specifier, offset: start + entry.offset });
|
|
165
|
+
}
|
|
166
|
+
} catch {
|
|
167
|
+
// Not ESM after all: the MDX compiler reports it.
|
|
168
|
+
}
|
|
169
|
+
index = end + 1;
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
index++;
|
|
173
|
+
}
|
|
174
|
+
return found;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* `text` with every CSS comment blanked to spaces (newlines kept), so offsets still point into the
|
|
179
|
+
* original. Strings are copied as they are: a `/*` inside one doesn't start a comment.
|
|
180
|
+
*/
|
|
181
|
+
function withoutCssComments(text: string): string {
|
|
182
|
+
let out = "";
|
|
183
|
+
let index = 0;
|
|
184
|
+
while (index < text.length) {
|
|
185
|
+
const char = text[index] as string;
|
|
186
|
+
if (char === '"' || char === "'") {
|
|
187
|
+
let end = index + 1;
|
|
188
|
+
while (end < text.length && text[end] !== char && text[end] !== "\n") {
|
|
189
|
+
end += text[end] === "\\" ? 2 : 1;
|
|
190
|
+
}
|
|
191
|
+
out += text.slice(index, end + 1);
|
|
192
|
+
index = end + 1;
|
|
193
|
+
} else if (char === "/" && text[index + 1] === "*") {
|
|
194
|
+
const close = text.indexOf("*/", index + 2);
|
|
195
|
+
const end = close === -1 ? text.length : close + 2;
|
|
196
|
+
out += text.slice(index, end).replace(/[^\n]/g, " ");
|
|
197
|
+
index = end;
|
|
198
|
+
} else {
|
|
199
|
+
out += char;
|
|
200
|
+
index++;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** The packages a CSS file loads: `@import`, Tailwind's `@plugin` and `@reference`. */
|
|
207
|
+
function cssImports(source: string): { specifier: string; offset: number }[] {
|
|
208
|
+
const text = withoutCssComments(source);
|
|
209
|
+
const found: { specifier: string; offset: number }[] = [];
|
|
210
|
+
const rule = /@(?:import|plugin|reference)\s+(?:url\(\s*)?(["'])([^"'\n]+)\1/g;
|
|
211
|
+
for (const match of text.matchAll(rule)) {
|
|
212
|
+
found.push({
|
|
213
|
+
specifier: match[2] ?? "",
|
|
214
|
+
offset: (match.index ?? 0) + match[0].indexOf(match[1] ?? ""),
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
return found;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async function listFiles(dir: string, prefix: string, out: string[]): Promise<void> {
|
|
221
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
222
|
+
const path = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
223
|
+
// `public/` is served as-is and never bundled (INTERFACES §2.1).
|
|
224
|
+
if (entry.isDirectory()) {
|
|
225
|
+
if (path !== "public") await listFiles(join(dir, entry.name), path, out);
|
|
226
|
+
} else if (entry.isFile()) out.push(path);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The npm packages the artifact in `sourceDir` imports, each with its first import site, sorted by
|
|
232
|
+
* name. Warm packages are included; the caller tells them apart.
|
|
233
|
+
*/
|
|
234
|
+
export async function scanImports(sourceDir: string): Promise<Map<string, ImportSite>> {
|
|
235
|
+
const files: string[] = [];
|
|
236
|
+
await listFiles(sourceDir, "", files);
|
|
237
|
+
files.sort();
|
|
238
|
+
const found = new Map<string, ImportSite>();
|
|
239
|
+
for (const file of files) {
|
|
240
|
+
const ext = extname(file);
|
|
241
|
+
// Declaration files hold types only; the build never loads them.
|
|
242
|
+
if (/\.d\.[cm]?ts$/.test(file)) continue;
|
|
243
|
+
const kind = SCRIPT.has(ext) ? "script" : ext === ".mdx" ? "mdx" : ext === ".css" ? "css" : "";
|
|
244
|
+
if (kind === "") continue;
|
|
245
|
+
const text = await readFile(join(sourceDir, file), "utf8");
|
|
246
|
+
let imports: { specifier: string; offset: number }[];
|
|
247
|
+
try {
|
|
248
|
+
imports =
|
|
249
|
+
kind === "script"
|
|
250
|
+
? scriptImports(file, text)
|
|
251
|
+
: kind === "mdx"
|
|
252
|
+
? mdxImports(file, text)
|
|
253
|
+
: cssImports(text);
|
|
254
|
+
} catch {
|
|
255
|
+
// A file that doesn't parse is the build's to report.
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
for (const { specifier, offset } of imports) {
|
|
259
|
+
const name = packageOf(specifier);
|
|
260
|
+
if (name !== undefined && !found.has(name)) {
|
|
261
|
+
found.set(name, { file, ...position(text, offset) });
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return new Map([...found].sort(([a], [b]) => (a < b ? -1 : 1)));
|
|
266
|
+
}
|