stream.Duplex.from(src)
src
<Stream> | <Blob> | <ArrayBuffer> | <string> | <Iterable> | <AsyncIterable> | <AsyncGeneratorFunction> | <AsyncFunction> | <Promise> | <Object>
创建双工流的实用方法。
Stream
将可写流转换为可写的Duplex
,将可读流转换为Duplex
。Blob
转换为可读的Duplex
。string
转换为可读的Duplex
。ArrayBuffer
转换为可读的Duplex
。AsyncIterable
转换为可读的Duplex
。 无法产生null
。AsyncGeneratorFunction
转换为可读/可写的转换Duplex
。 必须将源AsyncIterable
作为第一个参数。 无法产生null
。AsyncFunction
转换为可写的Duplex
。 必须返回null
或undefined
Object ({ writable, readable })
将readable
和writable
转换为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)), // 未处理的拒绝
]);
src
<Stream> | <Blob> | <ArrayBuffer> | <string> | <Iterable> | <AsyncIterable> | <AsyncGeneratorFunction> | <AsyncFunction> | <Promise> | <Object>
A utility method for creating duplex streams.
Stream
converts writable stream into writableDuplex
and readable stream toDuplex
.Blob
converts into readableDuplex
.string
converts into readableDuplex
.ArrayBuffer
converts into readableDuplex
.AsyncIterable
converts into a readableDuplex
. Cannot yieldnull
.AsyncGeneratorFunction
converts into a readable/writable transformDuplex
. Must take a sourceAsyncIterable
as first parameter. Cannot yieldnull
.AsyncFunction
converts into a writableDuplex
. Must return eithernull
orundefined
Object ({ writable, readable })
convertsreadable
andwritable
intoStream
and then combines them intoDuplex
where theDuplex
will write to thewritable
and read from thereadable
.Promise
converts into readableDuplex
. Valuenull
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
]);