alpha2 0.5.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -5
- package/THIRD_PARTY_NOTICES.md +191 -1
- package/dist/apply-edit.d.ts +124 -0
- package/dist/apply-edit.js +15 -0
- package/dist/assets.d.ts +12 -0
- package/dist/audio-layout.js +6 -3
- package/dist/audio-worklet-processor.js +27 -33
- package/dist/audio.d.ts +5 -4
- package/dist/audio.js +8 -6
- package/dist/compose.d.ts +105 -0
- package/dist/compose.js +63 -0
- package/dist/decoder-fallback.d.ts +3 -2
- package/dist/decoder-fallback.js +11 -3
- package/dist/decoder-stall.d.ts +15 -0
- package/dist/decoder-stall.js +52 -0
- package/dist/edit-data.d.ts +24 -0
- package/dist/edit-data.js +224 -0
- package/dist/edit-keys.d.ts +16 -0
- package/dist/edit-keys.js +233 -0
- package/dist/edit.d.ts +41 -8
- package/dist/edit.js +123 -30
- package/dist/export-failure.d.ts +10 -2
- package/dist/export-failure.js +55 -4
- package/dist/export-retry.d.ts +6 -1
- package/dist/export-retry.js +12 -1
- package/dist/exporter.d.ts +11 -7
- package/dist/exporter.js +10 -9
- package/dist/extensions.d.ts +86 -28
- package/dist/extensions.js +204 -61
- package/dist/extract.d.ts +65 -0
- package/dist/extract.js +291 -0
- package/dist/extractor.d.ts +151 -0
- package/dist/extractor.js +141 -0
- package/dist/float32.d.ts +2 -0
- package/dist/float32.js +10 -0
- package/dist/frame-intake.d.ts +5 -0
- package/dist/frame-intake.js +10 -3
- package/dist/generated/builtin-effects.d.ts +3 -0
- package/dist/generated/builtin-effects.js +1804 -0
- package/dist/generated/project-schema.d.ts +696 -41
- package/dist/generated/project-schema.js +67 -0
- package/dist/generated/project-shape.js +986 -383
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/json-text.js +14 -2
- package/dist/layout-values.js +7 -1
- package/dist/layout.d.ts +12 -4
- package/dist/media.d.ts +44 -11
- package/dist/media.js +256 -67
- package/dist/mediabunny.d.ts +32 -12
- package/dist/mediabunny.js +276 -90
- package/dist/player.d.ts +9 -3
- package/dist/player.js +114 -18
- package/dist/plugins.d.ts +62 -0
- package/dist/plugins.js +506 -0
- package/dist/project-data.d.ts +56 -0
- package/dist/project-data.js +765 -0
- package/dist/project.d.ts +36 -1
- package/dist/project.js +515 -42
- package/dist/robustness.d.ts +6 -33
- package/dist/robustness.js +2 -63
- package/dist/sha256.d.ts +3 -2
- package/dist/sha256.js +14 -6
- package/dist/wasm-module.d.ts +37 -0
- package/dist/wasm-module.js +47 -0
- package/dist/worker-error.d.ts +3 -2
- package/dist/worker-error.js +3 -1
- package/dist/worker.d.ts +13 -3
- package/dist/worker.js +36 -9
- package/package.json +36 -2
- package/wasm/alpha2_compose.d.ts +92 -0
- package/wasm/alpha2_compose.js +445 -0
- package/wasm/alpha2_compose_bg.wasm +0 -0
- package/wasm/alpha2_compose_bg.wasm.d.ts +17 -0
- package/wasm/alpha2_extensions.d.ts +4 -4
- package/wasm/alpha2_extensions.js +16 -4
- package/wasm/alpha2_extensions_bg.wasm +0 -0
- package/wasm/alpha2_extensions_pdf.d.ts +227 -0
- package/wasm/alpha2_extensions_pdf.js +841 -0
- package/wasm/alpha2_extensions_pdf_bg.wasm +0 -0
- package/wasm/alpha2_extensions_pdf_bg.wasm.d.ts +38 -0
- package/wasm/alpha2_wasm.d.ts +151 -18
- package/wasm/alpha2_wasm.js +403 -59
- package/wasm/alpha2_wasm_bg.wasm +0 -0
- package/wasm/alpha2_wasm_bg.wasm.d.ts +21 -6
- package/wasm/rust-crate-licenses.md +493 -54
- package/wasm/snippets/alpha2-wasm-1b02e9f302d283ba/inline3.js +7 -0
- /package/wasm/snippets/{alpha2-wasm-a21734ffde826659/inline1.js → alpha2-wasm-1b02e9f302d283ba/inline0.js} +0 -0
- /package/wasm/snippets/{alpha2-wasm-a21734ffde826659/inline2.js → alpha2-wasm-1b02e9f302d283ba/inline1.js} +0 -0
- /package/wasm/snippets/{alpha2-wasm-a21734ffde826659/inline3.js → alpha2-wasm-1b02e9f302d283ba/inline2.js} +0 -0
- /package/wasm/snippets/{alpha2-wasm-a21734ffde826659/inline0.js → alpha2-wasm-1b02e9f302d283ba/inline4.js} +0 -0
package/dist/plugins.js
ADDED
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
// Runs a plugin's wasm artifact in the browser, checked against its pinned hashes, through the
|
|
2
|
+
// `alpha2-plugin/1` ABI as an extractor for `runExtractor`. Native artifacts never run here.
|
|
3
|
+
/** The ABI this loader implements. */
|
|
4
|
+
export const PLUGIN_ABI = "alpha2-plugin/1";
|
|
5
|
+
/** Linear memory a plugin may grow to when the caller sets no cap. */
|
|
6
|
+
export const DEFAULT_PLUGIN_MEMORY_BYTES = 1024 * 1024 * 1024;
|
|
7
|
+
/** The memory a module's `i32` offsets address; a larger cap is taken as this. */
|
|
8
|
+
const MAX_PLUGIN_MEMORY_BYTES = 2 ** 31;
|
|
9
|
+
/** A plugin that was refused, did not load or failed, with the code every surface uses. */
|
|
10
|
+
export class PluginError extends Error {
|
|
11
|
+
code;
|
|
12
|
+
constructor(code, message, options) {
|
|
13
|
+
super(message, options);
|
|
14
|
+
this.name = "PluginError";
|
|
15
|
+
this.code = code;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async function sha256Hex(bytes) {
|
|
19
|
+
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
|
|
20
|
+
return [...digest].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
21
|
+
}
|
|
22
|
+
async function fetchBytes(fetcher, url, signal) {
|
|
23
|
+
let response;
|
|
24
|
+
try {
|
|
25
|
+
response = await fetcher(url, { signal });
|
|
26
|
+
}
|
|
27
|
+
catch (cause) {
|
|
28
|
+
throw new PluginError("PLUGIN_FETCH_FAILED", `${url.href}: ${String(cause)}`, {
|
|
29
|
+
cause,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
if (!response.ok)
|
|
33
|
+
throw new PluginError("PLUGIN_FETCH_FAILED", `${url.href}: HTTP ${response.status}`);
|
|
34
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
35
|
+
}
|
|
36
|
+
async function verified(fetcher, base, file, signal) {
|
|
37
|
+
const url = new URL(file.url, base);
|
|
38
|
+
const bytes = await fetchBytes(fetcher, url, signal);
|
|
39
|
+
if (bytes.byteLength !== file.bytes)
|
|
40
|
+
throw new PluginError("PLUGIN_HASH_MISMATCH", `${file.url} is ${bytes.byteLength} bytes; the manifest pins ${file.bytes}`);
|
|
41
|
+
const found = await sha256Hex(bytes);
|
|
42
|
+
if (found !== file.sha256)
|
|
43
|
+
throw new PluginError("PLUGIN_HASH_MISMATCH", `${file.url} has SHA-256 ${found}; the manifest pins ${file.sha256}`);
|
|
44
|
+
return bytes;
|
|
45
|
+
}
|
|
46
|
+
const HOST_FUNCTIONS = new Set([
|
|
47
|
+
"emit",
|
|
48
|
+
"resource_size",
|
|
49
|
+
"resource_read",
|
|
50
|
+
"fail",
|
|
51
|
+
"log",
|
|
52
|
+
]);
|
|
53
|
+
const EXPORTS = [
|
|
54
|
+
"memory",
|
|
55
|
+
"alpha2_abi",
|
|
56
|
+
"alpha2_alloc",
|
|
57
|
+
"alpha2_free",
|
|
58
|
+
"alpha2_describe",
|
|
59
|
+
"alpha2_begin",
|
|
60
|
+
"alpha2_pass",
|
|
61
|
+
"alpha2_video",
|
|
62
|
+
"alpha2_audio",
|
|
63
|
+
"alpha2_end",
|
|
64
|
+
];
|
|
65
|
+
/** Refuses a module importing anything but the host functions, or sharing its memory. */
|
|
66
|
+
function checkModule(module, bytes) {
|
|
67
|
+
for (const entry of WebAssembly.Module.imports(module)) {
|
|
68
|
+
if (entry.module !== "alpha2" ||
|
|
69
|
+
!HOST_FUNCTIONS.has(entry.name) ||
|
|
70
|
+
entry.kind !== "function")
|
|
71
|
+
throw new PluginError("PLUGIN_UNSUPPORTED_FEATURE", `the module imports ${entry.module}.${entry.name}; plugins import only the alpha2 host functions`);
|
|
72
|
+
}
|
|
73
|
+
const exported = new Set(WebAssembly.Module.exports(module).map((entry) => entry.name));
|
|
74
|
+
const missing = EXPORTS.filter((name) => !exported.has(name));
|
|
75
|
+
if (missing.length > 0)
|
|
76
|
+
throw new PluginError("PLUGIN_LOAD_FAILED", `the module does not export ${missing.join(", ")}`);
|
|
77
|
+
if (sharesMemory(bytes))
|
|
78
|
+
throw new PluginError("PLUGIN_UNSUPPORTED_FEATURE", "the module's memory is shared, which plugins may not use");
|
|
79
|
+
}
|
|
80
|
+
/** Whether the module defines a shared memory: a memory section entry with the shared flag. */
|
|
81
|
+
function sharesMemory(bytes) {
|
|
82
|
+
let at = 8;
|
|
83
|
+
const leb = () => {
|
|
84
|
+
let value = 0;
|
|
85
|
+
let shift = 0;
|
|
86
|
+
for (;;) {
|
|
87
|
+
const byte = bytes[at++] ?? 0;
|
|
88
|
+
value += (byte & 0x7f) * 2 ** shift;
|
|
89
|
+
if ((byte & 0x80) === 0)
|
|
90
|
+
return value;
|
|
91
|
+
shift += 7;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
while (at < bytes.length) {
|
|
95
|
+
const id = bytes[at++];
|
|
96
|
+
const size = leb();
|
|
97
|
+
const end = at + size;
|
|
98
|
+
if (id === 5) {
|
|
99
|
+
const count = leb();
|
|
100
|
+
for (let index = 0; index < count; index += 1) {
|
|
101
|
+
const flags = leb();
|
|
102
|
+
if ((flags & 0x02) !== 0)
|
|
103
|
+
return true;
|
|
104
|
+
leb();
|
|
105
|
+
if ((flags & 0x01) !== 0)
|
|
106
|
+
leb();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
at = end;
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
const ABSENT = 0xffffffff;
|
|
114
|
+
/** Reads a whole batch; throws a reason for the first record that does not read. */
|
|
115
|
+
export function decodeRecords(batch) {
|
|
116
|
+
const view = new DataView(batch.buffer, batch.byteOffset, batch.byteLength);
|
|
117
|
+
const text = new TextDecoder("utf-8", { fatal: true });
|
|
118
|
+
const records = [];
|
|
119
|
+
let at = 0;
|
|
120
|
+
while (at < batch.byteLength) {
|
|
121
|
+
const index = records.length;
|
|
122
|
+
const fail = (reason) => {
|
|
123
|
+
throw new PluginError("INVALID_EMIT", `record ${index}: ${reason}`);
|
|
124
|
+
};
|
|
125
|
+
if (at + 8 > batch.byteLength)
|
|
126
|
+
fail("the header ends early");
|
|
127
|
+
const tag = view.getUint8(at);
|
|
128
|
+
if (view.getUint8(at + 1) !== 0)
|
|
129
|
+
fail("the reserved header byte is not 0");
|
|
130
|
+
const output = view.getUint16(at + 2, true);
|
|
131
|
+
const size = view.getUint32(at + 4, true);
|
|
132
|
+
const start = at + 8;
|
|
133
|
+
const end = start + size;
|
|
134
|
+
if (end > batch.byteLength)
|
|
135
|
+
fail("the body is shorter than its length");
|
|
136
|
+
let cursor = start;
|
|
137
|
+
const need = (count) => {
|
|
138
|
+
if (cursor + count > end)
|
|
139
|
+
fail("the record's fields end early");
|
|
140
|
+
const from = cursor;
|
|
141
|
+
cursor += count;
|
|
142
|
+
return from;
|
|
143
|
+
};
|
|
144
|
+
const i64 = () => Number(view.getBigInt64(need(8), true));
|
|
145
|
+
const u32 = () => view.getUint32(need(4), true);
|
|
146
|
+
const f32 = () => view.getFloat32(need(4), true);
|
|
147
|
+
const string = (length) => {
|
|
148
|
+
if (length === ABSENT)
|
|
149
|
+
return undefined;
|
|
150
|
+
const from = need(length);
|
|
151
|
+
try {
|
|
152
|
+
return text.decode(batch.subarray(from, from + length));
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return fail("a text is not UTF-8");
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
if (tag === 1) {
|
|
159
|
+
const time = i64();
|
|
160
|
+
const count = u32();
|
|
161
|
+
if (count > (end - cursor) / 4)
|
|
162
|
+
fail("the sample's fields end early");
|
|
163
|
+
const masks = Array.from({ length: Math.ceil(count / 32) }, u32);
|
|
164
|
+
const values = Array.from({ length: count }, (_, value) => {
|
|
165
|
+
const read = f32();
|
|
166
|
+
return ((masks[Math.floor(value / 32)] ?? 0) >>> (value % 32)) & 1
|
|
167
|
+
? null
|
|
168
|
+
: read;
|
|
169
|
+
});
|
|
170
|
+
records.push({ kind: "sample", output, time, values });
|
|
171
|
+
}
|
|
172
|
+
else if (tag === 2) {
|
|
173
|
+
const startTick = i64();
|
|
174
|
+
const endTick = i64();
|
|
175
|
+
const textLength = u32();
|
|
176
|
+
const speakerLength = u32();
|
|
177
|
+
const word = string(textLength) ?? fail("a word has no text");
|
|
178
|
+
const speaker = string(speakerLength);
|
|
179
|
+
records.push({
|
|
180
|
+
kind: "word",
|
|
181
|
+
output,
|
|
182
|
+
start: startTick,
|
|
183
|
+
end: endTick,
|
|
184
|
+
text: word,
|
|
185
|
+
...(speaker === undefined ? {} : { speaker }),
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
else if (tag === 3) {
|
|
189
|
+
const startTick = i64();
|
|
190
|
+
const endTick = i64();
|
|
191
|
+
const flags = u32();
|
|
192
|
+
const score = f32();
|
|
193
|
+
const label = string(u32());
|
|
194
|
+
if (flags > 1)
|
|
195
|
+
fail("unknown interval flags");
|
|
196
|
+
records.push({
|
|
197
|
+
kind: "interval",
|
|
198
|
+
output,
|
|
199
|
+
start: startTick,
|
|
200
|
+
end: endTick,
|
|
201
|
+
...(label === undefined ? {} : { label }),
|
|
202
|
+
...(flags === 1 ? { score } : {}),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
else
|
|
206
|
+
fail("unknown record tag");
|
|
207
|
+
if (cursor !== end)
|
|
208
|
+
fail("the body is longer than its fields");
|
|
209
|
+
at = end;
|
|
210
|
+
}
|
|
211
|
+
return records;
|
|
212
|
+
}
|
|
213
|
+
const MAX_LOG_BYTES = 2048;
|
|
214
|
+
const LOG_BUDGET_BYTES = 64 * 1024;
|
|
215
|
+
const MAX_FAIL_BYTES = 4096;
|
|
216
|
+
/** How many of `bytes` to keep for at most `max`, not splitting a UTF-8 sequence. */
|
|
217
|
+
function utf8Cut(bytes, max) {
|
|
218
|
+
let end = Math.min(bytes.length, max);
|
|
219
|
+
if (end < bytes.length)
|
|
220
|
+
while (end > 0 && ((bytes[end] ?? 0) & 0xc0) === 0x80)
|
|
221
|
+
end -= 1;
|
|
222
|
+
return end;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* One run of the module, with an instance, its host functions and what they collected. It is
|
|
226
|
+
* instantiated asynchronously because browsers refuse large synchronous instantiation.
|
|
227
|
+
*/
|
|
228
|
+
class Run {
|
|
229
|
+
#exports;
|
|
230
|
+
#descriptor;
|
|
231
|
+
#memoryBytes;
|
|
232
|
+
#records = [];
|
|
233
|
+
#undecodable;
|
|
234
|
+
#failure;
|
|
235
|
+
#logLeft = LOG_BUDGET_BYTES;
|
|
236
|
+
#logExhausted = false;
|
|
237
|
+
constructor(descriptor, memoryBytes) {
|
|
238
|
+
this.#descriptor = descriptor;
|
|
239
|
+
this.#memoryBytes = memoryBytes;
|
|
240
|
+
}
|
|
241
|
+
static async start(module, descriptor, resources, memoryBytes) {
|
|
242
|
+
const run = new Run(descriptor, memoryBytes);
|
|
243
|
+
const instance = await WebAssembly.instantiate(module, run.#imports(resources));
|
|
244
|
+
run.#exports = instance.exports;
|
|
245
|
+
if (run.#exports.alpha2_abi() !== 1)
|
|
246
|
+
throw new PluginError("PLUGIN_ABI_UNSUPPORTED", `the module implements alpha2-plugin/${run.#exports.alpha2_abi()}`);
|
|
247
|
+
return run;
|
|
248
|
+
}
|
|
249
|
+
#imports(resources) {
|
|
250
|
+
const bytes = (ptr, len) => new Uint8Array(this.#exports.memory.buffer, ptr, len);
|
|
251
|
+
const utf8 = (ptr, len) => new TextDecoder().decode(bytes(ptr, len));
|
|
252
|
+
// Mirrors host-core: at most `max` bytes are decoded, one more read to see a split sequence.
|
|
253
|
+
const bounded = (ptr, len, max) => {
|
|
254
|
+
const read = bytes(ptr, Math.max(0, Math.min(len, max + 1)));
|
|
255
|
+
return read.subarray(0, utf8Cut(read, max));
|
|
256
|
+
};
|
|
257
|
+
return {
|
|
258
|
+
alpha2: {
|
|
259
|
+
emit: (ptr, len) => {
|
|
260
|
+
try {
|
|
261
|
+
// A loop, since spreading a large batch into arguments can pass the argument limit.
|
|
262
|
+
for (const record of decodeRecords(bytes(ptr, len).slice()))
|
|
263
|
+
this.#records.push(record);
|
|
264
|
+
return 0;
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
this.#undecodable =
|
|
268
|
+
error instanceof PluginError
|
|
269
|
+
? error
|
|
270
|
+
: new PluginError("INVALID_EMIT", String(error));
|
|
271
|
+
return 1;
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
resource_size: (name, nameLength) => {
|
|
275
|
+
const found = resources.get(utf8(name, nameLength));
|
|
276
|
+
return found === undefined ? -1n : BigInt(found.byteLength);
|
|
277
|
+
},
|
|
278
|
+
resource_read: (name, nameLength, dst, dstLength) => {
|
|
279
|
+
const found = resources.get(utf8(name, nameLength));
|
|
280
|
+
if (found === undefined || found.byteLength !== dstLength)
|
|
281
|
+
return 1;
|
|
282
|
+
bytes(dst, dstLength).set(found);
|
|
283
|
+
return 0;
|
|
284
|
+
},
|
|
285
|
+
fail: (ptr, len) => {
|
|
286
|
+
this.#failure = new TextDecoder().decode(bounded(ptr, len, MAX_FAIL_BYTES));
|
|
287
|
+
},
|
|
288
|
+
log: (_level, ptr, len) => {
|
|
289
|
+
if (this.#logExhausted)
|
|
290
|
+
return;
|
|
291
|
+
const line = bounded(ptr, len, MAX_LOG_BYTES);
|
|
292
|
+
if (line.byteLength > this.#logLeft) {
|
|
293
|
+
this.#logExhausted = true;
|
|
294
|
+
console.warn(`plugin: the plugin logged more than ${LOG_BUDGET_BYTES} bytes; its further lines are dropped`);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
this.#logLeft -= line.byteLength;
|
|
298
|
+
console.warn(`plugin: ${new TextDecoder().decode(line)}`);
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
/** The descriptor JSON the module itself returns. */
|
|
304
|
+
describe() {
|
|
305
|
+
try {
|
|
306
|
+
const packed = BigInt.asUintN(64, this.#exports.alpha2_describe());
|
|
307
|
+
const ptr = Number(packed & 0xffffffffn);
|
|
308
|
+
const len = Number(packed >> 32n);
|
|
309
|
+
return new TextDecoder().decode(new Uint8Array(this.#exports.memory.buffer, ptr, len));
|
|
310
|
+
}
|
|
311
|
+
catch (cause) {
|
|
312
|
+
throw new PluginError("PLUGIN_DESCRIPTOR_MISMATCH", `describe failed: ${String(cause)}`, { cause });
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/** Runs a guest call outside a step, reporting a trap or an out-of-range pointer as one. */
|
|
316
|
+
#guest(run) {
|
|
317
|
+
try {
|
|
318
|
+
return run();
|
|
319
|
+
}
|
|
320
|
+
catch (cause) {
|
|
321
|
+
throw new PluginError("PLUGIN_TRAPPED", `the plugin trapped: ${String(cause)}`, { cause });
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/** Runs a call, turning its status, a trap or the memory cap into the run's outcome. */
|
|
325
|
+
call(run) {
|
|
326
|
+
let status;
|
|
327
|
+
try {
|
|
328
|
+
status = run();
|
|
329
|
+
}
|
|
330
|
+
catch (cause) {
|
|
331
|
+
throw new PluginError("PLUGIN_TRAPPED", `the plugin trapped: ${String(cause)}`, { cause });
|
|
332
|
+
}
|
|
333
|
+
if (this.#exports.memory.buffer.byteLength > this.#memoryBytes)
|
|
334
|
+
throw new PluginError("PLUGIN_LIMIT", `the plugin needed more than ${this.#memoryBytes} bytes of memory`);
|
|
335
|
+
const records = this.#records;
|
|
336
|
+
this.#records = [];
|
|
337
|
+
if (status === 0)
|
|
338
|
+
return records;
|
|
339
|
+
if (status === 1) {
|
|
340
|
+
if (this.#undecodable !== undefined)
|
|
341
|
+
throw this.#undecodable;
|
|
342
|
+
throw new PluginError("EXTRACTOR_FAILED", this.#failure ?? "the plugin failed without saying why");
|
|
343
|
+
}
|
|
344
|
+
throw new PluginError("PLUGIN_TRAPPED", status === 2
|
|
345
|
+
? "the plugin panicked; it caught the panic at its boundary"
|
|
346
|
+
: `the plugin returned the unknown status ${status}`);
|
|
347
|
+
}
|
|
348
|
+
/** Copies `data` into memory the module allocates, calls `run` with it, and frees it. */
|
|
349
|
+
withBytes(data, align, run) {
|
|
350
|
+
const ptr = this.#guest(() => this.#exports.alpha2_alloc(data.byteLength, align));
|
|
351
|
+
if (ptr === 0 && data.byteLength > 0)
|
|
352
|
+
throw new PluginError("PLUGIN_LIMIT", "the plugin could not allocate the call's input");
|
|
353
|
+
this.#guest(() => new Uint8Array(this.#exports.memory.buffer, ptr, data.byteLength).set(data));
|
|
354
|
+
const records = this.call(() => run(ptr));
|
|
355
|
+
this.#guest(() => this.#exports.alpha2_free(ptr, data.byteLength, align));
|
|
356
|
+
return records;
|
|
357
|
+
}
|
|
358
|
+
get exports() {
|
|
359
|
+
return this.#exports;
|
|
360
|
+
}
|
|
361
|
+
/** Hands `records` to `out` by output name. */
|
|
362
|
+
deliver(records, out) {
|
|
363
|
+
for (const record of records) {
|
|
364
|
+
const output = this.#descriptor.outputs[record.output]?.name;
|
|
365
|
+
if (output === undefined)
|
|
366
|
+
throw new PluginError("INVALID_EMIT", `the plugin emitted to output ${record.output}, which it does not declare`);
|
|
367
|
+
if (record.kind === "sample")
|
|
368
|
+
out.sample(output, record.time, record.values);
|
|
369
|
+
else if (record.kind === "word")
|
|
370
|
+
out.word(output, record.start, record.end, record.text, record.speaker);
|
|
371
|
+
else
|
|
372
|
+
out.interval(output, record.start, record.end, record.label, record.score);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
function nothingEmitted(records, step) {
|
|
377
|
+
if (records.length > 0)
|
|
378
|
+
throw new PluginError("INVALID_EMIT", `the plugin emitted during ${step}, which takes no emits`);
|
|
379
|
+
}
|
|
380
|
+
/** The run context as the ABI's `begin` takes it. */
|
|
381
|
+
function abiContext(context, params) {
|
|
382
|
+
const raw = context;
|
|
383
|
+
return JSON.stringify({
|
|
384
|
+
range: context.range,
|
|
385
|
+
passes: context.passes,
|
|
386
|
+
from: context.from,
|
|
387
|
+
video: context.video,
|
|
388
|
+
audio: context.audio === null
|
|
389
|
+
? null
|
|
390
|
+
: {
|
|
391
|
+
rate: context.audio.rate,
|
|
392
|
+
channels: context.audio.channels,
|
|
393
|
+
block_frames: context.audio.blockFrames,
|
|
394
|
+
},
|
|
395
|
+
frame_rate: raw.frameRate ?? null,
|
|
396
|
+
params,
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Loads the plugin whose manifest is at `manifest` (a URL, absolute or relative to the page) and
|
|
401
|
+
* whose bytes have SHA-256 `sha256`, with its wasm artifact and resources, each checked against
|
|
402
|
+
* the hash and length the manifest pins.
|
|
403
|
+
*
|
|
404
|
+
* Rejects with a {@link PluginError} whose code is `PLUGIN_FETCH_FAILED`, `PLUGIN_HASH_MISMATCH`,
|
|
405
|
+
* `PLUGIN_ABI_UNSUPPORTED`, `PLUGIN_NO_ARTIFACT` (no wasm artifact), `PLUGIN_UNSUPPORTED_FEATURE`,
|
|
406
|
+
* `PLUGIN_LOAD_FAILED` or `PLUGIN_DESCRIPTOR_MISMATCH`.
|
|
407
|
+
*/
|
|
408
|
+
export async function loadPlugin(manifest, sha256, options = {}) {
|
|
409
|
+
const fetcher = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
410
|
+
const base = new URL(manifest, globalThis.location?.href);
|
|
411
|
+
const text = await fetchBytes(fetcher, base, options.signal);
|
|
412
|
+
const found = await sha256Hex(text);
|
|
413
|
+
if (found !== sha256.toLowerCase())
|
|
414
|
+
throw new PluginError("PLUGIN_HASH_MISMATCH", `the manifest has SHA-256 ${found}; the caller pins ${sha256}`);
|
|
415
|
+
let parsed;
|
|
416
|
+
try {
|
|
417
|
+
parsed = JSON.parse(new TextDecoder().decode(text));
|
|
418
|
+
}
|
|
419
|
+
catch (cause) {
|
|
420
|
+
throw new PluginError("PLUGIN_LOAD_FAILED", "the manifest is not JSON", {
|
|
421
|
+
cause,
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
if (parsed.abi !== PLUGIN_ABI)
|
|
425
|
+
throw new PluginError("PLUGIN_ABI_UNSUPPORTED", `the plugin needs ABI ${String(parsed.abi)}; this build implements ${PLUGIN_ABI}`);
|
|
426
|
+
if (parsed.kind !== "extractor" || parsed.id !== parsed.descriptor?.id)
|
|
427
|
+
throw new PluginError("PLUGIN_LOAD_FAILED", "the manifest is not an extractor whose id is its descriptor's");
|
|
428
|
+
const artifact = parsed.artifacts.find((entry) => entry.target === "wasm32" && entry.type === "wasm");
|
|
429
|
+
if (artifact === undefined)
|
|
430
|
+
throw new PluginError("PLUGIN_NO_ARTIFACT", `${parsed.id} has no wasm artifact; it offers ${parsed.artifacts.map((entry) => `${entry.target} (${entry.type})`).join(", ")}`);
|
|
431
|
+
const bytes = await verified(fetcher, base, artifact, options.signal);
|
|
432
|
+
const resources = new Map();
|
|
433
|
+
for (const resource of parsed.resources ?? [])
|
|
434
|
+
resources.set(resource.name, await verified(fetcher, base, resource, options.signal));
|
|
435
|
+
let module;
|
|
436
|
+
try {
|
|
437
|
+
module = await WebAssembly.compile(bytes);
|
|
438
|
+
}
|
|
439
|
+
catch (cause) {
|
|
440
|
+
throw new PluginError("PLUGIN_LOAD_FAILED", `the module is not valid: ${String(cause)}`, { cause });
|
|
441
|
+
}
|
|
442
|
+
checkModule(module, bytes);
|
|
443
|
+
const memoryBytes = Math.min(options.memoryBytes ?? DEFAULT_PLUGIN_MEMORY_BYTES, MAX_PLUGIN_MEMORY_BYTES);
|
|
444
|
+
const described = (await Run.start(module, parsed.descriptor, resources, memoryBytes)).describe();
|
|
445
|
+
if (!sameJson(JSON.parse(described), parsed.descriptor))
|
|
446
|
+
throw new PluginError("PLUGIN_DESCRIPTOR_MISMATCH", `the artifact ${artifact.url} describes another extractor than the manifest`);
|
|
447
|
+
const descriptor = parsed.descriptor;
|
|
448
|
+
return {
|
|
449
|
+
id: parsed.id,
|
|
450
|
+
version: parsed.version,
|
|
451
|
+
artifactSha256: artifact.sha256,
|
|
452
|
+
descriptor,
|
|
453
|
+
extractor(params = {}) {
|
|
454
|
+
let run;
|
|
455
|
+
const current = () => {
|
|
456
|
+
if (run === undefined)
|
|
457
|
+
throw new PluginError("PLUGIN_LOAD_FAILED", "the run has not begun");
|
|
458
|
+
return run;
|
|
459
|
+
};
|
|
460
|
+
return {
|
|
461
|
+
describe: () => descriptor,
|
|
462
|
+
async begin(context) {
|
|
463
|
+
run = await Run.start(module, descriptor, resources, memoryBytes);
|
|
464
|
+
const live = run;
|
|
465
|
+
const json = new TextEncoder().encode(abiContext(context, params));
|
|
466
|
+
const records = live.withBytes(json, 1, (ptr) => live.exports.alpha2_begin(ptr, json.byteLength));
|
|
467
|
+
nothingEmitted(records, "begin");
|
|
468
|
+
},
|
|
469
|
+
pass({ pass, direction }) {
|
|
470
|
+
const live = current();
|
|
471
|
+
nothingEmitted(live.call(() => live.exports.alpha2_pass(pass, direction === "backward" ? 1 : 0)), "a pass start");
|
|
472
|
+
},
|
|
473
|
+
video(frame, out) {
|
|
474
|
+
const live = current();
|
|
475
|
+
live.deliver(live.withBytes(frame.data, 1, (ptr) => live.exports.alpha2_video(BigInt(frame.time), frame.width, frame.height, frame.format === "luma8" ? 1 : 0, ptr, frame.data.byteLength)), out);
|
|
476
|
+
},
|
|
477
|
+
audio(block, out) {
|
|
478
|
+
const live = current();
|
|
479
|
+
const data = new Uint8Array(block.samples.buffer, block.samples.byteOffset, block.samples.byteLength);
|
|
480
|
+
live.deliver(live.withBytes(data, 4, (ptr) => live.exports.alpha2_audio(BigInt(block.start), BigInt(block.startSample), block.frames, block.rate, block.channels, ptr, block.samples.length)), out);
|
|
481
|
+
},
|
|
482
|
+
end(out) {
|
|
483
|
+
const live = current();
|
|
484
|
+
live.deliver(live.call(() => live.exports.alpha2_end()), out);
|
|
485
|
+
run = undefined;
|
|
486
|
+
},
|
|
487
|
+
};
|
|
488
|
+
},
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
function sameJson(a, b) {
|
|
492
|
+
if (Array.isArray(a) && Array.isArray(b))
|
|
493
|
+
return (a.length === b.length &&
|
|
494
|
+
a.every((item, index) => sameJson(item, b[index])));
|
|
495
|
+
if (a !== null &&
|
|
496
|
+
b !== null &&
|
|
497
|
+
typeof a === "object" &&
|
|
498
|
+
typeof b === "object") {
|
|
499
|
+
const left = a;
|
|
500
|
+
const right = b;
|
|
501
|
+
const keys = Object.keys(left);
|
|
502
|
+
return (keys.length === Object.keys(right).length &&
|
|
503
|
+
keys.every((key) => key in right && sameJson(left[key], right[key])));
|
|
504
|
+
}
|
|
505
|
+
return a === b;
|
|
506
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
type Json = Record<string, unknown>;
|
|
2
|
+
/** The type of a built-in effect's parameter, or `undefined` for a host effect. */
|
|
3
|
+
export type ParamType = (effectId: string, name: string) => string | undefined;
|
|
4
|
+
/** What holds a data reference. */
|
|
5
|
+
export type DataOwner = {
|
|
6
|
+
readonly kind: "clip";
|
|
7
|
+
readonly clip: string;
|
|
8
|
+
} | {
|
|
9
|
+
readonly kind: "track";
|
|
10
|
+
readonly track: string;
|
|
11
|
+
} | {
|
|
12
|
+
readonly kind: "transition";
|
|
13
|
+
readonly track: string;
|
|
14
|
+
readonly from: string;
|
|
15
|
+
readonly to: string;
|
|
16
|
+
} | {
|
|
17
|
+
readonly kind: "node";
|
|
18
|
+
readonly composition: string;
|
|
19
|
+
readonly node: string;
|
|
20
|
+
} | {
|
|
21
|
+
readonly kind: "master";
|
|
22
|
+
} | {
|
|
23
|
+
readonly kind: "source";
|
|
24
|
+
readonly source: string;
|
|
25
|
+
};
|
|
26
|
+
/** One place of a project that reads a data set. */
|
|
27
|
+
export interface DataReference {
|
|
28
|
+
/** The data set's id. */
|
|
29
|
+
readonly data: string;
|
|
30
|
+
readonly owner: DataOwner;
|
|
31
|
+
/**
|
|
32
|
+
* The property path in the syntax `bake` targets use: `visual.position`, `masks[0].feather`,
|
|
33
|
+
* `effect.params.radius`, `visual.correction`, or `captions` for a track's caption words.
|
|
34
|
+
*/
|
|
35
|
+
readonly property: string;
|
|
36
|
+
/** The clip or composition node a source-anchored binding reads through. */
|
|
37
|
+
readonly via?: {
|
|
38
|
+
readonly clip: string;
|
|
39
|
+
} | {
|
|
40
|
+
readonly node: string;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** `reference` as text in the lint path syntax, such as `clip c1 visual.position`. */
|
|
44
|
+
export declare function describeDataReference(reference: DataReference): string;
|
|
45
|
+
/**
|
|
46
|
+
* Every place of `project` that reads a data set, in document order: bindings, corrections and
|
|
47
|
+
* caption words. References to sets the project lacks are listed too.
|
|
48
|
+
*/
|
|
49
|
+
export declare function dataReferences(project: object): DataReference[];
|
|
50
|
+
/**
|
|
51
|
+
* Problems of the project's data sets and of every binding, correction and caption words
|
|
52
|
+
* reference. `validFingerprint` is the validator's own fingerprint check. A set `sealed` reports
|
|
53
|
+
* as unchangeable skips its own checks once it has passed them with the same source duration.
|
|
54
|
+
*/
|
|
55
|
+
export declare function dataProblems(project: Json, paramType: ParamType, validFingerprint: (fingerprint: unknown) => boolean, sealed?: (value: unknown) => boolean): string[];
|
|
56
|
+
export {};
|