@volter/world-runtime 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/known-external-services.json +0 -8
- package/dist/src/branch.js +79 -2
- package/dist/src/cli.js +47 -19
- package/dist/src/fixture-env.d.ts +3 -0
- package/dist/src/fixture-env.js +24 -0
- package/dist/src/infra-cli.js +59 -10
- package/dist/src/init.js +31 -5
- package/dist/src/pglite-backing.d.ts +8 -0
- package/dist/src/pglite-backing.js +118 -35
- package/dist/src/pglite-host.mjs +520 -14
- package/dist/src/redirect-proxy.d.ts +1 -1
- package/dist/src/redirect-proxy.js +2 -2
- package/dist/src/redis-backing.d.ts +8 -0
- package/dist/src/redis-backing.js +120 -0
- package/dist/src/runtime.d.ts +1 -0
- package/dist/src/runtime.js +24 -7
- package/known-external-services.json +0 -8
- package/package.json +9 -4
- package/src/branch.ts +66 -2
- package/src/cli.ts +40 -18
- package/src/fixture-env.ts +25 -0
- package/src/infra-cli.ts +54 -10
- package/src/init.ts +31 -5
- package/src/pglite-backing.ts +109 -36
- package/src/pglite-host.mjs +520 -14
- package/src/redirect-proxy.ts +2 -2
- package/src/redis-backing.ts +107 -0
- package/src/runtime.ts +25 -7
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
// The containerless backing for World-managed infrastructure
|
|
2
|
-
// (see pglite-host.
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
1
|
+
// The containerless backing for World-managed infrastructure, instead of docker
|
|
2
|
+
// compose: a POSTGRES service is a PGlite host (see pglite-host.mjs), a MONGODB
|
|
3
|
+
// service is the MongoDB twin (@volter/twin-mongodb: the MongoDB wire protocol over
|
|
4
|
+
// the World state kernel). Selected by `volter-world-infra` when no container
|
|
5
|
+
// runtime is available (or by explicit VOLTER_WORLD_INFRA_BACKING=pglite); the
|
|
6
|
+
// world config, definition file, injected env, and declared-service contract are
|
|
7
|
+
// byte-identical either way — backing is private, exactly as the boundary comment
|
|
8
|
+
// in infra-cli.ts promises. Each service's bytes live under VOLTER_WORLD_DATA, so
|
|
9
|
+
// they survive down/up as the compose volumes do.
|
|
8
10
|
//
|
|
9
|
-
// Capability is stated, not stretched: this backing serves POSTGRES
|
|
10
|
-
// only. A definition declaring mysql
|
|
11
|
-
// refused with the kinds named, never half-booted.
|
|
12
|
-
|
|
11
|
+
// Capability is stated, not stretched: this backing serves POSTGRES and MONGODB
|
|
12
|
+
// services only. A definition declaring mysql without a container runtime is
|
|
13
|
+
// refused with the kinds named, never half-booted. Redis never reaches it: every
|
|
14
|
+
// redis service is the redis twin's (redis-backing.ts).
|
|
15
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
13
16
|
import { connect } from 'node:net';
|
|
14
17
|
import { dirname, join } from 'node:path';
|
|
15
|
-
import { spawn } from 'node:child_process';
|
|
18
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
19
|
+
import { fileURLToPath } from 'node:url';
|
|
20
|
+
import { findInstalledPackage, packCli } from "./catalog.js";
|
|
16
21
|
/** Parse the services out of world.infrastructure.yml. This is NOT a YAML
|
|
17
22
|
* parser: init's renderComposeFile emits a deterministic shape, and this
|
|
18
23
|
* reads exactly that shape back (service key at 2-space indent = the kind;
|
|
@@ -61,10 +66,46 @@ export function infraConnections(services, env) {
|
|
|
61
66
|
}
|
|
62
67
|
return connections;
|
|
63
68
|
}
|
|
64
|
-
|
|
69
|
+
// this module's directory under Node as under Bun: `volter-world-infra` is a node-shebang bin, where
|
|
70
|
+
// `import.meta.dir` (Bun's) is undefined
|
|
71
|
+
const HERE = fileURLToPath(new URL('.', import.meta.url));
|
|
72
|
+
const HOST = join(HERE, 'pglite-host.mjs');
|
|
65
73
|
const READY_TIMEOUT_MS = 60_000;
|
|
74
|
+
const MONGODB_TWIN = '@volter/twin-mongodb';
|
|
75
|
+
/** The kinds this backing serves, and the name its files carry under the data dir. */
|
|
76
|
+
const CONTAINERLESS = { postgres: 'pglite-postgres', mongodb: 'twin-mongodb' };
|
|
66
77
|
function pidPath(dataDir, kind) {
|
|
67
|
-
return join(dataDir, `pglite-${kind}.pid`);
|
|
78
|
+
return join(dataDir, `${CONTAINERLESS[kind] ?? `pglite-${kind}`}.pid`);
|
|
79
|
+
}
|
|
80
|
+
/** The MongoDB twin's cli: the package installed above the World's config (an app repo), else the one
|
|
81
|
+
* this runtime was installed with (its dependency, or the checkout's workspace link). */
|
|
82
|
+
export function mongodbTwinCli(worldConfig, froms = [...(worldConfig ? [dirname(worldConfig)] : []), HERE]) {
|
|
83
|
+
for (const from of froms) {
|
|
84
|
+
const dir = findInstalledPackage(from, MONGODB_TWIN);
|
|
85
|
+
const cli = dir === undefined ? undefined : packCli(dir);
|
|
86
|
+
if (cli !== undefined)
|
|
87
|
+
return cli;
|
|
88
|
+
}
|
|
89
|
+
throw new Error(`the containerless mongodb service is served by ${MONGODB_TWIN} (an optional dependency of @volter/world-runtime), which is not installed above ${froms.join(' or ')} — install it, or run the World with a container runtime`);
|
|
90
|
+
}
|
|
91
|
+
/** The runtime a twin's cli runs under. A published package's cli is JavaScript and runs under this
|
|
92
|
+
* process's own runtime; a checkout's is TypeScript whose kernel needs more than Node's type
|
|
93
|
+
* stripping, so it runs under Bun (this process's, or the one on PATH) — the repository's toolchain —
|
|
94
|
+
* and without Bun it is refused by name rather than started to fail. */
|
|
95
|
+
export function twinRunner(cli, bunOnPath = () => spawnSync('bun', ['--version'], { stdio: 'ignore', timeout: 10_000 }).status === 0, underBun = typeof globalThis.Bun !== 'undefined') {
|
|
96
|
+
if (!cli.endsWith('.ts') || underBun)
|
|
97
|
+
return process.execPath;
|
|
98
|
+
if (bunOnPath())
|
|
99
|
+
return 'bun';
|
|
100
|
+
throw new Error(`a checkout's MongoDB twin runs under bun, which is not on PATH (${cli} is TypeScript that Node cannot run)`);
|
|
101
|
+
}
|
|
102
|
+
/** The argv that serves one service on its declared loopback port, with its bytes in `serviceData`. */
|
|
103
|
+
function hostArgv(service, serviceData) {
|
|
104
|
+
if (service.kind === 'mongodb') {
|
|
105
|
+
const cli = mongodbTwinCli(process.env.VOLTER_WORLD_CONFIG);
|
|
106
|
+
return [twinRunner(cli), cli, 'serve', '--port', String(service.hostPort), '--root', serviceData];
|
|
107
|
+
}
|
|
108
|
+
return ['node', HOST, '--port', String(service.hostPort), '--data', serviceData];
|
|
68
109
|
}
|
|
69
110
|
function alive(pid) {
|
|
70
111
|
try {
|
|
@@ -88,36 +129,78 @@ function listening(port) {
|
|
|
88
129
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
89
130
|
/** The kinds this backing cannot serve, or [] when it can serve the world. */
|
|
90
131
|
export function unsupportedKinds(services) {
|
|
91
|
-
return [...new Set(services.filter((s) => s.kind
|
|
132
|
+
return [...new Set(services.filter((s) => CONTAINERLESS[s.kind] === undefined).map((s) => s.kind))];
|
|
133
|
+
}
|
|
134
|
+
/** The line each host prints once ITS OWN listener is bound: readiness is taken from the child, never
|
|
135
|
+
* from a connect to the port, which any other process holding the port would answer. */
|
|
136
|
+
function readyLine(kind, port) {
|
|
137
|
+
return kind === 'mongodb' ? `listening on mongodb://127.0.0.1:${port}` : `serving postgres wire protocol on 127.0.0.1:${port}`;
|
|
138
|
+
}
|
|
139
|
+
function logSince(log, offset) {
|
|
140
|
+
try {
|
|
141
|
+
return readFileSync(log).subarray(offset).toString('utf8');
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return '';
|
|
145
|
+
}
|
|
92
146
|
}
|
|
93
147
|
export async function pgliteUp(services, dataDir) {
|
|
148
|
+
// Every check before any start: a refused up starts nothing, so it never leaves the World half-booted.
|
|
149
|
+
const plan = [];
|
|
94
150
|
for (const service of services) {
|
|
95
151
|
const pidFile = pidPath(dataDir, service.kind);
|
|
96
152
|
if (existsSync(pidFile) && alive(Number(readFileSync(pidFile, 'utf8').trim())))
|
|
97
153
|
continue;
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
const log = join(dataDir,
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
154
|
+
const name = CONTAINERLESS[service.kind] ?? `pglite-${service.kind}`;
|
|
155
|
+
const serviceData = join(dataDir, `${name}-data`);
|
|
156
|
+
const log = join(dataDir, `${name}.log`);
|
|
157
|
+
if (await listening(service.hostPort)) {
|
|
158
|
+
throw new Error(`${name} cannot serve port ${service.hostPort}: another process is already listening on it`);
|
|
159
|
+
}
|
|
160
|
+
plan.push({ service, name, serviceData, log, pidFile, argv: hostArgv(service, serviceData) });
|
|
161
|
+
}
|
|
162
|
+
const started = [];
|
|
163
|
+
try {
|
|
164
|
+
for (const { service, name, serviceData, log, pidFile, argv } of plan) {
|
|
165
|
+
mkdirSync(serviceData, { recursive: true });
|
|
166
|
+
mkdirSync(dirname(log), { recursive: true });
|
|
167
|
+
const offset = existsSync(log) ? statSync(log).size : 0;
|
|
168
|
+
const [command, ...args] = argv;
|
|
169
|
+
// a real fd, not a pipe: the host must outlive this process untethered
|
|
170
|
+
const logFd = openSync(log, 'a');
|
|
171
|
+
const child = spawn(command, args, {
|
|
172
|
+
detached: true,
|
|
173
|
+
stdio: ['ignore', logFd, logFd],
|
|
174
|
+
});
|
|
175
|
+
child.unref();
|
|
176
|
+
closeSync(logFd);
|
|
177
|
+
writeFileSync(pidFile, `${child.pid}\n`);
|
|
178
|
+
started.push({ pid: child.pid, pidFile });
|
|
179
|
+
const deadline = Date.now() + READY_TIMEOUT_MS;
|
|
180
|
+
const line = readyLine(service.kind, service.hostPort);
|
|
181
|
+
// ready when OUR child says its listener is bound (a squatter that took the port after the check
|
|
182
|
+
// above makes the child fail its bind and exit, never print this) and the port answers
|
|
183
|
+
while (!(alive(child.pid) && logSince(log, offset).includes(line) && await listening(service.hostPort))) {
|
|
184
|
+
if (!alive(child.pid))
|
|
185
|
+
throw new Error(`${name} exited before serving — see ${log}`);
|
|
186
|
+
if (Date.now() > deadline)
|
|
187
|
+
throw new Error(`${name} did not serve port ${service.hostPort} within ${READY_TIMEOUT_MS / 1000}s — see ${log}`);
|
|
188
|
+
await sleep(200);
|
|
115
189
|
}
|
|
116
|
-
|
|
117
|
-
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
// tear down what this up started, so a failed up leaves nothing running
|
|
194
|
+
for (const { pid, pidFile } of started) {
|
|
195
|
+
if (alive(pid)) {
|
|
196
|
+
try {
|
|
197
|
+
process.kill(pid, 'SIGKILL');
|
|
198
|
+
}
|
|
199
|
+
catch { /* already gone */ }
|
|
118
200
|
}
|
|
119
|
-
|
|
201
|
+
rmSync(pidFile, { force: true });
|
|
120
202
|
}
|
|
203
|
+
throw error;
|
|
121
204
|
}
|
|
122
205
|
}
|
|
123
206
|
export async function pgliteStatus(services, dataDir) {
|