@aws-sdk/core 3.975.3 → 3.977.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.
Files changed (61) hide show
  1. package/dist-cjs/submodules/httpAuthSchemes/index.js +66 -28
  2. package/dist-cjs/submodules/protocols/index.js +111 -12
  3. package/dist-es/submodules/httpAuthSchemes/aws_sdk/AwsSdkSigV4ASigner.js +7 -2
  4. package/dist-es/submodules/httpAuthSchemes/aws_sdk/AwsSdkSigV4Signer.js +28 -14
  5. package/dist-es/submodules/httpAuthSchemes/aws_sdk/clock-skew-defaults.browser.js +1 -0
  6. package/dist-es/submodules/httpAuthSchemes/aws_sdk/clock-skew-defaults.js +3 -0
  7. package/dist-es/submodules/httpAuthSchemes/aws_sdk/clock-skew-node-config.js +8 -0
  8. package/dist-es/submodules/httpAuthSchemes/aws_sdk/index.js +1 -1
  9. package/dist-es/submodules/httpAuthSchemes/aws_sdk/resolveAwsSdkSigV4Config.js +3 -3
  10. package/dist-es/submodules/httpAuthSchemes/index.browser.js +9 -0
  11. package/dist-es/submodules/httpAuthSchemes/index.js +5 -1
  12. package/dist-es/submodules/httpAuthSchemes/utils/getDateHeader.js +1 -0
  13. package/dist-es/submodules/httpAuthSchemes/utils/getUpdatedSystemClockOffset.js +12 -6
  14. package/dist-es/submodules/httpAuthSchemes/utils/index.js +2 -1
  15. package/dist-es/submodules/httpAuthSchemes/utils/isClockSkewed.js +1 -1
  16. package/dist-es/submodules/protocols/json/JsonShapeDeserializer.js +10 -1
  17. package/dist-es/submodules/protocols/json/JsonShapeSerializer.js +10 -0
  18. package/dist-es/submodules/protocols/json/detectBufferParsing.js +18 -0
  19. package/dist-es/submodules/protocols/json/experimental/BufferJsonShapeDeserializer.js +180 -0
  20. package/dist-es/submodules/protocols/json/experimental/ByteJsonShapeSerializer.js +483 -0
  21. package/dist-es/submodules/protocols/json/needsReviver.js +41 -0
  22. package/dist-es/submodules/protocols/json/parseJsonBody.js +33 -13
  23. package/dist-es/submodules/protocols/writeKey.js +3 -0
  24. package/dist-es/submodules/protocols/xml/XmlShapeDeserializer.js +4 -0
  25. package/dist-types/submodules/httpAuthSchemes/aws_sdk/AwsSdkSigV4Signer.d.ts +2 -1
  26. package/dist-types/submodules/httpAuthSchemes/aws_sdk/clock-skew-defaults.browser.d.ts +6 -0
  27. package/dist-types/submodules/httpAuthSchemes/aws_sdk/clock-skew-defaults.d.ts +7 -0
  28. package/dist-types/submodules/httpAuthSchemes/aws_sdk/clock-skew-node-config.d.ts +13 -0
  29. package/dist-types/submodules/httpAuthSchemes/aws_sdk/index.d.ts +1 -1
  30. package/dist-types/submodules/httpAuthSchemes/aws_sdk/resolveAwsSdkSigV4Config.d.ts +22 -6
  31. package/dist-types/submodules/httpAuthSchemes/index.browser.d.ts +11 -0
  32. package/dist-types/submodules/httpAuthSchemes/index.d.ts +7 -1
  33. package/dist-types/submodules/httpAuthSchemes/utils/getDateHeader.d.ts +4 -0
  34. package/dist-types/submodules/httpAuthSchemes/utils/getUpdatedSystemClockOffset.d.ts +22 -6
  35. package/dist-types/submodules/httpAuthSchemes/utils/index.d.ts +2 -1
  36. package/dist-types/submodules/protocols/json/detectBufferParsing.d.ts +1 -0
  37. package/dist-types/submodules/protocols/json/experimental/BufferJsonShapeDeserializer.d.ts +29 -0
  38. package/dist-types/submodules/protocols/json/experimental/ByteJsonShapeSerializer.d.ts +57 -0
  39. package/dist-types/submodules/protocols/json/needsReviver.d.ts +11 -0
  40. package/dist-types/submodules/protocols/json/parseJsonBody.d.ts +7 -2
  41. package/dist-types/submodules/protocols/writeKey.d.ts +4 -0
  42. package/dist-types/ts3.4/submodules/httpAuthSchemes/aws_sdk/AwsSdkSigV4Signer.d.ts +2 -0
  43. package/dist-types/ts3.4/submodules/httpAuthSchemes/aws_sdk/clock-skew-defaults.browser.d.ts +1 -0
  44. package/dist-types/ts3.4/submodules/httpAuthSchemes/aws_sdk/clock-skew-defaults.d.ts +1 -0
  45. package/dist-types/ts3.4/submodules/httpAuthSchemes/aws_sdk/clock-skew-node-config.d.ts +4 -0
  46. package/dist-types/ts3.4/submodules/httpAuthSchemes/aws_sdk/index.d.ts +1 -1
  47. package/dist-types/ts3.4/submodules/httpAuthSchemes/aws_sdk/resolveAwsSdkSigV4Config.d.ts +7 -6
  48. package/dist-types/ts3.4/submodules/httpAuthSchemes/index.browser.d.ts +32 -0
  49. package/dist-types/ts3.4/submodules/httpAuthSchemes/index.d.ts +10 -2
  50. package/dist-types/ts3.4/submodules/httpAuthSchemes/utils/getDateHeader.d.ts +1 -0
  51. package/dist-types/ts3.4/submodules/httpAuthSchemes/utils/getUpdatedSystemClockOffset.d.ts +2 -0
  52. package/dist-types/ts3.4/submodules/httpAuthSchemes/utils/index.d.ts +2 -1
  53. package/dist-types/ts3.4/submodules/protocols/json/detectBufferParsing.d.ts +1 -0
  54. package/dist-types/ts3.4/submodules/protocols/json/experimental/BufferJsonShapeDeserializer.d.ts +14 -0
  55. package/dist-types/ts3.4/submodules/protocols/json/experimental/{SinglePassJsonShapeSerializer.d.ts → ByteJsonShapeSerializer.d.ts} +16 -5
  56. package/dist-types/ts3.4/submodules/protocols/json/needsReviver.d.ts +2 -0
  57. package/dist-types/ts3.4/submodules/protocols/json/parseJsonBody.d.ts +7 -2
  58. package/dist-types/ts3.4/submodules/protocols/writeKey.d.ts +1 -0
  59. package/package.json +11 -6
  60. package/dist-es/submodules/protocols/json/experimental/SinglePassJsonShapeSerializer.js +0 -135
  61. package/dist-types/submodules/protocols/json/experimental/SinglePassJsonShapeSerializer.d.ts +0 -27
