You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

411 lines
13 KiB

4 years ago
  1. # mississippi
  2. a collection of useful stream utility modules. learn how the modules work using this and then pick the ones you want and use them individually
  3. the goal of the modules included in mississippi is to make working with streams easy without sacrificing speed, error handling or composability.
  4. ## usage
  5. ```js
  6. var miss = require('mississippi')
  7. ```
  8. ## methods
  9. - [pipe](#pipe)
  10. - [each](#each)
  11. - [pipeline](#pipeline)
  12. - [duplex](#duplex)
  13. - [through](#through)
  14. - [from](#from)
  15. - [to](#to)
  16. - [concat](#concat)
  17. - [finished](#finished)
  18. - [parallel](#parallel)
  19. ### pipe
  20. ##### `miss.pipe(stream1, stream2, stream3, ..., cb)`
  21. 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.
  22. 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.
  23. `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)
  24. #### original module
  25. `miss.pipe` is provided by [`require('pump')`](https://www.npmjs.com/package/pump)
  26. #### example
  27. ```js
  28. // lets do a simple file copy
  29. var fs = require('fs')
  30. var read = fs.createReadStream('./original.zip')
  31. var write = fs.createWriteStream('./copy.zip')
  32. // use miss.pipe instead of read.pipe(write)
  33. miss.pipe(read, write, function (err) {
  34. if (err) return console.error('Copy error!', err)
  35. console.log('Copied successfully')
  36. })
  37. ```
  38. ### each
  39. ##### `miss.each(stream, each, [done])`
  40. 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.
  41. 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.
  42. #### original module
  43. `miss.each` is provided by [`require('stream-each')`](https://www.npmjs.com/package/stream-each)
  44. #### example
  45. ```js
  46. var fs = require('fs')
  47. var split = require('split2')
  48. var newLineSeparatedNumbers = fs.createReadStream('numbers.txt')
  49. var pipeline = miss.pipeline(newLineSeparatedNumbers, split())
  50. miss.each(pipeline, eachLine, done)
  51. var sum = 0
  52. function eachLine (line, next) {
  53. sum += parseInt(line.toString())
  54. next()
  55. }
  56. function done (err) {
  57. if (err) throw err
  58. console.log('sum is', sum)
  59. }
  60. ```
  61. ### pipeline
  62. ##### `var pipeline = miss.pipeline(stream1, stream2, stream3, ...)`
  63. 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.
  64. If you are pumping object streams together use `pipeline = miss.pipeline.obj(s1, s2, ...)`.
  65. 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.
  66. #### original module
  67. `miss.pipeline` is provided by [`require('pumpify')`](https://www.npmjs.com/package/pumpify)
  68. #### example
  69. ```js
  70. // first create some transform streams (note: these two modules are fictional)
  71. var imageResize = require('image-resizer-stream')({width: 400})
  72. var pngOptimizer = require('png-optimizer-stream')({quality: 60})
  73. // instead of doing a.pipe(b), use pipelin
  74. var resizeAndOptimize = miss.pipeline(imageResize, pngOptimizer)
  75. // `resizeAndOptimize` is a transform stream. when you write to it, it writes
  76. // to `imageResize`. when you read from it, it reads from `pngOptimizer`.
  77. // it handles piping all the streams together for you
  78. // use it like any other transform stream
  79. var fs = require('fs')
  80. var read = fs.createReadStream('./image.png')
  81. var write = fs.createWriteStream('./resized-and-optimized.png')
  82. miss.pipe(read, resizeAndOptimize, write, function (err) {
  83. if (err) return console.error('Image processing error!', err)
  84. console.log('Image processed successfully')
  85. })
  86. ```
  87. ### duplex
  88. ##### `var duplex = miss.duplex([writable, readable, opts])`
  89. 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).
  90. The returned stream will emit data from the readable. When you write to it it writes to the writable.
  91. 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.
  92. #### original module
  93. `miss.duplex` is provided by [`require('duplexify')`](https://www.npmjs.com/package/duplexify)
  94. #### example
  95. ```js
  96. // lets spawn a process and take its stdout and stdin and combine them into 1 stream
  97. var child = require('child_process')
  98. // @- tells it to read from stdin, --data-binary sets 'raw' binary mode
  99. var curl = child.spawn('curl -X POST --data-binary @- http://foo.com')
  100. // duplexCurl will write to stdin and read from stdout
  101. var duplexCurl = miss.duplex(curl.stdin, curl.stdout)
  102. ```
  103. ### through
  104. ##### `var transformer = miss.through([options, transformFunction, flushFunction])`
  105. Make a custom [transform stream](https://nodejs.org/docs/latest/api/stream.html#stream_class_stream_transform).
  106. 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([...])`)
  107. 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)`
  108. The `flushFunction`, with signature `(cb)`, is called just before the stream is complete and should be used to wrap up stream processing.
  109. #### original module
  110. `miss.through` is provided by [`require('through2')`](https://www.npmjs.com/package/through2)
  111. #### example
  112. ```js
  113. var fs = require('fs')
  114. var read = fs.createReadStream('./boring_lowercase.txt')
  115. var write = fs.createWriteStream('./AWESOMECASE.TXT')
  116. // Leaving out the options object
  117. var uppercaser = miss.through(
  118. function (chunk, enc, cb) {
  119. cb(null, chunk.toString().toUpperCase())
  120. },
  121. function (cb) {
  122. cb(null, 'ONE LAST BIT OF UPPERCASE')
  123. }
  124. )
  125. miss.pipe(read, uppercaser, write, function (err) {
  126. if (err) return console.error('Trouble uppercasing!')
  127. console.log('Splendid uppercasing!')
  128. })
  129. ```
  130. ### from
  131. ##### `miss.from([opts], read)`
  132. Make a custom [readable stream](https://nodejs.org/docs/latest/api/stream.html#stream_class_stream_readable).
  133. `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([...])`).
  134. Returns a readable stream that calls `read(size, next)` when data is requested from the stream.
  135. - `size` is the recommended amount of data (in bytes) to retrieve.
  136. - `next(err, chunk)` should be called when you're ready to emit more data.
  137. #### original module
  138. `miss.from` is provided by [`require('from2')`](https://www.npmjs.com/package/from2)
  139. #### example
  140. ```js
  141. function fromString(string) {
  142. return miss.from(function(size, next) {
  143. // if there's no more content
  144. // left in the string, close the stream.
  145. if (string.length <= 0) return next(null, null)
  146. // Pull in a new chunk of text,
  147. // removing it from the string.
  148. var chunk = string.slice(0, size)
  149. string = string.slice(size)
  150. // Emit "chunk" from the stream.
  151. next(null, chunk)
  152. })
  153. }
  154. // pipe "hello world" out
  155. // to stdout.
  156. fromString('hello world').pipe(process.stdout)
  157. ```
  158. ### to
  159. ##### `miss.to([options], write, [flush])`
  160. Make a custom [writable stream](https://nodejs.org/docs/latest/api/stream.html#stream_class_stream_writable).
  161. `opts` contains the options to pass on to the WritableStream constructor e.g. for creating a writable object stream (or use the shortcut `miss.to.obj([...])`).
  162. Returns a writable stream that calls `write(data, enc, cb)` when data is written to the stream.
  163. - `data` is the received data to write the destination.
  164. - `enc` encoding of the piece of data received.
  165. - `cb(err, data)` should be called when you're ready to write more data, or encountered an error.
  166. `flush(cb)` is called before `finish` is emitted and allows for cleanup steps to occur.
  167. #### original module
  168. `miss.to` is provided by [`require('flush-write-stream')`](https://www.npmjs.com/package/flush-write-stream)
  169. #### example
  170. ```js
  171. var ws = miss.to(write, flush)
  172. ws.on('finish', function () {
  173. console.log('finished')
  174. })
  175. ws.write('hello')
  176. ws.write('world')
  177. ws.end()
  178. function write (data, enc, cb) {
  179. // i am your normal ._write method
  180. console.log('writing', data.toString())
  181. cb()
  182. }
  183. function flush (cb) {
  184. // i am called before finish is emitted
  185. setTimeout(cb, 1000) // wait 1 sec
  186. }
  187. ```
  188. If you run the above it will produce the following output
  189. ```
  190. writing hello
  191. writing world
  192. (nothing happens for 1 sec)
  193. finished
  194. ```
  195. ### concat
  196. ##### `var concat = miss.concat(cb)`
  197. Returns a writable stream that concatenates all data written to the stream and calls a callback with the single result.
  198. 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 contain the result of concatenating all the data written to the stream.
  199. Note that `miss.concat` will not handle stream errors for you. To handle errors, use `miss.pipe` or handle the `error` event manually.
  200. #### original module
  201. `miss.concat` is provided by [`require('concat-stream')`](https://www.npmjs.com/package/concat-stream)
  202. #### example
  203. ```js
  204. var fs = require('fs')
  205. var readStream = fs.createReadStream('cat.png')
  206. var concatStream = miss.concat(gotPicture)
  207. function callback (err) {
  208. if (err) {
  209. console.error(err)
  210. process.exit(1)
  211. }
  212. }
  213. miss.pipe(readStream, concatStream, callback)
  214. function gotPicture(imageBuffer) {
  215. // imageBuffer is all of `cat.png` as a node.js Buffer
  216. }
  217. function handleError(err) {
  218. // handle your error appropriately here, e.g.:
  219. console.error(err) // print the error to STDERR
  220. process.exit(1) // exit program with non-zero exit code
  221. }
  222. ```
  223. ### finished
  224. ##### `miss.finished(stream, cb)`
  225. 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.
  226. 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`.
  227. #### original module
  228. `miss.finished` is provided by [`require('end-of-stream')`](https://www.npmjs.com/package/end-of-stream)
  229. #### example
  230. ```js
  231. var copySource = fs.createReadStream('./movie.mp4')
  232. var copyDest = fs.createWriteStream('./movie-copy.mp4')
  233. copySource.pipe(copyDest)
  234. miss.finished(copyDest, function(err) {
  235. if (err) return console.log('write failed', err)
  236. console.log('write success')
  237. })
  238. ```
  239. ### parallel
  240. ##### `miss.parallel(concurrency, each)`
  241. This works like `through` except you can process items in parallel, while still preserving the original input order.
  242. This is handy if you wanna take advantage of node's async I/O and process streams of items in batches. With this module you can build your very own streaming parallel job queue.
  243. Note that `miss.parallel` preserves input ordering, if you don't need that then you can use [through2-concurrent](https://github.com/almost/through2-concurrent) instead, which is very similar to this otherwise.
  244. #### original module
  245. `miss.parallel` is provided by [`require('parallel-transform')`](https://npmjs.org/parallel-transform)
  246. #### example
  247. This example fetches the GET HTTP headers for a stream of input URLs 5 at a time in parallel.
  248. ```js
  249. function getResponse (item, cb) {
  250. var r = request(item.url)
  251. r.on('error', function (err) {
  252. cb(err)
  253. })
  254. r.on('response', function (re) {
  255. cb(null, {url: item.url, date: new Date(), status: re.statusCode, headers: re.headers})
  256. r.abort()
  257. })
  258. }
  259. miss.pipe(
  260. fs.createReadStream('./urls.txt'), // one url per line
  261. split(),
  262. miss.parallel(5, getResponse),
  263. miss.through(function (row, enc, next) {
  264. console.log(JSON.stringify(row))
  265. next()
  266. })
  267. )
  268. ```
  269. ## see also
  270. - [substack/stream-handbook](https://github.com/substack/stream-handbook)
  271. - [nodejs.org/api/stream.html](https://nodejs.org/api/stream.html)
  272. - [awesome-nodejs-streams](https://github.com/thejmazz/awesome-nodejs-streams)
  273. ## license
  274. Licensed under the BSD 2-clause license.