2010-10-11 02:21:36 +02:00
|
|
|
var events = require('events');
|
2010-10-11 23:04:09 +02:00
|
|
|
var util = require('util');
|
2010-10-11 02:21:36 +02:00
|
|
|
|
2010-12-02 03:07:20 +01:00
|
|
|
function Stream() {
|
2010-10-11 02:21:36 +02:00
|
|
|
events.EventEmitter.call(this);
|
|
|
|
}
|
2010-10-11 23:04:09 +02:00
|
|
|
util.inherits(Stream, events.EventEmitter);
|
2010-10-11 02:21:36 +02:00
|
|
|
exports.Stream = Stream;
|
|
|
|
|
2010-12-17 22:56:47 +01:00
|
|
|
Stream.prototype.pipe = function(dest /* options, filter */) {
|
2010-10-11 02:21:36 +02:00
|
|
|
var source = this;
|
|
|
|
|
2010-12-17 22:56:47 +01:00
|
|
|
// parse arguments
|
|
|
|
var options, filter;
|
|
|
|
if (typeof arguments[1] == 'object') {
|
|
|
|
options = arguments[1];
|
|
|
|
filter = arguments[2];
|
|
|
|
} else {
|
|
|
|
filter = arguments[1];
|
|
|
|
}
|
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
function ondata(chunk) {
|
2010-12-17 22:56:47 +01:00
|
|
|
// FIXME shouldn't need to test writable - this is working around bug.
|
|
|
|
// .writable should not change before a 'end' event is fired.
|
2010-11-24 03:30:52 +01:00
|
|
|
if (dest.writable) {
|
|
|
|
if (false === dest.write(chunk)) source.pause();
|
|
|
|
}
|
2010-11-21 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2010-12-17 22:56:47 +01:00
|
|
|
if (!filter) {
|
|
|
|
source.on('data', ondata);
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// TODO: needs tests
|
|
|
|
//
|
|
|
|
var wait = false;
|
|
|
|
var waitQueue = [];
|
|
|
|
|
|
|
|
function done () {
|
|
|
|
wait = false;
|
|
|
|
// Drain the waitQueue
|
|
|
|
if (dest.writable && waitQueue.length) {
|
|
|
|
wait = true;
|
|
|
|
filter(waitQueue.shift(), ondata, done);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
source.on('data', function (d) {
|
|
|
|
if (wait) {
|
|
|
|
waitQueue.push(d);
|
|
|
|
source.pause();
|
|
|
|
} else {
|
|
|
|
wait = true;
|
|
|
|
filter(d, ondata, done);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-10-11 02:21:36 +02:00
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
function ondrain() {
|
2010-10-11 02:21:36 +02:00
|
|
|
if (source.readable) source.resume();
|
2010-11-21 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
dest.on('drain', ondrain);
|
2010-10-11 02:21:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the 'end' option is not supplied, dest.end() will be called when
|
|
|
|
* source gets the 'end' event.
|
|
|
|
*/
|
|
|
|
|
2010-10-11 07:05:49 +02:00
|
|
|
if (!options || options.end !== false) {
|
2010-12-02 05:59:06 +01:00
|
|
|
function onend() {
|
2010-10-11 02:21:36 +02:00
|
|
|
dest.end();
|
2010-11-21 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
source.on('end', onend);
|
2010-10-11 02:21:36 +02:00
|
|
|
}
|
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
dest.on('close', function() {
|
2010-11-24 03:30:52 +01:00
|
|
|
source.removeListener('data', ondata);
|
2010-11-21 08:08:45 +01:00
|
|
|
dest.removeListener('drain', ondrain);
|
2010-11-24 03:30:52 +01:00
|
|
|
source.removeListener('end', onend);
|
2010-11-21 08:08:45 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2010-10-11 02:21:36 +02:00
|
|
|
/*
|
|
|
|
* Questionable:
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!source.pause) {
|
2010-12-02 05:59:06 +01:00
|
|
|
source.pause = function() {
|
|
|
|
source.emit('pause');
|
2010-10-11 02:21:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!source.resume) {
|
2010-12-02 05:59:06 +01:00
|
|
|
source.resume = function() {
|
|
|
|
source.emit('resume');
|
2010-10-11 02:21:36 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
dest.on('pause', function() {
|
2010-10-11 02:21:36 +02:00
|
|
|
source.pause();
|
|
|
|
});
|
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
dest.on('resume', function() {
|
2010-10-11 02:21:36 +02:00
|
|
|
if (source.readable) source.resume();
|
|
|
|
});
|
|
|
|
};
|