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');
|
2010-10-11 23:04:09 +02:00
|
|
|
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);
|
|
|
|
}
|
2010-10-11 23:04:09 +02:00
|
|
|
util.inherits(Stream, events.EventEmitter);
|
2011-10-24 23:52:10 +02:00
|
|
|
module.exports = Stream;
|
|
|
|
// Backwards-compat with node 0.4.x
|
|
|
|
Stream.Stream = Stream;
|
2010-10-11 02:21:36 +02:00
|
|
|
|
2011-01-02 01:41:39 +01:00
|
|
|
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) {
|
2011-10-24 20:55:02 +02:00
|
|
|
if (false === dest.write(chunk) && source.pause) {
|
|
|
|
source.pause();
|
|
|
|
}
|
2010-11-24 03:30:52 +01:00
|
|
|
}
|
2010-11-21 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2011-01-02 01:41:39 +01:00
|
|
|
source.on('data', ondata);
|
2010-10-11 02:21:36 +02:00
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
function ondrain() {
|
2011-10-24 20:55:02 +02:00
|
|
|
if (source.readable && source.resume) {
|
|
|
|
source.resume();
|
|
|
|
}
|
2010-11-21 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
dest.on('drain', ondrain);
|
2010-10-11 02:21:36 +02:00
|
|
|
|
2011-04-27 20:10:10 +02:00
|
|
|
// If the 'end' option is not supplied, dest.end() will be called when
|
|
|
|
// source gets the 'end' or 'close' events. Only dest.end() once, and
|
|
|
|
// only when all sources have ended.
|
2011-11-10 23:51:16 +01:00
|
|
|
if (!dest._isStdio && (!options || options.end !== false)) {
|
2011-04-27 20:10:10 +02:00
|
|
|
dest._pipeCount = dest._pipeCount || 0;
|
|
|
|
dest._pipeCount++;
|
|
|
|
|
|
|
|
source.on('end', onend);
|
2011-05-02 21:13:06 +02:00
|
|
|
source.on('close', onclose);
|
2011-04-27 20:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var didOnEnd = false;
|
|
|
|
function onend() {
|
|
|
|
if (didOnEnd) return;
|
|
|
|
didOnEnd = true;
|
|
|
|
|
|
|
|
dest._pipeCount--;
|
2011-04-14 20:33:54 +02:00
|
|
|
|
2011-04-27 20:10:10 +02:00
|
|
|
// remove the listeners
|
|
|
|
cleanup();
|
2011-04-14 20:33:54 +02:00
|
|
|
|
2011-04-27 20:10:10 +02:00
|
|
|
if (dest._pipeCount > 0) {
|
|
|
|
// waiting for other incoming streams to end.
|
|
|
|
return;
|
2010-11-21 08:08:45 +01:00
|
|
|
}
|
|
|
|
|
2011-04-27 20:10:10 +02:00
|
|
|
dest.end();
|
|
|
|
}
|
|
|
|
|
2011-05-02 21:13:06 +02:00
|
|
|
|
|
|
|
function onclose() {
|
|
|
|
if (didOnEnd) return;
|
|
|
|
didOnEnd = true;
|
|
|
|
|
|
|
|
dest._pipeCount--;
|
|
|
|
|
|
|
|
// remove the listeners
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
if (dest._pipeCount > 0) {
|
|
|
|
// waiting for other incoming streams to end.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dest.destroy();
|
|
|
|
}
|
|
|
|
|
2011-04-27 20:10:10 +02:00
|
|
|
// don't leave dangling pipes when there are errors.
|
|
|
|
function onerror(er) {
|
|
|
|
cleanup();
|
2011-05-23 11:02:53 +02:00
|
|
|
if (this.listeners('error').length === 0) {
|
2011-04-27 20:10:10 +02:00
|
|
|
throw er; // Unhandled stream error in pipe.
|
|
|
|
}
|
2010-10-11 02:21:36 +02:00
|
|
|
}
|
|
|
|
|
2011-04-27 20:10:10 +02:00
|
|
|
source.on('error', onerror);
|
|
|
|
dest.on('error', onerror);
|
2010-10-11 02:21:36 +02:00
|
|
|
|
2011-04-27 20:10:10 +02:00
|
|
|
// remove all the event listeners that were added.
|
|
|
|
function cleanup() {
|
2011-03-28 20:19:44 +02:00
|
|
|
source.removeListener('data', ondata);
|
|
|
|
dest.removeListener('drain', ondrain);
|
2011-04-27 20:10:10 +02:00
|
|
|
|
2011-03-28 20:19:44 +02:00
|
|
|
source.removeListener('end', onend);
|
2011-05-02 21:13:06 +02:00
|
|
|
source.removeListener('close', onclose);
|
2011-04-14 20:33:54 +02:00
|
|
|
|
2011-04-27 20:10:10 +02:00
|
|
|
source.removeListener('error', onerror);
|
|
|
|
dest.removeListener('error', onerror);
|
|
|
|
|
2011-03-28 20:19:44 +02:00
|
|
|
source.removeListener('end', cleanup);
|
|
|
|
source.removeListener('close', cleanup);
|
2011-04-14 20:33:54 +02:00
|
|
|
|
2011-03-28 20:19:44 +02:00
|
|
|
dest.removeListener('end', cleanup);
|
|
|
|
dest.removeListener('close', cleanup);
|
|
|
|
}
|
2011-04-14 20:33:54 +02:00
|
|
|
|
2011-03-28 20:19:44 +02:00
|
|
|
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);
|
2011-07-11 07:35:25 +02:00
|
|
|
|
|
|
|
// Allow for unix-like usage: A.pipe(B).pipe(C)
|
|
|
|
return dest;
|
2010-10-11 02:21:36 +02:00
|
|
|
};
|