Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
web-streams-polyfill
Advanced tools
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.
For node.js / browserify users:
var streams = require("web-streams-polyfill");
var readable = new streams.ReadableStream;
// Or, in ES6
import { ReadableStream } from "web-streams-polyfill";
For use as polyfill, include the dist/polyfill.min.js
file in your html before your main script.
<script src="/path/to/polyfill.min.js"></script>
<script type="text/javascript">
var readable = new window.ReadableStream;
</script>
Thanks to these people for their contributions:
FAQs
Web Streams, based on the WHATWG spec reference implementation
The npm package web-streams-polyfill receives a total of 10,049,090 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
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.