0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/lib/stream.js

112 lines
2.8 KiB
JavaScript
Raw Normal View History

2011-03-10 09:54:52 +01:00
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2010-10-11 02:21:36 +02:00
var events = require('events');
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);
}
util.inherits(Stream, events.EventEmitter);
2010-10-11 02:21:36 +02:00
exports.Stream = Stream;
Stream.prototype.pipe = function(dest, options) {
2010-10-11 02:21:36 +02:00
var source = this;
2010-12-02 05:59:06 +01:00
function ondata(chunk) {
2010-11-24 03:30:52 +01:00
if (dest.writable) {
if (false === dest.write(chunk)) source.pause();
}
}
source.on('data', ondata);
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-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-12-02 05:59:06 +01:00
source.on('end', onend);
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
};
}
2011-03-28 20:19:44 +02:00
var onpause = function() {
2010-10-11 02:21:36 +02:00
source.pause();
2011-03-28 20:19:44 +02:00
}
2010-10-11 02:21:36 +02:00
2011-03-28 20:19:44 +02:00
dest.on('pause', onpause);
var onresume = function() {
2010-10-11 02:21:36 +02:00
if (source.readable) source.resume();
2011-03-28 20:19:44 +02:00
};
dest.on('resume', onresume);
var cleanup = function () {
source.removeListener('data', ondata);
dest.removeListener('drain', ondrain);
source.removeListener('end', onend);
dest.removeListener('pause', onpause);
dest.removeListener('resume', onresume);
source.removeListener('end', cleanup);
source.removeListener('close', cleanup);
dest.removeListener('end', cleanup);
dest.removeListener('close', cleanup);
}
source.on('end', cleanup);
source.on('close', cleanup);
dest.on('end', cleanup);
dest.on('close', cleanup);
2011-02-10 08:02:51 +01:00
dest.emit('pipe', source);
2010-10-11 02:21:36 +02:00
};