Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 1 | /*! |
| 2 | * Socket.IO v2.1.1 |
| 3 | * (c) 2014-2018 Guillermo Rauch |
| 4 | * Released under the MIT License. |
| 5 | */ |
| 6 | (function webpackUniversalModuleDefinition(root, factory) { |
| 7 | if(typeof exports === 'object' && typeof module === 'object') |
| 8 | module.exports = factory(); |
| 9 | else if(typeof define === 'function' && define.amd) |
| 10 | define([], factory); |
| 11 | else if(typeof exports === 'object') |
| 12 | exports["io"] = factory(); |
| 13 | else |
| 14 | root["io"] = factory(); |
| 15 | })(this, function() { |
| 16 | return /******/ (function(modules) { // webpackBootstrap |
| 17 | /******/ // The module cache |
| 18 | /******/ var installedModules = {}; |
| 19 | /******/ |
| 20 | /******/ // The require function |
| 21 | /******/ function __webpack_require__(moduleId) { |
| 22 | /******/ |
| 23 | /******/ // Check if module is in cache |
| 24 | /******/ if(installedModules[moduleId]) |
| 25 | /******/ return installedModules[moduleId].exports; |
| 26 | /******/ |
| 27 | /******/ // Create a new module (and put it into the cache) |
| 28 | /******/ var module = installedModules[moduleId] = { |
| 29 | /******/ exports: {}, |
| 30 | /******/ id: moduleId, |
| 31 | /******/ loaded: false |
| 32 | /******/ }; |
| 33 | /******/ |
| 34 | /******/ // Execute the module function |
| 35 | /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); |
| 36 | /******/ |
| 37 | /******/ // Flag the module as loaded |
| 38 | /******/ module.loaded = true; |
| 39 | /******/ |
| 40 | /******/ // Return the exports of the module |
| 41 | /******/ return module.exports; |
| 42 | /******/ } |
| 43 | /******/ |
| 44 | /******/ |
| 45 | /******/ // expose the modules object (__webpack_modules__) |
| 46 | /******/ __webpack_require__.m = modules; |
| 47 | /******/ |
| 48 | /******/ // expose the module cache |
| 49 | /******/ __webpack_require__.c = installedModules; |
| 50 | /******/ |
| 51 | /******/ // __webpack_public_path__ |
| 52 | /******/ __webpack_require__.p = ""; |
| 53 | /******/ |
| 54 | /******/ // Load entry module and return exports |
| 55 | /******/ return __webpack_require__(0); |
| 56 | /******/ }) |
| 57 | /************************************************************************/ |
| 58 | /******/ ([ |
| 59 | /* 0 */ |
| 60 | /***/ (function(module, exports, __webpack_require__) { |
| 61 | |
| 62 | 'use strict'; |
| 63 | |
| 64 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; |
| 65 | |
| 66 | /** |
| 67 | * Module dependencies. |
| 68 | */ |
| 69 | |
| 70 | var url = __webpack_require__(1); |
| 71 | var parser = __webpack_require__(7); |
| 72 | var Manager = __webpack_require__(12); |
| 73 | var debug = __webpack_require__(3)('socket.io-client'); |
| 74 | |
| 75 | /** |
| 76 | * Module exports. |
| 77 | */ |
| 78 | |
| 79 | module.exports = exports = lookup; |
| 80 | |
| 81 | /** |
| 82 | * Managers cache. |
| 83 | */ |
| 84 | |
| 85 | var cache = exports.managers = {}; |
| 86 | |
| 87 | /** |
| 88 | * Looks up an existing `Manager` for multiplexing. |
| 89 | * If the user summons: |
| 90 | * |
| 91 | * `io('http://localhost/a');` |
| 92 | * `io('http://localhost/b');` |
| 93 | * |
| 94 | * We reuse the existing instance based on same scheme/port/host, |
| 95 | * and we initialize sockets for each namespace. |
| 96 | * |
| 97 | * @api public |
| 98 | */ |
| 99 | |
| 100 | function lookup(uri, opts) { |
| 101 | if ((typeof uri === 'undefined' ? 'undefined' : _typeof(uri)) === 'object') { |
| 102 | opts = uri; |
| 103 | uri = undefined; |
| 104 | } |
| 105 | |
| 106 | opts = opts || {}; |
| 107 | |
| 108 | var parsed = url(uri); |
| 109 | var source = parsed.source; |
| 110 | var id = parsed.id; |
| 111 | var path = parsed.path; |
| 112 | var sameNamespace = cache[id] && path in cache[id].nsps; |
| 113 | var newConnection = opts.forceNew || opts['force new connection'] || false === opts.multiplex || sameNamespace; |
| 114 | |
| 115 | var io; |
| 116 | |
| 117 | if (newConnection) { |
| 118 | debug('ignoring socket cache for %s', source); |
| 119 | io = Manager(source, opts); |
| 120 | } else { |
| 121 | if (!cache[id]) { |
| 122 | debug('new io instance for %s', source); |
| 123 | cache[id] = Manager(source, opts); |
| 124 | } |
| 125 | io = cache[id]; |
| 126 | } |
| 127 | if (parsed.query && !opts.query) { |
| 128 | opts.query = parsed.query; |
| 129 | } |
| 130 | return io.socket(parsed.path, opts); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Protocol version. |
| 135 | * |
| 136 | * @api public |
| 137 | */ |
| 138 | |
| 139 | exports.protocol = parser.protocol; |
| 140 | |
| 141 | /** |
| 142 | * `connect`. |
| 143 | * |
| 144 | * @param {String} uri |
| 145 | * @api public |
| 146 | */ |
| 147 | |
| 148 | exports.connect = lookup; |
| 149 | |
| 150 | /** |
| 151 | * Expose constructors for standalone build. |
| 152 | * |
| 153 | * @api public |
| 154 | */ |
| 155 | |
| 156 | exports.Manager = __webpack_require__(12); |
| 157 | exports.Socket = __webpack_require__(37); |
| 158 | |
| 159 | /***/ }), |
| 160 | /* 1 */ |
| 161 | /***/ (function(module, exports, __webpack_require__) { |
| 162 | |
| 163 | /* WEBPACK VAR INJECTION */(function(global) {'use strict'; |
| 164 | |
| 165 | /** |
| 166 | * Module dependencies. |
| 167 | */ |
| 168 | |
| 169 | var parseuri = __webpack_require__(2); |
| 170 | var debug = __webpack_require__(3)('socket.io-client:url'); |
| 171 | |
| 172 | /** |
| 173 | * Module exports. |
| 174 | */ |
| 175 | |
| 176 | module.exports = url; |
| 177 | |
| 178 | /** |
| 179 | * URL parser. |
| 180 | * |
| 181 | * @param {String} url |
| 182 | * @param {Object} An object meant to mimic window.location. |
| 183 | * Defaults to window.location. |
| 184 | * @api public |
| 185 | */ |
| 186 | |
| 187 | function url(uri, loc) { |
| 188 | var obj = uri; |
| 189 | |
| 190 | // default to window.location |
| 191 | loc = loc || global.location; |
| 192 | if (null == uri) uri = loc.protocol + '//' + loc.host; |
| 193 | |
| 194 | // relative path support |
| 195 | if ('string' === typeof uri) { |
| 196 | if ('/' === uri.charAt(0)) { |
| 197 | if ('/' === uri.charAt(1)) { |
| 198 | uri = loc.protocol + uri; |
| 199 | } else { |
| 200 | uri = loc.host + uri; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if (!/^(https?|wss?):\/\//.test(uri)) { |
| 205 | debug('protocol-less url %s', uri); |
| 206 | if ('undefined' !== typeof loc) { |
| 207 | uri = loc.protocol + '//' + uri; |
| 208 | } else { |
| 209 | uri = 'https://' + uri; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // parse |
| 214 | debug('parse %s', uri); |
| 215 | obj = parseuri(uri); |
| 216 | } |
| 217 | |
| 218 | // make sure we treat `localhost:80` and `localhost` equally |
| 219 | if (!obj.port) { |
| 220 | if (/^(http|ws)$/.test(obj.protocol)) { |
| 221 | obj.port = '80'; |
| 222 | } else if (/^(http|ws)s$/.test(obj.protocol)) { |
| 223 | obj.port = '443'; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | obj.path = obj.path || '/'; |
| 228 | |
| 229 | var ipv6 = obj.host.indexOf(':') !== -1; |
| 230 | var host = ipv6 ? '[' + obj.host + ']' : obj.host; |
| 231 | |
| 232 | // define unique id |
| 233 | obj.id = obj.protocol + '://' + host + ':' + obj.port; |
| 234 | // define href |
| 235 | obj.href = obj.protocol + '://' + host + (loc && loc.port === obj.port ? '' : ':' + obj.port); |
| 236 | |
| 237 | return obj; |
| 238 | } |
| 239 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 240 | |
| 241 | /***/ }), |
| 242 | /* 2 */ |
| 243 | /***/ (function(module, exports) { |
| 244 | |
| 245 | /** |
| 246 | * Parses an URI |
| 247 | * |
| 248 | * @author Steven Levithan <stevenlevithan.com> (MIT license) |
| 249 | * @api private |
| 250 | */ |
| 251 | |
| 252 | var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/; |
| 253 | |
| 254 | var parts = [ |
| 255 | 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor' |
| 256 | ]; |
| 257 | |
| 258 | module.exports = function parseuri(str) { |
| 259 | var src = str, |
| 260 | b = str.indexOf('['), |
| 261 | e = str.indexOf(']'); |
| 262 | |
| 263 | if (b != -1 && e != -1) { |
| 264 | str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length); |
| 265 | } |
| 266 | |
| 267 | var m = re.exec(str || ''), |
| 268 | uri = {}, |
| 269 | i = 14; |
| 270 | |
| 271 | while (i--) { |
| 272 | uri[parts[i]] = m[i] || ''; |
| 273 | } |
| 274 | |
| 275 | if (b != -1 && e != -1) { |
| 276 | uri.source = src; |
| 277 | uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':'); |
| 278 | uri.authority = uri.authority.replace('[', '').replace(']', '').replace(/;/g, ':'); |
| 279 | uri.ipv6uri = true; |
| 280 | } |
| 281 | |
| 282 | return uri; |
| 283 | }; |
| 284 | |
| 285 | |
| 286 | /***/ }), |
| 287 | /* 3 */ |
| 288 | /***/ (function(module, exports, __webpack_require__) { |
| 289 | |
| 290 | /* WEBPACK VAR INJECTION */(function(process) {/** |
| 291 | * This is the web browser implementation of `debug()`. |
| 292 | * |
| 293 | * Expose `debug()` as the module. |
| 294 | */ |
| 295 | |
| 296 | exports = module.exports = __webpack_require__(5); |
| 297 | exports.log = log; |
| 298 | exports.formatArgs = formatArgs; |
| 299 | exports.save = save; |
| 300 | exports.load = load; |
| 301 | exports.useColors = useColors; |
| 302 | exports.storage = 'undefined' != typeof chrome |
| 303 | && 'undefined' != typeof chrome.storage |
| 304 | ? chrome.storage.local |
| 305 | : localstorage(); |
| 306 | |
| 307 | /** |
| 308 | * Colors. |
| 309 | */ |
| 310 | |
| 311 | exports.colors = [ |
| 312 | '#0000CC', '#0000FF', '#0033CC', '#0033FF', '#0066CC', '#0066FF', '#0099CC', |
| 313 | '#0099FF', '#00CC00', '#00CC33', '#00CC66', '#00CC99', '#00CCCC', '#00CCFF', |
| 314 | '#3300CC', '#3300FF', '#3333CC', '#3333FF', '#3366CC', '#3366FF', '#3399CC', |
| 315 | '#3399FF', '#33CC00', '#33CC33', '#33CC66', '#33CC99', '#33CCCC', '#33CCFF', |
| 316 | '#6600CC', '#6600FF', '#6633CC', '#6633FF', '#66CC00', '#66CC33', '#9900CC', |
| 317 | '#9900FF', '#9933CC', '#9933FF', '#99CC00', '#99CC33', '#CC0000', '#CC0033', |
| 318 | '#CC0066', '#CC0099', '#CC00CC', '#CC00FF', '#CC3300', '#CC3333', '#CC3366', |
| 319 | '#CC3399', '#CC33CC', '#CC33FF', '#CC6600', '#CC6633', '#CC9900', '#CC9933', |
| 320 | '#CCCC00', '#CCCC33', '#FF0000', '#FF0033', '#FF0066', '#FF0099', '#FF00CC', |
| 321 | '#FF00FF', '#FF3300', '#FF3333', '#FF3366', '#FF3399', '#FF33CC', '#FF33FF', |
| 322 | '#FF6600', '#FF6633', '#FF9900', '#FF9933', '#FFCC00', '#FFCC33' |
| 323 | ]; |
| 324 | |
| 325 | /** |
| 326 | * Currently only WebKit-based Web Inspectors, Firefox >= v31, |
| 327 | * and the Firebug extension (any Firefox version) are known |
| 328 | * to support "%c" CSS customizations. |
| 329 | * |
| 330 | * TODO: add a `localStorage` variable to explicitly enable/disable colors |
| 331 | */ |
| 332 | |
| 333 | function useColors() { |
| 334 | // NB: In an Electron preload script, document will be defined but not fully |
| 335 | // initialized. Since we know we're in Chrome, we'll just detect this case |
| 336 | // explicitly |
| 337 | if (typeof window !== 'undefined' && window.process && window.process.type === 'renderer') { |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | // Internet Explorer and Edge do not support colors. |
| 342 | if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) { |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | // is webkit? http://stackoverflow.com/a/16459606/376773 |
| 347 | // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632 |
| 348 | return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) || |
| 349 | // is firebug? http://stackoverflow.com/a/398120/376773 |
| 350 | (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) || |
| 351 | // is firefox >= v31? |
| 352 | // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages |
| 353 | (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) || |
| 354 | // double check webkit in userAgent just in case we are in a worker |
| 355 | (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/)); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. |
| 360 | */ |
| 361 | |
| 362 | exports.formatters.j = function(v) { |
| 363 | try { |
| 364 | return JSON.stringify(v); |
| 365 | } catch (err) { |
| 366 | return '[UnexpectedJSONParseError]: ' + err.message; |
| 367 | } |
| 368 | }; |
| 369 | |
| 370 | |
| 371 | /** |
| 372 | * Colorize log arguments if enabled. |
| 373 | * |
| 374 | * @api public |
| 375 | */ |
| 376 | |
| 377 | function formatArgs(args) { |
| 378 | var useColors = this.useColors; |
| 379 | |
| 380 | args[0] = (useColors ? '%c' : '') |
| 381 | + this.namespace |
| 382 | + (useColors ? ' %c' : ' ') |
| 383 | + args[0] |
| 384 | + (useColors ? '%c ' : ' ') |
| 385 | + '+' + exports.humanize(this.diff); |
| 386 | |
| 387 | if (!useColors) return; |
| 388 | |
| 389 | var c = 'color: ' + this.color; |
| 390 | args.splice(1, 0, c, 'color: inherit') |
| 391 | |
| 392 | // the final "%c" is somewhat tricky, because there could be other |
| 393 | // arguments passed either before or after the %c, so we need to |
| 394 | // figure out the correct index to insert the CSS into |
| 395 | var index = 0; |
| 396 | var lastC = 0; |
| 397 | args[0].replace(/%[a-zA-Z%]/g, function(match) { |
| 398 | if ('%%' === match) return; |
| 399 | index++; |
| 400 | if ('%c' === match) { |
| 401 | // we only are interested in the *last* %c |
| 402 | // (the user may have provided their own) |
| 403 | lastC = index; |
| 404 | } |
| 405 | }); |
| 406 | |
| 407 | args.splice(lastC, 0, c); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Invokes `console.log()` when available. |
| 412 | * No-op when `console.log` is not a "function". |
| 413 | * |
| 414 | * @api public |
| 415 | */ |
| 416 | |
| 417 | function log() { |
| 418 | // this hackery is required for IE8/9, where |
| 419 | // the `console.log` function doesn't have 'apply' |
| 420 | return 'object' === typeof console |
| 421 | && console.log |
| 422 | && Function.prototype.apply.call(console.log, console, arguments); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Save `namespaces`. |
| 427 | * |
| 428 | * @param {String} namespaces |
| 429 | * @api private |
| 430 | */ |
| 431 | |
| 432 | function save(namespaces) { |
| 433 | try { |
| 434 | if (null == namespaces) { |
| 435 | exports.storage.removeItem('debug'); |
| 436 | } else { |
| 437 | exports.storage.debug = namespaces; |
| 438 | } |
| 439 | } catch(e) {} |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Load `namespaces`. |
| 444 | * |
| 445 | * @return {String} returns the previously persisted debug modes |
| 446 | * @api private |
| 447 | */ |
| 448 | |
| 449 | function load() { |
| 450 | var r; |
| 451 | try { |
| 452 | r = exports.storage.debug; |
| 453 | } catch(e) {} |
| 454 | |
| 455 | // If debug isn't set in LS, and we're in Electron, try to load $DEBUG |
| 456 | if (!r && typeof process !== 'undefined' && 'env' in process) { |
| 457 | r = process.env.DEBUG; |
| 458 | } |
| 459 | |
| 460 | return r; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Enable namespaces listed in `localStorage.debug` initially. |
| 465 | */ |
| 466 | |
| 467 | exports.enable(load()); |
| 468 | |
| 469 | /** |
| 470 | * Localstorage attempts to return the localstorage. |
| 471 | * |
| 472 | * This is necessary because safari throws |
| 473 | * when a user disables cookies/localstorage |
| 474 | * and you attempt to access it. |
| 475 | * |
| 476 | * @return {LocalStorage} |
| 477 | * @api private |
| 478 | */ |
| 479 | |
| 480 | function localstorage() { |
| 481 | try { |
| 482 | return window.localStorage; |
| 483 | } catch (e) {} |
| 484 | } |
| 485 | |
| 486 | /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(4))) |
| 487 | |
| 488 | /***/ }), |
| 489 | /* 4 */ |
| 490 | /***/ (function(module, exports) { |
| 491 | |
| 492 | // shim for using process in browser |
| 493 | var process = module.exports = {}; |
| 494 | |
| 495 | // cached from whatever global is present so that test runners that stub it |
| 496 | // don't break things. But we need to wrap it in a try catch in case it is |
| 497 | // wrapped in strict mode code which doesn't define any globals. It's inside a |
| 498 | // function because try/catches deoptimize in certain engines. |
| 499 | |
| 500 | var cachedSetTimeout; |
| 501 | var cachedClearTimeout; |
| 502 | |
| 503 | function defaultSetTimout() { |
| 504 | throw new Error('setTimeout has not been defined'); |
| 505 | } |
| 506 | function defaultClearTimeout () { |
| 507 | throw new Error('clearTimeout has not been defined'); |
| 508 | } |
| 509 | (function () { |
| 510 | try { |
| 511 | if (typeof setTimeout === 'function') { |
| 512 | cachedSetTimeout = setTimeout; |
| 513 | } else { |
| 514 | cachedSetTimeout = defaultSetTimout; |
| 515 | } |
| 516 | } catch (e) { |
| 517 | cachedSetTimeout = defaultSetTimout; |
| 518 | } |
| 519 | try { |
| 520 | if (typeof clearTimeout === 'function') { |
| 521 | cachedClearTimeout = clearTimeout; |
| 522 | } else { |
| 523 | cachedClearTimeout = defaultClearTimeout; |
| 524 | } |
| 525 | } catch (e) { |
| 526 | cachedClearTimeout = defaultClearTimeout; |
| 527 | } |
| 528 | } ()) |
| 529 | function runTimeout(fun) { |
| 530 | if (cachedSetTimeout === setTimeout) { |
| 531 | //normal enviroments in sane situations |
| 532 | return setTimeout(fun, 0); |
| 533 | } |
| 534 | // if setTimeout wasn't available but was latter defined |
| 535 | if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) { |
| 536 | cachedSetTimeout = setTimeout; |
| 537 | return setTimeout(fun, 0); |
| 538 | } |
| 539 | try { |
| 540 | // when when somebody has screwed with setTimeout but no I.E. maddness |
| 541 | return cachedSetTimeout(fun, 0); |
| 542 | } catch(e){ |
| 543 | try { |
| 544 | // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally |
| 545 | return cachedSetTimeout.call(null, fun, 0); |
| 546 | } catch(e){ |
| 547 | // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error |
| 548 | return cachedSetTimeout.call(this, fun, 0); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | |
| 553 | } |
| 554 | function runClearTimeout(marker) { |
| 555 | if (cachedClearTimeout === clearTimeout) { |
| 556 | //normal enviroments in sane situations |
| 557 | return clearTimeout(marker); |
| 558 | } |
| 559 | // if clearTimeout wasn't available but was latter defined |
| 560 | if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) { |
| 561 | cachedClearTimeout = clearTimeout; |
| 562 | return clearTimeout(marker); |
| 563 | } |
| 564 | try { |
| 565 | // when when somebody has screwed with setTimeout but no I.E. maddness |
| 566 | return cachedClearTimeout(marker); |
| 567 | } catch (e){ |
| 568 | try { |
| 569 | // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally |
| 570 | return cachedClearTimeout.call(null, marker); |
| 571 | } catch (e){ |
| 572 | // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error. |
| 573 | // Some versions of I.E. have different rules for clearTimeout vs setTimeout |
| 574 | return cachedClearTimeout.call(this, marker); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | |
| 579 | |
| 580 | } |
| 581 | var queue = []; |
| 582 | var draining = false; |
| 583 | var currentQueue; |
| 584 | var queueIndex = -1; |
| 585 | |
| 586 | function cleanUpNextTick() { |
| 587 | if (!draining || !currentQueue) { |
| 588 | return; |
| 589 | } |
| 590 | draining = false; |
| 591 | if (currentQueue.length) { |
| 592 | queue = currentQueue.concat(queue); |
| 593 | } else { |
| 594 | queueIndex = -1; |
| 595 | } |
| 596 | if (queue.length) { |
| 597 | drainQueue(); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | function drainQueue() { |
| 602 | if (draining) { |
| 603 | return; |
| 604 | } |
| 605 | var timeout = runTimeout(cleanUpNextTick); |
| 606 | draining = true; |
| 607 | |
| 608 | var len = queue.length; |
| 609 | while(len) { |
| 610 | currentQueue = queue; |
| 611 | queue = []; |
| 612 | while (++queueIndex < len) { |
| 613 | if (currentQueue) { |
| 614 | currentQueue[queueIndex].run(); |
| 615 | } |
| 616 | } |
| 617 | queueIndex = -1; |
| 618 | len = queue.length; |
| 619 | } |
| 620 | currentQueue = null; |
| 621 | draining = false; |
| 622 | runClearTimeout(timeout); |
| 623 | } |
| 624 | |
| 625 | process.nextTick = function (fun) { |
| 626 | var args = new Array(arguments.length - 1); |
| 627 | if (arguments.length > 1) { |
| 628 | for (var i = 1; i < arguments.length; i++) { |
| 629 | args[i - 1] = arguments[i]; |
| 630 | } |
| 631 | } |
| 632 | queue.push(new Item(fun, args)); |
| 633 | if (queue.length === 1 && !draining) { |
| 634 | runTimeout(drainQueue); |
| 635 | } |
| 636 | }; |
| 637 | |
| 638 | // v8 likes predictible objects |
| 639 | function Item(fun, array) { |
| 640 | this.fun = fun; |
| 641 | this.array = array; |
| 642 | } |
| 643 | Item.prototype.run = function () { |
| 644 | this.fun.apply(null, this.array); |
| 645 | }; |
| 646 | process.title = 'browser'; |
| 647 | process.browser = true; |
| 648 | process.env = {}; |
| 649 | process.argv = []; |
| 650 | process.version = ''; // empty string to avoid regexp issues |
| 651 | process.versions = {}; |
| 652 | |
| 653 | function noop() {} |
| 654 | |
| 655 | process.on = noop; |
| 656 | process.addListener = noop; |
| 657 | process.once = noop; |
| 658 | process.off = noop; |
| 659 | process.removeListener = noop; |
| 660 | process.removeAllListeners = noop; |
| 661 | process.emit = noop; |
| 662 | process.prependListener = noop; |
| 663 | process.prependOnceListener = noop; |
| 664 | |
| 665 | process.listeners = function (name) { return [] } |
| 666 | |
| 667 | process.binding = function (name) { |
| 668 | throw new Error('process.binding is not supported'); |
| 669 | }; |
| 670 | |
| 671 | process.cwd = function () { return '/' }; |
| 672 | process.chdir = function (dir) { |
| 673 | throw new Error('process.chdir is not supported'); |
| 674 | }; |
| 675 | process.umask = function() { return 0; }; |
| 676 | |
| 677 | |
| 678 | /***/ }), |
| 679 | /* 5 */ |
| 680 | /***/ (function(module, exports, __webpack_require__) { |
| 681 | |
| 682 | |
| 683 | /** |
| 684 | * This is the common logic for both the Node.js and web browser |
| 685 | * implementations of `debug()`. |
| 686 | * |
| 687 | * Expose `debug()` as the module. |
| 688 | */ |
| 689 | |
| 690 | exports = module.exports = createDebug.debug = createDebug['default'] = createDebug; |
| 691 | exports.coerce = coerce; |
| 692 | exports.disable = disable; |
| 693 | exports.enable = enable; |
| 694 | exports.enabled = enabled; |
| 695 | exports.humanize = __webpack_require__(6); |
| 696 | |
| 697 | /** |
| 698 | * Active `debug` instances. |
| 699 | */ |
| 700 | exports.instances = []; |
| 701 | |
| 702 | /** |
| 703 | * The currently active debug mode names, and names to skip. |
| 704 | */ |
| 705 | |
| 706 | exports.names = []; |
| 707 | exports.skips = []; |
| 708 | |
| 709 | /** |
| 710 | * Map of special "%n" handling functions, for the debug "format" argument. |
| 711 | * |
| 712 | * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N". |
| 713 | */ |
| 714 | |
| 715 | exports.formatters = {}; |
| 716 | |
| 717 | /** |
| 718 | * Select a color. |
| 719 | * @param {String} namespace |
| 720 | * @return {Number} |
| 721 | * @api private |
| 722 | */ |
| 723 | |
| 724 | function selectColor(namespace) { |
| 725 | var hash = 0, i; |
| 726 | |
| 727 | for (i in namespace) { |
| 728 | hash = ((hash << 5) - hash) + namespace.charCodeAt(i); |
| 729 | hash |= 0; // Convert to 32bit integer |
| 730 | } |
| 731 | |
| 732 | return exports.colors[Math.abs(hash) % exports.colors.length]; |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Create a debugger with the given `namespace`. |
| 737 | * |
| 738 | * @param {String} namespace |
| 739 | * @return {Function} |
| 740 | * @api public |
| 741 | */ |
| 742 | |
| 743 | function createDebug(namespace) { |
| 744 | |
| 745 | var prevTime; |
| 746 | |
| 747 | function debug() { |
| 748 | // disabled? |
| 749 | if (!debug.enabled) return; |
| 750 | |
| 751 | var self = debug; |
| 752 | |
| 753 | // set `diff` timestamp |
| 754 | var curr = +new Date(); |
| 755 | var ms = curr - (prevTime || curr); |
| 756 | self.diff = ms; |
| 757 | self.prev = prevTime; |
| 758 | self.curr = curr; |
| 759 | prevTime = curr; |
| 760 | |
| 761 | // turn the `arguments` into a proper Array |
| 762 | var args = new Array(arguments.length); |
| 763 | for (var i = 0; i < args.length; i++) { |
| 764 | args[i] = arguments[i]; |
| 765 | } |
| 766 | |
| 767 | args[0] = exports.coerce(args[0]); |
| 768 | |
| 769 | if ('string' !== typeof args[0]) { |
| 770 | // anything else let's inspect with %O |
| 771 | args.unshift('%O'); |
| 772 | } |
| 773 | |
| 774 | // apply any `formatters` transformations |
| 775 | var index = 0; |
| 776 | args[0] = args[0].replace(/%([a-zA-Z%])/g, function(match, format) { |
| 777 | // if we encounter an escaped % then don't increase the array index |
| 778 | if (match === '%%') return match; |
| 779 | index++; |
| 780 | var formatter = exports.formatters[format]; |
| 781 | if ('function' === typeof formatter) { |
| 782 | var val = args[index]; |
| 783 | match = formatter.call(self, val); |
| 784 | |
| 785 | // now we need to remove `args[index]` since it's inlined in the `format` |
| 786 | args.splice(index, 1); |
| 787 | index--; |
| 788 | } |
| 789 | return match; |
| 790 | }); |
| 791 | |
| 792 | // apply env-specific formatting (colors, etc.) |
| 793 | exports.formatArgs.call(self, args); |
| 794 | |
| 795 | var logFn = debug.log || exports.log || console.log.bind(console); |
| 796 | logFn.apply(self, args); |
| 797 | } |
| 798 | |
| 799 | debug.namespace = namespace; |
| 800 | debug.enabled = exports.enabled(namespace); |
| 801 | debug.useColors = exports.useColors(); |
| 802 | debug.color = selectColor(namespace); |
| 803 | debug.destroy = destroy; |
| 804 | |
| 805 | // env-specific initialization logic for debug instances |
| 806 | if ('function' === typeof exports.init) { |
| 807 | exports.init(debug); |
| 808 | } |
| 809 | |
| 810 | exports.instances.push(debug); |
| 811 | |
| 812 | return debug; |
| 813 | } |
| 814 | |
| 815 | function destroy () { |
| 816 | var index = exports.instances.indexOf(this); |
| 817 | if (index !== -1) { |
| 818 | exports.instances.splice(index, 1); |
| 819 | return true; |
| 820 | } else { |
| 821 | return false; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * Enables a debug mode by namespaces. This can include modes |
| 827 | * separated by a colon and wildcards. |
| 828 | * |
| 829 | * @param {String} namespaces |
| 830 | * @api public |
| 831 | */ |
| 832 | |
| 833 | function enable(namespaces) { |
| 834 | exports.save(namespaces); |
| 835 | |
| 836 | exports.names = []; |
| 837 | exports.skips = []; |
| 838 | |
| 839 | var i; |
| 840 | var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/); |
| 841 | var len = split.length; |
| 842 | |
| 843 | for (i = 0; i < len; i++) { |
| 844 | if (!split[i]) continue; // ignore empty strings |
| 845 | namespaces = split[i].replace(/\*/g, '.*?'); |
| 846 | if (namespaces[0] === '-') { |
| 847 | exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$')); |
| 848 | } else { |
| 849 | exports.names.push(new RegExp('^' + namespaces + '$')); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | for (i = 0; i < exports.instances.length; i++) { |
| 854 | var instance = exports.instances[i]; |
| 855 | instance.enabled = exports.enabled(instance.namespace); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Disable debug output. |
| 861 | * |
| 862 | * @api public |
| 863 | */ |
| 864 | |
| 865 | function disable() { |
| 866 | exports.enable(''); |
| 867 | } |
| 868 | |
| 869 | /** |
| 870 | * Returns true if the given mode name is enabled, false otherwise. |
| 871 | * |
| 872 | * @param {String} name |
| 873 | * @return {Boolean} |
| 874 | * @api public |
| 875 | */ |
| 876 | |
| 877 | function enabled(name) { |
| 878 | if (name[name.length - 1] === '*') { |
| 879 | return true; |
| 880 | } |
| 881 | var i, len; |
| 882 | for (i = 0, len = exports.skips.length; i < len; i++) { |
| 883 | if (exports.skips[i].test(name)) { |
| 884 | return false; |
| 885 | } |
| 886 | } |
| 887 | for (i = 0, len = exports.names.length; i < len; i++) { |
| 888 | if (exports.names[i].test(name)) { |
| 889 | return true; |
| 890 | } |
| 891 | } |
| 892 | return false; |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Coerce `val`. |
| 897 | * |
| 898 | * @param {Mixed} val |
| 899 | * @return {Mixed} |
| 900 | * @api private |
| 901 | */ |
| 902 | |
| 903 | function coerce(val) { |
| 904 | if (val instanceof Error) return val.stack || val.message; |
| 905 | return val; |
| 906 | } |
| 907 | |
| 908 | |
| 909 | /***/ }), |
| 910 | /* 6 */ |
| 911 | /***/ (function(module, exports) { |
| 912 | |
| 913 | /** |
| 914 | * Helpers. |
| 915 | */ |
| 916 | |
| 917 | var s = 1000; |
| 918 | var m = s * 60; |
| 919 | var h = m * 60; |
| 920 | var d = h * 24; |
| 921 | var y = d * 365.25; |
| 922 | |
| 923 | /** |
| 924 | * Parse or format the given `val`. |
| 925 | * |
| 926 | * Options: |
| 927 | * |
| 928 | * - `long` verbose formatting [false] |
| 929 | * |
| 930 | * @param {String|Number} val |
| 931 | * @param {Object} [options] |
| 932 | * @throws {Error} throw an error if val is not a non-empty string or a number |
| 933 | * @return {String|Number} |
| 934 | * @api public |
| 935 | */ |
| 936 | |
| 937 | module.exports = function(val, options) { |
| 938 | options = options || {}; |
| 939 | var type = typeof val; |
| 940 | if (type === 'string' && val.length > 0) { |
| 941 | return parse(val); |
| 942 | } else if (type === 'number' && isNaN(val) === false) { |
| 943 | return options.long ? fmtLong(val) : fmtShort(val); |
| 944 | } |
| 945 | throw new Error( |
| 946 | 'val is not a non-empty string or a valid number. val=' + |
| 947 | JSON.stringify(val) |
| 948 | ); |
| 949 | }; |
| 950 | |
| 951 | /** |
| 952 | * Parse the given `str` and return milliseconds. |
| 953 | * |
| 954 | * @param {String} str |
| 955 | * @return {Number} |
| 956 | * @api private |
| 957 | */ |
| 958 | |
| 959 | function parse(str) { |
| 960 | str = String(str); |
| 961 | if (str.length > 100) { |
| 962 | return; |
| 963 | } |
| 964 | var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec( |
| 965 | str |
| 966 | ); |
| 967 | if (!match) { |
| 968 | return; |
| 969 | } |
| 970 | var n = parseFloat(match[1]); |
| 971 | var type = (match[2] || 'ms').toLowerCase(); |
| 972 | switch (type) { |
| 973 | case 'years': |
| 974 | case 'year': |
| 975 | case 'yrs': |
| 976 | case 'yr': |
| 977 | case 'y': |
| 978 | return n * y; |
| 979 | case 'days': |
| 980 | case 'day': |
| 981 | case 'd': |
| 982 | return n * d; |
| 983 | case 'hours': |
| 984 | case 'hour': |
| 985 | case 'hrs': |
| 986 | case 'hr': |
| 987 | case 'h': |
| 988 | return n * h; |
| 989 | case 'minutes': |
| 990 | case 'minute': |
| 991 | case 'mins': |
| 992 | case 'min': |
| 993 | case 'm': |
| 994 | return n * m; |
| 995 | case 'seconds': |
| 996 | case 'second': |
| 997 | case 'secs': |
| 998 | case 'sec': |
| 999 | case 's': |
| 1000 | return n * s; |
| 1001 | case 'milliseconds': |
| 1002 | case 'millisecond': |
| 1003 | case 'msecs': |
| 1004 | case 'msec': |
| 1005 | case 'ms': |
| 1006 | return n; |
| 1007 | default: |
| 1008 | return undefined; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * Short format for `ms`. |
| 1014 | * |
| 1015 | * @param {Number} ms |
| 1016 | * @return {String} |
| 1017 | * @api private |
| 1018 | */ |
| 1019 | |
| 1020 | function fmtShort(ms) { |
| 1021 | if (ms >= d) { |
| 1022 | return Math.round(ms / d) + 'd'; |
| 1023 | } |
| 1024 | if (ms >= h) { |
| 1025 | return Math.round(ms / h) + 'h'; |
| 1026 | } |
| 1027 | if (ms >= m) { |
| 1028 | return Math.round(ms / m) + 'm'; |
| 1029 | } |
| 1030 | if (ms >= s) { |
| 1031 | return Math.round(ms / s) + 's'; |
| 1032 | } |
| 1033 | return ms + 'ms'; |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * Long format for `ms`. |
| 1038 | * |
| 1039 | * @param {Number} ms |
| 1040 | * @return {String} |
| 1041 | * @api private |
| 1042 | */ |
| 1043 | |
| 1044 | function fmtLong(ms) { |
| 1045 | return plural(ms, d, 'day') || |
| 1046 | plural(ms, h, 'hour') || |
| 1047 | plural(ms, m, 'minute') || |
| 1048 | plural(ms, s, 'second') || |
| 1049 | ms + ' ms'; |
| 1050 | } |
| 1051 | |
| 1052 | /** |
| 1053 | * Pluralization helper. |
| 1054 | */ |
| 1055 | |
| 1056 | function plural(ms, n, name) { |
| 1057 | if (ms < n) { |
| 1058 | return; |
| 1059 | } |
| 1060 | if (ms < n * 1.5) { |
| 1061 | return Math.floor(ms / n) + ' ' + name; |
| 1062 | } |
| 1063 | return Math.ceil(ms / n) + ' ' + name + 's'; |
| 1064 | } |
| 1065 | |
| 1066 | |
| 1067 | /***/ }), |
| 1068 | /* 7 */ |
| 1069 | /***/ (function(module, exports, __webpack_require__) { |
| 1070 | |
| 1071 | |
| 1072 | /** |
| 1073 | * Module dependencies. |
| 1074 | */ |
| 1075 | |
| 1076 | var debug = __webpack_require__(3)('socket.io-parser'); |
| 1077 | var Emitter = __webpack_require__(8); |
| 1078 | var binary = __webpack_require__(9); |
| 1079 | var isArray = __webpack_require__(10); |
| 1080 | var isBuf = __webpack_require__(11); |
| 1081 | |
| 1082 | /** |
| 1083 | * Protocol version. |
| 1084 | * |
| 1085 | * @api public |
| 1086 | */ |
| 1087 | |
| 1088 | exports.protocol = 4; |
| 1089 | |
| 1090 | /** |
| 1091 | * Packet types. |
| 1092 | * |
| 1093 | * @api public |
| 1094 | */ |
| 1095 | |
| 1096 | exports.types = [ |
| 1097 | 'CONNECT', |
| 1098 | 'DISCONNECT', |
| 1099 | 'EVENT', |
| 1100 | 'ACK', |
| 1101 | 'ERROR', |
| 1102 | 'BINARY_EVENT', |
| 1103 | 'BINARY_ACK' |
| 1104 | ]; |
| 1105 | |
| 1106 | /** |
| 1107 | * Packet type `connect`. |
| 1108 | * |
| 1109 | * @api public |
| 1110 | */ |
| 1111 | |
| 1112 | exports.CONNECT = 0; |
| 1113 | |
| 1114 | /** |
| 1115 | * Packet type `disconnect`. |
| 1116 | * |
| 1117 | * @api public |
| 1118 | */ |
| 1119 | |
| 1120 | exports.DISCONNECT = 1; |
| 1121 | |
| 1122 | /** |
| 1123 | * Packet type `event`. |
| 1124 | * |
| 1125 | * @api public |
| 1126 | */ |
| 1127 | |
| 1128 | exports.EVENT = 2; |
| 1129 | |
| 1130 | /** |
| 1131 | * Packet type `ack`. |
| 1132 | * |
| 1133 | * @api public |
| 1134 | */ |
| 1135 | |
| 1136 | exports.ACK = 3; |
| 1137 | |
| 1138 | /** |
| 1139 | * Packet type `error`. |
| 1140 | * |
| 1141 | * @api public |
| 1142 | */ |
| 1143 | |
| 1144 | exports.ERROR = 4; |
| 1145 | |
| 1146 | /** |
| 1147 | * Packet type 'binary event' |
| 1148 | * |
| 1149 | * @api public |
| 1150 | */ |
| 1151 | |
| 1152 | exports.BINARY_EVENT = 5; |
| 1153 | |
| 1154 | /** |
| 1155 | * Packet type `binary ack`. For acks with binary arguments. |
| 1156 | * |
| 1157 | * @api public |
| 1158 | */ |
| 1159 | |
| 1160 | exports.BINARY_ACK = 6; |
| 1161 | |
| 1162 | /** |
| 1163 | * Encoder constructor. |
| 1164 | * |
| 1165 | * @api public |
| 1166 | */ |
| 1167 | |
| 1168 | exports.Encoder = Encoder; |
| 1169 | |
| 1170 | /** |
| 1171 | * Decoder constructor. |
| 1172 | * |
| 1173 | * @api public |
| 1174 | */ |
| 1175 | |
| 1176 | exports.Decoder = Decoder; |
| 1177 | |
| 1178 | /** |
| 1179 | * A socket.io Encoder instance |
| 1180 | * |
| 1181 | * @api public |
| 1182 | */ |
| 1183 | |
| 1184 | function Encoder() {} |
| 1185 | |
| 1186 | var ERROR_PACKET = exports.ERROR + '"encode error"'; |
| 1187 | |
| 1188 | /** |
| 1189 | * Encode a packet as a single string if non-binary, or as a |
| 1190 | * buffer sequence, depending on packet type. |
| 1191 | * |
| 1192 | * @param {Object} obj - packet object |
| 1193 | * @param {Function} callback - function to handle encodings (likely engine.write) |
| 1194 | * @return Calls callback with Array of encodings |
| 1195 | * @api public |
| 1196 | */ |
| 1197 | |
| 1198 | Encoder.prototype.encode = function(obj, callback){ |
| 1199 | debug('encoding packet %j', obj); |
| 1200 | |
| 1201 | if (exports.BINARY_EVENT === obj.type || exports.BINARY_ACK === obj.type) { |
| 1202 | encodeAsBinary(obj, callback); |
| 1203 | } else { |
| 1204 | var encoding = encodeAsString(obj); |
| 1205 | callback([encoding]); |
| 1206 | } |
| 1207 | }; |
| 1208 | |
| 1209 | /** |
| 1210 | * Encode packet as string. |
| 1211 | * |
| 1212 | * @param {Object} packet |
| 1213 | * @return {String} encoded |
| 1214 | * @api private |
| 1215 | */ |
| 1216 | |
| 1217 | function encodeAsString(obj) { |
| 1218 | |
| 1219 | // first is type |
| 1220 | var str = '' + obj.type; |
| 1221 | |
| 1222 | // attachments if we have them |
| 1223 | if (exports.BINARY_EVENT === obj.type || exports.BINARY_ACK === obj.type) { |
| 1224 | str += obj.attachments + '-'; |
| 1225 | } |
| 1226 | |
| 1227 | // if we have a namespace other than `/` |
| 1228 | // we append it followed by a comma `,` |
| 1229 | if (obj.nsp && '/' !== obj.nsp) { |
| 1230 | str += obj.nsp + ','; |
| 1231 | } |
| 1232 | |
| 1233 | // immediately followed by the id |
| 1234 | if (null != obj.id) { |
| 1235 | str += obj.id; |
| 1236 | } |
| 1237 | |
| 1238 | // json data |
| 1239 | if (null != obj.data) { |
| 1240 | var payload = tryStringify(obj.data); |
| 1241 | if (payload !== false) { |
| 1242 | str += payload; |
| 1243 | } else { |
| 1244 | return ERROR_PACKET; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | debug('encoded %j as %s', obj, str); |
| 1249 | return str; |
| 1250 | } |
| 1251 | |
| 1252 | function tryStringify(str) { |
| 1253 | try { |
| 1254 | return JSON.stringify(str); |
| 1255 | } catch(e){ |
| 1256 | return false; |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | /** |
| 1261 | * Encode packet as 'buffer sequence' by removing blobs, and |
| 1262 | * deconstructing packet into object with placeholders and |
| 1263 | * a list of buffers. |
| 1264 | * |
| 1265 | * @param {Object} packet |
| 1266 | * @return {Buffer} encoded |
| 1267 | * @api private |
| 1268 | */ |
| 1269 | |
| 1270 | function encodeAsBinary(obj, callback) { |
| 1271 | |
| 1272 | function writeEncoding(bloblessData) { |
| 1273 | var deconstruction = binary.deconstructPacket(bloblessData); |
| 1274 | var pack = encodeAsString(deconstruction.packet); |
| 1275 | var buffers = deconstruction.buffers; |
| 1276 | |
| 1277 | buffers.unshift(pack); // add packet info to beginning of data list |
| 1278 | callback(buffers); // write all the buffers |
| 1279 | } |
| 1280 | |
| 1281 | binary.removeBlobs(obj, writeEncoding); |
| 1282 | } |
| 1283 | |
| 1284 | /** |
| 1285 | * A socket.io Decoder instance |
| 1286 | * |
| 1287 | * @return {Object} decoder |
| 1288 | * @api public |
| 1289 | */ |
| 1290 | |
| 1291 | function Decoder() { |
| 1292 | this.reconstructor = null; |
| 1293 | } |
| 1294 | |
| 1295 | /** |
| 1296 | * Mix in `Emitter` with Decoder. |
| 1297 | */ |
| 1298 | |
| 1299 | Emitter(Decoder.prototype); |
| 1300 | |
| 1301 | /** |
| 1302 | * Decodes an ecoded packet string into packet JSON. |
| 1303 | * |
| 1304 | * @param {String} obj - encoded packet |
| 1305 | * @return {Object} packet |
| 1306 | * @api public |
| 1307 | */ |
| 1308 | |
| 1309 | Decoder.prototype.add = function(obj) { |
| 1310 | var packet; |
| 1311 | if (typeof obj === 'string') { |
| 1312 | packet = decodeString(obj); |
| 1313 | if (exports.BINARY_EVENT === packet.type || exports.BINARY_ACK === packet.type) { // binary packet's json |
| 1314 | this.reconstructor = new BinaryReconstructor(packet); |
| 1315 | |
| 1316 | // no attachments, labeled binary but no binary data to follow |
| 1317 | if (this.reconstructor.reconPack.attachments === 0) { |
| 1318 | this.emit('decoded', packet); |
| 1319 | } |
| 1320 | } else { // non-binary full packet |
| 1321 | this.emit('decoded', packet); |
| 1322 | } |
| 1323 | } |
| 1324 | else if (isBuf(obj) || obj.base64) { // raw binary data |
| 1325 | if (!this.reconstructor) { |
| 1326 | throw new Error('got binary data when not reconstructing a packet'); |
| 1327 | } else { |
| 1328 | packet = this.reconstructor.takeBinaryData(obj); |
| 1329 | if (packet) { // received final buffer |
| 1330 | this.reconstructor = null; |
| 1331 | this.emit('decoded', packet); |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | else { |
| 1336 | throw new Error('Unknown type: ' + obj); |
| 1337 | } |
| 1338 | }; |
| 1339 | |
| 1340 | /** |
| 1341 | * Decode a packet String (JSON data) |
| 1342 | * |
| 1343 | * @param {String} str |
| 1344 | * @return {Object} packet |
| 1345 | * @api private |
| 1346 | */ |
| 1347 | |
| 1348 | function decodeString(str) { |
| 1349 | var i = 0; |
| 1350 | // look up type |
| 1351 | var p = { |
| 1352 | type: Number(str.charAt(0)) |
| 1353 | }; |
| 1354 | |
| 1355 | if (null == exports.types[p.type]) { |
| 1356 | return error('unknown packet type ' + p.type); |
| 1357 | } |
| 1358 | |
| 1359 | // look up attachments if type binary |
| 1360 | if (exports.BINARY_EVENT === p.type || exports.BINARY_ACK === p.type) { |
| 1361 | var buf = ''; |
| 1362 | while (str.charAt(++i) !== '-') { |
| 1363 | buf += str.charAt(i); |
| 1364 | if (i == str.length) break; |
| 1365 | } |
| 1366 | if (buf != Number(buf) || str.charAt(i) !== '-') { |
| 1367 | throw new Error('Illegal attachments'); |
| 1368 | } |
| 1369 | p.attachments = Number(buf); |
| 1370 | } |
| 1371 | |
| 1372 | // look up namespace (if any) |
| 1373 | if ('/' === str.charAt(i + 1)) { |
| 1374 | p.nsp = ''; |
| 1375 | while (++i) { |
| 1376 | var c = str.charAt(i); |
| 1377 | if (',' === c) break; |
| 1378 | p.nsp += c; |
| 1379 | if (i === str.length) break; |
| 1380 | } |
| 1381 | } else { |
| 1382 | p.nsp = '/'; |
| 1383 | } |
| 1384 | |
| 1385 | // look up id |
| 1386 | var next = str.charAt(i + 1); |
| 1387 | if ('' !== next && Number(next) == next) { |
| 1388 | p.id = ''; |
| 1389 | while (++i) { |
| 1390 | var c = str.charAt(i); |
| 1391 | if (null == c || Number(c) != c) { |
| 1392 | --i; |
| 1393 | break; |
| 1394 | } |
| 1395 | p.id += str.charAt(i); |
| 1396 | if (i === str.length) break; |
| 1397 | } |
| 1398 | p.id = Number(p.id); |
| 1399 | } |
| 1400 | |
| 1401 | // look up json data |
| 1402 | if (str.charAt(++i)) { |
| 1403 | var payload = tryParse(str.substr(i)); |
| 1404 | var isPayloadValid = payload !== false && (p.type === exports.ERROR || isArray(payload)); |
| 1405 | if (isPayloadValid) { |
| 1406 | p.data = payload; |
| 1407 | } else { |
| 1408 | return error('invalid payload'); |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | debug('decoded %s as %j', str, p); |
| 1413 | return p; |
| 1414 | } |
| 1415 | |
| 1416 | function tryParse(str) { |
| 1417 | try { |
| 1418 | return JSON.parse(str); |
| 1419 | } catch(e){ |
| 1420 | return false; |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | /** |
| 1425 | * Deallocates a parser's resources |
| 1426 | * |
| 1427 | * @api public |
| 1428 | */ |
| 1429 | |
| 1430 | Decoder.prototype.destroy = function() { |
| 1431 | if (this.reconstructor) { |
| 1432 | this.reconstructor.finishedReconstruction(); |
| 1433 | } |
| 1434 | }; |
| 1435 | |
| 1436 | /** |
| 1437 | * A manager of a binary event's 'buffer sequence'. Should |
| 1438 | * be constructed whenever a packet of type BINARY_EVENT is |
| 1439 | * decoded. |
| 1440 | * |
| 1441 | * @param {Object} packet |
| 1442 | * @return {BinaryReconstructor} initialized reconstructor |
| 1443 | * @api private |
| 1444 | */ |
| 1445 | |
| 1446 | function BinaryReconstructor(packet) { |
| 1447 | this.reconPack = packet; |
| 1448 | this.buffers = []; |
| 1449 | } |
| 1450 | |
| 1451 | /** |
| 1452 | * Method to be called when binary data received from connection |
| 1453 | * after a BINARY_EVENT packet. |
| 1454 | * |
| 1455 | * @param {Buffer | ArrayBuffer} binData - the raw binary data received |
| 1456 | * @return {null | Object} returns null if more binary data is expected or |
| 1457 | * a reconstructed packet object if all buffers have been received. |
| 1458 | * @api private |
| 1459 | */ |
| 1460 | |
| 1461 | BinaryReconstructor.prototype.takeBinaryData = function(binData) { |
| 1462 | this.buffers.push(binData); |
| 1463 | if (this.buffers.length === this.reconPack.attachments) { // done with buffer list |
| 1464 | var packet = binary.reconstructPacket(this.reconPack, this.buffers); |
| 1465 | this.finishedReconstruction(); |
| 1466 | return packet; |
| 1467 | } |
| 1468 | return null; |
| 1469 | }; |
| 1470 | |
| 1471 | /** |
| 1472 | * Cleans up binary packet reconstruction variables. |
| 1473 | * |
| 1474 | * @api private |
| 1475 | */ |
| 1476 | |
| 1477 | BinaryReconstructor.prototype.finishedReconstruction = function() { |
| 1478 | this.reconPack = null; |
| 1479 | this.buffers = []; |
| 1480 | }; |
| 1481 | |
| 1482 | function error(msg) { |
| 1483 | return { |
| 1484 | type: exports.ERROR, |
| 1485 | data: 'parser error: ' + msg |
| 1486 | }; |
| 1487 | } |
| 1488 | |
| 1489 | |
| 1490 | /***/ }), |
| 1491 | /* 8 */ |
| 1492 | /***/ (function(module, exports, __webpack_require__) { |
| 1493 | |
| 1494 | |
| 1495 | /** |
| 1496 | * Expose `Emitter`. |
| 1497 | */ |
| 1498 | |
| 1499 | if (true) { |
| 1500 | module.exports = Emitter; |
| 1501 | } |
| 1502 | |
| 1503 | /** |
| 1504 | * Initialize a new `Emitter`. |
| 1505 | * |
| 1506 | * @api public |
| 1507 | */ |
| 1508 | |
| 1509 | function Emitter(obj) { |
| 1510 | if (obj) return mixin(obj); |
| 1511 | }; |
| 1512 | |
| 1513 | /** |
| 1514 | * Mixin the emitter properties. |
| 1515 | * |
| 1516 | * @param {Object} obj |
| 1517 | * @return {Object} |
| 1518 | * @api private |
| 1519 | */ |
| 1520 | |
| 1521 | function mixin(obj) { |
| 1522 | for (var key in Emitter.prototype) { |
| 1523 | obj[key] = Emitter.prototype[key]; |
| 1524 | } |
| 1525 | return obj; |
| 1526 | } |
| 1527 | |
| 1528 | /** |
| 1529 | * Listen on the given `event` with `fn`. |
| 1530 | * |
| 1531 | * @param {String} event |
| 1532 | * @param {Function} fn |
| 1533 | * @return {Emitter} |
| 1534 | * @api public |
| 1535 | */ |
| 1536 | |
| 1537 | Emitter.prototype.on = |
| 1538 | Emitter.prototype.addEventListener = function(event, fn){ |
| 1539 | this._callbacks = this._callbacks || {}; |
| 1540 | (this._callbacks['$' + event] = this._callbacks['$' + event] || []) |
| 1541 | .push(fn); |
| 1542 | return this; |
| 1543 | }; |
| 1544 | |
| 1545 | /** |
| 1546 | * Adds an `event` listener that will be invoked a single |
| 1547 | * time then automatically removed. |
| 1548 | * |
| 1549 | * @param {String} event |
| 1550 | * @param {Function} fn |
| 1551 | * @return {Emitter} |
| 1552 | * @api public |
| 1553 | */ |
| 1554 | |
| 1555 | Emitter.prototype.once = function(event, fn){ |
| 1556 | function on() { |
| 1557 | this.off(event, on); |
| 1558 | fn.apply(this, arguments); |
| 1559 | } |
| 1560 | |
| 1561 | on.fn = fn; |
| 1562 | this.on(event, on); |
| 1563 | return this; |
| 1564 | }; |
| 1565 | |
| 1566 | /** |
| 1567 | * Remove the given callback for `event` or all |
| 1568 | * registered callbacks. |
| 1569 | * |
| 1570 | * @param {String} event |
| 1571 | * @param {Function} fn |
| 1572 | * @return {Emitter} |
| 1573 | * @api public |
| 1574 | */ |
| 1575 | |
| 1576 | Emitter.prototype.off = |
| 1577 | Emitter.prototype.removeListener = |
| 1578 | Emitter.prototype.removeAllListeners = |
| 1579 | Emitter.prototype.removeEventListener = function(event, fn){ |
| 1580 | this._callbacks = this._callbacks || {}; |
| 1581 | |
| 1582 | // all |
| 1583 | if (0 == arguments.length) { |
| 1584 | this._callbacks = {}; |
| 1585 | return this; |
| 1586 | } |
| 1587 | |
| 1588 | // specific event |
| 1589 | var callbacks = this._callbacks['$' + event]; |
| 1590 | if (!callbacks) return this; |
| 1591 | |
| 1592 | // remove all handlers |
| 1593 | if (1 == arguments.length) { |
| 1594 | delete this._callbacks['$' + event]; |
| 1595 | return this; |
| 1596 | } |
| 1597 | |
| 1598 | // remove specific handler |
| 1599 | var cb; |
| 1600 | for (var i = 0; i < callbacks.length; i++) { |
| 1601 | cb = callbacks[i]; |
| 1602 | if (cb === fn || cb.fn === fn) { |
| 1603 | callbacks.splice(i, 1); |
| 1604 | break; |
| 1605 | } |
| 1606 | } |
| 1607 | return this; |
| 1608 | }; |
| 1609 | |
| 1610 | /** |
| 1611 | * Emit `event` with the given args. |
| 1612 | * |
| 1613 | * @param {String} event |
| 1614 | * @param {Mixed} ... |
| 1615 | * @return {Emitter} |
| 1616 | */ |
| 1617 | |
| 1618 | Emitter.prototype.emit = function(event){ |
| 1619 | this._callbacks = this._callbacks || {}; |
| 1620 | var args = [].slice.call(arguments, 1) |
| 1621 | , callbacks = this._callbacks['$' + event]; |
| 1622 | |
| 1623 | if (callbacks) { |
| 1624 | callbacks = callbacks.slice(0); |
| 1625 | for (var i = 0, len = callbacks.length; i < len; ++i) { |
| 1626 | callbacks[i].apply(this, args); |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | return this; |
| 1631 | }; |
| 1632 | |
| 1633 | /** |
| 1634 | * Return array of callbacks for `event`. |
| 1635 | * |
| 1636 | * @param {String} event |
| 1637 | * @return {Array} |
| 1638 | * @api public |
| 1639 | */ |
| 1640 | |
| 1641 | Emitter.prototype.listeners = function(event){ |
| 1642 | this._callbacks = this._callbacks || {}; |
| 1643 | return this._callbacks['$' + event] || []; |
| 1644 | }; |
| 1645 | |
| 1646 | /** |
| 1647 | * Check if this emitter has `event` handlers. |
| 1648 | * |
| 1649 | * @param {String} event |
| 1650 | * @return {Boolean} |
| 1651 | * @api public |
| 1652 | */ |
| 1653 | |
| 1654 | Emitter.prototype.hasListeners = function(event){ |
| 1655 | return !! this.listeners(event).length; |
| 1656 | }; |
| 1657 | |
| 1658 | |
| 1659 | /***/ }), |
| 1660 | /* 9 */ |
| 1661 | /***/ (function(module, exports, __webpack_require__) { |
| 1662 | |
| 1663 | /* WEBPACK VAR INJECTION */(function(global) {/*global Blob,File*/ |
| 1664 | |
| 1665 | /** |
| 1666 | * Module requirements |
| 1667 | */ |
| 1668 | |
| 1669 | var isArray = __webpack_require__(10); |
| 1670 | var isBuf = __webpack_require__(11); |
| 1671 | var toString = Object.prototype.toString; |
| 1672 | var withNativeBlob = typeof global.Blob === 'function' || toString.call(global.Blob) === '[object BlobConstructor]'; |
| 1673 | var withNativeFile = typeof global.File === 'function' || toString.call(global.File) === '[object FileConstructor]'; |
| 1674 | |
| 1675 | /** |
| 1676 | * Replaces every Buffer | ArrayBuffer in packet with a numbered placeholder. |
| 1677 | * Anything with blobs or files should be fed through removeBlobs before coming |
| 1678 | * here. |
| 1679 | * |
| 1680 | * @param {Object} packet - socket.io event packet |
| 1681 | * @return {Object} with deconstructed packet and list of buffers |
| 1682 | * @api public |
| 1683 | */ |
| 1684 | |
| 1685 | exports.deconstructPacket = function(packet) { |
| 1686 | var buffers = []; |
| 1687 | var packetData = packet.data; |
| 1688 | var pack = packet; |
| 1689 | pack.data = _deconstructPacket(packetData, buffers); |
| 1690 | pack.attachments = buffers.length; // number of binary 'attachments' |
| 1691 | return {packet: pack, buffers: buffers}; |
| 1692 | }; |
| 1693 | |
| 1694 | function _deconstructPacket(data, buffers) { |
| 1695 | if (!data) return data; |
| 1696 | |
| 1697 | if (isBuf(data)) { |
| 1698 | var placeholder = { _placeholder: true, num: buffers.length }; |
| 1699 | buffers.push(data); |
| 1700 | return placeholder; |
| 1701 | } else if (isArray(data)) { |
| 1702 | var newData = new Array(data.length); |
| 1703 | for (var i = 0; i < data.length; i++) { |
| 1704 | newData[i] = _deconstructPacket(data[i], buffers); |
| 1705 | } |
| 1706 | return newData; |
| 1707 | } else if (typeof data === 'object' && !(data instanceof Date)) { |
| 1708 | var newData = {}; |
| 1709 | for (var key in data) { |
| 1710 | newData[key] = _deconstructPacket(data[key], buffers); |
| 1711 | } |
| 1712 | return newData; |
| 1713 | } |
| 1714 | return data; |
| 1715 | } |
| 1716 | |
| 1717 | /** |
| 1718 | * Reconstructs a binary packet from its placeholder packet and buffers |
| 1719 | * |
| 1720 | * @param {Object} packet - event packet with placeholders |
| 1721 | * @param {Array} buffers - binary buffers to put in placeholder positions |
| 1722 | * @return {Object} reconstructed packet |
| 1723 | * @api public |
| 1724 | */ |
| 1725 | |
| 1726 | exports.reconstructPacket = function(packet, buffers) { |
| 1727 | packet.data = _reconstructPacket(packet.data, buffers); |
| 1728 | packet.attachments = undefined; // no longer useful |
| 1729 | return packet; |
| 1730 | }; |
| 1731 | |
| 1732 | function _reconstructPacket(data, buffers) { |
| 1733 | if (!data) return data; |
| 1734 | |
| 1735 | if (data && data._placeholder) { |
| 1736 | return buffers[data.num]; // appropriate buffer (should be natural order anyway) |
| 1737 | } else if (isArray(data)) { |
| 1738 | for (var i = 0; i < data.length; i++) { |
| 1739 | data[i] = _reconstructPacket(data[i], buffers); |
| 1740 | } |
| 1741 | } else if (typeof data === 'object') { |
| 1742 | for (var key in data) { |
| 1743 | data[key] = _reconstructPacket(data[key], buffers); |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | return data; |
| 1748 | } |
| 1749 | |
| 1750 | /** |
| 1751 | * Asynchronously removes Blobs or Files from data via |
| 1752 | * FileReader's readAsArrayBuffer method. Used before encoding |
| 1753 | * data as msgpack. Calls callback with the blobless data. |
| 1754 | * |
| 1755 | * @param {Object} data |
| 1756 | * @param {Function} callback |
| 1757 | * @api private |
| 1758 | */ |
| 1759 | |
| 1760 | exports.removeBlobs = function(data, callback) { |
| 1761 | function _removeBlobs(obj, curKey, containingObject) { |
| 1762 | if (!obj) return obj; |
| 1763 | |
| 1764 | // convert any blob |
| 1765 | if ((withNativeBlob && obj instanceof Blob) || |
| 1766 | (withNativeFile && obj instanceof File)) { |
| 1767 | pendingBlobs++; |
| 1768 | |
| 1769 | // async filereader |
| 1770 | var fileReader = new FileReader(); |
| 1771 | fileReader.onload = function() { // this.result == arraybuffer |
| 1772 | if (containingObject) { |
| 1773 | containingObject[curKey] = this.result; |
| 1774 | } |
| 1775 | else { |
| 1776 | bloblessData = this.result; |
| 1777 | } |
| 1778 | |
| 1779 | // if nothing pending its callback time |
| 1780 | if(! --pendingBlobs) { |
| 1781 | callback(bloblessData); |
| 1782 | } |
| 1783 | }; |
| 1784 | |
| 1785 | fileReader.readAsArrayBuffer(obj); // blob -> arraybuffer |
| 1786 | } else if (isArray(obj)) { // handle array |
| 1787 | for (var i = 0; i < obj.length; i++) { |
| 1788 | _removeBlobs(obj[i], i, obj); |
| 1789 | } |
| 1790 | } else if (typeof obj === 'object' && !isBuf(obj)) { // and object |
| 1791 | for (var key in obj) { |
| 1792 | _removeBlobs(obj[key], key, obj); |
| 1793 | } |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | var pendingBlobs = 0; |
| 1798 | var bloblessData = data; |
| 1799 | _removeBlobs(bloblessData); |
| 1800 | if (!pendingBlobs) { |
| 1801 | callback(bloblessData); |
| 1802 | } |
| 1803 | }; |
| 1804 | |
| 1805 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 1806 | |
| 1807 | /***/ }), |
| 1808 | /* 10 */ |
| 1809 | /***/ (function(module, exports) { |
| 1810 | |
| 1811 | var toString = {}.toString; |
| 1812 | |
| 1813 | module.exports = Array.isArray || function (arr) { |
| 1814 | return toString.call(arr) == '[object Array]'; |
| 1815 | }; |
| 1816 | |
| 1817 | |
| 1818 | /***/ }), |
| 1819 | /* 11 */ |
| 1820 | /***/ (function(module, exports) { |
| 1821 | |
| 1822 | /* WEBPACK VAR INJECTION */(function(global) { |
| 1823 | module.exports = isBuf; |
| 1824 | |
| 1825 | var withNativeBuffer = typeof global.Buffer === 'function' && typeof global.Buffer.isBuffer === 'function'; |
| 1826 | var withNativeArrayBuffer = typeof global.ArrayBuffer === 'function'; |
| 1827 | |
| 1828 | var isView = (function () { |
| 1829 | if (withNativeArrayBuffer && typeof global.ArrayBuffer.isView === 'function') { |
| 1830 | return global.ArrayBuffer.isView; |
| 1831 | } else { |
| 1832 | return function (obj) { return obj.buffer instanceof global.ArrayBuffer; }; |
| 1833 | } |
| 1834 | })(); |
| 1835 | |
| 1836 | /** |
| 1837 | * Returns true if obj is a buffer or an arraybuffer. |
| 1838 | * |
| 1839 | * @api private |
| 1840 | */ |
| 1841 | |
| 1842 | function isBuf(obj) { |
| 1843 | return (withNativeBuffer && global.Buffer.isBuffer(obj)) || |
| 1844 | (withNativeArrayBuffer && (obj instanceof global.ArrayBuffer || isView(obj))); |
| 1845 | } |
| 1846 | |
| 1847 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 1848 | |
| 1849 | /***/ }), |
| 1850 | /* 12 */ |
| 1851 | /***/ (function(module, exports, __webpack_require__) { |
| 1852 | |
| 1853 | 'use strict'; |
| 1854 | |
| 1855 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; |
| 1856 | |
| 1857 | /** |
| 1858 | * Module dependencies. |
| 1859 | */ |
| 1860 | |
| 1861 | var eio = __webpack_require__(13); |
| 1862 | var Socket = __webpack_require__(37); |
| 1863 | var Emitter = __webpack_require__(8); |
| 1864 | var parser = __webpack_require__(7); |
| 1865 | var on = __webpack_require__(39); |
| 1866 | var bind = __webpack_require__(40); |
| 1867 | var debug = __webpack_require__(3)('socket.io-client:manager'); |
| 1868 | var indexOf = __webpack_require__(36); |
| 1869 | var Backoff = __webpack_require__(41); |
| 1870 | |
| 1871 | /** |
| 1872 | * IE6+ hasOwnProperty |
| 1873 | */ |
| 1874 | |
| 1875 | var has = Object.prototype.hasOwnProperty; |
| 1876 | |
| 1877 | /** |
| 1878 | * Module exports |
| 1879 | */ |
| 1880 | |
| 1881 | module.exports = Manager; |
| 1882 | |
| 1883 | /** |
| 1884 | * `Manager` constructor. |
| 1885 | * |
| 1886 | * @param {String} engine instance or engine uri/opts |
| 1887 | * @param {Object} options |
| 1888 | * @api public |
| 1889 | */ |
| 1890 | |
| 1891 | function Manager(uri, opts) { |
| 1892 | if (!(this instanceof Manager)) return new Manager(uri, opts); |
| 1893 | if (uri && 'object' === (typeof uri === 'undefined' ? 'undefined' : _typeof(uri))) { |
| 1894 | opts = uri; |
| 1895 | uri = undefined; |
| 1896 | } |
| 1897 | opts = opts || {}; |
| 1898 | |
| 1899 | opts.path = opts.path || '/socket.io'; |
| 1900 | this.nsps = {}; |
| 1901 | this.subs = []; |
| 1902 | this.opts = opts; |
| 1903 | this.reconnection(opts.reconnection !== false); |
| 1904 | this.reconnectionAttempts(opts.reconnectionAttempts || Infinity); |
| 1905 | this.reconnectionDelay(opts.reconnectionDelay || 1000); |
| 1906 | this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000); |
| 1907 | this.randomizationFactor(opts.randomizationFactor || 0.5); |
| 1908 | this.backoff = new Backoff({ |
| 1909 | min: this.reconnectionDelay(), |
| 1910 | max: this.reconnectionDelayMax(), |
| 1911 | jitter: this.randomizationFactor() |
| 1912 | }); |
| 1913 | this.timeout(null == opts.timeout ? 20000 : opts.timeout); |
| 1914 | this.readyState = 'closed'; |
| 1915 | this.uri = uri; |
| 1916 | this.connecting = []; |
| 1917 | this.lastPing = null; |
| 1918 | this.encoding = false; |
| 1919 | this.packetBuffer = []; |
| 1920 | var _parser = opts.parser || parser; |
| 1921 | this.encoder = new _parser.Encoder(); |
| 1922 | this.decoder = new _parser.Decoder(); |
| 1923 | this.autoConnect = opts.autoConnect !== false; |
| 1924 | if (this.autoConnect) this.open(); |
| 1925 | } |
| 1926 | |
| 1927 | /** |
| 1928 | * Propagate given event to sockets and emit on `this` |
| 1929 | * |
| 1930 | * @api private |
| 1931 | */ |
| 1932 | |
| 1933 | Manager.prototype.emitAll = function () { |
| 1934 | this.emit.apply(this, arguments); |
| 1935 | for (var nsp in this.nsps) { |
| 1936 | if (has.call(this.nsps, nsp)) { |
| 1937 | this.nsps[nsp].emit.apply(this.nsps[nsp], arguments); |
| 1938 | } |
| 1939 | } |
| 1940 | }; |
| 1941 | |
| 1942 | /** |
| 1943 | * Update `socket.id` of all sockets |
| 1944 | * |
| 1945 | * @api private |
| 1946 | */ |
| 1947 | |
| 1948 | Manager.prototype.updateSocketIds = function () { |
| 1949 | for (var nsp in this.nsps) { |
| 1950 | if (has.call(this.nsps, nsp)) { |
| 1951 | this.nsps[nsp].id = this.generateId(nsp); |
| 1952 | } |
| 1953 | } |
| 1954 | }; |
| 1955 | |
| 1956 | /** |
| 1957 | * generate `socket.id` for the given `nsp` |
| 1958 | * |
| 1959 | * @param {String} nsp |
| 1960 | * @return {String} |
| 1961 | * @api private |
| 1962 | */ |
| 1963 | |
| 1964 | Manager.prototype.generateId = function (nsp) { |
| 1965 | return (nsp === '/' ? '' : nsp + '#') + this.engine.id; |
| 1966 | }; |
| 1967 | |
| 1968 | /** |
| 1969 | * Mix in `Emitter`. |
| 1970 | */ |
| 1971 | |
| 1972 | Emitter(Manager.prototype); |
| 1973 | |
| 1974 | /** |
| 1975 | * Sets the `reconnection` config. |
| 1976 | * |
| 1977 | * @param {Boolean} true/false if it should automatically reconnect |
| 1978 | * @return {Manager} self or value |
| 1979 | * @api public |
| 1980 | */ |
| 1981 | |
| 1982 | Manager.prototype.reconnection = function (v) { |
| 1983 | if (!arguments.length) return this._reconnection; |
| 1984 | this._reconnection = !!v; |
| 1985 | return this; |
| 1986 | }; |
| 1987 | |
| 1988 | /** |
| 1989 | * Sets the reconnection attempts config. |
| 1990 | * |
| 1991 | * @param {Number} max reconnection attempts before giving up |
| 1992 | * @return {Manager} self or value |
| 1993 | * @api public |
| 1994 | */ |
| 1995 | |
| 1996 | Manager.prototype.reconnectionAttempts = function (v) { |
| 1997 | if (!arguments.length) return this._reconnectionAttempts; |
| 1998 | this._reconnectionAttempts = v; |
| 1999 | return this; |
| 2000 | }; |
| 2001 | |
| 2002 | /** |
| 2003 | * Sets the delay between reconnections. |
| 2004 | * |
| 2005 | * @param {Number} delay |
| 2006 | * @return {Manager} self or value |
| 2007 | * @api public |
| 2008 | */ |
| 2009 | |
| 2010 | Manager.prototype.reconnectionDelay = function (v) { |
| 2011 | if (!arguments.length) return this._reconnectionDelay; |
| 2012 | this._reconnectionDelay = v; |
| 2013 | this.backoff && this.backoff.setMin(v); |
| 2014 | return this; |
| 2015 | }; |
| 2016 | |
| 2017 | Manager.prototype.randomizationFactor = function (v) { |
| 2018 | if (!arguments.length) return this._randomizationFactor; |
| 2019 | this._randomizationFactor = v; |
| 2020 | this.backoff && this.backoff.setJitter(v); |
| 2021 | return this; |
| 2022 | }; |
| 2023 | |
| 2024 | /** |
| 2025 | * Sets the maximum delay between reconnections. |
| 2026 | * |
| 2027 | * @param {Number} delay |
| 2028 | * @return {Manager} self or value |
| 2029 | * @api public |
| 2030 | */ |
| 2031 | |
| 2032 | Manager.prototype.reconnectionDelayMax = function (v) { |
| 2033 | if (!arguments.length) return this._reconnectionDelayMax; |
| 2034 | this._reconnectionDelayMax = v; |
| 2035 | this.backoff && this.backoff.setMax(v); |
| 2036 | return this; |
| 2037 | }; |
| 2038 | |
| 2039 | /** |
| 2040 | * Sets the connection timeout. `false` to disable |
| 2041 | * |
| 2042 | * @return {Manager} self or value |
| 2043 | * @api public |
| 2044 | */ |
| 2045 | |
| 2046 | Manager.prototype.timeout = function (v) { |
| 2047 | if (!arguments.length) return this._timeout; |
| 2048 | this._timeout = v; |
| 2049 | return this; |
| 2050 | }; |
| 2051 | |
| 2052 | /** |
| 2053 | * Starts trying to reconnect if reconnection is enabled and we have not |
| 2054 | * started reconnecting yet |
| 2055 | * |
| 2056 | * @api private |
| 2057 | */ |
| 2058 | |
| 2059 | Manager.prototype.maybeReconnectOnOpen = function () { |
| 2060 | // Only try to reconnect if it's the first time we're connecting |
| 2061 | if (!this.reconnecting && this._reconnection && this.backoff.attempts === 0) { |
| 2062 | // keeps reconnection from firing twice for the same reconnection loop |
| 2063 | this.reconnect(); |
| 2064 | } |
| 2065 | }; |
| 2066 | |
| 2067 | /** |
| 2068 | * Sets the current transport `socket`. |
| 2069 | * |
| 2070 | * @param {Function} optional, callback |
| 2071 | * @return {Manager} self |
| 2072 | * @api public |
| 2073 | */ |
| 2074 | |
| 2075 | Manager.prototype.open = Manager.prototype.connect = function (fn, opts) { |
| 2076 | debug('readyState %s', this.readyState); |
| 2077 | if (~this.readyState.indexOf('open')) return this; |
| 2078 | |
| 2079 | debug('opening %s', this.uri); |
| 2080 | this.engine = eio(this.uri, this.opts); |
| 2081 | var socket = this.engine; |
| 2082 | var self = this; |
| 2083 | this.readyState = 'opening'; |
| 2084 | this.skipReconnect = false; |
| 2085 | |
| 2086 | // emit `open` |
| 2087 | var openSub = on(socket, 'open', function () { |
| 2088 | self.onopen(); |
| 2089 | fn && fn(); |
| 2090 | }); |
| 2091 | |
| 2092 | // emit `connect_error` |
| 2093 | var errorSub = on(socket, 'error', function (data) { |
| 2094 | debug('connect_error'); |
| 2095 | self.cleanup(); |
| 2096 | self.readyState = 'closed'; |
| 2097 | self.emitAll('connect_error', data); |
| 2098 | if (fn) { |
| 2099 | var err = new Error('Connection error'); |
| 2100 | err.data = data; |
| 2101 | fn(err); |
| 2102 | } else { |
| 2103 | // Only do this if there is no fn to handle the error |
| 2104 | self.maybeReconnectOnOpen(); |
| 2105 | } |
| 2106 | }); |
| 2107 | |
| 2108 | // emit `connect_timeout` |
| 2109 | if (false !== this._timeout) { |
| 2110 | var timeout = this._timeout; |
| 2111 | debug('connect attempt will timeout after %d', timeout); |
| 2112 | |
| 2113 | // set timer |
| 2114 | var timer = setTimeout(function () { |
| 2115 | debug('connect attempt timed out after %d', timeout); |
| 2116 | openSub.destroy(); |
| 2117 | socket.close(); |
| 2118 | socket.emit('error', 'timeout'); |
| 2119 | self.emitAll('connect_timeout', timeout); |
| 2120 | }, timeout); |
| 2121 | |
| 2122 | this.subs.push({ |
| 2123 | destroy: function destroy() { |
| 2124 | clearTimeout(timer); |
| 2125 | } |
| 2126 | }); |
| 2127 | } |
| 2128 | |
| 2129 | this.subs.push(openSub); |
| 2130 | this.subs.push(errorSub); |
| 2131 | |
| 2132 | return this; |
| 2133 | }; |
| 2134 | |
| 2135 | /** |
| 2136 | * Called upon transport open. |
| 2137 | * |
| 2138 | * @api private |
| 2139 | */ |
| 2140 | |
| 2141 | Manager.prototype.onopen = function () { |
| 2142 | debug('open'); |
| 2143 | |
| 2144 | // clear old subs |
| 2145 | this.cleanup(); |
| 2146 | |
| 2147 | // mark as open |
| 2148 | this.readyState = 'open'; |
| 2149 | this.emit('open'); |
| 2150 | |
| 2151 | // add new subs |
| 2152 | var socket = this.engine; |
| 2153 | this.subs.push(on(socket, 'data', bind(this, 'ondata'))); |
| 2154 | this.subs.push(on(socket, 'ping', bind(this, 'onping'))); |
| 2155 | this.subs.push(on(socket, 'pong', bind(this, 'onpong'))); |
| 2156 | this.subs.push(on(socket, 'error', bind(this, 'onerror'))); |
| 2157 | this.subs.push(on(socket, 'close', bind(this, 'onclose'))); |
| 2158 | this.subs.push(on(this.decoder, 'decoded', bind(this, 'ondecoded'))); |
| 2159 | }; |
| 2160 | |
| 2161 | /** |
| 2162 | * Called upon a ping. |
| 2163 | * |
| 2164 | * @api private |
| 2165 | */ |
| 2166 | |
| 2167 | Manager.prototype.onping = function () { |
| 2168 | this.lastPing = new Date(); |
| 2169 | this.emitAll('ping'); |
| 2170 | }; |
| 2171 | |
| 2172 | /** |
| 2173 | * Called upon a packet. |
| 2174 | * |
| 2175 | * @api private |
| 2176 | */ |
| 2177 | |
| 2178 | Manager.prototype.onpong = function () { |
| 2179 | this.emitAll('pong', new Date() - this.lastPing); |
| 2180 | }; |
| 2181 | |
| 2182 | /** |
| 2183 | * Called with data. |
| 2184 | * |
| 2185 | * @api private |
| 2186 | */ |
| 2187 | |
| 2188 | Manager.prototype.ondata = function (data) { |
| 2189 | this.decoder.add(data); |
| 2190 | }; |
| 2191 | |
| 2192 | /** |
| 2193 | * Called when parser fully decodes a packet. |
| 2194 | * |
| 2195 | * @api private |
| 2196 | */ |
| 2197 | |
| 2198 | Manager.prototype.ondecoded = function (packet) { |
| 2199 | this.emit('packet', packet); |
| 2200 | }; |
| 2201 | |
| 2202 | /** |
| 2203 | * Called upon socket error. |
| 2204 | * |
| 2205 | * @api private |
| 2206 | */ |
| 2207 | |
| 2208 | Manager.prototype.onerror = function (err) { |
| 2209 | debug('error', err); |
| 2210 | this.emitAll('error', err); |
| 2211 | }; |
| 2212 | |
| 2213 | /** |
| 2214 | * Creates a new socket for the given `nsp`. |
| 2215 | * |
| 2216 | * @return {Socket} |
| 2217 | * @api public |
| 2218 | */ |
| 2219 | |
| 2220 | Manager.prototype.socket = function (nsp, opts) { |
| 2221 | var socket = this.nsps[nsp]; |
| 2222 | if (!socket) { |
| 2223 | socket = new Socket(this, nsp, opts); |
| 2224 | this.nsps[nsp] = socket; |
| 2225 | var self = this; |
| 2226 | socket.on('connecting', onConnecting); |
| 2227 | socket.on('connect', function () { |
| 2228 | socket.id = self.generateId(nsp); |
| 2229 | }); |
| 2230 | |
| 2231 | if (this.autoConnect) { |
| 2232 | // manually call here since connecting event is fired before listening |
| 2233 | onConnecting(); |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | function onConnecting() { |
| 2238 | if (!~indexOf(self.connecting, socket)) { |
| 2239 | self.connecting.push(socket); |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | return socket; |
| 2244 | }; |
| 2245 | |
| 2246 | /** |
| 2247 | * Called upon a socket close. |
| 2248 | * |
| 2249 | * @param {Socket} socket |
| 2250 | */ |
| 2251 | |
| 2252 | Manager.prototype.destroy = function (socket) { |
| 2253 | var index = indexOf(this.connecting, socket); |
| 2254 | if (~index) this.connecting.splice(index, 1); |
| 2255 | if (this.connecting.length) return; |
| 2256 | |
| 2257 | this.close(); |
| 2258 | }; |
| 2259 | |
| 2260 | /** |
| 2261 | * Writes a packet. |
| 2262 | * |
| 2263 | * @param {Object} packet |
| 2264 | * @api private |
| 2265 | */ |
| 2266 | |
| 2267 | Manager.prototype.packet = function (packet) { |
| 2268 | debug('writing packet %j', packet); |
| 2269 | var self = this; |
| 2270 | if (packet.query && packet.type === 0) packet.nsp += '?' + packet.query; |
| 2271 | |
| 2272 | if (!self.encoding) { |
| 2273 | // encode, then write to engine with result |
| 2274 | self.encoding = true; |
| 2275 | this.encoder.encode(packet, function (encodedPackets) { |
| 2276 | for (var i = 0; i < encodedPackets.length; i++) { |
| 2277 | self.engine.write(encodedPackets[i], packet.options); |
| 2278 | } |
| 2279 | self.encoding = false; |
| 2280 | self.processPacketQueue(); |
| 2281 | }); |
| 2282 | } else { |
| 2283 | // add packet to the queue |
| 2284 | self.packetBuffer.push(packet); |
| 2285 | } |
| 2286 | }; |
| 2287 | |
| 2288 | /** |
| 2289 | * If packet buffer is non-empty, begins encoding the |
| 2290 | * next packet in line. |
| 2291 | * |
| 2292 | * @api private |
| 2293 | */ |
| 2294 | |
| 2295 | Manager.prototype.processPacketQueue = function () { |
| 2296 | if (this.packetBuffer.length > 0 && !this.encoding) { |
| 2297 | var pack = this.packetBuffer.shift(); |
| 2298 | this.packet(pack); |
| 2299 | } |
| 2300 | }; |
| 2301 | |
| 2302 | /** |
| 2303 | * Clean up transport subscriptions and packet buffer. |
| 2304 | * |
| 2305 | * @api private |
| 2306 | */ |
| 2307 | |
| 2308 | Manager.prototype.cleanup = function () { |
| 2309 | debug('cleanup'); |
| 2310 | |
| 2311 | var subsLength = this.subs.length; |
| 2312 | for (var i = 0; i < subsLength; i++) { |
| 2313 | var sub = this.subs.shift(); |
| 2314 | sub.destroy(); |
| 2315 | } |
| 2316 | |
| 2317 | this.packetBuffer = []; |
| 2318 | this.encoding = false; |
| 2319 | this.lastPing = null; |
| 2320 | |
| 2321 | this.decoder.destroy(); |
| 2322 | }; |
| 2323 | |
| 2324 | /** |
| 2325 | * Close the current socket. |
| 2326 | * |
| 2327 | * @api private |
| 2328 | */ |
| 2329 | |
| 2330 | Manager.prototype.close = Manager.prototype.disconnect = function () { |
| 2331 | debug('disconnect'); |
| 2332 | this.skipReconnect = true; |
| 2333 | this.reconnecting = false; |
| 2334 | if ('opening' === this.readyState) { |
| 2335 | // `onclose` will not fire because |
| 2336 | // an open event never happened |
| 2337 | this.cleanup(); |
| 2338 | } |
| 2339 | this.backoff.reset(); |
| 2340 | this.readyState = 'closed'; |
| 2341 | if (this.engine) this.engine.close(); |
| 2342 | }; |
| 2343 | |
| 2344 | /** |
| 2345 | * Called upon engine close. |
| 2346 | * |
| 2347 | * @api private |
| 2348 | */ |
| 2349 | |
| 2350 | Manager.prototype.onclose = function (reason) { |
| 2351 | debug('onclose'); |
| 2352 | |
| 2353 | this.cleanup(); |
| 2354 | this.backoff.reset(); |
| 2355 | this.readyState = 'closed'; |
| 2356 | this.emit('close', reason); |
| 2357 | |
| 2358 | if (this._reconnection && !this.skipReconnect) { |
| 2359 | this.reconnect(); |
| 2360 | } |
| 2361 | }; |
| 2362 | |
| 2363 | /** |
| 2364 | * Attempt a reconnection. |
| 2365 | * |
| 2366 | * @api private |
| 2367 | */ |
| 2368 | |
| 2369 | Manager.prototype.reconnect = function () { |
| 2370 | if (this.reconnecting || this.skipReconnect) return this; |
| 2371 | |
| 2372 | var self = this; |
| 2373 | |
| 2374 | if (this.backoff.attempts >= this._reconnectionAttempts) { |
| 2375 | debug('reconnect failed'); |
| 2376 | this.backoff.reset(); |
| 2377 | this.emitAll('reconnect_failed'); |
| 2378 | this.reconnecting = false; |
| 2379 | } else { |
| 2380 | var delay = this.backoff.duration(); |
| 2381 | debug('will wait %dms before reconnect attempt', delay); |
| 2382 | |
| 2383 | this.reconnecting = true; |
| 2384 | var timer = setTimeout(function () { |
| 2385 | if (self.skipReconnect) return; |
| 2386 | |
| 2387 | debug('attempting reconnect'); |
| 2388 | self.emitAll('reconnect_attempt', self.backoff.attempts); |
| 2389 | self.emitAll('reconnecting', self.backoff.attempts); |
| 2390 | |
| 2391 | // check again for the case socket closed in above events |
| 2392 | if (self.skipReconnect) return; |
| 2393 | |
| 2394 | self.open(function (err) { |
| 2395 | if (err) { |
| 2396 | debug('reconnect attempt error'); |
| 2397 | self.reconnecting = false; |
| 2398 | self.reconnect(); |
| 2399 | self.emitAll('reconnect_error', err.data); |
| 2400 | } else { |
| 2401 | debug('reconnect success'); |
| 2402 | self.onreconnect(); |
| 2403 | } |
| 2404 | }); |
| 2405 | }, delay); |
| 2406 | |
| 2407 | this.subs.push({ |
| 2408 | destroy: function destroy() { |
| 2409 | clearTimeout(timer); |
| 2410 | } |
| 2411 | }); |
| 2412 | } |
| 2413 | }; |
| 2414 | |
| 2415 | /** |
| 2416 | * Called upon successful reconnect. |
| 2417 | * |
| 2418 | * @api private |
| 2419 | */ |
| 2420 | |
| 2421 | Manager.prototype.onreconnect = function () { |
| 2422 | var attempt = this.backoff.attempts; |
| 2423 | this.reconnecting = false; |
| 2424 | this.backoff.reset(); |
| 2425 | this.updateSocketIds(); |
| 2426 | this.emitAll('reconnect', attempt); |
| 2427 | }; |
| 2428 | |
| 2429 | /***/ }), |
| 2430 | /* 13 */ |
| 2431 | /***/ (function(module, exports, __webpack_require__) { |
| 2432 | |
| 2433 | |
| 2434 | module.exports = __webpack_require__(14); |
| 2435 | |
| 2436 | /** |
| 2437 | * Exports parser |
| 2438 | * |
| 2439 | * @api public |
| 2440 | * |
| 2441 | */ |
| 2442 | module.exports.parser = __webpack_require__(21); |
| 2443 | |
| 2444 | |
| 2445 | /***/ }), |
| 2446 | /* 14 */ |
| 2447 | /***/ (function(module, exports, __webpack_require__) { |
| 2448 | |
| 2449 | /* WEBPACK VAR INJECTION */(function(global) {/** |
| 2450 | * Module dependencies. |
| 2451 | */ |
| 2452 | |
| 2453 | var transports = __webpack_require__(15); |
| 2454 | var Emitter = __webpack_require__(8); |
| 2455 | var debug = __webpack_require__(3)('engine.io-client:socket'); |
| 2456 | var index = __webpack_require__(36); |
| 2457 | var parser = __webpack_require__(21); |
| 2458 | var parseuri = __webpack_require__(2); |
| 2459 | var parseqs = __webpack_require__(30); |
| 2460 | |
| 2461 | /** |
| 2462 | * Module exports. |
| 2463 | */ |
| 2464 | |
| 2465 | module.exports = Socket; |
| 2466 | |
| 2467 | /** |
| 2468 | * Socket constructor. |
| 2469 | * |
| 2470 | * @param {String|Object} uri or options |
| 2471 | * @param {Object} options |
| 2472 | * @api public |
| 2473 | */ |
| 2474 | |
| 2475 | function Socket (uri, opts) { |
| 2476 | if (!(this instanceof Socket)) return new Socket(uri, opts); |
| 2477 | |
| 2478 | opts = opts || {}; |
| 2479 | |
| 2480 | if (uri && 'object' === typeof uri) { |
| 2481 | opts = uri; |
| 2482 | uri = null; |
| 2483 | } |
| 2484 | |
| 2485 | if (uri) { |
| 2486 | uri = parseuri(uri); |
| 2487 | opts.hostname = uri.host; |
| 2488 | opts.secure = uri.protocol === 'https' || uri.protocol === 'wss'; |
| 2489 | opts.port = uri.port; |
| 2490 | if (uri.query) opts.query = uri.query; |
| 2491 | } else if (opts.host) { |
| 2492 | opts.hostname = parseuri(opts.host).host; |
| 2493 | } |
| 2494 | |
| 2495 | this.secure = null != opts.secure ? opts.secure |
| 2496 | : (global.location && 'https:' === location.protocol); |
| 2497 | |
| 2498 | if (opts.hostname && !opts.port) { |
| 2499 | // if no port is specified manually, use the protocol default |
| 2500 | opts.port = this.secure ? '443' : '80'; |
| 2501 | } |
| 2502 | |
| 2503 | this.agent = opts.agent || false; |
| 2504 | this.hostname = opts.hostname || |
| 2505 | (global.location ? location.hostname : 'localhost'); |
| 2506 | this.port = opts.port || (global.location && location.port |
| 2507 | ? location.port |
| 2508 | : (this.secure ? 443 : 80)); |
| 2509 | this.query = opts.query || {}; |
| 2510 | if ('string' === typeof this.query) this.query = parseqs.decode(this.query); |
| 2511 | this.upgrade = false !== opts.upgrade; |
| 2512 | this.path = (opts.path || '/engine.io').replace(/\/$/, '') + '/'; |
| 2513 | this.forceJSONP = !!opts.forceJSONP; |
| 2514 | this.jsonp = false !== opts.jsonp; |
| 2515 | this.forceBase64 = !!opts.forceBase64; |
| 2516 | this.enablesXDR = !!opts.enablesXDR; |
| 2517 | this.timestampParam = opts.timestampParam || 't'; |
| 2518 | this.timestampRequests = opts.timestampRequests; |
| 2519 | this.transports = opts.transports || ['polling', 'websocket']; |
| 2520 | this.transportOptions = opts.transportOptions || {}; |
| 2521 | this.readyState = ''; |
| 2522 | this.writeBuffer = []; |
| 2523 | this.prevBufferLen = 0; |
| 2524 | this.policyPort = opts.policyPort || 843; |
| 2525 | this.rememberUpgrade = opts.rememberUpgrade || false; |
| 2526 | this.binaryType = null; |
| 2527 | this.onlyBinaryUpgrades = opts.onlyBinaryUpgrades; |
| 2528 | this.perMessageDeflate = false !== opts.perMessageDeflate ? (opts.perMessageDeflate || {}) : false; |
| 2529 | |
| 2530 | if (true === this.perMessageDeflate) this.perMessageDeflate = {}; |
| 2531 | if (this.perMessageDeflate && null == this.perMessageDeflate.threshold) { |
| 2532 | this.perMessageDeflate.threshold = 1024; |
| 2533 | } |
| 2534 | |
| 2535 | // SSL options for Node.js client |
| 2536 | this.pfx = opts.pfx || null; |
| 2537 | this.key = opts.key || null; |
| 2538 | this.passphrase = opts.passphrase || null; |
| 2539 | this.cert = opts.cert || null; |
| 2540 | this.ca = opts.ca || null; |
| 2541 | this.ciphers = opts.ciphers || null; |
| 2542 | this.rejectUnauthorized = opts.rejectUnauthorized === undefined ? true : opts.rejectUnauthorized; |
| 2543 | this.forceNode = !!opts.forceNode; |
| 2544 | |
| 2545 | // other options for Node.js client |
| 2546 | var freeGlobal = typeof global === 'object' && global; |
| 2547 | if (freeGlobal.global === freeGlobal) { |
| 2548 | if (opts.extraHeaders && Object.keys(opts.extraHeaders).length > 0) { |
| 2549 | this.extraHeaders = opts.extraHeaders; |
| 2550 | } |
| 2551 | |
| 2552 | if (opts.localAddress) { |
| 2553 | this.localAddress = opts.localAddress; |
| 2554 | } |
| 2555 | } |
| 2556 | |
| 2557 | // set on handshake |
| 2558 | this.id = null; |
| 2559 | this.upgrades = null; |
| 2560 | this.pingInterval = null; |
| 2561 | this.pingTimeout = null; |
| 2562 | |
| 2563 | // set on heartbeat |
| 2564 | this.pingIntervalTimer = null; |
| 2565 | this.pingTimeoutTimer = null; |
| 2566 | |
| 2567 | this.open(); |
| 2568 | } |
| 2569 | |
| 2570 | Socket.priorWebsocketSuccess = false; |
| 2571 | |
| 2572 | /** |
| 2573 | * Mix in `Emitter`. |
| 2574 | */ |
| 2575 | |
| 2576 | Emitter(Socket.prototype); |
| 2577 | |
| 2578 | /** |
| 2579 | * Protocol version. |
| 2580 | * |
| 2581 | * @api public |
| 2582 | */ |
| 2583 | |
| 2584 | Socket.protocol = parser.protocol; // this is an int |
| 2585 | |
| 2586 | /** |
| 2587 | * Expose deps for legacy compatibility |
| 2588 | * and standalone browser access. |
| 2589 | */ |
| 2590 | |
| 2591 | Socket.Socket = Socket; |
| 2592 | Socket.Transport = __webpack_require__(20); |
| 2593 | Socket.transports = __webpack_require__(15); |
| 2594 | Socket.parser = __webpack_require__(21); |
| 2595 | |
| 2596 | /** |
| 2597 | * Creates transport of the given type. |
| 2598 | * |
| 2599 | * @param {String} transport name |
| 2600 | * @return {Transport} |
| 2601 | * @api private |
| 2602 | */ |
| 2603 | |
| 2604 | Socket.prototype.createTransport = function (name) { |
| 2605 | debug('creating transport "%s"', name); |
| 2606 | var query = clone(this.query); |
| 2607 | |
| 2608 | // append engine.io protocol identifier |
| 2609 | query.EIO = parser.protocol; |
| 2610 | |
| 2611 | // transport name |
| 2612 | query.transport = name; |
| 2613 | |
| 2614 | // per-transport options |
| 2615 | var options = this.transportOptions[name] || {}; |
| 2616 | |
| 2617 | // session id if we already have one |
| 2618 | if (this.id) query.sid = this.id; |
| 2619 | |
| 2620 | var transport = new transports[name]({ |
| 2621 | query: query, |
| 2622 | socket: this, |
| 2623 | agent: options.agent || this.agent, |
| 2624 | hostname: options.hostname || this.hostname, |
| 2625 | port: options.port || this.port, |
| 2626 | secure: options.secure || this.secure, |
| 2627 | path: options.path || this.path, |
| 2628 | forceJSONP: options.forceJSONP || this.forceJSONP, |
| 2629 | jsonp: options.jsonp || this.jsonp, |
| 2630 | forceBase64: options.forceBase64 || this.forceBase64, |
| 2631 | enablesXDR: options.enablesXDR || this.enablesXDR, |
| 2632 | timestampRequests: options.timestampRequests || this.timestampRequests, |
| 2633 | timestampParam: options.timestampParam || this.timestampParam, |
| 2634 | policyPort: options.policyPort || this.policyPort, |
| 2635 | pfx: options.pfx || this.pfx, |
| 2636 | key: options.key || this.key, |
| 2637 | passphrase: options.passphrase || this.passphrase, |
| 2638 | cert: options.cert || this.cert, |
| 2639 | ca: options.ca || this.ca, |
| 2640 | ciphers: options.ciphers || this.ciphers, |
| 2641 | rejectUnauthorized: options.rejectUnauthorized || this.rejectUnauthorized, |
| 2642 | perMessageDeflate: options.perMessageDeflate || this.perMessageDeflate, |
| 2643 | extraHeaders: options.extraHeaders || this.extraHeaders, |
| 2644 | forceNode: options.forceNode || this.forceNode, |
| 2645 | localAddress: options.localAddress || this.localAddress, |
| 2646 | requestTimeout: options.requestTimeout || this.requestTimeout, |
| 2647 | protocols: options.protocols || void (0) |
| 2648 | }); |
| 2649 | |
| 2650 | return transport; |
| 2651 | }; |
| 2652 | |
| 2653 | function clone (obj) { |
| 2654 | var o = {}; |
| 2655 | for (var i in obj) { |
| 2656 | if (obj.hasOwnProperty(i)) { |
| 2657 | o[i] = obj[i]; |
| 2658 | } |
| 2659 | } |
| 2660 | return o; |
| 2661 | } |
| 2662 | |
| 2663 | /** |
| 2664 | * Initializes transport to use and starts probe. |
| 2665 | * |
| 2666 | * @api private |
| 2667 | */ |
| 2668 | Socket.prototype.open = function () { |
| 2669 | var transport; |
| 2670 | if (this.rememberUpgrade && Socket.priorWebsocketSuccess && this.transports.indexOf('websocket') !== -1) { |
| 2671 | transport = 'websocket'; |
| 2672 | } else if (0 === this.transports.length) { |
| 2673 | // Emit error on next tick so it can be listened to |
| 2674 | var self = this; |
| 2675 | setTimeout(function () { |
| 2676 | self.emit('error', 'No transports available'); |
| 2677 | }, 0); |
| 2678 | return; |
| 2679 | } else { |
| 2680 | transport = this.transports[0]; |
| 2681 | } |
| 2682 | this.readyState = 'opening'; |
| 2683 | |
| 2684 | // Retry with the next transport if the transport is disabled (jsonp: false) |
| 2685 | try { |
| 2686 | transport = this.createTransport(transport); |
| 2687 | } catch (e) { |
| 2688 | this.transports.shift(); |
| 2689 | this.open(); |
| 2690 | return; |
| 2691 | } |
| 2692 | |
| 2693 | transport.open(); |
| 2694 | this.setTransport(transport); |
| 2695 | }; |
| 2696 | |
| 2697 | /** |
| 2698 | * Sets the current transport. Disables the existing one (if any). |
| 2699 | * |
| 2700 | * @api private |
| 2701 | */ |
| 2702 | |
| 2703 | Socket.prototype.setTransport = function (transport) { |
| 2704 | debug('setting transport %s', transport.name); |
| 2705 | var self = this; |
| 2706 | |
| 2707 | if (this.transport) { |
| 2708 | debug('clearing existing transport %s', this.transport.name); |
| 2709 | this.transport.removeAllListeners(); |
| 2710 | } |
| 2711 | |
| 2712 | // set up transport |
| 2713 | this.transport = transport; |
| 2714 | |
| 2715 | // set up transport listeners |
| 2716 | transport |
| 2717 | .on('drain', function () { |
| 2718 | self.onDrain(); |
| 2719 | }) |
| 2720 | .on('packet', function (packet) { |
| 2721 | self.onPacket(packet); |
| 2722 | }) |
| 2723 | .on('error', function (e) { |
| 2724 | self.onError(e); |
| 2725 | }) |
| 2726 | .on('close', function () { |
| 2727 | self.onClose('transport close'); |
| 2728 | }); |
| 2729 | }; |
| 2730 | |
| 2731 | /** |
| 2732 | * Probes a transport. |
| 2733 | * |
| 2734 | * @param {String} transport name |
| 2735 | * @api private |
| 2736 | */ |
| 2737 | |
| 2738 | Socket.prototype.probe = function (name) { |
| 2739 | debug('probing transport "%s"', name); |
| 2740 | var transport = this.createTransport(name, { probe: 1 }); |
| 2741 | var failed = false; |
| 2742 | var self = this; |
| 2743 | |
| 2744 | Socket.priorWebsocketSuccess = false; |
| 2745 | |
| 2746 | function onTransportOpen () { |
| 2747 | if (self.onlyBinaryUpgrades) { |
| 2748 | var upgradeLosesBinary = !this.supportsBinary && self.transport.supportsBinary; |
| 2749 | failed = failed || upgradeLosesBinary; |
| 2750 | } |
| 2751 | if (failed) return; |
| 2752 | |
| 2753 | debug('probe transport "%s" opened', name); |
| 2754 | transport.send([{ type: 'ping', data: 'probe' }]); |
| 2755 | transport.once('packet', function (msg) { |
| 2756 | if (failed) return; |
| 2757 | if ('pong' === msg.type && 'probe' === msg.data) { |
| 2758 | debug('probe transport "%s" pong', name); |
| 2759 | self.upgrading = true; |
| 2760 | self.emit('upgrading', transport); |
| 2761 | if (!transport) return; |
| 2762 | Socket.priorWebsocketSuccess = 'websocket' === transport.name; |
| 2763 | |
| 2764 | debug('pausing current transport "%s"', self.transport.name); |
| 2765 | self.transport.pause(function () { |
| 2766 | if (failed) return; |
| 2767 | if ('closed' === self.readyState) return; |
| 2768 | debug('changing transport and sending upgrade packet'); |
| 2769 | |
| 2770 | cleanup(); |
| 2771 | |
| 2772 | self.setTransport(transport); |
| 2773 | transport.send([{ type: 'upgrade' }]); |
| 2774 | self.emit('upgrade', transport); |
| 2775 | transport = null; |
| 2776 | self.upgrading = false; |
| 2777 | self.flush(); |
| 2778 | }); |
| 2779 | } else { |
| 2780 | debug('probe transport "%s" failed', name); |
| 2781 | var err = new Error('probe error'); |
| 2782 | err.transport = transport.name; |
| 2783 | self.emit('upgradeError', err); |
| 2784 | } |
| 2785 | }); |
| 2786 | } |
| 2787 | |
| 2788 | function freezeTransport () { |
| 2789 | if (failed) return; |
| 2790 | |
| 2791 | // Any callback called by transport should be ignored since now |
| 2792 | failed = true; |
| 2793 | |
| 2794 | cleanup(); |
| 2795 | |
| 2796 | transport.close(); |
| 2797 | transport = null; |
| 2798 | } |
| 2799 | |
| 2800 | // Handle any error that happens while probing |
| 2801 | function onerror (err) { |
| 2802 | var error = new Error('probe error: ' + err); |
| 2803 | error.transport = transport.name; |
| 2804 | |
| 2805 | freezeTransport(); |
| 2806 | |
| 2807 | debug('probe transport "%s" failed because of error: %s', name, err); |
| 2808 | |
| 2809 | self.emit('upgradeError', error); |
| 2810 | } |
| 2811 | |
| 2812 | function onTransportClose () { |
| 2813 | onerror('transport closed'); |
| 2814 | } |
| 2815 | |
| 2816 | // When the socket is closed while we're probing |
| 2817 | function onclose () { |
| 2818 | onerror('socket closed'); |
| 2819 | } |
| 2820 | |
| 2821 | // When the socket is upgraded while we're probing |
| 2822 | function onupgrade (to) { |
| 2823 | if (transport && to.name !== transport.name) { |
| 2824 | debug('"%s" works - aborting "%s"', to.name, transport.name); |
| 2825 | freezeTransport(); |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | // Remove all listeners on the transport and on self |
| 2830 | function cleanup () { |
| 2831 | transport.removeListener('open', onTransportOpen); |
| 2832 | transport.removeListener('error', onerror); |
| 2833 | transport.removeListener('close', onTransportClose); |
| 2834 | self.removeListener('close', onclose); |
| 2835 | self.removeListener('upgrading', onupgrade); |
| 2836 | } |
| 2837 | |
| 2838 | transport.once('open', onTransportOpen); |
| 2839 | transport.once('error', onerror); |
| 2840 | transport.once('close', onTransportClose); |
| 2841 | |
| 2842 | this.once('close', onclose); |
| 2843 | this.once('upgrading', onupgrade); |
| 2844 | |
| 2845 | transport.open(); |
| 2846 | }; |
| 2847 | |
| 2848 | /** |
| 2849 | * Called when connection is deemed open. |
| 2850 | * |
| 2851 | * @api public |
| 2852 | */ |
| 2853 | |
| 2854 | Socket.prototype.onOpen = function () { |
| 2855 | debug('socket open'); |
| 2856 | this.readyState = 'open'; |
| 2857 | Socket.priorWebsocketSuccess = 'websocket' === this.transport.name; |
| 2858 | this.emit('open'); |
| 2859 | this.flush(); |
| 2860 | |
| 2861 | // we check for `readyState` in case an `open` |
| 2862 | // listener already closed the socket |
| 2863 | if ('open' === this.readyState && this.upgrade && this.transport.pause) { |
| 2864 | debug('starting upgrade probes'); |
| 2865 | for (var i = 0, l = this.upgrades.length; i < l; i++) { |
| 2866 | this.probe(this.upgrades[i]); |
| 2867 | } |
| 2868 | } |
| 2869 | }; |
| 2870 | |
| 2871 | /** |
| 2872 | * Handles a packet. |
| 2873 | * |
| 2874 | * @api private |
| 2875 | */ |
| 2876 | |
| 2877 | Socket.prototype.onPacket = function (packet) { |
| 2878 | if ('opening' === this.readyState || 'open' === this.readyState || |
| 2879 | 'closing' === this.readyState) { |
| 2880 | debug('socket receive: type "%s", data "%s"', packet.type, packet.data); |
| 2881 | |
| 2882 | this.emit('packet', packet); |
| 2883 | |
| 2884 | // Socket is live - any packet counts |
| 2885 | this.emit('heartbeat'); |
| 2886 | |
| 2887 | switch (packet.type) { |
| 2888 | case 'open': |
| 2889 | this.onHandshake(JSON.parse(packet.data)); |
| 2890 | break; |
| 2891 | |
| 2892 | case 'pong': |
| 2893 | this.setPing(); |
| 2894 | this.emit('pong'); |
| 2895 | break; |
| 2896 | |
| 2897 | case 'error': |
| 2898 | var err = new Error('server error'); |
| 2899 | err.code = packet.data; |
| 2900 | this.onError(err); |
| 2901 | break; |
| 2902 | |
| 2903 | case 'message': |
| 2904 | this.emit('data', packet.data); |
| 2905 | this.emit('message', packet.data); |
| 2906 | break; |
| 2907 | } |
| 2908 | } else { |
| 2909 | debug('packet received with socket readyState "%s"', this.readyState); |
| 2910 | } |
| 2911 | }; |
| 2912 | |
| 2913 | /** |
| 2914 | * Called upon handshake completion. |
| 2915 | * |
| 2916 | * @param {Object} handshake obj |
| 2917 | * @api private |
| 2918 | */ |
| 2919 | |
| 2920 | Socket.prototype.onHandshake = function (data) { |
| 2921 | this.emit('handshake', data); |
| 2922 | this.id = data.sid; |
| 2923 | this.transport.query.sid = data.sid; |
| 2924 | this.upgrades = this.filterUpgrades(data.upgrades); |
| 2925 | this.pingInterval = data.pingInterval; |
| 2926 | this.pingTimeout = data.pingTimeout; |
| 2927 | this.onOpen(); |
| 2928 | // In case open handler closes socket |
| 2929 | if ('closed' === this.readyState) return; |
| 2930 | this.setPing(); |
| 2931 | |
| 2932 | // Prolong liveness of socket on heartbeat |
| 2933 | this.removeListener('heartbeat', this.onHeartbeat); |
| 2934 | this.on('heartbeat', this.onHeartbeat); |
| 2935 | }; |
| 2936 | |
| 2937 | /** |
| 2938 | * Resets ping timeout. |
| 2939 | * |
| 2940 | * @api private |
| 2941 | */ |
| 2942 | |
| 2943 | Socket.prototype.onHeartbeat = function (timeout) { |
| 2944 | clearTimeout(this.pingTimeoutTimer); |
| 2945 | var self = this; |
| 2946 | self.pingTimeoutTimer = setTimeout(function () { |
| 2947 | if ('closed' === self.readyState) return; |
| 2948 | self.onClose('ping timeout'); |
| 2949 | }, timeout || (self.pingInterval + self.pingTimeout)); |
| 2950 | }; |
| 2951 | |
| 2952 | /** |
| 2953 | * Pings server every `this.pingInterval` and expects response |
| 2954 | * within `this.pingTimeout` or closes connection. |
| 2955 | * |
| 2956 | * @api private |
| 2957 | */ |
| 2958 | |
| 2959 | Socket.prototype.setPing = function () { |
| 2960 | var self = this; |
| 2961 | clearTimeout(self.pingIntervalTimer); |
| 2962 | self.pingIntervalTimer = setTimeout(function () { |
| 2963 | debug('writing ping packet - expecting pong within %sms', self.pingTimeout); |
| 2964 | self.ping(); |
| 2965 | self.onHeartbeat(self.pingTimeout); |
| 2966 | }, self.pingInterval); |
| 2967 | }; |
| 2968 | |
| 2969 | /** |
| 2970 | * Sends a ping packet. |
| 2971 | * |
| 2972 | * @api private |
| 2973 | */ |
| 2974 | |
| 2975 | Socket.prototype.ping = function () { |
| 2976 | var self = this; |
| 2977 | this.sendPacket('ping', function () { |
| 2978 | self.emit('ping'); |
| 2979 | }); |
| 2980 | }; |
| 2981 | |
| 2982 | /** |
| 2983 | * Called on `drain` event |
| 2984 | * |
| 2985 | * @api private |
| 2986 | */ |
| 2987 | |
| 2988 | Socket.prototype.onDrain = function () { |
| 2989 | this.writeBuffer.splice(0, this.prevBufferLen); |
| 2990 | |
| 2991 | // setting prevBufferLen = 0 is very important |
| 2992 | // for example, when upgrading, upgrade packet is sent over, |
| 2993 | // and a nonzero prevBufferLen could cause problems on `drain` |
| 2994 | this.prevBufferLen = 0; |
| 2995 | |
| 2996 | if (0 === this.writeBuffer.length) { |
| 2997 | this.emit('drain'); |
| 2998 | } else { |
| 2999 | this.flush(); |
| 3000 | } |
| 3001 | }; |
| 3002 | |
| 3003 | /** |
| 3004 | * Flush write buffers. |
| 3005 | * |
| 3006 | * @api private |
| 3007 | */ |
| 3008 | |
| 3009 | Socket.prototype.flush = function () { |
| 3010 | if ('closed' !== this.readyState && this.transport.writable && |
| 3011 | !this.upgrading && this.writeBuffer.length) { |
| 3012 | debug('flushing %d packets in socket', this.writeBuffer.length); |
| 3013 | this.transport.send(this.writeBuffer); |
| 3014 | // keep track of current length of writeBuffer |
| 3015 | // splice writeBuffer and callbackBuffer on `drain` |
| 3016 | this.prevBufferLen = this.writeBuffer.length; |
| 3017 | this.emit('flush'); |
| 3018 | } |
| 3019 | }; |
| 3020 | |
| 3021 | /** |
| 3022 | * Sends a message. |
| 3023 | * |
| 3024 | * @param {String} message. |
| 3025 | * @param {Function} callback function. |
| 3026 | * @param {Object} options. |
| 3027 | * @return {Socket} for chaining. |
| 3028 | * @api public |
| 3029 | */ |
| 3030 | |
| 3031 | Socket.prototype.write = |
| 3032 | Socket.prototype.send = function (msg, options, fn) { |
| 3033 | this.sendPacket('message', msg, options, fn); |
| 3034 | return this; |
| 3035 | }; |
| 3036 | |
| 3037 | /** |
| 3038 | * Sends a packet. |
| 3039 | * |
| 3040 | * @param {String} packet type. |
| 3041 | * @param {String} data. |
| 3042 | * @param {Object} options. |
| 3043 | * @param {Function} callback function. |
| 3044 | * @api private |
| 3045 | */ |
| 3046 | |
| 3047 | Socket.prototype.sendPacket = function (type, data, options, fn) { |
| 3048 | if ('function' === typeof data) { |
| 3049 | fn = data; |
| 3050 | data = undefined; |
| 3051 | } |
| 3052 | |
| 3053 | if ('function' === typeof options) { |
| 3054 | fn = options; |
| 3055 | options = null; |
| 3056 | } |
| 3057 | |
| 3058 | if ('closing' === this.readyState || 'closed' === this.readyState) { |
| 3059 | return; |
| 3060 | } |
| 3061 | |
| 3062 | options = options || {}; |
| 3063 | options.compress = false !== options.compress; |
| 3064 | |
| 3065 | var packet = { |
| 3066 | type: type, |
| 3067 | data: data, |
| 3068 | options: options |
| 3069 | }; |
| 3070 | this.emit('packetCreate', packet); |
| 3071 | this.writeBuffer.push(packet); |
| 3072 | if (fn) this.once('flush', fn); |
| 3073 | this.flush(); |
| 3074 | }; |
| 3075 | |
| 3076 | /** |
| 3077 | * Closes the connection. |
| 3078 | * |
| 3079 | * @api private |
| 3080 | */ |
| 3081 | |
| 3082 | Socket.prototype.close = function () { |
| 3083 | if ('opening' === this.readyState || 'open' === this.readyState) { |
| 3084 | this.readyState = 'closing'; |
| 3085 | |
| 3086 | var self = this; |
| 3087 | |
| 3088 | if (this.writeBuffer.length) { |
| 3089 | this.once('drain', function () { |
| 3090 | if (this.upgrading) { |
| 3091 | waitForUpgrade(); |
| 3092 | } else { |
| 3093 | close(); |
| 3094 | } |
| 3095 | }); |
| 3096 | } else if (this.upgrading) { |
| 3097 | waitForUpgrade(); |
| 3098 | } else { |
| 3099 | close(); |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | function close () { |
| 3104 | self.onClose('forced close'); |
| 3105 | debug('socket closing - telling transport to close'); |
| 3106 | self.transport.close(); |
| 3107 | } |
| 3108 | |
| 3109 | function cleanupAndClose () { |
| 3110 | self.removeListener('upgrade', cleanupAndClose); |
| 3111 | self.removeListener('upgradeError', cleanupAndClose); |
| 3112 | close(); |
| 3113 | } |
| 3114 | |
| 3115 | function waitForUpgrade () { |
| 3116 | // wait for upgrade to finish since we can't send packets while pausing a transport |
| 3117 | self.once('upgrade', cleanupAndClose); |
| 3118 | self.once('upgradeError', cleanupAndClose); |
| 3119 | } |
| 3120 | |
| 3121 | return this; |
| 3122 | }; |
| 3123 | |
| 3124 | /** |
| 3125 | * Called upon transport error |
| 3126 | * |
| 3127 | * @api private |
| 3128 | */ |
| 3129 | |
| 3130 | Socket.prototype.onError = function (err) { |
| 3131 | debug('socket error %j', err); |
| 3132 | Socket.priorWebsocketSuccess = false; |
| 3133 | this.emit('error', err); |
| 3134 | this.onClose('transport error', err); |
| 3135 | }; |
| 3136 | |
| 3137 | /** |
| 3138 | * Called upon transport close. |
| 3139 | * |
| 3140 | * @api private |
| 3141 | */ |
| 3142 | |
| 3143 | Socket.prototype.onClose = function (reason, desc) { |
| 3144 | if ('opening' === this.readyState || 'open' === this.readyState || 'closing' === this.readyState) { |
| 3145 | debug('socket close with reason: "%s"', reason); |
| 3146 | var self = this; |
| 3147 | |
| 3148 | // clear timers |
| 3149 | clearTimeout(this.pingIntervalTimer); |
| 3150 | clearTimeout(this.pingTimeoutTimer); |
| 3151 | |
| 3152 | // stop event from firing again for transport |
| 3153 | this.transport.removeAllListeners('close'); |
| 3154 | |
| 3155 | // ensure transport won't stay open |
| 3156 | this.transport.close(); |
| 3157 | |
| 3158 | // ignore further transport communication |
| 3159 | this.transport.removeAllListeners(); |
| 3160 | |
| 3161 | // set ready state |
| 3162 | this.readyState = 'closed'; |
| 3163 | |
| 3164 | // clear session id |
| 3165 | this.id = null; |
| 3166 | |
| 3167 | // emit close event |
| 3168 | this.emit('close', reason, desc); |
| 3169 | |
| 3170 | // clean buffers after, so users can still |
| 3171 | // grab the buffers on `close` event |
| 3172 | self.writeBuffer = []; |
| 3173 | self.prevBufferLen = 0; |
| 3174 | } |
| 3175 | }; |
| 3176 | |
| 3177 | /** |
| 3178 | * Filters upgrades, returning only those matching client transports. |
| 3179 | * |
| 3180 | * @param {Array} server upgrades |
| 3181 | * @api private |
| 3182 | * |
| 3183 | */ |
| 3184 | |
| 3185 | Socket.prototype.filterUpgrades = function (upgrades) { |
| 3186 | var filteredUpgrades = []; |
| 3187 | for (var i = 0, j = upgrades.length; i < j; i++) { |
| 3188 | if (~index(this.transports, upgrades[i])) filteredUpgrades.push(upgrades[i]); |
| 3189 | } |
| 3190 | return filteredUpgrades; |
| 3191 | }; |
| 3192 | |
| 3193 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 3194 | |
| 3195 | /***/ }), |
| 3196 | /* 15 */ |
| 3197 | /***/ (function(module, exports, __webpack_require__) { |
| 3198 | |
| 3199 | /* WEBPACK VAR INJECTION */(function(global) {/** |
| 3200 | * Module dependencies |
| 3201 | */ |
| 3202 | |
| 3203 | var XMLHttpRequest = __webpack_require__(16); |
| 3204 | var XHR = __webpack_require__(18); |
| 3205 | var JSONP = __webpack_require__(33); |
| 3206 | var websocket = __webpack_require__(34); |
| 3207 | |
| 3208 | /** |
| 3209 | * Export transports. |
| 3210 | */ |
| 3211 | |
| 3212 | exports.polling = polling; |
| 3213 | exports.websocket = websocket; |
| 3214 | |
| 3215 | /** |
| 3216 | * Polling transport polymorphic constructor. |
| 3217 | * Decides on xhr vs jsonp based on feature detection. |
| 3218 | * |
| 3219 | * @api private |
| 3220 | */ |
| 3221 | |
| 3222 | function polling (opts) { |
| 3223 | var xhr; |
| 3224 | var xd = false; |
| 3225 | var xs = false; |
| 3226 | var jsonp = false !== opts.jsonp; |
| 3227 | |
| 3228 | if (global.location) { |
| 3229 | var isSSL = 'https:' === location.protocol; |
| 3230 | var port = location.port; |
| 3231 | |
| 3232 | // some user agents have empty `location.port` |
| 3233 | if (!port) { |
| 3234 | port = isSSL ? 443 : 80; |
| 3235 | } |
| 3236 | |
| 3237 | xd = opts.hostname !== location.hostname || port !== opts.port; |
| 3238 | xs = opts.secure !== isSSL; |
| 3239 | } |
| 3240 | |
| 3241 | opts.xdomain = xd; |
| 3242 | opts.xscheme = xs; |
| 3243 | xhr = new XMLHttpRequest(opts); |
| 3244 | |
| 3245 | if ('open' in xhr && !opts.forceJSONP) { |
| 3246 | return new XHR(opts); |
| 3247 | } else { |
| 3248 | if (!jsonp) throw new Error('JSONP disabled'); |
| 3249 | return new JSONP(opts); |
| 3250 | } |
| 3251 | } |
| 3252 | |
| 3253 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 3254 | |
| 3255 | /***/ }), |
| 3256 | /* 16 */ |
| 3257 | /***/ (function(module, exports, __webpack_require__) { |
| 3258 | |
| 3259 | /* WEBPACK VAR INJECTION */(function(global) {// browser shim for xmlhttprequest module |
| 3260 | |
| 3261 | var hasCORS = __webpack_require__(17); |
| 3262 | |
| 3263 | module.exports = function (opts) { |
| 3264 | var xdomain = opts.xdomain; |
| 3265 | |
| 3266 | // scheme must be same when usign XDomainRequest |
| 3267 | // http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx |
| 3268 | var xscheme = opts.xscheme; |
| 3269 | |
| 3270 | // XDomainRequest has a flow of not sending cookie, therefore it should be disabled as a default. |
| 3271 | // https://github.com/Automattic/engine.io-client/pull/217 |
| 3272 | var enablesXDR = opts.enablesXDR; |
| 3273 | |
| 3274 | // XMLHttpRequest can be disabled on IE |
| 3275 | try { |
| 3276 | if ('undefined' !== typeof XMLHttpRequest && (!xdomain || hasCORS)) { |
| 3277 | return new XMLHttpRequest(); |
| 3278 | } |
| 3279 | } catch (e) { } |
| 3280 | |
| 3281 | // Use XDomainRequest for IE8 if enablesXDR is true |
| 3282 | // because loading bar keeps flashing when using jsonp-polling |
| 3283 | // https://github.com/yujiosaka/socke.io-ie8-loading-example |
| 3284 | try { |
| 3285 | if ('undefined' !== typeof XDomainRequest && !xscheme && enablesXDR) { |
| 3286 | return new XDomainRequest(); |
| 3287 | } |
| 3288 | } catch (e) { } |
| 3289 | |
| 3290 | if (!xdomain) { |
| 3291 | try { |
| 3292 | return new global[['Active'].concat('Object').join('X')]('Microsoft.XMLHTTP'); |
| 3293 | } catch (e) { } |
| 3294 | } |
| 3295 | }; |
| 3296 | |
| 3297 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 3298 | |
| 3299 | /***/ }), |
| 3300 | /* 17 */ |
| 3301 | /***/ (function(module, exports) { |
| 3302 | |
| 3303 | |
| 3304 | /** |
| 3305 | * Module exports. |
| 3306 | * |
| 3307 | * Logic borrowed from Modernizr: |
| 3308 | * |
| 3309 | * - https://github.com/Modernizr/Modernizr/blob/master/feature-detects/cors.js |
| 3310 | */ |
| 3311 | |
| 3312 | try { |
| 3313 | module.exports = typeof XMLHttpRequest !== 'undefined' && |
| 3314 | 'withCredentials' in new XMLHttpRequest(); |
| 3315 | } catch (err) { |
| 3316 | // if XMLHttp support is disabled in IE then it will throw |
| 3317 | // when trying to create |
| 3318 | module.exports = false; |
| 3319 | } |
| 3320 | |
| 3321 | |
| 3322 | /***/ }), |
| 3323 | /* 18 */ |
| 3324 | /***/ (function(module, exports, __webpack_require__) { |
| 3325 | |
| 3326 | /* WEBPACK VAR INJECTION */(function(global) {/** |
| 3327 | * Module requirements. |
| 3328 | */ |
| 3329 | |
| 3330 | var XMLHttpRequest = __webpack_require__(16); |
| 3331 | var Polling = __webpack_require__(19); |
| 3332 | var Emitter = __webpack_require__(8); |
| 3333 | var inherit = __webpack_require__(31); |
| 3334 | var debug = __webpack_require__(3)('engine.io-client:polling-xhr'); |
| 3335 | |
| 3336 | /** |
| 3337 | * Module exports. |
| 3338 | */ |
| 3339 | |
| 3340 | module.exports = XHR; |
| 3341 | module.exports.Request = Request; |
| 3342 | |
| 3343 | /** |
| 3344 | * Empty function |
| 3345 | */ |
| 3346 | |
| 3347 | function empty () {} |
| 3348 | |
| 3349 | /** |
| 3350 | * XHR Polling constructor. |
| 3351 | * |
| 3352 | * @param {Object} opts |
| 3353 | * @api public |
| 3354 | */ |
| 3355 | |
| 3356 | function XHR (opts) { |
| 3357 | Polling.call(this, opts); |
| 3358 | this.requestTimeout = opts.requestTimeout; |
| 3359 | this.extraHeaders = opts.extraHeaders; |
| 3360 | |
| 3361 | if (global.location) { |
| 3362 | var isSSL = 'https:' === location.protocol; |
| 3363 | var port = location.port; |
| 3364 | |
| 3365 | // some user agents have empty `location.port` |
| 3366 | if (!port) { |
| 3367 | port = isSSL ? 443 : 80; |
| 3368 | } |
| 3369 | |
| 3370 | this.xd = opts.hostname !== global.location.hostname || |
| 3371 | port !== opts.port; |
| 3372 | this.xs = opts.secure !== isSSL; |
| 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | /** |
| 3377 | * Inherits from Polling. |
| 3378 | */ |
| 3379 | |
| 3380 | inherit(XHR, Polling); |
| 3381 | |
| 3382 | /** |
| 3383 | * XHR supports binary |
| 3384 | */ |
| 3385 | |
| 3386 | XHR.prototype.supportsBinary = true; |
| 3387 | |
| 3388 | /** |
| 3389 | * Creates a request. |
| 3390 | * |
| 3391 | * @param {String} method |
| 3392 | * @api private |
| 3393 | */ |
| 3394 | |
| 3395 | XHR.prototype.request = function (opts) { |
| 3396 | opts = opts || {}; |
| 3397 | opts.uri = this.uri(); |
| 3398 | opts.xd = this.xd; |
| 3399 | opts.xs = this.xs; |
| 3400 | opts.agent = this.agent || false; |
| 3401 | opts.supportsBinary = this.supportsBinary; |
| 3402 | opts.enablesXDR = this.enablesXDR; |
| 3403 | |
| 3404 | // SSL options for Node.js client |
| 3405 | opts.pfx = this.pfx; |
| 3406 | opts.key = this.key; |
| 3407 | opts.passphrase = this.passphrase; |
| 3408 | opts.cert = this.cert; |
| 3409 | opts.ca = this.ca; |
| 3410 | opts.ciphers = this.ciphers; |
| 3411 | opts.rejectUnauthorized = this.rejectUnauthorized; |
| 3412 | opts.requestTimeout = this.requestTimeout; |
| 3413 | |
| 3414 | // other options for Node.js client |
| 3415 | opts.extraHeaders = this.extraHeaders; |
| 3416 | |
| 3417 | return new Request(opts); |
| 3418 | }; |
| 3419 | |
| 3420 | /** |
| 3421 | * Sends data. |
| 3422 | * |
| 3423 | * @param {String} data to send. |
| 3424 | * @param {Function} called upon flush. |
| 3425 | * @api private |
| 3426 | */ |
| 3427 | |
| 3428 | XHR.prototype.doWrite = function (data, fn) { |
| 3429 | var isBinary = typeof data !== 'string' && data !== undefined; |
| 3430 | var req = this.request({ method: 'POST', data: data, isBinary: isBinary }); |
| 3431 | var self = this; |
| 3432 | req.on('success', fn); |
| 3433 | req.on('error', function (err) { |
| 3434 | self.onError('xhr post error', err); |
| 3435 | }); |
| 3436 | this.sendXhr = req; |
| 3437 | }; |
| 3438 | |
| 3439 | /** |
| 3440 | * Starts a poll cycle. |
| 3441 | * |
| 3442 | * @api private |
| 3443 | */ |
| 3444 | |
| 3445 | XHR.prototype.doPoll = function () { |
| 3446 | debug('xhr poll'); |
| 3447 | var req = this.request(); |
| 3448 | var self = this; |
| 3449 | req.on('data', function (data) { |
| 3450 | self.onData(data); |
| 3451 | }); |
| 3452 | req.on('error', function (err) { |
| 3453 | self.onError('xhr poll error', err); |
| 3454 | }); |
| 3455 | this.pollXhr = req; |
| 3456 | }; |
| 3457 | |
| 3458 | /** |
| 3459 | * Request constructor |
| 3460 | * |
| 3461 | * @param {Object} options |
| 3462 | * @api public |
| 3463 | */ |
| 3464 | |
| 3465 | function Request (opts) { |
| 3466 | this.method = opts.method || 'GET'; |
| 3467 | this.uri = opts.uri; |
| 3468 | this.xd = !!opts.xd; |
| 3469 | this.xs = !!opts.xs; |
| 3470 | this.async = false !== opts.async; |
| 3471 | this.data = undefined !== opts.data ? opts.data : null; |
| 3472 | this.agent = opts.agent; |
| 3473 | this.isBinary = opts.isBinary; |
| 3474 | this.supportsBinary = opts.supportsBinary; |
| 3475 | this.enablesXDR = opts.enablesXDR; |
| 3476 | this.requestTimeout = opts.requestTimeout; |
| 3477 | |
| 3478 | // SSL options for Node.js client |
| 3479 | this.pfx = opts.pfx; |
| 3480 | this.key = opts.key; |
| 3481 | this.passphrase = opts.passphrase; |
| 3482 | this.cert = opts.cert; |
| 3483 | this.ca = opts.ca; |
| 3484 | this.ciphers = opts.ciphers; |
| 3485 | this.rejectUnauthorized = opts.rejectUnauthorized; |
| 3486 | |
| 3487 | // other options for Node.js client |
| 3488 | this.extraHeaders = opts.extraHeaders; |
| 3489 | |
| 3490 | this.create(); |
| 3491 | } |
| 3492 | |
| 3493 | /** |
| 3494 | * Mix in `Emitter`. |
| 3495 | */ |
| 3496 | |
| 3497 | Emitter(Request.prototype); |
| 3498 | |
| 3499 | /** |
| 3500 | * Creates the XHR object and sends the request. |
| 3501 | * |
| 3502 | * @api private |
| 3503 | */ |
| 3504 | |
| 3505 | Request.prototype.create = function () { |
| 3506 | var opts = { agent: this.agent, xdomain: this.xd, xscheme: this.xs, enablesXDR: this.enablesXDR }; |
| 3507 | |
| 3508 | // SSL options for Node.js client |
| 3509 | opts.pfx = this.pfx; |
| 3510 | opts.key = this.key; |
| 3511 | opts.passphrase = this.passphrase; |
| 3512 | opts.cert = this.cert; |
| 3513 | opts.ca = this.ca; |
| 3514 | opts.ciphers = this.ciphers; |
| 3515 | opts.rejectUnauthorized = this.rejectUnauthorized; |
| 3516 | |
| 3517 | var xhr = this.xhr = new XMLHttpRequest(opts); |
| 3518 | var self = this; |
| 3519 | |
| 3520 | try { |
| 3521 | debug('xhr open %s: %s', this.method, this.uri); |
| 3522 | xhr.open(this.method, this.uri, this.async); |
| 3523 | try { |
| 3524 | if (this.extraHeaders) { |
| 3525 | xhr.setDisableHeaderCheck && xhr.setDisableHeaderCheck(true); |
| 3526 | for (var i in this.extraHeaders) { |
| 3527 | if (this.extraHeaders.hasOwnProperty(i)) { |
| 3528 | xhr.setRequestHeader(i, this.extraHeaders[i]); |
| 3529 | } |
| 3530 | } |
| 3531 | } |
| 3532 | } catch (e) {} |
| 3533 | |
| 3534 | if ('POST' === this.method) { |
| 3535 | try { |
| 3536 | if (this.isBinary) { |
| 3537 | xhr.setRequestHeader('Content-type', 'application/octet-stream'); |
| 3538 | } else { |
| 3539 | xhr.setRequestHeader('Content-type', 'text/plain;charset=UTF-8'); |
| 3540 | } |
| 3541 | } catch (e) {} |
| 3542 | } |
| 3543 | |
| 3544 | try { |
| 3545 | xhr.setRequestHeader('Accept', '*/*'); |
| 3546 | } catch (e) {} |
| 3547 | |
| 3548 | // ie6 check |
| 3549 | if ('withCredentials' in xhr) { |
| 3550 | xhr.withCredentials = true; |
| 3551 | } |
| 3552 | |
| 3553 | if (this.requestTimeout) { |
| 3554 | xhr.timeout = this.requestTimeout; |
| 3555 | } |
| 3556 | |
| 3557 | if (this.hasXDR()) { |
| 3558 | xhr.onload = function () { |
| 3559 | self.onLoad(); |
| 3560 | }; |
| 3561 | xhr.onerror = function () { |
| 3562 | self.onError(xhr.responseText); |
| 3563 | }; |
| 3564 | } else { |
| 3565 | xhr.onreadystatechange = function () { |
| 3566 | if (xhr.readyState === 2) { |
| 3567 | try { |
| 3568 | var contentType = xhr.getResponseHeader('Content-Type'); |
| 3569 | if (self.supportsBinary && contentType === 'application/octet-stream') { |
| 3570 | xhr.responseType = 'arraybuffer'; |
| 3571 | } |
| 3572 | } catch (e) {} |
| 3573 | } |
| 3574 | if (4 !== xhr.readyState) return; |
| 3575 | if (200 === xhr.status || 1223 === xhr.status) { |
| 3576 | self.onLoad(); |
| 3577 | } else { |
| 3578 | // make sure the `error` event handler that's user-set |
| 3579 | // does not throw in the same tick and gets caught here |
| 3580 | setTimeout(function () { |
| 3581 | self.onError(xhr.status); |
| 3582 | }, 0); |
| 3583 | } |
| 3584 | }; |
| 3585 | } |
| 3586 | |
| 3587 | debug('xhr data %s', this.data); |
| 3588 | xhr.send(this.data); |
| 3589 | } catch (e) { |
| 3590 | // Need to defer since .create() is called directly fhrom the constructor |
| 3591 | // and thus the 'error' event can only be only bound *after* this exception |
| 3592 | // occurs. Therefore, also, we cannot throw here at all. |
| 3593 | setTimeout(function () { |
| 3594 | self.onError(e); |
| 3595 | }, 0); |
| 3596 | return; |
| 3597 | } |
| 3598 | |
| 3599 | if (global.document) { |
| 3600 | this.index = Request.requestsCount++; |
| 3601 | Request.requests[this.index] = this; |
| 3602 | } |
| 3603 | }; |
| 3604 | |
| 3605 | /** |
| 3606 | * Called upon successful response. |
| 3607 | * |
| 3608 | * @api private |
| 3609 | */ |
| 3610 | |
| 3611 | Request.prototype.onSuccess = function () { |
| 3612 | this.emit('success'); |
| 3613 | this.cleanup(); |
| 3614 | }; |
| 3615 | |
| 3616 | /** |
| 3617 | * Called if we have data. |
| 3618 | * |
| 3619 | * @api private |
| 3620 | */ |
| 3621 | |
| 3622 | Request.prototype.onData = function (data) { |
| 3623 | this.emit('data', data); |
| 3624 | this.onSuccess(); |
| 3625 | }; |
| 3626 | |
| 3627 | /** |
| 3628 | * Called upon error. |
| 3629 | * |
| 3630 | * @api private |
| 3631 | */ |
| 3632 | |
| 3633 | Request.prototype.onError = function (err) { |
| 3634 | this.emit('error', err); |
| 3635 | this.cleanup(true); |
| 3636 | }; |
| 3637 | |
| 3638 | /** |
| 3639 | * Cleans up house. |
| 3640 | * |
| 3641 | * @api private |
| 3642 | */ |
| 3643 | |
| 3644 | Request.prototype.cleanup = function (fromError) { |
| 3645 | if ('undefined' === typeof this.xhr || null === this.xhr) { |
| 3646 | return; |
| 3647 | } |
| 3648 | // xmlhttprequest |
| 3649 | if (this.hasXDR()) { |
| 3650 | this.xhr.onload = this.xhr.onerror = empty; |
| 3651 | } else { |
| 3652 | this.xhr.onreadystatechange = empty; |
| 3653 | } |
| 3654 | |
| 3655 | if (fromError) { |
| 3656 | try { |
| 3657 | this.xhr.abort(); |
| 3658 | } catch (e) {} |
| 3659 | } |
| 3660 | |
| 3661 | if (global.document) { |
| 3662 | delete Request.requests[this.index]; |
| 3663 | } |
| 3664 | |
| 3665 | this.xhr = null; |
| 3666 | }; |
| 3667 | |
| 3668 | /** |
| 3669 | * Called upon load. |
| 3670 | * |
| 3671 | * @api private |
| 3672 | */ |
| 3673 | |
| 3674 | Request.prototype.onLoad = function () { |
| 3675 | var data; |
| 3676 | try { |
| 3677 | var contentType; |
| 3678 | try { |
| 3679 | contentType = this.xhr.getResponseHeader('Content-Type'); |
| 3680 | } catch (e) {} |
| 3681 | if (contentType === 'application/octet-stream') { |
| 3682 | data = this.xhr.response || this.xhr.responseText; |
| 3683 | } else { |
| 3684 | data = this.xhr.responseText; |
| 3685 | } |
| 3686 | } catch (e) { |
| 3687 | this.onError(e); |
| 3688 | } |
| 3689 | if (null != data) { |
| 3690 | this.onData(data); |
| 3691 | } |
| 3692 | }; |
| 3693 | |
| 3694 | /** |
| 3695 | * Check if it has XDomainRequest. |
| 3696 | * |
| 3697 | * @api private |
| 3698 | */ |
| 3699 | |
| 3700 | Request.prototype.hasXDR = function () { |
| 3701 | return 'undefined' !== typeof global.XDomainRequest && !this.xs && this.enablesXDR; |
| 3702 | }; |
| 3703 | |
| 3704 | /** |
| 3705 | * Aborts the request. |
| 3706 | * |
| 3707 | * @api public |
| 3708 | */ |
| 3709 | |
| 3710 | Request.prototype.abort = function () { |
| 3711 | this.cleanup(); |
| 3712 | }; |
| 3713 | |
| 3714 | /** |
| 3715 | * Aborts pending requests when unloading the window. This is needed to prevent |
| 3716 | * memory leaks (e.g. when using IE) and to ensure that no spurious error is |
| 3717 | * emitted. |
| 3718 | */ |
| 3719 | |
| 3720 | Request.requestsCount = 0; |
| 3721 | Request.requests = {}; |
| 3722 | |
| 3723 | if (global.document) { |
| 3724 | if (global.attachEvent) { |
| 3725 | global.attachEvent('onunload', unloadHandler); |
| 3726 | } else if (global.addEventListener) { |
| 3727 | global.addEventListener('beforeunload', unloadHandler, false); |
| 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | function unloadHandler () { |
| 3732 | for (var i in Request.requests) { |
| 3733 | if (Request.requests.hasOwnProperty(i)) { |
| 3734 | Request.requests[i].abort(); |
| 3735 | } |
| 3736 | } |
| 3737 | } |
| 3738 | |
| 3739 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 3740 | |
| 3741 | /***/ }), |
| 3742 | /* 19 */ |
| 3743 | /***/ (function(module, exports, __webpack_require__) { |
| 3744 | |
| 3745 | /** |
| 3746 | * Module dependencies. |
| 3747 | */ |
| 3748 | |
| 3749 | var Transport = __webpack_require__(20); |
| 3750 | var parseqs = __webpack_require__(30); |
| 3751 | var parser = __webpack_require__(21); |
| 3752 | var inherit = __webpack_require__(31); |
| 3753 | var yeast = __webpack_require__(32); |
| 3754 | var debug = __webpack_require__(3)('engine.io-client:polling'); |
| 3755 | |
| 3756 | /** |
| 3757 | * Module exports. |
| 3758 | */ |
| 3759 | |
| 3760 | module.exports = Polling; |
| 3761 | |
| 3762 | /** |
| 3763 | * Is XHR2 supported? |
| 3764 | */ |
| 3765 | |
| 3766 | var hasXHR2 = (function () { |
| 3767 | var XMLHttpRequest = __webpack_require__(16); |
| 3768 | var xhr = new XMLHttpRequest({ xdomain: false }); |
| 3769 | return null != xhr.responseType; |
| 3770 | })(); |
| 3771 | |
| 3772 | /** |
| 3773 | * Polling interface. |
| 3774 | * |
| 3775 | * @param {Object} opts |
| 3776 | * @api private |
| 3777 | */ |
| 3778 | |
| 3779 | function Polling (opts) { |
| 3780 | var forceBase64 = (opts && opts.forceBase64); |
| 3781 | if (!hasXHR2 || forceBase64) { |
| 3782 | this.supportsBinary = false; |
| 3783 | } |
| 3784 | Transport.call(this, opts); |
| 3785 | } |
| 3786 | |
| 3787 | /** |
| 3788 | * Inherits from Transport. |
| 3789 | */ |
| 3790 | |
| 3791 | inherit(Polling, Transport); |
| 3792 | |
| 3793 | /** |
| 3794 | * Transport name. |
| 3795 | */ |
| 3796 | |
| 3797 | Polling.prototype.name = 'polling'; |
| 3798 | |
| 3799 | /** |
| 3800 | * Opens the socket (triggers polling). We write a PING message to determine |
| 3801 | * when the transport is open. |
| 3802 | * |
| 3803 | * @api private |
| 3804 | */ |
| 3805 | |
| 3806 | Polling.prototype.doOpen = function () { |
| 3807 | this.poll(); |
| 3808 | }; |
| 3809 | |
| 3810 | /** |
| 3811 | * Pauses polling. |
| 3812 | * |
| 3813 | * @param {Function} callback upon buffers are flushed and transport is paused |
| 3814 | * @api private |
| 3815 | */ |
| 3816 | |
| 3817 | Polling.prototype.pause = function (onPause) { |
| 3818 | var self = this; |
| 3819 | |
| 3820 | this.readyState = 'pausing'; |
| 3821 | |
| 3822 | function pause () { |
| 3823 | debug('paused'); |
| 3824 | self.readyState = 'paused'; |
| 3825 | onPause(); |
| 3826 | } |
| 3827 | |
| 3828 | if (this.polling || !this.writable) { |
| 3829 | var total = 0; |
| 3830 | |
| 3831 | if (this.polling) { |
| 3832 | debug('we are currently polling - waiting to pause'); |
| 3833 | total++; |
| 3834 | this.once('pollComplete', function () { |
| 3835 | debug('pre-pause polling complete'); |
| 3836 | --total || pause(); |
| 3837 | }); |
| 3838 | } |
| 3839 | |
| 3840 | if (!this.writable) { |
| 3841 | debug('we are currently writing - waiting to pause'); |
| 3842 | total++; |
| 3843 | this.once('drain', function () { |
| 3844 | debug('pre-pause writing complete'); |
| 3845 | --total || pause(); |
| 3846 | }); |
| 3847 | } |
| 3848 | } else { |
| 3849 | pause(); |
| 3850 | } |
| 3851 | }; |
| 3852 | |
| 3853 | /** |
| 3854 | * Starts polling cycle. |
| 3855 | * |
| 3856 | * @api public |
| 3857 | */ |
| 3858 | |
| 3859 | Polling.prototype.poll = function () { |
| 3860 | debug('polling'); |
| 3861 | this.polling = true; |
| 3862 | this.doPoll(); |
| 3863 | this.emit('poll'); |
| 3864 | }; |
| 3865 | |
| 3866 | /** |
| 3867 | * Overloads onData to detect payloads. |
| 3868 | * |
| 3869 | * @api private |
| 3870 | */ |
| 3871 | |
| 3872 | Polling.prototype.onData = function (data) { |
| 3873 | var self = this; |
| 3874 | debug('polling got data %s', data); |
| 3875 | var callback = function (packet, index, total) { |
| 3876 | // if its the first message we consider the transport open |
| 3877 | if ('opening' === self.readyState) { |
| 3878 | self.onOpen(); |
| 3879 | } |
| 3880 | |
| 3881 | // if its a close packet, we close the ongoing requests |
| 3882 | if ('close' === packet.type) { |
| 3883 | self.onClose(); |
| 3884 | return false; |
| 3885 | } |
| 3886 | |
| 3887 | // otherwise bypass onData and handle the message |
| 3888 | self.onPacket(packet); |
| 3889 | }; |
| 3890 | |
| 3891 | // decode payload |
| 3892 | parser.decodePayload(data, this.socket.binaryType, callback); |
| 3893 | |
| 3894 | // if an event did not trigger closing |
| 3895 | if ('closed' !== this.readyState) { |
| 3896 | // if we got data we're not polling |
| 3897 | this.polling = false; |
| 3898 | this.emit('pollComplete'); |
| 3899 | |
| 3900 | if ('open' === this.readyState) { |
| 3901 | this.poll(); |
| 3902 | } else { |
| 3903 | debug('ignoring poll - transport state "%s"', this.readyState); |
| 3904 | } |
| 3905 | } |
| 3906 | }; |
| 3907 | |
| 3908 | /** |
| 3909 | * For polling, send a close packet. |
| 3910 | * |
| 3911 | * @api private |
| 3912 | */ |
| 3913 | |
| 3914 | Polling.prototype.doClose = function () { |
| 3915 | var self = this; |
| 3916 | |
| 3917 | function close () { |
| 3918 | debug('writing close packet'); |
| 3919 | self.write([{ type: 'close' }]); |
| 3920 | } |
| 3921 | |
| 3922 | if ('open' === this.readyState) { |
| 3923 | debug('transport open - closing'); |
| 3924 | close(); |
| 3925 | } else { |
| 3926 | // in case we're trying to close while |
| 3927 | // handshaking is in progress (GH-164) |
| 3928 | debug('transport not open - deferring close'); |
| 3929 | this.once('open', close); |
| 3930 | } |
| 3931 | }; |
| 3932 | |
| 3933 | /** |
| 3934 | * Writes a packets payload. |
| 3935 | * |
| 3936 | * @param {Array} data packets |
| 3937 | * @param {Function} drain callback |
| 3938 | * @api private |
| 3939 | */ |
| 3940 | |
| 3941 | Polling.prototype.write = function (packets) { |
| 3942 | var self = this; |
| 3943 | this.writable = false; |
| 3944 | var callbackfn = function () { |
| 3945 | self.writable = true; |
| 3946 | self.emit('drain'); |
| 3947 | }; |
| 3948 | |
| 3949 | parser.encodePayload(packets, this.supportsBinary, function (data) { |
| 3950 | self.doWrite(data, callbackfn); |
| 3951 | }); |
| 3952 | }; |
| 3953 | |
| 3954 | /** |
| 3955 | * Generates uri for connection. |
| 3956 | * |
| 3957 | * @api private |
| 3958 | */ |
| 3959 | |
| 3960 | Polling.prototype.uri = function () { |
| 3961 | var query = this.query || {}; |
| 3962 | var schema = this.secure ? 'https' : 'http'; |
| 3963 | var port = ''; |
| 3964 | |
| 3965 | // cache busting is forced |
| 3966 | if (false !== this.timestampRequests) { |
| 3967 | query[this.timestampParam] = yeast(); |
| 3968 | } |
| 3969 | |
| 3970 | if (!this.supportsBinary && !query.sid) { |
| 3971 | query.b64 = 1; |
| 3972 | } |
| 3973 | |
| 3974 | query = parseqs.encode(query); |
| 3975 | |
| 3976 | // avoid port if default for schema |
| 3977 | if (this.port && (('https' === schema && Number(this.port) !== 443) || |
| 3978 | ('http' === schema && Number(this.port) !== 80))) { |
| 3979 | port = ':' + this.port; |
| 3980 | } |
| 3981 | |
| 3982 | // prepend ? to query |
| 3983 | if (query.length) { |
| 3984 | query = '?' + query; |
| 3985 | } |
| 3986 | |
| 3987 | var ipv6 = this.hostname.indexOf(':') !== -1; |
| 3988 | return schema + '://' + (ipv6 ? '[' + this.hostname + ']' : this.hostname) + port + this.path + query; |
| 3989 | }; |
| 3990 | |
| 3991 | |
| 3992 | /***/ }), |
| 3993 | /* 20 */ |
| 3994 | /***/ (function(module, exports, __webpack_require__) { |
| 3995 | |
| 3996 | /** |
| 3997 | * Module dependencies. |
| 3998 | */ |
| 3999 | |
| 4000 | var parser = __webpack_require__(21); |
| 4001 | var Emitter = __webpack_require__(8); |
| 4002 | |
| 4003 | /** |
| 4004 | * Module exports. |
| 4005 | */ |
| 4006 | |
| 4007 | module.exports = Transport; |
| 4008 | |
| 4009 | /** |
| 4010 | * Transport abstract constructor. |
| 4011 | * |
| 4012 | * @param {Object} options. |
| 4013 | * @api private |
| 4014 | */ |
| 4015 | |
| 4016 | function Transport (opts) { |
| 4017 | this.path = opts.path; |
| 4018 | this.hostname = opts.hostname; |
| 4019 | this.port = opts.port; |
| 4020 | this.secure = opts.secure; |
| 4021 | this.query = opts.query; |
| 4022 | this.timestampParam = opts.timestampParam; |
| 4023 | this.timestampRequests = opts.timestampRequests; |
| 4024 | this.readyState = ''; |
| 4025 | this.agent = opts.agent || false; |
| 4026 | this.socket = opts.socket; |
| 4027 | this.enablesXDR = opts.enablesXDR; |
| 4028 | |
| 4029 | // SSL options for Node.js client |
| 4030 | this.pfx = opts.pfx; |
| 4031 | this.key = opts.key; |
| 4032 | this.passphrase = opts.passphrase; |
| 4033 | this.cert = opts.cert; |
| 4034 | this.ca = opts.ca; |
| 4035 | this.ciphers = opts.ciphers; |
| 4036 | this.rejectUnauthorized = opts.rejectUnauthorized; |
| 4037 | this.forceNode = opts.forceNode; |
| 4038 | |
| 4039 | // other options for Node.js client |
| 4040 | this.extraHeaders = opts.extraHeaders; |
| 4041 | this.localAddress = opts.localAddress; |
| 4042 | } |
| 4043 | |
| 4044 | /** |
| 4045 | * Mix in `Emitter`. |
| 4046 | */ |
| 4047 | |
| 4048 | Emitter(Transport.prototype); |
| 4049 | |
| 4050 | /** |
| 4051 | * Emits an error. |
| 4052 | * |
| 4053 | * @param {String} str |
| 4054 | * @return {Transport} for chaining |
| 4055 | * @api public |
| 4056 | */ |
| 4057 | |
| 4058 | Transport.prototype.onError = function (msg, desc) { |
| 4059 | var err = new Error(msg); |
| 4060 | err.type = 'TransportError'; |
| 4061 | err.description = desc; |
| 4062 | this.emit('error', err); |
| 4063 | return this; |
| 4064 | }; |
| 4065 | |
| 4066 | /** |
| 4067 | * Opens the transport. |
| 4068 | * |
| 4069 | * @api public |
| 4070 | */ |
| 4071 | |
| 4072 | Transport.prototype.open = function () { |
| 4073 | if ('closed' === this.readyState || '' === this.readyState) { |
| 4074 | this.readyState = 'opening'; |
| 4075 | this.doOpen(); |
| 4076 | } |
| 4077 | |
| 4078 | return this; |
| 4079 | }; |
| 4080 | |
| 4081 | /** |
| 4082 | * Closes the transport. |
| 4083 | * |
| 4084 | * @api private |
| 4085 | */ |
| 4086 | |
| 4087 | Transport.prototype.close = function () { |
| 4088 | if ('opening' === this.readyState || 'open' === this.readyState) { |
| 4089 | this.doClose(); |
| 4090 | this.onClose(); |
| 4091 | } |
| 4092 | |
| 4093 | return this; |
| 4094 | }; |
| 4095 | |
| 4096 | /** |
| 4097 | * Sends multiple packets. |
| 4098 | * |
| 4099 | * @param {Array} packets |
| 4100 | * @api private |
| 4101 | */ |
| 4102 | |
| 4103 | Transport.prototype.send = function (packets) { |
| 4104 | if ('open' === this.readyState) { |
| 4105 | this.write(packets); |
| 4106 | } else { |
| 4107 | throw new Error('Transport not open'); |
| 4108 | } |
| 4109 | }; |
| 4110 | |
| 4111 | /** |
| 4112 | * Called upon open |
| 4113 | * |
| 4114 | * @api private |
| 4115 | */ |
| 4116 | |
| 4117 | Transport.prototype.onOpen = function () { |
| 4118 | this.readyState = 'open'; |
| 4119 | this.writable = true; |
| 4120 | this.emit('open'); |
| 4121 | }; |
| 4122 | |
| 4123 | /** |
| 4124 | * Called with data. |
| 4125 | * |
| 4126 | * @param {String} data |
| 4127 | * @api private |
| 4128 | */ |
| 4129 | |
| 4130 | Transport.prototype.onData = function (data) { |
| 4131 | var packet = parser.decodePacket(data, this.socket.binaryType); |
| 4132 | this.onPacket(packet); |
| 4133 | }; |
| 4134 | |
| 4135 | /** |
| 4136 | * Called with a decoded packet. |
| 4137 | */ |
| 4138 | |
| 4139 | Transport.prototype.onPacket = function (packet) { |
| 4140 | this.emit('packet', packet); |
| 4141 | }; |
| 4142 | |
| 4143 | /** |
| 4144 | * Called upon close. |
| 4145 | * |
| 4146 | * @api private |
| 4147 | */ |
| 4148 | |
| 4149 | Transport.prototype.onClose = function () { |
| 4150 | this.readyState = 'closed'; |
| 4151 | this.emit('close'); |
| 4152 | }; |
| 4153 | |
| 4154 | |
| 4155 | /***/ }), |
| 4156 | /* 21 */ |
| 4157 | /***/ (function(module, exports, __webpack_require__) { |
| 4158 | |
| 4159 | /* WEBPACK VAR INJECTION */(function(global) {/** |
| 4160 | * Module dependencies. |
| 4161 | */ |
| 4162 | |
| 4163 | var keys = __webpack_require__(22); |
| 4164 | var hasBinary = __webpack_require__(23); |
| 4165 | var sliceBuffer = __webpack_require__(24); |
| 4166 | var after = __webpack_require__(25); |
| 4167 | var utf8 = __webpack_require__(26); |
| 4168 | |
| 4169 | var base64encoder; |
| 4170 | if (global && global.ArrayBuffer) { |
| 4171 | base64encoder = __webpack_require__(28); |
| 4172 | } |
| 4173 | |
| 4174 | /** |
| 4175 | * Check if we are running an android browser. That requires us to use |
| 4176 | * ArrayBuffer with polling transports... |
| 4177 | * |
| 4178 | * http://ghinda.net/jpeg-blob-ajax-android/ |
| 4179 | */ |
| 4180 | |
| 4181 | var isAndroid = typeof navigator !== 'undefined' && /Android/i.test(navigator.userAgent); |
| 4182 | |
| 4183 | /** |
| 4184 | * Check if we are running in PhantomJS. |
| 4185 | * Uploading a Blob with PhantomJS does not work correctly, as reported here: |
| 4186 | * https://github.com/ariya/phantomjs/issues/11395 |
| 4187 | * @type boolean |
| 4188 | */ |
| 4189 | var isPhantomJS = typeof navigator !== 'undefined' && /PhantomJS/i.test(navigator.userAgent); |
| 4190 | |
| 4191 | /** |
| 4192 | * When true, avoids using Blobs to encode payloads. |
| 4193 | * @type boolean |
| 4194 | */ |
| 4195 | var dontSendBlobs = isAndroid || isPhantomJS; |
| 4196 | |
| 4197 | /** |
| 4198 | * Current protocol version. |
| 4199 | */ |
| 4200 | |
| 4201 | exports.protocol = 3; |
| 4202 | |
| 4203 | /** |
| 4204 | * Packet types. |
| 4205 | */ |
| 4206 | |
| 4207 | var packets = exports.packets = { |
| 4208 | open: 0 // non-ws |
| 4209 | , close: 1 // non-ws |
| 4210 | , ping: 2 |
| 4211 | , pong: 3 |
| 4212 | , message: 4 |
| 4213 | , upgrade: 5 |
| 4214 | , noop: 6 |
| 4215 | }; |
| 4216 | |
| 4217 | var packetslist = keys(packets); |
| 4218 | |
| 4219 | /** |
| 4220 | * Premade error packet. |
| 4221 | */ |
| 4222 | |
| 4223 | var err = { type: 'error', data: 'parser error' }; |
| 4224 | |
| 4225 | /** |
| 4226 | * Create a blob api even for blob builder when vendor prefixes exist |
| 4227 | */ |
| 4228 | |
| 4229 | var Blob = __webpack_require__(29); |
| 4230 | |
| 4231 | /** |
| 4232 | * Encodes a packet. |
| 4233 | * |
| 4234 | * <packet type id> [ <data> ] |
| 4235 | * |
| 4236 | * Example: |
| 4237 | * |
| 4238 | * 5hello world |
| 4239 | * 3 |
| 4240 | * 4 |
| 4241 | * |
| 4242 | * Binary is encoded in an identical principle |
| 4243 | * |
| 4244 | * @api private |
| 4245 | */ |
| 4246 | |
| 4247 | exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) { |
| 4248 | if (typeof supportsBinary === 'function') { |
| 4249 | callback = supportsBinary; |
| 4250 | supportsBinary = false; |
| 4251 | } |
| 4252 | |
| 4253 | if (typeof utf8encode === 'function') { |
| 4254 | callback = utf8encode; |
| 4255 | utf8encode = null; |
| 4256 | } |
| 4257 | |
| 4258 | var data = (packet.data === undefined) |
| 4259 | ? undefined |
| 4260 | : packet.data.buffer || packet.data; |
| 4261 | |
| 4262 | if (global.ArrayBuffer && data instanceof ArrayBuffer) { |
| 4263 | return encodeArrayBuffer(packet, supportsBinary, callback); |
| 4264 | } else if (Blob && data instanceof global.Blob) { |
| 4265 | return encodeBlob(packet, supportsBinary, callback); |
| 4266 | } |
| 4267 | |
| 4268 | // might be an object with { base64: true, data: dataAsBase64String } |
| 4269 | if (data && data.base64) { |
| 4270 | return encodeBase64Object(packet, callback); |
| 4271 | } |
| 4272 | |
| 4273 | // Sending data as a utf-8 string |
| 4274 | var encoded = packets[packet.type]; |
| 4275 | |
| 4276 | // data fragment is optional |
| 4277 | if (undefined !== packet.data) { |
| 4278 | encoded += utf8encode ? utf8.encode(String(packet.data), { strict: false }) : String(packet.data); |
| 4279 | } |
| 4280 | |
| 4281 | return callback('' + encoded); |
| 4282 | |
| 4283 | }; |
| 4284 | |
| 4285 | function encodeBase64Object(packet, callback) { |
| 4286 | // packet data is an object { base64: true, data: dataAsBase64String } |
| 4287 | var message = 'b' + exports.packets[packet.type] + packet.data.data; |
| 4288 | return callback(message); |
| 4289 | } |
| 4290 | |
| 4291 | /** |
| 4292 | * Encode packet helpers for binary types |
| 4293 | */ |
| 4294 | |
| 4295 | function encodeArrayBuffer(packet, supportsBinary, callback) { |
| 4296 | if (!supportsBinary) { |
| 4297 | return exports.encodeBase64Packet(packet, callback); |
| 4298 | } |
| 4299 | |
| 4300 | var data = packet.data; |
| 4301 | var contentArray = new Uint8Array(data); |
| 4302 | var resultBuffer = new Uint8Array(1 + data.byteLength); |
| 4303 | |
| 4304 | resultBuffer[0] = packets[packet.type]; |
| 4305 | for (var i = 0; i < contentArray.length; i++) { |
| 4306 | resultBuffer[i+1] = contentArray[i]; |
| 4307 | } |
| 4308 | |
| 4309 | return callback(resultBuffer.buffer); |
| 4310 | } |
| 4311 | |
| 4312 | function encodeBlobAsArrayBuffer(packet, supportsBinary, callback) { |
| 4313 | if (!supportsBinary) { |
| 4314 | return exports.encodeBase64Packet(packet, callback); |
| 4315 | } |
| 4316 | |
| 4317 | var fr = new FileReader(); |
| 4318 | fr.onload = function() { |
| 4319 | packet.data = fr.result; |
| 4320 | exports.encodePacket(packet, supportsBinary, true, callback); |
| 4321 | }; |
| 4322 | return fr.readAsArrayBuffer(packet.data); |
| 4323 | } |
| 4324 | |
| 4325 | function encodeBlob(packet, supportsBinary, callback) { |
| 4326 | if (!supportsBinary) { |
| 4327 | return exports.encodeBase64Packet(packet, callback); |
| 4328 | } |
| 4329 | |
| 4330 | if (dontSendBlobs) { |
| 4331 | return encodeBlobAsArrayBuffer(packet, supportsBinary, callback); |
| 4332 | } |
| 4333 | |
| 4334 | var length = new Uint8Array(1); |
| 4335 | length[0] = packets[packet.type]; |
| 4336 | var blob = new Blob([length.buffer, packet.data]); |
| 4337 | |
| 4338 | return callback(blob); |
| 4339 | } |
| 4340 | |
| 4341 | /** |
| 4342 | * Encodes a packet with binary data in a base64 string |
| 4343 | * |
| 4344 | * @param {Object} packet, has `type` and `data` |
| 4345 | * @return {String} base64 encoded message |
| 4346 | */ |
| 4347 | |
| 4348 | exports.encodeBase64Packet = function(packet, callback) { |
| 4349 | var message = 'b' + exports.packets[packet.type]; |
| 4350 | if (Blob && packet.data instanceof global.Blob) { |
| 4351 | var fr = new FileReader(); |
| 4352 | fr.onload = function() { |
| 4353 | var b64 = fr.result.split(',')[1]; |
| 4354 | callback(message + b64); |
| 4355 | }; |
| 4356 | return fr.readAsDataURL(packet.data); |
| 4357 | } |
| 4358 | |
| 4359 | var b64data; |
| 4360 | try { |
| 4361 | b64data = String.fromCharCode.apply(null, new Uint8Array(packet.data)); |
| 4362 | } catch (e) { |
| 4363 | // iPhone Safari doesn't let you apply with typed arrays |
| 4364 | var typed = new Uint8Array(packet.data); |
| 4365 | var basic = new Array(typed.length); |
| 4366 | for (var i = 0; i < typed.length; i++) { |
| 4367 | basic[i] = typed[i]; |
| 4368 | } |
| 4369 | b64data = String.fromCharCode.apply(null, basic); |
| 4370 | } |
| 4371 | message += global.btoa(b64data); |
| 4372 | return callback(message); |
| 4373 | }; |
| 4374 | |
| 4375 | /** |
| 4376 | * Decodes a packet. Changes format to Blob if requested. |
| 4377 | * |
| 4378 | * @return {Object} with `type` and `data` (if any) |
| 4379 | * @api private |
| 4380 | */ |
| 4381 | |
| 4382 | exports.decodePacket = function (data, binaryType, utf8decode) { |
| 4383 | if (data === undefined) { |
| 4384 | return err; |
| 4385 | } |
| 4386 | // String data |
| 4387 | if (typeof data === 'string') { |
| 4388 | if (data.charAt(0) === 'b') { |
| 4389 | return exports.decodeBase64Packet(data.substr(1), binaryType); |
| 4390 | } |
| 4391 | |
| 4392 | if (utf8decode) { |
| 4393 | data = tryDecode(data); |
| 4394 | if (data === false) { |
| 4395 | return err; |
| 4396 | } |
| 4397 | } |
| 4398 | var type = data.charAt(0); |
| 4399 | |
| 4400 | if (Number(type) != type || !packetslist[type]) { |
| 4401 | return err; |
| 4402 | } |
| 4403 | |
| 4404 | if (data.length > 1) { |
| 4405 | return { type: packetslist[type], data: data.substring(1) }; |
| 4406 | } else { |
| 4407 | return { type: packetslist[type] }; |
| 4408 | } |
| 4409 | } |
| 4410 | |
| 4411 | var asArray = new Uint8Array(data); |
| 4412 | var type = asArray[0]; |
| 4413 | var rest = sliceBuffer(data, 1); |
| 4414 | if (Blob && binaryType === 'blob') { |
| 4415 | rest = new Blob([rest]); |
| 4416 | } |
| 4417 | return { type: packetslist[type], data: rest }; |
| 4418 | }; |
| 4419 | |
| 4420 | function tryDecode(data) { |
| 4421 | try { |
| 4422 | data = utf8.decode(data, { strict: false }); |
| 4423 | } catch (e) { |
| 4424 | return false; |
| 4425 | } |
| 4426 | return data; |
| 4427 | } |
| 4428 | |
| 4429 | /** |
| 4430 | * Decodes a packet encoded in a base64 string |
| 4431 | * |
| 4432 | * @param {String} base64 encoded message |
| 4433 | * @return {Object} with `type` and `data` (if any) |
| 4434 | */ |
| 4435 | |
| 4436 | exports.decodeBase64Packet = function(msg, binaryType) { |
| 4437 | var type = packetslist[msg.charAt(0)]; |
| 4438 | if (!base64encoder) { |
| 4439 | return { type: type, data: { base64: true, data: msg.substr(1) } }; |
| 4440 | } |
| 4441 | |
| 4442 | var data = base64encoder.decode(msg.substr(1)); |
| 4443 | |
| 4444 | if (binaryType === 'blob' && Blob) { |
| 4445 | data = new Blob([data]); |
| 4446 | } |
| 4447 | |
| 4448 | return { type: type, data: data }; |
| 4449 | }; |
| 4450 | |
| 4451 | /** |
| 4452 | * Encodes multiple messages (payload). |
| 4453 | * |
| 4454 | * <length>:data |
| 4455 | * |
| 4456 | * Example: |
| 4457 | * |
| 4458 | * 11:hello world2:hi |
| 4459 | * |
| 4460 | * If any contents are binary, they will be encoded as base64 strings. Base64 |
| 4461 | * encoded strings are marked with a b before the length specifier |
| 4462 | * |
| 4463 | * @param {Array} packets |
| 4464 | * @api private |
| 4465 | */ |
| 4466 | |
| 4467 | exports.encodePayload = function (packets, supportsBinary, callback) { |
| 4468 | if (typeof supportsBinary === 'function') { |
| 4469 | callback = supportsBinary; |
| 4470 | supportsBinary = null; |
| 4471 | } |
| 4472 | |
| 4473 | var isBinary = hasBinary(packets); |
| 4474 | |
| 4475 | if (supportsBinary && isBinary) { |
| 4476 | if (Blob && !dontSendBlobs) { |
| 4477 | return exports.encodePayloadAsBlob(packets, callback); |
| 4478 | } |
| 4479 | |
| 4480 | return exports.encodePayloadAsArrayBuffer(packets, callback); |
| 4481 | } |
| 4482 | |
| 4483 | if (!packets.length) { |
| 4484 | return callback('0:'); |
| 4485 | } |
| 4486 | |
| 4487 | function setLengthHeader(message) { |
| 4488 | return message.length + ':' + message; |
| 4489 | } |
| 4490 | |
| 4491 | function encodeOne(packet, doneCallback) { |
| 4492 | exports.encodePacket(packet, !isBinary ? false : supportsBinary, false, function(message) { |
| 4493 | doneCallback(null, setLengthHeader(message)); |
| 4494 | }); |
| 4495 | } |
| 4496 | |
| 4497 | map(packets, encodeOne, function(err, results) { |
| 4498 | return callback(results.join('')); |
| 4499 | }); |
| 4500 | }; |
| 4501 | |
| 4502 | /** |
| 4503 | * Async array map using after |
| 4504 | */ |
| 4505 | |
| 4506 | function map(ary, each, done) { |
| 4507 | var result = new Array(ary.length); |
| 4508 | var next = after(ary.length, done); |
| 4509 | |
| 4510 | var eachWithIndex = function(i, el, cb) { |
| 4511 | each(el, function(error, msg) { |
| 4512 | result[i] = msg; |
| 4513 | cb(error, result); |
| 4514 | }); |
| 4515 | }; |
| 4516 | |
| 4517 | for (var i = 0; i < ary.length; i++) { |
| 4518 | eachWithIndex(i, ary[i], next); |
| 4519 | } |
| 4520 | } |
| 4521 | |
| 4522 | /* |
| 4523 | * Decodes data when a payload is maybe expected. Possible binary contents are |
| 4524 | * decoded from their base64 representation |
| 4525 | * |
| 4526 | * @param {String} data, callback method |
| 4527 | * @api public |
| 4528 | */ |
| 4529 | |
| 4530 | exports.decodePayload = function (data, binaryType, callback) { |
| 4531 | if (typeof data !== 'string') { |
| 4532 | return exports.decodePayloadAsBinary(data, binaryType, callback); |
| 4533 | } |
| 4534 | |
| 4535 | if (typeof binaryType === 'function') { |
| 4536 | callback = binaryType; |
| 4537 | binaryType = null; |
| 4538 | } |
| 4539 | |
| 4540 | var packet; |
| 4541 | if (data === '') { |
| 4542 | // parser error - ignoring payload |
| 4543 | return callback(err, 0, 1); |
| 4544 | } |
| 4545 | |
| 4546 | var length = '', n, msg; |
| 4547 | |
| 4548 | for (var i = 0, l = data.length; i < l; i++) { |
| 4549 | var chr = data.charAt(i); |
| 4550 | |
| 4551 | if (chr !== ':') { |
| 4552 | length += chr; |
| 4553 | continue; |
| 4554 | } |
| 4555 | |
| 4556 | if (length === '' || (length != (n = Number(length)))) { |
| 4557 | // parser error - ignoring payload |
| 4558 | return callback(err, 0, 1); |
| 4559 | } |
| 4560 | |
| 4561 | msg = data.substr(i + 1, n); |
| 4562 | |
| 4563 | if (length != msg.length) { |
| 4564 | // parser error - ignoring payload |
| 4565 | return callback(err, 0, 1); |
| 4566 | } |
| 4567 | |
| 4568 | if (msg.length) { |
| 4569 | packet = exports.decodePacket(msg, binaryType, false); |
| 4570 | |
| 4571 | if (err.type === packet.type && err.data === packet.data) { |
| 4572 | // parser error in individual packet - ignoring payload |
| 4573 | return callback(err, 0, 1); |
| 4574 | } |
| 4575 | |
| 4576 | var ret = callback(packet, i + n, l); |
| 4577 | if (false === ret) return; |
| 4578 | } |
| 4579 | |
| 4580 | // advance cursor |
| 4581 | i += n; |
| 4582 | length = ''; |
| 4583 | } |
| 4584 | |
| 4585 | if (length !== '') { |
| 4586 | // parser error - ignoring payload |
| 4587 | return callback(err, 0, 1); |
| 4588 | } |
| 4589 | |
| 4590 | }; |
| 4591 | |
| 4592 | /** |
| 4593 | * Encodes multiple messages (payload) as binary. |
| 4594 | * |
| 4595 | * <1 = binary, 0 = string><number from 0-9><number from 0-9>[...]<number |
| 4596 | * 255><data> |
| 4597 | * |
| 4598 | * Example: |
| 4599 | * 1 3 255 1 2 3, if the binary contents are interpreted as 8 bit integers |
| 4600 | * |
| 4601 | * @param {Array} packets |
| 4602 | * @return {ArrayBuffer} encoded payload |
| 4603 | * @api private |
| 4604 | */ |
| 4605 | |
| 4606 | exports.encodePayloadAsArrayBuffer = function(packets, callback) { |
| 4607 | if (!packets.length) { |
| 4608 | return callback(new ArrayBuffer(0)); |
| 4609 | } |
| 4610 | |
| 4611 | function encodeOne(packet, doneCallback) { |
| 4612 | exports.encodePacket(packet, true, true, function(data) { |
| 4613 | return doneCallback(null, data); |
| 4614 | }); |
| 4615 | } |
| 4616 | |
| 4617 | map(packets, encodeOne, function(err, encodedPackets) { |
| 4618 | var totalLength = encodedPackets.reduce(function(acc, p) { |
| 4619 | var len; |
| 4620 | if (typeof p === 'string'){ |
| 4621 | len = p.length; |
| 4622 | } else { |
| 4623 | len = p.byteLength; |
| 4624 | } |
| 4625 | return acc + len.toString().length + len + 2; // string/binary identifier + separator = 2 |
| 4626 | }, 0); |
| 4627 | |
| 4628 | var resultArray = new Uint8Array(totalLength); |
| 4629 | |
| 4630 | var bufferIndex = 0; |
| 4631 | encodedPackets.forEach(function(p) { |
| 4632 | var isString = typeof p === 'string'; |
| 4633 | var ab = p; |
| 4634 | if (isString) { |
| 4635 | var view = new Uint8Array(p.length); |
| 4636 | for (var i = 0; i < p.length; i++) { |
| 4637 | view[i] = p.charCodeAt(i); |
| 4638 | } |
| 4639 | ab = view.buffer; |
| 4640 | } |
| 4641 | |
| 4642 | if (isString) { // not true binary |
| 4643 | resultArray[bufferIndex++] = 0; |
| 4644 | } else { // true binary |
| 4645 | resultArray[bufferIndex++] = 1; |
| 4646 | } |
| 4647 | |
| 4648 | var lenStr = ab.byteLength.toString(); |
| 4649 | for (var i = 0; i < lenStr.length; i++) { |
| 4650 | resultArray[bufferIndex++] = parseInt(lenStr[i]); |
| 4651 | } |
| 4652 | resultArray[bufferIndex++] = 255; |
| 4653 | |
| 4654 | var view = new Uint8Array(ab); |
| 4655 | for (var i = 0; i < view.length; i++) { |
| 4656 | resultArray[bufferIndex++] = view[i]; |
| 4657 | } |
| 4658 | }); |
| 4659 | |
| 4660 | return callback(resultArray.buffer); |
| 4661 | }); |
| 4662 | }; |
| 4663 | |
| 4664 | /** |
| 4665 | * Encode as Blob |
| 4666 | */ |
| 4667 | |
| 4668 | exports.encodePayloadAsBlob = function(packets, callback) { |
| 4669 | function encodeOne(packet, doneCallback) { |
| 4670 | exports.encodePacket(packet, true, true, function(encoded) { |
| 4671 | var binaryIdentifier = new Uint8Array(1); |
| 4672 | binaryIdentifier[0] = 1; |
| 4673 | if (typeof encoded === 'string') { |
| 4674 | var view = new Uint8Array(encoded.length); |
| 4675 | for (var i = 0; i < encoded.length; i++) { |
| 4676 | view[i] = encoded.charCodeAt(i); |
| 4677 | } |
| 4678 | encoded = view.buffer; |
| 4679 | binaryIdentifier[0] = 0; |
| 4680 | } |
| 4681 | |
| 4682 | var len = (encoded instanceof ArrayBuffer) |
| 4683 | ? encoded.byteLength |
| 4684 | : encoded.size; |
| 4685 | |
| 4686 | var lenStr = len.toString(); |
| 4687 | var lengthAry = new Uint8Array(lenStr.length + 1); |
| 4688 | for (var i = 0; i < lenStr.length; i++) { |
| 4689 | lengthAry[i] = parseInt(lenStr[i]); |
| 4690 | } |
| 4691 | lengthAry[lenStr.length] = 255; |
| 4692 | |
| 4693 | if (Blob) { |
| 4694 | var blob = new Blob([binaryIdentifier.buffer, lengthAry.buffer, encoded]); |
| 4695 | doneCallback(null, blob); |
| 4696 | } |
| 4697 | }); |
| 4698 | } |
| 4699 | |
| 4700 | map(packets, encodeOne, function(err, results) { |
| 4701 | return callback(new Blob(results)); |
| 4702 | }); |
| 4703 | }; |
| 4704 | |
| 4705 | /* |
| 4706 | * Decodes data when a payload is maybe expected. Strings are decoded by |
| 4707 | * interpreting each byte as a key code for entries marked to start with 0. See |
| 4708 | * description of encodePayloadAsBinary |
| 4709 | * |
| 4710 | * @param {ArrayBuffer} data, callback method |
| 4711 | * @api public |
| 4712 | */ |
| 4713 | |
| 4714 | exports.decodePayloadAsBinary = function (data, binaryType, callback) { |
| 4715 | if (typeof binaryType === 'function') { |
| 4716 | callback = binaryType; |
| 4717 | binaryType = null; |
| 4718 | } |
| 4719 | |
| 4720 | var bufferTail = data; |
| 4721 | var buffers = []; |
| 4722 | |
| 4723 | while (bufferTail.byteLength > 0) { |
| 4724 | var tailArray = new Uint8Array(bufferTail); |
| 4725 | var isString = tailArray[0] === 0; |
| 4726 | var msgLength = ''; |
| 4727 | |
| 4728 | for (var i = 1; ; i++) { |
| 4729 | if (tailArray[i] === 255) break; |
| 4730 | |
| 4731 | // 310 = char length of Number.MAX_VALUE |
| 4732 | if (msgLength.length > 310) { |
| 4733 | return callback(err, 0, 1); |
| 4734 | } |
| 4735 | |
| 4736 | msgLength += tailArray[i]; |
| 4737 | } |
| 4738 | |
| 4739 | bufferTail = sliceBuffer(bufferTail, 2 + msgLength.length); |
| 4740 | msgLength = parseInt(msgLength); |
| 4741 | |
| 4742 | var msg = sliceBuffer(bufferTail, 0, msgLength); |
| 4743 | if (isString) { |
| 4744 | try { |
| 4745 | msg = String.fromCharCode.apply(null, new Uint8Array(msg)); |
| 4746 | } catch (e) { |
| 4747 | // iPhone Safari doesn't let you apply to typed arrays |
| 4748 | var typed = new Uint8Array(msg); |
| 4749 | msg = ''; |
| 4750 | for (var i = 0; i < typed.length; i++) { |
| 4751 | msg += String.fromCharCode(typed[i]); |
| 4752 | } |
| 4753 | } |
| 4754 | } |
| 4755 | |
| 4756 | buffers.push(msg); |
| 4757 | bufferTail = sliceBuffer(bufferTail, msgLength); |
| 4758 | } |
| 4759 | |
| 4760 | var total = buffers.length; |
| 4761 | buffers.forEach(function(buffer, i) { |
| 4762 | callback(exports.decodePacket(buffer, binaryType, true), i, total); |
| 4763 | }); |
| 4764 | }; |
| 4765 | |
| 4766 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 4767 | |
| 4768 | /***/ }), |
| 4769 | /* 22 */ |
| 4770 | /***/ (function(module, exports) { |
| 4771 | |
| 4772 | |
| 4773 | /** |
| 4774 | * Gets the keys for an object. |
| 4775 | * |
| 4776 | * @return {Array} keys |
| 4777 | * @api private |
| 4778 | */ |
| 4779 | |
| 4780 | module.exports = Object.keys || function keys (obj){ |
| 4781 | var arr = []; |
| 4782 | var has = Object.prototype.hasOwnProperty; |
| 4783 | |
| 4784 | for (var i in obj) { |
| 4785 | if (has.call(obj, i)) { |
| 4786 | arr.push(i); |
| 4787 | } |
| 4788 | } |
| 4789 | return arr; |
| 4790 | }; |
| 4791 | |
| 4792 | |
| 4793 | /***/ }), |
| 4794 | /* 23 */ |
| 4795 | /***/ (function(module, exports, __webpack_require__) { |
| 4796 | |
| 4797 | /* WEBPACK VAR INJECTION */(function(global) {/* global Blob File */ |
| 4798 | |
| 4799 | /* |
| 4800 | * Module requirements. |
| 4801 | */ |
| 4802 | |
| 4803 | var isArray = __webpack_require__(10); |
| 4804 | |
| 4805 | var toString = Object.prototype.toString; |
| 4806 | var withNativeBlob = typeof global.Blob === 'function' || toString.call(global.Blob) === '[object BlobConstructor]'; |
| 4807 | var withNativeFile = typeof global.File === 'function' || toString.call(global.File) === '[object FileConstructor]'; |
| 4808 | |
| 4809 | /** |
| 4810 | * Module exports. |
| 4811 | */ |
| 4812 | |
| 4813 | module.exports = hasBinary; |
| 4814 | |
| 4815 | /** |
| 4816 | * Checks for binary data. |
| 4817 | * |
| 4818 | * Supports Buffer, ArrayBuffer, Blob and File. |
| 4819 | * |
| 4820 | * @param {Object} anything |
| 4821 | * @api public |
| 4822 | */ |
| 4823 | |
| 4824 | function hasBinary (obj) { |
| 4825 | if (!obj || typeof obj !== 'object') { |
| 4826 | return false; |
| 4827 | } |
| 4828 | |
| 4829 | if (isArray(obj)) { |
| 4830 | for (var i = 0, l = obj.length; i < l; i++) { |
| 4831 | if (hasBinary(obj[i])) { |
| 4832 | return true; |
| 4833 | } |
| 4834 | } |
| 4835 | return false; |
| 4836 | } |
| 4837 | |
| 4838 | if ((typeof global.Buffer === 'function' && global.Buffer.isBuffer && global.Buffer.isBuffer(obj)) || |
| 4839 | (typeof global.ArrayBuffer === 'function' && obj instanceof ArrayBuffer) || |
| 4840 | (withNativeBlob && obj instanceof Blob) || |
| 4841 | (withNativeFile && obj instanceof File) |
| 4842 | ) { |
| 4843 | return true; |
| 4844 | } |
| 4845 | |
| 4846 | // see: https://github.com/Automattic/has-binary/pull/4 |
| 4847 | if (obj.toJSON && typeof obj.toJSON === 'function' && arguments.length === 1) { |
| 4848 | return hasBinary(obj.toJSON(), true); |
| 4849 | } |
| 4850 | |
| 4851 | for (var key in obj) { |
| 4852 | if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key])) { |
| 4853 | return true; |
| 4854 | } |
| 4855 | } |
| 4856 | |
| 4857 | return false; |
| 4858 | } |
| 4859 | |
| 4860 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 4861 | |
| 4862 | /***/ }), |
| 4863 | /* 24 */ |
| 4864 | /***/ (function(module, exports) { |
| 4865 | |
| 4866 | /** |
| 4867 | * An abstraction for slicing an arraybuffer even when |
| 4868 | * ArrayBuffer.prototype.slice is not supported |
| 4869 | * |
| 4870 | * @api public |
| 4871 | */ |
| 4872 | |
| 4873 | module.exports = function(arraybuffer, start, end) { |
| 4874 | var bytes = arraybuffer.byteLength; |
| 4875 | start = start || 0; |
| 4876 | end = end || bytes; |
| 4877 | |
| 4878 | if (arraybuffer.slice) { return arraybuffer.slice(start, end); } |
| 4879 | |
| 4880 | if (start < 0) { start += bytes; } |
| 4881 | if (end < 0) { end += bytes; } |
| 4882 | if (end > bytes) { end = bytes; } |
| 4883 | |
| 4884 | if (start >= bytes || start >= end || bytes === 0) { |
| 4885 | return new ArrayBuffer(0); |
| 4886 | } |
| 4887 | |
| 4888 | var abv = new Uint8Array(arraybuffer); |
| 4889 | var result = new Uint8Array(end - start); |
| 4890 | for (var i = start, ii = 0; i < end; i++, ii++) { |
| 4891 | result[ii] = abv[i]; |
| 4892 | } |
| 4893 | return result.buffer; |
| 4894 | }; |
| 4895 | |
| 4896 | |
| 4897 | /***/ }), |
| 4898 | /* 25 */ |
| 4899 | /***/ (function(module, exports) { |
| 4900 | |
| 4901 | module.exports = after |
| 4902 | |
| 4903 | function after(count, callback, err_cb) { |
| 4904 | var bail = false |
| 4905 | err_cb = err_cb || noop |
| 4906 | proxy.count = count |
| 4907 | |
| 4908 | return (count === 0) ? callback() : proxy |
| 4909 | |
| 4910 | function proxy(err, result) { |
| 4911 | if (proxy.count <= 0) { |
| 4912 | throw new Error('after called too many times') |
| 4913 | } |
| 4914 | --proxy.count |
| 4915 | |
| 4916 | // after first error, rest are passed to err_cb |
| 4917 | if (err) { |
| 4918 | bail = true |
| 4919 | callback(err) |
| 4920 | // future error callbacks will go to error handler |
| 4921 | callback = err_cb |
| 4922 | } else if (proxy.count === 0 && !bail) { |
| 4923 | callback(null, result) |
| 4924 | } |
| 4925 | } |
| 4926 | } |
| 4927 | |
| 4928 | function noop() {} |
| 4929 | |
| 4930 | |
| 4931 | /***/ }), |
| 4932 | /* 26 */ |
| 4933 | /***/ (function(module, exports, __webpack_require__) { |
| 4934 | |
| 4935 | var __WEBPACK_AMD_DEFINE_RESULT__;/* WEBPACK VAR INJECTION */(function(module, global) {/*! https://mths.be/utf8js v2.1.2 by @mathias */ |
| 4936 | ;(function(root) { |
| 4937 | |
| 4938 | // Detect free variables `exports` |
| 4939 | var freeExports = typeof exports == 'object' && exports; |
| 4940 | |
| 4941 | // Detect free variable `module` |
| 4942 | var freeModule = typeof module == 'object' && module && |
| 4943 | module.exports == freeExports && module; |
| 4944 | |
| 4945 | // Detect free variable `global`, from Node.js or Browserified code, |
| 4946 | // and use it as `root` |
| 4947 | var freeGlobal = typeof global == 'object' && global; |
| 4948 | if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { |
| 4949 | root = freeGlobal; |
| 4950 | } |
| 4951 | |
| 4952 | /*--------------------------------------------------------------------------*/ |
| 4953 | |
| 4954 | var stringFromCharCode = String.fromCharCode; |
| 4955 | |
| 4956 | // Taken from https://mths.be/punycode |
| 4957 | function ucs2decode(string) { |
| 4958 | var output = []; |
| 4959 | var counter = 0; |
| 4960 | var length = string.length; |
| 4961 | var value; |
| 4962 | var extra; |
| 4963 | while (counter < length) { |
| 4964 | value = string.charCodeAt(counter++); |
| 4965 | if (value >= 0xD800 && value <= 0xDBFF && counter < length) { |
| 4966 | // high surrogate, and there is a next character |
| 4967 | extra = string.charCodeAt(counter++); |
| 4968 | if ((extra & 0xFC00) == 0xDC00) { // low surrogate |
| 4969 | output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); |
| 4970 | } else { |
| 4971 | // unmatched surrogate; only append this code unit, in case the next |
| 4972 | // code unit is the high surrogate of a surrogate pair |
| 4973 | output.push(value); |
| 4974 | counter--; |
| 4975 | } |
| 4976 | } else { |
| 4977 | output.push(value); |
| 4978 | } |
| 4979 | } |
| 4980 | return output; |
| 4981 | } |
| 4982 | |
| 4983 | // Taken from https://mths.be/punycode |
| 4984 | function ucs2encode(array) { |
| 4985 | var length = array.length; |
| 4986 | var index = -1; |
| 4987 | var value; |
| 4988 | var output = ''; |
| 4989 | while (++index < length) { |
| 4990 | value = array[index]; |
| 4991 | if (value > 0xFFFF) { |
| 4992 | value -= 0x10000; |
| 4993 | output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800); |
| 4994 | value = 0xDC00 | value & 0x3FF; |
| 4995 | } |
| 4996 | output += stringFromCharCode(value); |
| 4997 | } |
| 4998 | return output; |
| 4999 | } |
| 5000 | |
| 5001 | function checkScalarValue(codePoint, strict) { |
| 5002 | if (codePoint >= 0xD800 && codePoint <= 0xDFFF) { |
| 5003 | if (strict) { |
| 5004 | throw Error( |
| 5005 | 'Lone surrogate U+' + codePoint.toString(16).toUpperCase() + |
| 5006 | ' is not a scalar value' |
| 5007 | ); |
| 5008 | } |
| 5009 | return false; |
| 5010 | } |
| 5011 | return true; |
| 5012 | } |
| 5013 | /*--------------------------------------------------------------------------*/ |
| 5014 | |
| 5015 | function createByte(codePoint, shift) { |
| 5016 | return stringFromCharCode(((codePoint >> shift) & 0x3F) | 0x80); |
| 5017 | } |
| 5018 | |
| 5019 | function encodeCodePoint(codePoint, strict) { |
| 5020 | if ((codePoint & 0xFFFFFF80) == 0) { // 1-byte sequence |
| 5021 | return stringFromCharCode(codePoint); |
| 5022 | } |
| 5023 | var symbol = ''; |
| 5024 | if ((codePoint & 0xFFFFF800) == 0) { // 2-byte sequence |
| 5025 | symbol = stringFromCharCode(((codePoint >> 6) & 0x1F) | 0xC0); |
| 5026 | } |
| 5027 | else if ((codePoint & 0xFFFF0000) == 0) { // 3-byte sequence |
| 5028 | if (!checkScalarValue(codePoint, strict)) { |
| 5029 | codePoint = 0xFFFD; |
| 5030 | } |
| 5031 | symbol = stringFromCharCode(((codePoint >> 12) & 0x0F) | 0xE0); |
| 5032 | symbol += createByte(codePoint, 6); |
| 5033 | } |
| 5034 | else if ((codePoint & 0xFFE00000) == 0) { // 4-byte sequence |
| 5035 | symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xF0); |
| 5036 | symbol += createByte(codePoint, 12); |
| 5037 | symbol += createByte(codePoint, 6); |
| 5038 | } |
| 5039 | symbol += stringFromCharCode((codePoint & 0x3F) | 0x80); |
| 5040 | return symbol; |
| 5041 | } |
| 5042 | |
| 5043 | function utf8encode(string, opts) { |
| 5044 | opts = opts || {}; |
| 5045 | var strict = false !== opts.strict; |
| 5046 | |
| 5047 | var codePoints = ucs2decode(string); |
| 5048 | var length = codePoints.length; |
| 5049 | var index = -1; |
| 5050 | var codePoint; |
| 5051 | var byteString = ''; |
| 5052 | while (++index < length) { |
| 5053 | codePoint = codePoints[index]; |
| 5054 | byteString += encodeCodePoint(codePoint, strict); |
| 5055 | } |
| 5056 | return byteString; |
| 5057 | } |
| 5058 | |
| 5059 | /*--------------------------------------------------------------------------*/ |
| 5060 | |
| 5061 | function readContinuationByte() { |
| 5062 | if (byteIndex >= byteCount) { |
| 5063 | throw Error('Invalid byte index'); |
| 5064 | } |
| 5065 | |
| 5066 | var continuationByte = byteArray[byteIndex] & 0xFF; |
| 5067 | byteIndex++; |
| 5068 | |
| 5069 | if ((continuationByte & 0xC0) == 0x80) { |
| 5070 | return continuationByte & 0x3F; |
| 5071 | } |
| 5072 | |
| 5073 | // If we end up here, it’s not a continuation byte |
| 5074 | throw Error('Invalid continuation byte'); |
| 5075 | } |
| 5076 | |
| 5077 | function decodeSymbol(strict) { |
| 5078 | var byte1; |
| 5079 | var byte2; |
| 5080 | var byte3; |
| 5081 | var byte4; |
| 5082 | var codePoint; |
| 5083 | |
| 5084 | if (byteIndex > byteCount) { |
| 5085 | throw Error('Invalid byte index'); |
| 5086 | } |
| 5087 | |
| 5088 | if (byteIndex == byteCount) { |
| 5089 | return false; |
| 5090 | } |
| 5091 | |
| 5092 | // Read first byte |
| 5093 | byte1 = byteArray[byteIndex] & 0xFF; |
| 5094 | byteIndex++; |
| 5095 | |
| 5096 | // 1-byte sequence (no continuation bytes) |
| 5097 | if ((byte1 & 0x80) == 0) { |
| 5098 | return byte1; |
| 5099 | } |
| 5100 | |
| 5101 | // 2-byte sequence |
| 5102 | if ((byte1 & 0xE0) == 0xC0) { |
| 5103 | byte2 = readContinuationByte(); |
| 5104 | codePoint = ((byte1 & 0x1F) << 6) | byte2; |
| 5105 | if (codePoint >= 0x80) { |
| 5106 | return codePoint; |
| 5107 | } else { |
| 5108 | throw Error('Invalid continuation byte'); |
| 5109 | } |
| 5110 | } |
| 5111 | |
| 5112 | // 3-byte sequence (may include unpaired surrogates) |
| 5113 | if ((byte1 & 0xF0) == 0xE0) { |
| 5114 | byte2 = readContinuationByte(); |
| 5115 | byte3 = readContinuationByte(); |
| 5116 | codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3; |
| 5117 | if (codePoint >= 0x0800) { |
| 5118 | return checkScalarValue(codePoint, strict) ? codePoint : 0xFFFD; |
| 5119 | } else { |
| 5120 | throw Error('Invalid continuation byte'); |
| 5121 | } |
| 5122 | } |
| 5123 | |
| 5124 | // 4-byte sequence |
| 5125 | if ((byte1 & 0xF8) == 0xF0) { |
| 5126 | byte2 = readContinuationByte(); |
| 5127 | byte3 = readContinuationByte(); |
| 5128 | byte4 = readContinuationByte(); |
| 5129 | codePoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0C) | |
| 5130 | (byte3 << 0x06) | byte4; |
| 5131 | if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) { |
| 5132 | return codePoint; |
| 5133 | } |
| 5134 | } |
| 5135 | |
| 5136 | throw Error('Invalid UTF-8 detected'); |
| 5137 | } |
| 5138 | |
| 5139 | var byteArray; |
| 5140 | var byteCount; |
| 5141 | var byteIndex; |
| 5142 | function utf8decode(byteString, opts) { |
| 5143 | opts = opts || {}; |
| 5144 | var strict = false !== opts.strict; |
| 5145 | |
| 5146 | byteArray = ucs2decode(byteString); |
| 5147 | byteCount = byteArray.length; |
| 5148 | byteIndex = 0; |
| 5149 | var codePoints = []; |
| 5150 | var tmp; |
| 5151 | while ((tmp = decodeSymbol(strict)) !== false) { |
| 5152 | codePoints.push(tmp); |
| 5153 | } |
| 5154 | return ucs2encode(codePoints); |
| 5155 | } |
| 5156 | |
| 5157 | /*--------------------------------------------------------------------------*/ |
| 5158 | |
| 5159 | var utf8 = { |
| 5160 | 'version': '2.1.2', |
| 5161 | 'encode': utf8encode, |
| 5162 | 'decode': utf8decode |
| 5163 | }; |
| 5164 | |
| 5165 | // Some AMD build optimizers, like r.js, check for specific condition patterns |
| 5166 | // like the following: |
| 5167 | if ( |
| 5168 | true |
| 5169 | ) { |
| 5170 | !(__WEBPACK_AMD_DEFINE_RESULT__ = function() { |
| 5171 | return utf8; |
| 5172 | }.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); |
| 5173 | } else if (freeExports && !freeExports.nodeType) { |
| 5174 | if (freeModule) { // in Node.js or RingoJS v0.8.0+ |
| 5175 | freeModule.exports = utf8; |
| 5176 | } else { // in Narwhal or RingoJS v0.7.0- |
| 5177 | var object = {}; |
| 5178 | var hasOwnProperty = object.hasOwnProperty; |
| 5179 | for (var key in utf8) { |
| 5180 | hasOwnProperty.call(utf8, key) && (freeExports[key] = utf8[key]); |
| 5181 | } |
| 5182 | } |
| 5183 | } else { // in Rhino or a web browser |
| 5184 | root.utf8 = utf8; |
| 5185 | } |
| 5186 | |
| 5187 | }(this)); |
| 5188 | |
| 5189 | /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(27)(module), (function() { return this; }()))) |
| 5190 | |
| 5191 | /***/ }), |
| 5192 | /* 27 */ |
| 5193 | /***/ (function(module, exports) { |
| 5194 | |
| 5195 | module.exports = function(module) { |
| 5196 | if(!module.webpackPolyfill) { |
| 5197 | module.deprecate = function() {}; |
| 5198 | module.paths = []; |
| 5199 | // module.parent = undefined by default |
| 5200 | module.children = []; |
| 5201 | module.webpackPolyfill = 1; |
| 5202 | } |
| 5203 | return module; |
| 5204 | } |
| 5205 | |
| 5206 | |
| 5207 | /***/ }), |
| 5208 | /* 28 */ |
| 5209 | /***/ (function(module, exports) { |
| 5210 | |
| 5211 | /* |
| 5212 | * base64-arraybuffer |
| 5213 | * https://github.com/niklasvh/base64-arraybuffer |
| 5214 | * |
| 5215 | * Copyright (c) 2012 Niklas von Hertzen |
| 5216 | * Licensed under the MIT license. |
| 5217 | */ |
| 5218 | (function(){ |
| 5219 | "use strict"; |
| 5220 | |
| 5221 | var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 5222 | |
| 5223 | // Use a lookup table to find the index. |
| 5224 | var lookup = new Uint8Array(256); |
| 5225 | for (var i = 0; i < chars.length; i++) { |
| 5226 | lookup[chars.charCodeAt(i)] = i; |
| 5227 | } |
| 5228 | |
| 5229 | exports.encode = function(arraybuffer) { |
| 5230 | var bytes = new Uint8Array(arraybuffer), |
| 5231 | i, len = bytes.length, base64 = ""; |
| 5232 | |
| 5233 | for (i = 0; i < len; i+=3) { |
| 5234 | base64 += chars[bytes[i] >> 2]; |
| 5235 | base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)]; |
| 5236 | base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)]; |
| 5237 | base64 += chars[bytes[i + 2] & 63]; |
| 5238 | } |
| 5239 | |
| 5240 | if ((len % 3) === 2) { |
| 5241 | base64 = base64.substring(0, base64.length - 1) + "="; |
| 5242 | } else if (len % 3 === 1) { |
| 5243 | base64 = base64.substring(0, base64.length - 2) + "=="; |
| 5244 | } |
| 5245 | |
| 5246 | return base64; |
| 5247 | }; |
| 5248 | |
| 5249 | exports.decode = function(base64) { |
| 5250 | var bufferLength = base64.length * 0.75, |
| 5251 | len = base64.length, i, p = 0, |
| 5252 | encoded1, encoded2, encoded3, encoded4; |
| 5253 | |
| 5254 | if (base64[base64.length - 1] === "=") { |
| 5255 | bufferLength--; |
| 5256 | if (base64[base64.length - 2] === "=") { |
| 5257 | bufferLength--; |
| 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | var arraybuffer = new ArrayBuffer(bufferLength), |
| 5262 | bytes = new Uint8Array(arraybuffer); |
| 5263 | |
| 5264 | for (i = 0; i < len; i+=4) { |
| 5265 | encoded1 = lookup[base64.charCodeAt(i)]; |
| 5266 | encoded2 = lookup[base64.charCodeAt(i+1)]; |
| 5267 | encoded3 = lookup[base64.charCodeAt(i+2)]; |
| 5268 | encoded4 = lookup[base64.charCodeAt(i+3)]; |
| 5269 | |
| 5270 | bytes[p++] = (encoded1 << 2) | (encoded2 >> 4); |
| 5271 | bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2); |
| 5272 | bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63); |
| 5273 | } |
| 5274 | |
| 5275 | return arraybuffer; |
| 5276 | }; |
| 5277 | })(); |
| 5278 | |
| 5279 | |
| 5280 | /***/ }), |
| 5281 | /* 29 */ |
| 5282 | /***/ (function(module, exports) { |
| 5283 | |
| 5284 | /* WEBPACK VAR INJECTION */(function(global) {/** |
| 5285 | * Create a blob builder even when vendor prefixes exist |
| 5286 | */ |
| 5287 | |
| 5288 | var BlobBuilder = global.BlobBuilder |
| 5289 | || global.WebKitBlobBuilder |
| 5290 | || global.MSBlobBuilder |
| 5291 | || global.MozBlobBuilder; |
| 5292 | |
| 5293 | /** |
| 5294 | * Check if Blob constructor is supported |
| 5295 | */ |
| 5296 | |
| 5297 | var blobSupported = (function() { |
| 5298 | try { |
| 5299 | var a = new Blob(['hi']); |
| 5300 | return a.size === 2; |
| 5301 | } catch(e) { |
| 5302 | return false; |
| 5303 | } |
| 5304 | })(); |
| 5305 | |
| 5306 | /** |
| 5307 | * Check if Blob constructor supports ArrayBufferViews |
| 5308 | * Fails in Safari 6, so we need to map to ArrayBuffers there. |
| 5309 | */ |
| 5310 | |
| 5311 | var blobSupportsArrayBufferView = blobSupported && (function() { |
| 5312 | try { |
| 5313 | var b = new Blob([new Uint8Array([1,2])]); |
| 5314 | return b.size === 2; |
| 5315 | } catch(e) { |
| 5316 | return false; |
| 5317 | } |
| 5318 | })(); |
| 5319 | |
| 5320 | /** |
| 5321 | * Check if BlobBuilder is supported |
| 5322 | */ |
| 5323 | |
| 5324 | var blobBuilderSupported = BlobBuilder |
| 5325 | && BlobBuilder.prototype.append |
| 5326 | && BlobBuilder.prototype.getBlob; |
| 5327 | |
| 5328 | /** |
| 5329 | * Helper function that maps ArrayBufferViews to ArrayBuffers |
| 5330 | * Used by BlobBuilder constructor and old browsers that didn't |
| 5331 | * support it in the Blob constructor. |
| 5332 | */ |
| 5333 | |
| 5334 | function mapArrayBufferViews(ary) { |
| 5335 | for (var i = 0; i < ary.length; i++) { |
| 5336 | var chunk = ary[i]; |
| 5337 | if (chunk.buffer instanceof ArrayBuffer) { |
| 5338 | var buf = chunk.buffer; |
| 5339 | |
| 5340 | // if this is a subarray, make a copy so we only |
| 5341 | // include the subarray region from the underlying buffer |
| 5342 | if (chunk.byteLength !== buf.byteLength) { |
| 5343 | var copy = new Uint8Array(chunk.byteLength); |
| 5344 | copy.set(new Uint8Array(buf, chunk.byteOffset, chunk.byteLength)); |
| 5345 | buf = copy.buffer; |
| 5346 | } |
| 5347 | |
| 5348 | ary[i] = buf; |
| 5349 | } |
| 5350 | } |
| 5351 | } |
| 5352 | |
| 5353 | function BlobBuilderConstructor(ary, options) { |
| 5354 | options = options || {}; |
| 5355 | |
| 5356 | var bb = new BlobBuilder(); |
| 5357 | mapArrayBufferViews(ary); |
| 5358 | |
| 5359 | for (var i = 0; i < ary.length; i++) { |
| 5360 | bb.append(ary[i]); |
| 5361 | } |
| 5362 | |
| 5363 | return (options.type) ? bb.getBlob(options.type) : bb.getBlob(); |
| 5364 | }; |
| 5365 | |
| 5366 | function BlobConstructor(ary, options) { |
| 5367 | mapArrayBufferViews(ary); |
| 5368 | return new Blob(ary, options || {}); |
| 5369 | }; |
| 5370 | |
| 5371 | module.exports = (function() { |
| 5372 | if (blobSupported) { |
| 5373 | return blobSupportsArrayBufferView ? global.Blob : BlobConstructor; |
| 5374 | } else if (blobBuilderSupported) { |
| 5375 | return BlobBuilderConstructor; |
| 5376 | } else { |
| 5377 | return undefined; |
| 5378 | } |
| 5379 | })(); |
| 5380 | |
| 5381 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 5382 | |
| 5383 | /***/ }), |
| 5384 | /* 30 */ |
| 5385 | /***/ (function(module, exports) { |
| 5386 | |
| 5387 | /** |
| 5388 | * Compiles a querystring |
| 5389 | * Returns string representation of the object |
| 5390 | * |
| 5391 | * @param {Object} |
| 5392 | * @api private |
| 5393 | */ |
| 5394 | |
| 5395 | exports.encode = function (obj) { |
| 5396 | var str = ''; |
| 5397 | |
| 5398 | for (var i in obj) { |
| 5399 | if (obj.hasOwnProperty(i)) { |
| 5400 | if (str.length) str += '&'; |
| 5401 | str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]); |
| 5402 | } |
| 5403 | } |
| 5404 | |
| 5405 | return str; |
| 5406 | }; |
| 5407 | |
| 5408 | /** |
| 5409 | * Parses a simple querystring into an object |
| 5410 | * |
| 5411 | * @param {String} qs |
| 5412 | * @api private |
| 5413 | */ |
| 5414 | |
| 5415 | exports.decode = function(qs){ |
| 5416 | var qry = {}; |
| 5417 | var pairs = qs.split('&'); |
| 5418 | for (var i = 0, l = pairs.length; i < l; i++) { |
| 5419 | var pair = pairs[i].split('='); |
| 5420 | qry[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]); |
| 5421 | } |
| 5422 | return qry; |
| 5423 | }; |
| 5424 | |
| 5425 | |
| 5426 | /***/ }), |
| 5427 | /* 31 */ |
| 5428 | /***/ (function(module, exports) { |
| 5429 | |
| 5430 | |
| 5431 | module.exports = function(a, b){ |
| 5432 | var fn = function(){}; |
| 5433 | fn.prototype = b.prototype; |
| 5434 | a.prototype = new fn; |
| 5435 | a.prototype.constructor = a; |
| 5436 | }; |
| 5437 | |
| 5438 | /***/ }), |
| 5439 | /* 32 */ |
| 5440 | /***/ (function(module, exports) { |
| 5441 | |
| 5442 | 'use strict'; |
| 5443 | |
| 5444 | var alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_'.split('') |
| 5445 | , length = 64 |
| 5446 | , map = {} |
| 5447 | , seed = 0 |
| 5448 | , i = 0 |
| 5449 | , prev; |
| 5450 | |
| 5451 | /** |
| 5452 | * Return a string representing the specified number. |
| 5453 | * |
| 5454 | * @param {Number} num The number to convert. |
| 5455 | * @returns {String} The string representation of the number. |
| 5456 | * @api public |
| 5457 | */ |
| 5458 | function encode(num) { |
| 5459 | var encoded = ''; |
| 5460 | |
| 5461 | do { |
| 5462 | encoded = alphabet[num % length] + encoded; |
| 5463 | num = Math.floor(num / length); |
| 5464 | } while (num > 0); |
| 5465 | |
| 5466 | return encoded; |
| 5467 | } |
| 5468 | |
| 5469 | /** |
| 5470 | * Return the integer value specified by the given string. |
| 5471 | * |
| 5472 | * @param {String} str The string to convert. |
| 5473 | * @returns {Number} The integer value represented by the string. |
| 5474 | * @api public |
| 5475 | */ |
| 5476 | function decode(str) { |
| 5477 | var decoded = 0; |
| 5478 | |
| 5479 | for (i = 0; i < str.length; i++) { |
| 5480 | decoded = decoded * length + map[str.charAt(i)]; |
| 5481 | } |
| 5482 | |
| 5483 | return decoded; |
| 5484 | } |
| 5485 | |
| 5486 | /** |
| 5487 | * Yeast: A tiny growing id generator. |
| 5488 | * |
| 5489 | * @returns {String} A unique id. |
| 5490 | * @api public |
| 5491 | */ |
| 5492 | function yeast() { |
| 5493 | var now = encode(+new Date()); |
| 5494 | |
| 5495 | if (now !== prev) return seed = 0, prev = now; |
| 5496 | return now +'.'+ encode(seed++); |
| 5497 | } |
| 5498 | |
| 5499 | // |
| 5500 | // Map each character to its index. |
| 5501 | // |
| 5502 | for (; i < length; i++) map[alphabet[i]] = i; |
| 5503 | |
| 5504 | // |
| 5505 | // Expose the `yeast`, `encode` and `decode` functions. |
| 5506 | // |
| 5507 | yeast.encode = encode; |
| 5508 | yeast.decode = decode; |
| 5509 | module.exports = yeast; |
| 5510 | |
| 5511 | |
| 5512 | /***/ }), |
| 5513 | /* 33 */ |
| 5514 | /***/ (function(module, exports, __webpack_require__) { |
| 5515 | |
| 5516 | /* WEBPACK VAR INJECTION */(function(global) { |
| 5517 | /** |
| 5518 | * Module requirements. |
| 5519 | */ |
| 5520 | |
| 5521 | var Polling = __webpack_require__(19); |
| 5522 | var inherit = __webpack_require__(31); |
| 5523 | |
| 5524 | /** |
| 5525 | * Module exports. |
| 5526 | */ |
| 5527 | |
| 5528 | module.exports = JSONPPolling; |
| 5529 | |
| 5530 | /** |
| 5531 | * Cached regular expressions. |
| 5532 | */ |
| 5533 | |
| 5534 | var rNewline = /\n/g; |
| 5535 | var rEscapedNewline = /\\n/g; |
| 5536 | |
| 5537 | /** |
| 5538 | * Global JSONP callbacks. |
| 5539 | */ |
| 5540 | |
| 5541 | var callbacks; |
| 5542 | |
| 5543 | /** |
| 5544 | * Noop. |
| 5545 | */ |
| 5546 | |
| 5547 | function empty () { } |
| 5548 | |
| 5549 | /** |
| 5550 | * JSONP Polling constructor. |
| 5551 | * |
| 5552 | * @param {Object} opts. |
| 5553 | * @api public |
| 5554 | */ |
| 5555 | |
| 5556 | function JSONPPolling (opts) { |
| 5557 | Polling.call(this, opts); |
| 5558 | |
| 5559 | this.query = this.query || {}; |
| 5560 | |
| 5561 | // define global callbacks array if not present |
| 5562 | // we do this here (lazily) to avoid unneeded global pollution |
| 5563 | if (!callbacks) { |
| 5564 | // we need to consider multiple engines in the same page |
| 5565 | if (!global.___eio) global.___eio = []; |
| 5566 | callbacks = global.___eio; |
| 5567 | } |
| 5568 | |
| 5569 | // callback identifier |
| 5570 | this.index = callbacks.length; |
| 5571 | |
| 5572 | // add callback to jsonp global |
| 5573 | var self = this; |
| 5574 | callbacks.push(function (msg) { |
| 5575 | self.onData(msg); |
| 5576 | }); |
| 5577 | |
| 5578 | // append to query string |
| 5579 | this.query.j = this.index; |
| 5580 | |
| 5581 | // prevent spurious errors from being emitted when the window is unloaded |
| 5582 | if (global.document && global.addEventListener) { |
| 5583 | global.addEventListener('beforeunload', function () { |
| 5584 | if (self.script) self.script.onerror = empty; |
| 5585 | }, false); |
| 5586 | } |
| 5587 | } |
| 5588 | |
| 5589 | /** |
| 5590 | * Inherits from Polling. |
| 5591 | */ |
| 5592 | |
| 5593 | inherit(JSONPPolling, Polling); |
| 5594 | |
| 5595 | /* |
| 5596 | * JSONP only supports binary as base64 encoded strings |
| 5597 | */ |
| 5598 | |
| 5599 | JSONPPolling.prototype.supportsBinary = false; |
| 5600 | |
| 5601 | /** |
| 5602 | * Closes the socket. |
| 5603 | * |
| 5604 | * @api private |
| 5605 | */ |
| 5606 | |
| 5607 | JSONPPolling.prototype.doClose = function () { |
| 5608 | if (this.script) { |
| 5609 | this.script.parentNode.removeChild(this.script); |
| 5610 | this.script = null; |
| 5611 | } |
| 5612 | |
| 5613 | if (this.form) { |
| 5614 | this.form.parentNode.removeChild(this.form); |
| 5615 | this.form = null; |
| 5616 | this.iframe = null; |
| 5617 | } |
| 5618 | |
| 5619 | Polling.prototype.doClose.call(this); |
| 5620 | }; |
| 5621 | |
| 5622 | /** |
| 5623 | * Starts a poll cycle. |
| 5624 | * |
| 5625 | * @api private |
| 5626 | */ |
| 5627 | |
| 5628 | JSONPPolling.prototype.doPoll = function () { |
| 5629 | var self = this; |
| 5630 | var script = document.createElement('script'); |
| 5631 | |
| 5632 | if (this.script) { |
| 5633 | this.script.parentNode.removeChild(this.script); |
| 5634 | this.script = null; |
| 5635 | } |
| 5636 | |
| 5637 | script.async = true; |
| 5638 | script.src = this.uri(); |
| 5639 | script.onerror = function (e) { |
| 5640 | self.onError('jsonp poll error', e); |
| 5641 | }; |
| 5642 | |
| 5643 | var insertAt = document.getElementsByTagName('script')[0]; |
| 5644 | if (insertAt) { |
| 5645 | insertAt.parentNode.insertBefore(script, insertAt); |
| 5646 | } else { |
| 5647 | (document.head || document.body).appendChild(script); |
| 5648 | } |
| 5649 | this.script = script; |
| 5650 | |
| 5651 | var isUAgecko = 'undefined' !== typeof navigator && /gecko/i.test(navigator.userAgent); |
| 5652 | |
| 5653 | if (isUAgecko) { |
| 5654 | setTimeout(function () { |
| 5655 | var iframe = document.createElement('iframe'); |
| 5656 | document.body.appendChild(iframe); |
| 5657 | document.body.removeChild(iframe); |
| 5658 | }, 100); |
| 5659 | } |
| 5660 | }; |
| 5661 | |
| 5662 | /** |
| 5663 | * Writes with a hidden iframe. |
| 5664 | * |
| 5665 | * @param {String} data to send |
| 5666 | * @param {Function} called upon flush. |
| 5667 | * @api private |
| 5668 | */ |
| 5669 | |
| 5670 | JSONPPolling.prototype.doWrite = function (data, fn) { |
| 5671 | var self = this; |
| 5672 | |
| 5673 | if (!this.form) { |
| 5674 | var form = document.createElement('form'); |
| 5675 | var area = document.createElement('textarea'); |
| 5676 | var id = this.iframeId = 'eio_iframe_' + this.index; |
| 5677 | var iframe; |
| 5678 | |
| 5679 | form.className = 'socketio'; |
| 5680 | form.style.position = 'absolute'; |
| 5681 | form.style.top = '-1000px'; |
| 5682 | form.style.left = '-1000px'; |
| 5683 | form.target = id; |
| 5684 | form.method = 'POST'; |
| 5685 | form.setAttribute('accept-charset', 'utf-8'); |
| 5686 | area.name = 'd'; |
| 5687 | form.appendChild(area); |
| 5688 | document.body.appendChild(form); |
| 5689 | |
| 5690 | this.form = form; |
| 5691 | this.area = area; |
| 5692 | } |
| 5693 | |
| 5694 | this.form.action = this.uri(); |
| 5695 | |
| 5696 | function complete () { |
| 5697 | initIframe(); |
| 5698 | fn(); |
| 5699 | } |
| 5700 | |
| 5701 | function initIframe () { |
| 5702 | if (self.iframe) { |
| 5703 | try { |
| 5704 | self.form.removeChild(self.iframe); |
| 5705 | } catch (e) { |
| 5706 | self.onError('jsonp polling iframe removal error', e); |
| 5707 | } |
| 5708 | } |
| 5709 | |
| 5710 | try { |
| 5711 | // ie6 dynamic iframes with target="" support (thanks Chris Lambacher) |
| 5712 | var html = '<iframe src="javascript:0" name="' + self.iframeId + '">'; |
| 5713 | iframe = document.createElement(html); |
| 5714 | } catch (e) { |
| 5715 | iframe = document.createElement('iframe'); |
| 5716 | iframe.name = self.iframeId; |
| 5717 | iframe.src = 'javascript:0'; |
| 5718 | } |
| 5719 | |
| 5720 | iframe.id = self.iframeId; |
| 5721 | |
| 5722 | self.form.appendChild(iframe); |
| 5723 | self.iframe = iframe; |
| 5724 | } |
| 5725 | |
| 5726 | initIframe(); |
| 5727 | |
| 5728 | // escape \n to prevent it from being converted into \r\n by some UAs |
| 5729 | // double escaping is required for escaped new lines because unescaping of new lines can be done safely on server-side |
| 5730 | data = data.replace(rEscapedNewline, '\\\n'); |
| 5731 | this.area.value = data.replace(rNewline, '\\n'); |
| 5732 | |
| 5733 | try { |
| 5734 | this.form.submit(); |
| 5735 | } catch (e) {} |
| 5736 | |
| 5737 | if (this.iframe.attachEvent) { |
| 5738 | this.iframe.onreadystatechange = function () { |
| 5739 | if (self.iframe.readyState === 'complete') { |
| 5740 | complete(); |
| 5741 | } |
| 5742 | }; |
| 5743 | } else { |
| 5744 | this.iframe.onload = complete; |
| 5745 | } |
| 5746 | }; |
| 5747 | |
| 5748 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 5749 | |
| 5750 | /***/ }), |
| 5751 | /* 34 */ |
| 5752 | /***/ (function(module, exports, __webpack_require__) { |
| 5753 | |
| 5754 | /* WEBPACK VAR INJECTION */(function(global) {/** |
| 5755 | * Module dependencies. |
| 5756 | */ |
| 5757 | |
| 5758 | var Transport = __webpack_require__(20); |
| 5759 | var parser = __webpack_require__(21); |
| 5760 | var parseqs = __webpack_require__(30); |
| 5761 | var inherit = __webpack_require__(31); |
| 5762 | var yeast = __webpack_require__(32); |
| 5763 | var debug = __webpack_require__(3)('engine.io-client:websocket'); |
| 5764 | var BrowserWebSocket = global.WebSocket || global.MozWebSocket; |
| 5765 | var NodeWebSocket; |
| 5766 | if (typeof window === 'undefined') { |
| 5767 | try { |
| 5768 | NodeWebSocket = __webpack_require__(35); |
| 5769 | } catch (e) { } |
| 5770 | } |
| 5771 | |
| 5772 | /** |
| 5773 | * Get either the `WebSocket` or `MozWebSocket` globals |
| 5774 | * in the browser or try to resolve WebSocket-compatible |
| 5775 | * interface exposed by `ws` for Node-like environment. |
| 5776 | */ |
| 5777 | |
| 5778 | var WebSocket = BrowserWebSocket; |
| 5779 | if (!WebSocket && typeof window === 'undefined') { |
| 5780 | WebSocket = NodeWebSocket; |
| 5781 | } |
| 5782 | |
| 5783 | /** |
| 5784 | * Module exports. |
| 5785 | */ |
| 5786 | |
| 5787 | module.exports = WS; |
| 5788 | |
| 5789 | /** |
| 5790 | * WebSocket transport constructor. |
| 5791 | * |
| 5792 | * @api {Object} connection options |
| 5793 | * @api public |
| 5794 | */ |
| 5795 | |
| 5796 | function WS (opts) { |
| 5797 | var forceBase64 = (opts && opts.forceBase64); |
| 5798 | if (forceBase64) { |
| 5799 | this.supportsBinary = false; |
| 5800 | } |
| 5801 | this.perMessageDeflate = opts.perMessageDeflate; |
| 5802 | this.usingBrowserWebSocket = BrowserWebSocket && !opts.forceNode; |
| 5803 | this.protocols = opts.protocols; |
| 5804 | if (!this.usingBrowserWebSocket) { |
| 5805 | WebSocket = NodeWebSocket; |
| 5806 | } |
| 5807 | Transport.call(this, opts); |
| 5808 | } |
| 5809 | |
| 5810 | /** |
| 5811 | * Inherits from Transport. |
| 5812 | */ |
| 5813 | |
| 5814 | inherit(WS, Transport); |
| 5815 | |
| 5816 | /** |
| 5817 | * Transport name. |
| 5818 | * |
| 5819 | * @api public |
| 5820 | */ |
| 5821 | |
| 5822 | WS.prototype.name = 'websocket'; |
| 5823 | |
| 5824 | /* |
| 5825 | * WebSockets support binary |
| 5826 | */ |
| 5827 | |
| 5828 | WS.prototype.supportsBinary = true; |
| 5829 | |
| 5830 | /** |
| 5831 | * Opens socket. |
| 5832 | * |
| 5833 | * @api private |
| 5834 | */ |
| 5835 | |
| 5836 | WS.prototype.doOpen = function () { |
| 5837 | if (!this.check()) { |
| 5838 | // let probe timeout |
| 5839 | return; |
| 5840 | } |
| 5841 | |
| 5842 | var uri = this.uri(); |
| 5843 | var protocols = this.protocols; |
| 5844 | var opts = { |
| 5845 | agent: this.agent, |
| 5846 | perMessageDeflate: this.perMessageDeflate |
| 5847 | }; |
| 5848 | |
| 5849 | // SSL options for Node.js client |
| 5850 | opts.pfx = this.pfx; |
| 5851 | opts.key = this.key; |
| 5852 | opts.passphrase = this.passphrase; |
| 5853 | opts.cert = this.cert; |
| 5854 | opts.ca = this.ca; |
| 5855 | opts.ciphers = this.ciphers; |
| 5856 | opts.rejectUnauthorized = this.rejectUnauthorized; |
| 5857 | if (this.extraHeaders) { |
| 5858 | opts.headers = this.extraHeaders; |
| 5859 | } |
| 5860 | if (this.localAddress) { |
| 5861 | opts.localAddress = this.localAddress; |
| 5862 | } |
| 5863 | |
| 5864 | try { |
| 5865 | this.ws = this.usingBrowserWebSocket ? (protocols ? new WebSocket(uri, protocols) : new WebSocket(uri)) : new WebSocket(uri, protocols, opts); |
| 5866 | } catch (err) { |
| 5867 | return this.emit('error', err); |
| 5868 | } |
| 5869 | |
| 5870 | if (this.ws.binaryType === undefined) { |
| 5871 | this.supportsBinary = false; |
| 5872 | } |
| 5873 | |
| 5874 | if (this.ws.supports && this.ws.supports.binary) { |
| 5875 | this.supportsBinary = true; |
| 5876 | this.ws.binaryType = 'nodebuffer'; |
| 5877 | } else { |
| 5878 | this.ws.binaryType = 'arraybuffer'; |
| 5879 | } |
| 5880 | |
| 5881 | this.addEventListeners(); |
| 5882 | }; |
| 5883 | |
| 5884 | /** |
| 5885 | * Adds event listeners to the socket |
| 5886 | * |
| 5887 | * @api private |
| 5888 | */ |
| 5889 | |
| 5890 | WS.prototype.addEventListeners = function () { |
| 5891 | var self = this; |
| 5892 | |
| 5893 | this.ws.onopen = function () { |
| 5894 | self.onOpen(); |
| 5895 | }; |
| 5896 | this.ws.onclose = function () { |
| 5897 | self.onClose(); |
| 5898 | }; |
| 5899 | this.ws.onmessage = function (ev) { |
| 5900 | self.onData(ev.data); |
| 5901 | }; |
| 5902 | this.ws.onerror = function (e) { |
| 5903 | self.onError('websocket error', e); |
| 5904 | }; |
| 5905 | }; |
| 5906 | |
| 5907 | /** |
| 5908 | * Writes data to socket. |
| 5909 | * |
| 5910 | * @param {Array} array of packets. |
| 5911 | * @api private |
| 5912 | */ |
| 5913 | |
| 5914 | WS.prototype.write = function (packets) { |
| 5915 | var self = this; |
| 5916 | this.writable = false; |
| 5917 | |
| 5918 | // encodePacket efficient as it uses WS framing |
| 5919 | // no need for encodePayload |
| 5920 | var total = packets.length; |
| 5921 | for (var i = 0, l = total; i < l; i++) { |
| 5922 | (function (packet) { |
| 5923 | parser.encodePacket(packet, self.supportsBinary, function (data) { |
| 5924 | if (!self.usingBrowserWebSocket) { |
| 5925 | // always create a new object (GH-437) |
| 5926 | var opts = {}; |
| 5927 | if (packet.options) { |
| 5928 | opts.compress = packet.options.compress; |
| 5929 | } |
| 5930 | |
| 5931 | if (self.perMessageDeflate) { |
| 5932 | var len = 'string' === typeof data ? global.Buffer.byteLength(data) : data.length; |
| 5933 | if (len < self.perMessageDeflate.threshold) { |
| 5934 | opts.compress = false; |
| 5935 | } |
| 5936 | } |
| 5937 | } |
| 5938 | |
| 5939 | // Sometimes the websocket has already been closed but the browser didn't |
| 5940 | // have a chance of informing us about it yet, in that case send will |
| 5941 | // throw an error |
| 5942 | try { |
| 5943 | if (self.usingBrowserWebSocket) { |
| 5944 | // TypeError is thrown when passing the second argument on Safari |
| 5945 | self.ws.send(data); |
| 5946 | } else { |
| 5947 | self.ws.send(data, opts); |
| 5948 | } |
| 5949 | } catch (e) { |
| 5950 | debug('websocket closed before onclose event'); |
| 5951 | } |
| 5952 | |
| 5953 | --total || done(); |
| 5954 | }); |
| 5955 | })(packets[i]); |
| 5956 | } |
| 5957 | |
| 5958 | function done () { |
| 5959 | self.emit('flush'); |
| 5960 | |
| 5961 | // fake drain |
| 5962 | // defer to next tick to allow Socket to clear writeBuffer |
| 5963 | setTimeout(function () { |
| 5964 | self.writable = true; |
| 5965 | self.emit('drain'); |
| 5966 | }, 0); |
| 5967 | } |
| 5968 | }; |
| 5969 | |
| 5970 | /** |
| 5971 | * Called upon close |
| 5972 | * |
| 5973 | * @api private |
| 5974 | */ |
| 5975 | |
| 5976 | WS.prototype.onClose = function () { |
| 5977 | Transport.prototype.onClose.call(this); |
| 5978 | }; |
| 5979 | |
| 5980 | /** |
| 5981 | * Closes socket. |
| 5982 | * |
| 5983 | * @api private |
| 5984 | */ |
| 5985 | |
| 5986 | WS.prototype.doClose = function () { |
| 5987 | if (typeof this.ws !== 'undefined') { |
| 5988 | this.ws.close(); |
| 5989 | } |
| 5990 | }; |
| 5991 | |
| 5992 | /** |
| 5993 | * Generates uri for connection. |
| 5994 | * |
| 5995 | * @api private |
| 5996 | */ |
| 5997 | |
| 5998 | WS.prototype.uri = function () { |
| 5999 | var query = this.query || {}; |
| 6000 | var schema = this.secure ? 'wss' : 'ws'; |
| 6001 | var port = ''; |
| 6002 | |
| 6003 | // avoid port if default for schema |
| 6004 | if (this.port && (('wss' === schema && Number(this.port) !== 443) || |
| 6005 | ('ws' === schema && Number(this.port) !== 80))) { |
| 6006 | port = ':' + this.port; |
| 6007 | } |
| 6008 | |
| 6009 | // append timestamp to URI |
| 6010 | if (this.timestampRequests) { |
| 6011 | query[this.timestampParam] = yeast(); |
| 6012 | } |
| 6013 | |
| 6014 | // communicate binary support capabilities |
| 6015 | if (!this.supportsBinary) { |
| 6016 | query.b64 = 1; |
| 6017 | } |
| 6018 | |
| 6019 | query = parseqs.encode(query); |
| 6020 | |
| 6021 | // prepend ? to query |
| 6022 | if (query.length) { |
| 6023 | query = '?' + query; |
| 6024 | } |
| 6025 | |
| 6026 | var ipv6 = this.hostname.indexOf(':') !== -1; |
| 6027 | return schema + '://' + (ipv6 ? '[' + this.hostname + ']' : this.hostname) + port + this.path + query; |
| 6028 | }; |
| 6029 | |
| 6030 | /** |
| 6031 | * Feature detection for WebSocket. |
| 6032 | * |
| 6033 | * @return {Boolean} whether this transport is available. |
| 6034 | * @api public |
| 6035 | */ |
| 6036 | |
| 6037 | WS.prototype.check = function () { |
| 6038 | return !!WebSocket && !('__initialize' in WebSocket && this.name === WS.prototype.name); |
| 6039 | }; |
| 6040 | |
| 6041 | /* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()))) |
| 6042 | |
| 6043 | /***/ }), |
| 6044 | /* 35 */ |
| 6045 | /***/ (function(module, exports) { |
| 6046 | |
| 6047 | /* (ignored) */ |
| 6048 | |
| 6049 | /***/ }), |
| 6050 | /* 36 */ |
| 6051 | /***/ (function(module, exports) { |
| 6052 | |
| 6053 | |
| 6054 | var indexOf = [].indexOf; |
| 6055 | |
| 6056 | module.exports = function(arr, obj){ |
| 6057 | if (indexOf) return arr.indexOf(obj); |
| 6058 | for (var i = 0; i < arr.length; ++i) { |
| 6059 | if (arr[i] === obj) return i; |
| 6060 | } |
| 6061 | return -1; |
| 6062 | }; |
| 6063 | |
| 6064 | /***/ }), |
| 6065 | /* 37 */ |
| 6066 | /***/ (function(module, exports, __webpack_require__) { |
| 6067 | |
| 6068 | 'use strict'; |
| 6069 | |
| 6070 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; |
| 6071 | |
| 6072 | /** |
| 6073 | * Module dependencies. |
| 6074 | */ |
| 6075 | |
| 6076 | var parser = __webpack_require__(7); |
| 6077 | var Emitter = __webpack_require__(8); |
| 6078 | var toArray = __webpack_require__(38); |
| 6079 | var on = __webpack_require__(39); |
| 6080 | var bind = __webpack_require__(40); |
| 6081 | var debug = __webpack_require__(3)('socket.io-client:socket'); |
| 6082 | var parseqs = __webpack_require__(30); |
| 6083 | var hasBin = __webpack_require__(23); |
| 6084 | |
| 6085 | /** |
| 6086 | * Module exports. |
| 6087 | */ |
| 6088 | |
| 6089 | module.exports = exports = Socket; |
| 6090 | |
| 6091 | /** |
| 6092 | * Internal events (blacklisted). |
| 6093 | * These events can't be emitted by the user. |
| 6094 | * |
| 6095 | * @api private |
| 6096 | */ |
| 6097 | |
| 6098 | var events = { |
| 6099 | connect: 1, |
| 6100 | connect_error: 1, |
| 6101 | connect_timeout: 1, |
| 6102 | connecting: 1, |
| 6103 | disconnect: 1, |
| 6104 | error: 1, |
| 6105 | reconnect: 1, |
| 6106 | reconnect_attempt: 1, |
| 6107 | reconnect_failed: 1, |
| 6108 | reconnect_error: 1, |
| 6109 | reconnecting: 1, |
| 6110 | ping: 1, |
| 6111 | pong: 1 |
| 6112 | }; |
| 6113 | |
| 6114 | /** |
| 6115 | * Shortcut to `Emitter#emit`. |
| 6116 | */ |
| 6117 | |
| 6118 | var emit = Emitter.prototype.emit; |
| 6119 | |
| 6120 | /** |
| 6121 | * `Socket` constructor. |
| 6122 | * |
| 6123 | * @api public |
| 6124 | */ |
| 6125 | |
| 6126 | function Socket(io, nsp, opts) { |
| 6127 | this.io = io; |
| 6128 | this.nsp = nsp; |
| 6129 | this.json = this; // compat |
| 6130 | this.ids = 0; |
| 6131 | this.acks = {}; |
| 6132 | this.receiveBuffer = []; |
| 6133 | this.sendBuffer = []; |
| 6134 | this.connected = false; |
| 6135 | this.disconnected = true; |
| 6136 | this.flags = {}; |
| 6137 | if (opts && opts.query) { |
| 6138 | this.query = opts.query; |
| 6139 | } |
| 6140 | if (this.io.autoConnect) this.open(); |
| 6141 | } |
| 6142 | |
| 6143 | /** |
| 6144 | * Mix in `Emitter`. |
| 6145 | */ |
| 6146 | |
| 6147 | Emitter(Socket.prototype); |
| 6148 | |
| 6149 | /** |
| 6150 | * Subscribe to open, close and packet events |
| 6151 | * |
| 6152 | * @api private |
| 6153 | */ |
| 6154 | |
| 6155 | Socket.prototype.subEvents = function () { |
| 6156 | if (this.subs) return; |
| 6157 | |
| 6158 | var io = this.io; |
| 6159 | this.subs = [on(io, 'open', bind(this, 'onopen')), on(io, 'packet', bind(this, 'onpacket')), on(io, 'close', bind(this, 'onclose'))]; |
| 6160 | }; |
| 6161 | |
| 6162 | /** |
| 6163 | * "Opens" the socket. |
| 6164 | * |
| 6165 | * @api public |
| 6166 | */ |
| 6167 | |
| 6168 | Socket.prototype.open = Socket.prototype.connect = function () { |
| 6169 | if (this.connected) return this; |
| 6170 | |
| 6171 | this.subEvents(); |
| 6172 | this.io.open(); // ensure open |
| 6173 | if ('open' === this.io.readyState) this.onopen(); |
| 6174 | this.emit('connecting'); |
| 6175 | return this; |
| 6176 | }; |
| 6177 | |
| 6178 | /** |
| 6179 | * Sends a `message` event. |
| 6180 | * |
| 6181 | * @return {Socket} self |
| 6182 | * @api public |
| 6183 | */ |
| 6184 | |
| 6185 | Socket.prototype.send = function () { |
| 6186 | var args = toArray(arguments); |
| 6187 | args.unshift('message'); |
| 6188 | this.emit.apply(this, args); |
| 6189 | return this; |
| 6190 | }; |
| 6191 | |
| 6192 | /** |
| 6193 | * Override `emit`. |
| 6194 | * If the event is in `events`, it's emitted normally. |
| 6195 | * |
| 6196 | * @param {String} event name |
| 6197 | * @return {Socket} self |
| 6198 | * @api public |
| 6199 | */ |
| 6200 | |
| 6201 | Socket.prototype.emit = function (ev) { |
| 6202 | if (events.hasOwnProperty(ev)) { |
| 6203 | emit.apply(this, arguments); |
| 6204 | return this; |
| 6205 | } |
| 6206 | |
| 6207 | var args = toArray(arguments); |
| 6208 | var packet = { |
| 6209 | type: (this.flags.binary !== undefined ? this.flags.binary : hasBin(args)) ? parser.BINARY_EVENT : parser.EVENT, |
| 6210 | data: args |
| 6211 | }; |
| 6212 | |
| 6213 | packet.options = {}; |
| 6214 | packet.options.compress = !this.flags || false !== this.flags.compress; |
| 6215 | |
| 6216 | // event ack callback |
| 6217 | if ('function' === typeof args[args.length - 1]) { |
| 6218 | debug('emitting packet with ack id %d', this.ids); |
| 6219 | this.acks[this.ids] = args.pop(); |
| 6220 | packet.id = this.ids++; |
| 6221 | } |
| 6222 | |
| 6223 | if (this.connected) { |
| 6224 | this.packet(packet); |
| 6225 | } else { |
| 6226 | this.sendBuffer.push(packet); |
| 6227 | } |
| 6228 | |
| 6229 | this.flags = {}; |
| 6230 | |
| 6231 | return this; |
| 6232 | }; |
| 6233 | |
| 6234 | /** |
| 6235 | * Sends a packet. |
| 6236 | * |
| 6237 | * @param {Object} packet |
| 6238 | * @api private |
| 6239 | */ |
| 6240 | |
| 6241 | Socket.prototype.packet = function (packet) { |
| 6242 | packet.nsp = this.nsp; |
| 6243 | this.io.packet(packet); |
| 6244 | }; |
| 6245 | |
| 6246 | /** |
| 6247 | * Called upon engine `open`. |
| 6248 | * |
| 6249 | * @api private |
| 6250 | */ |
| 6251 | |
| 6252 | Socket.prototype.onopen = function () { |
| 6253 | debug('transport is open - connecting'); |
| 6254 | |
| 6255 | // write connect packet if necessary |
| 6256 | if ('/' !== this.nsp) { |
| 6257 | if (this.query) { |
| 6258 | var query = _typeof(this.query) === 'object' ? parseqs.encode(this.query) : this.query; |
| 6259 | debug('sending connect packet with query %s', query); |
| 6260 | this.packet({ type: parser.CONNECT, query: query }); |
| 6261 | } else { |
| 6262 | this.packet({ type: parser.CONNECT }); |
| 6263 | } |
| 6264 | } |
| 6265 | }; |
| 6266 | |
| 6267 | /** |
| 6268 | * Called upon engine `close`. |
| 6269 | * |
| 6270 | * @param {String} reason |
| 6271 | * @api private |
| 6272 | */ |
| 6273 | |
| 6274 | Socket.prototype.onclose = function (reason) { |
| 6275 | debug('close (%s)', reason); |
| 6276 | this.connected = false; |
| 6277 | this.disconnected = true; |
| 6278 | delete this.id; |
| 6279 | this.emit('disconnect', reason); |
| 6280 | }; |
| 6281 | |
| 6282 | /** |
| 6283 | * Called with socket packet. |
| 6284 | * |
| 6285 | * @param {Object} packet |
| 6286 | * @api private |
| 6287 | */ |
| 6288 | |
| 6289 | Socket.prototype.onpacket = function (packet) { |
| 6290 | var sameNamespace = packet.nsp === this.nsp; |
| 6291 | var rootNamespaceError = packet.type === parser.ERROR && packet.nsp === '/'; |
| 6292 | |
| 6293 | if (!sameNamespace && !rootNamespaceError) return; |
| 6294 | |
| 6295 | switch (packet.type) { |
| 6296 | case parser.CONNECT: |
| 6297 | this.onconnect(); |
| 6298 | break; |
| 6299 | |
| 6300 | case parser.EVENT: |
| 6301 | this.onevent(packet); |
| 6302 | break; |
| 6303 | |
| 6304 | case parser.BINARY_EVENT: |
| 6305 | this.onevent(packet); |
| 6306 | break; |
| 6307 | |
| 6308 | case parser.ACK: |
| 6309 | this.onack(packet); |
| 6310 | break; |
| 6311 | |
| 6312 | case parser.BINARY_ACK: |
| 6313 | this.onack(packet); |
| 6314 | break; |
| 6315 | |
| 6316 | case parser.DISCONNECT: |
| 6317 | this.ondisconnect(); |
| 6318 | break; |
| 6319 | |
| 6320 | case parser.ERROR: |
| 6321 | this.emit('error', packet.data); |
| 6322 | break; |
| 6323 | } |
| 6324 | }; |
| 6325 | |
| 6326 | /** |
| 6327 | * Called upon a server event. |
| 6328 | * |
| 6329 | * @param {Object} packet |
| 6330 | * @api private |
| 6331 | */ |
| 6332 | |
| 6333 | Socket.prototype.onevent = function (packet) { |
| 6334 | var args = packet.data || []; |
| 6335 | debug('emitting event %j', args); |
| 6336 | |
| 6337 | if (null != packet.id) { |
| 6338 | debug('attaching ack callback to event'); |
| 6339 | args.push(this.ack(packet.id)); |
| 6340 | } |
| 6341 | |
| 6342 | if (this.connected) { |
| 6343 | emit.apply(this, args); |
| 6344 | } else { |
| 6345 | this.receiveBuffer.push(args); |
| 6346 | } |
| 6347 | }; |
| 6348 | |
| 6349 | /** |
| 6350 | * Produces an ack callback to emit with an event. |
| 6351 | * |
| 6352 | * @api private |
| 6353 | */ |
| 6354 | |
| 6355 | Socket.prototype.ack = function (id) { |
| 6356 | var self = this; |
| 6357 | var sent = false; |
| 6358 | return function () { |
| 6359 | // prevent double callbacks |
| 6360 | if (sent) return; |
| 6361 | sent = true; |
| 6362 | var args = toArray(arguments); |
| 6363 | debug('sending ack %j', args); |
| 6364 | |
| 6365 | self.packet({ |
| 6366 | type: hasBin(args) ? parser.BINARY_ACK : parser.ACK, |
| 6367 | id: id, |
| 6368 | data: args |
| 6369 | }); |
| 6370 | }; |
| 6371 | }; |
| 6372 | |
| 6373 | /** |
| 6374 | * Called upon a server acknowlegement. |
| 6375 | * |
| 6376 | * @param {Object} packet |
| 6377 | * @api private |
| 6378 | */ |
| 6379 | |
| 6380 | Socket.prototype.onack = function (packet) { |
| 6381 | var ack = this.acks[packet.id]; |
| 6382 | if ('function' === typeof ack) { |
| 6383 | debug('calling ack %s with %j', packet.id, packet.data); |
| 6384 | ack.apply(this, packet.data); |
| 6385 | delete this.acks[packet.id]; |
| 6386 | } else { |
| 6387 | debug('bad ack %s', packet.id); |
| 6388 | } |
| 6389 | }; |
| 6390 | |
| 6391 | /** |
| 6392 | * Called upon server connect. |
| 6393 | * |
| 6394 | * @api private |
| 6395 | */ |
| 6396 | |
| 6397 | Socket.prototype.onconnect = function () { |
| 6398 | this.connected = true; |
| 6399 | this.disconnected = false; |
| 6400 | this.emit('connect'); |
| 6401 | this.emitBuffered(); |
| 6402 | }; |
| 6403 | |
| 6404 | /** |
| 6405 | * Emit buffered events (received and emitted). |
| 6406 | * |
| 6407 | * @api private |
| 6408 | */ |
| 6409 | |
| 6410 | Socket.prototype.emitBuffered = function () { |
| 6411 | var i; |
| 6412 | for (i = 0; i < this.receiveBuffer.length; i++) { |
| 6413 | emit.apply(this, this.receiveBuffer[i]); |
| 6414 | } |
| 6415 | this.receiveBuffer = []; |
| 6416 | |
| 6417 | for (i = 0; i < this.sendBuffer.length; i++) { |
| 6418 | this.packet(this.sendBuffer[i]); |
| 6419 | } |
| 6420 | this.sendBuffer = []; |
| 6421 | }; |
| 6422 | |
| 6423 | /** |
| 6424 | * Called upon server disconnect. |
| 6425 | * |
| 6426 | * @api private |
| 6427 | */ |
| 6428 | |
| 6429 | Socket.prototype.ondisconnect = function () { |
| 6430 | debug('server disconnect (%s)', this.nsp); |
| 6431 | this.destroy(); |
| 6432 | this.onclose('io server disconnect'); |
| 6433 | }; |
| 6434 | |
| 6435 | /** |
| 6436 | * Called upon forced client/server side disconnections, |
| 6437 | * this method ensures the manager stops tracking us and |
| 6438 | * that reconnections don't get triggered for this. |
| 6439 | * |
| 6440 | * @api private. |
| 6441 | */ |
| 6442 | |
| 6443 | Socket.prototype.destroy = function () { |
| 6444 | if (this.subs) { |
| 6445 | // clean subscriptions to avoid reconnections |
| 6446 | for (var i = 0; i < this.subs.length; i++) { |
| 6447 | this.subs[i].destroy(); |
| 6448 | } |
| 6449 | this.subs = null; |
| 6450 | } |
| 6451 | |
| 6452 | this.io.destroy(this); |
| 6453 | }; |
| 6454 | |
| 6455 | /** |
| 6456 | * Disconnects the socket manually. |
| 6457 | * |
| 6458 | * @return {Socket} self |
| 6459 | * @api public |
| 6460 | */ |
| 6461 | |
| 6462 | Socket.prototype.close = Socket.prototype.disconnect = function () { |
| 6463 | if (this.connected) { |
| 6464 | debug('performing disconnect (%s)', this.nsp); |
| 6465 | this.packet({ type: parser.DISCONNECT }); |
| 6466 | } |
| 6467 | |
| 6468 | // remove socket from pool |
| 6469 | this.destroy(); |
| 6470 | |
| 6471 | if (this.connected) { |
| 6472 | // fire events |
| 6473 | this.onclose('io client disconnect'); |
| 6474 | } |
| 6475 | return this; |
| 6476 | }; |
| 6477 | |
| 6478 | /** |
| 6479 | * Sets the compress flag. |
| 6480 | * |
| 6481 | * @param {Boolean} if `true`, compresses the sending data |
| 6482 | * @return {Socket} self |
| 6483 | * @api public |
| 6484 | */ |
| 6485 | |
| 6486 | Socket.prototype.compress = function (compress) { |
| 6487 | this.flags.compress = compress; |
| 6488 | return this; |
| 6489 | }; |
| 6490 | |
| 6491 | /** |
| 6492 | * Sets the binary flag |
| 6493 | * |
| 6494 | * @param {Boolean} whether the emitted data contains binary |
| 6495 | * @return {Socket} self |
| 6496 | * @api public |
| 6497 | */ |
| 6498 | |
| 6499 | Socket.prototype.binary = function (binary) { |
| 6500 | this.flags.binary = binary; |
| 6501 | return this; |
| 6502 | }; |
| 6503 | |
| 6504 | /***/ }), |
| 6505 | /* 38 */ |
| 6506 | /***/ (function(module, exports) { |
| 6507 | |
| 6508 | module.exports = toArray |
| 6509 | |
| 6510 | function toArray(list, index) { |
| 6511 | var array = [] |
| 6512 | |
| 6513 | index = index || 0 |
| 6514 | |
| 6515 | for (var i = index || 0; i < list.length; i++) { |
| 6516 | array[i - index] = list[i] |
| 6517 | } |
| 6518 | |
| 6519 | return array |
| 6520 | } |
| 6521 | |
| 6522 | |
| 6523 | /***/ }), |
| 6524 | /* 39 */ |
| 6525 | /***/ (function(module, exports) { |
| 6526 | |
| 6527 | "use strict"; |
| 6528 | |
| 6529 | /** |
| 6530 | * Module exports. |
| 6531 | */ |
| 6532 | |
| 6533 | module.exports = on; |
| 6534 | |
| 6535 | /** |
| 6536 | * Helper for subscriptions. |
| 6537 | * |
| 6538 | * @param {Object|EventEmitter} obj with `Emitter` mixin or `EventEmitter` |
| 6539 | * @param {String} event name |
| 6540 | * @param {Function} callback |
| 6541 | * @api public |
| 6542 | */ |
| 6543 | |
| 6544 | function on(obj, ev, fn) { |
| 6545 | obj.on(ev, fn); |
| 6546 | return { |
| 6547 | destroy: function destroy() { |
| 6548 | obj.removeListener(ev, fn); |
| 6549 | } |
| 6550 | }; |
| 6551 | } |
| 6552 | |
| 6553 | /***/ }), |
| 6554 | /* 40 */ |
| 6555 | /***/ (function(module, exports) { |
| 6556 | |
| 6557 | /** |
| 6558 | * Slice reference. |
| 6559 | */ |
| 6560 | |
| 6561 | var slice = [].slice; |
| 6562 | |
| 6563 | /** |
| 6564 | * Bind `obj` to `fn`. |
| 6565 | * |
| 6566 | * @param {Object} obj |
| 6567 | * @param {Function|String} fn or string |
| 6568 | * @return {Function} |
| 6569 | * @api public |
| 6570 | */ |
| 6571 | |
| 6572 | module.exports = function(obj, fn){ |
| 6573 | if ('string' == typeof fn) fn = obj[fn]; |
| 6574 | if ('function' != typeof fn) throw new Error('bind() requires a function'); |
| 6575 | var args = slice.call(arguments, 2); |
| 6576 | return function(){ |
| 6577 | return fn.apply(obj, args.concat(slice.call(arguments))); |
| 6578 | } |
| 6579 | }; |
| 6580 | |
| 6581 | |
| 6582 | /***/ }), |
| 6583 | /* 41 */ |
| 6584 | /***/ (function(module, exports) { |
| 6585 | |
| 6586 | |
| 6587 | /** |
| 6588 | * Expose `Backoff`. |
| 6589 | */ |
| 6590 | |
| 6591 | module.exports = Backoff; |
| 6592 | |
| 6593 | /** |
| 6594 | * Initialize backoff timer with `opts`. |
| 6595 | * |
| 6596 | * - `min` initial timeout in milliseconds [100] |
| 6597 | * - `max` max timeout [10000] |
| 6598 | * - `jitter` [0] |
| 6599 | * - `factor` [2] |
| 6600 | * |
| 6601 | * @param {Object} opts |
| 6602 | * @api public |
| 6603 | */ |
| 6604 | |
| 6605 | function Backoff(opts) { |
| 6606 | opts = opts || {}; |
| 6607 | this.ms = opts.min || 100; |
| 6608 | this.max = opts.max || 10000; |
| 6609 | this.factor = opts.factor || 2; |
| 6610 | this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0; |
| 6611 | this.attempts = 0; |
| 6612 | } |
| 6613 | |
| 6614 | /** |
| 6615 | * Return the backoff duration. |
| 6616 | * |
| 6617 | * @return {Number} |
| 6618 | * @api public |
| 6619 | */ |
| 6620 | |
| 6621 | Backoff.prototype.duration = function(){ |
| 6622 | var ms = this.ms * Math.pow(this.factor, this.attempts++); |
| 6623 | if (this.jitter) { |
| 6624 | var rand = Math.random(); |
| 6625 | var deviation = Math.floor(rand * this.jitter * ms); |
| 6626 | ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation; |
| 6627 | } |
| 6628 | return Math.min(ms, this.max) | 0; |
| 6629 | }; |
| 6630 | |
| 6631 | /** |
| 6632 | * Reset the number of attempts. |
| 6633 | * |
| 6634 | * @api public |
| 6635 | */ |
| 6636 | |
| 6637 | Backoff.prototype.reset = function(){ |
| 6638 | this.attempts = 0; |
| 6639 | }; |
| 6640 | |
| 6641 | /** |
| 6642 | * Set the minimum duration |
| 6643 | * |
| 6644 | * @api public |
| 6645 | */ |
| 6646 | |
| 6647 | Backoff.prototype.setMin = function(min){ |
| 6648 | this.ms = min; |
| 6649 | }; |
| 6650 | |
| 6651 | /** |
| 6652 | * Set the maximum duration |
| 6653 | * |
| 6654 | * @api public |
| 6655 | */ |
| 6656 | |
| 6657 | Backoff.prototype.setMax = function(max){ |
| 6658 | this.max = max; |
| 6659 | }; |
| 6660 | |
| 6661 | /** |
| 6662 | * Set the jitter |
| 6663 | * |
| 6664 | * @api public |
| 6665 | */ |
| 6666 | |
| 6667 | Backoff.prototype.setJitter = function(jitter){ |
| 6668 | this.jitter = jitter; |
| 6669 | }; |
| 6670 | |
| 6671 | |
| 6672 | |
| 6673 | /***/ }) |
| 6674 | /******/ ]) |
| 6675 | }); |
| 6676 | ; |
| 6677 | //# sourceMappingURL=socket.io.dev.js.map |