Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/popular-squids-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/core": minor
---

implement SerdeContext base class
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ export * from "./middleware-http-auth-scheme";
export * from "./middleware-http-signing";
export * from "./normalizeProvider";
export { createPaginator } from "./pagination/createPaginator";
export * from "./protocols/requestBuilder";
export * from "./request-builder/requestBuilder";
export * from "./setFeature";
export * from "./util-identity-and-auth";
26 changes: 5 additions & 21 deletions packages/core/src/submodules/cbor/CborCodec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SerdeContext } from "@smithy/core/protocols";
import { NormalizedSchema } from "@smithy/core/schema";
import { _parseEpochTimestamp, generateIdempotencyToken } from "@smithy/core/serde";
import type { Codec, Schema, SerdeFunctions, ShapeDeserializer, ShapeSerializer } from "@smithy/types";
import type { Codec, Schema, ShapeDeserializer, ShapeSerializer } from "@smithy/types";
import { fromBase64 } from "@smithy/util-base64";

import { cbor } from "./cbor";
Expand All @@ -9,9 +10,7 @@ import { dateToTag } from "./parseCborBody";
/**
* @alpha
*/
export class CborCodec implements Codec<Uint8Array, Uint8Array> {
private serdeContext?: SerdeFunctions;

export class CborCodec extends SerdeContext implements Codec<Uint8Array, Uint8Array> {
public createSerializer(): CborShapeSerializer {
const serializer = new CborShapeSerializer();
serializer.setSerdeContext(this.serdeContext!);
Expand All @@ -23,23 +22,14 @@ export class CborCodec implements Codec<Uint8Array, Uint8Array> {
deserializer.setSerdeContext(this.serdeContext!);
return deserializer;
}

public setSerdeContext(serdeContext: SerdeFunctions): void {
this.serdeContext = serdeContext;
}
}

/**
* @alpha
*/
export class CborShapeSerializer implements ShapeSerializer {
private serdeContext?: SerdeFunctions;
export class CborShapeSerializer extends SerdeContext implements ShapeSerializer {
private value: unknown;

public setSerdeContext(serdeContext: SerdeFunctions) {
this.serdeContext = serdeContext;
}

public write(schema: Schema, value: unknown): void {
this.value = this.serialize(schema, value);
}
Expand Down Expand Up @@ -127,13 +117,7 @@ export class CborShapeSerializer implements ShapeSerializer {
/**
* @alpha
*/
export class CborShapeDeserializer implements ShapeDeserializer {
private serdeContext?: SerdeFunctions;

public setSerdeContext(serdeContext: SerdeFunctions) {
this.serdeContext = serdeContext;
}

export class CborShapeDeserializer extends SerdeContext implements ShapeDeserializer {
public read(schema: Schema, bytes: Uint8Array): any {
const data: any = cbor.deserialize(bytes);
return this.readValue(schema, data);
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/submodules/protocols/HttpProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ import type {
ShapeSerializer,
} from "@smithy/types";

import { SerdeContext } from "./SerdeContext";

/**
* Abstract base for HTTP-based client protocols.
*
* @alpha
*/
export abstract class HttpProtocol implements ClientProtocol<IHttpRequest, IHttpResponse> {
export abstract class HttpProtocol extends SerdeContext implements ClientProtocol<IHttpRequest, IHttpResponse> {
protected abstract serializer: ShapeSerializer<string | Uint8Array>;
protected abstract deserializer: ShapeDeserializer<string | Uint8Array>;
protected serdeContext?: SerdeFunctions;

protected constructor(
public readonly options: {
defaultNamespace: string;
}
) {}
) {
super();
}

public abstract getShapeId(): string;

Expand All @@ -49,6 +52,9 @@ export abstract class HttpProtocol implements ClientProtocol<IHttpRequest, IHttp
return HttpResponse;
}

/**
* @override
*/
public setSerdeContext(serdeContext: SerdeFunctions): void {
this.serdeContext = serdeContext;
this.serializer.setSerdeContext(serdeContext);
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/submodules/protocols/SerdeContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { ConfigurableSerdeContext, SerdeFunctions } from "@smithy/types";

/**
* @internal
*/
export abstract class SerdeContext implements ConfigurableSerdeContext {
protected serdeContext?: SerdeFunctions;

public setSerdeContext(serdeContext: SerdeFunctions): void {
this.serdeContext = serdeContext;
}
}
1 change: 1 addition & 0 deletions packages/core/src/submodules/protocols/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from "./serde/HttpInterceptingShapeDeserializer";
export * from "./serde/HttpInterceptingShapeSerializer";
export * from "./serde/ToStringShapeSerializer";
export * from "./serde/determineTimestampFormat";
export * from "./SerdeContext";
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import type {
CodecSettings,
Schema,
SerdeFunctions,
ShapeDeserializer,
TimestampDateTimeSchema,
TimestampEpochSecondsSchema,
Expand All @@ -19,20 +18,17 @@ import type {
import { fromBase64 } from "@smithy/util-base64";
import { toUtf8 } from "@smithy/util-utf8";

import { SerdeContext } from "../SerdeContext";
import { determineTimestampFormat } from "./determineTimestampFormat";

/**
* This deserializer reads strings.
*
* @alpha
*/
export class FromStringShapeDeserializer implements ShapeDeserializer<string> {
private serdeContext: SerdeFunctions | undefined;

public constructor(private settings: CodecSettings) {}

public setSerdeContext(serdeContext: SerdeFunctions): void {
this.serdeContext = serdeContext;
export class FromStringShapeDeserializer extends SerdeContext implements ShapeDeserializer<string> {
public constructor(private settings: CodecSettings) {
super();
}

public read(_schema: Schema, data: string): any {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NormalizedSchema } from "@smithy/core/schema";
import type { CodecSettings, Schema, SerdeFunctions, ShapeDeserializer } from "@smithy/types";
import { fromUtf8, toUtf8 } from "@smithy/util-utf8";

import { SerdeContext } from "../SerdeContext";
import { FromStringShapeDeserializer } from "./FromStringShapeDeserializer";

/**
Expand All @@ -14,18 +15,22 @@ import { FromStringShapeDeserializer } from "./FromStringShapeDeserializer";
* @alpha
*/
export class HttpInterceptingShapeDeserializer<CodecShapeDeserializer extends ShapeDeserializer<any>>
extends SerdeContext
implements ShapeDeserializer<string | Uint8Array>
{
private stringDeserializer: FromStringShapeDeserializer;
private serdeContext: SerdeFunctions | undefined;

public constructor(
private codecDeserializer: CodecShapeDeserializer,
codecSettings: CodecSettings
) {
super();
this.stringDeserializer = new FromStringShapeDeserializer(codecSettings);
}

/**
* @override
*/
public setSerdeContext(serdeContext: SerdeFunctions): void {
this.stringDeserializer.setSerdeContext(serdeContext);
this.codecDeserializer.setSerdeContext(serdeContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { NormalizedSchema } from "@smithy/core/schema";
import type { CodecSettings, Schema as ISchema, SerdeFunctions, ShapeSerializer } from "@smithy/types";
import type {
CodecSettings,
ConfigurableSerdeContext,
Schema as ISchema,
SerdeFunctions,
ShapeSerializer,
} from "@smithy/types";

import { ToStringShapeSerializer } from "./ToStringShapeSerializer";

Expand All @@ -13,7 +19,7 @@ import { ToStringShapeSerializer } from "./ToStringShapeSerializer";
* @alpha
*/
export class HttpInterceptingShapeSerializer<CodecShapeSerializer extends ShapeSerializer<string | Uint8Array>>
implements ShapeSerializer<string | Uint8Array>
implements ShapeSerializer<string | Uint8Array>, ConfigurableSerdeContext
{
private buffer: string | undefined;

Expand All @@ -23,6 +29,9 @@ export class HttpInterceptingShapeSerializer<CodecShapeSerializer extends ShapeS
private stringSerializer = new ToStringShapeSerializer(codecSettings)
) {}

/**
* @override
*/
public setSerdeContext(serdeContext: SerdeFunctions): void {
this.codecSerializer.setSerdeContext(serdeContext);
this.stringSerializer.setSerdeContext(serdeContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ import { dateToUtcString, LazyJsonString, quoteHeader } from "@smithy/core/serde
import type {
CodecSettings,
Schema,
SerdeFunctions,
ShapeSerializer,
TimestampDateTimeSchema,
TimestampEpochSecondsSchema,
TimestampHttpDateSchema,
} from "@smithy/types";
import { toBase64 } from "@smithy/util-base64";

import { SerdeContext } from "../SerdeContext";
import { determineTimestampFormat } from "./determineTimestampFormat";

/**
* Serializes a shape to string.
*
* @alpha
*/
export class ToStringShapeSerializer implements ShapeSerializer<string> {
export class ToStringShapeSerializer extends SerdeContext implements ShapeSerializer<string> {
private stringBuffer = "";
private serdeContext: SerdeFunctions | undefined = undefined;

public constructor(private settings: CodecSettings) {}

public setSerdeContext(serdeContext: SerdeFunctions): void {
this.serdeContext = serdeContext;
public constructor(private settings: CodecSettings) {
super();
}

public write(schema: Schema, value: unknown): void {
Expand Down