Streaming HTTP implementation for browsers with Node.js API compatibility
—
Core functionality for creating HTTP requests with Node.js API compatibility. Provides both simple URL-based requests and comprehensive options-based configuration.
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
cb (function, optional): Callback function called with (response) when response headers are receivedReturns: 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();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);
});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;
}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;
}interface RequestOptions extends StandardRequestOptions, BrowserRequestOptions {
// All properties from both interfaces above
}The mode option controls how stream-http handles streaming and data correctness:
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