web-streams-polyfill

中文文档
4.5.0发布于 2026/11/03MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 3.3亿

Web Streams, based on the WHATWG spec reference implementation

原始使用文档
文档目录7 节
  1. Links
  2. Usage
  3. Conditional loading
  4. Compatibility
  5. Tooling compatibility
  6. Compliance
  7. Contributors

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

[!WARNING] Compatibility with built-in streams

If your browser or runtime already supports Web Streams, loading the polyfill will unconditionally replace the global ReadableStream, WritableStream and TransformStream classes with the polyfill's versions. However, browser APIs like fetch() will still return stream objects using the built-in stream classes. This can lead to surprising results, for example Response.body will not be instanceof ReadableStream after the polyfill replaces the global ReadableStream class.

Consider using ReadableStream.from() to convert a built-in stream (e.g. from fetch()) to a polyfilled stream, or try loading the polyfill conditionally if you don't always need the polyfill.

See issue #20 for more details.

Conditional loading

Web Streams are widely supported across all modern browsers (including Chrome, Firefox and Safari) and server runtimes (including Node.js, Deno and Bun). Consider using feature detection to check if your platform's built-in streams implementation can fulfill your app's needs, and load the polyfill only when needed.

Here are a couple of examples to load the polyfill conditionally:

// Check for basic ReadableStream support
if (!globalThis.ReadableStream) {
  await import("web-streams-polyfill/polyfill");
}

// Check for basic TransformStream support
if (!globalThis.TransformStream) {
  await import("web-streams-polyfill/polyfill");
}

// Check for async iteration support
if (typeof globalThis.ReadableStream?.prototype[Symbol.asyncIterator] !== 'function') {
  await import("web-streams-polyfill/polyfill");
}

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 080852c (3 Apr 2025) 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:

版本与发布记录

web-streams-polyfill 共发布 40 个版本,首次发布于 2016/03/03;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.5.0latest2026/11/033 天前
4.4.02026/11/023 天前
4.3.02026/05/155 个月前
4.2.02025/08/171.2 年前
4.1.02025/01/061.8 年前
4.0.02024/02/292 年前

其他发布通道:next → 4.0.0-beta.3。

关于 web-streams-polyfill 的常见问题

web-streams-polyfill 是什么?

Web Streams, based on the WHATWG spec reference implementation

如何安装 web-streams-polyfill?

在项目目录运行 npm install web-streams-polyfill,或使用 pnpm add / yarn add / bun add。

web-streams-polyfill 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

web-streams-polyfill 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

web-streams-polyfill 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 8。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

web-streams-polyfill 可以商用吗?

web-streams-polyfill 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

web-streams-polyfill 的下载量有多少?

根据 npm 官方下载统计,web-streams-polyfill 在 2026/10/05 – 2026/11/03 这 30 天内被下载 330,668,565 次,日均约 11,022,286 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

web-streams-polyfill 还在维护吗?

最新版本 4.5.0 发布于 2026/11/03(3 天前);至今共发布 40 个版本,其中过去 12 个月发布了 3 个;首次发布于 2016/03/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。