
Product
Unify Your Security Stack with Socket Basics
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.
web-streams-polyfill
Advanced tools
Web Streams, based on the WHATWG spec reference implementation.
This library comes in multiple variants:
web-streams-polyfill
: a ponyfill that provides the stream implementations
without replacing any globals, targeting ES2015+ environments.
Recommended for use in Node 6+ applications, or in web libraries supporting modern browsers.web-streams-polyfill/es5
: a ponyfill targeting ES5+ environments.
Recommended for use in legacy Node applications, or in web libraries supporting older browsers.web-streams-polyfill/polyfill
: a polyfill that replaces the native stream implementations,
targeting ES2015+ environments.
Recommended for use in web apps supporting modern browsers through a <script>
tag.web-streams-polyfill/polyfill/es5
: a polyfill targeting ES5+ environments.
Recommended for use in web apps supporting older browsers through a <script>
tag.Each variant also includes TypeScript type definitions, compatible with the DOM type definitions for streams included in TypeScript. These type definitions require TypeScript version 5.7 or higher.
In version 4, the list of variants was reworked to have more modern defaults and to reduce the download size of the package. See the migration guide for more information.
Usage as a polyfill:
<!-- option 1: hosted by unpkg CDN -->
<script src="https://unpkg.com/web-streams-polyfill/dist/polyfill.js"></script>
<!-- option 2: self hosted -->
<script src="/path/to/web-streams-polyfill/dist/polyfill.js"></script>
<script>
var readable = new ReadableStream();
</script>
Usage as a Node module:
var streams = require("web-streams-polyfill");
var readable = new streams.ReadableStream();
Usage as a ponyfill from within a ES2015 module:
import { ReadableStream } from "web-streams-polyfill";
const readable = new ReadableStream();
Usage as a polyfill from within an ES2015 module:
import "web-streams-polyfill/polyfill";
const readable = new ReadableStream();
[!WARNING] Compatibility with built-in streams
If your browser or runtime already supports Web Streams, loading the polyfill will unconditionally replace the global
ReadableStream
,WritableStream
andTransformStream
classes with the polyfill's versions. However, browser APIs likefetch()
will still return stream objects using the built-in stream classes. This can lead to surprising results, for exampleResponse.body
will not beinstanceof ReadableStream
after the polyfill replaces the globalReadableStream
class.Consider using
ReadableStream.from()
to convert a built-in stream (e.g. fromfetch()
) to a polyfilled stream, or try loading the polyfill conditionally if you don't always need the polyfill.See issue #20 for more details.
Web Streams are widely supported across all modern browsers (including Chrome, Firefox and Safari) and server runtimes (including Node.js, Deno and Bun). Consider using feature detection to check if your platform's built-in streams implementation can fulfill your app's needs, and load the polyfill only when needed.
Here are a couple of examples to load the polyfill conditionally:
// Check for basic ReadableStream support
if (!globalThis.ReadableStream) {
await import("web-streams-polyfill/polyfill");
}
// Check for basic TransformStream support
if (!globalThis.TransformStream) {
await import("web-streams-polyfill/polyfill");
}
// Check for async iteration support
if (typeof globalThis.ReadableStream?.prototype[Symbol.asyncIterator] !== 'function') {
await import("web-streams-polyfill/polyfill");
}
The polyfill
and ponyfill
variants work in any ES2015-compatible environment.
The polyfill/es5
and ponyfill/es5
variants work in any ES5-compatible environment that has a global Promise
.
If you need to support older browsers or Node versions that do not have a native Promise
implementation
(check the support table), you must first include a Promise
polyfill
(e.g. promise-polyfill).
Async iterable support for ReadableStream
is available in all variants, but requires an ES2018-compatible environment or a polyfill for Symbol.asyncIterator
.
WritableStreamDefaultController.signal
is available in all variants, but requires a global AbortController
constructor. If necessary, consider using a polyfill such as abortcontroller-polyfill.
Reading with a BYOB reader is available in all variants, but requires ArrayBuffer.prototype.transfer()
or structuredClone()
to exist in order to correctly transfer the given view's buffer. If not available, then the buffer won't be transferred during the read.
This package uses subpath exports for its variants. As such, you need Node 12 or higher in order to import
or require()
such a variant.
When using TypeScript, make sure your moduleResolution
is set to "node16"
, "nodenext"
or "bundler"
.
The polyfill implements version 080852c
(3 Apr 2025) of the streams specification.
The polyfill is tested against the same web platform tests that are used by browsers to test their native implementations. The polyfill aims to pass all tests, although it allows some exceptions for practical reasons:
ReadableStream
's async iterator.
Retrieving the correct %AsyncIteratorPrototype%
requires using an async generator (async function* () {}
), which is invalid syntax before ES2018.
Instead, the polyfill creates its own version which is functionally equivalent to the real prototype.name
property of down-leveled constructors is incorrect.length
property of down-leveled constructors and methods with optional arguments is incorrect.Thanks to these people for their work on the original polyfill:
4.2.0 (2025-08-17)
080852c
(#161)This package is a browserify version of the Node.js core streams module. It provides similar functionality for handling streams of data but is designed to mimic Node.js streams specifically, rather than implementing the Web Streams API.
This package is a mirror of the Streams API from Node.js for use in browsers and other environments. It is similar to web-streams-polyfill but focuses on Node.js compatibility and may have differences in API and behavior compared to the Web Streams API.
While not a direct alternative to web-streams-polyfill, blob-polyfill provides Blob support in browsers that do not natively support the Blob constructor. It can be used in conjunction with streams to handle binary data.
FAQs
Web Streams, based on the WHATWG spec reference implementation
The npm package web-streams-polyfill receives a total of 16,691,307 weekly downloads. As such, web-streams-polyfill popularity was classified as popular.
We found that web-streams-polyfill demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.
Product
Socket is launching experimental protection for the Hugging Face ecosystem, scanning for malware and malicious payload injections inside model files to prevent silent AI supply chain attacks.
Research
/Security News
The Socket Threat Research Team uncovered a coordinated campaign that floods the Chrome Web Store with 131 rebranded clones of a WhatsApp Web automation extension to spam Brazilian users.