2c0c513c3ffe9a174a27eeb6caf2a588097fc6a18c608399af6c3b79be0c0a83f42e48b241416dea4e451e07271d89fa2083356760664d4ce633082629abe4 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # minipass-pipeline
  2. Create a pipeline of streams using Minipass.
  3. Calls `.pipe()` on all the streams in the list. Returns a stream where
  4. writes got to the first pipe in the chain, and reads are from the last.
  5. Errors are proxied along the chain and emitted on the Pipeline stream.
  6. ## USAGE
  7. ```js
  8. const Pipeline = require('minipass-pipeline')
  9. // the list of streams to pipeline together,
  10. // a bit like `input | transform | output` in bash
  11. const p = new Pipeline(input, transform, output)
  12. p.write('foo') // writes to input
  13. p.on('data', chunk => doSomething()) // reads from output stream
  14. // less contrived example (but still pretty contrived)...
  15. const decode = new bunzipDecoder()
  16. const unpack = tar.extract({ cwd: 'target-dir' })
  17. const tbz = new Pipeline(decode, unpack)
  18. fs.createReadStream('archive.tbz').pipe(tbz)
  19. // specify any minipass options if you like, as the first argument
  20. // it'll only try to pipeline event emitters with a .pipe() method
  21. const p = new Pipeline({ objectMode: true }, input, transform, output)
  22. // If you don't know the things to pipe in right away, that's fine.
  23. // use p.push(stream) to add to the end, or p.unshift(stream) to the front
  24. const databaseDecoderStreamDoohickey = (connectionInfo) => {
  25. const p = new Pipeline()
  26. logIntoDatabase(connectionInfo).then(connection => {
  27. initializeDecoderRing(connectionInfo).then(decoderRing => {
  28. p.push(connection, decoderRing)
  29. getUpstreamSource(upstream => {
  30. p.unshift(upstream)
  31. })
  32. })
  33. })
  34. // return to caller right away
  35. // emitted data will be upstream -> connection -> decoderRing pipeline
  36. return p
  37. }
  38. ```
  39. Pipeline is a [minipass](http://npm.im/minipass) stream, so it's as
  40. synchronous as the streams it wraps. It will buffer data until there is a
  41. reader, but no longer, so make sure to attach your listeners before you
  42. pipe it somewhere else.
  43. ## `new Pipeline(opts = {}, ...streams)`
  44. Create a new Pipeline with the specified Minipass options and any streams
  45. provided.
  46. ## `pipeline.push(stream, ...)`
  47. Attach one or more streams to the pipeline at the end (read) side of the
  48. pipe chain.
  49. ## `pipeline.unshift(stream, ...)`
  50. Attach one or more streams to the pipeline at the start (write) side of the
  51. pipe chain.