CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-smithy--node-http-handler

HTTP request handlers for Node.js environments in the Smithy TypeScript ecosystem

Pending
Overview
Eval results
Files

stream-collector.mddocs/

Stream Collection

The stream collector provides utilities for converting Node.js streams and Web API ReadableStreams to byte arrays, essential for processing HTTP response bodies in the Smithy ecosystem.

Capabilities

Stream Collector Function

Main utility function for converting various stream types to Uint8Array format.

/**
 * Converts a stream to a byte array.
 * Supports both Node.js Readable streams and Web API ReadableStreams.
 */
const streamCollector: StreamCollector;

/**
 * Type definition for the stream collector function
 */
type StreamCollector = (stream: Readable | ReadableStream): Promise<Uint8Array>;

Usage Examples:

import { streamCollector } from "@smithy/node-http-handler";
import { Readable } from "stream";
import { createReadStream } from "fs";

// Collect from Node.js Readable stream
const fileStream = createReadStream("data.txt");
const fileBytes = await streamCollector(fileStream);
console.log(fileBytes); // Uint8Array containing file contents

// Collect from string-based stream  
const textStream = Readable.from(["Hello", " ", "World"]);
const textBytes = await streamCollector(textStream);
const text = new TextDecoder().decode(textBytes);
console.log(text); // "Hello World"

// Collect from Web API ReadableStream (Node.js 18+)
const response = await fetch("https://api.example.com/data");
if (response.body) {
  const responseBytes = await streamCollector(response.body);
  const responseText = new TextDecoder().decode(responseBytes);
  console.log(responseText);
}

// Collect from HTTP response stream
import { NodeHttpHandler } from "@smithy/node-http-handler";
import { HttpRequest } from "@smithy/protocol-http";

const handler = new NodeHttpHandler();
const request = new HttpRequest({
  method: "GET",
  hostname: "api.example.com",
  path: "/large-file"
});

const { response } = await handler.handle(request);
if (response.body) {
  const bodyBytes = await streamCollector(response.body);
  console.log(`Downloaded ${bodyBytes.length} bytes`);
}

Collector Class

Internal writable stream implementation used for collecting data chunks from Node.js streams.

/**
 * Internal writable stream for collecting data chunks.
 * Extends Node.js Writable stream.
 */
class Collector extends Writable {
  /**
   * Array of collected buffer chunks
   */
  bufferedBytes: Buffer[];
  
  /**
   * Create a new collector instance - inherits Writable constructor
   * @param options - WritableOptions for the underlying Writable stream
   */
  constructor(options?: WritableOptions);
  
  /**
   * Stream write implementation
   * @param chunk - Data chunk to write
   * @param encoding - Character encoding (if applicable)
   * @param callback - Completion callback
   */
  _write(chunk: Buffer, encoding: string, callback: (err?: Error) => void): void;
}

Stream Type Handling

The stream collector automatically detects and handles different stream types:

Node.js Readable Streams

// Traditional Node.js streams
const nodeStream = new Readable({
  read() {
    this.push("chunk 1");
    this.push("chunk 2");
    this.push(null); // End stream
  }
});

const bytes = await streamCollector(nodeStream);

Web API ReadableStreams

// Web API streams (Node.js 18+)
const webStream = new ReadableStream({
  start(controller) {
    controller.enqueue(new TextEncoder().encode("Hello"));
    controller.enqueue(new TextEncoder().encode(" World"));
    controller.close();
  }
});

const bytes = await streamCollector(webStream);

Error Handling

The stream collector provides comprehensive error handling for stream processing:

import { streamCollector } from "@smithy/node-http-handler";
import { Readable } from "stream";

// Handle stream errors
const errorStream = new Readable({
  read() {
    // Simulate an error
    this.emit("error", new Error("Stream processing failed"));
  }
});

try {
  const bytes = await streamCollector(errorStream);
} catch (error) {
  console.error("Stream collection failed:", error.message);
}

// Handle network stream errors
import { NodeHttpHandler } from "@smithy/node-http-handler";

const handler = new NodeHttpHandler();
try {
  const { response } = await handler.handle(request);
  const bodyBytes = await streamCollector(response.body);
} catch (error) {
  if (error.code === "ECONNRESET") {
    console.error("Connection was reset during stream collection");
  } else if (error.code === "ETIMEDOUT") {
    console.error("Stream collection timed out");
  } else {
    console.error("Stream collection error:", error.message);
  }
}

Types

// From Node.js stream module
interface Readable {
  pipe<T extends Writable>(destination: T): T;
  on(event: string, listener: Function): this;
  // ... other Readable properties and methods
}

// From stream/web module (Node.js 16.5.0+)
interface ReadableStream<R = any> {
  getReader(): ReadableStreamDefaultReader<R>;
  // ... other ReadableStream properties and methods
}

// From Node.js stream module
abstract class Writable {
  abstract _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
  end(): void;
  on(event: string, listener: Function): this;
  // ... other Writable properties and methods
}

// From Node.js stream module  
interface WritableOptions {
  highWaterMark?: number;
  objectMode?: boolean;
  // ... other Writable options
}

The stream collector is optimized for memory efficiency and handles both legacy Node.js streams and modern Web API streams seamlessly, making it ideal for processing HTTP response bodies of any size.

Install with Tessl CLI

npx tessl i tessl/npm-smithy--node-http-handler

docs

http-handler.md

http2-handler.md

index.md

stream-collector.md

tile.json