pax_global_header00006660000000000000000000000064134146327650014525gustar00rootroot0000000000000052 comment=721f157cf757cf312813bcf972da43af19f0e4ef node-ordered-read-streams-1.0.1/000077500000000000000000000000001341463276500164605ustar00rootroot00000000000000node-ordered-read-streams-1.0.1/.gitignore000066400000000000000000000001521341463276500204460ustar00rootroot00000000000000lib-cov *.seed *.log *.csv *.dat *.out *.pid *.gz coverage pids logs results node_modules npm-debug.log node-ordered-read-streams-1.0.1/.jscsrc000066400000000000000000000001551341463276500177510ustar00rootroot00000000000000{ "preset": "google", "requireSemicolons": true, "disallowSpacesInAnonymousFunctionExpression": null } node-ordered-read-streams-1.0.1/.travis.yml000066400000000000000000000001601341463276500205660ustar00rootroot00000000000000sudo: false branches: except: /^v\d/ language: node_js node_js: - '6' - '5' - '4' - '0.12' - '0.10' node-ordered-read-streams-1.0.1/LICENSE000066400000000000000000000020741341463276500174700ustar00rootroot00000000000000The 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. node-ordered-read-streams-1.0.1/README.md000066400000000000000000000026021341463276500177370ustar00rootroot00000000000000# 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 node-ordered-read-streams-1.0.1/index.js000066400000000000000000000036151341463276500201320ustar00rootroot00000000000000var Readable = require('readable-stream/readable'); var util = require('util'); function isReadable(stream) { if (typeof stream.pipe !== 'function') { return false; } if (!stream.readable) { return false; } if (typeof stream._read !== 'function') { return false; } if (!stream._readableState) { return false; } return true; } 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(); while (chunk) { if (this === streams[0]) { self.push(chunk); } else { this._buffer.push(chunk); } chunk = stream.read(); } }); 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; node-ordered-read-streams-1.0.1/package.json000066400000000000000000000012161341463276500207460ustar00rootroot00000000000000{ "name": "ordered-read-streams", "version": "1.0.1", "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": { "readable-stream": "^2.0.1" }, "devDependencies": { "expect": "^1.20.2", "jscs": "^1.13.1", "jshint": "^2.8.0", "mississippi": "^1.3.0", "mocha": "^2.2.5", "pre-commit": "^1.0.10", "through2": "^2.0.0" } } node-ordered-read-streams-1.0.1/test/000077500000000000000000000000001341463276500174375ustar00rootroot00000000000000node-ordered-read-streams-1.0.1/test/main.js000066400000000000000000000072041341463276500207240ustar00rootroot00000000000000/* global it, describe */ var expect = require('expect'); var miss = require('mississippi'); var OrderedStreams = require('..'); var to = miss.to; var from = miss.from; var pipe = miss.pipe; var concat = miss.concat; function fromOnce(fn) { var called = false; return from.obj(function(size, next) { if (called) { return next(null, null); } called = true; fn.apply(this, arguments); }); } describe('ordered-read-streams', function () { it('ends if no streams are given', function (done) { var streams = new OrderedStreams(); pipe([ streams, concat() ], done); }); it('throws an error if stream is not readable', function (done) { var writable = to(); function withWritable() { new OrderedStreams(writable); } expect(withWritable).toThrow('All input streams must be readable'); done(); }); it('emits data from all streams', function(done) { var s1 = from.obj([{value: 'stream 1'}]); var s2 = from.obj([{value: 'stream 2'}]); var s3 = from.obj([{value: 'stream 3'}]); var streams = new OrderedStreams([s1, s2, s3]); function assert(results) { expect(results.length).toEqual(3); expect(results[0]).toEqual({value: 'stream 1'}); expect(results[1]).toEqual({value: 'stream 2'}); expect(results[2]).toEqual({value: 'stream 3'}); } pipe([ streams, concat(assert) ], done); }); it('emits all data event from each stream', function (done) { var s = from.obj([ {value: 'data1'}, {value: 'data2'}, {value: 'data3'} ]); var streams = new OrderedStreams(s); function assert(results) { expect(results.length).toEqual(3); } pipe([ streams, concat(assert) ], done); }); it('preserves streams order', function(done) { var s1 = fromOnce(function (size, next) { setTimeout(function () { next(null, {value: 'stream 1'}); }, 200); }); var s2 = fromOnce(function (size, next) { setTimeout(function () { next(null, {value: 'stream 2'}); }, 30); }); var s3 = fromOnce(function (size, next) { setTimeout(function () { next(null, {value: 'stream 3'}); }, 100); }); var streams = new OrderedStreams([s1, s2, s3]); function assert(results) { expect(results.length).toEqual(3); expect(results[0]).toEqual({value: 'stream 1'}); expect(results[1]).toEqual({value: 'stream 2'}); expect(results[2]).toEqual({value: 'stream 3'}); } pipe([ streams, concat(assert) ], done); }); it('emits stream errors downstream', function (done) { var s = fromOnce(function(size, next) { setTimeout(function () { next(new Error('stahp!')); }, 500); }); var s2 = from.obj([{value: 'Im ok!'}]); var streams = new OrderedStreams([s, s2]); function assert(err) { expect(err.message).toEqual('stahp!'); done(); } pipe([ streams, concat() ], assert); }); it('emits received data before a stream errors downstream', function (done) { var s = fromOnce(function(size, next) { setTimeout(function () { next(new Error('stahp!')); }, 500); }); var s2 = from.obj([{value: 'Im ok!'}]); // Invert the order to emit data first var streams = new OrderedStreams([s2, s]); function assertData(chunk, enc, next) { expect(chunk).toEqual({value: 'Im ok!'}); next(); } function assertErr(err) { expect(err.message).toEqual('stahp!'); done(); } pipe([ streams, to.obj(assertData) ], assertErr); }); });