web-streams-polyfill
Web Streams, based on the WHATWG spec reference implementation.
Links
Usage
This library comes in multiple variants:
web-streams-polyfill
: a ponyfill that provides the stream implementations
without replacing any globals, targeting ES2015+ environments.
- ✅ Node 6+ through
import
or require()
- ✅ Modern web browsers through
import
/export
or <script type="module">
- ✅ Web apps for modern browsers using a bundler (like webpack or Rollup)
web-streams-polyfill/es5
: a ponyfill targeting ES5+ environments.
- ✅ Legacy Node through
require()
- ✅ Legacy web browsers through AMD
- ✅ Web apps for legacy browsers using a bundler (like webpack or Rollup)
web-streams-polyfill/polyfill
: a polyfill that replaces the native stream implementations,
targeting ES2015+ environments.
- ✅ Modern web browsers through regular
<script>
web-streams-polyfill/polyfill/es5
: a polyfill targeting ES5+ environments.
- ✅ Legacy web browsers through regular
<script>
Each variant also includes TypeScript type definitions, compatible with the DOM type definitions for streams included in TypeScript.
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:
<script src="https://unpkg.com/web-streams-polyfill/dist/polyfill.js"></script>
<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 default and polyfill
variants work in any ES2015-compatible environment.
The es5
and polyfill/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.
Compliance
The polyfill implements version cada812
(8 Jul 2021) of the streams specification.
The polyfill is tested against the same web platform tests that are used by browsers to test their native implementations.
It 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 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.
The type definitions are compatible with the built-in stream types of TypeScript 3.3 and higher.
Contributors
Thanks to these people for their work on the original polyfill: