-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
48 lines (39 loc) · 1015 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var PauseStream = require('pause-stream');
module.exports = function(input) {
var streams;
if(input instanceof Array){
streams = input;
}
else {
streams = arguments;
}
streams = Array.prototype.slice.call(streams);
var pending = streams.length;
var stream = new PauseStream();
var buffers = new Array(pending),
ended = new Array(pending);
streams.forEach(function(e, i) {
// normalize for badly behaved streams
var pauseStream = new PauseStream();
e.pipe(pauseStream);
pauseStream.pause();
streams[i] = pauseStream;
});
function next() {
var e = streams.shift();
e.pipe(stream, {end: false});
e.on('end', finish);
e.resume();
function finish() {
if(!--pending) return stream.emit('end');
next();
}
}
var originalDestroy = stream.destroy;
stream.destroy = function() {
streams.forEach(function(e) { if(e.destroy) e.destroy(); });
originalDestroy.call(stream);
};
next();
return stream;
};