node/lib/stream.js

56 lines
1.0 KiB
JavaScript
Raw Normal View History

2010-10-11 08:21:36 +08:00
var events = require('events');
var util = require('util');
2010-10-11 08:21:36 +08:00
function Stream () {
events.EventEmitter.call(this);
}
util.inherits(Stream, events.EventEmitter);
2010-10-11 08:21:36 +08:00
exports.Stream = Stream;
Stream.prototype.pipe = function (dest, options) {
var source = this;
source.on("data", function (chunk) {
if (false === dest.write(chunk)) source.pause();
});
dest.on("drain", function () {
if (source.readable) source.resume();
});
/*
* 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-10-11 08:21:36 +08:00
source.on("end", function () {
dest.end();
});
}
/*
* Questionable:
*/
if (!source.pause) {
source.pause = function () {
source.emit("pause");
};
}
if (!source.resume) {
source.resume = function () {
source.emit("resume");
};
}
dest.on("pause", function () {
source.pause();
});
dest.on("resume", function () {
if (source.readable) source.resume();
});
};