Streaming HTTP implementation for browsers with Node.js API compatibility
—
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.
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();
});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.
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);
});
});// 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)
});// 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
});Supported in:
moz-chunked-arraybuffer)Benefits:
// 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);
}
});
});The response.url property (final URL after redirects) is available in:
Stream-http automatically detects and uses the best available method:
moz-chunked-arraybufferms-streamInstall with Tessl CLI
npx tessl i tessl/npm-stream-http