@mostra/runtime 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +39 -2
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +236 -0
- package/dist/bin.js.map +1 -0
- package/dist/builder/child/main.d.ts +1 -0
- package/dist/builder/child/main.js +1408 -0
- package/dist/builder/child/main.js.map +1 -0
- package/dist/child-xrHmytks.js +49 -0
- package/dist/child-xrHmytks.js.map +1 -0
- package/dist/deps/child/install.d.ts +1 -0
- package/dist/deps/child/install.js +33 -0
- package/dist/deps/child/install.js.map +1 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/instance-D7_UVdoN.js +15752 -0
- package/dist/instance-D7_UVdoN.js.map +1 -0
- package/dist/types-NjNy7gXX.js +57 -0
- package/dist/types-NjNy7gXX.js.map +1 -0
- package/package.json +87 -3
- package/src/actions/index.ts +622 -0
- package/src/addressing/index.ts +281 -0
- package/src/bin.ts +240 -0
- package/src/builder/child/dev.ts +383 -0
- package/src/builder/child/fields.ts +363 -0
- package/src/builder/child/main.ts +47 -0
- package/src/builder/child/plugins.ts +195 -0
- package/src/builder/child/scan.ts +459 -0
- package/src/builder/child/spa.ts +348 -0
- package/src/builder/coverage.ts +72 -0
- package/src/builder/index.ts +406 -0
- package/src/builder/types.ts +180 -0
- package/src/deps/child/install.ts +30 -0
- package/src/deps/index.ts +918 -0
- package/src/deps/installer.ts +321 -0
- package/src/deps/lock.ts +82 -0
- package/src/deps/scan.ts +266 -0
- package/src/deps/specifier.ts +64 -0
- package/src/deps/warm.ts +57 -0
- package/src/dev/index.ts +518 -0
- package/src/faults.ts +65 -0
- package/src/gate.ts +55 -0
- package/src/gateway/actions.ts +268 -0
- package/src/gateway/browser.ts +650 -0
- package/src/gateway/capabilities.ts +739 -0
- package/src/gateway/controls.ts +91 -0
- package/src/gateway/dev.ts +119 -0
- package/src/gateway/http.ts +229 -0
- package/src/gateway/index.ts +1209 -0
- package/src/gateway/integrity.ts +117 -0
- package/src/gateway/listener.ts +217 -0
- package/src/gateway/routes.ts +197 -0
- package/src/gateway/serve.ts +138 -0
- package/src/gc/index.ts +451 -0
- package/src/home/acquire.ts +276 -0
- package/src/home/index.ts +16 -0
- package/src/home/paths.ts +192 -0
- package/src/home/platform.ts +34 -0
- package/src/home/record.ts +76 -0
- package/src/home/settings.ts +113 -0
- package/src/index.ts +30 -0
- package/src/instance/index.ts +942 -0
- package/src/interaction/ephemeral.ts +300 -0
- package/src/interaction/events.ts +355 -0
- package/src/interaction/fields.ts +392 -0
- package/src/interaction/index.ts +1009 -0
- package/src/interaction/outbox.ts +493 -0
- package/src/interaction/policy.ts +26 -0
- package/src/interaction/results.ts +341 -0
- package/src/interaction/runs.ts +313 -0
- package/src/interaction/signals.ts +120 -0
- package/src/interaction/submissions.ts +585 -0
- package/src/interaction/subscriptions.ts +258 -0
- package/src/interaction/viewers.ts +173 -0
- package/src/interaction/wait.ts +201 -0
- package/src/ledger/index.ts +442 -0
- package/src/ledger/migrations.ts +583 -0
- package/src/ledger/requests.ts +105 -0
- package/src/ledger/types.ts +28 -0
- package/src/live/index.ts +573 -0
- package/src/log.ts +316 -0
- package/src/processes/child.ts +67 -0
- package/src/processes/environment.ts +64 -0
- package/src/processes/identity.ts +191 -0
- package/src/processes/index.ts +19 -0
- package/src/processes/manager.ts +543 -0
- package/src/protocol-server/client.ts +94 -0
- package/src/protocol-server/index.ts +91 -0
- package/src/protocol-server/server.ts +509 -0
- package/src/protocol-server/streams.ts +81 -0
- package/src/publish/checks.ts +92 -0
- package/src/publish/digest.ts +87 -0
- package/src/publish/index.ts +23 -0
- package/src/publish/manifest.ts +409 -0
- package/src/publish/methods.ts +96 -0
- package/src/publish/publisher.ts +913 -0
- package/src/publish/source.ts +272 -0
- package/src/publish/tar.ts +110 -0
- package/src/registry/index.ts +481 -0
- package/src/registry/store.ts +136 -0
- package/src/secrets/index.ts +223 -0
- package/src/service/idle.ts +64 -0
- package/src/service/index.ts +3 -0
- package/src/service/unit.ts +250 -0
- package/src/verify/fields.ts +125 -0
- package/src/verify/index.ts +913 -0
- package/src/verify/regression.ts +157 -0
- package/src/verify/runner.ts +230 -0
- package/src/verify/sandbox.ts +407 -0
- package/src/verify/typecheck.ts +371 -0
- package/src/workers/index.ts +17 -0
- package/src/workers/launcher.ts +260 -0
- package/src/workers/pool.ts +708 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Package names and specs (D95): what an import specifier names, and what `deps/add` accepts.
|
|
3
|
+
*/
|
|
4
|
+
import { builtinModules } from "node:module";
|
|
5
|
+
import semver from "semver";
|
|
6
|
+
|
|
7
|
+
/** npm's package-name rules, with the capitals that packages published before 2017 may have. */
|
|
8
|
+
const NAME = /^(?:@[a-z0-9][a-z0-9._~-]*\/)?[a-zA-Z0-9][a-zA-Z0-9._~-]*$/;
|
|
9
|
+
const BUILTINS: ReadonlySet<string> = new Set(builtinModules);
|
|
10
|
+
|
|
11
|
+
/** Packages an artifact imports that are provided outside its lockfile. */
|
|
12
|
+
const PROVIDED: ReadonlySet<string> = new Set([
|
|
13
|
+
// The worker provides it to server code (WP-08); the server bundle keeps it external.
|
|
14
|
+
"@mostra/server",
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
/** Whether `name` is a valid npm package name. */
|
|
18
|
+
export function isPackageName(name: string): boolean {
|
|
19
|
+
return name.length <= 214 && NAME.test(name);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The npm package an import specifier names, or `undefined` for a specifier that isn't a package
|
|
24
|
+
* import: relative and absolute paths, the `@/` alias, URLs and other schemes, subpath imports
|
|
25
|
+
* (`#…`), Node built-ins, Vite's virtual modules and packages provided outside the lockfile.
|
|
26
|
+
*/
|
|
27
|
+
export function packageOf(specifier: string): string | undefined {
|
|
28
|
+
const bare = specifier.split("?")[0] ?? specifier;
|
|
29
|
+
if (bare === "" || bare.startsWith(".") || bare.startsWith("/") || bare.startsWith("#")) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
if (bare.startsWith("@/") || bare.startsWith("\0") || /^[a-z][a-z0-9+.-]*:/i.test(bare)) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
const parts = bare.split("/");
|
|
36
|
+
const name = bare.startsWith("@") ? parts.slice(0, 2).join("/") : (parts[0] ?? "");
|
|
37
|
+
if (BUILTINS.has(name) || PROVIDED.has(name) || !isPackageName(name)) return undefined;
|
|
38
|
+
return name;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A parsed registry spec: `name`, `name@version`, `name@range` or `name@tag`. */
|
|
42
|
+
export interface PackageSpec {
|
|
43
|
+
readonly name: string;
|
|
44
|
+
/** The version, range or tag after `@`; absent for a bare name. */
|
|
45
|
+
readonly range?: string;
|
|
46
|
+
/** Set when `range` is one exact version, which installs whatever its age (D95). */
|
|
47
|
+
readonly exact?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Parses a `deps/add` spec, or returns `undefined` when it isn't a registry spec. */
|
|
51
|
+
export function parseSpec(spec: string): PackageSpec | undefined {
|
|
52
|
+
const text = spec.trim();
|
|
53
|
+
const at = text.indexOf("@", text.startsWith("@") ? 1 : 0);
|
|
54
|
+
const name = at === -1 ? text : text.slice(0, at);
|
|
55
|
+
const range = at === -1 ? undefined : text.slice(at + 1);
|
|
56
|
+
if (!isPackageName(name)) return undefined;
|
|
57
|
+
if (range === undefined) return { name };
|
|
58
|
+
if (range === "") return undefined;
|
|
59
|
+
const exact = semver.valid(range);
|
|
60
|
+
if (exact !== null) return { name, range: exact, exact };
|
|
61
|
+
// A semver range, or a dist-tag (`latest`, `next`, `beta`).
|
|
62
|
+
if (semver.validRange(range) === null && !/^[a-z][a-z0-9._-]*$/i.test(range)) return undefined;
|
|
63
|
+
return { name, range };
|
|
64
|
+
}
|
package/src/deps/warm.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The warm set (D91, D95): the pre-built, read-only layout every artifact resolves without an
|
|
3
|
+
* install. `@mostra/warm`'s `dependencies` are the warm packages; its `node_modules` is the layout.
|
|
4
|
+
*/
|
|
5
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
6
|
+
import { dirname, join, resolve } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { createDiagnostic, RpcError } from "@mostra/protocol";
|
|
9
|
+
import type { Environment } from "../log.ts";
|
|
10
|
+
|
|
11
|
+
/** The warm set: where its layout is and what versions it holds. */
|
|
12
|
+
export interface WarmSet {
|
|
13
|
+
/** The real path of its `node_modules`, the layout builds link to. */
|
|
14
|
+
readonly nodeModules: string;
|
|
15
|
+
/** Package name → installed version, sorted by name; written into every `lock.yaml`. */
|
|
16
|
+
readonly packages: Readonly<Record<string, string>>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The directory of `@mostra/runtime`'s package.json. */
|
|
20
|
+
export function runtimeRoot(): string {
|
|
21
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
22
|
+
for (;;) {
|
|
23
|
+
const manifest = join(dir, "package.json");
|
|
24
|
+
if (existsSync(manifest)) {
|
|
25
|
+
const { name } = JSON.parse(readFileSync(manifest, "utf8")) as { name?: string };
|
|
26
|
+
if (name === "@mostra/runtime") return dir;
|
|
27
|
+
}
|
|
28
|
+
const parent = dirname(dir);
|
|
29
|
+
if (parent === dir) throw new Error("Couldn't find @mostra/runtime's package.json.");
|
|
30
|
+
dir = parent;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function warmMissing(message: string): RpcError {
|
|
35
|
+
return new RpcError(createDiagnostic("MOSTRA_ENV_WARM_MISSING", { message }));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Finds the warm set: `MOSTRA_WARM_DIR`, else the `warm` directory beside the runtime package, as
|
|
40
|
+
* the runtime bundle and this repository lay it out. Every listed package must be installed.
|
|
41
|
+
*/
|
|
42
|
+
export function locateWarmSet(env: Environment): WarmSet {
|
|
43
|
+
const dir = env["MOSTRA_WARM_DIR"] ?? resolve(runtimeRoot(), "..", "warm");
|
|
44
|
+
const manifestFile = join(dir, "package.json");
|
|
45
|
+
if (!existsSync(manifestFile)) throw warmMissing(`No warm package set at ${dir}.`);
|
|
46
|
+
const { dependencies = {} } = JSON.parse(readFileSync(manifestFile, "utf8")) as {
|
|
47
|
+
dependencies?: Record<string, string>;
|
|
48
|
+
};
|
|
49
|
+
const nodeModules = join(dir, "node_modules");
|
|
50
|
+
const packages: Record<string, string> = {};
|
|
51
|
+
for (const name of Object.keys(dependencies).sort()) {
|
|
52
|
+
const installed = join(nodeModules, name, "package.json");
|
|
53
|
+
if (!existsSync(installed)) throw warmMissing(`The warm package set at ${dir} lacks ${name}.`);
|
|
54
|
+
packages[name] = (JSON.parse(readFileSync(installed, "utf8")) as { version: string }).version;
|
|
55
|
+
}
|
|
56
|
+
return { nodeModules: realpathSync(nodeModules), packages };
|
|
57
|
+
}
|
package/src/dev/index.ts
ADDED
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `mostra dev` (VR-08, D140 item 12): a development preview of an artifact's unpublished source,
|
|
3
|
+
* with Vite's hot reload, its server code, live sources and submissions working, kept apart from
|
|
4
|
+
* everything a viewer can reach.
|
|
5
|
+
*
|
|
6
|
+
* A session is a sandbox like a verify run's (D138 item 1): a scratch ledger in
|
|
7
|
+
* `cache/dev/<session>/`, its own worker pool, and two loopback listeners on ports the operating
|
|
8
|
+
* system picks, reached only with the link minted in that scratch ledger; the artifact origin's
|
|
9
|
+
* files and HMR WebSocket come from a Vite server in a builder child, behind the gateway's checks.
|
|
10
|
+
* The source is mirrored into the session's root (Mostra never writes into the artifact's
|
|
11
|
+
* directory) and kept in step as it changes. Nothing reaches the home's drafts, state, events,
|
|
12
|
+
* hosts or deliveries; submissions are reported to the session's connection with their Results.
|
|
13
|
+
*
|
|
14
|
+
* A session belongs to the connection that started it and ends with it (or `dev/stop`); open
|
|
15
|
+
* sessions keep a daemon from its idle exit (D133). Recovery at start removes leftovers.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
19
|
+
import { type FSWatcher, watch } from "node:fs";
|
|
20
|
+
import { copyFile, mkdir, readdir, readFile, rm, stat, unlink, writeFile } from "node:fs/promises";
|
|
21
|
+
import { dirname, join } from "node:path";
|
|
22
|
+
import {
|
|
23
|
+
ArtifactId,
|
|
24
|
+
type ArtifactManifest,
|
|
25
|
+
createDiagnostic,
|
|
26
|
+
DEV_NOTIFY,
|
|
27
|
+
type DevEventKind,
|
|
28
|
+
type DevNotifyParams,
|
|
29
|
+
type Diagnostic,
|
|
30
|
+
devStartMethod,
|
|
31
|
+
devStopMethod,
|
|
32
|
+
RpcError,
|
|
33
|
+
type SourceRef,
|
|
34
|
+
VersionId,
|
|
35
|
+
} from "@mostra/protocol";
|
|
36
|
+
import type { DevChange, DevChild, DevServerBuild, RenderMode } from "../builder/index.ts";
|
|
37
|
+
import { type Dependencies, lockKey } from "../deps/index.ts";
|
|
38
|
+
import type { Faults } from "../faults.ts";
|
|
39
|
+
import type { Gate } from "../gate.ts";
|
|
40
|
+
import type { Log } from "../log.ts";
|
|
41
|
+
import { handler, type MethodHandler, type Session } from "../protocol-server/index.ts";
|
|
42
|
+
import {
|
|
43
|
+
extractManifest,
|
|
44
|
+
listSource,
|
|
45
|
+
type ResolvedSource,
|
|
46
|
+
resolveSource,
|
|
47
|
+
} from "../publish/index.ts";
|
|
48
|
+
import type { Registry } from "../registry/index.ts";
|
|
49
|
+
import { type Sandbox, type SandboxOptions, startSandbox } from "../verify/index.ts";
|
|
50
|
+
|
|
51
|
+
/** How often the mirror is compared with the source, besides file-system events. */
|
|
52
|
+
const POLL_MS = 1000;
|
|
53
|
+
/** How long the source must be still before the mirror catches up. */
|
|
54
|
+
const SETTLE_MS = 60;
|
|
55
|
+
|
|
56
|
+
/** Options for {@link createDev}. */
|
|
57
|
+
export interface DevOptions {
|
|
58
|
+
/** The home's real path. */
|
|
59
|
+
readonly home: string;
|
|
60
|
+
readonly log: Log;
|
|
61
|
+
readonly registry: Registry;
|
|
62
|
+
readonly renderMode: () => RenderMode;
|
|
63
|
+
readonly dependencies: () => Dependencies;
|
|
64
|
+
readonly gate: Gate;
|
|
65
|
+
/** What a sandbox needs of the instance, as verify's. */
|
|
66
|
+
readonly sandbox: () => SandboxOptions;
|
|
67
|
+
/** The address the instance's gateway binds: dev refuses to run beside a non-loopback bind. */
|
|
68
|
+
readonly bindAddress: string;
|
|
69
|
+
/** Sends a notification to one connection; false when it couldn't. */
|
|
70
|
+
readonly notify: (session: number, method: string, params: DevNotifyParams) => boolean;
|
|
71
|
+
/** Test fault points (`dev:after-child`: setting up a dev server that started fails). */
|
|
72
|
+
readonly faults?: Faults;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The dev sessions of an instance. */
|
|
76
|
+
export interface Dev {
|
|
77
|
+
methods(): MethodHandler[];
|
|
78
|
+
/** How many sessions are open: each keeps a daemon from its idle exit. */
|
|
79
|
+
count(): number;
|
|
80
|
+
/** An artifact's dependencies changed (`deps/add`): its sessions resolve them again. */
|
|
81
|
+
dependenciesChanged(artifact: SourceRef): void;
|
|
82
|
+
/**
|
|
83
|
+
* The lockfiles open sessions run on, which gc keeps (Opus review finding 2): a session's
|
|
84
|
+
* packages may be named by no working lockfile and no version.
|
|
85
|
+
*/
|
|
86
|
+
locks(): string[];
|
|
87
|
+
stopAll(): Promise<void>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The directory every session lives in. */
|
|
91
|
+
export const devRoot = (home: string): string => join(home, "cache", "dev");
|
|
92
|
+
|
|
93
|
+
/** Removes the sessions a stopped instance left; call it before any session starts. */
|
|
94
|
+
export async function recoverDev(home: string, log: Log): Promise<void> {
|
|
95
|
+
let left: string[];
|
|
96
|
+
try {
|
|
97
|
+
left = await readdir(devRoot(home));
|
|
98
|
+
} catch {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
for (const name of left) await rm(join(devRoot(home), name), { recursive: true, force: true });
|
|
102
|
+
if (left.length > 0) log.info("dev.sessions_removed", { count: left.length });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const LOOPBACK = new Set(["127.0.0.1", "::1", "localhost"]);
|
|
106
|
+
|
|
107
|
+
interface Running {
|
|
108
|
+
readonly id: string;
|
|
109
|
+
readonly owner: number;
|
|
110
|
+
readonly artifactId: string;
|
|
111
|
+
readonly bindingPath: string;
|
|
112
|
+
stop(): Promise<void>;
|
|
113
|
+
resolveAgain(): void;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Creates the instance's dev sessions. */
|
|
117
|
+
export function createDev(options: DevOptions): Dev {
|
|
118
|
+
const { log } = options;
|
|
119
|
+
const sessions = new Map<string, Running>();
|
|
120
|
+
/** Each session's current lockfile, from its resolution until its dev server stopped. */
|
|
121
|
+
const locksInUse = new Map<string, string>();
|
|
122
|
+
|
|
123
|
+
const start = async (
|
|
124
|
+
params: {
|
|
125
|
+
readonly source: { readonly path: string } | { readonly artifactId: string };
|
|
126
|
+
readonly port?: number;
|
|
127
|
+
},
|
|
128
|
+
connection: Session,
|
|
129
|
+
): Promise<{ url: string; port: number; sessionId: string }> => {
|
|
130
|
+
if (!LOOPBACK.has(options.bindAddress)) {
|
|
131
|
+
throw new RpcError(
|
|
132
|
+
createDiagnostic("MOSTRA_ENV_BIND_FAILED", {
|
|
133
|
+
message: `mostra dev serves only on loopback, and this instance binds ${options.bindAddress}.`,
|
|
134
|
+
hint: "Run mostra dev against an instance started without --bind.",
|
|
135
|
+
}),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
const path =
|
|
139
|
+
"path" in params.source
|
|
140
|
+
? params.source.path
|
|
141
|
+
: (options.registry.sourceOf(params.source.artifactId) ??
|
|
142
|
+
(() => {
|
|
143
|
+
throw new RpcError(
|
|
144
|
+
createDiagnostic("MOSTRA_ARTIFACT_NOT_FOUND", {
|
|
145
|
+
message: `No artifact ${params.source.artifactId} was published in this home; name it by its source path.`,
|
|
146
|
+
}),
|
|
147
|
+
);
|
|
148
|
+
})());
|
|
149
|
+
const source = await resolveSource(path);
|
|
150
|
+
const sessionId = `dev_${ArtifactId.create().slice("art_".length)}`;
|
|
151
|
+
const dir = join(devRoot(options.home), sessionId);
|
|
152
|
+
const root = join(dir, "root");
|
|
153
|
+
const versionDir = join(dir, "version");
|
|
154
|
+
await mkdir(join(root, "source"), { recursive: true, mode: 0o700 });
|
|
155
|
+
await mkdir(versionDir, { recursive: true });
|
|
156
|
+
const secret = randomBytes(32).toString("base64url");
|
|
157
|
+
const versionId = VersionId.fromSha256(
|
|
158
|
+
new Uint8Array(createHash("sha256").update(`mostra dev ${sessionId}`).digest()),
|
|
159
|
+
);
|
|
160
|
+
const notify = (kind: DevEventKind, extra: Partial<DevNotifyParams> = {}): void => {
|
|
161
|
+
options.notify(connection.id, DEV_NOTIFY, {
|
|
162
|
+
sessionId,
|
|
163
|
+
kind,
|
|
164
|
+
at: new Date().toISOString(),
|
|
165
|
+
...extra,
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
let closed = false;
|
|
170
|
+
let child: DevChild | undefined;
|
|
171
|
+
let sandbox: Sandbox | undefined;
|
|
172
|
+
let watcher: FSWatcher | undefined;
|
|
173
|
+
let poll: ReturnType<typeof setInterval> | undefined;
|
|
174
|
+
let settleTimer: ReturnType<typeof setTimeout> | undefined;
|
|
175
|
+
let outputDigest = "";
|
|
176
|
+
let nodeModules = "";
|
|
177
|
+
let manifestKey = "";
|
|
178
|
+
let artifactId = "";
|
|
179
|
+
const mirrored = new Map<string, string>();
|
|
180
|
+
|
|
181
|
+
/** Copies changed files into the mirror and removes deleted ones; what changed. */
|
|
182
|
+
const sync = async (): Promise<DevChange[]> => {
|
|
183
|
+
const files = await listSource(source);
|
|
184
|
+
const changed: DevChange[] = [];
|
|
185
|
+
const seen = new Set<string>();
|
|
186
|
+
for (const file of files) {
|
|
187
|
+
seen.add(file.path);
|
|
188
|
+
let info: Awaited<ReturnType<typeof stat>>;
|
|
189
|
+
try {
|
|
190
|
+
info = await stat(file.absolute);
|
|
191
|
+
} catch {
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
const key = `${info.size}:${info.mtimeMs}:${info.ino}`;
|
|
195
|
+
const before = mirrored.get(file.path);
|
|
196
|
+
if (before === key) continue;
|
|
197
|
+
const target = join(root, "source", ...file.path.split("/"));
|
|
198
|
+
await mkdir(dirname(target), { recursive: true });
|
|
199
|
+
await copyFile(file.absolute, target);
|
|
200
|
+
mirrored.set(file.path, key);
|
|
201
|
+
changed.push({ file: file.path, kind: before === undefined ? "add" : "change" });
|
|
202
|
+
}
|
|
203
|
+
for (const path of [...mirrored.keys()]) {
|
|
204
|
+
if (seen.has(path)) continue;
|
|
205
|
+
mirrored.delete(path);
|
|
206
|
+
await unlink(join(root, "source", ...path.split("/"))).catch(() => {});
|
|
207
|
+
changed.push({ file: path, kind: "unlink" });
|
|
208
|
+
}
|
|
209
|
+
return changed;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
/** The manifest and identity of the mirrored source. */
|
|
213
|
+
const describe = async (): Promise<{
|
|
214
|
+
manifest: ArtifactManifest;
|
|
215
|
+
artifactId: string;
|
|
216
|
+
entryHasDefault: boolean;
|
|
217
|
+
warnings: readonly Diagnostic[];
|
|
218
|
+
}> => {
|
|
219
|
+
const extracted = extractManifest(
|
|
220
|
+
source.entry,
|
|
221
|
+
await readFile(join(root, "source", source.entry), "utf8"),
|
|
222
|
+
);
|
|
223
|
+
const manifest = extracted.manifest ?? { title: titleOf(source) };
|
|
224
|
+
const artifactId =
|
|
225
|
+
manifest.id ??
|
|
226
|
+
options.registry.artifactAt(source.bindingPath) ??
|
|
227
|
+
stableId(source.bindingPath);
|
|
228
|
+
return {
|
|
229
|
+
manifest,
|
|
230
|
+
artifactId,
|
|
231
|
+
entryHasDefault: extracted.hasDefaultExport,
|
|
232
|
+
warnings: extracted.warnings,
|
|
233
|
+
};
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
/** Resolves the packages, and writes the lockfile the worker resolves server packages by. */
|
|
237
|
+
const resolve = async (manifestId: string | undefined): Promise<string> => {
|
|
238
|
+
// The lock is known to gc before the gate is released, so no gc can take its layout
|
|
239
|
+
// between the resolution and the dev server's start.
|
|
240
|
+
const resolution = await options.gate.shared(async () => {
|
|
241
|
+
const resolved = await options.dependencies().resolve({
|
|
242
|
+
key: lockKey(
|
|
243
|
+
manifestId ?? options.registry.artifactAt(source.bindingPath),
|
|
244
|
+
source.bindingPath,
|
|
245
|
+
),
|
|
246
|
+
sourceDir: join(root, "source"),
|
|
247
|
+
artifactDir: source.root,
|
|
248
|
+
artifactPath: path,
|
|
249
|
+
});
|
|
250
|
+
if (!closed) locksInUse.set(sessionId, resolved.lock);
|
|
251
|
+
return resolved;
|
|
252
|
+
});
|
|
253
|
+
await writeFile(join(versionDir, "lock.yaml"), resolution.lock);
|
|
254
|
+
return resolution.nodeModules;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
/** Writes the dev version's manifest with a new output digest for its new server bundle. */
|
|
258
|
+
const writeVersion = async (
|
|
259
|
+
manifest: ArtifactManifest,
|
|
260
|
+
artifactId: string,
|
|
261
|
+
server: boolean,
|
|
262
|
+
): Promise<void> => {
|
|
263
|
+
outputDigest = createHash("sha256")
|
|
264
|
+
.update(`${sessionId}:${Date.now()}:${randomBytes(8).toString("hex")}`)
|
|
265
|
+
.digest("hex");
|
|
266
|
+
await writeFile(
|
|
267
|
+
join(versionDir, "manifest.json"),
|
|
268
|
+
`${JSON.stringify({ schemaVersion: 1, artifactId, versionId, mode: "spa", entry: source.entry, server, manifest, outputDigest, forcedCodes: [], dev: true }, null, 2)}\n`,
|
|
269
|
+
);
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const reported = (build: DevServerBuild): void => {
|
|
273
|
+
if (!build.ok) notify("diagnostics", { diagnostics: build.diagnostics });
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
/** Starts (or restarts) the dev child over the current mirror. */
|
|
277
|
+
const startChild = async (
|
|
278
|
+
described: Awaited<ReturnType<typeof describe>>,
|
|
279
|
+
): Promise<DevChild> => {
|
|
280
|
+
const started = await options.renderMode().dev(
|
|
281
|
+
{
|
|
282
|
+
root,
|
|
283
|
+
entry: source.entry,
|
|
284
|
+
entryHasDefault: described.entryHasDefault,
|
|
285
|
+
manifest: described.manifest,
|
|
286
|
+
versionId,
|
|
287
|
+
nodeModules,
|
|
288
|
+
outDir: versionDir,
|
|
289
|
+
secret,
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
server: (build, files) => {
|
|
293
|
+
void (async () => {
|
|
294
|
+
if (build.ok) {
|
|
295
|
+
await writeVersion(described.manifest, described.artifactId, build.server);
|
|
296
|
+
await sandbox?.replaceServer();
|
|
297
|
+
await child?.reload().catch(() => {});
|
|
298
|
+
notify("server-restarted", { files });
|
|
299
|
+
} else reported(build);
|
|
300
|
+
})().catch((error: unknown) => log.warn("dev.server_reload_failed", { error }));
|
|
301
|
+
},
|
|
302
|
+
update: (files) => notify("built", { files }),
|
|
303
|
+
},
|
|
304
|
+
);
|
|
305
|
+
void started.exited.then(() => {
|
|
306
|
+
if (!closed && child === started) {
|
|
307
|
+
log.warn("dev.child_exited", { sessionId });
|
|
308
|
+
notify("diagnostics", {
|
|
309
|
+
diagnostics: [
|
|
310
|
+
createDiagnostic("MOSTRA_BUILD_CRASHED", { message: "The dev server stopped." }),
|
|
311
|
+
],
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
// A dev server this function can't hand back (its version's manifest can't be written,
|
|
316
|
+
// because a stop removed the session meanwhile) is stopped here: nobody else knows it.
|
|
317
|
+
try {
|
|
318
|
+
options.faults?.at("dev:after-child");
|
|
319
|
+
if (started.ready.build.ok)
|
|
320
|
+
await writeVersion(described.manifest, described.artifactId, started.ready.build.server);
|
|
321
|
+
else reported(started.ready.build);
|
|
322
|
+
} catch (error) {
|
|
323
|
+
await started.stop().catch(() => {});
|
|
324
|
+
throw error;
|
|
325
|
+
}
|
|
326
|
+
return started;
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
/** Catches the mirror up; resolves packages again and restarts the dev server when they or
|
|
330
|
+
* the manifest changed. */
|
|
331
|
+
let catching: Promise<void> = Promise.resolve();
|
|
332
|
+
const catchUp = (forceResolve = false): void => {
|
|
333
|
+
catching = catching
|
|
334
|
+
.then(async () => {
|
|
335
|
+
if (closed) return;
|
|
336
|
+
const changed = await sync();
|
|
337
|
+
if (changed.length === 0 && !forceResolve) return;
|
|
338
|
+
const described = await describe();
|
|
339
|
+
const key = JSON.stringify(described.manifest);
|
|
340
|
+
const next = await resolve(described.manifest.id).catch((error: unknown) => {
|
|
341
|
+
if (error instanceof RpcError)
|
|
342
|
+
notify("diagnostics", { diagnostics: [error.diagnostic] });
|
|
343
|
+
return nodeModules;
|
|
344
|
+
});
|
|
345
|
+
const files = changed.map((change) => change.file);
|
|
346
|
+
if (next !== nodeModules || key !== manifestKey) {
|
|
347
|
+
// Other packages or another manifest: a new dev server over the same mirror, its
|
|
348
|
+
// server bundle in the worker, and every page loaded again (D140 item 12).
|
|
349
|
+
nodeModules = next;
|
|
350
|
+
manifestKey = key;
|
|
351
|
+
const old = child;
|
|
352
|
+
child = await startChild(described);
|
|
353
|
+
await old?.stop();
|
|
354
|
+
await sandbox?.replaceServer();
|
|
355
|
+
notify("reloaded", { files });
|
|
356
|
+
} else if (changed.length > 0) {
|
|
357
|
+
await child?.changed(changed);
|
|
358
|
+
}
|
|
359
|
+
})
|
|
360
|
+
.catch((error: unknown) => log.warn("dev.sync_failed", { sessionId, error }));
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
/** Releases everything the session holds; safe to run again, and while it still starts. */
|
|
364
|
+
const release = async (): Promise<void> => {
|
|
365
|
+
clearInterval(poll);
|
|
366
|
+
clearTimeout(settleTimer);
|
|
367
|
+
watcher?.close();
|
|
368
|
+
await catching.catch(() => {});
|
|
369
|
+
await child?.stop().catch(() => {});
|
|
370
|
+
await sandbox?.close().catch(() => {});
|
|
371
|
+
locksInUse.delete(sessionId);
|
|
372
|
+
await rm(dir, { recursive: true, force: true });
|
|
373
|
+
};
|
|
374
|
+
const stop = async (): Promise<void> => {
|
|
375
|
+
if (closed) return;
|
|
376
|
+
closed = true;
|
|
377
|
+
sessions.delete(sessionId);
|
|
378
|
+
await release();
|
|
379
|
+
log.info("dev.stopped", { sessionId });
|
|
380
|
+
};
|
|
381
|
+
/** The connection that started the session closed while it started: nothing may outlive it. */
|
|
382
|
+
const abandoned = (): RpcError =>
|
|
383
|
+
new RpcError(
|
|
384
|
+
createDiagnostic("MOSTRA_HOST_CONNECTION_CLOSED", {
|
|
385
|
+
message: "The connection that started this dev session closed while it started.",
|
|
386
|
+
}),
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
// The session ends with its connection (D140 item 12), from the first moment: a connection
|
|
390
|
+
// that closes while packages resolve or the dev server starts leaves nothing running.
|
|
391
|
+
connection.signal.addEventListener("abort", () => void stop(), { once: true });
|
|
392
|
+
try {
|
|
393
|
+
if (connection.signal.aborted) throw abandoned();
|
|
394
|
+
await sync();
|
|
395
|
+
const described = await describe();
|
|
396
|
+
manifestKey = JSON.stringify(described.manifest);
|
|
397
|
+
nodeModules = await resolve(described.manifest.id);
|
|
398
|
+
artifactId = described.artifactId;
|
|
399
|
+
child = await startChild(described);
|
|
400
|
+
if (closed) throw abandoned();
|
|
401
|
+
const upstream = (): { port: number; secret: string } | undefined =>
|
|
402
|
+
child ? { port: child.ready.port, secret } : undefined;
|
|
403
|
+
sandbox = await startSandbox(options.sandbox(), {
|
|
404
|
+
artifactId: described.artifactId,
|
|
405
|
+
versions: [
|
|
406
|
+
{
|
|
407
|
+
versionId,
|
|
408
|
+
dir: versionDir,
|
|
409
|
+
manifest: described.manifest,
|
|
410
|
+
sourceDigest: "0".repeat(64),
|
|
411
|
+
envDigest: "0".repeat(64),
|
|
412
|
+
get outputDigest() {
|
|
413
|
+
return outputDigest;
|
|
414
|
+
},
|
|
415
|
+
},
|
|
416
|
+
],
|
|
417
|
+
sourceDir: source.root,
|
|
418
|
+
runDir: join(dir, "sandbox"),
|
|
419
|
+
dev: { upstream },
|
|
420
|
+
...(params.port !== undefined ? { shellPort: params.port } : {}),
|
|
421
|
+
});
|
|
422
|
+
if (closed) throw abandoned();
|
|
423
|
+
sandbox.onSubmission((results) => notify("submission", { results }));
|
|
424
|
+
} catch (error) {
|
|
425
|
+
closed = true;
|
|
426
|
+
sessions.delete(sessionId);
|
|
427
|
+
// Again, after whatever a stop that ran meanwhile couldn't see yet (a child or sandbox
|
|
428
|
+
// that finished starting after it).
|
|
429
|
+
await release();
|
|
430
|
+
throw error;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Keep the mirror in step: file-system events when the platform gives them, a poll anyway.
|
|
434
|
+
try {
|
|
435
|
+
watcher = watch(source.kind === "dir" ? source.root : join(source.root, source.entry), {
|
|
436
|
+
recursive: source.kind === "dir",
|
|
437
|
+
persistent: false,
|
|
438
|
+
});
|
|
439
|
+
watcher.on("change", () => {
|
|
440
|
+
clearTimeout(settleTimer);
|
|
441
|
+
settleTimer = setTimeout(() => catchUp(), SETTLE_MS);
|
|
442
|
+
});
|
|
443
|
+
watcher.on("error", () => watcher?.close());
|
|
444
|
+
} catch {
|
|
445
|
+
watcher = undefined;
|
|
446
|
+
}
|
|
447
|
+
poll = setInterval(() => catchUp(), POLL_MS);
|
|
448
|
+
poll.unref();
|
|
449
|
+
|
|
450
|
+
sessions.set(sessionId, {
|
|
451
|
+
id: sessionId,
|
|
452
|
+
owner: connection.id,
|
|
453
|
+
artifactId,
|
|
454
|
+
bindingPath: source.bindingPath,
|
|
455
|
+
stop,
|
|
456
|
+
resolveAgain: () => catchUp(true),
|
|
457
|
+
});
|
|
458
|
+
const url = sandbox.link(versionId);
|
|
459
|
+
log.info("dev.started", { sessionId, artifactId });
|
|
460
|
+
notify("built");
|
|
461
|
+
return { url, port: Number(new URL(url).port), sessionId };
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
return {
|
|
465
|
+
methods: () => [
|
|
466
|
+
handler(devStartMethod, (params, session) => start(params, session)),
|
|
467
|
+
handler(devStopMethod, async (params, session) => {
|
|
468
|
+
const running = sessions.get(params.sessionId);
|
|
469
|
+
if (!running || running.owner !== session.id) {
|
|
470
|
+
throw new RpcError(
|
|
471
|
+
createDiagnostic("MOSTRA_HOST_INVALID_PARAMS", {
|
|
472
|
+
message: `This connection has no dev session ${params.sessionId}.`,
|
|
473
|
+
}),
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
await running.stop();
|
|
477
|
+
return {};
|
|
478
|
+
}),
|
|
479
|
+
],
|
|
480
|
+
count: () => sessions.size,
|
|
481
|
+
locks: () => [...locksInUse.values()],
|
|
482
|
+
dependenciesChanged: (artifact) => {
|
|
483
|
+
void (async () => {
|
|
484
|
+
const bindingPath =
|
|
485
|
+
"path" in artifact
|
|
486
|
+
? (await resolveSource(artifact.path).catch(() => undefined))?.bindingPath
|
|
487
|
+
: undefined;
|
|
488
|
+
for (const running of sessions.values()) {
|
|
489
|
+
const same =
|
|
490
|
+
"artifactId" in artifact
|
|
491
|
+
? running.artifactId === artifact.artifactId
|
|
492
|
+
: running.bindingPath === bindingPath;
|
|
493
|
+
if (same) running.resolveAgain();
|
|
494
|
+
}
|
|
495
|
+
})();
|
|
496
|
+
},
|
|
497
|
+
stopAll: async () => {
|
|
498
|
+
await Promise.all([...sessions.values()].map((running) => running.stop()));
|
|
499
|
+
},
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/** The title of a source without a manifest: its directory's or file's name (INTERFACES §2.2). */
|
|
504
|
+
function titleOf(source: ResolvedSource): string {
|
|
505
|
+
const name =
|
|
506
|
+
source.kind === "dir" ? source.root.split("/").at(-1) : source.entry.replace(/\.[^.]+$/, "");
|
|
507
|
+
return name || "Untitled";
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
511
|
+
|
|
512
|
+
/** A stand-in artifact ID for a source no manifest `id` or binding names: stable per path. */
|
|
513
|
+
function stableId(bindingPath: string): string {
|
|
514
|
+
const hash = createHash("sha256").update(`mostra dev provisional\u0000${bindingPath}`).digest();
|
|
515
|
+
let body = CROCKFORD[(hash[0] as number) % 8] as string;
|
|
516
|
+
for (let index = 1; index < 26; index += 1) body += CROCKFORD[(hash[index] as number) % 32];
|
|
517
|
+
return ArtifactId.parse(`art_${body}`);
|
|
518
|
+
}
|