UNPKG

7.01 kBMarkdownView Raw
1# web-streams-polyfill
2
3Web Streams, based on the WHATWG spec reference implementation.
4
5[![build status](https://api.travis-ci.com/MattiasBuelens/web-streams-polyfill.svg?branch=master)](https://travis-ci.com/MattiasBuelens/web-streams-polyfill)
6[![npm version](https://img.shields.io/npm/v/web-streams-polyfill.svg)](https://www.npmjs.com/package/web-streams-polyfill)
7[![license](https://img.shields.io/npm/l/web-streams-polyfill.svg)](https://github.com/MattiasBuelens/web-streams-polyfill/blob/master/LICENSE)
8
9## Links
10
11 - [Official spec][spec]
12 - [Reference implementation][ref-impl]
13
14## Usage
15
16This library comes in multiple variants:
17* `web-streams-polyfill`: a [ponyfill] that provides the stream implementations
18 without replacing any globals, targeting ES2015+ environments.
19 Recommended for use in Node 6+ applications, or in web libraries supporting modern browsers.
20* `web-streams-polyfill/es5`: a ponyfill targeting ES5+ environments.
21 Recommended for use in legacy Node applications, or in web libraries supporting older browsers.
22* `web-streams-polyfill/polyfill`: a polyfill that replaces the native stream implementations,
23 targeting ES2015+ environments.
24 Recommended for use in web apps supporting modern browsers through a `<script>` tag.
25* `web-streams-polyfill/polyfill/es5`: a polyfill targeting ES5+ environments.
26 Recommended for use in web apps supporting older browsers through a `<script>` tag.
27
28Each variant also includes TypeScript type definitions, compatible with the DOM type definitions for streams included in TypeScript.
29These type definitions require TypeScript version 4.7 or higher.
30
31In version 4, the list of variants was reworked to have more modern defaults and to reduce the download size of the package.
32See the [migration guide][migrating] for more information.
33
34Usage as a polyfill:
35```html
36<!-- option 1: hosted by unpkg CDN -->
37<script src="https://unpkg.com/web-streams-polyfill/dist/polyfill.js"></script>
38<!-- option 2: self hosted -->
39<script src="/path/to/web-streams-polyfill/dist/polyfill.js"></script>
40<script>
41var readable = new ReadableStream();
42</script>
43```
44Usage as a Node module:
45```js
46var streams = require("web-streams-polyfill");
47var readable = new streams.ReadableStream();
48```
49Usage as a ponyfill from within a ES2015 module:
50```js
51import { ReadableStream } from "web-streams-polyfill";
52const readable = new ReadableStream();
53```
54Usage as a polyfill from within an ES2015 module:
55```js
56import "web-streams-polyfill/polyfill";
57const readable = new ReadableStream();
58```
59
60## Compatibility
61
62The `polyfill` and `ponyfill` variants work in any ES2015-compatible environment.
63
64The `polyfill/es5` and `ponyfill/es5` variants work in any ES5-compatible environment that has a global `Promise`.
65If you need to support older browsers or Node versions that do not have a native `Promise` implementation
66(check the [support table][promise-support]), you must first include a `Promise` polyfill
67(e.g. [promise-polyfill][promise-polyfill]).
68
69[Async iterable support for `ReadableStream`][rs-asynciterator] is available in all variants, but requires an ES2018-compatible environment or a polyfill for `Symbol.asyncIterator`.
70
71[`WritableStreamDefaultController.signal`][ws-controller-signal] is available in all variants, but requires a global `AbortController` constructor. If necessary, consider using a polyfill such as [abortcontroller-polyfill].
72
73[Reading with a BYOB reader][mdn-byob-read] 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.
74
75### Tooling compatibility
76
77This package uses [subpath exports](https://nodejs.org/api/packages.html#subpath-exports) for its variants. As such, you need Node 12 or higher in order to `import` or `require()` such a variant.
78
79When using TypeScript, make sure your [`moduleResolution`](https://www.typescriptlang.org/tsconfig#moduleResolution) is set to `"node16"`, `"nodenext"` or `"bundler"`.
80
81## Compliance
82
83The polyfill implements [version `4dc123a` (13 Nov 2023)][spec-snapshot] of the streams specification.
84
85The polyfill is tested against the same [web platform tests][wpt] that are used by browsers to test their native implementations.
86The polyfill aims to pass all tests, although it allows some exceptions for practical reasons:
87* The default (ES2015) variant passes all of the tests, except for the [test for the prototype of `ReadableStream`'s async iterator][wpt-async-iterator-prototype].
88 Retrieving the correct `%AsyncIteratorPrototype%` requires using an async generator (`async function* () {}`), which is invalid syntax before ES2018.
89 Instead, the polyfill [creates its own version][stub-async-iterator-prototype] which is functionally equivalent to the real prototype.
90* The ES5 variant passes the same tests as the ES2015 variant, except for various tests about specific characteristics of the constructors, properties and methods.
91 These test failures do not affect the run-time behavior of the polyfill.
92 For example:
93 * The `name` property of down-leveled constructors is incorrect.
94 * The `length` property of down-leveled constructors and methods with optional arguments is incorrect.
95 * Not all properties and methods are correctly marked as non-enumerable.
96 * Down-leveled class methods are not correctly marked as non-constructable.
97
98## Contributors
99
100Thanks to these people for their work on [the original polyfill][creatorrr-polyfill]:
101
102 - Diwank Singh Tomer ([creatorrr](https://github.com/creatorrr))
103 - Anders Riutta ([ariutta](https://github.com/ariutta))
104
105[spec]: https://streams.spec.whatwg.org
106[ref-impl]: https://github.com/whatwg/streams
107[ponyfill]: https://github.com/sindresorhus/ponyfill
108[migrating]: https://github.com/MattiasBuelens/web-streams-polyfill/blob/master/MIGRATING.md
109[promise-support]: https://kangax.github.io/compat-table/es6/#test-Promise
110[promise-polyfill]: https://www.npmjs.com/package/promise-polyfill
111[rs-asynciterator]: https://streams.spec.whatwg.org/#rs-asynciterator
112[ws-controller-signal]: https://streams.spec.whatwg.org/#ws-default-controller-signal
113[abortcontroller-polyfill]: https://www.npmjs.com/package/abortcontroller-polyfill
114[mdn-byob-read]: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader/read
115[spec-snapshot]: https://streams.spec.whatwg.org/commit-snapshots/4dc123a6e7f7ba89a8c6a7975b021156f39cab52/
116[wpt]: https://github.com/web-platform-tests/wpt/tree/2a298b616b7c865917d7198a287310881cbfdd8d/streams
117[wpt-async-iterator-prototype]: https://github.com/web-platform-tests/wpt/blob/2a298b616b7c865917d7198a287310881cbfdd8d/streams/readable-streams/async-iterator.any.js#L24
118[stub-async-iterator-prototype]: https://github.com/MattiasBuelens/web-streams-polyfill/blob/v4.0.0/src/lib/readable-stream/async-iterator.ts#L143-L147
119[creatorrr-polyfill]: https://github.com/creatorrr/web-streams-polyfill