@mostra/runtime 0.2.0 → 0.4.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 (66) hide show
  1. package/dist/bin.js +23 -12
  2. package/dist/bin.js.map +1 -1
  3. package/dist/builder/child/main.js +20 -18
  4. package/dist/builder/child/main.js.map +1 -1
  5. package/dist/{child-xrHmytks.js → child-Chiavhdx.js} +7 -3
  6. package/dist/child-Chiavhdx.js.map +1 -0
  7. package/dist/deps/child/install.js +9 -6
  8. package/dist/deps/child/install.js.map +1 -1
  9. package/dist/directories-mJ1qlVht.js +322 -0
  10. package/dist/directories-mJ1qlVht.js.map +1 -0
  11. package/dist/files-FygSJIzK.js +388 -0
  12. package/dist/files-FygSJIzK.js.map +1 -0
  13. package/dist/index.d.ts +2 -0
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +2 -2
  16. package/dist/{instance-CzBkUyHu.js → instance-DxcGEQj_.js} +1680 -934
  17. package/dist/instance-DxcGEQj_.js.map +1 -0
  18. package/package.json +5 -5
  19. package/src/bin.ts +18 -16
  20. package/src/builder/child/dev.ts +8 -4
  21. package/src/builder/child/plugins.ts +12 -7
  22. package/src/builder/child/spa.ts +15 -9
  23. package/src/builder/index.ts +3 -6
  24. package/src/deps/child/install.ts +3 -2
  25. package/src/deps/index.ts +21 -16
  26. package/src/deps/warm.ts +3 -16
  27. package/src/dev/index.ts +37 -19
  28. package/src/faults.ts +9 -2
  29. package/src/gateway/capabilities.ts +2 -2
  30. package/src/gateway/listener.ts +36 -0
  31. package/src/home/acquire.ts +27 -11
  32. package/src/home/index.ts +1 -1
  33. package/src/home/paths.ts +45 -135
  34. package/src/home/platform.ts +17 -9
  35. package/src/home/record.ts +3 -3
  36. package/src/home/settings.ts +5 -16
  37. package/src/instance/index.ts +41 -9
  38. package/src/ledger/index.ts +5 -27
  39. package/src/platform/control.ts +409 -0
  40. package/src/platform/directories.ts +212 -0
  41. package/src/platform/environment.ts +64 -0
  42. package/src/platform/files.ts +386 -0
  43. package/src/platform/helper.ts +178 -0
  44. package/src/{processes → platform}/identity.ts +40 -8
  45. package/src/platform/index.ts +53 -0
  46. package/src/platform/launch.ts +373 -0
  47. package/src/processes/child.ts +11 -5
  48. package/src/processes/environment.ts +22 -3
  49. package/src/processes/index.ts +2 -1
  50. package/src/processes/manager.ts +76 -179
  51. package/src/protocol-server/client.ts +19 -18
  52. package/src/protocol-server/server.ts +75 -54
  53. package/src/publish/index.ts +2 -1
  54. package/src/publish/publisher.ts +17 -10
  55. package/src/publish/source.ts +7 -4
  56. package/src/registry/store.ts +7 -65
  57. package/src/secrets/index.ts +2 -2
  58. package/src/service/unit.ts +353 -33
  59. package/src/verify/index.ts +5 -3
  60. package/src/verify/typecheck.ts +3 -2
  61. package/src/workers/launcher.ts +3 -1
  62. package/src/workers/pool.ts +14 -1
  63. package/dist/child-xrHmytks.js.map +0 -1
  64. package/dist/instance-CzBkUyHu.js.map +0 -1
  65. package/dist/types-NjNy7gXX.js +0 -57
  66. package/dist/types-NjNy7gXX.js.map +0 -1
@@ -15,9 +15,10 @@ export { extractManifest } from "./manifest.ts";
15
15
  export { artifactMethods, canonicalRef } from "./methods.ts";
16
16
  export {
17
17
  createPublisher,
18
+ defaultTitle,
18
19
  type Publisher,
19
20
  versionDamage,
20
21
  versionOutputDigest,
21
22
  } from "./publisher.ts";
22
- export { listSource, type ResolvedSource, resolveSource } from "./source.ts";
23
+ export { listSource, resolveSource } from "./source.ts";
23
24
  export { readTar } from "./tar.ts";
@@ -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,
@@ -133,7 +134,7 @@ export interface Publisher {
133
134
  const ENTRY_NAMES = ["index.tsx", "index.jsx", "index.mdx"];
134
135
 
135
136
  /** The title of an artifact without a manifest: its directory's name, or a single file's own name. */
136
- function defaultTitle(source: ResolvedSource): string {
137
+ export function defaultTitle(source: ResolvedSource): string {
137
138
  if (source.kind === "dir") return basename(source.root);
138
139
  const stem = basename(source.entry, extname(source.entry));
139
140
  return stem === "page" || stem === "index" ? basename(source.root) : stem;
@@ -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
  },