stream.compose(...streams)
添加於:v16.9.0
Stability: 1 -
stream.compose
是實驗性的。參數
streams
<Stream[]> | <Iterable[]> | <AsyncIterable[]> | <Function[]>- 返回: <stream.Duplex>
將兩個或多個流組合成 Duplex
流,該流寫入第一個流並從最後一個流讀取。使用 stream.pipeline
將每個提供的流通過管道傳送到下一個。如果任何流錯誤,則所有流都將被銷毀,包括外部Duplex
流。
因為stream.compose
返回一個新流,該流又可以(並且應該)通過管道傳輸到其他流中,因此它啟用了組合。相反,當將流傳遞給 stream.pipeline
時,通常第一個流是可讀流,最後一個流是可寫流,形成一個閉路。
如果通過 Function
它必須是采用 source
Iterable
的工廠方法。
import { compose, Transform } from 'node:stream';
const removeSpaces = new Transform({
transform(chunk, encoding, callback) {
callback(null, String(chunk).replace(' ', ''));
}
});
async function* toUpper(source) {
for await (const chunk of source) {
yield String(chunk).toUpperCase();
}
}
let res = '';
for await (const buf of compose(removeSpaces, toUpper).end('hello world')) {
res += buf;
}
console.log(res); // prints 'HELLOWORLD'
stream.compose
可用於將異步迭代、生成器和函數轉換為流。
AsyncIterable
轉換為可讀的Duplex
。無法產生null
。AsyncGeneratorFunction
轉換為可讀/可寫轉換Duplex
。必須將源AsyncIterable
作為第一個參數。無法產生null
。AsyncFunction
轉換為可寫的Duplex
。必須返回null
或undefined
。
import { compose } from 'node:stream';
import { finished } from 'node:stream/promises';
// Convert AsyncIterable into readable Duplex.
const s1 = compose(async function*() {
yield 'Hello';
yield 'World';
}());
// Convert AsyncGenerator into transform Duplex.
const s2 = compose(async function*(source) {
for await (const chunk of source) {
yield String(chunk).toUpperCase();
}
});
let res = '';
// Convert AsyncFunction into writable Duplex.
const s3 = compose(async function(source) {
for await (const chunk of source) {
res += chunk;
}
});
await finished(compose(s1, s2, s3));
console.log(res); // prints 'HELLOWORLD'
相關用法
- Node.js stream.Writable.uncork()用法及代碼示例
- Node.js stream.Readable.take(limit[, options])用法及代碼示例
- Node.js stream.Readable.pipe(destination[, options])用法及代碼示例
- Node.js stream.Readable.setEncoding(encoding)用法及代碼示例
- Node.js stream.Readable.some(fn[, options])用法及代碼示例
- Node.js stream.Readable.map(fn[, options])用法及代碼示例
- Node.js stream.Readable.toArray([options])用法及代碼示例
- Node.js stream.Readable.isPaused()用法及代碼示例
- Node.js stream.Readable.forEach(fn[, options])用法及代碼示例
- Node.js stream.Readable.every(fn[, options])用法及代碼示例
- Node.js stream.finished()用法及代碼示例
- Node.js stream.Readable.from()用法及代碼示例
- Node.js stream.Readable.read([size])用法及代碼示例
- Node.js stream.Readable.flatMap(fn[, options])用法及代碼示例
- Node.js stream.finished(stream[, options], callback)用法及代碼示例
- Node.js stream.Readable.unshift(chunk[, encoding])用法及代碼示例
- Node.js stream.Writable.end([chunk[, encoding]][, callback])用法及代碼示例
- Node.js stream.Readable.filter(fn[, options])用法及代碼示例
- Node.js stream.Readable.asIndexedPairs([options])用法及代碼示例
- Node.js stream.Readable.drop(limit[, options])用法及代碼示例
- Node.js stream.Readable.resume()用法及代碼示例
- Node.js stream.Readable.reduce(fn[, initial[, options]])用法及代碼示例
- Node.js stream.addAbortSignal(signal, stream)用法及代碼示例
- Node.js stream.Readable.iterator([options])用法及代碼示例
- Node.js stream.Writable.destroyed用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 stream.compose(...streams)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。