@atomm-developer/generator-sdk 1.0.3
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/README.md +453 -0
- package/dist/index-qYdFih-w.js +81234 -0
- package/dist/index-wW-kUlHs.js +245 -0
- package/dist/index.d.ts +672 -0
- package/dist/index.es.js +7 -0
- package/dist/index.umd.js +20 -0
- package/package.json +57 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import { t as toUtf8, f as fromUtf8 } from "./index-qYdFih-w.js";
|
|
5
|
+
class EventStreamSerde {
|
|
6
|
+
constructor({ marshaller, serializer, deserializer, serdeContext, defaultContentType }) {
|
|
7
|
+
__publicField(this, "marshaller");
|
|
8
|
+
__publicField(this, "serializer");
|
|
9
|
+
__publicField(this, "deserializer");
|
|
10
|
+
__publicField(this, "serdeContext");
|
|
11
|
+
__publicField(this, "defaultContentType");
|
|
12
|
+
this.marshaller = marshaller;
|
|
13
|
+
this.serializer = serializer;
|
|
14
|
+
this.deserializer = deserializer;
|
|
15
|
+
this.serdeContext = serdeContext;
|
|
16
|
+
this.defaultContentType = defaultContentType;
|
|
17
|
+
}
|
|
18
|
+
async serializeEventStream({ eventStream, requestSchema, initialRequest }) {
|
|
19
|
+
const marshaller = this.marshaller;
|
|
20
|
+
const eventStreamMember = requestSchema.getEventStreamMember();
|
|
21
|
+
const unionSchema = requestSchema.getMemberSchema(eventStreamMember);
|
|
22
|
+
const serializer = this.serializer;
|
|
23
|
+
const defaultContentType = this.defaultContentType;
|
|
24
|
+
const initialRequestMarker = Symbol("initialRequestMarker");
|
|
25
|
+
const eventStreamIterable = {
|
|
26
|
+
async *[Symbol.asyncIterator]() {
|
|
27
|
+
if (initialRequest) {
|
|
28
|
+
const headers = {
|
|
29
|
+
":event-type": { type: "string", value: "initial-request" },
|
|
30
|
+
":message-type": { type: "string", value: "event" },
|
|
31
|
+
":content-type": { type: "string", value: defaultContentType }
|
|
32
|
+
};
|
|
33
|
+
serializer.write(requestSchema, initialRequest);
|
|
34
|
+
const body = serializer.flush();
|
|
35
|
+
yield {
|
|
36
|
+
[initialRequestMarker]: true,
|
|
37
|
+
headers,
|
|
38
|
+
body
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
for await (const page of eventStream) {
|
|
42
|
+
yield page;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
return marshaller.serialize(eventStreamIterable, (event) => {
|
|
47
|
+
if (event[initialRequestMarker]) {
|
|
48
|
+
return {
|
|
49
|
+
headers: event.headers,
|
|
50
|
+
body: event.body
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
const unionMember = Object.keys(event).find((key) => {
|
|
54
|
+
return key !== "__type";
|
|
55
|
+
}) ?? "";
|
|
56
|
+
const { additionalHeaders, body, eventType, explicitPayloadContentType } = this.writeEventBody(unionMember, unionSchema, event);
|
|
57
|
+
const headers = {
|
|
58
|
+
":event-type": { type: "string", value: eventType },
|
|
59
|
+
":message-type": { type: "string", value: "event" },
|
|
60
|
+
":content-type": { type: "string", value: explicitPayloadContentType ?? defaultContentType },
|
|
61
|
+
...additionalHeaders
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
headers,
|
|
65
|
+
body
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
async deserializeEventStream({ response, responseSchema, initialResponseContainer }) {
|
|
70
|
+
var _a;
|
|
71
|
+
const marshaller = this.marshaller;
|
|
72
|
+
const eventStreamMember = responseSchema.getEventStreamMember();
|
|
73
|
+
const unionSchema = responseSchema.getMemberSchema(eventStreamMember);
|
|
74
|
+
const memberSchemas = unionSchema.getMemberSchemas();
|
|
75
|
+
const initialResponseMarker = Symbol("initialResponseMarker");
|
|
76
|
+
const asyncIterable = marshaller.deserialize(response.body, async (event) => {
|
|
77
|
+
var _a2, _b;
|
|
78
|
+
const unionMember = Object.keys(event).find((key) => {
|
|
79
|
+
return key !== "__type";
|
|
80
|
+
}) ?? "";
|
|
81
|
+
const body = event[unionMember].body;
|
|
82
|
+
if (unionMember === "initial-response") {
|
|
83
|
+
const dataObject = await this.deserializer.read(responseSchema, body);
|
|
84
|
+
delete dataObject[eventStreamMember];
|
|
85
|
+
return {
|
|
86
|
+
[initialResponseMarker]: true,
|
|
87
|
+
...dataObject
|
|
88
|
+
};
|
|
89
|
+
} else if (unionMember in memberSchemas) {
|
|
90
|
+
const eventStreamSchema = memberSchemas[unionMember];
|
|
91
|
+
if (eventStreamSchema.isStructSchema()) {
|
|
92
|
+
const out = {};
|
|
93
|
+
let hasBindings = false;
|
|
94
|
+
for (const [name, member] of eventStreamSchema.structIterator()) {
|
|
95
|
+
const { eventHeader, eventPayload } = member.getMergedTraits();
|
|
96
|
+
hasBindings = hasBindings || Boolean(eventHeader || eventPayload);
|
|
97
|
+
if (eventPayload) {
|
|
98
|
+
if (member.isBlobSchema()) {
|
|
99
|
+
out[name] = body;
|
|
100
|
+
} else if (member.isStringSchema()) {
|
|
101
|
+
out[name] = (((_a2 = this.serdeContext) == null ? void 0 : _a2.utf8Encoder) ?? toUtf8)(body);
|
|
102
|
+
} else if (member.isStructSchema()) {
|
|
103
|
+
out[name] = await this.deserializer.read(member, body);
|
|
104
|
+
}
|
|
105
|
+
} else if (eventHeader) {
|
|
106
|
+
const value = (_b = event[unionMember].headers[name]) == null ? void 0 : _b.value;
|
|
107
|
+
if (value != null) {
|
|
108
|
+
if (member.isNumericSchema()) {
|
|
109
|
+
if (value && typeof value === "object" && "bytes" in value) {
|
|
110
|
+
out[name] = BigInt(value.toString());
|
|
111
|
+
} else {
|
|
112
|
+
out[name] = Number(value);
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
out[name] = value;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (hasBindings) {
|
|
121
|
+
return {
|
|
122
|
+
[unionMember]: out
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
if (body.byteLength === 0) {
|
|
126
|
+
return {
|
|
127
|
+
[unionMember]: {}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
[unionMember]: await this.deserializer.read(eventStreamSchema, body)
|
|
133
|
+
};
|
|
134
|
+
} else {
|
|
135
|
+
return {
|
|
136
|
+
$unknown: event
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
const asyncIterator = asyncIterable[Symbol.asyncIterator]();
|
|
141
|
+
const firstEvent = await asyncIterator.next();
|
|
142
|
+
if (firstEvent.done) {
|
|
143
|
+
return asyncIterable;
|
|
144
|
+
}
|
|
145
|
+
if ((_a = firstEvent.value) == null ? void 0 : _a[initialResponseMarker]) {
|
|
146
|
+
if (!responseSchema) {
|
|
147
|
+
throw new Error("@smithy::core/protocols - initial-response event encountered in event stream but no response schema given.");
|
|
148
|
+
}
|
|
149
|
+
for (const [key, value] of Object.entries(firstEvent.value)) {
|
|
150
|
+
initialResponseContainer[key] = value;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
async *[Symbol.asyncIterator]() {
|
|
155
|
+
var _a2;
|
|
156
|
+
if (!((_a2 = firstEvent == null ? void 0 : firstEvent.value) == null ? void 0 : _a2[initialResponseMarker])) {
|
|
157
|
+
yield firstEvent.value;
|
|
158
|
+
}
|
|
159
|
+
while (true) {
|
|
160
|
+
const { done, value } = await asyncIterator.next();
|
|
161
|
+
if (done) {
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
yield value;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
writeEventBody(unionMember, unionSchema, event) {
|
|
170
|
+
var _a;
|
|
171
|
+
const serializer = this.serializer;
|
|
172
|
+
let eventType = unionMember;
|
|
173
|
+
let explicitPayloadMember = null;
|
|
174
|
+
let explicitPayloadContentType;
|
|
175
|
+
const isKnownSchema = (() => {
|
|
176
|
+
const struct = unionSchema.getSchema();
|
|
177
|
+
return struct[4].includes(unionMember);
|
|
178
|
+
})();
|
|
179
|
+
const additionalHeaders = {};
|
|
180
|
+
if (!isKnownSchema) {
|
|
181
|
+
const [type, value] = event[unionMember];
|
|
182
|
+
eventType = type;
|
|
183
|
+
serializer.write(15, value);
|
|
184
|
+
} else {
|
|
185
|
+
const eventSchema = unionSchema.getMemberSchema(unionMember);
|
|
186
|
+
if (eventSchema.isStructSchema()) {
|
|
187
|
+
for (const [memberName, memberSchema] of eventSchema.structIterator()) {
|
|
188
|
+
const { eventHeader, eventPayload } = memberSchema.getMergedTraits();
|
|
189
|
+
if (eventPayload) {
|
|
190
|
+
explicitPayloadMember = memberName;
|
|
191
|
+
} else if (eventHeader) {
|
|
192
|
+
const value = event[unionMember][memberName];
|
|
193
|
+
let type = "binary";
|
|
194
|
+
if (memberSchema.isNumericSchema()) {
|
|
195
|
+
if ((-2) ** 31 <= value && value <= 2 ** 31 - 1) {
|
|
196
|
+
type = "integer";
|
|
197
|
+
} else {
|
|
198
|
+
type = "long";
|
|
199
|
+
}
|
|
200
|
+
} else if (memberSchema.isTimestampSchema()) {
|
|
201
|
+
type = "timestamp";
|
|
202
|
+
} else if (memberSchema.isStringSchema()) {
|
|
203
|
+
type = "string";
|
|
204
|
+
} else if (memberSchema.isBooleanSchema()) {
|
|
205
|
+
type = "boolean";
|
|
206
|
+
}
|
|
207
|
+
if (value != null) {
|
|
208
|
+
additionalHeaders[memberName] = {
|
|
209
|
+
type,
|
|
210
|
+
value
|
|
211
|
+
};
|
|
212
|
+
delete event[unionMember][memberName];
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (explicitPayloadMember !== null) {
|
|
217
|
+
const payloadSchema = eventSchema.getMemberSchema(explicitPayloadMember);
|
|
218
|
+
if (payloadSchema.isBlobSchema()) {
|
|
219
|
+
explicitPayloadContentType = "application/octet-stream";
|
|
220
|
+
} else if (payloadSchema.isStringSchema()) {
|
|
221
|
+
explicitPayloadContentType = "text/plain";
|
|
222
|
+
}
|
|
223
|
+
serializer.write(payloadSchema, event[unionMember][explicitPayloadMember]);
|
|
224
|
+
} else {
|
|
225
|
+
serializer.write(eventSchema, event[unionMember]);
|
|
226
|
+
}
|
|
227
|
+
} else if (eventSchema.isUnitSchema()) {
|
|
228
|
+
serializer.write(eventSchema, {});
|
|
229
|
+
} else {
|
|
230
|
+
throw new Error("@smithy/core/event-streams - non-struct member not supported in event stream union.");
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
const messageSerialization = serializer.flush() ?? new Uint8Array();
|
|
234
|
+
const body = typeof messageSerialization === "string" ? (((_a = this.serdeContext) == null ? void 0 : _a.utf8Decoder) ?? fromUtf8)(messageSerialization) : messageSerialization;
|
|
235
|
+
return {
|
|
236
|
+
body,
|
|
237
|
+
eventType,
|
|
238
|
+
explicitPayloadContentType,
|
|
239
|
+
additionalHeaders
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
export {
|
|
244
|
+
EventStreamSerde
|
|
245
|
+
};
|