示例#1
0
  'test pause()': function(defer){
    var calls = 0
      , data = []
      , req = new Stream;

    req.write = function(data){
      this.emit('data', data);
    };
    req.end = function(){
      this.emit('end');
    };

    var pause = utils.pause(req);

    req.write('one');
    req.write('two');
    req.end();

    req.on('data', function(chunk){
      ++calls;
      data.push(chunk);
    });
    req.on('end', function(){
      ++calls;
      data.should.have.length(2);
    });

    pause.resume();

    defer(function(){
      calls.should.equal(3);
    });
  },
 res.on = function(type, fn){
   if ('data' == type || 'end' == type) {
     decodedStream.on(type, fn);
   } else {
     _on.call(res, type, fn);
   }
 };
示例#3
0
    function upload(track) {
      clearTimeout(track.timeout);
      // if it takes more than ten minutes for the next chunk to
      // arrive, remove the temp file and consider this a failed upload.
      track.timeout = setTimeout(function() {
        delete handler.server.chunkedUploads[path];
        track.stream.emit("error", "Upload timed out");
        track.stream.end();
      }, 600000); //10 minutes timeout

      var stream = new Stream();
      stream.writable = true;

      stream.write = function(data) {
        track.length += data.length;
        track.stream.write(data);
      }

      stream.on("error", function(err) {
        track.stream.emit("error", err);
      });

      stream.end = function() {
        clearTimeout(track.timeout);
        if (track.length == parseInt(size, 10)) {
          delete handler.server.chunkedUploads[path];
          track.stream.end();
          handler.dispatchEvent("afterBind", handler.httpRequest.url, self.path + "/" + filename);
        }

        this.emit("close");
      };

      handler.getRequestBody(type, stream, false, callback);
    }
示例#4
0
es.readable = function (func, continueOnError) {
  var stream = new Stream()
    , i = 0
    , paused = false
    , ended = false
    , reading = false

  stream.readable = true  
  stream.writable = false
 
  if('function' !== typeof func)
    throw new Error('event-stream.readable expects async function')
  
  stream.on('end', function () { ended = true })
  
  function get (err, data) {
    
    if(err) {
      stream.emit('error', err)
      if(!continueOnError) stream.emit('end')
    } else if (arguments.length > 1)
      stream.emit('data', data)

    process.nextTick(function () {
      if(ended || paused || reading) return
      try {
        reading = true
        func.call(stream, i++, function () {
          reading = false
          get.apply(null, arguments)
        })
      } catch (err) {
        stream.emit('error', err)    
      }
    })
  
  }
  stream.resume = function () {
    paused = false
    get()
  }
  process.nextTick(get)
  stream.pause = function () {
     paused = true
  }
  stream.destroy = function () {
    stream.emit('end')
    stream.emit('close')
    ended = true
  }
  return stream
}
示例#5
0
 es.readable = function(func, continueOnError) {
   var stream = new Stream(),
       i = 0,
       paused = false,
       ended = false,
       reading = false;
   stream.readable = true;
   stream.writable = false;
   if ('function' !== typeof func)
     throw new Error('event-stream.readable expects async function');
   stream.on('end', function() {
     ended = true;
   });
   function get(err, data) {
     if (err) {
       stream.emit('error', err);
       if (!continueOnError)
         stream.emit('end');
     } else if (arguments.length > 1)
       stream.emit('data', data);
     immediately(function() {
       if (ended || paused || reading)
         return;
       try {
         reading = true;
         func.call(stream, i++, function() {
           reading = false;
           get.apply(null, arguments);
         });
       } catch (err) {
         stream.emit('error', err);
       }
     });
   }
   stream.resume = function() {
     paused = false;
     get();
   };
   process.nextTick(get);
   stream.pause = function() {
     paused = true;
   };
   stream.destroy = function() {
     stream.emit('end');
     stream.emit('close');
     ended = true;
   };
   return stream;
 };
