@smithy/core 3.29.8 → 3.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-cjs/submodules/cbor/index.js +15 -13
- package/dist-cjs/submodules/retry/index.browser.js +1 -0
- package/dist-cjs/submodules/retry/index.js +1 -0
- package/dist-cjs/submodules/schema/index.js +3 -0
- package/dist-cjs/submodules/serde/index.browser.js +2 -2
- package/dist-cjs/submodules/serde/index.js +2 -2
- package/dist-cjs/submodules/serde/index.native.js +2 -2
- package/dist-es/submodules/cbor/CborCodec.js +2 -203
- package/dist-es/submodules/cbor/codec-v1/CborShapeDeserializer.js +113 -0
- package/dist-es/submodules/cbor/codec-v1/CborShapeSerializer.js +95 -0
- package/dist-es/submodules/cbor/codec-v2/SinglePassCborShapeDeserializer.js +609 -0
- package/dist-es/submodules/cbor/codec-v2/SinglePassCborShapeSerializer.js +611 -0
- package/dist-es/submodules/cbor/index.js +3 -1
- package/dist-es/submodules/retry/service-error-classification/constants.js +1 -0
- package/dist-es/submodules/schema/schemas/NormalizedSchema.js +3 -0
- package/dist-es/submodules/serde/value/NumericValue.js +2 -2
- package/dist-types/submodules/cbor/CborCodec.d.ts +3 -25
- package/dist-types/submodules/cbor/SmithyRpcV2CborProtocol.d.ts +2 -2
- package/dist-types/submodules/cbor/codec-v1/CborShapeDeserializer.d.ts +13 -0
- package/dist-types/submodules/cbor/codec-v1/CborShapeSerializer.d.ts +15 -0
- package/dist-types/submodules/cbor/codec-v2/SinglePassCborShapeDeserializer.d.ts +19 -0
- package/dist-types/submodules/cbor/codec-v2/SinglePassCborShapeSerializer.d.ts +22 -0
- package/dist-types/submodules/cbor/index.d.ts +3 -1
- package/dist-types/submodules/retry/service-error-classification/constants.d.ts +1 -0
- package/dist-types/submodules/schema/schemas/NormalizedSchema.d.ts +12 -0
- package/dist-types/ts3.4/submodules/cbor/CborCodec.d.ts +3 -25
- package/dist-types/ts3.4/submodules/cbor/SmithyRpcV2CborProtocol.d.ts +2 -2
- package/dist-types/ts3.4/submodules/cbor/codec-v1/CborShapeDeserializer.d.ts +13 -0
- package/dist-types/ts3.4/submodules/cbor/codec-v1/CborShapeSerializer.d.ts +15 -0
- package/dist-types/ts3.4/submodules/cbor/codec-v2/SinglePassCborShapeDeserializer.d.ts +19 -0
- package/dist-types/ts3.4/submodules/cbor/codec-v2/SinglePassCborShapeSerializer.d.ts +22 -0
- package/dist-types/ts3.4/submodules/cbor/index.d.ts +3 -1
- package/dist-types/ts3.4/submodules/retry/service-error-classification/constants.d.ts +1 -0
- package/dist-types/ts3.4/submodules/schema/schemas/NormalizedSchema.d.ts +12 -0
- package/package.json +2 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { nv, NumericValue, calculateBodyLength,
|
|
1
|
+
const { nv, NumericValue, calculateBodyLength, generateIdempotencyToken, fromBase64, _parseEpochTimestamp } = require("@smithy/core/serde");
|
|
2
2
|
const { HttpRequest, collectBody, SerdeContext, RpcProtocol } = require("@smithy/core/protocols");
|
|
3
3
|
const { NormalizedSchema, deref, TypeRegistry } = require("@smithy/core/schema");
|
|
4
4
|
const { getSmithyContext } = require("@smithy/core/transport");
|
|
@@ -933,18 +933,6 @@ const buildHttpRpcRequest = async (context, headers, path, resolvedHostname, bod
|
|
|
933
933
|
return new HttpRequest(contents);
|
|
934
934
|
};
|
|
935
935
|
|
|
936
|
-
class CborCodec extends SerdeContext {
|
|
937
|
-
createSerializer() {
|
|
938
|
-
const serializer = new CborShapeSerializer();
|
|
939
|
-
serializer.setSerdeContext(this.serdeContext);
|
|
940
|
-
return serializer;
|
|
941
|
-
}
|
|
942
|
-
createDeserializer() {
|
|
943
|
-
const deserializer = new CborShapeDeserializer();
|
|
944
|
-
deserializer.setSerdeContext(this.serdeContext);
|
|
945
|
-
return deserializer;
|
|
946
|
-
}
|
|
947
|
-
}
|
|
948
936
|
class CborShapeSerializer extends SerdeContext {
|
|
949
937
|
value;
|
|
950
938
|
write(schema, value) {
|
|
@@ -1035,6 +1023,7 @@ class CborShapeSerializer extends SerdeContext {
|
|
|
1035
1023
|
return buffer;
|
|
1036
1024
|
}
|
|
1037
1025
|
}
|
|
1026
|
+
|
|
1038
1027
|
class CborShapeDeserializer extends SerdeContext {
|
|
1039
1028
|
read(schema, bytes) {
|
|
1040
1029
|
const data = cbor.deserialize(bytes);
|
|
@@ -1145,6 +1134,19 @@ class CborShapeDeserializer extends SerdeContext {
|
|
|
1145
1134
|
}
|
|
1146
1135
|
}
|
|
1147
1136
|
|
|
1137
|
+
class CborCodec extends SerdeContext {
|
|
1138
|
+
createSerializer() {
|
|
1139
|
+
const serializer = new CborShapeSerializer();
|
|
1140
|
+
serializer.setSerdeContext(this.serdeContext);
|
|
1141
|
+
return serializer;
|
|
1142
|
+
}
|
|
1143
|
+
createDeserializer() {
|
|
1144
|
+
const deserializer = new CborShapeDeserializer();
|
|
1145
|
+
deserializer.setSerdeContext(this.serdeContext);
|
|
1146
|
+
return deserializer;
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1148
1150
|
class SmithyRpcV2CborProtocol extends RpcProtocol {
|
|
1149
1151
|
codec = new CborCodec();
|
|
1150
1152
|
serializer = this.codec.createSerializer();
|
|
@@ -5,6 +5,7 @@ const { parseRfc7231DateTime, v4 } = require("@smithy/core/serde");
|
|
|
5
5
|
const isStreamingPayload = (request) => request?.body instanceof ReadableStream;
|
|
6
6
|
|
|
7
7
|
const CLOCK_SKEW_ERROR_CODES = [
|
|
8
|
+
"AccessDeniedException",
|
|
8
9
|
"AuthFailure",
|
|
9
10
|
"InvalidSignatureException",
|
|
10
11
|
"RequestExpired",
|
|
@@ -7,6 +7,7 @@ const isStreamingPayload = (request) => request?.body instanceof Readable ||
|
|
|
7
7
|
(typeof ReadableStream !== "undefined" && request?.body instanceof ReadableStream);
|
|
8
8
|
|
|
9
9
|
const CLOCK_SKEW_ERROR_CODES = [
|
|
10
|
+
"AccessDeniedException",
|
|
10
11
|
"AuthFailure",
|
|
11
12
|
"InvalidSignatureException",
|
|
12
13
|
"RequestExpired",
|
|
@@ -529,6 +529,9 @@ class NormalizedSchema {
|
|
|
529
529
|
}
|
|
530
530
|
struct[anno.it] = it;
|
|
531
531
|
}
|
|
532
|
+
structIteratorCbor() {
|
|
533
|
+
throw new Error("@smithy/core/schema - structIteratorCbor not loaded.");
|
|
534
|
+
}
|
|
532
535
|
}
|
|
533
536
|
function member(memberSchema, memberName) {
|
|
534
537
|
if (memberSchema instanceof NormalizedSchema) {
|
|
@@ -775,7 +775,7 @@ const splitHeader = (value) => {
|
|
|
775
775
|
});
|
|
776
776
|
};
|
|
777
777
|
|
|
778
|
-
const format =
|
|
778
|
+
const format = /^-?((0|[1-9]\d*)(\.\d+)?|\.\d+)([eE][+-]?\d+)?$/;
|
|
779
779
|
class NumericValue {
|
|
780
780
|
string;
|
|
781
781
|
type;
|
|
@@ -783,7 +783,7 @@ class NumericValue {
|
|
|
783
783
|
this.string = string;
|
|
784
784
|
this.type = type;
|
|
785
785
|
if (!format.test(string)) {
|
|
786
|
-
throw new Error(`@smithy/core/serde - NumericValue must
|
|
786
|
+
throw new Error(`@smithy/core/serde - NumericValue string must conform to the Smithy bigDecimal format. Received: "${string}"`);
|
|
787
787
|
}
|
|
788
788
|
}
|
|
789
789
|
toString() {
|
|
@@ -743,7 +743,7 @@ const splitHeader = (value) => {
|
|
|
743
743
|
});
|
|
744
744
|
};
|
|
745
745
|
|
|
746
|
-
const format =
|
|
746
|
+
const format = /^-?((0|[1-9]\d*)(\.\d+)?|\.\d+)([eE][+-]?\d+)?$/;
|
|
747
747
|
class NumericValue {
|
|
748
748
|
string;
|
|
749
749
|
type;
|
|
@@ -751,7 +751,7 @@ class NumericValue {
|
|
|
751
751
|
this.string = string;
|
|
752
752
|
this.type = type;
|
|
753
753
|
if (!format.test(string)) {
|
|
754
|
-
throw new Error(`@smithy/core/serde - NumericValue must
|
|
754
|
+
throw new Error(`@smithy/core/serde - NumericValue string must conform to the Smithy bigDecimal format. Received: "${string}"`);
|
|
755
755
|
}
|
|
756
756
|
}
|
|
757
757
|
toString() {
|
|
@@ -775,7 +775,7 @@ const splitHeader = (value) => {
|
|
|
775
775
|
});
|
|
776
776
|
};
|
|
777
777
|
|
|
778
|
-
const format =
|
|
778
|
+
const format = /^-?((0|[1-9]\d*)(\.\d+)?|\.\d+)([eE][+-]?\d+)?$/;
|
|
779
779
|
class NumericValue {
|
|
780
780
|
string;
|
|
781
781
|
type;
|
|
@@ -783,7 +783,7 @@ class NumericValue {
|
|
|
783
783
|
this.string = string;
|
|
784
784
|
this.type = type;
|
|
785
785
|
if (!format.test(string)) {
|
|
786
|
-
throw new Error(`@smithy/core/serde - NumericValue must
|
|
786
|
+
throw new Error(`@smithy/core/serde - NumericValue string must conform to the Smithy bigDecimal format. Received: "${string}"`);
|
|
787
787
|
}
|
|
788
788
|
}
|
|
789
789
|
toString() {
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { SerdeContext } from "@smithy/core/protocols";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { cbor } from "./cbor";
|
|
5
|
-
import { dateToTag } from "./parseCborBody";
|
|
2
|
+
import { CborShapeSerializer } from "./codec-v1/CborShapeSerializer";
|
|
3
|
+
import { CborShapeDeserializer } from "./codec-v1/CborShapeDeserializer";
|
|
6
4
|
export class CborCodec extends SerdeContext {
|
|
7
5
|
createSerializer() {
|
|
8
6
|
const serializer = new CborShapeSerializer();
|
|
@@ -15,202 +13,3 @@ export class CborCodec extends SerdeContext {
|
|
|
15
13
|
return deserializer;
|
|
16
14
|
}
|
|
17
15
|
}
|
|
18
|
-
export class CborShapeSerializer extends SerdeContext {
|
|
19
|
-
value;
|
|
20
|
-
write(schema, value) {
|
|
21
|
-
this.value = this.serialize(schema, value);
|
|
22
|
-
}
|
|
23
|
-
serialize(schema, source) {
|
|
24
|
-
const ns = NormalizedSchema.of(schema);
|
|
25
|
-
if (source == null) {
|
|
26
|
-
if (ns.isIdempotencyToken()) {
|
|
27
|
-
return generateIdempotencyToken();
|
|
28
|
-
}
|
|
29
|
-
return source;
|
|
30
|
-
}
|
|
31
|
-
if (ns.isBlobSchema()) {
|
|
32
|
-
if (typeof source === "string") {
|
|
33
|
-
return (this.serdeContext?.base64Decoder ?? fromBase64)(source);
|
|
34
|
-
}
|
|
35
|
-
return source;
|
|
36
|
-
}
|
|
37
|
-
if (ns.isTimestampSchema()) {
|
|
38
|
-
if (typeof source === "number" || typeof source === "bigint") {
|
|
39
|
-
return dateToTag(new Date((Number(source) / 1000) | 0));
|
|
40
|
-
}
|
|
41
|
-
return dateToTag(source);
|
|
42
|
-
}
|
|
43
|
-
if (typeof source === "function" || typeof source === "object") {
|
|
44
|
-
const sourceObject = source;
|
|
45
|
-
if (ns.isListSchema() && Array.isArray(sourceObject)) {
|
|
46
|
-
const sparse = !!ns.getMergedTraits().sparse;
|
|
47
|
-
const newArray = [];
|
|
48
|
-
let i = 0;
|
|
49
|
-
for (const item of sourceObject) {
|
|
50
|
-
const value = this.serialize(ns.getValueSchema(), item);
|
|
51
|
-
if (value != null || sparse) {
|
|
52
|
-
newArray[i++] = value;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return newArray;
|
|
56
|
-
}
|
|
57
|
-
if (sourceObject instanceof Date) {
|
|
58
|
-
return dateToTag(sourceObject);
|
|
59
|
-
}
|
|
60
|
-
const newObject = {};
|
|
61
|
-
if (ns.isMapSchema()) {
|
|
62
|
-
const sparse = !!ns.getMergedTraits().sparse;
|
|
63
|
-
for (const key in sourceObject) {
|
|
64
|
-
const value = this.serialize(ns.getValueSchema(), sourceObject[key]);
|
|
65
|
-
if (value != null || sparse) {
|
|
66
|
-
newObject[key] = value;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
else if (ns.isStructSchema()) {
|
|
71
|
-
for (const [key, memberSchema] of ns.structIterator()) {
|
|
72
|
-
const value = this.serialize(memberSchema, sourceObject[key]);
|
|
73
|
-
if (value != null) {
|
|
74
|
-
newObject[key] = value;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const isUnion = ns.isUnionSchema();
|
|
78
|
-
if (isUnion && Array.isArray(sourceObject.$unknown)) {
|
|
79
|
-
const [k, v] = sourceObject.$unknown;
|
|
80
|
-
newObject[k] = v;
|
|
81
|
-
}
|
|
82
|
-
else if (typeof sourceObject.__type === "string") {
|
|
83
|
-
for (const k in sourceObject) {
|
|
84
|
-
if (!(k in newObject)) {
|
|
85
|
-
newObject[k] = this.serialize(15, sourceObject[k]);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
else if (ns.isDocumentSchema()) {
|
|
91
|
-
for (const key in sourceObject) {
|
|
92
|
-
newObject[key] = this.serialize(ns.getValueSchema(), sourceObject[key]);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
else if (ns.isBigDecimalSchema()) {
|
|
96
|
-
return sourceObject;
|
|
97
|
-
}
|
|
98
|
-
return newObject;
|
|
99
|
-
}
|
|
100
|
-
return source;
|
|
101
|
-
}
|
|
102
|
-
flush() {
|
|
103
|
-
const buffer = cbor.serialize(this.value);
|
|
104
|
-
this.value = undefined;
|
|
105
|
-
return buffer;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
export class CborShapeDeserializer extends SerdeContext {
|
|
109
|
-
read(schema, bytes) {
|
|
110
|
-
const data = cbor.deserialize(bytes);
|
|
111
|
-
return this.readValue(schema, data);
|
|
112
|
-
}
|
|
113
|
-
readValue(_schema, value) {
|
|
114
|
-
const ns = NormalizedSchema.of(_schema);
|
|
115
|
-
if (ns.isTimestampSchema()) {
|
|
116
|
-
if (typeof value === "number") {
|
|
117
|
-
return _parseEpochTimestamp(value);
|
|
118
|
-
}
|
|
119
|
-
if (typeof value === "object") {
|
|
120
|
-
if (value.tag === 1 && "value" in value) {
|
|
121
|
-
return _parseEpochTimestamp(value.value);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (ns.isBlobSchema()) {
|
|
126
|
-
if (typeof value === "string") {
|
|
127
|
-
return (this.serdeContext?.base64Decoder ?? fromBase64)(value);
|
|
128
|
-
}
|
|
129
|
-
return value;
|
|
130
|
-
}
|
|
131
|
-
if (typeof value === "undefined" ||
|
|
132
|
-
typeof value === "boolean" ||
|
|
133
|
-
typeof value === "number" ||
|
|
134
|
-
typeof value === "string" ||
|
|
135
|
-
typeof value === "bigint" ||
|
|
136
|
-
typeof value === "symbol") {
|
|
137
|
-
return value;
|
|
138
|
-
}
|
|
139
|
-
else if (typeof value === "object") {
|
|
140
|
-
if (value === null) {
|
|
141
|
-
return null;
|
|
142
|
-
}
|
|
143
|
-
if ("byteLength" in value) {
|
|
144
|
-
return value;
|
|
145
|
-
}
|
|
146
|
-
if (value instanceof Date) {
|
|
147
|
-
return value;
|
|
148
|
-
}
|
|
149
|
-
if (ns.isDocumentSchema()) {
|
|
150
|
-
return value;
|
|
151
|
-
}
|
|
152
|
-
if (ns.isListSchema()) {
|
|
153
|
-
const newArray = [];
|
|
154
|
-
const memberSchema = ns.getValueSchema();
|
|
155
|
-
for (const item of value) {
|
|
156
|
-
const itemValue = this.readValue(memberSchema, item);
|
|
157
|
-
newArray.push(itemValue);
|
|
158
|
-
}
|
|
159
|
-
return newArray;
|
|
160
|
-
}
|
|
161
|
-
const newObject = {};
|
|
162
|
-
if (ns.isMapSchema()) {
|
|
163
|
-
const targetSchema = ns.getValueSchema();
|
|
164
|
-
for (const key in value) {
|
|
165
|
-
const itemValue = this.readValue(targetSchema, value[key]);
|
|
166
|
-
newObject[key] = itemValue;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
else if (ns.isStructSchema()) {
|
|
170
|
-
const isUnion = ns.isUnionSchema();
|
|
171
|
-
let keys;
|
|
172
|
-
if (isUnion) {
|
|
173
|
-
keys = new Set();
|
|
174
|
-
for (const k in value) {
|
|
175
|
-
if (k !== "__type") {
|
|
176
|
-
keys.add(k);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
for (const [key, memberSchema] of ns.structIterator()) {
|
|
181
|
-
if (isUnion) {
|
|
182
|
-
keys.delete(key);
|
|
183
|
-
}
|
|
184
|
-
if (value[key] != null) {
|
|
185
|
-
newObject[key] = this.readValue(memberSchema, value[key]);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
if (isUnion && keys?.size === 1) {
|
|
189
|
-
let newObjectEmpty = true;
|
|
190
|
-
for (const _ in newObject) {
|
|
191
|
-
newObjectEmpty = false;
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
if (newObjectEmpty) {
|
|
195
|
-
const k = keys.values().next().value;
|
|
196
|
-
newObject.$unknown = [k, value[k]];
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
else if (typeof value.__type === "string") {
|
|
200
|
-
for (const k in value) {
|
|
201
|
-
if (!(k in newObject)) {
|
|
202
|
-
newObject[k] = value[k];
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
else if (value instanceof NumericValue) {
|
|
208
|
-
return value;
|
|
209
|
-
}
|
|
210
|
-
return newObject;
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
return value;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { SerdeContext } from "@smithy/core/protocols";
|
|
2
|
+
import { NormalizedSchema } from "@smithy/core/schema";
|
|
3
|
+
import { NumericValue, _parseEpochTimestamp, fromBase64 } from "@smithy/core/serde";
|
|
4
|
+
import { cbor } from "../cbor";
|
|
5
|
+
export class CborShapeDeserializer extends SerdeContext {
|
|
6
|
+
read(schema, bytes) {
|
|
7
|
+
const data = cbor.deserialize(bytes);
|
|
8
|
+
return this.readValue(schema, data);
|
|
9
|
+
}
|
|
10
|
+
readValue(_schema, value) {
|
|
11
|
+
const ns = NormalizedSchema.of(_schema);
|
|
12
|
+
if (ns.isTimestampSchema()) {
|
|
13
|
+
if (typeof value === "number") {
|
|
14
|
+
return _parseEpochTimestamp(value);
|
|
15
|
+
}
|
|
16
|
+
if (typeof value === "object") {
|
|
17
|
+
if (value.tag === 1 && "value" in value) {
|
|
18
|
+
return _parseEpochTimestamp(value.value);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (ns.isBlobSchema()) {
|
|
23
|
+
if (typeof value === "string") {
|
|
24
|
+
return (this.serdeContext?.base64Decoder ?? fromBase64)(value);
|
|
25
|
+
}
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
if (typeof value === "undefined" ||
|
|
29
|
+
typeof value === "boolean" ||
|
|
30
|
+
typeof value === "number" ||
|
|
31
|
+
typeof value === "string" ||
|
|
32
|
+
typeof value === "bigint" ||
|
|
33
|
+
typeof value === "symbol") {
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
else if (typeof value === "object") {
|
|
37
|
+
if (value === null) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
if ("byteLength" in value) {
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
if (value instanceof Date) {
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
if (ns.isDocumentSchema()) {
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
if (ns.isListSchema()) {
|
|
50
|
+
const newArray = [];
|
|
51
|
+
const memberSchema = ns.getValueSchema();
|
|
52
|
+
for (const item of value) {
|
|
53
|
+
const itemValue = this.readValue(memberSchema, item);
|
|
54
|
+
newArray.push(itemValue);
|
|
55
|
+
}
|
|
56
|
+
return newArray;
|
|
57
|
+
}
|
|
58
|
+
const newObject = {};
|
|
59
|
+
if (ns.isMapSchema()) {
|
|
60
|
+
const targetSchema = ns.getValueSchema();
|
|
61
|
+
for (const key in value) {
|
|
62
|
+
const itemValue = this.readValue(targetSchema, value[key]);
|
|
63
|
+
newObject[key] = itemValue;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else if (ns.isStructSchema()) {
|
|
67
|
+
const isUnion = ns.isUnionSchema();
|
|
68
|
+
let keys;
|
|
69
|
+
if (isUnion) {
|
|
70
|
+
keys = new Set();
|
|
71
|
+
for (const k in value) {
|
|
72
|
+
if (k !== "__type") {
|
|
73
|
+
keys.add(k);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (const [key, memberSchema] of ns.structIterator()) {
|
|
78
|
+
if (isUnion) {
|
|
79
|
+
keys.delete(key);
|
|
80
|
+
}
|
|
81
|
+
if (value[key] != null) {
|
|
82
|
+
newObject[key] = this.readValue(memberSchema, value[key]);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (isUnion && keys?.size === 1) {
|
|
86
|
+
let newObjectEmpty = true;
|
|
87
|
+
for (const _ in newObject) {
|
|
88
|
+
newObjectEmpty = false;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
if (newObjectEmpty) {
|
|
92
|
+
const k = keys.values().next().value;
|
|
93
|
+
newObject.$unknown = [k, value[k]];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else if (typeof value.__type === "string") {
|
|
97
|
+
for (const k in value) {
|
|
98
|
+
if (!(k in newObject)) {
|
|
99
|
+
newObject[k] = value[k];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else if (value instanceof NumericValue) {
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
return newObject;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { SerdeContext } from "@smithy/core/protocols";
|
|
2
|
+
import { NormalizedSchema } from "@smithy/core/schema";
|
|
3
|
+
import { fromBase64, generateIdempotencyToken } from "@smithy/core/serde";
|
|
4
|
+
import { cbor } from "../cbor";
|
|
5
|
+
import { dateToTag } from "../parseCborBody";
|
|
6
|
+
export class CborShapeSerializer extends SerdeContext {
|
|
7
|
+
value;
|
|
8
|
+
write(schema, value) {
|
|
9
|
+
this.value = this.serialize(schema, value);
|
|
10
|
+
}
|
|
11
|
+
serialize(schema, source) {
|
|
12
|
+
const ns = NormalizedSchema.of(schema);
|
|
13
|
+
if (source == null) {
|
|
14
|
+
if (ns.isIdempotencyToken()) {
|
|
15
|
+
return generateIdempotencyToken();
|
|
16
|
+
}
|
|
17
|
+
return source;
|
|
18
|
+
}
|
|
19
|
+
if (ns.isBlobSchema()) {
|
|
20
|
+
if (typeof source === "string") {
|
|
21
|
+
return (this.serdeContext?.base64Decoder ?? fromBase64)(source);
|
|
22
|
+
}
|
|
23
|
+
return source;
|
|
24
|
+
}
|
|
25
|
+
if (ns.isTimestampSchema()) {
|
|
26
|
+
if (typeof source === "number" || typeof source === "bigint") {
|
|
27
|
+
return dateToTag(new Date((Number(source) / 1000) | 0));
|
|
28
|
+
}
|
|
29
|
+
return dateToTag(source);
|
|
30
|
+
}
|
|
31
|
+
if (typeof source === "function" || typeof source === "object") {
|
|
32
|
+
const sourceObject = source;
|
|
33
|
+
if (ns.isListSchema() && Array.isArray(sourceObject)) {
|
|
34
|
+
const sparse = !!ns.getMergedTraits().sparse;
|
|
35
|
+
const newArray = [];
|
|
36
|
+
let i = 0;
|
|
37
|
+
for (const item of sourceObject) {
|
|
38
|
+
const value = this.serialize(ns.getValueSchema(), item);
|
|
39
|
+
if (value != null || sparse) {
|
|
40
|
+
newArray[i++] = value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return newArray;
|
|
44
|
+
}
|
|
45
|
+
if (sourceObject instanceof Date) {
|
|
46
|
+
return dateToTag(sourceObject);
|
|
47
|
+
}
|
|
48
|
+
const newObject = {};
|
|
49
|
+
if (ns.isMapSchema()) {
|
|
50
|
+
const sparse = !!ns.getMergedTraits().sparse;
|
|
51
|
+
for (const key in sourceObject) {
|
|
52
|
+
const value = this.serialize(ns.getValueSchema(), sourceObject[key]);
|
|
53
|
+
if (value != null || sparse) {
|
|
54
|
+
newObject[key] = value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else if (ns.isStructSchema()) {
|
|
59
|
+
for (const [key, memberSchema] of ns.structIterator()) {
|
|
60
|
+
const value = this.serialize(memberSchema, sourceObject[key]);
|
|
61
|
+
if (value != null) {
|
|
62
|
+
newObject[key] = value;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const isUnion = ns.isUnionSchema();
|
|
66
|
+
if (isUnion && Array.isArray(sourceObject.$unknown)) {
|
|
67
|
+
const [k, v] = sourceObject.$unknown;
|
|
68
|
+
newObject[k] = v;
|
|
69
|
+
}
|
|
70
|
+
else if (typeof sourceObject.__type === "string") {
|
|
71
|
+
for (const k in sourceObject) {
|
|
72
|
+
if (!(k in newObject)) {
|
|
73
|
+
newObject[k] = this.serialize(15, sourceObject[k]);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else if (ns.isDocumentSchema()) {
|
|
79
|
+
for (const key in sourceObject) {
|
|
80
|
+
newObject[key] = this.serialize(ns.getValueSchema(), sourceObject[key]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else if (ns.isBigDecimalSchema()) {
|
|
84
|
+
return sourceObject;
|
|
85
|
+
}
|
|
86
|
+
return newObject;
|
|
87
|
+
}
|
|
88
|
+
return source;
|
|
89
|
+
}
|
|
90
|
+
flush() {
|
|
91
|
+
const buffer = cbor.serialize(this.value);
|
|
92
|
+
this.value = undefined;
|
|
93
|
+
return buffer;
|
|
94
|
+
}
|
|
95
|
+
}
|