HTTP request handlers for Node.js environments in the Smithy TypeScript ecosystem
npx @tessl/cli install tessl/npm-smithy--node-http-handler@4.2.0The Smithy Node HTTP Handler provides HTTP request handling functionality for Node.js environments in the Smithy TypeScript ecosystem. It implements default request handlers using Node.js built-in HTTP modules to enable HTTP client communication for Smithy-generated SDK clients.
npm install @smithy/node-http-handlerimport { NodeHttpHandler, NodeHttp2Handler, streamCollector } from "@smithy/node-http-handler";For CommonJS:
const { NodeHttpHandler, NodeHttp2Handler, streamCollector } = require("@smithy/node-http-handler");import { NodeHttpHandler, NodeHttp2Handler } from "@smithy/node-http-handler";
import { HttpRequest } from "@smithy/protocol-http";
// HTTP/1.1 handler
const httpHandler = new NodeHttpHandler({
requestTimeout: 30000,
connectionTimeout: 5000
});
// HTTP/2 handler
const http2Handler = new NodeHttp2Handler({
requestTimeout: 30000,
sessionTimeout: 300000
});
// Handle a request
const request = new HttpRequest({
method: "GET",
hostname: "example.com",
path: "/api/data",
headers: {
"Content-Type": "application/json"
}
});
const { response } = await httpHandler.handle(request);The package is built around several key components:
NodeHttpHandler using Node.js http and https modulesNodeHttp2Handler using Node.js http2 modulePrimary request handler using Node.js http and https modules with connection pooling, timeout management, and socket configuration.
class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
constructor(options?: NodeHttpHandlerOptions | Provider<NodeHttpHandlerOptions | void>);
static create(
instanceOrOptions?: HttpHandler<any> | NodeHttpHandlerOptions | Provider<NodeHttpHandlerOptions | void>
): NodeHttpHandler;
static checkSocketUsage(agent: Agent, socketWarningTimestamp: number, logger?: Logger): number;
handle(request: HttpRequest, options?: HttpHandlerOptions): Promise<{ response: HttpResponse }>;
destroy(): void;
updateHttpClientConfig(key: keyof NodeHttpHandlerOptions, value: NodeHttpHandlerOptions[typeof key]): void;
httpHandlerConfigs(): NodeHttpHandlerOptions;
readonly metadata: { handlerProtocol: "http/1.1" };
}
const DEFAULT_REQUEST_TIMEOUT: number;Request handler using Node.js http2 module with session pooling, stream management, and concurrent request support.
class NodeHttp2Handler implements HttpHandler<NodeHttp2HandlerOptions> {
constructor(options?: NodeHttp2HandlerOptions | Provider<NodeHttp2HandlerOptions | void>);
static create(
instanceOrOptions?: HttpHandler<any> | NodeHttp2HandlerOptions | Provider<NodeHttp2HandlerOptions | void>
): NodeHttp2Handler;
handle(request: HttpRequest, options?: HttpHandlerOptions): Promise<{ response: HttpResponse }>;
destroy(): void;
updateHttpClientConfig(key: keyof NodeHttp2HandlerOptions, value: NodeHttp2HandlerOptions[typeof key]): void;
httpHandlerConfigs(): NodeHttp2HandlerOptions;
readonly metadata: { handlerProtocol: "h2" };
}
interface NodeHttp2HandlerOptions {
requestTimeout?: number;
sessionTimeout?: number;
disableConcurrentStreams?: boolean;
maxConcurrentStreams?: number;
}Utilities for converting Node.js streams and Web API ReadableStreams to byte arrays for response body processing.
const streamCollector: StreamCollector;
type StreamCollector = (stream: Readable | ReadableStream): Promise<Uint8Array>;
class Collector extends Writable {
constructor(options?: WritableOptions);
bufferedBytes: Buffer[];
_write(chunk: Buffer, encoding: string, callback: (err?: Error) => void): void;
}// Re-exported from @smithy/types
interface NodeHttpHandlerOptions {
requestTimeout?: number;
connectionTimeout?: number;
socketTimeout?: number;
httpAgent?: Agent;
httpsAgent?: Agent;
// ... other options from @smithy/types
}
// From @smithy/protocol-http
interface HttpRequest {
method: string;
hostname?: string;
port?: number;
path?: string;
headers?: Record<string, string>;
body?: any;
// ... other properties
}
interface HttpResponse {
statusCode: number;
headers: Record<string, string>;
body?: any;
// ... other properties
}
interface HttpHandler<T> {
handle(request: HttpRequest, options?: HttpHandlerOptions): Promise<{ response: HttpResponse }>;
}
interface HttpHandlerOptions {
abortSignal?: AbortSignal;
}
// From @smithy/types
type Provider<T> = T | (() => T) | (() => Promise<T>);
// From Node.js http/https modules
interface Agent {
destroy(): void;
// ... other Agent properties
}
// From @smithy/types
interface Logger {
warn?(message: string): void;
// ... other Logger methods
}
// From Node.js stream module
interface Readable {
pipe<T extends Writable>(destination: T): T;
on(event: string, listener: Function): this;
// ... other Readable properties
}
// From Node.js stream module
interface WritableOptions {
highWaterMark?: number;
objectMode?: boolean;
// ... other Writable options
}
// From stream/web (Node.js 16.5.0+)
interface ReadableStream<R = any> {
getReader(): ReadableStreamDefaultReader<R>;
// ... other ReadableStream properties
}The handlers provide comprehensive error handling for various network conditions: