package/package.json000644 001750 001750 0000001022 13061071375013015 0ustar00000000 000000 { "name": "from", "version": "0.1.7", "description": "Easy way to make a Readable Stream", "main": "index.js", "scripts": { "test": "asynct test/*.js" }, "repository": { "type": "git", "url": "git://github.com/dominictarr/from.git" }, "keywords": [ "stream", "streams", "readable", "easy" ], "devDependencies": { "asynct": "1", "stream-spec": "0", "assertions": "~2.3.0" }, "author": "Dominic Tarr (dominictarr.com)", "license": "MIT" } package/.npmignore000644 001750 001750 0000000015 13061071350012520 0ustar00000000 000000 node_modules package/index.js000644 001750 001750 0000002664 13061071371012205 0ustar00000000 000000 'use strict'; var Stream = require('stream') // from // // a stream that reads from an source. // source may be an array, or a function. // from handles pause behaviour for you. module.exports = function from (source) { if(Array.isArray(source)) { var source_index = 0, source_len = source.length; return from (function (i) { if(source_index < source_len) this.emit('data', source[source_index++]) else this.emit('end') return true }) } var s = new Stream(), i = 0 s.ended = false s.started = false s.readable = true s.writable = false s.paused = false s.ended = false s.pause = function () { s.started = true s.paused = true } function next () { s.started = true if(s.ended) return while(!s.ended && !s.paused && source.call(s, i++, function () { if(!s.ended && !s.paused) process.nextTick(next); })) ; } s.resume = function () { s.started = true s.paused = false next() } s.on('end', function () { s.ended = true s.readable = false process.nextTick(s.destroy) }) s.destroy = function () { s.ended = true s.emit('close') } /* by default, the stream will start emitting at nextTick if you want, you can pause it, after pipeing. you can also resume before next tick, and that will also work. */ process.nextTick(function () { if(!s.started) s.resume() }) return s } package/.travis.yml000644 001750 001750 0000000076 13061071371012644 0ustar00000000 000000 language: node_js node_js: - "node" - "6" - "5" - "4" package/LICENSE.APACHE2000644 001750 001750 0000001112 13061071350012527 0ustar00000000 000000 Apache License, Version 2.0 Copyright (c) 2011 Dominic Tarr Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. package/LICENSE.MIT000644 001750 001750 0000002100 13061071350012153 0ustar00000000 000000 The MIT License Copyright (c) 2011 Dominic Tarr 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. package/readme.markdown000644 001750 001750 0000001550 13061071371013532 0ustar00000000 000000 [![TravisCI Build Status](https://travis-ci.org/nmhnmh/from.svg?branch=master)](https://travis-ci.org/nmhnmh/from) # from An easy way to create a `readable Stream`. ## from(function getChunk(count, next)) from takes a `getChunk` function and returns a stream. `getChunk` is called again and again, after each time the user calls `next()`, until the user emits `'end'` if `pause()` is called, the `getChunk` won't be called again untill `resume()` is called. ```js var from = require('from') var stream = from(function getChunk(count, next) { //do some sort of data this.emit('data', whatever) if(itsOver) this.emit('end') //ready to handle the next chunk next() //or, if it's sync: return true }) ``` ## from(array) from also takes an `Array` whose elements it emits one after another. ## License MIT / Apache2 package/test/index.js000644 001750 001750 0000007451 13061071371013163 0ustar00000000 000000 var from = require('..') var spec = require('stream-spec') var a = require('assertions') function read(stream, callback) { var actual = [] stream.on('data', function (data) { actual.push(data) }) stream.once('end', function () { callback(null, actual) }) stream.once('error', function (err) { callback(err) }) } function pause(stream) { stream.on('data', function () { if(Math.random() > 0.1) return stream.pause() process.nextTick(function () { stream.resume() }) }) } exports['inc'] = function (test) { var fs = from(function (i) { this.emit('data', i) if(i >= 99) return this.emit('end') return true }) spec(fs).readable().validateOnExit() read(fs, function (err, arr) { test.equal(arr.length, 100) test.done() }) } exports['inc - async'] = function (test) { var fs = from(function (i, next) { this.emit('data', i) if(i >= 99) return this.emit('end') next(); }) spec(fs).readable().validateOnExit() read(fs, function (err, arr) { test.equal(arr.length, 100) test.done() }) } exports['large stream - from an array'] = function (test) { var l = 100000 , expected = [] while(l--) expected.push(l * Math.random()) var fs = from(expected.slice()) spec(fs).readable().validateOnExit() read(fs, function (err, arr) { a.deepEqual(arr, expected) test.done() }) } exports['large stream - callback return true'] = function (test) { var fs = from(function (i, next) { this.emit('data', i) if(i >= 99999) return this.emit('end') return true; }) spec(fs).readable().validateOnExit() read(fs, function (err, arr) { test.equal(arr.length, 100000) test.done() }) } exports['large stream - callback call next()'] = function (test) { var fs = from(function (i, next) { this.emit('data', i) if(i >= 99999) return this.emit('end') next(); }) spec(fs).readable().validateOnExit() read(fs, function (err, arr) { test.equal(arr.length, 100000) test.done() }) } exports['simple'] = function (test) { var l = 1000 , expected = [] while(l--) expected.push(l * Math.random()) var t = from(expected.slice()) spec(t) .readable() .pausable({strict: true}) .validateOnExit() read(t, function (err, actual) { if(err) test.error(err) //fail a.deepEqual(actual, expected) test.done() }) } exports['simple pausable'] = function (test) { var l = 1000 , expected = [] while(l--) expected.push(l * Math.random()) var t = from(expected.slice()) spec(t) .readable() .pausable({strict: true}) .validateOnExit() pause(t) read(t, function (err, actual) { if(err) test.error(err) //fail a.deepEqual(actual, expected) test.done() }) } exports['simple (not strictly pausable) setTimeout'] = function (test) { var l = 10 , expected = [] while(l--) expected.push(l * Math.random()) var _expected = expected.slice() var t = from(function (i, n) { var self = this setTimeout(function () { if(_expected.length) self.emit('data', _expected.shift()) else if(!self.ended) self.emit('end') n() }, 3) }) /* using from in this way will not be strictly pausable. it could be extended to buffer outputs, but I think a better way would be to use a PauseStream that implements strict pause. */ spec(t) .readable() .pausable({strict: false }) .validateOnExit() //pause(t) var paused = false var i = setInterval(function () { if(!paused) t.pause() else t.resume() paused = !paused }, 2) t.on('end', function () { clearInterval(i) }) read(t, function (err, actual) { if(err) test.error(err) //fail a.deepEqual(actual, expected) test.done() }) }