readableStream.pipeThrough(transform[, options])


  • transform <Object>
    • readable <ReadableStream> transform.writable 将可能被修改的数据推送到的 ReadableStream 是从这个 ReadableStream 接收数据的。
    • writable <WritableStream> 将写入此 ReadableStream 数据的 WritableStream
  • options <Object>
    • preventAbort <boolean> 当为 true 时,该 ReadableStream 中的错误不会导致 transform.writable 被中止。
    • preventCancel <boolean> 当设置为 true 时,目标 transform.writable 中的错误不会导致此 ReadableStream 被取消。
    • preventClose <boolean> 当为 true 时,关闭此 ReadableStream 不会导致 transform.writable 被关闭。
    • signal <AbortSignal> 允许使用 <AbortController> 取消数据传输。
  • 返回:从 transform.readable 获取 <ReadableStream>

将此 <ReadableStream> 连接到 transform 参数中提供的 <ReadableStream><WritableStream> 对,使得此 <ReadableStream> 的数据被写入 transform.writable,可能经过转换,然后推送到 transform.readable。一旦管道配置完成,将返回 transform.readable

🌐 Connects this <ReadableStream> to the pair of <ReadableStream> and <WritableStream> provided in the transform argument such that the data from this <ReadableStream> is written in to transform.writable, possibly transformed, then pushed to transform.readable. Once the pipeline is configured, transform.readable is returned.

在管道操作进行时,会导致 readableStream.lockedtrue

🌐 Causes the readableStream.locked to be true while the pipe operation is active.

import {
  ReadableStream,
  TransformStream,
} from 'node:stream/web';

const stream = new ReadableStream({
  start(controller) {
    controller.enqueue('a');
  },
});

const transform = new TransformStream({
  transform(chunk, controller) {
    controller.enqueue(chunk.toUpperCase());
  },
});

const transformedStream = stream.pipeThrough(transform);

for await (const chunk of transformedStream)
  console.log(chunk);const {
  ReadableStream,
  TransformStream,
} = require('node:stream/web');

const stream = new ReadableStream({
  start(controller) {
    controller.enqueue('a');
  },
});

const transform = new TransformStream({
  transform(chunk, controller) {
    controller.enqueue(chunk.toUpperCase());
  },
});

const transformedStream = stream.pipeThrough(transform);

(async () => {
  for await (const chunk of transformedStream)
    console.log(chunk);
})();