Registry / http-networking / stream-http

stream-http

JSON →
library3.2.0jsnpmunverified

stream-http is a JavaScript library designed to provide a browser-compatible implementation of Node.js's native `http` module. Its primary goal is to replicate the Node.js HTTP client API and behavior as closely as possible within the constraints of web browsers, making it suitable for projects that need consistent HTTP request handling across Node.js and browser environments. The package is currently at version 3.2.0 and appears to be actively maintained, indicated by recent feature additions and ongoing support for modern browser capabilities. A key differentiator is its emphasis on streaming, delivering data to the caller before the request fully completes. It supports true streaming with backpressure in Chrome >= 58 via `fetch` and `WritableStream`, true streaming in Chrome >= 43 (via `fetch`) and Firefox >= 9 (via `moz-chunked-arraybuffer`), and pseudo-streaming in other supported browsers where the full response is held in memory but available early. It aims to replace `http-browserify`. It also provides additional browser-specific features like `message.url` for redirects and `options.withCredentials` for CORS requests.

npm install stream-http
INSTALL
IMPORT
SIG · STREAM-HTTP
S
stream-http
http-networkingjavascriptv3.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

http
import http from 'stream-http'
const http = require('stream-http')
For modern module bundlers and ESM environments. This is the recommended way.
http
const http = require('stream-http')
import http from 'stream-http'
CommonJS style, typically used with older bundlers like Browserify or Webpack < 5 without specific ESM configuration.
METHODS, STATUS_CODES
import http from 'stream-http'; const { METHODS, STATUS_CODES } = http;
import { METHODS, STATUS_CODES } from 'stream-http'
METHODS and STATUS_CODES are properties of the default `http` export, not named exports from the module root.

This quickstart demonstrates how to make a GET request using the `http.request` method, similar to Node.js. It shows how to set request options, handle the response stream, and process incoming data chunks until the request ends, including error handling.

import http from 'stream-http'; // Example: Fetch data from a public API const options = { host: 'jsonplaceholder.typicode.com', path: '/posts/1', method: 'GET' }; const req = http.request(options, (res) => { console.log(`STATUS: ${res.statusCode}`); console.log(`HEADERS: ${JSON.stringify(res.headers)}`); let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { console.log('No more data in response.'); try { const parsedData = JSON.parse(data); console.log('Response body:', parsedData); } catch (e) { console.error('Failed to parse JSON:', e); console.log('Raw response body:', data); } }); }); req.on('error', (e) => { console.error(`problem with request: ${e.message}`); }); req.end();
Debug
Known issues
breakingAs of version 3.0.0, Internet Explorer 10 and below are no longer supported. IE11 support remains but users of older IE versions must stick to stream-http v2 or earlier.
fix
Upgrade to a modern browser (IE11+) or use an older version of `stream-http` if IE10- support is critical.
affects: >=3.0.0
gotchaUsing `options.mode` set to 'allow-wrong-content-type' or 'prefer-streaming' can lead to incorrect 'content-type' response headers (e.g., 'text/plain; charset=x-user-defined') in some browsers, notably Safari and Chrome 42 and older. 'prefer-streaming' can also corrupt binary data.
fix
Use the default `options.mode` for general purpose requests, especially with binary data or when accurate Content-Type headers are critical. Only use 'prefer-streaming' for known text data.
affects: >=1.5.0
gotchaDue to inherent browser limitations, `stream-http` cannot replicate the Node.js `http` module's API and behavior with 100% fidelity. Specific features like fine-grained control over redirects or certain socket options are unavailable.
fix
Review the `stream-http` documentation and browser API capabilities for specific features that might not be fully supported. Adjust application logic to account for browser-specific behaviors like silent redirects.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'request')
The `http` object was not correctly imported or `require`d, or it's being used in an environment where it's not available.
fix
Ensure you are using the correct `import` or `require` statement for your module system (e.g., `import http from 'stream-http'` for ESM, `const http = require('stream-http')` for CommonJS).
XMLHttpRequest cannot load [URL]. No 'Access-Control-Allow-Origin' header is present on the requested resource.
A cross-origin request was blocked by the browser's Same-Origin Policy. The server does not send the necessary CORS headers.
fix
Ensure the server you are requesting from is configured to send `Access-Control-Allow-Origin` headers that permit requests from your domain. For requests that need to send cookies or authentication, set `options.withCredentials = true` in your request configuration.
Response content-type header is 'text/plain; charset=x-user-defined' when expecting JSON/XML/etc.
The `options.mode` was set to 'allow-wrong-content-type' or 'prefer-streaming', which can override or misreport the actual Content-Type header in some older browsers or specific configurations.
fix
Remove or set `options.mode` to 'default' (or `undefined`) unless you explicitly understand and accept the content-type reporting inaccuracies for streaming benefits. Verify browser compatibility for your chosen `mode`.
Upgrade
Version history
3.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
stream-http — npm install stream-http · libregistry