@mostra/sdk 0.6.0 → 0.6.1

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/bundles.json CHANGED
@@ -1,43 +1,43 @@
1
1
  {
2
2
  "schemaVersion": 1,
3
- "version": "0.6.0",
4
- "release": "https://github.com/mostra-dev/mostra/releases/download/v0.6.0",
3
+ "version": "0.6.1",
4
+ "release": "https://github.com/mostra-dev/mostra/releases/download/v0.6.1",
5
5
  "assets": {
6
6
  "darwin-arm64": {
7
- "file": "mostra-0.6.0-darwin-arm64.tar.gz",
8
- "sha256": "bd2eca1dce839b2c5a255bb47911b6c677ef6e6d14e804e7e628d4e70dcb1c25",
9
- "size": 198907941,
10
- "unpackedSize": 782604771
7
+ "file": "mostra-0.6.1-darwin-arm64.tar.gz",
8
+ "sha256": "8bfea575be1b4f694089fe31f9763573e5541cfe00d6983548e8dc93f985c3d0",
9
+ "size": 199785468,
10
+ "unpackedSize": 786411680
11
11
  },
12
12
  "darwin-x64": {
13
- "file": "mostra-0.6.0-darwin-x64.tar.gz",
14
- "sha256": "056c8bbd63d7dbc5403d3210ffef8b338afcb686d2d104719e116865b4fdd1ae",
15
- "size": 201977924,
16
- "unpackedSize": 790702635
13
+ "file": "mostra-0.6.1-darwin-x64.tar.gz",
14
+ "sha256": "656d63dbda3d7d056e0d7c7572c7f67ac17f0dae2959b9c0acf311d9d210adee",
15
+ "size": 202877688,
16
+ "unpackedSize": 794533344
17
17
  },
18
18
  "linux-x64": {
19
- "file": "mostra-0.6.0-linux-x64.tar.gz",
20
- "sha256": "cd228fbb3afefced20a7014663f8e812d74f5b38bf1d1aa0b8fd54e04a919fd4",
21
- "size": 206200892,
22
- "unpackedSize": 793561127
19
+ "file": "mostra-0.6.1-linux-x64.tar.gz",
20
+ "sha256": "a5988f2f13c8b7e21186fa669f792d7e2d5c56a706c1357a953ffd3a98a5947c",
21
+ "size": 207123938,
22
+ "unpackedSize": 797227236
23
23
  },
24
24
  "linux-arm64": {
25
- "file": "mostra-0.6.0-linux-arm64.tar.gz",
26
- "sha256": "8c7b60ebbb0b8ecf7825dd2eda3c04717d7ab6cbbb21a80c16b4abc63703440e",
27
- "size": 204297978,
28
- "unpackedSize": 783869080
25
+ "file": "mostra-0.6.1-linux-arm64.tar.gz",
26
+ "sha256": "00a28cf602a1f59a37c4368a2deb9d8f877df9ff0aa133d51b389ad16423a74c",
27
+ "size": 205201463,
28
+ "unpackedSize": 787726293
29
29
  },
30
30
  "win32-x64": {
31
- "file": "mostra-0.6.0-win32-x64.tar.gz",
32
- "sha256": "b08acec68a28edf99d542f571370b8efc0ec323778bfc4ebb15c666003825de1",
33
- "size": 185939146,
34
- "unpackedSize": 701209852
31
+ "file": "mostra-0.6.1-win32-x64.tar.gz",
32
+ "sha256": "4c8e41d18b3e2b703d51cd548d7c31e188137b0097de806e286993011d7a1e41",
33
+ "size": 186740204,
34
+ "unpackedSize": 704848844
35
35
  },
36
36
  "win32-arm64": {
37
- "file": "mostra-0.6.0-win32-arm64.tar.gz",
38
- "sha256": "200b2b94419992e9fe4d9b3b7c5f63b63b4a212be29b1db5c12e759d01bd673e",
39
- "size": 180062833,
40
- "unpackedSize": 682020663
37
+ "file": "mostra-0.6.1-win32-arm64.tar.gz",
38
+ "sha256": "c6ce052417bda23d98ae5f73165a63cfa0a3e1b63f0daff6f4fd8016312523f3",
39
+ "size": 180845692,
40
+ "unpackedSize": 685580295
41
41
  }
42
42
  }
43
43
  }
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { C as safeRelative, S as linkStaysInside, T as redactUrl, _ as setupBundle, a as sdkVersion, b as describeTree, c as bundleDir, d as browsersDir, f as checkBrowser, g as readSetup, h as readBundle, i as runtimeOf, l as cacheRoot, m as offline, n as readPins, o as bundlePlatform, p as checkFiles, r as resolveRuntime, s as currentPlatform, t as enclosingBundle, u as installPinned, v as setupExtras, w as childEnvironment, x as hashFile, y as checkTree } from "./runtime-BCvS_ISH.js";
1
+ import { C as safeRelative, S as linkStaysInside, T as redactUrl, _ as setupBundle, a as sdkVersion, b as describeTree, c as bundleDir, d as browsersDir, f as checkBrowser, g as readSetup, h as readBundle, i as runtimeOf, l as cacheRoot, m as offline, n as readPins, o as bundlePlatform, p as checkFiles, r as resolveRuntime, s as currentPlatform, t as enclosingBundle, u as installPinned, v as setupExtras, w as childEnvironment, x as hashFile, y as checkTree } from "./runtime-NiLgW1ED.js";
2
2
  import { dirname, join, resolve } from "node:path";
