@neta-art/cohub-cli 10.19.0 → 10.21.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/README.md +1 -0
- package/dist/about.d.ts +2 -0
- package/dist/about.js +71 -0
- package/dist/board-export.d.ts +4 -1
- package/dist/board-export.js +46 -13
- package/dist/board-gpu/daemon.d.ts +11 -0
- package/dist/board-gpu/daemon.js +295 -0
- package/dist/board-gpu/index.d.ts +3 -2
- package/dist/board-gpu/index.js +1 -0
- package/dist/board-gpu/page.js +1486 -407
- package/dist/board-gpu/protocol.d.ts +40 -1
- package/dist/board-kernel.d.ts +2 -2
- package/dist/board-kernel.js +1762 -458
- package/dist/board-media-export.d.ts +1 -0
- package/dist/board-media-export.js +17 -0
- package/dist/board-profile.d.ts +21 -0
- package/dist/board-profile.js +67 -0
- package/dist/board-simulate.d.ts +11 -1
- package/dist/board-simulate.js +116 -38
- package/dist/build.json +1 -0
- package/dist/commands/boards.js +154 -8
- package/dist/commands/feedback.js +2 -0
- package/dist/commands/version.d.ts +2 -0
- package/dist/commands/version.js +21 -0
- package/dist/host/serve.js +9 -3
- package/dist/index.js +4 -11
- package/dist/runtime/connection.js +2 -0
- package/dist/runtime/presentation.d.ts +1 -0
- package/dist/runtime/presentation.js +1 -0
- package/dist/runtime/supervisor.js +2 -1
- package/dist/version.d.ts +2 -0
- package/dist/version.js +17 -0
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -174,6 +174,7 @@ cohub runtime up ./project --harness pi --harness codex
|
|
|
174
174
|
cohub runtime up ./project --space <spaceId> --harness codex
|
|
175
175
|
cohub -s <spaceId> spaces prompt "Continue" --harness codex
|
|
176
176
|
cohub runtime status --space <spaceId>
|
|
177
|
+
cohub version # this CLI, the cohub on PATH, and each running Runtime's version
|
|
177
178
|
cohub runtime logs --space <spaceId> --follow
|
|
178
179
|
cohub runtime up -d
|
|
179
180
|
cohub runtime down
|
package/dist/about.d.ts
ADDED
package/dist/about.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import { access, realpath, stat } from "node:fs/promises";
|
|
4
|
+
import { delimiter, join } from "node:path";
|
|
5
|
+
import { controlRuntimeInstance, runtimeInstanceDirectory } from "./runtime/instance.js";
|
|
6
|
+
import { readRuntimeSpaceBindings } from "./runtime/space-binding.js";
|
|
7
|
+
import { CLI_VERSION, cliCommit } from "./version.js";
|
|
8
|
+
const PROBE_TIMEOUT_MS = 5_000;
|
|
9
|
+
const STATUS_TIMEOUT_MS = 1_000;
|
|
10
|
+
const PROBE_TTL_MS = 5 * 60_000;
|
|
11
|
+
let probe = null;
|
|
12
|
+
export async function describeCli(env, identity) {
|
|
13
|
+
const [path, runtimes] = await Promise.all([pathCli(env), identity ? runningRuntimes(identity) : []]);
|
|
14
|
+
return { version: CLI_VERSION, commit: cliCommit(), node: process.versions.node, path, runtimes };
|
|
15
|
+
}
|
|
16
|
+
async function pathCli(env) {
|
|
17
|
+
const path = await findOnPath("cohub", await searchPath(env), env);
|
|
18
|
+
if (!path)
|
|
19
|
+
return null;
|
|
20
|
+
// A shim keeps its file across upgrades, so a remembered answer also expires.
|
|
21
|
+
const target = await realpath(path).catch(() => path);
|
|
22
|
+
const key = `${target}\0${await stat(target).then((info) => info.mtimeMs, () => 0)}`;
|
|
23
|
+
if (probe?.key !== key || Date.now() - probe.at > PROBE_TTL_MS)
|
|
24
|
+
probe = { key, at: Date.now(), version: askVersion(path, env) };
|
|
25
|
+
const current = probe;
|
|
26
|
+
const version = await current.version;
|
|
27
|
+
if (!version && probe === current)
|
|
28
|
+
probe = null;
|
|
29
|
+
return { path, version };
|
|
30
|
+
}
|
|
31
|
+
/** A GUI app's own PATH is stale on Windows; the registry holds what a new terminal gets. */
|
|
32
|
+
async function searchPath(env) {
|
|
33
|
+
const own = (env.PATH ?? env.Path ?? "").split(delimiter);
|
|
34
|
+
if (process.platform !== "win32")
|
|
35
|
+
return own.filter(Boolean);
|
|
36
|
+
const read = (key) => new Promise((resolve) => execFile("reg.exe", ["query", key, "/v", "Path"], { timeout: PROBE_TIMEOUT_MS, windowsHide: true }, (_error, stdout) => {
|
|
37
|
+
resolve(/Path\s+REG_(?:EXPAND_)?SZ\s+(.*)/i.exec(stdout ?? "")?.[1]?.trim() ?? "");
|
|
38
|
+
}));
|
|
39
|
+
const [machine, user] = await Promise.all([read("HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"), read("HKCU\\Environment")]);
|
|
40
|
+
const expand = (value) => value.replace(/%([^%]+)%/g, (whole, name) => env[name] ?? env[name.toUpperCase()] ?? whole);
|
|
41
|
+
return [...new Set([machine, user].flatMap((value) => expand(value).split(delimiter)).concat(own).filter(Boolean))];
|
|
42
|
+
}
|
|
43
|
+
async function findOnPath(name, directories, env) {
|
|
44
|
+
const windows = process.platform === "win32";
|
|
45
|
+
const extensions = windows ? (env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean) : [""];
|
|
46
|
+
for (const directory of directories) {
|
|
47
|
+
for (const extension of extensions) {
|
|
48
|
+
const candidate = join(directory, name + extension.toLowerCase());
|
|
49
|
+
if (await access(candidate, windows ? constants.F_OK : constants.X_OK).then(() => true, () => false))
|
|
50
|
+
return candidate;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
function askVersion(path, env) {
|
|
56
|
+
// Node refuses to spawn batch files directly; cmd.exe runs them with the path quoted verbatim.
|
|
57
|
+
const [file, args, verbatim] = /\.(cmd|bat)$/i.test(path)
|
|
58
|
+
? [env.ComSpec ?? "cmd.exe", ["/d", "/s", "/c", `""${path}" --version"`], true]
|
|
59
|
+
: [path, ["--version"], false];
|
|
60
|
+
return new Promise((resolve) => execFile(file, args, { env, timeout: PROBE_TIMEOUT_MS, windowsHide: true, windowsVerbatimArguments: verbatim }, (error, stdout) => {
|
|
61
|
+
resolve(error ? null : /\d+\.\d+\.\d+\S*/.exec(stdout)?.[0] ?? null);
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
async function runningRuntimes(identity) {
|
|
65
|
+
const { bindings } = await readRuntimeSpaceBindings().catch(() => ({ bindings: [] }));
|
|
66
|
+
const spaces = new Map(bindings.filter((binding) => binding.key === identity).map((binding) => [binding.spaceId, binding.root]));
|
|
67
|
+
const statuses = await Promise.all([...spaces.keys()].slice(-256).map((spaceId) => controlRuntimeInstance(runtimeInstanceDirectory(identity, spaceId), "status", {}, STATUS_TIMEOUT_MS).catch(() => null)));
|
|
68
|
+
return statuses.flatMap((response) => response
|
|
69
|
+
? [{ spaceId: response.status.spaceId, root: response.status.root, version: response.status.version ?? null }]
|
|
70
|
+
: []);
|
|
71
|
+
}
|
package/dist/board-export.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BoardDocument, BoardExportRegion, BoardMediaState, BoardSceneItem, BoardThemeId } from "@neta-art/cohub/board";
|
|
1
|
+
import type { BoardDocument, BoardExportRegion, BoardMediaState, BoardSceneItem, BoardScriptSnapshot, BoardThemeId } from "@neta-art/cohub/board";
|
|
2
2
|
import type { BoardNodeExportFormat, BoardNodeFont, BoardNodeTexture } from "@neta-art/cohub/board/export/node";
|
|
3
3
|
import type { BoardScriptHost } from "@neta-art/cohub/board/render";
|
|
4
4
|
import { type BoardGpuResources } from "./board-gpu/index.js";
|
|
@@ -60,9 +60,11 @@ export type BoardExportRunOptions = {
|
|
|
60
60
|
};
|
|
61
61
|
document?: BoardDocument;
|
|
62
62
|
scripts?: BoardScriptHost;
|
|
63
|
+
snapshots?: Record<string, BoardScriptSnapshot>;
|
|
63
64
|
renderer?: BoardExportRenderer;
|
|
64
65
|
signal?: AbortSignal;
|
|
65
66
|
imageMemoryMiB?: number;
|
|
67
|
+
timeoutMs?: number;
|
|
66
68
|
onPlan?: (plan: {
|
|
67
69
|
width: number;
|
|
68
70
|
height: number;
|
|
@@ -104,4 +106,5 @@ export declare function runBoardVideoExport(options: BoardVideoExportOptions): P
|
|
|
104
106
|
}>;
|
|
105
107
|
export declare function runBoardExport(options: BoardExportRunOptions): Promise<BoardExportRunResult | null>;
|
|
106
108
|
export declare function gpuResources(spaceId: string, document: BoardDocument, framePicture?: (key: string) => Uint8Array | undefined): BoardGpuResources;
|
|
109
|
+
export declare const BOARD_EXPORT_FRAME_TIMEOUT_MS = 120000;
|
|
107
110
|
export {};
|
package/dist/board-export.js
CHANGED
|
@@ -9,11 +9,12 @@ import { dirname, join } from "node:path";
|
|
|
9
9
|
import { fileURLToPath } from "node:url";
|
|
10
10
|
import { tmpdir } from "node:os";
|
|
11
11
|
const execFile = promisify(execFileCallback);
|
|
12
|
-
import { boardMediaNaturalSize, parseBoardMediaSource } from "@neta-art/cohub/board";
|
|
12
|
+
import { boardExportView, boardMediaNaturalSize, parseBoardMediaSource } from "@neta-art/cohub/board";
|
|
13
13
|
import { imageVariantUrl, MEDIA_VARIANT_MAX, mediaVariantSize, videoFrameUrl } from "@neta-art/cohub/media";
|
|
14
|
-
import { hasBoardScripts, boardAssetKey, boardAssetKeySource, boardMediaRate, boardMediaSpans, boardRenderTheme, boardTimedMedia, boardTextureEdge, boardTexturePixels, createBoardRenderSampler, buildBoardScene, createNodeBoardRenderer, createNodeBoardScriptHost, describeBoardExportWarning, exportBoardImageBytes, parseBoardDocument, planBoardExport, prepareBoardExport, selectBoardExportAssets, } from "./board-kernel.js";
|
|
14
|
+
import { hasBoardScripts, boardAnimatesScriptParams, boardAssetKey, boardAssetKeySource, boardAutoplayLoops, boardMediaRate, boardMediaSpans, boardRenderTheme, boardTimedMedia, boardTextureEdge, boardTexturePixels, createBoardRenderSampler, buildBoardScene, createNodeBoardRenderer, createNodeBoardScriptHost, describeBoardExportWarning, describeBoardScriptLanes, exportBoardImageBytes, parseBoardDocument, planBoardExport, prepareBoardExport, selectBoardExportAssets, } from "./board-kernel.js";
|
|
15
15
|
import { settleBoardDocument } from "./board-media.js";
|
|
16
|
-
import {
|
|
16
|
+
import { openBoardGpu } from "./board-gpu/daemon.js";
|
|
17
|
+
import { findBoardGpuBrowser } from "./board-gpu/index.js";
|
|
17
18
|
import { BOARD_EXPORT_FONTS } from "./board-fonts.js";
|
|
18
19
|
import { createClient } from "./client.js";
|
|
19
20
|
import { downloadPublicImage } from "./safe-remote-image.js";
|
|
@@ -242,13 +243,17 @@ export async function runBoardExport(options) {
|
|
|
242
243
|
throw new Error(`Animation ${options.animation} does not exist.`);
|
|
243
244
|
}
|
|
244
245
|
const times = options.times?.length ? options.times : [null];
|
|
246
|
+
const timeoutMs = options.timeoutMs ?? BOARD_EXPORT_FRAME_TIMEOUT_MS;
|
|
245
247
|
const imageMemoryBytes = (options.imageMemoryMiB ?? BOARD_EXPORT_IMAGE_MEMORY_MIB) * 1024 ** 2;
|
|
246
248
|
if (!Number.isSafeInteger(imageMemoryBytes) || imageMemoryBytes <= 0)
|
|
247
249
|
throw new Error("Image memory budget must be a positive integer.");
|
|
248
250
|
const nodeRenderer = await createNodeBoardRenderer({ fonts: resolveBundledFonts() });
|
|
249
251
|
const images = createBoardExportImageBudget(nodeRenderer, imageMemoryBytes);
|
|
250
252
|
const renderer = { ...nodeRenderer, decodeImage: images.decodeImage };
|
|
253
|
+
const STAGES = { prepare: "running the scripts", gpu: "drawing in the browser", images: "loading images", render: "drawing" };
|
|
254
|
+
let doing = "preparing";
|
|
251
255
|
const trace = (stage, completed, dimensions) => {
|
|
256
|
+
doing = STAGES[stage] ?? doing;
|
|
252
257
|
if (!debugExport.enabled || completed % 30 !== 0)
|
|
253
258
|
return;
|
|
254
259
|
debugExport("%j", {
|
|
@@ -268,6 +273,7 @@ export async function runBoardExport(options) {
|
|
|
268
273
|
const theme = boardRenderTheme(options.theme);
|
|
269
274
|
const sampler = createBoardRenderSampler(document, options.animation ?? null, theme);
|
|
270
275
|
const documentAt = (time) => time === null ? document : sampler.at(time).document;
|
|
276
|
+
const history = options.animation !== undefined && boardAnimatesScriptParams(document);
|
|
271
277
|
const plan = planBoardExport({
|
|
272
278
|
scene: buildBoardScene(documentAt(times[0] ?? null)),
|
|
273
279
|
region: options.region,
|
|
@@ -347,9 +353,10 @@ export async function runBoardExport(options) {
|
|
|
347
353
|
options.onProgress?.(completed, times.length);
|
|
348
354
|
};
|
|
349
355
|
const launchGpu = async (browser) => {
|
|
350
|
-
|
|
356
|
+
doing = "starting the browser";
|
|
357
|
+
const exporter = await openBoardGpu(gpuResources(options.spaceId, document, (key) => framePictures.get(key)), { browser, signal: options.signal });
|
|
351
358
|
try {
|
|
352
|
-
await exporter.init({ document, animation: options.animation ?? null, theme: options.theme });
|
|
359
|
+
await exporter.init({ document, animation: options.animation ?? null, theme: options.theme, ...(options.snapshots ? { snapshots: options.snapshots } : {}) });
|
|
353
360
|
}
|
|
354
361
|
catch (error) {
|
|
355
362
|
await exporter.close();
|
|
@@ -365,18 +372,24 @@ export async function runBoardExport(options) {
|
|
|
365
372
|
throw new Error("GPU export needs Chrome, Chromium or Edge. Install one, or set COHUB_CHROME to its path.");
|
|
366
373
|
gpu = await launchGpu(browser);
|
|
367
374
|
}
|
|
368
|
-
|
|
375
|
+
const renderFrame = async (time) => {
|
|
369
376
|
options.signal?.throwIfAborted();
|
|
370
377
|
const host = scriptHost;
|
|
371
378
|
host?.clearFrames();
|
|
372
379
|
const frameOptions = { sampler, camera: options.camera, ...(time === null ? {} : { at: { animation: options.animation, time } }), region, scale: options.scale, padding: times.length > 1 ? 0 : options.padding };
|
|
373
380
|
const initial = prepareBoardExport(document, frameOptions);
|
|
374
381
|
if (!initial)
|
|
375
|
-
|
|
382
|
+
return;
|
|
376
383
|
let prepared = initial;
|
|
377
384
|
trace("prepare", completed, initial.plan);
|
|
378
385
|
const prepareScripts = async (scripts, shaders) => {
|
|
379
|
-
await scripts.prepare(initial.scene.items, [time ?? 0], initial.plan.scale, initial.document.board, {
|
|
386
|
+
await scripts.prepare(initial.scene.items, [time ?? 0], initial.plan.scale, initial.document.board, {
|
|
387
|
+
shaders,
|
|
388
|
+
view: boardExportView(initial.plan),
|
|
389
|
+
...(history ? { history: (at) => documentAt(at) } : {}),
|
|
390
|
+
});
|
|
391
|
+
for (const [id, message] of scripts.emitFailures)
|
|
392
|
+
report(`${id}: ${message}`);
|
|
380
393
|
for (const id of [...Object.keys(initial.document.board.scripts ?? {}), ...initial.scene.items.map((item) => item.id)]) {
|
|
381
394
|
const error = scripts.error(id);
|
|
382
395
|
if (error)
|
|
@@ -387,7 +400,7 @@ export async function runBoardExport(options) {
|
|
|
387
400
|
if (host && !gpu) {
|
|
388
401
|
const scripted = await prepareScripts(host, !decided);
|
|
389
402
|
if (!scripted)
|
|
390
|
-
|
|
403
|
+
return;
|
|
391
404
|
prepared = scripted;
|
|
392
405
|
}
|
|
393
406
|
if (!decided) {
|
|
@@ -401,7 +414,7 @@ export async function runBoardExport(options) {
|
|
|
401
414
|
host.clearFrames();
|
|
402
415
|
const scripted = await prepareScripts(host, false);
|
|
403
416
|
if (!scripted)
|
|
404
|
-
|
|
417
|
+
return;
|
|
405
418
|
prepared = scripted;
|
|
406
419
|
}
|
|
407
420
|
}
|
|
@@ -414,7 +427,8 @@ export async function runBoardExport(options) {
|
|
|
414
427
|
framePictures.clear();
|
|
415
428
|
if (options.withImages) {
|
|
416
429
|
const planned = new Set(prepared.plan.items.map((item) => item.id));
|
|
417
|
-
|
|
430
|
+
const loops = time === null ? [] : boardAutoplayLoops(prepared.scene, time);
|
|
431
|
+
for (const state of [...timed, ...loops]) {
|
|
418
432
|
const item = prepared.scene.get(state.id);
|
|
419
433
|
const base = item && state.kind === "video" && state.shown && planned.has(state.id) ? boardExportAssetKey(item) : null;
|
|
420
434
|
if (!item || !base)
|
|
@@ -455,7 +469,7 @@ export async function runBoardExport(options) {
|
|
|
455
469
|
for (const warning of result.warnings)
|
|
456
470
|
report(options.withImages && warning.kind === "images-missing" ? describeBoardExportWarning(warning) : describeWarning(warning));
|
|
457
471
|
await emit({ time, bytes: result.bytes, width: result.width, height: result.height, scale: result.scale, itemCount: result.itemCount, format: options.format });
|
|
458
|
-
|
|
472
|
+
return;
|
|
459
473
|
}
|
|
460
474
|
trace("images", completed, prepared.plan);
|
|
461
475
|
if (options.withImages) {
|
|
@@ -488,7 +502,7 @@ export async function runBoardExport(options) {
|
|
|
488
502
|
quality: options.quality,
|
|
489
503
|
});
|
|
490
504
|
if (!result)
|
|
491
|
-
|
|
505
|
+
return;
|
|
492
506
|
for (const warning of result.warnings) {
|
|
493
507
|
if (warning.kind === "images-missing" && options.withImages)
|
|
494
508
|
continue;
|
|
@@ -506,6 +520,18 @@ export async function runBoardExport(options) {
|
|
|
506
520
|
format: result.format,
|
|
507
521
|
});
|
|
508
522
|
trace("rendered", completed, result.plan);
|
|
523
|
+
};
|
|
524
|
+
for (const time of times) {
|
|
525
|
+
let timer;
|
|
526
|
+
const stalled = new Promise((_resolve, reject) => {
|
|
527
|
+
timer = setTimeout(() => reject(new Error(describeStall(time, timeoutMs, doing, scriptHost?.status() ?? null))), timeoutMs);
|
|
528
|
+
});
|
|
529
|
+
try {
|
|
530
|
+
await Promise.race([renderFrame(time), stalled]);
|
|
531
|
+
}
|
|
532
|
+
finally {
|
|
533
|
+
clearTimeout(timer);
|
|
534
|
+
}
|
|
509
535
|
}
|
|
510
536
|
return completed ? { frames, warnings, media } : null;
|
|
511
537
|
}
|
|
@@ -544,6 +570,13 @@ export function gpuResources(spaceId, document, framePicture = () => undefined)
|
|
|
544
570
|
},
|
|
545
571
|
};
|
|
546
572
|
}
|
|
573
|
+
export const BOARD_EXPORT_FRAME_TIMEOUT_MS = 120_000;
|
|
574
|
+
function describeStall(time, timeoutMs, doing, lanes) {
|
|
575
|
+
const at = time === null ? "The export" : `The frame at ${time} ms`;
|
|
576
|
+
const busy = lanes ? describeBoardScriptLanes(lanes) : [];
|
|
577
|
+
const hint = doing === "drawing in the browser" ? " The browser runs the scripts and shaders there; a shader may be too heavy." : "";
|
|
578
|
+
return [`${at} did not finish in ${timeoutMs / 1000} s, while ${doing}.${hint}`, ...busy.map((line) => ` ${line}`)].join("\n");
|
|
579
|
+
}
|
|
547
580
|
function describeWarning(warning) {
|
|
548
581
|
if (warning.kind === "scale-clamped") {
|
|
549
582
|
return `Scale reduced from ${warning.requested}x to ${Number(warning.applied).toFixed(2)}x to stay within the size limit.`;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type BoardGpuExporter, type BoardGpuResources } from "./index.js";
|
|
2
|
+
export declare function boardGpuSocket(browser: string): string;
|
|
3
|
+
export declare function serveBoardGpu(browser: string): Promise<void>;
|
|
4
|
+
export declare function connectBoardGpu(resources: BoardGpuResources, options: {
|
|
5
|
+
browser: string;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
}): Promise<BoardGpuExporter | null>;
|
|
8
|
+
export declare function openBoardGpu(resources: BoardGpuResources, options: {
|
|
9
|
+
browser: string;
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
}): Promise<BoardGpuExporter>;
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { lstatSync, mkdirSync, statSync, unlinkSync } from "node:fs";
|
|
4
|
+
import { createConnection, createServer } from "node:net";
|
|
5
|
+
import { tmpdir, userInfo } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { launchBoardGpuExporter } from "./index.js";
|
|
9
|
+
import { CLI_VERSION } from "../version.js";
|
|
10
|
+
const IDLE_MS = 5 * 60_000;
|
|
11
|
+
const CONNECT_MS = 20_000;
|
|
12
|
+
const TURN_MS = 20_000;
|
|
13
|
+
function lines(socket, handle) {
|
|
14
|
+
let buffer = "";
|
|
15
|
+
socket.setEncoding("utf8");
|
|
16
|
+
socket.on("data", (chunk) => {
|
|
17
|
+
buffer += chunk;
|
|
18
|
+
for (let end = buffer.indexOf("\n"); end >= 0; end = buffer.indexOf("\n")) {
|
|
19
|
+
const line = buffer.slice(0, end);
|
|
20
|
+
buffer = buffer.slice(end + 1);
|
|
21
|
+
let message;
|
|
22
|
+
try {
|
|
23
|
+
message = JSON.parse(line);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
handle(message);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return (message) => socket.write(`${JSON.stringify(message)}\n`);
|
|
32
|
+
}
|
|
33
|
+
export function boardGpuSocket(browser) {
|
|
34
|
+
let page = 0;
|
|
35
|
+
try {
|
|
36
|
+
page = statSync(fileURLToPath(new URL("./page.js", import.meta.url))).mtimeMs;
|
|
37
|
+
}
|
|
38
|
+
catch { }
|
|
39
|
+
const { uid } = userInfo();
|
|
40
|
+
const key = createHash("sha256").update(`${CLI_VERSION}\0${browser}\0${page}\0${uid}`).digest("hex").slice(0, 16);
|
|
41
|
+
if (process.platform === "win32")
|
|
42
|
+
return join("\\\\.\\pipe", `cohub-board-gpu-${key}`);
|
|
43
|
+
const dir = join(process.env.XDG_RUNTIME_DIR || tmpdir(), `cohub-${uid}`);
|
|
44
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
45
|
+
const stat = lstatSync(dir);
|
|
46
|
+
if (!stat.isDirectory() || stat.uid !== uid || (stat.mode & 0o077) !== 0)
|
|
47
|
+
throw new Error(`${dir} is not private to this user.`);
|
|
48
|
+
return join(dir, `board-gpu-${key}.sock`);
|
|
49
|
+
}
|
|
50
|
+
export async function serveBoardGpu(browser) {
|
|
51
|
+
const path = boardGpuSocket(browser);
|
|
52
|
+
const queue = [];
|
|
53
|
+
let exporter = null;
|
|
54
|
+
let nextAsk = 0;
|
|
55
|
+
let idle = null;
|
|
56
|
+
const active = () => queue[0] ?? null;
|
|
57
|
+
const ask = (request) => new Promise((resolve, reject) => {
|
|
58
|
+
const caller = active();
|
|
59
|
+
if (!caller || caller.gone)
|
|
60
|
+
return reject(new Error("The caller left."));
|
|
61
|
+
const id = ++nextAsk;
|
|
62
|
+
caller.asks.set(id, resolve);
|
|
63
|
+
caller.send({ ask: id, request });
|
|
64
|
+
});
|
|
65
|
+
const resources = {
|
|
66
|
+
fonts: [],
|
|
67
|
+
async readModule(path) {
|
|
68
|
+
const answer = await ask({ kind: "module", path });
|
|
69
|
+
if (answer.error !== undefined || answer.text === undefined)
|
|
70
|
+
throw new Error(answer.error ?? `${path} is not available.`);
|
|
71
|
+
return answer.text;
|
|
72
|
+
},
|
|
73
|
+
async readPicture(request) {
|
|
74
|
+
const answer = await ask({ kind: "picture", request });
|
|
75
|
+
if (answer.error !== undefined)
|
|
76
|
+
throw new Error(answer.error);
|
|
77
|
+
return answer.bytes === undefined ? null : { bytes: Buffer.from(answer.bytes, "base64"), mimeType: answer.mimeType ?? "application/octet-stream" };
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
const { resolveBundledFonts } = await import("../board-export.js");
|
|
81
|
+
resources.fonts = resolveBundledFonts().flatMap(({ path, family }) => (family ? [{ path, family }] : []));
|
|
82
|
+
const browserFor = () => (exporter ??= launchBoardGpuExporter(resources, { browser }).catch((error) => {
|
|
83
|
+
exporter = null;
|
|
84
|
+
throw error;
|
|
85
|
+
}));
|
|
86
|
+
const reset = async () => {
|
|
87
|
+
const current = exporter;
|
|
88
|
+
exporter = null;
|
|
89
|
+
await current?.then((value) => value.close(), () => { });
|
|
90
|
+
};
|
|
91
|
+
const shutdown = async () => {
|
|
92
|
+
await reset();
|
|
93
|
+
server.close();
|
|
94
|
+
process.exit(0);
|
|
95
|
+
};
|
|
96
|
+
const schedule = () => {
|
|
97
|
+
if (idle)
|
|
98
|
+
clearTimeout(idle);
|
|
99
|
+
idle = queue.length ? null : setTimeout(() => void shutdown(), IDLE_MS);
|
|
100
|
+
};
|
|
101
|
+
const advance = () => {
|
|
102
|
+
active()?.send({ granted: true });
|
|
103
|
+
schedule();
|
|
104
|
+
};
|
|
105
|
+
const release = (caller) => {
|
|
106
|
+
if (!caller.gone || caller.running)
|
|
107
|
+
return;
|
|
108
|
+
const index = queue.indexOf(caller);
|
|
109
|
+
if (index < 0)
|
|
110
|
+
return;
|
|
111
|
+
queue.splice(index, 1);
|
|
112
|
+
if (index === 0)
|
|
113
|
+
advance();
|
|
114
|
+
};
|
|
115
|
+
const server = createServer((socket) => {
|
|
116
|
+
const caller = { send: () => { }, asks: new Map(), running: 0, gone: false };
|
|
117
|
+
caller.send = lines(socket, (message) => {
|
|
118
|
+
if ("answer" in message) {
|
|
119
|
+
caller.asks.get(message.answer)?.(message);
|
|
120
|
+
caller.asks.delete(message.answer);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (!("method" in message) || active() !== caller)
|
|
124
|
+
return;
|
|
125
|
+
caller.running++;
|
|
126
|
+
void browserFor()
|
|
127
|
+
.then((gpu) => call(gpu, message.method, message.params))
|
|
128
|
+
.then((result) => caller.send({ id: message.id, result }), async (error) => {
|
|
129
|
+
const text = error instanceof Error ? error.message : String(error);
|
|
130
|
+
if (/stopped responding|browser (stopped|closed)|did not start/i.test(text))
|
|
131
|
+
await reset();
|
|
132
|
+
caller.send({ id: message.id, error: text });
|
|
133
|
+
})
|
|
134
|
+
.finally(() => {
|
|
135
|
+
caller.running--;
|
|
136
|
+
release(caller);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
const leave = () => {
|
|
140
|
+
if (caller.gone)
|
|
141
|
+
return;
|
|
142
|
+
caller.gone = true;
|
|
143
|
+
for (const resolve of caller.asks.values())
|
|
144
|
+
resolve({ answer: -1, error: "The caller left." });
|
|
145
|
+
caller.asks.clear();
|
|
146
|
+
release(caller);
|
|
147
|
+
};
|
|
148
|
+
socket.on("close", leave);
|
|
149
|
+
socket.on("error", leave);
|
|
150
|
+
queue.push(caller);
|
|
151
|
+
if (queue.length === 1)
|
|
152
|
+
advance();
|
|
153
|
+
});
|
|
154
|
+
await new Promise((resolve, reject) => {
|
|
155
|
+
server.once("error", async (error) => {
|
|
156
|
+
if (error.code !== "EADDRINUSE")
|
|
157
|
+
return reject(error);
|
|
158
|
+
const alive = await new Promise((done) => {
|
|
159
|
+
const probe = createConnection(path, () => { probe.destroy(); done(true); });
|
|
160
|
+
probe.once("error", () => done(false));
|
|
161
|
+
});
|
|
162
|
+
if (alive)
|
|
163
|
+
return process.exit(0);
|
|
164
|
+
try {
|
|
165
|
+
unlinkSync(path);
|
|
166
|
+
}
|
|
167
|
+
catch { }
|
|
168
|
+
server.listen(path, resolve);
|
|
169
|
+
});
|
|
170
|
+
server.listen(path, resolve);
|
|
171
|
+
});
|
|
172
|
+
process.on("SIGTERM", () => void shutdown());
|
|
173
|
+
schedule();
|
|
174
|
+
}
|
|
175
|
+
function call(gpu, method, params) {
|
|
176
|
+
switch (method) {
|
|
177
|
+
case "init": return gpu.init(params);
|
|
178
|
+
case "compile": return gpu.compile(params);
|
|
179
|
+
case "profile": return gpu.profile(params);
|
|
180
|
+
case "render": return gpu.render(params).then(({ bytes, ...rest }) => ({ ...rest, data: Buffer.from(bytes).toString("base64") }));
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function startDaemon(browser) {
|
|
184
|
+
const entry = process.argv[1];
|
|
185
|
+
if (!entry)
|
|
186
|
+
throw new Error("The CLI entry point is unknown.");
|
|
187
|
+
spawn(process.execPath, [...process.execArgv, entry, "boards", "gpu-daemon", "--browser", browser], {
|
|
188
|
+
detached: true,
|
|
189
|
+
stdio: "ignore",
|
|
190
|
+
env: process.env,
|
|
191
|
+
}).unref();
|
|
192
|
+
}
|
|
193
|
+
function connect(path) {
|
|
194
|
+
return new Promise((resolve, reject) => {
|
|
195
|
+
const socket = createConnection(path, () => resolve(socket));
|
|
196
|
+
socket.once("error", reject);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
export async function connectBoardGpu(resources, options) {
|
|
200
|
+
if (process.env.COHUB_BOARD_GPU_DAEMON === "0")
|
|
201
|
+
return null;
|
|
202
|
+
const path = boardGpuSocket(options.browser);
|
|
203
|
+
let socket = await connect(path).catch(() => null);
|
|
204
|
+
if (!socket) {
|
|
205
|
+
try {
|
|
206
|
+
startDaemon(options.browser);
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
for (const started = Date.now(); !socket && Date.now() - started < CONNECT_MS;) {
|
|
212
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
213
|
+
socket = await connect(path).catch(() => null);
|
|
214
|
+
}
|
|
215
|
+
if (!socket)
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
const connected = socket;
|
|
219
|
+
const pending = new Map();
|
|
220
|
+
let next = 0;
|
|
221
|
+
let granted = () => { };
|
|
222
|
+
const turn = new Promise((resolve) => { granted = resolve; });
|
|
223
|
+
const fail = (error) => {
|
|
224
|
+
granted(false);
|
|
225
|
+
for (const waiting of pending.values())
|
|
226
|
+
waiting.reject(error);
|
|
227
|
+
pending.clear();
|
|
228
|
+
};
|
|
229
|
+
const send = lines(connected, (message) => {
|
|
230
|
+
if ("granted" in message)
|
|
231
|
+
return granted(true);
|
|
232
|
+
if ("ask" in message) {
|
|
233
|
+
void answer(message.request).then((reply) => send({ answer: message.ask, ...reply }));
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if ("id" in message && !("method" in message)) {
|
|
237
|
+
const waiting = pending.get(message.id);
|
|
238
|
+
pending.delete(message.id);
|
|
239
|
+
if (message.error !== undefined)
|
|
240
|
+
waiting?.reject(new Error(message.error));
|
|
241
|
+
else
|
|
242
|
+
waiting?.resolve(message.result);
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
connected.on("close", () => fail(new Error("The shared browser closed.")));
|
|
246
|
+
connected.on("error", (error) => fail(error));
|
|
247
|
+
options.signal?.addEventListener("abort", () => connected.destroy(), { once: true });
|
|
248
|
+
const timer = setTimeout(() => granted(false), TURN_MS);
|
|
249
|
+
const ours = await turn;
|
|
250
|
+
clearTimeout(timer);
|
|
251
|
+
if (!ours) {
|
|
252
|
+
connected.destroy();
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
async function answer(request) {
|
|
256
|
+
try {
|
|
257
|
+
if (request.kind === "module")
|
|
258
|
+
return { text: await resources.readModule(request.path) };
|
|
259
|
+
const picture = await resources.readPicture(request.request);
|
|
260
|
+
return picture ? { bytes: Buffer.from(picture.bytes).toString("base64"), mimeType: picture.mimeType } : { missing: true };
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
const request = (method, params) => {
|
|
267
|
+
if (connected.destroyed)
|
|
268
|
+
return Promise.reject(new Error("The shared browser closed."));
|
|
269
|
+
const id = ++next;
|
|
270
|
+
return new Promise((resolve, reject) => {
|
|
271
|
+
pending.set(id, { resolve: resolve, reject });
|
|
272
|
+
send({ id, method, params });
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
return {
|
|
276
|
+
browser: options.browser,
|
|
277
|
+
init: (init) => request("init", init),
|
|
278
|
+
compile: (shaders) => request("compile", shaders),
|
|
279
|
+
profile: (input) => request("profile", input),
|
|
280
|
+
async render(frame) {
|
|
281
|
+
const { data, ...rest } = await request("render", frame);
|
|
282
|
+
return { ...rest, bytes: new Uint8Array(Buffer.from(data, "base64")) };
|
|
283
|
+
},
|
|
284
|
+
async close() {
|
|
285
|
+
connected.end();
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
export async function openBoardGpu(resources, options) {
|
|
290
|
+
const shared = await connectBoardGpu(resources, options).catch(() => null);
|
|
291
|
+
if (shared)
|
|
292
|
+
return shared;
|
|
293
|
+
options.signal?.throwIfAborted();
|
|
294
|
+
return launchBoardGpuExporter(resources, options);
|
|
295
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { BoardGpuFrame, BoardGpuInit, BoardGpuResult, BoardGpuShader } from "./protocol.js";
|
|
2
|
-
export type { BoardGpuFrame, BoardGpuResult, BoardGpuShader } from "./protocol.js";
|
|
1
|
+
import type { BoardGpuFrame, BoardGpuInit, BoardGpuProfile, BoardGpuProfileInput, BoardGpuResult, BoardGpuShader } from "./protocol.js";
|
|
2
|
+
export type { BoardGpuFrame, BoardGpuProfile, BoardGpuProfileInput, BoardGpuResult, BoardGpuShader } from "./protocol.js";
|
|
3
3
|
export declare function findBoardGpuBrowser(env?: NodeJS.ProcessEnv): string | null;
|
|
4
4
|
export type BoardGpuResources = {
|
|
5
5
|
readModule(path: string): Promise<string>;
|
|
@@ -24,6 +24,7 @@ export type BoardGpuExporter = {
|
|
|
24
24
|
bytes: Uint8Array;
|
|
25
25
|
}>;
|
|
26
26
|
compile(shaders: BoardGpuShader[]): Promise<Record<string, string>>;
|
|
27
|
+
profile(input: BoardGpuProfileInput): Promise<BoardGpuProfile>;
|
|
27
28
|
close(): Promise<void>;
|
|
28
29
|
};
|
|
29
30
|
export declare function launchBoardGpuExporter(resources: BoardGpuResources, options: {
|
package/dist/board-gpu/index.js
CHANGED
|
@@ -174,6 +174,7 @@ export async function launchBoardGpuExporter(resources, options) {
|
|
|
174
174
|
await call(`cohubBoardGpu.init(${JSON.stringify(init)})`);
|
|
175
175
|
},
|
|
176
176
|
compile: (shaders) => call(`cohubBoardGpu.compile(${JSON.stringify(shaders)})`),
|
|
177
|
+
profile: (input) => call(`cohubBoardGpu.profile(${JSON.stringify(input)})`, Math.max(CALL_TIMEOUT_MS, input.budgetMs * 2 + 60_000)),
|
|
177
178
|
async render(frame) {
|
|
178
179
|
const { data, ...rest } = await call(`cohubBoardGpu.render(${JSON.stringify(frame)})`);
|
|
179
180
|
return { ...rest, bytes: new Uint8Array(Buffer.from(data, "base64")) };
|