CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-stream-http

Streaming HTTP implementation for browsers with Node.js API compatibility

Pending
Overview
Eval results
Files

streaming.mddocs/

Request and Response Streaming

Comprehensive streaming capabilities for both outgoing request data and incoming response data. Stream-http provides true streaming in supported browsers and pseudo-streaming in older browsers.

Capabilities

ClientRequest Class

Writable stream for handling outgoing HTTP requests. Inherits from Node.js stream.Writable.

/**
 * HTTP request writable stream
 * Inherits from stream.Writable
 */
class ClientRequest extends stream.Writable {
  /**
   * Sets an HTTP request header
   * @param name - Header name (case-insensitive)
   * @param value - Header value
   * Note: Certain unsafe headers are filtered for security
   */
  setHeader(name, value);
  
  /**
   * Gets an HTTP request header value
   * @param name - Header name (case-insensitive)
   * @returns Header value or null if not set
   */
  getHeader(name);
  
  /**
   * Removes an HTTP request header
   * @param name - Header name (case-insensitive)
   */
  removeHeader(name);
  
  /**
   * Sets socket timeout for the request
   * @param timeout - Timeout in milliseconds
   * @param cb - Optional callback called on timeout
   */
  setTimeout(timeout, cb);
  
  /**
   * Aborts the request
   * @param err - Optional error to emit
   */
  abort(err);
  
  /**
   * Alias for abort()
   * @param err - Optional error to emit
   */
  destroy(err);
  
  /**
   * Finishes sending the request
   * @param data - Optional final data to write
   * @param encoding - Optional encoding for data
   * @param cb - Optional callback when finished
   */
  end(data, encoding, cb);
  
  /**
   * No-op stub for Node.js compatibility
   */
  flushHeaders();
  
  /**
   * No-op stub for Node.js compatibility
   */
  setNoDelay();
  
  /**
   * No-op stub for Node.js compatibility
   */
  setSocketKeepAlive();
}

Usage Examples:

const http = require('stream-http');

// Writing request data in chunks
const req = http.request({
  method: 'POST',
  hostname: 'api.example.com',
  path: '/upload',
  headers: {
    'Content-Type': 'application/octet-stream'
  }
});

// Write data in chunks
req.write(Buffer.from('chunk1'));
req.write(Buffer.from('chunk2'));
req.end(Buffer.from('final-chunk'));

// Setting headers
req.setHeader('X-Custom-Header', 'value');
console.log(req.getHeader('content-type')); // 'application/octet-stream'

// Handle timeout
req.setTimeout(5000, function() {
  console.log('Request timed out');
  req.abort();
});

Header Security

Stream-http automatically filters unsafe headers for security reasons. These headers are blocked by browsers:

/**
 * Headers that are filtered for security (cannot be set manually)
 */
const unsafeHeaders = [
  'accept-charset', 'accept-encoding', 'access-control-request-headers',
  'access-control-request-method', 'connection', 'content-length',
  'cookie', 'cookie2', 'date', 'dnt', 'expect', 'host', 'keep-alive',
  'origin', 'referer', 'te', 'trailer', 'transfer-encoding', 
  'upgrade', 'via'
];

Attempting to set these headers via setHeader() will be silently ignored to prevent warnings from browsers.

IncomingMessage Class

Readable stream for handling incoming HTTP responses. Inherits from Node.js stream.Readable.

/**
 * HTTP response readable stream
 * Inherits from stream.Readable
 */
class IncomingMessage extends stream.Readable {
  /** Response headers with lowercase keys */
  headers: object;
  
  /** Raw response headers as array [name, value, name, value, ...] */
  rawHeaders: string[];
  
  /** Response trailers (always empty in browser) */
  trailers: object;
  
  /** Raw response trailers (always empty in browser) */
  rawTrailers: string[];
  
  /** HTTP status code */
  statusCode: number;
  
  /** HTTP status message */
  statusMessage: string;
  
  /** Final URL after redirects (if supported by browser) */
  url: string;
}

Usage Examples:

http.get('https://api.example.com/data', function(res) {
  console.log('Status:', res.statusCode);
  console.log('Headers:', res.headers);
  console.log('Content-Type:', res.headers['content-type']);
  console.log('Final URL:', res.url);
  
  let responseData = '';
  
  res.on('data', function(chunk) {
    responseData += chunk.toString();
  });
  
  res.on('end', function() {
    console.log('Complete response:', responseData);
  });
  
  res.on('error', function(err) {
    console.error('Response error:', err);
  });
});

Streaming Events

ClientRequest Events

// Event emitters for ClientRequest
req.on('response', function(response) {
  // Emitted when response headers are received
  // response is an IncomingMessage instance
});

req.on('error', function(error) {
  // Emitted on request errors (network, timeout, etc.)
});

req.on('timeout', function() {
  // Emitted when socket timeout occurs
});

req.on('requestTimeout', function() {
  // Emitted when request timeout occurs (browser-specific)
});

IncomingMessage Events

// Event emitters for IncomingMessage
res.on('data', function(chunk) {
  // Emitted for each chunk of response data
  // chunk is a Buffer
});

res.on('end', function() {
  // Emitted when response is complete
});

res.on('close', function() {
  // Emitted after 'end' (fake event for Node.js compatibility)
});

res.on('error', function(error) {
  // Emitted on response errors
});

Streaming Performance

True Streaming (Modern Browsers)

Supported in:

  • Chrome 43+ (using Fetch API)
  • Firefox 9+ (using moz-chunked-arraybuffer)

Benefits:

  • Low memory usage (only current chunk in memory)
  • Backpressure support (Chrome 58+ with WritableStream)
  • Real-time data processing

Pseudo-Streaming (Legacy Browsers)

  • Data available before request completion
  • Entire response held in memory
  • Still useful for progress indication

Memory Management

// Efficient streaming example
http.get('https://api.example.com/large-file', function(res) {
  const chunks = [];
  
  res.on('data', function(chunk) {
    // Process chunk immediately to minimize memory usage
    processChunk(chunk);
    
    // Or collect for later processing
    chunks.push(chunk);
  });
  
  res.on('end', function() {
    if (chunks.length > 0) {
      const fullData = Buffer.concat(chunks);
      processFullData(fullData);
    }
  });
});

Browser Compatibility

Response URL Support

The response.url property (final URL after redirects) is available in:

  • Chrome 37+
  • Firefox 32+
  • Safari 9+
  • Not supported in IE/Edge (older versions)

Streaming Capabilities

Stream-http automatically detects and uses the best available method:

  1. Fetch API (preferred): Modern browsers with full streaming support
  2. XHR with chunked responses: Firefox with moz-chunked-arraybuffer
  3. XHR with MS Stream: Internet Explorer with ms-stream
  4. Standard XHR: Universal fallback with pseudo-streaming

Install with Tessl CLI

npx tessl i tessl/npm-stream-http

docs

browser-features.md

http-requests.md

index.md

streaming.md

tile.json