@holtjs/stage 0.13.0 → 0.15.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,15 +1,19 @@
1
- import { $t as OpenWgpuNativeFfiOptions, E as StagePlugin, Qt as OpenDawnWebGpuOptions, Tt as SceneDigest, Xt as HeadlessWebGpuHandle, Yt as HeadlessWebGpuBackend, Zt as HeadlessWebGpuOptions, en as openDawnWebGpu, f as SceneInput, kt as SceneDigestOptions, n as EngineClockSnapshot, nn as requestHeadlessWebGpu, qt as WebGpuDeviceLike, r as Game, tn as openWgpuNativeFfi, u as Scene } from "./game-C8bz7tay.js";
1
+ import { $t as HeadlessWebGpuOptions, Dt as SceneDigest, E as StagePlugin, Qt as HeadlessWebGpuHandle, Yt as WebGpuDeviceLike, Zt as HeadlessWebGpuBackend, en as OpenDawnWebGpuOptions, f as SceneInput, in as requestHeadlessWebGpu, jt as SceneDigestOptions, n as EngineClockSnapshot, nn as openDawnWebGpu, r as Game, rn as openWgpuNativeFfi, tn as OpenWgpuNativeFfiOptions, u as Scene } from "./game-Ce35GTH4.js";
2
2
  /** Minimal Canvas2D surface compatible with `@napi-rs/canvas`, `node-canvas`, and browser `<canvas>`. */
3
3
  export interface HeadlessCanvasLike {
4
4
  width: number;
5
5
  height: number;
6
6
  getContext(contextId: '2d', options?: unknown): CanvasRenderingContext2D | null;
7
- toBuffer?(mimeType: 'image/png'): Uint8Array;
7
+ toBuffer?(mimeType: string, quality?: number): Uint8Array;
8
8
  }
9
9
  export type CreateHeadlessCanvas = (width: number, height: number) => HeadlessCanvasLike;
