package/package.json000644 000765 000024 0000001072 12363102125013010 0ustar00000000 000000 { "name": "stream-consume", "version": "0.1.0", "description": "Consume a stream to ensure it keeps flowing", "main": "index.js", "scripts": { "test": "mocha" }, "repository": { "type": "git", "url": "https://github.com/aroneous/stream-consume.git" }, "author": "Aron Nopanen", "license": "MIT", "bugs": { "url": "https://github.com/aroneous/stream-consume/issues" }, "homepage": "https://github.com/aroneous/stream-consume", "devDependencies": { "mocha": "^1.20.1", "should": "^4.0.4", "through2": "^0.5.1" } } package/.npmignore000644 000765 000024 0000000015 12363101115012513 0ustar00000000 000000 node_modules package/README.md000644 000765 000024 0000003455 12363075070012020 0ustar00000000 000000 # stream-consume A node module ensures a Readable stream continues flowing if it's not piped to another destination. npm install stream-consume ## Usage Simply pass a stream to `stream-consume`. Both legacy streams and streams2 are supported. ``` js var consume = require('stream-consume'); consume(readableStream); ``` ## Details Only Readable streams are processed (as determined by presence of `readable` property and a `resume` property that is a function). If called with anything else, it's a NOP. For a streams2 stream (as determined by presence of a `_readableState` property), nothing is done if the stream has already been piped to at least one other destination. `resume()` is used to cause the stream to continue flowing. ## License The MIT License (MIT) Copyright (c) 2014 Aron Nopanen 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/index.js000644 000765 000024 0000000772 12363076312012205 0ustar00000000 000000 module.exports = function(stream) { if (stream.readable && typeof stream.resume === 'function') { var state = stream._readableState; if (!state || state.pipesCount === 0) { // Either a classic stream or streams2 that's not piped to another destination try { stream.resume(); } catch (err) { console.error("Got error: " + err); // If we can't, it's not worth dying over } } } }; package/test/tests.js000644 000765 000024 0000010447 12363100774013217 0ustar00000000 000000 /*jshint node:true */ /*global describe:false, it:false */ "use strict"; var consume = require('../'); var Stream = require('stream'); var Readable = Stream.Readable; var Writable = Stream.Writable; var Duplex = Stream.Duplex; var should = require('should'); var through = require('through2'); require('mocha'); describe('stream-consume', function() { it('should cause a Readable stream to complete if it\'s not piped anywhere', function(done) { var rs = new Readable({highWaterMark: 2}); var a = 0; var ended = false; rs._read = function() { if (a++ < 100) { rs.push(a + ""); } else { ended = true; rs.push(null); } }; rs.on("end", function() { a.should.be.above(99); ended.should.be.true; done(); }); consume(rs); }); it('should work with Readable streams in objectMode', function(done) { var rs = new Readable({highWaterMark: 2, objectMode: true}); var a = 0; var ended = false; rs._read = function() { if (a++ < 100) { rs.push(a); } else { ended = true; rs.push(null); } }; rs.on("end", function() { a.should.be.above(99); ended.should.be.true; done(); }); consume(rs); }); it('should not interfere with a Readable stream that is piped somewhere', function(done) { var rs = new Readable({highWaterMark: 2}); var a = 0; var ended = false; rs._read = function() { if (a++ < 100) { rs.push("."); } else { ended = true; rs.push(null); } }; var sizeRead = 0; var ws = new Writable({highWaterMark: 2}); ws._write = function(chunk, enc, next) { sizeRead += chunk.length; next(); } ws.on("finish", function() { a.should.be.above(99); ended.should.be.true; sizeRead.should.equal(100); done(); }); rs.pipe(ws); consume(rs); }); it('should not interfere with a Writable stream', function(done) { var rs = new Readable({highWaterMark: 2}); var a = 0; var ended = false; rs._read = function() { if (a++ < 100) { rs.push("."); } else { ended = true; rs.push(null); } }; var sizeRead = 0; var ws = new Writable({highWaterMark: 2}); ws._write = function(chunk, enc, next) { sizeRead += chunk.length; next(); } ws.on("finish", function() { a.should.be.above(99); ended.should.be.true; sizeRead.should.equal(100); done(); }); rs.pipe(ws); consume(ws); }); it('should handle a Transform stream', function(done) { var rs = new Readable({highWaterMark: 2}); var a = 0; var ended = false; rs._read = function() { if (a++ < 100) { rs.push("."); } else { ended = true; rs.push(null); } }; var sizeRead = 0; var flushed = false; var ts = through({highWaterMark: 2}, function(chunk, enc, cb) { sizeRead += chunk.length; this.push(chunk); cb(); }, function(cb) { flushed = true; cb(); }); ts.on("end", function() { a.should.be.above(99); ended.should.be.true; sizeRead.should.equal(100); flushed.should.be.true; done(); }); rs.pipe(ts); consume(ts); }); it('should handle a classic stream', function(done) { var rs = new Stream(); var ended = false; var i; rs.on("end", function() { ended.should.be.true; done(); }); consume(rs); for (i = 0; i < 100; i++) { rs.emit("data", i); } ended = true; rs.emit("end"); }); });