@neta-art/cohub-cli 10.18.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,8 +1,9 @@
1
- import type { BoardDocument, BoardExportRegion, 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";
5
5
  import { createClient } from "./client.js";
6
+ import { type BoardMediaFiles } from "./board-media-export.js";
6
7
  import { type BoardExportRenderer, type BoardExportDecodeImage } from "./board-export-resources.js";
7
8
  export declare function resolveBundledFonts(): BoardNodeFont[];
8
9
  export type BoardExportSource = {
@@ -59,6 +60,7 @@ export type BoardExportRunOptions = {
59
60
  };
60
61
  document?: BoardDocument;
61
62
  scripts?: BoardScriptHost;
63
+ snapshots?: Record<string, BoardScriptSnapshot>;
62
64
  renderer?: BoardExportRenderer;
63
65
  signal?: AbortSignal;
64
66
  imageMemoryMiB?: number;
@@ -69,6 +71,7 @@ export type BoardExportRunOptions = {
69
71
  }) => void;
70
72
  onFrame?: (frame: BoardExportFrame, index: number) => Promise<void>;
71
73
  onProgress?: (completed: number, total: number) => void;
74
+ mediaFiles?: BoardMediaFiles;
72
75
  };
73
76
  export type BoardExportFrame = {
74
77
  time: number | null;
@@ -82,6 +85,10 @@ export type BoardExportFrame = {
82
85
  export type BoardExportRunResult = {
83
86
  frames: BoardExportFrame[];
84
87
  warnings: string[];
88
+ media: Array<{
89
+ at: number;
90
+ media: BoardMediaState[];
91
+ }>;
85
92
  };
86
93
  export type BoardVideoExportFormat = "mp4" | "webm";
87
94
  export type BoardVideoExportOptions = Omit<BoardExportRunOptions, "format"> & {
@@ -97,5 +104,5 @@ export declare function runBoardVideoExport(options: BoardVideoExportOptions): P
97
104
  warnings: string[];
98
105
  }>;
99
106
  export declare function runBoardExport(options: BoardExportRunOptions): Promise<BoardExportRunResult | null>;
100
- export declare function gpuResources(spaceId: string, document: BoardDocument): BoardGpuResources;
107
+ export declare function gpuResources(spaceId: string, document: BoardDocument, framePicture?: (key: string) => Uint8Array | undefined): BoardGpuResources;
101
108
  export {};
@@ -11,12 +11,14 @@ 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, boardDocumentAt, boardRenderTheme, 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
- import { downloadPublicAudio, downloadPublicImage } from "./safe-remote-image.js";
20
+ import { downloadPublicImage } from "./safe-remote-image.js";
21
+ import { boardAudioMix, createBoardMediaFiles, createBoardVideoFrames } from "./board-media-export.js";
20
22
  import { BOARD_EXPORT_IMAGE_MEMORY_MIB, BoardExportResourceError, createBoardExportImageBudget, } from "./board-export-resources.js";
21
23
  const debugExport = debuglog("cohub:board-export");
22
24
  export function resolveBundledFonts() {
@@ -177,12 +179,8 @@ export async function runBoardVideoExport(options) {
177
179
  try {
178
180
  let count = 0;
179
181
  let dimensions = { width: 0, height: 0 };
180
- const result = await runBoardExport({ ...options, document, format: "png", onPlan: (plan) => {
181
- if (options.format === "mp4" && (plan.width % 2 || plan.height % 2)) {
182
- throw new Error(`MP4 needs even dimensions, but the export is ${plan.width}x${plan.height}. Use --camera --size with even dimensions, or adjust the export region.`);
183
- }
184
- options.onPlan?.(plan);
185
- }, onFrame: async (frame, index) => {
182
+ const mediaFiles = createBoardMediaFiles(options.spaceId, tempDir, options.signal);
183
+ const result = await runBoardExport({ ...options, document, format: "png", mediaFiles, onFrame: async (frame, index) => {
186
184
  if (count && (frame.width !== dimensions.width || frame.height !== dimensions.height))
187
185
  throw new Error("Video frame dimensions changed. Use a fixed export region or a smaller camera viewport.");
188
186
  await writeFile(join(tempDir, `${String(index + 1).padStart(6, "0")}.png`), frame.bytes, { signal: options.signal });
@@ -191,36 +189,18 @@ export async function runBoardVideoExport(options) {
191
189
  } });
192
190
  if (!result || !count)
193
191
  throw new Error("Nothing to export: the region contains no items.");
194
- const audioDocument = boardDocumentAt(document, options.animation, times[0]).document;
195
- const audio = Object.values(audioDocument.items).filter((item) => item.type === "audio");
196
- const audioInputs = [];
197
- for (const [index, item] of audio.entries()) {
198
- const source = parseBoardMediaSource(item.props.src);
199
- if (!source)
200
- throw new Error("Invalid board audio source");
201
- const { bytes } = source.kind === "url"
202
- ? await downloadPublicAudio(source.value)
203
- : await readSpaceFileBytes(createClient(), options.spaceId, source.value);
204
- const path = join(tempDir, `audio-${index}`);
205
- await writeFile(path, bytes);
206
- audioInputs.push({ path, offset: Math.max(0, item.props.time ?? 0) / 1000 });
207
- }
192
+ const start = times[0];
193
+ const sampled = boardMediaSpans(result.media.map((sample) => ({ ...sample, at: sample.at - start })));
194
+ const spans = sampled.map((span) => sampled.some((next) => next.id === span.id && next.start === span.end) ? span : { ...span, end: span.end + step });
195
+ const mix = await boardAudioMix(spans, mediaFiles, 1);
208
196
  const duration = Math.max(0.001, count / options.fps);
209
- const args = ["-y", "-f", "image2", "-framerate", String(options.fps), "-i", join(tempDir, "%06d.png")];
210
- for (const input of audioInputs) {
211
- if (input.offset > 0)
212
- args.push("-ss", input.offset.toFixed(3));
213
- args.push("-i", input.path);
214
- }
215
- if (audioInputs.length > 1) {
216
- const inputs = audioInputs.map((_, index) => `[${index + 1}:a]`).join("");
217
- args.push("-filter_complex", `${inputs}amix=inputs=${audioInputs.length}:duration=longest:dropout_transition=0[aout]`, "-map", "0:v:0", "-map", "[aout]");
218
- }
219
- else {
197
+ const args = ["-y", "-f", "image2", "-framerate", String(options.fps), "-i", join(tempDir, "%06d.png"), ...(mix?.inputs ?? [])];
198
+ if (mix)
199
+ args.push("-filter_complex", mix.filter, "-map", "0:v:0", "-map", "[aout]");
200
+ else
220
201
  args.push("-map", "0:v:0");
221
- if (audioInputs.length === 1)
222
- args.push("-map", "1:a:0");
223
- }
202
+ if (options.format === "mp4" && (dimensions.width % 2 || dimensions.height % 2))
203
+ args.push("-vf", "pad=ceil(iw/2)*2:ceil(ih/2)*2");
224
204
  if (options.format === "mp4")
225
205
  args.push("-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-movflags", "+faststart", "-f", "mp4");
226
206
  else
@@ -282,11 +262,14 @@ export async function runBoardExport(options) {
282
262
  let scriptHost = null;
283
263
  let gpu = null;
284
264
  const textureCache = { textures: new Map(), edges: new Map(), missed: new Map() };
265
+ const frameTextures = new Map();
266
+ let mediaDir = null;
285
267
  let backgroundTexture;
286
268
  try {
287
269
  const theme = boardRenderTheme(options.theme);
288
270
  const sampler = createBoardRenderSampler(document, options.animation ?? null, theme);
289
271
  const documentAt = (time) => time === null ? document : sampler.at(time).document;
272
+ const history = options.animation !== undefined && boardAnimatesScriptParams(document);
290
273
  const plan = planBoardExport({
291
274
  scene: buildBoardScene(documentAt(times[0] ?? null)),
292
275
  region: options.region,
@@ -336,6 +319,18 @@ export async function runBoardExport(options) {
336
319
  return key && omittedKeys.has(key) ? null : key;
337
320
  };
338
321
  const frames = [];
322
+ const media = [];
323
+ const fps = times.length > 1 && times[0] !== null && times[1] !== null ? 1000 / (times[1] - times[0]) : 30;
324
+ const lastMedia = new Map();
325
+ const framePictures = new Map();
326
+ let videoFrames = null;
327
+ const frameOf = async (state, rate, edge) => {
328
+ if (!videoFrames) {
329
+ mediaDir = await mkdtemp(join(tmpdir(), "cohub-board-media-"));
330
+ videoFrames = createBoardVideoFrames(options.mediaFiles ?? createBoardMediaFiles(options.spaceId, mediaDir, options.signal), mediaDir, { fps, signal: options.signal });
331
+ }
332
+ return videoFrames.frame(state, rate, edge);
333
+ };
339
334
  const reported = new Set();
340
335
  const report = (text) => {
341
336
  if (reported.has(text))
@@ -354,9 +349,9 @@ export async function runBoardExport(options) {
354
349
  options.onProgress?.(completed, times.length);
355
350
  };
356
351
  const launchGpu = async (browser) => {
357
- const exporter = await launchBoardGpuExporter(gpuResources(options.spaceId, document), { browser, signal: options.signal });
352
+ const exporter = await openBoardGpu(gpuResources(options.spaceId, document, (key) => framePictures.get(key)), { browser, signal: options.signal });
358
353
  try {
359
- 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 } : {}) });
360
355
  }
361
356
  catch (error) {
362
357
  await exporter.close();
@@ -383,7 +378,10 @@ export async function runBoardExport(options) {
383
378
  let prepared = initial;
384
379
  trace("prepare", completed, initial.plan);
385
380
  const prepareScripts = async (scripts, shaders) => {
386
- 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
+ });
387
385
  for (const id of [...Object.keys(initial.document.board.scripts ?? {}), ...initial.scene.items.map((item) => item.id)]) {
388
386
  const error = scripts.error(id);
389
387
  if (error)
@@ -412,6 +410,38 @@ export async function runBoardExport(options) {
412
410
  prepared = scripted;
413
411
  }
414
412
  }
413
+ const timed = boardTimedMedia(prepared.scene);
414
+ media.push({ at: time ?? 0, media: timed });
415
+ const frameKeys = {};
416
+ for (const texture of frameTextures.values())
417
+ texture.destroy(true);
418
+ frameTextures.clear();
419
+ framePictures.clear();
420
+ if (options.withImages) {
421
+ const planned = new Set(prepared.plan.items.map((item) => item.id));
422
+ for (const state of timed) {
423
+ const item = prepared.scene.get(state.id);
424
+ const base = item && state.kind === "video" && state.shown && planned.has(state.id) ? boardExportAssetKey(item) : null;
425
+ if (!item || !base)
426
+ continue;
427
+ const before = lastMedia.get(state.id);
428
+ if (time !== null)
429
+ lastMedia.set(state.id, { at: time, time: state.time });
430
+ const rate = times.length === 1 ? 0 : before && time !== null ? boardMediaRate(before, { at: time, time: state.time }) : 1;
431
+ const edge = Math.min(4096, textureEdge(item, prepared.plan.scale));
432
+ const bytes = await frameOf(state, rate, edge).catch((error) => {
433
+ report(`Frames of ${state.src} could not be decoded; its cover was drawn instead. ${error instanceof Error ? error.message : ""}`.trim());
434
+ return null;
435
+ });
436
+ if (!bytes?.length)
437
+ continue;
438
+ const key = `${base}@${Math.round(state.time)}`;
439
+ frameKeys[state.id] = key;
440
+ framePictures.set(key, bytes);
441
+ if (!gpu)
442
+ frameTextures.set(key, await nodeRenderer.decodeImage(bytes, "image/jpeg"));
443
+ }
444
+ }
415
445
  if (gpu) {
416
446
  trace("gpu", completed, prepared.plan);
417
447
  const result = await gpu.render({
@@ -425,6 +455,7 @@ export async function runBoardExport(options) {
425
455
  ...(options.quality === undefined ? {} : { quality: options.quality }),
426
456
  withImages: options.withImages,
427
457
  imageMemory: imageMemoryBytes,
458
+ assetKeys: frameKeys,
428
459
  });
429
460
  for (const warning of result.warnings)
430
461
  report(options.withImages && warning.kind === "images-missing" ? describeBoardExportWarning(warning) : describeWarning(warning));
@@ -433,7 +464,7 @@ export async function runBoardExport(options) {
433
464
  }
434
465
  trace("images", completed, prepared.plan);
435
466
  if (options.withImages) {
436
- const visible = prepared.plan.items.filter((item) => prepared.scene.opacity(item.id) > 0);
467
+ const visible = prepared.plan.items.filter((item) => prepared.scene.opacity(item.id) > 0 && !frameKeys[item.id]);
437
468
  const loaded = await loadBoardTextures(images, options.spaceId, visible, { scale: prepared.plan.scale, maxBytes: imageMemoryBytes, cache: textureCache });
438
469
  omittedKeys = new Set(loaded.omitted);
439
470
  const sources = (keys) => keys.slice(0, 3).map((key) => boardAssetKeySource(key)?.value ?? key).join(", ");
@@ -454,10 +485,10 @@ export async function runBoardExport(options) {
454
485
  ...frameOptions,
455
486
  ...theme,
456
487
  background: options.background,
457
- textures,
488
+ textures: frameTextures.size ? new Map([...textures, ...frameTextures]) : textures,
458
489
  scripts: options.scripts ?? scriptHost ?? undefined,
459
490
  backgroundTexture,
460
- assetKey,
491
+ assetKey: (item) => frameKeys[item.id] ?? assetKey(item),
461
492
  format: options.format,
462
493
  quality: options.quality,
463
494
  });
@@ -481,7 +512,7 @@ export async function runBoardExport(options) {
481
512
  });
482
513
  trace("rendered", completed, result.plan);
483
514
  }
484
- return completed ? { frames, warnings } : null;
515
+ return completed ? { frames, warnings, media } : null;
485
516
  }
486
517
  finally {
487
518
  await gpu?.close();
@@ -489,10 +520,14 @@ export async function runBoardExport(options) {
489
520
  for (const texture of textureCache.textures.values())
490
521
  texture.destroy(true);
491
522
  backgroundTexture?.destroy(true);
523
+ for (const texture of frameTextures.values())
524
+ texture.destroy(true);
525
+ if (mediaDir)
526
+ await rm(mediaDir, { recursive: true, force: true });
492
527
  renderer.destroy();
493
528
  }
494
529
  }
495
- export function gpuResources(spaceId, document) {
530
+ export function gpuResources(spaceId, document, framePicture = () => undefined) {
496
531
  const client = createClient();
497
532
  const scene = buildBoardScene(document);
498
533
  return {
@@ -505,6 +540,9 @@ export function gpuResources(spaceId, document) {
505
540
  return null;
506
541
  return source.kind === "url" ? downloadPublicImage(source.value) : readSpaceFileBytes(client, spaceId, source.value);
507
542
  }
543
+ const frame = framePicture(request.key);
544
+ if (frame)
545
+ return { bytes: frame, mimeType: "image/jpeg" };
508
546
  const source = boardAssetKeySource(request.key);
509
547
  const item = scene.items.find((entry) => boardAssetKey(entry) === request.key);
510
548
  return source && item ? fetchBoardPicture(client, spaceId, source, item, request.edge) : null;
@@ -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")) };