https://github.com/commenthol/streamss-through
A sync/async stream2 transformer
https://github.com/commenthol/streamss-through
stream2 through transform
Last synced: 11 months ago
JSON representation
A sync/async stream2 transformer
- Host: GitHub
- URL: https://github.com/commenthol/streamss-through
- Owner: commenthol
- License: mit
- Created: 2014-12-14T22:02:20.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2021-10-10T05:37:52.000Z (over 4 years ago)
- Last Synced: 2025-02-09T18:17:36.357Z (over 1 year ago)
- Topics: stream2, through, transform
- Language: JavaScript
- Size: 26.4 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# streamss-through
> A sync/async stream2 transformer
[](https://www.npmjs.com/package/streamss-through/)
[](https://travis-ci.org/commenthol/streamss-through)
Works with node v8.x and greater.
`Through` can be used in synchronous mode with:
```js
const { through } = require('streamss-through')
process.stdin
.pipe(through(
function transform (data){
// synchronous mode
},
function flush (){
// synchronous mode
}
))
```
or in asynchronous mode:
```js
const { through } = require('streamss-through')
process.stdin
.pipe(through(
function transform (data, enc, done){
// asynchronous mode
done() // explicit call of done required
},
function flush (done){
// asynchronous mode
done() // explicit call of done required
}
))
```
## Through([options], transform, flush)
**Parameters:**
- `{Object} [options]` - Stream options
- `{Boolean} options.objectMode` - Whether this stream should behave as a stream of objects. Default=false
- `{Number} options.highWaterMark` - The maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource. Default=16kb
- `{String} options.encoding` - Set encoding for string-decoder
- `{Boolean} options.decodeStrings` - Do not decode strings if set to `false`. Default=true
- `{Boolean} options.passError` - Pass error to next pipe. Default=true
- `{Function} transform` - Function called on transform
- `{Function} flush` - Function called on flush
## throughObj([options], transform, flush)
> Shortcut for object mode
**Parameters:**
- `{Object} [options]` - Stream options
- `{Function} transform` - Function called on transform
- `{Function} flush` - Function called on flush
### Example:
```javascript
const { through } = require('streamss-through')
let cnt = 0
require('fs').createReadStream(__filename, { encoding: 'utf8', highWaterMark: 30 })
.pipe(through(
{ decodeStrings: false },
function transform(str) {
cnt += 1
this.push(str.replace(/\s/g, '‧') + '\n')
},
function flush() {
console.log('\ncounted num of chunks: ' + cnt)
}
))
.pipe(process.stdout)
```
Try it with:
```bash
node examples/test.js
```
Check out the [tests](./test/index.mocha.js) for more examples.
## Contribution and License Agreement
If you contribute code to this project, you are implicitly allowing your
code to be distributed under the MIT license. You are also implicitly
verifying that all code is your original work or correctly attributed
with the source of its origin and licence.
## License
Copyright (c) 2014- Commenthol. (MIT License)
See [LICENSE][] for more info.
[LICENSE]: ./LICENSE