@3leaps/yamlprims 0.2.2 → 0.3.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 CHANGED
@@ -4,15 +4,24 @@ YAML 1.2.2 Core Schema value loader, strict wire decoder and canonical encoder
4
4
  (`yamlprims-value/1`): a binding over the Rust `yamlprims` crate, compiled to
5
5
  one WebAssembly module with no imports. Zero runtime dependencies.
6
6
 
7
- > **Status:** Under qualification against the `yamlprims` v1.5 specification
8
- > and pinned `3leaps/structcorpus` conformance vectors.
7
+ > **Status:** Under qualification against the `yamlprims` v1.6 specification
8
+ > and verified in-tree against pinned `3leaps/structcorpus` conformance vectors.
9
9
 
10
10
  ```ts
11
- import { load, loadAll, dump, loadIjson, toPlain, encode, decodeWire } from "@3leaps/yamlprims";
11
+ import {
12
+ load,
13
+ parse,
14
+ stringify,
15
+ lintYaml11,
16
+ loadIjson,
17
+ encode,
18
+ decodeWire,
19
+ } from "@3leaps/yamlprims";
12
20
 
13
21
  const doc = load("port: 8080\nenabled: true\n");
14
- const plain = toPlain(doc.root);
15
- const yamlOut = dump(plain);
22
+ const plain = parse("port: 8080\nenabled: true\n"); // v0.3.0+
23
+ const yamlOut = stringify(plain); // v0.3.0+ (or dump(plain) in v0.2.0+)
24
+ const lint = lintYaml11("flag: yes\n"); // v0.3.0+
16
25
  const ijsonBytes = loadIjson(yamlOut);
17
26
  const wire = encode([doc]);
18
27
  const decoded = decodeWire(wire);
package/dist/index.d.ts CHANGED
@@ -238,6 +238,22 @@ export declare class YamlprimsError extends Error {
238
238
  });
239
239
  location(): Location | null;
240
240
  }
241
+ export type LintCode = "yaml11_boolean" | "yaml11_number" | "yaml11_sexagesimal" | "yaml11_merge_key";
242
+ export interface LintDiagnostic {
243
+ code: LintCode;
244
+ span: Span;
245
+ documentIndex: number;
246
+ path: PathSegment[];
247
+ raw: string;
248
+ yaml12Resolution: string;
249
+ yaml11Note: string;
250
+ message: string;
251
+ }
252
+ export interface LintResult {
253
+ diagnostics: LintDiagnostic[];
254
+ warningCount: number;
255
+ truncated: boolean;
256
+ }
241
257
  export declare function defaultLimits(): Limits;
242
258
  export declare function defaultOptions(): Options;
243
259
  export declare function validateOptions(options?: OptionsInput): Options;
@@ -284,6 +300,12 @@ export declare function loadBytes(input: Uint8Array, options?: OptionsInput): Do
284
300
  export declare function loadAllBytes(input: Uint8Array, options?: OptionsInput): Document[];
285
301
  export declare function loadPlain(input: string | Uint8Array, options?: OptionsInput): string;
286
302
  export declare function loadIjson(input: string | Uint8Array, options?: OptionsInput): string;
303
+ export declare function lintYaml11(input: string | Uint8Array, options?: OptionsInput): LintResult;
304
+ export declare function encodeLint(result: LintResult, options?: OptionsInput): string;
287
305
  export declare function fromPlain(value: unknown, options?: EmitOptionsInput): Node;
288
306
  export declare function dumpAll(documents: readonly unknown[], options?: EmitOptionsInput): string;
289
307
  export declare function dump(value: unknown, options?: EmitOptionsInput): string;
308
+ export declare function parse(input: string | Uint8Array, options?: OptionsInput): unknown;
309
+ export declare function parseAll(input: string | Uint8Array, options?: OptionsInput): unknown[];
310
+ export declare function stringify(value: unknown, options?: EmitOptionsInput): string;
311
+ export declare function stringifyAll(documents: readonly unknown[], options?: EmitOptionsInput): string;
package/dist/index.js CHANGED
@@ -613,7 +613,13 @@ function runWasmLoad(inputBytes, opts, mode, simulateTrap = false) {
613
613
  if (instanceBusy) {
614
614
  throw notSourceError("internal", {}, "reentrant WASM execution is forbidden");
615
615
  }
616
- const optsBytes = serializeWasmOptions(opts, mode === "ijson");
616
+ const wasmOpts = mode === "encode-lint" && opts.limits.maxInputBytes < MAX_WASM_INPUT_CEILING
617
+ ? {
618
+ ...opts,
619
+ limits: { ...opts.limits, maxInputBytes: MAX_WASM_INPUT_CEILING },
620
+ }
621
+ : opts;
622
+ const optsBytes = serializeWasmOptions(wasmOpts, mode === "ijson" || mode === "encode-lint");
617
623
  const exports = getWasmExports();
618
624
  instanceBusy = true;
619
625
  let localTrapped = false;
@@ -629,15 +635,15 @@ function runWasmLoad(inputBytes, opts, mode, simulateTrap = false) {
629
635
  if (optsBytes.byteLength > 0) {
630
636
  new Uint8Array(exports.memory.buffer, optPtr, optsBytes.byteLength).set(optsBytes);
631
637
  }
632
- const wasmMaxInput = clampU32(opts.limits.maxInputBytes);
638
+ const wasmMaxInput = clampU32(wasmOpts.limits.maxInputBytes);
633
639
  const inPtr = exports.input_reserve(inputBytes.byteLength, wasmMaxInput) >>> 0;
634
640
  if (inPtr === 0) {
635
- if (inputBytes.byteLength > opts.limits.maxInputBytes) {
641
+ if (inputBytes.byteLength > wasmOpts.limits.maxInputBytes) {
636
642
  throw new YamlprimsError({
637
643
  kind: "limit_exceeded",
638
- detail: { limit: "max_input_bytes", configured: opts.limits.maxInputBytes },
644
+ detail: { limit: "max_input_bytes", configured: wasmOpts.limits.maxInputBytes },
639
645
  site: { source: { byteOffset: 0, utf16Offset: 0, line: 1, column: 1 } },
640
- message: `max_input_bytes (${opts.limits.maxInputBytes}) exceeded`,
646
+ message: `max_input_bytes (${wasmOpts.limits.maxInputBytes}) exceeded`,
641
647
  });
642
648
  }
643
649
  if (inputBytes.byteLength > MAX_WASM_INPUT_CEILING) {
@@ -661,7 +667,11 @@ function runWasmLoad(inputBytes, opts, mode, simulateTrap = false) {
661
667
  ? exports.load_plain(inputBytes.byteLength, optsBytes.byteLength) >>> 0
662
668
  : mode === "ijson"
663
669
  ? exports.load_ijson(inputBytes.byteLength, optsBytes.byteLength) >>> 0
664
- : exports.load_wire(inputBytes.byteLength, optsBytes.byteLength, mode === "wire-single" ? 1 : 0) >>> 0;
670
+ : mode === "lint"
671
+ ? exports.lint_wire(inputBytes.byteLength, optsBytes.byteLength) >>> 0
672
+ : mode === "encode-lint"
673
+ ? exports.encode_lint_wire(inputBytes.byteLength, optsBytes.byteLength) >>> 0
674
+ : exports.load_wire(inputBytes.byteLength, optsBytes.byteLength, mode === "wire-single" ? 1 : 0) >>> 0;
665
675
  const outPtr = exports.output_ptr() >>> 0;
666
676
  const outLen = exports.output_len() >>> 0;
667
677
  const output = new Uint8Array(exports.memory.buffer, outPtr, outLen).slice();
@@ -3564,6 +3574,296 @@ export function loadIjson(input, options) {
3564
3574
  }
3565
3575
  return text;
3566
3576
  }
3577
+ const VALID_LINT_CODES = new Set([
3578
+ "yaml11_boolean",
3579
+ "yaml11_number",
3580
+ "yaml11_sexagesimal",
3581
+ "yaml11_merge_key",
3582
+ ]);
3583
+ function decodeLintEnvelope(wireJson, opts, sourceBytes) {
3584
+ const maxJsonDepth = opts.limits.maxDepth * 4 + 32;
3585
+ const root = new WireJsonParser(wireJson, maxJsonDepth).parseRoot();
3586
+ if (root.t !== "obj") {
3587
+ throw notSourceError("internal", {}, "lint wire root must be a JSON object");
3588
+ }
3589
+ expectExactKeys(root.map, [
3590
+ "format",
3591
+ "version",
3592
+ "warning_count",
3593
+ "truncated",
3594
+ "diagnostics",
3595
+ ]);
3596
+ const fmt = expectStr(root.map.get("format"), "format");
3597
+ if (fmt !== "yamlprims-lint") {
3598
+ throw notSourceError("internal", {}, `unsupported lint wire format: ${fmt}`);
3599
+ }
3600
+ const ver = expectInt(root.map.get("version"), "version");
3601
+ if (ver !== 1) {
3602
+ throw notSourceError("internal", {}, `unsupported lint wire version: ${ver}`);
3603
+ }
3604
+ const warningCount = expectInt(root.map.get("warning_count"), "warning_count");
3605
+ const truncated = expectBool(root.map.get("truncated"), "truncated");
3606
+ const diagsVal = root.map.get("diagnostics");
3607
+ if (diagsVal.t !== "arr") {
3608
+ throw notSourceError("internal", {}, "diagnostics must be an array");
3609
+ }
3610
+ const idx = new Utf16Index(sourceBytes);
3611
+ const pathCtx = {
3612
+ opts,
3613
+ idx: null,
3614
+ nodesRemaining: opts.limits.maxNodes,
3615
+ ownedBytes: 0n,
3616
+ forbidLocations: true,
3617
+ };
3618
+ const diagnostics = [];
3619
+ for (const d of diagsVal.items) {
3620
+ if (d.t !== "obj") {
3621
+ throw notSourceError("internal", {}, "LintDiagnostic must be an object");
3622
+ }
3623
+ expectExactKeys(d.map, [
3624
+ "code",
3625
+ "span",
3626
+ "document_index",
3627
+ "path",
3628
+ "raw",
3629
+ "yaml12_resolution",
3630
+ "yaml11_note",
3631
+ "message",
3632
+ ]);
3633
+ const codeStr = expectStr(d.map.get("code"), "code");
3634
+ if (!VALID_LINT_CODES.has(codeStr)) {
3635
+ throw notSourceError("internal", {}, `unknown LintCode: ${codeStr}`);
3636
+ }
3637
+ const span = decodeSpan(d.map.get("span"), idx);
3638
+ const documentIndex = expectInt(d.map.get("document_index"), "document_index");
3639
+ const pathVal = d.map.get("path");
3640
+ if (pathVal.t !== "arr") {
3641
+ throw notSourceError("internal", {}, "diagnostic.path must be an array");
3642
+ }
3643
+ const path = [];
3644
+ for (const seg of pathVal.items) {
3645
+ if (seg.t !== "obj" || seg.map.size !== 1) {
3646
+ throw notSourceError("internal", {}, "invalid diagnostic path segment");
3647
+ }
3648
+ const idxSeg = seg.map.get("index");
3649
+ const keySeg = seg.map.get("key");
3650
+ if (idxSeg) {
3651
+ path.push({ index: expectInt(idxSeg, "path.index") });
3652
+ }
3653
+ else if (keySeg) {
3654
+ const keyNode = decodeNode(keySeg, pathCtx, 0, true);
3655
+ path.push({ key: keyNode });
3656
+ }
3657
+ else {
3658
+ throw notSourceError("internal", {}, "unknown diagnostic path segment");
3659
+ }
3660
+ }
3661
+ const raw = expectStr(d.map.get("raw"), "raw");
3662
+ const yaml12Resolution = expectStr(d.map.get("yaml12_resolution"), "yaml12_resolution");
3663
+ const yaml11Note = expectStr(d.map.get("yaml11_note"), "yaml11_note");
3664
+ const message = expectStr(d.map.get("message"), "message");
3665
+ diagnostics.push({
3666
+ code: codeStr,
3667
+ span,
3668
+ documentIndex,
3669
+ path,
3670
+ raw,
3671
+ yaml12Resolution,
3672
+ yaml11Note,
3673
+ message,
3674
+ });
3675
+ }
3676
+ return {
3677
+ diagnostics,
3678
+ warningCount,
3679
+ truncated,
3680
+ };
3681
+ }
3682
+ export function lintYaml11(input, options) {
3683
+ const opts = validateOptions(options);
3684
+ const inputBytes = toInputBytes(input);
3685
+ const { status, output } = runWasmLoad(inputBytes, opts, "lint");
3686
+ if (status === 2) {
3687
+ throw decodeFallbackStatus2(output, opts.limits.maxOutputBytes);
3688
+ }
3689
+ const text = decoder.decode(output);
3690
+ const bridgeOpts = widenBridgeOptions(opts, output.byteLength);
3691
+ if (status === 1) {
3692
+ const decoded = decodeWire(text, bridgeOpts, inputBytes);
3693
+ if (decoded.kind === "error") {
3694
+ throw decoded.error;
3695
+ }
3696
+ throw notSourceError("internal", {}, "expected error envelope from lint_wire status 1");
3697
+ }
3698
+ return decodeLintEnvelope(text, bridgeOpts, inputBytes);
3699
+ }
3700
+ function isSafeMetaInt(n) {
3701
+ return (typeof n === "number" &&
3702
+ Number.isInteger(n) &&
3703
+ n >= 0 &&
3704
+ n <= MAX_SAFE_METADATA_INT);
3705
+ }
3706
+ function validateLintSpanForEncode(span) {
3707
+ if (!span || typeof span !== "object") {
3708
+ throw invalidOptionsError("LintDiagnostic.span must be a Span object");
3709
+ }
3710
+ const s = span;
3711
+ if (!s.start || typeof s.start !== "object" || !s.end || typeof s.end !== "object") {
3712
+ throw invalidOptionsError("LintDiagnostic.span must have start and end Locations");
3713
+ }
3714
+ const { start, end } = s;
3715
+ if (!isSafeMetaInt(start.byteOffset) ||
3716
+ !isSafeMetaInt(start.line) ||
3717
+ !isSafeMetaInt(start.column) ||
3718
+ !isSafeMetaInt(end.byteOffset) ||
3719
+ !isSafeMetaInt(end.line) ||
3720
+ !isSafeMetaInt(end.column) ||
3721
+ start.line < 1 ||
3722
+ start.column < 1 ||
3723
+ end.line < 1 ||
3724
+ end.column < 1 ||
3725
+ start.byteOffset > end.byteOffset ||
3726
+ start.line > end.line ||
3727
+ (start.line === end.line && start.column > end.column)) {
3728
+ throw invalidOptionsError("LintDiagnostic.span has invalid coordinates");
3729
+ }
3730
+ return s;
3731
+ }
3732
+ export function encodeLint(result, options) {
3733
+ const opts = validateOptions(options);
3734
+ if (!result || typeof result !== "object") {
3735
+ throw invalidOptionsError("encodeLint requires a LintResult object");
3736
+ }
3737
+ if (!Array.isArray(result.diagnostics)) {
3738
+ throw invalidOptionsError("LintResult.diagnostics must be an array");
3739
+ }
3740
+ if (!isSafeMetaInt(result.warningCount)) {
3741
+ throw invalidOptionsError("LintResult.warningCount must be a safe non-negative integer");
3742
+ }
3743
+ if (typeof result.truncated !== "boolean") {
3744
+ throw invalidOptionsError("LintResult.truncated must be a boolean");
3745
+ }
3746
+ if (result.diagnostics.length > opts.limits.maxWarnings) {
3747
+ throw invalidOptionsError("LintResult.diagnostics length exceeds max_warnings");
3748
+ }
3749
+ if (result.truncated) {
3750
+ if (result.warningCount <= result.diagnostics.length) {
3751
+ throw invalidOptionsError("truncated LintResult must have warningCount > diagnostics.length");
3752
+ }
3753
+ }
3754
+ else if (result.warningCount !== result.diagnostics.length) {
3755
+ throw invalidOptionsError("non-truncated LintResult must have warningCount === diagnostics.length");
3756
+ }
3757
+ for (const d of result.diagnostics) {
3758
+ if (!d || typeof d !== "object") {
3759
+ throw invalidOptionsError("LintDiagnostic must be an object");
3760
+ }
3761
+ if (typeof d.code !== "string" || !VALID_LINT_CODES.has(d.code)) {
3762
+ throw invalidOptionsError("invalid LintDiagnostic.code");
3763
+ }
3764
+ validateLintSpanForEncode(d.span);
3765
+ if (!isSafeMetaInt(d.documentIndex)) {
3766
+ throw invalidOptionsError("invalid LintDiagnostic.documentIndex");
3767
+ }
3768
+ if (!Array.isArray(d.path)) {
3769
+ throw invalidOptionsError("LintDiagnostic.path must be an array");
3770
+ }
3771
+ for (const seg of d.path) {
3772
+ if (!seg || typeof seg !== "object") {
3773
+ throw invalidOptionsError("invalid LintDiagnostic.path segment");
3774
+ }
3775
+ if ("index" in seg) {
3776
+ if (!isSafeMetaInt(seg.index)) {
3777
+ throw invalidOptionsError("LintDiagnostic.path index out of range");
3778
+ }
3779
+ }
3780
+ else if (!("key" in seg) || !isNodeObject(seg.key)) {
3781
+ throw invalidOptionsError("LintDiagnostic.path key must be a Node");
3782
+ }
3783
+ }
3784
+ if (d.path.length > opts.limits.maxDepth) {
3785
+ throw notSourceError("limit_exceeded", { limit: "max_depth", configured: opts.limits.maxDepth }, `diagnostic path has ${d.path.length} segments, more than max_depth`);
3786
+ }
3787
+ for (const seg of d.path) {
3788
+ if ("key" in seg) {
3789
+ preflightKeyNode(seg.key, opts.limits);
3790
+ }
3791
+ }
3792
+ validateUtf8Str(d.raw);
3793
+ validateUtf8Str(d.yaml12Resolution);
3794
+ validateUtf8Str(d.yaml11Note);
3795
+ validateUtf8Str(d.message);
3796
+ }
3797
+ const w = new BoundedWriter(MAX_WASM_INPUT_CEILING);
3798
+ const pathOpts = {
3799
+ mergeKeys: opts.mergeKeys,
3800
+ unknownTags: opts.unknownTags,
3801
+ limits: {
3802
+ ...opts.limits,
3803
+ maxDepth: 255,
3804
+ maxNodes: MAX_SAFE_METADATA_INT,
3805
+ },
3806
+ };
3807
+ const st = {
3808
+ opts: pathOpts,
3809
+ nodesRemaining: MAX_SAFE_METADATA_INT,
3810
+ activeAncestors: new Set(),
3811
+ };
3812
+ w.pushRaw('{"format":"yamlprims-lint","version":1,"warning_count":');
3813
+ w.pushMetaInt(result.warningCount, "warningCount");
3814
+ w.pushRaw(result.truncated ? ',"truncated":true,"diagnostics":[' : ',"truncated":false,"diagnostics":[');
3815
+ for (let i = 0; i < result.diagnostics.length; i++) {
3816
+ if (i > 0)
3817
+ w.pushRaw(",");
3818
+ const d = result.diagnostics[i];
3819
+ w.pushRaw('{"code":');
3820
+ w.pushString(d.code);
3821
+ w.pushRaw(',"span":');
3822
+ w.pushSpan(d.span);
3823
+ w.pushRaw(',"document_index":');
3824
+ w.pushMetaInt(d.documentIndex, "documentIndex");
3825
+ w.pushRaw(',"path":[');
3826
+ for (let j = 0; j < d.path.length; j++) {
3827
+ if (j > 0)
3828
+ w.pushRaw(",");
3829
+ const p = d.path[j];
3830
+ if ("index" in p) {
3831
+ w.pushRaw('{"index":');
3832
+ w.pushMetaInt(p.index, "path.index");
3833
+ w.pushRaw("}");
3834
+ }
3835
+ else {
3836
+ w.pushRaw('{"key":');
3837
+ writeNodeValue(w, p.key, false, st, 0);
3838
+ w.pushRaw(',"span":null,"via":null,"tag_start":null}}');
3839
+ }
3840
+ }
3841
+ w.pushRaw('],"raw":');
3842
+ w.pushBoundedString(d.raw, MAX_FIELD_BYTES);
3843
+ w.pushRaw(',"yaml12_resolution":');
3844
+ w.pushBoundedString(d.yaml12Resolution, MAX_FIELD_BYTES);
3845
+ w.pushRaw(',"yaml11_note":');
3846
+ w.pushBoundedString(d.yaml11Note, MAX_FIELD_BYTES);
3847
+ w.pushRaw(',"message":');
3848
+ w.pushBoundedString(d.message, MAX_MESSAGE_BYTES);
3849
+ w.pushRaw("}");
3850
+ }
3851
+ w.pushRaw("]}");
3852
+ const wireBytes = encoder.encode(w.finish());
3853
+ const { status, output } = runWasmLoad(wireBytes, opts, "encode-lint");
3854
+ if (status === 2) {
3855
+ throw decodeFallbackStatus2(output, opts.limits.maxOutputBytes);
3856
+ }
3857
+ const text = decoder.decode(output);
3858
+ if (status === 1) {
3859
+ const decoded = decodeWire(text, widenBridgeOptions(opts, output.byteLength));
3860
+ if (decoded.kind === "error") {
3861
+ throw decoded.error;
3862
+ }
3863
+ throw notSourceError("internal", {}, "expected error envelope from encode_lint_wire status 1");
3864
+ }
3865
+ return text;
3866
+ }
3567
3867
  // ---- Host emission & fromPlain (`spec-v1.5 §10.2`, `§11.3`, `§11.5`) -----
3568
3868
  function validateUtf8Str(s) {
3569
3869
  if (typeof s !== "string") {
@@ -4350,6 +4650,20 @@ export function dumpAll(documents, options) {
4350
4650
  export function dump(value, options) {
4351
4651
  return dumpAll([value], options);
4352
4652
  }
4653
+ export function parse(input, options) {
4654
+ const doc = load(input, options);
4655
+ return toPlain(doc, options);
4656
+ }
4657
+ export function parseAll(input, options) {
4658
+ const docs = loadAll(input, options);
4659
+ return docs.map((d) => toPlain(d, options));
4660
+ }
4661
+ export function stringify(value, options) {
4662
+ return dump(value, options);
4663
+ }
4664
+ export function stringifyAll(documents, options) {
4665
+ return dumpAll(documents, options);
4666
+ }
4353
4667
  // Internal-only hook for in-repo linear-memory and trap tests (not part of the
4354
4668
  // public TypeScript declarations or supported package surface).
4355
4669
  Object.defineProperty(load, Symbol.for("yamlprims.internal.wasmTestHarness"), {