Node.js v26.0.0 文档


可迭代压缩#>

🌐 Iterable Compression

稳定性: 1 - 实验性

源代码: lib/zlib/iter.js

node:zlib/iter 模块提供压缩和解压缩转换,可用于 node:stream/iter 可迭代流 API。

🌐 The node:zlib/iter module provides compression and decompression transforms for use with the node:stream/iter iterable streams API.

此模块仅在启用 --experimental-stream-iter CLI 标志时可用。

🌐 This module is available only when the --experimental-stream-iter CLI flag is enabled.

每个算法都有一个异步版本(有状态异步生成器,用于 pull()pipeTo())以及一个同步版本(有状态同步生成器,用于 pullSync()pipeToSync())。

🌐 Each algorithm has both an async variant (stateful async generator, for use with pull() and pipeTo()) and a sync variant (stateful sync generator, for use with pullSync() and pipeToSync()).

异步转换在 libuv 线程池上运行压缩,将 I/O 与 JavaScript 执行重叠。同步转换直接在主线程上运行压缩。

🌐 The async transforms run compression on the libuv threadpool, overlapping I/O with JavaScript execution. The sync transforms run compression directly on the main thread.

注意:这些转换的默认设置针对流式传输吞吐量进行了优化, 并且与 node:zlib 中的默认设置不同。特别是,gzip/deflate 的默认级别为 4(而不是 6),内存级别为 9(而不是 8),而 Brotli 默认质量为 6(而不是 11)。这些选择符合常见的 HTTP 服务器配置,并在仅略微降低压缩率的情况下提供显著更快的压缩速度。所有默认设置都可以通过选项进行覆盖。

import { from, pull, bytes, text } from 'node:stream/iter';
import { compressGzip, decompressGzip } from 'node:zlib/iter';

// Async round-trip
const compressed = await bytes(pull(from('hello'), compressGzip()));
const original = await text(pull(from(compressed), decompressGzip()));
console.log(original); // 'hello'const { from, pull, bytes, text } = require('node:stream/iter');
const { compressGzip, decompressGzip } = require('node:zlib/iter');

async function run() {
  const compressed = await bytes(pull(from('hello'), compressGzip()));
  const original = await text(pull(from(compressed), decompressGzip()));
  console.log(original); // 'hello'
}

run().catch(console.error);
import { fromSync, pullSync, textSync } from 'node:stream/iter';
import { compressGzipSync, decompressGzipSync } from 'node:zlib/iter';

// Sync round-trip
const compressed = pullSync(fromSync('hello'), compressGzipSync());
const original = textSync(pullSync(compressed, decompressGzipSync()));
console.log(original); // 'hello'const { fromSync, pullSync, textSync } = require('node:stream/iter');
const { compressGzipSync, decompressGzipSync } = require('node:zlib/iter');

const compressed = pullSync(fromSync('hello'), compressGzipSync());
const original = textSync(pullSync(compressed, decompressGzipSync()));
console.log(original); // 'hello'

compressBrotli([options])#>

compressBrotliSync([options])#>

  • options <Object>
    • chunkSize <number> 输出缓冲区大小。默认值: 65536(64 KB)。
    • params <Object> 键值对象,其中键和值都是 zlib.constants 条目。最重要的压缩器参数是:
      • BROTLI_PARAM_MODE -- BROTLI_MODE_GENERIC(默认),BROTLI_MODE_TEXT,或BROTLI_MODE_FONT
      • BROTLI_PARAM_QUALITY -- 范围从 BROTLI_MIN_QUALITYBROTLI_MAX_QUALITY默认值: 6(不是 BROTLI_DEFAULT_QUALITY,后者为 11)。质量 6 适合流式传输;质量 11 适用于离线/构建时压缩。
      • BROTLI_PARAM_SIZE_HINT -- 预期输入大小。默认值: 0 (未知)。
      • BROTLI_PARAM_LGWIN -- 窗口大小 (以 log2 为单位)。默认值: 20(1 MB)。Brotli 库的默认值是 22(4 MB);减小默认值可以在不显著影响流式工作负载压缩的情况下节省内存。
      • BROTLI_PARAM_LGBLOCK -- 输入块大小 (以2为底的对数)。 有关完整列表,请参见 zlib 文档中的 Brotli 压缩器选项
    • dictionary <Buffer> | <TypedArray> | <DataView>
  • 返回:<Object> 有状态的变换。

