github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/sockjs-client-1.1.0/lib/transport/browser/abstract-xhr.js (about) 1 'use strict'; 2 3 var EventEmitter = require('events').EventEmitter 4 , inherits = require('inherits') 5 , utils = require('../../utils/event') 6 , urlUtils = require('../../utils/url') 7 , XHR = global.XMLHttpRequest 8 ; 9 10 var debug = function() {}; 11 if (process.env.NODE_ENV !== 'production') { 12 debug = require('debug')('sockjs-client:browser:xhr'); 13 } 14 15 function AbstractXHRObject(method, url, payload, opts) { 16 debug(method, url); 17 var self = this; 18 EventEmitter.call(this); 19 20 setTimeout(function () { 21 self._start(method, url, payload, opts); 22 }, 0); 23 } 24 25 inherits(AbstractXHRObject, EventEmitter); 26 27 AbstractXHRObject.prototype._start = function(method, url, payload, opts) { 28 var self = this; 29 30 try { 31 this.xhr = new XHR(); 32 } catch (x) { 33 // intentionally empty 34 } 35 36 if (!this.xhr) { 37 debug('no xhr'); 38 this.emit('finish', 0, 'no xhr support'); 39 this._cleanup(); 40 return; 41 } 42 43 // several browsers cache POSTs 44 url = urlUtils.addQuery(url, 't=' + (+new Date())); 45 46 // Explorer tends to keep connection open, even after the 47 // tab gets closed: http://bugs.jquery.com/ticket/5280 48 this.unloadRef = utils.unloadAdd(function() { 49 debug('unload cleanup'); 50 self._cleanup(true); 51 }); 52 try { 53 this.xhr.open(method, url, true); 54 if (this.timeout && 'timeout' in this.xhr) { 55 this.xhr.timeout = this.timeout; 56 this.xhr.ontimeout = function() { 57 debug('xhr timeout'); 58 self.emit('finish', 0, ''); 59 self._cleanup(false); 60 }; 61 } 62 } catch (e) { 63 debug('exception', e); 64 // IE raises an exception on wrong port. 65 this.emit('finish', 0, ''); 66 this._cleanup(false); 67 return; 68 } 69 70 if ((!opts || !opts.noCredentials) && AbstractXHRObject.supportsCORS) { 71 debug('withCredentials'); 72 // Mozilla docs says https://developer.mozilla.org/en/XMLHttpRequest : 73 // "This never affects same-site requests." 74 75 this.xhr.withCredentials = 'true'; 76 } 77 if (opts && opts.headers) { 78 for (var key in opts.headers) { 79 this.xhr.setRequestHeader(key, opts.headers[key]); 80 } 81 } 82 83 this.xhr.onreadystatechange = function() { 84 if (self.xhr) { 85 var x = self.xhr; 86 var text, status; 87 debug('readyState', x.readyState); 88 switch (x.readyState) { 89 case 3: 90 // IE doesn't like peeking into responseText or status 91 // on Microsoft.XMLHTTP and readystate=3 92 try { 93 status = x.status; 94 text = x.responseText; 95 } catch (e) { 96 // intentionally empty 97 } 98 debug('status', status); 99 // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450 100 if (status === 1223) { 101 status = 204; 102 } 103 104 // IE does return readystate == 3 for 404 answers. 105 if (status === 200 && text && text.length > 0) { 106 debug('chunk'); 107 self.emit('chunk', status, text); 108 } 109 break; 110 case 4: 111 status = x.status; 112 debug('status', status); 113 // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450 114 if (status === 1223) { 115 status = 204; 116 } 117 // IE returns this for a bad port 118 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa383770(v=vs.85).aspx 119 if (status === 12005 || status === 12029) { 120 status = 0; 121 } 122 123 debug('finish', status, x.responseText); 124 self.emit('finish', status, x.responseText); 125 self._cleanup(false); 126 break; 127 } 128 } 129 }; 130 131 try { 132 self.xhr.send(payload); 133 } catch (e) { 134 self.emit('finish', 0, ''); 135 self._cleanup(false); 136 } 137 }; 138 139 AbstractXHRObject.prototype._cleanup = function(abort) { 140 debug('cleanup'); 141 if (!this.xhr) { 142 return; 143 } 144 this.removeAllListeners(); 145 utils.unloadDel(this.unloadRef); 146 147 // IE needs this field to be a function 148 this.xhr.onreadystatechange = function() {}; 149 if (this.xhr.ontimeout) { 150 this.xhr.ontimeout = null; 151 } 152 153 if (abort) { 154 try { 155 this.xhr.abort(); 156 } catch (x) { 157 // intentionally empty 158 } 159 } 160 this.unloadRef = this.xhr = null; 161 }; 162 163 AbstractXHRObject.prototype.close = function() { 164 debug('close'); 165 this._cleanup(true); 166 }; 167 168 AbstractXHRObject.enabled = !!XHR; 169 // override XMLHttpRequest for IE6/7 170 // obfuscate to avoid firewalls 171 var axo = ['Active'].concat('Object').join('X'); 172 if (!AbstractXHRObject.enabled && (axo in global)) { 173 debug('overriding xmlhttprequest'); 174 XHR = function() { 175 try { 176 return new global[axo]('Microsoft.XMLHTTP'); 177 } catch (e) { 178 return null; 179 } 180 }; 181 AbstractXHRObject.enabled = !!new XHR(); 182 } 183 184 var cors = false; 185 try { 186 cors = 'withCredentials' in new XHR(); 187 } catch (ignored) { 188 // intentionally empty 189 } 190 191 AbstractXHRObject.supportsCORS = cors; 192 193 module.exports = AbstractXHRObject;