pax_global_header00006660000000000000000000000064130665011100014503gustar00rootroot0000000000000052 comment=adb93c2772a57d03b09b53b771ac0dd8498af722 node-stream-spigot-3.0.6/000077500000000000000000000000001306650111000152325ustar00rootroot00000000000000node-stream-spigot-3.0.6/.gitignore000066400000000000000000000000301306650111000172130ustar00rootroot00000000000000node_modules .tern-port node-stream-spigot-3.0.6/LICENSE000066400000000000000000000021061306650111000162360ustar00rootroot00000000000000(The MIT License) Copyright (c) Bryce B. Baril 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. node-stream-spigot-3.0.6/README.md000066400000000000000000000053201306650111000165110ustar00rootroot00000000000000Stream Spigot ============= [![NPM](https://nodei.co/npm/stream-spigot.png)](https://nodei.co/npm/stream-spigot/) [![david-dm](https://david-dm.org/brycebaril/node-stream-spigot.png)](https://david-dm.org/brycebaril/node-stream-spigot/) [![david-dm](https://david-dm.org/brycebaril/node-stream-spigot/dev-status.png)](https://david-dm.org/brycebaril/node-stream-spigot#info=devDependencies/) A generator for (streams2) Readable streams, useful for testing or converting simple lazy functions into Readable streams, or just creating Readable streams without all the boilerplate. ```javascript var spigot = require("stream-spigot") spigot.array(["ABCDEFG"]).pipe(process.stdout) // ABCDEFG spigot.array(["ABC", "DEF", "G"]).pipe(process.stdout) // same as: (short form) spigot(["ABC", "DEF", "G"]).pipe(process.stdout) // ABCDEFG // Create a stream out of a synchronous generator: var count = 0 function gen() { if (count++ < 5) { return {val: count} } } spigot.sync({objectMode: true}, gen).pipe(...) /* {val: 1} {val: 2} {val: 3} {val: 4} {val: 5} */ // Create a more traditional Readable stream: var source = spigot({objectMode: true}, function () { var self = this iterator.next(function (err, value) { if (err) return self.emit("error", err) self.push(value) }) }) source.pipe(...) ``` Usage ===== spigot([options,] _read) --- Create a Readable stream instance with the specified _read method. Your _read method should follow the normal [stream.Readable _read](http://nodejs.org/api/stream.html#stream_readable_read_size_1) syntax. (I.e. it should call `this.push(chunk)`) spigot([options, ], array) --- Create a Readable stream instance that will emit each member of the specified array until it is consumed. Creates a copy of the given array and consumes that -- if this will cause memory issues, consider implementing your own _read function to consume your array. var Spigot = spigot.ctor([options,], _read) --- Same as the above except provides a constructor for your Readable class. You can then create instances by using either `var source = new Spigot()` or `var source = Spigot()`. var Spigot = spigot.ctor([options,], array) --- Same as the above except provides a constructor for your Readable class. You can then create instances by using either `var source = new Spigot()` or `var source = Spigot()`. spigot.array([options, ], array) --- A manual version of the above to specify an array. spigot.sync([options,] fn) ------------------------ Create a readable instance providing a synchronous generator function. It will internally wrap your synchronous function as an async function. Options ------- Accepts standard [readable-stream](http://npmjs.org/api/stream.html) options. LICENSE ======= MIT node-stream-spigot-3.0.6/index.js000066400000000000000000000031711306650111000167010ustar00rootroot00000000000000module.exports = make module.exports.ctor = ctor module.exports.array = array module.exports.sync = sync const Readable = require("readable-stream/readable") , inherits = require("util").inherits , xtend = require("xtend") , setImmediate = global.setImmediate || process.nextTick function ctor (options, _read) { if (_read == null) { _read = options options = {} } if (Array.isArray(_read)) _read = _shifter(_read) if (typeof _read != "function") throw new Error("You must implement an _read function for Spigot") function Spigot (override) { if (!(this instanceof Spigot)) return new Spigot(override) this.options = xtend(options, override) Readable.call(this, this.options) } inherits(Spigot, Readable) Spigot.prototype._read = _read return Spigot } function make(options, _read) { return ctor(options, _read)() } function _shifter(array) { var copy = array.slice(0) return function _shift() { var self = this setImmediate(function later() { var val = copy.shift() if (val === undefined) { val = null } self.push(val) }) } } function array(options, array) { if (Array.isArray(options)) { array = options options = {} } return make(options, _shifter(array)) } function sync(options, fn) { if (typeof options == "function") { fn = options options = {} } var toAsync = function toAsync() { var self = this setImmediate(function later() { var val = fn() if (val === undefined) { val = null } self.push(val) }) } return make(options, toAsync) } node-stream-spigot-3.0.6/package.json000066400000000000000000000013401306650111000175160ustar00rootroot00000000000000{ "name": "stream-spigot", "version": "3.0.6", "description": "A readable stream generator, useful for testing or converting simple functions into Readable streams.", "main": "index.js", "repository": { "type": "git", "url": "https://github.com/brycebaril/node-stream-spigot" }, "directories": { "test": "test" }, "scripts": { "test": "node test/" }, "browser": { "readable-stream/readable": "_stream_readable" }, "keywords": [ "streams2", "testing", "readable" ], "author": "Bryce B. Baril", "license": "MIT", "devDependencies": { "concat-stream": "~1.6.0", "tape": "~4.6.3" }, "dependencies": { "readable-stream": "~2.2.6", "xtend": "~4.0.0" } } node-stream-spigot-3.0.6/test/000077500000000000000000000000001306650111000162115ustar00rootroot00000000000000node-stream-spigot-3.0.6/test/index.js000066400000000000000000000064161306650111000176650ustar00rootroot00000000000000var test = require("tape").test var concat = require("concat-stream") var spigot = require("../") test("simple", function (t) { t.plan(1) var content = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" function match(d) { t.equals(d.toString(), content) } var s = spigot.array([content]).pipe(concat(match)) }) test("chunked", function (t) { t.plan(1) var content = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" function match(d) { t.equals(d.toString(), content) } var s = spigot.array(["ABCDEFG","HIJKLMNOPQ","RSTUVWXYZ"]).pipe(concat(match)) }) test("chunked auto-detect array", function (t) { t.plan(1) var content = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" function match(d) { t.equals(d.toString(), content) } var s = spigot(["ABCDEFG","HIJKLMNOPQ","RSTUVWXYZ"]).pipe(concat(match)) }) test("chunked auto-detect array objectMode", function (t) { t.plan(1) var input = [{cats: "meow", dogs: "woof"}, {birds: "tweet", elephant: "toot"}] function match(d) { t.deepEquals(d, input) } var s = spigot.array({objectMode: true}, input).pipe(concat(match)) }) test("null in array", function (t) { t.plan(1) var content = "AB" function match(d) { t.equals(d.toString(), content) } var s = spigot.array(["A", "B", null, "C"]).pipe(concat(match)) }) test("objectMode", function (t) { t.plan(1) var input = {cats: "meow", dogs: "woof"} function match(d) { t.equals(d[0], input) } var s = spigot.array({objectMode: true}, [input]).pipe(concat(match)) }) test("function", function (t) { t.plan(1) var c = 0 var fn = function () { if (c++ < 5) { return c.toString() } } function match(d) { t.equals(d.toString(), "12345") } var s = spigot.sync(fn).pipe(concat(match)) }) test("async", function (t) { t.plan(1) var c = 0 var fn = function (cb) { var self = this if (c++ < 5) { setTimeout(function () { self.push(c.toString()) }, 10) } else { setTimeout(function () { self.push(null) }, 10) } } function match(d) { t.equals(d.toString(), "12345") } var s = spigot(fn).pipe(concat(match)) }) test("async objectMode", function (t) { t.plan(1) var c = 0 var fn = function (cb) { var self = this if (c++ < 5) { setTimeout(function () { self.push(c) }, 10) } else { setTimeout(function () { self.push(null) }, 10) } } function match(d) { t.deepEquals(d, [1, 2, 3, 4, 5]) } var s = spigot({objectMode: true}, fn).pipe(concat({encoding: "object"}, match)) }) test("async ctor", function (t) { t.plan(1) var c = 0 var fn = function (cb) { var self = this if (c++ < 5) { setTimeout(function () { self.push(c.toString()) }, 10) } else { setTimeout(function () { self.push(null) }, 10) } } function match(d) { t.equals(d.toString(), "12345") } var spig = spigot.ctor(fn) var s = spig().pipe(concat(match)) }) test("function objectMode", function (t) { t.plan(1) var c = 0 var fn = function () { if (c++ < 5) { return {val: c} } } function match(d) { t.deepEquals(d, [{val: 1}, {val: 2}, {val: 3}, {val: 4}, {val: 5}]) } var s = spigot.sync({objectMode: true}, fn).pipe(concat(match)) })