yeptris 0.6.59 → 0.6.61
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 +98 -20
- package/dist/yeptris.mjs +1 -1
- package/dist/yeptris.wasm +0 -0
- package/package.json +1 -1
- package/src/index.d.ts +245 -7
- package/src/index.js +819 -63
- package/src/yeptris_wasm.c +415 -5
package/src/index.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
// The public surface: YAML <-> JSON at libyeptris speed.
|
|
2
2
|
// Engine FFI through ../dist/yeptris.mjs (built by build.sh).
|
|
3
|
+
//
|
|
4
|
+
// Two call postures over one initialized module: the async forms
|
|
5
|
+
// (load/dump/validate/...) initialize the wasm on demand and stay the
|
|
6
|
+
// default entry; the *Sync forms (loadSync/dumpSync/validateSync/...)
|
|
7
|
+
// run synchronously against the module an earlier await already
|
|
8
|
+
// initialized (the wasm executes synchronously — init-then-work is
|
|
9
|
+
// sync by construction). Every exported function shares its body
|
|
10
|
+
// with its Sync twin through the *Core helpers below, so the two
|
|
11
|
+
// postures cannot drift.
|
|
3
12
|
|
|
4
13
|
function cstr(mod, s) {
|
|
5
14
|
const bytes = mod.lengthBytesUTF8(s) + 1;
|
|
@@ -21,14 +30,32 @@ class YamlError extends Error {
|
|
|
21
30
|
}
|
|
22
31
|
}
|
|
23
32
|
|
|
24
|
-
|
|
33
|
+
/** Raised by load/loadAll when the input PARSES but the schema walk
|
|
34
|
+
* (schema: / jsonSchema:) produced diagnostics. Carries the whole
|
|
35
|
+
* report as `diagnostics` — never first-fail, never a raw pointer.
|
|
36
|
+
* Not a YamlError: the input is well-formed (the same split the
|
|
37
|
+
* ruby/py faces draw between their parse errors and
|
|
38
|
+
* Validation::Error / ValidationError). */
|
|
39
|
+
export class ValidationError extends Error {
|
|
40
|
+
constructor(diagnostics) {
|
|
41
|
+
const first = diagnostics[0];
|
|
42
|
+
const detail = first
|
|
43
|
+
? `${first.keyword} at ${first.pointer || "(root)"}: ${first.message}`
|
|
44
|
+
: "no diagnostics";
|
|
45
|
+
super(`validation failed (${diagnostics.length} diagnostics): ${detail}`);
|
|
46
|
+
this.name = "ValidationError";
|
|
47
|
+
this.diagnostics = diagnostics;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function run(mod, fn, input, ...args) {
|
|
25
52
|
const [ptr, len] = cstr(mod, input);
|
|
26
53
|
try {
|
|
27
54
|
// len is the UTF-8 byte count: input.length counts UTF-16 code
|
|
28
55
|
// units, which is strictly shorter than the wire bytes for any
|
|
29
56
|
// non-ASCII text (é is 1 unit / 2 bytes) — every such document
|
|
30
57
|
// parsed truncated
|
|
31
|
-
const st = fn(ptr, len);
|
|
58
|
+
const st = fn(ptr, len, ...args);
|
|
32
59
|
if (st !== 0) {
|
|
33
60
|
const msg = mod.UTF8ToString(mod._yeptris_wasm_last_error());
|
|
34
61
|
throw new YamlError(
|
|
@@ -62,12 +89,23 @@ async function mod() {
|
|
|
62
89
|
"_yeptris_wasm_load",
|
|
63
90
|
"_yeptris_wasm_load_all",
|
|
64
91
|
"_yeptris_wasm_dump",
|
|
92
|
+
"_yeptris_wasm_dump_ex",
|
|
65
93
|
"_yeptris_wasm_events",
|
|
66
94
|
"_yeptris_wasm_event_records",
|
|
67
95
|
"_yeptris_wasm_event_count",
|
|
68
96
|
"_yeptris_wasm_event_arena",
|
|
69
97
|
"_yeptris_wasm_event_arena_len",
|
|
70
98
|
"_yeptris_wasm_events_free",
|
|
99
|
+
"_yeptris_wasm_each_open",
|
|
100
|
+
"_yeptris_wasm_each_next",
|
|
101
|
+
"_yeptris_wasm_each_frame",
|
|
102
|
+
"_yeptris_wasm_each_index",
|
|
103
|
+
"_yeptris_wasm_each_close",
|
|
104
|
+
"_yeptris_wasm_validate",
|
|
105
|
+
"_yeptris_wasm_json_schema_validate",
|
|
106
|
+
"_yeptris_wasm_schema_compile",
|
|
107
|
+
"_yeptris_wasm_schema_validate",
|
|
108
|
+
"_yeptris_wasm_schema_free",
|
|
71
109
|
"_yeptris_wasm_out",
|
|
72
110
|
"_yeptris_wasm_out_len",
|
|
73
111
|
"_yeptris_wasm_out_free",
|
|
@@ -86,34 +124,641 @@ async function mod() {
|
|
|
86
124
|
return cached;
|
|
87
125
|
}
|
|
88
126
|
|
|
89
|
-
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
|
|
94
|
-
|
|
127
|
+
/** Initialize the wasm module (idempotent). The async forms call this
|
|
128
|
+
* on demand; calling it first is what unlocks the Sync forms
|
|
129
|
+
* (loadSync/dumpSync/validateSync/...), which run synchronously
|
|
130
|
+
* against the same module instance. */
|
|
131
|
+
export async function init() {
|
|
132
|
+
await mod();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The module this package caches, when an async call (or init())
|
|
136
|
+
* has already initialized it; undefined before that. The Sync forms
|
|
137
|
+
* exist for the initialized posture only — they refuse to lazily
|
|
138
|
+
* import (that door is async by nature). */
|
|
139
|
+
function requireMod(who) {
|
|
140
|
+
if (!cached) {
|
|
141
|
+
throw new Error(
|
|
142
|
+
`yeptris: ${who} needs an initialized module — await init() first ` +
|
|
143
|
+
"(the async forms initialize on demand)",
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
return cached;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ---- the validate face (validate.h) ------------------------------------
|
|
150
|
+
//
|
|
151
|
+
// The engine's three doors, all fused parse+validate over the same
|
|
152
|
+
// front door load uses (encodings, BOM, every document of a stream):
|
|
153
|
+
// the SCHEMA-FAMILY walk (yeptris_validate — the YeptrisSchema kind
|
|
154
|
+
// rides as a parameter), the one-shot JSON SCHEMA walk (draft 2020-12
|
|
155
|
+
// subset), and the COMPILED economy (yeptris_schema_compile: parse
|
|
156
|
+
// the schema once, validate many streams). The engine carries NO kind
|
|
157
|
+
// on the compile door — a compiled handle is a JSON-Schema document
|
|
158
|
+
// by validate.h's contract; the family-kind selection lives on the
|
|
159
|
+
// validate door, spelled exactly as the C surface spells it.
|
|
160
|
+
//
|
|
161
|
+
// Diagnostics cross as data: the whole report drains as one JSON
|
|
162
|
+
// array of raw records (code/path/message/line/col/offset) and is
|
|
163
|
+
// mapped here into plain objects. The code -> keyword table is
|
|
164
|
+
// binding-owned (the same law the ruby/py faces follow); `path` is
|
|
165
|
+
// the JSON-pointer split into ~0/~1-unescaped segments, `pointer` its
|
|
166
|
+
// raw string form.
|
|
167
|
+
|
|
168
|
+
// YeptrisDiagCode -> the spec-spelled keyword (validate.h). The
|
|
169
|
+
// 2020-12 walk's codes name their JSON Schema keyword; the
|
|
170
|
+
// schema-family walk's codes name the violated YAML rule. Values are
|
|
171
|
+
// ABI-pinned (validate.h, appending-only).
|
|
172
|
+
const DIAG_KEYWORDS = {
|
|
173
|
+
0: "none",
|
|
174
|
+
1: "tag", // explicit tag contradicts content
|
|
175
|
+
2: "keyType", // json12: a mapping key is not a string
|
|
176
|
+
3: "jsonType", // json12: plain scalar matches no JSON production
|
|
177
|
+
4: "duplicateKey",
|
|
178
|
+
5: "type",
|
|
179
|
+
6: "required",
|
|
180
|
+
7: "enum",
|
|
181
|
+
8: "const",
|
|
182
|
+
9: "pattern",
|
|
183
|
+
10: "minimum",
|
|
184
|
+
11: "maximum",
|
|
185
|
+
12: "exclusiveMinimum",
|
|
186
|
+
13: "exclusiveMaximum",
|
|
187
|
+
14: "minLength",
|
|
188
|
+
15: "maxLength",
|
|
189
|
+
16: "minItems",
|
|
190
|
+
17: "maxItems",
|
|
191
|
+
18: "uniqueItems",
|
|
192
|
+
19: "additionalProperties",
|
|
193
|
+
20: "anyOf",
|
|
194
|
+
21: "oneOf",
|
|
195
|
+
22: "not",
|
|
196
|
+
23: "$ref",
|
|
197
|
+
24: "false",
|
|
198
|
+
25: "depth",
|
|
199
|
+
26: "limit",
|
|
200
|
+
27: "multipleOf",
|
|
201
|
+
28: "minProperties",
|
|
202
|
+
29: "maxProperties",
|
|
203
|
+
30: "contains",
|
|
204
|
+
31: "minContains",
|
|
205
|
+
32: "maxContains",
|
|
206
|
+
33: "dependentRequired",
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// YeptrisStatus values that carry a report in the wasm out-buffer:
|
|
210
|
+
// 0 = valid (an empty report is a valid answer), 9 =
|
|
211
|
+
// YEPTRIS_ERROR_SCHEMA (invalid: drain the diagnostics). Anything
|
|
212
|
+
// else is a call failure (the parse itself failed).
|
|
213
|
+
const ST_OK = 0;
|
|
214
|
+
const ST_SCHEMA_ERROR = 9;
|
|
215
|
+
|
|
216
|
+
/** '/a/b/0' -> ['a', 'b', '0'], with ~1 and ~0 unescaped; '' (the
|
|
217
|
+
* root) -> []. */
|
|
218
|
+
function pointerSegments(raw) {
|
|
219
|
+
if (typeof raw !== "string" || raw.length === 0) return [];
|
|
220
|
+
return raw
|
|
221
|
+
.split("/")
|
|
222
|
+
.slice(1)
|
|
223
|
+
.map((s) => s.replaceAll("~1", "/").replaceAll("~0", "~"));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function toDiagnostic(raw) {
|
|
227
|
+
return {
|
|
228
|
+
code: raw.code,
|
|
229
|
+
keyword: DIAG_KEYWORDS[raw.code] ?? `unknown-${raw.code}`,
|
|
230
|
+
path: pointerSegments(raw.path),
|
|
231
|
+
pointer: raw.path,
|
|
232
|
+
message: raw.message,
|
|
233
|
+
line: raw.line, // 1-based; 0 = unknown (unpositioned scalars)
|
|
234
|
+
col: raw.col,
|
|
235
|
+
offset: raw.offset,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function drainReport(mod, st) {
|
|
240
|
+
const raw = JSON.parse(
|
|
241
|
+
mod.UTF8ToString(mod._yeptris_wasm_out(), mod._yeptris_wasm_out_len()));
|
|
242
|
+
mod._yeptris_wasm_out_free();
|
|
243
|
+
return raw.map(toDiagnostic);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function failFrom(mod) {
|
|
247
|
+
return new YamlError(
|
|
248
|
+
mod.UTF8ToString(mod._yeptris_wasm_last_error()),
|
|
249
|
+
mod._yeptris_wasm_last_error_line(),
|
|
250
|
+
mod._yeptris_wasm_last_error_column());
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function runFamilyValidate(mod, input, kind) {
|
|
254
|
+
const [ptr, len] = cstr(mod, input);
|
|
255
|
+
try {
|
|
256
|
+
const st = mod._yeptris_wasm_validate(ptr, len, kind);
|
|
257
|
+
if (st === ST_OK || st === ST_SCHEMA_ERROR) return drainReport(mod, st);
|
|
258
|
+
throw failFrom(mod);
|
|
259
|
+
} finally {
|
|
260
|
+
freeCstr(mod, ptr);
|
|
261
|
+
mod._yeptris_wasm_out_free();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function runJsonSchemaValidate(mod, schemaBytes, input) {
|
|
266
|
+
const [sptr, slen] = cstr(mod, schemaBytes);
|
|
267
|
+
const [ptr, len] = cstr(mod, input);
|
|
268
|
+
try {
|
|
269
|
+
const st = mod._yeptris_wasm_json_schema_validate(sptr, slen, ptr, len);
|
|
270
|
+
if (st === ST_OK || st === ST_SCHEMA_ERROR) return drainReport(mod, st);
|
|
271
|
+
throw failFrom(mod);
|
|
272
|
+
} finally {
|
|
273
|
+
freeCstr(mod, sptr);
|
|
274
|
+
freeCstr(mod, ptr);
|
|
275
|
+
mod._yeptris_wasm_out_free();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function runCompiledValidate(mod, handle, input) {
|
|
280
|
+
const [ptr, len] = cstr(mod, input);
|
|
281
|
+
try {
|
|
282
|
+
const st = mod._yeptris_wasm_schema_validate(handle, ptr, len);
|
|
283
|
+
if (st === ST_OK || st === ST_SCHEMA_ERROR) return drainReport(mod, st);
|
|
284
|
+
throw failFrom(mod);
|
|
285
|
+
} finally {
|
|
286
|
+
freeCstr(mod, ptr);
|
|
287
|
+
mod._yeptris_wasm_out_free();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** A COMPILED JSON Schema (the compiled economy): the schema parses
|
|
292
|
+
* once — its DOM and pattern programs retained behind one handle —
|
|
293
|
+
* and validates MANY instance streams. Build with compileSchema();
|
|
294
|
+
* direct construction is refused. After free() the handle is dead:
|
|
295
|
+
* reuse throws, double-free is a no-op (the engine frees NULL as a
|
|
296
|
+
* documented no-op; the flag makes the JS door idempotent). */
|
|
297
|
+
export class Schema {
|
|
298
|
+
/** @private — produced by compileSchema() only */
|
|
299
|
+
constructor() {
|
|
300
|
+
throw new TypeError(
|
|
301
|
+
"yeptris: build schemas with compileSchema(), not new Schema()");
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
static _take(mod, handle) {
|
|
305
|
+
const s = Object.create(Schema.prototype);
|
|
306
|
+
s._mod = mod; // the module the handle lives in (compile ran => initialized)
|
|
307
|
+
s._handle = handle;
|
|
308
|
+
s._freed = false;
|
|
309
|
+
return s;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** True once free() ran. */
|
|
313
|
+
get freed() {
|
|
314
|
+
return this._freed;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
_requireAlive(who) {
|
|
318
|
+
if (this._freed) {
|
|
319
|
+
throw new Error(
|
|
320
|
+
`yeptris: the schema handle is freed — ${who} on a freed schema ` +
|
|
321
|
+
"is refused (compile a fresh one with compileSchema())");
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** Every diagnostic across every document of `input` ([] when
|
|
326
|
+
* valid). Same contract as validate(). Synchronous: the handle only
|
|
327
|
+
* exists once the module is initialized. */
|
|
328
|
+
validate(input) {
|
|
329
|
+
this._requireAlive("validate");
|
|
330
|
+
return runCompiledValidate(this._mod, this._handle, input);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** True when validate() answers no diagnostics. */
|
|
334
|
+
isValid(input) {
|
|
335
|
+
return this.validate(input).length === 0;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** Frees the handle and everything it owns in one call. Idempotent;
|
|
339
|
+
* afterwards the handle is dead (reuse throws, never segfaults). */
|
|
340
|
+
free() {
|
|
341
|
+
if (this._freed) return;
|
|
342
|
+
this._freed = true;
|
|
343
|
+
this._mod._yeptris_wasm_schema_free(this._handle);
|
|
344
|
+
this._handle = 0;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// The options door: schema: "core" | "json" (the YAML 1.2 family
|
|
349
|
+
// walk — js-yaml's load(str, { schema }) analog) | a compiled Schema;
|
|
350
|
+
// jsonSchema: object | string (the JSON Schema 2020-12 walk); collect:
|
|
351
|
+
// load-only (load answers { document, diagnostics } instead of
|
|
352
|
+
// raising). Strictness law (the dump precedent): unknown options are
|
|
353
|
+
// rejected, never silently ignored.
|
|
354
|
+
function parseValidateOptions(options, who, allowCollect) {
|
|
355
|
+
if (options === undefined || options === null) {
|
|
356
|
+
return { door: "family", kind: 0, collect: false }; // core_12, the engine default
|
|
357
|
+
}
|
|
358
|
+
if (typeof options !== "object") {
|
|
359
|
+
throw new TypeError(`yeptris: ${who} options must be an object`);
|
|
360
|
+
}
|
|
361
|
+
const KEYS = new Set(["schema", "jsonSchema", "collect"]);
|
|
362
|
+
for (const key of Object.keys(options)) {
|
|
363
|
+
if (!KEYS.has(key)) {
|
|
364
|
+
throw new TypeError(
|
|
365
|
+
`yeptris: ${who} option "${key}" is not supported ` +
|
|
366
|
+
`(the surface is schema + jsonSchema${allowCollect ? " + collect" : ""})`);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
let collect = false;
|
|
370
|
+
if (options.collect !== undefined) {
|
|
371
|
+
if (typeof options.collect !== "boolean") {
|
|
372
|
+
throw new TypeError(`yeptris: ${who} option collect must be a boolean`);
|
|
373
|
+
}
|
|
374
|
+
if (!allowCollect) {
|
|
375
|
+
throw new TypeError(
|
|
376
|
+
`yeptris: ${who} takes no collect option — collect rides load() ` +
|
|
377
|
+
"(loadAll validates the whole stream and returns documents)");
|
|
378
|
+
}
|
|
379
|
+
collect = options.collect;
|
|
380
|
+
}
|
|
381
|
+
let schema = options.schema;
|
|
382
|
+
if (schema !== undefined && schema !== null) {
|
|
383
|
+
if (typeof schema === "string") {
|
|
384
|
+
if (schema !== "core" && schema !== "json") {
|
|
385
|
+
throw new TypeError(
|
|
386
|
+
`yeptris: ${who} option schema must be "core", "json", or a ` +
|
|
387
|
+
`compiled Schema (got ${JSON.stringify(schema)})`);
|
|
388
|
+
}
|
|
389
|
+
} else if (!(schema instanceof Schema)) {
|
|
390
|
+
throw new TypeError(
|
|
391
|
+
`yeptris: ${who} option schema must be "core", "json", or a ` +
|
|
392
|
+
"compiled Schema (got " + typeof schema + ")");
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
let jsonSchema = options.jsonSchema;
|
|
396
|
+
if (jsonSchema !== undefined && jsonSchema !== null) {
|
|
397
|
+
if (typeof jsonSchema !== "string" && typeof jsonSchema !== "object") {
|
|
398
|
+
throw new TypeError(
|
|
399
|
+
`yeptris: ${who} option jsonSchema must be an object or a string ` +
|
|
400
|
+
"(the schema document)");
|
|
401
|
+
}
|
|
402
|
+
if (jsonSchema instanceof Schema) {
|
|
403
|
+
throw new TypeError(
|
|
404
|
+
`yeptris: ${who}: a compiled Schema rides schema:, not jsonSchema: ` +
|
|
405
|
+
"(jsonSchema takes the schema document itself)");
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
if (jsonSchema !== undefined && jsonSchema !== null && schema !== undefined && schema !== null) {
|
|
409
|
+
// the two doors are alternatives, not layers: a caller passing both
|
|
410
|
+
// is one edit away from the door they meant, so refuse instead of
|
|
411
|
+
// silently picking one (the dump precedent)
|
|
412
|
+
throw new TypeError(
|
|
413
|
+
`yeptris: ${who}: pass the compiled schema as schema: OR the ` +
|
|
414
|
+
"schema document as jsonSchema: — not both");
|
|
415
|
+
}
|
|
416
|
+
if (jsonSchema !== undefined && jsonSchema !== null) {
|
|
417
|
+
const bytes = typeof jsonSchema === "string" ? jsonSchema : JSON.stringify(jsonSchema);
|
|
418
|
+
return { door: "json", schemaBytes: bytes, collect, armed: true };
|
|
419
|
+
}
|
|
420
|
+
if (schema instanceof Schema) {
|
|
421
|
+
return { door: "handle", handle: schema, collect, armed: true };
|
|
422
|
+
}
|
|
423
|
+
if (schema !== undefined && schema !== null) {
|
|
424
|
+
const kind = schema === "json" ? 2 : 0; // YEPTRIS_SCHEMA_12_JSON / _12_CORE
|
|
425
|
+
return { door: "family", kind, collect, armed: true };
|
|
426
|
+
}
|
|
427
|
+
if (collect) {
|
|
428
|
+
throw new TypeError(
|
|
429
|
+
`yeptris: ${who}: collect requires a schema — pass schema: or jsonSchema: ` +
|
|
430
|
+
"(collect:true without a gate has nothing to collect)");
|
|
431
|
+
}
|
|
432
|
+
return { door: "family", kind: 0, collect: false, armed: false };
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function validateWithOptions(mod, input, vo) {
|
|
436
|
+
if (vo.door === "handle") return vo.handle.validate(input);
|
|
437
|
+
if (vo.door === "json") {
|
|
438
|
+
return runJsonSchemaValidate(mod, vo.schemaBytes, input);
|
|
439
|
+
}
|
|
440
|
+
return runFamilyValidate(mod, input, vo.kind);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** Validate a YAML stream, answering the WHOLE report: every
|
|
444
|
+
* diagnostic, in engine order ([] when valid). Each diagnostic:
|
|
445
|
+
* `{ code, keyword, path, pointer, message, line, col, offset }` —
|
|
446
|
+
* `path` is the JSON-pointer instance path split into ~0/~1-unescaped
|
|
447
|
+
* segments ([] at the root), `pointer` its raw string form, `line`/
|
|
448
|
+
* `col` the offending node's 1-based mark (0 = unknown: scalars whose
|
|
449
|
+
* bytes the DOM owns have no input position; the path still
|
|
450
|
+
* identifies them).
|
|
451
|
+
*
|
|
452
|
+
* options picks the walk (js-yaml parity names):
|
|
453
|
+
* schema: "core" (default) — explicit-tag consistency + canonical-
|
|
454
|
+
* form key uniqueness; "json" — the spec 10.2 JSON productions
|
|
455
|
+
* (every plain scalar a JSON type, string mapping keys);
|
|
456
|
+
* schema: a compiled Schema (compileSchema()) — validate against
|
|
457
|
+
* the retained model;
|
|
458
|
+
* jsonSchema: object | string — the JSON Schema draft 2020-12 walk
|
|
459
|
+
* (the engine's keyword subset; unknown keywords are ignored).
|
|
460
|
+
* The doors are alternatives: passing schema AND jsonSchema throws
|
|
461
|
+
* TypeError (pick the door you meant — nothing is silently dropped).
|
|
462
|
+
* A stream that does not parse throws YamlError; a $ref that resolves
|
|
463
|
+
* nowhere is a diagnostic, not an exception. */
|
|
464
|
+
export async function validate(input, options) {
|
|
95
465
|
const m = await mod();
|
|
96
|
-
return
|
|
466
|
+
return validateWithOptions(m, input, parseValidateOptions(options, "validate", false));
|
|
97
467
|
}
|
|
98
468
|
|
|
99
|
-
/**
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
469
|
+
/** True when validate() answers no diagnostics. */
|
|
470
|
+
export async function isValid(input, options) {
|
|
471
|
+
return (await validate(input, options)).length === 0;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** Compile a JSON Schema document (object or string; JSON is YAML
|
|
475
|
+
* flow, so a YAML-shaped mapping schema compiles too) into a
|
|
476
|
+
* reusable Schema handle — parse ONCE, validate many. Throws
|
|
477
|
+
* TypeError for empty/absent schema bytes and YamlError (with the
|
|
478
|
+
* engine's message) for bytes that do not parse. */
|
|
479
|
+
export async function compileSchema(jsonSchema) {
|
|
106
480
|
const m = await mod();
|
|
107
|
-
return
|
|
481
|
+
return compileSchemaCore(m, jsonSchema);
|
|
108
482
|
}
|
|
109
483
|
|
|
110
|
-
|
|
111
|
-
|
|
484
|
+
function compileSchemaCore(mod, jsonSchema) {
|
|
485
|
+
if (jsonSchema === undefined || jsonSchema === null) {
|
|
486
|
+
throw new TypeError("yeptris: schema bytes are required");
|
|
487
|
+
}
|
|
488
|
+
const bytes = typeof jsonSchema === "string"
|
|
489
|
+
? jsonSchema
|
|
490
|
+
: JSON.stringify(jsonSchema);
|
|
491
|
+
if (bytes.length === 0) {
|
|
492
|
+
throw new TypeError("yeptris: schema bytes hold no document");
|
|
493
|
+
}
|
|
494
|
+
const [ptr, len] = cstr(mod, bytes);
|
|
495
|
+
let handle;
|
|
496
|
+
try {
|
|
497
|
+
handle = mod._yeptris_wasm_schema_compile(ptr, len);
|
|
498
|
+
} finally {
|
|
499
|
+
freeCstr(mod, ptr);
|
|
500
|
+
}
|
|
501
|
+
if (handle === 0) {
|
|
502
|
+
throw failFrom(mod); // the engine's why rides last_error
|
|
503
|
+
}
|
|
504
|
+
return Schema._take(mod, handle);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// ---- the tolerant stream walk (parse.h's stream iterator) ---------------
|
|
508
|
+
//
|
|
509
|
+
// loadAll is all-or-nothing: one bad document fails the whole stream.
|
|
510
|
+
// The engine's stream iterator (leptris/yeptris#921) walks document by
|
|
511
|
+
// document: a parse-class failure is ONE failed item carrying the
|
|
512
|
+
// failed document's OWN line/column, and the walk RESUMES at the next
|
|
513
|
+
// line-start marker — the valid documents after a bad one are kept.
|
|
514
|
+
// The stream-fatal classes (memory/encoding) end the walk after their
|
|
515
|
+
// item; an unrecoverable tail ends it.
|
|
516
|
+
//
|
|
517
|
+
// Across the wasm boundary the walk is RETAINED state (like the
|
|
518
|
+
// recorder above): open copies the feed into the heap (the engine
|
|
519
|
+
// iterator only borrows its buffer until free), every next() advances
|
|
520
|
+
// one step, close frees both. The step answer crosses through the
|
|
521
|
+
// g_out discipline: frame() says whether the last successful step
|
|
522
|
+
// published a document JSON, index() says which stream position it
|
|
523
|
+
// consumed (failures consume an index too — the yield order is the
|
|
524
|
+
// stream's). Each document serializes through the same serialize_json
|
|
525
|
+
// door load uses, so a frame's value is load's value by construction.
|
|
526
|
+
//
|
|
527
|
+
// The JS shape is the rs face's: an iterator of result objects —
|
|
528
|
+
// `{ document, index }` for a valid document, `{ error, index }` for
|
|
529
|
+
// a failed one. The engine default (schema 1.2 core) rides as-is;
|
|
530
|
+
// the options door (schema/max_depth — ruby/py parity) fills the
|
|
531
|
+
// engine's parse-options struct through the shim's open params.
|
|
532
|
+
|
|
533
|
+
const ST_MEMORY = 2; // YEPTRIS_ERROR_MEMORY — stream-fatal
|
|
534
|
+
const ST_ENCODING = 4; // YEPTRIS_ERROR_ENCODING — stream-fatal
|
|
535
|
+
|
|
536
|
+
// eachDocument's options door (the engine's YeptrisParseOptions, the
|
|
537
|
+
// ruby `each_document(yaml, schema:, max_depth:)` / py
|
|
538
|
+
// `each_document(yaml, schema=, max_depth=)` parity): `schema` is the
|
|
539
|
+
// resolver family every frame types its plain scalars through (the
|
|
540
|
+
// parse resolver's kind, spelled the js-yaml parity way this face
|
|
541
|
+
// already uses for validate/load — "core" the 1.2 core default,
|
|
542
|
+
// "json" the spec 10.2 JSON resolver), `max_depth` caps per-frame
|
|
543
|
+
// nesting (0 = the library default; a frame past the cap fails as
|
|
544
|
+
// that frame's error item and the walk continues at the next marker).
|
|
545
|
+
// The compiled/JSON-Schema doors have no seat on the walk — the
|
|
546
|
+
// iterator carries a resolver KIND, not a schema document; those
|
|
547
|
+
// doors ride validate/load. Strictness law (the dump precedent):
|
|
548
|
+
// unknown or mistyped keys are rejected, never silently ignored.
|
|
549
|
+
// With no options the shim keeps its NULL ride — the engine defaults,
|
|
550
|
+
// the face's shipped v1 contract — byte-identical to the pre-door era.
|
|
551
|
+
function parseEachOptions(options, who) {
|
|
552
|
+
if (options === undefined || options === null) {
|
|
553
|
+
return { kind: -1, maxDepth: 0 }; // -1: the shim's NULL-opts ride
|
|
554
|
+
}
|
|
555
|
+
if (typeof options !== "object") {
|
|
556
|
+
throw new TypeError(`yeptris: ${who} options must be an object`);
|
|
557
|
+
}
|
|
558
|
+
const KEYS = new Set(["schema", "max_depth"]);
|
|
559
|
+
for (const key of Object.keys(options)) {
|
|
560
|
+
if (!KEYS.has(key)) {
|
|
561
|
+
throw new TypeError(
|
|
562
|
+
`yeptris: ${who} option "${key}" is not supported ` +
|
|
563
|
+
"(the surface is schema + max_depth)");
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
let kind = 0; // an options object arms the door: the 1.2 core family
|
|
567
|
+
if (options.schema !== undefined && options.schema !== null) {
|
|
568
|
+
if (options.schema instanceof Schema) {
|
|
569
|
+
throw new TypeError(
|
|
570
|
+
`yeptris: ${who}: a compiled Schema rides validate()/load(), ` +
|
|
571
|
+
"not the walk — the iterator takes the resolver kind " +
|
|
572
|
+
'(schema: "core" | "json")');
|
|
573
|
+
}
|
|
574
|
+
if (typeof options.schema !== "string" ||
|
|
575
|
+
(options.schema !== "core" && options.schema !== "json")) {
|
|
576
|
+
throw new TypeError(
|
|
577
|
+
`yeptris: ${who} option schema must be "core" or "json" (got ` +
|
|
578
|
+
JSON.stringify(options.schema) + ")");
|
|
579
|
+
}
|
|
580
|
+
kind = options.schema === "json" ? 2 : 0; // YEPTRIS_SCHEMA_12_JSON / _12_CORE
|
|
581
|
+
}
|
|
582
|
+
let maxDepth = 0;
|
|
583
|
+
if (options.max_depth !== undefined && options.max_depth !== null) {
|
|
584
|
+
const d = options.max_depth;
|
|
585
|
+
if (typeof d !== "number" || !Number.isInteger(d) || d < 0) {
|
|
586
|
+
throw new TypeError(
|
|
587
|
+
`yeptris: ${who} option max_depth must be a non-negative integer ` +
|
|
588
|
+
"(0 rides the library default)");
|
|
589
|
+
}
|
|
590
|
+
maxDepth = d;
|
|
591
|
+
}
|
|
592
|
+
return { kind, maxDepth };
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function eachDocumentCore(mod, yaml, vo) {
|
|
596
|
+
return (function* () {
|
|
597
|
+
const [ptr, len] = cstr(mod, yaml);
|
|
598
|
+
try {
|
|
599
|
+
// The capability probe: the door rides a NEW export (the v1
|
|
600
|
+
// open's 2-param wasm ABI stays frozen — this surface's ABI is
|
|
601
|
+
// append-only), so a dist built before the door simply lacks
|
|
602
|
+
// it. The probe is the missing-export check's own idiom, and it
|
|
603
|
+
// gates the DOOR ONLY: the no-options walk is the v1 contract
|
|
604
|
+
// and rides either era unchanged. Without the probe wasm would
|
|
605
|
+
// never even see the call — the export isn't there to call —
|
|
606
|
+
// and the options would have no seat at all; with it, a stale
|
|
607
|
+
// dist is refused loudly instead of pretending.
|
|
608
|
+
let openSt;
|
|
609
|
+
if (vo.kind >= 0) {
|
|
610
|
+
const openEx = mod._yeptris_wasm_each_open_ex;
|
|
611
|
+
if (typeof openEx !== "function") {
|
|
612
|
+
throw new Error(
|
|
613
|
+
"yeptris: the eachDocument options door needs a dist rebuilt " +
|
|
614
|
+
"with build.sh — the loaded wasm predates the options era " +
|
|
615
|
+
"(stale artifact)",
|
|
616
|
+
);
|
|
617
|
+
}
|
|
618
|
+
openSt = openEx(ptr, len, vo.kind, vo.maxDepth);
|
|
619
|
+
} else {
|
|
620
|
+
openSt = mod._yeptris_wasm_each_open(ptr, len);
|
|
621
|
+
}
|
|
622
|
+
if (openSt !== 0) {
|
|
623
|
+
throw failFrom(mod);
|
|
624
|
+
}
|
|
625
|
+
for (;;) {
|
|
626
|
+
const st = mod._yeptris_wasm_each_next();
|
|
627
|
+
const index = mod._yeptris_wasm_each_index();
|
|
628
|
+
if (st === 0) {
|
|
629
|
+
if (mod._yeptris_wasm_each_frame() === 0) {
|
|
630
|
+
return; // the stream is over
|
|
631
|
+
}
|
|
632
|
+
const document = JSON.parse(
|
|
633
|
+
mod.UTF8ToString(mod._yeptris_wasm_out(), mod._yeptris_wasm_out_len()));
|
|
634
|
+
mod._yeptris_wasm_out_free();
|
|
635
|
+
yield { document, index, error: undefined };
|
|
636
|
+
} else {
|
|
637
|
+
yield {
|
|
638
|
+
document: undefined,
|
|
639
|
+
index,
|
|
640
|
+
error: new YamlError(
|
|
641
|
+
mod.UTF8ToString(mod._yeptris_wasm_last_error()),
|
|
642
|
+
mod._yeptris_wasm_last_error_line(),
|
|
643
|
+
mod._yeptris_wasm_last_error_column()),
|
|
644
|
+
};
|
|
645
|
+
if (st === ST_MEMORY || st === ST_ENCODING) {
|
|
646
|
+
return; // stream-fatal: the iterator is done
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
} finally {
|
|
651
|
+
mod._yeptris_wasm_out_free(); // idempotent (frees NULL = no-op)
|
|
652
|
+
mod._yeptris_wasm_each_close();
|
|
653
|
+
}
|
|
654
|
+
})();
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/** Walk a multi-document stream one document at a time, TOLERANTLY
|
|
658
|
+
* (the engine's stream iterator): yields one result per document in
|
|
659
|
+
* stream order —
|
|
660
|
+
*
|
|
661
|
+
* { document: value, index: 0, error: undefined } // a valid document
|
|
662
|
+
* { document: undefined, index: 1, error: YamlError } // a failed one
|
|
663
|
+
*
|
|
664
|
+
* A parse-class failure carries the failed document's OWN line/column
|
|
665
|
+
* and its 0-based stream index, and the walk CONTINUES at the next
|
|
666
|
+
* `---` line-start marker — the valid documents after a bad one are
|
|
667
|
+
* kept (parse()/loadAll would fail the whole stream instead). The
|
|
668
|
+
* stream-fatal classes (memory/encoding) yield their item and end the
|
|
669
|
+
* walk; an unrecoverable tail ends it. Lazy: nothing parses until
|
|
670
|
+
* iteration starts. Documents materialize through load's serialize
|
|
671
|
+
* door, so each frame's value is load's value by construction.
|
|
672
|
+
*
|
|
673
|
+
* options (the engine's parse options — ruby/py parity): schema —
|
|
674
|
+
* the resolver family every frame types through, "core" (default) or
|
|
675
|
+
* "json"; max_depth — the per-frame nesting cap, 0 = the library
|
|
676
|
+
* default (a frame past the cap fails as that frame's item and the
|
|
677
|
+
* walk continues). Unknown or mistyped options throw TypeError. */
|
|
678
|
+
export async function* eachDocument(yaml, options) {
|
|
112
679
|
const m = await mod();
|
|
113
|
-
|
|
680
|
+
yield* eachDocumentCore(m, yaml, parseEachOptions(options, "eachDocument"));
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/** eachDocument's Sync twin: a sync generator over an initialized
|
|
684
|
+
* module (await init() first). Takes the same options door. */
|
|
685
|
+
export function eachDocumentSync(yaml, options) {
|
|
686
|
+
return eachDocumentCore(
|
|
687
|
+
requireMod("eachDocumentSync"),
|
|
688
|
+
yaml,
|
|
689
|
+
parseEachOptions(options, "eachDocumentSync"),
|
|
690
|
+
);
|
|
114
691
|
}
|
|
115
692
|
|
|
116
|
-
// ----
|
|
693
|
+
// ---- load/dump cores (shared by the async and Sync forms) --------------
|
|
694
|
+
|
|
695
|
+
function loadCore(mod, yaml, options) {
|
|
696
|
+
const vo = parseValidateOptions(options, "load", true);
|
|
697
|
+
if (!vo.armed) {
|
|
698
|
+
return JSON.parse(run(mod, mod._yeptris_wasm_load, yaml));
|
|
699
|
+
}
|
|
700
|
+
// validate BEFORE loading (the ruby/py law): the document only
|
|
701
|
+
// materializes when the walk is clean — or when collect asked for
|
|
702
|
+
// both, in which case the pair answers even for a dirty walk
|
|
703
|
+
const diags = validateWithOptions(mod, yaml, vo);
|
|
704
|
+
if (vo.collect) {
|
|
705
|
+
return { document: JSON.parse(run(mod, mod._yeptris_wasm_load, yaml)), diagnostics: diags };
|
|
706
|
+
}
|
|
707
|
+
if (diags.length > 0) {
|
|
708
|
+
throw new ValidationError(diags);
|
|
709
|
+
}
|
|
710
|
+
return JSON.parse(run(mod, mod._yeptris_wasm_load, yaml));
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
function loadAllCore(mod, yaml, options) {
|
|
714
|
+
const vo = parseValidateOptions(options, "loadAll", false);
|
|
715
|
+
if (vo.armed) {
|
|
716
|
+
// whole-stream validation BEFORE the parse (the ruby/py law): one
|
|
717
|
+
// walk over every document, then the materialization
|
|
718
|
+
const diags = validateWithOptions(mod, yaml, vo);
|
|
719
|
+
if (diags.length > 0) {
|
|
720
|
+
throw new ValidationError(diags);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
return JSON.parse(run(mod, mod._yeptris_wasm_load_all, yaml));
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
function dumpCore(mod, json, options) {
|
|
727
|
+
const text = typeof json === "string" ? json : JSON.stringify(json);
|
|
728
|
+
let canonical = 0;
|
|
729
|
+
let lineWidth = 0; // <= 0 rides the library default (emit.h)
|
|
730
|
+
if (options !== undefined && options !== null) {
|
|
731
|
+
if (typeof options !== "object") {
|
|
732
|
+
throw new TypeError("yeptris: dump options must be an object");
|
|
733
|
+
}
|
|
734
|
+
for (const key of Object.keys(options)) {
|
|
735
|
+
if (key !== "lineWidth" && key !== "canonical") {
|
|
736
|
+
throw new TypeError(
|
|
737
|
+
`yeptris: dump option "${key}" is not supported ` +
|
|
738
|
+
`(the engine surface is lineWidth + canonical)`);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
if (options.lineWidth !== undefined) {
|
|
742
|
+
const w = options.lineWidth;
|
|
743
|
+
if (typeof w !== "number" || !Number.isInteger(w) || w === 0 || w < -1) {
|
|
744
|
+
throw new TypeError(
|
|
745
|
+
"yeptris: dump option lineWidth must be an integer > 0 (or -1 for no folding)");
|
|
746
|
+
}
|
|
747
|
+
// -1 is js-yaml's "unlimited": the engine folds only past the
|
|
748
|
+
// width, so the largest width IS "never fold"
|
|
749
|
+
lineWidth = w === -1 ? 0x7fffffff : w;
|
|
750
|
+
}
|
|
751
|
+
if (options.canonical !== undefined) {
|
|
752
|
+
if (typeof options.canonical !== "boolean") {
|
|
753
|
+
throw new TypeError("yeptris: dump option canonical must be a boolean");
|
|
754
|
+
}
|
|
755
|
+
canonical = options.canonical ? 1 : 0;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
return run(mod, mod._yeptris_wasm_dump_ex, text, canonical, lineWidth);
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
// ---- the event face (the recorder's bulk drain) ------------------------
|
|
117
762
|
//
|
|
118
763
|
// The engine ships three event models over the one parse: push (a
|
|
119
764
|
// callback per event), pull (a StAX cursor) and the recorder (fixed
|
|
@@ -181,48 +826,23 @@ function decodeArena(u8, off, len) {
|
|
|
181
826
|
return utf8Decoder.decode(u8.subarray(off, off + len));
|
|
182
827
|
}
|
|
183
828
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
* is:
|
|
187
|
-
*
|
|
188
|
-
* { type: "stream-start" | "stream-end" | "document-start" |
|
|
189
|
-
* "document-end" | "sequence-start" | "sequence-end" |
|
|
190
|
-
* "mapping-start" | "mapping-end" | "scalar" | "alias",
|
|
191
|
-
* line, column, // 1-based start mark (0 = no position)
|
|
192
|
-
* endLine, endColumn, // libyaml end_mark: col one past the end
|
|
193
|
-
* value?, // scalar content; the alias NAME for alias
|
|
194
|
-
* anchor?, tag?, // node properties, present when written
|
|
195
|
-
* style?, // scalars: plain|single|double|literal|folded
|
|
196
|
-
* tagId?, // scalars: the resolved kind (str|int|...)
|
|
197
|
-
* implicit?, // scalars: plain style (the typing trigger)
|
|
198
|
-
* flow?, // collection starts: flow context
|
|
199
|
-
* explicit? } // document start/end: --- / ... marker seen
|
|
200
|
-
*
|
|
201
|
-
* Absent properties are omitted, never null. The faces of
|
|
202
|
-
* ruby/py/rs consume the same normalized vocabulary (see
|
|
203
|
-
* test/fixtures/events.json, whose header documents it as data).
|
|
204
|
-
* options is accepted for call-site symmetry and reserved. Throws
|
|
205
|
-
* YamlError on malformed input (position from the recorder's status;
|
|
206
|
-
* its message is the generic "parse failed" — the recorder carries
|
|
207
|
-
* status + position, not the error text). */
|
|
208
|
-
export async function events(yaml, options) {
|
|
209
|
-
const m = await mod();
|
|
210
|
-
const [ptr, len] = cstr(m, yaml);
|
|
829
|
+
function eventsCore(mod, yaml, options) {
|
|
830
|
+
const [ptr, len] = cstr(mod, yaml);
|
|
211
831
|
try {
|
|
212
|
-
const st =
|
|
832
|
+
const st = mod._yeptris_wasm_events(ptr, len);
|
|
213
833
|
if (st !== 0) {
|
|
214
834
|
throw new YamlError(
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
835
|
+
mod.UTF8ToString(mod._yeptris_wasm_last_error()),
|
|
836
|
+
mod._yeptris_wasm_last_error_line(),
|
|
837
|
+
mod._yeptris_wasm_last_error_column());
|
|
218
838
|
}
|
|
219
|
-
const n =
|
|
220
|
-
const recs =
|
|
221
|
-
const arena =
|
|
839
|
+
const n = mod._yeptris_wasm_event_count();
|
|
840
|
+
const recs = mod._yeptris_wasm_event_records();
|
|
841
|
+
const arena = mod._yeptris_wasm_event_arena();
|
|
222
842
|
// Heap views fetched AFTER the call: the feed may have grown the
|
|
223
843
|
// memory, and the exported views are re-bound on growth.
|
|
224
|
-
const u8 =
|
|
225
|
-
const u32 =
|
|
844
|
+
const u8 = mod.HEAPU8;
|
|
845
|
+
const u32 = mod.HEAPU32;
|
|
226
846
|
const out = new Array(n);
|
|
227
847
|
for (let i = 0; i < n; i++) {
|
|
228
848
|
const base = recs + i * EV_RECORD_SIZE;
|
|
@@ -270,17 +890,96 @@ export async function events(yaml, options) {
|
|
|
270
890
|
} finally {
|
|
271
891
|
// the drain is decoded: release the recorder (nothing retained
|
|
272
892
|
// past this call — the answer left the heap as JS objects)
|
|
273
|
-
|
|
893
|
+
mod._yeptris_wasm_events_free();
|
|
274
894
|
}
|
|
275
895
|
}
|
|
276
896
|
|
|
897
|
+
/** Parse YAML text and return the document as a JavaScript value
|
|
898
|
+
* (the engine's canonical JSON, JSON.parse'd). Throws YamlError with
|
|
899
|
+
* line/column on malformed input. A multi-document stream answers
|
|
900
|
+
* with the FIRST document (the engine's json contract); every
|
|
901
|
+
* document of a stream comes from loadAll.
|
|
902
|
+
*
|
|
903
|
+
* options arms a validation gate (the ruby/py load law: validate
|
|
904
|
+
* BEFORE loading) — schema: "core" | "json" | a compiled Schema,
|
|
905
|
+
* jsonSchema: object | string. With diagnostics present the load
|
|
906
|
+
* raises ValidationError carrying the whole report; with `collect:
|
|
907
|
+
* true` it answers `{ document, diagnostics }` instead (collect
|
|
908
|
+
* requires an armed gate). */
|
|
909
|
+
export async function load(yaml, options) {
|
|
910
|
+
const m = await mod();
|
|
911
|
+
return loadCore(m, yaml, options);
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
/** Parse a multi-document YAML stream and return EVERY document as a
|
|
915
|
+
* JavaScript value, in stream order (js-yaml's loadAll shape). An
|
|
916
|
+
* empty or comment-only stream answers []; an empty document
|
|
917
|
+
* ("---\n---") answers null at its position. options arms a
|
|
918
|
+
* whole-stream validation gate (schema:/jsonSchema:) that runs
|
|
919
|
+
* BEFORE the parse and raises ValidationError carrying the whole
|
|
920
|
+
* report (collect rides load() only). Throws YamlError with
|
|
921
|
+
* line/column on malformed input. */
|
|
922
|
+
export async function loadAll(yaml, options) {
|
|
923
|
+
const m = await mod();
|
|
924
|
+
return loadAllCore(m, yaml, options);
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
/** Parse YAML text and return the engine's canonical JSON string. */
|
|
928
|
+
export async function loadJSON(yaml) {
|
|
929
|
+
const m = await mod();
|
|
930
|
+
return run(m, m._yeptris_wasm_load, yaml);
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
// ---- the async entries (initialize on demand) ---------------------------
|
|
934
|
+
|
|
935
|
+
/** Parse YAML text and return EVERY event of the stream as an array
|
|
936
|
+
* of plain objects, in stream order (leptris/yeptris#886). One event
|
|
937
|
+
* is:
|
|
938
|
+
*
|
|
939
|
+
* { type: "stream-start" | "stream-end" | "document-start" |
|
|
940
|
+
* "document-end" | "sequence-start" | "sequence-end" |
|
|
941
|
+
* "mapping-start" | "mapping-end" | "scalar" | "alias",
|
|
942
|
+
* line, column, // 1-based start mark (0 = no position)
|
|
943
|
+
* endLine, endColumn, // libyaml end_mark: col one past the end
|
|
944
|
+
* value?, // scalar content; the alias NAME for alias
|
|
945
|
+
* anchor?, tag?, // node properties, present when written
|
|
946
|
+
* style?, // scalars: plain|single|double|literal|folded
|
|
947
|
+
* tagId?, // scalars: the resolved kind (str|int|...)
|
|
948
|
+
* implicit?, // scalars: plain style (the typing trigger)
|
|
949
|
+
* flow?, // collection starts: flow context
|
|
950
|
+
* explicit? } // document start/end: --- / ... marker seen
|
|
951
|
+
*
|
|
952
|
+
* Absent properties are omitted, never null. The faces of
|
|
953
|
+
* ruby/py/rs consume the same normalized vocabulary (see
|
|
954
|
+
* test/fixtures/events.json, whose header documents it as data).
|
|
955
|
+
* options is accepted for call-site symmetry and reserved. Throws
|
|
956
|
+
* YamlError on malformed input (position from the recorder's status;
|
|
957
|
+
* its message is the generic "parse failed" — the recorder carries
|
|
958
|
+
* status + position, not the error text). */
|
|
959
|
+
export async function events(yaml, options) {
|
|
960
|
+
const m = await mod();
|
|
961
|
+
return eventsCore(m, yaml, options);
|
|
962
|
+
}
|
|
963
|
+
|
|
277
964
|
/** Convert JSON text (or any JSON-serializable value) to the engine's
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
|
|
965
|
+
* byte-stable YAML. JSON is a subset of YAML 1.2 flow, so the one
|
|
966
|
+
* parse engine takes it.
|
|
967
|
+
*
|
|
968
|
+
* options (js-yaml parity names over emit.h's yeptris_emit_options):
|
|
969
|
+
* lineWidth — fold flow collections once the current line exceeds
|
|
970
|
+
* this column (the engine's best_width). Default: the engine
|
|
971
|
+
* default (80); -1 disables folding.
|
|
972
|
+
* canonical — the engine's fixed form: flow collections, quoted
|
|
973
|
+
* strings, typed words, shortest floats — a parse of the output
|
|
974
|
+
* re-serializes byte-identically. Default: false.
|
|
975
|
+
*
|
|
976
|
+
* `indent` and `flowLevel` have no engine option (the block indent is
|
|
977
|
+
* fixed at 2; there is no flow-level override) — they are not accepted
|
|
978
|
+
* here rather than silently ignored. Throws TypeError on a mistyped
|
|
979
|
+
* known option; throws YamlError on malformed JSON input. */
|
|
980
|
+
export async function dump(json, options) {
|
|
281
981
|
const m = await mod();
|
|
282
|
-
|
|
283
|
-
return run(m, m._yeptris_wasm_dump, text);
|
|
982
|
+
return dumpCore(m, json, options);
|
|
284
983
|
}
|
|
285
984
|
|
|
286
985
|
/** The libyeptris version string. */
|
|
@@ -289,6 +988,63 @@ export async function version() {
|
|
|
289
988
|
return m.UTF8ToString(m._yeptris_wasm_version());
|
|
290
989
|
}
|
|
291
990
|
|
|
991
|
+
// ---- the Sync entries (usable once the module is initialized) ----------
|
|
992
|
+
//
|
|
993
|
+
// The wasm executes synchronously; every async form is async only
|
|
994
|
+
// because the FIRST call may initialize the module. After one await
|
|
995
|
+
// (init(), or any async call) the same bodies run synchronously. Each
|
|
996
|
+
// Sync twin refuses to run uninitialized — the lazy import is an
|
|
997
|
+
// async door and half-initializing it would be a lie — and names the
|
|
998
|
+
// way out in the error.
|
|
999
|
+
|
|
1000
|
+
/** Parse YAML text (load's Sync twin). Throws unless the module is
|
|
1001
|
+
* initialized (await init() first). */
|
|
1002
|
+
export function loadSync(yaml, options) {
|
|
1003
|
+
return loadCore(requireMod("loadSync"), yaml, options);
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
/** loadAll's Sync twin. Throws unless the module is initialized. */
|
|
1007
|
+
export function loadAllSync(yaml, options) {
|
|
1008
|
+
return loadAllCore(requireMod("loadAllSync"), yaml, options);
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
/** loadJSON's Sync twin. Throws unless the module is initialized. */
|
|
1012
|
+
export function loadJSONSync(yaml) {
|
|
1013
|
+
return run(requireMod("loadJSONSync"), cached._yeptris_wasm_load, yaml);
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
/** events' Sync twin. Throws unless the module is initialized. */
|
|
1017
|
+
export function eventsSync(yaml, options) {
|
|
1018
|
+
return eventsCore(requireMod("eventsSync"), yaml, options);
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
/** dump's Sync twin. Throws unless the module is initialized. */
|
|
1022
|
+
export function dumpSync(json, options) {
|
|
1023
|
+
return dumpCore(requireMod("dumpSync"), json, options);
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
/** validate's Sync twin. Throws unless the module is initialized. */
|
|
1027
|
+
export function validateSync(input, options) {
|
|
1028
|
+
return validateWithOptions(
|
|
1029
|
+
requireMod("validateSync"), input, parseValidateOptions(options, "validate", false));
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
/** isValid's Sync twin. Throws unless the module is initialized. */
|
|
1033
|
+
export function isValidSync(input, options) {
|
|
1034
|
+
return validateSync(input, options).length === 0;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
/** compileSchema's Sync twin. Throws unless the module is initialized. */
|
|
1038
|
+
export function compileSchemaSync(jsonSchema) {
|
|
1039
|
+
return compileSchemaCore(requireMod("compileSchemaSync"), jsonSchema);
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
/** version's Sync twin. Throws unless the module is initialized. */
|
|
1043
|
+
export function versionSync() {
|
|
1044
|
+
const m = requireMod("versionSync");
|
|
1045
|
+
return m.UTF8ToString(m._yeptris_wasm_version());
|
|
1046
|
+
}
|
|
1047
|
+
|
|
292
1048
|
/** The wasm module instance this package caches (diagnostics:
|
|
293
1049
|
* heap/memory inspection, the engine's raw exports). The load/dump
|
|
294
1050
|
* calls all share this one instance. */
|