or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

authentication.mdclient.mddns.mdhttp.mdindex.mdmiddleware.mdserialization.md
tile.json

serialization.mddocs/

Serialization & Data Processing

Types for data serialization, deserialization, encoding, and streaming used for processing AWS service requests and responses. These types provide the foundation for converting between different data formats and handling various AWS service protocols.

Capabilities

Serialization Context

Core context types that flow through serialization and deserialization operations carrying configuration and utilities.

/**
 * Serialization context containing encoding/decoding utilities
 * Flows through serialization pipeline providing consistent data processing
 */
interface SerdeContext {
  /** Base64 encoder function */
  base64Encoder?: Encoder;
  /** Base64 decoder function */
  base64Decoder?: Decoder;
  /** UTF-8 encoder function */
  utf8Encoder?: Encoder;
  /** UTF-8 decoder function */
  utf8Decoder?: Decoder;
  /** Stream collection utility */
  streamCollector?: StreamCollector;
  /** Logger for serialization debugging */
  logger?: Logger;
  /** Disable validation of body checksums */
  disableBodyChecksumValidation?: boolean;
  /** SHA-256 hash constructor */
  sha256?: HashConstructor;
  /** MD5 hash constructor */
  md5?: HashConstructor;
}

/**
 * SDK stream serialization context with additional stream utilities
 * Extended context for handling streaming data in AWS services
 */
interface SdkStreamSerdeContext extends SerdeContext {
  /** SDK stream utility functions */
  sdkStreamMixin?: SdkStreamMixin;
  /** Request stream hash calculator */
  requestHash?: StreamHasher;
  /** Response stream hash validator */
  responseHash?: StreamHasher;
}

Encoding & Decoding

Universal encoder and decoder interfaces for data transformation between different formats.

/**
 * Generic encoder interface for converting data to encoded format
 * Used for base64, UTF-8, and other encoding operations
 */
interface Encoder {
  /**
   * Encode input data to target format
   * @param input - Data to encode
   * @returns Encoded data as Uint8Array
   */
  encode(input: string): Uint8Array;
}

/**
 * Generic decoder interface for converting encoded data back to original format
 * Used for base64, UTF-8, and other decoding operations
 */
interface Decoder {
  /**
   * Decode input data from encoded format
   * @param input - Encoded data to decode
   * @returns Decoded data as string
   */
  decode(input: Uint8Array): string;
}

/**
 * Stream collector interface for gathering streaming data
 * Used to collect and buffer streaming responses
 */
interface StreamCollector {
  /**
   * Collect stream data into a single buffer
   * @param stream - Input stream to collect
   * @returns Promise resolving to collected data
   */
  collect(stream: any): Promise<Uint8Array>;
}

Usage Examples:

import { Encoder, Decoder, StreamCollector, SerdeContext } from "@aws-sdk/types";

// Example base64 encoder/decoder implementation
const base64Encoder: Encoder = {
  encode: (input: string): Uint8Array => {
    const encoded = btoa(input);
    return new TextEncoder().encode(encoded);
  }
};

const base64Decoder: Decoder = {
  decode: (input: Uint8Array): string => {
    const encoded = new TextDecoder().decode(input);
    return atob(encoded);
  }
};

// Stream collector for gathering response data
const streamCollector: StreamCollector = {
  collect: async (stream: ReadableStream): Promise<Uint8Array> => {
    const reader = stream.getReader();
    const chunks: Uint8Array[] = [];
    
    try {
      while (true) {
        const { done, value } = await reader.read();
        if (done) break;
        chunks.push(value);
      }
    } finally {
      reader.releaseLock();
    }
    
    // Combine all chunks
    const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
    const result = new Uint8Array(totalLength);
    let offset = 0;
    
    for (const chunk of chunks) {
      result.set(chunk, offset);
      offset += chunk.length;
    }
    
    return result;
  }
};

// Create serialization context
const serdeContext: SerdeContext = {
  base64Encoder,
  base64Decoder,
  utf8Encoder: new TextEncoder(),
  utf8Decoder: new TextDecoder(),
  streamCollector,
  logger: console
};

Request & Response Serialization

Interfaces for serializing requests and deserializing responses in AWS service communications.

/**
 * Request serializer interface for converting command input to HTTP requests
 * Transforms high-level command objects into wire-format HTTP requests
 */
interface RequestSerializer<InputType, RequestType = HttpRequest> {
  /**
   * Serialize command input to HTTP request
   * @param input - Command input object
   * @param context - Serialization context with utilities
   * @returns Promise resolving to serialized HTTP request
   */
  serialize(input: InputType, context: SerdeContext): Promise<RequestType>;
}

/**
 * Response deserializer interface for converting HTTP responses to command output
 * Transforms wire-format HTTP responses into high-level result objects
 */
interface ResponseDeserializer<OutputType, ResponseType = HttpResponse> {
  /**
   * Deserialize HTTP response to command output
   * @param response - HTTP response to deserialize
   * @param options - Deserialization options and context
   * @returns Promise resolving to deserialized output object
   */
  deserialize(
    response: ResponseType,
    options: DeserializeHandlerOptions & SerdeContext
  ): Promise<OutputType>;
}

/**
 * Endpoint bearer interface for objects that carry endpoint information
 * Used by serializers to determine target endpoints for requests
 */
interface EndpointBearer {
  /** Target endpoint configuration */
  endpoint: Endpoint;
}

Usage Examples:

import { 
  RequestSerializer, 
  ResponseDeserializer,
  SerdeContext,
  HttpRequest,
  HttpResponse 
} from "@aws-sdk/types";

// Example JSON request serializer
class JsonRequestSerializer implements RequestSerializer<any, HttpRequest> {
  async serialize(input: any, context: SerdeContext): Promise<HttpRequest> {
    const body = JSON.stringify(input);
    const encodedBody = context.utf8Encoder?.encode(body) || new TextEncoder().encode(body);
    
    return {
      method: "POST",
      protocol: "https:",
      hostname: "service.amazonaws.com",
      path: "/",
      headers: {
        "Content-Type": "application/x-amz-json-1.1",
        "Content-Length": encodedBody.length.toString()
      },
      body: encodedBody
    };
  }
}

// Example JSON response deserializer
class JsonResponseDeserializer implements ResponseDeserializer<any, HttpResponse> {
  async deserialize(response: HttpResponse, options: any): Promise<any> {
    if (response.statusCode !== 200) {
      throw new Error(`HTTP ${response.statusCode}: ${response.reason}`);
    }
    
    const bodyBytes = await options.streamCollector?.collect(response.body);
    const bodyText = options.utf8Decoder?.decode(bodyBytes) || 
                    new TextDecoder().decode(bodyBytes);
    
    return JSON.parse(bodyText);
  }
}

Stream Processing

Types for handling streaming data in AWS services with SDK-specific stream utilities.

/**
 * SDK stream mixin interface providing stream utility functions
 * Extends basic streams with AWS SDK specific functionality
 */
interface SdkStreamMixin {
  /** Transform stream to different format */
  transformToByteArray(): Promise<Uint8Array>;
  /** Transform stream to string */
  transformToString(encoding?: string): Promise<string>;
  /** Transform stream to web stream */
  transformToWebStream(): ReadableStream;
}

/**
 * SDK stream type combining base stream with mixin functionality
 * Provides enhanced streaming capabilities for AWS services
 */
type SdkStream<BaseStream> = BaseStream & SdkStreamMixin;

/**
 * Utility type for objects that have SDK stream mixin capabilities
 * Used for type checking and stream enhancement
 */
type WithSdkStreamMixin<T> = T & {
  /** SDK stream mixin functions */
  [K in keyof SdkStreamMixin]: SdkStreamMixin[K];
};

/**
 * SDK stream mixin injector function type
 * Used to add SDK stream capabilities to base streams
 */
type SdkStreamMixinInjector = <T>(stream: T) => WithSdkStreamMixin<T>;

