├── .gitignore ├── index.js ├── package.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports.pipe = require('pump') 2 | module.exports.merge = require("multi-duplex-stream") 3 | module.exports.condition = require("ternary-stream") 4 | module.exports.each = require('stream-each') 5 | module.exports.filter = require("through2-filter") 6 | module.exports.map = require("through2-map") 7 | module.exports.reduce = require("through2-reduce") 8 | module.exports.spy = require("through2-spy") 9 | module.exports.split = require("split2") 10 | module.exports.pipeline = require('pumpify') 11 | module.exports.duplex = require('duplexify') 12 | module.exports.through = require('through2') 13 | module.exports.concat = require('concat-stream') 14 | module.exports.finished = require('end-of-stream') 15 | module.exports.unique = require("unique-hash-stream") 16 | module.exports.child = require("duplex-child-process") 17 | 18 | module.exports.stringify = require("streaming-json-stringify") 19 | module.exports.toJSON = require("stream-to-json") 20 | 21 | module.exports.to = require('flush-write-stream') 22 | module.exports.toArray = require("stream-to-array") 23 | module.exports.toString = require("stream-to-string") 24 | module.exports.toPromise = require("stream-to-promise2") 25 | module.exports.toObservable = require("rx-node").fromStream 26 | 27 | module.exports.from = require('from2') 28 | module.exports.fromValue = require('stream-from-value') 29 | module.exports.fromPromise = require("stream-from-promise") 30 | module.exports.fromObservable = require("rx-node").writeToStream 31 | 32 | var Throttle = require("throttle"); 33 | module.exports.throttle = function (n) { 34 | return new Throttle(n); 35 | } 36 | 37 | module.exports.Parser = require('stream-parser') 38 | 39 | module.exports.balance = require("stream-balance") 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mississippi2", 3 | "version": "1.0.5", 4 | "description": "a collection of useful streams", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "max ogden", 10 | "license": "BSD-2-Clause", 11 | "dependencies": { 12 | "concat-stream": "^1.5.0", 13 | "duplex-child-process": "0.0.5", 14 | "duplexify": "^3.4.2", 15 | "end-of-stream": "^1.1.0", 16 | "flush-write-stream": "^1.0.0", 17 | "from2": "^2.1.0", 18 | "multi-duplex-stream": "^0.1.1", 19 | "pump": "^2.0.0", 20 | "pumpify": "^1.3.3", 21 | "rx-node": "^1.0.2", 22 | "split2": "^2.0.1", 23 | "stream-array": "^1.1.1", 24 | "stream-balance": "0.0.6", 25 | "stream-each": "^1.1.0", 26 | "stream-from-array": "^1.0.0", 27 | "stream-from-promise": "^1.0.0", 28 | "stream-from-value": "^0.1.0", 29 | "stream-parser": "^0.3.1", 30 | "stream-to-array": "^2.3.0", 31 | "stream-to-json": "0.0.1", 32 | "stream-to-promise2": "^1.2.0", 33 | "stream-to-string": "^1.0.1", 34 | "streaming-json-stringify": "^3.1.0", 35 | "ternary-stream": "^2.0.0", 36 | "throttle": "^1.0.3", 37 | "through2": "^2.0.0", 38 | "through2-filter": "^2.0.0", 39 | "through2-map": "^3.0.0", 40 | "through2-reduce": "^1.1.1", 41 | "through2-spy": "^2.0.0", 42 | "unique-hash-stream": "^1.0.0" 43 | }, 44 | "devDependencies": {}, 45 | "repository": { 46 | "type": "git", 47 | "url": "git+https://github.com/htoooth/mississippi2.git" 48 | }, 49 | "bugs": { 50 | "url": "https://github.com/htoooth/mississippi2/issues" 51 | }, 52 | "homepage": "https://github.com/htoooth/mississippi2#readme" 53 | } 54 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # mississippi2 2 | 3 | [![NPM](https://nodei.co/npm/mississippi2.png?downloads=true)](https://nodei.co/npm/mississippi2/) 4 | 5 | This module was inspired from [mississippi](https://github.com/maxogden/mississippi). 6 | 7 | ## usage 8 | 9 | ```bash 10 | npm install mississippi2 --save 11 | ``` 12 | 13 | ```js 14 | var miss = require('mississippi2') 15 | ``` 16 | 17 | ## methods 18 | 19 | - [pipe](#pipe) 20 | - [merge](#merge) * 21 | - [condition](#condition) * 22 | - [each](#each) 23 | - [map](#map) * 24 | - [filter](#filter) * 25 | - [reduce](#reduce) * 26 | - [split](#split) * 27 | - [spy](#spy) * 28 | - [pipeline](#pipeline) 29 | - [duplex](#duplex) 30 | - [through](#through) 31 | - [from](#from) 32 | - [fromValue](#fromvalue) * 33 | - [fromPromise](#frompromise) * 34 | - [fromObservable](#fromobservable) * 35 | - [to](#to) 36 | - [toString](#tostring) * 37 | - [toArray](#toarray) * 38 | - [toPromise](#topromise) * 39 | - [toObservable](#toobservable) * 40 | - [concat](#concat) 41 | - [unique](#unique) * 42 | - [toJSON](#tojson) * 43 | - [stringify](#stringify) * 44 | - [child](#child) * 45 | - [finished](#finished) 46 | - [throttle](#throttle) * 47 | - [Parser](#parser) * 48 | 49 | ### pipe 50 | 51 | ##### `miss.pipe(stream1, stream2, stream3, ..., cb)` 52 | 53 | Pipes streams together and destroys all of them if one of them closes. Calls `cb` with `(error)` if there was an error in any of the streams. 54 | 55 | When using standard `source.pipe(destination)` the source will _not_ be destroyed if the destination emits close or error. You are also not able to provide a callback to tell when the pipe has finished. 56 | 57 | `miss.pipe` does these two things for you, ensuring you handle stream errors 100% of the time (unhandled errors are probably the most common bug in most node streams code) 58 | 59 | #### original module 60 | 61 | `miss.pipe` is provided by [`require('pump')`](https://npmjs.org/pump) 62 | 63 | #### example 64 | 65 | ```js 66 | // lets do a simple file copy 67 | var fs = require('fs') 68 | 69 | var read = fs.createReadStream('./original.zip') 70 | var write = fs.createWriteStream('./copy.zip') 71 | 72 | // use miss.pipe instead of read.pipe(write) 73 | miss.pipe(read, write, function (err) { 74 | if (err) return console.error('Copy error!', err) 75 | console.log('Copied successfully') 76 | }) 77 | ``` 78 | 79 | ### merge 80 | 81 | ##### `miss.merge(streams, [options])` 82 | 83 | Return a streams that merged alll streams together and emmit parallely events. When merge readable streams, return a readable stream that reads from multiple readable streams at the same time. If you want to emits multiple other streams one after another, use [merge2](https://github.com/teambition/merge2). When merge writable streams, retrun a writable stream that writes to multiple other writeable streams. 84 | 85 | #### original module 86 | 87 | `miss.merge` is provided by [`require('multi-duplex-stream')`](https://github.com/emilbayes/multi-duplex-stream) 88 | 89 | #### example 90 | 91 | ```js 92 | // merge readable stream 93 | var multiRead = miss.merge([ 94 | miss.fromValue("hello"), 95 | miss.fromValue("world") 96 | ]); 97 | 98 | multiRead.on("data", function(data){ 99 | console.log(data); // "hello" "world" or "world" "hello" 100 | }); 101 | 102 | multiRead.on("end", function(){ 103 | console.log("no more data"); 104 | }); 105 | 106 | // merge writable stream 107 | var read = miss.fromValue('hello, world'); 108 | var write1 = fs.createWriteStream('./file1.txt'); 109 | var write2 = fs.createWriteStream('./file2.txt'); 110 | 111 | var multiWrite = miss.merge([write1,write2]); 112 | 113 | read.pipe(multiWrite).on("end",function(){ 114 | // both file1 and file2 now contains "hello, world" 115 | }); 116 | ``` 117 | 118 | ### condition 119 | 120 | ##### `miss.condition(condition, stream, [elseStream])` 121 | 122 | Condition stream can conditionally control the flow of stream data. 123 | 124 | Condition stream will pipe data to `stream` whenever `condition` is truthy. 125 | 126 | If `condition` is falsey and `elseStream` is passed, data will pipe to `elseStream`. 127 | 128 | After data is piped to `stream` or `elseStream` or neither, data is piped down-stream. 129 | 130 | #### original module 131 | 132 | `miss.condition` is provided by [`require('ternary-stream')`](https://github.com/robrich/ternary-stream) 133 | 134 | #### example 135 | 136 | ```js 137 | // if the condition returns truthy, data is piped to the child stream 138 | var condition = function (data) { 139 | return true; 140 | }; 141 | 142 | process.stdin 143 | .pipe(miss.condition(condition, process.stdout)) 144 | .pipe(fs.createWriteStream('./out.txt')); 145 | 146 | // Data will conditionally go to stdout, and always go to the file 147 | var through2 = require('through2'); 148 | 149 | var count = 0; 150 | var condition = function (data) { 151 | count++; 152 | return count % 2; 153 | }; 154 | 155 | process.stdin 156 | .pipe(miss.condition(condition, fs.createWriteStream('./truthy.txt'), fs.createWriteStream('./falsey.txt'))) 157 | .pipe(process.stdout); 158 | ``` 159 | 160 | ### each 161 | 162 | ##### `miss.each(stream, each, [done])` 163 | 164 | Iterate the data in `stream` one chunk at a time. Your `each` function will be called with `(data, next)` where data is a data chunk and next is a callback. Call `next` when you are ready to consume the next chunk. 165 | 166 | Optionally you can call `next` with an error to destroy the stream. You can also pass the optional third argument, `done`, which is a function that will be called with `(err)` when the stream ends. The `err` argument will be populated with an error if the stream emitted an error. 167 | 168 | #### original module 169 | 170 | `miss.each` is provided by [`require('stream-each')`](https://npmjs.org/stream-each) 171 | 172 | #### example 173 | 174 | ```js 175 | var fs = require('fs') 176 | var split = require('split2') 177 | 178 | var newLineSeparatedNumbers = fs.createReadStream('numbers.txt') 179 | 180 | var pipeline = miss.pipeline(newLineSeparatedNumbers, split()) 181 | var each = miss.each(pipeline, eachLine, done) 182 | var sum = 0 183 | 184 | function eachLine (line, next) { 185 | sum += parseInt(line.toString()) 186 | next() 187 | } 188 | 189 | function done (err) { 190 | if (err) throw err 191 | console.log('sum is', sum) 192 | } 193 | ``` 194 | 195 | ### map 196 | 197 | ##### `miss.map([options,] fn)` 198 | 199 | Return a `stream.Transfrom` instance that will call `fn(chunk, index)` on each stream segment. 200 | 201 | Note you will __NOT__ be able to skip chunks. This is intended for modification only. If you want filter the stream content, use `miss.filter`. This transform also does not have a flush function. 202 | 203 | #### original module 204 | 205 | `miss.map` is provided by [`require('through2-map')`](https://github.com/brycebaril/through2-map) 206 | 207 | #### example 208 | 209 | ```js 210 | var truncate = miss.map(function (chunk) { 211 | return chunk.slice(0, 10) 212 | }); 213 | 214 | // Then use your map: 215 | source.pipe(truncate).pipe(sink) 216 | 217 | ``` 218 | 219 | ### filter 220 | 221 | ##### `miss.filter([options], fn)` 222 | 223 | Create a `through2-filter` instance that will call `fn(chunk)`. If `fn(chunk)` returns "true" the chunk will be passed downstream. Otherwise it will be dropped. 224 | 225 | Note you will __NOT__ be able to alter the content of the chunks. This is intended for filtering only. If you want to modify the stream content, use either `miss.through` or `miss.map`. 226 | 227 | #### original module 228 | 229 | `miss.filter` is provided by [`require('through2-filter')`](https://github.com/brycebaril/through2-filter) 230 | 231 | #### example 232 | 233 | ```js 234 | var skip = miss.filter(function (chunk) { 235 | // skip buffers longer than 100 236 | return chunk.length < 100 237 | }) 238 | 239 | // Then use your filter: 240 | source.pipe(skip).pipe(sink) 241 | ``` 242 | 243 | ### reduce 244 | 245 | ##### `miss.reduce([options,] fn [,initial])` 246 | 247 | Create a Reduce *instance*. Works like `Array.prototype.reduce` meaning you can specify a `fn` function that takes up to *three* arguments: fn(previous, current, index) and you can specify an `initial` value. 248 | 249 | This stream will only ever emit a *single* chunk. For more traditional `stream.Transform` filters or transforms, consider `miss.through` `miss.filter` or `miss.map`. 250 | 251 | #### original module 252 | 253 | `miss.reduce` is provided by [`require('through2-reduce')`](https://github.com/brycebaril/through2-reduce) 254 | 255 | #### example 256 | 257 | ```js 258 | var sum = miss.reduce({objectMode: true}, function (previous, current) { return previous + current }) 259 | 260 | // Then use your reduce: (e.g. source is an objectMode stream of numbers) 261 | source.pipe(sum).pipe(sink) 262 | ``` 263 | 264 | ### split 265 | 266 | ##### `miss.split([matcher, mapper, options])` 267 | 268 | Break up a stream and reassemble it so that each line is a chunk. 269 | 270 | `matcher` may be a `String`, or a `RegExp`. 271 | 272 | #### original module 273 | 274 | `miss.split` is provided by [`require('split2')`](https://github.com/mcollina/split2) 275 | 276 | #### example 277 | 278 | ```js 279 | fs.createReadStream(file) 280 | .pipe(miss.split()) 281 | .on('data', function (line) { 282 | //each chunk now is a seperate line! 283 | }) 284 | ``` 285 | 286 | ### spy 287 | 288 | ##### `miss.spy([options], fn)` 289 | 290 | Create a `miss.spy` instance that will call `fn(chunk)` and then silently pass through data downstream. 291 | 292 | Note you will **NOT** be able to do anything but spy and abort the stream pipeline. To do any filtering or transformations you should consider `miss.through` `miss.filter` or `miss.map`. 293 | 294 | #### original module 295 | 296 | `miss.spy` is provided by [`require('through2-spy')`](https://github.com/brycebaril/through2-spy) 297 | 298 | #### example 299 | 300 | ```js 301 | var count = 0; 302 | var countChunks = miss.spy(function (chunk) { 303 | count++ 304 | }); 305 | 306 | // Then use your spy: 307 | source.pipe(countChunks).pipe(sink); 308 | ``` 309 | 310 | ### pipeline 311 | 312 | ##### `var pipeline = miss.pipeline(stream1, stream2, stream3, ...)` 313 | 314 | Builds a pipeline from all the transform streams passed in as arguments by piping them together and returning a single stream object that lets you write to the first stream and read from the last stream. 315 | 316 | If any of the streams in the pipeline emits an error or gets destroyed, or you destroy the stream it returns, all of the streams will be destroyed and cleaned up for you. 317 | 318 | #### original module 319 | 320 | `miss.pipeline` is provided by [`require('pumpify')`](https://npmjs.org/pumpify) 321 | 322 | #### example 323 | 324 | ```js 325 | // first create some transform streams (note: these two modules are fictional) 326 | var imageResize = require('image-resizer-stream')({width: 400}) 327 | var pngOptimizer = require('png-optimizer-stream')({quality: 60}) 328 | 329 | // instead of doing a.pipe(b), use pipelin 330 | var resizeAndOptimize = miss.pipeline(imageResize, pngOptimizer) 331 | // `resizeAndOptimize` is a transform stream. when you write to it, it writes 332 | // to `imageResize`. when you read from it, it reads from `pngOptimizer`. 333 | // it handles piping all the streams together for you 334 | 335 | // use it like any other transform stream 336 | var fs = require('fs') 337 | 338 | var read = fs.createReadStream('./image.png') 339 | var write = fs.createWriteStream('./resized-and-optimized.png') 340 | 341 | miss.pipe(read, resizeAndOptimize, write, function (err) { 342 | if (err) return console.error('Image processing error!', err) 343 | console.log('Image processed successfully') 344 | }) 345 | ``` 346 | 347 | ### duplex 348 | 349 | ##### `var duplex = miss.duplex([writable, readable, opts])` 350 | 351 | Take two separate streams, a writable and a readable, and turn them into a single [duplex (readable and writable) stream](https://nodejs.org/api/stream.html#stream_class_stream_duplex). 352 | 353 | The returned stream will emit data from the readable. When you write to it it writes to the writable. 354 | 355 | You can either choose to supply the writable and the readable at the time you create the stream, or you can do it later using the `.setWritable` and `.setReadable` methods and data written to the stream in the meantime will be buffered for you. 356 | 357 | #### original module 358 | 359 | `miss.duplex` is provided by [`require('duplexify')`](https://npmjs.org/duplexify) 360 | 361 | #### example 362 | 363 | ```js 364 | // lets spawn a process and take its stdout and stdin and combine them into 1 stream 365 | var child = require('child_process') 366 | 367 | // @- tells it to read from stdin, --data-binary sets 'raw' binary mode 368 | var curl = child.spawn('curl -X POST --data-binary @- http://foo.com') 369 | 370 | // duplexCurl will write to stdin and read from stdout 371 | var duplexCurl = miss.duplex(curl.stdin, curl.stdout) 372 | ``` 373 | 374 | ### through 375 | 376 | #####`var transformer = miss.through([options, transformFunction, flushFunction])` 377 | 378 | Make a custom [transform stream](https://nodejs.org/docs/latest/api/stream.html#stream_class_stream_transform). 379 | 380 | The `options` object is passed to the internal transform stream and can be used to create an `objectMode` stream (or use the shortcut `miss.through.obj([...])`) 381 | 382 | The `transformFunction` is called when data is available for the writable side and has the signature `(chunk, encoding, cb)`. Within the function, add data to the readable side any number of times with `this.push(data)`. Call `cb()` to indicate processing of the `chunk` is complete. Or to easily emit a single error or chunk, call `cb(err, chunk)` 383 | 384 | The `flushFunction`, with signature `(cb)`, is called just before the stream is complete and should be used to wrap up stream processing. 385 | 386 | #### original module 387 | 388 | `miss.through` is provided by [`require('through2')`](https://npmjs.org/through2) 389 | 390 | #### example 391 | 392 | ```js 393 | var fs = require('fs') 394 | 395 | var read = fs.createReadStream('./boring_lowercase.txt') 396 | var write = fs.createWriteStream('./AWESOMECASE.TXT') 397 | 398 | // Leaving out the options object 399 | var uppercaser = miss.through( 400 | function (chunk, enc, cb) { 401 | cb(null, chunk.toString().toUpperCase()) 402 | }, 403 | function (cb) { 404 | cb(null, 'ONE LAST BIT OF UPPERCASE') 405 | } 406 | ) 407 | 408 | miss.pipe(read, uppercaser, write, function (err) { 409 | if (err) return console.error('Trouble uppercasing!') 410 | console.log('Splendid uppercasing!') 411 | }) 412 | ``` 413 | 414 | ### from 415 | 416 | #####`miss.from([opts], read)` 417 | 418 | Make a custom [readable stream](https://nodejs.org/docs/latest/api/stream.html#stream_class_stream_readable). 419 | 420 | `opts` contains the options to pass on to the ReadableStream constructor e.g. for creating a readable object stream (or use the shortcut `miss.from.obj([...])`). 421 | 422 | Returns a readable stream that calls `read(size, next)` when data is requested from the stream. 423 | 424 | - `size` is the recommended amount of data (in bytes) to retrieve. 425 | - `next(err, chunk)` should be called when you're ready to emit more data. 426 | 427 | #### original module 428 | 429 | `miss.from` is provided by [`require('from2')`](https://npmjs.org/from2) 430 | 431 | #### example 432 | 433 | ```js 434 | function fromString(string) { 435 | return miss.from(function(size, next) { 436 | // if there's no more content 437 | // left in the string, close the stream. 438 | if (string.length <= 0) return next(null, null) 439 | 440 | // Pull in a new chunk of text, 441 | // removing it from the string. 442 | var chunk = string.slice(0, size) 443 | string = string.slice(size) 444 | 445 | // Emit "chunk" from the stream. 446 | next(null, chunk) 447 | }) 448 | } 449 | 450 | // pipe "hello world" out 451 | // to stdout. 452 | fromString('hello world').pipe(process.stdout) 453 | ``` 454 | 455 | ### fromValue 456 | 457 | #####`miss.fromValue(value)` 458 | 459 | Create streams from (single) arbitrary Javascript values like `strings`, `functions`, `arrays`, etc. 460 | 461 | Please __Note__ miss.fromValue are [Readable](http://nodejs.org/api/stream.html#stream_class_stream_readable_1) streams. 462 | 463 | #### original module 464 | 465 | `miss.fromValue` is provided by [`require('stream-from-value')`](https://github.com/schnittstabil/stream-from-value) 466 | 467 | #### example 468 | 469 | ```js 470 | miss.fromValue('some string') 471 | .pipe(process.stdout); // output: some string 472 | 473 | miss.fromValue(new Buffer('some string')) 474 | .pipe(process.stdout); // output: some string 475 | 476 | // Stream of (arbitrary) Javascript Value 477 | miss.fromValue.obj(['some', 'mixed', 'array', 42]).on('data', function(data){ 478 | console.log(data); // output: [ 'some', 'mixed', 'array', 42 ] 479 | }); 480 | ``` 481 | 482 | ### fromPromise 483 | 484 | #####`miss.fromPromise(promise)` 485 | 486 | Make a [Readable](http://nodejs.org/api/stream.html#stream_class_stream_readable_1) streams from `ECMAScript 2015 Promises` that return Javascript values like numbers, strings, objects, functions. 487 | 488 | #### original module 489 | 490 | `miss.fromPromise` is provided by [`require('stream-from-promise')`](https://github.com/schnittstabil/stream-from-promise) 491 | 492 | #### example 493 | 494 | ```js 495 | // `String promises 496 | var stringPromise = new Promise(function(resolve, reject){ 497 | setTimeout(function(){ 498 | resolve('strrrring!'); 499 | }, 500); 500 | }); 501 | 502 | miss.fromPromise(stringPromise).pipe(process.stdout); // => output: strrrring! 503 | 504 | // Buffer promises 505 | var bufferPromise = new Promise(function(resolve, reject){ 506 | setTimeout(function(){ 507 | resolve(new Buffer('buff!')); 508 | }, 500); 509 | }); 510 | 511 | StreamFromPromise(bufferPromise) 512 | .pipe(process.stdout); // output: buff! 513 | 514 | ``` 515 | 516 | ### fromObservable 517 | 518 | #####`miss.fromObservable(observable, stream, [encoding])` 519 | 520 | Writes an observable sequence to a writable stream. 521 | 522 | __Arguments__ 523 | 524 | - `observable` *(Observable)*: Observable sequence to write to a stream. 525 | - `stream` *(Stream)*: The stream to write to. 526 | - `[encoding]` *(String)*: The encoding of the item to write. 527 | 528 | __Returns__ 529 | - *(Disposable)*: The subscription handle. 530 | 531 | #### original module 532 | 533 | `miss.fromObsesrvable` is provided by [`require("rx-node").writeToStream`](https://github.com/Reactive-Extensions/rx-node) 534 | 535 | #### example 536 | 537 | ```js 538 | var Rx = require('rx'); 539 | var source = Rx.Observable.range(0, 5); 540 | 541 | var subscription = miss.fromObsesrvable(source, process.stdout, 'utf8'); 542 | // => 01234 543 | ``` 544 | 545 | ### to 546 | 547 | #####`miss.to([options], write, [flush])` 548 | 549 | Make a custom [writable stream](https://nodejs.org/docs/latest/api/stream.html#stream_class_stream_writable). 550 | 551 | `opts` contains the options to pass on to the WritableStream constructor e.g. for creating a readable object stream (or use the shortcut `miss.to.obj([...])`). 552 | 553 | Returns a writable stream that calls `write(data, enc, cb)` when data is written to the stream. 554 | 555 | - `data` is the received data to write the destination. 556 | - `enc` encoding of the piece of data received. 557 | - `next(err, chunk)` should be called when you're ready to write more data, or encountered an error. 558 | 559 | `flush(cb)` is called before `finish` is emitted and allows for cleanup steps to occur. 560 | 561 | #### original module 562 | 563 | `miss.to` is provided by [`require('flush-write-stream')`](https://npmjs.org/flush-write-stream) 564 | 565 | #### example 566 | 567 | ```js 568 | var ws = miss.to(write, flush) 569 | 570 | ws.on('finish', function () { 571 | console.log('finished') 572 | }) 573 | 574 | ws.write('hello') 575 | ws.write('world') 576 | ws.end() 577 | 578 | function write (data, enc, cb) { 579 | // i am your normal ._write method 580 | console.log('writing', data.toString()) 581 | cb() 582 | } 583 | 584 | function flush (cb) { 585 | // i am called before finish is emitted 586 | setTimeout(cb, 1000) // wait 1 sec 587 | } 588 | ``` 589 | 590 | If you run the above it will produce the following output 591 | 592 | ``` 593 | writing hello 594 | writing world 595 | (nothing happens for 1 sec) 596 | finished 597 | ``` 598 | 599 | ### toString 600 | 601 | #####`miss.toString(stream [, callback])` 602 | 603 | Pipe a stream into a string, collect value with callback or promise. 604 | 605 | Collects stream data into a string. Executes optional `callback(err, string)`. Returns a promise. 606 | 607 | #### original module 608 | 609 | `miss.toString` is provided by [`require('stream-to-string')`](https://github.com/jasonpincin/stream-to-string) 610 | 611 | #### example 612 | 613 | ```js 614 | // with callback 615 | var stream = miss.through(); 616 | 617 | miss.toString(stream, function (err, msg) { 618 | console.log(msg) 619 | }) 620 | 621 | // or with promises 622 | miss.toString(stream).then(function (msg) { 623 | console.log(msg) 624 | }) 625 | 626 | stream.write('this is a') 627 | stream.write(' test') 628 | stream.end() 629 | ``` 630 | 631 | ### toArray 632 | 633 | #####`miss.toArray([stream], [callback(err, arr)])` 634 | 635 | Concatenate a readable stream's data into a single array. 636 | 637 | Returns all the data objects in an array. This is useful for streams in object mode if you want to just use an array. 638 | 639 | #### original module 640 | 641 | `miss.toArray` is provided by [`require('stream-to-array')`](https://github.com/stream-utils/stream-to-array) 642 | 643 | #### example 644 | 645 | ```js 646 | // with callback 647 | var stream = miss.through(); 648 | 649 | miss.toArray(stream, function (err, msg) { 650 | console.log(msg) 651 | }) 652 | 653 | // or with promises 654 | miss.toArray(stream).then(function (msg) { 655 | console.log(msg) 656 | }) 657 | 658 | stream.write('this is a') 659 | stream.write(' test') 660 | stream.end() 661 | ``` 662 | 663 | ### toPromise 664 | 665 | #####`miss.toPromise(stream)` 666 | 667 | Convert streams (readable or writable) to promises. 668 | 669 | #### original module 670 | 671 | `miss.toPromise` is provided by [`require('stream-to-promise2')`](https://github.com/htoooth/stream-to-promise2) 672 | 673 | #### example 674 | 675 | ```js 676 | miss.toPromise(readableStream).then(function (buffer) { 677 | // buffer.length === 3 678 | }); 679 | readableStream.emit('data', new Buffer()); 680 | readableStream.emit('data', new Buffer()); 681 | readableStream.emit('data', new Buffer()); 682 | readableStream.emit('end'); // promise is resolved here 683 | ``` 684 | 685 | ### toObservable 686 | 687 | #####`miss.toObservable(stream, finishEventName, dataEventName)` 688 | 689 | Converts a flowing readable to an Observable sequence. 690 | 691 | __Arguments__ 692 | 693 | - `stream` *(Stream)*: A stream to convert to a observable sequence. 694 | - `[dataEventName]` *(String)*: Event that notifies about incoming data. ("data" by default) 695 | 696 | __Returns__ 697 | 698 | - *(Observable)*: An observable sequence which fires on each 'data' event as well as handling 'error' and 'end' events. 699 | 700 | #### original module 701 | 702 | `miss.toObservable` is provided by [`require("rx-node").writeToStream`](https://github.com/Reactive-Extensions/rx-node) 703 | 704 | #### example 705 | 706 | ```js 707 | var subscription = miss.toObservable(process.stdin) 708 | .subscribe(function (x) { console.log(x); }); 709 | 710 | // => r 711 | // => x 712 | ``` 713 | 714 | ### concat 715 | 716 | #####`var concat = miss.concat(cb)` 717 | 718 | Returns a writable stream that concatenates all data written to the stream and calls a callback with the single result. 719 | 720 | Calling `miss.concat(cb)` returns a writable stream. `cb` is called when the writable stream is finished, e.g. when all data is done being written to it. `cb` is called with a single argument, `(data)`, which will containe the result of concatenating all the data written to the stream. 721 | 722 | Note that `miss.concat` will not handle stream errors for you. To handle errors, use `miss.pipe` or handle the `error` event manually. 723 | 724 | #### original module 725 | 726 | `miss.concat` is provided by [`require('concat-stream')`](https://npmjs.org/concat-stream) 727 | 728 | #### example 729 | 730 | ```js 731 | var fs = require('fs') 732 | var concat = require('concat-stream') 733 | 734 | var readStream = fs.createReadStream('cat.png') 735 | var concatStream = concat(gotPicture) 736 | 737 | readStream.on('error', handleError) 738 | readStream.pipe(concatStream) 739 | 740 | function gotPicture(imageBuffer) { 741 | // imageBuffer is all of `cat.png` as a node.js Buffer 742 | } 743 | 744 | function handleError(err) { 745 | // handle your error appropriately here, e.g.: 746 | console.error(err) // print the error to STDERR 747 | process.exit(1) // exit program with non-zero exit code 748 | } 749 | ``` 750 | 751 | ### unique 752 | 753 | #####`miss.unique([fn])` 754 | 755 | Filter duplicates from a stream based on a hashing `fn(chunk)`. By default, this hashing function is: 756 | 757 | ```js 758 | sha256sum(JSON.stringify(doc)) 759 | ``` 760 | 761 | #### original module 762 | 763 | `miss.unique` is provided by [`require('unique-hash-stream')`](https://github.com/stream-utils/unique-hash-stream) 764 | 765 | #### example 766 | 767 | ```js 768 | var stream = miss.through(); 769 | 770 | var hashFn = function(chunk){ 771 | return chunk; 772 | }; 773 | 774 | stream.pipe(miss.unique(hashFn)).pipe(process.stdout); 775 | 776 | stream.write("a"); 777 | stream.write("a"); 778 | stream.write("b"); 779 | stream.end(); 780 | 781 | ``` 782 | 783 | ### toJSON 784 | 785 | #####`miss.toJSON(stream, callback)` 786 | 787 | Read all from `stream`, then `JSON.parse` and call `callback(err, json)` with the result. If there's an Error in the stream itself, or parsing the JSON, an error will be passed. 788 | 789 | #### original module 790 | 791 | `miss.toJSON` is provided by [`require('stream-to-json')`](https://www.npmjs.com/package/stream-to-json) 792 | 793 | #### example 794 | 795 | ```js 796 | var request = require('request'); 797 | 798 | miss.toJSON(request('/some/url.json'), function(err, json) { 799 | if (err) throw err; 800 | console.log(json); 801 | }); 802 | ``` 803 | 804 | 805 | ### stringify 806 | 807 | #####`miss.stringify([options])` 808 | 809 | Similar to JSONStream.stringify() except it is, by default, a binary stream, and it is a streams2 implementation. 810 | 811 | Please __NOTE__ : The main use case for this is to stream a database query to a web client. This is meant to be used only with `arrays`, not `objects`. 812 | 813 | __Separators__ 814 | 815 | - The stream always starts with '[\n'. 816 | - Documents are separated by '\n,\n'. 817 | - The stream is terminated with '\n]\n'. 818 | 819 | #### original module 820 | 821 | `miss.stringify` is provided by [`require('streaming-json-stringify')`](https://github.com/stream-utils/streaming-json-stringify) 822 | 823 | #### example 824 | 825 | ```js 826 | app.get('/things', function (req, res, next) { 827 | res.setHeader('Content-Type', 'application/json; charset=utf-8') 828 | 829 | db.things.find() 830 | .stream() 831 | .pipe(miss.stringify()) 832 | .pipe(res) 833 | }) 834 | ``` 835 | 836 | will yield something like 837 | 838 | ``` 839 | [ 840 | {"_id":"123412341234123412341234"} 841 | , 842 | {"_id":"123412341234123412341234"} 843 | ] 844 | ``` 845 | 846 | 847 | ### child 848 | 849 | #####`miss.child(command, [args], [options])` 850 | 851 | Spawn a child process as a duplex stream. 852 | 853 | Convenience wrapper for: 854 | 855 | ```js 856 | new Child_Process().spawn(command, [args], [options]) 857 | ``` 858 | 859 | #### original module 860 | 861 | `miss.child` is provided by [`require('duplex-child-process')`](https://github.com/stream-utils/duplex-child-process) 862 | 863 | #### example 864 | 865 | ```js 866 | var toJPEG = miss.child.spawn('convert', ['-', 'JPEG:-']) 867 | var getFormat = miss.child.spawn('identify', ['-format', '%m', '-']) 868 | 869 | fs.createReadStream('img.png') 870 | .pipe(toJPEG) 871 | .pipe(getFormat) 872 | .once('readable', function () { 873 | var format = this.read().toString('utf8') 874 | assert.equal(format, 'JPEG') 875 | }) 876 | ``` 877 | 878 | ### finished 879 | 880 | #####`miss.finished(stream, cb)` 881 | 882 | Waits for `stream` to finish or error and then calls `cb` with `(err)`. `cb` will only be called once. `err` will be null if the stream finished without error, or else it will be populated with the error from the streams `error` event. 883 | 884 | This function is useful for simplifying stream handling code as it lets you handle success or error conditions in a single code path. It's used internally `miss.pipe`. 885 | 886 | #### original module 887 | 888 | `miss.finished` is provided by [`require('end-of-stream')`](https://npmjs.org/end-of-stream) 889 | 890 | #### example 891 | 892 | ```js 893 | var copySource = fs.createReadStream('./movie.mp4') 894 | var copyDest = fs.createWriteStream('./movie-copy.mp4') 895 | 896 | copySource.pipe(copyDest) 897 | 898 | miss.finished(copyDest, function(err) { 899 | if (err) return console.log('write failed', err) 900 | console.log('write success') 901 | }) 902 | ``` 903 | 904 | ### throttle 905 | 906 | #####`miss.throttle(n)` 907 | 908 | This stream offers a `Throttle` passthrough stream class, which allows you to write data to it and it will be passed through in `n` bytes per second. It can be useful for throttling HTTP uploads or to simulate reading from a file in real-time, etc. 909 | 910 | #### original module 911 | 912 | `miss.throttle` is provided by [`require("throttle")`](https://github.com/TooTallNate/node-throttle) 913 | 914 | #### example 915 | 916 | ```js 917 | // throttling stdin at 1 byte per second and outputting the data to stdout: 918 | process.stdin.pipe(miss.throttle(1)).pipe(process.stdout); 919 | ``` 920 | 921 | ### Parser 922 | 923 | #####`miss.Parser` 924 | 925 | This tool offers the `stream-parser` mixin, which provides an easy-to-use API for parsing bytes from `Writable` and/or `Transform` stream instances. This module is great for implementing streaming parsers for standardized file formats. 926 | 927 | For `Writable` streams, the parser takes control over the `_write` callback function. For Transform streams, the parser controls the `_transform` callback function. 928 | 929 | ##### api 930 | 931 | The `Parser` stream mixin works with either `Writable` or `Transform` stream 932 | instances/subclasses. Provides a convenient generic "parsing" API: 933 | 934 | ```js 935 | _bytes(n, cb) - buffers "n" bytes and then calls "cb" with the "chunk" 936 | _skipBytes(n, cb) - skips "n" bytes and then calls "cb" when done 937 | ``` 938 | 939 | If you extend a `Transform` stream, then the `_passthrough()` function is also 940 | added: 941 | 942 | ```js 943 | _passthrough(n, cb) - passes through "n" bytes untouched and then calls "cb" 944 | ``` 945 | 946 | ###### ._bytes(n, cb) 947 | 948 | Buffers `n` bytes and then invokes `cb` once that amount has been collected. 949 | 950 | ###### ._skipBytes(n, cb) 951 | 952 | Skips over the next `n` bytes and then invokes `cb` once that amount has been 953 | discarded. 954 | 955 | ###### ._passthrough(n, cb) 956 | 957 | Passes through `n` bytes to the readable side of this stream untouched, 958 | then invokes `cb` once that amount has been passed through. This function is only defined 959 | when stream-parser is extending a `Transform` stream. 960 | 961 | ##### original module 962 | 963 | `miss.Parser` is provided by [`require('stream-parser')`](https://github.com/TooTallNate/node-stream-parser) 964 | 965 | #### example 966 | 967 | Let's create a quick `Transform` stream subclass that utilizes the parser's 968 | `_bytes()` and `_passthrough()` functions to parse a theoretical file format that 969 | has an 8-byte header we want to parse, and then pass through the rest of the data. 970 | 971 | ```js 972 | var inherits = require('util').inherits; 973 | var Transform = require('stream').Transform; 974 | 975 | // create a Transform stream subclass 976 | function MyParser () { 977 | Transform.call(this); 978 | 979 | // buffer the first 8 bytes written 980 | this._bytes(8, this.onheader); 981 | } 982 | inherits(MyParser, Transform); 983 | 984 | // mixin stream-parser into MyParser's `prototype` 985 | miss.Parser(MyParser.prototype); 986 | 987 | // invoked when the first 8 bytes have been received 988 | MyParser.prototype.onheader = function (buffer, output) { 989 | // parse the "buffer" into a useful "header" object 990 | var header = {}; 991 | header.type = buffer.readUInt32LE(0); 992 | header.name = buffer.toString('utf8', 4); 993 | this.emit('header', header); 994 | 995 | // it's usually a good idea to queue the next "piece" within the callback 996 | this._passthrough(Infinity); 997 | }; 998 | 999 | 1000 | // now we can *use* it! 1001 | var parser = new MyParser(); 1002 | parser.on('header', function (header) { 1003 | console.error('got "header"', header); 1004 | }); 1005 | process.stdin.pipe(parser).pipe(process.stdout); 1006 | ``` --------------------------------------------------------------------------------