创建一个 Brotli 压缩转换。输出与 zlib.brotliDecompress()decompressBrotli()/decompressBrotliSync() 兼容。

🌐 Create a Brotli compression transform. Output is compatible with zlib.brotliDecompress() and decompressBrotli()/decompressBrotliSync().

compressDeflate([options])#>

compressDeflateSync([options])#>

创建一个 deflate 压缩转换。输出与 zlib.inflate()decompressDeflate()/decompressDeflateSync() 兼容。

🌐 Create a deflate compression transform. Output is compatible with zlib.inflate() and decompressDeflate()/decompressDeflateSync().

compressGzip([options])#>

compressGzipSync([options])#>

创建一个 gzip 压缩转换。输出与 zlib.gunzip()decompressGzip()/decompressGzipSync() 兼容。

🌐 Create a gzip compression transform. Output is compatible with zlib.gunzip() and decompressGzip()/decompressGzipSync().

compressZstd([options])#>

compressZstdSync([options])#>

  • options <Object>
    • chunkSize <number> 输出缓冲区大小。默认值: 65536(64 KB)。
    • params <Object> 键值对象,其中键和值都是 zlib.constants 条目。最重要的压缩器参数是:
      • ZSTD_c_compressionLevel -- 默认: ZSTD_CLEVEL_DEFAULT (3)。
      • ZSTD_c_checksumFlag -- 生成校验和。默认值: 0
      • ZSTD_c_strategy -- 压缩策略。值包括 ZSTD_fastZSTD_dfastZSTD_greedyZSTD_lazyZSTD_lazy2ZSTD_btlazy2ZSTD_btoptZSTD_btultraZSTD_btultra2。完整列表请参见 zlib 文档中的 Zstd 压缩器选项
    • pledgedSrcSize <number> 预期未压缩大小(可选提示)。
    • dictionary <Buffer> | <TypedArray> | <DataView>
  • 返回:<Object> 有状态的变换。

创建一个 Zstandard 压缩转换。输出与 zlib.zstdDecompress()decompressZstd()/decompressZstdSync() 兼容。

🌐 Create a Zstandard compression transform. Output is compatible with zlib.zstdDecompress() and decompressZstd()/decompressZstdSync().

decompressBrotli([options])#>

decompressBrotliSync([options])#>

  • options <Object>
    • chunkSize <number> 输出缓冲区大小。默认值: 65536(64 KB)。
    • params <Object> 键值对象,其中键和值都是 zlib.constants 条目。可用的解压参数:
      • BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION -- 布尔值 影响内部内存分配的标志。
      • BROTLI_DECODER_PARAM_LARGE_WINDOW -- 布尔标志,用于启用“大窗口 Brotli”模式(与 RFC 7932 不兼容)。有关详细信息,请参见 zlib 文档中的 Brotli 解压缩器选项
    • dictionary <Buffer> | <TypedArray> | <DataView>
  • 返回:<Object> 有状态的变换。

创建一个 Brotli 解压缩转换。

🌐 Create a Brotli decompression transform.

decompressDeflate([options])#>

decompressDeflateSync([options])#>

创建一个 deflate 解压缩转换。

🌐 Create a deflate decompression transform.

decompressGzip([options])#>

decompressGzipSync([options])#>

创建一个 gzip 解压缩转换。

🌐 Create a gzip decompression transform.

decompressZstd([options])#>

decompressZstdSync([options])#>

  • options <Object>
    • chunkSize <number> 输出缓冲区大小。默认值: 65536(64 KB)。
    • params <Object> 键值对象,其中键和值都是 zlib.constants 条目。可用的解压参数:
      • ZSTD_d_windowLogMax -- 解压器将分配的最大窗口大小(以2为底的对数)。用于限制恶意输入的内存使用。有关详细信息,请参阅 zlib 文档中的 Zstd 解压选项
    • dictionary <Buffer> | <TypedArray> | <DataView>
  • 返回:<Object> 有状态的变换。

创建一个 Zstandard 解压缩转换。

🌐 Create a Zstandard decompression transform.

Node.js 中文网 - 粤ICP备13048890号