HTTP request handlers for Node.js environments in the Smithy TypeScript ecosystem
—
The NodeHttp2Handler provides HTTP/2 request handling using Node.js http2 module with session pooling, concurrent stream management, and comprehensive configuration options.
HTTP/2 request handler implementing the HttpHandler interface with advanced session and stream management.
/**
* A request handler using the node:http2 package.
*/
class NodeHttp2Handler implements HttpHandler<NodeHttp2HandlerOptions> {
/**
* Create a new HTTP/2 handler instance
* @param options - Handler configuration options or provider function
*/
constructor(options?: NodeHttp2HandlerOptions | Provider<NodeHttp2HandlerOptions | void>);
/**
* Factory method to create handler instance or return existing HttpHandler
* @param instanceOrOptions - Existing handler, options, or provider function
* @returns NodeHttp2Handler instance
*/
static create(
instanceOrOptions?: HttpHandler<any> | NodeHttp2HandlerOptions | Provider<NodeHttp2HandlerOptions | void>
): NodeHttp2Handler;
/**
* Handle HTTP/2 request and return response
* @param request - HTTP request object
* @param options - Request-specific options including abort signal
* @returns Promise resolving to response object
*/
handle(request: HttpRequest, options?: HttpHandlerOptions): Promise<{ response: HttpResponse }>;
/**
* Clean up handler resources and close all HTTP/2 sessions
*/
destroy(): void;
/**
* Update a specific configuration option
* @param key - Configuration key to update
* @param value - New value for the configuration key
*/
updateHttpClientConfig(key: keyof NodeHttp2HandlerOptions, value: NodeHttp2HandlerOptions[typeof key]): void;
/**
* Get current handler configuration
* @returns Current NodeHttp2HandlerOptions configuration
*/
httpHandlerConfigs(): NodeHttp2HandlerOptions;
/**
* Handler metadata indicating protocol version
*/
readonly metadata: { handlerProtocol: "h2" };
}Usage Examples:
import { NodeHttp2Handler } from "@smithy/node-http-handler";
import { HttpRequest } from "@smithy/protocol-http";
// Basic HTTP/2 handler with defaults
const handler = new NodeHttp2Handler();
// Handler with custom configuration
const configuredHandler = new NodeHttp2Handler({
requestTimeout: 30000,
sessionTimeout: 300000, // 5 minutes
maxConcurrentStreams: 100,
disableConcurrentStreams: false
});
// Using factory method
const factoryHandler = NodeHttp2Handler.create({
requestTimeout: 60000,
sessionTimeout: 600000 // 10 minutes
});
// Handle multiple concurrent requests (default behavior)
const request1 = new HttpRequest({
method: "GET",
hostname: "http2.example.com",
path: "/api/users",
headers: { "Authorization": "Bearer token123" }
});
const request2 = new HttpRequest({
method: "GET",
hostname: "http2.example.com",
path: "/api/posts",
headers: { "Authorization": "Bearer token123" }
});
// Both requests can use the same HTTP/2 session concurrently
const [response1, response2] = await Promise.all([
handler.handle(request1),
handler.handle(request2)
]);
// Handle request with timeout
const timeoutRequest = new HttpRequest({
method: "POST",
hostname: "http2.example.com",
path: "/api/data",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ key: "value" })
});
const { response } = await handler.handle(timeoutRequest, {
abortSignal: AbortSignal.timeout(10000) // 10 second timeout
});
// Update configuration after creation
handler.updateHttpClientConfig("maxConcurrentStreams", 200);
handler.updateHttpClientConfig("requestTimeout", 45000);
// Clean up when done
handler.destroy();Configuration interface for HTTP/2 handler options.
/**
* Represents the http2 options that can be passed to a node http2 client.
*/
interface NodeHttp2HandlerOptions {
/**
* The maximum time in milliseconds that a stream may remain idle before it
* is closed.
*/
requestTimeout?: number;
/**
* The maximum time in milliseconds that a session or socket may remain idle
* before it is closed.
* https://nodejs.org/docs/latest-v12.x/api/http2.html#http2_http2session_and_sockets
*/
sessionTimeout?: number;
/**
* Disables processing concurrent streams on a ClientHttp2Session instance. When set
* to true, a new session instance is created for each request to a URL.
* Default: false.
* https://nodejs.org/api/http2.html#http2_class_clienthttp2session
*/
disableConcurrentStreams?: boolean;
/**
* Maximum number of concurrent Http2Stream instances per ClientHttp2Session. Each session
* may have up to 2^31-1 Http2Stream instances over its lifetime.
* This value must be greater than or equal to 0.
* https://nodejs.org/api/http2.html#class-http2stream
*/
maxConcurrentStreams?: number;
}The HTTP/2 handler automatically manages session pooling and connection lifecycle internally. Sessions are reused for multiple requests to the same origin when concurrent streams are enabled, and properly cleaned up when the handler is destroyed.
The handler automatically manages HTTP/2 sessions, multiplexing multiple requests over single connections, stream lifecycle, server push support, and comprehensive error handling for HTTP/2-specific scenarios.
Install with Tessl CLI
npx tessl i tessl/npm-smithy--node-http-handler