@mermanjs/node-wasm 0.11.0 → 0.12.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/dist/errors.mjs CHANGED
@@ -1,3 +1,5 @@
1
+ import { Buffer } from "node:buffer";
2
+
1
3
  import {
2
4
  BINDING_OPERATION_METADATA_CONTRACT,
3
5
  BINDING_PAYLOAD_SCHEMAS,
@@ -8,6 +10,7 @@ import {
8
10
  NODE_TRANSPORT_FIELD_LIMITS,
9
11
  NODE_TRANSPORT_LIMITS,
10
12
  NODE_WIRE_CONTRACT,
13
+ nodeWireContract,
11
14
  } from "./transport-contract.mjs";
12
15
 
13
16
  const BINDING_RESULT_SCHEMA = BINDING_PAYLOAD_SCHEMAS.find(
@@ -175,6 +178,25 @@ export { NODE_TRANSPORT_FIELD_LIMITS, NODE_TRANSPORT_LIMITS, NODE_WIRE_CONTRACT
175
178
 
176
179
  export function parseTransportJsonText(value, label, limits) {
177
180
  assertTransportJsonTextLimits(value, label, limits);
181
+ return parseJsonText(value, label);
182
+ }
183
+
184
+ export function validateOperationOptionsJson(value, countJsonEscapes = false) {
185
+ if (typeof value !== "string") {
186
+ throw new TypeError("optionsJson must be a JSON string when provided.");
187
+ }
188
+ const label = "operation request optionsJson";
189
+ const limits = NODE_TRANSPORT_LIMITS.binding_options;
190
+ const bytes = measureUtf8Field(
191
+ value, label, NODE_TRANSPORT_FIELD_LIMITS.options_json_utf8_bytes,
192
+ countJsonEscapes, limits.max_utf8_bytes,
193
+ );
194
+ scanBoundedJsonText(value, label, limits);
195
+ parseJsonText(value, label);
196
+ return bytes;
197
+ }
198
+
199
+ function parseJsonText(value, label) {
178
200
  try {
179
201
  return JSON.parse(value);
180
202
  } catch (cause) {
@@ -192,12 +214,10 @@ function assertTransportJsonTextLimits(value, label, limits) {
192
214
  if (!limits || typeof limits !== "object") {
193
215
  throw new TypeError(`Missing Node wire limits for ${label}.`);
194
216
  }
195
- const byteLength = strictUtf8ByteLength(value, label);
196
- if (byteLength > limits.max_utf8_bytes) {
197
- throw new MermanInvalidTransportError(
198
- `Merman transport ${label} exceeds the ${limits.max_utf8_bytes}-byte wire limit.`,
199
- );
217
+ if (value.length > limits.max_utf8_bytes) {
218
+ throw utf8WireLimitError(label, limits.max_utf8_bytes);
200
219
  }
220
+ measureUtf8Field(value, label, Infinity, false, limits.max_utf8_bytes);
201
221
  scanBoundedJsonText(value, label, limits);
202
222
  }
203
223
 
@@ -379,7 +399,9 @@ function addJsonWireToken(budget, limits) {
379
399
  }
380
400
 
381
401
  function addJsonStringWireBytes(value, label, limits, budget) {
402
+ const maxStringUtf8Bytes = limits.max_string_utf8_bytes;
382
403
  let utf8Bytes = 0;
404
+ let hasIsolatedSurrogate = false;
383
405
  addJsonWireBytes(budget, 1);
384
406
  for (let index = 0; index < value.length; index += 1) {
385
407
  const codeUnit = value.charCodeAt(index);
@@ -387,30 +409,50 @@ function addJsonStringWireBytes(value, label, limits, budget) {
387
409
  let wireBytes = characterBytes;
388
410
  if (codeUnit <= 0x7f) {
389
411
  wireBytes = jsonAsciiByteLength(codeUnit);
390
- } else if (codeUnit >= 0xd800 && codeUnit <= 0xdbff) {
412
+ } else if (codeUnit >= 0xd800 && codeUnit <= 0xdfff) {
391
413
  const low = value.charCodeAt(index + 1);
392
- if (!(low >= 0xdc00 && low <= 0xdfff)) {
393
- throw new MermanInvalidTransportError(
394
- `Merman transport ${label} contains an isolated UTF-16 surrogate.`,
395
- );
414
+ if (codeUnit > 0xdbff || !(low >= 0xdc00 && low <= 0xdfff)) {
415
+ hasIsolatedSurrogate = true;
416
+ wireBytes = 6;
417
+ } else {
418
+ characterBytes = 4;
419
+ wireBytes = 4;
420
+ index += 1;
396
421
  }
397
- characterBytes = 4;
398
- wireBytes = 4;
399
- index += 1;
400
- } else if (codeUnit >= 0xdc00 && codeUnit <= 0xdfff) {
401
- throw new MermanInvalidTransportError(
402
- `Merman transport ${label} contains an isolated UTF-16 surrogate.`,
403
- );
404
422
  }
405
423
  utf8Bytes += characterBytes;
406
- if (utf8Bytes > limits.max_string_utf8_bytes) {
407
- throw new MermanInvalidTransportError(
408
- `Merman transport ${label} contains a string exceeding the ${limits.max_string_utf8_bytes}-byte field limit.`,
409
- );
424
+ if (utf8Bytes > maxStringUtf8Bytes) {
425
+ throw jsonStringFieldLimitError(label, maxStringUtf8Bytes);
410
426
  }
411
427
  addJsonWireBytes(budget, wireBytes);
412
428
  }
413
429
  addJsonWireBytes(budget, 1);
430
+ if (hasIsolatedSurrogate) {
431
+ throw new MermanInvalidTransportError(
432
+ `Merman transport ${label} contains an isolated UTF-16 surrogate.`,
433
+ );
434
+ }
435
+ }
436
+
437
+ function jsonStringFieldLimitError(label, maxUtf8Bytes) {
438
+ return new MermanInvalidTransportError(
439
+ `Merman transport ${label} contains a string exceeding the ${maxUtf8Bytes}-byte field limit.`,
440
+ );
441
+ }
442
+
443
+ const UNBOUNDED_JSON_STRING_LIMITS = Object.freeze({
444
+ max_string_utf8_bytes: Infinity,
445
+ });
446
+
447
+ export function addJsonEnvelopeBytes(budget, bytes) {
448
+ addJsonWireBytes(budget, bytes);
449
+ }
450
+
451
+ export function addJsonEnvelopeStringBytes(value, label, budget) {
452
+ if (value.length > budget.max - budget.used - 2) {
453
+ throw utf8WireLimitError(budget.label, budget.max);
454
+ }
455
+ addJsonStringWireBytes(value, label, UNBOUNDED_JSON_STRING_LIMITS, budget);
414
456
  }
415
457
 
416
458
  function addJsonWireBytes(budget, bytes) {
@@ -422,6 +464,19 @@ function addJsonWireBytes(budget, bytes) {
422
464
  budget.used += bytes;
423
465
  }
424
466
 
467
+ export function normalizeOperationSignal(signal) {
468
+ if (signal === null || signal === undefined) return undefined;
469
+ if (
470
+ typeof signal !== "object" ||
471
+ !("aborted" in signal) ||
472
+ typeof signal.addEventListener !== "function" ||
473
+ typeof signal.removeEventListener !== "function"
474
+ ) {
475
+ throw new TypeError("operation signal must be an AbortSignal.");
476
+ }
477
+ return signal;
478
+ }
479
+
425
480
  export function assertUtf8Field(value, label, maxUtf8Bytes) {
426
481
  return measureUtf8Field(value, label, maxUtf8Bytes, false);
427
482
  }
@@ -447,7 +502,7 @@ export function parseRuntimeCatalogJsonText(value, label = "runtime catalog") {
447
502
 
448
503
  export function validateTransportIdentityJson(
449
504
  value,
450
- { expectedTransport, expectedPackageVersion },
505
+ { expectedTransport, expectedPackageVersion, localLayout = null },
451
506
  ) {
452
507
  const identity = parseTransportJsonText(
453
508
  value,
@@ -465,7 +520,9 @@ export function validateTransportIdentityJson(
465
520
  identity.binding_result_payload_version !==
466
521
  NODE_WIRE_CONTRACT.binding_result_payload_version ||
467
522
  identity.capability_descriptor_digest !== CAPABILITY_DESCRIPTOR_DIGEST ||
468
- !sameJsonValue(identity.wire_contract, NODE_WIRE_CONTRACT)
523
+ !sameJsonValue(identity.wire_contract, nodeWireContract(
524
+ localLayout, expectedTransport, identity.wire_contract?.artifact?.capability_ids,
525
+ ))
469
526
  ) {
470
527
  throw new MermanInvalidTransportError(
471
528
  "The Merman candidate module identity is incompatible with this loader package.",
@@ -486,7 +543,7 @@ export function validateTransportIdentityJson(
486
543
 
487
544
  export function validateTransportIdentityExport(
488
545
  readIdentity,
489
- { expectedTransport, expectedPackageVersion, label },
546
+ { expectedTransport, expectedPackageVersion, label, localLayout = null },
490
547
  ) {
491
548
  if (typeof readIdentity !== "function") {
492
549
  throw new MermanInvalidTransportError(
@@ -505,6 +562,7 @@ export function validateTransportIdentityExport(
505
562
  return validateTransportIdentityJson(value, {
506
563
  expectedPackageVersion,
507
564
  expectedTransport,
565
+ localLayout,
508
566
  });
509
567
  }
510
568
 
@@ -512,7 +570,43 @@ export function strictUtf8ByteLength(value, label = "text") {
512
570
  return measureUtf8Field(value, label, Infinity, false);
513
571
  }
514
572
 
515
- function measureUtf8Field(value, label, maxUtf8Bytes, countJsonEscapes) {
573
+ function utf8FieldLimitError(label, maxUtf8Bytes) {
574
+ return new MermanInvalidTransportError(
575
+ `Merman transport ${label} exceeds the ${maxUtf8Bytes}-byte field limit.`,
576
+ );
577
+ }
578
+
579
+ function utf8WireLimitError(label, maxUtf8Bytes) {
580
+ return new MermanInvalidTransportError(
581
+ `Merman transport ${label} exceeds the ${maxUtf8Bytes}-byte wire limit.`,
582
+ );
583
+ }
584
+
585
+ function utf8LimitError(label, bytes, maxUtf8Bytes, maxDocumentUtf8Bytes) {
586
+ if (bytes > maxUtf8Bytes) {
587
+ return utf8FieldLimitError(label, maxUtf8Bytes);
588
+ }
589
+ if (bytes > maxDocumentUtf8Bytes) {
590
+ return utf8WireLimitError(label, maxDocumentUtf8Bytes);
591
+ }
592
+ }
593
+
594
+ function isolatedUtf16FieldError(value, label, maxUtf8Bytes, maxDocumentUtf8Bytes) {
595
+ if (value.length > Math.min(maxUtf8Bytes, maxDocumentUtf8Bytes) / 3) {
596
+ const bytes = Buffer.byteLength(value, "utf8");
597
+ const error = utf8LimitError(label, bytes, maxUtf8Bytes, maxDocumentUtf8Bytes);
598
+ if (error) return error;
599
+ }
600
+ return new MermanInvalidTransportError(
601
+ `Merman transport ${label} contains an isolated UTF-16 surrogate.`,
602
+ );
603
+ }
604
+
605
+ function measureUtf8Field(value, label, maxUtf8Bytes, countJsonEscapes, maxDocumentUtf8Bytes = Infinity) {
606
+ // Each UTF-16 unit contributes at least one byte, including replacement for an isolated unit.
607
+ if (value.length > maxUtf8Bytes) {
608
+ throw utf8FieldLimitError(label, maxUtf8Bytes);
609
+ }
516
610
  let bytes = 0;
517
611
  let escapeBytes = 0;
518
612
  for (let index = 0; index < value.length; index += 1) {
@@ -525,25 +619,18 @@ function measureUtf8Field(value, label, maxUtf8Bytes, countJsonEscapes) {
525
619
  } else if (codeUnit >= 0xd800 && codeUnit <= 0xdbff) {
526
620
  const low = value.charCodeAt(index + 1);
527
621
  if (!(low >= 0xdc00 && low <= 0xdfff)) {
528
- throw new MermanInvalidTransportError(
529
- `Merman transport ${label} contains an isolated UTF-16 surrogate.`,
530
- );
622
+ throw isolatedUtf16FieldError(value, label, maxUtf8Bytes, maxDocumentUtf8Bytes);
531
623
  }
532
624
  bytes += 4;
533
625
  index += 1;
534
626
  } else if (codeUnit >= 0xdc00 && codeUnit <= 0xdfff) {
535
- throw new MermanInvalidTransportError(
536
- `Merman transport ${label} contains an isolated UTF-16 surrogate.`,
537
- );
627
+ throw isolatedUtf16FieldError(value, label, maxUtf8Bytes, maxDocumentUtf8Bytes);
538
628
  } else {
539
629
  bytes += 3;
540
630
  }
541
631
  }
542
- if (bytes > maxUtf8Bytes) {
543
- throw new MermanInvalidTransportError(
544
- `Merman transport ${label} exceeds the ${maxUtf8Bytes}-byte field limit.`,
545
- );
546
- }
632
+ const error = utf8LimitError(label, bytes, maxUtf8Bytes, maxDocumentUtf8Bytes);
633
+ if (error) throw error;
547
634
  return bytes + escapeBytes;
548
635
  }
549
636
 
@@ -567,14 +654,14 @@ function scanBoundedJsonText(source, label, limits) {
567
654
  let members = 0;
568
655
  let tokens = 0;
569
656
  const stack = [];
570
- for (let index = 0; index < source.length;) {
657
+ const length = source.length;
658
+ for (let index = 0; index < length;) {
571
659
  const codeUnit = source.charCodeAt(index);
572
660
  if (isJsonWhitespaceCodeUnit(codeUnit)) {
573
661
  index += 1;
574
662
  continue;
575
663
  }
576
- const character = source[index];
577
- if (character === '"') {
664
+ if (codeUnit === 0x22) {
578
665
  const scanned = scanJsonStringToken(source, index, label);
579
666
  if (scanned.utf8Bytes > limits.max_string_utf8_bytes) {
580
667
  throw new MermanInvalidTransportError(
@@ -583,8 +670,8 @@ function scanBoundedJsonText(source, label, limits) {
583
670
  }
584
671
  tokens += 1;
585
672
  index = scanned.end;
586
- } else if (character === "{" || character === "[") {
587
- stack.push(character);
673
+ } else if (codeUnit === 0x7b || codeUnit === 0x5b) {
674
+ stack.push(codeUnit);
588
675
  depth += 1;
589
676
  tokens += 1;
590
677
  if (depth > limits.max_depth) {
@@ -593,16 +680,16 @@ function scanBoundedJsonText(source, label, limits) {
593
680
  );
594
681
  }
595
682
  index += 1;
596
- } else if (character === "}" || character === "]") {
597
- const expected = character === "}" ? "{" : "[";
683
+ } else if (codeUnit === 0x7d || codeUnit === 0x5d) {
684
+ const expected = codeUnit === 0x7d ? 0x7b : 0x5b;
598
685
  if (stack.at(-1) !== expected) break;
599
686
  stack.pop();
600
687
  depth -= 1;
601
688
  index += 1;
602
- } else if (character === ":") {
689
+ } else if (codeUnit === 0x3a) {
603
690
  members += 1;
604
691
  index += 1;
605
- } else if (character === "-" || (character >= "0" && character <= "9")) {
692
+ } else if (codeUnit === 0x2d || (codeUnit >= 0x30 && codeUnit <= 0x39)) {
606
693
  JSON_NUMBER_TOKEN.lastIndex = index;
607
694
  const match = JSON_NUMBER_TOKEN.exec(source);
608
695
  if (match === null) break;
@@ -613,13 +700,13 @@ function scanBoundedJsonText(source, label, limits) {
613
700
  }
614
701
  tokens += 1;
615
702
  index = JSON_NUMBER_TOKEN.lastIndex;
616
- } else if (source.startsWith("true", index)) {
703
+ } else if (codeUnit === 0x74 && source.startsWith("true", index)) {
617
704
  tokens += 1;
618
705
  index += 4;
619
- } else if (source.startsWith("false", index)) {
706
+ } else if (codeUnit === 0x66 && source.startsWith("false", index)) {
620
707
  tokens += 1;
621
708
  index += 5;
622
- } else if (source.startsWith("null", index)) {
709
+ } else if (codeUnit === 0x6e && source.startsWith("null", index)) {
623
710
  tokens += 1;
624
711
  index += 4;
625
712
  } else {
@@ -640,15 +727,26 @@ function scanBoundedJsonText(source, label, limits) {
640
727
 
641
728
  function scanJsonStringToken(source, start, label) {
642
729
  let utf8Bytes = 0;
643
- for (let index = start + 1; index < source.length; index += 1) {
644
- const codeUnit = source.charCodeAt(index);
730
+ const length = source.length;
731
+ let index = start + 1;
732
+ while (index < length) {
733
+ // Fast-scan plain ASCII runs without per-character string allocations.
734
+ const runStart = index;
735
+ let codeUnit = 0;
736
+ while (index < length) {
737
+ codeUnit = source.charCodeAt(index);
738
+ if (codeUnit === 0x22 || codeUnit === 0x5c || codeUnit > 0x7f) break;
739
+ index += 1;
740
+ }
741
+ utf8Bytes += index - runStart;
742
+ if (index >= length) break;
645
743
  if (codeUnit === 0x22) return { end: index + 1, utf8Bytes };
646
744
  if (codeUnit === 0x5c) {
647
- const escaped = source[index + 1];
648
- if (escaped === "u") {
745
+ const escaped = source.charCodeAt(index + 1);
746
+ if (escaped === 0x75) {
649
747
  const first = parseJsonUnicodeEscape(source, index, label);
650
748
  if (first >= 0xd800 && first <= 0xdbff) {
651
- if (source[index + 6] !== "\\" || source[index + 7] !== "u") {
749
+ if (source.charCodeAt(index + 6) !== 0x5c || source.charCodeAt(index + 7) !== 0x75) {
652
750
  throw new MermanInvalidTransportError(
653
751
  `Merman transport ${label} contains an isolated JSON surrogate escape.`,
654
752
  );
@@ -660,7 +758,7 @@ function scanJsonStringToken(source, start, label) {
660
758
  );
661
759
  }
662
760
  utf8Bytes += 4;
663
- index += 11;
761
+ index += 12;
664
762
  continue;
665
763
  }
666
764
  if (first >= 0xdc00 && first <= 0xdfff) {
@@ -669,17 +767,16 @@ function scanJsonStringToken(source, start, label) {
669
767
  );
670
768
  }
671
769
  utf8Bytes += utf8LengthOfCodeUnit(first);
672
- index += 5;
770
+ index += 6;
673
771
  continue;
674
772
  }
675
773
  utf8Bytes += 1;
676
- index += 1;
774
+ index += 2;
677
775
  continue;
678
776
  }
679
- if (codeUnit <= 0x7f) {
680
- utf8Bytes += 1;
681
- } else if (codeUnit <= 0x7ff) {
777
+ if (codeUnit <= 0x7ff) {
682
778
  utf8Bytes += 2;
779
+ index += 1;
683
780
  } else if (codeUnit >= 0xd800 && codeUnit <= 0xdbff) {
684
781
  const low = source.charCodeAt(index + 1);
685
782
  if (!(low >= 0xdc00 && low <= 0xdfff)) {
@@ -688,16 +785,17 @@ function scanJsonStringToken(source, start, label) {
688
785
  );
689
786
  }
690
787
  utf8Bytes += 4;
691
- index += 1;
788
+ index += 2;
692
789
  } else if (codeUnit >= 0xdc00 && codeUnit <= 0xdfff) {
693
790
  throw new MermanInvalidTransportError(
694
791
  `Merman transport ${label} contains an isolated UTF-16 surrogate.`,
695
792
  );
696
793
  } else {
697
794
  utf8Bytes += 3;
795
+ index += 1;
698
796
  }
699
797
  }
700
- return { end: source.length, utf8Bytes };
798
+ return { end: length, utf8Bytes };
701
799
  }
702
800
 
703
801
  function parseJsonUnicodeEscape(source, slashIndex, label) {
@@ -854,16 +952,21 @@ function skipJsonWhitespace(source, index) {
854
952
  }
855
953
 
856
954
  function skipJsonString(value, index) {
857
- while (index < value.length) {
858
- if (value[index] === "\\") {
859
- index += 2;
860
- } else if (value[index] === '"') {
861
- return index + 1;
862
- } else {
863
- index += 1;
955
+ const length = value.length;
956
+ while (index < length) {
957
+ const quote = value.indexOf('"', index);
958
+ if (quote === -1) return length;
959
+ // Count consecutive backslashes immediately preceding the quote; even means unescaped end.
960
+ let slashes = 0;
961
+ let cursor = quote - 1;
962
+ while (cursor >= index && value.charCodeAt(cursor) === 0x5c) {
963
+ slashes += 1;
964
+ cursor -= 1;
864
965
  }
966
+ if ((slashes & 1) === 0) return quote + 1;
967
+ index = quote + 1;
865
968
  }
866
- return index;
969
+ return length;
867
970
  }
868
971
 
869
972
  function isExactSafeIntegerJsonToken(token, sourceLength) {
@@ -954,7 +1057,7 @@ export function decodeWireResponse(
954
1057
  throw new MermanOperationError(error);
955
1058
  }
956
1059
  if (requireError) {
957
- throw new MermanInvalidTransportError("The native SVG failure must contain an error envelope.");
1060
+ throw new MermanInvalidTransportError("The direct SVG failure must contain an error envelope.");
958
1061
  }
959
1062
  if (envelope.ok !== true || Object.hasOwn(envelope, "error")) {
960
1063
  throw new MermanInvalidTransportError(