@mostra/runtime 0.9.0 → 0.10.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/dist/bin.js +2879 -8
- package/dist/bin.js.map +1 -1
- package/dist/builder/child/main.js +2 -2
- package/dist/{child-Chiavhdx.js → child-BmNZ4a70.js} +2 -2
- package/dist/{child-Chiavhdx.js.map → child-BmNZ4a70.js.map} +1 -1
- package/dist/deps/child/install.js +1 -1
- package/dist/{directories-mJ1qlVht.js → directories-BLJjeSPD.js} +5 -5
- package/dist/directories-BLJjeSPD.js.map +1 -0
- package/dist/{files-FygSJIzK.js → files-DGeWuE4I.js} +2 -2
- package/dist/{files-FygSJIzK.js.map → files-DGeWuE4I.js.map} +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/{instance-EnaZPs14.js → instance-FExcyENK.js} +3341 -396
- package/dist/instance-FExcyENK.js.map +1 -0
- package/package.json +7 -5
- package/src/actions/index.ts +163 -10
- package/src/bin.ts +78 -1
- package/src/gateway/actions.ts +20 -4
- package/src/gateway/browser.ts +118 -95
- package/src/gateway/capabilities.ts +15 -6
- package/src/gateway/index.ts +112 -15
- package/src/gateway/routes.ts +15 -7
- package/src/gc/index.ts +14 -0
- package/src/instance/index.ts +41 -2
- package/src/interaction/ephemeral.ts +32 -23
- package/src/interaction/events.ts +21 -1
- package/src/interaction/imports.ts +317 -0
- package/src/interaction/index.ts +172 -19
- package/src/interaction/outbox.ts +9 -0
- package/src/interaction/results.ts +8 -1
- package/src/interaction/runs.ts +52 -2
- package/src/interaction/submissions.ts +17 -1
- package/src/interaction/tally.ts +129 -0
- package/src/ledger/index.ts +104 -9
- package/src/ledger/migrations.ts +183 -0
- package/src/ledger/types.ts +8 -0
- package/src/link/binding.ts +289 -0
- package/src/link/hosted.ts +65 -0
- package/src/link/http.ts +136 -0
- package/src/link/index.ts +8 -0
- package/src/link/manager.ts +1385 -0
- package/src/link/relay.ts +512 -0
- package/src/live/index.ts +112 -2
- package/src/platform/filesystems.ts +78 -0
- package/src/platform/index.ts +1 -0
- package/src/protocol-server/index.ts +18 -1
- package/src/protocol-server/server.ts +38 -7
- package/src/protocol-server/websocket.ts +161 -0
- package/src/publish/bundle.ts +398 -0
- package/src/publish/manifest.ts +9 -0
- package/src/publish/methods.ts +12 -1
- package/src/publish/publisher.ts +127 -0
- package/src/registry/index.ts +35 -14
- package/src/server/api.ts +439 -0
- package/src/server/auth.ts +613 -0
- package/src/server/config.ts +221 -0
- package/src/server/data.ts +369 -0
- package/src/server/directory.ts +141 -0
- package/src/server/hosted.ts +822 -0
- package/src/server/index.ts +368 -0
- package/src/server/relay.ts +1163 -0
- package/src/share/index.ts +110 -6
- package/dist/directories-mJ1qlVht.js.map +0 -1
- package/dist/instance-EnaZPs14.js.map +0 -1
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `bundle` export class (ARCHITECTURE §3, INTERFACES §15, RM-01): the portable artifact package.
|
|
3
|
+
* A bundle is one published version, whole: its version directory (`manifest.json`, the client and
|
|
4
|
+
* server builds, `registry.json`, `lock.yaml`), its source snapshot, and `mostra-bundle.json`,
|
|
5
|
+
* which names them. Importing it into another home (`artifact/import`, `mostra run <bundle>`)
|
|
6
|
+
* makes the same version there: the same artifact ID, version ID, digests and, when free, number,
|
|
7
|
+
* because a version's identity is its artifact ID and digests (D89), and each is checked against
|
|
8
|
+
* the bundle's own bytes before anything is kept. Its server code runs in the importing home's
|
|
9
|
+
* workers, against the bundle's lockfile, which that home's launcher installs into its own layout
|
|
10
|
+
* before a worker starts (D135).
|
|
11
|
+
*
|
|
12
|
+
* ```text
|
|
13
|
+
* <bundle>/ or <bundle>.tar.zst, the same tree in one archive
|
|
14
|
+
* mostra-bundle.json { schemaVersion, kind, artifactId, versionId, number, title, digests… }
|
|
15
|
+
* version/ the version directory, as the home stores it
|
|
16
|
+
* source.tar.zst the source snapshot (DATA_MODEL §1)
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
import { existsSync } from "node:fs";
|
|
20
|
+
import { mkdir, mkdtemp, open, readdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
21
|
+
import { dirname, join, relative } from "node:path";
|
|
22
|
+
import { zstdCompressSync, zstdDecompressSync } from "node:zlib";
|
|
23
|
+
import { ArtifactManifest, createDiagnostic, RpcError } from "@mostra/protocol";
|
|
24
|
+
import { renamePath } from "../platform/index.ts";
|
|
25
|
+
import { fsyncPath, removeTree } from "../registry/index.ts";
|
|
26
|
+
import {
|
|
27
|
+
type BuildEnvironment,
|
|
28
|
+
environmentDigest,
|
|
29
|
+
listingDigest,
|
|
30
|
+
sha256Hex,
|
|
31
|
+
versionIdFor,
|
|
32
|
+
} from "./digest.ts";
|
|
33
|
+
import { extractManifest } from "./manifest.ts";
|
|
34
|
+
import { readTar, TAR_END, tarFile } from "./tar.ts";
|
|
35
|
+
|
|
36
|
+
/** What `mostra-bundle.json` holds. */
|
|
37
|
+
export interface BundleInfo {
|
|
38
|
+
readonly schemaVersion: 1;
|
|
39
|
+
readonly kind: "mostra-bundle";
|
|
40
|
+
readonly artifactId: string;
|
|
41
|
+
readonly versionId: string;
|
|
42
|
+
/** The version's number in the home that exported it; kept on import when free there. */
|
|
43
|
+
readonly number: number;
|
|
44
|
+
readonly title: string;
|
|
45
|
+
readonly sourceDigest: string;
|
|
46
|
+
readonly envDigest: string;
|
|
47
|
+
readonly outputDigest: string;
|
|
48
|
+
readonly lockDigest: string;
|
|
49
|
+
/** Whether the version has server code. */
|
|
50
|
+
readonly server: boolean;
|
|
51
|
+
/** The runtime that exported it. */
|
|
52
|
+
readonly mostraVersion: string;
|
|
53
|
+
readonly exportedAt: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The version directory's `manifest.json`, as publish writes it. */
|
|
57
|
+
interface VersionManifest {
|
|
58
|
+
readonly schemaVersion: number;
|
|
59
|
+
readonly artifactId: string;
|
|
60
|
+
readonly versionId: string;
|
|
61
|
+
readonly entry: string;
|
|
62
|
+
readonly server: boolean;
|
|
63
|
+
readonly manifest: unknown;
|
|
64
|
+
readonly sourceDigest: string;
|
|
65
|
+
readonly envDigest: string;
|
|
66
|
+
readonly environment: BuildEnvironment;
|
|
67
|
+
readonly outputDigest: string;
|
|
68
|
+
readonly forcedCodes?: readonly string[];
|
|
69
|
+
readonly verify?: { readonly status: "pass" | "fail"; readonly verifyRevision: string };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const INFO = "mostra-bundle.json";
|
|
73
|
+
const VERSION = "version";
|
|
74
|
+
const SOURCE = "source.tar.zst";
|
|
75
|
+
|
|
76
|
+
/** The largest bundle archive read whole into memory: a version's builds and source. */
|
|
77
|
+
const ARCHIVE_MAX = 2 * 1024 * 1024 * 1024;
|
|
78
|
+
|
|
79
|
+
/** JSON with sorted keys, to compare two manifests. */
|
|
80
|
+
function canonical(value: unknown): string {
|
|
81
|
+
return JSON.stringify(value, (_key, inner: unknown) =>
|
|
82
|
+
inner !== null && typeof inner === "object" && !Array.isArray(inner)
|
|
83
|
+
? Object.fromEntries(Object.entries(inner).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)))
|
|
84
|
+
: inner,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function invalid(message: string): RpcError {
|
|
89
|
+
return new RpcError(createDiagnostic("MOSTRA_ARTIFACT_BUNDLE_INVALID", { message }));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Every file under `dir`, as `/`-separated paths relative to it, sorted. */
|
|
93
|
+
async function filesUnder(dir: string): Promise<string[]> {
|
|
94
|
+
const out: string[] = [];
|
|
95
|
+
const walk = async (current: string): Promise<void> => {
|
|
96
|
+
for (const entry of await readdir(current, { withFileTypes: true })) {
|
|
97
|
+
const path = join(current, entry.name);
|
|
98
|
+
if (entry.isDirectory()) await walk(path);
|
|
99
|
+
else if (entry.isFile()) out.push(relative(dir, path).split(/[\\/]/).join("/"));
|
|
100
|
+
else throw invalid(`${relative(dir, path)} isn't a file or a directory.`);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
await walk(dir);
|
|
104
|
+
return out.sort();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The canonical listing of a version directory's output, as `outputDigest` hashes it. */
|
|
108
|
+
async function outputDigestOf(versionDir: string): Promise<string> {
|
|
109
|
+
const files = await filesUnder(versionDir);
|
|
110
|
+
const listed = [];
|
|
111
|
+
for (const path of files) {
|
|
112
|
+
if (path === "registry.json" || path.startsWith("client/") || path.startsWith("server/")) {
|
|
113
|
+
listed.push({
|
|
114
|
+
path,
|
|
115
|
+
executable: false,
|
|
116
|
+
sha256: sha256Hex(await readFile(join(versionDir, ...path.split("/")))),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return listingDigest(listed);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Writes the bundle of the version stored in `versionDir`, with its source snapshot `snapshot`,
|
|
125
|
+
* to `out`: a directory, or one archive when `out` ends in `.tar.zst`. `out` must not exist, or be
|
|
126
|
+
* an empty directory (`MOSTRA_ARTIFACT_DIR_NOT_EMPTY`).
|
|
127
|
+
*/
|
|
128
|
+
export async function writeBundle(input: {
|
|
129
|
+
readonly versionDir: string;
|
|
130
|
+
readonly snapshot: string;
|
|
131
|
+
readonly number: number;
|
|
132
|
+
readonly title: string;
|
|
133
|
+
readonly mostraVersion: string;
|
|
134
|
+
readonly out: string;
|
|
135
|
+
}): Promise<string[]> {
|
|
136
|
+
const archive = input.out.endsWith(".tar.zst");
|
|
137
|
+
if (existsSync(input.out)) {
|
|
138
|
+
const empty =
|
|
139
|
+
!archive && (await stat(input.out)).isDirectory() && (await readdir(input.out)).length === 0;
|
|
140
|
+
if (!empty) {
|
|
141
|
+
throw new RpcError(
|
|
142
|
+
createDiagnostic("MOSTRA_ARTIFACT_DIR_NOT_EMPTY", {
|
|
143
|
+
message: `${input.out} already exists; a bundle is written only to a new path or an empty directory.`,
|
|
144
|
+
}),
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const manifest = JSON.parse(
|
|
149
|
+
await readFile(join(input.versionDir, "manifest.json"), "utf8"),
|
|
150
|
+
) as VersionManifest;
|
|
151
|
+
const info: BundleInfo = {
|
|
152
|
+
schemaVersion: 1,
|
|
153
|
+
kind: "mostra-bundle",
|
|
154
|
+
artifactId: manifest.artifactId,
|
|
155
|
+
versionId: manifest.versionId,
|
|
156
|
+
number: input.number,
|
|
157
|
+
title: input.title,
|
|
158
|
+
sourceDigest: manifest.sourceDigest,
|
|
159
|
+
envDigest: manifest.envDigest,
|
|
160
|
+
outputDigest: manifest.outputDigest,
|
|
161
|
+
lockDigest: manifest.environment.lockDigest,
|
|
162
|
+
server: manifest.server,
|
|
163
|
+
mostraVersion: input.mostraVersion,
|
|
164
|
+
exportedAt: new Date().toISOString(),
|
|
165
|
+
};
|
|
166
|
+
const entries: { path: string; data: Uint8Array }[] = [
|
|
167
|
+
{ path: INFO, data: Buffer.from(`${JSON.stringify(info, null, 2)}\n`) },
|
|
168
|
+
];
|
|
169
|
+
for (const path of await filesUnder(input.versionDir)) {
|
|
170
|
+
entries.push({
|
|
171
|
+
path: `${VERSION}/${path}`,
|
|
172
|
+
data: await readFile(join(input.versionDir, ...path.split("/"))),
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
entries.push({ path: SOURCE, data: await readFile(input.snapshot) });
|
|
176
|
+
if (archive) {
|
|
177
|
+
await mkdir(dirname(input.out), { recursive: true });
|
|
178
|
+
const blocks: Buffer[] = [];
|
|
179
|
+
for (const entry of entries) blocks.push(...tarFile({ ...entry, executable: false }));
|
|
180
|
+
blocks.push(TAR_END);
|
|
181
|
+
const temporary = `${input.out}.${process.pid}.tmp`;
|
|
182
|
+
await writeFile(temporary, zstdCompressSync(Buffer.concat(blocks)));
|
|
183
|
+
fsyncPath(temporary);
|
|
184
|
+
await renamePath(temporary, input.out);
|
|
185
|
+
fsyncPath(dirname(input.out), true);
|
|
186
|
+
return [input.out];
|
|
187
|
+
}
|
|
188
|
+
for (const entry of entries) {
|
|
189
|
+
const path = join(input.out, ...entry.path.split("/"));
|
|
190
|
+
await mkdir(dirname(path), { recursive: true });
|
|
191
|
+
await writeFile(path, entry.data);
|
|
192
|
+
}
|
|
193
|
+
return [input.out];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** A bundle read and checked, laid out in a scratch directory ready to be promoted. */
|
|
197
|
+
export interface ReadBundle {
|
|
198
|
+
readonly info: BundleInfo;
|
|
199
|
+
/** The artifact's manifest, as the version records it. */
|
|
200
|
+
readonly manifest: ArtifactManifest;
|
|
201
|
+
/** The checked version directory, inside `scratch`. */
|
|
202
|
+
readonly versionDir: string;
|
|
203
|
+
/** The checked source snapshot, inside `scratch`. */
|
|
204
|
+
readonly snapshot: string;
|
|
205
|
+
readonly forcedCodes: readonly string[];
|
|
206
|
+
/** Removes the scratch directory. */
|
|
207
|
+
dispose(): Promise<void>;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** A bundle path inside the tree: relative, `/`-separated, with no empty, `.` or `..` part. */
|
|
211
|
+
function safeEntry(path: string): boolean {
|
|
212
|
+
if (path === "" || path.startsWith("/") || path.includes("\\") || path.includes("\0"))
|
|
213
|
+
return false;
|
|
214
|
+
return path.split("/").every((part) => part !== "" && part !== "." && part !== "..");
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Reads the bundle at `path` (a directory or a `.tar.zst` archive) into a scratch directory under
|
|
219
|
+
* `scratchRoot` and checks every link of its identity, refusing it with
|
|
220
|
+
* `MOSTRA_ARTIFACT_BUNDLE_INVALID` before anything is kept: `mostra-bundle.json` and the version's
|
|
221
|
+
* own `manifest.json` agree; the lockfile has the environment's `lockDigest`; the environment has
|
|
222
|
+
* `envDigest`; the source snapshot's listing has `sourceDigest`; the builds have `outputDigest`; and
|
|
223
|
+
* the version ID is the one the artifact ID and those digests give (D89).
|
|
224
|
+
*/
|
|
225
|
+
export async function readBundle(path: string, scratchRoot: string): Promise<ReadBundle> {
|
|
226
|
+
// An archive is read through the one handle it was opened with, so the file sized and the file
|
|
227
|
+
// read are the same; a directory is read file by file below.
|
|
228
|
+
let archive: Buffer | undefined;
|
|
229
|
+
try {
|
|
230
|
+
const handle = await open(path, "r");
|
|
231
|
+
try {
|
|
232
|
+
const info = await handle.stat();
|
|
233
|
+
if (!info.isDirectory()) {
|
|
234
|
+
if (info.size > ARCHIVE_MAX) throw invalid(`${path} is larger than a bundle can be.`);
|
|
235
|
+
archive = await handle.readFile();
|
|
236
|
+
}
|
|
237
|
+
} finally {
|
|
238
|
+
await handle.close();
|
|
239
|
+
}
|
|
240
|
+
} catch (error) {
|
|
241
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
242
|
+
if (code === "ENOENT") throw invalid(`${path} doesn't exist.`);
|
|
243
|
+
// Windows opens no directory as a file: it is read as a directory below.
|
|
244
|
+
if (code !== "EISDIR" && code !== "EPERM" && code !== "EACCES") throw error;
|
|
245
|
+
}
|
|
246
|
+
await mkdir(scratchRoot, { recursive: true, mode: 0o700 });
|
|
247
|
+
const scratch = await mkdtemp(join(scratchRoot, "import-"));
|
|
248
|
+
const dispose = (): Promise<void> => removeTree(scratch);
|
|
249
|
+
try {
|
|
250
|
+
if (archive === undefined) {
|
|
251
|
+
for (const file of await filesUnder(path)) {
|
|
252
|
+
const target = join(scratch, ...file.split("/"));
|
|
253
|
+
await mkdir(dirname(target), { recursive: true });
|
|
254
|
+
await writeFile(target, await readFile(join(path, ...file.split("/"))));
|
|
255
|
+
}
|
|
256
|
+
} else {
|
|
257
|
+
let files: ReturnType<typeof readTar>;
|
|
258
|
+
try {
|
|
259
|
+
files = readTar(zstdDecompressSync(archive));
|
|
260
|
+
} catch (error) {
|
|
261
|
+
throw invalid(`${path} isn't a bundle archive: ${(error as Error).message}`);
|
|
262
|
+
}
|
|
263
|
+
const seen = new Set<string>();
|
|
264
|
+
for (const file of files) {
|
|
265
|
+
if (!safeEntry(file.path) || seen.has(file.path.toLowerCase())) {
|
|
266
|
+
throw invalid(`The archive holds an entry it can't: ${JSON.stringify(file.path)}.`);
|
|
267
|
+
}
|
|
268
|
+
seen.add(file.path.toLowerCase());
|
|
269
|
+
const target = join(scratch, ...file.path.split("/"));
|
|
270
|
+
await mkdir(dirname(target), { recursive: true });
|
|
271
|
+
await writeFile(target, file.data);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
const read = async (file: string): Promise<Buffer> => {
|
|
275
|
+
try {
|
|
276
|
+
return await readFile(join(scratch, ...file.split("/")));
|
|
277
|
+
} catch {
|
|
278
|
+
throw invalid(`The bundle has no ${file}.`);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
let info: BundleInfo;
|
|
282
|
+
let version: VersionManifest;
|
|
283
|
+
try {
|
|
284
|
+
info = JSON.parse((await read(INFO)).toString("utf8")) as BundleInfo;
|
|
285
|
+
version = JSON.parse(
|
|
286
|
+
(await read(`${VERSION}/manifest.json`)).toString("utf8"),
|
|
287
|
+
) as VersionManifest;
|
|
288
|
+
} catch (error) {
|
|
289
|
+
if (error instanceof RpcError) throw error;
|
|
290
|
+
throw invalid("Its mostra-bundle.json or its version's manifest.json isn't JSON.");
|
|
291
|
+
}
|
|
292
|
+
if (info.schemaVersion !== 1 || info.kind !== "mostra-bundle") {
|
|
293
|
+
throw invalid("Its mostra-bundle.json isn't one this release reads (schemaVersion 1).");
|
|
294
|
+
}
|
|
295
|
+
if (!Number.isSafeInteger(info.number) || info.number < 1) {
|
|
296
|
+
throw invalid("Its mostra-bundle.json names no version number.");
|
|
297
|
+
}
|
|
298
|
+
for (const key of [
|
|
299
|
+
"artifactId",
|
|
300
|
+
"versionId",
|
|
301
|
+
"sourceDigest",
|
|
302
|
+
"envDigest",
|
|
303
|
+
"outputDigest",
|
|
304
|
+
] as const) {
|
|
305
|
+
if (typeof info[key] !== "string" || info[key] !== version[key]) {
|
|
306
|
+
throw invalid(
|
|
307
|
+
`Its mostra-bundle.json and its version's manifest.json differ in \`${key}\`.`,
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
const manifest = ArtifactManifest.safeParse(version.manifest);
|
|
312
|
+
if (!manifest.success) throw invalid("Its version's artifact manifest isn't valid.");
|
|
313
|
+
// Only a version directory's own files: anything else would be promoted with it unchecked.
|
|
314
|
+
for (const file of await filesUnder(join(scratch, VERSION))) {
|
|
315
|
+
const allowed =
|
|
316
|
+
["manifest.json", "lock.yaml", "registry.json"].includes(file) ||
|
|
317
|
+
file.startsWith("client/") ||
|
|
318
|
+
file.startsWith("server/");
|
|
319
|
+
if (!allowed) throw invalid(`Its version holds a file no version has: ${file}.`);
|
|
320
|
+
}
|
|
321
|
+
if (version.server !== existsSync(join(scratch, VERSION, "server", "entry.mjs"))) {
|
|
322
|
+
throw invalid(
|
|
323
|
+
"Its manifest.json and its server build disagree on whether it has server code.",
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
const lock = await read(`${VERSION}/lock.yaml`);
|
|
327
|
+
if (
|
|
328
|
+
sha256Hex(lock) !== version.environment?.lockDigest ||
|
|
329
|
+
info.lockDigest !== version.environment.lockDigest
|
|
330
|
+
) {
|
|
331
|
+
throw invalid("Its lock.yaml doesn't have the lockDigest its environment records.");
|
|
332
|
+
}
|
|
333
|
+
if (environmentDigest(version.environment) !== version.envDigest) {
|
|
334
|
+
throw invalid("Its environment doesn't have the envDigest it records.");
|
|
335
|
+
}
|
|
336
|
+
let sourceFiles: ReturnType<typeof readTar>;
|
|
337
|
+
try {
|
|
338
|
+
sourceFiles = readTar(zstdDecompressSync(await read(SOURCE)));
|
|
339
|
+
} catch (error) {
|
|
340
|
+
if (error instanceof RpcError) throw error;
|
|
341
|
+
throw invalid("Its source.tar.zst isn't a source snapshot.");
|
|
342
|
+
}
|
|
343
|
+
const sourceDigest = listingDigest(
|
|
344
|
+
sourceFiles.map((file) => ({
|
|
345
|
+
path: file.path,
|
|
346
|
+
executable: file.executable,
|
|
347
|
+
sha256: sha256Hex(file.data),
|
|
348
|
+
})),
|
|
349
|
+
);
|
|
350
|
+
if (sourceDigest !== version.sourceDigest) {
|
|
351
|
+
throw invalid("Its source snapshot doesn't have the sourceDigest it records.");
|
|
352
|
+
}
|
|
353
|
+
// The artifact's manifest is read again from the source, which the digest covers: the copy in
|
|
354
|
+
// manifest.json isn't, and it decides a hosted artifact's settings and its tallies.
|
|
355
|
+
const entry = sourceFiles.find((file) => file.path === version.entry);
|
|
356
|
+
if (!entry) throw invalid(`Its source snapshot has no entry ${String(version.entry)}.`);
|
|
357
|
+
let extracted: ArtifactManifest | undefined;
|
|
358
|
+
try {
|
|
359
|
+
extracted = extractManifest(entry.path, Buffer.from(entry.data).toString("utf8")).manifest;
|
|
360
|
+
} catch {
|
|
361
|
+
throw invalid("Its entry's manifest can't be read.");
|
|
362
|
+
}
|
|
363
|
+
if (extracted?.id !== undefined && extracted.id !== version.artifactId) {
|
|
364
|
+
throw invalid(
|
|
365
|
+
`Its source names the artifact ${extracted.id}, and its version ${version.artifactId}.`,
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
// An entry without a manifest publishes with only a title, its directory's name (D51).
|
|
369
|
+
const matches =
|
|
370
|
+
extracted === undefined
|
|
371
|
+
? Object.keys(manifest.data).join() === "title"
|
|
372
|
+
: canonical(extracted) === canonical(manifest.data);
|
|
373
|
+
if (!matches) {
|
|
374
|
+
throw invalid("Its manifest.json records another artifact manifest than its source has.");
|
|
375
|
+
}
|
|
376
|
+
const versionDir = join(scratch, VERSION);
|
|
377
|
+
if ((await outputDigestOf(versionDir)) !== version.outputDigest) {
|
|
378
|
+
throw invalid("Its builds don't have the outputDigest they record.");
|
|
379
|
+
}
|
|
380
|
+
if (
|
|
381
|
+
versionIdFor(version.artifactId, version.sourceDigest, version.envDigest) !==
|
|
382
|
+
version.versionId
|
|
383
|
+
) {
|
|
384
|
+
throw invalid("Its version ID isn't the one its artifact ID and digests give.");
|
|
385
|
+
}
|
|
386
|
+
return {
|
|
387
|
+
info,
|
|
388
|
+
manifest: manifest.data,
|
|
389
|
+
versionDir,
|
|
390
|
+
snapshot: join(scratch, SOURCE),
|
|
391
|
+
forcedCodes: version.forcedCodes ?? [],
|
|
392
|
+
dispose,
|
|
393
|
+
};
|
|
394
|
+
} catch (error) {
|
|
395
|
+
await dispose().catch(() => {});
|
|
396
|
+
throw error;
|
|
397
|
+
}
|
|
398
|
+
}
|
package/src/publish/manifest.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
ArtifactManifest,
|
|
13
13
|
createDiagnostic,
|
|
14
14
|
type Diagnostic,
|
|
15
|
+
manifestProblem,
|
|
15
16
|
RpcError,
|
|
16
17
|
} from "@mostra/protocol";
|
|
17
18
|
import type * as Oxc from "oxc-parser";
|
|
@@ -316,6 +317,14 @@ function validate(
|
|
|
316
317
|
at,
|
|
317
318
|
);
|
|
318
319
|
}
|
|
320
|
+
const problem = manifestProblem(checked.data);
|
|
321
|
+
if (problem) {
|
|
322
|
+
return x.fail(
|
|
323
|
+
"MOSTRA_MANIFEST_INVALID",
|
|
324
|
+
`The manifest field \`${problem.path}\` is invalid: ${problem.message}.`,
|
|
325
|
+
at,
|
|
326
|
+
);
|
|
327
|
+
}
|
|
319
328
|
return { manifest: checked.data, warnings };
|
|
320
329
|
}
|
|
321
330
|
|
package/src/publish/methods.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The authoring methods of the host protocol (INTERFACES §6): `artifact/create`,
|
|
3
|
-
* `artifact/publish`, `artifact/list`, `artifact/get
|
|
3
|
+
* `artifact/publish`, `artifact/list`, `artifact/get`, `deps/add`, and a bundle's
|
|
4
|
+
* `artifact/export` and `artifact/import` (RM-01).
|
|
4
5
|
*/
|
|
5
6
|
import { realpathSync } from "node:fs";
|
|
6
7
|
import { basename, dirname, join, resolve } from "node:path";
|
|
@@ -8,7 +9,9 @@ import {
|
|
|
8
9
|
ARTIFACT_PROGRESS,
|
|
9
10
|
type ArtifactRef,
|
|
10
11
|
artifactCreateMethod,
|
|
12
|
+
artifactExportMethod,
|
|
11
13
|
artifactGetMethod,
|
|
14
|
+
artifactImportMethod,
|
|
12
15
|
artifactListMethod,
|
|
13
16
|
artifactPublishMethod,
|
|
14
17
|
artifactVerifyMethod,
|
|
@@ -92,6 +95,14 @@ export function artifactMethods(options: {
|
|
|
92
95
|
: undefined;
|
|
93
96
|
return options.publisher().verify(params, progress);
|
|
94
97
|
}),
|
|
98
|
+
handler(artifactExportMethod, (params) =>
|
|
99
|
+
options.publisher().exportBundle({ ...params, artifact: canonicalRef(params.artifact) }),
|
|
100
|
+
),
|
|
101
|
+
handler(artifactImportMethod, async (params) => {
|
|
102
|
+
const receipt = await options.publisher().importBundle(params);
|
|
103
|
+
options.published?.(receipt.artifactId);
|
|
104
|
+
return options.link ? { ...receipt, ...(await options.link(receipt.artifactId)) } : receipt;
|
|
105
|
+
}),
|
|
95
106
|
handler(artifactListMethod, (params) => ({ artifacts: registry.list(params.filter ?? {}) })),
|
|
96
107
|
handler(artifactGetMethod, (params) => {
|
|
97
108
|
return registry.get(canonicalRef(params.artifact));
|
package/src/publish/publisher.ts
CHANGED
|
@@ -19,6 +19,7 @@ import { createHash } from "node:crypto";
|
|
|
19
19
|
import { createReadStream, createWriteStream, existsSync, readFileSync } from "node:fs";
|
|
20
20
|
import {
|
|
21
21
|
chmod,
|
|
22
|
+
copyFile,
|
|
22
23
|
mkdir,
|
|
23
24
|
mkdtemp,
|
|
24
25
|
readdir,
|
|
@@ -35,7 +36,10 @@ import { createZstdCompress } from "node:zlib";
|
|
|
35
36
|
import {
|
|
36
37
|
type ArtifactCreateParams,
|
|
37
38
|
type ArtifactCreateResult,
|
|
39
|
+
type ArtifactExportParams,
|
|
40
|
+
type ArtifactExportResult,
|
|
38
41
|
ArtifactId,
|
|
42
|
+
type ArtifactImportParams,
|
|
39
43
|
type ArtifactManifest,
|
|
40
44
|
type ArtifactPublishParams,
|
|
41
45
|
type ArtifactVerifyParams,
|
|
@@ -72,6 +76,7 @@ import {
|
|
|
72
76
|
VERIFY_REVISION,
|
|
73
77
|
type Verifier,
|
|
74
78
|
} from "../verify/index.ts";
|
|
79
|
+
import { readBundle, writeBundle } from "./bundle.ts";
|
|
75
80
|
import {
|
|
76
81
|
type BuildEnvironment,
|
|
77
82
|
canonicalJson,
|
|
@@ -130,6 +135,10 @@ export interface Publisher {
|
|
|
130
135
|
create(params: ArtifactCreateParams): Promise<ArtifactCreateResult>;
|
|
131
136
|
/** `deps/add`: adds packages to an artifact's working lockfile (D95). */
|
|
132
137
|
addDependencies(params: DepsAddParams): Promise<DepsAddResult>;
|
|
138
|
+
/** `artifact/export`: writes a version's portable bundle (RM-01). */
|
|
139
|
+
exportBundle(params: ArtifactExportParams): Promise<ArtifactExportResult>;
|
|
140
|
+
/** `artifact/import`: makes a bundle a version of this home, with its identity (RM-01). */
|
|
141
|
+
importBundle(params: ArtifactImportParams): Promise<PublishReceipt>;
|
|
133
142
|
}
|
|
134
143
|
|
|
135
144
|
const ENTRY_NAMES = ["index.tsx", "index.jsx", "index.mdx"];
|
|
@@ -867,8 +876,126 @@ export function createPublisher(options: PublisherOptions): Publisher {
|
|
|
867
876
|
// Runs in the source path's queue, like its publishes, so a publish that binds the path never
|
|
868
877
|
// races an addition to the path-keyed lockfile it re-keys.
|
|
869
878
|
addDependencies: (params) => gated(() => addDependencies(params)),
|
|
879
|
+
// gc removes files no kept version names: an export reads a version's files, and an import
|
|
880
|
+
// writes them, so both hold the gate shared like a publish (D140 item 14).
|
|
881
|
+
exportBundle: (params) => gated(() => exportBundle(params)),
|
|
882
|
+
importBundle: (params) =>
|
|
883
|
+
gated(() => idempotent(requestKey("artifact/import", params), () => importBundle(params))),
|
|
870
884
|
};
|
|
871
885
|
|
|
886
|
+
async function exportBundle(params: ArtifactExportParams): Promise<ArtifactExportResult> {
|
|
887
|
+
// `artifact` arrives canonical: the method's handler made it so (canonicalRef).
|
|
888
|
+
const detail = registry.get(params.artifact);
|
|
889
|
+
const wanted = params.version;
|
|
890
|
+
const summary =
|
|
891
|
+
wanted === undefined
|
|
892
|
+
? detail.latestVersion
|
|
893
|
+
: detail.versions.find((version) =>
|
|
894
|
+
typeof wanted === "number" ? version.number === wanted : version.id === wanted,
|
|
895
|
+
);
|
|
896
|
+
if (!summary) {
|
|
897
|
+
throw new RpcError(
|
|
898
|
+
createDiagnostic("MOSTRA_ARTIFACT_VERSION_NOT_FOUND", {
|
|
899
|
+
message: `${detail.title} has no version ${String(wanted ?? "")}.`,
|
|
900
|
+
}),
|
|
901
|
+
);
|
|
902
|
+
}
|
|
903
|
+
if (summary.collected !== undefined) {
|
|
904
|
+
throw new RpcError(
|
|
905
|
+
createDiagnostic("MOSTRA_ARTIFACT_VERSION_COLLECTED", {
|
|
906
|
+
message: `Version ${summary.number} of ${detail.title} was collected; publish its source again to export it.`,
|
|
907
|
+
}),
|
|
908
|
+
);
|
|
909
|
+
}
|
|
910
|
+
const versionDir = registry.store.versionDir(summary.id);
|
|
911
|
+
const damage = await versionDamage(versionDir);
|
|
912
|
+
if (damage !== undefined) {
|
|
913
|
+
throw new RpcError(
|
|
914
|
+
createDiagnostic("MOSTRA_HOME_INTEGRITY", {
|
|
915
|
+
message: `Version ${summary.number} of ${detail.title} is damaged: ${damage}.`,
|
|
916
|
+
}),
|
|
917
|
+
);
|
|
918
|
+
}
|
|
919
|
+
const files = await writeBundle({
|
|
920
|
+
versionDir,
|
|
921
|
+
snapshot: registry.store.snapshotPath(detail.id, summary.sourceDigest),
|
|
922
|
+
number: summary.number,
|
|
923
|
+
title: detail.title,
|
|
924
|
+
mostraVersion: options.runtimeVersion,
|
|
925
|
+
out: params.out,
|
|
926
|
+
});
|
|
927
|
+
log.info("publish.exported", { artifactId: detail.id, versionId: summary.id });
|
|
928
|
+
return { files, versionId: summary.id };
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
async function importBundle(params: ArtifactImportParams): Promise<PublishReceipt> {
|
|
932
|
+
const read = await readBundle(params.bundle, registry.store.stagingDir);
|
|
933
|
+
const { info } = read;
|
|
934
|
+
return queue.run(`artifact:${info.artifactId}`, async () => {
|
|
935
|
+
const undo: (() => Promise<void>)[] = [];
|
|
936
|
+
let committed = false;
|
|
937
|
+
try {
|
|
938
|
+
if (!registry.hasVersion(info.versionId)) {
|
|
939
|
+
const archive = registry.store.snapshotPath(info.artifactId, info.sourceDigest);
|
|
940
|
+
if (!existsSync(archive)) {
|
|
941
|
+
await mkdir(dirname(archive), { recursive: true, mode: 0o700 });
|
|
942
|
+
await copyFile(read.snapshot, archive);
|
|
943
|
+
fsyncPath(archive);
|
|
944
|
+
fsyncPath(dirname(archive), true);
|
|
945
|
+
undo.push(() => removeTree(archive));
|
|
946
|
+
}
|
|
947
|
+
undo.push(() => removeTree(registry.store.versionDir(info.versionId)));
|
|
948
|
+
await registry.store.promote(read.versionDir, info.versionId);
|
|
949
|
+
}
|
|
950
|
+
const result = registry.commit({
|
|
951
|
+
artifactId: info.artifactId,
|
|
952
|
+
bindingPath: undefined,
|
|
953
|
+
manifestId: read.manifest.id,
|
|
954
|
+
versionId: info.versionId,
|
|
955
|
+
sourceDigest: info.sourceDigest,
|
|
956
|
+
envDigest: info.envDigest,
|
|
957
|
+
outputDigest: info.outputDigest,
|
|
958
|
+
manifest: read.manifest,
|
|
959
|
+
number: info.number,
|
|
960
|
+
policy: undefined,
|
|
961
|
+
homePolicy: options.homePolicy?.(),
|
|
962
|
+
forcedCodes: read.forcedCodes,
|
|
963
|
+
// Another home's verdict isn't this one's: an imported version records none, so it
|
|
964
|
+
// counts as passing here only once verify passes here (D138 item 4).
|
|
965
|
+
stillClaims: () => false,
|
|
966
|
+
faults,
|
|
967
|
+
request: requestKey("artifact/import", params),
|
|
968
|
+
});
|
|
969
|
+
committed = true;
|
|
970
|
+
log.info("publish.imported", {
|
|
971
|
+
artifactId: info.artifactId,
|
|
972
|
+
versionId: info.versionId,
|
|
973
|
+
version: result.version,
|
|
974
|
+
reused: result.reused,
|
|
975
|
+
});
|
|
976
|
+
return {
|
|
977
|
+
artifactId: info.artifactId,
|
|
978
|
+
versionId: info.versionId,
|
|
979
|
+
version: result.version,
|
|
980
|
+
cursor: result.cursor,
|
|
981
|
+
reused: result.reused,
|
|
982
|
+
...(read.forcedCodes.length > 0 ? { forcedDiagnostics: [...read.forcedCodes] } : {}),
|
|
983
|
+
};
|
|
984
|
+
} catch (error) {
|
|
985
|
+
if (!committed) {
|
|
986
|
+
for (const step of undo.reverse()) {
|
|
987
|
+
await step().catch((cleanup: unknown) =>
|
|
988
|
+
log.warn("publish.undo_failed", { error: cleanup }),
|
|
989
|
+
);
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
throw error;
|
|
993
|
+
} finally {
|
|
994
|
+
await read.dispose().catch(() => {});
|
|
995
|
+
}
|
|
996
|
+
});
|
|
997
|
+
}
|
|
998
|
+
|
|
872
999
|
async function addDependencies(params: DepsAddParams): Promise<DepsAddResult> {
|
|
873
1000
|
{
|
|
874
1001
|
const path = pathOf(params.artifact);
|