Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

web-streams-polyfill

MattiasBuelens66.7mMIT4.1.0TypeScript support: included

Web Streams, based on the WHATWG spec reference implementation

streams, whatwg, polyfill

readme

web-streams-polyfill

Web Streams, based on the WHATWG spec reference implementation.

build status npm version license

Usage

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 4.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();

Compatibility

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.

Tooling compatibility

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".

Compliance

The polyfill implements version fa4891a (3 Dec 2024) 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:

  • The default (ES2015) variant passes all of the tests, except for the test for the prototype of 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.
  • The ES5 variant passes the same tests as the ES2015 variant, except for various tests about specific characteristics of the constructors, properties and methods. These test failures do not affect the run-time behavior of the polyfill. For example:
    • The name property of down-leveled constructors is incorrect.
    • The length property of down-leveled constructors and methods with optional arguments is incorrect.
    • Not all properties and methods are correctly marked as non-enumerable.
    • Down-leveled class methods are not correctly marked as non-constructable.

Contributors

Thanks to these people for their work on the original polyfill:

changelog

Changelog

Tags:

  • 💥 Breaking Change
  • 👓 Spec Compliance
  • 🚀 New Feature
  • 🐛 Bug Fix
  • 👎 Deprecation
  • 📝 Documentation
  • 🏠 Internal
  • 💅 Polish

