Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dbhowell/chunky-stream
https://github.com/dbhowell/chunky-stream
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/dbhowell/chunky-stream
- Owner: dbhowell
- License: mit
- Created: 2017-09-19T00:51:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-02-11T01:18:47.000Z (almost 3 years ago)
- Last Synced: 2024-11-14T19:06:41.288Z (about 1 month ago)
- Language: JavaScript
- Size: 74.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# chunky-stream
Buffer up chunks and push them based on certain conditions or if a period of time has passed.
`chunk-stream` is a simple object-mode transform stream that you can pass any number of testable conditions to force a push/flush of the buffered chunks. It also has a interval feature to allow chunks to flush if a certain time has passed and no further chunks have been received.``` js
fs.createReadStream(file)
.pipe(split2())
.pipe(new ChunkyStream({ interval: 1000 }));
````chunky-stream` takes in the following options:
* `interval` - The amount of time (in ms) to wait since receiving data (`0` to disable)
* `ignoreEmpty` - If set to true, do not count an empty string as a chunk (i.e. '', default = false)## API
### ChunkyStream.use(condition)
This is similar to Express middleware, `condition` is a function that will run each time a chunk is received to determine whether the buffered chunks should be released.
`condition` should have 1 argument:
* `chunks` - The array of chunks currently buffered and not yet flushed.
`condition` should return `true` if you want to release the chunks to the consumer of the stream, `false` if you want to continue to buffer.## Examples
### Buffer 10 text lines at a time
``` js
var ChunkyStream = require('chunky-stream');
var split = require('split2');
var stream = fs.createReadStream('./test.txt');
var chunk = new ChunkyStream();chunk.use(function (chunks) {
return (chunks.length === 10);
});stream.pipe(split()).pipe(chunk).on('data', function (err, lines) {
console.log(lines);
});
```