Brooklet
Streams for functional programming in Node.js
About
Inspired by Highland.js by Caolan, the goal of this project is to provide a minimalistic, (almost) dependency-free library for stream-based functional programming in Node.js.
Status
Under development, unstable. Mainly a way to learn a bit more about streams and functional programming. Serves as the basis for a stream-based attempt at a functional web framework.
Documentation
Install with:
$ npm install --save brooklet
Use with:
var brooklet = require('brooklet');
Brooklet builds upon node.js' native streams (tracked through the readable-stream
module) and extends them with a number of useful methods (see API).
- brooklet.extend(obj) - this function extends the
obj
argument with all the methods provided by brooklet. It can be used both on instances and prototypes.
- brooklet.Transform() - this class extends the native
stream.Transform
class with all the methods provided by brooklet.extend()
.
- brooklet.Duplexer() - this class wraps a readable and a writable stream into a duplex stream. It extends the native
stream.Duplex
class with all the methods provided by brooklet.extend()
.
Methods from brooklet.extend()
can also be called on a stream through fn.apply()
or fn.call()
.
brooklet.extend.consume.call(stream, opts, function(chunk, next) {});
API
brooklet.Transform()
Constructor for the Transform
class.
var transform = brooklet.Transform({
objectMode: true,
transform: function timesTwo(chunk, enc, cb) {
this.push(chunk * 2);
cb();
}
});
brooklet.Duplexer()
Constructor for the Duplexer
class. A Duplexer
instance reads from its reader
stream and writes to its writer
stream.
var readable = brooklet.Transform();
var writable = brooklet.Transform();
var duplexer = brooklet.Duplexer({
reader: readable,
writer: writable
});
duplexer.reader === reader; // true
duplexer.writer === writer; // true
.consume()
stream.consume()
allows for controlled asynchronous consumption of chunks.
stream.consume(function(chunk, cb) {
if (chunk === null) {
return; // stream has ended
}
somethingAsync(chunk, function() {
cb();
});
});
After all chunks have been read and the stream has ended, the consume()
callback is called with a null
chunk.
.send()
Wrapper around writable.write()
, allows for both single chunks and array of chunks to be written to the stream. Supports chaining.
stream.send(['foo', 'bar'], 'utf8', stream.end.bind(stream));
.decode()
Wrapper around readable.setEncoding()
, throws if the encoding is not supported. Supports chaining.
stream.decode('utf8').on('data', function() {});
.into()
ToDo.
.from()
ToDo.
.compact()
ToDo.
.extend()
ToDo.
.through()
ToDo.
.process()
ToDo.
.capture()
ToDo.
Testing
$ git clone git@github.com:jacoscaz/node-brooklet.git
$ cd node-brooklet
$ npm install
$ npm test
Browser build
$ git clone git@github.com:jacoscaz/node-brooklet.git
$ cd node-brooklet
$ npm install
$ npm run browserify
License
Released under the included MIT license. See LICENSE.
Jacopo Scazzosi (c) 2015.