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