|
|
před 3 roky | |
|---|---|---|
| .github | před 3 roky | |
| .vscode | před 7 roky | |
| src | před 3 roky | |
| tests | před 3 roky | |
| .editorconfig | před 7 roky | |
| .gitignore | před 7 roky | |
| .prettierrc | před 3 roky | |
| LICENSE | před 3 roky | |
| README.md | před 3 roky | |
| package-lock.json | před 3 roky | |
| package.json | před 3 roky |
stream-chain creates a chain of streams out of regular functions, asynchronous functions, generator functions, and existing streams, while properly handling backpressure. The resulting chain is represented as a Duplex stream, which can be combined with other streams the usual way. It eliminates a boilerplate helping to concentrate on functionality without losing the performance especially make it easy to build object mode data processing pipelines.
Originally stream-chain was used internally with stream-fork and stream-json to create flexible data processing pipelines.
stream-chain is a lightweight, no-dependencies micro-package. It is distributed under New BSD license.
import chain from 'stream-chain';
// or: const chain = require('stream-chain');
import fs from 'fs';
import zlib from 'zlib';
import {Transform} from 'stream';
// the chain will work on a stream of number objects
const pipeline = new chain([
// transforms a value
x => x * x,
// returns several values
x => chain.many([x - 1, x, x + 1]),
// waits for an asynchronous operation
async x => await getTotalFromDatabaseByKey(x),
// returns multiple values with a generator
function* (x) {
for (let i = x; i > 0; --i) {
yield i;
}
return 0;
},
// filters out even values
x => x % 2 ? x : null,
// uses an arbitrary transform stream
new Transform({
objectMode: true,
transform(x, _, callback) {
callback(null, x + 1);
}
}),
// transform to strings
x => '' + x,
// compress
zlib.createGzip()
]);
// log errors
pipeline.on('error', error => console.log(error));
// use the chain, and save the result to a file
dataSource.pipe(pipeline).pipe(fs.createWriteStream('output.txt.gz'));
Making processing pipelines appears to be easy: just chain functions one after another, and we are done. Real life pipelines filter objects out and/or produce more objects out of a few ones. On top of that we have to deal with asynchronous operations, while processing or producing data: networking, databases, files, user responses, and so on. Unequal number of values per stage, and unequal throughput of stages introduced problems like backpressure, which requires algorithms implemented by streams.
While a lot of API improvements were made to make streams easy to use, in reality, a lot of boilerplate is required when creating a pipeline. stream-chain eliminates most of it.
npm i --save stream-chain
# or: yarn add stream-chain
All documentation can be found in the wiki. It document in details the main function and various utilities and helpers that can simplify stream programming. Additionally it includes a support for JSONL (line-separated JSON files).
An object that is returned by chain() is based on Duplex. It chains its dependents in a single pipeline optionally binding error events.
Many details about this package can be discovered by looking at test files located in tests/ and in the source code (src/).
chain(fns[, options])The factory function accepts the following arguments:
fns is an array of functions, arrays or stream instances.
chunk (an object), and an optional encoding. See Node's documentation for more details on those parameters.undefined or null, no value shall be passed.js
// produces no values:
x => null
x => undefined
// produces one value:
x => x
then()), it will be waited for. Its result should be a regular value.
js
// delays by 0.5s:
x => new Promise(
resolve => setTimeout(() => resolve(x), 500))
next()), it will be iterated according to the generator protocol. The results should be regular values.
js
// produces multiple values:
class Nextable {
constructor(x) {
this.x = x;
this.i = -1;
}
next() {
return {
done: this.i <= 1,
value: this.x + this.i++
};
}
}
x => new Nextable(x)
next() can return a Promise according to the asynchronous generator protocol.js
// fails
x => { throw new Error('Bad!'); }
js
// delays by 0.5s:
async x => {
await new Promise(resolve => setTimeout(() => resolve(), 500));
return x;
}
js
// produces multiple values:
function* (x) {
for (let i = -1; i <= 1; ++i) {
if (i) yield x + i;
}
return x;
}
js
// produces multiple values:
async function* (x) {
for (let i = -1; i <= 1; ++i) {
if (i) {
await new Promise(resolve => setTimeout(() => resolve(), 50));
yield x + i;
}
}
return x;
}
Chain instance ignores all possible writes to the front, and ends when the first stream ends.Chain instance does not produce any output, and finishes when the last stream finishes.'data' event is not used in this case, the instance resumes itself automatically. Read about it in Node's documentation:options is an optional object detailed in the Node's documentation.
js
{writableObjectMode: true, readableObjectMode: true}
If options is specified it is copied over the default options.writableObjectMode is the same as the corresponding object mode of the first stream, and readableObjectMode is the same as the corresponding object mode of the last stream.skipEvents is an optional boolean flag. If it is falsy (the default), 'error' events from all streams are forwarded to the created instance. If it is truthy, no event forwarding is made. A user can always do so externally or in a constructor of derived classes.noGrouping is an optional boolean flag. If it is falsy (the default), all subsequent functions are grouped together using the gen() utility for improved performance. If it is specified and truthy, all functions will be wrapped as streams individually. This mode is compatible with how the 2.x version works.An instance can be used to attach handlers for stream events.
const pipeline = chain([x => x * x, x => [x - 1, x, x + 1]]);
pipeline.on('error', error => console.error(error));
dataSource.pipe(pipeline);
const-ness in the async generator branch (thx Patrick Pang).take, takeWhile, skip, skipWhile, fold, scan, Reduce, comp.'finish' event instead of _final().