(function testErrorListenerCatches() {
  var source = new Stream();
  var dest = new Stream();

  source.pipe(dest);

  var gotErr = null;
  source.on('error', function(err) {
    gotErr = err;
  });

  var err = new Error('This stream turned into bacon.');
  source.emit('error', err);
  assert.strictEqual(gotErr, err);
})();
exports['check-both-parent-and-streamstack-get-data-event'] = function() {
  var stream = new Stream();
  var stack = new StreamStack(stream);
  
  var gotParentData = false;
  stream.on('data', function() {
    gotParentData = true;
  });
  var gotStackData = false;
  stack.on('data', function() {
    gotStackData = true;
  });
  
  stream.emit('data', new Buffer('Hello World!'));
  
  assert.equal(true, gotParentData);
  assert.equal(true, gotStackData);
}
exports['check-with-empty-streamstack-data-handler'] = function() {
  var stream = new Stream();
  var stack = new StreamStack(stream, {
    data: function() {}
  });
  
  var gotParentData = false;
  stream.on('data', function() {
    gotParentData = true;
  });
  var gotStackData = false;
  stack.on('data', function() {
    gotStackData = true;
  });
  
  stream.emit('data', new Buffer('Hello World!'));
  
  assert.equal(true, gotParentData);
  assert.equal(false, gotStackData);
}
示例#9
0
function chooseIn(list, propertyName, callback) {
    var next = function() {
        process.stdout.write('enter your choice: ');
        utils.readInput(function(v) {
            var index = parseInt(v, 10);
            if (list.length > index) callback(index, list[index]);
            else chooseIn(list, propertyName, callback);
        });
    }
    var stream = new Stream();
    pump(stream, process.stdout);
    if (usePager(list.length)) {
        var pager = utils.pipe(process.env.PAGER || "less", []);
        stream.pipe(pager.stdin);
        pager.on('exit', next);
    } else {
        stream.on('close', next);
    }
    formatListIn(stream, list, propertyName);
    stream.emit('close');
}
示例#10
0
  createReadStream: function createReadStream() {
    var req = this;
    var stream = new Stream();

    stream.readable = true;
    stream.on('newListener', function(event) {
      if (event === 'data') {
        process.nextTick(function() { req.send(); });
      }
    });

    this.on('httpHeaders', function(statusCode) {
      if (statusCode < 300) {
        // Remove httpData, httpError listeners, add streaming listeners
        req.removeListener('httpData', AWS.EventListeners.Core.HTTP_DATA);
        req.removeListener('httpError', AWS.EventListeners.Core.HTTP_ERROR);

        req.on('httpData', function(data) {
          stream.emit('data', data);
        });

        req.on('httpDone', function() {
          stream.emit('end');
          stream.readable = false;
        });

        req.on('httpError', function(error, resp) {
          resp.error = error;
          resp.error.retryable = false;
          this.completeRequest(resp);
        });
      }
    });

    this.on('error', function(err) {
      stream.emit('error', err);
    });

    return stream;
  },
示例#11
0
test('cycles', function (t) {
  outstr.on('end', function () {
    output.forEach(function (o, i) {
      // Drop variable parts for comparison.
      delete o.hostname;
      delete o.pid;
      delete o.time;
      // Hack object/dict comparison: JSONify.
      t.equal(JSON.stringify(o), JSON.stringify(expect[i]),
        'log item ' + i + ' matches');
    });
    t.end();
  });

  var obj = { bang: 'boom' };
  obj.KABOOM = obj;
  log.info('bango', obj);
  log.info('kaboom', obj.KABOOM);
  log.info(obj);
  outstr.end();
  t.ok('did not throw');
});
'use strict';
const common = require('../common');
const assert = require('assert');
const Stream = require('stream').Stream;

{
  const source = new Stream();
  const dest = new Stream();

  source.pipe(dest);

  let gotErr = null;
  source.on('error', function(err) {
    gotErr = err;
  });

  const err = new Error('This stream turned into bacon.');
  source.emit('error', err);
  assert.strictEqual(gotErr, err);
}

