Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var Transform = require('stream').Transform;
class StreamConcat extends Transform {
constructor(streams, options) {
super(options);
this.streams = streams;
this.canAddStream = true;
this.currentStream = null;
this.streamIndex = 0;
this.nextStream();
}
nextStream () {
this.currentStream = null;
if (Array.isArray(this.streams) && this.streamIndex < this.streams.length) {
this.currentStream = this.streams[this.streamIndex++];
} else if (typeof this.streams === 'function') {
this.canAddStream = false;
this.currentStream = self.streams();
}
if (this.currentStream === null) {
this.canAddStream = false;
this.push(null);
} else {
this.currentStream.pipe(this, {end: false});
this.currentStream.on('end', () => this.nextStream());
}
}
addStream (newStream) {
if (this.canAddStream)
this.streams.push(newStream);
else
this.emit('error', new Error('Can\'t add stream.'));
}
_transform (chunk, encoding, callback) {
callback(null, chunk);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment