@mermanjs/node-wasm 0.10.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,48 +399,60 @@ 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);
386
408
  let characterBytes = utf8LengthOfCodeUnit(codeUnit);
387
409
  let wireBytes = characterBytes;
388
- if (codeUnit === 0x22 || codeUnit === 0x5c) {
389
- wireBytes = 2;
390
- } else if (
391
- codeUnit === 0x08 ||
392
- codeUnit === 0x09 ||
393
- codeUnit === 0x0a ||
394
- codeUnit === 0x0c ||
395
- codeUnit === 0x0d
396
- ) {
397
- wireBytes = 2;
398
- } else if (codeUnit <= 0x1f) {
399
- wireBytes = 6;
400
- } else if (codeUnit >= 0xd800 && codeUnit <= 0xdbff) {
410
+ if (codeUnit <= 0x7f) {
411
+ wireBytes = jsonAsciiByteLength(codeUnit);
412
+ } else if (codeUnit >= 0xd800 && codeUnit <= 0xdfff) {
401
413
  const low = value.charCodeAt(index + 1);
402
- if (!(low >= 0xdc00 && low <= 0xdfff)) {
403
- throw new MermanInvalidTransportError(
404
- `Merman transport ${label} contains an isolated UTF-16 surrogate.`,
405
- );
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;
406
421
  }
407
- characterBytes = 4;
408
- wireBytes = 4;
409
- index += 1;
410
- } else if (codeUnit >= 0xdc00 && codeUnit <= 0xdfff) {
411
- throw new MermanInvalidTransportError(
412
- `Merman transport ${label} contains an isolated UTF-16 surrogate.`,
413
- );
414
422
  }
415
423
  utf8Bytes += characterBytes;
416
- if (utf8Bytes > limits.max_string_utf8_bytes) {
417
- throw new MermanInvalidTransportError(
418
- `Merman transport ${label} contains a string exceeding the ${limits.max_string_utf8_bytes}-byte field limit.`,
419
- );
424
+ if (utf8Bytes > maxStringUtf8Bytes) {
425
+ throw jsonStringFieldLimitError(label, maxStringUtf8Bytes);
420
426
  }
421
427
  addJsonWireBytes(budget, wireBytes);
422
428
  }
423
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);
424
456
  }
425
457
 
426
458
  function addJsonWireBytes(budget, bytes) {
@@ -432,14 +464,26 @@ function addJsonWireBytes(budget, bytes) {
432
464
  budget.used += bytes;
433
465
  }
434
466
 
435
- export function assertUtf8Field(value, label, maxUtf8Bytes) {
436
- const bytes = strictUtf8ByteLength(value, label);
437
- if (bytes > maxUtf8Bytes) {
438
- throw new MermanInvalidTransportError(
439
- `Merman transport ${label} exceeds the ${maxUtf8Bytes}-byte field limit.`,
440
- );
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.");
441
476
  }
442
- return bytes;
477
+ return signal;
478
+ }
479
+
480
+ export function assertUtf8Field(value, label, maxUtf8Bytes) {
481
+ return measureUtf8Field(value, label, maxUtf8Bytes, false);
482
+ }
483
+
484
+ // Counts an escaped JSON string's contents without allocating it or including its quotes.
485
+ export function jsonStringFieldBytes(value, label, maxUtf8Bytes) {
486
+ return measureUtf8Field(value, label, maxUtf8Bytes, true);
443
487
  }
444
488
 
445
489
  export function parseRuntimeCatalogJsonText(value, label = "runtime catalog") {
@@ -458,7 +502,7 @@ export function parseRuntimeCatalogJsonText(value, label = "runtime catalog") {
458
502
 
459
503
  export function validateTransportIdentityJson(
460
504
  value,
461
- { expectedTransport, expectedPackageVersion },
505
+ { expectedTransport, expectedPackageVersion, localLayout = null },
462
506
  ) {
463
507
  const identity = parseTransportJsonText(
464
508
  value,
@@ -476,7 +520,9 @@ export function validateTransportIdentityJson(
476
520
  identity.binding_result_payload_version !==
477
521
  NODE_WIRE_CONTRACT.binding_result_payload_version ||
478
522
  identity.capability_descriptor_digest !== CAPABILITY_DESCRIPTOR_DIGEST ||
479
- !sameJsonValue(identity.wire_contract, NODE_WIRE_CONTRACT)
523
+ !sameJsonValue(identity.wire_contract, nodeWireContract(
524
+ localLayout, expectedTransport, identity.wire_contract?.artifact?.capability_ids,
525
+ ))
480
526
  ) {
481
527
  throw new MermanInvalidTransportError(
482
528
  "The Merman candidate module identity is incompatible with this loader package.",
@@ -497,7 +543,7 @@ export function validateTransportIdentityJson(
497
543
 
498
544
  export function validateTransportIdentityExport(
499
545
  readIdentity,
500
- { expectedTransport, expectedPackageVersion, label },
546
+ { expectedTransport, expectedPackageVersion, label, localLayout = null },
501
547
  ) {
502
548
  if (typeof readIdentity !== "function") {
503
549
  throw new MermanInvalidTransportError(
@@ -516,35 +562,91 @@ export function validateTransportIdentityExport(
516
562
  return validateTransportIdentityJson(value, {
517
563
  expectedPackageVersion,
518
564
  expectedTransport,
565
+ localLayout,
519
566
  });
520
567
  }
521
568
 
522
569
  export function strictUtf8ByteLength(value, label = "text") {
570
+ return measureUtf8Field(value, label, Infinity, false);
571
+ }
572
+
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
+ }
523
610
  let bytes = 0;
611
+ let escapeBytes = 0;
524
612
  for (let index = 0; index < value.length; index += 1) {
525
613
  const codeUnit = value.charCodeAt(index);
526
614
  if (codeUnit <= 0x7f) {
527
615
  bytes += 1;
616
+ if (countJsonEscapes) escapeBytes += jsonAsciiByteLength(codeUnit) - 1;
528
617
  } else if (codeUnit <= 0x7ff) {
529
618
  bytes += 2;
530
619
  } else if (codeUnit >= 0xd800 && codeUnit <= 0xdbff) {
531
620
  const low = value.charCodeAt(index + 1);
532
621
  if (!(low >= 0xdc00 && low <= 0xdfff)) {
533
- throw new MermanInvalidTransportError(
534
- `Merman transport ${label} contains an isolated UTF-16 surrogate.`,
535
- );
622
+ throw isolatedUtf16FieldError(value, label, maxUtf8Bytes, maxDocumentUtf8Bytes);
536
623
  }
537
624
  bytes += 4;
538
625
  index += 1;
539
626
  } else if (codeUnit >= 0xdc00 && codeUnit <= 0xdfff) {
540
- throw new MermanInvalidTransportError(
541
- `Merman transport ${label} contains an isolated UTF-16 surrogate.`,
542
- );
627
+ throw isolatedUtf16FieldError(value, label, maxUtf8Bytes, maxDocumentUtf8Bytes);
543
628
  } else {
544
629
  bytes += 3;
545
630
  }
546
631
  }
547
- return bytes;
632
+ const error = utf8LimitError(label, bytes, maxUtf8Bytes, maxDocumentUtf8Bytes);
633
+ if (error) throw error;
634
+ return bytes + escapeBytes;
635
+ }
636
+
637
+ function jsonAsciiByteLength(codeUnit) {
638
+ switch (codeUnit) {
639
+ case 0x08:
640
+ case 0x09:
641
+ case 0x0a:
642
+ case 0x0c:
643
+ case 0x0d:
644
+ case 0x22:
645
+ case 0x5c:
646
+ return 2;
647
+ default:
648
+ return codeUnit <= 0x1f ? 6 : 1;
649
+ }
548
650
  }
549
651
 
550
652
  function scanBoundedJsonText(source, label, limits) {
@@ -552,14 +654,14 @@ function scanBoundedJsonText(source, label, limits) {
552
654
  let members = 0;
553
655
  let tokens = 0;
554
656
  const stack = [];
555
- for (let index = 0; index < source.length;) {
657
+ const length = source.length;
658
+ for (let index = 0; index < length;) {
556
659
  const codeUnit = source.charCodeAt(index);
557
660
  if (isJsonWhitespaceCodeUnit(codeUnit)) {
558
661
  index += 1;
559
662
  continue;
560
663
  }
561
- const character = source[index];
562
- if (character === '"') {
664
+ if (codeUnit === 0x22) {
563
665
  const scanned = scanJsonStringToken(source, index, label);
564
666
  if (scanned.utf8Bytes > limits.max_string_utf8_bytes) {
565
667
  throw new MermanInvalidTransportError(
@@ -568,8 +670,8 @@ function scanBoundedJsonText(source, label, limits) {
568
670
  }
569
671
  tokens += 1;
570
672
  index = scanned.end;
571
- } else if (character === "{" || character === "[") {
572
- stack.push(character);
673
+ } else if (codeUnit === 0x7b || codeUnit === 0x5b) {
674
+ stack.push(codeUnit);
573
675
  depth += 1;
574
676
  tokens += 1;
575
677
  if (depth > limits.max_depth) {
@@ -578,16 +680,16 @@ function scanBoundedJsonText(source, label, limits) {
578
680
  );
579
681
  }
580
682
  index += 1;
581
- } else if (character === "}" || character === "]") {
582
- const expected = character === "}" ? "{" : "[";
683
+ } else if (codeUnit === 0x7d || codeUnit === 0x5d) {
684
+ const expected = codeUnit === 0x7d ? 0x7b : 0x5b;
583
685
  if (stack.at(-1) !== expected) break;
584
686
  stack.pop();
585
687
  depth -= 1;
586
688
  index += 1;
587
- } else if (character === ":") {
689
+ } else if (codeUnit === 0x3a) {
588
690
  members += 1;
589
691
  index += 1;
590
- } else if (character === "-" || (character >= "0" && character <= "9")) {
692
+ } else if (codeUnit === 0x2d || (codeUnit >= 0x30 && codeUnit <= 0x39)) {
591
693
  JSON_NUMBER_TOKEN.lastIndex = index;
592
694
  const match = JSON_NUMBER_TOKEN.exec(source);
593
695
  if (match === null) break;
@@ -598,13 +700,13 @@ function scanBoundedJsonText(source, label, limits) {
598
700
  }
599
701
  tokens += 1;
600
702
  index = JSON_NUMBER_TOKEN.lastIndex;
601
- } else if (source.startsWith("true", index)) {
703
+ } else if (codeUnit === 0x74 && source.startsWith("true", index)) {
602
704
  tokens += 1;
603
705
  index += 4;
604
- } else if (source.startsWith("false", index)) {
706
+ } else if (codeUnit === 0x66 && source.startsWith("false", index)) {
605
707
  tokens += 1;
606
708
  index += 5;
607
- } else if (source.startsWith("null", index)) {
709
+ } else if (codeUnit === 0x6e && source.startsWith("null", index)) {
608
710
  tokens += 1;
609
711
  index += 4;
610
712
  } else {
@@ -625,15 +727,26 @@ function scanBoundedJsonText(source, label, limits) {
625
727
 
626
728
  function scanJsonStringToken(source, start, label) {
627
729
  let utf8Bytes = 0;
628
- for (let index = start + 1; index < source.length; index += 1) {
629
- 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;
630
743
  if (codeUnit === 0x22) return { end: index + 1, utf8Bytes };
631
744
  if (codeUnit === 0x5c) {
632
- const escaped = source[index + 1];
633
- if (escaped === "u") {
745
+ const escaped = source.charCodeAt(index + 1);
746
+ if (escaped === 0x75) {
634
747
  const first = parseJsonUnicodeEscape(source, index, label);
635
748
  if (first >= 0xd800 && first <= 0xdbff) {
636
- if (source[index + 6] !== "\\" || source[index + 7] !== "u") {
749
+ if (source.charCodeAt(index + 6) !== 0x5c || source.charCodeAt(index + 7) !== 0x75) {
637
750
  throw new MermanInvalidTransportError(
638
751
  `Merman transport ${label} contains an isolated JSON surrogate escape.`,
639
752
  );
@@ -645,7 +758,7 @@ function scanJsonStringToken(source, start, label) {
645
758
  );
646
759
  }
647
760
  utf8Bytes += 4;
648
- index += 11;
761
+ index += 12;
649
762
  continue;
650
763
  }
651
764
  if (first >= 0xdc00 && first <= 0xdfff) {
@@ -654,17 +767,16 @@ function scanJsonStringToken(source, start, label) {
654
767
  );
655
768
  }
656
769
  utf8Bytes += utf8LengthOfCodeUnit(first);
657
- index += 5;
770
+ index += 6;
658
771
  continue;
659
772
  }
660
773
  utf8Bytes += 1;
661
- index += 1;
774
+ index += 2;
662
775
  continue;
663
776
  }
664
- if (codeUnit <= 0x7f) {
665
- utf8Bytes += 1;
666
- } else if (codeUnit <= 0x7ff) {
777
+ if (codeUnit <= 0x7ff) {
667
778
  utf8Bytes += 2;
779
+ index += 1;
668
780
  } else if (codeUnit >= 0xd800 && codeUnit <= 0xdbff) {
669
781
  const low = source.charCodeAt(index + 1);
670
782
  if (!(low >= 0xdc00 && low <= 0xdfff)) {
@@ -673,16 +785,17 @@ function scanJsonStringToken(source, start, label) {
673
785
  );
674
786
  }
675
787
  utf8Bytes += 4;
676
- index += 1;
788
+ index += 2;
677
789
  } else if (codeUnit >= 0xdc00 && codeUnit <= 0xdfff) {
678
790
  throw new MermanInvalidTransportError(
679
791
  `Merman transport ${label} contains an isolated UTF-16 surrogate.`,
680
792
  );
681
793
  } else {
682
794
  utf8Bytes += 3;
795
+ index += 1;
683
796
  }
684
797
  }
685
- return { end: source.length, utf8Bytes };
798
+ return { end: length, utf8Bytes };
686
799
  }
687
800
 
688
801
  function parseJsonUnicodeEscape(source, slashIndex, label) {
@@ -839,16 +952,21 @@ function skipJsonWhitespace(source, index) {
839
952
  }
840
953
 
841
954
  function skipJsonString(value, index) {
842
- while (index < value.length) {
843
- if (value[index] === "\\") {
844
- index += 2;
845
- } else if (value[index] === '"') {
846
- return index + 1;
847
- } else {
848
- 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;
849
965
  }
966
+ if ((slashes & 1) === 0) return quote + 1;
967
+ index = quote + 1;
850
968
  }
851
- return index;
969
+ return length;
852
970
  }
853
971
 
854
972
  function isExactSafeIntegerJsonToken(token, sourceLength) {
@@ -904,6 +1022,7 @@ export function decodeWireResponse(
904
1022
  expectation,
905
1023
  {
906
1024
  includeMetadata = true,
1025
+ requireError = false,
907
1026
  allowedCancellationReasons = [],
908
1027
  requireUnavailable = false,
909
1028
  availableCapabilityIds = [],
@@ -937,6 +1056,9 @@ export function decodeWireResponse(
937
1056
  );
938
1057
  throw new MermanOperationError(error);
939
1058
  }
1059
+ if (requireError) {
1060
+ throw new MermanInvalidTransportError("The direct SVG failure must contain an error envelope.");
1061
+ }
940
1062
  if (envelope.ok !== true || Object.hasOwn(envelope, "error")) {
941
1063
  throw new MermanInvalidTransportError(
942
1064
  "Merman transport success envelopes must contain result only.",
@@ -947,7 +1069,12 @@ export function decodeWireResponse(
947
1069
  "Merman transport executed an operation that its runtime catalog does not advertise.",
948
1070
  );
949
1071
  }
950
- return validateSuccessResult(envelope.result, expectation, includeMetadata);
1072
+ return validateSuccessResult(
1073
+ envelope.result,
1074
+ expectation,
1075
+ includeMetadata,
1076
+ Object.hasOwn(envelope, "result"),
1077
+ );
951
1078
  }
952
1079
 
953
1080
  function validateAllowedCancellationReasons(reasons) {
@@ -1003,7 +1130,7 @@ function validateEnvelopeHeader(envelope, label) {
1003
1130
  }
1004
1131
  }
1005
1132
 
1006
- function validateSuccessResult(result, expectation, includeMetadata) {
1133
+ function validateSuccessResult(result, expectation, includeMetadata, hasOwnResult) {
1007
1134
  if (
1008
1135
  !isPlainJsonObject(result) ||
1009
1136
  typeof result.operation_id !== "string" ||
@@ -1025,11 +1152,22 @@ function validateSuccessResult(result, expectation, includeMetadata) {
1025
1152
  "response media_type",
1026
1153
  NODE_TRANSPORT_FIELD_LIMITS.media_type_utf8_bytes,
1027
1154
  );
1028
- const dataBytes = assertUtf8Field(
1029
- result.data,
1030
- "response data",
1031
- NODE_TRANSPORT_FIELD_LIMITS.data_utf8_bytes,
1032
- );
1155
+ // Own parsed data already passed the response scanner's decoded-string bound.
1156
+ // Metadata and unscanned inherited values still need the exact field check.
1157
+ let dataBytes;
1158
+ if (
1159
+ includeMetadata ||
1160
+ !hasOwnResult ||
1161
+ !Object.hasOwn(result, "data") ||
1162
+ NODE_TRANSPORT_FIELD_LIMITS.data_utf8_bytes <
1163
+ NODE_TRANSPORT_LIMITS.response.max_string_utf8_bytes
1164
+ ) {
1165
+ dataBytes = assertUtf8Field(
1166
+ result.data,
1167
+ "response data",
1168
+ NODE_TRANSPORT_FIELD_LIMITS.data_utf8_bytes,
1169
+ );
1170
+ }
1033
1171
  if (
1034
1172
  !expectation ||
1035
1173
  result.operation_id !== expectation.operation_id ||