mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.0 KiB
55 lines
1.0 KiB
var events = require('events');
|
|
var inherits = require('sys').inherits;
|
|
|
|
function Stream () {
|
|
events.EventEmitter.call(this);
|
|
}
|
|
inherits(Stream, events.EventEmitter);
|
|
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.
|
|
*/
|
|
|
|
if (!options || options.end !== false) {
|
|
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();
|
|
});
|
|
};
|
|
|