pax_global_header00006660000000000000000000000064127657101030014514gustar00rootroot0000000000000052 comment=e3081d2c18d847a4517d5e5eb7e9554c8adca3cf ordered-read-streams-1.0/000077500000000000000000000000001276571010300153655ustar00rootroot00000000000000ordered-read-streams-1.0/.gitignore000066400000000000000000000001521276571010300173530ustar00rootroot00000000000000lib-cov *.seed *.log *.csv *.dat *.out *.pid *.gz coverage pids logs results node_modules npm-debug.log ordered-read-streams-1.0/.jscsrc000066400000000000000000000001551276571010300166560ustar00rootroot00000000000000{ "preset": "google", "requireSemicolons": true, "disallowSpacesInAnonymousFunctionExpression": null } ordered-read-streams-1.0/.travis.yml000066400000000000000000000001371276571010300174770ustar00rootroot00000000000000sudo: false branches: except: /^v\d/ language: node_js node_js: - iojs - node - '0.10' ordered-read-streams-1.0/LICENSE000066400000000000000000000020741276571010300163750ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 Artem Medeusheyev 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. ordered-read-streams-1.0/README.md000066400000000000000000000026021276571010300166440ustar00rootroot00000000000000# ordered-read-streams [![NPM version](https://img.shields.io/npm/v/ordered-read-streams.svg)](http://badge.fury.io/js/ordered-read-streams) [![Build Status](https://travis-ci.org/armed/ordered-read-streams.svg?branch=master)](https://travis-ci.org/armed/ordered-read-streams) Combines array of streams into one read stream in strict order. ## Installation `npm install ordered-read-streams` ## Overview `ordered-read-streams` handles all data/errors from input streams in parallel, but emits data/errors in strict order in which streams are passed to constructor. This is `objectMode = true` stream. ## Example ```js var through = require('through2'); var Ordered = require('ordered-read-streams'); var s1 = through.obj(function (data, enc, next) { var self = this; setTimeout(function () { self.push(data); next(); }, 200) }); var s2 = through.obj(function (data, enc, next) { var self = this; setTimeout(function () { self.push(data); next(); }, 30) }); var s3 = through.obj(function (data, enc, next) { var self = this; setTimeout(function () { self.push(data); next(); }, 100) }); var streams = new Ordered([s1, s2, s3]); streams.on('data', function (data) { console.log(data); }) s1.write('stream 1'); s1.end(); s2.write('stream 2'); s2.end(); s3.write('stream 3'); s3.end(); ``` Ouput will be: ``` stream 1 stream 2 stream 3 ``` ## Licence MIT ordered-read-streams-1.0/index.js000066400000000000000000000032161276571010300170340ustar00rootroot00000000000000var Readable = require('readable-stream/readable'); var isReadable = require('is-stream').readable; var util = require('util'); function addStream (streams, stream) { if (!isReadable(stream)) { throw new Error('All input streams must be readable'); } var self = this; stream._buffer = []; stream.on('readable', function () { var chunk = stream.read(); if (chunk === null) { return; } if (this === streams[0]) { self.push(chunk); } else { this._buffer.push(chunk); } }); stream.on('end', function () { for (var stream = streams[0]; stream && stream._readableState.ended; stream = streams[0]) { while (stream._buffer.length) { self.push(stream._buffer.shift()); } streams.shift(); } if (!streams.length) { self.push(null); } }); stream.on('error', this.emit.bind(this, 'error')); streams.push(stream); } function OrderedStreams (streams, options) { if (!(this instanceof(OrderedStreams))) { return new OrderedStreams(streams, options); } streams = streams || []; options = options || {}; options.objectMode = true; Readable.call(this, options); if (!Array.isArray(streams)) { streams = [streams]; } if (!streams.length) { return this.push(null); // no streams, close } var addStreamBinded = addStream.bind(this, []); streams.forEach(function (item) { if (Array.isArray(item)) { item.forEach(addStreamBinded); } else { addStreamBinded(item); } }); } util.inherits(OrderedStreams, Readable); OrderedStreams.prototype._read = function () {}; module.exports = OrderedStreams; ordered-read-streams-1.0/package.json000066400000000000000000000012131276571010300176500ustar00rootroot00000000000000{ "name": "ordered-read-streams", "version": "1.0.0", "description": "Combines array of streams into one read stream in strict order", "files": [ "index.js" ], "scripts": { "test": "jscs *.js test/*js && jshint *.js test/*.js && mocha" }, "repository": "armed/ordered-read-streams", "author": "Artem Medeusheyev ", "license": "MIT", "dependencies": { "is-stream": "^1.0.1", "readable-stream": "^2.0.1" }, "devDependencies": { "jscs": "^1.13.1", "jshint": "^2.8.0", "mocha": "^2.2.5", "pre-commit": "^1.0.10", "should": "^7.0.1", "through2": "^2.0.0" } } ordered-read-streams-1.0/test/000077500000000000000000000000001276571010300163445ustar00rootroot00000000000000ordered-read-streams-1.0/test/main.js000066400000000000000000000066031276571010300176330ustar00rootroot00000000000000/* global it, describe */ require('should'); var through = require('through2'); var OrderedStreams = require('..'); describe('ordered-read-streams', function () { it('should end if no streams are given', function (done) { var streams = new OrderedStreams(); streams.on('data', function () { done('error'); }); streams.on('end', done); }); it('should throw error if stream is not readable', function (done) { var writable = {readable: false}; try { new OrderedStreams(writable); } catch (e) { e.message.should.equal('All input streams must be readable'); done(); } }); it('should emit data from all streams', function(done) { var s1 = through.obj(); var s2 = through.obj(); var s3 = through.obj(); var streams = new OrderedStreams([s1, s2, s3]); var results = []; streams.on('data', function (data) { results.push(data); }); streams.on('end', function () { results.length.should.be.exactly(3); results[0].should.equal('stream 1'); results[1].should.equal('stream 2'); results[2].should.equal('stream 3'); done(); }); s1.write('stream 1'); s1.end(); s2.write('stream 2'); s2.end(); s3.write('stream 3'); s3.end(); }); it('should emit all data event from each stream', function (done) { var s = through.obj(); var streams = new OrderedStreams(s); var results = []; streams.on('data', function (data) { results.push(data); }); streams.on('end', function () { results.length.should.be.exactly(3); done(); }); s.write('data1'); s.write('data2'); s.write('data3'); s.end(); }); it('should preserve streams order', function(done) { var s1 = through.obj(function (data, enc, next) { var self = this; setTimeout(function () { self.push(data); next(); }, 200); }); var s2 = through.obj(function (data, enc, next) { var self = this; setTimeout(function () { self.push(data); next(); }, 30); }); var s3 = through.obj(function (data, enc, next) { var self = this; setTimeout(function () { self.push(data); next(); }, 100); }); var streams = new OrderedStreams([s1, s2, s3]); var results = []; streams.on('data', function (data) { results.push(data); }); streams.on('end', function () { results.length.should.be.exactly(3); results[0].should.equal('stream 1'); results[1].should.equal('stream 2'); results[2].should.equal('stream 3'); done(); }); s1.write('stream 1'); s1.end(); s2.write('stream 2'); s2.end(); s3.write('stream 3'); s3.end(); }); it('should emit stream errors downstream', function (done) { var s = through.obj(function (data, enc, next) { this.emit('error', new Error('stahp!')); next(); }); var s2 = through.obj(); var errMsg; var streamData; var streams = new OrderedStreams([s, s2]); streams.on('data', function (data) { streamData = data; }); streams.on('error', function (err) { errMsg = err.message; }); streams.on('end', function () { errMsg.should.equal('stahp!'); streamData.should.equal('Im ok!'); done(); }); s.write('go'); s.end(); s2.write('Im ok!'); s2.end(); }); });