alpha2 0.6.2 → 0.7.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 +3 -3
- package/dist/effects.d.ts +3 -2
- package/dist/export-failure.d.ts +6 -0
- package/dist/export-failure.js +18 -1
- package/dist/export-retry.d.ts +2 -1
- package/dist/export-retry.js +12 -5
- package/dist/extract.d.ts +0 -5
- package/dist/extract.js +5 -12
- package/dist/extractor.d.ts +16 -3
- package/dist/extractor.js +2 -1
- package/dist/generated/project-schema.d.ts +31 -1
- package/dist/generated/project-schema.js +5 -0
- package/dist/generated/project-shape.js +14 -1
- package/dist/gpu-recovery.d.ts +13 -5
- package/dist/gpu-recovery.js +19 -8
- package/dist/hdr-frames.js +2 -0
- package/dist/media-source.d.ts +3 -0
- package/dist/media-source.js +13 -0
- package/dist/media.d.ts +12 -1
- package/dist/media.js +15 -1
- package/dist/mediabunny.d.ts +4 -1
- package/dist/mediabunny.js +22 -23
- package/dist/player.js +2 -0
- package/dist/plugin-manifest.d.ts +119 -0
- package/dist/plugin-manifest.js +583 -0
- package/dist/plugin-supplied.d.ts +55 -0
- package/dist/plugin-supplied.js +221 -0
- package/dist/plugins.d.ts +87 -9
- package/dist/plugins.js +196 -47
- package/dist/project-data.d.ts +19 -0
- package/dist/project-data.js +210 -7
- package/dist/project.d.ts +1 -1
- package/dist/project.js +1 -1
- package/dist/sha256.d.ts +1 -1
- package/package.json +1 -1
- package/wasm/alpha2_compose_bg.wasm +0 -0
- package/wasm/alpha2_extensions_bg.wasm +0 -0
- package/wasm/alpha2_extensions_pdf_bg.wasm +0 -0
- package/wasm/alpha2_wasm.d.ts +14 -12
- package/wasm/alpha2_wasm.js +54 -52
- package/wasm/alpha2_wasm_bg.wasm +0 -0
- package/wasm/alpha2_wasm_bg.wasm.d.ts +6 -6
- /package/wasm/snippets/{alpha2-wasm-d984447de3610be7/inline4.js → alpha2-wasm-7cccf477e10e3d0d/inline0.js} +0 -0
- /package/wasm/snippets/{alpha2-wasm-d984447de3610be7/inline0.js → alpha2-wasm-7cccf477e10e3d0d/inline1.js} +0 -0
- /package/wasm/snippets/{alpha2-wasm-d984447de3610be7/inline1.js → alpha2-wasm-7cccf477e10e3d0d/inline2.js} +0 -0
- /package/wasm/snippets/{alpha2-wasm-d984447de3610be7/inline2.js → alpha2-wasm-7cccf477e10e3d0d/inline3.js} +0 -0
- /package/wasm/snippets/{alpha2-wasm-d984447de3610be7/inline3.js → alpha2-wasm-7cccf477e10e3d0d/inline4.js} +0 -0
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
// Plugin manifests, checked as `alpha2_engine::extract::PluginManifest::from_json` checks them.
|
|
2
|
+
import { MAX_DATA_CHANNELS, MAX_DATA_LABEL_BYTES, MAX_SAMPLE_RATE, MIN_SAMPLE_RATE, } from "./generated/project-schema.js";
|
|
3
|
+
/** The ABI this build implements. */
|
|
4
|
+
export const PLUGIN_ABI = "alpha2-plugin/1";
|
|
5
|
+
/** Mirrors `alpha2_engine::extract::MAX_MANIFEST_BYTES`: the longest manifest, in UTF-8 bytes. */
|
|
6
|
+
export const MAX_MANIFEST_BYTES = 1024 * 1024;
|
|
7
|
+
/** Mirrors `alpha2_engine::extract::MAX_PLUGIN_ARTIFACTS`. */
|
|
8
|
+
export const MAX_PLUGIN_ARTIFACTS = 64;
|
|
9
|
+
/** Mirrors `alpha2_engine::extract::MAX_PLUGIN_RESOURCES`. */
|
|
10
|
+
export const MAX_PLUGIN_RESOURCES = 64;
|
|
11
|
+
/** Mirrors `alpha2_engine::extract::MAX_PLUGIN_SLOTS`, counted within the resources. */
|
|
12
|
+
export const MAX_PLUGIN_SLOTS = 16;
|
|
13
|
+
/** Mirrors `alpha2_engine::extract::MAX_PLUGIN_ARTIFACT_BYTES`. */
|
|
14
|
+
export const MAX_PLUGIN_ARTIFACT_BYTES = 256 * 1024 * 1024;
|
|
15
|
+
/** Mirrors `alpha2_engine::extract::MAX_PLUGIN_RESOURCE_BYTES`, one pinned resource. */
|
|
16
|
+
export const MAX_PLUGIN_RESOURCE_BYTES = 2 * 1024 * 1024 * 1024;
|
|
17
|
+
/** Mirrors `alpha2_engine::extract::MAX_PLUGIN_RESOURCES_TOTAL_BYTES`, pinned resources only. */
|
|
18
|
+
export const MAX_PLUGIN_RESOURCES_TOTAL_BYTES = 4 * 1024 * 1024 * 1024;
|
|
19
|
+
/** Mirrors `alpha2_engine::extract::MAX_SLOT_NAME_BYTES`. */
|
|
20
|
+
export const MAX_SLOT_NAME_BYTES = 64;
|
|
21
|
+
/** Mirrors `alpha2_engine::extract::MAX_SLOT_ACCEPT`. */
|
|
22
|
+
export const MAX_SLOT_ACCEPT = 16;
|
|
23
|
+
/** Mirrors `alpha2_engine::extract::MAX_SLOT_ACCEPT_BYTES`. */
|
|
24
|
+
export const MAX_SLOT_ACCEPT_BYTES = 64;
|
|
25
|
+
/** Mirrors `alpha2_engine::extract::MAX_ABOUT_NAME_CHARS`, in Unicode code points. */
|
|
26
|
+
export const MAX_ABOUT_NAME_CHARS = 80;
|
|
27
|
+
/** Mirrors `alpha2_engine::extract::MAX_ABOUT_SUMMARY_CHARS`. */
|
|
28
|
+
export const MAX_ABOUT_SUMMARY_CHARS = 400;
|
|
29
|
+
/** Mirrors `alpha2_engine::extract::MAX_ABOUT_AUTHOR_CHARS`. */
|
|
30
|
+
export const MAX_ABOUT_AUTHOR_CHARS = 80;
|
|
31
|
+
/** Mirrors `alpha2_engine::extract::MAX_ABOUT_HOMEPAGE_CHARS`. */
|
|
32
|
+
export const MAX_ABOUT_HOMEPAGE_CHARS = 2048;
|
|
33
|
+
/** Mirrors `alpha2_engine::extract::MAX_ABOUT_LICENSE_CHARS`. */
|
|
34
|
+
export const MAX_ABOUT_LICENSE_CHARS = 256;
|
|
35
|
+
/** Mirrors `alpha2_engine::extract::MAX_OUTPUT_LABEL_CHARS`. */
|
|
36
|
+
export const MAX_OUTPUT_LABEL_CHARS = 80;
|
|
37
|
+
/** Mirrors `alpha2_engine::extract::MAX_OUTPUT_DESCRIPTION_CHARS`. */
|
|
38
|
+
export const MAX_OUTPUT_DESCRIPTION_CHARS = 400;
|
|
39
|
+
/** Mirrors `alpha2_engine::extract::MAX_EXTRACTOR_OUTPUTS`. */
|
|
40
|
+
export const MAX_EXTRACTOR_OUTPUTS = 32;
|
|
41
|
+
const DEFAULT_MAX_SIZE = 960;
|
|
42
|
+
const DEFAULT_BLOCK_FRAMES = 1024;
|
|
43
|
+
const BLOCK_FRAMES = { min: 64, max: 65_536 };
|
|
44
|
+
const MAX_U32 = 0xffff_ffff;
|
|
45
|
+
const SPDX_MAX_NESTING = 32;
|
|
46
|
+
/**
|
|
47
|
+
* A plugin that was refused, did not load or failed, with the code every surface uses.
|
|
48
|
+
* `details` is what a caller reads besides the code: `{ slot, reason }` for
|
|
49
|
+
* `PLUGIN_RESOURCE_INVALID`.
|
|
50
|
+
*/
|
|
51
|
+
export class PluginError extends Error {
|
|
52
|
+
code;
|
|
53
|
+
details;
|
|
54
|
+
constructor(code, message, options) {
|
|
55
|
+
super(message, options);
|
|
56
|
+
this.name = "PluginError";
|
|
57
|
+
this.code = code;
|
|
58
|
+
if (options?.details !== undefined)
|
|
59
|
+
this.details = options.details;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Mirrors `ManifestError::Invalid`. */
|
|
63
|
+
class Invalid extends Error {
|
|
64
|
+
}
|
|
65
|
+
const fail = (why) => {
|
|
66
|
+
throw new Invalid(why);
|
|
67
|
+
};
|
|
68
|
+
function isRecord(value) {
|
|
69
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
70
|
+
}
|
|
71
|
+
function fields(value, path, allowed, required) {
|
|
72
|
+
if (!isRecord(value))
|
|
73
|
+
return fail(`${path} is not an object`);
|
|
74
|
+
for (const key of Object.keys(value))
|
|
75
|
+
if (!allowed.includes(key))
|
|
76
|
+
fail(`${path} has unknown field ${key}`);
|
|
77
|
+
for (const key of required)
|
|
78
|
+
if (value[key] === undefined)
|
|
79
|
+
fail(`${path} lacks ${key}`);
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
function optional(value) {
|
|
83
|
+
return value === null ? undefined : value;
|
|
84
|
+
}
|
|
85
|
+
function text(value, path) {
|
|
86
|
+
return typeof value === "string" ? value : fail(`${path} is not a string`);
|
|
87
|
+
}
|
|
88
|
+
function integer(value, path, max) {
|
|
89
|
+
return typeof value === "number" &&
|
|
90
|
+
Number.isSafeInteger(value) &&
|
|
91
|
+
value >= 0 &&
|
|
92
|
+
value <= max
|
|
93
|
+
? value
|
|
94
|
+
: fail(`${path} is not an integer from 0 to ${max}`);
|
|
95
|
+
}
|
|
96
|
+
function oneOf(value, path, values) {
|
|
97
|
+
return values.includes(value)
|
|
98
|
+
? value
|
|
99
|
+
: fail(`${path} is not one of ${values.join(", ")}`);
|
|
100
|
+
}
|
|
101
|
+
function list(value, path) {
|
|
102
|
+
return Array.isArray(value) ? value : fail(`${path} is not a list`);
|
|
103
|
+
}
|
|
104
|
+
const encoder = new TextEncoder();
|
|
105
|
+
const utf8Length = (value) => encoder.encode(value).length;
|
|
106
|
+
const CONTROL = /\p{Cc}/u;
|
|
107
|
+
/** Mirrors `plain_text`: `min` to `max` code points, no control character. */
|
|
108
|
+
function plainText(value, path, min, max) {
|
|
109
|
+
const read = text(value, path);
|
|
110
|
+
const length = [...read].length;
|
|
111
|
+
if (length < min || length > max || CONTROL.test(read))
|
|
112
|
+
fail(`${path} is not ${min} to ${max} characters without control characters`);
|
|
113
|
+
return read;
|
|
114
|
+
}
|
|
115
|
+
/** Mirrors `semver`. */
|
|
116
|
+
function semver(version) {
|
|
117
|
+
const plus = version.indexOf("+");
|
|
118
|
+
const rest = plus < 0 ? version : version.slice(0, plus);
|
|
119
|
+
const build = plus < 0 ? undefined : version.slice(plus + 1);
|
|
120
|
+
const dash = rest.indexOf("-");
|
|
121
|
+
const core = dash < 0 ? rest : rest.slice(0, dash);
|
|
122
|
+
const pre = dash < 0 ? undefined : rest.slice(dash + 1);
|
|
123
|
+
const identifiers = (part) => part.split(".").every((item) => /^[A-Za-z0-9-]+$/.test(item));
|
|
124
|
+
const numbers = core.split(".");
|
|
125
|
+
return (numbers.length === 3 &&
|
|
126
|
+
numbers.every((part) => /^(0|[1-9][0-9]*)$/.test(part)) &&
|
|
127
|
+
(pre === undefined || identifiers(pre)) &&
|
|
128
|
+
(build === undefined || identifiers(build)));
|
|
129
|
+
}
|
|
130
|
+
/** Mirrors `reference`: a path under the manifest's directory or an `https:` URL. */
|
|
131
|
+
function checkReference(url) {
|
|
132
|
+
if (url.length === 0)
|
|
133
|
+
fail("a URL is empty");
|
|
134
|
+
const scheme = /^([A-Za-z][A-Za-z0-9+.-]*):/.exec(url)?.[1];
|
|
135
|
+
if (scheme === "https")
|
|
136
|
+
return;
|
|
137
|
+
if (scheme !== undefined)
|
|
138
|
+
fail(scheme.length === 1
|
|
139
|
+
? `${url}: a drive-prefixed path is not under the manifest`
|
|
140
|
+
: `${url}: only https: URLs and paths under the manifest are allowed`);
|
|
141
|
+
if (/[\\\0]/.test(url))
|
|
142
|
+
fail(`${url}: a path takes neither backslashes nor NUL`);
|
|
143
|
+
if (url.startsWith("/"))
|
|
144
|
+
fail(`${url}: an absolute path is not under the manifest`);
|
|
145
|
+
let depth = 0;
|
|
146
|
+
for (const segment of url.split(/[?#]/)[0].split("/")) {
|
|
147
|
+
if (segment === "" || segment === ".")
|
|
148
|
+
continue;
|
|
149
|
+
if (segment === "..") {
|
|
150
|
+
depth -= 1;
|
|
151
|
+
if (depth < 0)
|
|
152
|
+
fail(`${url}: the path climbs above the manifest`);
|
|
153
|
+
}
|
|
154
|
+
else
|
|
155
|
+
depth += 1;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function checkFile(entry, path) {
|
|
159
|
+
checkReference(text(entry.url, `${path}.url`));
|
|
160
|
+
if (!/^[0-9a-f]{64}$/.test(text(entry.sha256, `${path}.sha256`)))
|
|
161
|
+
fail(`${String(entry.url)}: sha256 is not 64 lowercase hexadecimal digits`);
|
|
162
|
+
}
|
|
163
|
+
/** Mirrors `is_slot_name`. */
|
|
164
|
+
function isSlotName(name) {
|
|
165
|
+
return (utf8Length(name) <= MAX_SLOT_NAME_BYTES &&
|
|
166
|
+
/^[A-Za-z0-9_-][A-Za-z0-9._-]*$/.test(name));
|
|
167
|
+
}
|
|
168
|
+
function spdxId(token) {
|
|
169
|
+
return (token !== undefined &&
|
|
170
|
+
!["AND", "OR", "WITH", "(", ")"].includes(token) &&
|
|
171
|
+
/^[A-Za-z0-9.:-]+$/.test(token));
|
|
172
|
+
}
|
|
173
|
+
/** Mirrors `spdx_expression` (SPDX 2.3 annex D); the ids are not looked up. */
|
|
174
|
+
function spdxExpression(expression) {
|
|
175
|
+
const tokens = expression
|
|
176
|
+
.replaceAll("(", " ( ")
|
|
177
|
+
.replaceAll(")", " ) ")
|
|
178
|
+
.split(/[\t\n\f\r ]+/)
|
|
179
|
+
.filter((token) => token !== "");
|
|
180
|
+
let at = 0;
|
|
181
|
+
const term = (depth) => {
|
|
182
|
+
const token = tokens[at++];
|
|
183
|
+
if (token === undefined)
|
|
184
|
+
return false;
|
|
185
|
+
if (token === "(") {
|
|
186
|
+
if (depth >= SPDX_MAX_NESTING || !or(depth + 1) || tokens[at] !== ")")
|
|
187
|
+
return false;
|
|
188
|
+
at += 1;
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
if (!spdxId(token.endsWith("+") ? token.slice(0, -1) : token))
|
|
192
|
+
return false;
|
|
193
|
+
if (tokens[at] === "WITH") {
|
|
194
|
+
at += 1;
|
|
195
|
+
return spdxId(tokens[at++]);
|
|
196
|
+
}
|
|
197
|
+
return true;
|
|
198
|
+
};
|
|
199
|
+
const and = (depth) => {
|
|
200
|
+
if (!term(depth))
|
|
201
|
+
return false;
|
|
202
|
+
while (tokens[at] === "AND") {
|
|
203
|
+
at += 1;
|
|
204
|
+
if (!term(depth))
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
return true;
|
|
208
|
+
};
|
|
209
|
+
const or = (depth) => {
|
|
210
|
+
if (!and(depth))
|
|
211
|
+
return false;
|
|
212
|
+
while (tokens[at] === "OR") {
|
|
213
|
+
at += 1;
|
|
214
|
+
if (!and(depth))
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
return true;
|
|
218
|
+
};
|
|
219
|
+
return or(0) && at === tokens.length;
|
|
220
|
+
}
|
|
221
|
+
function readAbout(value) {
|
|
222
|
+
const about = fields(value, "about", ["name", "summary", "author", "homepage", "license"], ["name"]);
|
|
223
|
+
const read = {
|
|
224
|
+
name: plainText(about.name, "about.name", 1, MAX_ABOUT_NAME_CHARS),
|
|
225
|
+
};
|
|
226
|
+
const summary = optional(about.summary);
|
|
227
|
+
if (summary !== undefined)
|
|
228
|
+
read.summary = plainText(summary, "about.summary", 0, MAX_ABOUT_SUMMARY_CHARS);
|
|
229
|
+
const author = optional(about.author);
|
|
230
|
+
if (author !== undefined)
|
|
231
|
+
read.author = plainText(author, "about.author", 0, MAX_ABOUT_AUTHOR_CHARS);
|
|
232
|
+
const homepage = optional(about.homepage);
|
|
233
|
+
if (homepage !== undefined) {
|
|
234
|
+
const url = text(homepage, "about.homepage");
|
|
235
|
+
if (url.length > MAX_ABOUT_HOMEPAGE_CHARS ||
|
|
236
|
+
!/^https:\/\/[!-~]+$/.test(url))
|
|
237
|
+
fail(`about.homepage is not an https: URL of printable ASCII, at most ${MAX_ABOUT_HOMEPAGE_CHARS} characters`);
|
|
238
|
+
read.homepage = url;
|
|
239
|
+
}
|
|
240
|
+
const license = optional(about.license);
|
|
241
|
+
if (license !== undefined) {
|
|
242
|
+
const expression = text(license, "about.license");
|
|
243
|
+
if (expression.length > MAX_ABOUT_LICENSE_CHARS ||
|
|
244
|
+
!/^[A-Za-z0-9.+:() -]+$/.test(expression) ||
|
|
245
|
+
!spdxExpression(expression))
|
|
246
|
+
fail(`about.license ${JSON.stringify(expression)} is not an SPDX license expression of at most ${MAX_ABOUT_LICENSE_CHARS} characters`);
|
|
247
|
+
read.license = expression;
|
|
248
|
+
}
|
|
249
|
+
return read;
|
|
250
|
+
}
|
|
251
|
+
function readOutput(value, path) {
|
|
252
|
+
const output = fields(value, path, [
|
|
253
|
+
"name",
|
|
254
|
+
"kind",
|
|
255
|
+
"channels",
|
|
256
|
+
"interpolation",
|
|
257
|
+
"suggest",
|
|
258
|
+
"label",
|
|
259
|
+
"description",
|
|
260
|
+
], ["name", "kind"]);
|
|
261
|
+
const read = {
|
|
262
|
+
name: text(output.name, `${path}.name`),
|
|
263
|
+
kind: oneOf(output.kind, `${path}.kind`, [
|
|
264
|
+
"points",
|
|
265
|
+
"scalars",
|
|
266
|
+
"similarity",
|
|
267
|
+
"words",
|
|
268
|
+
"intervals",
|
|
269
|
+
]),
|
|
270
|
+
};
|
|
271
|
+
if (output.channels !== undefined) {
|
|
272
|
+
const channels = list(output.channels, `${path}.channels`).map((channel, index) => text(channel, `${path}.channels[${index}]`));
|
|
273
|
+
if (channels.length > 0)
|
|
274
|
+
read.channels = channels;
|
|
275
|
+
}
|
|
276
|
+
const interpolation = optional(output.interpolation);
|
|
277
|
+
if (interpolation !== undefined)
|
|
278
|
+
read.interpolation = oneOf(interpolation, `${path}.interpolation`, [
|
|
279
|
+
"hold",
|
|
280
|
+
"linear",
|
|
281
|
+
]);
|
|
282
|
+
const suggest = optional(output.suggest);
|
|
283
|
+
if (suggest !== undefined)
|
|
284
|
+
read.suggest = oneOf(suggest, `${path}.suggest`, [
|
|
285
|
+
"bind",
|
|
286
|
+
"remove_ranges",
|
|
287
|
+
"split_at",
|
|
288
|
+
]);
|
|
289
|
+
const label = optional(output.label);
|
|
290
|
+
if (label !== undefined)
|
|
291
|
+
read.label = plainText(label, `${path}.label`, 1, MAX_OUTPUT_LABEL_CHARS);
|
|
292
|
+
const description = optional(output.description);
|
|
293
|
+
if (description !== undefined)
|
|
294
|
+
read.description = plainText(description, `${path}.description`, 0, MAX_OUTPUT_DESCRIPTION_CHARS);
|
|
295
|
+
return read;
|
|
296
|
+
}
|
|
297
|
+
function readInput(value) {
|
|
298
|
+
const media = isRecord(value) ? value.media : undefined;
|
|
299
|
+
const path = "descriptor.input";
|
|
300
|
+
const from = (input) => list(input.from, `${path}.from`).map((item, index) => oneOf(item, `${path}.from[${index}]`, ["source", "timeline"]));
|
|
301
|
+
if (media === "video") {
|
|
302
|
+
const input = fields(value, path, ["media", "from", "format", "max_size"], ["media", "from", "format"]);
|
|
303
|
+
return {
|
|
304
|
+
media: "video",
|
|
305
|
+
from: from(input),
|
|
306
|
+
format: oneOf(input.format, `${path}.format`, ["rgba8", "luma8"]),
|
|
307
|
+
max_size: input.max_size === undefined
|
|
308
|
+
? DEFAULT_MAX_SIZE
|
|
309
|
+
: integer(input.max_size, `${path}.max_size`, MAX_U32),
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
if (media === "audio") {
|
|
313
|
+
const input = fields(value, path, ["media", "from", "format", "sample_rate", "channels", "block_frames"], ["media", "from", "format"]);
|
|
314
|
+
const sampleRate = optional(input.sample_rate);
|
|
315
|
+
const channels = optional(input.channels);
|
|
316
|
+
return {
|
|
317
|
+
media: "audio",
|
|
318
|
+
from: from(input),
|
|
319
|
+
format: oneOf(input.format, `${path}.format`, ["f32"]),
|
|
320
|
+
...(sampleRate === undefined
|
|
321
|
+
? {}
|
|
322
|
+
: { sample_rate: integer(sampleRate, `${path}.sample_rate`, MAX_U32) }),
|
|
323
|
+
...(channels === undefined
|
|
324
|
+
? {}
|
|
325
|
+
: {
|
|
326
|
+
channels: oneOf(channels, `${path}.channels`, [
|
|
327
|
+
"mono",
|
|
328
|
+
"source",
|
|
329
|
+
]),
|
|
330
|
+
}),
|
|
331
|
+
block_frames: input.block_frames === undefined
|
|
332
|
+
? DEFAULT_BLOCK_FRAMES
|
|
333
|
+
: integer(input.block_frames, `${path}.block_frames`, MAX_U32),
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
return fail(`${path}.media is not video or audio`);
|
|
337
|
+
}
|
|
338
|
+
/** Mirrors `validate_descriptor`. */
|
|
339
|
+
function checkDescriptor(descriptor) {
|
|
340
|
+
if (descriptor.id.length === 0)
|
|
341
|
+
fail("empty id");
|
|
342
|
+
const { outputs, input } = descriptor;
|
|
343
|
+
if (outputs.length === 0 || outputs.length > MAX_EXTRACTOR_OUTPUTS)
|
|
344
|
+
fail(`needs 1 to ${MAX_EXTRACTOR_OUTPUTS} outputs`);
|
|
345
|
+
const names = new Set();
|
|
346
|
+
for (const output of outputs) {
|
|
347
|
+
if (output.name.length === 0 || names.has(output.name))
|
|
348
|
+
fail("an output name is empty or repeated");
|
|
349
|
+
names.add(output.name);
|
|
350
|
+
const channels = output.channels ?? [];
|
|
351
|
+
const sampled = output.kind === "points" || output.kind === "scalars";
|
|
352
|
+
if (sampled === (channels.length === 0))
|
|
353
|
+
fail("points and scalars outputs need channels; others take none");
|
|
354
|
+
if (channels.length > MAX_DATA_CHANNELS)
|
|
355
|
+
fail("an output has too many channels");
|
|
356
|
+
if (new Set(channels).size !== channels.length ||
|
|
357
|
+
channels.some((channel) => channel.length === 0 || utf8Length(channel) > MAX_DATA_LABEL_BYTES))
|
|
358
|
+
fail("a channel name is empty, too long or repeated");
|
|
359
|
+
if (output.interpolation !== undefined &&
|
|
360
|
+
(output.kind === "words" || output.kind === "intervals"))
|
|
361
|
+
fail("only sampled outputs take an interpolation");
|
|
362
|
+
}
|
|
363
|
+
if (input.from.length === 0)
|
|
364
|
+
fail("reads no input");
|
|
365
|
+
if (input.media === "video" && input.max_size === 0)
|
|
366
|
+
fail("max_size is zero");
|
|
367
|
+
if (input.media === "audio" &&
|
|
368
|
+
((input.block_frames ?? DEFAULT_BLOCK_FRAMES) < BLOCK_FRAMES.min ||
|
|
369
|
+
(input.block_frames ?? DEFAULT_BLOCK_FRAMES) > BLOCK_FRAMES.max ||
|
|
370
|
+
(input.sample_rate !== undefined &&
|
|
371
|
+
(input.sample_rate < MIN_SAMPLE_RATE ||
|
|
372
|
+
input.sample_rate > MAX_SAMPLE_RATE))))
|
|
373
|
+
fail("block_frames or sample_rate out of range");
|
|
374
|
+
}
|
|
375
|
+
function readDescriptor(value) {
|
|
376
|
+
const descriptor = fields(value, "descriptor", ["id", "version", "input", "params_schema", "outputs"], ["id", "version", "input", "outputs"]);
|
|
377
|
+
const read = {
|
|
378
|
+
id: text(descriptor.id, "descriptor.id"),
|
|
379
|
+
version: integer(descriptor.version, "descriptor.version", MAX_U32),
|
|
380
|
+
input: readInput(descriptor.input),
|
|
381
|
+
params_schema: descriptor.params_schema === undefined ? {} : descriptor.params_schema,
|
|
382
|
+
outputs: list(descriptor.outputs, "descriptor.outputs").map((output, index) => readOutput(output, `descriptor.outputs[${index}]`)),
|
|
383
|
+
};
|
|
384
|
+
checkDescriptor(read);
|
|
385
|
+
return read;
|
|
386
|
+
}
|
|
387
|
+
function readResource(value, index) {
|
|
388
|
+
const path = `resources[${index}]`;
|
|
389
|
+
const entry = fields(value, path, ["name", "url", "sha256", "bytes", "supplied"], ["name"]);
|
|
390
|
+
const name = text(entry.name, `${path}.name`);
|
|
391
|
+
const pin = ["url", "sha256", "bytes"].map((key) => optional(entry[key]));
|
|
392
|
+
const supplied = optional(entry.supplied);
|
|
393
|
+
if (supplied !== undefined) {
|
|
394
|
+
if (pin.some((part) => part !== undefined))
|
|
395
|
+
fail(`resource ${JSON.stringify(name)} has supplied and a pin; a resource is one or the other`);
|
|
396
|
+
const slot = fields(supplied, `${path}.supplied`, ["shape", "required", "max_bytes", "accept"], ["shape"]);
|
|
397
|
+
const read = {
|
|
398
|
+
shape: oneOf(slot.shape, `${path}.supplied.shape`, ["file", "directory"]),
|
|
399
|
+
};
|
|
400
|
+
if (slot.required !== undefined) {
|
|
401
|
+
if (typeof slot.required !== "boolean")
|
|
402
|
+
fail(`${path}.supplied.required is not a boolean`);
|
|
403
|
+
read.required = slot.required;
|
|
404
|
+
}
|
|
405
|
+
const maxBytes = optional(slot.max_bytes);
|
|
406
|
+
if (maxBytes !== undefined)
|
|
407
|
+
read.max_bytes = integer(maxBytes, `${path}.supplied.max_bytes`, Number.MAX_SAFE_INTEGER);
|
|
408
|
+
if (slot.accept !== undefined)
|
|
409
|
+
read.accept = list(slot.accept, `${path}.supplied.accept`).map((suffix, at) => text(suffix, `${path}.supplied.accept[${at}]`));
|
|
410
|
+
return { name, supplied: read };
|
|
411
|
+
}
|
|
412
|
+
if (pin.every((part) => part === undefined))
|
|
413
|
+
fail(`resource ${JSON.stringify(name)} has neither url, sha256 and bytes nor supplied`);
|
|
414
|
+
if (pin.some((part) => part === undefined))
|
|
415
|
+
fail(`resource ${JSON.stringify(name)} needs all of url, sha256 and bytes`);
|
|
416
|
+
return {
|
|
417
|
+
name,
|
|
418
|
+
url: text(entry.url, `${path}.url`),
|
|
419
|
+
sha256: text(entry.sha256, `${path}.sha256`),
|
|
420
|
+
bytes: integer(entry.bytes, `${path}.bytes`, Number.MAX_SAFE_INTEGER),
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function checkSlot(resource) {
|
|
424
|
+
const { name, supplied } = resource;
|
|
425
|
+
if (!isSlotName(name))
|
|
426
|
+
fail(`slot name ${JSON.stringify(name)} is not 1 to ${MAX_SLOT_NAME_BYTES} of A-Z, a-z, 0-9, '.', '_' and '-', not starting with '.'`);
|
|
427
|
+
if (supplied.max_bytes === 0)
|
|
428
|
+
fail(`slot ${name}: max_bytes is 0`);
|
|
429
|
+
const accept = supplied.accept ?? [];
|
|
430
|
+
if (accept.length > MAX_SLOT_ACCEPT)
|
|
431
|
+
fail(`slot ${name}: accept lists more than ${MAX_SLOT_ACCEPT} suffixes`);
|
|
432
|
+
const bad = accept.find((suffix) => suffix.length === 0 || utf8Length(suffix) > MAX_SLOT_ACCEPT_BYTES);
|
|
433
|
+
if (bad !== undefined)
|
|
434
|
+
fail(`slot ${name}: accept suffix ${JSON.stringify(bad)} is empty or longer than ${MAX_SLOT_ACCEPT_BYTES} bytes`);
|
|
435
|
+
}
|
|
436
|
+
/** Mirrors `validate_resources`. */
|
|
437
|
+
function checkResources(resources) {
|
|
438
|
+
const names = new Set();
|
|
439
|
+
const directories = [];
|
|
440
|
+
let slots = 0;
|
|
441
|
+
let total = 0;
|
|
442
|
+
for (const resource of resources) {
|
|
443
|
+
if (resource.name.length === 0 || names.has(resource.name))
|
|
444
|
+
fail(`resource name ${JSON.stringify(resource.name)} is empty or repeated`);
|
|
445
|
+
names.add(resource.name);
|
|
446
|
+
if (resource.supplied === undefined) {
|
|
447
|
+
checkFile(resource, `resource ${resource.name}`);
|
|
448
|
+
if (resource.bytes > MAX_PLUGIN_RESOURCE_BYTES)
|
|
449
|
+
fail(`resource ${resource.name} is longer than ${MAX_PLUGIN_RESOURCE_BYTES} bytes`);
|
|
450
|
+
total += resource.bytes;
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
slots += 1;
|
|
454
|
+
checkSlot(resource);
|
|
455
|
+
if (resource.supplied.shape === "directory")
|
|
456
|
+
directories.push(`${resource.name}/`);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (slots > MAX_PLUGIN_SLOTS)
|
|
460
|
+
fail(`declares more than ${MAX_PLUGIN_SLOTS} resource slots`);
|
|
461
|
+
const under = resources.find((resource) => resource.supplied === undefined &&
|
|
462
|
+
directories.some((prefix) => resource.name.startsWith(prefix)));
|
|
463
|
+
if (under !== undefined)
|
|
464
|
+
fail(`pinned resource ${under.name} lies under a directory slot's names`);
|
|
465
|
+
if (total > MAX_PLUGIN_RESOURCES_TOTAL_BYTES)
|
|
466
|
+
fail(`the pinned resources are longer than ${MAX_PLUGIN_RESOURCES_TOTAL_BYTES} bytes together`);
|
|
467
|
+
}
|
|
468
|
+
function readManifest(value) {
|
|
469
|
+
const manifest = fields(value, "the manifest", [
|
|
470
|
+
"id",
|
|
471
|
+
"version",
|
|
472
|
+
"abi",
|
|
473
|
+
"kind",
|
|
474
|
+
"artifacts",
|
|
475
|
+
"resources",
|
|
476
|
+
"about",
|
|
477
|
+
"descriptor",
|
|
478
|
+
], ["id", "version", "abi", "kind", "artifacts", "descriptor"]);
|
|
479
|
+
if (manifest.abi !== PLUGIN_ABI)
|
|
480
|
+
fail(`abi is not ${PLUGIN_ABI}`);
|
|
481
|
+
const id = text(manifest.id, "id");
|
|
482
|
+
const version = text(manifest.version, "version");
|
|
483
|
+
oneOf(manifest.kind, "kind", ["extractor"]);
|
|
484
|
+
const artifacts = list(manifest.artifacts, "artifacts").map((item, index) => {
|
|
485
|
+
const path = `artifacts[${index}]`;
|
|
486
|
+
const artifact = fields(item, path, ["target", "type", "url", "sha256", "bytes"], ["target", "type", "url", "sha256", "bytes"]);
|
|
487
|
+
return {
|
|
488
|
+
target: text(artifact.target, `${path}.target`),
|
|
489
|
+
type: oneOf(artifact.type, `${path}.type`, [
|
|
490
|
+
"wasm",
|
|
491
|
+
"cdylib",
|
|
492
|
+
"staticlib",
|
|
493
|
+
]),
|
|
494
|
+
url: text(artifact.url, `${path}.url`),
|
|
495
|
+
sha256: text(artifact.sha256, `${path}.sha256`),
|
|
496
|
+
bytes: integer(artifact.bytes, `${path}.bytes`, Number.MAX_SAFE_INTEGER),
|
|
497
|
+
};
|
|
498
|
+
});
|
|
499
|
+
const resources = manifest.resources === undefined
|
|
500
|
+
? []
|
|
501
|
+
: list(manifest.resources, "resources").map(readResource);
|
|
502
|
+
const aboutValue = optional(manifest.about);
|
|
503
|
+
const about = aboutValue === undefined ? undefined : readAbout(aboutValue);
|
|
504
|
+
const descriptor = readDescriptor(manifest.descriptor);
|
|
505
|
+
if (id !== descriptor.id)
|
|
506
|
+
fail(`id ${id} differs from the descriptor's ${descriptor.id}`);
|
|
507
|
+
if (!semver(version))
|
|
508
|
+
fail(`version ${version} is not a semantic version`);
|
|
509
|
+
if (artifacts.length === 0 || artifacts.length > MAX_PLUGIN_ARTIFACTS)
|
|
510
|
+
fail(`needs 1 to ${MAX_PLUGIN_ARTIFACTS} artifacts`);
|
|
511
|
+
if (resources.length > MAX_PLUGIN_RESOURCES)
|
|
512
|
+
fail(`lists more than ${MAX_PLUGIN_RESOURCES} resources`);
|
|
513
|
+
const targets = new Set();
|
|
514
|
+
for (const artifact of artifacts) {
|
|
515
|
+
const key = `${artifact.type}\u0000${artifact.target}`;
|
|
516
|
+
if (artifact.target.length === 0 || targets.has(key))
|
|
517
|
+
fail(`artifact target ${JSON.stringify(artifact.target)} is empty or listed twice for one type`);
|
|
518
|
+
targets.add(key);
|
|
519
|
+
if ((artifact.target === "wasm32") !== (artifact.type === "wasm"))
|
|
520
|
+
fail(`artifact ${artifact.url}: only the wasm32 target is a wasm module`);
|
|
521
|
+
checkFile(artifact, `artifact ${artifact.url}`);
|
|
522
|
+
if (artifact.bytes > MAX_PLUGIN_ARTIFACT_BYTES)
|
|
523
|
+
fail(`artifact ${artifact.url} is longer than ${MAX_PLUGIN_ARTIFACT_BYTES} bytes`);
|
|
524
|
+
}
|
|
525
|
+
checkResources(resources);
|
|
526
|
+
return {
|
|
527
|
+
id,
|
|
528
|
+
version,
|
|
529
|
+
abi: PLUGIN_ABI,
|
|
530
|
+
kind: "extractor",
|
|
531
|
+
artifacts,
|
|
532
|
+
...(resources.length === 0 ? {} : { resources }),
|
|
533
|
+
...(about === undefined ? {} : { about }),
|
|
534
|
+
descriptor,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Reads and checks a manifest by the rules the engine's loaders use, fetching nothing. The
|
|
539
|
+
* descriptor comes back as the engine reads it, with `max_size`, `block_frames` and
|
|
540
|
+
* `params_schema` filled in and `null` options left out.
|
|
541
|
+
*
|
|
542
|
+
* Throws a {@link PluginError}: `PLUGIN_ABI_UNSUPPORTED` for another `abi`, checked first, and
|
|
543
|
+
* `PLUGIN_LOAD_FAILED` for text past {@link MAX_MANIFEST_BYTES}, text that is not JSON, or a
|
|
544
|
+
* manifest that breaks a rule of `docs/plugin-abi.md`.
|
|
545
|
+
*/
|
|
546
|
+
export function parsePluginManifest(text) {
|
|
547
|
+
const invalid = (why, cause) => new PluginError("PLUGIN_LOAD_FAILED", `invalid plugin manifest: ${why}`, {
|
|
548
|
+
cause,
|
|
549
|
+
});
|
|
550
|
+
if (text.length > MAX_MANIFEST_BYTES ||
|
|
551
|
+
(text.length * 3 > MAX_MANIFEST_BYTES &&
|
|
552
|
+
utf8Length(text) > MAX_MANIFEST_BYTES))
|
|
553
|
+
throw invalid(`longer than ${MAX_MANIFEST_BYTES} bytes`);
|
|
554
|
+
let value;
|
|
555
|
+
try {
|
|
556
|
+
value = JSON.parse(text);
|
|
557
|
+
}
|
|
558
|
+
catch (cause) {
|
|
559
|
+
throw invalid(String(cause), cause);
|
|
560
|
+
}
|
|
561
|
+
const abi = isRecord(value) ? value.abi : undefined;
|
|
562
|
+
if (typeof abi === "string" && abi !== PLUGIN_ABI)
|
|
563
|
+
throw new PluginError("PLUGIN_ABI_UNSUPPORTED", `the plugin needs ABI ${abi}; this build implements ${PLUGIN_ABI}`);
|
|
564
|
+
try {
|
|
565
|
+
return readManifest(value);
|
|
566
|
+
}
|
|
567
|
+
catch (cause) {
|
|
568
|
+
if (cause instanceof Invalid)
|
|
569
|
+
throw invalid(cause.message);
|
|
570
|
+
throw cause;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
/** Reads a descriptor JSON as {@link parsePluginManifest} reads a manifest's. */
|
|
574
|
+
export function readPluginDescriptor(value) {
|
|
575
|
+
try {
|
|
576
|
+
return readDescriptor(value);
|
|
577
|
+
}
|
|
578
|
+
catch (cause) {
|
|
579
|
+
if (cause instanceof Invalid)
|
|
580
|
+
throw new PluginError("PLUGIN_DESCRIPTOR_MISMATCH", `the artifact's descriptor is not one: ${cause.message}`);
|
|
581
|
+
throw cause;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type SuppliedFile } from "./generated/project-schema.js";
|
|
2
|
+
import { type PluginManifest } from "./plugin-manifest.js";
|
|
3
|
+
/** Mirrors `alpha2_host_core::plugins::MAX_SLOT_DEPTH`: directories below a slot's own. */
|
|
4
|
+
export declare const MAX_SLOT_DEPTH = 8;
|
|
5
|
+
/**
|
|
6
|
+
* What fills one slot: a `File` for a `file` slot; for a `directory` slot a directory handle, or
|
|
7
|
+
* the `File`s of a directory picker (`<input webkitdirectory>`), named by `webkitRelativePath`
|
|
8
|
+
* below the picked directory.
|
|
9
|
+
*/
|
|
10
|
+
export type SuppliedSource = File | FileSystemDirectoryHandle | readonly File[];
|
|
11
|
+
interface Offered {
|
|
12
|
+
readonly slot: string;
|
|
13
|
+
readonly file: File;
|
|
14
|
+
readonly maxBytes: number | undefined;
|
|
15
|
+
}
|
|
16
|
+
/** The filled slots of one plugin, resolved at load; nothing is read. */
|
|
17
|
+
export interface SuppliedSet {
|
|
18
|
+
/** By resource name, in UTF-8 byte order. */
|
|
19
|
+
readonly files: ReadonlyMap<string, Offered>;
|
|
20
|
+
/** As `begin`'s context names them. */
|
|
21
|
+
readonly context: Readonly<Record<string, {
|
|
22
|
+
readonly shape: "file" | "directory";
|
|
23
|
+
readonly files: readonly {
|
|
24
|
+
name: string;
|
|
25
|
+
bytes: number;
|
|
26
|
+
}[];
|
|
27
|
+
}>>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Resolves the slots of `manifest` from `supplied`, refusing what host-core's loader refuses,
|
|
31
|
+
* with the same `details.reason`. Nothing is read; directory handles are walked.
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveSupplied(manifest: PluginManifest, supplied: Readonly<Record<string, SuppliedSource>>): Promise<SuppliedSet>;
|
|
34
|
+
/**
|
|
35
|
+
* Serves one run's reads of supplied files synchronously, as wasm imports must: in a worker
|
|
36
|
+
* through `FileReaderSync`, step by step; on a page, from bytes read beforehand by
|
|
37
|
+
* {@link SuppliedReader.prepare}. Each file is hashed the first time it is read.
|
|
38
|
+
*/
|
|
39
|
+
export declare class SuppliedReader {
|
|
40
|
+
#private;
|
|
41
|
+
constructor(set: SuppliedSet);
|
|
42
|
+
/** Reads every offered file into memory where no synchronous file read exists. */
|
|
43
|
+
prepare(): Promise<void>;
|
|
44
|
+
/** The length of the supplied resource `name`, or `undefined` for another name. */
|
|
45
|
+
size(name: string): number | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Copies the supplied resource `name` into `target`, its length exactly. Throws the failure the
|
|
48
|
+
* call it happened in ends with: `PLUGIN_RESOURCE_INVALID` (`too_large`) past the slot's
|
|
49
|
+
* `max_bytes`, `PLUGIN_FETCH_FAILED` for a file that could not be read or changed.
|
|
50
|
+
*/
|
|
51
|
+
read(name: string, target: Uint8Array): void;
|
|
52
|
+
/** The files read so far, as `made_by.plugin.supplied` records them. */
|
|
53
|
+
supplied(): SuppliedFile[];
|
|
54
|
+
}
|
|
55
|
+
export {};
|