Skip to content

Commit 72a02d9

Browse files
committed
feat: parse serialize(deserialize) util functions from client config
* Serializers/Deserializers should take utils functions from client config first, if not available then fallback to generated dependencies. This allows configuring the runtime dependencies manually from client config when runtime-specific bundlers' decision is not accountable
1 parent 7fbf9d0 commit 72a02d9

File tree

12 files changed

+80
-44
lines changed

12 files changed

+80
-44
lines changed

clients/node/client-rds-data-node/commands/ExecuteStatementCommand.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Command } from '@aws-sdk/smithy-client';
22
import { serializerPlugin } from "@aws-sdk/middleware-serializer";
3-
import { deserializerMiddleware } from "@aws-sdk/middleware-deserializer";
3+
import { deserializerPlugin } from "@aws-sdk/middleware-deserializer";
44
import * as __aws_sdk_types from "@aws-sdk/types";
55
import { RDSDataResolvedConfiguration } from "../RDSDataConfiguration";
66
import { HttpRequest } from '@aws-sdk/protocol-http';
@@ -32,17 +32,7 @@ export class ExecuteStatementCommand extends Command<ExecuteStatementInput, Exec
3232
const { httpHandler } = configuration;
3333

3434
this.use(serializerPlugin(configuration, executeStatementSerializer));
35-
this.middlewareStack.add(
36-
deserializerMiddleware<ExecuteStatementInput, ExecuteStatementOutput>(
37-
configuration.protocol,
38-
executeStatementDeserializer
39-
) as any,
40-
{
41-
step: "deserialize",
42-
priority: Infinity,
43-
tags: { DESERIALIZER: true }
44-
}
45-
);
35+
this.use(deserializerPlugin<ExecuteStatementOutput>(configuration, executeStatementDeserializer));
4636

4737
const stack = clientStack.concat(this.middlewareStack);
4838

clients/node/client-rds-data-node/protocol/AwsRestJson1_1.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import {
66
SqlParameter
77
} from "../models/rdsdataservice";
88
import { HttpRequest, HttpResponse } from "@aws-sdk/protocol-http";
9+
import { SerializerUtils, DeserializerUtils } from '@aws-sdk/types';
910
import * as __aws_sdk_stream_collector_node from "@aws-sdk/stream-collector-node";
1011
import * as __aws_sdk_util_utf8_node from "@aws-sdk/util-utf8-node";
1112

