@cdk8s/awscdk-resolver 0.0.645 → 0.0.646

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.
Files changed (38) hide show
  1. package/.jsii +3 -3
  2. package/lib/resolve.js +1 -1
  3. package/node_modules/@aws-sdk/client-cloudformation/dist-cjs/index.js +1 -1
  4. package/node_modules/@aws-sdk/client-cloudformation/package.json +3 -3
  5. package/node_modules/@aws-sdk/core/dist-cjs/submodules/protocols/index.js +1164 -1130
  6. package/node_modules/@aws-sdk/core/dist-es/submodules/protocols/json/AwsJsonRpcProtocol.js +2 -2
  7. package/node_modules/@aws-sdk/core/dist-es/submodules/protocols/json/AwsRestJsonProtocol.js +3 -3
  8. package/node_modules/@aws-sdk/core/dist-es/submodules/protocols/json/codec-v1/JsonShapeSerializer.js +5 -2
  9. package/node_modules/@aws-sdk/core/dist-es/submodules/protocols/json/codec-v2/JsonShapeSerializer2.js +42 -12
  10. package/node_modules/@aws-sdk/core/dist-types/submodules/protocols/json/AwsJson1_0Protocol.d.ts +2 -1
  11. package/node_modules/@aws-sdk/core/dist-types/submodules/protocols/json/AwsJson1_1Protocol.d.ts +2 -1
  12. package/node_modules/@aws-sdk/core/dist-types/submodules/protocols/json/AwsJsonRpcProtocol.d.ts +4 -3
  13. package/node_modules/@aws-sdk/core/dist-types/submodules/protocols/json/AwsRestJsonProtocol.d.ts +5 -3
  14. package/node_modules/@aws-sdk/core/dist-types/ts3.4/submodules/protocols/json/AwsJson1_0Protocol.d.ts +2 -1
  15. package/node_modules/@aws-sdk/core/dist-types/ts3.4/submodules/protocols/json/AwsJson1_1Protocol.d.ts +2 -1
  16. package/node_modules/@aws-sdk/core/dist-types/ts3.4/submodules/protocols/json/AwsJsonRpcProtocol.d.ts +3 -2
  17. package/node_modules/@aws-sdk/core/dist-types/ts3.4/submodules/protocols/json/AwsRestJsonProtocol.d.ts +4 -1
  18. package/node_modules/@aws-sdk/core/package.json +1 -1
  19. package/node_modules/@aws-sdk/credential-provider-env/package.json +2 -2
  20. package/node_modules/@aws-sdk/credential-provider-http/package.json +2 -2
  21. package/node_modules/@aws-sdk/credential-provider-ini/package.json +9 -9
  22. package/node_modules/@aws-sdk/credential-provider-login/dist-cjs/index.js +35 -28
  23. package/node_modules/@aws-sdk/credential-provider-login/dist-es/LoginCredentialsFetcher.js +35 -28
  24. package/node_modules/@aws-sdk/credential-provider-login/dist-types/LoginCredentialsFetcher.d.ts +1 -0
  25. package/node_modules/@aws-sdk/credential-provider-login/dist-types/ts3.4/LoginCredentialsFetcher.d.ts +1 -0
  26. package/node_modules/@aws-sdk/credential-provider-login/package.json +3 -3
  27. package/node_modules/@aws-sdk/credential-provider-node/package.json +7 -7
  28. package/node_modules/@aws-sdk/credential-provider-process/package.json +2 -2
  29. package/node_modules/@aws-sdk/credential-provider-sso/package.json +4 -4
  30. package/node_modules/@aws-sdk/credential-provider-web-identity/package.json +3 -3
  31. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/cognito-identity/index.js +1 -1
  32. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/signin/index.js +1 -1
  33. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sso/index.js +1 -1
  34. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sso-oidc/index.js +1 -1
  35. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sts/index.js +1 -1
  36. package/node_modules/@aws-sdk/nested-clients/package.json +2 -2
  37. package/node_modules/@aws-sdk/token-providers/package.json +3 -3
  38. package/package.json +3 -3
