@smithy/core 3.3.3 → 3.5.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 +212 -2
- package/dist-cjs/submodules/protocols/index.js +667 -2
- package/dist-cjs/submodules/schema/index.js +771 -0
- package/dist-cjs/submodules/serde/index.js +680 -2
- package/dist-es/submodules/cbor/CborCodec.js +136 -0
- package/dist-es/submodules/cbor/SmithyRpcV2CborProtocol.js +74 -0
- package/dist-es/submodules/cbor/index.js +3 -1
- package/dist-es/submodules/cbor/parseCborBody.js +3 -2
- package/dist-es/submodules/protocols/HttpBindingProtocol.js +183 -0
- package/dist-es/submodules/protocols/HttpProtocol.js +164 -0
- package/dist-es/submodules/protocols/RpcProtocol.js +68 -0
- package/dist-es/submodules/protocols/index.js +7 -0
- package/dist-es/submodules/protocols/serde/FromStringShapeDeserializer.js +64 -0
- package/dist-es/submodules/protocols/serde/HttpInterceptingShapeDeserializer.js +38 -0
- package/dist-es/submodules/protocols/serde/HttpInterceptingShapeSerializer.js +30 -0
- package/dist-es/submodules/protocols/serde/ToStringShapeSerializer.js +87 -0
- package/dist-es/submodules/protocols/serde/determineTimestampFormat.js +20 -0
- package/dist-es/submodules/schema/TypeRegistry.js +49 -0
- package/dist-es/submodules/schema/deref.js +6 -0
- package/dist-es/submodules/schema/index.js +12 -0
- package/dist-es/submodules/schema/middleware/getSchemaSerdePlugin.js +23 -0
- package/dist-es/submodules/schema/middleware/schema-middleware-types.js +1 -0
- package/dist-es/submodules/schema/middleware/schemaDeserializationMiddleware.js +60 -0
- package/dist-es/submodules/schema/middleware/schemaSerializationMiddleware.js +16 -0
- package/dist-es/submodules/schema/schemas/ErrorSchema.js +17 -0
- package/dist-es/submodules/schema/schemas/ListSchema.js +15 -0
- package/dist-es/submodules/schema/schemas/MapSchema.js +16 -0
- package/dist-es/submodules/schema/schemas/NormalizedSchema.js +291 -0
- package/dist-es/submodules/schema/schemas/OperationSchema.js +16 -0
- package/dist-es/submodules/schema/schemas/Schema.js +6 -0
- package/dist-es/submodules/schema/schemas/SimpleSchema.js +15 -0
- package/dist-es/submodules/schema/schemas/StructureSchema.js +22 -0
- package/dist-es/submodules/schema/schemas/sentinels.js +16 -0
- package/dist-es/submodules/serde/copyDocumentWithTransform.js +53 -0
- package/dist-es/submodules/serde/date-utils.js +190 -0
- package/dist-es/submodules/serde/index.js +7 -0
- package/dist-es/submodules/serde/lazy-json.js +24 -0
- package/dist-es/submodules/serde/parse-utils.js +230 -0
- package/dist-es/submodules/serde/quote-header.js +6 -0
- package/dist-es/submodules/serde/split-every.js +27 -0
- package/dist-es/submodules/serde/split-header.js +37 -0
- package/dist-types/submodules/cbor/CborCodec.d.ts +29 -0
- package/dist-types/submodules/cbor/SmithyRpcV2CborProtocol.d.ts +22 -0
- package/dist-types/submodules/cbor/index.d.ts +3 -1
- package/dist-types/submodules/protocols/HttpBindingProtocol.d.ts +15 -0
- package/dist-types/submodules/protocols/HttpProtocol.d.ts +29 -0
- package/dist-types/submodules/protocols/RpcProtocol.d.ts +11 -0
- package/dist-types/submodules/protocols/index.d.ts +7 -0
- package/dist-types/submodules/protocols/serde/FromStringShapeDeserializer.d.ts +14 -0
- package/dist-types/submodules/protocols/serde/HttpInterceptingShapeDeserializer.d.ts +18 -0
- package/dist-types/submodules/protocols/serde/HttpInterceptingShapeSerializer.d.ts +20 -0
- package/dist-types/submodules/protocols/serde/ToStringShapeSerializer.d.ts +15 -0
- package/dist-types/submodules/protocols/serde/determineTimestampFormat.d.ts +9 -0
- package/dist-types/submodules/schema/TypeRegistry.d.ts +55 -0
- package/dist-types/submodules/schema/deref.d.ts +6 -0
- package/dist-types/submodules/schema/index.d.ts +12 -0
- package/dist-types/submodules/schema/middleware/getSchemaSerdePlugin.d.ts +14 -0
- package/dist-types/submodules/schema/middleware/schema-middleware-types.d.ts +8 -0
- package/dist-types/submodules/schema/middleware/schemaDeserializationMiddleware.d.ts +9 -0
- package/dist-types/submodules/schema/middleware/schemaSerializationMiddleware.d.ts +6 -0
- package/dist-types/submodules/schema/schemas/ErrorSchema.d.ts +38 -0
- package/dist-types/submodules/schema/schemas/ListSchema.d.ts +20 -0
- package/dist-types/submodules/schema/schemas/MapSchema.d.ts +25 -0
- package/dist-types/submodules/schema/schemas/NormalizedSchema.d.ts +122 -0
- package/dist-types/submodules/schema/schemas/OperationSchema.d.ts +20 -0
- package/dist-types/submodules/schema/schemas/Schema.d.ts +11 -0
- package/dist-types/submodules/schema/schemas/SimpleSchema.d.ts +20 -0
- package/dist-types/submodules/schema/schemas/StructureSchema.d.ts +21 -0
- package/dist-types/submodules/schema/schemas/sentinels.d.ts +21 -0
- package/dist-types/submodules/serde/copyDocumentWithTransform.d.ts +5 -0
- package/dist-types/submodules/serde/date-utils.d.ts +73 -0
- package/dist-types/submodules/serde/index.d.ts +7 -0
- package/dist-types/submodules/serde/lazy-json.d.ts +45 -0
- package/dist-types/submodules/serde/parse-utils.d.ts +270 -0
- package/dist-types/submodules/serde/quote-header.d.ts +6 -0
- package/dist-types/submodules/serde/split-every.d.ts +11 -0
- package/dist-types/submodules/serde/split-header.d.ts +5 -0
- package/dist-types/ts3.4/submodules/cbor/CborCodec.d.ts +29 -0
- package/dist-types/ts3.4/submodules/cbor/SmithyRpcV2CborProtocol.d.ts +22 -0
- package/dist-types/ts3.4/submodules/cbor/index.d.ts +3 -1
- package/dist-types/ts3.4/submodules/protocols/HttpBindingProtocol.d.ts +15 -0
- package/dist-types/ts3.4/submodules/protocols/HttpProtocol.d.ts +29 -0
- package/dist-types/ts3.4/submodules/protocols/RpcProtocol.d.ts +11 -0
- package/dist-types/ts3.4/submodules/protocols/index.d.ts +7 -0
- package/dist-types/ts3.4/submodules/protocols/serde/FromStringShapeDeserializer.d.ts +14 -0
- package/dist-types/ts3.4/submodules/protocols/serde/HttpInterceptingShapeDeserializer.d.ts +18 -0
- package/dist-types/ts3.4/submodules/protocols/serde/HttpInterceptingShapeSerializer.d.ts +20 -0
- package/dist-types/ts3.4/submodules/protocols/serde/ToStringShapeSerializer.d.ts +15 -0
- package/dist-types/ts3.4/submodules/protocols/serde/determineTimestampFormat.d.ts +9 -0
- package/dist-types/ts3.4/submodules/schema/TypeRegistry.d.ts +55 -0
- package/dist-types/ts3.4/submodules/schema/deref.d.ts +6 -0
- package/dist-types/ts3.4/submodules/schema/index.d.ts +12 -0
- package/dist-types/ts3.4/submodules/schema/middleware/getSchemaSerdePlugin.d.ts +14 -0
- package/dist-types/ts3.4/submodules/schema/middleware/schema-middleware-types.d.ts +11 -0
- package/dist-types/ts3.4/submodules/schema/middleware/schemaDeserializationMiddleware.d.ts +9 -0
- package/dist-types/ts3.4/submodules/schema/middleware/schemaSerializationMiddleware.d.ts +6 -0
- package/dist-types/ts3.4/submodules/schema/schemas/ErrorSchema.d.ts +38 -0
- package/dist-types/ts3.4/submodules/schema/schemas/ListSchema.d.ts +20 -0
- package/dist-types/ts3.4/submodules/schema/schemas/MapSchema.d.ts +25 -0
- package/dist-types/ts3.4/submodules/schema/schemas/NormalizedSchema.d.ts +125 -0
- package/dist-types/ts3.4/submodules/schema/schemas/OperationSchema.d.ts +20 -0
- package/dist-types/ts3.4/submodules/schema/schemas/Schema.d.ts +11 -0
- package/dist-types/ts3.4/submodules/schema/schemas/SimpleSchema.d.ts +20 -0
- package/dist-types/ts3.4/submodules/schema/schemas/StructureSchema.d.ts +24 -0
- package/dist-types/ts3.4/submodules/schema/schemas/sentinels.d.ts +21 -0
- package/dist-types/ts3.4/submodules/serde/copyDocumentWithTransform.d.ts +5 -0
- package/dist-types/ts3.4/submodules/serde/date-utils.d.ts +73 -0
- package/dist-types/ts3.4/submodules/serde/index.d.ts +7 -0
- package/dist-types/ts3.4/submodules/serde/lazy-json.d.ts +45 -0
- package/dist-types/ts3.4/submodules/serde/parse-utils.d.ts +270 -0
- package/dist-types/ts3.4/submodules/serde/quote-header.d.ts +6 -0
- package/dist-types/ts3.4/submodules/serde/split-every.d.ts +11 -0
- package/dist-types/ts3.4/submodules/serde/split-header.d.ts +5 -0
- package/package.json +18 -7
- package/schema.d.ts +7 -0
- package/schema.js +6 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { NormalizedSchema } from "@smithy/core/schema";
|
|
2
|
+
import { copyDocumentWithTransform, parseEpochTimestamp } from "@smithy/core/serde";
|
|
3
|
+
import { cbor } from "./cbor";
|
|
4
|
+
import { dateToTag } from "./parseCborBody";
|
|
5
|
+
export class CborCodec {
|
|
6
|
+
createSerializer() {
|
|
7
|
+
const serializer = new CborShapeSerializer();
|
|
8
|
+
serializer.setSerdeContext(this.serdeContext);
|
|
9
|
+
return serializer;
|
|
10
|
+
}
|
|
11
|
+
createDeserializer() {
|
|
12
|
+
const deserializer = new CborShapeDeserializer();
|
|
13
|
+
deserializer.setSerdeContext(this.serdeContext);
|
|
14
|
+
return deserializer;
|
|
15
|
+
}
|
|
16
|
+
setSerdeContext(serdeContext) {
|
|
17
|
+
this.serdeContext = serdeContext;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class CborShapeSerializer {
|
|
21
|
+
setSerdeContext(serdeContext) {
|
|
22
|
+
this.serdeContext = serdeContext;
|
|
23
|
+
}
|
|
24
|
+
write(schema, value) {
|
|
25
|
+
this.value = copyDocumentWithTransform(value, schema, (_, schemaRef) => {
|
|
26
|
+
if (_ instanceof Date) {
|
|
27
|
+
return dateToTag(_);
|
|
28
|
+
}
|
|
29
|
+
if (_ instanceof Uint8Array) {
|
|
30
|
+
return _;
|
|
31
|
+
}
|
|
32
|
+
const ns = NormalizedSchema.of(schemaRef);
|
|
33
|
+
const sparse = !!ns.getMergedTraits().sparse;
|
|
34
|
+
if (ns.isListSchema() && Array.isArray(_)) {
|
|
35
|
+
if (!sparse) {
|
|
36
|
+
return _.filter((item) => item != null);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else if (_ && typeof _ === "object") {
|
|
40
|
+
const members = ns.getMemberSchemas();
|
|
41
|
+
const isStruct = ns.isStructSchema();
|
|
42
|
+
if (!sparse || isStruct) {
|
|
43
|
+
for (const [k, v] of Object.entries(_)) {
|
|
44
|
+
const filteredOutByNonSparse = !sparse && v == null;
|
|
45
|
+
const filteredOutByUnrecognizedMember = isStruct && !(k in members);
|
|
46
|
+
if (filteredOutByNonSparse || filteredOutByUnrecognizedMember) {
|
|
47
|
+
delete _[k];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return _;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return _;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
flush() {
|
|
57
|
+
const buffer = cbor.serialize(this.value);
|
|
58
|
+
this.value = undefined;
|
|
59
|
+
return buffer;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export class CborShapeDeserializer {
|
|
63
|
+
setSerdeContext(serdeContext) {
|
|
64
|
+
this.serdeContext = serdeContext;
|
|
65
|
+
}
|
|
66
|
+
read(schema, bytes) {
|
|
67
|
+
const data = cbor.deserialize(bytes);
|
|
68
|
+
return this.readValue(schema, data);
|
|
69
|
+
}
|
|
70
|
+
readValue(_schema, value) {
|
|
71
|
+
const ns = NormalizedSchema.of(_schema);
|
|
72
|
+
const schema = ns.getSchema();
|
|
73
|
+
if (typeof schema === "number") {
|
|
74
|
+
if (ns.isTimestampSchema()) {
|
|
75
|
+
return parseEpochTimestamp(value);
|
|
76
|
+
}
|
|
77
|
+
if (ns.isBlobSchema()) {
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (typeof value === "undefined" ||
|
|
82
|
+
typeof value === "boolean" ||
|
|
83
|
+
typeof value === "number" ||
|
|
84
|
+
typeof value === "string" ||
|
|
85
|
+
typeof value === "bigint" ||
|
|
86
|
+
typeof value === "symbol") {
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
else if (typeof value === "function" || typeof value === "object") {
|
|
90
|
+
if (value === null) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
if ("byteLength" in value) {
|
|
94
|
+
return value;
|
|
95
|
+
}
|
|
96
|
+
if (value instanceof Date) {
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
if (ns.isDocumentSchema()) {
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
102
|
+
if (ns.isListSchema()) {
|
|
103
|
+
const newArray = [];
|
|
104
|
+
const memberSchema = ns.getValueSchema();
|
|
105
|
+
const sparse = ns.isListSchema() && !!ns.getMergedTraits().sparse;
|
|
106
|
+
for (const item of value) {
|
|
107
|
+
newArray.push(this.readValue(memberSchema, item));
|
|
108
|
+
if (!sparse && newArray[newArray.length - 1] == null) {
|
|
109
|
+
newArray.pop();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return newArray;
|
|
113
|
+
}
|
|
114
|
+
const newObject = {};
|
|
115
|
+
if (ns.isMapSchema()) {
|
|
116
|
+
const sparse = ns.getMergedTraits().sparse;
|
|
117
|
+
const targetSchema = ns.getValueSchema();
|
|
118
|
+
for (const key of Object.keys(value)) {
|
|
119
|
+
newObject[key] = this.readValue(targetSchema, value[key]);
|
|
120
|
+
if (newObject[key] == null && !sparse) {
|
|
121
|
+
delete newObject[key];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
else if (ns.isStructSchema()) {
|
|
126
|
+
for (const [key, memberSchema] of ns.structIterator()) {
|
|
127
|
+
newObject[key] = this.readValue(memberSchema, value[key]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return newObject;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
return value;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { RpcProtocol } from "@smithy/core/protocols";
|
|
2
|
+
import { deref, TypeRegistry } from "@smithy/core/schema";
|
|
3
|
+
import { getSmithyContext } from "@smithy/util-middleware";
|
|
4
|
+
import { CborCodec } from "./CborCodec";
|
|
5
|
+
import { loadSmithyRpcV2CborErrorCode } from "./parseCborBody";
|
|
6
|
+
export class SmithyRpcV2CborProtocol extends RpcProtocol {
|
|
7
|
+
constructor({ defaultNamespace }) {
|
|
8
|
+
super({ defaultNamespace });
|
|
9
|
+
this.codec = new CborCodec();
|
|
10
|
+
this.serializer = this.codec.createSerializer();
|
|
11
|
+
this.deserializer = this.codec.createDeserializer();
|
|
12
|
+
}
|
|
13
|
+
getShapeId() {
|
|
14
|
+
return "smithy.protocols#rpcv2Cbor";
|
|
15
|
+
}
|
|
16
|
+
getPayloadCodec() {
|
|
17
|
+
return this.codec;
|
|
18
|
+
}
|
|
19
|
+
async serializeRequest(operationSchema, input, context) {
|
|
20
|
+
const request = await super.serializeRequest(operationSchema, input, context);
|
|
21
|
+
Object.assign(request.headers, {
|
|
22
|
+
"content-type": "application/cbor",
|
|
23
|
+
"smithy-protocol": "rpc-v2-cbor",
|
|
24
|
+
accept: "application/cbor",
|
|
25
|
+
});
|
|
26
|
+
if (deref(operationSchema.input) === "unit") {
|
|
27
|
+
delete request.body;
|
|
28
|
+
delete request.headers["content-type"];
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
if (!request.body) {
|
|
32
|
+
this.serializer.write(15, {});
|
|
33
|
+
request.body = this.serializer.flush();
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
request.headers["content-length"] = String(request.body.byteLength);
|
|
37
|
+
}
|
|
38
|
+
catch (e) { }
|
|
39
|
+
}
|
|
40
|
+
const { service, operation } = getSmithyContext(context);
|
|
41
|
+
const path = `/service/${service}/operation/${operation}`;
|
|
42
|
+
if (request.path.endsWith("/")) {
|
|
43
|
+
request.path += path.slice(1);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
request.path += path;
|
|
47
|
+
}
|
|
48
|
+
return request;
|
|
49
|
+
}
|
|
50
|
+
async deserializeResponse(operationSchema, context, response) {
|
|
51
|
+
return super.deserializeResponse(operationSchema, context, response);
|
|
52
|
+
}
|
|
53
|
+
async handleError(operationSchema, context, response, dataObject, metadata) {
|
|
54
|
+
const error = loadSmithyRpcV2CborErrorCode(response, dataObject) ?? "Unknown";
|
|
55
|
+
let namespace = this.options.defaultNamespace;
|
|
56
|
+
if (error.includes("#")) {
|
|
57
|
+
[namespace] = error.split("#");
|
|
58
|
+
}
|
|
59
|
+
const registry = TypeRegistry.for(namespace);
|
|
60
|
+
const errorSchema = registry.getSchema(error);
|
|
61
|
+
if (!errorSchema) {
|
|
62
|
+
throw new Error("schema not found for " + error);
|
|
63
|
+
}
|
|
64
|
+
const message = dataObject.message ?? dataObject.Message ?? "Unknown";
|
|
65
|
+
const exception = new errorSchema.ctor(message);
|
|
66
|
+
Object.assign(exception, {
|
|
67
|
+
$metadata: metadata,
|
|
68
|
+
$response: response,
|
|
69
|
+
message,
|
|
70
|
+
...dataObject,
|
|
71
|
+
});
|
|
72
|
+
throw exception;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -50,8 +50,9 @@ export const loadSmithyRpcV2CborErrorCode = (output, data) => {
|
|
|
50
50
|
if (data["__type"] !== undefined) {
|
|
51
51
|
return sanitizeErrorCode(data["__type"]);
|
|
52
52
|
}
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
const codeKey = Object.keys(data).find((key) => key.toLowerCase() === "code");
|
|
54
|
+
if (codeKey && data[codeKey] !== undefined) {
|
|
55
|
+
return sanitizeErrorCode(data[codeKey]);
|
|
55
56
|
}
|
|
56
57
|
};
|
|
57
58
|
export const checkCborResponse = (response) => {
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { NormalizedSchema, SCHEMA } from "@smithy/core/schema";
|
|
2
|
+
import { HttpRequest } from "@smithy/protocol-http";
|
|
3
|
+
import { collectBody } from "./collect-stream-body";
|
|
4
|
+
import { extendedEncodeURIComponent } from "./extended-encode-uri-component";
|
|
5
|
+
import { HttpProtocol } from "./HttpProtocol";
|
|
6
|
+
export class HttpBindingProtocol extends HttpProtocol {
|
|
7
|
+
async serializeRequest(operationSchema, input, context) {
|
|
8
|
+
const serializer = this.serializer;
|
|
9
|
+
const query = {};
|
|
10
|
+
const headers = {};
|
|
11
|
+
const endpoint = await context.endpoint();
|
|
12
|
+
const ns = NormalizedSchema.of(operationSchema?.input);
|
|
13
|
+
const schema = ns.getSchema();
|
|
14
|
+
let hasNonHttpBindingMember = false;
|
|
15
|
+
let payload;
|
|
16
|
+
const request = new HttpRequest({
|
|
17
|
+
protocol: "",
|
|
18
|
+
hostname: "",
|
|
19
|
+
port: undefined,
|
|
20
|
+
path: "",
|
|
21
|
+
fragment: undefined,
|
|
22
|
+
query: query,
|
|
23
|
+
headers: headers,
|
|
24
|
+
body: undefined,
|
|
25
|
+
});
|
|
26
|
+
if (endpoint) {
|
|
27
|
+
this.updateServiceEndpoint(request, endpoint);
|
|
28
|
+
this.setHostPrefix(request, operationSchema, input);
|
|
29
|
+
const opTraits = NormalizedSchema.translateTraits(operationSchema.traits);
|
|
30
|
+
if (opTraits.http) {
|
|
31
|
+
request.method = opTraits.http[0];
|
|
32
|
+
const [path, search] = opTraits.http[1].split("?");
|
|
33
|
+
if (request.path == "/") {
|
|
34
|
+
request.path = path;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
request.path += path;
|
|
38
|
+
}
|
|
39
|
+
const traitSearchParams = new URLSearchParams(search ?? "");
|
|
40
|
+
Object.assign(query, Object.fromEntries(traitSearchParams));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const _input = {
|
|
44
|
+
...input,
|
|
45
|
+
};
|
|
46
|
+
for (const memberName of Object.keys(_input)) {
|
|
47
|
+
const memberNs = ns.getMemberSchema(memberName);
|
|
48
|
+
if (memberNs === undefined) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const memberTraits = memberNs.getMergedTraits();
|
|
52
|
+
const inputMember = _input[memberName];
|
|
53
|
+
if (memberTraits.httpPayload) {
|
|
54
|
+
const isStreaming = memberNs.isStreaming();
|
|
55
|
+
if (isStreaming) {
|
|
56
|
+
const isEventStream = memberNs.isStructSchema();
|
|
57
|
+
if (isEventStream) {
|
|
58
|
+
throw new Error("serialization of event streams is not yet implemented");
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
payload = inputMember;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
serializer.write(memberNs, inputMember);
|
|
66
|
+
payload = serializer.flush();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else if (memberTraits.httpLabel) {
|
|
70
|
+
serializer.write(memberNs, inputMember);
|
|
71
|
+
const replacement = serializer.flush();
|
|
72
|
+
if (request.path.includes(`{${memberName}+}`)) {
|
|
73
|
+
request.path = request.path.replace(`{${memberName}+}`, replacement.split("/").map(extendedEncodeURIComponent).join("/"));
|
|
74
|
+
}
|
|
75
|
+
else if (request.path.includes(`{${memberName}}`)) {
|
|
76
|
+
request.path = request.path.replace(`{${memberName}}`, extendedEncodeURIComponent(replacement));
|
|
77
|
+
}
|
|
78
|
+
delete _input[memberName];
|
|
79
|
+
}
|
|
80
|
+
else if (memberTraits.httpHeader) {
|
|
81
|
+
serializer.write(memberNs, inputMember);
|
|
82
|
+
headers[memberTraits.httpHeader.toLowerCase()] = String(serializer.flush());
|
|
83
|
+
delete _input[memberName];
|
|
84
|
+
}
|
|
85
|
+
else if (typeof memberTraits.httpPrefixHeaders === "string") {
|
|
86
|
+
for (const [key, val] of Object.entries(inputMember)) {
|
|
87
|
+
const amalgam = memberTraits.httpPrefixHeaders + key;
|
|
88
|
+
serializer.write([memberNs.getValueSchema(), { httpHeader: amalgam }], val);
|
|
89
|
+
headers[amalgam.toLowerCase()] = serializer.flush();
|
|
90
|
+
}
|
|
91
|
+
delete _input[memberName];
|
|
92
|
+
}
|
|
93
|
+
else if (memberTraits.httpQuery || memberTraits.httpQueryParams) {
|
|
94
|
+
this.serializeQuery(memberNs, inputMember, query);
|
|
95
|
+
delete _input[memberName];
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
hasNonHttpBindingMember = true;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (hasNonHttpBindingMember && input) {
|
|
102
|
+
serializer.write(schema, _input);
|
|
103
|
+
payload = serializer.flush();
|
|
104
|
+
}
|
|
105
|
+
request.headers = headers;
|
|
106
|
+
request.query = query;
|
|
107
|
+
request.body = payload;
|
|
108
|
+
return request;
|
|
109
|
+
}
|
|
110
|
+
serializeQuery(ns, data, query) {
|
|
111
|
+
const serializer = this.serializer;
|
|
112
|
+
const traits = ns.getMergedTraits();
|
|
113
|
+
if (traits.httpQueryParams) {
|
|
114
|
+
for (const [key, val] of Object.entries(data)) {
|
|
115
|
+
if (!(key in query)) {
|
|
116
|
+
this.serializeQuery(NormalizedSchema.of([
|
|
117
|
+
ns.getValueSchema(),
|
|
118
|
+
{
|
|
119
|
+
...traits,
|
|
120
|
+
httpQuery: key,
|
|
121
|
+
httpQueryParams: undefined,
|
|
122
|
+
},
|
|
123
|
+
]), val, query);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (ns.isListSchema()) {
|
|
129
|
+
const sparse = !!ns.getMergedTraits().sparse;
|
|
130
|
+
const buffer = [];
|
|
131
|
+
for (const item of data) {
|
|
132
|
+
serializer.write([ns.getValueSchema(), traits], item);
|
|
133
|
+
const serializable = serializer.flush();
|
|
134
|
+
if (sparse || serializable !== undefined) {
|
|
135
|
+
buffer.push(serializable);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
query[traits.httpQuery] = buffer;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
serializer.write([ns, traits], data);
|
|
142
|
+
query[traits.httpQuery] = serializer.flush();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
async deserializeResponse(operationSchema, context, response) {
|
|
146
|
+
const deserializer = this.deserializer;
|
|
147
|
+
const ns = NormalizedSchema.of(operationSchema.output);
|
|
148
|
+
const dataObject = {};
|
|
149
|
+
if (response.statusCode >= 300) {
|
|
150
|
+
const bytes = await collectBody(response.body, context);
|
|
151
|
+
if (bytes.byteLength > 0) {
|
|
152
|
+
Object.assign(dataObject, await deserializer.read(SCHEMA.DOCUMENT, bytes));
|
|
153
|
+
}
|
|
154
|
+
await this.handleError(operationSchema, context, response, dataObject, this.deserializeMetadata(response));
|
|
155
|
+
throw new Error("@smithy/core/protocols - HTTP Protocol error handler failed to throw.");
|
|
156
|
+
}
|
|
157
|
+
for (const header in response.headers) {
|
|
158
|
+
const value = response.headers[header];
|
|
159
|
+
delete response.headers[header];
|
|
160
|
+
response.headers[header.toLowerCase()] = value;
|
|
161
|
+
}
|
|
162
|
+
const headerBindings = new Set(Object.values(ns.getMemberSchemas())
|
|
163
|
+
.map((schema) => {
|
|
164
|
+
return schema.getMergedTraits().httpHeader;
|
|
165
|
+
})
|
|
166
|
+
.filter(Boolean));
|
|
167
|
+
const nonHttpBindingMembers = await this.deserializeHttpMessage(ns, context, response, headerBindings, dataObject);
|
|
168
|
+
if (nonHttpBindingMembers.length) {
|
|
169
|
+
const bytes = await collectBody(response.body, context);
|
|
170
|
+
if (bytes.byteLength > 0) {
|
|
171
|
+
const dataFromBody = await deserializer.read(ns, bytes);
|
|
172
|
+
for (const member of nonHttpBindingMembers) {
|
|
173
|
+
dataObject[member] = dataFromBody[member];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const output = {
|
|
178
|
+
$metadata: this.deserializeMetadata(response),
|
|
179
|
+
...dataObject,
|
|
180
|
+
};
|
|
181
|
+
return output;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { NormalizedSchema, SCHEMA } from "@smithy/core/schema";
|
|
2
|
+
import { splitEvery, splitHeader } from "@smithy/core/serde";
|
|
3
|
+
import { HttpRequest, HttpResponse } from "@smithy/protocol-http";
|
|
4
|
+
import { sdkStreamMixin } from "@smithy/util-stream";
|
|
5
|
+
import { collectBody } from "./collect-stream-body";
|
|
6
|
+
export class HttpProtocol {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
}
|
|
10
|
+
getRequestType() {
|
|
11
|
+
return HttpRequest;
|
|
12
|
+
}
|
|
13
|
+
getResponseType() {
|
|
14
|
+
return HttpResponse;
|
|
15
|
+
}
|
|
16
|
+
setSerdeContext(serdeContext) {
|
|
17
|
+
this.serdeContext = serdeContext;
|
|
18
|
+
this.serializer.setSerdeContext(serdeContext);
|
|
19
|
+
this.deserializer.setSerdeContext(serdeContext);
|
|
20
|
+
if (this.getPayloadCodec()) {
|
|
21
|
+
this.getPayloadCodec().setSerdeContext(serdeContext);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
updateServiceEndpoint(request, endpoint) {
|
|
25
|
+
if ("url" in endpoint) {
|
|
26
|
+
request.protocol = endpoint.url.protocol;
|
|
27
|
+
request.hostname = endpoint.url.hostname;
|
|
28
|
+
request.port = endpoint.url.port ? Number(endpoint.url.port) : undefined;
|
|
29
|
+
request.path = endpoint.url.pathname;
|
|
30
|
+
request.fragment = endpoint.url.hash || void 0;
|
|
31
|
+
request.username = endpoint.url.username || void 0;
|
|
32
|
+
request.password = endpoint.url.password || void 0;
|
|
33
|
+
for (const [k, v] of endpoint.url.searchParams.entries()) {
|
|
34
|
+
if (!request.query) {
|
|
35
|
+
request.query = {};
|
|
36
|
+
}
|
|
37
|
+
request.query[k] = v;
|
|
38
|
+
}
|
|
39
|
+
return request;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
request.protocol = endpoint.protocol;
|
|
43
|
+
request.hostname = endpoint.hostname;
|
|
44
|
+
request.port = endpoint.port ? Number(endpoint.port) : undefined;
|
|
45
|
+
request.path = endpoint.path;
|
|
46
|
+
request.query = {
|
|
47
|
+
...endpoint.query,
|
|
48
|
+
};
|
|
49
|
+
return request;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
setHostPrefix(request, operationSchema, input) {
|
|
53
|
+
const operationNs = NormalizedSchema.of(operationSchema);
|
|
54
|
+
const inputNs = NormalizedSchema.of(operationSchema.input);
|
|
55
|
+
if (operationNs.getMergedTraits().endpoint) {
|
|
56
|
+
let hostPrefix = operationNs.getMergedTraits().endpoint?.[0];
|
|
57
|
+
if (typeof hostPrefix === "string") {
|
|
58
|
+
const hostLabelInputs = [...inputNs.structIterator()].filter(([, member]) => member.getMergedTraits().hostLabel);
|
|
59
|
+
for (const [name] of hostLabelInputs) {
|
|
60
|
+
const replacement = input[name];
|
|
61
|
+
if (typeof replacement !== "string") {
|
|
62
|
+
throw new Error(`@smithy/core/schema - ${name} in input must be a string as hostLabel.`);
|
|
63
|
+
}
|
|
64
|
+
hostPrefix = hostPrefix.replace(`{${name}}`, replacement);
|
|
65
|
+
}
|
|
66
|
+
request.hostname = hostPrefix + request.hostname;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
deserializeMetadata(output) {
|
|
71
|
+
return {
|
|
72
|
+
httpStatusCode: output.statusCode,
|
|
73
|
+
requestId: output.headers["x-amzn-requestid"] ?? output.headers["x-amzn-request-id"] ?? output.headers["x-amz-request-id"],
|
|
74
|
+
extendedRequestId: output.headers["x-amz-id-2"],
|
|
75
|
+
cfId: output.headers["x-amz-cf-id"],
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
async deserializeHttpMessage(schema, context, response, headerBindings, dataObject) {
|
|
79
|
+
const deserializer = this.deserializer;
|
|
80
|
+
const ns = NormalizedSchema.of(schema);
|
|
81
|
+
const nonHttpBindingMembers = [];
|
|
82
|
+
for (const [memberName, memberSchema] of ns.structIterator()) {
|
|
83
|
+
const memberTraits = memberSchema.getMemberTraits();
|
|
84
|
+
if (memberTraits.httpPayload) {
|
|
85
|
+
const isStreaming = memberSchema.isStreaming();
|
|
86
|
+
if (isStreaming) {
|
|
87
|
+
const isEventStream = memberSchema.isStructSchema();
|
|
88
|
+
if (isEventStream) {
|
|
89
|
+
const context = this.serdeContext;
|
|
90
|
+
if (!context.eventStreamMarshaller) {
|
|
91
|
+
throw new Error("@smithy/core - HttpProtocol: eventStreamMarshaller missing in serdeContext.");
|
|
92
|
+
}
|
|
93
|
+
const memberSchemas = memberSchema.getMemberSchemas();
|
|
94
|
+
dataObject[memberName] = context.eventStreamMarshaller.deserialize(response.body, async (event) => {
|
|
95
|
+
const unionMember = Object.keys(event).find((key) => {
|
|
96
|
+
return key !== "__type";
|
|
97
|
+
}) ?? "";
|
|
98
|
+
if (unionMember in memberSchemas) {
|
|
99
|
+
const eventStreamSchema = memberSchemas[unionMember];
|
|
100
|
+
return {
|
|
101
|
+
[unionMember]: await deserializer.read(eventStreamSchema, event[unionMember].body),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
return {
|
|
106
|
+
$unknown: event,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
dataObject[memberName] = sdkStreamMixin(response.body);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else if (response.body) {
|
|
116
|
+
const bytes = await collectBody(response.body, context);
|
|
117
|
+
if (bytes.byteLength > 0) {
|
|
118
|
+
dataObject[memberName] = await deserializer.read(memberSchema, bytes);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else if (memberTraits.httpHeader) {
|
|
123
|
+
const key = String(memberTraits.httpHeader).toLowerCase();
|
|
124
|
+
const value = response.headers[key];
|
|
125
|
+
if (null != value) {
|
|
126
|
+
if (memberSchema.isListSchema()) {
|
|
127
|
+
const headerListValueSchema = memberSchema.getValueSchema();
|
|
128
|
+
let sections;
|
|
129
|
+
if (headerListValueSchema.isTimestampSchema() &&
|
|
130
|
+
headerListValueSchema.getSchema() === SCHEMA.TIMESTAMP_DEFAULT) {
|
|
131
|
+
sections = splitEvery(value, ",", 2);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
sections = splitHeader(value);
|
|
135
|
+
}
|
|
136
|
+
const list = [];
|
|
137
|
+
for (const section of sections) {
|
|
138
|
+
list.push(await deserializer.read([headerListValueSchema, { httpHeader: key }], section.trim()));
|
|
139
|
+
}
|
|
140
|
+
dataObject[memberName] = list;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
dataObject[memberName] = await deserializer.read(memberSchema, value);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else if (memberTraits.httpPrefixHeaders !== undefined) {
|
|
148
|
+
dataObject[memberName] = {};
|
|
149
|
+
for (const [header, value] of Object.entries(response.headers)) {
|
|
150
|
+
if (!headerBindings.has(header) && header.startsWith(memberTraits.httpPrefixHeaders)) {
|
|
151
|
+
dataObject[memberName][header.slice(memberTraits.httpPrefixHeaders.length)] = await deserializer.read([memberSchema.getValueSchema(), { httpHeader: header }], value);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
else if (memberTraits.httpResponseCode) {
|
|
156
|
+
dataObject[memberName] = response.statusCode;
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
nonHttpBindingMembers.push(memberName);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return nonHttpBindingMembers;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { NormalizedSchema, SCHEMA } from "@smithy/core/schema";
|
|
2
|
+
import { HttpRequest } from "@smithy/protocol-http";
|
|
3
|
+
import { collectBody } from "./collect-stream-body";
|
|
4
|
+
import { HttpProtocol } from "./HttpProtocol";
|
|
5
|
+
export class RpcProtocol extends HttpProtocol {
|
|
6
|
+
async serializeRequest(operationSchema, input, context) {
|
|
7
|
+
const serializer = this.serializer;
|
|
8
|
+
const query = {};
|
|
9
|
+
const headers = {};
|
|
10
|
+
const endpoint = await context.endpoint();
|
|
11
|
+
const ns = NormalizedSchema.of(operationSchema?.input);
|
|
12
|
+
const schema = ns.getSchema();
|
|
13
|
+
let payload;
|
|
14
|
+
const request = new HttpRequest({
|
|
15
|
+
protocol: "",
|
|
16
|
+
hostname: "",
|
|
17
|
+
port: undefined,
|
|
18
|
+
path: "/",
|
|
19
|
+
fragment: undefined,
|
|
20
|
+
query: query,
|
|
21
|
+
headers: headers,
|
|
22
|
+
body: undefined,
|
|
23
|
+
});
|
|
24
|
+
if (endpoint) {
|
|
25
|
+
this.updateServiceEndpoint(request, endpoint);
|
|
26
|
+
this.setHostPrefix(request, operationSchema, input);
|
|
27
|
+
}
|
|
28
|
+
const _input = {
|
|
29
|
+
...input,
|
|
30
|
+
};
|
|
31
|
+
if (input) {
|
|
32
|
+
serializer.write(schema, _input);
|
|
33
|
+
payload = serializer.flush();
|
|
34
|
+
}
|
|
35
|
+
request.headers = headers;
|
|
36
|
+
request.query = query;
|
|
37
|
+
request.body = payload;
|
|
38
|
+
request.method = "POST";
|
|
39
|
+
return request;
|
|
40
|
+
}
|
|
41
|
+
async deserializeResponse(operationSchema, context, response) {
|
|
42
|
+
const deserializer = this.deserializer;
|
|
43
|
+
const ns = NormalizedSchema.of(operationSchema.output);
|
|
44
|
+
const dataObject = {};
|
|
45
|
+
if (response.statusCode >= 300) {
|
|
46
|
+
const bytes = await collectBody(response.body, context);
|
|
47
|
+
if (bytes.byteLength > 0) {
|
|
48
|
+
Object.assign(dataObject, await deserializer.read(SCHEMA.DOCUMENT, bytes));
|
|
49
|
+
}
|
|
50
|
+
await this.handleError(operationSchema, context, response, dataObject, this.deserializeMetadata(response));
|
|
51
|
+
throw new Error("@smithy/core/protocols - RPC Protocol error handler failed to throw.");
|
|
52
|
+
}
|
|
53
|
+
for (const header in response.headers) {
|
|
54
|
+
const value = response.headers[header];
|
|
55
|
+
delete response.headers[header];
|
|
56
|
+
response.headers[header.toLowerCase()] = value;
|
|
57
|
+
}
|
|
58
|
+
const bytes = await collectBody(response.body, context);
|
|
59
|
+
if (bytes.byteLength > 0) {
|
|
60
|
+
Object.assign(dataObject, await deserializer.read(ns, bytes));
|
|
61
|
+
}
|
|
62
|
+
const output = {
|
|
63
|
+
$metadata: this.deserializeMetadata(response),
|
|
64
|
+
...dataObject,
|
|
65
|
+
};
|
|
66
|
+
return output;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
export * from "./collect-stream-body";
|
|
2
2
|
export * from "./extended-encode-uri-component";
|
|
3
|
+
export * from "./HttpBindingProtocol";
|
|
4
|
+
export * from "./RpcProtocol";
|
|
3
5
|
export * from "./requestBuilder";
|
|
4
6
|
export * from "./resolve-path";
|
|
7
|
+
export * from "./serde/FromStringShapeDeserializer";
|
|
8
|
+
export * from "./serde/HttpInterceptingShapeDeserializer";
|
|
9
|
+
export * from "./serde/HttpInterceptingShapeSerializer";
|
|
10
|
+
export * from "./serde/ToStringShapeSerializer";
|
|
11
|
+
export * from "./serde/determineTimestampFormat";
|