13+
type Utils = { [key: string]: any };
14+
1215
export function executeStatementAwsRestJson1_1Serialize(
13-
input: ExecuteStatementRequest
16+
input: ExecuteStatementRequest,
17+
utils?: Utils
1418
): HttpRequest {
1519
let body: any = {};
1620
if (input.resourceArn !== undefined) {
@@ -61,12 +65,13 @@ export function executeStatementAwsRestJson1_1Serialize(
6165
}
6266

6367
export function executeStatementAwsRestJson1_1Deserialize(
64-
output: HttpResponse
68+
output: HttpResponse,
69+
utils?: Utils
6570
): Promise<ExecuteStatementResponse> {
6671
if (output.statusCode !== 200) {
6772
return executeStatementAwsRestJson1_1DeserializeError(output);
6873
}
69-
let data: any = parseBody(output.body);
74+
let data: any = parseBody(output.body, utils);
7075
return Promise.resolve({
7176
__type: "com.amazon.rdsdataservice#ExecuteStatementResponse",
7277
records: recordsAwsRestJson1_1Deserialize(data.records),
@@ -290,8 +295,14 @@ function recordsListAwsRestJson1_1Deserialize(input: any): Array<Field> {
290295
return list;
291296
}
292297

293-
function parseBody(streamBody: any): any {
294-
__aws_sdk_stream_collector_node.streamCollector(streamBody).then(body => {
295-
return __aws_sdk_util_utf8_node.toUtf8(body);
298+
function parseBody(streamBody: any, utils?: Utils): any {
299+
const streamCollector = utils && utils['streamCollector'] ?
300+
(<DeserializerUtils>utils)['streamCollector'] :
301+
__aws_sdk_stream_collector_node.streamCollector;
302+
const toUtf8 = utils && utils['streamCollector'] ?
303+
(<DeserializerUtils>utils)['utf8Encoder'] :
304+
__aws_sdk_util_utf8_node.toUtf8;
305+
streamCollector(streamBody).then(body => {
306+
return toUtf8(body);
296307
});
297308
}

clients/node/client-rds-data-node/protocol/ExecuteStatement.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,29 @@ import {
88
executeStatementAwsRestJson1_1Deserialize
99
} from "./AwsRestJson1_1";
1010

11+
type Utils = { [key: string]: any };
12+
1113
export function executeStatementSerializer(
1214
input: ExecuteStatementRequest,
13-
protocol: string
15+
protocol: string,
16+
utils?: Utils
1417
): HttpRequest {
1518
switch (protocol) {
1619
case "aws.rest-json-1.1":
17-
return executeStatementAwsRestJson1_1Serialize(input);
20+
return executeStatementAwsRestJson1_1Serialize(input, utils);
1821
default:
1922
throw new Error("Unknown protocol, use aws.rest-json-1.1");
2023
}
2124
}
2225

2326
export async function executeStatementDeserializer(
2427
output: HttpResponse,
25-
protocol: string
28+
protocol: string,
29+
utils?: Utils
2630
): Promise<ExecuteStatementResponse> {
2731
switch (protocol) {
2832
case "aws.rest-json-1.1":
29-
return executeStatementAwsRestJson1_1Deserialize(output);
33+
return executeStatementAwsRestJson1_1Deserialize(output, utils);
3034
default:
3135
throw new Error("Unknown protocol, use aws.rest-json-1.1");
3236
}

packages/config-resolver/src/components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface RuntimeDependencies {
2727
regionDefaultProvider?: (input: any) => Provider<string>;
2828
urlParser?: UrlParser;
2929
bodyLengthChecker?: (body: any) => number | undefined;
30-
streamCollector?: StreamCollector<any>;
30+
streamCollector?: StreamCollector;
3131
base64Decoder?: Decoder;
3232
base64Encoder?: Encoder;
3333
utf8Decoder?: Decoder;

packages/middleware-deserializer/src/index.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import {
44
DeserializeHandlerArguments,
55
DeserializeMiddleware,
66
DeserializeHandlerOutput,
7-
Protocol
7+
Protocol,
8+
DeserializerUtils,
9+
InjectableMiddleware
810
} from "@aws-sdk/types";
911

1012
export function deserializerMiddleware<
1113
Input extends object,
1214
Output extends object
1315
>(
14-
protocol: Protocol<any, any>,
16+
options: DeserializerMiddlewareConfig,
1517
deserializer: ResponseDeserializer<any>
1618
): DeserializeMiddleware<Input, Output> {
1719
return (
@@ -20,10 +22,29 @@ export function deserializerMiddleware<
2022
args: DeserializeHandlerArguments<Input>
2123
): Promise<DeserializeHandlerOutput<Output>> => {
2224
const { response } = await next(args);
23-
const parsed = await protocol.parse(deserializer, response);
25+
const parsed = await options.protocol.deserialize(
26+
deserializer,
27+
response,
28+
options
29+
);
2430
return {
2531
response,
2632
output: parsed as Output
2733
};
2834
};
2935
}
36+
37+
export interface DeserializerMiddlewareConfig extends DeserializerUtils {
38+
protocol: Protocol<any, any>;
39+
}
40+
41+
export function deserializerPlugin<OutputType>(
42+
config: DeserializerMiddlewareConfig,
43+
serializer: ResponseDeserializer<OutputType>
44+
): InjectableMiddleware {
45+
return {
46+
middleware: deserializerMiddleware(config, serializer),
47+
step: "deserialize",
48+
tags: { DESERIALIZER: true }
49+
};
50+
}

packages/middleware-serializer/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
SerializeMiddleware,
66
SerializeHandlerOutput,
77
Protocol,
8-
SerializerConfig,
8+
SerializerUtils,
99
InjectableMiddleware
1010
} from "@aws-sdk/types";
1111

@@ -29,7 +29,7 @@ export function serializerMiddleware<
2929
};
3030
}
3131

32-
export interface SerializerMiddlewareConfig extends SerializerConfig {
32+
export interface SerializerMiddlewareConfig extends SerializerUtils {
3333
protocol: Protocol<any, any>;
3434
}
3535

packages/protocol-rest-json/src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
ResponseDeserializer,
44
Protocol,
55
TransferHandler,
6-
HttpOptions
6+
HttpOptions,
7+
SerializerUtils,
8+
DeserializerUtils
79
} from "@aws-sdk/types";
810
import { HttpRequest, HttpResponse } from "@aws-sdk/protocol-http";
911

@@ -17,10 +19,18 @@ export class RestJsonProtocol extends Protocol<
1719
) {
1820
super(handler);
1921
}
20-
serialize(serializer: RequestSerializer<HttpRequest>, input: any) {
22+
serialize(
23+
serializer: RequestSerializer<HttpRequest>,
24+
input: any,
25+
utils?: SerializerUtils
26+
) {
2127
return serializer(input, "aws.rest-json-1.1");
2228
}
23-
parse(parser: ResponseDeserializer<HttpResponse>, output: HttpResponse) {
29+
deserialize(
30+
parser: ResponseDeserializer<HttpResponse>,
31+
output: HttpResponse,
32+
utils?: DeserializerUtils
33+
) {
2434
return parser(output, "aws.rest-json-1.1") as any;
2535
}
2636
}

packages/stream-collector-browser/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { StreamCollector } from "@aws-sdk/types";
22

3-
export const streamCollector: StreamCollector<Blob> = function streamCollector(
3+
export const streamCollector: StreamCollector = function streamCollector(
44
stream: Blob
55
): Promise<Uint8Array> {
66
return new Promise<Uint8Array>((resolve, reject) => {

packages/stream-collector-node/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { Readable } from "stream";
22
import { StreamCollector } from "@aws-sdk/types";
33
import { Collector } from "./collector";
44

5-
export const streamCollector: StreamCollector<
6-
Readable
7-
> = function streamCollector(stream): Promise<Uint8Array> {
5+
export const streamCollector: StreamCollector = function streamCollector(
6+
stream: Readable
7+
): Promise<Uint8Array> {
88
return new Promise((resolve, reject) => {
99
const collector = new Collector();
1010
stream.pipe(collector);

packages/types/src/deserializer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ export interface StreamCollector {
1010
}
1111

1212
/**
13-
* Response deserializer config interface for AWS services
13+
* Response deserializer utils functions for AWS services
1414
*/
15-
export interface DeserializerConfig {
15+
export interface DeserializerUtils {
1616
base64Decoder: Decoder;
1717
utf8Encoder: Encoder;
1818
streamCollector: StreamCollector;
@@ -26,12 +26,12 @@ export interface ResponseDeserializer<OutputType, ResponseType = any> {
2626
* response received
2727
* @param input The HTTP response received from the service
2828
*
29-
* @param config The runtime-specific util functions. If provided will
29+
* @param utils The runtime-specific util functions. If provided will
3030
* overwrite the provided ones
3131
*/
3232
(
3333
output: ResponseType,
3434
protocolName: string,
35-
config?: { [key: string]: any }
35+
utils?: { [key: string]: any }
3636
): Promise<OutputType>;
3737
}

packages/types/src/serializer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Decoder, Encoder } from "./util";
22

33
/**
4-
* Response deserializer config interface for AWS services
4+
* Response deserializer util functions for AWS services
55
*/
6-
export interface SerializerConfig {
6+
export interface SerializerUtils {
77
utf8Decoder: Decoder;
88
base64Encoder: Encoder;
99
}
@@ -22,6 +22,6 @@ export interface RequestSerializer<Request> {
2222
(
2323
input: any,
2424
transferProtocol: string,
25-
config?: { [key: string]: any }
25+
utils?: { [key: string]: any }
2626
): Request;
2727
}

packages/types/src/transfer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export abstract class Protocol<RequestType, ResponseType, HandlerOptions = {}> {
2222
abstract serialize(
2323
serializer: RequestSerializer<RequestType>,
2424
input: any,
25-
config?: { [key: string]: any }
25+
utils?: { [key: string]: any }
2626
): RequestType;
2727
abstract deserialize<T extends ResponseDeserializer<ResponseType>>(
2828
parser: T,
2929
input: ResponseType,
30-
config?: { [key: string]: any }
30+
utils?: { [key: string]: any }
3131
): ReturnType<T>;
3232
}

0 commit comments

Comments
 (0)