@beff/aula-cli 0.0.0-stage → 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/README.md +10 -2
- package/dist/licenses/aula-g2p-NOTICE.txt +13 -0
- package/dist/licenses/espeak-ng-COPYING.txt +674 -0
- package/dist/licenses/fonts-OFL-JetBrainsMono.txt +93 -0
- package/dist/licenses/fonts-OFL-NunitoSans.txt +93 -0
- package/dist/src/aula-render.core.wasm +0 -0
- package/dist/src/aula-voice-tts-ZXQ7XXHJ.js +14 -0
- package/dist/src/chunk-2XGNOG2U.js +28 -0
- package/dist/src/chunk-3IBMAZRS.js +21 -0
- package/dist/src/chunk-EYXQ2I7K.js +604 -0
- package/dist/src/chunk-GFCJYE75.js +42 -0
- package/dist/src/chunk-KLE5UOUB.js +57 -0
- package/dist/src/chunk-OMBYV6CK.js +1063 -0
- package/dist/src/chunk-PYRORQA5.js +88 -0
- package/dist/src/chunk-SDFXIMLW.js +1083 -0
- package/dist/src/chunk-UREQAEFC.js +491 -0
- package/dist/src/chunk-X3HJY34X.js +81 -0
- package/dist/src/cli.js +269 -0
- package/dist/src/commands-VDGWDVWT.js +28 -0
- package/dist/src/problems-OGUR4BLZ.js +12 -0
- package/dist/src/project-M63R4ENB.js +11 -0
- package/dist/src/render-child.js +3771 -0
- package/dist/src/run-CY72QGZJ.js +127 -0
- package/dist/src/server-ZG6K7Z7H.js +280 -0
- package/dist/web/app/assets/__vite-browser-external-Bbi05A7N.js +1 -0
- package/dist/web/app/assets/__vite-browser-external-BtQTJ51O.js +1 -0
- package/dist/web/app/assets/aula-method.js +1 -0
- package/dist/web/app/assets/aula-voice-worker-BL6WcrSe.js +52 -0
- package/dist/web/app/assets/aula.core-BtiPQSZi.wasm +0 -0
- package/dist/web/app/assets/aula.core5-BdGB-M10.wasm +0 -0
- package/dist/web/app/assets/aula.js +1 -0
- package/dist/web/app/assets/build-worker.js +697 -0
- package/dist/web/app/assets/esbuild-BaXVPFT2.wasm +0 -0
- package/dist/web/app/assets/index-BVQqteYG.js +53 -0
- package/dist/web/app/assets/index-CS60sOpp.css +2 -0
- package/dist/web/app/assets/protocol-Ds4jI5bY.js +15 -0
- package/dist/web/app/assets/rolldown-runtime-BLXQ4ufn.js +1 -0
- package/dist/web/app/assets/src-BbJ5r410.js +25 -0
- package/dist/web/app/index.html +15 -0
- package/package.json +26 -3
|
@@ -0,0 +1,1083 @@
|
|
|
1
|
+
// ../aula-narration/src/script.ts
|
|
2
|
+
import { chapterNarration } from "@beff/aula/text";
|
|
3
|
+
function joinChapter(cues, speeches) {
|
|
4
|
+
let rate = 0;
|
|
5
|
+
for (const [i, speech] of speeches.entries()) {
|
|
6
|
+
if (!speech.samples.length || speech.samples.some((s) => !Number.isFinite(s))) {
|
|
7
|
+
const text = cues[i]?.text.slice(0, 60) ?? "";
|
|
8
|
+
throw new Error(`the narrator returned empty or invalid audio for ${JSON.stringify(text)}`);
|
|
9
|
+
}
|
|
10
|
+
if (rate !== 0 && speech.sampleRate !== rate) throw new Error("the narrator changed its sample rate");
|
|
11
|
+
rate = speech.sampleRate;
|
|
12
|
+
}
|
|
13
|
+
const spoken = speeches.map((s) => ({
|
|
14
|
+
samples: s.samples.length,
|
|
15
|
+
words: s.words.map((w) => ({ text: w.text, char: w.char, start: w.start }))
|
|
16
|
+
}));
|
|
17
|
+
const narration = chapterNarration(cues, spoken, rate);
|
|
18
|
+
const samples = new Float32Array(narration.endFrame);
|
|
19
|
+
for (const [i, speech] of speeches.entries()) samples.set(speech.samples, narration.cues[i]?.speechStartFrame ?? 0);
|
|
20
|
+
return { samples, narration };
|
|
21
|
+
}
|
|
22
|
+
function withWaits(samples, waits) {
|
|
23
|
+
if (waits.length === 0) return samples;
|
|
24
|
+
const out = new Float32Array(samples.length + waits.reduce((sum, w) => sum + w.samples, 0));
|
|
25
|
+
let from = 0;
|
|
26
|
+
let to = 0;
|
|
27
|
+
for (const wait of waits.toSorted((a, b) => a.sample - b.sample)) {
|
|
28
|
+
const at = Math.min(wait.sample, samples.length);
|
|
29
|
+
out.set(samples.subarray(from, at), to);
|
|
30
|
+
to += at - from + wait.samples;
|
|
31
|
+
from = at;
|
|
32
|
+
}
|
|
33
|
+
out.set(samples.subarray(from), to);
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
function timeChapter(cues, timings) {
|
|
37
|
+
const rate = timings[0]?.sampleRate ?? 0;
|
|
38
|
+
if (timings.some((t) => t.sampleRate !== rate)) throw new Error("the narrator changed its sample rate");
|
|
39
|
+
const spoken = timings.map((t) => ({
|
|
40
|
+
samples: t.samples,
|
|
41
|
+
words: t.words.map((w) => ({ text: w.text, char: w.char, start: w.start }))
|
|
42
|
+
}));
|
|
43
|
+
return chapterNarration(cues, spoken, rate);
|
|
44
|
+
}
|
|
45
|
+
async function narrateScript(narrator, chapters, speaking) {
|
|
46
|
+
const count = chapters.reduce((sum, c) => sum + c.length, 0);
|
|
47
|
+
let spoken = 0;
|
|
48
|
+
const narrated = [];
|
|
49
|
+
for (const cues of chapters) {
|
|
50
|
+
const speeches = [];
|
|
51
|
+
for (const cue of cues) {
|
|
52
|
+
spoken += 1;
|
|
53
|
+
speaking(spoken, count, cue);
|
|
54
|
+
speeches.push(await narrator.speak(cue.text, cue.speed));
|
|
55
|
+
}
|
|
56
|
+
narrated.push(joinChapter(cues, speeches));
|
|
57
|
+
}
|
|
58
|
+
if (!narrated.some((c) => c.narration.sampleRate > 0)) throw new Error("the script has no cues");
|
|
59
|
+
return narrated;
|
|
60
|
+
}
|
|
61
|
+
function scriptAudio(chapters, waits) {
|
|
62
|
+
const sounds = chapters.map((c, i) => withWaits(c.samples, waits[i] ?? []));
|
|
63
|
+
const samples = new Float32Array(sounds.reduce((sum, s) => sum + s.length, 0));
|
|
64
|
+
let at = 0;
|
|
65
|
+
for (const sound of sounds) {
|
|
66
|
+
samples.set(sound, at);
|
|
67
|
+
at += sound.length;
|
|
68
|
+
}
|
|
69
|
+
const sampleRate = chapters.find((c) => c.narration.sampleRate > 0)?.narration.sampleRate ?? 0;
|
|
70
|
+
return { samples, sampleRate };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ../aula-narration/src/wav.ts
|
|
74
|
+
function wav(samples, rate) {
|
|
75
|
+
const bytes = new Uint8Array(44 + samples.length * 2);
|
|
76
|
+
const view = new DataView(bytes.buffer);
|
|
77
|
+
const text = (at, value) => {
|
|
78
|
+
for (let i = 0; i < value.length; i++) view.setUint8(at + i, value.charCodeAt(i));
|
|
79
|
+
};
|
|
80
|
+
text(0, "RIFF");
|
|
81
|
+
view.setUint32(4, 36 + samples.length * 2, true);
|
|
82
|
+
text(8, "WAVE");
|
|
83
|
+
text(12, "fmt ");
|
|
84
|
+
view.setUint32(16, 16, true);
|
|
85
|
+
view.setUint16(20, 1, true);
|
|
86
|
+
view.setUint16(22, 1, true);
|
|
87
|
+
view.setUint32(24, rate, true);
|
|
88
|
+
view.setUint32(28, rate * 2, true);
|
|
89
|
+
view.setUint16(32, 2, true);
|
|
90
|
+
view.setUint16(34, 16, true);
|
|
91
|
+
text(36, "data");
|
|
92
|
+
view.setUint32(40, samples.length * 2, true);
|
|
93
|
+
samples.forEach((sample, i) => {
|
|
94
|
+
view.setInt16(44 + i * 2, Math.round(Math.min(1, Math.max(-1, sample)) * 32767), true);
|
|
95
|
+
});
|
|
96
|
+
return bytes;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ../../node_modules/.pnpm/@beff+client@1.0.1/node_modules/@beff/client/dist/esm/openapi-pp.js
|
|
100
|
+
var removeNullUnionBranch = (definition) => {
|
|
101
|
+
if (typeof definition === "boolean") {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
const variantsKey = definition.anyOf != null ? "anyOf" : definition.oneOf != null ? "oneOf" : null;
|
|
105
|
+
if (variantsKey == null) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
const variants = definition[variantsKey];
|
|
109
|
+
if (variants == null) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
const nonNull = variants.filter((variant) => !isNullDefinition(variant));
|
|
113
|
+
if (nonNull.length === variants.length || nonNull.length === 0) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
const normalizedNonNull = nonNull.map((variant) => removeNullUnionBranch(variant) ?? variant);
|
|
117
|
+
if (normalizedNonNull.length === 1) {
|
|
118
|
+
return normalizedNonNull[0];
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
...definition,
|
|
122
|
+
[variantsKey]: normalizedNonNull
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
var isNullDefinition = (definition) => {
|
|
126
|
+
return typeof definition !== "boolean" && definition.type === "null";
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// ../../node_modules/.pnpm/@beff+client@1.0.1/node_modules/@beff/client/dist/esm/err.js
|
|
130
|
+
var prettyPrintValue = (it) => {
|
|
131
|
+
if (typeof it === "string") {
|
|
132
|
+
return `"${it}"`;
|
|
133
|
+
}
|
|
134
|
+
if (typeof it === "number") {
|
|
135
|
+
return `${it}`;
|
|
136
|
+
}
|
|
137
|
+
if (typeof it === "boolean") {
|
|
138
|
+
return `${it}`;
|
|
139
|
+
}
|
|
140
|
+
if (typeof it === "bigint") {
|
|
141
|
+
return `${it}n`;
|
|
142
|
+
}
|
|
143
|
+
if (it === null) {
|
|
144
|
+
return "null";
|
|
145
|
+
}
|
|
146
|
+
if (Array.isArray(it)) {
|
|
147
|
+
return `Array`;
|
|
148
|
+
}
|
|
149
|
+
if (typeof it === "object") {
|
|
150
|
+
return `Object`;
|
|
151
|
+
}
|
|
152
|
+
return JSON.stringify(it);
|
|
153
|
+
};
|
|
154
|
+
var joinWithDot = (it) => {
|
|
155
|
+
if (it.length === 0) {
|
|
156
|
+
return "";
|
|
157
|
+
}
|
|
158
|
+
let acc = it[0];
|
|
159
|
+
for (const item of it.slice(1)) {
|
|
160
|
+
if (item.startsWith("[")) {
|
|
161
|
+
acc += item;
|
|
162
|
+
} else {
|
|
163
|
+
acc += "." + item;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return acc;
|
|
167
|
+
};
|
|
168
|
+
var printPath = (parentPath, path) => {
|
|
169
|
+
const mergedPath = [...parentPath, ...path];
|
|
170
|
+
return mergedPath.length > 0 ? `(${joinWithDot(mergedPath)})` : "";
|
|
171
|
+
};
|
|
172
|
+
var joinFilteredStrings = (it) => {
|
|
173
|
+
return it.filter((it2) => it2.length > 0).join(" ");
|
|
174
|
+
};
|
|
175
|
+
var printRegularError = (err, parentPath, showReceived) => {
|
|
176
|
+
const path = printPath(parentPath, err.path);
|
|
177
|
+
const msg = [err.message, showReceived ? `received: ${prettyPrintValue(err.received)}` : ""].filter((it) => it.length > 0).join(", ");
|
|
178
|
+
return joinFilteredStrings([path, msg]);
|
|
179
|
+
};
|
|
180
|
+
var printUnionError = (err, parentPath) => {
|
|
181
|
+
const path = printPath(parentPath, err.path);
|
|
182
|
+
const printedErrors = printErrorsPart(err.errors, [], false);
|
|
183
|
+
const innerMessages = printedErrors.length > 5 ? printedErrors.slice(0, 5).join(" OR ") + " and more..." : printedErrors.join(" | ");
|
|
184
|
+
const msg = [`Failed to decode one of (${innerMessages})`, `received: ${prettyPrintValue(err.received)}`].filter((it) => it.length > 0).join(", ");
|
|
185
|
+
return joinFilteredStrings([path, msg]);
|
|
186
|
+
};
|
|
187
|
+
var printErrorsPart = (it, parentPath, showReceived) => {
|
|
188
|
+
return it.map((err) => {
|
|
189
|
+
if ("isUnionError" in err) {
|
|
190
|
+
return printUnionError(err, parentPath);
|
|
191
|
+
}
|
|
192
|
+
return printRegularError(err, parentPath, showReceived);
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
var printErrors = (it, parentPath = []) => {
|
|
196
|
+
return printErrorsPart(it, parentPath, true).map((msg, idx, all) => all.length == 1 ? joinFilteredStrings([msg]) : joinFilteredStrings([`#${idx}`, msg])).join(" | ");
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
// ../../node_modules/.pnpm/@beff+client@1.0.1/node_modules/@beff/client/dist/esm/runtime.js
|
|
200
|
+
var JSON_PROTO = Object.getPrototypeOf({});
|
|
201
|
+
function deepmergeConstructor(options) {
|
|
202
|
+
function setKey(result, key, value) {
|
|
203
|
+
if (key === "__proto__") {
|
|
204
|
+
Object.defineProperty(result, key, { value, enumerable: true, writable: true, configurable: true });
|
|
205
|
+
} else {
|
|
206
|
+
result[key] = value;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
function cloneArray(value) {
|
|
210
|
+
let i = 0;
|
|
211
|
+
const il = value.length;
|
|
212
|
+
const result = new Array(il);
|
|
213
|
+
for (i; i < il; ++i) {
|
|
214
|
+
result[i] = clone(value[i]);
|
|
215
|
+
}
|
|
216
|
+
return result;
|
|
217
|
+
}
|
|
218
|
+
function cloneObject(target) {
|
|
219
|
+
const result = {};
|
|
220
|
+
if (cloneProtoObject && Object.getPrototypeOf(target) !== JSON_PROTO) {
|
|
221
|
+
return cloneProtoObject(target);
|
|
222
|
+
}
|
|
223
|
+
const targetKeys = getKeys(target);
|
|
224
|
+
let i, il, key;
|
|
225
|
+
for (i = 0, il = targetKeys.length; i < il; ++i) {
|
|
226
|
+
key = targetKeys[i];
|
|
227
|
+
setKey(result, key, clone(target[key]));
|
|
228
|
+
}
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
function concatArrays(target, source) {
|
|
232
|
+
const tl = target.length;
|
|
233
|
+
const sl = source.length;
|
|
234
|
+
let i = 0;
|
|
235
|
+
const result = new Array(tl + sl);
|
|
236
|
+
for (i; i < tl; ++i) {
|
|
237
|
+
result[i] = clone(target[i]);
|
|
238
|
+
}
|
|
239
|
+
for (i = 0; i < sl; ++i) {
|
|
240
|
+
result[i + tl] = clone(source[i]);
|
|
241
|
+
}
|
|
242
|
+
return result;
|
|
243
|
+
}
|
|
244
|
+
const propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
|
|
245
|
+
function getSymbolsAndKeys(value) {
|
|
246
|
+
const result = Object.keys(value);
|
|
247
|
+
const keys = Object.getOwnPropertySymbols(value);
|
|
248
|
+
for (let i = 0, il = keys.length; i < il; ++i) {
|
|
249
|
+
propertyIsEnumerable.call(value, keys[i]) && result.push(keys[i]);
|
|
250
|
+
}
|
|
251
|
+
return result;
|
|
252
|
+
}
|
|
253
|
+
const getKeys = options?.symbols ? getSymbolsAndKeys : Object.keys;
|
|
254
|
+
const cloneProtoObject = typeof options?.cloneProtoObject === "function" ? options.cloneProtoObject : void 0;
|
|
255
|
+
function isMergeableObject(value) {
|
|
256
|
+
return typeof value === "object" && value !== null && !(value instanceof RegExp) && !(value instanceof Date) && !(value instanceof Map) && !(value instanceof Set) && !ArrayBuffer.isView(value);
|
|
257
|
+
}
|
|
258
|
+
function isPrimitive(value) {
|
|
259
|
+
return typeof value !== "object" || value === null;
|
|
260
|
+
}
|
|
261
|
+
const isPrimitiveOrBuiltIn = (
|
|
262
|
+
// @ts-ignore
|
|
263
|
+
typeof Buffer !== "undefined" ? (value) => typeof value !== "object" || value === null || value instanceof RegExp || value instanceof Date || ArrayBuffer.isView(value) || // @ts-ignore
|
|
264
|
+
value instanceof Buffer : (value) => typeof value !== "object" || value === null || value instanceof RegExp || value instanceof Date || ArrayBuffer.isView(value)
|
|
265
|
+
);
|
|
266
|
+
const mergeArray = options && typeof options.mergeArray === "function" ? options.mergeArray({ clone, deepmerge: _deepmerge, getKeys, isMergeableObject }) : concatArrays;
|
|
267
|
+
function clone(entry) {
|
|
268
|
+
return isMergeableObject(entry) ? Array.isArray(entry) ? cloneArray(entry) : cloneObject(entry) : entry;
|
|
269
|
+
}
|
|
270
|
+
function mergeObject(target, source) {
|
|
271
|
+
const result = {};
|
|
272
|
+
const targetKeys = getKeys(target);
|
|
273
|
+
const sourceKeys = getKeys(source);
|
|
274
|
+
let i, il, key;
|
|
275
|
+
for (i = 0, il = targetKeys.length; i < il; ++i) {
|
|
276
|
+
key = targetKeys[i];
|
|
277
|
+
sourceKeys.indexOf(key) === -1 && setKey(result, key, clone(target[key]));
|
|
278
|
+
}
|
|
279
|
+
for (i = 0, il = sourceKeys.length; i < il; ++i) {
|
|
280
|
+
key = sourceKeys[i];
|
|
281
|
+
if (targetKeys.indexOf(key) !== -1) {
|
|
282
|
+
if (cloneProtoObject && isMergeableObject(source[key]) && Object.getPrototypeOf(source[key]) !== JSON_PROTO) {
|
|
283
|
+
setKey(result, key, cloneProtoObject(source[key]));
|
|
284
|
+
} else {
|
|
285
|
+
setKey(result, key, _deepmerge(target[key], source[key]));
|
|
286
|
+
}
|
|
287
|
+
} else {
|
|
288
|
+
setKey(result, key, clone(source[key]));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return result;
|
|
292
|
+
}
|
|
293
|
+
function _deepmerge(target, source) {
|
|
294
|
+
const sourceIsArray = Array.isArray(source);
|
|
295
|
+
const targetIsArray = Array.isArray(target);
|
|
296
|
+
if (isPrimitive(source) || source instanceof Map || source instanceof Set) {
|
|
297
|
+
return source;
|
|
298
|
+
} else if (isPrimitiveOrBuiltIn(target)) {
|
|
299
|
+
return clone(source);
|
|
300
|
+
} else if (sourceIsArray && targetIsArray) {
|
|
301
|
+
return mergeArray(target, source);
|
|
302
|
+
} else if (sourceIsArray !== targetIsArray) {
|
|
303
|
+
return clone(source);
|
|
304
|
+
} else {
|
|
305
|
+
return mergeObject(target, source);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function _deepmergeAll() {
|
|
309
|
+
switch (arguments.length) {
|
|
310
|
+
case 0:
|
|
311
|
+
return {};
|
|
312
|
+
case 1:
|
|
313
|
+
return clone(arguments[0]);
|
|
314
|
+
case 2:
|
|
315
|
+
return _deepmerge(arguments[0], arguments[1]);
|
|
316
|
+
}
|
|
317
|
+
let result;
|
|
318
|
+
for (let i = 0, il = arguments.length; i < il; ++i) {
|
|
319
|
+
result = _deepmerge(result, arguments[i]);
|
|
320
|
+
}
|
|
321
|
+
return result;
|
|
322
|
+
}
|
|
323
|
+
return _deepmergeAll;
|
|
324
|
+
}
|
|
325
|
+
function deepmergeArray(options) {
|
|
326
|
+
const deepmerge2 = options.deepmerge;
|
|
327
|
+
const clone = options.clone;
|
|
328
|
+
return function(target, source) {
|
|
329
|
+
let i = 0;
|
|
330
|
+
const sl = source.length;
|
|
331
|
+
const il = Math.max(target.length, source.length);
|
|
332
|
+
const result = new Array(il);
|
|
333
|
+
for (i = 0; i < il; ++i) {
|
|
334
|
+
if (i < sl) {
|
|
335
|
+
result[i] = deepmerge2(target[i], source[i]);
|
|
336
|
+
} else {
|
|
337
|
+
result[i] = clone(target[i]);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return result;
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
var deepmerge = deepmergeConstructor({ mergeArray: deepmergeArray });
|
|
344
|
+
function buildError(ctx, message, received) {
|
|
345
|
+
return [
|
|
346
|
+
{
|
|
347
|
+
message,
|
|
348
|
+
path: [...ctx.path],
|
|
349
|
+
received
|
|
350
|
+
}
|
|
351
|
+
];
|
|
352
|
+
}
|
|
353
|
+
function editDistance(a, b) {
|
|
354
|
+
const d = [];
|
|
355
|
+
for (let i = 0; i <= a.length; i++) {
|
|
356
|
+
d.push([i]);
|
|
357
|
+
for (let j = 1; j <= b.length; j++) {
|
|
358
|
+
d[i][j] = i === 0 ? j : Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
359
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
360
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return d[a.length][b.length];
|
|
365
|
+
}
|
|
366
|
+
function extraPropertyError(ctx, key, allowed, received) {
|
|
367
|
+
if (allowed == null) {
|
|
368
|
+
pushPath(ctx, key);
|
|
369
|
+
const err2 = buildError(ctx, "extra property", received);
|
|
370
|
+
popPath(ctx);
|
|
371
|
+
return err2;
|
|
372
|
+
}
|
|
373
|
+
let nearest = null;
|
|
374
|
+
let best = Math.max(1, Math.floor(key.length / 3));
|
|
375
|
+
for (const k of allowed) {
|
|
376
|
+
const distance = editDistance(key, k);
|
|
377
|
+
if (distance <= best && distance < key.length && (nearest == null || distance < best)) {
|
|
378
|
+
nearest = k;
|
|
379
|
+
best = distance;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
const suggestion = nearest == null ? "" : ` Did you mean ${JSON.stringify(nearest)}?`;
|
|
383
|
+
const list = allowed.length === 0 ? "none" : allowed.map((k) => JSON.stringify(k)).join(", ");
|
|
384
|
+
pushPath(ctx, key);
|
|
385
|
+
const err = buildError(ctx, `extra property.${suggestion} Allowed: ${list}`, received);
|
|
386
|
+
popPath(ctx);
|
|
387
|
+
return err;
|
|
388
|
+
}
|
|
389
|
+
function reportAtPath(ctx, acc, key, runtype, input) {
|
|
390
|
+
if (!runtype.validate(ctx, input)) {
|
|
391
|
+
pushPath(ctx, key);
|
|
392
|
+
acc.push(...runtype.reportDecodeError(ctx, input));
|
|
393
|
+
popPath(ctx);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function pushPath(ctx, key) {
|
|
397
|
+
ctx.path.push(key);
|
|
398
|
+
}
|
|
399
|
+
function popPath(ctx) {
|
|
400
|
+
ctx.path.pop();
|
|
401
|
+
}
|
|
402
|
+
function registerStringFormatter(stringFormatters, name, value) {
|
|
403
|
+
if (typeof value === "function") {
|
|
404
|
+
stringFormatters[name] = {
|
|
405
|
+
validator: value
|
|
406
|
+
};
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
stringFormatters[name] = value;
|
|
410
|
+
}
|
|
411
|
+
function registerNumberFormatter(numberFormatters, name, value) {
|
|
412
|
+
if (typeof value === "function") {
|
|
413
|
+
numberFormatters[name] = {
|
|
414
|
+
validator: value
|
|
415
|
+
};
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
numberFormatters[name] = value;
|
|
419
|
+
}
|
|
420
|
+
function annotateSchema(metadata, schema) {
|
|
421
|
+
if (metadata?.description == null) {
|
|
422
|
+
return schema;
|
|
423
|
+
}
|
|
424
|
+
return {
|
|
425
|
+
...schema,
|
|
426
|
+
description: metadata.description
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
function jsdocDescription(description) {
|
|
430
|
+
const sanitized = description.replace(/\*\//g, "* /");
|
|
431
|
+
const lines = sanitized.split("\n");
|
|
432
|
+
if (lines.length === 1) {
|
|
433
|
+
return `/** ${lines[0]} */`;
|
|
434
|
+
}
|
|
435
|
+
return ["/**", ...lines.map((line) => ` * ${line}`), " */"].join("\n");
|
|
436
|
+
}
|
|
437
|
+
function typeDescription(typeExpr, docText) {
|
|
438
|
+
if (docText == null) {
|
|
439
|
+
return { typeExpr };
|
|
440
|
+
}
|
|
441
|
+
return { typeExpr, docText };
|
|
442
|
+
}
|
|
443
|
+
var BaseRuntype = class {
|
|
444
|
+
metadata;
|
|
445
|
+
constructor(metadata) {
|
|
446
|
+
this.metadata = metadata;
|
|
447
|
+
}
|
|
448
|
+
describe(ctx) {
|
|
449
|
+
return typeDescription(this.describeTypeExpr(ctx), this.metadata?.description);
|
|
450
|
+
}
|
|
451
|
+
describeChildren() {
|
|
452
|
+
return [];
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
function describeTypeExpr(ctx, runtype) {
|
|
456
|
+
return runtype.describe(ctx).typeExpr;
|
|
457
|
+
}
|
|
458
|
+
function renderTypeDescription(description) {
|
|
459
|
+
return description.docText == null ? description.typeExpr : `${jsdocDescription(description.docText)}
|
|
460
|
+
${description.typeExpr}`;
|
|
461
|
+
}
|
|
462
|
+
function renderTypeAlias(name, description) {
|
|
463
|
+
const declaration = `type ${name} = ${description.typeExpr};`;
|
|
464
|
+
return description.docText == null ? declaration : `${jsdocDescription(description.docText)}
|
|
465
|
+
${declaration}`;
|
|
466
|
+
}
|
|
467
|
+
function describePropertyKey(key) {
|
|
468
|
+
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) || /^(0|[1-9][0-9]*)$/.test(key)) {
|
|
469
|
+
return key;
|
|
470
|
+
}
|
|
471
|
+
return JSON.stringify(key);
|
|
472
|
+
}
|
|
473
|
+
function describeObjectMember(ctx, key, value) {
|
|
474
|
+
const optionalMark = value instanceof OptionalFieldRuntype ? "?" : "";
|
|
475
|
+
const description = value.describe(ctx);
|
|
476
|
+
return {
|
|
477
|
+
docText: description.docText,
|
|
478
|
+
member: `${key}${optionalMark}: ${description.typeExpr}`
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
function describeIndexObjectMember(ctx, key, value) {
|
|
482
|
+
return describeObjectMember(ctx, `[K in ${describeTypeExpr(ctx, key)}]`, value);
|
|
483
|
+
}
|
|
484
|
+
function renderObjectMember(member) {
|
|
485
|
+
const renderedMember = `${member.member};`;
|
|
486
|
+
return member.docText == null ? renderedMember : `${jsdocDescription(member.docText)}
|
|
487
|
+
${renderedMember}`;
|
|
488
|
+
}
|
|
489
|
+
function collectDescribeRefs(runtype, ctx) {
|
|
490
|
+
if (runtype instanceof RefRuntype) {
|
|
491
|
+
runtype.collectDescribeRefs(ctx);
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
for (const child of runtype.describeChildren()) {
|
|
495
|
+
collectDescribeRefs(child, ctx);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
var TypeofRuntype = class extends BaseRuntype {
|
|
499
|
+
typeName;
|
|
500
|
+
constructor(metadata, typeName) {
|
|
501
|
+
super(metadata);
|
|
502
|
+
this.typeName = typeName;
|
|
503
|
+
}
|
|
504
|
+
describeTypeExpr(_ctx) {
|
|
505
|
+
return this.typeName;
|
|
506
|
+
}
|
|
507
|
+
schema(_ctx) {
|
|
508
|
+
return annotateSchema(this.metadata, { type: this.typeName });
|
|
509
|
+
}
|
|
510
|
+
validate(_ctx, input) {
|
|
511
|
+
return typeof input === this.typeName;
|
|
512
|
+
}
|
|
513
|
+
parseAfterValidation(_ctx, input) {
|
|
514
|
+
return input;
|
|
515
|
+
}
|
|
516
|
+
reportDecodeError(ctx, input) {
|
|
517
|
+
return buildError(ctx, "expected " + this.typeName, input);
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
var NUMBER_KEY_PATTERN = "^-?(0|[1-9][0-9]*)(\\.[0-9]+)?([eE][+-]?[0-9]+)?$";
|
|
521
|
+
var NUMBER_KEY_REGEX = new RegExp(NUMBER_KEY_PATTERN);
|
|
522
|
+
var AnyRuntype = class extends BaseRuntype {
|
|
523
|
+
constructor(metadata) {
|
|
524
|
+
super(metadata);
|
|
525
|
+
}
|
|
526
|
+
describeTypeExpr(_ctx) {
|
|
527
|
+
return "any";
|
|
528
|
+
}
|
|
529
|
+
schema(_ctx) {
|
|
530
|
+
return annotateSchema(this.metadata, {});
|
|
531
|
+
}
|
|
532
|
+
validate(_ctx, _input) {
|
|
533
|
+
return true;
|
|
534
|
+
}
|
|
535
|
+
parseAfterValidation(_ctx, input) {
|
|
536
|
+
return input;
|
|
537
|
+
}
|
|
538
|
+
reportDecodeError(ctx, input) {
|
|
539
|
+
return buildError(ctx, "expected any", input);
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
var NeverRuntype = class extends BaseRuntype {
|
|
543
|
+
constructor(metadata) {
|
|
544
|
+
super(metadata);
|
|
545
|
+
}
|
|
546
|
+
describeTypeExpr(_ctx) {
|
|
547
|
+
return "never";
|
|
548
|
+
}
|
|
549
|
+
schema(_ctx) {
|
|
550
|
+
return annotateSchema(this.metadata, { anyOf: [] });
|
|
551
|
+
}
|
|
552
|
+
validate(_ctx, _input) {
|
|
553
|
+
return false;
|
|
554
|
+
}
|
|
555
|
+
parseAfterValidation(_ctx, _input) {
|
|
556
|
+
throw new Error("unreachable");
|
|
557
|
+
}
|
|
558
|
+
reportDecodeError(ctx, input) {
|
|
559
|
+
return buildError(ctx, "expected never", input);
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
var ArrayRuntype = class extends BaseRuntype {
|
|
563
|
+
itemParser;
|
|
564
|
+
constructor(metadata, itemParser) {
|
|
565
|
+
super(metadata);
|
|
566
|
+
this.itemParser = itemParser;
|
|
567
|
+
}
|
|
568
|
+
schema(ctx) {
|
|
569
|
+
pushPath(ctx, "[]");
|
|
570
|
+
const items = this.itemParser.schema(ctx);
|
|
571
|
+
popPath(ctx);
|
|
572
|
+
return annotateSchema(this.metadata, {
|
|
573
|
+
type: "array",
|
|
574
|
+
items
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
validate(ctx, input) {
|
|
578
|
+
if (Array.isArray(input)) {
|
|
579
|
+
for (let i = 0; i < input.length; i++) {
|
|
580
|
+
const v = input[i];
|
|
581
|
+
const ok = this.itemParser.validate(ctx, v);
|
|
582
|
+
if (!ok) {
|
|
583
|
+
return false;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
return false;
|
|
589
|
+
}
|
|
590
|
+
parseAfterValidation(ctx, input) {
|
|
591
|
+
return input.map((v) => this.itemParser.parseAfterValidation(ctx, v));
|
|
592
|
+
}
|
|
593
|
+
reportDecodeError(ctx, input) {
|
|
594
|
+
if (!Array.isArray(input)) {
|
|
595
|
+
return buildError(ctx, "expected array", input);
|
|
596
|
+
}
|
|
597
|
+
let acc = [];
|
|
598
|
+
for (let i = 0; i < input.length; i++) {
|
|
599
|
+
reportAtPath(ctx, acc, `[${i}]`, this.itemParser, input[i]);
|
|
600
|
+
}
|
|
601
|
+
return acc;
|
|
602
|
+
}
|
|
603
|
+
describeTypeExpr(ctx) {
|
|
604
|
+
return `Array<${describeTypeExpr(ctx, this.itemParser)}>`;
|
|
605
|
+
}
|
|
606
|
+
describeChildren() {
|
|
607
|
+
return [this.itemParser];
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
var OptionalFieldRuntype = class {
|
|
611
|
+
t;
|
|
612
|
+
constructor(t) {
|
|
613
|
+
this.t = t;
|
|
614
|
+
}
|
|
615
|
+
schema(ctx) {
|
|
616
|
+
const inner = this.t.schema(ctx);
|
|
617
|
+
return {
|
|
618
|
+
anyOf: [inner, { type: "null" }]
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
validate(ctx, input) {
|
|
622
|
+
if (input == null) {
|
|
623
|
+
return true;
|
|
624
|
+
}
|
|
625
|
+
return this.t.validate(ctx, input);
|
|
626
|
+
}
|
|
627
|
+
parseAfterValidation(ctx, input) {
|
|
628
|
+
if (input == null) {
|
|
629
|
+
return input;
|
|
630
|
+
}
|
|
631
|
+
return this.t.parseAfterValidation(ctx, input);
|
|
632
|
+
}
|
|
633
|
+
// validate() returns true for null/undefined, so reportDecodeError is only
|
|
634
|
+
// called when input is non-null. Reporting "expected nullish value" here
|
|
635
|
+
// would be noise — only the inner type's errors are relevant.
|
|
636
|
+
reportDecodeError(ctx, input) {
|
|
637
|
+
return this.t.reportDecodeError(ctx, input);
|
|
638
|
+
}
|
|
639
|
+
describe(ctx) {
|
|
640
|
+
return this.t.describe(ctx);
|
|
641
|
+
}
|
|
642
|
+
describeChildren() {
|
|
643
|
+
return [this.t];
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
var hasOwn = Object.prototype.hasOwnProperty;
|
|
647
|
+
function setOwnProperty(target, key, value) {
|
|
648
|
+
if (key === "__proto__") {
|
|
649
|
+
Object.defineProperty(target, key, { value, enumerable: true, writable: true, configurable: true });
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
target[key] = value;
|
|
653
|
+
}
|
|
654
|
+
var ObjectRuntype = class extends BaseRuntype {
|
|
655
|
+
properties;
|
|
656
|
+
indexedPropertiesParser;
|
|
657
|
+
constructor(metadata, properties, indexedPropertiesParser) {
|
|
658
|
+
super(metadata);
|
|
659
|
+
this.properties = properties;
|
|
660
|
+
this.indexedPropertiesParser = indexedPropertiesParser;
|
|
661
|
+
}
|
|
662
|
+
describeTypeExpr(ctx) {
|
|
663
|
+
const sortedKeys = Object.keys(this.properties).sort();
|
|
664
|
+
const props = sortedKeys.map((k) => {
|
|
665
|
+
const it = this.properties[k];
|
|
666
|
+
return describeObjectMember(ctx, describePropertyKey(k), it);
|
|
667
|
+
});
|
|
668
|
+
const indexProps = this.indexedPropertiesParser.map(({ key, value }) => describeIndexObjectMember(ctx, key, value));
|
|
669
|
+
const members = [...props, ...indexProps];
|
|
670
|
+
const hasMemberDescriptions = members.some((it) => it.docText != null);
|
|
671
|
+
if (hasMemberDescriptions) {
|
|
672
|
+
if (members.length === 0) {
|
|
673
|
+
return "{}";
|
|
674
|
+
}
|
|
675
|
+
const content2 = members.map(renderObjectMember).join("\n");
|
|
676
|
+
return `{
|
|
677
|
+
${content2}
|
|
678
|
+
}`;
|
|
679
|
+
}
|
|
680
|
+
const content = members.map((it) => it.member).join(", ");
|
|
681
|
+
return `{ ${content} }`;
|
|
682
|
+
}
|
|
683
|
+
describeChildren() {
|
|
684
|
+
return [
|
|
685
|
+
...Object.values(this.properties),
|
|
686
|
+
...this.indexedPropertiesParser.flatMap(({ key, value }) => [key, value])
|
|
687
|
+
];
|
|
688
|
+
}
|
|
689
|
+
schema(ctx) {
|
|
690
|
+
const properties = {};
|
|
691
|
+
const optionalized = /* @__PURE__ */ new Set();
|
|
692
|
+
for (const k in this.properties) {
|
|
693
|
+
pushPath(ctx, k);
|
|
694
|
+
const item = this.properties[k];
|
|
695
|
+
const raw = item.schema(ctx);
|
|
696
|
+
const rewrite = removeNullUnionBranch(raw);
|
|
697
|
+
if (rewrite != null) {
|
|
698
|
+
properties[k] = rewrite;
|
|
699
|
+
optionalized.add(k);
|
|
700
|
+
} else {
|
|
701
|
+
properties[k] = raw;
|
|
702
|
+
}
|
|
703
|
+
popPath(ctx);
|
|
704
|
+
}
|
|
705
|
+
const required = Object.keys(this.properties).filter((k) => !optionalized.has(k));
|
|
706
|
+
const base = {
|
|
707
|
+
type: "object",
|
|
708
|
+
properties,
|
|
709
|
+
...required.length > 0 ? { required } : {}
|
|
710
|
+
};
|
|
711
|
+
const indexSchemas = this.indexedPropertiesParser.map(({ key, value }) => {
|
|
712
|
+
pushPath(ctx, "[key]");
|
|
713
|
+
const keySchema = key.schema(ctx);
|
|
714
|
+
popPath(ctx);
|
|
715
|
+
pushPath(ctx, "[value]");
|
|
716
|
+
const valueSchema = value.schema(ctx);
|
|
717
|
+
popPath(ctx);
|
|
718
|
+
return {
|
|
719
|
+
type: "object",
|
|
720
|
+
additionalProperties: valueSchema,
|
|
721
|
+
propertyNames: keySchema
|
|
722
|
+
};
|
|
723
|
+
});
|
|
724
|
+
if (indexSchemas.length === 0) {
|
|
725
|
+
return annotateSchema(this.metadata, { ...base, additionalProperties: false });
|
|
726
|
+
}
|
|
727
|
+
if (Object.keys(properties).length === 0 && indexSchemas.length === 1) {
|
|
728
|
+
const valueRuntype = this.indexedPropertiesParser[0].value;
|
|
729
|
+
if (valueRuntype instanceof NeverRuntype) {
|
|
730
|
+
return annotateSchema(this.metadata, { type: "object", additionalProperties: false });
|
|
731
|
+
}
|
|
732
|
+
if (valueRuntype instanceof AnyRuntype) {
|
|
733
|
+
return annotateSchema(this.metadata, { ...indexSchemas[0], additionalProperties: true });
|
|
734
|
+
}
|
|
735
|
+
return annotateSchema(this.metadata, indexSchemas[0]);
|
|
736
|
+
}
|
|
737
|
+
return annotateSchema(this.metadata, {
|
|
738
|
+
allOf: [base, ...indexSchemas]
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
validate(ctx, input) {
|
|
742
|
+
if (typeof input === "object" && !Array.isArray(input) && input !== null) {
|
|
743
|
+
const configKeys = Object.keys(this.properties);
|
|
744
|
+
for (const k of configKeys) {
|
|
745
|
+
const validator = this.properties[k];
|
|
746
|
+
if (!validator.validate(ctx, input[k])) {
|
|
747
|
+
return false;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
if (this.indexedPropertiesParser.length > 0) {
|
|
751
|
+
const inputKeys = Object.keys(input);
|
|
752
|
+
const extraKeys = inputKeys.filter((k) => !hasOwn.call(this.properties, k));
|
|
753
|
+
for (const k of extraKeys) {
|
|
754
|
+
let isValid = false;
|
|
755
|
+
for (const p of this.indexedPropertiesParser) {
|
|
756
|
+
if (!p.key.validate(ctx, k)) {
|
|
757
|
+
continue;
|
|
758
|
+
}
|
|
759
|
+
const v = input[k];
|
|
760
|
+
if (!p.value.validate(ctx, v)) {
|
|
761
|
+
continue;
|
|
762
|
+
}
|
|
763
|
+
isValid = true;
|
|
764
|
+
break;
|
|
765
|
+
}
|
|
766
|
+
if (!isValid && !this.leavesKeyToIntersection(ctx, input, k)) {
|
|
767
|
+
return false;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
} else {
|
|
771
|
+
if (ctx.disallowExtraProperties && ctx.intersectionInput !== input) {
|
|
772
|
+
const inputKeys = Object.keys(input);
|
|
773
|
+
const extraKeys = inputKeys.filter((k) => !hasOwn.call(this.properties, k));
|
|
774
|
+
if (extraKeys.length > 0) {
|
|
775
|
+
return false;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
return true;
|
|
780
|
+
}
|
|
781
|
+
return false;
|
|
782
|
+
}
|
|
783
|
+
parseAfterValidation(ctx, input) {
|
|
784
|
+
let acc = {};
|
|
785
|
+
const inputKeys = Object.keys(input);
|
|
786
|
+
if (ctx.objectKeyOrder === "input") {
|
|
787
|
+
for (const k of inputKeys) {
|
|
788
|
+
if (hasOwn.call(this.properties, k)) {
|
|
789
|
+
setOwnProperty(acc, k, this.properties[k].parseAfterValidation(ctx, input[k]));
|
|
790
|
+
continue;
|
|
791
|
+
}
|
|
792
|
+
for (const p of this.indexedPropertiesParser) {
|
|
793
|
+
const v = input[k];
|
|
794
|
+
const isValid = p.key.validate(ctx, k) && p.value.validate(ctx, v);
|
|
795
|
+
if (isValid) {
|
|
796
|
+
const itemParsed = p.value.parseAfterValidation(ctx, v);
|
|
797
|
+
const keyParsed = p.key.parseAfterValidation(ctx, k);
|
|
798
|
+
setOwnProperty(acc, keyParsed, itemParsed);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
} else {
|
|
803
|
+
const configKeys = Object.keys(this.properties).sort();
|
|
804
|
+
for (const k of configKeys) {
|
|
805
|
+
if (!hasOwn.call(input, k)) {
|
|
806
|
+
continue;
|
|
807
|
+
}
|
|
808
|
+
const v = input[k];
|
|
809
|
+
const itemParsed = this.properties[k].parseAfterValidation(ctx, v);
|
|
810
|
+
setOwnProperty(acc, k, itemParsed);
|
|
811
|
+
}
|
|
812
|
+
if (this.indexedPropertiesParser.length > 0) {
|
|
813
|
+
const extraKeys = inputKeys.filter((k) => !hasOwn.call(this.properties, k)).sort();
|
|
814
|
+
for (const k of extraKeys) {
|
|
815
|
+
const v = input[k];
|
|
816
|
+
for (const p of this.indexedPropertiesParser) {
|
|
817
|
+
const isValid = p.key.validate(ctx, k) && p.value.validate(ctx, v);
|
|
818
|
+
if (isValid) {
|
|
819
|
+
const itemParsed = p.value.parseAfterValidation(ctx, v);
|
|
820
|
+
const keyParsed = p.key.parseAfterValidation(ctx, k);
|
|
821
|
+
setOwnProperty(acc, keyParsed, itemParsed);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
return acc;
|
|
828
|
+
}
|
|
829
|
+
reportDecodeError(ctx, input) {
|
|
830
|
+
if (typeof input !== "object" || Array.isArray(input) || input === null) {
|
|
831
|
+
return buildError(ctx, "expected object", input);
|
|
832
|
+
}
|
|
833
|
+
let acc = [];
|
|
834
|
+
const configKeys = Object.keys(this.properties);
|
|
835
|
+
for (const k of configKeys) {
|
|
836
|
+
reportAtPath(ctx, acc, k, this.properties[k], input[k]);
|
|
837
|
+
}
|
|
838
|
+
if (this.indexedPropertiesParser.length > 0) {
|
|
839
|
+
const inputKeys = Object.keys(input);
|
|
840
|
+
const extraKeys = inputKeys.filter((k) => !hasOwn.call(this.properties, k));
|
|
841
|
+
for (const k of extraKeys) {
|
|
842
|
+
if (this.leavesKeyToIntersection(ctx, input, k)) {
|
|
843
|
+
continue;
|
|
844
|
+
}
|
|
845
|
+
for (const p of this.indexedPropertiesParser) {
|
|
846
|
+
const keyOk = p.key.validate(ctx, k);
|
|
847
|
+
const valueOk = p.value.validate(ctx, input[k]);
|
|
848
|
+
const ok = keyOk && valueOk;
|
|
849
|
+
if (!ok) {
|
|
850
|
+
pushPath(ctx, k);
|
|
851
|
+
if (!keyOk) {
|
|
852
|
+
const keyReported = p.key.reportDecodeError(ctx, k);
|
|
853
|
+
acc.push(...keyReported);
|
|
854
|
+
}
|
|
855
|
+
if (!valueOk) {
|
|
856
|
+
const valueReported = p.value.reportDecodeError(ctx, input[k]);
|
|
857
|
+
acc.push(...valueReported);
|
|
858
|
+
}
|
|
859
|
+
popPath(ctx);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
} else {
|
|
864
|
+
if (ctx.disallowExtraProperties && ctx.intersectionInput !== input) {
|
|
865
|
+
const inputKeys = Object.keys(input);
|
|
866
|
+
const extraKeys = inputKeys.filter((k) => !hasOwn.call(this.properties, k));
|
|
867
|
+
if (extraKeys.length > 0) {
|
|
868
|
+
return extraKeys.flatMap((k) => extraPropertyError(ctx, k, configKeys, input[k]));
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
return acc;
|
|
873
|
+
}
|
|
874
|
+
allowedKeys() {
|
|
875
|
+
return this.indexedPropertiesParser.length > 0 ? null : Object.keys(this.properties);
|
|
876
|
+
}
|
|
877
|
+
// In an intersection, an index signature only constrains the keys it matches: another member may know the rest.
|
|
878
|
+
leavesKeyToIntersection(ctx, input, k) {
|
|
879
|
+
return ctx.intersectionInput === input && !this.indexedPropertiesParser.some((p) => p.key.validate(ctx, k));
|
|
880
|
+
}
|
|
881
|
+
};
|
|
882
|
+
var RefRuntype = class extends BaseRuntype {
|
|
883
|
+
refName;
|
|
884
|
+
namedRuntypes;
|
|
885
|
+
constructor(metadata, refName, namedRuntypes) {
|
|
886
|
+
super(metadata);
|
|
887
|
+
this.refName = refName;
|
|
888
|
+
this.namedRuntypes = namedRuntypes;
|
|
889
|
+
}
|
|
890
|
+
getNamedRuntypes() {
|
|
891
|
+
return this.namedRuntypes;
|
|
892
|
+
}
|
|
893
|
+
describeTypeExpr(_ctx) {
|
|
894
|
+
return this.refName;
|
|
895
|
+
}
|
|
896
|
+
collectDescribeRefs(ctx) {
|
|
897
|
+
const name = this.refName;
|
|
898
|
+
ctx.refCounts[name] = (ctx.refCounts[name] || 0) + 1;
|
|
899
|
+
if (ctx.activeRefs.has(name)) {
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
if (ctx.visitedRefs.has(name)) {
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
ctx.visitedRefs.add(name);
|
|
906
|
+
ctx.activeRefs.add(name);
|
|
907
|
+
collectDescribeRefs(this.getNamedRuntypes()[name], ctx);
|
|
908
|
+
ctx.activeRefs.delete(name);
|
|
909
|
+
}
|
|
910
|
+
describe(ctx) {
|
|
911
|
+
const name = this.refName;
|
|
912
|
+
const to = this.getNamedRuntypes()[this.refName];
|
|
913
|
+
const refDescription = () => typeDescription(name, this.metadata?.description);
|
|
914
|
+
if (ctx.refCounts[name] > 1) {
|
|
915
|
+
if (ctx.activeRefs.has(name)) {
|
|
916
|
+
return refDescription();
|
|
917
|
+
}
|
|
918
|
+
if (ctx.definitions[name] == null) {
|
|
919
|
+
ctx.activeRefs.add(name);
|
|
920
|
+
ctx.definitions[name] = to.describe(ctx);
|
|
921
|
+
ctx.activeRefs.delete(name);
|
|
922
|
+
}
|
|
923
|
+
return refDescription();
|
|
924
|
+
}
|
|
925
|
+
const target = to.describe(ctx);
|
|
926
|
+
return this.metadata?.description == null ? target : typeDescription(target.typeExpr, this.metadata.description);
|
|
927
|
+
}
|
|
928
|
+
schema(ctx) {
|
|
929
|
+
const name = this.refName;
|
|
930
|
+
const to = this.getNamedRuntypes()[this.refName];
|
|
931
|
+
if (ctx.mode === "contextual") {
|
|
932
|
+
const printingContext = ctx.printingContext;
|
|
933
|
+
if (printingContext == null) {
|
|
934
|
+
throw new Error("INTERNAL ERROR: Missing SchemaPrintingContext");
|
|
935
|
+
}
|
|
936
|
+
if (!printingContext.hasDefinition(name) && !printingContext.isDefinitionInProgress(name)) {
|
|
937
|
+
printingContext.markDefinitionInProgress(name);
|
|
938
|
+
const schemaTarget = printingContext.getNamedTypeSchemaOverride(name) ?? to;
|
|
939
|
+
const body = schemaTarget.schema(ctx);
|
|
940
|
+
printingContext.storeDefinition(name, body);
|
|
941
|
+
}
|
|
942
|
+
return annotateSchema(this.metadata, { $ref: printingContext.getRef(name) });
|
|
943
|
+
}
|
|
944
|
+
if (ctx.seen[name]) {
|
|
945
|
+
return annotateSchema(this.metadata, {});
|
|
946
|
+
}
|
|
947
|
+
ctx.seen[name] = true;
|
|
948
|
+
var tmp = to.schema(ctx);
|
|
949
|
+
delete ctx.seen[name];
|
|
950
|
+
return annotateSchema(this.metadata, tmp);
|
|
951
|
+
}
|
|
952
|
+
validate(ctx, input) {
|
|
953
|
+
const to = this.getNamedRuntypes()[this.refName];
|
|
954
|
+
return to.validate(ctx, input);
|
|
955
|
+
}
|
|
956
|
+
parseAfterValidation(ctx, input) {
|
|
957
|
+
const to = this.getNamedRuntypes()[this.refName];
|
|
958
|
+
return to.parseAfterValidation(ctx, input);
|
|
959
|
+
}
|
|
960
|
+
reportDecodeError(ctx, input) {
|
|
961
|
+
const to = this.getNamedRuntypes()[this.refName];
|
|
962
|
+
return to.reportDecodeError(ctx, input);
|
|
963
|
+
}
|
|
964
|
+
};
|
|
965
|
+
var ParserFromRuntype = class {
|
|
966
|
+
_runtype;
|
|
967
|
+
name;
|
|
968
|
+
hideTypeNameInDescribe;
|
|
969
|
+
constructor(runtype, name, hideTypeNameInDescribe) {
|
|
970
|
+
this._runtype = runtype;
|
|
971
|
+
this.name = name;
|
|
972
|
+
this.hideTypeNameInDescribe = hideTypeNameInDescribe;
|
|
973
|
+
}
|
|
974
|
+
parse(input, options) {
|
|
975
|
+
const safe = this.safeParse(input, options);
|
|
976
|
+
if (safe.success) {
|
|
977
|
+
return safe.data;
|
|
978
|
+
}
|
|
979
|
+
const explained = printErrors(safe.errors, []);
|
|
980
|
+
throw new Error(`Failed to parse ${this.name} - ${explained}`);
|
|
981
|
+
}
|
|
982
|
+
safeParse(input, options) {
|
|
983
|
+
const disallowExtraProperties = options?.disallowExtraProperties ?? false;
|
|
984
|
+
const objectKeyOrder = options?.objectKeyOrder ?? "input";
|
|
985
|
+
const ok = this.validate(input, options);
|
|
986
|
+
if (ok) {
|
|
987
|
+
let ctx2 = { disallowExtraProperties, objectKeyOrder };
|
|
988
|
+
const parsed = this._runtype.parseAfterValidation(ctx2, input);
|
|
989
|
+
return { success: true, data: parsed };
|
|
990
|
+
}
|
|
991
|
+
let ctx = { path: [], disallowExtraProperties };
|
|
992
|
+
return {
|
|
993
|
+
success: false,
|
|
994
|
+
errors: this._runtype.reportDecodeError(ctx, input).slice(0, 10)
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
validate(input, options) {
|
|
998
|
+
const disallowExtraProperties = options?.disallowExtraProperties ?? false;
|
|
999
|
+
const ctx = { disallowExtraProperties };
|
|
1000
|
+
const ok = this._runtype.validate(ctx, input);
|
|
1001
|
+
if (typeof ok !== "boolean") {
|
|
1002
|
+
throw new Error("INTERNAL ERROR: Expected boolean");
|
|
1003
|
+
}
|
|
1004
|
+
return ok;
|
|
1005
|
+
}
|
|
1006
|
+
schema() {
|
|
1007
|
+
const ctx = {
|
|
1008
|
+
path: [],
|
|
1009
|
+
seen: {},
|
|
1010
|
+
mode: "flat"
|
|
1011
|
+
};
|
|
1012
|
+
return this._runtype.schema(ctx);
|
|
1013
|
+
}
|
|
1014
|
+
schemaWithContext(schemaPrintingContext) {
|
|
1015
|
+
const ctx = {
|
|
1016
|
+
path: [],
|
|
1017
|
+
seen: {},
|
|
1018
|
+
mode: "contextual",
|
|
1019
|
+
printingContext: schemaPrintingContext
|
|
1020
|
+
};
|
|
1021
|
+
return this._runtype.schema(ctx);
|
|
1022
|
+
}
|
|
1023
|
+
describe() {
|
|
1024
|
+
const ctx = {
|
|
1025
|
+
activeRefs: /* @__PURE__ */ new Set(),
|
|
1026
|
+
definitions: {},
|
|
1027
|
+
refCounts: {},
|
|
1028
|
+
visitedRefs: /* @__PURE__ */ new Set()
|
|
1029
|
+
};
|
|
1030
|
+
collectDescribeRefs(this._runtype, ctx);
|
|
1031
|
+
const out = this._runtype.describe(ctx);
|
|
1032
|
+
let sortedDepsKeys = Object.keys(ctx.definitions).sort();
|
|
1033
|
+
const depsPart = sortedDepsKeys.map((key) => [key, ctx.definitions[key]]).map(([key, description]) => renderTypeAlias(key, description)).join("\n\n");
|
|
1034
|
+
if (this.hideTypeNameInDescribe) {
|
|
1035
|
+
return [depsPart, renderTypeDescription(out)].filter((it) => it != null && it.length > 0).join("\n\n");
|
|
1036
|
+
}
|
|
1037
|
+
const outPart = renderTypeAlias(`Codec${this.name}`, out);
|
|
1038
|
+
return [depsPart, outPart].filter((it) => it != null && it.length > 0).join("\n\n");
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
var buildParserFromRuntype = (runtype, name, hideTypeNameInDescribe) => new ParserFromRuntype(runtype, name, hideTypeNameInDescribe);
|
|
1042
|
+
var buildParsers = (generated, args) => {
|
|
1043
|
+
const stringFormats = args?.stringFormats ?? {};
|
|
1044
|
+
for (const k of generated.requiredStringFormats) {
|
|
1045
|
+
if (stringFormats[k] == null) {
|
|
1046
|
+
throw new Error(`Missing custom format ${k}`);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
Object.keys(stringFormats).forEach((k) => {
|
|
1050
|
+
const v = stringFormats[k];
|
|
1051
|
+
registerStringFormatter(generated.stringFormatters, k, v);
|
|
1052
|
+
});
|
|
1053
|
+
const numberFormats = args?.numberFormats ?? {};
|
|
1054
|
+
for (const k of generated.requiredNumberFormats) {
|
|
1055
|
+
if (numberFormats[k] == null) {
|
|
1056
|
+
throw new Error(`Missing custom format ${k}`);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
Object.keys(numberFormats).forEach((k) => {
|
|
1060
|
+
const v = numberFormats[k];
|
|
1061
|
+
registerNumberFormatter(generated.numberFormatters, k, v);
|
|
1062
|
+
});
|
|
1063
|
+
let acc = {};
|
|
1064
|
+
for (const k of Object.keys(generated.parsers)) {
|
|
1065
|
+
const it = buildParserFromRuntype(generated.parsers[k], k, false);
|
|
1066
|
+
acc[k] = it;
|
|
1067
|
+
}
|
|
1068
|
+
return acc;
|
|
1069
|
+
};
|
|
1070
|
+
|
|
1071
|
+
export {
|
|
1072
|
+
timeChapter,
|
|
1073
|
+
narrateScript,
|
|
1074
|
+
scriptAudio,
|
|
1075
|
+
wav,
|
|
1076
|
+
TypeofRuntype,
|
|
1077
|
+
AnyRuntype,
|
|
1078
|
+
ArrayRuntype,
|
|
1079
|
+
OptionalFieldRuntype,
|
|
1080
|
+
ObjectRuntype,
|
|
1081
|
+
RefRuntype,
|
|
1082
|
+
buildParsers
|
|
1083
|
+
};
|