@mostra/sdk 0.5.0 → 0.6.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.
@@ -1,9 +1,9 @@
1
1
  import { createHash, randomBytes } from "node:crypto";
2
- import { link, lstat, mkdir, open, readFile, readdir, readlink, rename, rm, stat, statfs, utimes, writeFile } from "node:fs/promises";
2
+ import { link, lstat, mkdir, open, readFile, readdir, readlink, rename, rm, rmdir, stat, statfs, symlink, unlink, utimes, writeFile } from "node:fs/promises";
3
3
  import { homedir } from "node:os";
4
- import { dirname, isAbsolute, join, resolve, sep } from "node:path";
5
- import { BUNDLE_PLATFORMS, BUNDLE_SETUP_MARKER, BundleAsset, BundleFiles, BundleInfo, BundlePins, BundleSetup, RpcError, bundleAssetName, createDiagnostic } from "@mostra/protocol";
6
- import { createReadStream, createWriteStream, existsSync, readFileSync } from "node:fs";
4
+ import { dirname, isAbsolute, join, posix, relative, resolve, sep, win32 } from "node:path";
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";
7
7
  import { Readable } from "node:stream";
8
8
  import { pipeline } from "node:stream/promises";
9
9
  import { fileURLToPath } from "node:url";
@@ -180,7 +180,8 @@ function delay(ms, signal) {
180
180
  * absolute paths, `..` and writing through a link; it does create a link whatever its target, so
181
181
  * setup then checks the whole tree against the bundle's file list (`checkTree`), which refuses any
182
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.
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`.
184
185
  */
185
186
  const ACCEPTED = /* @__PURE__ */ new Set([
186
187
  "File",
@@ -190,7 +191,8 @@ const ACCEPTED = /* @__PURE__ */ new Set([
190
191
  "SymbolicLink"
191
192
  ]);
192
193
  /** Extracts `archive` (a `.tar.gz` with one top directory `top`) into the empty directory `into`. */
193
- async function extractBundle(archive, into, top) {
194
+ async function extractBundle(archive, into, top, options = {}) {
195
+ const accepted = options.links === "refuse" ? new Set([...ACCEPTED].filter((type) => type !== "SymbolicLink")) : ACCEPTED;
194
196
  let refused;
195
197
  try {
196
198
  await x({
@@ -203,7 +205,7 @@ async function extractBundle(archive, into, top) {
203
205
  noChmod: false,
204
206
  filter: (path, entry) => {
205
207
  const type = "type" in entry ? String(entry.type) : "File";
206
- if (!(path === `${top}/` || path === top || path.startsWith(`${top}/`)) || !ACCEPTED.has(type)) {
208
+ if (!(path === `${top}/` || path === top || path.startsWith(`${top}/`)) || !accepted.has(type)) {
207
209
  refused ??= `${path} (${type})`;
208
210
  return false;
209
211
  }
@@ -218,12 +220,91 @@ async function extractBundle(archive, into, top) {
218
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}.` }));
219
221
  }
220
222
  //#endregion
223
+ //#region src/environment.ts
224
+ /** What Windows gives every program, which a child of the SDK keeps (D150 item 9). */
225
+ const WINDOWS_SYSTEM_VARIABLES = [
226
+ "SystemRoot",
227
+ "SystemDrive",
228
+ "windir",
229
+ "ComSpec",
230
+ "PATHEXT",
231
+ "TEMP",
232
+ "TMP",
233
+ "USERPROFILE",
234
+ "HOMEDRIVE",
235
+ "HOMEPATH",
236
+ "APPDATA",
237
+ "LOCALAPPDATA",
238
+ "ProgramData",
239
+ "ProgramFiles",
240
+ "ProgramFiles(x86)",
241
+ "ProgramW6432",
242
+ "CommonProgramFiles",
243
+ "CommonProgramFiles(x86)",
244
+ "CommonProgramW6432",
245
+ "USERNAME",
246
+ "USERDOMAIN",
247
+ "COMPUTERNAME",
248
+ "OS",
249
+ "NUMBER_OF_PROCESSORS",
250
+ "PROCESSOR_ARCHITECTURE",
251
+ "PROCESSOR_IDENTIFIER"
252
+ ];
253
+ /** `env` for a child process: on Windows with the system's own variables this process has. */
254
+ function childEnvironment(env, options = {}) {
255
+ const out = {};
256
+ for (const [name, value] of Object.entries(env)) if (value !== void 0) out[name] = value;
257
+ if ((options.platform ?? process.platform) !== "win32") return out;
258
+ const own = options.own ?? process.env;
259
+ const present = new Set(Object.keys(out).map((name) => name.toUpperCase()));
260
+ for (const name of WINDOWS_SYSTEM_VARIABLES) {
261
+ if (present.has(name.toUpperCase())) continue;
262
+ const found = Object.entries(own).find(([key]) => key.toUpperCase() === name.toUpperCase());
263
+ if (found?.[1] !== void 0) out[found[0]] = found[1];
264
+ }
265
+ return out;
266
+ }
267
+ //#endregion
221
268
  //#region src/bundle/tree.ts
