{"ast":null,"code":"'use strict';\n\nvar EventEmitter = require('events').EventEmitter,\n    inherits = require('inherits'),\n    utils = require('../../utils/event'),\n    urlUtils = require('../../utils/url'),\n    XHR = global.XMLHttpRequest;\n\nvar debug = function () {};\n\nif (process.env.NODE_ENV !== 'production') {\n  debug = require('debug')('sockjs-client:browser:xhr');\n}\n\nfunction AbstractXHRObject(method, url, payload, opts) {\n  debug(method, url);\n  var self = this;\n  EventEmitter.call(this);\n  setTimeout(function () {\n    self._start(method, url, payload, opts);\n  }, 0);\n}\n\ninherits(AbstractXHRObject, EventEmitter);\n\nAbstractXHRObject.prototype._start = function (method, url, payload, opts) {\n  var self = this;\n\n  try {\n    this.xhr = new XHR();\n  } catch (x) {// intentionally empty\n  }\n\n  if (!this.xhr) {\n    debug('no xhr');\n    this.emit('finish', 0, 'no xhr support');\n\n    this._cleanup();\n\n    return;\n  } // several browsers cache POSTs\n\n\n  url = urlUtils.addQuery(url, 't=' + +new Date()); // Explorer tends to keep connection open, even after the\n  // tab gets closed: http://bugs.jquery.com/ticket/5280\n\n  this.unloadRef = utils.unloadAdd(function () {\n    debug('unload cleanup');\n\n    self._cleanup(true);\n  });\n\n  try {\n    this.xhr.open(method, url, true);\n\n    if (this.timeout && 'timeout' in this.xhr) {\n      this.xhr.timeout = this.timeout;\n\n      this.xhr.ontimeout = function () {\n        debug('xhr timeout');\n        self.emit('finish', 0, '');\n\n        self._cleanup(false);\n      };\n    }\n  } catch (e) {\n    debug('exception', e); // IE raises an exception on wrong port.\n\n    this.emit('finish', 0, '');\n\n    this._cleanup(false);\n\n    return;\n  }\n\n  if ((!opts || !opts.noCredentials) && AbstractXHRObject.supportsCORS) {\n    debug('withCredentials'); // Mozilla docs says https://developer.mozilla.org/en/XMLHttpRequest :\n    // \"This never affects same-site requests.\"\n\n    this.xhr.withCredentials = true;\n  }\n\n  if (opts && opts.headers) {\n    for (var key in opts.headers) {\n      this.xhr.setRequestHeader(key, opts.headers[key]);\n    }\n  }\n\n  this.xhr.onreadystatechange = function () {\n    if (self.xhr) {\n      var x = self.xhr;\n      var text, status;\n      debug('readyState', x.readyState);\n\n      switch (x.readyState) {\n        case 3:\n          // IE doesn't like peeking into responseText or status\n          // on Microsoft.XMLHTTP and readystate=3\n          try {\n            status = x.status;\n            text = x.responseText;\n          } catch (e) {// intentionally empty\n          }\n\n          debug('status', status); // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450\n\n          if (status === 1223) {\n            status = 204;\n          } // IE does return readystate == 3 for 404 answers.\n\n\n          if (status === 200 && text && text.length > 0) {\n            debug('chunk');\n            self.emit('chunk', status, text);\n          }\n\n          break;\n\n        case 4:\n          status = x.status;\n          debug('status', status); // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450\n\n          if (status === 1223) {\n            status = 204;\n          } // IE returns this for a bad port\n          // http://msdn.microsoft.com/en-us/library/windows/desktop/aa383770(v=vs.85).aspx\n\n\n          if (status === 12005 || status === 12029) {\n            status = 0;\n          }\n\n          debug('finish', status, x.responseText);\n          self.emit('finish', status, x.responseText);\n\n          self._cleanup(false);\n\n          break;\n      }\n    }\n  };\n\n  try {\n    self.xhr.send(payload);\n  } catch (e) {\n    self.emit('finish', 0, '');\n\n    self._cleanup(false);\n  }\n};\n\nAbstractXHRObject.prototype._cleanup = function (abort) {\n  debug('cleanup');\n\n  if (!this.xhr) {\n    return;\n  }\n\n  this.removeAllListeners();\n  utils.unloadDel(this.unloadRef); // IE needs this field to be a function\n\n  this.xhr.onreadystatechange = function () {};\n\n  if (this.xhr.ontimeout) {\n    this.xhr.ontimeout = null;\n  }\n\n  if (abort) {\n    try {\n      this.xhr.abort();\n    } catch (x) {// intentionally empty\n    }\n  }\n\n  this.unloadRef = this.xhr = null;\n};\n\nAbstractXHRObject.prototype.close = function () {\n  debug('close');\n\n  this._cleanup(true);\n};\n\nAbstractXHRObject.enabled = !!XHR; // override XMLHttpRequest for IE6/7\n// obfuscate to avoid firewalls\n\nvar axo = ['Active'].concat('Object').join('X');\n\nif (!AbstractXHRObject.enabled && axo in global) {\n  debug('overriding xmlhttprequest');\n\n  XHR = function () {\n    try {\n      return new global[axo]('Microsoft.XMLHTTP');\n    } catch (e) {\n      return null;\n    }\n  };\n\n  AbstractXHRObject.enabled = !!new XHR();\n}\n\nvar cors = false;\n\ntry {\n  cors = 'withCredentials' in new XHR();\n} catch (ignored) {// intentionally empty\n}\n\nAbstractXHRObject.supportsCORS = cors;\nmodule.exports = AbstractXHRObject;","map":null,"metadata":{},"sourceType":"script"}