@mostra/runtime 0.2.0 → 0.3.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.
Files changed (58) hide show
  1. package/dist/bin.js +7 -7
  2. package/dist/bin.js.map +1 -1
  3. package/dist/builder/child/main.js +18 -17
  4. package/dist/builder/child/main.js.map +1 -1
  5. package/dist/{child-xrHmytks.js → child-BQc3arf7.js} +5 -2
  6. package/dist/{child-xrHmytks.js.map → child-BQc3arf7.js.map} +1 -1
  7. package/dist/deps/child/install.js +9 -6
  8. package/dist/deps/child/install.js.map +1 -1
  9. package/dist/files-KxzOxckl.js +674 -0
  10. package/dist/files-KxzOxckl.js.map +1 -0
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +2 -2
  14. package/dist/{instance-CzBkUyHu.js → instance-DiKSe7s6.js} +1148 -737
  15. package/dist/instance-DiKSe7s6.js.map +1 -0
  16. package/package.json +5 -5
  17. package/src/bin.ts +4 -10
  18. package/src/builder/child/dev.ts +4 -3
  19. package/src/builder/child/plugins.ts +12 -7
  20. package/src/builder/child/spa.ts +15 -9
  21. package/src/builder/index.ts +3 -6
  22. package/src/deps/child/install.ts +3 -2
  23. package/src/deps/index.ts +21 -16
  24. package/src/deps/warm.ts +3 -16
  25. package/src/faults.ts +9 -2
  26. package/src/gateway/capabilities.ts +2 -2
  27. package/src/home/acquire.ts +27 -11
  28. package/src/home/paths.ts +45 -135
  29. package/src/home/platform.ts +17 -9
  30. package/src/home/record.ts +3 -3
  31. package/src/home/settings.ts +5 -16
  32. package/src/instance/index.ts +31 -7
  33. package/src/ledger/index.ts +5 -27
  34. package/src/platform/control.ts +409 -0
  35. package/src/platform/directories.ts +212 -0
  36. package/src/platform/environment.ts +64 -0
  37. package/src/platform/files.ts +368 -0
  38. package/src/platform/helper.ts +152 -0
  39. package/src/{processes → platform}/identity.ts +40 -8
  40. package/src/platform/index.ts +46 -0
  41. package/src/platform/launch.ts +373 -0
  42. package/src/processes/child.ts +4 -1
  43. package/src/processes/environment.ts +22 -3
  44. package/src/processes/index.ts +2 -1
  45. package/src/processes/manager.ts +76 -179
  46. package/src/protocol-server/client.ts +19 -18
  47. package/src/protocol-server/server.ts +75 -54
  48. package/src/publish/publisher.ts +16 -9
  49. package/src/publish/source.ts +7 -4
  50. package/src/registry/store.ts +7 -65
  51. package/src/secrets/index.ts +2 -2
  52. package/src/service/unit.ts +3 -2
  53. package/src/verify/index.ts +5 -3
  54. package/src/verify/typecheck.ts +3 -2
  55. package/src/workers/launcher.ts +3 -1
  56. package/dist/instance-CzBkUyHu.js.map +0 -1
  57. package/dist/types-NjNy7gXX.js +0 -57
  58. package/dist/types-NjNy7gXX.js.map +0 -1
@@ -18,12 +18,12 @@
18
18
  import { createHash } from "node:crypto";
19
19
  import { createReadStream, createWriteStream, existsSync, readFileSync } from "node:fs";
