0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 23:43:09 +01:00
nodejs/lib/stream.js

58 lines
1.1 KiB
JavaScript
Raw Normal View History

2010-10-11 02:21:36 +02:00
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.
*/
options.end = options && options.end === false ? false : true;
if (options.end) {
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();
});
};