2014-11-22 23:59:48 +08:00
|
|
|
'use strict';
|
|
|
|
|
2012-10-03 06:44:50 +08:00
|
|
|
module.exports = Stream;
|
|
|
|
|
2015-01-22 00:36:59 +08:00
|
|
|
const EE = require('events').EventEmitter;
|
|
|
|
const util = require('util');
|
2010-10-11 08:21:36 +08:00
|
|
|
|
2013-02-14 16:48:11 +08:00
|
|
|
util.inherits(Stream, EE);
|
2012-10-03 06:44:50 +08:00
|
|
|
Stream.Readable = require('_stream_readable');
|
|
|
|
Stream.Writable = require('_stream_writable');
|
|
|
|
Stream.Duplex = require('_stream_duplex');
|
|
|
|
Stream.Transform = require('_stream_transform');
|
|
|
|
Stream.PassThrough = require('_stream_passthrough');
|
|
|
|
|
2011-10-25 05:52:10 +08:00
|
|
|
// Backwards-compat with node 0.4.x
|
|
|
|
Stream.Stream = Stream;
|
2010-10-11 08:21:36 +08:00
|
|
|
|
2012-10-03 06:44:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
// old-style streams. Note that the pipe method (the only relevant
|
|
|
|
// part of this class) is overridden in the Readable class.
|
|
|
|
|
|
|
|
function Stream() {
|
2013-02-14 16:48:11 +08:00
|
|
|
EE.call(this);
|
2012-10-03 06:44:50 +08:00
|
|
|
}
|
|
|
|
|
2011-01-02 08:41:39 +08:00
|
|
|
Stream.prototype.pipe = function(dest, options) {
|
2010-10-11 08:21:36 +08:00
|
|
|
var source = this;
|
|
|
|
|
2010-12-02 12:59:06 +08:00
|
|
|
function ondata(chunk) {
|
2010-11-24 10:30:52 +08:00
|
|
|
if (dest.writable) {
|
2011-10-25 02:55:02 +08:00
|
|
|
if (false === dest.write(chunk) && source.pause) {
|
|
|
|
source.pause();
|
|
|
|
}
|
2010-11-24 10:30:52 +08:00
|
|
|
}
|
2010-11-21 15:08:45 +08:00
|
|
|
}
|
|
|
|
|
2011-01-02 08:41:39 +08:00
|
|
|
source.on('data', ondata);
|
2010-10-11 08:21:36 +08:00
|
|
|
|
2010-12-02 12:59:06 +08:00
|
|
|
function ondrain() {
|
2011-10-25 02:55:02 +08:00
|
|
|
if (source.readable && source.resume) {
|
|
|
|
source.resume();
|
|
|
|
}
|
2010-11-21 15:08:45 +08:00
|
|
|
}
|
|
|
|
|
2010-12-02 12:59:06 +08:00
|
|
|
dest.on('drain', ondrain);
|
2010-10-11 08:21:36 +08:00
|
|
|
|
2011-04-28 02:10:10 +08:00
|
|
|
// If the 'end' option is not supplied, dest.end() will be called when
|
2011-11-22 05:57:33 +08:00
|
|
|
// source gets the 'end' or 'close' events. Only dest.end() once.
|
2011-11-11 06:51:16 +08:00
|
|
|
if (!dest._isStdio && (!options || options.end !== false)) {
|
2011-04-28 02:10:10 +08:00
|
|
|
source.on('end', onend);
|
2011-05-03 03:13:06 +08:00
|
|
|
source.on('close', onclose);
|
2011-04-28 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var didOnEnd = false;
|
|
|
|
function onend() {
|
|
|
|
if (didOnEnd) return;
|
|
|
|
didOnEnd = true;
|
|
|
|
|
|
|
|
dest.end();
|
|
|
|
}
|
|
|
|
|
2011-05-03 03:13:06 +08:00
|
|
|
|
|
|
|
function onclose() {
|
|
|
|
if (didOnEnd) return;
|
|
|
|
didOnEnd = true;
|
|
|
|
|
2015-01-29 09:05:53 +08:00
|
|
|
if (typeof dest.destroy === 'function') dest.destroy();
|
2011-05-03 03:13:06 +08:00
|
|
|
}
|
|
|
|
|
2011-04-28 02:10:10 +08:00
|
|
|
// don't leave dangling pipes when there are errors.
|
|
|
|
function onerror(er) {
|
|
|
|
cleanup();
|
2013-02-14 16:48:11 +08:00
|
|
|
if (EE.listenerCount(this, 'error') === 0) {
|
2011-04-28 02:10:10 +08:00
|
|
|
throw er; // Unhandled stream error in pipe.
|
|
|
|
}
|
2010-10-11 08:21:36 +08:00
|
|
|
}
|
|
|
|
|
2011-04-28 02:10:10 +08:00
|
|
|
source.on('error', onerror);
|
|
|
|
dest.on('error', onerror);
|
2010-10-11 08:21:36 +08:00
|
|
|
|
2011-04-28 02:10:10 +08:00
|
|
|
// remove all the event listeners that were added.
|
|
|
|
function cleanup() {
|
2011-03-29 02:19:44 +08:00
|
|
|
source.removeListener('data', ondata);
|
|
|
|
dest.removeListener('drain', ondrain);
|
2011-04-28 02:10:10 +08:00
|
|
|
|
2011-03-29 02:19:44 +08:00
|
|
|
source.removeListener('end', onend);
|
2011-05-03 03:13:06 +08:00
|
|
|
source.removeListener('close', onclose);
|
2011-04-15 02:33:54 +08:00
|
|
|
|
2011-04-28 02:10:10 +08:00
|
|
|
source.removeListener('error', onerror);
|
|
|
|
dest.removeListener('error', onerror);
|
|
|
|
|
2011-03-29 02:19:44 +08:00
|
|
|
source.removeListener('end', cleanup);
|
|
|
|
source.removeListener('close', cleanup);
|
2011-04-15 02:33:54 +08:00
|
|
|
|
2011-03-29 02:19:44 +08:00
|
|
|
dest.removeListener('close', cleanup);
|
|
|
|
}
|
2011-04-15 02:33:54 +08:00
|
|
|
|
2011-03-29 02:19:44 +08:00
|
|
|
source.on('end', cleanup);
|
|
|
|
source.on('close', cleanup);
|
|
|
|
|
|
|
|
dest.on('close', cleanup);
|
2011-02-10 15:02:51 +08:00
|
|
|
|
|
|
|
dest.emit('pipe', source);
|
2011-07-11 13:35:25 +08:00
|
|
|
|
|
|
|
// Allow for unix-like usage: A.pipe(B).pipe(C)
|
|
|
|
return dest;
|
2010-10-11 08:21:36 +08:00
|
|
|
};
|