20
20
  import {
21
+ chmod,
21
22
  mkdir,
22
23
  mkdtemp,
23
24
  readdir,
24
25
  readFile,
25
26
  realpath,
26
- rename,
27
27
  rm,
28
28
  stat,
29
29
  writeFile,
@@ -63,6 +63,7 @@ import {
63
63
  requestKey,
64
64
  } from "../ledger/index.ts";
65
65
  import type { Log } from "../log.ts";
66
+ import { renamePath } from "../platform/index.ts";
66
67
  import { fsyncPath, type Registry, removeTree } from "../registry/index.ts";
67
68
  import {
68
69
  type BuildOutcome,
@@ -229,15 +230,21 @@ async function archiveSnapshot(
229
230
  }
230
231
  yield TAR_END;
231
232
  }
232
- const output = createWriteStream(temporary, { mode: 0o400 });
233
- await pipeline(Readable.from(blocks()), createZstdCompress(), output);
234
- const handle = await import("node:fs/promises").then((fs) => fs.open(temporary, "r"));
233
+ // Written owner-writable, synced, then made read-only: Windows flushes a file only through a
234
+ // handle that may write, which a read-only file never opens again.
235
235
  try {
236
- await handle.sync();
237
- } finally {
238
- await handle.close();
236
+ await pipeline(
237
+ Readable.from(blocks()),
238
+ createZstdCompress(),
239
+ createWriteStream(temporary, { mode: 0o600 }),
240
+ );
241
+ fsyncPath(temporary);
242
+ await chmod(temporary, 0o400);
243
+ } catch (error) {
244
+ await removeTree(temporary).catch(() => {});
245
+ throw error;
239
246
  }
240
- await rename(temporary, target);
247
+ await renamePath(temporary, target);
241
248
  fsyncPath(dirname(target), true);
242
249
  return true;
243
250
  }