222
269
  /**
223
270
  * A bundle's files against its manifest (RT-01): one walk that both writes `files.json` (the
224
271
  * assembler) and checks an extracted bundle against it (setup), so the two can't disagree about
225
272
  * what counts. Links must stay inside the bundle; nothing may be missing, changed or extra.
273
+ *
274
+ * On Windows (D153) a link is read as the relative, `/`-separated path from its directory to its
275
+ * target, whether it is a symbolic link (`..\\x`, as pnpm makes them for an administrator) or a
276
+ * junction (an absolute target, as setup makes them for everyone), so `files.json` reads the same
277
+ * on every system; and no file is executable, since Windows keeps no execute bit (D150 item 8).
278
+ */
279
+ /** Whether this process runs on Windows, whose links and modes the walk reads its own way. */
280
+ const WINDOWS = process.platform === "win32";
281
+ /**
282
+ * A link's target as the manifest spells it: relative to the link's directory, `/`-separated.
283
+ * POSIX targets are kept exactly as written; a Windows junction's absolute target, and a Windows
284
+ * symbolic link's `\\`-separated one, are rewritten into that form. `realRoot` is `root`'s real
285
+ * path, which a caller reading many links computes once (Opus review of WP-W3, finding 8).
226
286
  */
287
+ function linkTarget(root, path, written, realRoot = realOrNone(root)) {
288
+ if (!WINDOWS) return written;
289
+ const plain = written.startsWith("\\\\?\\") ? written.slice(4) : written;
290
+ if (!isAbsolute(plain)) return plain.split(sep).join("/");
291
+ const given = resolve(root);
292
+ const dir = dirname(join(given, path));
293
+ const spellings = [[given, dir]];
294
+ if (realRoot !== void 0) spellings.push([realRoot, join(realRoot, relative(given, dir))]);
295
+ const lower = plain.toLowerCase();
296
+ for (const [base, from] of spellings) if (lower === base.toLowerCase() || lower.startsWith(`${base.toLowerCase()}${sep}`)) return relative(from, plain).split(sep).join("/");
297
+ return relative(dir, plain).split(sep).join("/");
298
+ }
299
+ /** `root`'s real path on Windows (where junction targets need it), or `undefined`. */
300
+ function realOrNone(root) {
301
+ if (!WINDOWS) return void 0;
302
+ try {
303
+ return realpathSync.native(resolve(root));
304
+ } catch {
305
+ return;
306
+ }
307
+ }
227
308
  /** The SHA-256 and size of one file, read as a stream. */