4.1.0 (2025-01-05)

  • 👓 Align with spec version fa4891a (#156)
    • Commit pull-into descriptors after filling them from the internal queue. This prevents an issue where an incorrect BYOB request would temporarily be visible through a patched Object.prototype.then, which broke some internal invariants.
    • The next() and return() methods of ReadableStream's async iterator are now correctly "chained", such that the promises returned by either of these methods are always resolved in the same order as those methods were called.
  • 💅 Improve type of WritableStreamDefaultController.signal. (#157)

4.0.0 (2024-02-28)

  • 💥 Rework the list of variants to have more modern defaults. The table below shows how to upgrade your v3 import to their equivalent v4 import. See the migration guide for more information. (#82, #139)
  • 💥 Remove the ES2018 variant, in favor of the ES2015 variant.
  • 💥 Switch to subpath exports for variants.
    • Node 12 or higher is required to import or require() a variant.
    • When using TypeScript, version 4.7 or higher is now required. Additionally, moduleResolution must be set to "node16", "nodenext" or "bundler".
  • 🚀 Support importing as ESM in Node.
  • 💅 Minify all code in the published package, to reduce the download size.
  • 💅 Rework ReadableStream.from() implementation to avoid depending on async function* down-leveling for ES5. (#144)
v3 import v4 import description
web-streams-polyfill web-streams-polyfill/polyfill/es5 ES5+ polyfill
web-streams-polyfill/es6 web-streams-polyfill/polyfill ES2015+ polyfill
web-streams-polyfill/es2018 web-streams-polyfill/polyfill ES2015+ polyfill
web-streams-polyfill/ponyfill web-streams-polyfill/es5 ES5+ ponyfill
web-streams-polyfill/ponyfill/es6 web-streams-polyfill ES2015+ ponyfill
web-streams-polyfill/ponyfill/es2018 web-streams-polyfill ES2015+ ponyfill

3.3.3 (2024-02-16)

  • 🐛 Fix ReadableStream to match TypeScript's AsyncIterable<R> type. (#141, #142)

3.3.2 (2024-01-04)

  • 🐛 Fix bad publish to npm.

3.3.1 (2024-01-04)

  • 🐛 Revert engines bump in package.json. (#137, #138)
  • 🐛 Re-introduce support for TypeScript 3.5 and below. (#137, #138)

3.3.0 (2024-01-04)

  • 🚀 Added global augmentations for ReadableStream to the polyfill's type definitions. (#130)
    • This allows TypeScript users to use new methods such as ReadableStream.prototype[Symbol.asyncIterator](), even when TypeScript doesn't yet have a built-in type definition for them.
  • 💥 The type definitions now require TypeScript 3.6 or higher. (#130) (Reverted in version 3.3.1)
  • 👓 Align with spec version 4dc123a (#115, #134)
    • Added ReadableStream.from(asyncIterable), which creates a ReadableStream wrapping the given iterable or async iterable. This can also be used to wrap a native ReadableStream (e.g. a Response.body from fetch()), even if the native ReadableStream doesn't yet support async iteration. (#135)
    • Added Transformer.cancel method, which is called when the readable side of a TransformStream is cancelled or when its writable side is aborted.
    • Added min option to ReadableStreamBYOBReader.read(view, options).
    • Added support for AbortSignal.reason when aborting a pipe.
  • 🚀 Buffers passed to ReadableStreamBYOBReader.read(view) will now be correctly transferred if either ArrayBuffer.prototype.transfer() or structuredClone() is available. (#136)
  • 🐛 Prevent warnings from Bluebird about a promise being created within a handler but not being returned from a handler. (#131)
  • 🏠 Improve internal DOMException polyfill. (#133)

v3.2.1 (2022-04-07)

  • 🐛 Fix an error in very old browsers where Function.name is non-configurable. (#112)

v3.2.0 (2021-11-06)

  • 👎 Deprecate WritableStreamDefaultController.abortReason (#102)
    • Use WritableStreamDefaultController.signal.reason instead.
  • 👓 Align with spec version 4b6b93c (#103)

v3.1.1 (2021-09-06)

  • 🐛 Fix compatibility with TypeScript's DOM types for ReadableStream and TransformStream (#85, #86)

v3.1.0 (2021-07-21)

  • 🚀 Calling ReadableStream.tee() on a readable byte stream now returns two readable byte streams. (#81)
  • 🚀 Add WritableStreamDefaultController.signal and .abortReason. (#81)
    • .signal requires a global AbortController constructor to be available. If necessary, use a polyfill.
  • 🐛 Make sure streams created with a different version of the polyfill do not pass the brand checks. (#75, #77)
  • 👓 Align with spec version cada812 (#79, #81)

v3.0.3 (2021-04-09)

  • 💅 Change Promise<void> to Promise<undefined> in TypeScript type definitions (#72)
  • 🐛 Fix ReadableStream.tee() resolving its cancel() promise too early (#73)
  • 👓 Align with spec version 6762cdb (#73)

v3.0.2 (2021-02-10)

v3.0.1 (2020-11-12)

  • 📝 Add documentation to type definitions (#62)
  • 👓 Align with spec version 6cd5e81 (#63)
  • 🐛 Fix an issue where the polyfill could throw an error when resolving/rejecting reader.closed when it was already resolved/rejected (#66, #67)

v3.0.0 (2020-07-20)

  • 💥 Align with spec version 62fe4c8 (#52, #57, #59)
    This includes the following breaking changes:
    • All classes are now exposed globally. Concretely, this adds the following classes:
      • ReadableStreamDefaultController
      • ReadableByteStreamController
      • ReadableStreamBYOBRequest
      • ReadableStreamDefaultReader
      • ReadableStreamBYOBReader
      • WritableStreamDefaultController
      • WritableStreamDefaultWriter
      • TransformStreamDefaultController
    • ReadableStream.getIterator() is renamed to ReadableStream.values()
    • ReadableByteStreamController.byobRequest can be null (instead of undefined) if there is no current BYOB request.
    • ReadableStreamBYOBRequest.view can be null (instead of undefined) if the BYOB request has already been responded to.
    • Constructors and methods have stricter type checking for object arguments. For example, new ReadableStream(null) would previously behave like new ReadableStream({}), but now it throws a TypeError instead.
    • Some constructors and methods may throw slightly different errors when given invalid arguments.
    • Various byte-stream-related APIs now prohibit zero-length views or buffers.
    • The async iterator of a ReadableStream now behaves more like an async generator, e.g. returning promises fulfilled with { value: undefined, done: true } after return()ing the iterator, instead of returning a rejected promise.
  • 💥 Updated TypeScript types to align with new specification (#60)
    While these are technically breaking changes, you should only be affected if you manually reference these types from your code.
    • PipeOptions is renamed to StreamPipeOptions
    • ReadResult is replaced by ReadableStreamDefaultReadResult and ReadableStreamBYOBReadResult
    • ReadableStreamDefaultControllerCallback is replaced by UnderlyingSourceStartCallback and UnderlyingSourcePullCallback
    • ReadableByteStreamControllerCallback is replaced by UnderlyingByteSourceStartCallback and UnderlyingByteSourcePullCallback
    • ReadableStreamErrorCallback is renamed to UnderlyingSourceCancelCallback
    • WritableStreamDefaultControllerStartCallback is renamed to UnderlyingSinkStartCallback
    • WritableStreamDefaultControllerWriteCallback is renamed to UnderlyingSinkWriteCallback
    • WritableStreamDefaultControllerCloseCallback is renamed to UnderlyingSinkCloseCallback
    • WritableStreamErrorCallback is renamed to UnderlyingSinkAbortCallback
    • TransformStreamDefaultControllerCallback is replaced by TransformerStartCallback and TransformerFlushCallback
    • TransformStreamDefaultControllerTransformCallback is renamed to TransformerTransformCallback

v2.1.1 (2020-04-11)

  • 💅 Improve ReadResult in TypeScript type definitions. (759506e, #49)

v2.1.0 (2020-02-23)

v2.0.6 (2019-11-08)

  • 🐛 Fix type definitions to be compatible with TypeScript 3.3 and lower. (#39, #40)

v2.0.5 (2019-10-08)

  • 👓 Align with spec version ae5e0cb (#33)
  • 🐛 Fix support for non-browser environments, such as Node.
    • Accept polyfilled AbortSignals. (#36)
    • Polyfill DOMException if necessary. (#37)

v2.0.4 (2019-08-01)

v2.0.3 (2019-04-04)

v2.0.2 (2019-03-17)

  • 💅 Improve performance of reader.read() and writer.write() (#17, #18)

v2.0.1 (2019-03-16)

  • 🐛 Fix performance issue with large queues (#15, #16)

v2.0.0 (2019-03-10)