@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,918 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependencies (D20, D43, D94, D95; DP-01 to DP-05): which packages an artifact's build resolves.
|
|
3
|
+
*
|
|
4
|
+
* - The **warm set** is a pre-built, read-only layout. An artifact that imports only warm packages
|
|
5
|
+
* builds against it with no install.
|
|
6
|
+
* - **On-demand packages** install through pnpm 10 into the home's shared, content-addressed store
|
|
7
|
+
* (`deps/store`) and are linked into one layout per lockfile (`deps/locks/<lockDigest>`), whose
|
|
8
|
+
* `package.json` links every warm package through one `warm` symlink. pnpm resolves each peer
|
|
9
|
+
* dependency to the warm copy, so React exists once.
|
|
10
|
+
* - Each artifact has a **working lockfile** in the ledger, keyed by its artifact ID (or source
|
|
11
|
+
* path before its first publish). Every pin is exact, and the lockfile seeds each resolution, so
|
|
12
|
+
* a republish is reproducible and works offline; publish freezes it into the version.
|
|
13
|
+
* - Installs run in an **install child** in the artifact's source directory with the artifact
|
|
14
|
+
* environment; lifecycle scripts run there, never in the supervisor.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const deps = createDependencies({ home, ledger, processes, log, env, warm });
|
|
18
|
+
* const resolved = await deps.resolve({ key, sourceDir, artifactDir, artifactPath });
|
|
19
|
+
* // resolved.lock → versions/<id>/lock.yaml; resolved.lockDigest → envDigest; resolved.nodeModules → the build
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
import { randomBytes } from "node:crypto";
|
|
23
|
+
import { existsSync, readlinkSync } from "node:fs";
|
|
24
|
+
import { lstat, mkdir, readdir, readFile, rename, rm, symlink, writeFile } from "node:fs/promises";
|
|
25
|
+
import { networkInterfaces } from "node:os";
|
|
26
|
+
import { join } from "node:path";
|
|
27
|
+
import {
|
|
28
|
+
type AddedPackage,
|
|
29
|
+
createDiagnostic,
|
|
30
|
+
type DependencyPolicy,
|
|
31
|
+
type DepsAddResult,
|
|
32
|
+
type Diagnostic,
|
|
33
|
+
RpcError,
|
|
34
|
+
} from "@mostra/protocol";
|
|
35
|
+
import semver from "semver";
|
|
36
|
+
import { type Faults, noFaults } from "../faults.ts";
|
|
37
|
+
import type { Gate } from "../gate.ts";
|
|
38
|
+
import {
|
|
39
|
+
type Ledger,
|
|
40
|
+
type LedgerDb,
|
|
41
|
+
type RequestKey,
|
|
42
|
+
recordedResult,
|
|
43
|
+
recordResult,
|
|
44
|
+
} from "../ledger/index.ts";
|
|
45
|
+
import type { Environment, Log } from "../log.ts";
|
|
46
|
+
import type { ProcessManager } from "../processes/index.ts";
|
|
47
|
+
import { createInstaller, INSTALL_TIMEOUT_MS, type Installer } from "./installer.ts";
|
|
48
|
+
import { type Lock, lockDigest, lockText, parseLock, pinnedPackages } from "./lock.ts";
|
|
49
|
+
import { scanImports } from "./scan.ts";
|
|
50
|
+
import { type PackageSpec, parseSpec } from "./specifier.ts";
|
|
51
|
+
import type { WarmSet } from "./warm.ts";
|
|
52
|
+
|
|
53
|
+
export { lockDigest, lockText, parseLock } from "./lock.ts";
|
|
54
|
+
export { scanImports } from "./scan.ts";
|
|
55
|
+
export { packageOf, parseSpec } from "./specifier.ts";
|
|
56
|
+
export { locateWarmSet, runtimeRoot, type WarmSet } from "./warm.ts";
|
|
57
|
+
|
|
58
|
+
/** The marker a complete lock layout carries; a layout without it is rebuilt. */
|
|
59
|
+
const LAYOUT_MARKER = ".mostra-layout";
|
|
60
|
+
|
|
61
|
+
/** Options for {@link createDependencies}. */
|
|
62
|
+
export interface DependenciesOptions {
|
|
63
|
+
/** The instance's gate: layout installs hold it shared, so gc never runs beside one (D140). */
|
|
64
|
+
readonly gate?: Gate;
|
|
65
|
+
/** The home's real path; `deps/` lives in it. */
|
|
66
|
+
readonly home: string;
|
|
67
|
+
readonly ledger: Ledger;
|
|
68
|
+
readonly processes: ProcessManager;
|
|
69
|
+
readonly log: Log;
|
|
70
|
+
/** The supervisor's environment: `MOSTRA_OFFLINE`, and the source of the artifact environment. */
|
|
71
|
+
readonly env: Environment;
|
|
72
|
+
readonly warm: WarmSet;
|
|
73
|
+
/** The home's dependency policy, read when it is needed. */
|
|
74
|
+
readonly policy: () => DependencyPolicy;
|
|
75
|
+
/** Default 300 s (FAILURE_MODEL §9). */
|
|
76
|
+
readonly installTimeoutMs?: number;
|
|
77
|
+
/** Test fault points `deps:*`. */
|
|
78
|
+
readonly faults?: Faults;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** What a build resolves its packages from. */
|
|
82
|
+
export interface ResolvedDependencies {
|
|
83
|
+
/** The lockfile's text, written into the version as `lock.yaml`. */
|
|
84
|
+
readonly lock: string;
|
|
85
|
+
/** Its SHA-256, part of `envDigest` (DP-03). */
|
|
86
|
+
readonly lockDigest: string;
|
|
87
|
+
/** The `node_modules` the build links: the warm layout, or the lock's layout. */
|
|
88
|
+
readonly nodeModules: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A resolution, and the step that makes it the artifact's working lockfile. */
|
|
92
|
+
export interface Resolution extends ResolvedDependencies {
|
|
93
|
+
/**
|
|
94
|
+
* Inside the transaction that commits the publish: records the lockfile as the working
|
|
95
|
+
* lockfile of `artifactId`, moving a path-keyed one to it. Nothing is recorded when the publish
|
|
96
|
+
* fails, and a working lockfile changed since the resolution read it (by `deps/add`) is kept.
|
|
97
|
+
*/
|
|
98
|
+
save(db: LedgerDb, artifactId: string): void;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** One artifact, as dependencies see it. */
|
|
102
|
+
export interface DependencyTarget {
|
|
103
|
+
/** The working lockfile's key: {@link lockKey}. */
|
|
104
|
+
readonly key: string;
|
|
105
|
+
/** The artifact's source directory: the install child's working directory (DP-05). */
|
|
106
|
+
readonly artifactDir: string;
|
|
107
|
+
/** The artifact's source path as callers name it, for the `mostra add` command in diagnostics. */
|
|
108
|
+
readonly artifactPath: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** What gc collects of the dependency state (D140 item 14). */
|
|
112
|
+
export interface DependencyCollection {
|
|
113
|
+
/** The lock layouts, and the bytes only they hold (files the store doesn't share). */
|
|
114
|
+
readonly lockLayouts: {
|
|
115
|
+
readonly count: number;
|
|
116
|
+
readonly bytes: number;
|
|
117
|
+
readonly names: readonly string[];
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* The store packages no kept lockfile needs. `bytes` is what the store shrank by in a real run
|
|
121
|
+
* (its files are shared between packages, so a dry run can't attribute them) and 0 in a dry run.
|
|
122
|
+
*/
|
|
123
|
+
readonly storePackages: {
|
|
124
|
+
readonly count: number;
|
|
125
|
+
readonly bytes: number;
|
|
126
|
+
readonly packages: readonly string[];
|
|
127
|
+
/** Why the store couldn't be pruned; its packages then stay, still indexed. */
|
|
128
|
+
readonly failed?: Diagnostic;
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The dependencies of one instance. */
|
|
133
|
+
export interface Dependencies {
|
|
134
|
+
readonly warm: WarmSet;
|
|
135
|
+
/**
|
|
136
|
+
* Resolves the packages the snapshot in `sourceDir` imports into a lockfile and a layout to
|
|
137
|
+
* build with, installing what's missing (or, under the `explicit` policy, refusing it with
|
|
138
|
+
* `MOSTRA_DEP_MISSING`). The caller records it with {@link Resolution.save}.
|
|
139
|
+
*/
|
|
140
|
+
resolve(target: DependencyTarget & { readonly sourceDir: string }): Promise<Resolution>;
|
|
141
|
+
/**
|
|
142
|
+
* The `node_modules` a version's server code resolves its packages from at run time (D135):
|
|
143
|
+
* the warm set when the version has no on-demand packages, else the layout of its frozen
|
|
144
|
+
* lockfile, installed again from the store if it was removed. When the warm set moved since the
|
|
145
|
+
* version was built (a runtime with other warm versions), a pinned layout installs the warm
|
|
146
|
+
* packages the version locked at their exact versions, so its server code runs against what it
|
|
147
|
+
* was built with.
|
|
148
|
+
*
|
|
149
|
+
* @throws RpcError `MOSTRA_DEP_OFFLINE`, `MOSTRA_DEP_INSTALL_FAILED` … when a layout can't be made.
|
|
150
|
+
*/
|
|
151
|
+
runtimeModules(target: {
|
|
152
|
+
readonly artifactId: string;
|
|
153
|
+
/** The version's directory, holding its `lock.yaml`. */
|
|
154
|
+
readonly versionDir: string;
|
|
155
|
+
/** The artifact's source directory, when it still exists: lifecycle scripts run there. */
|
|
156
|
+
readonly sourceDir?: string;
|
|
157
|
+
}): Promise<string>;
|
|
158
|
+
/**
|
|
159
|
+
* gc's part (D95, D135, D140 item 14): the lock layouts in `deps/locks` that no working
|
|
160
|
+
* lockfile and no lock text in `keep` names (a `pinned-*` layout counts as named by the
|
|
161
|
+
* `lockDigest` its marker records), and the store packages only they need. A dry run lists them;
|
|
162
|
+
* a real run removes the layouts, drops the packages from the store index and runs
|
|
163
|
+
* `pnpm store prune` in the install child. The caller holds the instance's gate alone, so no
|
|
164
|
+
* install runs meanwhile.
|
|
165
|
+
*/
|
|
166
|
+
collect(input: {
|
|
167
|
+
/** The `lock.yaml` texts of every version gc keeps. */
|
|
168
|
+
readonly keep: readonly string[];
|
|
169
|
+
readonly dryRun: boolean;
|
|
170
|
+
}): Promise<DependencyCollection>;
|
|
171
|
+
/** `deps/add`: adds packages to the artifact's working lockfile and installs them. */
|
|
172
|
+
add(
|
|
173
|
+
target: DependencyTarget & {
|
|
174
|
+
readonly packages: readonly string[];
|
|
175
|
+
readonly request?: RequestKey | undefined;
|
|
176
|
+
},
|
|
177
|
+
): Promise<DepsAddResult>;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* At start-up, before any request: removes resolutions and lock layouts a crash left unfinished
|
|
182
|
+
* (a layout is complete only once it carries its marker).
|
|
183
|
+
*/
|
|
184
|
+
export async function recoverDependencies(options: {
|
|
185
|
+
readonly home: string;
|
|
186
|
+
readonly log: Log;
|
|
187
|
+
}): Promise<void> {
|
|
188
|
+
const root = join(options.home, "deps");
|
|
189
|
+
await rm(join(root, "tmp"), { recursive: true, force: true });
|
|
190
|
+
let entries: string[];
|
|
191
|
+
try {
|
|
192
|
+
entries = await readdir(join(root, "locks"));
|
|
193
|
+
} catch {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
for (const entry of entries) {
|
|
197
|
+
if (!existsSync(join(root, "locks", entry, LAYOUT_MARKER))) {
|
|
198
|
+
options.log.info("deps.layout_removed", { lockDigest: entry, reason: "incomplete" });
|
|
199
|
+
await rm(join(root, "locks", entry), { recursive: true, force: true });
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** The working lockfile's key: the artifact ID when there is one, else its source path. */
|
|
205
|
+
export function lockKey(artifactId: string | undefined, bindingPath: string): string {
|
|
206
|
+
return artifactId ?? `path:${bindingPath}`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Whether Mostra is offline (D95): `MOSTRA_OFFLINE` is set, or no network interface has a
|
|
211
|
+
* routable address (link-local addresses don't reach a registry).
|
|
212
|
+
*/
|
|
213
|
+
export function isOffline(env: Environment): boolean {
|
|
214
|
+
const flag = env["MOSTRA_OFFLINE"];
|
|
215
|
+
if (flag !== undefined && flag !== "" && !/^(?:0|false|no)$/i.test(flag)) return true;
|
|
216
|
+
return !Object.values(networkInterfaces()).some((addresses) =>
|
|
217
|
+
(addresses ?? []).some(
|
|
218
|
+
(address) =>
|
|
219
|
+
!address.internal &&
|
|
220
|
+
!address.address.startsWith("169.254.") &&
|
|
221
|
+
!/^fe[89ab]/i.test(address.address),
|
|
222
|
+
),
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** A shell-safe rendering of `value`, for the commands in diagnostics. */
|
|
227
|
+
function shellQuote(value: string): string {
|
|
228
|
+
return /^[\w@%+=:,./-]+$/.test(value) ? value : `'${value.replaceAll("'", `'\\''`)}'`;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Serializes work per key; one supervisor, so in-process order is the order. */
|
|
232
|
+
class KeyedQueue {
|
|
233
|
+
readonly #tails = new Map<string, Promise<unknown>>();
|
|
234
|
+
run<T>(key: string, task: () => Promise<T>): Promise<T> {
|
|
235
|
+
const next = (this.#tails.get(key) ?? Promise.resolve()).then(task, task);
|
|
236
|
+
const tail = next.then(
|
|
237
|
+
() => undefined,
|
|
238
|
+
() => undefined,
|
|
239
|
+
);
|
|
240
|
+
this.#tails.set(key, tail);
|
|
241
|
+
void tail.then(() => {
|
|
242
|
+
if (this.#tails.get(key) === tail) this.#tails.delete(key);
|
|
243
|
+
});
|
|
244
|
+
return next;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
interface WorkingLock {
|
|
249
|
+
readonly lock: Lock;
|
|
250
|
+
/** Packages added through `deps/add`, kept whether or not the source imports them. */
|
|
251
|
+
readonly added: readonly string[];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const fail = (code: Parameters<typeof createDiagnostic>[0], message: string): RpcError =>
|
|
255
|
+
new RpcError(createDiagnostic(code, { message }));
|
|
256
|
+
|
|
257
|
+
/** Creates the dependencies of one instance. */
|
|
258
|
+
export function createDependencies(options: DependenciesOptions): Dependencies {
|
|
259
|
+
const { ledger, log, warm } = options;
|
|
260
|
+
const faults = options.faults ?? noFaults;
|
|
261
|
+
const root = join(options.home, "deps");
|
|
262
|
+
const dirs = {
|
|
263
|
+
store: join(root, "store"),
|
|
264
|
+
cache: join(root, "cache"),
|
|
265
|
+
state: join(root, "state"),
|
|
266
|
+
locks: join(root, "locks"),
|
|
267
|
+
tmp: join(root, "tmp"),
|
|
268
|
+
};
|
|
269
|
+
let installer: Installer | undefined;
|
|
270
|
+
const install = (): Installer =>
|
|
271
|
+
(installer ??= createInstaller({
|
|
272
|
+
processes: options.processes,
|
|
273
|
+
log,
|
|
274
|
+
dirs,
|
|
275
|
+
env: options.env,
|
|
276
|
+
timeoutMs: options.installTimeoutMs ?? INSTALL_TIMEOUT_MS,
|
|
277
|
+
}));
|
|
278
|
+
const byArtifact = new KeyedQueue();
|
|
279
|
+
const byLayout = new KeyedQueue();
|
|
280
|
+
const offline = (): boolean => isOffline(options.env);
|
|
281
|
+
|
|
282
|
+
// Every warm package as a link through the layout's `warm` symlink (D95).
|
|
283
|
+
const warmLinks = Object.fromEntries(
|
|
284
|
+
Object.keys(warm.packages).map((name) => [name, `link:warm/${name}`]),
|
|
285
|
+
);
|
|
286
|
+
const manifestOf = (packages: Readonly<Record<string, string>>): string =>
|
|
287
|
+
`${JSON.stringify(
|
|
288
|
+
{
|
|
289
|
+
name: "mostra-artifact-dependencies",
|
|
290
|
+
private: true,
|
|
291
|
+
dependencies: Object.fromEntries(
|
|
292
|
+
Object.entries({ ...warmLinks, ...packages }).sort(([a], [b]) => (a < b ? -1 : 1)),
|
|
293
|
+
),
|
|
294
|
+
},
|
|
295
|
+
null,
|
|
296
|
+
2,
|
|
297
|
+
)}\n`;
|
|
298
|
+
|
|
299
|
+
const readWorking = (key: string): WorkingLock | undefined => {
|
|
300
|
+
const row = ledger.read((db) =>
|
|
301
|
+
db.get<{ lock_text: string; added_json: string }>(
|
|
302
|
+
"SELECT lock_text, added_json FROM dependency_lock WHERE key = ?",
|
|
303
|
+
key,
|
|
304
|
+
),
|
|
305
|
+
);
|
|
306
|
+
return row
|
|
307
|
+
? { lock: parseLock(row.lock_text), added: JSON.parse(row.added_json) as string[] }
|
|
308
|
+
: undefined;
|
|
309
|
+
};
|
|
310
|
+
const writeWorking = (key: string, text: string, added: readonly string[]): void => {
|
|
311
|
+
ledger.write((db) =>
|
|
312
|
+
db.run(
|
|
313
|
+
"INSERT INTO dependency_lock (key, lock_text, added_json, updated_at) VALUES (?, ?, ?, ?) ON CONFLICT(key) DO UPDATE SET lock_text = excluded.lock_text, added_json = excluded.added_json, updated_at = excluded.updated_at",
|
|
314
|
+
key,
|
|
315
|
+
text,
|
|
316
|
+
JSON.stringify([...new Set(added)].sort()),
|
|
317
|
+
new Date().toISOString(),
|
|
318
|
+
),
|
|
319
|
+
);
|
|
320
|
+
};
|
|
321
|
+
/** A working lockfile's stored text, or `null` without one. */
|
|
322
|
+
const rowText = (key: string): string | null =>
|
|
323
|
+
ledger.read(
|
|
324
|
+
(db) =>
|
|
325
|
+
db.get<{ lock_text: string }>("SELECT lock_text FROM dependency_lock WHERE key = ?", key)
|
|
326
|
+
?.lock_text ?? null,
|
|
327
|
+
);
|
|
328
|
+
/** Versions of `name` the home's store holds, from the store index. */
|
|
329
|
+
const stored = (name: string): string[] =>
|
|
330
|
+
ledger
|
|
331
|
+
.read((db) =>
|
|
332
|
+
db.all<{ version: string }>("SELECT version FROM dependency_package WHERE name = ?", name),
|
|
333
|
+
)
|
|
334
|
+
.map((row) => row.version);
|
|
335
|
+
|
|
336
|
+
const sameRecord = (a: Readonly<Record<string, string>>, b: Readonly<Record<string, string>>) =>
|
|
337
|
+
JSON.stringify(Object.entries(a).sort()) === JSON.stringify(Object.entries(b).sort());
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Resolves a new lockfile in a scratch project: the pins stay exactly as they are, `additions`
|
|
341
|
+
* resolve through `pnpm add --save-exact`, and the previous installer lockfile seeds the graph.
|
|
342
|
+
* Packages without a pin or an addition leave the lockfile.
|
|
343
|
+
*/
|
|
344
|
+
const resolveLock = async (input: {
|
|
345
|
+
readonly key: string;
|
|
346
|
+
readonly artifactDir: string;
|
|
347
|
+
readonly pins: Readonly<Record<string, string>>;
|
|
348
|
+
readonly additions: readonly PackageSpec[];
|
|
349
|
+
readonly previous: string | undefined;
|
|
350
|
+
}): Promise<Lock> => {
|
|
351
|
+
const isOff = offline();
|
|
352
|
+
const additions = input.additions;
|
|
353
|
+
if (isOff) {
|
|
354
|
+
// Offline, only the store can answer (DP-04). When no version it holds can match, say so at
|
|
355
|
+
// once; otherwise pnpm resolves the spec as given from its metadata cache, so tags and the
|
|
356
|
+
// release age still mean what they mean online.
|
|
357
|
+
for (const spec of additions) {
|
|
358
|
+
const versions = stored(spec.name);
|
|
359
|
+
const range = spec.range !== undefined && semver.validRange(spec.range) ? spec.range : "*";
|
|
360
|
+
if (semver.maxSatisfying(versions, range) === null) {
|
|
361
|
+
throw fail(
|
|
362
|
+
"MOSTRA_DEP_OFFLINE",
|
|
363
|
+
`Mostra is offline and the home's package store doesn't hold ${spec.name}${spec.range ? `@${spec.range}` : ""}.`,
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
await mkdir(dirs.tmp, { recursive: true });
|
|
369
|
+
const work = join(dirs.tmp, `resolve-${randomBytes(6).toString("hex")}`);
|
|
370
|
+
await mkdir(work);
|
|
371
|
+
try {
|
|
372
|
+
await symlink(warm.nodeModules, join(work, "warm"), "dir");
|
|
373
|
+
await writeFile(join(work, "package.json"), manifestOf(input.pins));
|
|
374
|
+
if (input.previous !== undefined)
|
|
375
|
+
await writeFile(join(work, "pnpm-lock.yaml"), input.previous);
|
|
376
|
+
const outcome = await install().run({
|
|
377
|
+
command:
|
|
378
|
+
additions.length > 0
|
|
379
|
+
? [
|
|
380
|
+
"add",
|
|
381
|
+
...additions.map((spec) => (spec.range ? `${spec.name}@${spec.range}` : spec.name)),
|
|
382
|
+
"--save-exact",
|
|
383
|
+
"--lockfile-only",
|
|
384
|
+
]
|
|
385
|
+
: ["install", "--lockfile-only", "--no-frozen-lockfile"],
|
|
386
|
+
dir: work,
|
|
387
|
+
cwd: input.artifactDir,
|
|
388
|
+
offline: isOff,
|
|
389
|
+
releaseAge: {
|
|
390
|
+
exempt: additions.filter((spec) => spec.exact !== undefined).map((spec) => spec.name),
|
|
391
|
+
},
|
|
392
|
+
ownerRef: input.key,
|
|
393
|
+
});
|
|
394
|
+
if (!outcome.ok) throw new RpcError(outcome.diagnostic);
|
|
395
|
+
const manifest = JSON.parse(await readFile(join(work, "package.json"), "utf8")) as {
|
|
396
|
+
dependencies?: Record<string, string>;
|
|
397
|
+
};
|
|
398
|
+
const packages = Object.fromEntries(
|
|
399
|
+
Object.entries(manifest.dependencies ?? {}).filter(([name]) => !(name in warm.packages)),
|
|
400
|
+
);
|
|
401
|
+
if (Object.keys(packages).length === 0) return { warm: warm.packages, packages: {} };
|
|
402
|
+
return {
|
|
403
|
+
warm: warm.packages,
|
|
404
|
+
packages,
|
|
405
|
+
pnpm: await readFile(join(work, "pnpm-lock.yaml"), "utf8"),
|
|
406
|
+
};
|
|
407
|
+
} finally {
|
|
408
|
+
await rm(work, { recursive: true, force: true });
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
/** Points a layout's `warm` symlink at the current warm set (the runtime may have moved). */
|
|
413
|
+
const relinkWarm = async (dir: string): Promise<void> => {
|
|
414
|
+
const link = join(dir, "warm");
|
|
415
|
+
let target: string | undefined;
|
|
416
|
+
try {
|
|
417
|
+
target = readlinkSync(link);
|
|
418
|
+
} catch {
|
|
419
|
+
target = undefined;
|
|
420
|
+
}
|
|
421
|
+
if (target === warm.nodeModules) return;
|
|
422
|
+
const fresh = `${link}.${randomBytes(4).toString("hex")}`;
|
|
423
|
+
await symlink(warm.nodeModules, fresh, "dir");
|
|
424
|
+
await rename(fresh, link);
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* The layout of `lock`, installed from the store (and the registry for what the store lacks)
|
|
429
|
+
* into `deps/locks/<lockDigest>` unless it is already complete.
|
|
430
|
+
*/
|
|
431
|
+
const ensureLayout = (
|
|
432
|
+
lock: Lock,
|
|
433
|
+
digest: string,
|
|
434
|
+
target: { readonly key: string; readonly artifactDir: string },
|
|
435
|
+
): Promise<string> =>
|
|
436
|
+
byLayout.run(digest, async () => {
|
|
437
|
+
const dir = join(dirs.locks, digest);
|
|
438
|
+
const nodeModules = join(dir, "node_modules");
|
|
439
|
+
if (existsSync(join(dir, LAYOUT_MARKER))) {
|
|
440
|
+
await relinkWarm(dir);
|
|
441
|
+
return nodeModules;
|
|
442
|
+
}
|
|
443
|
+
const pnpm = lock.pnpm as string;
|
|
444
|
+
const isOff = offline();
|
|
445
|
+
if (isOff) {
|
|
446
|
+
const missing = pinnedPackages(pnpm).find(
|
|
447
|
+
({ name, version }) => !stored(name).includes(version),
|
|
448
|
+
);
|
|
449
|
+
if (missing) {
|
|
450
|
+
throw fail(
|
|
451
|
+
"MOSTRA_DEP_OFFLINE",
|
|
452
|
+
`Mostra is offline and the home's package store doesn't hold ${missing.name}@${missing.version}.`,
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
await rm(dir, { recursive: true, force: true });
|
|
457
|
+
await mkdir(dir, { recursive: true });
|
|
458
|
+
try {
|
|
459
|
+
await symlink(warm.nodeModules, join(dir, "warm"), "dir");
|
|
460
|
+
await writeFile(join(dir, "package.json"), manifestOf(lock.packages));
|
|
461
|
+
await writeFile(join(dir, "pnpm-lock.yaml"), pnpm);
|
|
462
|
+
const outcome = await install().run({
|
|
463
|
+
command: ["install", "--frozen-lockfile"],
|
|
464
|
+
dir,
|
|
465
|
+
cwd: target.artifactDir,
|
|
466
|
+
offline: isOff,
|
|
467
|
+
releaseAge: false,
|
|
468
|
+
ownerRef: target.key,
|
|
469
|
+
});
|
|
470
|
+
if (!outcome.ok) throw new RpcError(outcome.diagnostic);
|
|
471
|
+
// The store index first, then the marker: a crash between them leaves an incomplete
|
|
472
|
+
// layout (removed at the next start) over packages the store does hold, never a complete
|
|
473
|
+
// layout whose packages the index doesn't know (D80 review finding 5).
|
|
474
|
+
const now = new Date().toISOString();
|
|
475
|
+
ledger.write((db) => {
|
|
476
|
+
for (const { name, version } of pinnedPackages(pnpm)) {
|
|
477
|
+
db.run(
|
|
478
|
+
"INSERT OR IGNORE INTO dependency_package (name, version, stored_at) VALUES (?, ?, ?)",
|
|
479
|
+
name,
|
|
480
|
+
version,
|
|
481
|
+
now,
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
faults.at("deps:before-marker");
|
|
486
|
+
await writeFile(
|
|
487
|
+
join(dir, LAYOUT_MARKER),
|
|
488
|
+
`${JSON.stringify({ lockDigest: digest, installer: install().version })}\n`,
|
|
489
|
+
);
|
|
490
|
+
} catch (error) {
|
|
491
|
+
await rm(dir, { recursive: true, force: true });
|
|
492
|
+
throw error;
|
|
493
|
+
}
|
|
494
|
+
log.info("deps.layout_installed", {
|
|
495
|
+
lockDigest: digest,
|
|
496
|
+
packages: Object.keys(lock.packages),
|
|
497
|
+
});
|
|
498
|
+
return nodeModules;
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* A layout for a version whose locked warm packages the current warm set no longer holds at
|
|
503
|
+
* those versions: each such package installs at its locked version, the others link to the warm
|
|
504
|
+
* set as usual, and the version's installer lockfile seeds the rest of the graph.
|
|
505
|
+
*/
|
|
506
|
+
const ensurePinnedLayout = (
|
|
507
|
+
lock: Lock,
|
|
508
|
+
digest: string,
|
|
509
|
+
drifted: readonly string[],
|
|
510
|
+
target: { readonly key: string; readonly artifactDir: string },
|
|
511
|
+
): Promise<string> => {
|
|
512
|
+
const name = `pinned-${lockDigest(`${digest}\n${JSON.stringify(warm.packages)}`)}`;
|
|
513
|
+
return byLayout.run(name, async () => {
|
|
514
|
+
const dir = join(dirs.locks, name);
|
|
515
|
+
const nodeModules = join(dir, "node_modules");
|
|
516
|
+
if (existsSync(join(dir, LAYOUT_MARKER))) {
|
|
517
|
+
await relinkWarm(dir);
|
|
518
|
+
return nodeModules;
|
|
519
|
+
}
|
|
520
|
+
const pinnedWarm = Object.fromEntries(
|
|
521
|
+
Object.keys(lock.warm).map((pkg) => [
|
|
522
|
+
pkg,
|
|
523
|
+
drifted.includes(pkg) ? (lock.warm[pkg] as string) : `link:warm/${pkg}`,
|
|
524
|
+
]),
|
|
525
|
+
);
|
|
526
|
+
const isOff = offline();
|
|
527
|
+
if (isOff) {
|
|
528
|
+
const missing = drifted.find((pkg) => !stored(pkg).includes(lock.warm[pkg] as string));
|
|
529
|
+
if (missing) {
|
|
530
|
+
throw fail(
|
|
531
|
+
"MOSTRA_DEP_OFFLINE",
|
|
532
|
+
`Mostra is offline, and this version's server code needs ${missing}@${lock.warm[missing]}, which the warm set no longer holds and the home's package store doesn't.`,
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
await rm(dir, { recursive: true, force: true });
|
|
537
|
+
await mkdir(dir, { recursive: true });
|
|
538
|
+
try {
|
|
539
|
+
await symlink(warm.nodeModules, join(dir, "warm"), "dir");
|
|
540
|
+
await writeFile(
|
|
541
|
+
join(dir, "package.json"),
|
|
542
|
+
`${JSON.stringify(
|
|
543
|
+
{
|
|
544
|
+
name: "mostra-artifact-dependencies",
|
|
545
|
+
private: true,
|
|
546
|
+
dependencies: Object.fromEntries(
|
|
547
|
+
Object.entries({ ...pinnedWarm, ...lock.packages }).sort(([a], [b]) =>
|
|
548
|
+
a < b ? -1 : 1,
|
|
549
|
+
),
|
|
550
|
+
),
|
|
551
|
+
},
|
|
552
|
+
null,
|
|
553
|
+
2,
|
|
554
|
+
)}\n`,
|
|
555
|
+
);
|
|
556
|
+
if (lock.pnpm !== undefined) await writeFile(join(dir, "pnpm-lock.yaml"), lock.pnpm);
|
|
557
|
+
const outcome = await install().run({
|
|
558
|
+
command: ["install", "--no-frozen-lockfile"],
|
|
559
|
+
dir,
|
|
560
|
+
cwd: target.artifactDir,
|
|
561
|
+
offline: isOff,
|
|
562
|
+
// Every version here was accepted when the version was published.
|
|
563
|
+
releaseAge: false,
|
|
564
|
+
ownerRef: target.key,
|
|
565
|
+
});
|
|
566
|
+
if (!outcome.ok) throw new RpcError(outcome.diagnostic);
|
|
567
|
+
const now = new Date().toISOString();
|
|
568
|
+
const pinned = await readFile(join(dir, "pnpm-lock.yaml"), "utf8").catch(() => "");
|
|
569
|
+
ledger.write((db) => {
|
|
570
|
+
for (const { name: pkg, version } of pinnedPackages(pinned)) {
|
|
571
|
+
db.run(
|
|
572
|
+
"INSERT OR IGNORE INTO dependency_package (name, version, stored_at) VALUES (?, ?, ?)",
|
|
573
|
+
pkg,
|
|
574
|
+
version,
|
|
575
|
+
now,
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
await writeFile(
|
|
580
|
+
join(dir, LAYOUT_MARKER),
|
|
581
|
+
`${JSON.stringify({ lockDigest: digest, pinned: drifted, installer: install().version })}\n`,
|
|
582
|
+
);
|
|
583
|
+
} catch (error) {
|
|
584
|
+
await rm(dir, { recursive: true, force: true });
|
|
585
|
+
throw error;
|
|
586
|
+
}
|
|
587
|
+
log.info("deps.pinned_layout_installed", { lockDigest: digest, pinned: drifted });
|
|
588
|
+
return nodeModules;
|
|
589
|
+
});
|
|
590
|
+
};
|
|
591
|
+
|
|
592
|
+
/** Settles a lockfile: its text, digest and layout. */
|
|
593
|
+
const settle = async (
|
|
594
|
+
lock: Lock,
|
|
595
|
+
target: { readonly key: string; readonly artifactDir: string },
|
|
596
|
+
): Promise<ResolvedDependencies> => {
|
|
597
|
+
const text = lockText(lock);
|
|
598
|
+
const digest = lockDigest(text);
|
|
599
|
+
const nodeModules =
|
|
600
|
+
Object.keys(lock.packages).length === 0
|
|
601
|
+
? warm.nodeModules
|
|
602
|
+
: await ensureLayout(lock, digest, target);
|
|
603
|
+
return { lock: text, lockDigest: digest, nodeModules };
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
const runtimeModules = async (target: {
|
|
607
|
+
readonly artifactId: string;
|
|
608
|
+
readonly versionDir: string;
|
|
609
|
+
readonly sourceDir?: string;
|
|
610
|
+
}): Promise<string> => {
|
|
611
|
+
const text = await readFile(join(target.versionDir, "lock.yaml"), "utf8");
|
|
612
|
+
const lock = parseLock(text);
|
|
613
|
+
const digest = lockDigest(text);
|
|
614
|
+
const where = { key: target.artifactId, artifactDir: target.sourceDir ?? target.versionDir };
|
|
615
|
+
// Mostra's own packages always come from this runtime: they aren't on a registry to pin.
|
|
616
|
+
const drifted = Object.keys(lock.warm)
|
|
617
|
+
.filter((pkg) => !pkg.startsWith("@mostra/"))
|
|
618
|
+
.filter((pkg) => warm.packages[pkg] !== lock.warm[pkg]);
|
|
619
|
+
if (drifted.length > 0) return ensurePinnedLayout(lock, digest, drifted, where);
|
|
620
|
+
if (Object.keys(lock.packages).length === 0) return warm.nodeModules;
|
|
621
|
+
return ensureLayout(lock, digest, where);
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
/** gc's part (D140 item 14); see {@link Dependencies.collect}. */
|
|
625
|
+
const collect = async (input: {
|
|
626
|
+
readonly keep: readonly string[];
|
|
627
|
+
readonly dryRun: boolean;
|
|
628
|
+
}): Promise<DependencyCollection> => {
|
|
629
|
+
const working = ledger.read((db) =>
|
|
630
|
+
db
|
|
631
|
+
.all<{ lock_text: string }>("SELECT lock_text FROM dependency_lock")
|
|
632
|
+
.map((row) => row.lock_text),
|
|
633
|
+
);
|
|
634
|
+
const texts = [...working, ...input.keep];
|
|
635
|
+
const named = new Set(texts.map((text) => lockDigest(text)));
|
|
636
|
+
const needed = new Set<string>();
|
|
637
|
+
const need = (pnpm: string | undefined): void => {
|
|
638
|
+
if (pnpm === undefined) return;
|
|
639
|
+
for (const { name, version } of pinnedPackages(pnpm)) needed.add(`${name}@${version}`);
|
|
640
|
+
};
|
|
641
|
+
for (const text of texts) {
|
|
642
|
+
try {
|
|
643
|
+
need(parseLock(text).pnpm);
|
|
644
|
+
} catch {
|
|
645
|
+
// A lockfile Mostra can't read names no package it could keep.
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
const entries = await readdir(dirs.locks).catch(() => [] as string[]);
|
|
649
|
+
const layouts: string[] = [];
|
|
650
|
+
let layoutBytes = 0;
|
|
651
|
+
for (const name of entries.sort()) {
|
|
652
|
+
const dir = join(dirs.locks, name);
|
|
653
|
+
let digest = name;
|
|
654
|
+
try {
|
|
655
|
+
const marker = JSON.parse(await readFile(join(dir, LAYOUT_MARKER), "utf8")) as {
|
|
656
|
+
lockDigest?: unknown;
|
|
657
|
+
};
|
|
658
|
+
if (typeof marker.lockDigest === "string") digest = marker.lockDigest;
|
|
659
|
+
} catch {
|
|
660
|
+
// Unfinished: recovery at start removes it, never gc.
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
if (named.has(digest)) {
|
|
664
|
+
// A kept pinned layout needs its own installer lockfile's packages too.
|
|
665
|
+
need(await readFile(join(dir, "pnpm-lock.yaml"), "utf8").catch(() => undefined));
|
|
666
|
+
continue;
|
|
667
|
+
}
|
|
668
|
+
layouts.push(name);
|
|
669
|
+
layoutBytes += await unsharedBytes(dir);
|
|
670
|
+
}
|
|
671
|
+
const unneeded = ledger
|
|
672
|
+
.read((db) =>
|
|
673
|
+
db.all<{ name: string; version: string }>("SELECT name, version FROM dependency_package"),
|
|
674
|
+
)
|
|
675
|
+
.map((row) => `${row.name}@${row.version}`)
|
|
676
|
+
.filter((id) => !needed.has(id))
|
|
677
|
+
.sort();
|
|
678
|
+
let storeBytes = 0;
|
|
679
|
+
let failed: Diagnostic | undefined;
|
|
680
|
+
let pruned = unneeded;
|
|
681
|
+
if (!input.dryRun) {
|
|
682
|
+
for (const name of layouts) {
|
|
683
|
+
await rm(join(dirs.locks, name), { recursive: true, force: true });
|
|
684
|
+
log.info("deps.layout_collected", { layout: name });
|
|
685
|
+
}
|
|
686
|
+
if (existsSync(dirs.store) && (layouts.length > 0 || unneeded.length > 0)) {
|
|
687
|
+
const before = await apparentBytes(dirs.store);
|
|
688
|
+
const scratch = join(dirs.tmp, `prune-${randomBytes(6).toString("hex")}`);
|
|
689
|
+
await mkdir(scratch, { recursive: true });
|
|
690
|
+
try {
|
|
691
|
+
const outcome = await install().prune({ dir: scratch, ownerRef: "gc" });
|
|
692
|
+
if (!outcome.ok) failed = outcome.diagnostic;
|
|
693
|
+
} finally {
|
|
694
|
+
await rm(scratch, { recursive: true, force: true });
|
|
695
|
+
}
|
|
696
|
+
storeBytes = Math.max(0, before - (await apparentBytes(dirs.store)));
|
|
697
|
+
}
|
|
698
|
+
// The index says what the store holds (offline answers, DP-04): a package leaves it only
|
|
699
|
+
// once the prune that removed it succeeded.
|
|
700
|
+
if (failed) {
|
|
701
|
+
log.warn("deps.prune_failed", { diagnostic: failed });
|
|
702
|
+
pruned = [];
|
|
703
|
+
} else if (unneeded.length > 0) {
|
|
704
|
+
ledger.write((db) => {
|
|
705
|
+
for (const id of unneeded) {
|
|
706
|
+
const at = id.lastIndexOf("@");
|
|
707
|
+
db.run(
|
|
708
|
+
"DELETE FROM dependency_package WHERE name = ? AND version = ?",
|
|
709
|
+
id.slice(0, at),
|
|
710
|
+
id.slice(at + 1),
|
|
711
|
+
);
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return {
|
|
717
|
+
lockLayouts: { count: layouts.length, bytes: layoutBytes, names: layouts },
|
|
718
|
+
storePackages: {
|
|
719
|
+
count: pruned.length,
|
|
720
|
+
bytes: storeBytes,
|
|
721
|
+
packages: pruned,
|
|
722
|
+
...(failed ? { failed } : {}),
|
|
723
|
+
},
|
|
724
|
+
};
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
return {
|
|
728
|
+
warm,
|
|
729
|
+
|
|
730
|
+
runtimeModules: (target) =>
|
|
731
|
+
options.gate ? options.gate.shared(() => runtimeModules(target)) : runtimeModules(target),
|
|
732
|
+
|
|
733
|
+
collect,
|
|
734
|
+
|
|
735
|
+
resolve: (target) =>
|
|
736
|
+
byArtifact.run(target.key, async () => {
|
|
737
|
+
const imports = await scanImports(target.sourceDir);
|
|
738
|
+
const working = readWorking(target.key);
|
|
739
|
+
const added = (working?.added ?? []).filter((name) => !(name in warm.packages));
|
|
740
|
+
const needed = [
|
|
741
|
+
...new Set([...[...imports.keys()].filter((name) => !(name in warm.packages)), ...added]),
|
|
742
|
+
].sort();
|
|
743
|
+
const pinned = working?.lock.packages ?? {};
|
|
744
|
+
const missing = needed.filter((name) => pinned[name] === undefined);
|
|
745
|
+
if (missing.length > 0 && options.policy() === "explicit") {
|
|
746
|
+
const first = missing[0] as string;
|
|
747
|
+
const site = imports.get(first);
|
|
748
|
+
const command = `mostra add ${missing.join(" ")} --artifact ${shellQuote(target.artifactPath)}`;
|
|
749
|
+
throw new RpcError(
|
|
750
|
+
createDiagnostic("MOSTRA_DEP_MISSING", {
|
|
751
|
+
message: `${missing.join(", ")} ${missing.length === 1 ? "isn't" : "aren't"} in the warm set or the artifact's lockfile, and this home's dependency policy is explicit.`,
|
|
752
|
+
...(site ? { location: site } : {}),
|
|
753
|
+
fix: { kind: "command", command },
|
|
754
|
+
}),
|
|
755
|
+
);
|
|
756
|
+
}
|
|
757
|
+
const pins = Object.fromEntries(
|
|
758
|
+
needed
|
|
759
|
+
.filter((name) => pinned[name] !== undefined)
|
|
760
|
+
.map((name) => [name, pinned[name] as string]),
|
|
761
|
+
);
|
|
762
|
+
let lock: Lock;
|
|
763
|
+
if (needed.length === 0) {
|
|
764
|
+
lock = { warm: warm.packages, packages: {} };
|
|
765
|
+
} else if (
|
|
766
|
+
missing.length === 0 &&
|
|
767
|
+
working?.lock.pnpm !== undefined &&
|
|
768
|
+
sameRecord(pins, pinned) &&
|
|
769
|
+
sameRecord(working.lock.warm, warm.packages)
|
|
770
|
+
) {
|
|
771
|
+
// Exactly the working lockfile: nothing to resolve, and it works offline.
|
|
772
|
+
lock = working.lock;
|
|
773
|
+
} else {
|
|
774
|
+
lock = await resolveLock({
|
|
775
|
+
key: target.key,
|
|
776
|
+
artifactDir: target.artifactDir,
|
|
777
|
+
pins,
|
|
778
|
+
additions: missing.map((name) => ({ name })),
|
|
779
|
+
previous: working?.lock.pnpm,
|
|
780
|
+
});
|
|
781
|
+
}
|
|
782
|
+
const resolved = await settle(lock, target);
|
|
783
|
+
const seen = rowText(target.key);
|
|
784
|
+
return {
|
|
785
|
+
...resolved,
|
|
786
|
+
save(db, artifactId) {
|
|
787
|
+
const current = db.get<{ lock_text: string; added_json: string }>(
|
|
788
|
+
"SELECT lock_text, added_json FROM dependency_lock WHERE key = ?",
|
|
789
|
+
target.key,
|
|
790
|
+
);
|
|
791
|
+
// deps/add changed it after this resolution read it: the newer lockfile stays.
|
|
792
|
+
if ((current?.lock_text ?? null) !== seen) return;
|
|
793
|
+
const now = new Date().toISOString();
|
|
794
|
+
if (target.key !== artifactId) {
|
|
795
|
+
db.run("DELETE FROM dependency_lock WHERE key = ?", target.key);
|
|
796
|
+
}
|
|
797
|
+
db.run(
|
|
798
|
+
"INSERT INTO dependency_lock (key, lock_text, added_json, updated_at) VALUES (?, ?, ?, ?) ON CONFLICT(key) DO UPDATE SET lock_text = excluded.lock_text, added_json = excluded.added_json, updated_at = excluded.updated_at",
|
|
799
|
+
artifactId,
|
|
800
|
+
resolved.lock,
|
|
801
|
+
JSON.stringify([...new Set(added)].sort()),
|
|
802
|
+
now,
|
|
803
|
+
);
|
|
804
|
+
},
|
|
805
|
+
};
|
|
806
|
+
}),
|
|
807
|
+
|
|
808
|
+
add: (target) =>
|
|
809
|
+
byArtifact.run(target.key, async () => {
|
|
810
|
+
const request = target.request;
|
|
811
|
+
const recorded =
|
|
812
|
+
request === undefined
|
|
813
|
+
? undefined
|
|
814
|
+
: ledger.read((db) => recordedResult<DepsAddResult>(db, request));
|
|
815
|
+
if (recorded !== undefined) return recorded;
|
|
816
|
+
const specs: PackageSpec[] = [];
|
|
817
|
+
for (const text of target.packages) {
|
|
818
|
+
const spec = parseSpec(text);
|
|
819
|
+
if (!spec) {
|
|
820
|
+
throw fail(
|
|
821
|
+
"MOSTRA_DEP_SPEC_INVALID",
|
|
822
|
+
`"${text}" isn't a registry package spec (name, name@version, name@range or name@tag).`,
|
|
823
|
+
);
|
|
824
|
+
}
|
|
825
|
+
specs.push(spec);
|
|
826
|
+
}
|
|
827
|
+
for (const spec of specs) {
|
|
828
|
+
const version = warm.packages[spec.name];
|
|
829
|
+
if (version === undefined || spec.range === undefined) continue;
|
|
830
|
+
const fits = semver.validRange(spec.range)
|
|
831
|
+
? semver.satisfies(version, spec.range)
|
|
832
|
+
: spec.range === "latest";
|
|
833
|
+
if (!fits) {
|
|
834
|
+
throw fail(
|
|
835
|
+
"MOSTRA_DEP_WARM_CONFLICT",
|
|
836
|
+
`${spec.name} is in the warm set at ${version}, which every artifact uses; ${spec.name}@${spec.range} can't replace it.`,
|
|
837
|
+
);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
const working = readWorking(target.key);
|
|
841
|
+
const pinned = working?.lock.packages ?? {};
|
|
842
|
+
const onDemand = specs.filter((spec) => !(spec.name in warm.packages));
|
|
843
|
+
// A bare name that is already pinned keeps its pin; anything else resolves now.
|
|
844
|
+
const additions = onDemand.filter(
|
|
845
|
+
(spec) => spec.range !== undefined || pinned[spec.name] === undefined,
|
|
846
|
+
);
|
|
847
|
+
const additionNames = new Set(additions.map((spec) => spec.name));
|
|
848
|
+
const pins = Object.fromEntries(
|
|
849
|
+
Object.entries(pinned).filter(
|
|
850
|
+
([name]) => !additionNames.has(name) && !(name in warm.packages),
|
|
851
|
+
),
|
|
852
|
+
);
|
|
853
|
+
const lock: Lock =
|
|
854
|
+
additions.length === 0 &&
|
|
855
|
+
working !== undefined &&
|
|
856
|
+
sameRecord(working.lock.warm, warm.packages)
|
|
857
|
+
? working.lock
|
|
858
|
+
: Object.keys(pins).length === 0 && additions.length === 0
|
|
859
|
+
? { warm: warm.packages, packages: {} }
|
|
860
|
+
: await resolveLock({
|
|
861
|
+
key: target.key,
|
|
862
|
+
artifactDir: target.artifactDir,
|
|
863
|
+
pins,
|
|
864
|
+
additions,
|
|
865
|
+
previous: working?.lock.pnpm,
|
|
866
|
+
});
|
|
867
|
+
const resolved = await settle(lock, target);
|
|
868
|
+
const added = [...(working?.added ?? []), ...onDemand.map((spec) => spec.name)];
|
|
869
|
+
writeWorking(target.key, resolved.lock, added);
|
|
870
|
+
const packages: AddedPackage[] = specs.map((spec) => {
|
|
871
|
+
const warmVersion = warm.packages[spec.name];
|
|
872
|
+
return warmVersion !== undefined
|
|
873
|
+
? { name: spec.name, version: warmVersion, warm: true }
|
|
874
|
+
: { name: spec.name, version: lock.packages[spec.name] as string, warm: false };
|
|
875
|
+
});
|
|
876
|
+
const result: DepsAddResult = { packages, lockDigest: resolved.lockDigest };
|
|
877
|
+
if (request !== undefined) ledger.write((db) => recordResult(db, request, result));
|
|
878
|
+
return result;
|
|
879
|
+
}),
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
/** The bytes of the files under `dir` that nothing else links to (a layout's own files). */
|
|
884
|
+
async function unsharedBytes(dir: string): Promise<number> {
|
|
885
|
+
let total = 0;
|
|
886
|
+
for (const info of await files(dir)) if (info.nlink <= 1) total += info.size;
|
|
887
|
+
return total;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
/** The apparent size of a tree, counting each inode once. */
|
|
891
|
+
async function apparentBytes(dir: string): Promise<number> {
|
|
892
|
+
const seen = new Set<number>();
|
|
893
|
+
let total = 0;
|
|
894
|
+
for (const info of await files(dir)) {
|
|
895
|
+
if (seen.has(info.ino)) continue;
|
|
896
|
+
seen.add(info.ino);
|
|
897
|
+
total += info.size;
|
|
898
|
+
}
|
|
899
|
+
return total;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
/** The regular files under `dir`, as `lstat` sees them; symbolic links are never followed. */
|
|
903
|
+
async function files(dir: string): Promise<{ size: number; nlink: number; ino: number }[]> {
|
|
904
|
+
const found: { size: number; nlink: number; ino: number }[] = [];
|
|
905
|
+
const walk = async (path: string): Promise<void> => {
|
|
906
|
+
const entries = await readdir(path, { withFileTypes: true }).catch(() => []);
|
|
907
|
+
for (const entry of entries) {
|
|
908
|
+
const child = join(path, entry.name);
|
|
909
|
+
if (entry.isDirectory()) await walk(child);
|
|
910
|
+
else if (entry.isFile()) {
|
|
911
|
+
const info = await lstat(child).catch(() => undefined);
|
|
912
|
+
if (info) found.push({ size: info.size, nlink: info.nlink, ino: info.ino });
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
};
|
|
916
|
+
await walk(dir);
|
|
917
|
+
return found;
|
|
918
|
+
}
|