twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

transport.js (2147B)


      1 
      2 /**
      3  * Module dependencies.
      4  */
      5 
      6 var EventEmitter = require('events').EventEmitter;
      7 var parser = require('engine.io-parser');
      8 var util = require('util');
      9 var debug = require('debug')('engine:transport');
     10 
     11 /**
     12  * Expose the constructor.
     13  */
     14 
     15 module.exports = Transport;
     16 
     17 /**
     18  * Noop function.
     19  *
     20  * @api private
     21  */
     22 
     23 function noop () {}
     24 
     25 /**
     26  * Transport constructor.
     27  *
     28  * @param {http.IncomingMessage} request
     29  * @api public
     30  */
     31 
     32 function Transport (req) {
     33   this.readyState = 'open';
     34   this.discarded = false;
     35 }
     36 
     37 /**
     38  * Inherits from EventEmitter.
     39  */
     40 
     41 util.inherits(Transport, EventEmitter);
     42 
     43 /**
     44  * Flags the transport as discarded.
     45  *
     46  * @api private
     47  */
     48 
     49 Transport.prototype.discard = function () {
     50   this.discarded = true;
     51 };
     52 
     53 /**
     54  * Called with an incoming HTTP request.
     55  *
     56  * @param {http.IncomingMessage} request
     57  * @api private
     58  */
     59 
     60 Transport.prototype.onRequest = function (req) {
     61   debug('setting request');
     62   this.req = req;
     63 };
     64 
     65 /**
     66  * Closes the transport.
     67  *
     68  * @api private
     69  */
     70 
     71 Transport.prototype.close = function (fn) {
     72   if ('closed' === this.readyState || 'closing' === this.readyState) return;
     73 
     74   this.readyState = 'closing';
     75   this.doClose(fn || noop);
     76 };
     77 
     78 /**
     79  * Called with a transport error.
     80  *
     81  * @param {String} message error
     82  * @param {Object} error description
     83  * @api private
     84  */
     85 
     86 Transport.prototype.onError = function (msg, desc) {
     87   if (this.listeners('error').length) {
     88     var err = new Error(msg);
     89     err.type = 'TransportError';
     90     err.description = desc;
     91     this.emit('error', err);
     92   } else {
     93     debug('ignored transport error %s (%s)', msg, desc);
     94   }
     95 };
     96 
     97 /**
     98  * Called with parsed out a packets from the data stream.
     99  *
    100  * @param {Object} packet
    101  * @api private
    102  */
    103 
    104 Transport.prototype.onPacket = function (packet) {
    105   this.emit('packet', packet);
    106 };
    107 
    108 /**
    109  * Called with the encoded packet data.
    110  *
    111  * @param {String} data
    112  * @api private
    113  */
    114 
    115 Transport.prototype.onData = function (data) {
    116   this.onPacket(parser.decodePacket(data));
    117 };
    118 
    119 /**
    120  * Called upon transport close.
    121  *
    122  * @api private
    123  */
    124 
    125 Transport.prototype.onClose = function () {
    126   this.readyState = 'closed';
    127   this.emit('close');
    128 };