Streaming HTTP implementation for browsers with Node.js API compatibility
—
Enhanced browser functionality beyond the standard Node.js HTTP module, including CORS credentials, request modes, timeout handling, and automatic capability detection.
Browser-specific options for handling Cross-Origin Resource Sharing (CORS) and authentication.
interface CORSOptions {
/**
* Send cookies and authentication information with CORS requests
* @default false
*/
withCredentials?: boolean;
}Usage Examples:
const http = require('stream-http');
// Request with credentials (cookies, auth headers)
http.request({
hostname: 'api.example.com',
path: '/protected',
withCredentials: true
}, function(res) {
// This request will include cookies and auth headers
console.log('Authenticated request status:', res.statusCode);
});
// Without credentials (default)
http.get('https://public-api.example.com/data', function(res) {
// This request won't include cookies
});Fine-grained control over streaming behavior and data handling to balance performance with correctness.
interface ModeOptions {
/**
* Request mode affecting streaming behavior and data correctness
* @default 'default'
*/
mode?: 'default' | 'allow-wrong-content-type' | 'prefer-streaming' | 'disable-fetch' | 'prefer-fast';
}Mode Descriptions:
Usage Examples:
// Maximum streaming for text data
http.get({
hostname: 'api.example.com',
path: '/text-stream',
mode: 'prefer-streaming'
}, function(res) {
// Optimized for streaming text data
});
// Force XHR for binary data
http.get({
hostname: 'cdn.example.com',
path: '/image.jpg',
mode: 'disable-fetch'
}, function(res) {
// Ensures binary data integrity
});
// Balanced approach (default)
http.get({
hostname: 'api.example.com',
path: '/mixed-content'
// mode: 'default' is implicit
}, function(res) {
// Good balance of streaming and correctness
});Multiple timeout mechanisms for different aspects of the HTTP request lifecycle.
interface TimeoutOptions {
/**
* Request timeout in milliseconds (entire request lifecycle)
* Browser-specific feature for XHR and Fetch
*/
requestTimeout?: number;
/**
* Socket timeout in milliseconds (Node.js compatibility)
* Applies to pauses in data transfer
*/
timeout?: number;
}Usage Examples:
// Request timeout (entire request must complete within time)
http.request({
hostname: 'slow-api.example.com',
path: '/heavy-computation',
requestTimeout: 10000 // 10 seconds total
}, function(res) {
// Request will timeout if not completed in 10 seconds
});
// Socket timeout (pauses in data transfer)
const req = http.request('https://api.example.com/streaming-data');
req.setTimeout(5000, function() {
console.log('Socket timeout - no data received for 5 seconds');
req.abort();
});
// Combined timeouts
http.request({
hostname: 'api.example.com',
path: '/data',
requestTimeout: 30000, // Total request timeout
timeout: 5000 // Socket inactivity timeout
}, function(res) {
// Will timeout if total request > 30s OR if no data for 5s
});Access to final URLs after browser-handled redirects (when supported).
interface ResponseURL {
/** Final URL after all redirects (browser-dependent) */
url?: string;
}Usage Examples:
http.get('https://short.ly/redirect-url', function(res) {
if (res.url) {
console.log('Original URL: https://short.ly/redirect-url');
console.log('Final URL:', res.url);
} else {
console.log('URL tracking not supported in this browser');
}
});Stream-http automatically detects browser capabilities to choose the optimal implementation strategy.
interface BrowserCapabilities {
/** Fetch API and ReadableStream support */
fetch: boolean;
/** WritableStream support for backpressure */
writableStream: boolean;
/** AbortController support for request cancellation */
abortController: boolean;
/** ArrayBuffer responseType support */
arraybuffer: boolean;
/** IE-specific MSStream support */
msstream: boolean;
/** Firefox-specific chunked arraybuffer support */
mozchunkedarraybuffer: boolean;
/** XHR overrideMimeType support */
overrideMimeType: boolean;
}Stream-http automatically selects the best implementation:
Fetch API (preferred when available)
XHR with Progressive Response
moz-chunked-arraybuffer for true streamingms-stream for streaming supportarraybuffer or text modesStandard XHR (universal fallback)
The library chooses implementation based on:
// The library handles this automatically:
// if (capability.fetch && useFetch) {
// return 'fetch'
// } else if (capability.mozchunkedarraybuffer) {
// return 'moz-chunked-arraybuffer'
// } else if (capability.msstream) {
// return 'ms-stream'
// } else if (capability.arraybuffer && preferBinary) {
// return 'arraybuffer'
// } else {
// return 'text'
// }| Browser | Version | Fetch API | True Streaming | Backpressure | AbortController | response.url |
|---|---|---|---|---|---|---|
| Chrome | 43+ | 42+ | ✓ (43+) | ✓ (58+) | ✓ (66+) | ✓ (37+) |
| Firefox | 9+ | 39+ | ✓ (moz-chunked) | ✗ | ✓ (57+) | ✓ (32+) |
| Safari | 6+ | 10.1+ | ✓ (10.1+) | ✗ | ✓ (11.1+) | ✓ (9+) |
| Edge | 12+ | 14+ | ✓ (14+) | ✗ | ✓ (16+) | ✓ (14+) |
| IE | 11 | ✗ | ✗ (pseudo) | ✗ | ✗ | ✗ |
Stream-http automatically detects these capabilities:
// Detected automatically by the library
const capabilities = {
fetch: typeof fetch === 'function' && typeof ReadableStream === 'function',
writableStream: typeof WritableStream === 'function',
abortController: typeof AbortController === 'function',
arraybuffer: true, // or detected via XHR responseType support
msstream: false, // IE-specific, detected via XHR responseType
mozchunkedarraybuffer: false, // Firefox-specific streaming
overrideMimeType: true // or detected via XHR.overrideMimeType
};// Request timeout (requestTimeout option)
req.on('requestTimeout', function() {
console.log('Request exceeded requestTimeout limit');
});
// Socket timeout (setTimeout method)
req.on('timeout', function() {
console.log('Socket was inactive for too long');
});
// Network/CORS errors
req.on('error', function(err) {
console.log('Request failed:', err.message);
});abort() method// Comprehensive error handling
const req = http.request({
hostname: 'api.example.com',
path: '/data',
requestTimeout: 10000
});
req.on('error', function(err) {
console.error('Request error:', err);
});
req.on('requestTimeout', function() {
console.error('Request timed out');
});
req.setTimeout(5000, function() {
console.error('Socket timeout');
req.abort();
});
req.end();Install with Tessl CLI
npx tessl i tessl/npm-stream-http