From 56484e205fffeebe7f69aa65214e3d9ec88d6e1e Mon Sep 17 00:00:00 2001 From: George Fu Date: Mon, 1 Dec 2025 16:26:37 -0500 Subject: [PATCH 1/2] chore(core/protocols): dynamodb serde performance adjustments --- clients/client-dynamodb/package.json | 1 + .../src/runtimeConfig.shared.ts | 2 + .../typescript/codegen/AddProtocolConfig.java | 52 +++-- .../aws/typescript/codegen/AwsDependency.java | 4 +- .../protocols/json/AwsJson1_0Protocol.ts | 4 + .../protocols/json/AwsJson1_1Protocol.ts | 4 + .../protocols/json/AwsJsonRpcProtocol.ts | 18 +- .../protocols/json/JsonShapeDeserializer.ts | 68 +++--- .../protocols/json/JsonShapeSerializer.ts | 148 ++++++++----- .../protocols/structIterator.spec.ts | 14 +- .../submodules/protocols/structIterator.ts | 16 +- packages/dynamodb-codec/CHANGELOG.md | 0 packages/dynamodb-codec/LICENSE | 201 ++++++++++++++++++ packages/dynamodb-codec/README.md | 4 + packages/dynamodb-codec/package.json | 61 ++++++ .../src/codec/DynamoDBJsonCodec.spec.ts | 7 + .../src/codec/DynamoDBJsonCodec.ts | 138 ++++++++++++ packages/dynamodb-codec/src/index.ts | 1 + packages/dynamodb-codec/tsconfig.cjs.json | 10 + packages/dynamodb-codec/tsconfig.es.json | 11 + packages/dynamodb-codec/tsconfig.types.json | 9 + packages/dynamodb-codec/vitest.config.mts | 9 + scripts/validation/api.json | 3 + yarn.lock | 21 ++ 24 files changed, 680 insertions(+), 126 deletions(-) create mode 100644 packages/dynamodb-codec/CHANGELOG.md create mode 100644 packages/dynamodb-codec/LICENSE create mode 100644 packages/dynamodb-codec/README.md create mode 100644 packages/dynamodb-codec/package.json create mode 100644 packages/dynamodb-codec/src/codec/DynamoDBJsonCodec.spec.ts create mode 100644 packages/dynamodb-codec/src/codec/DynamoDBJsonCodec.ts create mode 100644 packages/dynamodb-codec/src/index.ts create mode 100644 packages/dynamodb-codec/tsconfig.cjs.json create mode 100644 packages/dynamodb-codec/tsconfig.es.json create mode 100644 packages/dynamodb-codec/tsconfig.types.json create mode 100644 packages/dynamodb-codec/vitest.config.mts diff --git a/clients/client-dynamodb/package.json b/clients/client-dynamodb/package.json index 8c5f753ff294..0108c121fc64 100644 --- a/clients/client-dynamodb/package.json +++ b/clients/client-dynamodb/package.json @@ -24,6 +24,7 @@ "@aws-crypto/sha256-js": "5.2.0", "@aws-sdk/core": "*", "@aws-sdk/credential-provider-node": "*", + "@aws-sdk/dynamodb-codec": "*", "@aws-sdk/middleware-endpoint-discovery": "*", "@aws-sdk/middleware-host-header": "*", "@aws-sdk/middleware-logger": "*", diff --git a/clients/client-dynamodb/src/runtimeConfig.shared.ts b/clients/client-dynamodb/src/runtimeConfig.shared.ts index b3b475c82399..4e1ac6de99ab 100644 --- a/clients/client-dynamodb/src/runtimeConfig.shared.ts +++ b/clients/client-dynamodb/src/runtimeConfig.shared.ts @@ -1,6 +1,7 @@ // smithy-typescript generated code import { AwsSdkSigV4Signer } from "@aws-sdk/core"; import { AwsJson1_0Protocol } from "@aws-sdk/core/protocols"; +import { DynamoDBJsonCodec } from "@aws-sdk/dynamodb-codec"; import { NoOpLogger } from "@smithy/smithy-client"; import { IdentityProviderConfig } from "@smithy/types"; import { parseUrl } from "@smithy/url-parser"; @@ -37,6 +38,7 @@ export const getRuntimeConfig = (config: DynamoDBClientConfig) => { defaultNamespace: "com.amazonaws.dynamodb", serviceTarget: "DynamoDB_20120810", awsQueryCompatible: false, + jsonCodec: new DynamoDBJsonCodec(), }), serviceId: config?.serviceId ?? "DynamoDB", urlParser: config?.urlParser ?? parseUrl, diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddProtocolConfig.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddProtocolConfig.java index 982d9e2812e7..26e3730d04ed 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddProtocolConfig.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddProtocolConfig.java @@ -36,11 +36,18 @@ */ @SmithyInternalApi public final class AddProtocolConfig implements TypeScriptIntegration { - static { init(); } + public static final Map> CUSTOMIZATIONS = MapUtils.of( + ShapeId.from("com.amazonaws.dynamodb#DynamoDB_20120810"), + writer -> { + writer.addImport("DynamoDBJsonCodec", null, AwsDependency.DYNAMODB_CODEC); + writer.write("jsonCodec: new DynamoDBJsonCodec(),"); + } + ); + static void init() { List allowed = List.of( AwsJson1_0Trait.ID, @@ -114,7 +121,8 @@ public Map> getRuntimeConfigWriters( AwsDependency.AWS_SDK_CORE, "/protocols"); writer.write(""" new AwsRestXmlProtocol({ - defaultNamespace: $S, xmlNamespace: $S, + defaultNamespace: $S, + xmlNamespace: $S, })""", namespace, xmlns @@ -132,7 +140,7 @@ public Map> getRuntimeConfigWriters( new AwsQueryProtocol({ defaultNamespace: $S, xmlNamespace: $S, - version: $S + version: $S, })""", namespace, xmlns, @@ -151,7 +159,7 @@ public Map> getRuntimeConfigWriters( new AwsEc2QueryProtocol({ defaultNamespace: $S, xmlNamespace: $S, - version: $S + version: $S, })""", namespace, xmlns, @@ -174,16 +182,22 @@ public Map> getRuntimeConfigWriters( writer.addImportSubmodule( "AwsJson1_0Protocol", null, AwsDependency.AWS_SDK_CORE, "/protocols"); - writer.write( + writer.openBlock( """ new AwsJson1_0Protocol({ - defaultNamespace: $S, - serviceTarget: $S, - awsQueryCompatible: $L + defaultNamespace: $S, + serviceTarget: $S, + awsQueryCompatible: $L,""", + """ })""", namespace, rpcTarget, - awsQueryCompat + awsQueryCompat, + () -> { + if (CUSTOMIZATIONS.containsKey(settings.getService())) { + CUSTOMIZATIONS.get(settings.getService()).accept(writer); + } + } ); } ); @@ -193,16 +207,22 @@ public Map> getRuntimeConfigWriters( writer.addImportSubmodule( "AwsJson1_1Protocol", null, AwsDependency.AWS_SDK_CORE, "/protocols"); - writer.write( + writer.openBlock( """ new AwsJson1_1Protocol({ - defaultNamespace: $S, - serviceTarget: $S, - awsQueryCompatible: $L + defaultNamespace: $S, + serviceTarget: $S, + awsQueryCompatible: $L,""", + """ })""", namespace, rpcTarget, - awsQueryCompat + awsQueryCompat, + () -> { + if (CUSTOMIZATIONS.containsKey(settings.getService())) { + CUSTOMIZATIONS.get(settings.getService()).accept(writer); + } + } ); } ); @@ -215,8 +235,8 @@ public Map> getRuntimeConfigWriters( writer.write( """ new AwsSmithyRpcV2CborProtocol({ - defaultNamespace: $S, - awsQueryCompatible: $L + defaultNamespace: $S, + awsQueryCompatible: $L, })""", namespace, awsQueryCompat diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsDependency.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsDependency.java index cbc08a4314da..3f01bc36e4ac 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsDependency.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsDependency.java @@ -101,8 +101,8 @@ public enum AwsDependency implements Dependency { REGION_CONFIG_RESOLVER(NORMAL_DEPENDENCY, "@aws-sdk/region-config-resolver"), CLIENT_DYNAMODB_PEER(PEER_DEPENDENCY, "@aws-sdk/client-dynamodb", "^3.0.0"), - UTIL_DYNAMODB(NORMAL_DEPENDENCY, "@aws-sdk/util-dynamodb", "*"); - + UTIL_DYNAMODB(NORMAL_DEPENDENCY, "@aws-sdk/util-dynamodb", "*"), + DYNAMODB_CODEC(NORMAL_DEPENDENCY, "@aws-sdk/dynamodb-codec"); public final String packageName; public final String version; diff --git a/packages/core/src/submodules/protocols/json/AwsJson1_0Protocol.ts b/packages/core/src/submodules/protocols/json/AwsJson1_0Protocol.ts index 3e8390bed26f..8287c5c57c9f 100644 --- a/packages/core/src/submodules/protocols/json/AwsJson1_0Protocol.ts +++ b/packages/core/src/submodules/protocols/json/AwsJson1_0Protocol.ts @@ -1,4 +1,5 @@ import { AwsJsonRpcProtocol } from "./AwsJsonRpcProtocol"; +import type { JsonCodec } from "./JsonCodec"; /** * @public @@ -9,15 +10,18 @@ export class AwsJson1_0Protocol extends AwsJsonRpcProtocol { defaultNamespace, serviceTarget, awsQueryCompatible, + jsonCodec, }: { defaultNamespace: string; serviceTarget: string; awsQueryCompatible?: boolean; + jsonCodec?: JsonCodec; }) { super({ defaultNamespace, serviceTarget, awsQueryCompatible, + jsonCodec, }); } diff --git a/packages/core/src/submodules/protocols/json/AwsJson1_1Protocol.ts b/packages/core/src/submodules/protocols/json/AwsJson1_1Protocol.ts index d848edc24f72..de0929c9ba2e 100644 --- a/packages/core/src/submodules/protocols/json/AwsJson1_1Protocol.ts +++ b/packages/core/src/submodules/protocols/json/AwsJson1_1Protocol.ts @@ -1,4 +1,5 @@ import { AwsJsonRpcProtocol } from "./AwsJsonRpcProtocol"; +import type { JsonCodec } from "./JsonCodec"; /** * @public @@ -9,15 +10,18 @@ export class AwsJson1_1Protocol extends AwsJsonRpcProtocol { defaultNamespace, serviceTarget, awsQueryCompatible, + jsonCodec, }: { defaultNamespace: string; serviceTarget: string; awsQueryCompatible?: boolean; + jsonCodec?: JsonCodec; }) { super({ defaultNamespace, serviceTarget, awsQueryCompatible, + jsonCodec, }); } diff --git a/packages/core/src/submodules/protocols/json/AwsJsonRpcProtocol.ts b/packages/core/src/submodules/protocols/json/AwsJsonRpcProtocol.ts index f12b832f2e0e..ece94fe900cd 100644 --- a/packages/core/src/submodules/protocols/json/AwsJsonRpcProtocol.ts +++ b/packages/core/src/submodules/protocols/json/AwsJsonRpcProtocol.ts @@ -32,22 +32,26 @@ export abstract class AwsJsonRpcProtocol extends RpcProtocol { defaultNamespace, serviceTarget, awsQueryCompatible, + jsonCodec, }: { defaultNamespace: string; serviceTarget: string; awsQueryCompatible?: boolean; + jsonCodec?: JsonCodec; }) { super({ defaultNamespace, }); this.serviceTarget = serviceTarget; - this.codec = new JsonCodec({ - timestampFormat: { - useTrait: true, - default: 7 as const satisfies TimestampEpochSecondsSchema, - }, - jsonName: false, - }); + this.codec = + jsonCodec ?? + new JsonCodec({ + timestampFormat: { + useTrait: true, + default: 7 as const satisfies TimestampEpochSecondsSchema, + }, + jsonName: false, + }); this.serializer = this.codec.createSerializer(); this.deserializer = this.codec.createDeserializer(); this.awsQueryCompatible = !!awsQueryCompatible; diff --git a/packages/core/src/submodules/protocols/json/JsonShapeDeserializer.ts b/packages/core/src/submodules/protocols/json/JsonShapeDeserializer.ts index 393549f486f1..de37a1a89873 100644 --- a/packages/core/src/submodules/protocols/json/JsonShapeDeserializer.ts +++ b/packages/core/src/submodules/protocols/json/JsonShapeDeserializer.ts @@ -42,49 +42,51 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe return this._read(schema, data); } - private _read(schema: Schema, value: unknown): any { + protected _read(schema: Schema, value: unknown): any { const isObject = value !== null && typeof value === "object"; const ns = NormalizedSchema.of(schema); - // === aggregate types === - if (ns.isListSchema() && Array.isArray(value)) { - const listMember = ns.getValueSchema(); - const out = [] as any[]; - const sparse = !!ns.getMergedTraits().sparse; - for (const item of value) { - if (sparse || item != null) { - out.push(this._read(listMember, item)); + if (isObject) { + if (ns.isStructSchema()) { + const out = {} as any; + for (const [memberName, memberSchema] of deserializingStructIterator( + ns, + value, + this.settings.jsonName ? "jsonName" : false + )) { + const fromKey = this.settings.jsonName ? memberSchema.getMergedTraits().jsonName ?? memberName : memberName; + const deserializedValue = this._read(memberSchema, (value as any)[fromKey]); + if (deserializedValue != null) { + out[memberName] = deserializedValue; + } } + return out; } - return out; - } else if (ns.isMapSchema() && isObject) { - const mapMember = ns.getValueSchema(); - const out = {} as any; - const sparse = !!ns.getMergedTraits().sparse; - for (const [_k, _v] of Object.entries(value)) { - if (sparse || _v != null) { - out[_k] = this._read(mapMember, _v); + if (Array.isArray(value) && ns.isListSchema()) { + const listMember = ns.getValueSchema(); + const out = [] as any[]; + const sparse = !!ns.getMergedTraits().sparse; + for (const item of value) { + if (sparse || item != null) { + out.push(this._read(listMember, item)); + } } + return out; } - return out; - } else if (ns.isStructSchema() && isObject) { - const out = {} as any; - for (const [memberName, memberSchema] of deserializingStructIterator( - ns, - value, - this.settings.jsonName ? "jsonName" : false - )) { - const fromKey = this.settings.jsonName ? memberSchema.getMergedTraits().jsonName ?? memberName : memberName; - const deserializedValue = this._read(memberSchema, (value as any)[fromKey]); - if (deserializedValue != null) { - out[memberName] = deserializedValue; + if (ns.isMapSchema()) { + const mapMember = ns.getValueSchema(); + const out = {} as any; + const sparse = !!ns.getMergedTraits().sparse; + for (const [_k, _v] of Object.entries(value)) { + if (sparse || _v != null) { + out[_k] = this._read(mapMember, _v); + } } + return out; } - return out; } - // === simple types === if (ns.isBlobSchema() && typeof value === "string") { return fromBase64(value); } @@ -95,6 +97,7 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe if (isJson) { return LazyJsonString.from(value); } + return value; } if (ns.isTimestampSchema() && value != null) { @@ -136,6 +139,7 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe case "NaN": return NaN; } + return value; } if (ns.isDocumentSchema()) { @@ -154,7 +158,7 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe } } - // covers string, numeric, boolean, document, bigDecimal + // covers boolean, bigint (long/BigInt), bigDecimal return value; } } diff --git a/packages/core/src/submodules/protocols/json/JsonShapeSerializer.ts b/packages/core/src/submodules/protocols/json/JsonShapeSerializer.ts index 2ea82502bb27..48a8d955b284 100644 --- a/packages/core/src/submodules/protocols/json/JsonShapeSerializer.ts +++ b/packages/core/src/submodules/protocols/json/JsonShapeSerializer.ts @@ -19,8 +19,13 @@ import { JsonReplacer } from "./jsonReplacer"; * @public */ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSerializer { - private buffer: any; - private rootSchema: NormalizedSchema | undefined; + /** + * Write buffer. Reused per value serialization pass. + * In the initial implementation, this is not an incremental buffer. + */ + protected buffer: any; + protected useReplacer = false; + protected rootSchema: NormalizedSchema | undefined; public constructor(public readonly settings: JsonSettings) { super(); @@ -42,10 +47,14 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri } public flush(): string { - const { rootSchema } = this; + const { rootSchema, useReplacer } = this; this.rootSchema = undefined; + this.useReplacer = false; if (rootSchema?.isStructSchema() || rootSchema?.isDocumentSchema()) { + if (!useReplacer) { + return JSON.stringify(this.buffer); + } const replacer = new JsonReplacer(); return replacer.replaceInJson(JSON.stringify(this.buffer, replacer.createReplacer(), 0)); } @@ -53,78 +62,81 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri return this.buffer; } - private _write(schema: Schema, value: unknown, container?: NormalizedSchema): any { + /** + * Order if-statements by order of likelihood. + */ + protected _write(schema: Schema, value: unknown, container?: NormalizedSchema): any { const isObject = value !== null && typeof value === "object"; const ns = NormalizedSchema.of(schema); - // === aggregate types === - if (ns.isListSchema() && Array.isArray(value)) { - const listMember = ns.getValueSchema(); - const out = [] as any[]; - const sparse = !!ns.getMergedTraits().sparse; - for (const item of value) { - if (sparse || item != null) { - out.push(this._write(listMember, item)); + if (isObject) { + if (ns.isStructSchema()) { + const out = {} as any; + for (const [memberName, memberSchema] of serializingStructIterator(ns, value)) { + const serializableValue = this._write(memberSchema, (value as any)[memberName], ns); + if (serializableValue !== undefined) { + const jsonName = memberSchema.getMergedTraits().jsonName; + const targetKey = this.settings.jsonName ? jsonName ?? memberName : memberName; + out[targetKey] = serializableValue; + } } + return out; } - return out; - } else if (ns.isMapSchema() && isObject) { - const mapMember = ns.getValueSchema(); - const out = {} as any; - const sparse = !!ns.getMergedTraits().sparse; - for (const [_k, _v] of Object.entries(value)) { - if (sparse || _v != null) { - out[_k] = this._write(mapMember, _v); + + if (Array.isArray(value) && ns.isListSchema()) { + const listMember = ns.getValueSchema(); + const out = [] as any[]; + const sparse = !!ns.getMergedTraits().sparse; + for (const item of value) { + if (sparse || item != null) { + out.push(this._write(listMember, item)); + } } + return out; } - return out; - } else if (ns.isStructSchema() && isObject) { - const out = {} as any; - for (const [memberName, memberSchema] of serializingStructIterator(ns, value)) { - const serializableValue = this._write(memberSchema, (value as any)[memberName], ns); - if (serializableValue !== undefined) { - const targetKey = this.settings.jsonName ? memberSchema.getMergedTraits().jsonName ?? memberName : memberName; - out[targetKey] = serializableValue; + + if (ns.isMapSchema()) { + const mapMember = ns.getValueSchema(); + const out = {} as any; + const sparse = !!ns.getMergedTraits().sparse; + for (const [_k, _v] of Object.entries(value)) { + if (sparse || _v != null) { + out[_k] = this._write(mapMember, _v); + } } + return out; } - return out; - } - // === simple types === - if (value === null && container?.isStructSchema()) { - return void 0; - } + if (value instanceof Uint8Array && (ns.isBlobSchema() || ns.isDocumentSchema())) { + if (ns === this.rootSchema) { + return value; + } + return (this.serdeContext?.base64Encoder ?? toBase64)(value); + } - if ( - (ns.isBlobSchema() && (value instanceof Uint8Array || typeof value === "string")) || - (ns.isDocumentSchema() && value instanceof Uint8Array) - ) { - if (ns === this.rootSchema) { - return value; + if (value instanceof Date && (ns.isTimestampSchema() || ns.isDocumentSchema())) { + const format = determineTimestampFormat(ns, this.settings); + switch (format) { + case 5 satisfies TimestampDateTimeSchema: + return value.toISOString().replace(".000Z", "Z"); + case 6 satisfies TimestampHttpDateSchema: + return dateToUtcString(value); + case 7 satisfies TimestampEpochSecondsSchema: + return value.getTime() / 1000; + default: + console.warn("Missing timestamp format, using epoch seconds", value); + return value.getTime() / 1000; + } } - return (this.serdeContext?.base64Encoder ?? toBase64)(value); - } - if ((ns.isTimestampSchema() || ns.isDocumentSchema()) && value instanceof Date) { - const format = determineTimestampFormat(ns, this.settings); - switch (format) { - case 5 satisfies TimestampDateTimeSchema: - return value.toISOString().replace(".000Z", "Z"); - case 6 satisfies TimestampHttpDateSchema: - return dateToUtcString(value); - case 7 satisfies TimestampEpochSecondsSchema: - return value.getTime() / 1000; - default: - console.warn("Missing timestamp format, using epoch seconds", value); - return value.getTime() / 1000; + if (value instanceof NumericValue) { + this.useReplacer = true; } } - if (ns.isNumericSchema() && typeof value === "number") { - if (Math.abs(value) === Infinity || isNaN(value)) { - return String(value); - } + if (value === null && container?.isStructSchema()) { + return void 0; } if (ns.isStringSchema()) { @@ -140,6 +152,25 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri return LazyJsonString.from(value); } } + return value; + } + + if (typeof value === "number" && ns.isNumericSchema()) { + if (Math.abs(value) === Infinity || isNaN(value)) { + return String(value); + } + return value; + } + + if (typeof value === "string" && ns.isBlobSchema()) { + if (ns === this.rootSchema) { + return value; + } + return (this.serdeContext?.base64Encoder ?? toBase64)(value); + } + + if (typeof value === "bigint") { + this.useReplacer = true; } if (ns.isDocumentSchema()) { @@ -147,6 +178,7 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri const out = Array.isArray(value) ? [] : ({} as any); for (const [k, v] of Object.entries(value)) { if (v instanceof NumericValue) { + this.useReplacer = true; out[k] = v; } else { out[k] = this._write(ns, v); diff --git a/packages/core/src/submodules/protocols/structIterator.spec.ts b/packages/core/src/submodules/protocols/structIterator.spec.ts index 8f94d672d85e..75a9b0213f4e 100644 --- a/packages/core/src/submodules/protocols/structIterator.spec.ts +++ b/packages/core/src/submodules/protocols/structIterator.spec.ts @@ -95,25 +95,23 @@ describe("filtered struct iteration", () => { }); it("halts iteration once all keys from the source object have been iterated", () => { - vi.spyOn(NormalizedSchema.prototype, "getMergedTraits"); + vi.spyOn(NormalizedSchema, Symbol.hasInstance); + // regular iteration iterates all schema keys expect([...ns.structIterator()].map(([k]) => k)).toEqual(["a", "b", "c", "e", "f", "g", "h", "i", "j", "k", "l"]); - expect(NormalizedSchema.prototype.getMergedTraits).toHaveBeenCalledTimes(0); + expect(NormalizedSchema[Symbol.hasInstance]).toHaveBeenCalledTimes(22); vi.resetAllMocks(); expect([...deserializingStructIterator(ns, { a: "a" }, "jsonName")].map(([k]) => k)).toEqual(["a"]); - // only 1 call because iteration halts after 'a', since the total key count was 1. - expect(NormalizedSchema.prototype.getMergedTraits).toHaveBeenCalledTimes(1); + expect(NormalizedSchema[Symbol.hasInstance]).toHaveBeenCalledTimes(1); vi.resetAllMocks(); expect([...deserializingStructIterator(ns, { a: "a", l: "l" }, "jsonName")].map(([k]) => k)).toEqual(["a", "l"]); - // 11 calls because iteration continues in member order, and 'l' is the last key. - expect(NormalizedSchema.prototype.getMergedTraits).toHaveBeenCalledTimes(11); + expect(NormalizedSchema[Symbol.hasInstance]).toHaveBeenCalledTimes(11); vi.resetAllMocks(); expect([...deserializingStructIterator(ns, { a: "a", l: "l" }, false)].map(([k]) => k)).toEqual(["a", "l"]); - // no calls because no jsonName checking is involved. - expect(NormalizedSchema.prototype.getMergedTraits).toHaveBeenCalledTimes(0); + expect(NormalizedSchema[Symbol.hasInstance]).toHaveBeenCalledTimes(11); }); }); }); diff --git a/packages/core/src/submodules/protocols/structIterator.ts b/packages/core/src/submodules/protocols/structIterator.ts index fdbd93874cd0..6caed0a63d09 100644 --- a/packages/core/src/submodules/protocols/structIterator.ts +++ b/packages/core/src/submodules/protocols/structIterator.ts @@ -20,10 +20,14 @@ export function* serializingStructIterator(ns: NormalizedSchema, sourceObject: S const struct = ns.getSchema() as StaticStructureSchema; for (let i = 0; i < struct[4].length; ++i) { const key = struct[4][i]; - const memberNs = new (NormalizedSchema as any)([struct[5][i], 0], key); + const memberSchema = struct[5][i]; + + const memberNs = new (NormalizedSchema as any)([memberSchema, 0], key); + if (!(key in sourceObject) && !memberNs.isIdempotencyToken()) { continue; } + yield [key, memberNs]; } } @@ -49,20 +53,26 @@ export function* deserializingStructIterator( return; } const struct = ns.getSchema() as StaticStructureSchema; - let keysRemaining = Object.keys(sourceObject).length; + let keysRemaining = Object.keys(sourceObject).filter((k) => k !== "__type").length; + for (let i = 0; i < struct[4].length; ++i) { if (keysRemaining === 0) { break; } const key = struct[4][i]; - const memberNs = new (NormalizedSchema as any)([struct[5][i], 0], key); + const memberSchema = struct[5][i]; + const memberNs = new (NormalizedSchema as any)([memberSchema, 0], key); + let serializationKey = key; + if (nameTrait) { serializationKey = memberNs.getMergedTraits()[nameTrait] ?? key; } + if (!(serializationKey in sourceObject)) { continue; } + yield [key, memberNs]; keysRemaining -= 1; } diff --git a/packages/dynamodb-codec/CHANGELOG.md b/packages/dynamodb-codec/CHANGELOG.md new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/dynamodb-codec/LICENSE b/packages/dynamodb-codec/LICENSE new file mode 100644 index 000000000000..7b6491ba7876 --- /dev/null +++ b/packages/dynamodb-codec/LICENSE @@ -0,0 +1,201 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/packages/dynamodb-codec/README.md b/packages/dynamodb-codec/README.md new file mode 100644 index 000000000000..3fcf452386f2 --- /dev/null +++ b/packages/dynamodb-codec/README.md @@ -0,0 +1,4 @@ +# @aws-sdk/dynamodb-codec + +This is an internal dependency used with `@aws-sdk/client-dynamodb`. +You should not take a direct dependency on this package. diff --git a/packages/dynamodb-codec/package.json b/packages/dynamodb-codec/package.json new file mode 100644 index 000000000000..074b06df30d6 --- /dev/null +++ b/packages/dynamodb-codec/package.json @@ -0,0 +1,61 @@ +{ + "name": "@aws-sdk/dynamodb-codec", + "version": "3.0.0", + "scripts": { + "build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'", + "build:cjs": "node ../../scripts/compilation/inline dynamodb-codec", + "build:es": "tsc -p tsconfig.es.json", + "build:include:deps": "lerna run --scope $npm_package_name --include-dependencies build", + "build:types": "tsc -p tsconfig.types.json", + "build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4", + "clean": "rimraf ./dist-* && rimraf *.tsbuildinfo", + "test": "yarn g:vitest run", + "test:watch": "yarn g:vitest watch" + }, + "main": "./dist-cjs/index.js", + "module": "./dist-es/index.js", + "types": "./dist-types/index.d.ts", + "sideEffects": false, + "author": { + "name": "AWS SDK for JavaScript Team", + "url": "https://aws.amazon.com/javascript/" + }, + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "*", + "@smithy/core": "^3.18.5", + "@smithy/smithy-client": "^4.9.8", + "@smithy/types": "^4.9.0", + "@smithy/util-base64": "^4.3.0", + "tslib": "^2.6.2" + }, + "devDependencies": { + "@tsconfig/recommended": "1.0.1", + "concurrently": "7.0.0", + "downlevel-dts": "0.10.1", + "rimraf": "3.0.2", + "typescript": "~5.8.3" + }, + "peerDependencies": { + "@aws-sdk/client-dynamodb": "*" + }, + "engines": { + "node": ">=18.0.0" + }, + "typesVersions": { + "<4.0": { + "dist-types/*": [ + "dist-types/ts3.4/*" + ] + } + }, + "files": [ + "dist-*/**" + ], + "homepage": "https://github.com/aws/aws-sdk-js-v3/tree/main/packages/dynamodb-codec", + "repository": { + "type": "git", + "url": "https://github.com/aws/aws-sdk-js-v3.git", + "directory": "packages/dynamodb-codec" + } +} diff --git a/packages/dynamodb-codec/src/codec/DynamoDBJsonCodec.spec.ts b/packages/dynamodb-codec/src/codec/DynamoDBJsonCodec.spec.ts new file mode 100644 index 000000000000..834ee2a569cc --- /dev/null +++ b/packages/dynamodb-codec/src/codec/DynamoDBJsonCodec.spec.ts @@ -0,0 +1,7 @@ +import { describe, test as it } from "vitest"; + +import { DynamoDBJsonCodec } from "./DynamoDBJsonCodec"; + +describe(DynamoDBJsonCodec.name, () => { + it("placeholder", async () => {}); +}); diff --git a/packages/dynamodb-codec/src/codec/DynamoDBJsonCodec.ts b/packages/dynamodb-codec/src/codec/DynamoDBJsonCodec.ts new file mode 100644 index 000000000000..9167fc0aa434 --- /dev/null +++ b/packages/dynamodb-codec/src/codec/DynamoDBJsonCodec.ts @@ -0,0 +1,138 @@ +import { JsonCodec, JsonShapeDeserializer, JsonShapeSerializer } from "@aws-sdk/core/protocols"; +import { NormalizedSchema } from "@smithy/core/schema"; +import { _json } from "@smithy/smithy-client"; +import type { Schema } from "@smithy/types"; +import { fromBase64, toBase64 } from "@smithy/util-base64"; + +/** + * A throughput optimized version of the AWS JSON Codec + * for use with Amazon DynamoDB in JSON RPC mode. + * + * @internal + */ +export class DynamoDBJsonCodec extends JsonCodec { + public constructor() { + super({ + timestampFormat: { + useTrait: true, + default: 7 as const, + }, + jsonName: false, + }); + } + + /** + * @override + */ + public createSerializer(): JsonShapeSerializer { + const serializer = new DynamoDBJsonShapeSerializer(this.settings); + serializer.setSerdeContext(this.serdeContext!); + return serializer; + } + + /** + * @override + */ + public createDeserializer(): JsonShapeDeserializer { + const deserializer = new DynamoDBJsonShapeDeserializer(this.settings); + deserializer.setSerdeContext(this.serdeContext!); + return deserializer; + } +} + +const ATTRIBUTE_VALUE = "com.amazonaws.dynamodb#AttributeValue"; + +type AttributeValueInput = { + B?: Uint8Array | string; + BS?: Array; + L?: Array; + M?: Record; +}; + +type AttributeValueOutput = { + B?: Uint8Array; + BS?: Uint8Array[]; + L?: Array; + M?: Record; +}; + +type SerializedAttributeValue = { + B?: string; + BS?: string[]; + L?: Array; + M?: Record; +}; + +/** + * @internal + */ +class DynamoDBJsonShapeSerializer extends JsonShapeSerializer { + /** + * @override + */ + protected _write(schema: Schema, value: unknown, container?: NormalizedSchema): any { + const ns = NormalizedSchema.of(schema); + if (ns.isStructSchema() && ns.getName(true) === ATTRIBUTE_VALUE) { + if (value && typeof value === "object") { + const av = value as AttributeValueInput; + const out: SerializedAttributeValue = _json(av); + const base64Encode = this.serdeContext?.base64Encoder ?? toBase64; + if (av.B instanceof Uint8Array) { + out.B = base64Encode(av.B); + } + if (Array.isArray(av.BS)) { + out.BS = av.BS.map(base64Encode); + } + if (Array.isArray(av.L)) { + out.L = av.L.filter((v) => v != null).map((v) => this._write(ns, v, container)); + } + if (av.M && typeof av.M === "object") { + out.M = {}; + for (const [k, v] of Object.entries(av.M)) { + if (v != null) { + out.M[k] = this._write(ns, v, container); + } + } + } + return out; + } + } + return super._write(ns, value, container); + } +} + +/** + * @internal + */ +class DynamoDBJsonShapeDeserializer extends JsonShapeDeserializer { + /** + * @override + */ + protected _read(schema: Schema, value: unknown): any { + const ns = NormalizedSchema.of(schema); + if (ns.isStructSchema() && ns.getName(true) === ATTRIBUTE_VALUE) { + if (value && typeof value === "object") { + const av = value as SerializedAttributeValue; + const out: AttributeValueOutput = _json(av); + const base64Decoder = this.serdeContext?.base64Decoder ?? fromBase64; + if (typeof av.B === "string") { + out.B = base64Decoder(av.B); + } + if (Array.isArray(av.BS)) { + out.BS = av.BS.map(base64Decoder); + } + if (Array.isArray(av.L)) { + out.L = av.L.map((v) => this._read(ns, v)); + } + if (av.M && typeof av.M === "object") { + out.M = {}; + for (const [k, v] of Object.entries(av.M)) { + out.M[k] = this._read(ns, v); + } + } + return out; + } + } + return super._read(ns, value); + } +} diff --git a/packages/dynamodb-codec/src/index.ts b/packages/dynamodb-codec/src/index.ts new file mode 100644 index 000000000000..e6413c5a9132 --- /dev/null +++ b/packages/dynamodb-codec/src/index.ts @@ -0,0 +1 @@ +export * from "./codec/DynamoDBJsonCodec"; diff --git a/packages/dynamodb-codec/tsconfig.cjs.json b/packages/dynamodb-codec/tsconfig.cjs.json new file mode 100644 index 000000000000..d863b538bd61 --- /dev/null +++ b/packages/dynamodb-codec/tsconfig.cjs.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "outDir": "dist-cjs", + "rootDir": "src", + "noCheck": true + }, + "extends": "../../tsconfig.cjs.json", + "include": ["src/"] +} diff --git a/packages/dynamodb-codec/tsconfig.es.json b/packages/dynamodb-codec/tsconfig.es.json new file mode 100644 index 000000000000..4d73a8e45a79 --- /dev/null +++ b/packages/dynamodb-codec/tsconfig.es.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "lib": ["dom"], + "outDir": "dist-es", + "rootDir": "src", + "noCheck": true + }, + "extends": "../../tsconfig.es.json", + "include": ["src/"] +} diff --git a/packages/dynamodb-codec/tsconfig.types.json b/packages/dynamodb-codec/tsconfig.types.json new file mode 100644 index 000000000000..6cdf9f52ea06 --- /dev/null +++ b/packages/dynamodb-codec/tsconfig.types.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "declarationDir": "dist-types", + "rootDir": "src" + }, + "extends": "../../tsconfig.types.json", + "include": ["src/"] +} diff --git a/packages/dynamodb-codec/vitest.config.mts b/packages/dynamodb-codec/vitest.config.mts new file mode 100644 index 000000000000..4e46707824a5 --- /dev/null +++ b/packages/dynamodb-codec/vitest.config.mts @@ -0,0 +1,9 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + exclude: ["**/*.{integ,e2e,browser}.spec.ts"], + include: ["**/*.spec.ts"], + environment: "node", + }, +}); diff --git a/scripts/validation/api.json b/scripts/validation/api.json index 43c7ade6d029..9cc37a7e9c66 100644 --- a/scripts/validation/api.json +++ b/scripts/validation/api.json @@ -586,5 +586,8 @@ }, "@aws-sdk/credential-provider-login": { "fromLoginCredentials": "function, since <=3.936.0" + }, + "@aws-sdk/dynamodb-codec": { + "DynamoDBJsonCodec": "function, since <=3.0.0" } } diff --git a/yarn.lock b/yarn.lock index 4a396ef79d80..05a82842bea5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8318,6 +8318,7 @@ __metadata: "@aws-crypto/sha256-js": "npm:5.2.0" "@aws-sdk/core": "npm:*" "@aws-sdk/credential-provider-node": "npm:*" + "@aws-sdk/dynamodb-codec": "npm:*" "@aws-sdk/middleware-endpoint-discovery": "npm:*" "@aws-sdk/middleware-host-header": "npm:*" "@aws-sdk/middleware-logger": "npm:*" @@ -23597,6 +23598,26 @@ __metadata: languageName: unknown linkType: soft +"@aws-sdk/dynamodb-codec@npm:*, @aws-sdk/dynamodb-codec@workspace:packages/dynamodb-codec": + version: 0.0.0-use.local + resolution: "@aws-sdk/dynamodb-codec@workspace:packages/dynamodb-codec" + dependencies: + "@aws-sdk/core": "npm:*" + "@smithy/core": "npm:^3.18.5" + "@smithy/smithy-client": "npm:^4.9.8" + "@smithy/types": "npm:^4.9.0" + "@smithy/util-base64": "npm:^4.3.0" + "@tsconfig/recommended": "npm:1.0.1" + concurrently: "npm:7.0.0" + downlevel-dts: "npm:0.10.1" + rimraf: "npm:3.0.2" + tslib: "npm:^2.6.2" + typescript: "npm:~5.8.3" + peerDependencies: + "@aws-sdk/client-dynamodb": "*" + languageName: unknown + linkType: soft + "@aws-sdk/ec2-metadata-service@workspace:packages/ec2-metadata-service": version: 0.0.0-use.local resolution: "@aws-sdk/ec2-metadata-service@workspace:packages/ec2-metadata-service" From d1afb8b48efa3b4d15d237fc934ef6ec84dfd74b Mon Sep 17 00:00:00 2001 From: George Fu Date: Tue, 2 Dec 2025 10:42:23 -0500 Subject: [PATCH 2/2] test(dynamodb-codec): add unit test --- .../src/codec/DynamoDBJsonCodec.spec.ts | 119 +++++++++++++++++- 1 file changed, 117 insertions(+), 2 deletions(-) diff --git a/packages/dynamodb-codec/src/codec/DynamoDBJsonCodec.spec.ts b/packages/dynamodb-codec/src/codec/DynamoDBJsonCodec.spec.ts index 834ee2a569cc..584ebfe2ea9f 100644 --- a/packages/dynamodb-codec/src/codec/DynamoDBJsonCodec.spec.ts +++ b/packages/dynamodb-codec/src/codec/DynamoDBJsonCodec.spec.ts @@ -1,7 +1,122 @@ -import { describe, test as it } from "vitest"; +import { JsonCodec } from "@aws-sdk/core/protocols"; +import { StaticListSchema, StaticMapSchema, StaticStructureSchema } from "@smithy/types"; +import { describe, expect, test as it } from "vitest"; import { DynamoDBJsonCodec } from "./DynamoDBJsonCodec"; describe(DynamoDBJsonCodec.name, () => { - it("placeholder", async () => {}); + const baseCodec = new JsonCodec({ + timestampFormat: { + useTrait: true, + default: 7 as const, + }, + jsonName: false, + }); + const baseSerializer = baseCodec.createSerializer(); + const baseDeserializer = baseCodec.createDeserializer(); + + const codec = new DynamoDBJsonCodec(); + const serializer = codec.createSerializer(); + const deserializer = codec.createDeserializer(); + + const ns = `com.amazonaws.dynamodb`; + + const AttributeValue: StaticStructureSchema = [ + 3, + ns, + "AttributeValue", + 0, + ["S", "N", "B", "SS", "NS", "BS", "M", "L", "NULL", "BOOL"], + [0, 0, 21, 64 | 0, 64 | 0, 64 | 21, () => MapAttributeValue, () => ListAttributeValue, 2, 2], + ]; + const ListAttributeValue: StaticListSchema = [1, ns, "ListAttributeValue", 0, () => AttributeValue]; + const MapAttributeValue: StaticMapSchema = [2, ns, "MapAttributeValue", 0, 0, () => AttributeValue]; + + const avInput = { + M: { + string: { + S: "bar", + }, + number: { + N: "123", + }, + boolean: { + B: false, + }, + stringSet: { + SS: ["a", "b", "c"], + }, + numberSet: { + NS: ["0", "1", "2"], + }, + blobSet: { + BS: [new Uint8Array([0, 0, 0]), new Uint8Array([0, 1, 2, 3, 4, 5, 6])], + }, + map: { + M: { + a: { S: "a" }, + b: { NULL: true }, + }, + }, + list: { + L: [{ B: new Uint8Array([0, 0, 0]) }, { B: new Uint8Array([0, 1, 2, 3, 4, 5, 6]) }], + }, + null: { + NULL: true, + }, + }, + }; + const canonicalSerialization = { + M: { + string: { + S: "bar", + }, + number: { + N: "123", + }, + boolean: { + B: false, + }, + stringSet: { + SS: ["a", "b", "c"], + }, + numberSet: { + NS: ["0", "1", "2"], + }, + blobSet: { + BS: ["AAAA", "AAECAwQFBg=="], + }, + map: { + M: { + a: { S: "a" }, + b: { NULL: true }, + }, + }, + list: { + L: [{ B: "AAAA" }, { B: "AAECAwQFBg==" }], + }, + null: { + NULL: true, + }, + }, + }; + + it("serializes identically to the default codec", async () => { + serializer.write(AttributeValue, avInput); + const serialization = serializer.flush(); + expect(serialization).toEqual(JSON.stringify(canonicalSerialization)); + expect(JSON.parse(serialization)).toEqual(canonicalSerialization); + + baseSerializer.write(AttributeValue, avInput); + const baseSerialization = baseSerializer.flush(); + expect(JSON.parse(baseSerialization)).toEqual(canonicalSerialization); + }); + + it("deserializes identically to the default codec", async () => { + const deserialization = await deserializer.read(AttributeValue, JSON.stringify(canonicalSerialization)); + expect(deserialization).toEqual(avInput); + + const baseDeserialization = await baseDeserializer.read(AttributeValue, JSON.stringify(canonicalSerialization)); + expect(baseDeserialization).toEqual(avInput); + }); });