The Pipage
pip•age
- n. plumbing, a system of pipes
- n. node module, a splice-able stream pipeline
npm
Install via$ npm install --save pipage
Usage
For detailed API documentation, see doc/
var Pipage =
The Pipage is a duplex stream,
inheriting from Node's stream.Duplex
and behaves in the same way, while containing an
internal pipeline which can be added to, removed from and spliced at runtime:
readable -> pipage[ transform, transform, ... ] -> writable
Creating a pipeline
// A blank pass-through pipeline:var pipeline = // Initialized with already existing streams:var pipeline = transform1 transform2 ...
Adding streams
// Streams can be appended,pipelinepipeline // prepended,pipeline // inserted at a specific index,pipeline // which is the same as splicing in streams (add N streams at index 3):pipeline
Removing streams
// Streams can be shifted off the beginning,var firstStream = pipeline // or popped off the end,var lastStream = pipeline // spliced out at a specific index (remove 3 from index 2),var removedStreams = pipeline // or removed by reference:pipeline
Selecting streams
// Get a stream at a specific index in the pipeline:var stream = pipelinevar lastStream = pipeline // Find a stream in the pipeline:var index = pipelinevar lastIndex = pipeline
Example
var Pipage = var path = var zlib = var unbzip2 = var xz = var pipeline = fs
Events
Error handling
Errors from streams within the pipeline are listened to,
and re-emitted on the pipeline itself, with an additional .stream
property
being set on the error
object, which is the stream that emitted it:
pipeline
Binding to contained stream's events
var stream = pipeline // Re-emit a stream's events on the pipeline:pipelinepipeline // Remove re-emission of a stream's event:pipeline // Stop re-emission of all of stream's events on the pipeline:pipeline
Example
var Pipage = // Let's say we have a stream which emits an event// we want to capture without having to get a reference to// that particular stream:module { var checksumStream = var pipeline = checksumStream // This will cause the pipeline to re-emit // the 'checksums' event from the `checksumStream` pipeline // Add some more fancy things to the pipeline... return pipeline }
var createPipeline = var pipeline = // Now we can listen for the bound event directly on the pipelinepipeline fs
Nested Pipelines
Since pipelines are duplex streams, and contain duplex streams, they can be nested arbitrarily:
var pipeline =