/**
 * AWS chunked encoding stream function
 * Creates streams with AWS-specific chunked encoding for large uploads
 */
interface GetAwsChunkedEncodingStream {
  /**
   * Create AWS chunked encoding stream
   * @param source - Source stream to encode
   * @param options - Encoding options
   * @returns Chunked encoded stream
   */
  (source: any, options: GetAwsChunkedEncodingStreamOptions): any;
}

/**
 * Options for AWS chunked encoding stream creation
 */
interface GetAwsChunkedEncodingStreamOptions {
  /** Chunk size in bytes */
  chunkSize?: number;
  /** Signature function for chunk signing */
  chunkSigner?: (chunk: Uint8Array) => Promise<string>;
  /** Request signature for initial chunk */
  requestSignature?: string;
  /** Signing key for chunk signatures */
  signingKey?: Uint8Array;
  /** Signature date for chunk signing */
  signingDate?: Date;
  /** AWS region for chunk signing */
  region?: string;
  /** Service name for chunk signing */
  service?: string;
}

Usage Examples:

import { 
  SdkStream,
  SdkStreamMixin,
  GetAwsChunkedEncodingStream,
  GetAwsChunkedEncodingStreamOptions 
} from "@aws-sdk/types";

// Example SDK stream enhancement
const sdkStreamMixin: SdkStreamMixin = {
  async transformToByteArray(): Promise<Uint8Array> {
    const reader = (this as any).getReader();
    const chunks: Uint8Array[] = [];
    
    try {
      while (true) {
        const { done, value } = await reader.read();
        if (done) break;
        chunks.push(value);
      }
    } finally {
      reader.releaseLock();
    }
    
    const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
    const result = new Uint8Array(totalLength);
    let offset = 0;
    
    for (const chunk of chunks) {
      result.set(chunk, offset);
      offset += chunk.length;
    }
    
    return result;
  },
  
  async transformToString(encoding = "utf-8"): Promise<string> {
    const bytes = await this.transformToByteArray();
    return new TextDecoder(encoding).decode(bytes);
  },
  
  transformToWebStream(): ReadableStream {
    return this as any; // Assume already a web stream
  }
};

// Create chunked encoding stream for large uploads
const createChunkedStream: GetAwsChunkedEncodingStream = (source, options) => {
  return new ReadableStream({
    start(controller) {
      // Implementation would create AWS chunked encoding
      // This is a simplified example
      const chunkSize = options.chunkSize || 64 * 1024; // 64KB chunks
      
      // Process source stream in chunks with AWS signatures
      // Implementation details would handle chunk formatting and signing
    }
  });
};

// Usage for large file uploads
const fileStream = new ReadableStream(/* file data */);
const chunkedOptions: GetAwsChunkedEncodingStreamOptions = {
  chunkSize: 1024 * 1024, // 1MB chunks
  region: "us-east-1",
  service: "s3",
  signingDate: new Date(),
  chunkSigner: async (chunk) => {
    // Sign individual chunk
    return "chunk-signature";
  }
};

const chunkedStream = createChunkedStream(fileStream, chunkedOptions);

Document Types

Types for handling document-style data structures commonly used in AWS services.

/**
 * Document type for representing arbitrary JSON-like data structures
 * Used in DynamoDB and other services that handle flexible schemas
 */
type DocumentType = 
  | null
  | boolean
  | number
  | string
  | DocumentType[]
  | { [key: string]: DocumentType };

Usage Examples:

import { DocumentType } from "@aws-sdk/types";

// Example DynamoDB item as DocumentType
const dynamoItem: DocumentType = {
  id: "user123",
  name: "John Doe",
  age: 30,
  active: true,
  tags: ["developer", "typescript"],
  metadata: {
    created: "2023-01-01",
    updated: null
  }
};

// Function that processes document data
function processDocument(doc: DocumentType): void {
  if (typeof doc === "object" && doc !== null && !Array.isArray(doc)) {
    // Process object properties
    for (const [key, value] of Object.entries(doc)) {
      console.log(`${key}:`, value);
    }
  }
}