@kitsra/kavio-render-worker 0.1.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/LICENSE +94 -0
- package/dist/index.d.ts +237 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +462 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Elastic License 2.0
|
|
2
|
+
|
|
3
|
+
URL: https://www.elastic.co/licensing/elastic-license
|
|
4
|
+
|
|
5
|
+
Acceptance
|
|
6
|
+
|
|
7
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
8
|
+
|
|
9
|
+
Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide,
|
|
12
|
+
non-sublicensable, non-transferable license to use, copy, distribute, make
|
|
13
|
+
available, and prepare derivative works of the software, in each case subject to
|
|
14
|
+
the limitations and conditions below.
|
|
15
|
+
|
|
16
|
+
Limitations
|
|
17
|
+
|
|
18
|
+
You may not provide the software to third parties as a hosted or managed
|
|
19
|
+
service, where the service provides users with access to any substantial set of
|
|
20
|
+
the features or functionality of the software.
|
|
21
|
+
|
|
22
|
+
You may not move, change, disable, or circumvent the license key functionality
|
|
23
|
+
in the software, and you may not remove or obscure any functionality in the
|
|
24
|
+
software that is protected by the license key.
|
|
25
|
+
|
|
26
|
+
You may not alter, remove, or obscure any licensing, copyright, or other
|
|
27
|
+
notices of the licensor in the software. Any use of the licensor's trademarks
|
|
28
|
+
is subject to applicable law.
|
|
29
|
+
|
|
30
|
+
Patents
|
|
31
|
+
|
|
32
|
+
The licensor grants you a license, under any patent claims the licensor can
|
|
33
|
+
license, or becomes able to license, to make, have made, use, sell, offer for
|
|
34
|
+
sale, import and have imported the software, in each case subject to the
|
|
35
|
+
limitations and conditions in this license. This license does not cover any
|
|
36
|
+
patent claims that you cause to be infringed by modifications or additions to
|
|
37
|
+
the software.
|
|
38
|
+
|
|
39
|
+
If you or your company make any written claim that the software infringes or
|
|
40
|
+
contributes to infringement of any patent, your patent license for the software
|
|
41
|
+
granted under these terms ends immediately. If your company makes such a claim,
|
|
42
|
+
your patent license ends immediately for work on behalf of your company.
|
|
43
|
+
|
|
44
|
+
Notices
|
|
45
|
+
|
|
46
|
+
You must ensure that anyone who gets a copy of any part of the software from
|
|
47
|
+
you also gets a copy of these terms.
|
|
48
|
+
|
|
49
|
+
If you modify the software, you must include in any modified copies of the
|
|
50
|
+
software prominent notices stating that you have modified the software.
|
|
51
|
+
|
|
52
|
+
No Other Rights
|
|
53
|
+
|
|
54
|
+
These terms do not imply any licenses other than those expressly granted in
|
|
55
|
+
these terms.
|
|
56
|
+
|
|
57
|
+
Termination
|
|
58
|
+
|
|
59
|
+
If you use the software in violation of these terms, such use is not licensed,
|
|
60
|
+
and your licenses will automatically terminate. If the licensor provides you
|
|
61
|
+
with a notice of your violation, and you cease all violation of this license no
|
|
62
|
+
later than 30 days after you receive that notice, your licenses will be
|
|
63
|
+
reinstated retroactively. However, if you violate these terms after such
|
|
64
|
+
reinstatement, any additional violation of these terms will cause your licenses
|
|
65
|
+
to terminate automatically and permanently.
|
|
66
|
+
|
|
67
|
+
No Liability
|
|
68
|
+
|
|
69
|
+
As far as the law allows, the software comes as is, without any warranty or
|
|
70
|
+
condition, and the licensor will not be liable to you for any damages arising
|
|
71
|
+
out of these terms or the use or nature of the software, under any kind of
|
|
72
|
+
legal claim.
|
|
73
|
+
|
|
74
|
+
Definitions
|
|
75
|
+
|
|
76
|
+
The licensor is the entity offering these terms, and the software is the
|
|
77
|
+
software the licensor makes available under these terms, including any portion
|
|
78
|
+
of it.
|
|
79
|
+
|
|
80
|
+
you refers to the individual or entity agreeing to these terms.
|
|
81
|
+
|
|
82
|
+
your company is any legal entity, sole proprietorship, or other kind of
|
|
83
|
+
organization that you work for, plus all organizations that have control over,
|
|
84
|
+
are under the control of, or are under common control with that organization.
|
|
85
|
+
control means ownership of substantially all the assets of an entity, or the
|
|
86
|
+
power to direct its management and policies by vote, contract, or otherwise.
|
|
87
|
+
Control can be direct or indirect.
|
|
88
|
+
|
|
89
|
+
your licenses are all the licenses granted to you for the software under these
|
|
90
|
+
terms.
|
|
91
|
+
|
|
92
|
+
use means anything you do with the software requiring one of your licenses.
|
|
93
|
+
|
|
94
|
+
trademark means trademarks, service marks, and similar rights.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import type { KavioAudioCodec, KavioDocument, KavioExportCodec, KavioExportFormat, KavioExportPreset, KavioJsonValue } from "@kitsra/kavio-schema";
|
|
2
|
+
export type BrowserDriverKind = "playwright" | "puppeteer" | "cloud-browser";
|
|
3
|
+
export type BrowserEngine = "chromium";
|
|
4
|
+
export type BrowserScreenshotFormat = "png";
|
|
5
|
+
export type BrowserScreenshotMimeType = "image/png";
|
|
6
|
+
export type ChromiumColorProfile = "srgb";
|
|
7
|
+
export declare const DEFAULT_BROWSER_DRIVER_KIND = "playwright";
|
|
8
|
+
export declare const DEFAULT_BROWSER_ENGINE = "chromium";
|
|
9
|
+
export declare const DEFAULT_BROWSER_SCREENSHOT_FORMAT = "png";
|
|
10
|
+
export declare const DEFAULT_BROWSER_SCREENSHOT_MIME_TYPE = "image/png";
|
|
11
|
+
export declare const DEFAULT_BROWSER_DEVICE_SCALE_FACTOR = 1;
|
|
12
|
+
export declare const DEFAULT_CHROMIUM_COLOR_PROFILE = "srgb";
|
|
13
|
+
export declare const DETERMINISTIC_CHROMIUM_FLAGS: readonly ["--disable-gpu", "--disable-gpu-compositing", "--disable-font-subpixel-positioning", "--force-color-profile=srgb", "--font-render-hinting=none"];
|
|
14
|
+
export interface BrowserViewport {
|
|
15
|
+
width: number;
|
|
16
|
+
height: number;
|
|
17
|
+
deviceScaleFactor: number;
|
|
18
|
+
}
|
|
19
|
+
export interface BrowserOpenOptions {
|
|
20
|
+
viewport?: BrowserViewport;
|
|
21
|
+
}
|
|
22
|
+
export interface BrowserFrameCaptureOptions {
|
|
23
|
+
omitBackground?: boolean;
|
|
24
|
+
}
|
|
25
|
+
export interface BrowserFrameCapture {
|
|
26
|
+
frame: number;
|
|
27
|
+
bytes: Uint8Array;
|
|
28
|
+
format: BrowserScreenshotFormat;
|
|
29
|
+
mimeType: BrowserScreenshotMimeType;
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
omitBackground: boolean;
|
|
33
|
+
}
|
|
34
|
+
export interface DeterministicChromiumLaunchOptions {
|
|
35
|
+
headless: true;
|
|
36
|
+
args: readonly string[];
|
|
37
|
+
}
|
|
38
|
+
export declare const DEFAULT_CHROMIUM_LAUNCH_OPTIONS: {
|
|
39
|
+
readonly headless: true;
|
|
40
|
+
readonly args: readonly ["--disable-gpu", "--disable-gpu-compositing", "--disable-font-subpixel-positioning", "--force-color-profile=srgb", "--font-render-hinting=none"];
|
|
41
|
+
};
|
|
42
|
+
export declare const DEFAULT_BROWSER_FRAME_CAPTURE_OPTIONS: {
|
|
43
|
+
readonly omitBackground: true;
|
|
44
|
+
};
|
|
45
|
+
export interface BrowserLaunchMetadata {
|
|
46
|
+
engine: BrowserEngine;
|
|
47
|
+
chromiumRevision: string;
|
|
48
|
+
headless: true;
|
|
49
|
+
args: readonly string[];
|
|
50
|
+
colorProfile: ChromiumColorProfile;
|
|
51
|
+
deviceScaleFactor: number;
|
|
52
|
+
}
|
|
53
|
+
export interface BrowserDriverMetadata {
|
|
54
|
+
kind: BrowserDriverKind;
|
|
55
|
+
name: string;
|
|
56
|
+
version?: string;
|
|
57
|
+
launch: BrowserLaunchMetadata;
|
|
58
|
+
}
|
|
59
|
+
export interface BrowserDriverMetadataOptions {
|
|
60
|
+
kind?: BrowserDriverKind;
|
|
61
|
+
name?: string;
|
|
62
|
+
version?: string;
|
|
63
|
+
chromiumRevision: string;
|
|
64
|
+
launchOptions?: DeterministicChromiumLaunchOptions;
|
|
65
|
+
deviceScaleFactor?: number;
|
|
66
|
+
colorProfile?: ChromiumColorProfile;
|
|
67
|
+
}
|
|
68
|
+
export interface BrowserDriver {
|
|
69
|
+
open(composition: KavioDocument, options?: BrowserOpenOptions): Promise<void>;
|
|
70
|
+
renderFrame(frame: number, options?: BrowserFrameCaptureOptions): Promise<BrowserFrameCapture>;
|
|
71
|
+
close(): Promise<void>;
|
|
72
|
+
}
|
|
73
|
+
export declare function createBrowserViewport(composition: KavioDocument, deviceScaleFactor?: number): BrowserViewport;
|
|
74
|
+
export declare function createBrowserDriverMetadata(options: BrowserDriverMetadataOptions): BrowserDriverMetadata;
|
|
75
|
+
export declare function createPngFrameCapture(options: {
|
|
76
|
+
frame: number;
|
|
77
|
+
bytes: Uint8Array;
|
|
78
|
+
viewport: BrowserViewport;
|
|
79
|
+
omitBackground?: boolean;
|
|
80
|
+
}): BrowserFrameCapture;
|
|
81
|
+
export type RenderChecksumAlgorithm = "sha256" | "sha512";
|
|
82
|
+
export type RenderCleanupReason = "success" | "failure" | "manual";
|
|
83
|
+
export type RenderCleanupScope = "browser-context" | "temporary-frames" | "temporary-file" | "custom";
|
|
84
|
+
export type RenderPropValues = Record<string, KavioJsonValue>;
|
|
85
|
+
export type RenderBatchPresetInput = string | KavioExportPreset;
|
|
86
|
+
export interface RenderChecksum {
|
|
87
|
+
algorithm: RenderChecksumAlgorithm;
|
|
88
|
+
value: string;
|
|
89
|
+
bytes?: number;
|
|
90
|
+
}
|
|
91
|
+
export interface RenderOutputReference {
|
|
92
|
+
name: string;
|
|
93
|
+
path?: string;
|
|
94
|
+
format: KavioExportFormat;
|
|
95
|
+
}
|
|
96
|
+
export interface RenderOutputDimensions {
|
|
97
|
+
width: number;
|
|
98
|
+
height: number;
|
|
99
|
+
}
|
|
100
|
+
export interface RenderOutputDuration {
|
|
101
|
+
frames: number;
|
|
102
|
+
fps: number;
|
|
103
|
+
seconds: number;
|
|
104
|
+
}
|
|
105
|
+
export interface RenderOutputCodecs {
|
|
106
|
+
video: KavioExportCodec | null;
|
|
107
|
+
audio: KavioAudioCodec | null;
|
|
108
|
+
}
|
|
109
|
+
export interface RenderToolMetadata {
|
|
110
|
+
ffmpeg: {
|
|
111
|
+
version: string;
|
|
112
|
+
};
|
|
113
|
+
chromium: {
|
|
114
|
+
revision: string;
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
export interface RenderOutputMetadata {
|
|
118
|
+
version: "0.1";
|
|
119
|
+
output: RenderOutputReference;
|
|
120
|
+
dimensions: RenderOutputDimensions;
|
|
121
|
+
duration: RenderOutputDuration;
|
|
122
|
+
codecs: RenderOutputCodecs;
|
|
123
|
+
checksums: RenderChecksum[];
|
|
124
|
+
tools: RenderToolMetadata;
|
|
125
|
+
createdAt: string;
|
|
126
|
+
}
|
|
127
|
+
export interface CreateRenderMetadataOptions {
|
|
128
|
+
composition: KavioDocument["composition"];
|
|
129
|
+
preset: KavioExportPreset;
|
|
130
|
+
outputName?: string;
|
|
131
|
+
outputPath?: string;
|
|
132
|
+
checksums?: RenderChecksum | readonly RenderChecksum[];
|
|
133
|
+
ffmpegVersion: string;
|
|
134
|
+
chromiumRevision: string;
|
|
135
|
+
createdAt?: string;
|
|
136
|
+
}
|
|
137
|
+
export interface RenderCleanupContext {
|
|
138
|
+
reason: RenderCleanupReason;
|
|
139
|
+
error?: unknown;
|
|
140
|
+
}
|
|
141
|
+
export interface RenderCleanupTask {
|
|
142
|
+
id: string;
|
|
143
|
+
scope: RenderCleanupScope;
|
|
144
|
+
cleanup(context: RenderCleanupContext): void | Promise<void>;
|
|
145
|
+
}
|
|
146
|
+
export interface RenderCleanupRecord {
|
|
147
|
+
id: string;
|
|
148
|
+
scope: RenderCleanupScope;
|
|
149
|
+
}
|
|
150
|
+
export interface RenderCleanupFailure extends RenderCleanupRecord {
|
|
151
|
+
error: unknown;
|
|
152
|
+
}
|
|
153
|
+
export interface RenderCleanupReport {
|
|
154
|
+
reason: RenderCleanupReason;
|
|
155
|
+
cleaned: RenderCleanupRecord[];
|
|
156
|
+
failures: RenderCleanupFailure[];
|
|
157
|
+
}
|
|
158
|
+
export interface RenderBatchRow {
|
|
159
|
+
id?: string;
|
|
160
|
+
props?: RenderPropValues;
|
|
161
|
+
}
|
|
162
|
+
export type FrameCaptureProgressPhase = "open" | "capture" | "frame-error" | "complete";
|
|
163
|
+
export interface FrameCaptureProgress {
|
|
164
|
+
phase: FrameCaptureProgressPhase;
|
|
165
|
+
frame?: number;
|
|
166
|
+
totalFrames: number;
|
|
167
|
+
completedFrames: number;
|
|
168
|
+
capturedFrames: number;
|
|
169
|
+
failedFrames: number;
|
|
170
|
+
bytesCaptured: number;
|
|
171
|
+
error?: unknown;
|
|
172
|
+
}
|
|
173
|
+
export interface FrameCaptureFrameError {
|
|
174
|
+
frame: number;
|
|
175
|
+
error: unknown;
|
|
176
|
+
}
|
|
177
|
+
export interface CaptureFramesOptions {
|
|
178
|
+
driver: BrowserDriver;
|
|
179
|
+
composition: KavioDocument;
|
|
180
|
+
openOptions?: BrowserOpenOptions;
|
|
181
|
+
captureOptions?: BrowserFrameCaptureOptions;
|
|
182
|
+
startFrame?: number;
|
|
183
|
+
frameCount?: number;
|
|
184
|
+
continueOnFrameError?: boolean;
|
|
185
|
+
onProgress?: (progress: FrameCaptureProgress) => void | Promise<void>;
|
|
186
|
+
onFrame?: (capture: BrowserFrameCapture, progress: FrameCaptureProgress) => void | Promise<void>;
|
|
187
|
+
onFrameError?: (failure: FrameCaptureFrameError, progress: FrameCaptureProgress) => void | Promise<void>;
|
|
188
|
+
}
|
|
189
|
+
export interface CaptureFramesResult {
|
|
190
|
+
captures: BrowserFrameCapture[];
|
|
191
|
+
errors: FrameCaptureFrameError[];
|
|
192
|
+
totalFrames: number;
|
|
193
|
+
completedFrames: number;
|
|
194
|
+
capturedFrames: number;
|
|
195
|
+
failedFrames: number;
|
|
196
|
+
bytesCaptured: number;
|
|
197
|
+
}
|
|
198
|
+
export interface RenderBatchInput {
|
|
199
|
+
template: KavioDocument;
|
|
200
|
+
rows: readonly RenderBatchRow[];
|
|
201
|
+
presets?: readonly RenderBatchPresetInput[];
|
|
202
|
+
outputDirectory?: string;
|
|
203
|
+
outputNamePrefix?: string;
|
|
204
|
+
}
|
|
205
|
+
export interface RenderBatchJob {
|
|
206
|
+
id: string;
|
|
207
|
+
rowIndex: number;
|
|
208
|
+
rowId: string;
|
|
209
|
+
presetIndex: number;
|
|
210
|
+
presetName: string;
|
|
211
|
+
props: RenderPropValues;
|
|
212
|
+
preset: KavioExportPreset;
|
|
213
|
+
document: KavioDocument;
|
|
214
|
+
outputName: string;
|
|
215
|
+
outputPath?: string;
|
|
216
|
+
}
|
|
217
|
+
export declare class RenderCleanupStack {
|
|
218
|
+
private tasks;
|
|
219
|
+
private closed;
|
|
220
|
+
defer(task: RenderCleanupTask): RenderCleanupTask;
|
|
221
|
+
release(id: string): boolean;
|
|
222
|
+
cleanup(reason: RenderCleanupReason, error?: unknown): Promise<RenderCleanupReport>;
|
|
223
|
+
}
|
|
224
|
+
export declare function createRenderMetadata(options: CreateRenderMetadataOptions): RenderOutputMetadata;
|
|
225
|
+
export declare function createBrowserContextCleanupTask(driver: Pick<BrowserDriver, "close">, id?: string): RenderCleanupTask;
|
|
226
|
+
export declare function createTemporaryFramesCleanupTask(cleanup: (context: RenderCleanupContext) => void | Promise<void>, id?: string): RenderCleanupTask;
|
|
227
|
+
export declare function withRenderCleanup<T>(operation: (cleanup: RenderCleanupStack) => T | Promise<T>): Promise<T>;
|
|
228
|
+
export declare function captureFrames(options: CaptureFramesOptions): Promise<CaptureFramesResult>;
|
|
229
|
+
export declare function expandRenderBatch(input: RenderBatchInput): RenderBatchJob[];
|
|
230
|
+
export declare function createStableOutputName(options: {
|
|
231
|
+
prefix?: string;
|
|
232
|
+
row: RenderBatchRow;
|
|
233
|
+
rowIndex: number;
|
|
234
|
+
preset: KavioExportPreset;
|
|
235
|
+
presetIndex: number;
|
|
236
|
+
}): string;
|
|
237
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index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AAoB,CAAC,OAAO,EAAE,2BAA2B,GAAG,oBAAoB,CA4C/F;AAED,wBAAgB,+BAA+B,CAC7C,MAAM,EAAE,IAAI,CAAC,aAAa,EAAE,OAAO,CAAC,EACpC,EAAE,SAAoB,GACrB,iBAAiB,CAMnB;AAED,wBAAgB,gCAAgC,CAC9C,OAAO,EAAE,CAAC,OAAO,EAAE,oBAAoB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EAChE,EAAE,SAAqB,GACtB,iBAAiB,CAMnB;AAED,wBAAsB,iBAAiB,CAAC,CAAC,EACvC,SAAS,EAAE,CAAC,OAAO,EAAE,kBAAkB,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GACzD,OAAO,CAAC,CAAC,CAAC,CAuBZ;AAED,wBAAsB,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAgF/F;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,gBAAgB,GAAG,cAAc,EAAE,CAkD3E;AAED,wBAAgB,sBAAsB,CAAC,OAAO,EAAE;IAC9C,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,cAAc,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,iBAAiB,CAAC;IAC1B,WAAW,EAAE,MAAM,CAAC;CACrB,GAAG,MAAM,CAQT"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
import { extensionForFormat } from "@kitsra/kavio-schema";
|
|
2
|
+
export const DEFAULT_BROWSER_DRIVER_KIND = "playwright";
|
|
3
|
+
export const DEFAULT_BROWSER_ENGINE = "chromium";
|
|
4
|
+
export const DEFAULT_BROWSER_SCREENSHOT_FORMAT = "png";
|
|
5
|
+
export const DEFAULT_BROWSER_SCREENSHOT_MIME_TYPE = "image/png";
|
|
6
|
+
export const DEFAULT_BROWSER_DEVICE_SCALE_FACTOR = 1;
|
|
7
|
+
export const DEFAULT_CHROMIUM_COLOR_PROFILE = "srgb";
|
|
8
|
+
export const DETERMINISTIC_CHROMIUM_FLAGS = [
|
|
9
|
+
"--disable-gpu",
|
|
10
|
+
"--disable-gpu-compositing",
|
|
11
|
+
"--disable-font-subpixel-positioning",
|
|
12
|
+
"--force-color-profile=srgb",
|
|
13
|
+
"--font-render-hinting=none"
|
|
14
|
+
];
|
|
15
|
+
export const DEFAULT_CHROMIUM_LAUNCH_OPTIONS = {
|
|
16
|
+
headless: true,
|
|
17
|
+
args: DETERMINISTIC_CHROMIUM_FLAGS
|
|
18
|
+
};
|
|
19
|
+
export const DEFAULT_BROWSER_FRAME_CAPTURE_OPTIONS = {
|
|
20
|
+
omitBackground: true
|
|
21
|
+
};
|
|
22
|
+
export function createBrowserViewport(composition, deviceScaleFactor = DEFAULT_BROWSER_DEVICE_SCALE_FACTOR) {
|
|
23
|
+
assertPositiveInteger(composition.composition.width, "composition.composition.width");
|
|
24
|
+
assertPositiveInteger(composition.composition.height, "composition.composition.height");
|
|
25
|
+
assertPositiveNumber(deviceScaleFactor, "deviceScaleFactor");
|
|
26
|
+
return {
|
|
27
|
+
width: composition.composition.width,
|
|
28
|
+
height: composition.composition.height,
|
|
29
|
+
deviceScaleFactor
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export function createBrowserDriverMetadata(options) {
|
|
33
|
+
const launchOptions = options.launchOptions ?? DEFAULT_CHROMIUM_LAUNCH_OPTIONS;
|
|
34
|
+
const deviceScaleFactor = options.deviceScaleFactor ?? DEFAULT_BROWSER_DEVICE_SCALE_FACTOR;
|
|
35
|
+
const colorProfile = options.colorProfile ?? DEFAULT_CHROMIUM_COLOR_PROFILE;
|
|
36
|
+
assertNonEmptyString(options.chromiumRevision, "chromiumRevision");
|
|
37
|
+
assertPositiveNumber(deviceScaleFactor, "deviceScaleFactor");
|
|
38
|
+
const launch = {
|
|
39
|
+
engine: DEFAULT_BROWSER_ENGINE,
|
|
40
|
+
chromiumRevision: options.chromiumRevision,
|
|
41
|
+
headless: launchOptions.headless,
|
|
42
|
+
args: [...launchOptions.args],
|
|
43
|
+
colorProfile,
|
|
44
|
+
deviceScaleFactor
|
|
45
|
+
};
|
|
46
|
+
const metadataBase = {
|
|
47
|
+
kind: options.kind ?? DEFAULT_BROWSER_DRIVER_KIND,
|
|
48
|
+
name: options.name ?? DEFAULT_BROWSER_DRIVER_KIND,
|
|
49
|
+
launch
|
|
50
|
+
};
|
|
51
|
+
if (options.version === undefined) {
|
|
52
|
+
return metadataBase;
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
...metadataBase,
|
|
56
|
+
version: options.version
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function createPngFrameCapture(options) {
|
|
60
|
+
assertNonNegativeInteger(options.frame, "frame");
|
|
61
|
+
assertPositiveInteger(options.viewport.width, "viewport.width");
|
|
62
|
+
assertPositiveInteger(options.viewport.height, "viewport.height");
|
|
63
|
+
return {
|
|
64
|
+
frame: options.frame,
|
|
65
|
+
bytes: options.bytes,
|
|
66
|
+
format: DEFAULT_BROWSER_SCREENSHOT_FORMAT,
|
|
67
|
+
mimeType: DEFAULT_BROWSER_SCREENSHOT_MIME_TYPE,
|
|
68
|
+
width: options.viewport.width,
|
|
69
|
+
height: options.viewport.height,
|
|
70
|
+
omitBackground: options.omitBackground ?? true
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export class RenderCleanupStack {
|
|
74
|
+
tasks = [];
|
|
75
|
+
closed = false;
|
|
76
|
+
defer(task) {
|
|
77
|
+
if (this.closed) {
|
|
78
|
+
throw new Error("Cannot register render cleanup after cleanup has started.");
|
|
79
|
+
}
|
|
80
|
+
assertNonEmptyString(task.id, "task.id");
|
|
81
|
+
this.tasks.push(task);
|
|
82
|
+
return task;
|
|
83
|
+
}
|
|
84
|
+
release(id) {
|
|
85
|
+
if (this.closed) {
|
|
86
|
+
throw new Error("Cannot release render cleanup after cleanup has started.");
|
|
87
|
+
}
|
|
88
|
+
const index = this.tasks.findIndex((task) => task.id === id);
|
|
89
|
+
if (index === -1) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
this.tasks.splice(index, 1);
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
async cleanup(reason, error) {
|
|
96
|
+
this.closed = true;
|
|
97
|
+
const cleaned = [];
|
|
98
|
+
const failures = [];
|
|
99
|
+
const context = createCleanupContext(reason, error);
|
|
100
|
+
for (let index = this.tasks.length - 1; index >= 0; index -= 1) {
|
|
101
|
+
const task = this.tasks[index];
|
|
102
|
+
if (!task) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
await task.cleanup(context);
|
|
107
|
+
cleaned.push({ id: task.id, scope: task.scope });
|
|
108
|
+
}
|
|
109
|
+
catch (cleanupError) {
|
|
110
|
+
failures.push({ id: task.id, scope: task.scope, error: cleanupError });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
this.tasks = [];
|
|
114
|
+
return { reason, cleaned, failures };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
export function createRenderMetadata(options) {
|
|
118
|
+
assertPositiveInteger(options.composition.durationFrames, "composition.durationFrames");
|
|
119
|
+
assertPositiveInteger(options.preset.width, "preset.width");
|
|
120
|
+
assertPositiveInteger(options.preset.height, "preset.height");
|
|
121
|
+
assertPositiveNumber(options.preset.fps ?? options.composition.fps, "fps");
|
|
122
|
+
assertNonEmptyString(options.ffmpegVersion, "ffmpegVersion");
|
|
123
|
+
assertNonEmptyString(options.chromiumRevision, "chromiumRevision");
|
|
124
|
+
const fps = options.preset.fps ?? options.composition.fps;
|
|
125
|
+
const output = {
|
|
126
|
+
name: options.outputName ?? defaultOutputName(options.preset),
|
|
127
|
+
format: options.preset.format
|
|
128
|
+
};
|
|
129
|
+
if (options.outputPath !== undefined) {
|
|
130
|
+
output.path = options.outputPath;
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
version: "0.1",
|
|
134
|
+
output,
|
|
135
|
+
dimensions: {
|
|
136
|
+
width: options.preset.width,
|
|
137
|
+
height: options.preset.height
|
|
138
|
+
},
|
|
139
|
+
duration: {
|
|
140
|
+
frames: options.composition.durationFrames,
|
|
141
|
+
fps,
|
|
142
|
+
seconds: options.composition.durationFrames / fps
|
|
143
|
+
},
|
|
144
|
+
codecs: {
|
|
145
|
+
video: options.preset.codec ?? defaultVideoCodec(options.preset.format),
|
|
146
|
+
audio: options.preset.audioCodec ?? null
|
|
147
|
+
},
|
|
148
|
+
checksums: normalizeChecksums(options.checksums),
|
|
149
|
+
tools: {
|
|
150
|
+
ffmpeg: {
|
|
151
|
+
version: options.ffmpegVersion
|
|
152
|
+
},
|
|
153
|
+
chromium: {
|
|
154
|
+
revision: options.chromiumRevision
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
createdAt: options.createdAt ?? new Date().toISOString()
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
export function createBrowserContextCleanupTask(driver, id = "browser-context") {
|
|
161
|
+
return {
|
|
162
|
+
id,
|
|
163
|
+
scope: "browser-context",
|
|
164
|
+
cleanup: () => driver.close()
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
export function createTemporaryFramesCleanupTask(cleanup, id = "temporary-frames") {
|
|
168
|
+
return {
|
|
169
|
+
id,
|
|
170
|
+
scope: "temporary-frames",
|
|
171
|
+
cleanup
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
export async function withRenderCleanup(operation) {
|
|
175
|
+
const cleanup = new RenderCleanupStack();
|
|
176
|
+
let result;
|
|
177
|
+
let operationError;
|
|
178
|
+
let completed = false;
|
|
179
|
+
try {
|
|
180
|
+
result = await operation(cleanup);
|
|
181
|
+
completed = true;
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
operationError = error;
|
|
185
|
+
}
|
|
186
|
+
const report = await cleanup.cleanup(completed ? "success" : "failure", operationError);
|
|
187
|
+
if (report.failures.length > 0) {
|
|
188
|
+
throw createCleanupFailureError(report, operationError);
|
|
189
|
+
}
|
|
190
|
+
if (operationError !== undefined) {
|
|
191
|
+
throw operationError;
|
|
192
|
+
}
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
export async function captureFrames(options) {
|
|
196
|
+
const range = resolveFrameCaptureRange(options.composition, options.startFrame, options.frameCount);
|
|
197
|
+
const captureOptions = {
|
|
198
|
+
...DEFAULT_BROWSER_FRAME_CAPTURE_OPTIONS,
|
|
199
|
+
...options.captureOptions
|
|
200
|
+
};
|
|
201
|
+
const captures = [];
|
|
202
|
+
const errors = [];
|
|
203
|
+
let completedFrames = 0;
|
|
204
|
+
let capturedFrames = 0;
|
|
205
|
+
let failedFrames = 0;
|
|
206
|
+
let bytesCaptured = 0;
|
|
207
|
+
const createProgress = (phase, frame, error) => {
|
|
208
|
+
const progress = {
|
|
209
|
+
phase,
|
|
210
|
+
totalFrames: range.totalFrames,
|
|
211
|
+
completedFrames,
|
|
212
|
+
capturedFrames,
|
|
213
|
+
failedFrames,
|
|
214
|
+
bytesCaptured
|
|
215
|
+
};
|
|
216
|
+
if (frame !== undefined) {
|
|
217
|
+
progress.frame = frame;
|
|
218
|
+
}
|
|
219
|
+
if (error !== undefined) {
|
|
220
|
+
progress.error = error;
|
|
221
|
+
}
|
|
222
|
+
return progress;
|
|
223
|
+
};
|
|
224
|
+
await withRenderCleanup(async (cleanup) => {
|
|
225
|
+
cleanup.defer(createBrowserContextCleanupTask(options.driver));
|
|
226
|
+
await emitCaptureProgress(options.onProgress, createProgress("open"));
|
|
227
|
+
await options.driver.open(options.composition, {
|
|
228
|
+
viewport: createBrowserViewport(options.composition),
|
|
229
|
+
...options.openOptions
|
|
230
|
+
});
|
|
231
|
+
for (let frame = range.startFrame; frame < range.endFrame; frame += 1) {
|
|
232
|
+
let capture;
|
|
233
|
+
try {
|
|
234
|
+
capture = await options.driver.renderFrame(frame, captureOptions);
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
const failure = { frame, error };
|
|
238
|
+
errors.push(failure);
|
|
239
|
+
failedFrames += 1;
|
|
240
|
+
completedFrames += 1;
|
|
241
|
+
const progress = createProgress("frame-error", frame, error);
|
|
242
|
+
await emitCaptureFrameError(options.onFrameError, failure, progress);
|
|
243
|
+
await emitCaptureProgress(options.onProgress, progress);
|
|
244
|
+
if (options.continueOnFrameError !== true) {
|
|
245
|
+
throw createFrameCaptureError(failure);
|
|
246
|
+
}
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
captures.push(capture);
|
|
250
|
+
capturedFrames += 1;
|
|
251
|
+
completedFrames += 1;
|
|
252
|
+
bytesCaptured += capture.bytes.byteLength;
|
|
253
|
+
const progress = createProgress("capture", frame);
|
|
254
|
+
await emitCapturedFrame(options.onFrame, capture, progress);
|
|
255
|
+
await emitCaptureProgress(options.onProgress, progress);
|
|
256
|
+
}
|
|
257
|
+
await emitCaptureProgress(options.onProgress, createProgress("complete"));
|
|
258
|
+
});
|
|
259
|
+
return {
|
|
260
|
+
captures,
|
|
261
|
+
errors,
|
|
262
|
+
totalFrames: range.totalFrames,
|
|
263
|
+
completedFrames,
|
|
264
|
+
capturedFrames,
|
|
265
|
+
failedFrames,
|
|
266
|
+
bytesCaptured
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
export function expandRenderBatch(input) {
|
|
270
|
+
if (input.rows.length === 0) {
|
|
271
|
+
throw new Error("Render batch must include at least one prop row.");
|
|
272
|
+
}
|
|
273
|
+
const presets = resolveBatchPresets(input.template, input.presets);
|
|
274
|
+
if (presets.length === 0) {
|
|
275
|
+
throw new Error("Render batch must include at least one export preset.");
|
|
276
|
+
}
|
|
277
|
+
const jobs = [];
|
|
278
|
+
input.rows.forEach((row, rowIndex) => {
|
|
279
|
+
const rowId = stableRowId(row, rowIndex);
|
|
280
|
+
presets.forEach((preset, presetIndex) => {
|
|
281
|
+
const presetName = stableNamePart(preset.name, `preset-${formatIndex(presetIndex)}`);
|
|
282
|
+
const outputNameOptions = {
|
|
283
|
+
row,
|
|
284
|
+
rowIndex,
|
|
285
|
+
preset,
|
|
286
|
+
presetIndex
|
|
287
|
+
};
|
|
288
|
+
const outputName = createStableOutputName(input.outputNamePrefix === undefined
|
|
289
|
+
? outputNameOptions
|
|
290
|
+
: {
|
|
291
|
+
...outputNameOptions,
|
|
292
|
+
prefix: input.outputNamePrefix
|
|
293
|
+
});
|
|
294
|
+
const outputPath = joinOutputPath(input.outputDirectory, outputName);
|
|
295
|
+
const props = cloneJsonObject(row.props ?? {});
|
|
296
|
+
const job = {
|
|
297
|
+
id: `${rowId}:${presetName}`,
|
|
298
|
+
rowIndex,
|
|
299
|
+
rowId,
|
|
300
|
+
presetIndex,
|
|
301
|
+
presetName,
|
|
302
|
+
props,
|
|
303
|
+
preset: cloneJsonObject(preset),
|
|
304
|
+
document: cloneDocumentForPreset(input.template, preset),
|
|
305
|
+
outputName
|
|
306
|
+
};
|
|
307
|
+
if (outputPath !== undefined) {
|
|
308
|
+
job.outputPath = outputPath;
|
|
309
|
+
}
|
|
310
|
+
jobs.push(job);
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
return jobs;
|
|
314
|
+
}
|
|
315
|
+
export function createStableOutputName(options) {
|
|
316
|
+
const parts = [
|
|
317
|
+
options.prefix === undefined ? undefined : stableNamePart(options.prefix, "batch"),
|
|
318
|
+
stableRowId(options.row, options.rowIndex),
|
|
319
|
+
stableNamePart(options.preset.name, `preset-${formatIndex(options.presetIndex)}`)
|
|
320
|
+
].filter((part) => part !== undefined && part.length > 0);
|
|
321
|
+
return `${parts.join("-")}.${extensionForFormat(options.preset.format)}`;
|
|
322
|
+
}
|
|
323
|
+
function resolveFrameCaptureRange(composition, startFrame = 0, frameCount = composition.composition.durationFrames - startFrame) {
|
|
324
|
+
assertPositiveInteger(composition.composition.durationFrames, "composition.composition.durationFrames");
|
|
325
|
+
assertNonNegativeInteger(startFrame, "startFrame");
|
|
326
|
+
assertPositiveInteger(frameCount, "frameCount");
|
|
327
|
+
const endFrame = startFrame + frameCount;
|
|
328
|
+
if (startFrame >= composition.composition.durationFrames) {
|
|
329
|
+
throw new Error("startFrame must be less than composition.composition.durationFrames.");
|
|
330
|
+
}
|
|
331
|
+
if (endFrame > composition.composition.durationFrames) {
|
|
332
|
+
throw new Error("frame capture range must not exceed composition.composition.durationFrames.");
|
|
333
|
+
}
|
|
334
|
+
return {
|
|
335
|
+
startFrame,
|
|
336
|
+
endFrame,
|
|
337
|
+
totalFrames: frameCount
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
async function emitCaptureProgress(onProgress, progress) {
|
|
341
|
+
if (onProgress !== undefined) {
|
|
342
|
+
await onProgress(progress);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
async function emitCapturedFrame(onFrame, capture, progress) {
|
|
346
|
+
if (onFrame !== undefined) {
|
|
347
|
+
await onFrame(capture, progress);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
async function emitCaptureFrameError(onFrameError, failure, progress) {
|
|
351
|
+
if (onFrameError !== undefined) {
|
|
352
|
+
await onFrameError(failure, progress);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
function createFrameCaptureError(failure) {
|
|
356
|
+
if (failure.error instanceof Error) {
|
|
357
|
+
return new Error(`Failed to capture frame ${failure.frame}: ${failure.error.message}`, { cause: failure.error });
|
|
358
|
+
}
|
|
359
|
+
return new Error(`Failed to capture frame ${failure.frame}.`, { cause: failure.error });
|
|
360
|
+
}
|
|
361
|
+
function createCleanupContext(reason, error) {
|
|
362
|
+
if (error === undefined) {
|
|
363
|
+
return { reason };
|
|
364
|
+
}
|
|
365
|
+
return { reason, error };
|
|
366
|
+
}
|
|
367
|
+
function createCleanupFailureError(report, operationError) {
|
|
368
|
+
const cleanupErrors = report.failures.map((failure) => failure.error);
|
|
369
|
+
if (operationError !== undefined) {
|
|
370
|
+
return new AggregateError([operationError, ...cleanupErrors], "Render operation failed and one or more cleanup tasks failed.");
|
|
371
|
+
}
|
|
372
|
+
return new AggregateError(cleanupErrors, "One or more render cleanup tasks failed.");
|
|
373
|
+
}
|
|
374
|
+
function resolveBatchPresets(template, presets) {
|
|
375
|
+
const inputs = presets ?? template.exports;
|
|
376
|
+
return inputs.map((preset) => {
|
|
377
|
+
if (typeof preset !== "string") {
|
|
378
|
+
return cloneJsonObject(preset);
|
|
379
|
+
}
|
|
380
|
+
const match = template.exports.find((candidate) => candidate.name === preset);
|
|
381
|
+
if (!match) {
|
|
382
|
+
throw new Error(`Unknown export preset "${preset}".`);
|
|
383
|
+
}
|
|
384
|
+
return cloneJsonObject(match);
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
function cloneDocumentForPreset(template, preset) {
|
|
388
|
+
return {
|
|
389
|
+
...cloneJsonObject(template),
|
|
390
|
+
exports: [cloneJsonObject(preset)]
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
function stableRowId(row, rowIndex) {
|
|
394
|
+
const indexPart = `row-${formatIndex(rowIndex)}`;
|
|
395
|
+
if (row.id === undefined || row.id.length === 0) {
|
|
396
|
+
return indexPart;
|
|
397
|
+
}
|
|
398
|
+
return `${indexPart}-${stableNamePart(row.id, "item")}`;
|
|
399
|
+
}
|
|
400
|
+
function stableNamePart(value, fallback) {
|
|
401
|
+
const normalized = value
|
|
402
|
+
.trim()
|
|
403
|
+
.toLowerCase()
|
|
404
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
405
|
+
.replace(/^-+|-+$/g, "")
|
|
406
|
+
.replace(/-{2,}/g, "-");
|
|
407
|
+
return normalized.length === 0 ? fallback : normalized;
|
|
408
|
+
}
|
|
409
|
+
function formatIndex(index) {
|
|
410
|
+
return String(index + 1).padStart(3, "0");
|
|
411
|
+
}
|
|
412
|
+
function joinOutputPath(directory, outputName) {
|
|
413
|
+
if (directory === undefined || directory.length === 0) {
|
|
414
|
+
return undefined;
|
|
415
|
+
}
|
|
416
|
+
return `${directory.replace(/\/+$/g, "")}/${outputName}`;
|
|
417
|
+
}
|
|
418
|
+
function defaultOutputName(preset) {
|
|
419
|
+
return `${stableNamePart(preset.name, "render")}.${extensionForFormat(preset.format)}`;
|
|
420
|
+
}
|
|
421
|
+
function defaultVideoCodec(format) {
|
|
422
|
+
switch (format) {
|
|
423
|
+
case "mp4":
|
|
424
|
+
return "h264";
|
|
425
|
+
case "webm":
|
|
426
|
+
return "vp9";
|
|
427
|
+
case "mov":
|
|
428
|
+
return "prores";
|
|
429
|
+
case "gif":
|
|
430
|
+
case "png-sequence":
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
function normalizeChecksums(checksums) {
|
|
435
|
+
if (checksums === undefined) {
|
|
436
|
+
return [];
|
|
437
|
+
}
|
|
438
|
+
return (Array.isArray(checksums) ? checksums : [checksums]).map((checksum) => ({ ...checksum }));
|
|
439
|
+
}
|
|
440
|
+
function cloneJsonObject(value) {
|
|
441
|
+
return JSON.parse(JSON.stringify(value));
|
|
442
|
+
}
|
|
443
|
+
function assertNonEmptyString(value, name) {
|
|
444
|
+
if (value.length === 0) {
|
|
445
|
+
throw new Error(`${name} must not be empty.`);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function assertPositiveInteger(value, name) {
|
|
449
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
450
|
+
throw new Error(`${name} must be a positive integer.`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
function assertNonNegativeInteger(value, name) {
|
|
454
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
455
|
+
throw new Error(`${name} must be a non-negative integer.`);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
function assertPositiveNumber(value, name) {
|
|
459
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
460
|
+
throw new Error(`${name} must be a positive number.`);
|
|
461
|
+
}
|
|
462
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kitsra/kavio-render-worker",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Browser automation and frame capture worker for Kavio.",
|
|
5
|
+
"license": "Elastic-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@kitsra/kavio-schema": "0.1.0"
|
|
13
|
+
},
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"!dist/**/*.tsbuildinfo",
|
|
23
|
+
"!dist/self-check.*"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc -p tsconfig.json",
|
|
27
|
+
"check": "tsc -p tsconfig.json --noEmit",
|
|
28
|
+
"test": "tsc -p tsconfig.json && node dist/self-check.js"
|
|
29
|
+
}
|
|
30
|
+
}
|