0.0.1 • Published 12 years ago
stream-map v0.0.1
STREAM-MAP
An augmentation module for Stream, adds support for map, filter and reduce, The passed functions are applied to the data and their return values writen into the stream again. Internally uses a secondary stream that will be piped to the target one. Please note the following:
- Reduce must wait for the Stream to end, obviously.
- To do this, a mapBuffered method is provided.
- I'm aware that the Stream object will likely change in next node's versions, I'll delete this package as soon as we get support for this functionality.
Some examples
An angry echo server:
var net = require('net');
var Stream = require('..');
net.createServer(function(stream){
stream.map(function(buff){
return 'NO, '+buff.toString('utf8').toUpperCase();
}).pipe(stream);
}).listen('47641');
> echo "are you angry?" | nc localhost 47641
> NO, ARE YOU ANGRY?
An http server that performs byte sums:
var http = require('http');
var Stream = require('..');
http.createServer(function(req, res){
req.reduce(function(acc, buff){
acc = parseInt(acc) + buff.length;
return acc.toString();
}, 0).pipe(res);
}).listen('21934');
> curl -XPOST -d @index.js http://localhost:21934
> 2213
0.0.1
12 years ago