stream.Duplex.from(src)


创建双工流的实用方法。

  • Stream 将可写流转换为可写的 Duplex,将可读流转换为 Duplex
  • Blob 转换为可读的 Duplex
  • string 转换为可读的 Duplex
  • ArrayBuffer 转换为可读的 Duplex
  • AsyncIterable 转换为可读的 Duplex。 无法产生 null
  • AsyncGeneratorFunction 转换为可读/可写的转换 Duplex。 必须将源 AsyncIterable 作为第一个参数。 无法产生 null
  • AsyncFunction 转换为可写的 Duplex。 必须返回 nullundefined
  • Object ({ writable, readable })readablewritable 转换为 Stream,然后将它们组合成 Duplex,其中 Duplex 将写入 writable 并从 readable 读取。
  • Promise 转换为可读的 Duplex。 值 null 被忽略。
  • 返回: <stream.Duplex>

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

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

Duplex.from([
  new Promise((resolve) => setTimeout(resolve('1'), 1500)),
  new Promise((_, reject) => setTimeout(reject(new Error('2')), 1000)), // 未处理的拒绝
]);

A utility method for creating duplex streams.

  • Stream converts writable stream into writable Duplex and readable stream to Duplex.
  • Blob converts into readable Duplex.
  • string converts into readable Duplex.
  • ArrayBuffer converts into readable Duplex.
  • AsyncIterable converts into a readable Duplex. Cannot yield null.
  • AsyncGeneratorFunction converts into a readable/writable transform Duplex. Must take a source AsyncIterable as first parameter. Cannot yield null.
  • AsyncFunction converts into a writable Duplex. Must return either null or undefined
  • 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 converts into readable Duplex. Value null is ignored.
  • Returns: <stream.Duplex>

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
]);