Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
brycebaril committed Aug 17, 2013
0 parents commit cde6d64
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.tern-port
9 changes: 9 additions & 0 deletions LICENSE
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.
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
stream-splice
=====

[![NPM](https://nodei.co/npm/stream-splice.png)](https://nodei.co/npm/stream-splice/)

Compose multi-step streams into a single pipeline segment.

E.g. your transform module is actually two consecutive transform operations, but you want it exposed as a single "stream.Transform" object.

```javascript
// In your module
module.exports = tx
var filter = require("through2-filter")
var map = require("through2-map")
var splice = require("transform-pipeline")

function tx() {
var noBs = filter(function (chunk) { return /[^bB]/.exec(chunk) })
var uc = map(function (chunk) { return chunk.toString().toUpperCase() })
var zz = map(function (chunk) { return chunk.toString() + "z" })

return splice(noBs, uc, zz)
}

// Using your module:
var tx = require("my-module")

source.pipe(tx()).pipe(/* ... */)

// That is equivalent to:
source.pipe(noBs)
.pipe(uc)
.pipe(zz)
.pipe(/* ... */)

// A less contrived example:
// catLines is equivalent to fs.createReadStream except stream chunks
// will be the lines of the file
module.exports = catLines

var fs = require("fs")
var split = require("split")
var map = require("through2-map")
var splice = require("stream-splice")

function catLines(filename, options) {
var rs = fs.createReadStream(filename, options)
var reAddNewline = map(function (chunk) { return chunk.toString() + "\n" })
return splice(rs, split(), reAddNewline)
}


```

API
===

`require("stream-splice")(stream1 [,stream2] [,...streamN])`
---

Creates a pipeline that can be piped into/out of which is composed of all of the spliced streams piped together.

E.g.

```javascript
source.pipe(splice(a, b, c)).pipe(drain)

// is equivalent to

source.pipe(a)
.pipe(b)
.pipe(c)
.pipe(drain)

```

LICENSE
=======

MIT
13 changes: 13 additions & 0 deletions example/catlines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = catLines

var fs = require("fs")
var split = require("split")
var splice = require("../")
var map = require("through2-map")

function catLines(filename, options) {
var rs = fs.createReadStream(filename, options)
var reAddNewline = map(function (chunk) { return chunk.toString() + "\n" })

return splice(rs, split(), reAddNewline)
}
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = splice

var Transform = require("stream").Transform
|| require("readable-stream/transform")

function splice() {
var streams = [].slice.call(arguments)
if (streams.length == 0) throw new Error("No streams provided to splice")
if (streams.length == 1) return streams[0]

var first = streams[0]
var last = streams.reduce(function (prev, curr) {
return prev.pipe(curr)
})

first.pipe = function (destination, options) {
Transform.prototype.pipe.call(last, destination, options)
return last
}

return first
}
39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "stream-splice",
"version": "0.0.0",
"description": "Splice multiple streams into a single pipeline. Useful for exposing multi-step piped streams as a single stream.",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "node test/"
},
"repository": {
"type": "git",
"url": "git@github.com:brycebaril/stream-splice.git"
},
"keywords": [
"stream",
"streams",
"streams2",
"transform",
"pipeline",
"splice"
],
"author": "Bryce B. Baril",
"license": "MIT",
"bugs": {
"url": "https://github.com/brycebaril/stream-splice/issues"
},
"dependencies": {
"readable-stream": "~1.0.15"
},
"devDependencies": {
"tape": "~1.0.4",
"through2-map": "~1.1.0",
"through2-filter": "~1.1.0",
"stream-spigot": "~2.1.1",
"concat-stream": "~1.0.1"
}
}
80 changes: 80 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
var test = require("tape").test

var spigot = require("stream-spigot")
var filter = require("through2-filter")
var map = require("through2-map")
var concat = require("concat-stream")
var splice = require("../")

test("init", function (t) {
t.plan(2)
t.ok(splice, "loaded splice")
t.equals(typeof splice, "function", "And it is a function")
})

test("edge cases", function (t) {
t.throws(splice, "Splice with no args throws")
var source = spigot(["a", "b", "c"])
t.equals(splice(source), source, "Splicing a single stream is a noop")
t.end()
})

test("two transforms", function (t) {
t.plan(2)

var source = spigot(["a", "b", "c"])

function collect(results) {
t.equals(results.toString(), "AC", "Both streams ran")
}

var noBs = filter(function (chunk) { return /[^b]/.exec(chunk) })

var uc = map(function (chunk) { return chunk.toString().toUpperCase() })

var pipeline = splice(noBs, uc)

t.equal(pipeline, noBs, "pipeline is actually the first stream")

source.pipe(pipeline).pipe(concat(collect))
})

test("three transforms", function (t) {
t.plan(2)

var source = spigot(["a", "b", "c"])

function collect(results) {
t.equals(results.toString(), "AzCz", "All three streams ran")
}

var noBs = filter(function (chunk) { return /[^b]/.exec(chunk) })

var uc = map(function (chunk) { return chunk.toString().toUpperCase() })

var zz = map(function (chunk) { return chunk.toString() + "z" })

var pipeline = splice(noBs, uc, zz)

t.equal(pipeline, noBs, "pipeline is actually the first stream")

source.pipe(pipeline).pipe(concat(collect))
})

test("readable + transform", function (t) {
t.plan(2)

var source = spigot(["a", "b", "c"])

function collect(results) {
t.equals(results.toString(), "ABC", "Both streams ran")
}

var uc = map(function (chunk) { return chunk.toString().toUpperCase() })

var pipeline = splice(source, uc)

t.equal(pipeline, source, "pipeline is actually the first stream")

pipeline.pipe(concat(collect))
})

0 comments on commit cde6d64

Please sign in to comment.