2010-10-11 08:21:36 +08:00
|
|
|
var events = require('events');
|
2010-10-12 05:04:09 +08:00
|
|
|
var util = require('util');
|
2010-10-11 08:21:36 +08:00
|
|
|
|
2010-12-02 10:07:20 +08:00
|
|
|
function Stream() {
|
2010-10-11 08:21:36 +08:00
|
|
|
events.EventEmitter.call(this);
|
|
|
|
}
|
2010-10-12 05:04:09 +08:00
|
|
|
util.inherits(Stream, events.EventEmitter);
|
2010-10-11 08:21:36 +08:00
|
|
|
exports.Stream = Stream;
|
|
|
|
|
2011-01-02 08:41:39 +08:00
|
|
|
Stream.prototype.pipe = function(dest, options) {
|
2010-10-11 08:21:36 +08:00
|
|
|
var source = this;
|
|
|
|
|
2010-12-02 12:59:06 +08:00
|
|
|
function ondata(chunk) {
|
2010-11-24 10:30:52 +08:00
|
|
|
if (dest.writable) {
|
|
|
|
if (false === dest.write(chunk)) source.pause();
|
|
|
|
}
|
2010-11-21 15:08:45 +08:00
|
|
|
}
|
|
|
|
|
2011-01-02 08:41:39 +08:00
|
|
|
source.on('data', ondata);
|
2010-10-11 08:21:36 +08:00
|
|
|
|
2010-12-02 12:59:06 +08:00
|
|
|
function ondrain() {
|
2010-10-11 08:21:36 +08:00
|
|
|
if (source.readable) source.resume();
|
2010-11-21 15:08:45 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 12:59:06 +08:00
|
|
|
dest.on('drain', ondrain);
|
2010-10-11 08:21:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the 'end' option is not supplied, dest.end() will be called when
|
|
|
|
* source gets the 'end' event.
|
|
|
|
*/
|
|
|
|
|
2010-10-11 13:05:49 +08:00
|
|
|
if (!options || options.end !== false) {
|
2010-12-02 12:59:06 +08:00
|
|
|
function onend() {
|
2010-10-11 08:21:36 +08:00
|
|
|
dest.end();
|
2010-11-21 15:08:45 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 12:59:06 +08:00
|
|
|
source.on('end', onend);
|
2010-10-11 08:21:36 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 12:59:06 +08:00
|
|
|
dest.on('close', function() {
|
2010-11-24 10:30:52 +08:00
|
|
|
source.removeListener('data', ondata);
|
2010-11-21 15:08:45 +08:00
|
|
|
dest.removeListener('drain', ondrain);
|
2010-11-24 10:30:52 +08:00
|
|
|
source.removeListener('end', onend);
|
2010-11-21 15:08:45 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2010-10-11 08:21:36 +08:00
|
|
|
/*
|
|
|
|
* Questionable:
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!source.pause) {
|
2010-12-02 12:59:06 +08:00
|
|
|
source.pause = function() {
|
|
|
|
source.emit('pause');
|
2010-10-11 08:21:36 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!source.resume) {
|
2010-12-02 12:59:06 +08:00
|
|
|
source.resume = function() {
|
|
|
|
source.emit('resume');
|
2010-10-11 08:21:36 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-12-02 12:59:06 +08:00
|
|
|
dest.on('pause', function() {
|
2010-10-11 08:21:36 +08:00
|
|
|
source.pause();
|
|
|
|
});
|
|
|
|
|
2010-12-02 12:59:06 +08:00
|
|
|
dest.on('resume', function() {
|
2010-10-11 08:21:36 +08:00
|
|
|
if (source.readable) source.resume();
|
|
|
|
});
|
|
|
|
};
|