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

http-requests.mddocs/

HTTP Requests

Core functionality for creating HTTP requests with Node.js API compatibility. Provides both simple URL-based requests and comprehensive options-based configuration.

Capabilities

HTTP Request Function

Creates a new HTTP request with full Node.js API compatibility.

/**
 * Creates a new HTTP request
 * @param opts - URL string or options object containing request configuration
 * @param cb - Optional callback function that will be called with the response
 * @returns ClientRequest instance for the outgoing request
 */
function request(opts, cb);

Parameters:

  • opts (string | object): Either a URL string or options object
    • If string: URL to request
    • If object: Request configuration (see RequestOptions below)
  • cb (function, optional): Callback function called with (response) when response headers are received

Returns: ClientRequest instance

Usage Examples:

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

// Simple string URL
const req = http.request('https://api.example.com/data', function(res) {
  console.log('Status:', res.statusCode);
});
req.end();

// Options object
const req = http.request({
  hostname: 'api.example.com',
  port: 443,
  path: '/users',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer token123'
  }
}, function(res) {
  res.on('data', function(chunk) {
    console.log('Data:', chunk.toString());
  });
});

req.write(JSON.stringify({ name: 'Alice' }));
req.end();

HTTP GET Function

Convenience method for GET requests that automatically calls end() on the request.

/**
 * Convenience method for GET requests
 * @param opts - URL string or options object
 * @param cb - Optional callback for 'response' event
 * @returns ClientRequest instance with end() already called
 */
function get(opts, cb);

Parameters:

  • opts (string | object): URL string or options object (same as request())
  • cb (function, optional): Response callback (same as request())

Returns: ClientRequest instance (already ended)

Usage Examples:

// Simple GET request
http.get('https://api.example.com/users', function(res) {
  let data = '';
  res.on('data', function(chunk) {
    data += chunk;
  });
  res.on('end', function() {
    console.log('Users:', JSON.parse(data));
  });
});

// GET with headers
http.get({
  hostname: 'api.example.com',
  path: '/protected',
  headers: {
    'Authorization': 'Bearer token123'
  }
}, function(res) {
  console.log('Protected data status:', res.statusCode);
});

Request Options

Standard Node.js Options

interface StandardRequestOptions {
  /** Protocol (http: or https:) */
  protocol?: string;
  /** Server hostname */
  hostname?: string;
  /** Server host (alternative to hostname) */
  host?: string;
  /** Server port number */
  port?: number;
  /** Request path including query string */
  path?: string;
  /** HTTP method (default: 'GET') */
  method?: string;
  /** Request headers object */
  headers?: object;
  /** Basic authentication string ('user:pass') */
  auth?: string;
}

Browser-Specific Options

interface BrowserRequestOptions {
  /** Send cookies/credentials with CORS requests (default: false) */
  withCredentials?: boolean;
  /** Request mode affecting streaming behavior */
  mode?: 'default' | 'allow-wrong-content-type' | 'prefer-streaming' | 'disable-fetch' | 'prefer-fast';
  /** Request timeout in milliseconds */
  requestTimeout?: number;
  /** Socket timeout in milliseconds (Node.js compatibility) */
  timeout?: number;
}

Complete Request Options

interface RequestOptions extends StandardRequestOptions, BrowserRequestOptions {
  // All properties from both interfaces above
}

Request Modes

The mode option controls how stream-http handles streaming and data correctness:

  • 'default' (or falsy): Balanced performance and correctness
  • 'allow-wrong-content-type': More streaming but may affect content-type header
  • 'prefer-streaming': Maximum streaming, may corrupt binary data
  • 'disable-fetch': Force XHR usage for full compatibility
  • 'prefer-fast': Deprecated, same as 'default'

URL Construction

Stream-http automatically constructs URLs from options:

// These are equivalent:
http.request('https://example.com:8080/path?query=value');

http.request({
  protocol: 'https:',
  hostname: 'example.com',
  port: 8080,
  path: '/path?query=value'
});

Protocol defaults to current page protocol if not specified (except for file: protocol which defaults to 'http:').

Install with Tessl CLI

npx tessl i tessl/npm-stream-http

docs

browser-features.md

http-requests.md

index.md

streaming.md

tile.json