@@ -0,0 +1,180 @@
1
+ import { determineTimestampFormat } from "@smithy/core/protocols";
2
+ import { NormalizedSchema } from "@smithy/core/schema";
3
+ import { LazyJsonString, NumericValue, parseEpochTimestamp, parseRfc3339DateTimeWithOffset, parseRfc7231DateTime, } from "@smithy/core/serde";
4
+ import { fromBase64 } from "@smithy/core/serde";
5
+ import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
6
+ import { UnionSerde } from "../../UnionSerde";
7
+ import { detectBufferParsing } from "../detectBufferParsing";
8
+ import { jsonReviver } from "../jsonReviver";
9
+ import { needsReviver } from "../needsReviver";
10
+ import { parseJsonBody } from "../parseJsonBody";
11
+ import { writeKey } from "../../writeKey";
12
+ export class BufferJsonShapeDeserializer extends SerdeContextConfig {
13
+ settings;
14
+ constructor(settings) {
15
+ super();
16
+ this.settings = settings;
17
+ }
18
+ async read(schema, data) {
19
+ const reviver = needsReviver(schema) ? jsonReviver : undefined;
20
+ let parsed;
21
+ if (typeof data === "string") {
22
+ parsed = JSON.parse(data, reviver);
23
+ }
24
+ else if (data instanceof Uint8Array && detectBufferParsing()) {
25
+ const buf = Buffer.isBuffer(data) ? data : Buffer.from(data.buffer, data.byteOffset, data.byteLength);
26
+ parsed = JSON.parse(buf, reviver);
27
+ }
28
+ else {
29
+ parsed = await parseJsonBody(data, this.serdeContext);
30
+ }
31
+ return this._read(schema, parsed);
32
+ }
33
+ readObject(schema, data) {
34
+ return this._read(schema, data);
35
+ }
36
+ _read(schema, value) {
37
+ const isObject = value !== null && typeof value === "object";
38
+ const ns = NormalizedSchema.of(schema);
39
+ if (isObject) {
40
+ if (ns.isStructSchema()) {
41
+ return this._readStruct(ns, value);
42
+ }
43
+ if (Array.isArray(value) && ns.isListSchema()) {
44
+ const listMember = ns.getValueSchema();
45
+ for (let i = 0; i < value.length; ++i) {
46
+ value[i] = this._read(listMember, value[i]);
47
+ }
48
+ return value;
49
+ }
50
+ if (ns.isMapSchema()) {
51
+ const mapMember = ns.getValueSchema();
52
+ const map = value;
53
+ for (const k in map) {
54
+ if (k === "__proto__") {
55
+ writeKey(map);
56
+ }
57
+ map[k] = this._read(mapMember, map[k]);
58
+ }
59
+ return map;
60
+ }
61
+ }
62
+ if (ns.isBlobSchema() && typeof value === "string") {
63
+ return fromBase64(value);
64
+ }
65
+ const mediaType = ns.getMergedTraits().mediaType;
66
+ if (ns.isStringSchema() && typeof value === "string" && mediaType) {
67
+ const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
68
+ if (isJson) {
69
+ return LazyJsonString.from(value);
70
+ }
71
+ return value;
72
+ }
73
+ if (ns.isTimestampSchema() && value != null) {
74
+ const format = determineTimestampFormat(ns, this.settings);
75
+ switch (format) {
76
+ case 5:
77
+ return parseRfc3339DateTimeWithOffset(value);
78
+ case 6:
79
+ return parseRfc7231DateTime(value);
80
+ case 7:
81
+ return parseEpochTimestamp(value);
82
+ default:
83
+ console.warn("Missing timestamp format, parsing value with Date constructor:", value);
84
+ return new Date(value);
85
+ }
86
+ }
87
+ if (ns.isBigIntegerSchema() && (typeof value === "number" || typeof value === "string")) {
88
+ return BigInt(value);
89
+ }
90
+ if (ns.isBigDecimalSchema() && value != undefined) {
91
+ if (value instanceof NumericValue) {
92
+ return value;
93
+ }
94
+ const untyped = value;
95
+ if (untyped.type === "bigDecimal" && "string" in untyped) {
96
+ return new NumericValue(untyped.string, untyped.type);
97
+ }
98
+ return new NumericValue(String(value), "bigDecimal");
99
+ }
100
+ if (ns.isNumericSchema() && typeof value === "string") {
101
+ switch (value) {
102
+ case "Infinity":
103
+ return Infinity;
104
+ case "-Infinity":
105
+ return -Infinity;
106
+ case "NaN":
107
+ return NaN;
108
+ }
109
+ return value;
110
+ }
111
+ if (ns.isDocumentSchema()) {
112
+ if (isObject) {
113
+ if (Array.isArray(value)) {
114
+ for (let i = 0; i < value.length; ++i) {
115
+ const v = value[i];
116
+ if (!(v instanceof NumericValue)) {
117
+ value[i] = this._read(ns, v);
118
+ }
119
+ }
120
+ }
121
+ else {
122
+ const doc = value;
123
+ for (const k in doc) {
124
+ if (k === "__proto__") {
125
+ writeKey(doc);
126
+ }
127
+ const v = doc[k];
128
+ if (!(v instanceof NumericValue)) {
129
+ doc[k] = this._read(ns, v);
130
+ }
131
+ }
132
+ }
133
+ return value;
134
+ }
135
+ else {
136
+ return value;
137
+ }
138
+ }
139
+ return value;
140
+ }
141
+ _readStruct(ns, record) {
142
+ const union = ns.isUnionSchema();
143
+ const out = {};
144
+ let nameMap = void 0;
145
+ const { jsonName } = this.settings;
146
+ if (jsonName) {
147
+ nameMap = {};
148
+ }
149
+ let unionSerde;
150
+ if (union) {
151
+ unionSerde = new UnionSerde(record, out);
152
+ }
153
+ for (const [memberName, memberSchema] of ns.structIterator()) {
154
+ let fromKey = memberName;
155
+ if (jsonName) {
156
+ fromKey = memberSchema.getMergedTraits().jsonName ?? fromKey;
157
+ nameMap[fromKey] = memberName;
158
+ }
159
+ if (union) {
160
+ unionSerde.mark(fromKey);
161
+ }
162
+ if (record[fromKey] != null) {
163
+ out[memberName] = this._read(memberSchema, record[fromKey]);
164
+ }
165
+ }
166
+ if (union) {
167
+ unionSerde.writeUnknown();
168
+ }
169
+ else if (typeof record.__type === "string") {
170
+ for (const k in record) {
171
+ const v = record[k];
172
+ const t = jsonName ? (nameMap[k] ?? k) : k;
173
+ if (!(t in out)) {
174
+ out[t] = v;
175
+ }
176
+ }
177
+ }
178
+ return out;
179
+ }
180
+ }
@@ -0,0 +1,483 @@
1
+ import { determineTimestampFormat } from "@smithy/core/protocols";
2
+ import { NormalizedSchema } from "@smithy/core/schema";
3
+ import { dateToUtcString, generateIdempotencyToken, LazyJsonString, NumericValue, toBase64 } from "@smithy/core/serde";
4
+ import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
5
+ import { writeKey } from "../../writeKey";
6
+ const encoder = new TextEncoder();
7
+ const OPEN_BRACE = 0x7b;
8
+ const CLOSE_BRACE = 0x7d;
9
+ const OPEN_BRACKET = 0x5b;
10
+ const CLOSE_BRACKET = 0x5d;
11
+ const QUOTE = 0x22;
12
+ const COLON = 0x3a;
13
+ const COMMA = 0x2c;
14
+ const BACKSLASH = 0x5c;
15
+ const TRUE = new Uint8Array([0x74, 0x72, 0x75, 0x65]);
16
+ const FALSE = new Uint8Array([0x66, 0x61, 0x6c, 0x73, 0x65]);
17
+ const NULL = new Uint8Array([0x6e, 0x75, 0x6c, 0x6c]);
18
+ const ESCAPE_TABLE = new Array(128).fill(null);
19
+ ESCAPE_TABLE[0x08] = "b";
20
+ ESCAPE_TABLE[0x09] = "t";
21
+ ESCAPE_TABLE[0x0a] = "n";
22
+ ESCAPE_TABLE[0x0c] = "f";
23
+ ESCAPE_TABLE[0x0d] = "r";
24
+ ESCAPE_TABLE[0x22] = '"';
25
+ ESCAPE_TABLE[0x5c] = "\\";
26
+ for (let i = 0; i < 0x20; i++) {
27
+ if (ESCAPE_TABLE[i] === null) {
28
+ ESCAPE_TABLE[i] = "u00" + i.toString(16).padStart(2, "0");
29
+ }
30
+ }
31
+ const INITIAL_BUFFER_SIZE = 2048;
32
+ function alloc(size) {
33
+ return typeof Buffer !== "undefined" ? Buffer.allocUnsafe(size) : new Uint8Array(size);
34
+ }
35
+ export class ByteJsonShapeSerializer extends SerdeContextConfig {
36
+ settings;
37
+ json;
38
+ i = 0;
39
+ rootSchema;
40
+ constructor(settings) {
41
+ super();
42
+ this.settings = settings;
43
+ this.json = alloc(INITIAL_BUFFER_SIZE);
44
+ }
45
+ write(schema, value) {
46
+ this.i = 0;
47
+ this.rootSchema = NormalizedSchema.of(schema);
48
+ this.writeValue(this.rootSchema, value, undefined);
49
+ }
50
+ writeDiscriminatedDocument(schema, value) {
51
+ this.i = 0;
52
+ this.rootSchema = NormalizedSchema.of(schema);
53
+ const ns = this.rootSchema;
54
+ if (ns.isStructSchema() && value != null && typeof value === "object") {
55
+ this.ensure(2);
56
+ this.json[this.i++] = OPEN_BRACE;
57
+ this.writeAsciiQuoted("__type");
58
+ this.json[this.i++] = COLON;
59
+ this.writeAsciiQuoted(ns.getName(true) ?? "Unknown");
60
+ let wroteAny = true;
61
+ const { jsonName } = this.settings;
62
+ for (const [memberName, memberSchema] of ns.structIterator()) {
63
+ const item = value[memberName];
64
+ if (item == null && !memberSchema.isIdempotencyToken()) {
65
+ continue;
66
+ }
67
+ if (wroteAny) {
68
+ this.ensure(1);
69
+ this.json[this.i++] = COMMA;
70
+ }
71
+ const targetKey = jsonName ? (memberSchema.getMergedTraits().jsonName ?? memberName) : memberName;
72
+ this.writeAsciiQuoted(targetKey);
73
+ this.json[this.i++] = COLON;
74
+ this.writeValue(memberSchema, item, ns);
75
+ wroteAny = true;
76
+ }
77
+ this.ensure(1);
78
+ this.json[this.i++] = CLOSE_BRACE;
79
+ }
80
+ else {
81
+ this.writeValue(ns, value, undefined);
82
+ }
83
+ }
84
+ flush() {
85
+ this.rootSchema = undefined;
86
+ const finalPosition = this.i;
87
+ this.i = 0;
88
+ const result = this.json.subarray(0, finalPosition);
89
+ this.json = alloc(INITIAL_BUFFER_SIZE);
90
+ return result;
91
+ }
92
+ ensure(byteCount) {
93
+ const { i, json } = this;
94
+ if (i + byteCount > json.length) {
95
+ let newSize = json.length * 2;
96
+ while (newSize < i + byteCount) {
97
+ newSize *= 2;
98
+ }
99
+ const next = alloc(newSize);
100
+ next.set(this.json);
101
+ this.json = next;
102
+ }
103
+ }
104
+ writeAscii(s) {
105
+ const z = s.length;
106
+ this.ensure(z);
107
+ let { i, json } = this;
108
+ for (let j = 0; j < z; ++j) {
109
+ json[i] = s.charCodeAt(j);
110
+ i += 1;
111
+ }
112
+ this.i = i;
113
+ }
114
+ writeAsciiQuoted(s) {
115
+ const z = s.length;
116
+ this.ensure(z + 4);
117
+ let { json, i } = this;
118
+ json[i++] = QUOTE;
119
+ for (let j = 0; j < z; ++j) {
120
+ json[i++] = s.charCodeAt(j);
121
+ }
122
+ json[i++] = QUOTE;
123
+ this.i = i;
124
+ }
125
+ writeJsonString(s) {
126
+ this.ensure(s.length * 2 + 2);
127
+ this.json[this.i++] = QUOTE;
128
+ const z = s.length;
129
+ for (let j = 0; j < z; ++j) {
130
+ const c = s.charCodeAt(j);
131
+ if (c > 0x22 && c < 0x5c) {
132
+ this.json[this.i++] = c;
133
+ }
134
+ else if (c < 0x80) {
135
+ const esc = ESCAPE_TABLE[c];
136
+ if (esc !== null) {
137
+ this.ensure(esc.length + 1);
138
+ this.json[this.i++] = BACKSLASH;
139
+ for (let k = 0; k < esc.length; k++) {
140
+ this.json[this.i++] = esc.charCodeAt(k);
141
+ }
142
+ }
143
+ else {
144
+ this.json[this.i++] = c;
145
+ }
146
+ }
147
+ else if (c >= 0xd800 && c <= 0xdbff) {
148
+ const next = j + 1 < z ? s.charCodeAt(j + 1) : 0;
149
+ if (next >= 0xdc00 && next <= 0xdfff) {
150
+ this.ensure(4);
151
+ const { written } = encoder.encodeInto(s.substring(j, j + 2), this.json.subarray(this.i));
152
+ this.i += written;
153
+ j++;
154
+ }
155
+ else {
156
+ this.ensure(6);
157
+ this.writeUnicodeEscape(c);
158
+ }
159
+ }
160
+ else if (c >= 0xdc00 && c <= 0xdfff) {
161
+ this.ensure(6);
162
+ this.writeUnicodeEscape(c);
163
+ }
164
+ else {
165
+ let { i, json } = this;
166
+ if (c < 0x800) {
167
+ json[i++] = 0xc0 | (c >> 6);
168
+ json[i++] = 0x80 | (c & 0x3f);
169
+ }
170
+ else {
171
+ json[i++] = 0xe0 | (c >> 12);
172
+ json[i++] = 0x80 | ((c >> 6) & 0x3f);
173
+ json[i++] = 0x80 | (c & 0x3f);
174
+ }
175
+ this.i = i;
176
+ }
177
+ }
178
+ this.json[this.i++] = QUOTE;
179
+ }
180
+ writeUnicodeEscape(code) {
181
+ let { json, i } = this;
182
+ json[i++] = BACKSLASH;
183
+ json[i++] = 0x75;
184
+ const hex = code.toString(16).padStart(4, "0");
185
+ for (let j = 0; j < 4; ++j) {
186
+ json[i++] = hex.charCodeAt(j);
187
+ }
188
+ this.i = i;
189
+ }
190
+ static B64 = (() => {
191
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
192
+ const table = new Uint8Array(64);
193
+ for (let i = 0; i < 64; i++)
194
+ table[i] = chars.charCodeAt(i);
195
+ return table;
196
+ })();
197
+ writeBase64(data) {
198
+ const b64Len = Math.ceil(data.length / 3) * 4;
199
+ this.ensure(b64Len + 2);
200
+ const json = this.json;
201
+ const B64 = ByteJsonShapeSerializer.B64;
202
+ let i = this.i;
203
+ json[i++] = QUOTE;
204
+ const len = data.length;
205
+ const remainder = len % 3;
206
+ const mainLen = len - remainder;
207
+ for (let j = 0; j < mainLen; j += 3) {
208
+ const a = data[j];
209
+ const b = data[j + 1];
210
+ const c = data[j + 2];
211
+ json[i++] = B64[a >> 2];
212
+ json[i++] = B64[((a & 0x03) << 4) | (b >> 4)];
213
+ json[i++] = B64[((b & 0x0f) << 2) | (c >> 6)];
214
+ json[i++] = B64[c & 0x3f];
215
+ }
216
+ if (remainder === 2) {
217
+ const a = data[mainLen];
218
+ const b = data[mainLen + 1];
219
+ json[i++] = B64[a >> 2];
220
+ json[i++] = B64[((a & 0x03) << 4) | (b >> 4)];
221
+ json[i++] = B64[(b & 0x0f) << 2];
222
+ json[i++] = 0x3d;
223
+ }
224
+ else if (remainder === 1) {
225
+ const a = data[mainLen];
226
+ json[i++] = B64[a >> 2];
227
+ json[i++] = B64[(a & 0x03) << 4];
228
+ json[i++] = 0x3d;
229
+ json[i++] = 0x3d;
230
+ }
231
+ json[i++] = QUOTE;
232
+ this.i = i;
233
+ }
234
+ writeValue(schema, value, container) {
235
+ if (value == null) {
236
+ if (container?.isStructSchema()) {
237
+ if (value === undefined) {
238
+ const ns = NormalizedSchema.of(schema);
239
+ if (ns.isIdempotencyToken()) {
240
+ this.writeAsciiQuoted(generateIdempotencyToken());
241
+ return;
242
+ }
243
+ }
244
+ return;
245
+ }
246
+ this.ensure(4);
247
+ this.json.set(NULL, this.i);
248
+ this.i += 4;
249
+ return;
250
+ }
251
+ const ns = NormalizedSchema.of(schema);
252
+ const isObject = typeof value === "object";
253
+ if (isObject) {
254
+ if (ns.isStructSchema()) {
255
+ this.writeStruct(ns, value);
256
+ return;
257
+ }
258
+ if (Array.isArray(value) && (ns.isListSchema() || ns.isDocumentSchema())) {
259
+ this.writeList(ns, value, ns.isDocumentSchema());
260
+ return;
261
+ }
262
+ if (ns.isMapSchema()) {
263
+ this.writeMap(ns, value, false);
264
+ return;
265
+ }
266
+ if (value instanceof Uint8Array && (ns.isBlobSchema() || ns.isDocumentSchema())) {
267
+ this.writeBase64(value);
268
+ return;
269
+ }
270
+ if (value instanceof Date && (ns.isTimestampSchema() || ns.isDocumentSchema())) {
271
+ this.writeTimestamp(ns, value);
272
+ return;
273
+ }
274
+ if (value instanceof NumericValue) {
275
+ this.writeAscii(value.string);
276
+ return;
277
+ }
278
+ if (ns.isDocumentSchema()) {
279
+ if (Array.isArray(value)) {
280
+ this.writeList(ns, value, true);
281
+ }
282
+ else {
283
+ this.writeMap(ns, value, true);
284
+ }
285
+ return;
286
+ }
287
+ const json = JSON.stringify(value);
288
+ this.writeAscii(json);
289
+ return;
290
+ }
291
+ if (typeof value === "string") {
292
+ if (ns.isStringSchema()) {
293
+ const mediaType = ns.getMergedTraits().mediaType;
294
+ if (mediaType) {
295
+ const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
296
+ if (isJson) {
297
+ this.writeJsonString(LazyJsonString.from(value).toString());
298
+ return;
299
+ }
300
+ }
301
+ }
302
+ if (ns.isBlobSchema()) {
303
+ const b64 = (this.serdeContext?.base64Encoder ?? toBase64)(value);
304
+ this.writeAsciiQuoted(b64);
305
+ return;
306
+ }
307
+ this.writeJsonString(value);
308
+ return;
309
+ }
310
+ if (typeof value === "number") {
311
+ if (ns.isNumericSchema() && (Math.abs(value) === Infinity || isNaN(value))) {
312
+ this.writeAsciiQuoted(String(value));
313
+ return;
314
+ }
315
+ const numStr = String(value);
316
+ this.writeAscii(numStr);
317
+ return;
318
+ }
319
+ if (typeof value === "boolean") {
320
+ this.ensure(5);
321
+ if (value) {
322
+ this.json.set(TRUE, this.i);
323
+ this.i += 4;
324
+ }
325
+ else {
326
+ this.json.set(FALSE, this.i);
327
+ this.i += 5;
328
+ }
329
+ return;
330
+ }
331
+ if (typeof value === "bigint") {
332
+ this.writeAscii(value.toString());
333
+ return;
334
+ }
335
+ this.writeAscii(String(value));
336
+ }
337
+ writeStruct(ns, value) {
338
+ this.ensure(2);
339
+ this.json[this.i++] = OPEN_BRACE;
340
+ let first = true;
341
+ let wroteAny = false;
342
+ const hasType = typeof value.__type === "string";
343
+ let writtenKeys;
344
+ if (hasType) {
345
+ writtenKeys = new Set();
346
+ }
347
+ for (const [memberName, memberSchema] of ns.structIterator()) {
348
+ const item = value[memberName];
349
+ if (item == null && !memberSchema.isIdempotencyToken())
350
+ continue;
351
+ if (!first) {
352
+ this.ensure(1);
353
+ this.json[this.i++] = COMMA;
354
+ }
355
+ first = false;
356
+ wroteAny = true;
357
+ const targetKey = this.settings.jsonName ? (memberSchema.getMergedTraits().jsonName ?? memberName) : memberName;
358
+ if (writtenKeys) {
359
+ writtenKeys.add(memberName);
360
+ writtenKeys.add(targetKey);
361
+ }
362
+ this.writeAsciiQuoted(targetKey);
363
+ this.json[this.i++] = COLON;
364
+ this.writeValue(memberSchema, item, ns);
365
+ }
366
+ if (!wroteAny && ns.isUnionSchema()) {
367
+ const { $unknown } = value;
368
+ if (Array.isArray($unknown)) {
369
+ const [k, v] = $unknown;
370
+ this.writeAsciiQuoted(k);
371
+ this.ensure(1);
372
+ this.json[this.i++] = COLON;
373
+ this.writeValue(15, v, ns);
374
+ }
375
+ }
376
+ else if (hasType) {
377
+ for (const k in value) {
378
+ const targetKey = this.settings.jsonName ? (writtenKeys.has(k) ? k : k) : k;
379
+ if (writtenKeys.has(targetKey))
380
+ continue;
381
+ writtenKeys.add(targetKey);
382
+ const v = value[k];
383
+ if (!first) {
384
+ this.ensure(1);
385
+ this.json[this.i++] = COMMA;
386
+ }
387
+ first = false;
388
+ this.writeAsciiQuoted(targetKey);
389
+ this.ensure(1);
390
+ this.json[this.i++] = COLON;
391
+ this.writeValue(15, v, undefined);
392
+ }
393
+ }
394
+ this.ensure(1);
395
+ this.json[this.i++] = CLOSE_BRACE;
396
+ }
397
+ writeList(ns, value, isDocument) {
398
+ this.ensure(2);
399
+ this.json[this.i++] = OPEN_BRACKET;
400
+ const sparse = !!ns.getMergedTraits().sparse;
401
+ const valueSchema = ns.getValueSchema();
402
+ for (let i = 0; i < value.length; ++i) {
403
+ const item = value[i];
404
+ if (isDocument ? item === undefined : item == null && !sparse) {
405
+ continue;
406
+ }
407
+ if (i !== 0) {
408
+ this.ensure(1);
409
+ this.json[this.i++] = COMMA;
410
+ }
411
+ this.writeValue(valueSchema, item, undefined);
412
+ }
413
+ this.ensure(1);
414
+ this.json[this.i++] = CLOSE_BRACKET;
415
+ }
416
+ writeMap(ns, value, isDocument) {
417
+ const sparse = !!ns.getMergedTraits().sparse;
418
+ const valueSchema = ns.getValueSchema();
419
+ if (!isDocument) {
420
+ if (valueSchema.isStringSchema() || valueSchema.isNumericSchema() || valueSchema.isBooleanSchema()) {
421
+ let input = value;
422
+ if (sparse) {
423
+ input = {};
424
+ for (const k in value) {
425
+ if (k === "__proto__") {
426
+ writeKey(input);
427
+ }
428
+ input[k] = value[k] ?? null;
429
+ }
430
+ }
431
+ const json = JSON.stringify(input);
432
+ this.ensure(json.length * 3);
433
+ const { written } = encoder.encodeInto(json, this.json.subarray(this.i));
434
+ this.i += written;
435
+ return;
436
+ }
437
+ }
438
+ this.ensure(2);
439
+ this.json[this.i++] = OPEN_BRACE;
440
+ let first = true;
441
+ for (const k in value) {
442
+ const v = value[k];
443
+ if (isDocument ? v === undefined : v == null && !sparse) {
444
+ continue;
445
+ }
446
+ if (!first) {
447
+ this.ensure(1);
448
+ this.json[this.i++] = COMMA;
449
+ }
450
+ first = false;
451
+ this.writeJsonString(k);
452
+ this.ensure(1);
453
+ this.json[this.i++] = COLON;
454
+ this.writeValue(valueSchema, v, undefined);
455
+ }
456
+ this.ensure(1);
457
+ this.json[this.i++] = CLOSE_BRACE;
458
+ }
459
+ writeTimestamp(ns, value) {
460
+ const format = determineTimestampFormat(ns, this.settings);
461
+ switch (format) {
462
+ case 5: {
463
+ const iso = value.toISOString().replace(".000Z", "Z");
464
+ this.writeAsciiQuoted(iso);
465
+ return;
466
+ }
467
+ case 6: {
468
+ this.writeAsciiQuoted(dateToUtcString(value));
469
+ return;
470
+ }
471
+ case 7: {
472
+ const epochSecs = String(value.getTime() / 1000);
473
+ this.writeAscii(epochSecs);
474
+ return;
475
+ }
476
+ default: {
477
+ const epochSecs = String(value.getTime() / 1000);
478
+ this.writeAscii(epochSecs);
479
+ return;
480
+ }
481
+ }
482
+ }
483
+ }
@@ -0,0 +1,41 @@
1
+ import { NormalizedSchema } from "@smithy/core/schema";
2
+ const REVIVER_SYMBOL = Symbol.for("@aws-sdk/reviver");
3
+ export function needsReviver(schema) {
4
+ const ns = NormalizedSchema.of(schema);
5
+ const raw = ns.getSchema();
6
+ if (Array.isArray(raw) && ns.isStructSchema()) {
7
+ if (REVIVER_SYMBOL in raw) {
8
+ return raw[REVIVER_SYMBOL];
9
+ }
10
+ const result = _check(ns, new Set());
11
+ raw[REVIVER_SYMBOL] = result;
12
+ return result;
13
+ }
14
+ return _check(ns, new Set());
15
+ }
16
+ function _check(ns, seen) {
17
+ const raw = ns.getSchema();
18
+ if (seen.has(raw)) {
19
+ return false;
20
+ }
21
+ seen.add(raw);
22
+ if (ns.isBigIntegerSchema() || ns.isBigDecimalSchema()) {
23
+ return true;
24
+ }
25
+ if (ns.isStructSchema()) {
26
+ for (const [, memberSchema] of ns.structIterator()) {
27
+ if (_check(memberSchema, seen)) {
28
+ return true;
29
+ }
30
+ }
31
+ }
32
+ else if (ns.isListSchema() || ns.isMapSchema()) {
33
+ if (_check(ns.getValueSchema(), seen)) {
34
+ return true;
35
+ }
36
+ }
37
+ else if (ns.isDocumentSchema()) {
38
+ return true;
39
+ }
40
+ return false;
41
+ }