node-pipe-segment
Think of a pipe-segment as a bundle of streams, that do one thing together. See the discussion here: https://github.com/jbenet/random-ideas/issues/24 . Pipe segments are designed to be joined together into more complicated pipelines. It's similar in spirit to substack/labeled-stream-splicer, but rather than making one big pipeline, it uses segments that expose different streams for different things.
Inherit from this module and follow the pattern to make reusable components that can be joined together.
Install
npm install pipe-segment
Examples
Integrity Checksum pipe segment
Let's make a stream that wraps raw data with checksums (into a checksum-buffer
). This is just a simple transform stream.
var multihash = var CkBuffer = var through2 = { func = multihash // ensure valid func code return through2 { return buffer }}
And one that unwraps a checksum-buffer
. This is just a simple transform stream again.
var CkBuffer = var through2 = { // dont need func return through2 { return data }}
Now, let's make a segment that checks the checksum on a checksum-buffer
. A segment is like a bundle of streams that are all related. This one is basically a pipe-segment-filter
, so its stream interfaces will be input
, output
, and failed
.
var CkBuffer = var multihash = var filterSegment = // filterSegment is like a T pipe segment.// it returns a segment object, with three streams:// - segment.input// - segment.output// - segment.filtered { // func = optional check for a specific multihash function if func func = multihash return { var ckbuf = if func && ckbufmhparamscode !== func return false return ckbuf }}
Now, let's make a checksum integrity checked pipe-segment
.
// we'll reuse the two functions abovevar segment = // makes pipe segmentsvar duplexer2 = { // raw ----------- wrap -------> cksum // side <--- unwrap --- check --- side // make the internal streams var check = var wrap = var unwrap = // wire up the internal streams checkoutput // make duplex interfaces for each "side" of the streams var side1 = // read from unwrap, write to wrap var side2 = // read from wrap, write to check.input return }
Boom, now we have a T pipe that wraps/unwraps/checks multihash checksums.