@smithy/core 3.7.2 → 3.9.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 +121 -53
- package/dist-cjs/submodules/event-streams/index.js +261 -0
- package/dist-cjs/submodules/protocols/index.js +197 -104
- package/dist-cjs/submodules/schema/index.js +37 -0
- package/dist-cjs/submodules/serde/index.js +6 -51
- package/dist-es/submodules/cbor/CborCodec.js +77 -41
- package/dist-es/submodules/cbor/SmithyRpcV2CborProtocol.js +36 -15
- package/dist-es/submodules/event-streams/EventStreamSerde.js +200 -0
- package/dist-es/submodules/event-streams/index.js +1 -0
- package/dist-es/submodules/protocols/HttpBindingProtocol.js +86 -6
- package/dist-es/submodules/protocols/HttpProtocol.js +42 -94
- package/dist-es/submodules/protocols/RpcProtocol.js +36 -10
- package/dist-es/submodules/protocols/index.js +1 -0
- package/dist-es/submodules/schema/schemas/NormalizedSchema.js +26 -0
- package/dist-es/submodules/serde/copyDocumentWithTransform.js +1 -53
- package/dist-es/submodules/serde/generateIdempotencyToken.js +2 -0
- package/dist-es/submodules/serde/index.js +1 -0
- package/dist-types/submodules/cbor/CborCodec.d.ts +11 -2
- package/dist-types/submodules/cbor/SmithyRpcV2CborProtocol.d.ts +1 -0
- package/dist-types/submodules/cbor/parseCborBody.d.ts +1 -1
- package/dist-types/submodules/event-streams/EventStreamSerde.d.ts +60 -0
- package/dist-types/submodules/event-streams/index.d.ts +1 -0
- package/dist-types/submodules/protocols/HttpBindingProtocol.d.ts +9 -1
- package/dist-types/submodules/protocols/HttpProtocol.d.ts +42 -2
- package/dist-types/submodules/protocols/RpcProtocol.d.ts +1 -1
- package/dist-types/submodules/protocols/index.d.ts +1 -0
- package/dist-types/submodules/protocols/serde/FromStringShapeDeserializer.d.ts +1 -1
- package/dist-types/submodules/protocols/serde/HttpInterceptingShapeDeserializer.d.ts +1 -1
- package/dist-types/submodules/protocols/serde/HttpInterceptingShapeSerializer.d.ts +1 -1
- package/dist-types/submodules/protocols/serde/ToStringShapeSerializer.d.ts +1 -1
- package/dist-types/submodules/protocols/serde/determineTimestampFormat.d.ts +1 -1
- package/dist-types/submodules/schema/middleware/getSchemaSerdePlugin.d.ts +1 -1
- package/dist-types/submodules/schema/middleware/schemaDeserializationMiddleware.d.ts +1 -1
- package/dist-types/submodules/schema/schemas/NormalizedSchema.d.ts +15 -0
- package/dist-types/submodules/schema/schemas/SimpleSchema.d.ts +1 -1
- package/dist-types/submodules/schema/schemas/sentinels.d.ts +1 -1
- package/dist-types/submodules/serde/copyDocumentWithTransform.d.ts +2 -1
- package/dist-types/submodules/serde/generateIdempotencyToken.d.ts +2 -0
- package/dist-types/submodules/serde/index.d.ts +1 -0
- package/dist-types/ts3.4/submodules/cbor/CborCodec.d.ts +10 -1
- package/dist-types/ts3.4/submodules/cbor/SmithyRpcV2CborProtocol.d.ts +1 -0
- package/dist-types/ts3.4/submodules/event-streams/EventStreamSerde.d.ts +60 -0
- package/dist-types/ts3.4/submodules/event-streams/index.d.ts +1 -0
- package/dist-types/ts3.4/submodules/protocols/HttpBindingProtocol.d.ts +9 -1
- package/dist-types/ts3.4/submodules/protocols/HttpProtocol.d.ts +42 -2
- package/dist-types/ts3.4/submodules/protocols/index.d.ts +1 -0
- package/dist-types/ts3.4/submodules/protocols/serde/determineTimestampFormat.d.ts +1 -1
- package/dist-types/ts3.4/submodules/schema/schemas/NormalizedSchema.d.ts +15 -0
- package/dist-types/ts3.4/submodules/serde/copyDocumentWithTransform.d.ts +1 -0
- package/dist-types/ts3.4/submodules/serde/generateIdempotencyToken.d.ts +2 -0
- package/dist-types/ts3.4/submodules/serde/index.d.ts +1 -0
- package/event-streams.d.ts +7 -0
- package/event-streams.js +6 -0
- package/package.json +19 -7
|
@@ -792,6 +792,7 @@ var import_util_middleware = require("@smithy/util-middleware");
|
|
|
792
792
|
// src/submodules/cbor/CborCodec.ts
|
|
793
793
|
var import_schema = require("@smithy/core/schema");
|
|
794
794
|
var import_serde3 = require("@smithy/core/serde");
|
|
795
|
+
var import_util_base64 = require("@smithy/util-base64");
|
|
795
796
|
var CborCodec = class {
|
|
796
797
|
createSerializer() {
|
|
797
798
|
const serializer = new CborShapeSerializer();
|
|
@@ -812,35 +813,73 @@ var CborShapeSerializer = class {
|
|
|
812
813
|
this.serdeContext = serdeContext;
|
|
813
814
|
}
|
|
814
815
|
write(schema, value) {
|
|
815
|
-
this.value =
|
|
816
|
-
|
|
817
|
-
|
|
816
|
+
this.value = this.serialize(schema, value);
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Recursive serializer transform that copies and prepares the user input object
|
|
820
|
+
* for CBOR serialization.
|
|
821
|
+
*/
|
|
822
|
+
serialize(schema, source) {
|
|
823
|
+
const ns = import_schema.NormalizedSchema.of(schema);
|
|
824
|
+
if (source == null) {
|
|
825
|
+
if (ns.isIdempotencyToken()) {
|
|
826
|
+
return (0, import_serde3.generateIdempotencyToken)();
|
|
818
827
|
}
|
|
819
|
-
|
|
820
|
-
|
|
828
|
+
return source;
|
|
829
|
+
}
|
|
830
|
+
if (ns.isBlobSchema()) {
|
|
831
|
+
if (typeof source === "string") {
|
|
832
|
+
return (this.serdeContext?.base64Decoder ?? import_util_base64.fromBase64)(source);
|
|
833
|
+
}
|
|
834
|
+
return source;
|
|
835
|
+
}
|
|
836
|
+
if (ns.isTimestampSchema()) {
|
|
837
|
+
if (typeof source === "number" || typeof source === "bigint") {
|
|
838
|
+
return dateToTag(new Date(Number(source) / 1e3 | 0));
|
|
821
839
|
}
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
840
|
+
return dateToTag(source);
|
|
841
|
+
}
|
|
842
|
+
if (typeof source === "function" || typeof source === "object") {
|
|
843
|
+
const sourceObject = source;
|
|
844
|
+
if (ns.isListSchema() && Array.isArray(sourceObject)) {
|
|
845
|
+
const sparse = !!ns.getMergedTraits().sparse;
|
|
846
|
+
const newArray = [];
|
|
847
|
+
let i = 0;
|
|
848
|
+
for (const item of sourceObject) {
|
|
849
|
+
const value = this.serialize(ns.getValueSchema(), item);
|
|
850
|
+
if (value != null || sparse) {
|
|
851
|
+
newArray[i++] = value;
|
|
852
|
+
}
|
|
827
853
|
}
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
854
|
+
return newArray;
|
|
855
|
+
}
|
|
856
|
+
if (sourceObject instanceof Date) {
|
|
857
|
+
return dateToTag(sourceObject);
|
|
858
|
+
}
|
|
859
|
+
const newObject = {};
|
|
860
|
+
if (ns.isMapSchema()) {
|
|
861
|
+
const sparse = !!ns.getMergedTraits().sparse;
|
|
862
|
+
for (const key of Object.keys(sourceObject)) {
|
|
863
|
+
const value = this.serialize(ns.getValueSchema(), sourceObject[key]);
|
|
864
|
+
if (value != null || sparse) {
|
|
865
|
+
newObject[key] = value;
|
|
838
866
|
}
|
|
839
|
-
|
|
867
|
+
}
|
|
868
|
+
} else if (ns.isStructSchema()) {
|
|
869
|
+
for (const [key, memberSchema] of ns.structIterator()) {
|
|
870
|
+
const value = this.serialize(memberSchema, sourceObject[key]);
|
|
871
|
+
if (value != null) {
|
|
872
|
+
newObject[key] = value;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
} else if (ns.isDocumentSchema()) {
|
|
876
|
+
for (const key of Object.keys(sourceObject)) {
|
|
877
|
+
newObject[key] = this.serialize(ns.getValueSchema(), sourceObject[key]);
|
|
840
878
|
}
|
|
841
879
|
}
|
|
842
|
-
return
|
|
843
|
-
}
|
|
880
|
+
return newObject;
|
|
881
|
+
}
|
|
882
|
+
return source;
|
|
844
883
|
}
|
|
845
884
|
flush() {
|
|
846
885
|
const buffer = cbor.serialize(this.value);
|
|
@@ -856,16 +895,20 @@ var CborShapeDeserializer = class {
|
|
|
856
895
|
const data2 = cbor.deserialize(bytes);
|
|
857
896
|
return this.readValue(schema, data2);
|
|
858
897
|
}
|
|
898
|
+
/**
|
|
899
|
+
* Public because it's called by the protocol implementation to deserialize errors.
|
|
900
|
+
* @internal
|
|
901
|
+
*/
|
|
859
902
|
readValue(_schema, value) {
|
|
860
903
|
const ns = import_schema.NormalizedSchema.of(_schema);
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
return value;
|
|
904
|
+
if (ns.isTimestampSchema() && typeof value === "number") {
|
|
905
|
+
return (0, import_serde3.parseEpochTimestamp)(value);
|
|
906
|
+
}
|
|
907
|
+
if (ns.isBlobSchema()) {
|
|
908
|
+
if (typeof value === "string") {
|
|
909
|
+
return (this.serdeContext?.base64Decoder ?? import_util_base64.fromBase64)(value);
|
|
868
910
|
}
|
|
911
|
+
return value;
|
|
869
912
|
}
|
|
870
913
|
if (typeof value === "undefined" || typeof value === "boolean" || typeof value === "number" || typeof value === "string" || typeof value === "bigint" || typeof value === "symbol") {
|
|
871
914
|
return value;
|
|
@@ -885,23 +928,23 @@ var CborShapeDeserializer = class {
|
|
|
885
928
|
if (ns.isListSchema()) {
|
|
886
929
|
const newArray = [];
|
|
887
930
|
const memberSchema = ns.getValueSchema();
|
|
888
|
-
const sparse =
|
|
931
|
+
const sparse = !!ns.getMergedTraits().sparse;
|
|
889
932
|
for (const item of value) {
|
|
890
|
-
|
|
891
|
-
if (
|
|
892
|
-
newArray.
|
|
933
|
+
const itemValue = this.readValue(memberSchema, item);
|
|
934
|
+
if (itemValue != null || sparse) {
|
|
935
|
+
newArray.push(itemValue);
|
|
893
936
|
}
|
|
894
937
|
}
|
|
895
938
|
return newArray;
|
|
896
939
|
}
|
|
897
940
|
const newObject = {};
|
|
898
941
|
if (ns.isMapSchema()) {
|
|
899
|
-
const sparse = ns.getMergedTraits().sparse;
|
|
942
|
+
const sparse = !!ns.getMergedTraits().sparse;
|
|
900
943
|
const targetSchema = ns.getValueSchema();
|
|
901
944
|
for (const key of Object.keys(value)) {
|
|
902
|
-
|
|
903
|
-
if (
|
|
904
|
-
|
|
945
|
+
const itemValue = this.readValue(targetSchema, value[key]);
|
|
946
|
+
if (itemValue != null || sparse) {
|
|
947
|
+
newObject[key] = itemValue;
|
|
905
948
|
}
|
|
906
949
|
}
|
|
907
950
|
} else if (ns.isStructSchema()) {
|
|
@@ -933,9 +976,9 @@ var SmithyRpcV2CborProtocol = class extends import_protocols2.RpcProtocol {
|
|
|
933
976
|
async serializeRequest(operationSchema, input, context) {
|
|
934
977
|
const request = await super.serializeRequest(operationSchema, input, context);
|
|
935
978
|
Object.assign(request.headers, {
|
|
936
|
-
"content-type":
|
|
979
|
+
"content-type": this.getDefaultContentType(),
|
|
937
980
|
"smithy-protocol": "rpc-v2-cbor",
|
|
938
|
-
accept:
|
|
981
|
+
accept: this.getDefaultContentType()
|
|
939
982
|
});
|
|
940
983
|
if ((0, import_schema2.deref)(operationSchema.input) === "unit") {
|
|
941
984
|
delete request.body;
|
|
@@ -963,25 +1006,50 @@ var SmithyRpcV2CborProtocol = class extends import_protocols2.RpcProtocol {
|
|
|
963
1006
|
return super.deserializeResponse(operationSchema, context, response);
|
|
964
1007
|
}
|
|
965
1008
|
async handleError(operationSchema, context, response, dataObject, metadata) {
|
|
966
|
-
const
|
|
1009
|
+
const errorName = loadSmithyRpcV2CborErrorCode(response, dataObject) ?? "Unknown";
|
|
967
1010
|
let namespace = this.options.defaultNamespace;
|
|
968
|
-
if (
|
|
969
|
-
[namespace] =
|
|
1011
|
+
if (errorName.includes("#")) {
|
|
1012
|
+
[namespace] = errorName.split("#");
|
|
970
1013
|
}
|
|
1014
|
+
const errorMetadata = {
|
|
1015
|
+
$metadata: metadata,
|
|
1016
|
+
$response: response,
|
|
1017
|
+
$fault: response.statusCode <= 500 ? "client" : "server"
|
|
1018
|
+
};
|
|
971
1019
|
const registry = import_schema2.TypeRegistry.for(namespace);
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
1020
|
+
let errorSchema;
|
|
1021
|
+
try {
|
|
1022
|
+
errorSchema = registry.getSchema(errorName);
|
|
1023
|
+
} catch (e) {
|
|
1024
|
+
if (dataObject.Message) {
|
|
1025
|
+
dataObject.message = dataObject.Message;
|
|
1026
|
+
}
|
|
1027
|
+
const baseExceptionSchema = import_schema2.TypeRegistry.for("smithy.ts.sdk.synthetic." + namespace).getBaseException();
|
|
1028
|
+
if (baseExceptionSchema) {
|
|
1029
|
+
const ErrorCtor = baseExceptionSchema.ctor;
|
|
1030
|
+
throw Object.assign(new ErrorCtor({ name: errorName }), errorMetadata, dataObject);
|
|
1031
|
+
}
|
|
1032
|
+
throw Object.assign(new Error(errorName), errorMetadata, dataObject);
|
|
975
1033
|
}
|
|
1034
|
+
const ns = import_schema2.NormalizedSchema.of(errorSchema);
|
|
976
1035
|
const message = dataObject.message ?? dataObject.Message ?? "Unknown";
|
|
977
1036
|
const exception = new errorSchema.ctor(message);
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
1037
|
+
const output = {};
|
|
1038
|
+
for (const [name, member] of ns.structIterator()) {
|
|
1039
|
+
output[name] = this.deserializer.readValue(member, dataObject[name]);
|
|
1040
|
+
}
|
|
1041
|
+
throw Object.assign(
|
|
1042
|
+
exception,
|
|
1043
|
+
errorMetadata,
|
|
1044
|
+
{
|
|
1045
|
+
$fault: ns.getMergedTraits().error,
|
|
1046
|
+
message
|
|
1047
|
+
},
|
|
1048
|
+
output
|
|
1049
|
+
);
|
|
1050
|
+
}
|
|
1051
|
+
getDefaultContentType() {
|
|
1052
|
+
return "application/cbor";
|
|
985
1053
|
}
|
|
986
1054
|
};
|
|
987
1055
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/submodules/event-streams/index.ts
|
|
20
|
+
var event_streams_exports = {};
|
|
21
|
+
__export(event_streams_exports, {
|
|
22
|
+
EventStreamSerde: () => EventStreamSerde
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(event_streams_exports);
|
|
25
|
+
|
|
26
|
+
// src/submodules/event-streams/EventStreamSerde.ts
|
|
27
|
+
var import_schema = require("@smithy/core/schema");
|
|
28
|
+
var import_util_utf8 = require("@smithy/util-utf8");
|
|
29
|
+
var EventStreamSerde = class {
|
|
30
|
+
/**
|
|
31
|
+
* Properties are injected by the HttpProtocol.
|
|
32
|
+
*/
|
|
33
|
+
constructor({
|
|
34
|
+
marshaller,
|
|
35
|
+
serializer,
|
|
36
|
+
deserializer,
|
|
37
|
+
serdeContext,
|
|
38
|
+
defaultContentType
|
|
39
|
+
}) {
|
|
40
|
+
this.marshaller = marshaller;
|
|
41
|
+
this.serializer = serializer;
|
|
42
|
+
this.deserializer = deserializer;
|
|
43
|
+
this.serdeContext = serdeContext;
|
|
44
|
+
this.defaultContentType = defaultContentType;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* @param eventStream - the iterable provided by the caller.
|
|
48
|
+
* @param requestSchema - the schema of the event stream container (struct).
|
|
49
|
+
* @param [initialRequest] - only provided if the initial-request is part of the event stream (RPC).
|
|
50
|
+
*
|
|
51
|
+
* @returns a stream suitable for the HTTP body of a request.
|
|
52
|
+
*/
|
|
53
|
+
async serializeEventStream({
|
|
54
|
+
eventStream,
|
|
55
|
+
requestSchema,
|
|
56
|
+
initialRequest
|
|
57
|
+
}) {
|
|
58
|
+
const marshaller = this.marshaller;
|
|
59
|
+
const eventStreamMember = requestSchema.getEventStreamMember();
|
|
60
|
+
const unionSchema = requestSchema.getMemberSchema(eventStreamMember);
|
|
61
|
+
const memberSchemas = unionSchema.getMemberSchemas();
|
|
62
|
+
const serializer = this.serializer;
|
|
63
|
+
const defaultContentType = this.defaultContentType;
|
|
64
|
+
const initialRequestMarker = Symbol("initialRequestMarker");
|
|
65
|
+
const eventStreamIterable = {
|
|
66
|
+
async *[Symbol.asyncIterator]() {
|
|
67
|
+
if (initialRequest) {
|
|
68
|
+
const headers = {
|
|
69
|
+
":event-type": { type: "string", value: "initial-request" },
|
|
70
|
+
":message-type": { type: "string", value: "event" },
|
|
71
|
+
":content-type": { type: "string", value: defaultContentType }
|
|
72
|
+
};
|
|
73
|
+
serializer.write(requestSchema, initialRequest);
|
|
74
|
+
const body = serializer.flush();
|
|
75
|
+
yield {
|
|
76
|
+
[initialRequestMarker]: true,
|
|
77
|
+
headers,
|
|
78
|
+
body
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
for await (const page of eventStream) {
|
|
82
|
+
yield page;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
return marshaller.serialize(eventStreamIterable, (event) => {
|
|
87
|
+
if (event[initialRequestMarker]) {
|
|
88
|
+
return {
|
|
89
|
+
headers: event.headers,
|
|
90
|
+
body: event.body
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
const unionMember = Object.keys(event).find((key) => {
|
|
94
|
+
return key !== "__type";
|
|
95
|
+
}) ?? "";
|
|
96
|
+
const { additionalHeaders, body, eventType, explicitPayloadContentType } = this.writeEventBody(
|
|
97
|
+
unionMember,
|
|
98
|
+
unionSchema,
|
|
99
|
+
event
|
|
100
|
+
);
|
|
101
|
+
const headers = {
|
|
102
|
+
":event-type": { type: "string", value: eventType },
|
|
103
|
+
":message-type": { type: "string", value: "event" },
|
|
104
|
+
":content-type": { type: "string", value: explicitPayloadContentType ?? defaultContentType },
|
|
105
|
+
...additionalHeaders
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
headers,
|
|
109
|
+
body
|
|
110
|
+
};
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* @param response - http response from which to read the event stream.
|
|
115
|
+
* @param unionSchema - schema of the event stream container (struct).
|
|
116
|
+
* @param [initialResponseContainer] - provided and written to only if the initial response is part of the event stream (RPC).
|
|
117
|
+
*
|
|
118
|
+
* @returns the asyncIterable of the event stream for the end-user.
|
|
119
|
+
*/
|
|
120
|
+
async deserializeEventStream({
|
|
121
|
+
response,
|
|
122
|
+
responseSchema,
|
|
123
|
+
initialResponseContainer
|
|
124
|
+
}) {
|
|
125
|
+
const marshaller = this.marshaller;
|
|
126
|
+
const eventStreamMember = responseSchema.getEventStreamMember();
|
|
127
|
+
const unionSchema = responseSchema.getMemberSchema(eventStreamMember);
|
|
128
|
+
const memberSchemas = unionSchema.getMemberSchemas();
|
|
129
|
+
const initialResponseMarker = Symbol("initialResponseMarker");
|
|
130
|
+
const asyncIterable = marshaller.deserialize(response.body, async (event) => {
|
|
131
|
+
const unionMember = Object.keys(event).find((key) => {
|
|
132
|
+
return key !== "__type";
|
|
133
|
+
}) ?? "";
|
|
134
|
+
if (unionMember === "initial-response") {
|
|
135
|
+
const dataObject = await this.deserializer.read(responseSchema, event[unionMember].body);
|
|
136
|
+
delete dataObject[eventStreamMember];
|
|
137
|
+
return {
|
|
138
|
+
[initialResponseMarker]: true,
|
|
139
|
+
...dataObject
|
|
140
|
+
};
|
|
141
|
+
} else if (unionMember in memberSchemas) {
|
|
142
|
+
const eventStreamSchema = memberSchemas[unionMember];
|
|
143
|
+
return {
|
|
144
|
+
[unionMember]: await this.deserializer.read(eventStreamSchema, event[unionMember].body)
|
|
145
|
+
};
|
|
146
|
+
} else {
|
|
147
|
+
return {
|
|
148
|
+
$unknown: event
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
const asyncIterator = asyncIterable[Symbol.asyncIterator]();
|
|
153
|
+
const firstEvent = await asyncIterator.next();
|
|
154
|
+
if (firstEvent.done) {
|
|
155
|
+
return asyncIterable;
|
|
156
|
+
}
|
|
157
|
+
if (firstEvent.value?.[initialResponseMarker]) {
|
|
158
|
+
if (!responseSchema) {
|
|
159
|
+
throw new Error(
|
|
160
|
+
"@smithy::core/protocols - initial-response event encountered in event stream but no response schema given."
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
for (const [key, value] of Object.entries(firstEvent.value)) {
|
|
164
|
+
initialResponseContainer[key] = value;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
async *[Symbol.asyncIterator]() {
|
|
169
|
+
if (!firstEvent?.value?.[initialResponseMarker]) {
|
|
170
|
+
yield firstEvent.value;
|
|
171
|
+
}
|
|
172
|
+
while (true) {
|
|
173
|
+
const { done, value } = await asyncIterator.next();
|
|
174
|
+
if (done) {
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
yield value;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* @param unionMember - member name within the structure that contains an event stream union.
|
|
184
|
+
* @param unionSchema - schema of the union.
|
|
185
|
+
* @param event
|
|
186
|
+
*
|
|
187
|
+
* @returns the event body (bytes) and event type (string).
|
|
188
|
+
*/
|
|
189
|
+
writeEventBody(unionMember, unionSchema, event) {
|
|
190
|
+
const serializer = this.serializer;
|
|
191
|
+
let eventType = unionMember;
|
|
192
|
+
let explicitPayloadMember = null;
|
|
193
|
+
let explicitPayloadContentType;
|
|
194
|
+
const isKnownSchema = unionSchema.hasMemberSchema(unionMember);
|
|
195
|
+
const additionalHeaders = {};
|
|
196
|
+
if (!isKnownSchema) {
|
|
197
|
+
const [type, value] = event[unionMember];
|
|
198
|
+
eventType = type;
|
|
199
|
+
serializer.write(import_schema.SCHEMA.DOCUMENT, value);
|
|
200
|
+
} else {
|
|
201
|
+
const eventSchema = unionSchema.getMemberSchema(unionMember);
|
|
202
|
+
if (eventSchema.isStructSchema()) {
|
|
203
|
+
for (const [memberName, memberSchema] of eventSchema.structIterator()) {
|
|
204
|
+
const { eventHeader, eventPayload } = memberSchema.getMergedTraits();
|
|
205
|
+
if (eventPayload) {
|
|
206
|
+
explicitPayloadMember = memberName;
|
|
207
|
+
break;
|
|
208
|
+
} else if (eventHeader) {
|
|
209
|
+
const value = event[unionMember][memberName];
|
|
210
|
+
let type = "binary";
|
|
211
|
+
if (memberSchema.isNumericSchema()) {
|
|
212
|
+
if ((-2) ** 31 <= value && value <= 2 ** 31 - 1) {
|
|
213
|
+
type = "integer";
|
|
214
|
+
} else {
|
|
215
|
+
type = "long";
|
|
216
|
+
}
|
|
217
|
+
} else if (memberSchema.isTimestampSchema()) {
|
|
218
|
+
type = "timestamp";
|
|
219
|
+
} else if (memberSchema.isStringSchema()) {
|
|
220
|
+
type = "string";
|
|
221
|
+
} else if (memberSchema.isBooleanSchema()) {
|
|
222
|
+
type = "boolean";
|
|
223
|
+
}
|
|
224
|
+
if (value != null) {
|
|
225
|
+
additionalHeaders[memberName] = {
|
|
226
|
+
type,
|
|
227
|
+
value
|
|
228
|
+
};
|
|
229
|
+
delete event[unionMember][memberName];
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (explicitPayloadMember !== null) {
|
|
234
|
+
const payloadSchema = eventSchema.getMemberSchema(explicitPayloadMember);
|
|
235
|
+
if (payloadSchema.isBlobSchema()) {
|
|
236
|
+
explicitPayloadContentType = "application/octet-stream";
|
|
237
|
+
} else if (payloadSchema.isStringSchema()) {
|
|
238
|
+
explicitPayloadContentType = "text/plain";
|
|
239
|
+
}
|
|
240
|
+
serializer.write(payloadSchema, event[unionMember][explicitPayloadMember]);
|
|
241
|
+
} else {
|
|
242
|
+
serializer.write(eventSchema, event[unionMember]);
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
throw new Error("@smithy/core/event-streams - non-struct member not supported in event stream union.");
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const messageSerialization = serializer.flush();
|
|
249
|
+
const body = typeof messageSerialization === "string" ? (this.serdeContext?.utf8Decoder ?? import_util_utf8.fromUtf8)(messageSerialization) : messageSerialization;
|
|
250
|
+
return {
|
|
251
|
+
body,
|
|
252
|
+
eventType,
|
|
253
|
+
explicitPayloadContentType,
|
|
254
|
+
additionalHeaders
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
259
|
+
0 && (module.exports = {
|
|
260
|
+
EventStreamSerde
|
|
261
|
+
});
|