@@ -2,7 +2,7 @@ const { SmithyRpcV2CborProtocol, loadSmithyRpcV2CborErrorCode } = require("@smit
2
2
  const { TypeRegistry, NormalizedSchema, deref } = require("@smithy/core/schema");
3
3
  const { decorateServiceException, getValueFromTextNode } = require("@smithy/core/client");
4
4
  const { collectBody, determineTimestampFormat, RpcProtocol, HttpBindingProtocol, HttpInterceptingShapeSerializer, HttpInterceptingShapeDeserializer, FromStringShapeDeserializer, extendedEncodeURIComponent } = require("@smithy/core/protocols");
5
- const { NumericValue, toUtf8, fromBase64, LazyJsonString, parseEpochTimestamp, parseRfc7231DateTime, parseRfc3339DateTimeWithOffset, toBase64, dateToUtcString, generateIdempotencyToken, expectUnion } = require("@smithy/core/serde");
5
+ const { NumericValue, toUtf8, fromBase64, LazyJsonString, parseEpochTimestamp, parseRfc7231DateTime, parseRfc3339DateTimeWithOffset, generateIdempotencyToken, toBase64, dateToUtcString, expectUnion } = require("@smithy/core/serde");
6
6
  const { parseXML, XmlNode, XmlText } = require("@aws-sdk/xml-builder");
7
7
 
8
8
  class ProtocolLib {
@@ -227,6 +227,25 @@ class UnionSerde {
227
227
  }
228
228
  }
229
229
 
230
+ let canParseBuffer;
231
+ function detectBufferParsing() {
232
+ if (canParseBuffer === undefined) {
233
+ try {
234
+ if (typeof Buffer !== "function") {
235
+ canParseBuffer = false;
236
+ }
237
+ else {
238
+ const result = JSON.parse(Buffer.from([0x7b, 0x7d]));
239
+ canParseBuffer = result !== null && typeof result === "object";
240
+ }
241
+ }
242
+ catch {
243
+ canParseBuffer = false;
244
+ }
245
+ }
246
+ return canParseBuffer;
247
+ }
248
+
230
249
  function jsonReviver(key, value, context) {
231
250
  if (context?.source) {
232
251
  const numericString = context.source;
@@ -307,25 +326,6 @@ function _check(ns, seen) {
307
326
 
308
327
  const collectBodyString = (streamBody, context) => collectBody(streamBody, context).then((body) => (context?.utf8Encoder ?? toUtf8)(body));
309
328
 
310
- let canParseBuffer;
311
- function detectBufferParsing() {
312
- if (canParseBuffer === undefined) {
313
- try {
314
- if (typeof Buffer !== "function") {
315
- canParseBuffer = false;
316
- }
317
- else {
318
- const result = JSON.parse(Buffer.from([0x7b, 0x7d]));
319
- canParseBuffer = result !== null && typeof result === "object";
320
- }
321
- }
322
- catch {
323
- canParseBuffer = false;
324
- }
325
- }
326
- return canParseBuffer;
327
- }
328
-
329
329
  async function parseJsonBody(streamBody, context, schema) {
330
330
  let parsingInput;
331
331
  if (detectBufferParsing() && typeof streamBody?.[Symbol.asyncIterator] === "function") {
@@ -415,7 +415,7 @@ function writeKey(obj) {
415
415
  Object.defineProperty(obj, "__proto__", { value: undefined, writable: true, enumerable: true, configurable: true });
416
416
  }
417
417
 
418
- class JsonShapeDeserializer extends SerdeContextConfig {
418
+ class JsonShapeDeserializer2 extends SerdeContextConfig {
419
419
  settings;
420
420
  constructor(settings) {
421
421
  super();
@@ -423,7 +423,24 @@ class JsonShapeDeserializer extends SerdeContextConfig {
423
423
  }
424
424
  async read(schema, data) {
425
425
  const reviver = needsReviver(schema) ? jsonReviver : undefined;
426
- return this._read(schema, typeof data === "string" ? JSON.parse(data, reviver) : await parseJsonBody(data, this.serdeContext, schema));
426
+ let parsed;
427
+ if (typeof data === "string") {
428
+ if (data.length === 0) {
429
+ return {};
430
+ }
431
+ parsed = JSON.parse(data, reviver);
432
+ }
433
+ else if (data instanceof Uint8Array && detectBufferParsing()) {
434
+ if (data.byteLength === 0) {
435
+ return {};
436
+ }
437
+ const buf = Buffer.isBuffer(data) ? data : Buffer.from(data.buffer, data.byteOffset, data.byteLength);
438
+ parsed = JSON.parse(buf, reviver);
439
+ }
440
+ else {
441
+ parsed = await parseJsonBody(data, this.serdeContext, schema);
442
+ }
443
+ return this._read(schema, parsed);
427
444
  }
428
445
  readObject(schema, data) {
429
446
  return this._read(schema, data);
@@ -433,63 +450,29 @@ class JsonShapeDeserializer extends SerdeContextConfig {
433
450
  const ns = NormalizedSchema.of(schema);
434
451
  if (isObject) {
435
452
  if (ns.isStructSchema()) {
436
- const record = value;
437
- const union = ns.isUnionSchema();
438
- const out = {};
439
- let nameMap = void 0;
440
- const { jsonName } = this.settings;
441
- if (jsonName) {
442
- nameMap = {};
443
- }
444
- let unionSerde;
445
- if (union) {
446
- unionSerde = new UnionSerde(record, out);
447
- }
448
- for (const [memberName, memberSchema] of ns.structIterator()) {
449
- let fromKey = memberName;
450
- if (jsonName) {
451
- fromKey = memberSchema.getMergedTraits().jsonName ?? fromKey;
452
- nameMap[fromKey] = memberName;
453
- }
454
- if (union) {
455
- unionSerde.mark(fromKey);
456
- }
457
- if (record[fromKey] != null) {
458
- out[memberName] = this._read(memberSchema, record[fromKey]);
459
- }
460
- }
461
- if (union) {
462
- unionSerde.writeUnknown();
463
- }
464
- else if (typeof record.__type === "string") {
465
- for (const k in record) {
466
- const v = record[k];
467
- const t = jsonName ? (nameMap[k] ?? k) : k;
468
- if (!(t in out)) {
469
- out[t] = v;
470
- }
471
- }
472
- }
473
- return out;
453
+ return this._readStruct(ns, value);
474
454
  }
475
455
  if (Array.isArray(value) && ns.isListSchema()) {
476
456
  const listMember = ns.getValueSchema();
477
- const out = [];
478
- for (const item of value) {
479
- out.push(this._read(listMember, item));
457
+ if (this.needsTransform(listMember)) {
458
+ for (let i = 0; i < value.length; ++i) {
459
+ value[i] = this._read(listMember, value[i]);
460
+ }
480
461
  }
481
- return out;
462
+ return value;
482
463
  }
483
464
  if (ns.isMapSchema()) {
484
465
  const mapMember = ns.getValueSchema();
485
- const out = {};
486
- for (const _k in value) {
487
- if (_k === "__proto__") {
488
- writeKey(out);
466
+ const map = value;
467
+ if (this.needsTransform(mapMember)) {
468
+ for (const k in map) {
469
+ if (k === "__proto__") {
470
+ writeKey(map);
471
+ }
472
+ map[k] = this._read(mapMember, map[k]);
489
473
  }
490
- out[_k] = this._read(mapMember, value[_k]);
491
474
  }
492
- return out;
475
+ return map;
493
476
  }
494
477
  }
495
478
  if (ns.isBlobSchema() && typeof value === "string") {
@@ -543,1266 +526,1317 @@ class JsonShapeDeserializer extends SerdeContextConfig {
543
526
  }
544
527
  if (ns.isDocumentSchema()) {
545
528
  if (isObject) {
546
- const out = Array.isArray(value) ? [] : {};
547
- for (const k in value) {
548
- if (k === "__proto__") {
549
- writeKey(out);
550
- }
551
- const v = value[k];
552
- if (v instanceof NumericValue) {
553
- out[k] = v;
529
+ if (Array.isArray(value)) {
530
+ for (let i = 0; i < value.length; ++i) {
531
+ const v = value[i];
532
+ if (!(v instanceof NumericValue)) {
533
+ value[i] = this._read(ns, v);
534
+ }
554
535
  }
555
- else {
556
- out[k] = this._read(ns, v);
536
+ }
537
+ else {
538
+ const doc = value;
539
+ for (const k in doc) {
540
+ if (k === "__proto__") {
541
+ writeKey(doc);
542
+ }
543
+ const v = doc[k];
544
+ if (!(v instanceof NumericValue)) {
545
+ doc[k] = this._read(ns, v);
546
+ }
557
547
  }
558
548
  }
559
- return out;
560
- }
561
- else {
562
- return structuredClone(value);
563
549
  }
564
550
  }
565
551
  return value;
566
552
  }
567
- }
568
-
569
- const NUMERIC_CONTROL_CHAR = String.fromCharCode(925);
570
- class JsonReplacer {
571
- values = new Map();
572
- counter = 0;
573
- stage = 0;
574
- createReplacer() {
575
- if (this.stage === 1) {
576
- throw new Error("@aws-sdk/core/protocols - JsonReplacer already created.");
553
+ _readStruct(ns, record) {
554
+ const union = ns.isUnionSchema();
555
+ const out = {};
556
+ let nameMap;
557
+ const hasType = typeof record.__type === "string";
558
+ const { jsonName } = this.settings;
559
+ if (jsonName && hasType) {
560
+ nameMap = {};
577
561
  }
578
- if (this.stage === 2) {
579
- throw new Error("@aws-sdk/core/protocols - JsonReplacer exhausted.");
562
+ let unionSerde;
563
+ if (union) {
564
+ unionSerde = new UnionSerde(record, out);
580
565
  }
581
- this.stage = 1;
582
- return (key, value) => {
583
- if (value instanceof NumericValue) {
584
- const v = `${NUMERIC_CONTROL_CHAR + "nv" + this.counter++}_` + value.string;
585
- this.values.set(`"${v}"`, value.string);
586
- return v;
566
+ for (const [memberName, memberSchema] of ns.structIterator()) {
567
+ let fromKey = memberName;
568
+ if (jsonName) {
569
+ fromKey = memberSchema.getMergedTraits().jsonName ?? fromKey;
570
+ if (hasType) {
571
+ nameMap[fromKey] = memberName;
572
+ }
587
573
  }
588
- if (typeof value === "bigint") {
589
- const s = value.toString();
590
- const v = `${NUMERIC_CONTROL_CHAR + "b" + this.counter++}_` + s;
591
- this.values.set(`"${v}"`, s);
592
- return v;
574
+ if (union) {
575
+ unionSerde.mark(fromKey);
576
+ }
577
+ if (record[fromKey] != null) {
578
+ out[memberName] = this._read(memberSchema, record[fromKey]);
593
579
  }
594
- return value;
595
- };
596
- }
597
- replaceInJson(json) {
598
- if (this.stage === 0) {
599
- throw new Error("@aws-sdk/core/protocols - JsonReplacer not created yet.");
600
580
  }
601
- if (this.stage === 2) {
602
- throw new Error("@aws-sdk/core/protocols - JsonReplacer exhausted.");
581
+ if (union) {
582
+ unionSerde.writeUnknown();
603
583
  }
604
- this.stage = 2;
605
- if (this.counter === 0) {
606
- return json;
584
+ else if (hasType) {
585
+ for (const k in record) {
586
+ const v = record[k];
587
+ const t = jsonName ? (nameMap[k] ?? k) : k;
588
+ if (!(t in out)) {
589
+ out[t] = v;
590
+ }
591
+ }
607
592
  }
608
- for (const [key, value] of this.values) {
609
- json = json.replace(key, value);
593
+ return out;
594
+ }
595
+ needsTransform(ns) {
596
+ if (ns.isBlobSchema() || ns.isTimestampSchema() || ns.isBigIntegerSchema() || ns.isBigDecimalSchema()) {
597
+ return true;
610
598
  }
611
- return json;
599
+ if (ns.isDocumentSchema() || ns.isStructSchema() || ns.isListSchema() || ns.isMapSchema()) {
600
+ return true;
601
+ }
602
+ if (ns.isStringSchema() && ns.getMergedTraits().mediaType) {
603
+ return true;
604
+ }
605
+ return false;
612
606
  }
613
607
  }
614
608
 
615
- class JsonShapeSerializer extends SerdeContextConfig {
616
- settings;
617
- buffer;
618
- useReplacer = false;
619
- rootSchema;
620
- constructor(settings) {
621
- super();
622
- this.settings = settings;
609
+ class JsonBytesStringAdapter extends Uint8Array {
610
+ string = null;
611
+ static allocUnsafe(bytes) {
612
+ if (typeof Buffer === "function") {
613
+ const buffer = Buffer.allocUnsafe(bytes);
614
+ return new JsonBytesStringAdapter(buffer.buffer, buffer.byteOffset, buffer.byteLength);
615
+ }
616
+ return new JsonBytesStringAdapter(bytes);
623
617
  }
624
- write(schema, value) {
625
- this.rootSchema = NormalizedSchema.of(schema);
626
- this.buffer = this._write(this.rootSchema, value);
618
+ toString() {
619
+ return this.s();
627
620
  }
628
- flush() {
629
- const { rootSchema, useReplacer } = this;
630
- this.rootSchema = undefined;
631
- this.useReplacer = false;
632
- if (rootSchema?.isStructSchema() || rootSchema?.isDocumentSchema()) {
633
- if (!useReplacer) {
634
- return JSON.stringify(this.buffer);
635
- }
636
- const replacer = new JsonReplacer();
637
- return replacer.replaceInJson(JSON.stringify(this.buffer, replacer.createReplacer(), 0));
638
- }
639
- return this.buffer;
621
+ valueOf() {
622
+ return this.s();
640
623
  }
641
- writeDiscriminatedDocument(schema, value) {
642
- this.write(schema, value);
643
- if (typeof this.buffer === "object") {
644
- this.buffer.__type = NormalizedSchema.of(schema).getName(true);
624
+ includes(searchString, position) {
625
+ if (typeof searchString === "string") {
626
+ return this.s().includes(searchString, position);
645
627
  }
628
+ return Uint8Array.prototype.includes.call(this, searchString, position);
646
629
  }
647
- _write(schema, value, container) {
648
- const isObject = value !== null && typeof value === "object";
649
- const ns = NormalizedSchema.of(schema);
650
- if (isObject) {
651
- if (ns.isStructSchema()) {
652
- const record = value;
653
- const out = {};
654
- const { jsonName } = this.settings;
655
- let nameMap = void 0;
656
- if (jsonName) {
657
- nameMap = {};
658
- }
659
- let outCount = 0;
660
- for (const [memberName, memberSchema] of ns.structIterator()) {
661
- const serializableValue = this._write(memberSchema, record[memberName], ns);
662
- if (serializableValue !== undefined) {
663
- let targetKey = memberName;
664
- if (jsonName) {
665
- targetKey = memberSchema.getMergedTraits().jsonName ?? memberName;
666
- nameMap[memberName] = targetKey;
667
- }
668
- out[targetKey] = serializableValue;
669
- outCount++;
670
- }
671
- }
672
- if (ns.isUnionSchema() && outCount === 0) {
673
- const { $unknown } = record;
674
- if (Array.isArray($unknown)) {
675
- const [k, v] = $unknown;
676
- if (k === "__proto__") {
677
- writeKey(out);
678
- }
679
- out[k] = this._write(15, v);
680
- }
681
- }
682
- else if (typeof record.__type === "string") {
683
- for (const k in record) {
684
- const v = record[k];
685
- const targetKey = jsonName ? (nameMap[k] ?? k) : k;
686
- if (!(targetKey in out)) {
687
- out[targetKey] = this._write(15, v);
688
- }
689
- }
690
- }
691
- return out;
692
- }
693
- if (Array.isArray(value) && ns.isListSchema()) {
694
- const listMember = ns.getValueSchema();
695
- const out = [];
696
- const sparse = !!ns.getMergedTraits().sparse;
697
- for (const item of value) {
698
- if (sparse || item != null) {
699
- out.push(this._write(listMember, item));
700
- }
701
- }
702
- return out;
703
- }
704
- if (ns.isMapSchema()) {
705
- const mapMember = ns.getValueSchema();
706
- const out = {};
707
- const sparse = !!ns.getMergedTraits().sparse;
708
- for (const _k in value) {
709
- const _v = value[_k];
710
- if (sparse || _v != null) {
711
- if (_k === "__proto__") {
712
- writeKey(out);
713
- }
714
- out[_k] = this._write(mapMember, _v);
715
- }
716
- }
717
- return out;
718
- }
719
- if (value instanceof Uint8Array && (ns.isBlobSchema() || ns.isDocumentSchema())) {
720
- if (ns === this.rootSchema) {
721
- return value;
722
- }
723
- return (this.serdeContext?.base64Encoder ?? toBase64)(value);
724
- }
725
- if (value instanceof Date && (ns.isTimestampSchema() || ns.isDocumentSchema())) {
726
- const format = determineTimestampFormat(ns, this.settings);
727
- switch (format) {
728
- case 5:
729
- return value.toISOString().replace(".000Z", "Z");
730
- case 6:
731
- return dateToUtcString(value);
732
- case 7:
733
- return value.getTime() / 1000;
734
- default:
735
- console.warn("Missing timestamp format, using epoch seconds", value);
736
- return value.getTime() / 1000;
737
- }
738
- }
739
- if (value instanceof NumericValue) {
740
- this.useReplacer = true;
741
- }
742
- }
743
- if (value === null && container?.isStructSchema()) {
744
- return void 0;
745
- }
746
- if (ns.isStringSchema()) {
747
- if (typeof value === "undefined" && ns.isIdempotencyToken()) {
748
- return generateIdempotencyToken();
749
- }
750
- const mediaType = ns.getMergedTraits().mediaType;
751
- if (value != null && mediaType) {
752
- const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
753
- if (isJson) {
754
- return LazyJsonString.from(value);
755
- }
756
- }
757
- return value;
758
- }
759
- if (typeof value === "number" && ns.isNumericSchema()) {
760
- if (Math.abs(value) === Infinity || isNaN(value)) {
761
- return String(value);
762
- }
763
- return value;
764
- }
765
- if (typeof value === "string" && ns.isBlobSchema()) {
766
- if (ns === this.rootSchema) {
767
- return value;
768
- }
769
- return (this.serdeContext?.base64Encoder ?? toBase64)(value);
630
+ indexOf(searchString, position) {
631
+ if (typeof searchString === "string") {
632
+ return this.s().indexOf(searchString, position);
770
633
  }
771
- if (typeof value === "bigint") {
772
- this.useReplacer = true;
634
+ return Uint8Array.prototype.indexOf.call(this, searchString, position);
635
+ }
636
+ lastIndexOf(searchString, position) {
637
+ if (typeof searchString === "string") {
638
+ return this.s().lastIndexOf(searchString, position);
773
639
  }
774
- if (ns.isDocumentSchema()) {
775
- if (isObject) {
776
- const out = Array.isArray(value) ? [] : {};
777
- for (const k in value) {
778
- const v = value[k];
779
- if (k === "__proto__") {
780
- writeKey(out);
781
- }
782
- if (v instanceof NumericValue) {
783
- this.useReplacer = true;
784
- out[k] = v;
785
- }
786
- else {
787
- out[k] = this._write(ns, v);
788
- }
789
- }
790
- return out;
791
- }
792
- else {
793
- return structuredClone(value);
794
- }
640
+ const fn = Uint8Array.prototype.lastIndexOf;
641
+ if (position !== undefined) {
642
+ return fn.call(this, searchString, position);
795
643
  }
796
- return value;
644
+ return fn.call(this, searchString);
797
645
  }
798
- }
799
-
800
- class JsonCodec extends SerdeContextConfig {
801
- settings;
802
- constructor(settings) {
803
- super();
804
- this.settings = settings;
646
+ startsWith(searchString, position) {
647
+ return this.s().startsWith(searchString, position);
805
648
  }
806
- createSerializer() {
807
- const serializer = new JsonShapeSerializer(this.settings);
808
- serializer.setSerdeContext(this.serdeContext);
809
- return serializer;
649
+ endsWith(searchString, endPosition) {
650
+ return this.s().endsWith(searchString, endPosition);
810
651
  }
811
- createDeserializer() {
812
- const deserializer = new JsonShapeDeserializer(this.settings);
813
- deserializer.setSerdeContext(this.serdeContext);
814
- return deserializer;
652
+ match(regexp) {
653
+ return this.s().match(regexp);
815
654
  }
816
- }
817
-
818
- class AwsJsonRpcProtocol extends RpcProtocol {
819
- serializer;
820
- deserializer;
821
- serviceTarget;
822
- codec;
823
- mixin;
824
- awsQueryCompatible;
825
- constructor({ defaultNamespace, errorTypeRegistries, serviceTarget, awsQueryCompatible, jsonCodec, }) {
826
- super({
827
- defaultNamespace,
828
- errorTypeRegistries,
829
- });
830
- this.serviceTarget = serviceTarget;
831
- this.codec =
832
- jsonCodec ??
833
- new JsonCodec({
834
- timestampFormat: {
835
- useTrait: true,
836
- default: 7,
837
- },
838
- jsonName: false,
839
- });
840
- this.serializer = this.codec.createSerializer();
841
- this.deserializer = this.codec.createDeserializer();
842
- this.awsQueryCompatible = !!awsQueryCompatible;
843
- this.mixin = new ProtocolLib(this.awsQueryCompatible);
655
+ replace(searchValue, replaceValue) {
656
+ return this.s().replace(searchValue, replaceValue);
844
657
  }
845
- async serializeRequest(operationSchema, input, context) {
846
- const request = await super.serializeRequest(operationSchema, input, context);
847
- if (!request.path.endsWith("/")) {
848
- request.path += "/";
849
- }
850
- request.headers["content-type"] = `application/x-amz-json-${this.getJsonRpcVersion()}`;
851
- request.headers["x-amz-target"] = `${this.serviceTarget}.${operationSchema.name}`;
852
- if (this.awsQueryCompatible) {
853
- request.headers["x-amzn-query-mode"] = "true";
854
- }
855
- if (deref(operationSchema.input) === "unit" || !request.body) {
856
- request.body = "{}";
857
- }
858
- return request;
658
+ search(regexp) {
659
+ return this.s().search(regexp);
859
660
  }
860
- getPayloadCodec() {
861
- return this.codec;
661
+ split(separator, limit) {
662
+ return this.s().split(separator, limit);
862
663
  }
863
- async handleError(operationSchema, context, response, dataObject, metadata) {
864
- const { awsQueryCompatible } = this;
865
- if (awsQueryCompatible) {
866
- this.mixin.setQueryCompatError(dataObject, response);
867
- }
868
- const errorIdentifier = loadJsonRpcErrorCode(response, dataObject, awsQueryCompatible) ?? "Unknown";
869
- this.mixin.compose(this.compositeErrorRegistry, errorIdentifier, this.options.defaultNamespace);
870
- const { errorSchema, errorMetadata } = await this.mixin.getErrorSchemaOrThrowBaseException(errorIdentifier, this.options.defaultNamespace, response, dataObject, metadata, awsQueryCompatible ? this.mixin.findQueryCompatibleError : undefined);
871
- const ns = NormalizedSchema.of(errorSchema);
872
- const message = dataObject.message ?? dataObject.Message ?? "UnknownError";
873
- const ErrorCtor = this.compositeErrorRegistry.getErrorCtor(errorSchema) ?? Error;
874
- const exception = new ErrorCtor({});
875
- const output = {};
876
- const errorDeserializer = this.codec.createDeserializer();
877
- for (const [name, member] of ns.structIterator()) {
878
- if (dataObject[name] != null) {
879
- output[name] = errorDeserializer.readObject(member, dataObject[name]);
880
- }
881
- }
882
- if (awsQueryCompatible) {
883
- this.mixin.queryCompatOutput(dataObject, output);
884
- }
885
- throw this.mixin.decorateServiceException(Object.assign(exception, errorMetadata, {
886
- $fault: ns.getMergedTraits().error,
887
- message,
888
- }, output), dataObject);
664
+ substring(start, end) {
665
+ return this.s().substring(start, end);
889
666
  }
890
- }
891
-
892
- class AwsJson1_0Protocol extends AwsJsonRpcProtocol {
893
- constructor({ defaultNamespace, errorTypeRegistries, serviceTarget, awsQueryCompatible, jsonCodec, }) {
894
- super({
895
- defaultNamespace,
896
- errorTypeRegistries,
897
- serviceTarget,
898
- awsQueryCompatible,
899
- jsonCodec,
900
- });
667
+ trim() {
668
+ return this.s().trim();
901
669
  }
902
- getShapeId() {
903
- return "aws.protocols#awsJson1_0";
670
+ trimStart() {
671
+ return this.s().trimStart();
904
672
  }
905
- getJsonRpcVersion() {
906
- return "1.0";
673
+ trimEnd() {
674
+ return this.s().trimEnd();
907
675
  }
908
- getDefaultContentType() {
909
- return "application/x-amz-json-1.0";
676
+ charAt(pos) {
677
+ return this.s().charAt(pos);
910
678
  }
911
- }
912
-
913
- class AwsJson1_1Protocol extends AwsJsonRpcProtocol {
914
- constructor({ defaultNamespace, errorTypeRegistries, serviceTarget, awsQueryCompatible, jsonCodec, }) {
915
- super({
916
- defaultNamespace,
917
- errorTypeRegistries,
918
- serviceTarget,
919
- awsQueryCompatible,
920
- jsonCodec,
921
- });
679
+ charCodeAt(index) {
680
+ return this.s().charCodeAt(index);
922
681
  }
923
- getShapeId() {
924
- return "aws.protocols#awsJson1_1";
682
+ padStart(maxLength, fillString) {
683
+ return this.s().padStart(maxLength, fillString);
925
684
  }
926
- getJsonRpcVersion() {
927
- return "1.1";
685
+ padEnd(maxLength, fillString) {
686
+ return this.s().padEnd(maxLength, fillString);
928
687
  }
929
- getDefaultContentType() {
930
- return "application/x-amz-json-1.1";
688
+ repeat(count) {
689
+ return this.s().repeat(count);
690
+ }
691
+ toUpperCase() {
692
+ return this.s().toUpperCase();
693
+ }
694
+ toLowerCase() {
695
+ return this.s().toLowerCase();
696
+ }
697
+ s() {
698
+ if (this.string == null) {
699
+ const n = Date.now();
700
+ if (n > warned + 60_000) {
701
+ console.warn("@aws-sdk/core/protocols - WARN - JsonCodec2: you have called a string method on a Uint8Array request body. " +
702
+ "It has been automatically converted to string. In a future version this will throw an error.");
703
+ warned = n;
704
+ }
705
+ this.string = toUtf8(this);
706
+ }
707
+ return this.string;
931
708
  }
932
709
  }
710
+ var warned = 0;
933
711
 
934
- class AwsRestJsonProtocol extends HttpBindingProtocol {
935
- serializer;
936
- deserializer;
937
- codec;
938
- mixin = new ProtocolLib();
939
- constructor({ defaultNamespace, errorTypeRegistries, }) {
940
- super({
941
- defaultNamespace,
942
- errorTypeRegistries,
943
- });
944
- const settings = {
945
- timestampFormat: {
946
- useTrait: true,
947
- default: 7,
948
- },
949
- httpBindings: true,
950
- jsonName: true,
951
- };
952
- this.codec = new JsonCodec(settings);
953
- this.serializer = new HttpInterceptingShapeSerializer(this.codec.createSerializer(), settings);
954
- this.deserializer = new HttpInterceptingShapeDeserializer(this.codec.createDeserializer(), settings);
955
- }
956
- getShapeId() {
957
- return "aws.protocols#restJson1";
712
+ const encoder = new TextEncoder();
713
+ const OPEN_BRACE = 0x7b;
714
+ const CLOSE_BRACE = 0x7d;
715
+ const OPEN_BRACKET = 0x5b;
716
+ const CLOSE_BRACKET = 0x5d;
717
+ const QUOTE = 0x22;
718
+ const COLON = 0x3a;
719
+ const COMMA = 0x2c;
720
+ const BACKSLASH = 0x5c;
721
+ const TRUE = new Uint8Array([0x74, 0x72, 0x75, 0x65]);
722
+ const FALSE = new Uint8Array([0x66, 0x61, 0x6c, 0x73, 0x65]);
723
+ const NULL = new Uint8Array([0x6e, 0x75, 0x6c, 0x6c]);
724
+ const ESCAPE_TABLE = new Array(128).fill(null);
725
+ ESCAPE_TABLE[0x08] = "b";
726
+ ESCAPE_TABLE[0x09] = "t";
727
+ ESCAPE_TABLE[0x0a] = "n";
728
+ ESCAPE_TABLE[0x0c] = "f";
729
+ ESCAPE_TABLE[0x0d] = "r";
730
+ ESCAPE_TABLE[0x22] = '"';
731
+ ESCAPE_TABLE[0x5c] = "\\";
732
+ for (let i = 0; i < 0x20; i++) {
733
+ if (ESCAPE_TABLE[i] === null) {
734
+ ESCAPE_TABLE[i] = "u00" + i.toString(16).padStart(2, "0");
958
735
  }
959
- getPayloadCodec() {
960
- return this.codec;
736
+ }
737
+ const INITIAL_BUFFER_SIZE = 2048;
738
+ function alloc(size) {
739
+ return JsonBytesStringAdapter.allocUnsafe(size);
740
+ }
741
+ class JsonShapeSerializer2 extends SerdeContextConfig {
742
+ settings;
743
+ json;
744
+ i = 0;
745
+ rootSchema;
746
+ rawValue;
747
+ passthrough = false;
748
+ constructor(settings) {
749
+ super();
750
+ this.settings = settings;
751
+ this.json = alloc(INITIAL_BUFFER_SIZE);
961
752
  }
962
- setSerdeContext(serdeContext) {
963
- this.codec.setSerdeContext(serdeContext);
964
- super.setSerdeContext(serdeContext);
753
+ write(schema, value) {
754
+ this.i = 0;
755
+ this.rawValue = value;
756
+ this.rootSchema = NormalizedSchema.of(schema);
757
+ this.passthrough = this.rootSchema.isBlobSchema() || this.rootSchema.isStringSchema();
758
+ if (!this.passthrough) {
759
+ this.writeValue(this.rootSchema, value, undefined);
760
+ }
965
761
  }
966
- async serializeRequest(operationSchema, input, context) {
967
- const request = await super.serializeRequest(operationSchema, input, context);
968
- const inputSchema = NormalizedSchema.of(operationSchema.input);
969
- if (!request.headers["content-type"]) {
970
- const contentType = this.mixin.resolveRestContentType(this.getDefaultContentType(), inputSchema);
971
- if (contentType) {
972
- request.headers["content-type"] = contentType;
973
- }
762
+ writeDiscriminatedDocument(schema, value) {
763
+ this.i = 0;
764
+ this.rootSchema = NormalizedSchema.of(schema);
765
+ const ns = this.rootSchema;
766
+ if (ns.isStructSchema() && value != null && typeof value === "object") {
767
+ this.writeValue(ns, value, undefined);
768
+ const prefix = `"__type":"${ns.getName(true) ?? "Unknown"}",`;
769
+ const z = prefix.length;
770
+ this.ensure(z);
771
+ this.json.copyWithin(1 + z, 1, this.i);
772
+ encoder.encodeInto(prefix, this.json.subarray(1));
773
+ this.i += z;
974
774
  }
975
- if (request.body == null && request.headers["content-type"] === this.getDefaultContentType()) {
976
- request.body = "{}";
775
+ else {
776
+ this.writeValue(ns, value, undefined);
977
777
  }
978
- return request;
979
778
  }
980
- async deserializeResponse(operationSchema, context, response) {
981
- const output = await super.deserializeResponse(operationSchema, context, response);
982
- const outputSchema = NormalizedSchema.of(operationSchema.output);
983
- for (const [name, member] of outputSchema.structIterator()) {
984
- if (member.getMemberTraits().httpPayload && !(name in output)) {
985
- output[name] = null;
779
+ flush() {
780
+ this.rootSchema = undefined;
781
+ const finalPosition = this.i;
782
+ this.i = 0;
783
+ const raw = this.rawValue;
784
+ this.rawValue = undefined;
785
+ if (finalPosition === 0) {
786
+ return raw;
787
+ }
788
+ const result = this.json.subarray(0, finalPosition);
789
+ this.json = alloc(INITIAL_BUFFER_SIZE);
790
+ return result;
791
+ }
792
+ ensure(byteCount) {
793
+ const { i, json } = this;
794
+ if (i + byteCount > json.length) {
795
+ let newSize = json.length * 2;
796
+ while (newSize < i + byteCount) {
797
+ newSize *= 2;
986
798
  }
799
+ const next = alloc(newSize);
800
+ next.set(this.json);
801
+ this.json = next;
987
802
  }
988
- return output;
989
803
  }
990
- async handleError(operationSchema, context, response, dataObject, metadata) {
991
- const errorIdentifier = loadRestJsonErrorCode(response, dataObject) ?? "Unknown";
992
- this.mixin.compose(this.compositeErrorRegistry, errorIdentifier, this.options.defaultNamespace);
993
- const { errorSchema, errorMetadata } = await this.mixin.getErrorSchemaOrThrowBaseException(errorIdentifier, this.options.defaultNamespace, response, dataObject, metadata);
994
- const ns = NormalizedSchema.of(errorSchema);
995
- const message = dataObject.message ?? dataObject.Message ?? "UnknownError";
996
- const ErrorCtor = this.compositeErrorRegistry.getErrorCtor(errorSchema) ?? Error;
997
- const exception = new ErrorCtor({});
998
- await this.deserializeHttpMessage(errorSchema, context, response, dataObject);
999
- const output = {};
1000
- const errorDeserializer = this.codec.createDeserializer();
1001
- for (const [name, member] of ns.structIterator()) {
1002
- const target = member.getMergedTraits().jsonName ?? name;
1003
- output[name] = errorDeserializer.readObject(member, dataObject[target]);
804
+ writeAscii(s) {
805
+ const z = s.length;
806
+ this.ensure(z);
807
+ let { i, json } = this;
808
+ for (let j = 0; j < z; ++j) {
809
+ json[i] = s.charCodeAt(j);
810
+ i += 1;
1004
811
  }
1005
- throw this.mixin.decorateServiceException(Object.assign(exception, errorMetadata, {
1006
- $fault: ns.getMergedTraits().error,
1007
- message,
1008
- }, output), dataObject);
1009
- }
1010
- getDefaultContentType() {
1011
- return "application/json";
812
+ this.i = i;
1012
813
  }
1013
- }
1014
-
1015
- class JsonShapeDeserializer2 extends SerdeContextConfig {
1016
- settings;
1017
- constructor(settings) {
1018
- super();
1019
- this.settings = settings;
814
+ writeAsciiQuoted(s) {
815
+ const z = s.length;
816
+ this.ensure(z + 4);
817
+ let { json, i } = this;
818
+ json[i++] = QUOTE;
819
+ for (let j = 0; j < z; ++j) {
820
+ json[i++] = s.charCodeAt(j);
821
+ }
822
+ json[i++] = QUOTE;
823
+ this.i = i;
1020
824
  }
1021
- async read(schema, data) {
1022
- const reviver = needsReviver(schema) ? jsonReviver : undefined;
1023
- let parsed;
1024
- if (typeof data === "string") {
1025
- if (data.length === 0) {
1026
- return {};
825
+ writeJsonString(s) {
826
+ this.ensure(s.length * 3 + 2);
827
+ this.json[this.i++] = QUOTE;
828
+ const z = s.length;
829
+ for (let j = 0; j < z; ++j) {
830
+ const c = s.charCodeAt(j);
831
+ if (c > 0x22 && c < 0x5c) {
832
+ this.json[this.i++] = c;
1027
833
  }
1028
- parsed = JSON.parse(data, reviver);
1029
- }
1030
- else if (data instanceof Uint8Array && detectBufferParsing()) {
1031
- if (data.byteLength === 0) {
1032
- return {};
834
+ else if (c < 0x80) {
835
+ const esc = ESCAPE_TABLE[c];
836
+ if (esc !== null) {
837
+ this.ensure(esc.length + 1);
838
+ this.json[this.i++] = BACKSLASH;
839
+ for (let k = 0; k < esc.length; k++) {
840
+ this.json[this.i++] = esc.charCodeAt(k);
841
+ }
842
+ }
843
+ else {
844
+ this.json[this.i++] = c;
845
+ }
846
+ }
847
+ else if (c >= 0xd800 && c <= 0xdbff) {
848
+ const next = j + 1 < z ? s.charCodeAt(j + 1) : 0;
849
+ if (next >= 0xdc00 && next <= 0xdfff) {
850
+ this.ensure(4);
851
+ const { written } = encoder.encodeInto(s.substring(j, j + 2), this.json.subarray(this.i));
852
+ this.i += written;
853
+ ++j;
854
+ }
855
+ else {
856
+ this.ensure(6);
857
+ this.writeUnicodeEscape(c);
858
+ }
859
+ }
860
+ else if (c >= 0xdc00 && c <= 0xdfff) {
861
+ this.ensure(6);
862
+ this.writeUnicodeEscape(c);
863
+ }
864
+ else {
865
+ let { i, json } = this;
866
+ if (c < 0x800) {
867
+ json[i++] = 0xc0 | (c >> 6);
868
+ json[i++] = 0x80 | (c & 0x3f);
869
+ }
870
+ else {
871
+ json[i++] = 0xe0 | (c >> 12);
872
+ json[i++] = 0x80 | ((c >> 6) & 0x3f);
873
+ json[i++] = 0x80 | (c & 0x3f);
874
+ }
875
+ this.i = i;
1033
876
  }
1034
- const buf = Buffer.isBuffer(data) ? data : Buffer.from(data.buffer, data.byteOffset, data.byteLength);
1035
- parsed = JSON.parse(buf, reviver);
1036
877
  }
1037
- else {
1038
- parsed = await parseJsonBody(data, this.serdeContext, schema);
878
+ this.json[this.i++] = QUOTE;
879
+ }
880
+ writeUnicodeEscape(code) {
881
+ let { json, i } = this;
882
+ json[i++] = BACKSLASH;
883
+ json[i++] = 0x75;
884
+ const hex = code.toString(16).padStart(4, "0");
885
+ for (let j = 0; j < 4; ++j) {
886
+ json[i++] = hex.charCodeAt(j);
1039
887
  }
1040
- return this._read(schema, parsed);
888
+ this.i = i;
1041
889
  }
1042
- readObject(schema, data) {
1043
- return this._read(schema, data);
890
+ static B64 = (() => {
891
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
892
+ const table = new Uint8Array(64);
893
+ for (let i = 0; i < 64; ++i) {
894
+ table[i] = chars.charCodeAt(i);
895
+ }
896
+ return table;
897
+ })();
898
+ writeBase64(data) {
899
+ const b64Len = Math.ceil(data.length / 3) * 4;
900
+ this.ensure(b64Len + 2);
901
+ const json = this.json;
902
+ const B64 = JsonShapeSerializer2.B64;
903
+ let i = this.i;
904
+ json[i++] = QUOTE;
905
+ const len = data.length;
906
+ const remainder = len % 3;
907
+ const mainLen = len - remainder;
908
+ for (let j = 0; j < mainLen; j += 3) {
909
+ const a = data[j];
910
+ const b = data[j + 1];
911
+ const c = data[j + 2];
912
+ json[i++] = B64[a >> 2];
913
+ json[i++] = B64[((a & 0x03) << 4) | (b >> 4)];
914
+ json[i++] = B64[((b & 0x0f) << 2) | (c >> 6)];
915
+ json[i++] = B64[c & 0x3f];
916
+ }
917
+ if (remainder === 2) {
918
+ const a = data[mainLen];
919
+ const b = data[mainLen + 1];
920
+ json[i++] = B64[a >> 2];
921
+ json[i++] = B64[((a & 0x03) << 4) | (b >> 4)];
922
+ json[i++] = B64[(b & 0x0f) << 2];
923
+ json[i++] = 0x3d;
924
+ }
925
+ else if (remainder === 1) {
926
+ const a = data[mainLen];
927
+ json[i++] = B64[a >> 2];
928
+ json[i++] = B64[(a & 0x03) << 4];
929
+ json[i++] = 0x3d;
930
+ json[i++] = 0x3d;
931
+ }
932
+ json[i++] = QUOTE;
933
+ this.i = i;
1044
934
  }
1045
- _read(schema, value) {
1046
- const isObject = value !== null && typeof value === "object";
935
+ writeValue(schema, value, container) {
936
+ if (value == null) {
937
+ if (container?.isStructSchema()) {
938
+ if (value === undefined) {
939
+ const ns = NormalizedSchema.of(schema);
940
+ if (ns.isIdempotencyToken()) {
941
+ this.writeAsciiQuoted(generateIdempotencyToken());
942
+ return;
943
+ }
944
+ }
945
+ return;
946
+ }
947
+ this.ensure(4);
948
+ this.json.set(NULL, this.i);
949
+ this.i += 4;
950
+ return;
951
+ }
1047
952
  const ns = NormalizedSchema.of(schema);
953
+ const isObject = typeof value === "object";
954
+ if (ns.isStringSchema()) {
955
+ const mediaType = ns.getMergedTraits().mediaType;
956
+ if (mediaType) {
957
+ const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
958
+ if (isJson) {
959
+ this.writeJsonString(LazyJsonString.from(value).toString());
960
+ return;
961
+ }
962
+ }
963
+ }
1048
964
  if (isObject) {
1049
965
  if (ns.isStructSchema()) {
1050
- return this._readStruct(ns, value);
966
+ this.writeStruct(ns, value);
967
+ return;
1051
968
  }
1052
- if (Array.isArray(value) && ns.isListSchema()) {
1053
- const listMember = ns.getValueSchema();
1054
- if (this.needsTransform(listMember)) {
1055
- for (let i = 0; i < value.length; ++i) {
1056
- value[i] = this._read(listMember, value[i]);
1057
- }
1058
- }
1059
- return value;
969
+ if (Array.isArray(value) && (ns.isListSchema() || ns.isDocumentSchema())) {
970
+ this.writeList(ns, value, ns.isDocumentSchema());
971
+ return;
1060
972
  }
1061
973
  if (ns.isMapSchema()) {
1062
- const mapMember = ns.getValueSchema();
1063
- const map = value;
1064
- if (this.needsTransform(mapMember)) {
1065
- for (const k in map) {
1066
- if (k === "__proto__") {
1067
- writeKey(map);
1068
- }
1069
- map[k] = this._read(mapMember, map[k]);
1070
- }
974
+ this.writeMap(ns, value, false);
975
+ return;
976
+ }
977
+ if (value instanceof Uint8Array && (ns.isBlobSchema() || ns.isDocumentSchema())) {
978
+ this.writeBase64(value);
979
+ return;
980
+ }
981
+ if (value instanceof Date && (ns.isTimestampSchema() || ns.isDocumentSchema())) {
982
+ this.writeTimestamp(ns, value);
983
+ return;
984
+ }
985
+ if (value instanceof NumericValue) {
986
+ this.writeAscii(value.string);
987
+ return;
988
+ }
989
+ if (ns.isDocumentSchema()) {
990
+ if (Array.isArray(value)) {
991
+ this.writeList(ns, value, true);
1071
992
  }
1072
- return map;
993
+ else {
994
+ this.writeMap(ns, value, true);
995
+ }
996
+ return;
1073
997
  }
998
+ const json = JSON.stringify(value);
999
+ this.writeAscii(json);
1000
+ return;
1074
1001
  }
1075
- if (ns.isBlobSchema() && typeof value === "string") {
1076
- return fromBase64(value);
1002
+ if (typeof value === "string") {
1003
+ if (ns.isBlobSchema()) {
1004
+ const b64 = (this.serdeContext?.base64Encoder ?? toBase64)(value);
1005
+ this.writeAsciiQuoted(b64);
1006
+ return;
1007
+ }
1008
+ this.writeJsonString(value);
1009
+ return;
1077
1010
  }
1078
- const mediaType = ns.getMergedTraits().mediaType;
1079
- if (ns.isStringSchema() && typeof value === "string" && mediaType) {
1080
- const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
1081
- if (isJson) {
1082
- return LazyJsonString.from(value);
1011
+ if (typeof value === "number") {
1012
+ if (Math.abs(value) === Infinity || Number.isNaN(value)) {
1013
+ this.writeAsciiQuoted(String(value));
1014
+ return;
1083
1015
  }
1084
- return value;
1016
+ const numStr = String(value);
1017
+ this.writeAscii(numStr);
1018
+ return;
1085
1019
  }
1086
- if (ns.isTimestampSchema() && value != null) {
1087
- const format = determineTimestampFormat(ns, this.settings);
1088
- switch (format) {
1089
- case 5:
1090
- return parseRfc3339DateTimeWithOffset(value);
1091
- case 6:
1092
- return parseRfc7231DateTime(value);
1093
- case 7:
1094
- return parseEpochTimestamp(value);
1095
- default:
1096
- console.warn("Missing timestamp format, parsing value with Date constructor:", value);
1097
- return new Date(value);
1020
+ if (typeof value === "boolean") {
1021
+ this.ensure(5);
1022
+ let { i, json } = this;
1023
+ if (value) {
1024
+ json.set(TRUE, i);
1025
+ i += 4;
1026
+ }
1027
+ else {
1028
+ json.set(FALSE, i);
1029
+ i += 5;
1098
1030
  }
1031
+ this.i = i;
1032
+ return;
1099
1033
  }
1100
- if (ns.isBigIntegerSchema() && (typeof value === "number" || typeof value === "string")) {
1101
- return BigInt(value);
1034
+ if (typeof value === "bigint") {
1035
+ this.writeAscii(value.toString());
1036
+ return;
1102
1037
  }
1103
- if (ns.isBigDecimalSchema() && value != undefined) {
1104
- if (value instanceof NumericValue) {
1105
- return value;
1038
+ this.writeAscii(String(value));
1039
+ }
1040
+ writeStruct(ns, value) {
1041
+ this.ensure(2);
1042
+ this.json[this.i++] = OPEN_BRACE;
1043
+ let wroteAny = false;
1044
+ const hasType = typeof value.__type === "string";
1045
+ let writtenKeys;
1046
+ if (hasType) {
1047
+ writtenKeys = new Set();
1048
+ }
1049
+ for (const [memberName, memberSchema] of ns.structIterator()) {
1050
+ const item = value[memberName];
1051
+ if (item == null && !memberSchema.isIdempotencyToken()) {
1052
+ continue;
1106
1053
  }
1107
- const untyped = value;
1108
- if (untyped.type === "bigDecimal" && "string" in untyped) {
1109
- return new NumericValue(untyped.string, untyped.type);
1054
+ if (wroteAny) {
1055
+ this.ensure(1);
1056
+ this.json[this.i++] = COMMA;
1110
1057
  }
1111
- return new NumericValue(String(value), "bigDecimal");
1058
+ wroteAny = true;
1059
+ const targetKey = this.settings.jsonName ? (memberSchema.getMergedTraits().jsonName ?? memberName) : memberName;
1060
+ if (writtenKeys) {
1061
+ writtenKeys.add(memberName);
1062
+ writtenKeys.add(targetKey);
1063
+ }
1064
+ this.writeAsciiQuoted(targetKey);
1065
+ this.json[this.i++] = COLON;
1066
+ this.writeValue(memberSchema, item, ns);
1112
1067
  }
1113
- if (ns.isNumericSchema() && typeof value === "string") {
1114
- switch (value) {
1115
- case "Infinity":
1116
- return Infinity;
1117
- case "-Infinity":
1118
- return -Infinity;
1119
- case "NaN":
1120
- return NaN;
1068
+ if (!wroteAny && ns.isUnionSchema()) {
1069
+ const { $unknown } = value;
1070
+ if (Array.isArray($unknown)) {
1071
+ const [k, v] = $unknown;
1072
+ this.writeAsciiQuoted(k);
1073
+ this.ensure(1);
1074
+ this.json[this.i++] = COLON;
1075
+ this.writeValue(15, v, ns);
1121
1076
  }
1122
- return value;
1123
1077
  }
1124
- if (ns.isDocumentSchema()) {
1125
- if (isObject) {
1126
- if (Array.isArray(value)) {
1127
- for (let i = 0; i < value.length; ++i) {
1128
- const v = value[i];
1129
- if (!(v instanceof NumericValue)) {
1130
- value[i] = this._read(ns, v);
1131
- }
1078
+ else if (hasType) {
1079
+ for (const k in value) {
1080
+ if (writtenKeys.has(k)) {
1081
+ continue;
1082
+ }
1083
+ writtenKeys.add(k);
1084
+ const v = value[k];
1085
+ if (wroteAny) {
1086
+ this.ensure(1);
1087
+ this.json[this.i++] = COMMA;
1088
+ }
1089
+ wroteAny = true;
1090
+ this.writeAsciiQuoted(k);
1091
+ this.ensure(1);
1092
+ this.json[this.i++] = COLON;
1093
+ this.writeValue(15, v, undefined);
1094
+ }
1095
+ }
1096
+ this.ensure(1);
1097
+ this.json[this.i++] = CLOSE_BRACE;
1098
+ }
1099
+ writeList(ns, value, isDocument) {
1100
+ const sparse = !!ns.getMergedTraits().sparse;
1101
+ const valueSchema = ns.getValueSchema();
1102
+ if (!isDocument) {
1103
+ if (valueSchema.isStringSchema() || valueSchema.isNumericSchema() || valueSchema.isBooleanSchema()) {
1104
+ let hasSpecials = false;
1105
+ for (let i = 0; i < value.length; ++i) {
1106
+ const v = value[i];
1107
+ if (Number.isNaN(v) || v === Infinity || v === -Infinity || (v == null && !sparse)) {
1108
+ hasSpecials = true;
1109
+ break;
1132
1110
  }
1133
1111
  }
1112
+ let json;
1113
+ if (!hasSpecials) {
1114
+ json = JSON.stringify(value);
1115
+ }
1134
1116
  else {
1135
- const doc = value;
1136
- for (const k in doc) {
1137
- if (k === "__proto__") {
1138
- writeKey(doc);
1117
+ const out = [];
1118
+ for (let i = 0; i < value.length; ++i) {
1119
+ const v = value[i];
1120
+ if (v == null && !sparse)
1121
+ continue;
1122
+ if (Number.isNaN(v) || v === Infinity || v === -Infinity) {
1123
+ out.push(String(v));
1139
1124
  }
1140
- const v = doc[k];
1141
- if (!(v instanceof NumericValue)) {
1142
- doc[k] = this._read(ns, v);
1125
+ else {
1126
+ out.push(v);
1143
1127
  }
1144
1128
  }
1129
+ json = JSON.stringify(out);
1145
1130
  }
1131
+ this.ensure(json.length * 3);
1132
+ this.i += encoder.encodeInto(json, this.json.subarray(this.i)).written;
1133
+ return;
1146
1134
  }
1147
1135
  }
1148
- return value;
1149
- }
1150
- _readStruct(ns, record) {
1151
- const union = ns.isUnionSchema();
1152
- const out = {};
1153
- let nameMap;
1154
- const hasType = typeof record.__type === "string";
1155
- const { jsonName } = this.settings;
1156
- if (jsonName && hasType) {
1157
- nameMap = {};
1158
- }
1159
- let unionSerde;
1160
- if (union) {
1161
- unionSerde = new UnionSerde(record, out);
1136
+ this.ensure(2);
1137
+ this.json[this.i++] = OPEN_BRACKET;
1138
+ let wroteFirstItem = false;
1139
+ for (let i = 0; i < value.length; ++i) {
1140
+ const item = value[i];
1141
+ if (isDocument ? item === undefined : item == null && !sparse) {
1142
+ continue;
1143
+ }
1144
+ if (wroteFirstItem) {
1145
+ this.ensure(1);
1146
+ this.json[this.i++] = COMMA;
1147
+ }
1148
+ this.writeValue(valueSchema, item, undefined);
1149
+ wroteFirstItem = true;
1162
1150
  }
1163
- for (const [memberName, memberSchema] of ns.structIterator()) {
1164
- let fromKey = memberName;
1165
- if (jsonName) {
1166
- fromKey = memberSchema.getMergedTraits().jsonName ?? fromKey;
1167
- if (hasType) {
1168
- nameMap[fromKey] = memberName;
1151
+ this.ensure(1);
1152
+ this.json[this.i++] = CLOSE_BRACKET;
1153
+ }
1154
+ writeMap(ns, value, isDocument) {
1155
+ const sparse = !!ns.getMergedTraits().sparse;
1156
+ const valueSchema = ns.getValueSchema();
1157
+ if (!isDocument) {
1158
+ if (valueSchema.isStringSchema() || valueSchema.isNumericSchema() || valueSchema.isBooleanSchema()) {
1159
+ let modifications;
1160
+ for (const k in value) {
1161
+ const v = value[k];
1162
+ if (Number.isNaN(v) || v === Infinity || v === -Infinity) {
1163
+ (modifications ??= {})[k] = v;
1164
+ value[k] = String(v);
1165
+ }
1166
+ else if (v === null && !sparse) {
1167
+ (modifications ??= {})[k] = null;
1168
+ value[k] = undefined;
1169
+ }
1170
+ }
1171
+ const json = JSON.stringify(value);
1172
+ if (modifications) {
1173
+ Object.assign(value, modifications);
1169
1174
  }
1175
+ this.ensure(json.length * 3);
1176
+ this.i += encoder.encodeInto(json, this.json.subarray(this.i)).written;
1177
+ return;
1170
1178
  }
1171
- if (union) {
1172
- unionSerde.mark(fromKey);
1179
+ }
1180
+ this.ensure(2);
1181
+ this.json[this.i++] = OPEN_BRACE;
1182
+ let first = true;
1183
+ for (const k in value) {
1184
+ const v = value[k];
1185
+ if (isDocument ? v === undefined : v == null && !sparse) {
1186
+ continue;
1173
1187
  }
1174
- if (record[fromKey] != null) {
1175
- out[memberName] = this._read(memberSchema, record[fromKey]);
1188
+ if (!first) {
1189
+ this.ensure(1);
1190
+ this.json[this.i++] = COMMA;
1176
1191
  }
1192
+ first = false;
1193
+ this.writeJsonString(k);
1194
+ this.ensure(1);
1195
+ this.json[this.i++] = COLON;
1196
+ this.writeValue(valueSchema, v, undefined);
1177
1197
  }
1178
- if (union) {
1179
- unionSerde.writeUnknown();
1180
- }
1181
- else if (hasType) {
1182
- for (const k in record) {
1183
- const v = record[k];
1184
- const t = jsonName ? (nameMap[k] ?? k) : k;
1185
- if (!(t in out)) {
1186
- out[t] = v;
1187
- }
1198
+ this.ensure(1);
1199
+ this.json[this.i++] = CLOSE_BRACE;
1200
+ }
1201
+ writeTimestamp(ns, value) {
1202
+ const format = determineTimestampFormat(ns, this.settings);
1203
+ switch (format) {
1204
+ case 5: {
1205
+ const iso = value.toISOString().replace(".000Z", "Z");
1206
+ this.writeAsciiQuoted(iso);
1207
+ return;
1208
+ }
1209
+ case 6: {
1210
+ this.writeAsciiQuoted(dateToUtcString(value));
1211
+ return;
1212
+ }
1213
+ case 7: {
1214
+ const epochSecs = String(value.getTime() / 1000);
1215
+ this.writeAscii(epochSecs);
1216
+ return;
1217
+ }
1218
+ default: {
1219
+ const epochSecs = String(value.getTime() / 1000);
1220
+ this.writeAscii(epochSecs);
1221
+ return;
1188
1222
  }
1189
1223
  }
1190
- return out;
1191
- }
1192
- needsTransform(ns) {
1193
- if (ns.isBlobSchema() || ns.isTimestampSchema() || ns.isBigIntegerSchema() || ns.isBigDecimalSchema()) {
1194
- return true;
1195
- }
1196
- if (ns.isDocumentSchema() || ns.isStructSchema() || ns.isListSchema() || ns.isMapSchema()) {
1197
- return true;
1198
- }
1199
- if (ns.isStringSchema() && ns.getMergedTraits().mediaType) {
1200
- return true;
1201
- }
1202
- return false;
1203
1224
  }
1204
1225
  }
1205
1226
 
1206
- class JsonBytesStringAdapter extends Uint8Array {
1207
- string = null;
1208
- static allocUnsafe(bytes) {
1209
- if (typeof Buffer === "function") {
1210
- const buffer = Buffer.allocUnsafe(bytes);
1211
- return new JsonBytesStringAdapter(buffer.buffer, buffer.byteOffset, buffer.byteLength);
1212
- }
1213
- return new JsonBytesStringAdapter(bytes);
1227
+ class JsonCodec2 extends SerdeContextConfig {
1228
+ settings;
1229
+ constructor(settings) {
1230
+ super();
1231
+ this.settings = settings;
1214
1232
  }
1215
- toString() {
1216
- return this.s();
1233
+ createSerializer() {
1234
+ const serializer = new JsonShapeSerializer2(this.settings);
1235
+ serializer.setSerdeContext(this.serdeContext);
1236
+ return serializer;
1217
1237
  }
1218
- valueOf() {
1219
- return this.s();
1238
+ createDeserializer() {
1239
+ const deserializer = new JsonShapeDeserializer2(this.settings);
1240
+ deserializer.setSerdeContext(this.serdeContext);
1241
+ return deserializer;
1220
1242
  }
1221
- includes(searchString, position) {
1222
- if (typeof searchString === "string") {
1223
- return this.s().includes(searchString, position);
1224
- }
1225
- return Uint8Array.prototype.includes.call(this, searchString, position);
1243
+ }
1244
+
1245
+ class AwsJsonRpcProtocol extends RpcProtocol {
1246
+ serializer;
1247
+ deserializer;
1248
+ serviceTarget;
1249
+ codec;
1250
+ mixin;
1251
+ awsQueryCompatible;
1252
+ constructor({ defaultNamespace, errorTypeRegistries, serviceTarget, awsQueryCompatible, jsonCodec, }) {
1253
+ super({
1254
+ defaultNamespace,
1255
+ errorTypeRegistries,
1256
+ });
1257
+ this.serviceTarget = serviceTarget;
1258
+ this.codec =
1259
+ jsonCodec ??
1260
+ new JsonCodec2({
1261
+ timestampFormat: {
1262
+ useTrait: true,
1263
+ default: 7,
1264
+ },
1265
+ jsonName: false,
1266
+ });
1267
+ this.serializer = this.codec.createSerializer();
1268
+ this.deserializer = this.codec.createDeserializer();
1269
+ this.awsQueryCompatible = !!awsQueryCompatible;
1270
+ this.mixin = new ProtocolLib(this.awsQueryCompatible);
1226
1271
  }
1227
- indexOf(searchString, position) {
1228
- if (typeof searchString === "string") {
1229
- return this.s().indexOf(searchString, position);
1272
+ async serializeRequest(operationSchema, input, context) {
1273
+ const request = await super.serializeRequest(operationSchema, input, context);
1274
+ if (!request.path.endsWith("/")) {
1275
+ request.path += "/";
1230
1276
  }
1231
- return Uint8Array.prototype.indexOf.call(this, searchString, position);
1232
- }
1233
- lastIndexOf(searchString, position) {
1234
- if (typeof searchString === "string") {
1235
- return this.s().lastIndexOf(searchString, position);
1277
+ request.headers["content-type"] = `application/x-amz-json-${this.getJsonRpcVersion()}`;
1278
+ request.headers["x-amz-target"] = `${this.serviceTarget}.${operationSchema.name}`;
1279
+ if (this.awsQueryCompatible) {
1280
+ request.headers["x-amzn-query-mode"] = "true";
1236
1281
  }
1237
- const fn = Uint8Array.prototype.lastIndexOf;
1238
- if (position !== undefined) {
1239
- return fn.call(this, searchString, position);
1282
+ if (deref(operationSchema.input) === "unit" || !request.body) {
1283
+ request.body = "{}";
1240
1284
  }
1241
- return fn.call(this, searchString);
1242
- }
1243
- startsWith(searchString, position) {
1244
- return this.s().startsWith(searchString, position);
1245
- }
1246
- endsWith(searchString, endPosition) {
1247
- return this.s().endsWith(searchString, endPosition);
1285
+ return request;
1248
1286
  }
1249
- match(regexp) {
1250
- return this.s().match(regexp);
1287
+ getPayloadCodec() {
1288
+ return this.codec;
1251
1289
  }
1252
- replace(searchValue, replaceValue) {
1253
- return this.s().replace(searchValue, replaceValue);
1290
+ async handleError(operationSchema, context, response, dataObject, metadata) {
1291
+ const { awsQueryCompatible } = this;
1292
+ if (awsQueryCompatible) {
1293
+ this.mixin.setQueryCompatError(dataObject, response);
1294
+ }
1295
+ const errorIdentifier = loadJsonRpcErrorCode(response, dataObject, awsQueryCompatible) ?? "Unknown";
1296
+ this.mixin.compose(this.compositeErrorRegistry, errorIdentifier, this.options.defaultNamespace);
1297
+ const { errorSchema, errorMetadata } = await this.mixin.getErrorSchemaOrThrowBaseException(errorIdentifier, this.options.defaultNamespace, response, dataObject, metadata, awsQueryCompatible ? this.mixin.findQueryCompatibleError : undefined);
1298
+ const ns = NormalizedSchema.of(errorSchema);
1299
+ const message = dataObject.message ?? dataObject.Message ?? "UnknownError";
1300
+ const ErrorCtor = this.compositeErrorRegistry.getErrorCtor(errorSchema) ?? Error;
1301
+ const exception = new ErrorCtor({});
1302
+ const output = {};
1303
+ const errorDeserializer = this.codec.createDeserializer();
1304
+ for (const [name, member] of ns.structIterator()) {
1305
+ if (dataObject[name] != null) {
1306
+ output[name] = errorDeserializer.readObject(member, dataObject[name]);
1307
+ }
1308
+ }
1309
+ if (awsQueryCompatible) {
1310
+ this.mixin.queryCompatOutput(dataObject, output);
1311
+ }
1312
+ throw this.mixin.decorateServiceException(Object.assign(exception, errorMetadata, {
1313
+ $fault: ns.getMergedTraits().error,
1314
+ message,
1315
+ }, output), dataObject);
1254
1316
  }
1255
- search(regexp) {
1256
- return this.s().search(regexp);
1317
+ }
1318
+
1319
+ class AwsJson1_0Protocol extends AwsJsonRpcProtocol {
1320
+ constructor({ defaultNamespace, errorTypeRegistries, serviceTarget, awsQueryCompatible, jsonCodec, }) {
1321
+ super({
1322
+ defaultNamespace,
1323
+ errorTypeRegistries,
1324
+ serviceTarget,
1325
+ awsQueryCompatible,
1326
+ jsonCodec,
1327
+ });
1257
1328
  }
1258
- split(separator, limit) {
1259
- return this.s().split(separator, limit);
1329
+ getShapeId() {
1330
+ return "aws.protocols#awsJson1_0";
1260
1331
  }
1261
- substring(start, end) {
1262
- return this.s().substring(start, end);
1332
+ getJsonRpcVersion() {
1333
+ return "1.0";
1263
1334
  }
1264
- trim() {
1265
- return this.s().trim();
1335
+ getDefaultContentType() {
1336
+ return "application/x-amz-json-1.0";
1266
1337
  }
1267
- trimStart() {
1268
- return this.s().trimStart();
1338
+ }
1339
+
1340
+ class AwsJson1_1Protocol extends AwsJsonRpcProtocol {
1341
+ constructor({ defaultNamespace, errorTypeRegistries, serviceTarget, awsQueryCompatible, jsonCodec, }) {
1342
+ super({
1343
+ defaultNamespace,
1344
+ errorTypeRegistries,
1345
+ serviceTarget,
1346
+ awsQueryCompatible,
1347
+ jsonCodec,
1348
+ });
1269
1349
  }
1270
- trimEnd() {
1271
- return this.s().trimEnd();
1350
+ getShapeId() {
1351
+ return "aws.protocols#awsJson1_1";
1272
1352
  }
1273
- charAt(pos) {
1274
- return this.s().charAt(pos);
1353
+ getJsonRpcVersion() {
1354
+ return "1.1";
1275
1355
  }
1276
- charCodeAt(index) {
1277
- return this.s().charCodeAt(index);
1356
+ getDefaultContentType() {
1357
+ return "application/x-amz-json-1.1";
1278
1358
  }
1279
- padStart(maxLength, fillString) {
1280
- return this.s().padStart(maxLength, fillString);
1359
+ }
1360
+
1361
+ class AwsRestJsonProtocol extends HttpBindingProtocol {
1362
+ serializer;
1363
+ deserializer;
1364
+ codec;
1365
+ mixin = new ProtocolLib();
1366
+ constructor({ defaultNamespace, errorTypeRegistries, jsonCodec, }) {
1367
+ super({
1368
+ defaultNamespace,
1369
+ errorTypeRegistries,
1370
+ });
1371
+ const settings = {
1372
+ timestampFormat: {
1373
+ useTrait: true,
1374
+ default: 7,
1375
+ },
1376
+ httpBindings: true,
1377
+ jsonName: true,
1378
+ };
1379
+ this.codec = jsonCodec ?? new JsonCodec2(settings);
1380
+ this.serializer = new HttpInterceptingShapeSerializer(this.codec.createSerializer(), settings);
1381
+ this.deserializer = new HttpInterceptingShapeDeserializer(this.codec.createDeserializer(), settings);
1281
1382
  }
1282
- padEnd(maxLength, fillString) {
1283
- return this.s().padEnd(maxLength, fillString);
1383
+ getShapeId() {
1384
+ return "aws.protocols#restJson1";
1284
1385
  }
1285
- repeat(count) {
1286
- return this.s().repeat(count);
1386
+ getPayloadCodec() {
1387
+ return this.codec;
1287
1388
  }
1288
- toUpperCase() {
1289
- return this.s().toUpperCase();
1389
+ setSerdeContext(serdeContext) {
1390
+ this.codec.setSerdeContext(serdeContext);
1391
+ super.setSerdeContext(serdeContext);
1290
1392
  }
1291
- toLowerCase() {
1292
- return this.s().toLowerCase();
1393
+ async serializeRequest(operationSchema, input, context) {
1394
+ const request = await super.serializeRequest(operationSchema, input, context);
1395
+ const inputSchema = NormalizedSchema.of(operationSchema.input);
1396
+ if (!request.headers["content-type"]) {
1397
+ const contentType = this.mixin.resolveRestContentType(this.getDefaultContentType(), inputSchema);
1398
+ if (contentType) {
1399
+ request.headers["content-type"] = contentType;
1400
+ }
1401
+ }
1402
+ if (request.body == null && request.headers["content-type"] === this.getDefaultContentType()) {
1403
+ request.body = "{}";
1404
+ }
1405
+ return request;
1293
1406
  }
1294
- s() {
1295
- if (this.string == null) {
1296
- const n = Date.now();
1297
- if (n > warned + 60_000) {
1298
- console.warn("@aws-sdk/core/protocols - WARN - JsonCodec2: you have called a string method on a Uint8Array request body. " +
1299
- "It has been automatically converted to string. In a future version this will throw an error.");
1300
- warned = n;
1407
+ async deserializeResponse(operationSchema, context, response) {
1408
+ const output = await super.deserializeResponse(operationSchema, context, response);
1409
+ const outputSchema = NormalizedSchema.of(operationSchema.output);
1410
+ for (const [name, member] of outputSchema.structIterator()) {
1411
+ if (member.getMemberTraits().httpPayload && !(name in output)) {
1412
+ output[name] = null;
1301
1413
  }
1302
- this.string = toUtf8(this);
1303
1414
  }
1304
- return this.string;
1415
+ return output;
1416
+ }
1417
+ async handleError(operationSchema, context, response, dataObject, metadata) {
1418
+ const errorIdentifier = loadRestJsonErrorCode(response, dataObject) ?? "Unknown";
1419
+ this.mixin.compose(this.compositeErrorRegistry, errorIdentifier, this.options.defaultNamespace);
1420
+ const { errorSchema, errorMetadata } = await this.mixin.getErrorSchemaOrThrowBaseException(errorIdentifier, this.options.defaultNamespace, response, dataObject, metadata);
1421
+ const ns = NormalizedSchema.of(errorSchema);
1422
+ const message = dataObject.message ?? dataObject.Message ?? "UnknownError";
1423
+ const ErrorCtor = this.compositeErrorRegistry.getErrorCtor(errorSchema) ?? Error;
1424
+ const exception = new ErrorCtor({});
1425
+ await this.deserializeHttpMessage(errorSchema, context, response, dataObject);
1426
+ const output = {};
1427
+ const errorDeserializer = this.codec.createDeserializer();
1428
+ for (const [name, member] of ns.structIterator()) {
1429
+ const target = member.getMergedTraits().jsonName ?? name;
1430
+ output[name] = errorDeserializer.readObject(member, dataObject[target]);
1431
+ }
1432
+ throw this.mixin.decorateServiceException(Object.assign(exception, errorMetadata, {
1433
+ $fault: ns.getMergedTraits().error,
1434
+ message,
1435
+ }, output), dataObject);
1305
1436
  }
1306
- }
1307
- var warned = 0;
1308
-
1309
- const encoder = new TextEncoder();
1310
- const OPEN_BRACE = 0x7b;
1311
- const CLOSE_BRACE = 0x7d;
1312
- const OPEN_BRACKET = 0x5b;
1313
- const CLOSE_BRACKET = 0x5d;
1314
- const QUOTE = 0x22;
1315
- const COLON = 0x3a;
1316
- const COMMA = 0x2c;
1317
- const BACKSLASH = 0x5c;
1318
- const TRUE = new Uint8Array([0x74, 0x72, 0x75, 0x65]);
1319
- const FALSE = new Uint8Array([0x66, 0x61, 0x6c, 0x73, 0x65]);
1320
- const NULL = new Uint8Array([0x6e, 0x75, 0x6c, 0x6c]);
1321
- const ESCAPE_TABLE = new Array(128).fill(null);
1322
- ESCAPE_TABLE[0x08] = "b";
1323
- ESCAPE_TABLE[0x09] = "t";
1324
- ESCAPE_TABLE[0x0a] = "n";
1325
- ESCAPE_TABLE[0x0c] = "f";
1326
- ESCAPE_TABLE[0x0d] = "r";
1327
- ESCAPE_TABLE[0x22] = '"';
1328
- ESCAPE_TABLE[0x5c] = "\\";
1329
- for (let i = 0; i < 0x20; i++) {
1330
- if (ESCAPE_TABLE[i] === null) {
1331
- ESCAPE_TABLE[i] = "u00" + i.toString(16).padStart(2, "0");
1437
+ getDefaultContentType() {
1438
+ return "application/json";
1332
1439
  }
1333
1440
  }
1334
- const INITIAL_BUFFER_SIZE = 2048;
1335
- function alloc(size) {
1336
- return JsonBytesStringAdapter.allocUnsafe(size);
1337
- }
1338
- class JsonShapeSerializer2 extends SerdeContextConfig {
1441
+
1442
+ class JsonShapeDeserializer extends SerdeContextConfig {
1339
1443
  settings;
1340
- json;
1341
- i = 0;
1342
- rootSchema;
1343
- rawValue;
1344
- passthrough = false;
1345
1444
  constructor(settings) {
1346
1445
  super();
1347
1446
  this.settings = settings;
1348
- this.json = alloc(INITIAL_BUFFER_SIZE);
1349
1447
  }
1350
- write(schema, value) {
1351
- this.i = 0;
1352
- this.rawValue = value;
1353
- this.rootSchema = NormalizedSchema.of(schema);
1354
- this.passthrough = this.rootSchema.isBlobSchema() || this.rootSchema.isStringSchema();
1355
- if (!this.passthrough) {
1356
- this.writeValue(this.rootSchema, value, undefined);
1357
- }
1448
+ async read(schema, data) {
1449
+ const reviver = needsReviver(schema) ? jsonReviver : undefined;
1450
+ return this._read(schema, typeof data === "string" ? JSON.parse(data, reviver) : await parseJsonBody(data, this.serdeContext, schema));
1358
1451
  }
1359
- writeDiscriminatedDocument(schema, value) {
1360
- this.i = 0;
1361
- this.rootSchema = NormalizedSchema.of(schema);
1362
- const ns = this.rootSchema;
1363
- if (ns.isStructSchema() && value != null && typeof value === "object") {
1364
- this.writeValue(ns, value, undefined);
1365
- const prefix = `"__type":"${ns.getName(true) ?? "Unknown"}",`;
1366
- const z = prefix.length;
1367
- this.ensure(z);
1368
- this.json.copyWithin(1 + z, 1, this.i);
1369
- encoder.encodeInto(prefix, this.json.subarray(1));
1370
- this.i += z;
1371
- }
1372
- else {
1373
- this.writeValue(ns, value, undefined);
1374
- }
1452
+ readObject(schema, data) {
1453
+ return this._read(schema, data);
1375
1454
  }
1376
- flush() {
1377
- this.rootSchema = undefined;
1378
- const finalPosition = this.i;
1379
- this.i = 0;
1380
- const raw = this.rawValue;
1381
- this.rawValue = undefined;
1382
- if (finalPosition === 0) {
1383
- return raw;
1455
+ _read(schema, value) {
1456
+ const isObject = value !== null && typeof value === "object";
1457
+ const ns = NormalizedSchema.of(schema);
1458
+ if (isObject) {
1459
+ if (ns.isStructSchema()) {
1460
+ const record = value;
1461
+ const union = ns.isUnionSchema();
1462
+ const out = {};
1463
+ let nameMap = void 0;
1464
+ const { jsonName } = this.settings;
1465
+ if (jsonName) {
1466
+ nameMap = {};
1467
+ }
1468
+ let unionSerde;
1469
+ if (union) {
1470
+ unionSerde = new UnionSerde(record, out);
1471
+ }
1472
+ for (const [memberName, memberSchema] of ns.structIterator()) {
1473
+ let fromKey = memberName;
1474
+ if (jsonName) {
1475
+ fromKey = memberSchema.getMergedTraits().jsonName ?? fromKey;
1476
+ nameMap[fromKey] = memberName;
1477
+ }
1478
+ if (union) {
1479
+ unionSerde.mark(fromKey);
1480
+ }
1481
+ if (record[fromKey] != null) {
1482
+ out[memberName] = this._read(memberSchema, record[fromKey]);
1483
+ }
1484
+ }
1485
+ if (union) {
1486
+ unionSerde.writeUnknown();
1487
+ }
1488
+ else if (typeof record.__type === "string") {
1489
+ for (const k in record) {
1490
+ const v = record[k];
1491
+ const t = jsonName ? (nameMap[k] ?? k) : k;
1492
+ if (!(t in out)) {
1493
+ out[t] = v;
1494
+ }
1495
+ }
1496
+ }
1497
+ return out;
1498
+ }
1499
+ if (Array.isArray(value) && ns.isListSchema()) {
1500
+ const listMember = ns.getValueSchema();
1501
+ const out = [];
1502
+ for (const item of value) {
1503
+ out.push(this._read(listMember, item));
1504
+ }
1505
+ return out;
1506
+ }
1507
+ if (ns.isMapSchema()) {
1508
+ const mapMember = ns.getValueSchema();
1509
+ const out = {};
1510
+ for (const _k in value) {
1511
+ if (_k === "__proto__") {
1512
+ writeKey(out);
1513
+ }
1514
+ out[_k] = this._read(mapMember, value[_k]);
1515
+ }
1516
+ return out;
1517
+ }
1384
1518
  }
1385
- const result = this.json.subarray(0, finalPosition);
1386
- this.json = alloc(INITIAL_BUFFER_SIZE);
1387
- return result;
1388
- }
1389
- ensure(byteCount) {
1390
- const { i, json } = this;
1391
- if (i + byteCount > json.length) {
1392
- let newSize = json.length * 2;
1393
- while (newSize < i + byteCount) {
1394
- newSize *= 2;
1519
+ if (ns.isBlobSchema() && typeof value === "string") {
1520
+ return fromBase64(value);
1521
+ }
1522
+ const mediaType = ns.getMergedTraits().mediaType;
1523
+ if (ns.isStringSchema() && typeof value === "string" && mediaType) {
1524
+ const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
1525
+ if (isJson) {
1526
+ return LazyJsonString.from(value);
1395
1527
  }
1396
- const next = alloc(newSize);
1397
- next.set(this.json);
1398
- this.json = next;
1528
+ return value;
1399
1529
  }
1400
- }
1401
- writeAscii(s) {
1402
- const z = s.length;
1403
- this.ensure(z);
1404
- let { i, json } = this;
1405
- for (let j = 0; j < z; ++j) {
1406
- json[i] = s.charCodeAt(j);
1407
- i += 1;
1530
+ if (ns.isTimestampSchema() && value != null) {
1531
+ const format = determineTimestampFormat(ns, this.settings);
1532
+ switch (format) {
1533
+ case 5:
1534
+ return parseRfc3339DateTimeWithOffset(value);
1535
+ case 6:
1536
+ return parseRfc7231DateTime(value);
1537
+ case 7:
1538
+ return parseEpochTimestamp(value);
1539
+ default:
1540
+ console.warn("Missing timestamp format, parsing value with Date constructor:", value);
1541
+ return new Date(value);
1542
+ }
1408
1543
  }
1409
- this.i = i;
1410
- }
1411
- writeAsciiQuoted(s) {
1412
- const z = s.length;
1413
- this.ensure(z + 4);
1414
- let { json, i } = this;
1415
- json[i++] = QUOTE;
1416
- for (let j = 0; j < z; ++j) {
1417
- json[i++] = s.charCodeAt(j);
1544
+ if (ns.isBigIntegerSchema() && (typeof value === "number" || typeof value === "string")) {
1545
+ return BigInt(value);
1418
1546
  }
1419
- json[i++] = QUOTE;
1420
- this.i = i;
1421
- }
1422
- writeJsonString(s) {
1423
- this.ensure(s.length * 3 + 2);
1424
- this.json[this.i++] = QUOTE;
1425
- const z = s.length;
1426
- for (let j = 0; j < z; ++j) {
1427
- const c = s.charCodeAt(j);
1428
- if (c > 0x22 && c < 0x5c) {
1429
- this.json[this.i++] = c;
1547
+ if (ns.isBigDecimalSchema() && value != undefined) {
1548
+ if (value instanceof NumericValue) {
1549
+ return value;
1430
1550
  }
1431
- else if (c < 0x80) {
1432
- const esc = ESCAPE_TABLE[c];
1433
- if (esc !== null) {
1434
- this.ensure(esc.length + 1);
1435
- this.json[this.i++] = BACKSLASH;
1436
- for (let k = 0; k < esc.length; k++) {
1437
- this.json[this.i++] = esc.charCodeAt(k);
1438
- }
1439
- }
1440
- else {
1441
- this.json[this.i++] = c;
1442
- }
1551
+ const untyped = value;
1552
+ if (untyped.type === "bigDecimal" && "string" in untyped) {
1553
+ return new NumericValue(untyped.string, untyped.type);
1554
+ }
1555
+ return new NumericValue(String(value), "bigDecimal");
1556
+ }
1557
+ if (ns.isNumericSchema() && typeof value === "string") {
1558
+ switch (value) {
1559
+ case "Infinity":
1560
+ return Infinity;
1561
+ case "-Infinity":
1562
+ return -Infinity;
1563
+ case "NaN":
1564
+ return NaN;
1443
1565
  }
1444
- else if (c >= 0xd800 && c <= 0xdbff) {
1445
- const next = j + 1 < z ? s.charCodeAt(j + 1) : 0;
1446
- if (next >= 0xdc00 && next <= 0xdfff) {
1447
- this.ensure(4);
1448
- const { written } = encoder.encodeInto(s.substring(j, j + 2), this.json.subarray(this.i));
1449
- this.i += written;
1450
- ++j;
1451
- }
1452
- else {
1453
- this.ensure(6);
1454
- this.writeUnicodeEscape(c);
1566
+ return value;
1567
+ }
1568
+ if (ns.isDocumentSchema()) {
1569
+ if (isObject) {
1570
+ const out = Array.isArray(value) ? [] : {};
1571
+ for (const k in value) {
1572
+ if (k === "__proto__") {
1573
+ writeKey(out);
1574
+ }
1575
+ const v = value[k];
1576
+ if (v instanceof NumericValue) {
1577
+ out[k] = v;
1578
+ }
1579
+ else {
1580
+ out[k] = this._read(ns, v);
1581
+ }
1455
1582
  }
1456
- }
1457
- else if (c >= 0xdc00 && c <= 0xdfff) {
1458
- this.ensure(6);
1459
- this.writeUnicodeEscape(c);
1583
+ return out;
1460
1584
  }
1461
1585
  else {
1462
- let { i, json } = this;
1463
- if (c < 0x800) {
1464
- json[i++] = 0xc0 | (c >> 6);
1465
- json[i++] = 0x80 | (c & 0x3f);
1466
- }
1467
- else {
1468
- json[i++] = 0xe0 | (c >> 12);
1469
- json[i++] = 0x80 | ((c >> 6) & 0x3f);
1470
- json[i++] = 0x80 | (c & 0x3f);
1471
- }
1472
- this.i = i;
1586
+ return structuredClone(value);
1473
1587
  }
1474
1588
  }
1475
- this.json[this.i++] = QUOTE;
1589
+ return value;
1476
1590
  }
1477
- writeUnicodeEscape(code) {
1478
- let { json, i } = this;
1479
- json[i++] = BACKSLASH;
1480
- json[i++] = 0x75;
1481
- const hex = code.toString(16).padStart(4, "0");
1482
- for (let j = 0; j < 4; ++j) {
1483
- json[i++] = hex.charCodeAt(j);
1591
+ }
1592
+
1593
+ const NUMERIC_CONTROL_CHAR = String.fromCharCode(925);
1594
+ class JsonReplacer {
1595
+ values = new Map();
1596
+ counter = 0;
1597
+ stage = 0;
1598
+ createReplacer() {
1599
+ if (this.stage === 1) {
1600
+ throw new Error("@aws-sdk/core/protocols - JsonReplacer already created.");
1484
1601
  }
1485
- this.i = i;
1602
+ if (this.stage === 2) {
1603
+ throw new Error("@aws-sdk/core/protocols - JsonReplacer exhausted.");
1604
+ }
1605
+ this.stage = 1;
1606
+ return (key, value) => {
1607
+ if (value instanceof NumericValue) {
1608
+ const v = `${NUMERIC_CONTROL_CHAR + "nv" + this.counter++}_` + value.string;
1609
+ this.values.set(`"${v}"`, value.string);
1610
+ return v;
1611
+ }
1612
+ if (typeof value === "bigint") {
1613
+ const s = value.toString();
1614
+ const v = `${NUMERIC_CONTROL_CHAR + "b" + this.counter++}_` + s;
1615
+ this.values.set(`"${v}"`, s);
1616
+ return v;
1617
+ }
1618
+ return value;
1619
+ };
1486
1620
  }
1487
- static B64 = (() => {
1488
- const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1489
- const table = new Uint8Array(64);
1490
- for (let i = 0; i < 64; ++i) {
1491
- table[i] = chars.charCodeAt(i);
1621
+ replaceInJson(json) {
1622
+ if (this.stage === 0) {
1623
+ throw new Error("@aws-sdk/core/protocols - JsonReplacer not created yet.");
1492
1624
  }
1493
- return table;
1494
- })();
1495
- writeBase64(data) {
1496
- const b64Len = Math.ceil(data.length / 3) * 4;
1497
- this.ensure(b64Len + 2);
1498
- const json = this.json;
1499
- const B64 = JsonShapeSerializer2.B64;
1500
- let i = this.i;
1501
- json[i++] = QUOTE;
1502
- const len = data.length;
1503
- const remainder = len % 3;
1504
- const mainLen = len - remainder;
1505
- for (let j = 0; j < mainLen; j += 3) {
1506
- const a = data[j];
1507
- const b = data[j + 1];
1508
- const c = data[j + 2];
1509
- json[i++] = B64[a >> 2];
1510
- json[i++] = B64[((a & 0x03) << 4) | (b >> 4)];
1511
- json[i++] = B64[((b & 0x0f) << 2) | (c >> 6)];
1512
- json[i++] = B64[c & 0x3f];
1625
+ if (this.stage === 2) {
1626
+ throw new Error("@aws-sdk/core/protocols - JsonReplacer exhausted.");
1513
1627
  }
1514
- if (remainder === 2) {
1515
- const a = data[mainLen];
1516
- const b = data[mainLen + 1];
1517
- json[i++] = B64[a >> 2];
1518
- json[i++] = B64[((a & 0x03) << 4) | (b >> 4)];
1519
- json[i++] = B64[(b & 0x0f) << 2];
1520
- json[i++] = 0x3d;
1628
+ this.stage = 2;
1629
+ if (this.counter === 0) {
1630
+ return json;
1521
1631
  }
1522
- else if (remainder === 1) {
1523
- const a = data[mainLen];
1524
- json[i++] = B64[a >> 2];
1525
- json[i++] = B64[(a & 0x03) << 4];
1526
- json[i++] = 0x3d;
1527
- json[i++] = 0x3d;
1632
+ for (const [key, value] of this.values) {
1633
+ json = json.replace(key, value);
1528
1634
  }
1529
- json[i++] = QUOTE;
1530
- this.i = i;
1635
+ return json;
1531
1636
  }
1532
- writeValue(schema, value, container) {
1533
- if (value == null) {
1534
- if (container?.isStructSchema()) {
1535
- if (value === undefined) {
1536
- const ns = NormalizedSchema.of(schema);
1537
- if (ns.isIdempotencyToken()) {
1538
- this.writeAsciiQuoted(generateIdempotencyToken());
1539
- return;
1540
- }
1541
- }
1542
- return;
1637
+ }
1638
+
1639
+ class JsonShapeSerializer extends SerdeContextConfig {
1640
+ settings;
1641
+ buffer;
1642
+ useReplacer = false;
1643
+ rootSchema;
1644
+ constructor(settings) {
1645
+ super();
1646
+ this.settings = settings;
1647
+ }
1648
+ write(schema, value) {
1649
+ this.rootSchema = NormalizedSchema.of(schema);
1650
+ this.buffer = this._write(this.rootSchema, value);
1651
+ }
1652
+ flush() {
1653
+ const { rootSchema, useReplacer } = this;
1654
+ this.rootSchema = undefined;
1655
+ this.useReplacer = false;
1656
+ if (rootSchema?.isStructSchema() || rootSchema?.isDocumentSchema()) {
1657
+ if (!useReplacer) {
1658
+ return JSON.stringify(this.buffer);
1543
1659
  }
1544
- this.ensure(4);
1545
- this.json.set(NULL, this.i);
1546
- this.i += 4;
1547
- return;
1660
+ const replacer = new JsonReplacer();
1661
+ return replacer.replaceInJson(JSON.stringify(this.buffer, replacer.createReplacer(), 0));
1548
1662
  }
1549
- const ns = NormalizedSchema.of(schema);
1550
- const isObject = typeof value === "object";
1551
- if (ns.isStringSchema()) {
1552
- const mediaType = ns.getMergedTraits().mediaType;
1553
- if (mediaType) {
1554
- const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
1555
- if (isJson) {
1556
- this.writeJsonString(LazyJsonString.from(value).toString());
1557
- return;
1558
- }
1559
- }
1663
+ return this.buffer;
1664
+ }
1665
+ writeDiscriminatedDocument(schema, value) {
1666
+ this.write(schema, value);
1667
+ if (typeof this.buffer === "object") {
1668
+ this.buffer.__type = NormalizedSchema.of(schema).getName(true);
1560
1669
  }
1670
+ }
1671
+ _write(schema, value, container) {
1672
+ const isObject = value !== null && typeof value === "object";
1673
+ const ns = NormalizedSchema.of(schema);
1561
1674
  if (isObject) {
1562
1675
  if (ns.isStructSchema()) {
1563
- this.writeStruct(ns, value);
1564
- return;
1676
+ const record = value;
1677
+ const out = {};
1678
+ const { jsonName } = this.settings;
1679
+ let nameMap = void 0;
1680
+ if (jsonName) {
1681
+ nameMap = {};
1682
+ }
1683
+ let outCount = 0;
1684
+ for (const [memberName, memberSchema] of ns.structIterator()) {
1685
+ const serializableValue = this._write(memberSchema, record[memberName], ns);
1686
+ if (serializableValue !== undefined) {
1687
+ let targetKey = memberName;
1688
+ if (jsonName) {
1689
+ targetKey = memberSchema.getMergedTraits().jsonName ?? memberName;
1690
+ nameMap[memberName] = targetKey;
1691
+ }
1692
+ out[targetKey] = serializableValue;
1693
+ outCount++;
1694
+ }
1695
+ }
1696
+ if (ns.isUnionSchema() && outCount === 0) {
1697
+ const { $unknown } = record;
1698
+ if (Array.isArray($unknown)) {
1699
+ const [k, v] = $unknown;
1700
+ if (k === "__proto__") {
1701
+ writeKey(out);
1702
+ }
1703
+ out[k] = this._write(15, v);
1704
+ }
1705
+ }
1706
+ else if (typeof record.__type === "string") {
1707
+ for (const k in record) {
1708
+ const v = record[k];
1709
+ const targetKey = jsonName ? (nameMap[k] ?? k) : k;
1710
+ if (!(targetKey in out)) {
1711
+ out[targetKey] = this._write(15, v);
1712
+ }
1713
+ }
1714
+ }
1715
+ return out;
1565
1716
  }
1566
- if (Array.isArray(value) && (ns.isListSchema() || ns.isDocumentSchema())) {
1567
- this.writeList(ns, value, ns.isDocumentSchema());
1568
- return;
1717
+ if (Array.isArray(value) && ns.isListSchema()) {
1718
+ const listMember = ns.getValueSchema();
1719
+ const out = [];
1720
+ const sparse = !!ns.getMergedTraits().sparse;
1721
+ for (const item of value) {
1722
+ if (sparse || item != null) {
1723
+ out.push(this._write(listMember, item));
1724
+ }
1725
+ }
1726
+ return out;
1569
1727
  }
1570
1728
  if (ns.isMapSchema()) {
1571
- this.writeMap(ns, value, false);
1572
- return;
1729
+ const mapMember = ns.getValueSchema();
1730
+ const out = {};
1731
+ const sparse = !!ns.getMergedTraits().sparse;
1732
+ for (const _k in value) {
1733
+ const _v = value[_k];
1734
+ if (sparse || _v != null) {
1735
+ if (_k === "__proto__") {
1736
+ writeKey(out);
1737
+ }
1738
+ out[_k] = this._write(mapMember, _v);
1739
+ }
1740
+ }
1741
+ return out;
1573
1742
  }
1574
- if (value instanceof Uint8Array && (ns.isBlobSchema() || ns.isDocumentSchema())) {
1575
- this.writeBase64(value);
1576
- return;
1743
+ if (value instanceof Uint8Array && ns.isBlobSchema()) {
1744
+ if (ns === this.rootSchema) {
1745
+ return value;
1746
+ }
1747
+ return (this.serdeContext?.base64Encoder ?? toBase64)(value);
1577
1748
  }
1578
1749
  if (value instanceof Date && (ns.isTimestampSchema() || ns.isDocumentSchema())) {
1579
- this.writeTimestamp(ns, value);
1580
- return;
1750
+ const format = determineTimestampFormat(ns, this.settings);
1751
+ switch (format) {
1752
+ case 5:
1753
+ return value.toISOString().replace(".000Z", "Z");
1754
+ case 6:
1755
+ return dateToUtcString(value);
1756
+ case 7:
1757
+ return value.getTime() / 1000;
1758
+ default:
1759
+ console.warn("Missing timestamp format, using epoch seconds", value);
1760
+ return value.getTime() / 1000;
1761
+ }
1581
1762
  }
1582
1763
  if (value instanceof NumericValue) {
1583
- this.writeAscii(value.string);
1584
- return;
1585
- }
1586
- if (ns.isDocumentSchema()) {
1587
- if (Array.isArray(value)) {
1588
- this.writeList(ns, value, true);
1589
- }
1590
- else {
1591
- this.writeMap(ns, value, true);
1592
- }
1593
- return;
1764
+ this.useReplacer = true;
1594
1765
  }
1595
- const json = JSON.stringify(value);
1596
- this.writeAscii(json);
1597
- return;
1598
1766
  }
1599
- if (typeof value === "string") {
1600
- if (ns.isBlobSchema()) {
1601
- const b64 = (this.serdeContext?.base64Encoder ?? toBase64)(value);
1602
- this.writeAsciiQuoted(b64);
1603
- return;
1767
+ if (value === null && container?.isStructSchema()) {
1768
+ return void 0;
1769
+ }
1770
+ if (ns.isStringSchema()) {
1771
+ if (typeof value === "undefined" && ns.isIdempotencyToken()) {
1772
+ return generateIdempotencyToken();
1604
1773
  }
1605
- this.writeJsonString(value);
1606
- return;
1774
+ const mediaType = ns.getMergedTraits().mediaType;
1775
+ if (value != null && mediaType) {
1776
+ const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
1777
+ if (isJson) {
1778
+ return LazyJsonString.from(value);
1779
+ }
1780
+ }
1781
+ return value;
1607
1782
  }
1608
1783
  if (typeof value === "number") {
1609
- if (ns.isNumericSchema() && (Math.abs(value) === Infinity || isNaN(value))) {
1610
- this.writeAsciiQuoted(String(value));
1611
- return;
1784
+ if (Math.abs(value) === Infinity || isNaN(value)) {
1785
+ return String(value);
1612
1786
  }
1613
- const numStr = String(value);
1614
- this.writeAscii(numStr);
1615
- return;
1787
+ return value;
1616
1788
  }
1617
- if (typeof value === "boolean") {
1618
- this.ensure(5);
1619
- let { i, json } = this;
1620
- if (value) {
1621
- json.set(TRUE, i);
1622
- i += 4;
1623
- }
1624
- else {
1625
- json.set(FALSE, i);
1626
- i += 5;
1789
+ if (typeof value === "string" && ns.isBlobSchema()) {
1790
+ if (ns === this.rootSchema) {
1791
+ return value;
1627
1792
  }
1628
- this.i = i;
1629
- return;
1793
+ return (this.serdeContext?.base64Encoder ?? toBase64)(value);
1630
1794
  }
1631
1795
  if (typeof value === "bigint") {
1632
- this.writeAscii(value.toString());
1633
- return;
1634
- }
1635
- this.writeAscii(String(value));
1636
- }
1637
- writeStruct(ns, value) {
1638
- this.ensure(2);
1639
- this.json[this.i++] = OPEN_BRACE;
1640
- let wroteAny = false;
1641
- const hasType = typeof value.__type === "string";
1642
- let writtenKeys;
1643
- if (hasType) {
1644
- writtenKeys = new Set();
1645
- }
1646
- for (const [memberName, memberSchema] of ns.structIterator()) {
1647
- const item = value[memberName];
1648
- if (item == null && !memberSchema.isIdempotencyToken()) {
1649
- continue;
1650
- }
1651
- if (wroteAny) {
1652
- this.ensure(1);
1653
- this.json[this.i++] = COMMA;
1654
- }
1655
- wroteAny = true;
1656
- const targetKey = this.settings.jsonName ? (memberSchema.getMergedTraits().jsonName ?? memberName) : memberName;
1657
- if (writtenKeys) {
1658
- writtenKeys.add(memberName);
1659
- writtenKeys.add(targetKey);
1660
- }
1661
- this.writeAsciiQuoted(targetKey);
1662
- this.json[this.i++] = COLON;
1663
- this.writeValue(memberSchema, item, ns);
1664
- }
1665
- if (!wroteAny && ns.isUnionSchema()) {
1666
- const { $unknown } = value;
1667
- if (Array.isArray($unknown)) {
1668
- const [k, v] = $unknown;
1669
- this.writeAsciiQuoted(k);
1670
- this.ensure(1);
1671
- this.json[this.i++] = COLON;
1672
- this.writeValue(15, v, ns);
1673
- }
1796
+ this.useReplacer = true;
1674
1797
  }
1675
- else if (hasType) {
1676
- for (const k in value) {
1677
- if (writtenKeys.has(k)) {
1678
- continue;
1679
- }
1680
- writtenKeys.add(k);
1681
- const v = value[k];
1682
- if (wroteAny) {
1683
- this.ensure(1);
1684
- this.json[this.i++] = COMMA;
1798
+ if (ns.isDocumentSchema()) {
1799
+ if (isObject) {
1800
+ if (value instanceof Uint8Array) {
1801
+ return (this.serdeContext?.base64Encoder ?? toBase64)(value);
1685
1802
  }
1686
- wroteAny = true;
1687
- this.writeAsciiQuoted(k);
1688
- this.ensure(1);
1689
- this.json[this.i++] = COLON;
1690
- this.writeValue(15, v, undefined);
1691
- }
1692
- }
1693
- this.ensure(1);
1694
- this.json[this.i++] = CLOSE_BRACE;
1695
- }
1696
- writeList(ns, value, isDocument) {
1697
- const sparse = !!ns.getMergedTraits().sparse;
1698
- const valueSchema = ns.getValueSchema();
1699
- if (!isDocument) {
1700
- if (valueSchema.isStringSchema() || valueSchema.isNumericSchema() || valueSchema.isBooleanSchema()) {
1701
- const json = sparse ? JSON.stringify(value) : JSON.stringify(value.filter((_) => _ != null));
1702
- this.ensure(json.length * 3);
1703
- this.i += encoder.encodeInto(json, this.json.subarray(this.i)).written;
1704
- return;
1705
- }
1706
- }
1707
- this.ensure(2);
1708
- this.json[this.i++] = OPEN_BRACKET;
1709
- let wroteFirstItem = false;
1710
- for (let i = 0; i < value.length; ++i) {
1711
- const item = value[i];
1712
- if (isDocument ? item === undefined : item == null && !sparse) {
1713
- continue;
1714
- }
1715
- if (wroteFirstItem) {
1716
- this.ensure(1);
1717
- this.json[this.i++] = COMMA;
1718
- }
1719
- this.writeValue(valueSchema, item, undefined);
1720
- wroteFirstItem = true;
1721
- }
1722
- this.ensure(1);
1723
- this.json[this.i++] = CLOSE_BRACKET;
1724
- }
1725
- writeMap(ns, value, isDocument) {
1726
- const sparse = !!ns.getMergedTraits().sparse;
1727
- const valueSchema = ns.getValueSchema();
1728
- if (!isDocument) {
1729
- if (valueSchema.isStringSchema() || valueSchema.isNumericSchema() || valueSchema.isBooleanSchema()) {
1730
- let input = value;
1731
- if (sparse) {
1732
- input = {};
1733
- for (const k in value) {
1734
- if (k === "__proto__") {
1735
- writeKey(input);
1736
- }
1737
- input[k] = value[k] ?? null;
1803
+ const out = Array.isArray(value) ? [] : {};
1804
+ for (const k in value) {
1805
+ const v = value[k];
1806
+ if (k === "__proto__") {
1807
+ writeKey(out);
1808
+ }
1809
+ if (v instanceof NumericValue) {
1810
+ this.useReplacer = true;
1811
+ out[k] = v;
1812
+ }
1813
+ else {
1814
+ out[k] = this._write(ns, v);
1738
1815
  }
1739
1816
  }
1740
- const json = JSON.stringify(input);
1741
- this.ensure(json.length * 3);
1742
- this.i += encoder.encodeInto(json, this.json.subarray(this.i)).written;
1743
- return;
1744
- }
1745
- }
1746
- this.ensure(2);
1747
- this.json[this.i++] = OPEN_BRACE;
1748
- let first = true;
1749
- for (const k in value) {
1750
- const v = value[k];
1751
- if (isDocument ? v === undefined : v == null && !sparse) {
1752
- continue;
1753
- }
1754
- if (!first) {
1755
- this.ensure(1);
1756
- this.json[this.i++] = COMMA;
1757
- }
1758
- first = false;
1759
- this.writeJsonString(k);
1760
- this.ensure(1);
1761
- this.json[this.i++] = COLON;
1762
- this.writeValue(valueSchema, v, undefined);
1763
- }
1764
- this.ensure(1);
1765
- this.json[this.i++] = CLOSE_BRACE;
1766
- }
1767
- writeTimestamp(ns, value) {
1768
- const format = determineTimestampFormat(ns, this.settings);
1769
- switch (format) {
1770
- case 5: {
1771
- const iso = value.toISOString().replace(".000Z", "Z");
1772
- this.writeAsciiQuoted(iso);
1773
- return;
1774
- }
1775
- case 6: {
1776
- this.writeAsciiQuoted(dateToUtcString(value));
1777
- return;
1778
- }
1779
- case 7: {
1780
- const epochSecs = String(value.getTime() / 1000);
1781
- this.writeAscii(epochSecs);
1782
- return;
1817
+ return out;
1783
1818
  }
1784
- default: {
1785
- const epochSecs = String(value.getTime() / 1000);
1786
- this.writeAscii(epochSecs);
1787
- return;
1819
+ else {
1820
+ return structuredClone(value);
1788
1821
  }
1789
1822
  }
1823
+ return value;
1790
1824
  }
1791
1825
  }
1792
1826
 
1793
- class JsonCodec2 extends SerdeContextConfig {
1827
+ class JsonCodec extends SerdeContextConfig {
1794
1828
  settings;
1795
1829
  constructor(settings) {
1796
1830
  super();
1797
1831
  this.settings = settings;
1798
1832
  }
1799
1833
  createSerializer() {
1800
- const serializer = new JsonShapeSerializer2(this.settings);
1834
+ const serializer = new JsonShapeSerializer(this.settings);
1801
1835
  serializer.setSerdeContext(this.serdeContext);
1802
1836
  return serializer;
1803
1837
  }
1804
1838
  createDeserializer() {
1805
- const deserializer = new JsonShapeDeserializer2(this.settings);
1839
+ const deserializer = new JsonShapeDeserializer(this.settings);
1806
1840
  deserializer.setSerdeContext(this.serdeContext);
1807
1841
  return deserializer;
1808
1842
  }