twitst4tz

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

transport.js (2929B)


      1 /**
      2  * Module dependencies.
      3  */
      4 
      5 var parser = require('engine.io-parser');
      6 var Emitter = require('component-emitter');
      7 
      8 /**
      9  * Module exports.
     10  */
     11 
     12 module.exports = Transport;
     13 
     14 /**
     15  * Transport abstract constructor.
     16  *
     17  * @param {Object} options.
     18  * @api private
     19  */
     20 
     21 function Transport (opts) {
     22   this.path = opts.path;
     23   this.hostname = opts.hostname;
     24   this.port = opts.port;
     25   this.secure = opts.secure;
     26   this.query = opts.query;
     27   this.timestampParam = opts.timestampParam;
     28   this.timestampRequests = opts.timestampRequests;
     29   this.readyState = '';
     30   this.agent = opts.agent || false;
     31   this.socket = opts.socket;
     32   this.enablesXDR = opts.enablesXDR;
     33   this.withCredentials = opts.withCredentials;
     34 
     35   // SSL options for Node.js client
     36   this.pfx = opts.pfx;
     37   this.key = opts.key;
     38   this.passphrase = opts.passphrase;
     39   this.cert = opts.cert;
     40   this.ca = opts.ca;
     41   this.ciphers = opts.ciphers;
     42   this.rejectUnauthorized = opts.rejectUnauthorized;
     43   this.forceNode = opts.forceNode;
     44 
     45   // results of ReactNative environment detection
     46   this.isReactNative = opts.isReactNative;
     47 
     48   // other options for Node.js client
     49   this.extraHeaders = opts.extraHeaders;
     50   this.localAddress = opts.localAddress;
     51 }
     52 
     53 /**
     54  * Mix in `Emitter`.
     55  */
     56 
     57 Emitter(Transport.prototype);
     58 
     59 /**
     60  * Emits an error.
     61  *
     62  * @param {String} str
     63  * @return {Transport} for chaining
     64  * @api public
     65  */
     66 
     67 Transport.prototype.onError = function (msg, desc) {
     68   var err = new Error(msg);
     69   err.type = 'TransportError';
     70   err.description = desc;
     71   this.emit('error', err);
     72   return this;
     73 };
     74 
     75 /**
     76  * Opens the transport.
     77  *
     78  * @api public
     79  */
     80 
     81 Transport.prototype.open = function () {
     82   if ('closed' === this.readyState || '' === this.readyState) {
     83     this.readyState = 'opening';
     84     this.doOpen();
     85   }
     86 
     87   return this;
     88 };
     89 
     90 /**
     91  * Closes the transport.
     92  *
     93  * @api private
     94  */
     95 
     96 Transport.prototype.close = function () {
     97   if ('opening' === this.readyState || 'open' === this.readyState) {
     98     this.doClose();
     99     this.onClose();
    100   }
    101 
    102   return this;
    103 };
    104 
    105 /**
    106  * Sends multiple packets.
    107  *
    108  * @param {Array} packets
    109  * @api private
    110  */
    111 
    112 Transport.prototype.send = function (packets) {
    113   if ('open' === this.readyState) {
    114     this.write(packets);
    115   } else {
    116     throw new Error('Transport not open');
    117   }
    118 };
    119 
    120 /**
    121  * Called upon open
    122  *
    123  * @api private
    124  */
    125 
    126 Transport.prototype.onOpen = function () {
    127   this.readyState = 'open';
    128   this.writable = true;
    129   this.emit('open');
    130 };
    131 
    132 /**
    133  * Called with data.
    134  *
    135  * @param {String} data
    136  * @api private
    137  */
    138 
    139 Transport.prototype.onData = function (data) {
    140   var packet = parser.decodePacket(data, this.socket.binaryType);
    141   this.onPacket(packet);
    142 };
    143 
    144 /**
    145  * Called with a decoded packet.
    146  */
    147 
    148 Transport.prototype.onPacket = function (packet) {
    149   this.emit('packet', packet);
    150 };
    151 
    152 /**
    153  * Called upon close.
    154  *
    155  * @api private
    156  */
    157 
    158 Transport.prototype.onClose = function () {
    159   this.readyState = 'closed';
    160   this.emit('close');
    161 };