@@ -747,7 +754,7 @@ export function createPublisher(options: PublisherOptions): Publisher {
747
754
  // again from the same snapshot otherwise (D138 item 5).
748
755
  const cachedBuild = verifier().builtVersion(versionId);
749
756
  if (cachedBuild && (await versionDamage(cachedBuild)) === undefined) {
750
- await rename(cachedBuild, out);
757
+ await renamePath(cachedBuild, out);
751
758
  outputDigest = (
752
759
  JSON.parse(await readFile(join(out, "manifest.json"), "utf8")) as {
753
760
  outputDigest: string;
@@ -20,6 +20,7 @@ import {
20
20
  import { basename, dirname, extname, join } from "node:path";
21
21
  import { createDiagnostic, RpcError } from "@mostra/protocol";
22
22
  import ignore from "ignore";
23
+ import { nameKey, WINDOWS } from "../platform/index.ts";
23
24
  import type { ListedFile } from "./digest.ts";
24
25
 
25
26
  /** The entry files a directory artifact may have, in INTERFACES §2.1's order. */
@@ -158,7 +159,7 @@ export async function listSource(source: ResolvedSource): Promise<SourceFile[]>
158
159
  }
159
160
  }
160
161
  if (kind === "dir") {
161
- if (IGNORED_DIRS.has(entry.name) || rules.ignores(`${path}/`)) continue;
162
+ if (IGNORED_DIRS.has(nameKey(entry.name)) || rules.ignores(`${path}/`)) continue;
162
163
  let real: string;
163
164
  try {
164
165
  real = await realpath(absolute);
@@ -169,14 +170,16 @@ export async function listSource(source: ResolvedSource): Promise<SourceFile[]>
169
170
  await walk(absolute, path, new Set([...ancestors, real]));
170
171
  } else if (kind === "file") {
171
172
  if (IGNORED_FILE.test(entry.name) || rules.ignores(path)) continue;
172
- const clash = seen.get(path);
173
+ // Two names one file system tells apart and another doesn't (NFC forms, or case on
174
+ // Windows) would become one file in the snapshot.
175
+ const clash = seen.get(nameKey(path));
173
176
  if (clash !== undefined) {
174
177
  throw artifactError(
175
178
  "MOSTRA_ARTIFACT_SOURCE_UNREADABLE",
176
- `${clash} and ${absolute} have the same name after Unicode normalization (NFC).`,
179
+ `${clash} and ${absolute} have the same name after Unicode normalization (NFC)${WINDOWS ? " and ignoring case, as Windows compares names" : ""}.`,
177
180
  );
178
181
  }
179
- seen.set(path, absolute);
182
+ seen.set(nameKey(path), absolute);
180
183
  files.push({ path, absolute });
181
184
  }
182
185
  }
@@ -1,72 +1,15 @@
1
1
  /**
2
2
  * The content-addressed version store on disk (DATA_MODEL §1, FAILURE_MODEL §3): `versions/<id>/`
3
3
  * and `sources/<artifactId>/<sourceDigest>.tar.zst`. A version directory is assembled in staging,
4
- * made read-only and synced, then renamed into place; it becomes visible only when its ledger row
4
+ * synced and made read-only (`platform`: modes on POSIX, an access list on Windows), then renamed
5
+ * into place; it becomes visible only when its ledger row
5
6
  * commits, and anything without a row is removed at the next start.
6
7
  */
7
- import { closeSync, fsyncSync, openSync } from "node:fs";
8
- import { chmod, lstat, mkdir, readdir, rename, rm, stat } from "node:fs/promises";
8
+ import { mkdir, readdir, rm, stat } from "node:fs/promises";
9
9
  import { join } from "node:path";
10
+ import { fsyncPath, removeTree, renamePath, sealPlaced, sealTree } from "../platform/index.ts";
10
11
 
11
- /** Calls `visit` for every entry under `dir`, children before their directory. */
12
- async function walk(
13
- dir: string,
14
- visit: (path: string, isDirectory: boolean) => Promise<void>,
15
- ): Promise<void> {
16
- for (const entry of await readdir(dir, { withFileTypes: true })) {
17
- const path = join(dir, entry.name);
18
- if (entry.isDirectory()) await walk(path, visit);
19
- await visit(path, entry.isDirectory());
20
- }
21
- }
22
-
23
- /**
24
- * Syncs a file or a directory to disk. A file that can't be synced fails the caller (its bytes
25
- * would not be durable); a directory may not support it on some file systems, and only that
26
- * case is tolerated (EINVAL, ENOTSUP, EISDIR, EPERM, EACCES on `open`).
27
- */
28
- export function fsyncPath(path: string, directory = false): void {
29
- let fd: number | undefined;
30
- try {
31
- fd = openSync(path, "r");
32
- fsyncSync(fd);
33
- } catch (error) {
34
- const code = (error as NodeJS.ErrnoException).code;
35
- const unsupported = ["EINVAL", "ENOTSUP", "EISDIR", "EPERM", "EACCES"];
36
- if (!directory || !unsupported.includes(code ?? "")) throw error;
37
- } finally {
38
- if (fd !== undefined) closeSync(fd);
39
- }
40
- }
41
-
42
- /**
43
- * Makes a tree read-only (files 0444, directories 0555) and syncs it to disk. The top directory is
44
- * left writable when `keepTop` is set: renaming a directory needs write access to it, to update `..`.
45
- */
46
- export async function sealTree(dir: string, keepTop = false): Promise<void> {
47
- await walk(dir, async (path, isDirectory) => {
48
- await chmod(path, isDirectory ? 0o555 : 0o444);
49
- fsyncPath(path, isDirectory);
50
- });
51
- if (!keepTop) await chmod(dir, 0o555);
52
- fsyncPath(dir, true);
53
- }
54
-
55
- /** Removes a tree, sealed or not. Missing trees are fine. */
56
- export async function removeTree(dir: string): Promise<void> {
57
- try {
58
- const info = await lstat(dir);
59
- if (info.isDirectory()) {
60
- await chmod(dir, 0o755);
61
- await walk(dir, async (path, isDirectory) => {
62
- if (isDirectory) await chmod(path, 0o755);
63
- }).catch(() => {});
64
- }
65
- } catch {
66
- return;
67
- }
68
- await rm(dir, { recursive: true, force: true });
69
- }
12
+ export { fsyncPath, removeTree, sealTree };
70
13
 
71
14
  /** The version store of one home. */
72
15
  export interface VersionStore {
@@ -105,9 +48,8 @@ export async function openVersionStore(home: string): Promise<VersionStore> {
105
48
  const target = versionDir(versionId);
106
49
  // A directory without a ledger row is a leftover of an interrupted publish (FAILURE_MODEL §3).
107
50
  await removeTree(target);
108
- await rename(built, target);
109
- await chmod(target, 0o555);
110
- fsyncPath(target, true);
51
+ await renamePath(built, target);
52
+ await sealPlaced(target);
111
53
  fsyncPath(versionsDir, true);
112
54
  },
113
55
  async sweep(known) {
@@ -14,7 +14,6 @@ import {
14
14
  mkdirSync,
15
15
  readdirSync,
16
16
  readFileSync,
17
- renameSync,
18
17
  rmSync,
19
18
  statSync,
20
19
  writeFileSync,
@@ -32,6 +31,7 @@ import {
32
31
  secretSetMethod,
33
32
  } from "@mostra/protocol";
34
33
  import type { Log, Redactor } from "../log.ts";
34
+ import { renamePathSync } from "../platform/index.ts";
35
35
  import { handler, type MethodHandler } from "../protocol-server/index.ts";
36
36
 
37
37
  /** Where a secret lives: the home's shared store, or one artifact's. */
@@ -151,7 +151,7 @@ export function openSecrets(options: SecretsOptions): Secrets {
151
151
  const temporary = join(dir, `.${name}.${process.pid}.tmp`);
152
152
  writeFileSync(temporary, value, { mode: 0o600, flag: "w" });
153
153
  chmodSync(temporary, 0o600);
154
- renameSync(temporary, join(dir, name));
154
+ renamePathSync(temporary, join(dir, name));
155
155
  options.log.info("secret.set", { name, scope: scope.kind });
156
156
  return entry(dir, name, scope);
157
157
  },
@@ -12,6 +12,7 @@ import { dirname, join } from "node:path";
12
12
  import { createDiagnostic, RpcError } from "@mostra/protocol";
13
13
  import { homeHash } from "../home/index.ts";
14
14
  import type { Environment } from "../log.ts";
15
+ import { OS } from "../platform/index.ts";
15
16
 
16
17
  /** Where a home's service is, and whether it is there. */
17
18
  export interface ServiceStatus {
@@ -35,7 +36,7 @@ export interface ServiceSpec {
35
36
  readonly node: string;
36
37
  readonly entry: string;
37
38
  readonly env?: Environment;
38
- /** `process.platform`. */
39
+ /** The operating system (`OS` of the platform layer). */
39
40
  readonly platform?: string;
40
41
  }
41
42
 
@@ -50,7 +51,7 @@ function userHome(env: Environment): string {
50
51
  /** The service a home's daemon would be, installed or not. */
51
52
  export function serviceFor(spec: ServiceSpec): ServiceStatus {
52
53
  const env = spec.env ?? process.env;
53
- const platform = spec.platform ?? process.platform;
54
+ const platform = spec.platform ?? OS;
54
55
  const hash = homeHash(spec.home);
55
56
  const command = [spec.node, spec.entry, "daemon", "--always-on", "--home", spec.home];
56
57
  if (platform === "darwin") {
@@ -16,7 +16,7 @@
16
16
  */
17
17
  import { createHash, randomUUID } from "node:crypto";
18
18
  import { existsSync, readFileSync } from "node:fs";
19
- import { mkdir, readdir, rename, rm, stat, utimes, writeFile } from "node:fs/promises";
19
+ import { mkdir, readdir, rm, stat, utimes, writeFile } from "node:fs/promises";
20
20
  import { join } from "node:path";
21
21
  import {
22
22
  type ArtifactManifest,
@@ -59,6 +59,8 @@ export {
59
59
  startSandbox,
60
60
  } from "./sandbox.ts";
61
61
 
62
+ import { renamePath } from "../platform/index.ts";
63
+
62
64
  /** The revision of verify's rules: a cached verdict made under other rules is never reused. */
63
65
  export const VERIFY_REVISION: string = createHash("sha256")
64
66
  .update(`verify 2 ${TYPECHECK_REVISION}`)
@@ -339,7 +341,7 @@ export function createVerifier(options: VerifierOptions): Verifier {
339
341
  const outcome = await run.build(temporary);
340
342
  if (!outcome.ok) return outcome;
341
343
  await rm(join(buildsDir, prepared.versionId), { recursive: true, force: true });
342
- await rename(temporary, join(buildsDir, prepared.versionId));
344
+ await renamePath(temporary, join(buildsDir, prepared.versionId));
343
345
  await pruneBuilds(prepared.versionId);
344
346
  return outcome;
345
347
  } finally {
@@ -756,7 +758,7 @@ export function createVerifier(options: VerifierOptions): Verifier {
756
758
  temporary,
757
759
  `${JSON.stringify({ verdict: checkedVerdict, built: built.ok }, null, 2)}\n`,
758
760
  );
759
- await rename(temporary, target);
761
+ await renamePath(temporary, target);
760
762
  await pruneVerdicts();
761
763
  } else {
762
764
  // Its screenshots stay for the caller who reads this verdict; the cache's pruning removes
@@ -12,13 +12,14 @@
12
12
  */
13
13
  import { createHash } from "node:crypto";
14
14
  import { existsSync, readFileSync } from "node:fs";
15
- import { cp, mkdir, rm, symlink, writeFile } from "node:fs/promises";
15
+ import { cp, mkdir, rm, writeFile } from "node:fs/promises";
16
16
  import { createRequire } from "node:module";
17
17
  import { dirname, join } from "node:path";
18
18
  import { createDiagnostic, type Diagnostic } from "@mostra/protocol";
19
19
  import { LIVE_DECLARATION } from "../builder/index.ts";
20
20
  import { runtimeRoot } from "../deps/index.ts";
21
21
  import type { Environment, Log } from "../log.ts";
22
+ import { linkDirectory } from "../platform/index.ts";
22
23
  import type { ProcessManager } from "../processes/index.ts";
23
24
 
24
25
  /** How long the check may take: the build's own limit, which it runs beside (FAILURE_MODEL §9). */
@@ -247,7 +248,7 @@ export async function typecheck(options: {
247
248
  await mkdir(join(workDir, ".mostra-check"), { recursive: true });
248
249
  try {
249
250
  await cp(options.sourceDir, join(workDir, "source"), { recursive: true });
250
- await symlink(options.nodeModules, join(workDir, "node_modules"), "dir");
251
+ await linkDirectory(options.nodeModules, join(workDir, "node_modules"));
251
252
  await writeFile(join(workDir, ".mostra-check", "assets.d.ts"), AMBIENT);
252
253
  if (options.hasLive)
253
254
  await writeFile(join(workDir, ".mostra-check", "mostra-env.d.ts"), LIVE_DECLARATION);
@@ -16,6 +16,7 @@ import {
16
16
  type WorkerVersionRef,
17
17
  } from "@mostra/protocol";
18
18
  import type { Environment } from "../log.ts";
19
+ import { pathKey } from "../platform/index.ts";
19
20
  import type { ProcessManager } from "../processes/index.ts";
20
21
 
21
22
  /** How a worker ended. */
@@ -121,7 +122,8 @@ export function localLauncher(options: LocalLauncherOptions): WorkerLauncher {
121
122
  resolve(options.home, "cache", "builds"),
122
123
  resolve(options.home, "cache", "dev"),
123
124
  ];
124
- if (!roots.some((root) => versionDir.startsWith(`${root}/`))) {
125
+ // Strictly inside one of them, compared as the platform compares paths (WP-W1).
126
+ if (!roots.some((root) => pathKey(versionDir).startsWith(`${pathKey(root)}/`))) {
125
127
  throw new Error(
126
128
  "A home: locator names a version directory, a verify build or a dev session of the home.",
127
129
  );