@neta-art/cohub-cli 10.19.0 → 10.20.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.
@@ -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,6 +60,7 @@ 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;
@@ -11,9 +11,10 @@ import { tmpdir } from "node:os";
11
11
  const execFile = promisify(execFileCallback);
12
12
  import { 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, boardMediaRate, boardMediaSpans, boardRenderTheme, boardTimedMedia, boardTextureEdge, boardTexturePixels, createBoardRenderSampler, buildBoardScene, createNodeBoardRenderer, createNodeBoardScriptHost, describeBoardExportWarning, exportBoardImageBytes, parseBoardDocument, planBoardExport, prepareBoardExport, selectBoardExportAssets, } from "./board-kernel.js";
15
15
  import { settleBoardDocument } from "./board-media.js";
16
- import { findBoardGpuBrowser, launchBoardGpuExporter } from "./board-gpu/index.js";
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";
@@ -268,6 +269,7 @@ export async function runBoardExport(options) {
268
269
  const theme = boardRenderTheme(options.theme);
269
270
  const sampler = createBoardRenderSampler(document, options.animation ?? null, theme);
270
271
  const documentAt = (time) => time === null ? document : sampler.at(time).document;
272
+ const history = options.animation !== undefined && boardAnimatesScriptParams(document);
271
273
  const plan = planBoardExport({
272
274
  scene: buildBoardScene(documentAt(times[0] ?? null)),
273
275
  region: options.region,
@@ -347,9 +349,9 @@ export async function runBoardExport(options) {
347
349
  options.onProgress?.(completed, times.length);
348
350
  };
349
351
  const launchGpu = async (browser) => {
350
- const exporter = await launchBoardGpuExporter(gpuResources(options.spaceId, document, (key) => framePictures.get(key)), { browser, signal: options.signal });
352
+ const exporter = await openBoardGpu(gpuResources(options.spaceId, document, (key) => framePictures.get(key)), { browser, signal: options.signal });
351
353
  try {
352
- await exporter.init({ document, animation: options.animation ?? null, theme: options.theme });
354
+ await exporter.init({ document, animation: options.animation ?? null, theme: options.theme, ...(options.snapshots ? { snapshots: options.snapshots } : {}) });
353
355
  }
354
356
  catch (error) {
355
357
  await exporter.close();
@@ -376,7 +378,10 @@ export async function runBoardExport(options) {
376
378
  let prepared = initial;
377
379
  trace("prepare", completed, initial.plan);
378
380
  const prepareScripts = async (scripts, shaders) => {
379
- await scripts.prepare(initial.scene.items, [time ?? 0], initial.plan.scale, initial.document.board, { shaders });
381
+ await scripts.prepare(initial.scene.items, [time ?? 0], initial.plan.scale, initial.document.board, {
382
+ shaders,
383
+ ...(history ? { history: (at) => documentAt(at) } : {}),
384
+ });
380
385
  for (const id of [...Object.keys(initial.document.board.scripts ?? {}), ...initial.scene.items.map((item) => item.id)]) {
381
386
  const error = scripts.error(id);
382
387
  if (error)
@@ -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,302 @@
1
+ import { spawn } from "node:child_process";
2
+ import { createHash } from "node:crypto";
3
+ import { lstatSync, mkdirSync, readFileSync, 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
+ const IDLE_MS = 5 * 60_000;
10
+ const CONNECT_MS = 20_000;
11
+ const TURN_MS = 20_000;
12
+ function lines(socket, handle) {
13
+ let buffer = "";
14
+ socket.setEncoding("utf8");
15
+ socket.on("data", (chunk) => {
16
+ buffer += chunk;
17
+ for (let end = buffer.indexOf("\n"); end >= 0; end = buffer.indexOf("\n")) {
18
+ const line = buffer.slice(0, end);
19
+ buffer = buffer.slice(end + 1);
20
+ let message;
21
+ try {
22
+ message = JSON.parse(line);
23
+ }
24
+ catch {
25
+ continue;
26
+ }
27
+ handle(message);
28
+ }
29
+ });
30
+ return (message) => socket.write(`${JSON.stringify(message)}\n`);
31
+ }
32
+ function version() {
33
+ try {
34
+ return JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version;
35
+ }
36
+ catch {
37
+ return "dev";
38
+ }
39
+ }
40
+ export function boardGpuSocket(browser) {
41
+ let page = 0;
42
+ try {
43
+ page = statSync(fileURLToPath(new URL("./page.js", import.meta.url))).mtimeMs;
44
+ }
45
+ catch { }
46
+ const { uid } = userInfo();
47
+ const key = createHash("sha256").update(`${version()}\0${browser}\0${page}\0${uid}`).digest("hex").slice(0, 16);
48
+ if (process.platform === "win32")
49
+ return join("\\\\.\\pipe", `cohub-board-gpu-${key}`);
50
+ const dir = join(process.env.XDG_RUNTIME_DIR || tmpdir(), `cohub-${uid}`);
51
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
52
+ const stat = lstatSync(dir);
53
+ if (!stat.isDirectory() || stat.uid !== uid || (stat.mode & 0o077) !== 0)
54
+ throw new Error(`${dir} is not private to this user.`);
55
+ return join(dir, `board-gpu-${key}.sock`);
56
+ }
57
+ export async function serveBoardGpu(browser) {
58
+ const path = boardGpuSocket(browser);
59
+ const queue = [];
60
+ let exporter = null;
61
+ let nextAsk = 0;
62
+ let idle = null;
63
+ const active = () => queue[0] ?? null;
64
+ const ask = (request) => new Promise((resolve, reject) => {
65
+ const caller = active();
66
+ if (!caller || caller.gone)
67
+ return reject(new Error("The caller left."));
68
+ const id = ++nextAsk;
69
+ caller.asks.set(id, resolve);
70
+ caller.send({ ask: id, request });
71
+ });
72
+ const resources = {
73
+ fonts: [],
74
+ async readModule(path) {
75
+ const answer = await ask({ kind: "module", path });
76
+ if (answer.error !== undefined || answer.text === undefined)
77
+ throw new Error(answer.error ?? `${path} is not available.`);
78
+ return answer.text;
79
+ },
80
+ async readPicture(request) {
81
+ const answer = await ask({ kind: "picture", request });
82
+ if (answer.error !== undefined)
83
+ throw new Error(answer.error);
84
+ return answer.bytes === undefined ? null : { bytes: Buffer.from(answer.bytes, "base64"), mimeType: answer.mimeType ?? "application/octet-stream" };
85
+ },
86
+ };
87
+ const { resolveBundledFonts } = await import("../board-export.js");
88
+ resources.fonts = resolveBundledFonts().flatMap(({ path, family }) => (family ? [{ path, family }] : []));
89
+ const browserFor = () => (exporter ??= launchBoardGpuExporter(resources, { browser }).catch((error) => {
90
+ exporter = null;
91
+ throw error;
92
+ }));
93
+ const reset = async () => {
94
+ const current = exporter;
95
+ exporter = null;
96
+ await current?.then((value) => value.close(), () => { });
97
+ };
98
+ const shutdown = async () => {
99
+ await reset();
100
+ server.close();
101
+ process.exit(0);
102
+ };
103
+ const schedule = () => {
104
+ if (idle)
105
+ clearTimeout(idle);
106
+ idle = queue.length ? null : setTimeout(() => void shutdown(), IDLE_MS);
107
+ };
108
+ const advance = () => {
109
+ active()?.send({ granted: true });
110
+ schedule();
111
+ };
112
+ const release = (caller) => {
113
+ if (!caller.gone || caller.running)
114
+ return;
115
+ const index = queue.indexOf(caller);
116
+ if (index < 0)
117
+ return;
118
+ queue.splice(index, 1);
119
+ if (index === 0)
120
+ advance();
121
+ };
122
+ const server = createServer((socket) => {
123
+ const caller = { send: () => { }, asks: new Map(), running: 0, gone: false };
124
+ caller.send = lines(socket, (message) => {
125
+ if ("answer" in message) {
126
+ caller.asks.get(message.answer)?.(message);
127
+ caller.asks.delete(message.answer);
128
+ return;
129
+ }
130
+ if (!("method" in message) || active() !== caller)
131
+ return;
132
+ caller.running++;
133
+ void browserFor()
134
+ .then((gpu) => call(gpu, message.method, message.params))
135
+ .then((result) => caller.send({ id: message.id, result }), async (error) => {
136
+ const text = error instanceof Error ? error.message : String(error);
137
+ if (/stopped responding|browser (stopped|closed)|did not start/i.test(text))
138
+ await reset();
139
+ caller.send({ id: message.id, error: text });
140
+ })
141
+ .finally(() => {
142
+ caller.running--;
143
+ release(caller);
144
+ });
145
+ });
146
+ const leave = () => {
147
+ if (caller.gone)
148
+ return;
149
+ caller.gone = true;
150
+ for (const resolve of caller.asks.values())
151
+ resolve({ answer: -1, error: "The caller left." });
152
+ caller.asks.clear();
153
+ release(caller);
154
+ };
155
+ socket.on("close", leave);
156
+ socket.on("error", leave);
157
+ queue.push(caller);
158
+ if (queue.length === 1)
159
+ advance();
160
+ });
161
+ await new Promise((resolve, reject) => {
162
+ server.once("error", async (error) => {
163
+ if (error.code !== "EADDRINUSE")
164
+ return reject(error);
165
+ const alive = await new Promise((done) => {
166
+ const probe = createConnection(path, () => { probe.destroy(); done(true); });
167
+ probe.once("error", () => done(false));
168
+ });
169
+ if (alive)
170
+ return process.exit(0);
171
+ try {
172
+ unlinkSync(path);
173
+ }
174
+ catch { }
175
+ server.listen(path, resolve);
176
+ });
177
+ server.listen(path, resolve);
178
+ });
179
+ process.on("SIGTERM", () => void shutdown());
180
+ schedule();
181
+ }
182
+ function call(gpu, method, params) {
183
+ switch (method) {
184
+ case "init": return gpu.init(params);
185
+ case "compile": return gpu.compile(params);
186
+ case "profile": return gpu.profile(params);
187
+ case "render": return gpu.render(params).then(({ bytes, ...rest }) => ({ ...rest, data: Buffer.from(bytes).toString("base64") }));
188
+ }
189
+ }
190
+ function startDaemon(browser) {
191
+ const entry = process.argv[1];
192
+ if (!entry)
193
+ throw new Error("The CLI entry point is unknown.");
194
+ spawn(process.execPath, [...process.execArgv, entry, "boards", "gpu-daemon", "--browser", browser], {
195
+ detached: true,
196
+ stdio: "ignore",
197
+ env: process.env,
198
+ }).unref();
199
+ }
200
+ function connect(path) {
201
+ return new Promise((resolve, reject) => {
202
+ const socket = createConnection(path, () => resolve(socket));
203
+ socket.once("error", reject);
204
+ });
205
+ }
206
+ export async function connectBoardGpu(resources, options) {
207
+ if (process.env.COHUB_BOARD_GPU_DAEMON === "0")
208
+ return null;
209
+ const path = boardGpuSocket(options.browser);
210
+ let socket = await connect(path).catch(() => null);
211
+ if (!socket) {
212
+ try {
213
+ startDaemon(options.browser);
214
+ }
215
+ catch {
216
+ return null;
217
+ }
218
+ for (const started = Date.now(); !socket && Date.now() - started < CONNECT_MS;) {
219
+ await new Promise((resolve) => setTimeout(resolve, 100));
220
+ socket = await connect(path).catch(() => null);
221
+ }
222
+ if (!socket)
223
+ return null;
224
+ }
225
+ const connected = socket;
226
+ const pending = new Map();
227
+ let next = 0;
228
+ let granted = () => { };
229
+ const turn = new Promise((resolve) => { granted = resolve; });
230
+ const fail = (error) => {
231
+ granted(false);
232
+ for (const waiting of pending.values())
233
+ waiting.reject(error);
234
+ pending.clear();
235
+ };
236
+ const send = lines(connected, (message) => {
237
+ if ("granted" in message)
238
+ return granted(true);
239
+ if ("ask" in message) {
240
+ void answer(message.request).then((reply) => send({ answer: message.ask, ...reply }));
241
+ return;
242
+ }
243
+ if ("id" in message && !("method" in message)) {
244
+ const waiting = pending.get(message.id);
245
+ pending.delete(message.id);
246
+ if (message.error !== undefined)
247
+ waiting?.reject(new Error(message.error));
248
+ else
249
+ waiting?.resolve(message.result);
250
+ }
251
+ });
252
+ connected.on("close", () => fail(new Error("The shared browser closed.")));
253
+ connected.on("error", (error) => fail(error));
254
+ options.signal?.addEventListener("abort", () => connected.destroy(), { once: true });
255
+ const timer = setTimeout(() => granted(false), TURN_MS);
256
+ const ours = await turn;
257
+ clearTimeout(timer);
258
+ if (!ours) {
259
+ connected.destroy();
260
+ return null;
261
+ }
262
+ async function answer(request) {
263
+ try {
264
+ if (request.kind === "module")
265
+ return { text: await resources.readModule(request.path) };
266
+ const picture = await resources.readPicture(request.request);
267
+ return picture ? { bytes: Buffer.from(picture.bytes).toString("base64"), mimeType: picture.mimeType } : { missing: true };
268
+ }
269
+ catch (error) {
270
+ return { error: error instanceof Error ? error.message : String(error) };
271
+ }
272
+ }
273
+ const request = (method, params) => {
274
+ if (connected.destroyed)
275
+ return Promise.reject(new Error("The shared browser closed."));
276
+ const id = ++next;
277
+ return new Promise((resolve, reject) => {
278
+ pending.set(id, { resolve: resolve, reject });
279
+ send({ id, method, params });
280
+ });
281
+ };
282
+ return {
283
+ browser: options.browser,
284
+ init: (init) => request("init", init),
285
+ compile: (shaders) => request("compile", shaders),
286
+ profile: (input) => request("profile", input),
287
+ async render(frame) {
288
+ const { data, ...rest } = await request("render", frame);
289
+ return { ...rest, bytes: new Uint8Array(Buffer.from(data, "base64")) };
290
+ },
291
+ async close() {
292
+ connected.end();
293
+ },
294
+ };
295
+ }
296
+ export async function openBoardGpu(resources, options) {
297
+ const shared = await connectBoardGpu(resources, options).catch(() => null);
298
+ if (shared)
299
+ return shared;
300
+ options.signal?.throwIfAborted();
301
+ return launchBoardGpuExporter(resources, options);
302
+ }
@@ -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: {
@@ -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)})`),
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")) };