10
+ export type HeadlessStageBackend = HeadlessWebGpuBackend | 'webgpu' | 'webgl' | 'canvas2d';
10
11
  export interface HeadlessStageOptions {
11
- /** Canvas factory such as `createCanvas` from `@napi-rs/canvas`. */
12
- readonly createCanvas: CreateHeadlessCanvas;
12
+ /**
13
+ * Canvas factory such as `createCanvas` from `@napi-rs/canvas`.
14
+ * Required for `CANVAS` rendering or `TextObject` rasterization; optional when `gpuDevice` or `glContext` is supplied.
15
+ */
16
+ readonly createCanvas?: CreateHeadlessCanvas;
13
17
  /** Optional `Path2D` constructor (e.g. from `@napi-rs/canvas`) so `MsdfIconObject` paths render in Node. */
14
18
  readonly Path2D?: typeof Path2D;
15
19
  /** Viewport width in pixels (default `1280`). */
@@ -30,11 +34,26 @@ export interface HeadlessStageOptions {
30
34
  readonly gpuDevice?: WebGpuDeviceLike | null;
31
35
  /** Optional WebGPU offscreen render format when `gpuDevice` is passed (default `'rgba8unorm'`). */
32
36
  readonly gpuFormat?: string;
37
+ /** Optional explicit backend label recorded on `stage.backend`. */
38
+ readonly backend?: HeadlessStageBackend;
39
+ /** Optional cleanup hook invoked when `stage.destroy()` runs. */
40
+ readonly onDestroy?: () => void;
33
41
  /** Optional `StagePlugin[]` (e.g. `createInputTimeline`, `createInputRecorder`) mounted at boot. */
34
42
  readonly plugins?: StagePlugin[];
35
43
  /** Scene instance or constructor to boot synchronously at frame 0. */
36
44
  readonly scene: SceneInput;
37
45
  }
46
+ export interface AsyncHeadlessStageOptions extends Omit<HeadlessStageOptions, 'gpuDevice' | 'gpuFormat' | 'backend'> {
47
+ /**
48
+ * Renderer selection (`default = 'auto'`):
49
+ * - `'auto'`: acquires native Headless WebGPU (`wgpu-native` FFI -> Dawn `webgpu` -> `navigator.gpu`) when available, falling back cleanly to Canvas2D.
50
+ * - `'webgpu'`: requires native Headless WebGPU and throws an actionable setup error if no GPU driver is available.
51
+ * - `'canvas'`: forces deterministic CPU Canvas2D rendering.
52
+ */
53
+ readonly renderer?: 'auto' | 'webgpu' | 'canvas';
54
+ /** Optional native WebGPU discovery options (`libPath`, `backend`, `powerPreference`, `dawnFlags`). */
55
+ readonly webgpu?: HeadlessWebGpuOptions;
56
+ }
38
57
  export interface HeadlessRecordedFrame {
39
58
  /** Zero-based recorded frame index (`0 .. frames - 1`). */
40
59
  readonly index: number;
@@ -65,6 +84,8 @@ export interface HeadlessStage {
65
84
  readonly game: Game;
66
85
  readonly scene: Scene;
67
86
  readonly canvas: HeadlessCanvasLike;
87
+ /** Active headless rendering backend (`'wgpu-bun-ffi' | 'wgpu-node-koffi' | 'dawn' | 'navigator' | 'webgpu' | 'webgl' | 'canvas2d'`). */
88
+ readonly backend: HeadlessStageBackend;
68
89
  readonly width: number;
69
90
  readonly height: number;
70
91
  readonly fps: number;
@@ -113,6 +134,15 @@ export interface HeadlessStage {
113
134
  * (such as `@napi-rs/canvas` in Node) for AI-agent drawing, CI visual assertions, and 60 FPS video rendering.
114
135
  */
115
136
  export declare function createHeadlessStage(options: HeadlessStageOptions): HeadlessStage;
137
+ /**
138
+ * Turnkey async headless Stage initializer across **Bun**, **Node.js**, and **Deno**:
139
+ * - Automatically discovers native Headless WebGPU (`wgpu-native` FFI -> Dawn `webgpu` -> `navigator.gpu`) when `renderer` is `'auto'` (default) or `'webgpu'`
140
+ * - Automatically discovers `@napi-rs/canvas` (`createCanvas` & `Path2D`) if installed when `createCanvas` is omitted (and runs pure-GPU scenes with zero npm dependencies!)
141
+ * - Manages `GPUDevice` cleanup automatically inside `stage.destroy()` and exposes the active backend via `stage.backend`
142
+ */
143
+ export declare function createHeadlessStageAsync(options: AsyncHeadlessStageOptions): Promise<HeadlessStage>;
144
+ /** Alias for `createHeadlessStageAsync` (`await openHeadlessStage({ renderer: 'webgpu', scene })`). */
145
+ export declare const openHeadlessStage: typeof createHeadlessStageAsync;
116
146
  export interface StoryboardOptions {
117
147
  /** Ascending frame numbers to capture (e.g. `[0, 30, 60, 90, 120, 150]`). */
118
148
  readonly frames: readonly number[];
@@ -168,30 +198,33 @@ export interface AudioTimeline {
168
198
  * chiptune cues with 60 FPS headless video renders. Exports ready-to-mux stereo 16-bit `.wav` bytes.
169
199
  */
170
200
  export declare function createAudioTimeline(options?: AudioTimelineOptions): AudioTimeline;
201
+ export type FfmpegInputFormat = 'auto' | 'rawvideo' | 'mjpeg' | 'png';
171
202
  export interface FfmpegRawVideoArgsOptions {
172
203
  readonly width: number;
173
204
  readonly height: number;
174
205
  readonly fps: number;
175
206
  readonly outputPath: string;
207
+ /** Input pipe format (`'rawvideo'` default for `buildFfmpegRawVideoArgs`, `'mjpeg'` or `'png'` for `image2pipe`). */
208
+ readonly inputFormat?: Exclude<FfmpegInputFormat, 'auto'>;
176
209
  /** Optional path to a `.wav` audio file (e.g. written from `AudioTimeline.toWav()`). */
177
210
  readonly audioWavPath?: string;
178
- /** Video codec (default `'libx264'`). */
211
+ /** Video codec (default `'libx264'`, or `'libvpx-vp9'` when `outputPath` ends with `.webm`). */
179
212
  readonly videoCodec?: string;
180
- /** Encoder preset (default `'veryfast'`). */
213
+ /** Encoder preset (default `'veryfast'` for x264/x265). */
181
214
  readonly preset?: string;
182
- /** Constant Rate Factor quality `0..51` (default `19`). */
215
+ /** Constant Rate Factor quality `0..51` (default `19`, or `28` for VP9). */
183
216
  readonly crf?: number;
184
217
  /** Output pixel format for universal player compatibility (default `'yuv420p'`). */
185
218
  readonly outputPixelFormat?: string;
186
- /** Audio codec when `audioWavPath` is supplied (default `'aac'`). */
219
+ /** Audio codec when `audioWavPath` is supplied (default `'aac'`, or `'libopus'` for `.webm`). */
187
220
  readonly audioCodec?: string;
188
221
  /** Audio bitrate when `audioWavPath` is supplied (default `'160k'`). */
189
222
  readonly audioBitrate?: string;
190
- /** Extra CLI flags appended before `-movflags +faststart <outputPath>`. */
223
+ /** Extra CLI flags appended before `<outputPath>`. */
191
224
  readonly extraArgs?: readonly string[];
192
225
  }
193
226
  export interface EncodeVideoWithFfmpegOptions {
194
- /** Destination video file path (e.g. `'out.mp4'`). */
227
+ /** Destination video file path (e.g. `'out.mp4'` or `'out.webm'`). */
195
228
  readonly outputPath: string;
196
229
  /** Total frames to render (`frames` or `durationMs` required). */
197
230
  readonly frames?: number;
@@ -199,26 +232,36 @@ export interface EncodeVideoWithFfmpegOptions {
199
232
  readonly durationMs?: number;
200
233
  /** Optional `AudioTimeline`, stereo `.wav` `Uint8Array`, or `.wav` file path muxed into the output video. */
201
234
  readonly audio?: AudioTimeline | Uint8Array | string;
202
- /** Custom `ffmpeg` executable path (default `'ffmpeg'`). */
235
+ /** Custom `ffmpeg` executable path (auto-discovers system `ffmpeg` or Playwright's cached `ffmpeg` when omitted). */
203
236
  readonly ffmpegPath?: string;
204
- /** Video codec (default `'libx264'`). */
237
+ /**
238
+ * Frame pipe format (`default = 'auto'`):
239
+ * - `'auto'`: uses zero-copy `'rawvideo'` when supported by `ffmpeg`, falling back automatically to `'mjpeg'` on trimmed Playwright `ffmpeg` builds.
240
+ * - `'rawvideo'`: streams uncompressed `RGBA8888` (`-f rawvideo -pix_fmt rgba`).
241
+ * - `'mjpeg'`: encodes each frame as JPEG (`-f image2pipe -vcodec mjpeg`) for stripped `ffmpeg` builds.
242
+ * - `'png'`: encodes each frame as PNG (`-f image2pipe -vcodec png`).
243
+ */
244
+ readonly inputFormat?: FfmpegInputFormat;
245
+ /** JPEG quality `1..100` when `inputFormat` resolves to `'mjpeg'` (`default = 90`). */
246
+ readonly jpegQuality?: number;
247
+ /** Video codec (auto-selects `'libx264'` or `'libvpx-vp9'` based on container and `ffmpeg` capabilities when omitted). */
205
248
  readonly videoCodec?: string;
206
- /** Encoder preset (default `'veryfast'`). */
249
+ /** Encoder preset (default `'veryfast'` on x264/x265). */
207
250
  readonly preset?: string;
208
- /** Constant Rate Factor quality `0..51` (default `19`). */
251
+ /** Constant Rate Factor quality `0..51` (default `19` for H.264, `28` for VP9). */
209
252
  readonly crf?: number;
210
253
  /** Output pixel format (default `'yuv420p'`). */
211
254
  readonly outputPixelFormat?: string;
212
- /** Audio codec when `audio` is provided (default `'aac'`). */
255
+ /** Audio codec when `audio` is provided (default `'aac'` for MP4, `'libopus'` for WebM). */
213
256
  readonly audioCodec?: string;
214
257
  /** Audio bitrate when `audio` is provided (default `'160k'`). */
215
258
  readonly audioBitrate?: string;
216
- /** Extra CLI flags appended before `-movflags +faststart <outputPath>`. */
259
+ /** Extra CLI flags appended before `<outputPath>`. */
217
260
  readonly extraArgs?: readonly string[];
218
261
  /** Capture a zero-PII `SceneDigest` every N frames (`0` = disabled; default `0`). */
219
262
  readonly digestEvery?: number;
220
263
  readonly digestOptions?: SceneDigestOptions;
221
- /** Optional per-frame hook invoked after stepping each frame before piping raw RGBA8888 to `ffmpeg`. */
264
+ /** Optional per-frame hook invoked after stepping each frame before piping to `ffmpeg`. */
222
265
  readonly onFrame?: (frame: HeadlessRecordedFrame) => void | Promise<void>;
223
266
  }
224
267
  export interface EncodeVideoResult {
@@ -230,15 +273,18 @@ export interface EncodeVideoResult {
230
273
  readonly durationMs: number;
231
274
  readonly clock: EngineClockSnapshot;
232
275
  readonly digests: SceneDigest[];
276
+ readonly inputFormat: Exclude<FfmpegInputFormat, 'auto'>;
277
+ readonly videoCodec: string;
233
278
  }
234
279
  /**
235
- * Build deterministic `ffmpeg` CLI arguments that consume raw `RGBA8888` frames from `stdin` (`pipe:0`),
236
- * bypassing per-frame PNG/JPEG `image2pipe` CPU encoding overhead (~33 ms/frame saved at 1280x720!).
280
+ * Build deterministic `ffmpeg` CLI arguments that consume `rawvideo` (`RGBA8888`), `mjpeg`, or `png` frames from `stdin` (`pipe:0`),
281
+ * adapting container flags automatically for MP4 (`libx264` + `+faststart`) and WebM (`libvpx-vp9`).
237
282
  */
238
283
  export declare function buildFfmpegRawVideoArgs(options: FfmpegRawVideoArgsOptions): string[];
239
284
  /**
240
- * Step a `HeadlessStage` in Node.js or Bun and stream raw `RGBA8888` frames (`-f rawvideo -pix_fmt rgba`)
241
- * directly into `ffmpeg` stdin with backpressure handling and optional stereo `.wav` audio muxing.
285
+ * Step a `HeadlessStage` in Node.js or Bun and stream frames into `ffmpeg` stdin with backpressure handling.
286
+ * Automatically uses zero-copy `rawvideo` (`RGBA8888`) on full `ffmpeg` builds and falls back seamlessly to
287
+ * `image2pipe` (`mjpeg` + `libvpx-vp9` WebM) when only Playwright's trimmed `ffmpeg` binary is available.
242
288
  */
243
289
  export declare function encodeVideoWithFfmpeg(stage: HeadlessStage, options: EncodeVideoWithFfmpegOptions): Promise<EncodeVideoResult>;
244
290
  export { type HeadlessWebGpuBackend, type HeadlessWebGpuHandle, type HeadlessWebGpuOptions, type OpenDawnWebGpuOptions, type OpenWgpuNativeFfiOptions, openDawnWebGpu, openWgpuNativeFfi, requestHeadlessWebGpu };