pull-stream
Advanced tools
Comparing version 2.0.0 to 2.1.0
19
index.js
@@ -7,13 +7,14 @@ | ||
for(var k in sources) | ||
exports[k] = pipeableSource(sources[k]) | ||
exports[k] = Source(sources[k]) | ||
for(var k in throughs) | ||
exports[k] = pipeable(throughs[k]) | ||
exports[k] = Duplex(throughs[k]) | ||
for(var k in sinks) | ||
exports[k] = pipeableSink(sinks[k]) | ||
exports[k] = Sink(sinks[k]) | ||
exports.pipeableSource = pipeableSource | ||
exports.pipeable = pipeable | ||
exports.pipeableSink = pipeableSink | ||
exports.Duplex = | ||
exports.Through = exports.pipeable = Through | ||
exports.Source = exports.pipeableSource = Source | ||
exports.Sink = exports.pipeableSink = Sink | ||
@@ -33,3 +34,3 @@ function addPipe(read) { | ||
function pipeableSource (createRead) { | ||
function Source (createRead) { | ||
return function () { | ||
@@ -41,3 +42,3 @@ var args = [].slice.call(arguments) | ||
function pipeable (createRead) { | ||
function Through (createRead) { | ||
return function () { | ||
@@ -62,3 +63,3 @@ var args = [].slice.call(arguments) | ||
function pipeableSink(createReader) { | ||
function Sink(createReader) { | ||
return function () { | ||
@@ -65,0 +66,0 @@ var args = [].slice.call(arguments) |
{ | ||
"name": "pull-stream", | ||
"description": "minimal pull stream", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"homepage": "https://github.com/dominictarr/pull-stream", | ||
@@ -6,0 +6,0 @@ "repository": { |
192
README.md
@@ -7,6 +7,6 @@ # pull-stream | ||
streams _push_ data to the next stream in the pipeline. | ||
In [new-streams](https://github.com/joyent/node/blob/v0.8/doc/api/stream.markdown), | ||
In [new-streams](https://github.com/joyent/node/blob/v0.10/doc/api/stream.markdown), | ||
data is pulled out of the source stream, into the destination. | ||
`pull-stream` is an minimal take on pull streams. | ||
`pull-stream` is a minimal take on pull streams. | ||
@@ -17,12 +17,30 @@ ## Examples | ||
### Pipeable | ||
### Pipeable Streams | ||
`pull.{Source,Through,Sink}` just wrap a function and give it a `.pipe(dest)`! | ||
``` js | ||
var pipeable = require('pull-stream').pipeable | ||
var pull = require('pull-stream') | ||
var createStream = pipeable(function (read) { | ||
var createSourceStream = pull.Source(function () { | ||
return function (end, cb) { | ||
return cb(end, Math.random()) | ||
} | ||
}) | ||
var createThroughStream = pull.Through(function (read) { | ||
return function (end, cb) { | ||
read(end, cb) | ||
} | ||
}) | ||
var createSinkStream = pull.Sink(function (read) { | ||
read(null, function next (end, data) { | ||
if(end) return | ||
console.log(data) | ||
read(null, next) | ||
}) | ||
}) | ||
createSourceStream().pipe(createThroughStream()).pipe(createSinkStream()) | ||
``` | ||
@@ -35,10 +53,25 @@ | ||
the readable stream is just a function, that may be called many times, | ||
and will (asynchronously) callback. | ||
See also: | ||
* [Sources](https://github.com/dominictarr/pull-stream/blob/master/docs/sources.md) | ||
* [Throughs](https://github.com/dominictarr/pull-stream/blob/master/docs/throughs.md) | ||
* [Sinks](https://github.com/dominictarr/pull-stream/blob/master/docs/sinks.md) | ||
if the user passes in `end`, then stop returning data. | ||
### Readable | ||
The readable stream is just a `function(end, cb)`, | ||
that may be called many times, | ||
and will (asynchronously) `callback(null, data)` once for each call. | ||
The readable stream eventually `callback(err)` if there was an error, or `callback(true)` | ||
if the stream has no more data. | ||
if the user passes in `end = true`, then stop getting data from wherever. | ||
All [Sources](https://github.com/dominictarr/pull-stream/blob/master/docs/sources.md) | ||
and [Throughs](https://github.com/dominictarr/pull-stream/blob/master/docs/throughs.md) | ||
are readable streams. | ||
``` js | ||
var i = 100 | ||
var randomReadable = function () { | ||
var randomReadable = pull.Source(function () { | ||
return function (end, cb) { | ||
@@ -50,19 +83,24 @@ if(end) return cb(end) | ||
} | ||
} | ||
}) | ||
``` | ||
A `reader`, is just a function that calls a readable. | ||
If you get an `end` stop reading. | ||
### Reader (aka, "writable") | ||
A `reader`, is just a function that calls a readable, | ||
until it decideds to stop, or the readable `cb(err || true)` | ||
All [Throughs](https://github.com/dominictarr/pull-stream/blob/master/docs/throughs.md) | ||
and [Sinks](https://github.com/dominictarr/pull-stream/blob/master/docs/sinks.md) | ||
are reader streams. | ||
``` js | ||
var logger = function (read) { | ||
read(null, function next(end, data) { | ||
if(end === true) return | ||
if(end) throw err | ||
var logger = pull.Sink(function (read) { | ||
read(null, function next(end, data) { | ||
if(end === true) return | ||
if(end) throw err | ||
console.log(data) | ||
readable(end, next) | ||
}) | ||
} | ||
} | ||
console.log(data) | ||
readable(end, next) | ||
}) | ||
}) | ||
``` | ||
@@ -73,14 +111,20 @@ | ||
``` js | ||
logger(randomReadable()) | ||
logger()(randomReadable()) | ||
``` | ||
Thats cool, but to be useful, we need transformation streams, | ||
that do input _and_ output. | ||
Or, if you prefer to read things left-to-right | ||
Simple! | ||
``` js | ||
randomReadable().pipe(logger()) | ||
``` | ||
### Duplex | ||
### Through / Duplex | ||
A duplex/through stream is both a `reader` that is also `readable` | ||
A duplex/through stream is just a function that takes a `read` function, | ||
and returns another `read` function. | ||
``` js | ||
var map = function (read, map) { | ||
var map = pull.Through(function (read, map) { | ||
//return a readable function! | ||
@@ -92,20 +136,5 @@ return function (end, cb) { | ||
} | ||
} | ||
}) | ||
``` | ||
join them together! | ||
### function composition style "pipe" | ||
``` js | ||
logger( | ||
map(randomReadable(), function (e) { | ||
return Math.round(e * 1000) | ||
})) | ||
``` | ||
That is good -- but it's kinda weird, | ||
because we are used to left to right syntax | ||
for streams... `ls | grep | wc -l` | ||
### pipeability | ||
@@ -123,72 +152,5 @@ | ||
Use `pipeable`, `pipeableSource` and `pipeableSink`, | ||
Use `Source`, `Through` and `Sink`, | ||
to add pipeability to your pull-streams. | ||
#### Sources | ||
``` js | ||
//infinite stream of random noise | ||
var pull = require('pull-stream') | ||
var infinite = pull.pipeableSource(function () { | ||
return function (end, cb) { | ||
if(end) return cb(end) | ||
cb(null, Math.random()) | ||
} | ||
}) | ||
//create an instace like this | ||
var infStream = infinite() | ||
``` | ||
#### Throughs/Transforms | ||
``` js | ||
//map! | ||
var pull = require('pull-stream') | ||
var map = pull.pipeable(function (read, map) { | ||
return function (end, cb) { | ||
read(end, function (end, data) { | ||
if(end) return cb(end) | ||
cb(null, map(data)) | ||
}) | ||
} | ||
}) | ||
//create an instance like this: | ||
var mapStream = map(function (d) { return d * 100 }) | ||
``` | ||
### Sinks | ||
``` js | ||
var pull = require('pull-stream') | ||
var log = pull.pipeableSink(function (read, done) { | ||
read(null, function next(end, data) { | ||
if(!end) { | ||
console.log(data) | ||
return setTimeout(function () { | ||
read(null, next) | ||
}, 200) | ||
} | ||
else //callback! | ||
done(end == true ? null : end) | ||
}) | ||
}) | ||
``` | ||
Now PIPE THEM TOGETHER! | ||
``` js | ||
infinite() | ||
.pipe(map(function (d) { return d * 100 })) | ||
.pipe(log()) | ||
``` | ||
JUST LIKE THAT! | ||
## More Cool Stuff | ||
@@ -200,10 +162,6 @@ | ||
var trippleThrough = | ||
through1() | ||
.pipe(through2()) | ||
.pipe(through3()) | ||
through1().pipe(through2()).pipe(through3()) | ||
//THE THREE THROUGHS BECOME ONE | ||
source() | ||
.pipe(trippleThrough) | ||
.pipe(sink()) | ||
source().pipe(trippleThrough).pipe(sink()) | ||
@@ -210,0 +168,0 @@ //and then pipe it later! |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
24706
562
0
166