Skip to content

Commit 554d671

Browse files
committed
feat: encoders accept strings
1 parent 3e30223 commit 554d671

File tree

14 files changed

+103
-37
lines changed

14 files changed

+103
-37
lines changed

.changeset/fluffy-deers-doubt.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@smithy/middleware-serde": minor
3+
"@smithy/util-base64": minor
4+
"@smithy/util-utf8": minor
5+
"@smithy/types": minor
6+
---
7+
8+
encoders allow string inputs

packages/middleware-serde/src/deserializerMiddleware.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { EndpointBearer, SerdeFunctions } from "@smithy/types";
2+
13
import { deserializerMiddleware } from "./deserializerMiddleware";
24

35
describe("deserializerMiddleware", () => {
@@ -11,7 +13,7 @@ describe("deserializerMiddleware", () => {
1113
hostname: "hostname",
1214
path: "path",
1315
}),
14-
};
16+
} as EndpointBearer & SerdeFunctions;
1517

1618
const mockArgs = {
1719
input: {

packages/middleware-serde/src/deserializerMiddleware.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import {
55
DeserializeMiddleware,
66
HandlerExecutionContext,
77
ResponseDeserializer,
8+
SerdeFunctions,
89
} from "@smithy/types";
910

10-
export const deserializerMiddleware = <Input extends object, Output extends object, RuntimeUtils = any>(
11-
options: RuntimeUtils,
12-
deserializer: ResponseDeserializer<any, any, RuntimeUtils>
11+
/**
12+
* @internal
13+
*/
14+
export const deserializerMiddleware = <Input extends object, Output extends object>(
15+
options: SerdeFunctions,
16+
deserializer: ResponseDeserializer<any, any, SerdeFunctions>
1317
): DeserializeMiddleware<Input, Output> => (
1418
next: DeserializeHandler<Input, Output>,
1519
context: HandlerExecutionContext

packages/middleware-serde/src/serdePlugin.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Provider,
99
RequestSerializer,
1010
ResponseDeserializer,
11+
SerdeFunctions,
1112
SerializeHandlerOptions,
1213
UrlParser,
1314
} from "@smithy/types";
@@ -39,14 +40,19 @@ export type V1OrV2Endpoint = {
3940
endpoint?: Provider<Endpoint>;
4041
};
4142

42-
export function getSerdePlugin<InputType extends object, SerDeContext, OutputType extends MetadataBearer>(
43-
config: V1OrV2Endpoint,
44-
serializer: RequestSerializer<any, SerDeContext & EndpointBearer>,
45-
deserializer: ResponseDeserializer<OutputType, any, SerDeContext>
43+
/**
44+
* @internal
45+
*
46+
* Note: 2nd type parameter is deprecated and unused.
47+
*/
48+
export function getSerdePlugin<InputType extends object, _, OutputType extends MetadataBearer>(
49+
config: V1OrV2Endpoint & SerdeFunctions,
50+
serializer: RequestSerializer<any, SerdeFunctions & EndpointBearer>,
51+
deserializer: ResponseDeserializer<OutputType, any, SerdeFunctions>
4652
): Pluggable<InputType, OutputType> {
4753
return {
4854
applyToStack: (commandStack: MiddlewareStack<InputType, OutputType>) => {
49-
commandStack.add(deserializerMiddleware(config as SerDeContext, deserializer), deserializerMiddlewareOption);
55+
commandStack.add(deserializerMiddleware(config, deserializer), deserializerMiddlewareOption);
5056
commandStack.add(serializerMiddleware(config, serializer), serializerMiddlewareOption);
5157
},
5258
};

packages/middleware-serde/src/serializerMiddleware.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EndpointBearer } from "@smithy/types";
1+
import { EndpointBearer, SerdeFunctions } from "@smithy/types";
22

33
import { serializerMiddleware } from "./serializerMiddleware";
44

@@ -13,7 +13,7 @@ describe("serializerMiddleware", () => {
1313
hostname: "hostname",
1414
path: "path",
1515
}),
16-
};
16+
} as EndpointBearer & SerdeFunctions;
1717

1818
const mockRequest = {
1919
method: "GET",

packages/middleware-serde/src/serializerMiddleware.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
EndpointBearer,
33
HandlerExecutionContext,
44
RequestSerializer,
5+
SerdeFunctions,
56
SerializeHandler,
67
SerializeHandlerArguments,
78
SerializeHandlerOutput,
@@ -10,9 +11,14 @@ import {
1011

1112
import type { V1OrV2Endpoint } from "./serdePlugin";
1213

13-
export const serializerMiddleware = <Input extends object, Output extends object, RuntimeUtils extends EndpointBearer>(
14-
options: V1OrV2Endpoint,
15-
serializer: RequestSerializer<any, RuntimeUtils>
14+
/**
15+
* @internal
16+
*
17+
* Note: 3rd type parameter is deprecated and unused.
18+
*/
19+
export const serializerMiddleware = <Input extends object, Output extends object, _>(
20+
options: V1OrV2Endpoint & SerdeFunctions,
21+
serializer: RequestSerializer<any, SerdeFunctions & EndpointBearer>
1622
): SerializeMiddleware<Input, Output> => (
1723
next: SerializeHandler<Input, Output>,
1824
context: HandlerExecutionContext
@@ -28,7 +34,7 @@ export const serializerMiddleware = <Input extends object, Output extends object
2834
throw new Error("No valid endpoint provider available.");
2935
}
3036

31-
const request = await serializer(args.input, { ...options, endpoint } as RuntimeUtils);
37+
const request = await serializer(args.input, { ...options, endpoint });
3238

3339
return next({
3440
...args,

packages/types/src/serde.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@ export interface StreamCollector {
3131
*
3232
* Request and Response serde util functions and settings for AWS services
3333
*/
34-
export interface SerdeContext extends EndpointBearer {
34+
export interface SerdeContext extends SerdeFunctions, EndpointBearer {
35+
requestHandler: RequestHandler<any, any>;
36+
disableHostPrefix: boolean;
37+
}
38+
39+
/**
40+
* @public
41+
*
42+
* Serde functions from the client config.
43+
*/
44+
export interface SerdeFunctions {
3545
base64Encoder: Encoder;
3646
base64Decoder: Decoder;
3747
utf8Encoder: Encoder;
3848
utf8Decoder: Decoder;
3949
streamCollector: StreamCollector;
40-
requestHandler: RequestHandler<any, any>;
41-
disableHostPrefix: boolean;
4250
}
4351

4452
/**

packages/types/src/util.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,31 @@ export type Exact<Type1, Type2> = [Type1] extends [Type2] ? ([Type2] extends [Ty
1212
/**
1313
* @public
1414
*
15-
* A function that, given a TypedArray of bytes, can produce a string
16-
* representation thereof.
15+
* A function that, given a Uint8Array of bytes, can produce a string
16+
* representation thereof. The function may optionally attempt to
17+
* convert other input types to Uint8Array before encoding.
1718
*
1819
* @example An encoder function that converts bytes to hexadecimal
19-
* representation would return `'deadbeef'` when given
20-
* `new Uint8Array([0xde, 0xad, 0xbe, 0xef])`.
20+
* representation would return `'hello'` when given
21+
* `new Uint8Array([104, 101, 108, 108, 111])`.
2122
*/
2223
export interface Encoder {
23-
(input: Uint8Array): string;
24+
(input: Uint8Array | string | any): string;
2425
}
2526

2627
/**
2728
* @public
2829
*
2930
* A function that, given a string, can derive the bytes represented by that
30-
* string.
31+
* string. The function may optionally attempt to
32+
* convert other input types to string before decoding.
3133
*
3234
* @example A decoder function that converts bytes to hexadecimal
33-
* representation would return `new Uint8Array([0xde, 0xad, 0xbe, 0xef])` when
34-
* given the string `'deadbeef'`.
35+
* representation would return `new Uint8Array([104, 101, 108, 108, 111])` when
36+
* given the string `'hello'`.
3537
*/
3638
export interface Decoder {
37-
(input: string): Uint8Array;
39+
(input: Uint8Array | string | any): Uint8Array;
3840
}
3941

4042
/**

packages/util-base64/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"license": "Apache-2.0",
2424
"dependencies": {
2525
"@smithy/util-buffer-from": "workspace:^",
26+
"@smithy/util-utf8": "workspace:^",
2627
"tslib": "^2.5.0"
2728
},
2829
"devDependencies": {

packages/util-base64/src/toBase64.browser.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1+
import { fromUtf8 } from "@smithy/util-utf8";
2+
13
import { alphabetByValue, bitsPerByte, bitsPerLetter, maxLetterValue } from "./constants.browser";
4+
25
/**
3-
* Converts a Uint8Array of binary data to a base-64 encoded string.
6+
* Converts a Uint8Array of binary data or a utf-8 string to a base-64 encoded string.
47
*
5-
* @param input The binary data to encode
8+
* @param _input - the binary data or string to encode.
9+
* @returns base64 string.
610
*
711
* @see https://tools.ietf.org/html/rfc4648#section-4
812
*/
9-
export function toBase64(input: Uint8Array): string {
13+
export function toBase64(_input: Uint8Array | string): string {
14+
let input: Uint8Array;
15+
if (typeof _input === "string") {
16+
input = fromUtf8(_input);
17+
} else {
18+
input = _input as Uint8Array;
19+
}
1020
let str = "";
1121
for (let i = 0; i < input.length; i += 3) {
1222
let bits = 0;

packages/util-base64/src/toBase64.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { fromArrayBuffer } from "@smithy/util-buffer-from";
2+
import { fromUtf8 } from "@smithy/util-utf8";
23

34
/**
4-
* Converts a Uint8Array of binary data to a base-64 encoded string using
5+
* Converts a Uint8Array of binary data or a utf-8 string to a base-64 encoded string using
56
* Node.JS's `buffer` module.
67
*
7-
* @param input The binary data to encode
8+
* @param _input - the binary data or string to encode.
9+
* @returns base64 string.
810
*/
9-
export const toBase64 = (input: Uint8Array): string =>
10-
fromArrayBuffer(input.buffer, input.byteOffset, input.byteLength).toString("base64");
11+
export const toBase64 = (_input: Uint8Array | string): string => {
12+
let input: Uint8Array;
13+
if (typeof _input === "string") {
14+
input = fromUtf8(_input);
15+
} else {
16+
input = _input as Uint8Array;
17+
}
18+
return fromArrayBuffer(input.buffer, input.byteOffset, input.byteLength).toString("base64");
19+
};
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
export const toUtf8 = (input: Uint8Array): string => new TextDecoder("utf-8").decode(input);
1+
export const toUtf8 = (input: Uint8Array | string): string => {
2+
if (typeof input === "string") {
3+
return input;
4+
}
5+
return new TextDecoder("utf-8").decode(input);
6+
};

packages/util-utf8/src/toUtf8.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { fromArrayBuffer } from "@smithy/util-buffer-from";
22

3-
export const toUtf8 = (input: Uint8Array): string =>
4-
fromArrayBuffer(input.buffer, input.byteOffset, input.byteLength).toString("utf8");
3+
export const toUtf8 = (input: Uint8Array | string): string => {
4+
if (typeof input === "string") {
5+
return input;
6+
}
7+
return fromArrayBuffer(input.buffer, input.byteOffset, input.byteLength).toString("utf8");
8+
};

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,6 +2554,7 @@ __metadata:
25542554
resolution: "@smithy/util-base64@workspace:packages/util-base64"
25552555
dependencies:
25562556
"@smithy/util-buffer-from": "workspace:^"
2557+
"@smithy/util-utf8": "workspace:^"
25572558
"@tsconfig/recommended": 1.0.1
25582559
"@types/node": ^14.14.31
25592560
concurrently: 7.0.0

0 commit comments

Comments
 (0)