github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/sockjs-client-1.1.0/lib/transport/receiver/htmlfile.js (about)

     1  'use strict';
     2  
     3  var inherits = require('inherits')
     4    , iframeUtils = require('../../utils/iframe')
     5    , urlUtils = require('../../utils/url')
     6    , EventEmitter = require('events').EventEmitter
     7    , random = require('../../utils/random')
     8    ;
     9  
    10  var debug = function() {};
    11  if (process.env.NODE_ENV !== 'production') {
    12    debug = require('debug')('sockjs-client:receiver:htmlfile');
    13  }
    14  
    15  function HtmlfileReceiver(url) {
    16    debug(url);
    17    EventEmitter.call(this);
    18    var self = this;
    19    iframeUtils.polluteGlobalNamespace();
    20  
    21    this.id = 'a' + random.string(6);
    22    url = urlUtils.addQuery(url, 'c=' + decodeURIComponent(iframeUtils.WPrefix + '.' + this.id));
    23  
    24    debug('using htmlfile', HtmlfileReceiver.htmlfileEnabled);
    25    var constructFunc = HtmlfileReceiver.htmlfileEnabled ?
    26        iframeUtils.createHtmlfile : iframeUtils.createIframe;
    27  
    28    global[iframeUtils.WPrefix][this.id] = {
    29      start: function() {
    30        debug('start');
    31        self.iframeObj.loaded();
    32      }
    33    , message: function(data) {
    34        debug('message', data);
    35        self.emit('message', data);
    36      }
    37    , stop: function() {
    38        debug('stop');
    39        self._cleanup();
    40        self._close('network');
    41      }
    42    };
    43    this.iframeObj = constructFunc(url, function() {
    44      debug('callback');
    45      self._cleanup();
    46      self._close('permanent');
    47    });
    48  }
    49  
    50  inherits(HtmlfileReceiver, EventEmitter);
    51  
    52  HtmlfileReceiver.prototype.abort = function() {
    53    debug('abort');
    54    this._cleanup();
    55    this._close('user');
    56  };
    57  
    58  HtmlfileReceiver.prototype._cleanup = function() {
    59    debug('_cleanup');
    60    if (this.iframeObj) {
    61      this.iframeObj.cleanup();
    62      this.iframeObj = null;
    63    }
    64    delete global[iframeUtils.WPrefix][this.id];
    65  };
    66  
    67  HtmlfileReceiver.prototype._close = function(reason) {
    68    debug('_close', reason);
    69    this.emit('close', null, reason);
    70    this.removeAllListeners();
    71  };
    72  
    73  HtmlfileReceiver.htmlfileEnabled = false;
    74  
    75  // obfuscate to avoid firewalls
    76  var axo = ['Active'].concat('Object').join('X');
    77  if (axo in global) {
    78    try {
    79      HtmlfileReceiver.htmlfileEnabled = !!new global[axo]('htmlfile');
    80    } catch (x) {
    81      // intentionally empty
    82    }
    83  }
    84  
    85  HtmlfileReceiver.enabled = HtmlfileReceiver.htmlfileEnabled || iframeUtils.iframeEnabled;
    86  
    87  module.exports = HtmlfileReceiver;