stream.Duplex.from(src)


用于创建双工流的实用方法。

¥A utility method for creating duplex streams.

  • Stream 将可写流转换为可写的 Duplex,将可读流转换为 Duplex

    ¥Stream converts writable stream into writable Duplex and readable stream to Duplex.

  • Blob 转换为可读的 Duplex

    ¥Blob converts into readable Duplex.

  • string 转换为可读的 Duplex

    ¥string converts into readable Duplex.

  • ArrayBuffer 转换为可读的 Duplex

    ¥ArrayBuffer converts into readable Duplex.

  • AsyncIterable 转换为可读的 Duplex。无法生成 null

    ¥AsyncIterable converts into a readable Duplex. Cannot yield null.

  • AsyncGeneratorFunction 转换为可读/可写的转换 Duplex。必须将源 AsyncIterable 作为第一个参数。无法生成 null

    ¥AsyncGeneratorFunction converts into a readable/writable transform Duplex. Must take a source AsyncIterable as first parameter. Cannot yield null.

  • AsyncFunction 转换为可写的 Duplex。必须返回 nullundefined

    ¥AsyncFunction converts into a writable Duplex. Must return either null or undefined

  • Object ({ writable, readable })readablewritable 转换为 Stream,然后将它们组合成 Duplex,其中 Duplex 将写入 writable 并从 readable 读取。

    ¥Object ({ writable, readable }) converts readable and writable into Stream and then combines them into Duplex where the Duplex will write to the writable and read from the readable.

  • Promise 转换为可读的 Duplex。忽略值 null

    ¥Promise converts into readable Duplex. Value null is ignored.

  • ReadableStream 转换为可读的 Duplex

    ¥ReadableStream converts into readable Duplex.

  • WritableStream 转换为可写的 Duplex

    ¥WritableStream converts into writable Duplex.

  • 返回:<stream.Duplex>

    ¥Returns: <stream.Duplex>

如果将包含 promise 的 Iterable 对象作为参数传递,可能会导致未处理的拒绝。

¥If an Iterable object containing promises is passed as an argument, it might result in unhandled rejection.

const { Duplex } = require('node:stream');

Duplex.from([
  new Promise((resolve) => setTimeout(resolve('1'), 1500)),
  new Promise((_, reject) => setTimeout(reject(new Error('2')), 1000)), // Unhandled rejection
]);