Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
web-streams-polyfill
Advanced tools
Web Streams, based on the WHATWG spec reference implementation
The web-streams-polyfill package is a polyfill for the Streams API, which provides a standard interface for representing and handling a sequence of data in JavaScript. This polyfill allows developers to use the Streams API in environments where it is not natively supported.
ReadableStream
This feature allows you to create a stream of data that can be read in chunks. The code sample demonstrates how to create a simple ReadableStream that enqueues two chunks of data and then closes the stream.
const readableStream = new ReadableStream({
start(controller) {
controller.enqueue('Hello, ');
controller.enqueue('World!');
controller.close();
}
});
readableStream.getReader().read().then(({ value, done }) => {
if (!done) console.log(value);
});
WritableStream
This feature allows you to create a stream where data can be written. The code sample shows how to create a WritableStream and write a chunk of data to it, then close the stream.
const writableStream = new WritableStream({
write(chunk) {
console.log(chunk);
},
close() {
console.log('Stream closed');
}
});
const writer = writableStream.getWriter();
writer.write('Hello, World!').then(() => writer.close());
TransformStream
This feature allows you to create a stream that transforms data as it passes through. The code sample demonstrates how to create a TransformStream that converts chunks of text to uppercase and then pipes data from a ReadableStream through it to a WritableStream.
const uppercaseTransform = new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk.toUpperCase());
}
});
const readable = new ReadableStream({
start(controller) {
controller.enqueue('hello');
controller.enqueue('world');
controller.close();
}
});
const writable = new WritableStream({
write(chunk) {
console.log(chunk);
}
});
readable.pipeThrough(uppercaseTransform).pipeTo(writable);
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.
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.
import
or require()
import
/export
or <script type="module">
web-streams-polyfill/es5
: a ponyfill targeting ES5+ environments.
require()
web-streams-polyfill/polyfill
: a polyfill that replaces the native stream implementations,
targeting ES2015+ environments.
<script>
web-streams-polyfill/polyfill/es5
: a polyfill targeting ES5+ environments.
<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:
<!-- 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();
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.
The polyfill implements version e9355ce
(18 Apr 2022) 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:
ArrayBuffer
, which is not yet possible from JavaScript (although there is a proposal to make it possible).
The reference implementation "cheats" on these tests by making a copy instead, but that is unacceptable for the polyfill's performance (#3).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.Object.prototype.then
.
These tests are meant for browsers to ensure user-land modifications cannot affect the internal logic of pipeTo()
and tee()
.
However, it's not reasonable or desirable for a user-land polyfill to try and isolate itself completely from using the global Object
.pipeTo()
tests that require synchronous inspection of the stream's state (1, 2).
Because the polyfill uses the public getReader()
and getWriter()
API to implement pipeTo()
, it can only asynchronously observe if and when a stream becomes closed or errored.
Therefore, when the readable and the writable end become errored at the exact same time, it's difficult for the polyfill to observe these state changes in exactly the same order.name
property of down-leveled constructors is incorrect.length
property of down-leveled constructors and methods with optional arguments is incorrect.The type definitions are compatible with the built-in stream types of TypeScript 3.3 and higher.
Thanks to these people for their work on the original polyfill:
FAQs
Web Streams, based on the WHATWG spec reference implementation
The npm package web-streams-polyfill receives a total of 11,687,058 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.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.