<commit_before>
/**
 * Module dependencies.
 */

var WS = require('./websocket');
var util = require('../util');
var debug = require('debug')('engine.io-client:flashsocket');
var inherit = require('inherit');

/**
 * Module exports.
 */

module.exports = FlashWS;

/**
 * Obfuscated key for Blue Coat.
 */

var xobject = global[['Active'].concat('Object').join('X')];

/**
 * FlashWS constructor.
 *
 * @api public
 */

function FlashWS(options){
  WS.call(this, options);
  this.flashPath = options.flashPath;
  this.policyPort = options.policyPort;
}

/**
 * Inherits from WebSocket.
 */

inherit(FlashWS, WS);

/**
 * Transport name.
 *
 * @api public
 */

FlashWS.prototype.name = 'flashsocket';

/*
 * FlashSockets only support binary as base64 encoded strings
 */

FlashWS.prototype.supportsBinary = false;

/**
 * Opens the transport.
 *
 * @api public
 */

FlashWS.prototype.doOpen = function(){
  if (!this.check()) {
    // let the probe timeout
    return;
  }

  // instrument websocketjs logging
  function log(type){
    return function(){
      var str = Array.prototype.join.call(arguments, ' ');
      debug('[websocketjs %s] %s', type, str);
    };
  }

  global.WEB_SOCKET_LOGGER = { log: log('debug'), error: log('error') };
  global.WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR = true;
  global.WEB_SOCKET_DISABLE_AUTO_INITIALIZATION = true;

  if (!global.WEB_SOCKET_SWF_LOCATION) {
    global.WEB_SOCKET_SWF_LOCATION = this.flashPath + 'WebSocketMainInsecure.swf';
  }

  // dependencies
  var deps = [this.flashPath + 'web_socket.js'];

  if (!global.swfobject) {
    deps.unshift(this.flashPath + 'swfobject.js');
  }

  var self = this;

  load(deps, function(){
    self.ready(function(){
      WebSocket.__addTask(function () {
        self.ws = new WebSocket(self.uri());
        self.addEventListeners();
      });
    });
  });
};

/**
 * Override to prevent closing uninitialized flashsocket.
 *
 * @api private
 */

FlashWS.prototype.doClose = function(){
  if (!this.ws) return;
  var self = this;
  WebSocket.__addTask(function(){
    WS.prototype.doClose.call(self);
  });
};

/**
 * Writes to the Flash socket.
 *
 * @api private
 */

FlashWS.prototype.write = function(){
  var self = this, args = arguments;
  WebSocket.__addTask(function(){
    WS.prototype.write.apply(self, args);
  });
};

/**
 * Called upon dependencies are loaded.
 *
 * @api private
 */

FlashWS.prototype.ready = function(fn){
  if (typeof WebSocket == 'undefined' ||
    !('__initialize' in WebSocket) || !global.swfobject) {
    return;
  }

  if (global.swfobject.getFlashPlayerVersion().major < 10) {
    return;
  }

  function init () {
    // only start downloading the swf file when
    // we checked that this browser actually supports it
    if (!FlashWS.loaded) {
      if (843 != self.policyPort) {
        var policy = 'xmlsocket://' + self.hostname + ':' + self.policyPort;
        WebSocket.loadFlashPolicyFile(policy);
      }

      WebSocket.__initialize();
      FlashWS.loaded = true;
    }

    fn.call(self);
  }

  var self = this;
  if (document.body) {
    return init();
  }
  
  if (global.document && document.readyState === 'complete') {
    init();
  }

  var element = global, event = 'load', fn = init, capture = false;

  if (element.attachEvent) {
    element.attachEvent('on' + event, fn);
  } else if (element.addEventListener) {
    element.addEventListener(event, fn, capture);
  }
};

/**
 * Feature detection for flashsocket.
 *
 * @return {Boolean} whether this transport is available.
 * @api public
 */<commit_msg>pass values directly in FlashWS.ready method<commit_after>
/**
 * Module dependencies.
 */

var WS = require('./websocket');
var util = require('../util');
var debug = require('debug')('engine.io-client:flashsocket');
var inherit = require('inherit');

/**
 * Module exports.
 */

module.exports = FlashWS;

/**
 * Obfuscated key for Blue Coat.
 */

var xobject = global[['Active'].concat('Object').join('X')];

/**
 * FlashWS constructor.
 *
 * @api public
 */

function FlashWS(options){
  WS.call(this, options);
  this.flashPath = options.flashPath;
  this.policyPort = options.policyPort;
}

/**
 * Inherits from WebSocket.
 */

inherit(FlashWS, WS);

/**
 * Transport name.
 *
 * @api public
 */

FlashWS.prototype.name = 'flashsocket';

/*
 * FlashSockets only support binary as base64 encoded strings
 */

FlashWS.prototype.supportsBinary = false;

/**
 * Opens the transport.
 *
 * @api public
 */

FlashWS.prototype.doOpen = function(){
  if (!this.check()) {
    // let the probe timeout
    return;
  }

  // instrument websocketjs logging
  function log(type){
    return function(){
      var str = Array.prototype.join.call(arguments, ' ');
      debug('[websocketjs %s] %s', type, str);
    };
  }

  global.WEB_SOCKET_LOGGER = { log: log('debug'), error: log('error') };
  global.WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR = true;
  global.WEB_SOCKET_DISABLE_AUTO_INITIALIZATION = true;

  if (!global.WEB_SOCKET_SWF_LOCATION) {
    global.WEB_SOCKET_SWF_LOCATION = this.flashPath + 'WebSocketMainInsecure.swf';
  }

  // dependencies
  var deps = [this.flashPath + 'web_socket.js'];

  if (!global.swfobject) {
    deps.unshift(this.flashPath + 'swfobject.js');
  }

  var self = this;

  load(deps, function(){
    self.ready(function(){
      WebSocket.__addTask(function () {
        self.ws = new WebSocket(self.uri());
        self.addEventListeners();
      });
    });
  });
};

/**
 * Override to prevent closing uninitialized flashsocket.
 *
 * @api private
 */

FlashWS.prototype.doClose = function(){
  if (!this.ws) return;
  var self = this;
  WebSocket.__addTask(function(){
    WS.prototype.doClose.call(self);
  });
};

/**
 * Writes to the Flash socket.
 *
 * @api private
 */

FlashWS.prototype.write = function(){
  var self = this, args = arguments;
  WebSocket.__addTask(function(){
    WS.prototype.write.apply(self, args);
  });
};

/**
 * Called upon dependencies are loaded.
 *
 * @api private
 */

FlashWS.prototype.ready = function(fn){
  if (typeof WebSocket == 'undefined' ||
    !('__initialize' in WebSocket) || !global.swfobject) {
    return;
  }

  if (global.swfobject.getFlashPlayerVersion().major < 10) {
    return;
  }

  function init () {
    // only start downloading the swf file when
    // we checked that this browser actually supports it
    if (!FlashWS.loaded) {
      if (843 != self.policyPort) {
        var policy = 'xmlsocket://' + self.hostname + ':' + self.policyPort;
        WebSocket.loadFlashPolicyFile(policy);
      }

      WebSocket.__initialize();
      FlashWS.loaded = true;
    }

    fn.call(self);
  }

  var self = this;
  if (document.body) {
    return init();
  }
  
  if (global.document && document.readyState == 'complete') {
    init();
  }

  if (global.attachEvent) {
    global.attachEvent('onload', init);
  } else if (global.addEventListener) {
    global.addEventListener('load', init, false);
  }
};

/**
 * Feature detection for flashsocket.
 *
 * @return {Boolean} whether this transport is available.
 * @api public
 */
{'fraction_non_alphanumeric': 0.15548350718917395, 'fraction_numerical': 0.0019734987313222443, 'mean_word_length': 3.1661773341162656, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1980325', 'n_tokens_mistral': 2378, 'n_tokens_neox': 2314, 'n_words': 509}