228
309
  async function hashFile(path) {
229
310
  const hash = createHash("sha256");
@@ -240,23 +321,26 @@ async function hashFile(path) {
240
321
  /** Every file and link under `root`, sorted by path, skipping `exclude`d paths (and below). */
241
322
  async function walk(root, exclude) {
242
323
  const out = [];
324
+ const realRoot = realOrNone(root);
243
325
  const visit = async (dir) => {
244
326
  const entries = await readdir(join(root, dir), { withFileTypes: true });
245
327
  for (const entry of entries) {
246
328
  const path = dir === "" ? entry.name : `${dir}/${entry.name}`;
247
329
  if (exclude(path)) continue;
248
330
  if (entry.isDirectory()) await visit(path);
249
- else if (entry.isSymbolicLink()) out.push({
250
- kind: "link",
251
- path,
252
- target: await readlink(join(root, path))
253
- });
254
- else if (entry.isFile()) {
255
- const stats = await lstat(join(root, path));
331
+ else if (entry.isSymbolicLink()) {
332
+ const written = await readlink(join(root, path));
333
+ out.push({
334
+ kind: "link",
335
+ path,
336
+ target: linkTarget(root, path, written, realRoot)
337
+ });
338
+ } else if (entry.isFile()) {
339
+ const executable = WINDOWS ? false : ((await lstat(join(root, path))).mode & 64) !== 0;
256
340
  out.push({
257
341
  kind: "file",
258
342
  path,
259
- executable: (stats.mode & 64) !== 0
343
+ executable
260
344
  });
261
345
  } else out.push({
262
346
  kind: "other",
@@ -282,7 +366,7 @@ async function mapLimit(items, limit, task) {
282
366
  }
283
367
  /** Whether a link's target is relative and resolves inside `root`. */
284
368
  function linkStaysInside(root, path, target) {
285
- if (isAbsolute(target)) return false;
369
+ if (isAbsolute(target) || /[\\:\0]/.test(target)) return false;
286
370
  const base = resolve(root);
287
371
  const resolved = resolve(base, dirname(path), target);
288
372
  return resolved === base || resolved.startsWith(base + sep);
@@ -335,6 +419,7 @@ async function describeTree(root, exclude = () => false) {
335
419
  async function checkTree(root, manifest, options = {}) {
336
420
  const limit = options.limit ?? 20;
337
421
  const allowExtra = options.allowExtra ?? (() => false);
422
+ const checkLinks = options.links ?? true;
338
423
  const problems = [];
339
424
  const add = (problem) => {
340
425
  if (problems.length < limit) problems.push(problem);
@@ -353,7 +438,7 @@ async function checkTree(root, manifest, options = {}) {
353
438
  for (const entry of walked) if (!expectedFiles.has(entry.path) && !expectedLinks.has(entry.path)) {
354
439
  if (!allowExtra(entry.path)) add(`${entry.path}: not in the bundle's file list`);
355
440
  }
356
- for (const [path, link] of expectedLinks) {
441
+ for (const [path, link] of checkLinks ? expectedLinks : /* @__PURE__ */ new Map()) {
357
442
  const actual = found.get(path);
358
443
  if (actual?.kind !== "link") add(`${path}: missing link`);
359
444
  else if (actual.target !== link.target) add(`${path}: links to ${actual.target}, not ${link.target}`);
@@ -371,6 +456,142 @@ async function checkTree(root, manifest, options = {}) {
371
456
  return problems;
372
457
  }
373
458
  //#endregion
459
+ //#region src/bundle/links.ts
460
+ /**
461
+ * A Windows bundle's links (D153). An ordinary Windows user can't create symbolic links (that
462
+ * takes an administrator or Developer Mode), so a Windows archive holds none: its `files.json`
463
+ * lists each link, every one to a directory inside the bundle, and setup makes each a junction,
464
+ * which any user can create. A junction names its target by absolute path, so it is made at the
465
+ * bundle's real path, and a bundle moved since is set up again (`readSetup` compares the root
466
+ * its marker names).
467
+ *
468
+ * Nothing outside the bundle is ever touched: a listed link's path and target must stay inside
469
+ * it, every directory above a link must be a real directory (never a link), the target must be a
470
+ * directory, and what already sits at a link's path is replaced only when it is itself a link, or
471
+ * a real directory holding exactly the files and bytes of the link's target. That is what a copy
472
+ * made by a tool that follows junctions leaves (Explorer, `Copy-Item`, `robocopy` without `/SJ`,
473
+ * and a move to another drive, which is a copy), so such a copy is set up again; a directory
474
+ * holding anything else is refused and kept (Opus review of WP-W3, finding 4).
475
+ *
476
+ * Two setups of one bundle may run at once (a moved bundle met by two commands): each step
477
+ * tolerates the other's, and a link counts as made once it names its target, whoever made it
478
+ * (finding 5).
479
+ */
480
+ /** Why a link couldn't be made; the caller reports it as `MOSTRA_ENV_BUNDLE_INVALID`. */
481
+ var LinkRefused = class extends Error {};
482
+ /** Whether `path` (an absolute path inside `root`) has only real directories above it. */
483
+ async function realParents(root, path) {
484
+ let dir = dirname(path);
485
+ const chain = [];
486
+ while (dir.length > root.length) {
487
+ chain.unshift(dir);
488
+ dir = dirname(dir);
489
+ }
490
+ for (const parent of chain) {
491
+ const info = await lstat(parent).catch(() => void 0);
492
+ if (info === void 0) {
493
+ await mkdir(parent);
494
+ continue;
495
+ }
496
+ if (!info.isDirectory() || info.isSymbolicLink()) return false;
497
+ }
498
+ return true;
499
+ }
500
+ /**
501
+ * Makes every link `manifest` lists as a junction under `root`, which must be the bundle's real
502
+ * path (`realpath.native`). A link that already names its target is kept; one that names another
503
+ * (the bundle moved) is replaced. Throws {@link LinkRefused} for a link the manifest can't have.
504
+ */
505
+ async function makeJunctions(root, manifest) {
506
+ const realRoot = realOrNone(root);
507
+ let waiting = [...manifest.links].sort((a, b) => a.path.length - b.path.length);
508
+ while (waiting.length > 0) {
509
+ const later = [];
510
+ for (const link of waiting) if (!await makeJunction(root, link, realRoot)) later.push(link);
511
+ if (later.length === waiting.length) {
512
+ const first = later[0];
513
+ throw new LinkRefused(`${first.path} links to ${first.target}, which is no directory.`);
514
+ }
515
+ waiting = later;
516
+ }
517
+ }
518
+ /** Makes one link; `false` when its target isn't a directory (yet). */
519
+ async function makeJunction(root, link, realRoot) {
520
+ if (!safeRelative(link.path) || !linkStaysInside(root, link.path, link.target)) throw new LinkRefused(`${link.path} links outside the bundle (${link.target}).`);
521
+ const at = join(root, link.path);
522
+ const target = resolve(dirname(at), link.target);
523
+ if (!await realParents(root, at)) throw new LinkRefused(`${link.path} sits below a link or a file.`);
524
+ const kind = await stat(target).catch(() => void 0);
525
+ if (kind === void 0 || !kind.isDirectory()) return names(root, link, at, realRoot);
526
+ let last;
527
+ for (let attempt = 0; attempt < 20; attempt++) {
528
+ if (await names(root, link, at, realRoot)) return true;
529
+ const current = await lstat(at).catch(() => void 0);
530
+ try {
531
+ if (current?.isSymbolicLink()) await rmdir(at).catch(async (error) => {
532
+ if (error.code !== "ENOENT") await unlink(at);
533
+ });
534
+ else if (current !== void 0) {
535
+ if (!current.isDirectory() || !await sameTree(at, target)) {
536
+ if (await names(root, link, at, realRoot)) return true;
537
+ const now = await lstat(at).catch(() => void 0);
538
+ if (now !== void 0 && !now.isSymbolicLink()) throw new LinkRefused(`${link.path} is a file or directory where the bundle has a link, and it isn't a copy of ${link.target}.`);
539
+ continue;
540
+ }
541
+ await rm(at, {
542
+ recursive: true,
543
+ force: true,
544
+ maxRetries: 5,
545
+ retryDelay: 100
546
+ });
547
+ }
548
+ await symlink(target, at, "junction");
549
+ } catch (error) {
550
+ if (error instanceof LinkRefused) throw error;
551
+ last = error;
552
+ }
553
+ }
554
+ if (await names(root, link, at, realRoot)) return true;
555
+ throw new LinkRefused(`${link.path} couldn't be made as a junction to ${link.target}${last ? `: ${last.message}` : ""}.`);
556
+ }
557
+ /** Whether the link at `at` exists and names the manifest's target. */
558
+ async function names(root, link, at, realRoot) {
559
+ const info = await lstat(at).catch(() => void 0);
560
+ if (info === void 0 || !info.isSymbolicLink()) return false;
561
+ const written = await readlink(at).catch(() => void 0);
562
+ return written !== void 0 && linkTarget(root, link.path, written, realRoot) === link.target;
563
+ }
564
+ /**
565
+ * Whether the real directory `copy` holds exactly what `original` holds: the same names, files of
566
+ * the same bytes, and directories alike all the way down. A link anywhere in the copy, or an entry
567
+ * of any other kind, makes it no copy.
568
+ */
569
+ async function sameTree(copy, original) {
570
+ const [mine, theirs] = await Promise.all([readdir(copy, { withFileTypes: true }).catch(() => void 0), readdir(original, { withFileTypes: true }).catch(() => void 0)]);
571
+ if (mine === void 0 || theirs === void 0 || mine.length !== theirs.length) return false;
572
+ const byName = new Map(theirs.map((entry) => [entry.name, entry]));
573
+ for (const entry of mine) {
574
+ const other = byName.get(entry.name);
575
+ if (other === void 0 || entry.isSymbolicLink()) return false;
576
+ const a = join(copy, entry.name);
577
+ const b = join(original, entry.name);
578
+ if (entry.isDirectory()) {
579
+ if (!(await stat(b).catch(() => void 0))?.isDirectory()) return false;
580
+ if (!await sameTree(a, b)) return false;
581
+ } else if (entry.isFile()) {
582
+ if (!(other.isFile() || other.isSymbolicLink()) || !await sameBytes(a, b)) return false;
583
+ } else return false;
584
+ }
585
+ return true;
586
+ }
587
+ /** Whether two files hold the same bytes (streamed, so a large program isn't held in memory). */
588
+ async function sameBytes(a, b) {
589
+ const [sa, sb] = await Promise.all([stat(a), stat(b)]).catch(() => [void 0, void 0]);
590
+ if (sa === void 0 || sb === void 0 || !sb.isFile() || sa.size !== sb.size) return false;
591
+ const [ha, hb] = await Promise.all([hashFile(a), hashFile(b)]);
592
+ return ha.sha256 === hb.sha256;
593
+ }
594
+ //#endregion
374
595
  //#region src/bundle/setup.ts
375
596
  /**
376
597
  * Making an extracted bundle usable (RT-01, D142): its files checked against `files.json`, which
@@ -419,12 +640,23 @@ async function readBundle(root) {
419
640
  info
420
641
  };
421
642
  }
422
- /** The setup marker, when it describes exactly this bundle. */
643
+ /**
644
+ * The bundle's real path: links followed, Windows' short names written out, the case as the file
645
+ * system keeps it. A Windows bundle's junctions point into it, and its marker names it.
646
+ */
647
+ function realRoot(root) {
648
+ return realpathSync.native(root);
649
+ }
650
+ /**
651
+ * The setup marker, when it describes exactly this bundle. A Windows bundle's marker must also
652
+ * name the root it sits at now: its junctions point into the root setup ran at, so a bundle moved
653
+ * or copied since is set up again, which makes them afresh (D153).
654
+ */
423
655
  async function readSetup(bundle) {
424
656
  try {
425
657
  const marker = BundleSetup.parse(JSON.parse(await readFile(join(bundle.root, BUNDLE_SETUP_MARKER), "utf8")));
426
658
  const { info } = bundle;
427
- return marker.files === info.files.sha256 && marker.version === info.version && marker.platform === info.platform && marker.browser.revision === info.browser.revision ? marker : void 0;
659
+ return (isWindowsBundle(info.platform) ? marker.root === realRoot(bundle.root) : true) && marker.files === info.files.sha256 && marker.version === info.version && marker.platform === info.platform && marker.browser.revision === info.browser.revision ? marker : void 0;
428
660
  } catch {
429
661
  return;
430
662
  }
@@ -433,15 +665,41 @@ async function readSetup(bundle) {
433
665
  function setupExtras(path) {
434
666
  return path === "bundle.json" || path === "files.json" || path === "browsers" || path.startsWith("browsers/") || path === BUNDLE_SETUP_MARKER || path.startsWith(`${BUNDLE_SETUP_MARKER}.tmp-`);
435
667
  }
436
- /** Checks the bundle's files; throws `MOSTRA_ENV_BUNDLE_INVALID` naming what differs. */
437
- async function checkFiles(bundle) {
668
+ /** The bundle's `files.json`, once its SHA-256 is the one `bundle.json` pins. */
669
+ async function readManifest(bundle) {
438
670
  const { root, info } = bundle;
439
671
  const listing = join(root, info.files.path);
440
672
  const { sha256 } = await hashFile(listing).catch(() => ({ sha256: "" }));
441
673
  if (sha256 !== info.files.sha256) throw invalid(`${info.files.path} doesn't match the SHA-256 that bundle.json pins.`);
442
674
  const manifest = BundleFiles.safeParse(JSON.parse(await readFile(listing, "utf8")));
443
675
  if (!manifest.success) throw invalid(`${info.files.path} isn't a bundle's file list.`);
444
- const problems = await checkTree(root, manifest.data, { allowExtra: setupExtras });
676
+ return manifest.data;
677
+ }
678
+ /**
679
+ * Makes a Windows bundle's links, each a junction at the bundle's real path (D153); nothing for
680
+ * any other bundle, whose archive carries its links. Throws `MOSTRA_ENV_BUNDLE_INVALID` for a link
681
+ * the bundle can't have.
682
+ */
683
+ async function makeLinks(bundle, read) {
684
+ if (!isWindowsBundle(bundle.info.platform)) return;
685
+ const manifest = read ?? await readManifest(bundle);
686
+ try {
687
+ await makeJunctions(realRoot(bundle.root), manifest);
688
+ } catch (error) {
689
+ if (error instanceof LinkRefused) throw invalid(`The runtime bundle's links: ${error.message}`);
690
+ throw error;
691
+ }
692
+ }
693
+ /**
694
+ * Checks the bundle's files; throws `MOSTRA_ENV_BUNDLE_INVALID` naming what differs. With
695
+ * `links: false` the files alone are checked (a Windows bundle before `makeLinks` made its junctions).
696
+ */
697
+ async function checkFiles(bundle, options = {}) {
698
+ const { root, info } = bundle;
699
+ const problems = await checkTree(root, options.manifest ?? await readManifest(bundle), {
700
+ allowExtra: setupExtras,
701
+ links: options.links ?? true
702
+ });
445
703
  if (problems.length > 0) throw invalid(`Mostra ${info.version}'s runtime bundle for ${info.platform} differs from its file list: ${problems.join("; ")}.`);
446
704
  }
447
705
  /** The browser files that differ from the pin, relative to `browsers/`. */
@@ -457,7 +715,7 @@ function installEnvironment(bundle, env) {
457
715
  const out = {};
458
716
  for (const [name, value] of Object.entries(env)) if (value !== void 0 && !name.startsWith("MOSTRA_") && name !== "PLAYWRIGHT_BROWSERS_PATH") out[name] = value;
459
717
  out["PLAYWRIGHT_BROWSERS_PATH"] = browsersDir(bundle.root);
460
- return out;
718
+ return childEnvironment(out);
461
719
  }
462
720
  /** The variables whose URLs may carry credentials the browser's fetch uses. */
463
721
  const CREDENTIAL_URLS = [
@@ -501,6 +759,7 @@ async function installBrowser(bundle, options) {
501
759
  "pipe",
502
760
  "pipe"
503
761
  ],
762
+ windowsHide: true,
504
763
  ...options.signal ? { signal: options.signal } : {},
505
764
  timeout: 9e5
506
765
  });
@@ -526,9 +785,11 @@ async function installBrowser(bundle, options) {
526
785
  async function setupBundle(root, options = {}) {
527
786
  const env = options.env ?? process.env;
528
787
  const bundle = await readBundle(root);
788
+ const manifest = isWindowsBundle(bundle.info.platform) || !options.filesChecked ? await readManifest(bundle) : void 0;
789
+ await makeLinks(bundle, manifest);
529
790
  if (!options.filesChecked) {
530
791
  options.onStep?.("files");
531
- await checkFiles(bundle);
792
+ await checkFiles(bundle, manifest ? { manifest } : {});
532
793
  }
533
794
  options.onStep?.("browser");
534
795
  if ((await checkBrowser(bundle)).length > 0) {
@@ -547,7 +808,8 @@ async function setupBundle(root, options = {}) {
547
808
  platform: bundle.info.platform,
548
809
  files: bundle.info.files.sha256,
549
810
  browser: { revision: bundle.info.browser.revision },
550
- at: (/* @__PURE__ */ new Date()).toISOString()
811
+ at: (/* @__PURE__ */ new Date()).toISOString(),
812
+ root: realRoot(root)
551
813
  };
552
814
  const temporary = join(root, `${BUNDLE_SETUP_MARKER}.tmp-${process.pid}`);
553
815
  await writeFile(temporary, `${JSON.stringify(marker, null, 2)}\n`);
@@ -564,14 +826,23 @@ async function setupBundle(root, options = {}) {
564
826
  * installers of one bundle share a lock, so it is downloaded once; a crash leaves a temporary
565
827
  * directory the next install removes, never a half-ready bundle.
566
828
  */
567
- /** Where the cache is: `MOSTRA_CACHE_DIR`, else the platform's per-user cache directory. */
829
+ /**
830
+ * Where the cache is: `MOSTRA_CACHE_DIR`, else the platform's per-user cache directory:
831
+ * `~/Library/Caches/mostra` on macOS, `%LOCALAPPDATA%\mostra-cache` on Windows (beside the home,
832
+ * `%LOCALAPPDATA%\mostra`, as npm keeps `npm-cache`), `${XDG_CACHE_HOME:-~/.cache}/mostra` on
833
+ * Linux.
834
+ */
568
835
  function cacheRoot(env, platform = process.platform) {
569
836
  const override = env["MOSTRA_CACHE_DIR"];
570
837
  if (override) return resolve(override);
838
+ if (platform === "win32") {
839
+ const local = env["LOCALAPPDATA"] || win32.join(env["USERPROFILE"] || homedir(), "AppData", "Local");
840
+ return win32.join(local, "mostra-cache");
841
+ }
571
842
  const home = env["HOME"] || homedir();
572
- if (platform === "darwin") return join(home, "Library", "Caches", "mostra");
843
+ if (platform === "darwin") return posix.join(home, "Library", "Caches", "mostra");
573
844
  const xdg = env["XDG_CACHE_HOME"];
574
- return join(xdg?.startsWith("/") ? xdg : join(home, ".cache"), "mostra");
845
+ return posix.join(xdg?.startsWith("/") ? xdg : posix.join(home, ".cache"), "mostra");
575
846
  }
576
847
  /** The directory a bundle with this archive digest lives in. */
577
848
  function bundleDir(env, sha256) {
@@ -628,7 +899,8 @@ async function installPinned(pins, options) {
628
899
  */
629
900
  async function filesIntact(dir) {
630
901
  try {
631
- await checkFiles(await readBundle(dir));
902
+ const bundle = await readBundle(dir);
903
+ await checkFiles(bundle, { links: !isWindowsBundle(bundle.info.platform) });
632
904
  return true;
633
905
  } catch (error) {
634
906
  if (error instanceof RpcError && error.diagnostic.code === "MOSTRA_ENV_BUNDLE_INVALID") return false;
@@ -670,23 +942,44 @@ async function fetchInto(pins, asset, dir, bundles, options) {
670
942
  await mkdir(root);
671
943
  const top = asset.file.replace(/\.tar\.gz$/, "");
672
944
  if (top !== bundleAssetName(pins.version, options.platform).replace(/\.tar\.gz$/, "")) throw new RpcError(createDiagnostic("MOSTRA_ENV_BUNDLE_INVALID", { message: `The pinned archive ${asset.file} isn't Mostra ${pins.version}'s bundle for ${options.platform}.` }));
673
- await extractBundle(archive, root, top);
945
+ const windows = isWindowsBundle(options.platform);
946
+ await extractBundle(archive, root, top, { links: windows ? "refuse" : "keep" });
674
947
  await rm(archive, { force: true });
675
948
  const bundle = await readBundle(root);
676
949
  if (bundle.info.version !== pins.version || bundle.info.platform !== options.platform) throw new RpcError(createDiagnostic("MOSTRA_ENV_BUNDLE_INVALID", { message: `The archive holds Mostra ${bundle.info.version} for ${bundle.info.platform}, not ${pins.version} for ${options.platform}.` }));
677
950
  options.onProgress?.({ step: "files" });
678
- await checkFiles(bundle);
951
+ await checkFiles(bundle, { links: !windows });
679
952
  try {
680
- await rename(root, dir);
953
+ await renameIntoPlace(root, dir);
681
954
  } catch (error) {
682
955
  const code = error.code;
683
- if (!(code === "ENOTEMPTY" || code === "EEXIST") || !await exists(dir)) throw error;
956
+ if (!(code === "ENOTEMPTY" || code === "EEXIST" || code === "EPERM") || !await exists(dir)) throw error;
684
957
  }
685
958
  } finally {
686
959
  await rm(scratch, {
687
960
  recursive: true,
688
- force: true
689
- });
961
+ force: true,
962
+ maxRetries: 10,
963
+ retryDelay: 200
964
+ }).catch(() => void 0);
965
+ }
966
+ }
967
+ /**
968
+ * Renames `from` to `to`. On Windows a file that an antivirus scan or the indexer holds for a
969
+ * moment refuses the rename (EPERM, EACCES, EBUSY) though nothing is wrong, so the rename is
970
+ * tried again with backoff for up to 3 s, as the runtime's renames are (D150 item 8), and never
971
+ * turned into a copy; a refusal that lasts is thrown, and EPERM over an existing directory with it.
972
+ */
973
+ async function renameIntoPlace(from, to) {
974
+ const deadline = Date.now() + 3e3;
975
+ for (let wait = 25;; wait = Math.min(wait * 2, 400)) try {
976
+ await rename(from, to);
977
+ return;
978
+ } catch (error) {
979
+ const code = error.code;
980
+ const held = code === "EPERM" || code === "EACCES" || code === "EBUSY";
981
+ if (process.platform !== "win32" || !held || Date.now() > deadline || await exists(to)) throw error;
982
+ await new Promise((done) => setTimeout(done, wait));
690
983
  }
691
984
  }
692
985
  /** Fails early when the cache's file system can't hold the bundle. */
@@ -806,8 +1099,9 @@ async function lock(path, signal) {
806
1099
  //#endregion
807
1100
  //#region src/platform.ts
808
1101
  /**
809
- * Which runtime bundle this machine takes (RT-01, D35): one per operating system and architecture
810
- * of v1, glibc only on Linux. Anything else fails closed before a download starts.
1102
+ * Which runtime bundle this machine takes (RT-01, RT-12, D35, D146): one per operating system and
1103
+ * architecture of v1 (macOS, glibc Linux and Windows, each on x64 and arm64). Anything else fails
1104
+ * closed before a download starts.
811
1105
  */
812
1106
  /**
813
1107
  * The glibc release Node.js reports, read from its diagnostic report's header. The report runs in
@@ -839,14 +1133,14 @@ function currentPlatform() {
839
1133
  return facts;
840
1134
  }
841
1135
  /**
842
- * The bundle platform for these facts. Throws `MOSTRA_PLATFORM_UNSUPPORTED` for Windows, other
843
- * architectures and Linux without glibc, whose libraries the bundle's Node.js and browser need.
1136
+ * The bundle platform for these facts. Throws `MOSTRA_PLATFORM_UNSUPPORTED` for other systems and
1137
+ * architectures and for Linux without glibc, whose libraries the bundle's Node.js and browser need.
844
1138
  */
845
1139
  function bundlePlatform(facts = currentPlatform()) {
846
1140
  const name = `${facts.platform}-${facts.arch}`;
847
1141
  const known = BUNDLE_PLATFORMS.includes(name);
848
- if (known && facts.platform === "linux" && facts.glibc === void 0) throw unsupported(`Mostra's Linux bundle needs glibc, and this system has none (musl, as on Alpine Linux); it supports macOS and glibc-based Linux on x64 and arm64.`);
849
- if (!known) throw unsupported(`Mostra doesn't run on ${facts.platform === "win32" ? `Windows (${name})` : name}; it supports macOS and Linux on x64 and arm64.`);
1142
+ if (known && facts.platform === "linux" && facts.glibc === void 0) throw unsupported(`Mostra's Linux bundle needs glibc, and this system has none (musl, as on Alpine Linux); it supports macOS, glibc-based Linux and Windows on x64 and arm64.`);
1143
+ if (!known) throw unsupported(`Mostra doesn't run on ${facts.platform === "win32" ? `Windows (${name})` : name}; it supports macOS, Linux and Windows on x64 and arm64.`);
850
1144
  return name;
851
1145
  }
852
1146
  function unsupported(message) {
@@ -907,7 +1201,7 @@ function packageRoot() {
907
1201
  */
908
1202
  function pinsFile(env, root) {
909
1203
  const override = env["MOSTRA_TEST_BUNDLE_PINS"];
910
- const installed = root.split(sep).includes("node_modules");
1204
+ const installed = root.split(/[\\/]/).includes("node_modules");
911
1205
  return override && !installed ? override : join(root, "dist", "bundles.json");
912
1206
  }
913
1207
  /** The pins this build carries: `dist/bundles.json` (a test override applies only in a development checkout). */
@@ -1005,6 +1299,6 @@ async function resolveRuntime(options = {}) {
1005
1299
  throw new RpcError(createDiagnostic("MOSTRA_ENV_BUNDLE_UNAVAILABLE", { message: "This build of @mostra/sdk pins no runtime bundle and has no runtime beside it; set MOSTRA_BUNDLE to an extracted bundle." }));
1006
1300
  }
1007
1301
  //#endregion
1008
- export { safeRelative as C, linkStaysInside as S, 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, redactUrl as w, hashFile as x, checkTree as y };
1302
+ 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 };
1009
1303
 
1010
- //# sourceMappingURL=runtime-CUbnK5Za.js.map
1304
+ //# sourceMappingURL=runtime-BCvS_ISH.js.map