Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 47a597f
Showing
8 changed files
with
523 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
.tern-port |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
examples | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(The MIT License) | ||
|
||
Copyright (c) Bryce B. Baril <bryce@ravenwall.com> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
terminus | ||
===== | ||
|
||
[![NPM](https://nodei.co/npm/terminus.png)](https://nodei.co/npm/terminus/) | ||
|
||
`terminus` makes it easier to create streams2 Writable streams. You can either use it like `through2` to eliminate subclassing boilerplate, or use one of the provided helper terminus streams. | ||
|
||
```javascript | ||
var terminus = require("terminus") | ||
var through2 = require("through2") | ||
var spigot = require("stream-spigot") | ||
|
||
// Streams2 all the way down... | ||
|
||
function uc(chunk, encoding, callback) { | ||
this.push(chunk.toString().toUpperCase()) | ||
callback() | ||
} | ||
|
||
function log(chunk, encoding, callback) { | ||
// This example is very contrived, you're likely better off directly piping to `process.stdout` | ||
console.log(chunk.toString()) | ||
callback() | ||
} | ||
|
||
spigot(["my ", "dog ", "has ", "fleas"]) | ||
.pipe(through2(uc)) | ||
.pipe(terminus(log)) | ||
|
||
/* | ||
MY | ||
DOG | ||
HAS | ||
FLEAS | ||
*/ | ||
|
||
// devnull | ||
|
||
var spy = require("through2-spy") | ||
|
||
spigot(["my ", "dog ", "has ", "fleas"]) | ||
.pipe(spy({highWaterMark: 2}, function (buf) {console.log(buf.toString())})) | ||
.pipe(terminus.devnull()) | ||
|
||
/* | ||
my | ||
dog | ||
has | ||
fleas | ||
*/ | ||
|
||
// concat | ||
|
||
function reverse(contents) { | ||
console.log(contents.toString().split("").reverse().join("")) | ||
} | ||
|
||
spigot(["my ", "dog ", "has ", "fleas"]) | ||
.pipe(terminus.concat(reverse)) | ||
|
||
/* | ||
saelf sah god ym | ||
*/ | ||
|
||
// tail | ||
|
||
var chunkLengths = [] | ||
function logLength(chunk) { | ||
chunkLengths.push(chunk.length) | ||
} | ||
|
||
var ws = terminus.tail(logLength) | ||
ws.on("finish", function () { | ||
console.log(chunkLengths) | ||
}) | ||
|
||
spigot(["my ", "dog ", "has ", "fleas"]) | ||
.pipe(ws) | ||
|
||
/* | ||
[ 3, 4, 4, 5 ] | ||
*/ | ||
|
||
// objectMode | ||
|
||
var s = spigot({objectMode: true}, [ | ||
{foo: 1}, | ||
{foo: 2}, | ||
{foo: 3}, | ||
{foo: 4}, | ||
]) | ||
|
||
function timesTwo(record, encoding, callback) { | ||
record.foo *= 2 | ||
this.push(record) | ||
callback() | ||
} | ||
|
||
function logRecords(records) { | ||
console.log(records) | ||
} | ||
|
||
s.pipe(through2({objectMode: true}, timesTwo)) | ||
.pipe(terminus.concat({objectMode: true}, logRecords)) | ||
|
||
/* | ||
[ { foo: 2 }, { foo: 4 }, { foo: 6 }, { foo: 8 } ] | ||
*/ | ||
``` | ||
|
||
API | ||
=== | ||
|
||
`terminus([options,] _writeFunction)` | ||
--- | ||
|
||
Create a `streams.Writable` instance that will call `_writeFunction` on every chunk. Consult the [stream.Writable](http://nodejs.org/api/stream.html#stream_class_stream_writable_1) documentation for instructions on creating a `_write` function. | ||
|
||
`terminus.ctor([options,] _writeFunction)` | ||
--- | ||
|
||
Create a `streams.Writable` Subclass that can be used to re-create stream.Writable instances with the same _writeFunction. | ||
|
||
`terminus.devnull([options])` | ||
--- | ||
|
||
Create a `stream.Writable` instance that is akin to writing to `dev/null` i.e. it doesn't do anything except give your stream somewhere to go. | ||
|
||
Why? Because if your pipeline doesn't terminate on a Writable stream, it will get paused at the High Water Mark with nothing to unpause it. I've most often seen this when people are using PassThrough streams, or Transforms that incorporate all required behavior. | ||
|
||
`terminus.concat([options], fn)` | ||
--- | ||
|
||
Collect the entire stream and when it is done, call `fn(contents)`. This is similar to the stream behavior of [concat-stream](http://npm.im/concat-stream) without the extra Array/Buffer concat behavior and entirely in streams2. | ||
|
||
`terminus.tail([options], fn)` | ||
--- | ||
|
||
A slightly less complicated version of `terminus([options,] _writeFunction)` that only requries you to provide a function that operates as `fn(chunk, encoding)`. | ||
|
||
options | ||
--- | ||
|
||
All functions accept standard `streams.Writable` options, that is: | ||
|
||
* highWaterMark `[Number]` Buffer level when write() starts returning false. `Default=16kb` | ||
* decodeStrings `[Boolean]` Whether not to decode strings into Buffers before passing them to _write() `Default=true` | ||
* objectMode `[Boolean]` If the content is Javascript objects versus strings/buffers. `Default=false` | ||
|
||
objectMode | ||
--- | ||
|
||
The most common option you'll be setting is `objectMode` which will enable you to stream Javascript objects, e.g. records. Unfortunately this is currently required and **ALL** streams2 parts of your stream pipeline must be in `objectMode` or you'll get errors. It's annoying, I know. | ||
|
||
LICENSE | ||
======= | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
var terminus = require("../terminus") | ||
var through2 = require("through2") | ||
var spigot = require("stream-spigot") | ||
|
||
// Streams2 all the way down... | ||
|
||
function uc(chunk, encoding, callback) { | ||
this.push(chunk.toString().toUpperCase()) | ||
callback() | ||
} | ||
|
||
function log(chunk, encoding, callback) { | ||
// This example is very contrived, you're likely better off directly piping to `process.stdout` | ||
console.log(chunk.toString()) | ||
callback() | ||
} | ||
|
||
spigot(["my ", "dog ", "has ", "fleas"]) | ||
.pipe(through2(uc)) | ||
.pipe(terminus(log)) | ||
|
||
// devnull | ||
|
||
var spy = require("through2-spy") | ||
|
||
spigot(["my ", "dog ", "has ", "fleas"]) | ||
.pipe(spy({highWaterMark: 2}, function (buf) {console.log(buf.toString())})) | ||
.pipe(terminus.devnull()) | ||
|
||
// concat | ||
|
||
function reverse(contents) { | ||
console.log(contents.toString().split("").reverse().join("")) | ||
} | ||
|
||
spigot(["my ", "dog ", "has ", "fleas"]) | ||
.pipe(terminus.concat(reverse)) | ||
|
||
// tail | ||
|
||
var chunkLengths = [] | ||
function logLength(chunk) { | ||
chunkLengths.push(chunk.length) | ||
} | ||
|
||
var ws = terminus.tail(logLength) | ||
ws.on("finish", function () { | ||
console.log(chunkLengths) | ||
}) | ||
|
||
spigot(["my ", "dog ", "has ", "fleas"]) | ||
.pipe(ws) | ||
|
||
// objectMode | ||
|
||
var s = spigot({objectMode: true}, [ | ||
{foo: 1}, | ||
{foo: 2}, | ||
{foo: 3}, | ||
{foo: 4}, | ||
]) | ||
|
||
function timesTwo(record, encoding, callback) { | ||
record.foo *= 2 | ||
this.push(record) | ||
callback() | ||
} | ||
|
||
function logRecords(records) { | ||
console.log(records) | ||
} | ||
|
||
s.pipe(through2({objectMode: true}, timesTwo)) | ||
.pipe(terminus.concat({objectMode: true}, logRecords)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "terminus", | ||
"version": "0.0.0", | ||
"description": "An abstraction for making stream.Writable streams without all the boilerplate.", | ||
"main": "terminus.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "node test/" | ||
}, | ||
"keywords": [ | ||
"streams", | ||
"streams2", | ||
"concat", | ||
"tail", | ||
"devnull" | ||
], | ||
"author": "Bryce B. Baril", | ||
"license": "MIT", | ||
"dependencies": { | ||
"readable-stream": "~1.1.9", | ||
"stream-spigot": "~2.1.1", | ||
"xtend": "~2.1.1", | ||
"bops": "0.0.6" | ||
}, | ||
"devDependencies": { | ||
"tape": "~1.1.1", | ||
"through2-spy": "~1.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
module.exports = make | ||
module.exports.ctor = ctor | ||
|
||
module.exports.devnull = devnull | ||
module.exports.concat = concat | ||
module.exports.tail = tail | ||
|
||
const Writable = require("stream").Writable || require("readable-stream/writable") | ||
, inherits = require("util").inherits | ||
, xtend = require("xtend") | ||
, bops = require("bops") | ||
|
||
function noop (chunk, enc, callback) { | ||
callback() | ||
} | ||
|
||
function ctor (options, _write) { | ||
if (typeof options == "function") { | ||
flush = _write | ||
_write = options | ||
options = {} | ||
} | ||
|
||
if (typeof _write != "function") | ||
_write = noop | ||
|
||
function Terminus (override) { | ||
if (!(this instanceof Terminus)) | ||
return new Terminus(override) | ||
|
||
this.options = xtend(options, override) | ||
Writable.call(this, this.options) | ||
} | ||
|
||
inherits(Terminus, Writable) | ||
|
||
Terminus.prototype._write = _write | ||
|
||
return Terminus | ||
} | ||
|
||
function make(options, _write) { | ||
return ctor(options, _write)() | ||
} | ||
|
||
function devnull(options) { | ||
return make(options, noop) | ||
} | ||
|
||
function concat(options, fn) { | ||
if (typeof options == "function") { | ||
fn = options | ||
options = {} | ||
} | ||
var terminus = make(options, function (chunk, encoding, callback) { | ||
if (this._collection == null) this._collection = [] | ||
this._collection.push(chunk) | ||
callback() | ||
}) | ||
|
||
terminus.on("finish", function () { | ||
if (options.objectMode) | ||
fn(this._collection) | ||
else | ||
fn(bops.join(this._collection)) | ||
}) | ||
|
||
return terminus | ||
} | ||
|
||
function tail(options, fn) { | ||
if (typeof options == "function") { | ||
fn = options | ||
options = {} | ||
} | ||
return make(options, function (chunk, encoding, callback) { | ||
fn(chunk, encoding) | ||
return callback() | ||
}) | ||
} |
Oops, something went wrong.