{
  const source = new Stream();
  const dest = new Stream();

  source.pipe(dest);

  const err = new Error('This stream turned into bacon.');

  let gotErr = null;
  try {
示例#13
0
module.exports = function (write, end, error) {

    write = write || function (data) { this.emit('data', data) }
    end = end || function () { this.emit('end') }

    var ended = false, destroyed = false
    var stream = new Stream(), buffer = []
    stream.buffer = buffer
    stream.readable = stream.writable = true
    stream.paused = false  
    stream.write = function (data) {
      if(!data){ 
        this.emit('data'); 
      }else{
        write.call(this, data)
      }
      return !stream.paused
    }
    
    function drain() {
      while(buffer.length && !stream.paused) {
        var data = buffer.shift()
        if(null === data)
          return stream.emit('end')
        else
          stream.emit('data', data)
      }
    }

    stream.queue = function (data) {
      buffer.push(data)
      drain()
    }

    //this will be registered as the first 'end' listener
    //must call destroy next tick, to make sure we're after any
    //stream piped from here. 
    //this is only a problem if end is not emitted synchronously.
    //a nicer way to do this is to make sure this is the last listener for 'end'


    stream.on('end', function () {
      stream.readable = false
      if(!stream.writable)
        process.nextTick(function () {
          stream.destroy()
        })
    })

    function _end () {
      stream.writable = false
      end.call(stream)
      if(!stream.readable)
        stream.destroy()
    }

    stream.end = function (data) {
      if(ended) return 
      //this breaks, because pipe doesn't check writable before calling end.
      //throw new Error('cannot call end twice')
      ended = true
      if(arguments.length) stream.write(data)
      if(!buffer.length) _end()
    }

    stream.destroy = function () {
      if(destroyed) return
      destroyed = true
      ended = true
      buffer.length = 0
      stream.writable = stream.readable = false
      stream.emit('close')
    }

    stream.pause = function () {
      if(stream.paused) return
      stream.paused = true
      stream.emit('pause')
    }
    stream.wait = function () {
      if(stream.paused) return
      stream.paused = true
  //    stream.emit('pause')
    }
    
    stream.resume = function () {
      if(stream.paused) {
        stream.paused = false
      }
      drain()
      //may have become paused again,
      //as drain emits 'data'.
      if(!stream.paused)
        stream.emit('drain')
    }


    /**********ADDED BY BRANDON*************/
    var streamler = stream.streamler = this.streamler,
        metric = stream.metric = this.metric,
        channelname = stream.channelname = this.channelname,
        parentmap = stream.parentmap = this.parentmap,
        routemap = stream.routemap = this.routemap,
        root = stream.root = this.root ? streamler.merge(this.root, {count:0}) : {count:0},
        _start = stream._start = this._start;


    if(error && error.length){
      error.forEach(function(err){
        stream.on("error", err);
      });
    }

    stream.setMaxListeners(0);

    stream.filter = stream.filt = stream.link = function(type, options){
      var ret;
      if( options && options.hasOwnProperty("condition") && !options.condition )
        ret = this;
      else if( this.streamler.filters[metric] && typeof this.streamler.filters[metric][type] === "function" )
        ret = this.pipe( this.streamler.filters[metric][type].call( this, options ) );
      else if( typeof this.streamler.filters.global[type] === "function" )
        ret = this.pipe( this.streamler.filters.global[type].call( this, options ) );
      else {
        console.log("broken filter! metric not correct OR filter doesnt exist "+this.metric+type); process.exit();
      }
      return ret;
    }

    stream.dumper = stream.dump = function(type, options){
      if( options && options.hasOwnProperty("condition") && !options.condition )
        return this;
      else if( streamler.dumpers[metric] && typeof streamler.dumpers[metric][type] === "function" )
        this.pipe( streamler.dumpers[metric][type].call( this, options ) );
      else if( typeof streamler.dumpers.global[type] === "function" )
        this.pipe( streamler.dumpers.global[type].call( this, options ) );
      else {
        console.log("broken dumper!"+metric+type); process.exit();
      }
      return this;
    }

    stream.done = function(options){
      this.pipe( streamler.channelDone.apply(this, arguments) );
    }

    stream.convert = function( toconvert ){
      var me = toconvert;
      me.streamler = streamler;
      me.channelname = channelname;
      me.routemap = routemap;
      me.root = root;
      me.metric = metric;
      me._start = _start;
      toconvert.filter = toconvert.filt = toconvert.link = stream.filt.bind( me );
      toconvert.dumper = toconvert.dump = stream.dump.bind( me );
      toconvert.done = stream.done.bind( me );
      return me;
    }

    return stream
}
示例#14
0
 error.forEach(function(err){
   stream.on("error", err);
 });
'use strict';
const common = require('../common');
const stream = require('stream');

const r = new stream.Stream();
r.listenerCount = undefined;

const w = new stream.Stream();
w.listenerCount = undefined;

w.on('pipe', function() {
  r.emit('error', new Error('Readable Error'));
  w.emit('error', new Error('Writable Error'));
});
r.on('error', common.mustCall(noop));
w.on('error', common.mustCall(noop));
r.pipe(w);

function noop() {}