Skip to content

Commit 8b4fc1e

Browse files
committed
refactoring to reduce code size
fix base64 alphabetByValue date util testing chore: inline schema magic numbers
1 parent c1544be commit 8b4fc1e

25 files changed

+960
-414
lines changed

.changeset/tame-suns-carry.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@smithy/util-base64": minor
3+
"@smithy/util-stream": minor
4+
"@smithy/core": minor
5+
---
6+
7+
refactoring to reduce code size

packages/core/src/submodules/cbor/CborCodec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NormalizedSchema } from "@smithy/core/schema";
2-
import { generateIdempotencyToken, parseEpochTimestamp } from "@smithy/core/serde";
2+
import { _parseEpochTimestamp, generateIdempotencyToken } from "@smithy/core/serde";
33
import type { Codec, Schema, SerdeFunctions, ShapeDeserializer, ShapeSerializer } from "@smithy/types";
44
import { fromBase64 } from "@smithy/util-base64";
55

@@ -148,7 +148,7 @@ export class CborShapeDeserializer implements ShapeDeserializer {
148148

149149
if (ns.isTimestampSchema() && typeof value === "number") {
150150
// format is ignored.
151-
return parseEpochTimestamp(value);
151+
return _parseEpochTimestamp(value);
152152
}
153153

154154
if (ns.isBlobSchema()) {

packages/core/src/submodules/cbor/SmithyRpcV2CborProtocol.spec.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
import type { ErrorSchema } from "@smithy/core/schema";
21
import { error, list, map, op, SCHEMA, struct, TypeRegistry } from "@smithy/core/schema";
32
import { HttpRequest, HttpResponse } from "@smithy/protocol-http";
4-
import type { ResponseMetadata, RetryableTrait, SchemaRef } from "@smithy/types";
3+
import type {
4+
BlobSchema,
5+
BooleanSchema,
6+
MapSchemaModifier,
7+
NumericSchema,
8+
ResponseMetadata,
9+
RetryableTrait,
10+
SchemaRef,
11+
StringSchema,
12+
TimestampDefaultSchema,
13+
} from "@smithy/types";
514
import { beforeEach, describe, expect, test as it } from "vitest";
615

716
import { cbor } from "./cbor";
@@ -29,8 +38,8 @@ describe(SmithyRpcV2CborProtocol.name, () => {
2938
{},
3039
["timestamp", "blob"],
3140
[
32-
[SCHEMA.TIMESTAMP_DEFAULT, 0],
33-
[SCHEMA.BLOB, 0],
41+
[4 satisfies TimestampDefaultSchema, 0],
42+
[21 satisfies BlobSchema, 0],
3443
]
3544
),
3645
input: {
@@ -56,11 +65,11 @@ describe(SmithyRpcV2CborProtocol.name, () => {
5665
{},
5766
["bool", "timestamp", "blob", "prefixHeaders", "searchParams"],
5867
[
59-
[SCHEMA.BOOLEAN, { httpQuery: "bool" }],
60-
[SCHEMA.TIMESTAMP_DEFAULT, { httpHeader: "timestamp" }],
61-
[SCHEMA.BLOB, { httpHeader: "blob" }],
62-
[SCHEMA.MAP_MODIFIER | SCHEMA.STRING, { httpPrefixHeaders: "anti-" }],
63-
[SCHEMA.MAP_MODIFIER | SCHEMA.STRING, { httpQueryParams: 1 }],
68+
[2 satisfies BooleanSchema, { httpQuery: "bool" }],
69+
[4 satisfies TimestampDefaultSchema, { httpHeader: "timestamp" }],
70+
[21 satisfies BlobSchema, { httpHeader: "blob" }],
71+
[(128 satisfies MapSchemaModifier) | (0 satisfies StringSchema), { httpPrefixHeaders: "anti-" }],
72+
[(128 satisfies MapSchemaModifier) | (0 satisfies StringSchema), { httpQueryParams: 1 }],
6473
]
6574
),
6675
input: {
@@ -104,10 +113,10 @@ describe(SmithyRpcV2CborProtocol.name, () => {
104113
0,
105114
["mySparseList", "myRegularList", "mySparseMap", "myRegularMap"],
106115
[
107-
[() => list("", "MySparseList", { sparse: 1 }, SCHEMA.NUMERIC), {}],
108-
[() => list("", "MyList", {}, SCHEMA.NUMERIC), {}],
109-
[() => map("", "MySparseMap", { sparse: 1 }, SCHEMA.STRING, SCHEMA.NUMERIC), {}],
110-
[() => map("", "MyMap", {}, SCHEMA.STRING, SCHEMA.NUMERIC), {}],
116+
[() => list("", "MySparseList", { sparse: 1 }, 1 satisfies NumericSchema), {}],
117+
[() => list("", "MyList", {}, 1 satisfies NumericSchema), {}],
118+
[() => map("", "MySparseMap", { sparse: 1 }, 0 satisfies StringSchema, 1 satisfies NumericSchema), {}],
119+
[() => map("", "MyMap", {}, 0 satisfies StringSchema, 1 satisfies NumericSchema), {}],
111120
]
112121
),
113122
input: {
@@ -207,10 +216,10 @@ describe(SmithyRpcV2CborProtocol.name, () => {
207216
0,
208217
["mySparseList", "myRegularList", "mySparseMap", "myRegularMap"],
209218
[
210-
[() => list("", "MyList", { sparse: 1 }, SCHEMA.NUMERIC), {}],
211-
[() => list("", "MyList", {}, SCHEMA.NUMERIC), {}],
212-
[() => map("", "MyMap", { sparse: 1 }, SCHEMA.STRING, SCHEMA.NUMERIC), {}],
213-
[() => map("", "MyMap", {}, SCHEMA.STRING, SCHEMA.NUMERIC), {}],
219+
[() => list("", "MyList", { sparse: 1 }, 1 satisfies NumericSchema), {}],
220+
[() => list("", "MyList", {}, 1 satisfies NumericSchema), {}],
221+
[() => map("", "MyMap", { sparse: 1 }, 0 satisfies StringSchema, 1 satisfies NumericSchema), {}],
222+
[() => map("", "MyMap", {}, 0 satisfies StringSchema, 1 satisfies NumericSchema), {}],
214223
]
215224
),
216225
mockOutput: {

packages/core/src/submodules/event-streams/EventStreamSerde.spec.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { cbor, CborCodec, dateToTag } from "@smithy/core/cbor";
2-
import { NormalizedSchema, SCHEMA, sim, struct } from "@smithy/core/schema";
2+
import { NormalizedSchema, sim, struct } from "@smithy/core/schema";
33
import { EventStreamMarshaller } from "@smithy/eventstream-serde-node";
44
import { HttpResponse } from "@smithy/protocol-http";
5-
import type { Message as EventMessage } from "@smithy/types";
5+
import type {
6+
BlobSchema,
7+
Message as EventMessage,
8+
StreamingBlobSchema,
9+
StringSchema,
10+
TimestampEpochSecondsSchema,
11+
} from "@smithy/types";
612
import { fromUtf8, toUtf8 } from "@smithy/util-utf8";
713
import { describe, expect, test as it } from "vitest";
814

@@ -53,9 +59,15 @@ describe(EventStreamSerde.name, () => {
5359
"Payload",
5460
0,
5561
["payload"],
56-
[sim("ns", "StreamingBlobPayload", SCHEMA.STREAMING_BLOB, { eventPayload: 1 })]
62+
[sim("ns", "StreamingBlobPayload", 42 satisfies StreamingBlobSchema, { eventPayload: 1 })]
63+
),
64+
struct(
65+
"ns",
66+
"TextPayload",
67+
0,
68+
["payload"],
69+
[sim("ns", "TextPayload", 0 satisfies StringSchema, { eventPayload: 1 })]
5770
),
58-
struct("ns", "TextPayload", 0, ["payload"], [sim("ns", "TextPayload", SCHEMA.STRING, { eventPayload: 1 })]),
5971
struct(
6072
"ns",
6173
"CustomHeaders",
@@ -73,7 +85,7 @@ describe(EventStreamSerde.name, () => {
7385
// here the non-eventstream members form an initial-request
7486
// or initial-response when present.
7587
["eventStreamMember", "dateMember", "blobMember"],
76-
[eventStreamUnionSchema, SCHEMA.TIMESTAMP_EPOCH_SECONDS, SCHEMA.BLOB]
88+
[eventStreamUnionSchema, 7 satisfies TimestampEpochSecondsSchema, 21 satisfies BlobSchema]
7789
);
7890

7991
describe("serialization", () => {

packages/core/src/submodules/event-streams/EventStreamSerde.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { NormalizedSchema } from "@smithy/core/schema";
2-
import { SCHEMA } from "@smithy/core/schema";
1+
import type { NormalizedSchema, StructureSchema } from "@smithy/core/schema";
32
import type {
3+
DocumentSchema,
44
EventStreamMarshaller,
55
HttpRequest as IHttpRequest,
66
HttpResponse as IHttpResponse,
@@ -232,14 +232,17 @@ export class EventStreamSerde {
232232
let explicitPayloadMember = null as null | string;
233233
let explicitPayloadContentType: undefined | string;
234234

235-
const isKnownSchema = unionSchema.hasMemberSchema(unionMember);
235+
const isKnownSchema = (() => {
236+
const struct = unionSchema.getSchema() as StructureSchema;
237+
return struct.memberNames.includes(unionMember);
238+
})();
236239
const additionalHeaders: MessageHeaders = {};
237240

238241
if (!isKnownSchema) {
239242
// $unknown member
240243
const [type, value] = event[unionMember];
241244
eventType = type;
242-
serializer.write(SCHEMA.DOCUMENT, value);
245+
serializer.write(15 satisfies DocumentSchema, value);
243246
} else {
244247
const eventSchema = unionSchema.getMemberSchema(unionMember);
245248

packages/core/src/submodules/protocols/HttpBindingProtocol.spec.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import { map, op, SCHEMA, struct } from "@smithy/core/schema";
1+
import { map, op, struct } from "@smithy/core/schema";
22
import { HttpResponse } from "@smithy/protocol-http";
33
import type {
44
Codec,
55
CodecSettings,
66
HandlerExecutionContext,
77
HttpResponse as IHttpResponse,
8+
ListSchemaModifier,
9+
MapSchemaModifier,
810
MetadataBearer,
911
OperationSchema,
1012
ResponseMetadata,
1113
Schema,
1214
SerdeFunctions,
1315
ShapeDeserializer,
1416
ShapeSerializer,
17+
StringSchema,
18+
TimestampDefaultSchema,
19+
TimestampEpochSecondsSchema,
1520
} from "@smithy/types";
1621
import { parseUrl } from "@smithy/url-parser/src";
1722
import { describe, expect, test as it } from "vitest";
@@ -32,7 +37,7 @@ describe(HttpBindingProtocol.name, () => {
3237
const settings: CodecSettings = {
3338
timestampFormat: {
3439
useTrait: true,
35-
default: SCHEMA.TIMESTAMP_EPOCH_SECONDS,
40+
default: 7 satisfies TimestampEpochSecondsSchema,
3641
},
3742
httpBindings: true,
3843
};
@@ -82,7 +87,7 @@ describe(HttpBindingProtocol.name, () => {
8287
["timestampList"],
8388
[
8489
[
85-
SCHEMA.LIST_MODIFIER | SCHEMA.TIMESTAMP_DEFAULT,
90+
(64 satisfies ListSchemaModifier) | (4 satisfies TimestampDefaultSchema),
8691
{
8792
httpHeader: "x-timestamplist",
8893
},
@@ -122,7 +127,7 @@ describe(HttpBindingProtocol.name, () => {
122127
["httpPrefixHeaders"],
123128
[
124129
[
125-
SCHEMA.MAP_MODIFIER | SCHEMA.STRING,
130+
(128 satisfies MapSchemaModifier) | (0 satisfies StringSchema),
126131
{
127132
httpPrefixHeaders: "",
128133
},
@@ -175,7 +180,7 @@ describe(HttpBindingProtocol.name, () => {
175180
httpBindings: true,
176181
timestampFormat: {
177182
useTrait: true,
178-
default: SCHEMA.TIMESTAMP_EPOCH_SECONDS,
183+
default: 7 satisfies TimestampEpochSecondsSchema,
179184
},
180185
}),
181186
}

packages/core/src/submodules/protocols/HttpBindingProtocol.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { NormalizedSchema, SCHEMA } from "@smithy/core/schema";
1+
import { NormalizedSchema, translateTraits } from "@smithy/core/schema";
22
import { splitEvery, splitHeader } from "@smithy/core/serde";
33
import { HttpRequest } from "@smithy/protocol-http";
44
import type {
5+
DocumentSchema,
56
Endpoint,
67
EndpointBearer,
78
HandlerExecutionContext,
@@ -11,6 +12,7 @@ import type {
1112
OperationSchema,
1213
Schema,
1314
SerdeFunctions,
15+
TimestampDefaultSchema,
1416
} from "@smithy/types";
1517
import { sdkStreamMixin } from "@smithy/util-stream";
1618

@@ -58,7 +60,7 @@ export abstract class HttpBindingProtocol extends HttpProtocol {
5860
if (endpoint) {
5961
this.updateServiceEndpoint(request, endpoint);
6062
this.setHostPrefix(request, operationSchema, input);
61-
const opTraits = NormalizedSchema.translateTraits(operationSchema.traits);
63+
const opTraits = translateTraits(operationSchema.traits);
6264
if (opTraits.http) {
6365
request.method = opTraits.http[0];
6466
const [path, search] = opTraits.http[1].split("?");
@@ -203,7 +205,7 @@ export abstract class HttpBindingProtocol extends HttpProtocol {
203205
if (response.statusCode >= 300) {
204206
const bytes: Uint8Array = await collectBody(response.body, context);
205207
if (bytes.byteLength > 0) {
206-
Object.assign(dataObject, await deserializer.read(SCHEMA.DOCUMENT, bytes));
208+
Object.assign(dataObject, await deserializer.read(15 satisfies DocumentSchema, bytes));
207209
}
208210
await this.handleError(operationSchema, context, response, dataObject, this.deserializeMetadata(response));
209211
throw new Error("@smithy/core/protocols - HTTP Protocol error handler failed to throw.");
@@ -304,7 +306,7 @@ export abstract class HttpBindingProtocol extends HttpProtocol {
304306
let sections: string[];
305307
if (
306308
headerListValueSchema.isTimestampSchema() &&
307-
headerListValueSchema.getSchema() === SCHEMA.TIMESTAMP_DEFAULT
309+
headerListValueSchema.getSchema() === (4 satisfies TimestampDefaultSchema)
308310
) {
309311
sections = splitEvery(value, ",", 2);
310312
} else {

packages/core/src/submodules/protocols/HttpProtocol.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import { map, SCHEMA, struct } from "@smithy/core/schema";
2-
import type { HandlerExecutionContext, HttpResponse as IHttpResponse, Schema, SerdeFunctions } from "@smithy/types";
1+
import { map, struct } from "@smithy/core/schema";
2+
import type {
3+
HandlerExecutionContext,
4+
HttpResponse as IHttpResponse,
5+
Schema,
6+
SerdeFunctions,
7+
TimestampEpochSecondsSchema,
8+
} from "@smithy/types";
39
import { describe, expect, test as it } from "vitest";
410

511
import { HttpProtocol } from "./HttpProtocol";
@@ -18,7 +24,7 @@ describe(HttpProtocol.name, () => {
1824
httpBindings: true,
1925
timestampFormat: {
2026
useTrait: true,
21-
default: SCHEMA.TIMESTAMP_EPOCH_SECONDS,
27+
default: 7 satisfies TimestampEpochSecondsSchema,
2228
},
2329
}),
2430
});

packages/core/src/submodules/protocols/HttpProtocol.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ export abstract class HttpProtocol implements ClientProtocol<IHttpRequest, IHttp
7373
request.fragment = endpoint.url.hash || void 0;
7474
request.username = endpoint.url.username || void 0;
7575
request.password = endpoint.url.password || void 0;
76+
if (!request.query) {
77+
request.query = {};
78+
}
7679
for (const [k, v] of endpoint.url.searchParams.entries()) {
77-
if (!request.query) {
78-
request.query = {};
79-
}
8080
request.query[k] = v;
8181
}
8282
return request;

packages/core/src/submodules/protocols/RpcProtocol.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { NormalizedSchema, SCHEMA } from "@smithy/core/schema";
1+
import { NormalizedSchema } from "@smithy/core/schema";
22
import { HttpRequest } from "@smithy/protocol-http";
33
import type {
4+
DocumentSchema,
45
Endpoint,
56
EndpointBearer,
67
HandlerExecutionContext,
@@ -101,7 +102,7 @@ export abstract class RpcProtocol extends HttpProtocol {
101102
if (response.statusCode >= 300) {
102103
const bytes: Uint8Array = await collectBody(response.body, context as SerdeFunctions);
103104
if (bytes.byteLength > 0) {
104-
Object.assign(dataObject, await deserializer.read(SCHEMA.DOCUMENT, bytes));
105+
Object.assign(dataObject, await deserializer.read(15 satisfies DocumentSchema, bytes));
105106
}
106107
await this.handleError(operationSchema, context, response, dataObject, this.deserializeMetadata(response));
107108
throw new Error("@smithy/core/protocols - RPC Protocol error handler failed to throw.");

0 commit comments

Comments
 (0)