3
3
  import { ARTIFACT_PROGRESS, EVENTS_NOTIFY, EventsNotifyParams, HOST_PROTOCOL_REVISIONS, LineSplitter, RpcError, RpcPeer, WAIT_MAX_MS, artifactPublishMethod, artifactVerifyMethod, createDiagnostic, encodeLine, eventsAckMethod, eventsSubscribeMethod, eventsUnsubscribeMethod, livePushMethod, mostraInitializeMethod, portDraftsMirrorMethod, portIdentityResolveMethod, portSecretsGetMethod, portSubmissionsDeliverMethod, portTrustedControlsActivatedMethod, portTrustedControlsPolicyMethod, receivable, resultsGetMethod, stateGetMethod, statePatchMethod, viewUrlMethod, waitMethod } from "@mostra/protocol";
4
4
  import { existsSync, realpathSync } from "node:fs";
@@ -3,12 +3,12 @@ import { link, lstat, mkdir, open, readFile, readdir, readlink, rename, rm, rmdi
3
3
  import { homedir } from "node:os";
4
4
  import { dirname, isAbsolute, join, posix, relative, resolve, sep, win32 } from "node:path";
5
5
  import { BUNDLE_PLATFORMS, BUNDLE_SETUP_MARKER, BundleAsset, BundleFiles, BundleInfo, BundlePins, BundleSetup, RpcError, bundleAssetName, createDiagnostic, isWindowsBundle } from "@mostra/protocol";
6
- import { createReadStream, createWriteStream, existsSync, readFileSync, realpathSync } from "node:fs";
6
+ import { closeSync, createReadStream, createWriteStream, existsSync, mkdirSync, openSync, readFileSync, realpathSync, symlinkSync, writeSync } from "node:fs";
7
7
  import { Readable } from "node:stream";
8
8
  import { pipeline } from "node:stream/promises";
9
9
  import { fileURLToPath } from "node:url";
10
10
  import { EnvHttpProxyAgent, fetch } from "undici";
11
- import { x } from "tar";
11
+ import { Parser } from "tar";
12
12
  import { spawn } from "node:child_process";
13
13
  //#region src/bundle/download.ts
14
14
  /**
@@ -175,49 +175,174 @@ function delay(ms, signal) {
175
175
  //#endregion
176
176
  //#region src/bundle/extract.ts
177
177
  /**
178
- * Unpacking a downloaded archive (RT-01). Only regular files, directories and symbolic links are
179
- * accepted, all under the archive's one top directory, which is stripped. node-tar refuses
180
- * absolute paths, `..` and writing through a link; it does create a link whatever its target, so
181
- * setup then checks the whole tree against the bundle's file list (`checkTree`), which refuses any
182
- * file or link it doesn't list and any link that leaves the bundle. A pinned archive's SHA-256 is
183
- * checked before any of this. A Windows archive holds no links at all (D153): one that does is
184
- * refused, since its junctions are made by setup, from `files.json`.
178
+ * Unpacking a downloaded archive (RT-01) into a new, empty directory. Only regular files,
179
+ * directories and symbolic links are accepted, all under the archive's one top directory, which is
180
+ * stripped; an entry's path must be relative, with no `..`, `.` or empty part and no backslash
181
+ * (nor, on Windows, a colon; node-tar reads a backslash there as a separator, so its parts are
182
+ * judged as separate ones), and no path may appear twice. Links are made only after every file
183
+ * and directory is written, and after every link's own directory, so no write and no link ever
184
+ * goes through one. Setup then checks the whole tree
185
+ * against the bundle's file list (`checkTree`), which refuses any file or link it doesn't list and
186
+ * any link that leaves the bundle. A pinned archive's SHA-256 is checked before any of this. A
187
+ * Windows archive holds no links at all (D153): one that does is refused, since its junctions are
188
+ * made by setup, from `files.json`.
189
+ *
190
+ * The files are written synchronously as node-tar's parser reads them. node-tar's own `x` checks
191
+ * and clobbers what each path already holds, and on Windows writes one entry at a time; into an
192
+ * empty directory none of that is needed, and a first install wrote its ~50,000 files 4 to 10
193
+ * times slower for it: 6 minutes on Windows arm64 (#88, VERIFIED_FACTS, MT-02).
185
194
  */
186
- const ACCEPTED = /* @__PURE__ */ new Set([
195
+ const FILES = /* @__PURE__ */ new Set([
187
196
  "File",
188
197
  "OldFile",
189
- "ContiguousFile",
190
- "Directory",
191
- "SymbolicLink"
198
+ "ContiguousFile"
192
199
  ]);
200
+ /** An entry a bundle's archive never has, or an archive that doesn't read as a tar at all. */
201
+ var ArchiveRefused = class extends Error {};
202
+ /**
203
+ * Whether `error` is two of the archive's entries meeting where the file system names them as one
204
+ * path: a file where a directory must go, a directory or file already there, names the file system
205
+ * folds alike (`σ` and `ς`, `ß` and `ss`). The directory is new and only this extractor writes in
206
+ * it, so these are never the disk's failure (the Opus review of MT-02).
207
+ */
208
+ function collides(error) {
209
+ const code = error.code;
210
+ return code === "EEXIST" || code === "ENOTDIR" || code === "EISDIR";
211
+ }
193
212
  /** Extracts `archive` (a `.tar.gz` with one top directory `top`) into the empty directory `into`. */
194
213
  async function extractBundle(archive, into, top, options = {}) {
195
- const accepted = options.links === "refuse" ? new Set([...ACCEPTED].filter((type) => type !== "SymbolicLink")) : ACCEPTED;
196
- let refused;
197
214
  try {
198
- await x({
199
- file: archive,
200
- cwd: into,
201
- strip: 1,
202
- strict: true,
203
- preservePaths: false,
204
- preserveOwner: false,
205
- noChmod: false,
206
- filter: (path, entry) => {
207
- const type = "type" in entry ? String(entry.type) : "File";
208
- if (!(path === `${top}/` || path === top || path.startsWith(`${top}/`)) || !accepted.has(type)) {
209
- refused ??= `${path} (${type})`;
210
- return false;
211
- }
212
- return true;
213
- }
214
- });
215
+ await unpack(archive, resolve(into), top, options.links === "refuse");
215
216
  } catch (error) {
217
+ if (error instanceof ArchiveRefused) throw new RpcError(createDiagnostic("MOSTRA_ENV_BUNDLE_INVALID", { message: error.message }));
216
218
  const code = error.code;
217
219
  if (typeof code === "string" && /^E[A-Z]+$/.test(code) && code !== "EOF") throw new RpcError(createDiagnostic("MOSTRA_ENV_BUNDLE_DOWNLOAD_FAILED", { message: `The runtime bundle couldn't be written to ${into}: ${error.message}${code === "ENOSPC" ? " (the disk is full)" : ""}` }));
218
220
  throw new RpcError(createDiagnostic("MOSTRA_ENV_BUNDLE_INVALID", { message: `The runtime bundle's archive couldn't be read: ${error.message}` }));
219
221
  }
220
- if (refused !== void 0) throw new RpcError(createDiagnostic("MOSTRA_ENV_BUNDLE_INVALID", { message: `The runtime bundle's archive holds an entry a bundle never has: ${refused}.` }));
222
+ }
223
+ /** The path below `top` an entry names, as parts, or why it is refused. */
224
+ function entryParts(path, top, windows) {
225
+ const parts = path.split("/");
226
+ if (parts[0] !== top) return "it is outside the bundle's directory";
227
+ if (parts.length > 1 && parts.at(-1) === "") parts.pop();
228
+ const below = parts.slice(1);
229
+ for (const part of below) {
230
+ if (part === "" || part === "." || part === "..") return "its path isn't a plain relative one";
231
+ if (part.includes("\\") || windows && part.includes(":")) return "its path holds a character a bundle's paths never have";
232
+ }
233
+ return below;
234
+ }
235
+ async function unpack(archive, root, top, refuseLinks) {
236
+ const windows = process.platform === "win32";
237
+ const seen = /* @__PURE__ */ new Set();
238
+ const made = /* @__PURE__ */ new Set([root]);
239
+ const links = [];
240
+ const ensureDir = (dir) => {
241
+ if (made.has(dir)) return;
242
+ mkdirSync(dir, {
243
+ recursive: true,
244
+ mode: 493
245
+ });
246
+ for (let at = dir; !made.has(at); at = dirname(at)) made.add(at);
247
+ };
248
+ let failure;
249
+ let open;
250
+ const fail = (error) => {
251
+ failure ??= error;
252
+ };
253
+ const refuse = (path, type, why) => fail(new ArchiveRefused(`The runtime bundle's archive holds an entry a bundle never has: ${path} (${type}): ${why}.`));
254
+ const parser = new Parser({ strict: true });
255
+ parser.on("entry", (entry) => {
256
+ const type = String(entry.type);
257
+ if (failure !== void 0) return entry.resume();
258
+ const parts = entryParts(entry.path, top, windows);
259
+ if (typeof parts === "string") {
260
+ refuse(entry.path, type, parts);
261
+ return entry.resume();
262
+ }
263
+ const path = parts.length === 0 ? root : join(root, ...parts);
264
+ const key = windows || process.platform === "darwin" ? path.normalize("NFC").toLowerCase() : path;
265
+ if (parts.length > 0 && seen.has(key)) {
266
+ refuse(entry.path, type, "its path appears twice");
267
+ return entry.resume();
268
+ }
269
+ seen.add(key);
270
+ try {
271
+ if (type === "Directory") {
272
+ entry.resume();
273
+ ensureDir(path);
274
+ } else if (type === "SymbolicLink" && parts.length > 0) {
275
+ entry.resume();
276
+ if (refuseLinks) refuse(entry.path, type, "a Windows bundle's links are made by setup");
277
+ else if (!entry.linkpath) refuse(entry.path, type, "it links to nothing");
278
+ else links.push({
279
+ path,
280
+ target: entry.linkpath
281
+ });
282
+ } else if (FILES.has(type) && parts.length > 0) {
283
+ ensureDir(dirname(path));
284
+ const fd = openSync(path, "wx", (entry.mode ?? 0) & 73 ? 493 : 420);
285
+ open = fd;
286
+ entry.on("data", (chunk) => {
287
+ if (failure !== void 0) return;
288
+ try {
289
+ let at = 0;
290
+ while (at < chunk.length) at += writeSync(fd, chunk, at);
291
+ } catch (error) {
292
+ fail(error);
293
+ }
294
+ });
295
+ entry.on("end", () => {
296
+ open = void 0;
297
+ try {
298
+ closeSync(fd);
299
+ } catch (error) {
300
+ fail(error);
301
+ }
302
+ });
303
+ } else {
304
+ refuse(entry.path, type, "a bundle holds only files, directories and links");
305
+ entry.resume();
306
+ }
307
+ } catch (error) {
308
+ if (collides(error)) refuse(entry.path, type, "its path meets another entry's, as this file system names them");
309
+ else fail(error);
310
+ entry.resume();
311
+ }
312
+ });
313
+ const ended = new Promise((done) => {
314
+ parser.on("end", done);
315
+ parser.on("error", (error) => {
316
+ fail(new ArchiveRefused(`The runtime bundle's archive couldn't be read: ${error.message}`));
317
+ done();
318
+ });
319
+ });
320
+ try {
321
+ for await (const chunk of createReadStream(archive, { highWaterMark: 1048576 })) {
322
+ parser.write(chunk);
323
+ if (failure !== void 0) break;
324
+ }
325
+ if (failure === void 0) {
326
+ parser.end();
327
+ await ended;
328
+ }
329
+ } finally {
330
+ if (open !== void 0) closeSync(open);
331
+ }
332
+ if (failure !== void 0) throw failure;
333
+ const refused = (path, why) => new ArchiveRefused(`The runtime bundle's archive holds an entry a bundle never has: ${path} (SymbolicLink): ${why}.`);
334
+ for (const link of links) try {
335
+ ensureDir(dirname(link.path));
336
+ } catch (error) {
337
+ if (collides(error)) throw refused(link.path, "it lies beneath a file");
338
+ throw error;
339
+ }
340
+ for (const link of links) try {
341
+ symlinkSync(link.target, link.path);
342
+ } catch (error) {
343
+ if (collides(error)) throw refused(link.path, "other entries are inside it, or meet its path");
344
+ throw error;
345
+ }
221
346
  }
222
347
  //#endregion
223
348
  //#region src/environment.ts
@@ -1028,40 +1153,65 @@ function withoutTrailingSlashes(url) {
1028
1153
  return url.slice(0, end);
1029
1154
  }
1030
1155
  /**
1031
- * Removes the lock at `path` when it is stale (its owner gone, or older than an hour) and says
1032
- * whether it did. The lock is read and judged through one descriptor, then moved aside, and only
1033
- * the file judged is removed: a fresh lock another installer took in between is put back (or,
1034
- * if a third took one meanwhile, left to the next round). The lock only saves a second download;
1035
- * the scratch directory and its rename keep the cache correct whatever happens here.
1156
+ * Judges the lock at `path` through one descriptor: `gone`, `live`, or stale (its owner gone, or
1157
+ * older than an hour) with what identifies the file judged.
1036
1158
  */
1037
- async function takeOverIfStale(path) {
1159
+ async function judgeLock(path) {
1038
1160
  let handle;
1039
1161
  try {
1040
1162
  handle = await open(path, "r");
1041
1163
  } catch {
1042
- return true;
1164
+ return "gone";
1043
1165
  }
1044
- let judged;
1045
1166
  try {
1046
1167
  const info = await handle.stat();
1047
- const owner = Number.parseInt(await handle.readFile("utf8"), 10);
1048
- if (!(Date.now() - info.mtimeMs > STALE_LOCK_MS || Number.isInteger(owner) && owner > 0 && !alive(owner))) return false;
1049
- judged = {
1168
+ const text = await handle.readFile("utf8");
1169
+ const owner = Number.parseInt(text, 10);
1170
+ return Date.now() - info.mtimeMs > STALE_LOCK_MS || Number.isInteger(owner) && owner > 0 && !alive(owner) ? {
1050
1171
  ino: info.ino,
1051
- dev: info.dev
1052
- };
1172
+ dev: info.dev,
1173
+ text
1174
+ } : "live";
1053
1175
  } finally {
1054
1176
  await handle.close();
1055
1177
  }
1178
+ }
1179
+ /**
1180
+ * Moves the lock at `path` aside and removes it only if it is the file `judged` describes, by
1181
+ * identity and by contents: a fresh lock another installer took in between is put back (or, if a
1182
+ * third took one meanwhile, left to the next round). The contents matter because a file system may
1183
+ * give a new file the number of one just removed (ext4 does at once), and each lock holds its
1184
+ * owner's PID and its own token (MT-02).
1185
+ */
1186
+ async function removeJudged(path, judged) {
1056
1187
  const aside = `${path}.stale-${randomBytes(6).toString("hex")}`;
1057
1188
  try {
1058
1189
  await rename(path, aside);
1059
1190
  } catch {
1060
- return true;
1191
+ return;
1061
1192
  }
1062
- const moved = await stat(aside).catch(() => void 0);
1063
- if (moved && (moved.ino !== judged.ino || moved.dev !== judged.dev)) await link(aside, path).catch(() => void 0);
1193
+ let same = false;
1194
+ const handle = await open(aside, "r").catch(() => void 0);
1195
+ if (handle) try {
1196
+ const moved = await handle.stat();
1197
+ const text = await handle.readFile("utf8");
1198
+ same = moved.ino === judged.ino && moved.dev === judged.dev && text === judged.text;
1199
+ } catch {} finally {
1200
+ await handle.close();
1201
+ }
1202
+ if (!same) await link(aside, path).catch(() => void 0);
1064
1203
  await rm(aside, { force: true });
1204
+ }
1205
+ /**
1206
+ * Removes the lock at `path` when it is stale and says whether it may be taken now. The lock only
1207
+ * saves a second download; the scratch directory and its rename keep the cache correct whatever
1208
+ * happens here.
1209
+ */
1210
+ async function takeOverIfStale(path) {
1211
+ const judged = await judgeLock(path);
1212
+ if (judged === "gone") return true;
1213
+ if (judged === "live") return false;
1214
+ await removeJudged(path, judged);
1065
1215
  return true;
1066
1216
  }
1067
1217
  /**
@@ -1301,4 +1451,4 @@ async function resolveRuntime(options = {}) {
1301
1451
  //#endregion
1302
1452
  export { safeRelative as C, linkStaysInside as S, redactUrl as T, setupBundle as _, sdkVersion as a, describeTree as b, bundleDir as c, browsersDir as d, checkBrowser as f, readSetup as g, readBundle as h, runtimeOf as i, cacheRoot as l, offline as m, readPins as n, bundlePlatform as o, checkFiles as p, resolveRuntime as r, currentPlatform as s, enclosingBundle as t, installPinned as u, setupExtras as v, childEnvironment as w, hashFile as x, checkTree as y };
1303
1453
 
1304
- //# sourceMappingURL=runtime-BCvS_ISH.js.map
1454
+ //# sourceMappingURL=runtime-NiLgW1ED.js.map