PageRenderTime 69ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/libs/engine.io-client/1.4.3/engine.io.js

https://gitlab.com/mba811/static
JavaScript | 3872 lines | 2169 code | 672 blank | 1031 comment | 551 complexity | 37bbbe99db35ea7420f80666147d49cf MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.eio=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  2. module.exports = _dereq_('./lib/');
  3. },{"./lib/":2}],2:[function(_dereq_,module,exports){
  4. module.exports = _dereq_('./socket');
  5. /**
  6. * Exports parser
  7. *
  8. * @api public
  9. *
  10. */
  11. module.exports.parser = _dereq_('engine.io-parser');
  12. },{"./socket":3,"engine.io-parser":17}],3:[function(_dereq_,module,exports){
  13. (function (global){
  14. /**
  15. * Module dependencies.
  16. */
  17. var transports = _dereq_('./transports');
  18. var Emitter = _dereq_('component-emitter');
  19. var debug = _dereq_('debug')('engine.io-client:socket');
  20. var index = _dereq_('indexof');
  21. var parser = _dereq_('engine.io-parser');
  22. var parseuri = _dereq_('parseuri');
  23. var parsejson = _dereq_('parsejson');
  24. var parseqs = _dereq_('parseqs');
  25. /**
  26. * Module exports.
  27. */
  28. module.exports = Socket;
  29. /**
  30. * Noop function.
  31. *
  32. * @api private
  33. */
  34. function noop(){}
  35. /**
  36. * Socket constructor.
  37. *
  38. * @param {String|Object} uri or options
  39. * @param {Object} options
  40. * @api public
  41. */
  42. function Socket(uri, opts){
  43. if (!(this instanceof Socket)) return new Socket(uri, opts);
  44. opts = opts || {};
  45. if (uri && 'object' == typeof uri) {
  46. opts = uri;
  47. uri = null;
  48. }
  49. if (uri) {
  50. uri = parseuri(uri);
  51. opts.host = uri.host;
  52. opts.secure = uri.protocol == 'https' || uri.protocol == 'wss';
  53. opts.port = uri.port;
  54. if (uri.query) opts.query = uri.query;
  55. }
  56. this.secure = null != opts.secure ? opts.secure :
  57. (global.location && 'https:' == location.protocol);
  58. if (opts.host) {
  59. var pieces = opts.host.split(':');
  60. opts.hostname = pieces.shift();
  61. if (pieces.length) opts.port = pieces.pop();
  62. }
  63. this.agent = opts.agent || false;
  64. this.hostname = opts.hostname ||
  65. (global.location ? location.hostname : 'localhost');
  66. this.port = opts.port || (global.location && location.port ?
  67. location.port :
  68. (this.secure ? 443 : 80));
  69. this.query = opts.query || {};
  70. if ('string' == typeof this.query) this.query = parseqs.decode(this.query);
  71. this.upgrade = false !== opts.upgrade;
  72. this.path = (opts.path || '/engine.io').replace(/\/$/, '') + '/';
  73. this.forceJSONP = !!opts.forceJSONP;
  74. this.jsonp = false !== opts.jsonp;
  75. this.forceBase64 = !!opts.forceBase64;
  76. this.enablesXDR = !!opts.enablesXDR;
  77. this.timestampParam = opts.timestampParam || 't';
  78. this.timestampRequests = opts.timestampRequests;
  79. this.transports = opts.transports || ['polling', 'websocket'];
  80. this.readyState = '';
  81. this.writeBuffer = [];
  82. this.callbackBuffer = [];
  83. this.policyPort = opts.policyPort || 843;
  84. this.rememberUpgrade = opts.rememberUpgrade || false;
  85. this.open();
  86. this.binaryType = null;
  87. this.onlyBinaryUpgrades = opts.onlyBinaryUpgrades;
  88. }
  89. Socket.priorWebsocketSuccess = false;
  90. /**
  91. * Mix in `Emitter`.
  92. */
  93. Emitter(Socket.prototype);
  94. /**
  95. * Protocol version.
  96. *
  97. * @api public
  98. */
  99. Socket.protocol = parser.protocol; // this is an int
  100. /**
  101. * Expose deps for legacy compatibility
  102. * and standalone browser access.
  103. */
  104. Socket.Socket = Socket;
  105. Socket.Transport = _dereq_('./transport');
  106. Socket.transports = _dereq_('./transports');
  107. Socket.parser = _dereq_('engine.io-parser');
  108. /**
  109. * Creates transport of the given type.
  110. *
  111. * @param {String} transport name
  112. * @return {Transport}
  113. * @api private
  114. */
  115. Socket.prototype.createTransport = function (name) {
  116. debug('creating transport "%s"', name);
  117. var query = clone(this.query);
  118. // append engine.io protocol identifier
  119. query.EIO = parser.protocol;
  120. // transport name
  121. query.transport = name;
  122. // session id if we already have one
  123. if (this.id) query.sid = this.id;
  124. var transport = new transports[name]({
  125. agent: this.agent,
  126. hostname: this.hostname,
  127. port: this.port,
  128. secure: this.secure,
  129. path: this.path,
  130. query: query,
  131. forceJSONP: this.forceJSONP,
  132. jsonp: this.jsonp,
  133. forceBase64: this.forceBase64,
  134. enablesXDR: this.enablesXDR,
  135. timestampRequests: this.timestampRequests,
  136. timestampParam: this.timestampParam,
  137. policyPort: this.policyPort,
  138. socket: this
  139. });
  140. return transport;
  141. };
  142. function clone (obj) {
  143. var o = {};
  144. for (var i in obj) {
  145. if (obj.hasOwnProperty(i)) {
  146. o[i] = obj[i];
  147. }
  148. }
  149. return o;
  150. }
  151. /**
  152. * Initializes transport to use and starts probe.
  153. *
  154. * @api private
  155. */
  156. Socket.prototype.open = function () {
  157. var transport;
  158. if (this.rememberUpgrade && Socket.priorWebsocketSuccess && this.transports.indexOf('websocket') != -1) {
  159. transport = 'websocket';
  160. } else if (0 == this.transports.length) {
  161. // Emit error on next tick so it can be listened to
  162. var self = this;
  163. setTimeout(function() {
  164. self.emit('error', 'No transports available');
  165. }, 0);
  166. return;
  167. } else {
  168. transport = this.transports[0];
  169. }
  170. this.readyState = 'opening';
  171. // Retry with the next transport if the transport is disabled (jsonp: false)
  172. var transport;
  173. try {
  174. transport = this.createTransport(transport);
  175. } catch (e) {
  176. this.transports.shift();
  177. this.open();
  178. return;
  179. }
  180. transport.open();
  181. this.setTransport(transport);
  182. };
  183. /**
  184. * Sets the current transport. Disables the existing one (if any).
  185. *
  186. * @api private
  187. */
  188. Socket.prototype.setTransport = function(transport){
  189. debug('setting transport %s', transport.name);
  190. var self = this;
  191. if (this.transport) {
  192. debug('clearing existing transport %s', this.transport.name);
  193. this.transport.removeAllListeners();
  194. }
  195. // set up transport
  196. this.transport = transport;
  197. // set up transport listeners
  198. transport
  199. .on('drain', function(){
  200. self.onDrain();
  201. })
  202. .on('packet', function(packet){
  203. self.onPacket(packet);
  204. })
  205. .on('error', function(e){
  206. self.onError(e);
  207. })
  208. .on('close', function(){
  209. self.onClose('transport close');
  210. });
  211. };
  212. /**
  213. * Probes a transport.
  214. *
  215. * @param {String} transport name
  216. * @api private
  217. */
  218. Socket.prototype.probe = function (name) {
  219. debug('probing transport "%s"', name);
  220. var transport = this.createTransport(name, { probe: 1 })
  221. , failed = false
  222. , self = this;
  223. Socket.priorWebsocketSuccess = false;
  224. function onTransportOpen(){
  225. if (self.onlyBinaryUpgrades) {
  226. var upgradeLosesBinary = !this.supportsBinary && self.transport.supportsBinary;
  227. failed = failed || upgradeLosesBinary;
  228. }
  229. if (failed) return;
  230. debug('probe transport "%s" opened', name);
  231. transport.send([{ type: 'ping', data: 'probe' }]);
  232. transport.once('packet', function (msg) {
  233. if (failed) return;
  234. if ('pong' == msg.type && 'probe' == msg.data) {
  235. debug('probe transport "%s" pong', name);
  236. self.upgrading = true;
  237. self.emit('upgrading', transport);
  238. if (!transport) return;
  239. Socket.priorWebsocketSuccess = 'websocket' == transport.name;
  240. debug('pausing current transport "%s"', self.transport.name);
  241. self.transport.pause(function () {
  242. if (failed) return;
  243. if ('closed' == self.readyState) return;
  244. debug('changing transport and sending upgrade packet');
  245. cleanup();
  246. self.setTransport(transport);
  247. transport.send([{ type: 'upgrade' }]);
  248. self.emit('upgrade', transport);
  249. transport = null;
  250. self.upgrading = false;
  251. self.flush();
  252. });
  253. } else {
  254. debug('probe transport "%s" failed', name);
  255. var err = new Error('probe error');
  256. err.transport = transport.name;
  257. self.emit('upgradeError', err);
  258. }
  259. });
  260. }
  261. function freezeTransport() {
  262. if (failed) return;
  263. // Any callback called by transport should be ignored since now
  264. failed = true;
  265. cleanup();
  266. transport.close();
  267. transport = null;
  268. }
  269. //Handle any error that happens while probing
  270. function onerror(err) {
  271. var error = new Error('probe error: ' + err);
  272. error.transport = transport.name;
  273. freezeTransport();
  274. debug('probe transport "%s" failed because of error: %s', name, err);
  275. self.emit('upgradeError', error);
  276. }
  277. function onTransportClose(){
  278. onerror("transport closed");
  279. }
  280. //When the socket is closed while we're probing
  281. function onclose(){
  282. onerror("socket closed");
  283. }
  284. //When the socket is upgraded while we're probing
  285. function onupgrade(to){
  286. if (transport && to.name != transport.name) {
  287. debug('"%s" works - aborting "%s"', to.name, transport.name);
  288. freezeTransport();
  289. }
  290. }
  291. //Remove all listeners on the transport and on self
  292. function cleanup(){
  293. transport.removeListener('open', onTransportOpen);
  294. transport.removeListener('error', onerror);
  295. transport.removeListener('close', onTransportClose);
  296. self.removeListener('close', onclose);
  297. self.removeListener('upgrading', onupgrade);
  298. }
  299. transport.once('open', onTransportOpen);
  300. transport.once('error', onerror);
  301. transport.once('close', onTransportClose);
  302. this.once('close', onclose);
  303. this.once('upgrading', onupgrade);
  304. transport.open();
  305. };
  306. /**
  307. * Called when connection is deemed open.
  308. *
  309. * @api public
  310. */
  311. Socket.prototype.onOpen = function () {
  312. debug('socket open');
  313. this.readyState = 'open';
  314. Socket.priorWebsocketSuccess = 'websocket' == this.transport.name;
  315. this.emit('open');
  316. this.flush();
  317. // we check for `readyState` in case an `open`
  318. // listener already closed the socket
  319. if ('open' == this.readyState && this.upgrade && this.transport.pause) {
  320. debug('starting upgrade probes');
  321. for (var i = 0, l = this.upgrades.length; i < l; i++) {
  322. this.probe(this.upgrades[i]);
  323. }
  324. }
  325. };
  326. /**
  327. * Handles a packet.
  328. *
  329. * @api private
  330. */
  331. Socket.prototype.onPacket = function (packet) {
  332. if ('opening' == this.readyState || 'open' == this.readyState) {
  333. debug('socket receive: type "%s", data "%s"', packet.type, packet.data);
  334. this.emit('packet', packet);
  335. // Socket is live - any packet counts
  336. this.emit('heartbeat');
  337. switch (packet.type) {
  338. case 'open':
  339. this.onHandshake(parsejson(packet.data));
  340. break;
  341. case 'pong':
  342. this.setPing();
  343. break;
  344. case 'error':
  345. var err = new Error('server error');
  346. err.code = packet.data;
  347. this.emit('error', err);
  348. break;
  349. case 'message':
  350. this.emit('data', packet.data);
  351. this.emit('message', packet.data);
  352. break;
  353. }
  354. } else {
  355. debug('packet received with socket readyState "%s"', this.readyState);
  356. }
  357. };
  358. /**
  359. * Called upon handshake completion.
  360. *
  361. * @param {Object} handshake obj
  362. * @api private
  363. */
  364. Socket.prototype.onHandshake = function (data) {
  365. this.emit('handshake', data);
  366. this.id = data.sid;
  367. this.transport.query.sid = data.sid;
  368. this.upgrades = this.filterUpgrades(data.upgrades);
  369. this.pingInterval = data.pingInterval;
  370. this.pingTimeout = data.pingTimeout;
  371. this.onOpen();
  372. // In case open handler closes socket
  373. if ('closed' == this.readyState) return;
  374. this.setPing();
  375. // Prolong liveness of socket on heartbeat
  376. this.removeListener('heartbeat', this.onHeartbeat);
  377. this.on('heartbeat', this.onHeartbeat);
  378. };
  379. /**
  380. * Resets ping timeout.
  381. *
  382. * @api private
  383. */
  384. Socket.prototype.onHeartbeat = function (timeout) {
  385. clearTimeout(this.pingTimeoutTimer);
  386. var self = this;
  387. self.pingTimeoutTimer = setTimeout(function () {
  388. if ('closed' == self.readyState) return;
  389. self.onClose('ping timeout');
  390. }, timeout || (self.pingInterval + self.pingTimeout));
  391. };
  392. /**
  393. * Pings server every `this.pingInterval` and expects response
  394. * within `this.pingTimeout` or closes connection.
  395. *
  396. * @api private
  397. */
  398. Socket.prototype.setPing = function () {
  399. var self = this;
  400. clearTimeout(self.pingIntervalTimer);
  401. self.pingIntervalTimer = setTimeout(function () {
  402. debug('writing ping packet - expecting pong within %sms', self.pingTimeout);
  403. self.ping();
  404. self.onHeartbeat(self.pingTimeout);
  405. }, self.pingInterval);
  406. };
  407. /**
  408. * Sends a ping packet.
  409. *
  410. * @api public
  411. */
  412. Socket.prototype.ping = function () {
  413. this.sendPacket('ping');
  414. };
  415. /**
  416. * Called on `drain` event
  417. *
  418. * @api private
  419. */
  420. Socket.prototype.onDrain = function() {
  421. for (var i = 0; i < this.prevBufferLen; i++) {
  422. if (this.callbackBuffer[i]) {
  423. this.callbackBuffer[i]();
  424. }
  425. }
  426. this.writeBuffer.splice(0, this.prevBufferLen);
  427. this.callbackBuffer.splice(0, this.prevBufferLen);
  428. // setting prevBufferLen = 0 is very important
  429. // for example, when upgrading, upgrade packet is sent over,
  430. // and a nonzero prevBufferLen could cause problems on `drain`
  431. this.prevBufferLen = 0;
  432. if (this.writeBuffer.length == 0) {
  433. this.emit('drain');
  434. } else {
  435. this.flush();
  436. }
  437. };
  438. /**
  439. * Flush write buffers.
  440. *
  441. * @api private
  442. */
  443. Socket.prototype.flush = function () {
  444. if ('closed' != this.readyState && this.transport.writable &&
  445. !this.upgrading && this.writeBuffer.length) {
  446. debug('flushing %d packets in socket', this.writeBuffer.length);
  447. this.transport.send(this.writeBuffer);
  448. // keep track of current length of writeBuffer
  449. // splice writeBuffer and callbackBuffer on `drain`
  450. this.prevBufferLen = this.writeBuffer.length;
  451. this.emit('flush');
  452. }
  453. };
  454. /**
  455. * Sends a message.
  456. *
  457. * @param {String} message.
  458. * @param {Function} callback function.
  459. * @return {Socket} for chaining.
  460. * @api public
  461. */
  462. Socket.prototype.write =
  463. Socket.prototype.send = function (msg, fn) {
  464. this.sendPacket('message', msg, fn);
  465. return this;
  466. };
  467. /**
  468. * Sends a packet.
  469. *
  470. * @param {String} packet type.
  471. * @param {String} data.
  472. * @param {Function} callback function.
  473. * @api private
  474. */
  475. Socket.prototype.sendPacket = function (type, data, fn) {
  476. if ('closing' == this.readyState || 'closed' == this.readyState) {
  477. return;
  478. }
  479. var packet = { type: type, data: data };
  480. this.emit('packetCreate', packet);
  481. this.writeBuffer.push(packet);
  482. this.callbackBuffer.push(fn);
  483. this.flush();
  484. };
  485. /**
  486. * Closes the connection.
  487. *
  488. * @api private
  489. */
  490. Socket.prototype.close = function () {
  491. if ('opening' == this.readyState || 'open' == this.readyState) {
  492. this.readyState = 'closing';
  493. var self = this;
  494. function close() {
  495. self.onClose('forced close');
  496. debug('socket closing - telling transport to close');
  497. self.transport.close();
  498. }
  499. function cleanupAndClose() {
  500. self.removeListener('upgrade', cleanupAndClose);
  501. self.removeListener('upgradeError', cleanupAndClose);
  502. close();
  503. }
  504. function waitForUpgrade() {
  505. // wait for upgrade to finish since we can't send packets while pausing a transport
  506. self.once('upgrade', cleanupAndClose);
  507. self.once('upgradeError', cleanupAndClose);
  508. }
  509. if (this.writeBuffer.length) {
  510. this.once('drain', function() {
  511. if (this.upgrading) {
  512. waitForUpgrade();
  513. } else {
  514. close();
  515. }
  516. });
  517. } else if (this.upgrading) {
  518. waitForUpgrade();
  519. } else {
  520. close();
  521. }
  522. }
  523. return this;
  524. };
  525. /**
  526. * Called upon transport error
  527. *
  528. * @api private
  529. */
  530. Socket.prototype.onError = function (err) {
  531. debug('socket error %j', err);
  532. Socket.priorWebsocketSuccess = false;
  533. this.emit('error', err);
  534. this.onClose('transport error', err);
  535. };
  536. /**
  537. * Called upon transport close.
  538. *
  539. * @api private
  540. */
  541. Socket.prototype.onClose = function (reason, desc) {
  542. if ('opening' == this.readyState || 'open' == this.readyState || 'closing' == this.readyState) {
  543. debug('socket close with reason: "%s"', reason);
  544. var self = this;
  545. // clear timers
  546. clearTimeout(this.pingIntervalTimer);
  547. clearTimeout(this.pingTimeoutTimer);
  548. // clean buffers in next tick, so developers can still
  549. // grab the buffers on `close` event
  550. setTimeout(function() {
  551. self.writeBuffer = [];
  552. self.callbackBuffer = [];
  553. self.prevBufferLen = 0;
  554. }, 0);
  555. // stop event from firing again for transport
  556. this.transport.removeAllListeners('close');
  557. // ensure transport won't stay open
  558. this.transport.close();
  559. // ignore further transport communication
  560. this.transport.removeAllListeners();
  561. // set ready state
  562. this.readyState = 'closed';
  563. // clear session id
  564. this.id = null;
  565. // emit close event
  566. this.emit('close', reason, desc);
  567. }
  568. };
  569. /**
  570. * Filters upgrades, returning only those matching client transports.
  571. *
  572. * @param {Array} server upgrades
  573. * @api private
  574. *
  575. */
  576. Socket.prototype.filterUpgrades = function (upgrades) {
  577. var filteredUpgrades = [];
  578. for (var i = 0, j = upgrades.length; i<j; i++) {
  579. if (~index(this.transports, upgrades[i])) filteredUpgrades.push(upgrades[i]);
  580. }
  581. return filteredUpgrades;
  582. };
  583. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  584. },{"./transport":4,"./transports":5,"component-emitter":12,"debug":14,"engine.io-parser":17,"indexof":25,"parsejson":26,"parseqs":27,"parseuri":28}],4:[function(_dereq_,module,exports){
  585. /**
  586. * Module dependencies.
  587. */
  588. var parser = _dereq_('engine.io-parser');
  589. var Emitter = _dereq_('component-emitter');
  590. /**
  591. * Module exports.
  592. */
  593. module.exports = Transport;
  594. /**
  595. * Transport abstract constructor.
  596. *
  597. * @param {Object} options.
  598. * @api private
  599. */
  600. function Transport (opts) {
  601. this.path = opts.path;
  602. this.hostname = opts.hostname;
  603. this.port = opts.port;
  604. this.secure = opts.secure;
  605. this.query = opts.query;
  606. this.timestampParam = opts.timestampParam;
  607. this.timestampRequests = opts.timestampRequests;
  608. this.readyState = '';
  609. this.agent = opts.agent || false;
  610. this.socket = opts.socket;
  611. this.enablesXDR = opts.enablesXDR;
  612. }
  613. /**
  614. * Mix in `Emitter`.
  615. */
  616. Emitter(Transport.prototype);
  617. /**
  618. * A counter used to prevent collisions in the timestamps used
  619. * for cache busting.
  620. */
  621. Transport.timestamps = 0;
  622. /**
  623. * Emits an error.
  624. *
  625. * @param {String} str
  626. * @return {Transport} for chaining
  627. * @api public
  628. */
  629. Transport.prototype.onError = function (msg, desc) {
  630. var err = new Error(msg);
  631. err.type = 'TransportError';
  632. err.description = desc;
  633. this.emit('error', err);
  634. return this;
  635. };
  636. /**
  637. * Opens the transport.
  638. *
  639. * @api public
  640. */
  641. Transport.prototype.open = function () {
  642. if ('closed' == this.readyState || '' == this.readyState) {
  643. this.readyState = 'opening';
  644. this.doOpen();
  645. }
  646. return this;
  647. };
  648. /**
  649. * Closes the transport.
  650. *
  651. * @api private
  652. */
  653. Transport.prototype.close = function () {
  654. if ('opening' == this.readyState || 'open' == this.readyState) {
  655. this.doClose();
  656. this.onClose();
  657. }
  658. return this;
  659. };
  660. /**
  661. * Sends multiple packets.
  662. *
  663. * @param {Array} packets
  664. * @api private
  665. */
  666. Transport.prototype.send = function(packets){
  667. if ('open' == this.readyState) {
  668. this.write(packets);
  669. } else {
  670. throw new Error('Transport not open');
  671. }
  672. };
  673. /**
  674. * Called upon open
  675. *
  676. * @api private
  677. */
  678. Transport.prototype.onOpen = function () {
  679. this.readyState = 'open';
  680. this.writable = true;
  681. this.emit('open');
  682. };
  683. /**
  684. * Called with data.
  685. *
  686. * @param {String} data
  687. * @api private
  688. */
  689. Transport.prototype.onData = function(data){
  690. var packet = parser.decodePacket(data, this.socket.binaryType);
  691. this.onPacket(packet);
  692. };
  693. /**
  694. * Called with a decoded packet.
  695. */
  696. Transport.prototype.onPacket = function (packet) {
  697. this.emit('packet', packet);
  698. };
  699. /**
  700. * Called upon close.
  701. *
  702. * @api private
  703. */
  704. Transport.prototype.onClose = function () {
  705. this.readyState = 'closed';
  706. this.emit('close');
  707. };
  708. },{"component-emitter":12,"engine.io-parser":17}],5:[function(_dereq_,module,exports){
  709. (function (global){
  710. /**
  711. * Module dependencies
  712. */
  713. var XMLHttpRequest = _dereq_('xmlhttprequest');
  714. var XHR = _dereq_('./polling-xhr');
  715. var JSONP = _dereq_('./polling-jsonp');
  716. var websocket = _dereq_('./websocket');
  717. /**
  718. * Export transports.
  719. */
  720. exports.polling = polling;
  721. exports.websocket = websocket;
  722. /**
  723. * Polling transport polymorphic constructor.
  724. * Decides on xhr vs jsonp based on feature detection.
  725. *
  726. * @api private
  727. */
  728. function polling(opts){
  729. var xhr;
  730. var xd = false;
  731. var xs = false;
  732. var jsonp = false !== opts.jsonp;
  733. if (global.location) {
  734. var isSSL = 'https:' == location.protocol;
  735. var port = location.port;
  736. // some user agents have empty `location.port`
  737. if (!port) {
  738. port = isSSL ? 443 : 80;
  739. }
  740. xd = opts.hostname != location.hostname || port != opts.port;
  741. xs = opts.secure != isSSL;
  742. }
  743. opts.xdomain = xd;
  744. opts.xscheme = xs;
  745. xhr = new XMLHttpRequest(opts);
  746. if ('open' in xhr && !opts.forceJSONP) {
  747. return new XHR(opts);
  748. } else {
  749. if (!jsonp) throw new Error('JSONP disabled');
  750. return new JSONP(opts);
  751. }
  752. }
  753. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  754. },{"./polling-jsonp":6,"./polling-xhr":7,"./websocket":9,"xmlhttprequest":10}],6:[function(_dereq_,module,exports){
  755. (function (global){
  756. /**
  757. * Module requirements.
  758. */
  759. var Polling = _dereq_('./polling');
  760. var inherit = _dereq_('component-inherit');
  761. /**
  762. * Module exports.
  763. */
  764. module.exports = JSONPPolling;
  765. /**
  766. * Cached regular expressions.
  767. */
  768. var rNewline = /\n/g;
  769. var rEscapedNewline = /\\n/g;
  770. /**
  771. * Global JSONP callbacks.
  772. */
  773. var callbacks;
  774. /**
  775. * Callbacks count.
  776. */
  777. var index = 0;
  778. /**
  779. * Noop.
  780. */
  781. function empty () { }
  782. /**
  783. * JSONP Polling constructor.
  784. *
  785. * @param {Object} opts.
  786. * @api public
  787. */
  788. function JSONPPolling (opts) {
  789. Polling.call(this, opts);
  790. this.query = this.query || {};
  791. // define global callbacks array if not present
  792. // we do this here (lazily) to avoid unneeded global pollution
  793. if (!callbacks) {
  794. // we need to consider multiple engines in the same page
  795. if (!global.___eio) global.___eio = [];
  796. callbacks = global.___eio;
  797. }
  798. // callback identifier
  799. this.index = callbacks.length;
  800. // add callback to jsonp global
  801. var self = this;
  802. callbacks.push(function (msg) {
  803. self.onData(msg);
  804. });
  805. // append to query string
  806. this.query.j = this.index;
  807. // prevent spurious errors from being emitted when the window is unloaded
  808. if (global.document && global.addEventListener) {
  809. global.addEventListener('beforeunload', function () {
  810. if (self.script) self.script.onerror = empty;
  811. }, false);
  812. }
  813. }
  814. /**
  815. * Inherits from Polling.
  816. */
  817. inherit(JSONPPolling, Polling);
  818. /*
  819. * JSONP only supports binary as base64 encoded strings
  820. */
  821. JSONPPolling.prototype.supportsBinary = false;
  822. /**
  823. * Closes the socket.
  824. *
  825. * @api private
  826. */
  827. JSONPPolling.prototype.doClose = function () {
  828. if (this.script) {
  829. this.script.parentNode.removeChild(this.script);
  830. this.script = null;
  831. }
  832. if (this.form) {
  833. this.form.parentNode.removeChild(this.form);
  834. this.form = null;
  835. this.iframe = null;
  836. }
  837. Polling.prototype.doClose.call(this);
  838. };
  839. /**
  840. * Starts a poll cycle.
  841. *
  842. * @api private
  843. */
  844. JSONPPolling.prototype.doPoll = function () {
  845. var self = this;
  846. var script = document.createElement('script');
  847. if (this.script) {
  848. this.script.parentNode.removeChild(this.script);
  849. this.script = null;
  850. }
  851. script.async = true;
  852. script.src = this.uri();
  853. script.onerror = function(e){
  854. self.onError('jsonp poll error',e);
  855. };
  856. var insertAt = document.getElementsByTagName('script')[0];
  857. insertAt.parentNode.insertBefore(script, insertAt);
  858. this.script = script;
  859. var isUAgecko = 'undefined' != typeof navigator && /gecko/i.test(navigator.userAgent);
  860. if (isUAgecko) {
  861. setTimeout(function () {
  862. var iframe = document.createElement('iframe');
  863. document.body.appendChild(iframe);
  864. document.body.removeChild(iframe);
  865. }, 100);
  866. }
  867. };
  868. /**
  869. * Writes with a hidden iframe.
  870. *
  871. * @param {String} data to send
  872. * @param {Function} called upon flush.
  873. * @api private
  874. */
  875. JSONPPolling.prototype.doWrite = function (data, fn) {
  876. var self = this;
  877. if (!this.form) {
  878. var form = document.createElement('form');
  879. var area = document.createElement('textarea');
  880. var id = this.iframeId = 'eio_iframe_' + this.index;
  881. var iframe;
  882. form.className = 'socketio';
  883. form.style.position = 'absolute';
  884. form.style.top = '-1000px';
  885. form.style.left = '-1000px';
  886. form.target = id;
  887. form.method = 'POST';
  888. form.setAttribute('accept-charset', 'utf-8');
  889. area.name = 'd';
  890. form.appendChild(area);
  891. document.body.appendChild(form);
  892. this.form = form;
  893. this.area = area;
  894. }
  895. this.form.action = this.uri();
  896. function complete () {
  897. initIframe();
  898. fn();
  899. }
  900. function initIframe () {
  901. if (self.iframe) {
  902. try {
  903. self.form.removeChild(self.iframe);
  904. } catch (e) {
  905. self.onError('jsonp polling iframe removal error', e);
  906. }
  907. }
  908. try {
  909. // ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
  910. var html = '<iframe src="javascript:0" name="'+ self.iframeId +'">';
  911. iframe = document.createElement(html);
  912. } catch (e) {
  913. iframe = document.createElement('iframe');
  914. iframe.name = self.iframeId;
  915. iframe.src = 'javascript:0';
  916. }
  917. iframe.id = self.iframeId;
  918. self.form.appendChild(iframe);
  919. self.iframe = iframe;
  920. }
  921. initIframe();
  922. // escape \n to prevent it from being converted into \r\n by some UAs
  923. // double escaping is required for escaped new lines because unescaping of new lines can be done safely on server-side
  924. data = data.replace(rEscapedNewline, '\\\n');
  925. this.area.value = data.replace(rNewline, '\\n');
  926. try {
  927. this.form.submit();
  928. } catch(e) {}
  929. if (this.iframe.attachEvent) {
  930. this.iframe.onreadystatechange = function(){
  931. if (self.iframe.readyState == 'complete') {
  932. complete();
  933. }
  934. };
  935. } else {
  936. this.iframe.onload = complete;
  937. }
  938. };
  939. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  940. },{"./polling":8,"component-inherit":13}],7:[function(_dereq_,module,exports){
  941. (function (global){
  942. /**
  943. * Module requirements.
  944. */
  945. var XMLHttpRequest = _dereq_('xmlhttprequest');
  946. var Polling = _dereq_('./polling');
  947. var Emitter = _dereq_('component-emitter');
  948. var inherit = _dereq_('component-inherit');
  949. var debug = _dereq_('debug')('engine.io-client:polling-xhr');
  950. /**
  951. * Module exports.
  952. */
  953. module.exports = XHR;
  954. module.exports.Request = Request;
  955. /**
  956. * Empty function
  957. */
  958. function empty(){}
  959. /**
  960. * XHR Polling constructor.
  961. *
  962. * @param {Object} opts
  963. * @api public
  964. */
  965. function XHR(opts){
  966. Polling.call(this, opts);
  967. if (global.location) {
  968. var isSSL = 'https:' == location.protocol;
  969. var port = location.port;
  970. // some user agents have empty `location.port`
  971. if (!port) {
  972. port = isSSL ? 443 : 80;
  973. }
  974. this.xd = opts.hostname != global.location.hostname ||
  975. port != opts.port;
  976. this.xs = opts.secure != isSSL;
  977. }
  978. }
  979. /**
  980. * Inherits from Polling.
  981. */
  982. inherit(XHR, Polling);
  983. /**
  984. * XHR supports binary
  985. */
  986. XHR.prototype.supportsBinary = true;
  987. /**
  988. * Creates a request.
  989. *
  990. * @param {String} method
  991. * @api private
  992. */
  993. XHR.prototype.request = function(opts){
  994. opts = opts || {};
  995. opts.uri = this.uri();
  996. opts.xd = this.xd;
  997. opts.xs = this.xs;
  998. opts.agent = this.agent || false;
  999. opts.supportsBinary = this.supportsBinary;
  1000. opts.enablesXDR = this.enablesXDR;
  1001. return new Request(opts);
  1002. };
  1003. /**
  1004. * Sends data.
  1005. *
  1006. * @param {String} data to send.
  1007. * @param {Function} called upon flush.
  1008. * @api private
  1009. */
  1010. XHR.prototype.doWrite = function(data, fn){
  1011. var isBinary = typeof data !== 'string' && data !== undefined;
  1012. var req = this.request({ method: 'POST', data: data, isBinary: isBinary });
  1013. var self = this;
  1014. req.on('success', fn);
  1015. req.on('error', function(err){
  1016. self.onError('xhr post error', err);
  1017. });
  1018. this.sendXhr = req;
  1019. };
  1020. /**
  1021. * Starts a poll cycle.
  1022. *
  1023. * @api private
  1024. */
  1025. XHR.prototype.doPoll = function(){
  1026. debug('xhr poll');
  1027. var req = this.request();
  1028. var self = this;
  1029. req.on('data', function(data){
  1030. self.onData(data);
  1031. });
  1032. req.on('error', function(err){
  1033. self.onError('xhr poll error', err);
  1034. });
  1035. this.pollXhr = req;
  1036. };
  1037. /**
  1038. * Request constructor
  1039. *
  1040. * @param {Object} options
  1041. * @api public
  1042. */
  1043. function Request(opts){
  1044. this.method = opts.method || 'GET';
  1045. this.uri = opts.uri;
  1046. this.xd = !!opts.xd;
  1047. this.xs = !!opts.xs;
  1048. this.async = false !== opts.async;
  1049. this.data = undefined != opts.data ? opts.data : null;
  1050. this.agent = opts.agent;
  1051. this.isBinary = opts.isBinary;
  1052. this.supportsBinary = opts.supportsBinary;
  1053. this.enablesXDR = opts.enablesXDR;
  1054. this.create();
  1055. }
  1056. /**
  1057. * Mix in `Emitter`.
  1058. */
  1059. Emitter(Request.prototype);
  1060. /**
  1061. * Creates the XHR object and sends the request.
  1062. *
  1063. * @api private
  1064. */
  1065. Request.prototype.create = function(){
  1066. var xhr = this.xhr = new XMLHttpRequest({ agent: this.agent, xdomain: this.xd, xscheme: this.xs, enablesXDR: this.enablesXDR });
  1067. var self = this;
  1068. try {
  1069. debug('xhr open %s: %s', this.method, this.uri);
  1070. xhr.open(this.method, this.uri, this.async);
  1071. if (this.supportsBinary) {
  1072. // This has to be done after open because Firefox is stupid
  1073. // http://stackoverflow.com/questions/13216903/get-binary-data-with-xmlhttprequest-in-a-firefox-extension
  1074. xhr.responseType = 'arraybuffer';
  1075. }
  1076. if ('POST' == this.method) {
  1077. try {
  1078. if (this.isBinary) {
  1079. xhr.setRequestHeader('Content-type', 'application/octet-stream');
  1080. } else {
  1081. xhr.setRequestHeader('Content-type', 'text/plain;charset=UTF-8');
  1082. }
  1083. } catch (e) {}
  1084. }
  1085. // ie6 check
  1086. if ('withCredentials' in xhr) {
  1087. xhr.withCredentials = true;
  1088. }
  1089. if (this.hasXDR()) {
  1090. xhr.onload = function(){
  1091. self.onLoad();
  1092. };
  1093. xhr.onerror = function(){
  1094. self.onError(xhr.responseText);
  1095. };
  1096. } else {
  1097. xhr.onreadystatechange = function(){
  1098. if (4 != xhr.readyState) return;
  1099. if (200 == xhr.status || 1223 == xhr.status) {
  1100. self.onLoad();
  1101. } else {
  1102. // make sure the `error` event handler that's user-set
  1103. // does not throw in the same tick and gets caught here
  1104. setTimeout(function(){
  1105. self.onError(xhr.status);
  1106. }, 0);
  1107. }
  1108. };
  1109. }
  1110. debug('xhr data %s', this.data);
  1111. xhr.send(this.data);
  1112. } catch (e) {
  1113. // Need to defer since .create() is called directly fhrom the constructor
  1114. // and thus the 'error' event can only be only bound *after* this exception
  1115. // occurs. Therefore, also, we cannot throw here at all.
  1116. setTimeout(function() {
  1117. self.onError(e);
  1118. }, 0);
  1119. return;
  1120. }
  1121. if (global.document) {
  1122. this.index = Request.requestsCount++;
  1123. Request.requests[this.index] = this;
  1124. }
  1125. };
  1126. /**
  1127. * Called upon successful response.
  1128. *
  1129. * @api private
  1130. */
  1131. Request.prototype.onSuccess = function(){
  1132. this.emit('success');
  1133. this.cleanup();
  1134. };
  1135. /**
  1136. * Called if we have data.
  1137. *
  1138. * @api private
  1139. */
  1140. Request.prototype.onData = function(data){
  1141. this.emit('data', data);
  1142. this.onSuccess();
  1143. };
  1144. /**
  1145. * Called upon error.
  1146. *
  1147. * @api private
  1148. */
  1149. Request.prototype.onError = function(err){
  1150. this.emit('error', err);
  1151. this.cleanup();
  1152. };
  1153. /**
  1154. * Cleans up house.
  1155. *
  1156. * @api private
  1157. */
  1158. Request.prototype.cleanup = function(){
  1159. if ('undefined' == typeof this.xhr || null === this.xhr) {
  1160. return;
  1161. }
  1162. // xmlhttprequest
  1163. if (this.hasXDR()) {
  1164. this.xhr.onload = this.xhr.onerror = empty;
  1165. } else {
  1166. this.xhr.onreadystatechange = empty;
  1167. }
  1168. try {
  1169. this.xhr.abort();
  1170. } catch(e) {}
  1171. if (global.document) {
  1172. delete Request.requests[this.index];
  1173. }
  1174. this.xhr = null;
  1175. };
  1176. /**
  1177. * Called upon load.
  1178. *
  1179. * @api private
  1180. */
  1181. Request.prototype.onLoad = function(){
  1182. var data;
  1183. try {
  1184. var contentType;
  1185. try {
  1186. contentType = this.xhr.getResponseHeader('Content-Type').split(';')[0];
  1187. } catch (e) {}
  1188. if (contentType === 'application/octet-stream') {
  1189. data = this.xhr.response;
  1190. } else {
  1191. if (!this.supportsBinary) {
  1192. data = this.xhr.responseText;
  1193. } else {
  1194. data = 'ok';
  1195. }
  1196. }
  1197. } catch (e) {
  1198. this.onError(e);
  1199. }
  1200. if (null != data) {
  1201. this.onData(data);
  1202. }
  1203. };
  1204. /**
  1205. * Check if it has XDomainRequest.
  1206. *
  1207. * @api private
  1208. */
  1209. Request.prototype.hasXDR = function(){
  1210. return 'undefined' !== typeof global.XDomainRequest && !this.xs && this.enablesXDR;
  1211. };
  1212. /**
  1213. * Aborts the request.
  1214. *
  1215. * @api public
  1216. */
  1217. Request.prototype.abort = function(){
  1218. this.cleanup();
  1219. };
  1220. /**
  1221. * Aborts pending requests when unloading the window. This is needed to prevent
  1222. * memory leaks (e.g. when using IE) and to ensure that no spurious error is
  1223. * emitted.
  1224. */
  1225. if (global.document) {
  1226. Request.requestsCount = 0;
  1227. Request.requests = {};
  1228. if (global.attachEvent) {
  1229. global.attachEvent('onunload', unloadHandler);
  1230. } else if (global.addEventListener) {
  1231. global.addEventListener('beforeunload', unloadHandler, false);
  1232. }
  1233. }
  1234. function unloadHandler() {
  1235. for (var i in Request.requests) {
  1236. if (Request.requests.hasOwnProperty(i)) {
  1237. Request.requests[i].abort();
  1238. }
  1239. }
  1240. }
  1241. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  1242. },{"./polling":8,"component-emitter":12,"component-inherit":13,"debug":14,"xmlhttprequest":10}],8:[function(_dereq_,module,exports){
  1243. /**
  1244. * Module dependencies.
  1245. */
  1246. var Transport = _dereq_('../transport');
  1247. var parseqs = _dereq_('parseqs');
  1248. var parser = _dereq_('engine.io-parser');
  1249. var inherit = _dereq_('component-inherit');
  1250. var debug = _dereq_('debug')('engine.io-client:polling');
  1251. /**
  1252. * Module exports.
  1253. */
  1254. module.exports = Polling;
  1255. /**
  1256. * Is XHR2 supported?
  1257. */
  1258. var hasXHR2 = (function() {
  1259. var XMLHttpRequest = _dereq_('xmlhttprequest');
  1260. var xhr = new XMLHttpRequest({ xdomain: false });
  1261. return null != xhr.responseType;
  1262. })();
  1263. /**
  1264. * Polling interface.
  1265. *
  1266. * @param {Object} opts
  1267. * @api private
  1268. */
  1269. function Polling(opts){
  1270. var forceBase64 = (opts && opts.forceBase64);
  1271. if (!hasXHR2 || forceBase64) {
  1272. this.supportsBinary = false;
  1273. }
  1274. Transport.call(this, opts);
  1275. }
  1276. /**
  1277. * Inherits from Transport.
  1278. */
  1279. inherit(Polling, Transport);
  1280. /**
  1281. * Transport name.
  1282. */
  1283. Polling.prototype.name = 'polling';
  1284. /**
  1285. * Opens the socket (triggers polling). We write a PING message to determine
  1286. * when the transport is open.
  1287. *
  1288. * @api private
  1289. */
  1290. Polling.prototype.doOpen = function(){
  1291. this.poll();
  1292. };
  1293. /**
  1294. * Pauses polling.
  1295. *
  1296. * @param {Function} callback upon buffers are flushed and transport is paused
  1297. * @api private
  1298. */
  1299. Polling.prototype.pause = function(onPause){
  1300. var pending = 0;
  1301. var self = this;
  1302. this.readyState = 'pausing';
  1303. function pause(){
  1304. debug('paused');
  1305. self.readyState = 'paused';
  1306. onPause();
  1307. }
  1308. if (this.polling || !this.writable) {
  1309. var total = 0;
  1310. if (this.polling) {
  1311. debug('we are currently polling - waiting to pause');
  1312. total++;
  1313. this.once('pollComplete', function(){
  1314. debug('pre-pause polling complete');
  1315. --total || pause();
  1316. });
  1317. }
  1318. if (!this.writable) {
  1319. debug('we are currently writing - waiting to pause');
  1320. total++;
  1321. this.once('drain', function(){
  1322. debug('pre-pause writing complete');
  1323. --total || pause();
  1324. });
  1325. }
  1326. } else {
  1327. pause();
  1328. }
  1329. };
  1330. /**
  1331. * Starts polling cycle.
  1332. *
  1333. * @api public
  1334. */
  1335. Polling.prototype.poll = function(){
  1336. debug('polling');
  1337. this.polling = true;
  1338. this.doPoll();
  1339. this.emit('poll');
  1340. };
  1341. /**
  1342. * Overloads onData to detect payloads.
  1343. *
  1344. * @api private
  1345. */
  1346. Polling.prototype.onData = function(data){
  1347. var self = this;
  1348. debug('polling got data %s', data);
  1349. var callback = function(packet, index, total) {
  1350. // if its the first message we consider the transport open
  1351. if ('opening' == self.readyState) {
  1352. self.onOpen();
  1353. }
  1354. // if its a close packet, we close the ongoing requests
  1355. if ('close' == packet.type) {
  1356. self.onClose();
  1357. return false;
  1358. }
  1359. // otherwise bypass onData and handle the message
  1360. self.onPacket(packet);
  1361. };
  1362. // decode payload
  1363. parser.decodePayload(data, this.socket.binaryType, callback);
  1364. // if an event did not trigger closing
  1365. if ('closed' != this.readyState) {
  1366. // if we got data we're not polling
  1367. this.polling = false;
  1368. this.emit('pollComplete');
  1369. if ('open' == this.readyState) {
  1370. this.poll();
  1371. } else {
  1372. debug('ignoring poll - transport state "%s"', this.readyState);
  1373. }
  1374. }
  1375. };
  1376. /**
  1377. * For polling, send a close packet.
  1378. *
  1379. * @api private
  1380. */
  1381. Polling.prototype.doClose = function(){
  1382. var self = this;
  1383. function close(){
  1384. debug('writing close packet');
  1385. self.write([{ type: 'close' }]);
  1386. }
  1387. if ('open' == this.readyState) {
  1388. debug('transport open - closing');
  1389. close();
  1390. } else {
  1391. // in case we're trying to close while
  1392. // handshaking is in progress (GH-164)
  1393. debug('transport not open - deferring close');
  1394. this.once('open', close);
  1395. }
  1396. };
  1397. /**
  1398. * Writes a packets payload.
  1399. *
  1400. * @param {Array} data packets
  1401. * @param {Function} drain callback
  1402. * @api private
  1403. */
  1404. Polling.prototype.write = function(packets){
  1405. var self = this;
  1406. this.writable = false;
  1407. var callbackfn = function() {
  1408. self.writable = true;
  1409. self.emit('drain');
  1410. };
  1411. var self = this;
  1412. parser.encodePayload(packets, this.supportsBinary, function(data) {
  1413. self.doWrite(data, callbackfn);
  1414. });
  1415. };
  1416. /**
  1417. * Generates uri for connection.
  1418. *
  1419. * @api private
  1420. */
  1421. Polling.prototype.uri = function(){
  1422. var query = this.query || {};
  1423. var schema = this.secure ? 'https' : 'http';
  1424. var port = '';
  1425. // cache busting is forced
  1426. if (false !== this.timestampRequests) {
  1427. query[this.timestampParam] = +new Date + '-' + Transport.timestamps++;
  1428. }
  1429. if (!this.supportsBinary && !query.sid) {
  1430. query.b64 = 1;
  1431. }
  1432. query = parseqs.encode(query);
  1433. // avoid port if default for schema
  1434. if (this.port && (('https' == schema && this.port != 443) ||
  1435. ('http' == schema && this.port != 80))) {
  1436. port = ':' + this.port;
  1437. }
  1438. // prepend ? to query
  1439. if (query.length) {
  1440. query = '?' + query;
  1441. }
  1442. return schema + '://' + this.hostname + port + this.path + query;
  1443. };
  1444. },{"../transport":4,"component-inherit":13,"debug":14,"engine.io-parser":17,"parseqs":27,"xmlhttprequest":10}],9:[function(_dereq_,module,exports){
  1445. /**
  1446. * Module dependencies.
  1447. */
  1448. var Transport = _dereq_('../transport');
  1449. var parser = _dereq_('engine.io-parser');
  1450. var parseqs = _dereq_('parseqs');
  1451. var inherit = _dereq_('component-inherit');
  1452. var debug = _dereq_('debug')('engine.io-client:websocket');
  1453. /**
  1454. * `ws` exposes a WebSocket-compatible interface in
  1455. * Node, or the `WebSocket` or `MozWebSocket` globals
  1456. * in the browser.
  1457. */
  1458. var WebSocket = _dereq_('ws');
  1459. /**
  1460. * Module exports.
  1461. */
  1462. module.exports = WS;
  1463. /**
  1464. * WebSocket transport constructor.
  1465. *
  1466. * @api {Object} connection options
  1467. * @api public
  1468. */
  1469. function WS(opts){
  1470. var forceBase64 = (opts && opts.forceBase64);
  1471. if (forceBase64) {
  1472. this.supportsBinary = false;
  1473. }
  1474. Transport.call(this, opts);
  1475. }
  1476. /**
  1477. * Inherits from Transport.
  1478. */
  1479. inherit(WS, Transport);
  1480. /**
  1481. * Transport name.
  1482. *
  1483. * @api public
  1484. */
  1485. WS.prototype.name = 'websocket';
  1486. /*
  1487. * WebSockets support binary
  1488. */
  1489. WS.prototype.supportsBinary = true;
  1490. /**
  1491. * Opens socket.
  1492. *
  1493. * @api private
  1494. */
  1495. WS.prototype.doOpen = function(){
  1496. if (!this.check()) {
  1497. // let probe timeout
  1498. return;
  1499. }
  1500. var self = this;
  1501. var uri = this.uri();
  1502. var protocols = void(0);
  1503. var opts = { agent: this.agent };
  1504. this.ws = new WebSocket(uri, protocols, opts);
  1505. if (this.ws.binaryType === undefined) {
  1506. this.supportsBinary = false;
  1507. }
  1508. this.ws.binaryType = 'arraybuffer';
  1509. this.addEventListeners();
  1510. };
  1511. /**
  1512. * Adds event listeners to the socket
  1513. *
  1514. * @api private
  1515. */
  1516. WS.prototype.addEventListeners = function(){
  1517. var self = this;
  1518. this.ws.onopen = function(){
  1519. self.onOpen();
  1520. };
  1521. this.ws.onclose = function(){
  1522. self.onClose();
  1523. };
  1524. this.ws.onmessage = function(ev){
  1525. self.onData(ev.data);
  1526. };
  1527. this.ws.onerror = function(e){
  1528. self.onError('websocket error', e);
  1529. };
  1530. };
  1531. /**
  1532. * Override `onData` to use a timer on iOS.
  1533. * See: https://gist.github.com/mloughran/2052006
  1534. *
  1535. * @api private
  1536. */
  1537. if ('undefined' != typeof navigator
  1538. && /iPad|iPhone|iPod/i.test(navigator.userAgent)) {
  1539. WS.prototype.onData = function(data){
  1540. var self = this;
  1541. setTimeout(function(){
  1542. Transport.prototype.onData.call(self, data);
  1543. }, 0);
  1544. };
  1545. }
  1546. /**
  1547. * Writes data to socket.
  1548. *
  1549. * @param {Array} array of packets.
  1550. * @api private
  1551. */
  1552. WS.prototype.write = function(packets){
  1553. var self = this;
  1554. this.writable = false;
  1555. // encodePacket efficient as it uses WS framing
  1556. // no need for encodePayload
  1557. for (var i = 0, l = packets.length; i < l; i++) {
  1558. parser.encodePacket(packets[i], this.supportsBinary, function(data) {
  1559. //Sometimes the websocket has already been closed but the browser didn't
  1560. //have a chance of informing us about it yet, in that case send will
  1561. //throw an error
  1562. try {
  1563. self.ws.send(data);
  1564. } catch (e){
  1565. debug('websocket closed before onclose event');
  1566. }
  1567. });
  1568. }
  1569. function ondrain() {
  1570. self.writable = true;
  1571. self.emit('drain');
  1572. }
  1573. // fake drain
  1574. // defer to next tick to allow Socket to clear writeBuffer
  1575. setTimeout(ondrain, 0);
  1576. };
  1577. /**
  1578. * Called upon close
  1579. *
  1580. * @api private
  1581. */
  1582. WS.prototype.onClose = function(){
  1583. Transport.prototype.onClose.call(this);
  1584. };
  1585. /**
  1586. * Closes socket.
  1587. *
  1588. * @api private
  1589. */
  1590. WS.prototype.doClose = function(){
  1591. if (typeof this.ws !== 'undefined') {
  1592. this.ws.close();
  1593. }
  1594. };
  1595. /**
  1596. * Generates uri for connection.
  1597. *
  1598. * @api private
  1599. */
  1600. WS.prototype.uri = function(){
  1601. var query = this.query || {};
  1602. var schema = this.secure ? 'wss' : 'ws';
  1603. var port = '';
  1604. // avoid port if default for schema
  1605. if (this.port && (('wss' == schema && this.port != 443)
  1606. || ('ws' == schema && this.port != 80))) {
  1607. port = ':' + this.port;
  1608. }
  1609. // append timestamp to URI
  1610. if (this.timestampRequests) {
  1611. query[this.timestampParam] = +new Date;
  1612. }
  1613. // communicate binary support capabilities
  1614. if (!this.supportsBinary) {
  1615. query.b64 = 1;
  1616. }
  1617. query = parseqs.encode(query);
  1618. // prepend ? to query
  1619. if (query.length) {
  1620. query = '?' + query;
  1621. }
  1622. return schema + '://' + this.hostname + port + this.path + query;
  1623. };
  1624. /**
  1625. * Feature detection for WebSocket.
  1626. *
  1627. * @return {Boolean} whether this transport is available.
  1628. * @api public
  1629. */
  1630. WS.prototype.check = function(){
  1631. return !!WebSocket && !('__initialize' in WebSocket && this.name === WS.prototype.name);
  1632. };
  1633. },{"../transport":4,"component-inherit":13,"debug":14,"engine.io-parser":17,"parseqs":27,"ws":29}],10:[function(_dereq_,module,exports){
  1634. // browser shim for xmlhttprequest module
  1635. var hasCORS = _dereq_('has-cors');
  1636. module.exports = function(opts) {
  1637. var xdomain = opts.xdomain;
  1638. // scheme must be same when usign XDomainRequest
  1639. // http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
  1640. var xscheme = opts.xscheme;
  1641. // XDomainRequest has a flow of not sending cookie, therefore it should be disabled as a default.
  1642. // https://github.com/Automattic/engine.io-client/pull/217
  1643. var enablesXDR = opts.enablesXDR;
  1644. // XMLHttpRequest can be disabled on IE
  1645. try {
  1646. if ('undefined' != typeof XMLHttpRequest && (!xdomain || hasCORS)) {
  1647. return new XMLHttpRequest();
  1648. }
  1649. } catch (e) { }
  1650. // Use XDomainRequest for IE8 if enablesXDR is true
  1651. // because loading bar keeps flashing when using jsonp-polling
  1652. // https://github.com/yujiosaka/socke.io-ie8-loading-example
  1653. try {
  1654. if ('undefined' != typeof XDomainRequest && !xscheme && enablesXDR) {
  1655. return new XDomainRequest();
  1656. }
  1657. } catch (e) { }
  1658. if (!xdomain) {
  1659. try {
  1660. return new ActiveXObject('Microsoft.XMLHTTP');
  1661. } catch(e) { }
  1662. }
  1663. }
  1664. },{"has-cors":23}],11:[function(_dereq_,module,exports){
  1665. (function (global){
  1666. /**
  1667. * Create a blob builder even when vendor prefixes exist
  1668. */
  1669. var BlobBuilder = global.BlobBuilder
  1670. || global.WebKitBlobBuilder
  1671. || global.MSBlobBuilder
  1672. || global.MozBlobBuilder;
  1673. /**
  1674. * Check if Blob constructor is supported
  1675. */
  1676. var blobSupported = (function() {
  1677. try {
  1678. var b = new Blob(['hi']);
  1679. return b.size == 2;
  1680. } catch(e) {
  1681. return false;
  1682. }
  1683. })();
  1684. /**
  1685. * Check if BlobBuilder is supported
  1686. */
  1687. var blobBuilderSupported = BlobBuilder
  1688. && BlobBuilder.prototype.append
  1689. && BlobBuilder.prototype.getBlob;
  1690. function BlobBuilderConstructor(ary, options) {
  1691. options = options || {};
  1692. var bb = new BlobBuilder();
  1693. for (var i = 0; i < ary.length; i++) {
  1694. bb.append(ary[i]);
  1695. }
  1696. return (options.type) ? bb.getBlob(options.type) : bb.getBlob();
  1697. };
  1698. module.exports = (function() {
  1699. if (blobSupported) {
  1700. return global.Blob;
  1701. } else if (blobBuilderSupported) {
  1702. return BlobBuilderConstructor;
  1703. } else {
  1704. return undefined;
  1705. }
  1706. })();
  1707. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  1708. },{}],12:[function(_dereq_,module,exports){
  1709. /**
  1710. * Expose `Emitter`.
  1711. */
  1712. module.exports = Emitter;
  1713. /**
  1714. * Initialize a new `Emitter`.
  1715. *
  1716. * @api public
  1717. */
  1718. function Emitter(obj) {
  1719. if (obj) return mixin(obj);
  1720. };
  1721. /**
  1722. * Mixin the emitter properties.
  1723. *
  1724. * @param {Object} obj
  1725. * @return {Object}
  1726. * @api private
  1727. */
  1728. function mixin(obj) {
  1729. for (var key in Emitter.prototype) {
  1730. obj[key] = Emitter.prototype[key];
  1731. }
  1732. return obj;
  1733. }
  1734. /**
  1735. * Listen on the given `event` with `fn`.
  1736. *
  1737. * @param {String} event
  1738. * @param {Function} fn
  1739. * @return {Emitter}
  1740. * @api public
  1741. */
  1742. Emitter.prototype.on =
  1743. Emitter.prototype.addEventListener = function(event, fn){
  1744. this._callbacks = this._callbacks || {};
  1745. (this._callbacks[event] = this._callbacks[event] || [])
  1746. .push(fn);
  1747. return this;
  1748. };
  1749. /**
  1750. * Adds an `event` listener that will be invoked a single
  1751. * time then automatically removed.
  1752. *
  1753. * @param {String} event
  1754. * @param {Function} fn
  1755. * @return {Emitter}
  1756. * @api public
  1757. */
  1758. Emitter.prototype.once = function(event, fn){
  1759. var self = this;
  1760. this._callbacks = this._callbacks || {};
  1761. function on() {
  1762. self.off(event, on);
  1763. fn.apply(this, arguments);
  1764. }
  1765. on.fn = fn;
  1766. this.on(event, on);
  1767. return this;
  1768. };
  1769. /**
  1770. * Remove the given callback for `event` or all
  1771. * registered callbacks.
  1772. *
  1773. * @param {String} event
  1774. * @param {Function} fn
  1775. * @return {Emitter}
  1776. * @api public
  1777. */
  1778. Emitter.prototype.off =
  1779. Emitter.prototype.removeListener =
  1780. Emitter.prototype.removeAllListeners =
  1781. Emitter.prototype.removeEventListener = function(event, fn){
  1782. this._callbacks = this._callbacks || {};
  1783. // all
  1784. if (0 == arguments.length) {
  1785. this._callbacks = {};
  1786. return this;
  1787. }
  1788. // specific event
  1789. var callbacks = this._callbacks[event];
  1790. if (!callbacks) return this;
  1791. // remove all handlers
  1792. if (1 == arguments.length) {
  1793. delete this._callbacks[event];
  1794. return this;
  1795. }
  1796. // remove specific handler
  1797. var cb;
  1798. for (var i = 0; i < callbacks.length; i++) {
  1799. cb = callbacks[i];
  1800. if (cb === fn || cb.fn === fn) {
  1801. callbacks.splice(i, 1);
  1802. break;
  1803. }
  1804. }
  1805. return this;
  1806. };
  1807. /**
  1808. * Emit `event` with the given args.
  1809. *
  1810. * @param {String} event
  1811. * @param {Mixed} ...
  1812. * @return {Emitter}
  1813. */
  1814. Emitter.prototype.emit = function(event){
  1815. this._callbacks = this._callbacks || {};
  1816. var args = [].slice.call(arguments, 1)
  1817. , callbacks = this._callbacks[event];
  1818. if (callbacks) {
  1819. callbacks = callbacks.slice(0);
  1820. for (var i = 0, len = callbacks.length; i < len; ++i) {
  1821. callbacks[i].apply(this, args);
  1822. }
  1823. }
  1824. return this;
  1825. };
  1826. /**
  1827. * Return array of callbacks for `event`.
  1828. *
  1829. * @param {String} event
  1830. * @return {Array}
  1831. * @api public
  1832. */
  1833. Emitter.prototype.listeners = function(event){
  1834. this._callbacks = this._callbacks || {};
  1835. return this._callbacks[event] || [];
  1836. };
  1837. /**
  1838. * Check if this emitter has `event` handlers.
  1839. *
  1840. * @param {String} event
  1841. * @return {Boolean}
  1842. * @api public
  1843. */
  1844. Emitter.prototype.hasListeners = function(event){
  1845. return !! this.listeners(event).length;
  1846. };
  1847. },{}],13:[function(_dereq_,module,exports){
  1848. module.exports = function(a, b){
  1849. var fn = function(){};
  1850. fn.prototype = b.prototype;
  1851. a.prototype = new fn;
  1852. a.prototype.constructor = a;
  1853. };
  1854. },{}],14:[function(_dereq_,module,exports){
  1855. /**
  1856. * This is the web browser implementation of `debug()`.
  1857. *
  1858. * Expose `debug()` as the module.
  1859. */
  1860. exports = module.exports = _dereq_('./debug');
  1861. exports.log = log;
  1862. exports.formatArgs = formatArgs;
  1863. exports.save = save;
  1864. exports.load = load;
  1865. exports.useColors = useColors;
  1866. /**
  1867. * Colors.
  1868. */
  1869. exports.colors = [
  1870. 'lightseagreen',
  1871. 'forestgreen',
  1872. 'goldenrod',
  1873. 'dodgerblue',
  1874. 'darkorchid',
  1875. 'crimson'
  1876. ];
  1877. /**
  1878. * Currently only WebKit-based Web Inspectors, Firefox >= v31,
  1879. * and the Firebug extension (any Firefox version) are known
  1880. * to support "%c" CSS customizations.
  1881. *
  1882. * TODO: add a `localStorage` variable to explicitly enable/disable colors
  1883. */
  1884. function useColors() {
  1885. // is webkit? http://stackoverflow.com/a/16459606/376773
  1886. return ('WebkitAppearance' in document.documentElement.style) ||
  1887. // is firebug? http://stackoverflow.com/a/398120/376773
  1888. (window.console && (console.firebug || (console.exception && console.table))) ||
  1889. // is firefox >= v31?
  1890. // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
  1891. (navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31);
  1892. }
  1893. /**
  1894. * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
  1895. */
  1896. exports.formatters.j = function(v) {
  1897. return JSON.stringify(v);
  1898. };
  1899. /**
  1900. * Colorize log arguments if enabled.
  1901. *
  1902. * @api public
  1903. */
  1904. function formatArgs() {
  1905. var args = arguments;
  1906. var useColors = this.useColors;
  1907. args[0] = (useColors ? '%c' : '')
  1908. + this.namespace
  1909. + (useColors ? ' %c' : ' ')
  1910. + args[0]
  1911. + (useColors ? '%c ' : ' ')
  1912. + '+' + exports.humanize(this.diff);
  1913. if (!useColors) return args;
  1914. var c = 'color: ' + this.color;
  1915. args = [args[0], c, 'color: inherit'].concat(Array.prototype.slice.call(args, 1));
  1916. // the final "%c" is somewhat tricky, because there could be other
  1917. // arguments passed either before or after the %c, so we need to
  1918. // figure out the correct index to insert the CSS into
  1919. var index = 0;
  1920. var lastC = 0;
  1921. args[0].replace(/%[a-z%]/g, function(match) {
  1922. if ('%' === match) return;
  1923. index++;
  1924. if ('%c' === match) {
  1925. // we only are interested in the *last* %c
  1926. // (the user may have provided their own)
  1927. lastC = index;
  1928. }
  1929. });
  1930. args.splice(lastC, 0, c);
  1931. return args;
  1932. }
  1933. /**
  1934. * Invokes `console.log()` when available.
  1935. * No-op when `console.log` is not a "function".
  1936. *
  1937. * @api public
  1938. */
  1939. function log() {
  1940. // This hackery is required for IE8,
  1941. // where the `console.log` function doesn't have 'apply'
  1942. return 'object' == typeof console
  1943. && 'function' == typeof console.log
  1944. && Function.prototype.apply.call(console.log, console, arguments);
  1945. }
  1946. /**
  1947. * Save `namespaces`.
  1948. *
  1949. * @param {String} namespaces
  1950. * @api private
  1951. */
  1952. function save(namespaces) {
  1953. try {
  1954. if (null == namespaces) {
  1955. localStorage.removeItem('debug');
  1956. } else {
  1957. localStorage.debug = namespaces;
  1958. }
  1959. } catch(e) {}
  1960. }
  1961. /**
  1962. * Load `namespaces`.
  1963. *
  1964. * @return {String} returns the previously persisted debug modes
  1965. * @api private
  1966. */
  1967. function load() {
  1968. var r;
  1969. try {
  1970. r = localStorage.debug;
  1971. } catch(e) {}
  1972. return r;
  1973. }
  1974. /**
  1975. * Enable namespaces listed in `localStorage.debug` initially.
  1976. */
  1977. exports.enable(load());
  1978. },{"./debug":15}],15:[function(_dereq_,module,exports){
  1979. /**
  1980. * This is the common logic for both the Node.js and web browser
  1981. * implementations of `debug()`.
  1982. *
  1983. * Expose `debug()` as the module.
  1984. */
  1985. exports = module.exports = debug;
  1986. exports.coerce = coerce;
  1987. exports.disable = disable;
  1988. exports.enable = enable;
  1989. exports.enabled = enabled;
  1990. exports.humanize = _dereq_('ms');
  1991. /**
  1992. * The currently active debug mode names, and names to skip.
  1993. */
  1994. exports.names = [];
  1995. exports.skips = [];
  1996. /**
  1997. * Map of special "%n" handling functions, for the debug "format" argument.
  1998. *
  1999. * Valid key names are a single, lowercased letter, i.e. "n".
  2000. */
  2001. exports.formatters = {};
  2002. /**
  2003. * Previously assigned color.
  2004. */
  2005. var prevColor = 0;
  2006. /**
  2007. * Previous log timestamp.
  2008. */
  2009. var prevTime;
  2010. /**
  2011. * Select a color.
  2012. *
  2013. * @return {Number}
  2014. * @api private
  2015. */
  2016. function selectColor() {
  2017. return exports.colors[prevColor++ % exports.colors.length];
  2018. }
  2019. /**
  2020. * Create a debugger with the given `namespace`.
  2021. *
  2022. * @param {String} namespace
  2023. * @return {Function}
  2024. * @api public
  2025. */
  2026. function debug(namespace) {
  2027. // define the `disabled` version
  2028. function disabled() {
  2029. }
  2030. disabled.enabled = false;
  2031. // define the `enabled` version
  2032. function enabled() {
  2033. var self = enabled;
  2034. // set `diff` timestamp
  2035. var curr = +new Date();
  2036. var ms = curr - (prevTime || curr);
  2037. self.diff = ms;
  2038. self.prev = prevTime;
  2039. self.curr = curr;
  2040. prevTime = curr;
  2041. // add the `color` if not set
  2042. if (null == self.useColors) self.useColors = exports.useColors();
  2043. if (null == self.color && self.useColors) self.color = selectColor();
  2044. var args = Array.prototype.slice.call(arguments);
  2045. args[0] = exports.coerce(args[0]);
  2046. if ('string' !== typeof args[0]) {
  2047. // anything else let's inspect with %o
  2048. args = ['%o'].concat(args);
  2049. }
  2050. // apply any `formatters` transformations
  2051. var index = 0;
  2052. args[0] = args[0].replace(/%([a-z%])/g, function(match, format) {
  2053. // if we encounter an escaped % then don't increase the array index
  2054. if (match === '%') return match;
  2055. index++;
  2056. var formatter = exports.formatters[format];
  2057. if ('function' === typeof formatter) {
  2058. var val = args[index];
  2059. match = formatter.call(self, val);
  2060. // now we need to remove `args[index]` since it's inlined in the `format`
  2061. args.splice(index, 1);
  2062. index--;
  2063. }
  2064. return match;
  2065. });
  2066. if ('function' === typeof exports.formatArgs) {
  2067. args = exports.formatArgs.apply(self, args);
  2068. }
  2069. var logFn = enabled.log || exports.log || console.log.bind(console);
  2070. logFn.apply(self, args);
  2071. }
  2072. enabled.enabled = true;
  2073. var fn = exports.enabled(namespace) ? enabled : disabled;
  2074. fn.namespace = namespace;
  2075. return fn;
  2076. }
  2077. /**
  2078. * Enables a debug mode by namespaces. This can include modes
  2079. * separated by a colon and wildcards.
  2080. *
  2081. * @param {String} namespaces
  2082. * @api public
  2083. */
  2084. function enable(namespaces) {
  2085. exports.save(namespaces);
  2086. var split = (namespaces || '').split(/[\s,]+/);
  2087. var len = split.length;
  2088. for (var i = 0; i < len; i++) {
  2089. if (!split[i]) continue; // ignore empty strings
  2090. namespaces = split[i].replace(/\*/g, '.*?');
  2091. if (namespaces[0] === '-') {
  2092. exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
  2093. } else {
  2094. exports.names.push(new RegExp('^' + namespaces + '$'));
  2095. }
  2096. }
  2097. }
  2098. /**
  2099. * Disable debug output.
  2100. *
  2101. * @api public
  2102. */
  2103. function disable() {
  2104. exports.enable('');
  2105. }
  2106. /**
  2107. * Returns true if the given mode name is enabled, false otherwise.
  2108. *
  2109. * @param {String} name
  2110. * @return {Boolean}
  2111. * @api public
  2112. */
  2113. function enabled(name) {
  2114. var i, len;
  2115. for (i = 0, len = exports.skips.length; i < len; i++) {
  2116. if (exports.skips[i].test(name)) {
  2117. return false;
  2118. }
  2119. }
  2120. for (i = 0, len = exports.names.length; i < len; i++) {
  2121. if (exports.names[i].test(name)) {
  2122. return true;
  2123. }
  2124. }
  2125. return false;
  2126. }
  2127. /**
  2128. * Coerce `val`.
  2129. *
  2130. * @param {Mixed} val
  2131. * @return {Mixed}
  2132. * @api private
  2133. */
  2134. function coerce(val) {
  2135. if (val instanceof Error) return val.stack || val.message;
  2136. return val;
  2137. }
  2138. },{"ms":16}],16:[function(_dereq_,module,exports){
  2139. /**
  2140. * Helpers.
  2141. */
  2142. var s = 1000;
  2143. var m = s * 60;
  2144. var h = m * 60;
  2145. var d = h * 24;
  2146. var y = d * 365.25;
  2147. /**
  2148. * Parse or format the given `val`.
  2149. *
  2150. * Options:
  2151. *
  2152. * - `long` verbose formatting [false]
  2153. *
  2154. * @param {String|Number} val
  2155. * @param {Object} options
  2156. * @return {String|Number}
  2157. * @api public
  2158. */
  2159. module.exports = function(val, options){
  2160. options = options || {};
  2161. if ('string' == typeof val) return parse(val);
  2162. return options.long
  2163. ? long(val)
  2164. : short(val);
  2165. };
  2166. /**
  2167. * Parse the given `str` and return milliseconds.
  2168. *
  2169. * @param {String} str
  2170. * @return {Number}
  2171. * @api private
  2172. */
  2173. function parse(str) {
  2174. var match = /^((?:\d+)?\.?\d+) *(ms|seconds?|s|minutes?|m|hours?|h|days?|d|years?|y)?$/i.exec(str);
  2175. if (!match) return;
  2176. var n = parseFloat(match[1]);
  2177. var type = (match[2] || 'ms').toLowerCase();
  2178. switch (type) {
  2179. case 'years':
  2180. case 'year':
  2181. case 'y':
  2182. return n * y;
  2183. case 'days':
  2184. case 'day':
  2185. case 'd':
  2186. return n * d;
  2187. case 'hours':
  2188. case 'hour':
  2189. case 'h':
  2190. return n * h;
  2191. case 'minutes':
  2192. case 'minute':
  2193. case 'm':
  2194. return n * m;
  2195. case 'seconds':
  2196. case 'second':
  2197. case 's':
  2198. return n * s;
  2199. case 'ms':
  2200. return n;
  2201. }
  2202. }
  2203. /**
  2204. * Short format for `ms`.
  2205. *
  2206. * @param {Number} ms
  2207. * @return {String}
  2208. * @api private
  2209. */
  2210. function short(ms) {
  2211. if (ms >= d) return Math.round(ms / d) + 'd';
  2212. if (ms >= h) return Math.round(ms / h) + 'h';
  2213. if (ms >= m) return Math.round(ms / m) + 'm';
  2214. if (ms >= s) return Math.round(ms / s) + 's';
  2215. return ms + 'ms';
  2216. }
  2217. /**
  2218. * Long format for `ms`.
  2219. *
  2220. * @param {Number} ms
  2221. * @return {String}
  2222. * @api private
  2223. */
  2224. function long(ms) {
  2225. return plural(ms, d, 'day')
  2226. || plural(ms, h, 'hour')
  2227. || plural(ms, m, 'minute')
  2228. || plural(ms, s, 'second')
  2229. || ms + ' ms';
  2230. }
  2231. /**
  2232. * Pluralization helper.
  2233. */
  2234. function plural(ms, n, name) {
  2235. if (ms < n) return;
  2236. if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name;
  2237. return Math.ceil(ms / n) + ' ' + name + 's';
  2238. }
  2239. },{}],17:[function(_dereq_,module,exports){
  2240. (function (global){
  2241. /**
  2242. * Module dependencies.
  2243. */
  2244. var keys = _dereq_('./keys');
  2245. var sliceBuffer = _dereq_('arraybuffer.slice');
  2246. var base64encoder = _dereq_('base64-arraybuffer');
  2247. var after = _dereq_('after');
  2248. var utf8 = _dereq_('utf8');
  2249. /**
  2250. * Check if we are running an android browser. That requires us to use
  2251. * ArrayBuffer with polling transports...
  2252. *
  2253. * http://ghinda.net/jpeg-blob-ajax-android/
  2254. */
  2255. var isAndroid = navigator.userAgent.match(/Android/i);
  2256. /**
  2257. * Current protocol version.
  2258. */
  2259. exports.protocol = 3;
  2260. /**
  2261. * Packet types.
  2262. */
  2263. var packets = exports.packets = {
  2264. open: 0 // non-ws
  2265. , close: 1 // non-ws
  2266. , ping: 2
  2267. , pong: 3
  2268. , message: 4
  2269. , upgrade: 5
  2270. , noop: 6
  2271. };
  2272. var packetslist = keys(packets);
  2273. /**
  2274. * Premade error packet.
  2275. */
  2276. var err = { type: 'error', data: 'parser error' };
  2277. /**
  2278. * Create a blob api even for blob builder when vendor prefixes exist
  2279. */
  2280. var Blob = _dereq_('blob');
  2281. /**
  2282. * Encodes a packet.
  2283. *
  2284. * <packet type id> [ <data> ]
  2285. *
  2286. * Example:
  2287. *
  2288. * 5hello world
  2289. * 3
  2290. * 4
  2291. *
  2292. * Binary is encoded in an identical principle
  2293. *
  2294. * @api private
  2295. */
  2296. exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {
  2297. if ('function' == typeof supportsBinary) {
  2298. callback = supportsBinary;
  2299. supportsBinary = false;
  2300. }
  2301. if ('function' == typeof utf8encode) {
  2302. callback = utf8encode;
  2303. utf8encode = null;
  2304. }
  2305. var data = (packet.data === undefined)
  2306. ? undefined
  2307. : packet.data.buffer || packet.data;
  2308. if (global.ArrayBuffer && data instanceof ArrayBuffer) {
  2309. return encodeArrayBuffer(packet, supportsBinary, callback);
  2310. } else if (Blob && data instanceof global.Blob) {
  2311. return encodeBlob(packet, supportsBinary, callback);
  2312. }
  2313. // Sending data as a utf-8 string
  2314. var encoded = packets[packet.type];
  2315. // data fragment is optional
  2316. if (undefined !== packet.data) {
  2317. encoded += utf8encode ? utf8.encode(String(packet.data)) : String(packet.data);
  2318. }
  2319. return callback('' + encoded);
  2320. };
  2321. /**
  2322. * Encode packet helpers for binary types
  2323. */
  2324. function encodeArrayBuffer(packet, supportsBinary, callback) {
  2325. if (!supportsBinary) {
  2326. return exports.encodeBase64Packet(packet, callback);
  2327. }
  2328. var data = packet.data;
  2329. var contentArray = new Uint8Array(data);
  2330. var resultBuffer = new Uint8Array(1 + data.byteLength);
  2331. resultBuffer[0] = packets[packet.type];
  2332. for (var i = 0; i < contentArray.length; i++) {
  2333. resultBuffer[i+1] = contentArray[i];
  2334. }
  2335. return callback(resultBuffer.buffer);
  2336. }
  2337. function encodeBlobAsArrayBuffer(packet, supportsBinary, callback) {
  2338. if (!supportsBinary) {
  2339. return exports.encodeBase64Packet(packet, callback);
  2340. }
  2341. var fr = new FileReader();
  2342. fr.onload = function() {
  2343. packet.data = fr.result;
  2344. exports.encodePacket(packet, supportsBinary, true, callback);
  2345. };
  2346. return fr.readAsArrayBuffer(packet.data);
  2347. }
  2348. function encodeBlob(packet, supportsBinary, callback) {
  2349. if (!supportsBinary) {
  2350. return exports.encodeBase64Packet(packet, callback);
  2351. }
  2352. if (isAndroid) {
  2353. return encodeBlobAsArrayBuffer(packet, supportsBinary, callback);
  2354. }
  2355. var length = new Uint8Array(1);
  2356. length[0] = packets[packet.type];
  2357. var blob = new Blob([length.buffer, packet.data]);
  2358. return callback(blob);
  2359. }
  2360. /**
  2361. * Encodes a packet with binary data in a base64 string
  2362. *
  2363. * @param {Object} packet, has `type` and `data`
  2364. * @return {String} base64 encoded message
  2365. */
  2366. exports.encodeBase64Packet = function(packet, callback) {
  2367. var message = 'b' + exports.packets[packet.type];
  2368. if (Blob && packet.data instanceof Blob) {
  2369. var fr = new FileReader();
  2370. fr.onload = function() {
  2371. var b64 = fr.result.split(',')[1];
  2372. callback(message + b64);
  2373. };
  2374. return fr.readAsDataURL(packet.data);
  2375. }
  2376. var b64data;
  2377. try {
  2378. b64data = String.fromCharCode.apply(null, new Uint8Array(packet.data));
  2379. } catch (e) {
  2380. // iPhone Safari doesn't let you apply with typed arrays
  2381. var typed = new Uint8Array(packet.data);
  2382. var basic = new Array(typed.length);
  2383. for (var i = 0; i < typed.length; i++) {
  2384. basic[i] = typed[i];
  2385. }
  2386. b64data = String.fromCharCode.apply(null, basic);
  2387. }
  2388. message += global.btoa(b64data);
  2389. return callback(message);
  2390. };
  2391. /**
  2392. * Decodes a packet. Changes format to Blob if requested.
  2393. *
  2394. * @return {Object} with `type` and `data` (if any)
  2395. * @api private
  2396. */
  2397. exports.decodePacket = function (data, binaryType, utf8decode) {
  2398. // String data
  2399. if (typeof data == 'string' || data === undefined) {
  2400. if (data.charAt(0) == 'b') {
  2401. return exports.decodeBase64Packet(data.substr(1), binaryType);
  2402. }
  2403. if (utf8decode) {
  2404. try {
  2405. data = utf8.decode(data);
  2406. } catch (e) {
  2407. return err;
  2408. }
  2409. }
  2410. var type = data.charAt(0);
  2411. if (Number(type) != type || !packetslist[type]) {
  2412. return err;
  2413. }
  2414. if (data.length > 1) {
  2415. return { type: packetslist[type], data: data.substring(1) };
  2416. } else {
  2417. return { type: packetslist[type] };
  2418. }
  2419. }
  2420. var asArray = new Uint8Array(data);
  2421. var type = asArray[0];
  2422. var rest = sliceBuffer(data, 1);
  2423. if (Blob && binaryType === 'blob') {
  2424. rest = new Blob([rest]);
  2425. }
  2426. return { type: packetslist[type], data: rest };
  2427. };
  2428. /**
  2429. * Decodes a packet encoded in a base64 string
  2430. *
  2431. * @param {String} base64 encoded message
  2432. * @return {Object} with `type` and `data` (if any)
  2433. */
  2434. exports.decodeBase64Packet = function(msg, binaryType) {
  2435. var type = packetslist[msg.charAt(0)];
  2436. if (!global.ArrayBuffer) {
  2437. return { type: type, data: { base64: true, data: msg.substr(1) } };
  2438. }
  2439. var data = base64encoder.decode(msg.substr(1));
  2440. if (binaryType === 'blob' && Blob) {
  2441. data = new Blob([data]);
  2442. }
  2443. return { type: type, data: data };
  2444. };
  2445. /**
  2446. * Encodes multiple messages (payload).
  2447. *
  2448. * <length>:data
  2449. *
  2450. * Example:
  2451. *
  2452. * 11:hello world2:hi
  2453. *
  2454. * If any contents are binary, they will be encoded as base64 strings. Base64
  2455. * encoded strings are marked with a b before the length specifier
  2456. *
  2457. * @param {Array} packets
  2458. * @api private
  2459. */
  2460. exports.encodePayload = function (packets, supportsBinary, callback) {
  2461. if (typeof supportsBinary == 'function') {
  2462. callback = supportsBinary;
  2463. supportsBinary = null;
  2464. }
  2465. if (supportsBinary) {
  2466. if (Blob && !isAndroid) {
  2467. return exports.encodePayloadAsBlob(packets, callback);
  2468. }
  2469. return exports.encodePayloadAsArrayBuffer(packets, callback);
  2470. }
  2471. if (!packets.length) {
  2472. return callback('0:');
  2473. }
  2474. function setLengthHeader(message) {
  2475. return message.length + ':' + message;
  2476. }
  2477. function encodeOne(packet, doneCallback) {
  2478. exports.encodePacket(packet, supportsBinary, true, function(message) {
  2479. doneCallback(null, setLengthHeader(message));
  2480. });
  2481. }
  2482. map(packets, encodeOne, function(err, results) {
  2483. return callback(results.join(''));
  2484. });
  2485. };
  2486. /**
  2487. * Async array map using after
  2488. */
  2489. function map(ary, each, done) {
  2490. var result = new Array(ary.length);
  2491. var next = after(ary.length, done);
  2492. var eachWithIndex = function(i, el, cb) {
  2493. each(el, function(error, msg) {
  2494. result[i] = msg;
  2495. cb(error, result);
  2496. });
  2497. };
  2498. for (var i = 0; i < ary.length; i++) {
  2499. eachWithIndex(i, ary[i], next);
  2500. }
  2501. }
  2502. /*
  2503. * Decodes data when a payload is maybe expected. Possible binary contents are
  2504. * decoded from their base64 representation
  2505. *
  2506. * @param {String} data, callback method
  2507. * @api public
  2508. */
  2509. exports.decodePayload = function (data, binaryType, callback) {
  2510. if (typeof data != 'string') {
  2511. return exports.decodePayloadAsBinary(data, binaryType, callback);
  2512. }
  2513. if (typeof binaryType === 'function') {
  2514. callback = binaryType;
  2515. binaryType = null;
  2516. }
  2517. var packet;
  2518. if (data == '') {
  2519. // parser error - ignoring payload
  2520. return callback(err, 0, 1);
  2521. }
  2522. var length = ''
  2523. , n, msg;
  2524. for (var i = 0, l = data.length; i < l; i++) {
  2525. var chr = data.charAt(i);
  2526. if (':' != chr) {
  2527. length += chr;
  2528. } else {
  2529. if ('' == length || (length != (n = Number(length)))) {
  2530. // parser error - ignoring payload
  2531. return callback(err, 0, 1);
  2532. }
  2533. msg = data.substr(i + 1, n);
  2534. if (length != msg.length) {
  2535. // parser error - ignoring payload
  2536. return callback(err, 0, 1);
  2537. }
  2538. if (msg.length) {
  2539. packet = exports.decodePacket(msg, binaryType, true);
  2540. if (err.type == packet.type && err.data == packet.data) {
  2541. // parser error in individual packet - ignoring payload
  2542. return callback(err, 0, 1);
  2543. }
  2544. var ret = callback(packet, i + n, l);
  2545. if (false === ret) return;
  2546. }
  2547. // advance cursor
  2548. i += n;
  2549. length = '';
  2550. }
  2551. }
  2552. if (length != '') {
  2553. // parser error - ignoring payload
  2554. return callback(err, 0, 1);
  2555. }
  2556. };
  2557. /**
  2558. * Encodes multiple messages (payload) as binary.
  2559. *
  2560. * <1 = binary, 0 = string><number from 0-9><number from 0-9>[...]<number
  2561. * 255><data>
  2562. *
  2563. * Example:
  2564. * 1 3 255 1 2 3, if the binary contents are interpreted as 8 bit integers
  2565. *
  2566. * @param {Array} packets
  2567. * @return {ArrayBuffer} encoded payload
  2568. * @api private
  2569. */
  2570. exports.encodePayloadAsArrayBuffer = function(packets, callback) {
  2571. if (!packets.length) {
  2572. return callback(new ArrayBuffer(0));
  2573. }
  2574. function encodeOne(packet, doneCallback) {
  2575. exports.encodePacket(packet, true, true, function(data) {
  2576. return doneCallback(null, data);
  2577. });
  2578. }
  2579. map(packets, encodeOne, function(err, encodedPackets) {
  2580. var totalLength = encodedPackets.reduce(function(acc, p) {
  2581. var len;
  2582. if (typeof p === 'string'){
  2583. len = p.length;
  2584. } else {
  2585. len = p.byteLength;
  2586. }
  2587. return acc + len.toString().length + len + 2; // string/binary identifier + separator = 2
  2588. }, 0);
  2589. var resultArray = new Uint8Array(totalLength);
  2590. var bufferIndex = 0;
  2591. encodedPackets.forEach(function(p) {
  2592. var isString = typeof p === 'string';
  2593. var ab = p;
  2594. if (isString) {
  2595. var view = new Uint8Array(p.length);
  2596. for (var i = 0; i < p.length; i++) {
  2597. view[i] = p.charCodeAt(i);
  2598. }
  2599. ab = view.buffer;
  2600. }
  2601. if (isString) { // not true binary
  2602. resultArray[bufferIndex++] = 0;
  2603. } else { // true binary
  2604. resultArray[bufferIndex++] = 1;
  2605. }
  2606. var lenStr = ab.byteLength.toString();
  2607. for (var i = 0; i < lenStr.length; i++) {
  2608. resultArray[bufferIndex++] = parseInt(lenStr[i]);
  2609. }
  2610. resultArray[bufferIndex++] = 255;
  2611. var view = new Uint8Array(ab);
  2612. for (var i = 0; i < view.length; i++) {
  2613. resultArray[bufferIndex++] = view[i];
  2614. }
  2615. });
  2616. return callback(resultArray.buffer);
  2617. });
  2618. };
  2619. /**
  2620. * Encode as Blob
  2621. */
  2622. exports.encodePayloadAsBlob = function(packets, callback) {
  2623. function encodeOne(packet, doneCallback) {
  2624. exports.encodePacket(packet, true, true, function(encoded) {
  2625. var binaryIdentifier = new Uint8Array(1);
  2626. binaryIdentifier[0] = 1;
  2627. if (typeof encoded === 'string') {
  2628. var view = new Uint8Array(encoded.length);
  2629. for (var i = 0; i < encoded.length; i++) {
  2630. view[i] = encoded.charCodeAt(i);
  2631. }
  2632. encoded = view.buffer;
  2633. binaryIdentifier[0] = 0;
  2634. }
  2635. var len = (encoded instanceof ArrayBuffer)
  2636. ? encoded.byteLength
  2637. : encoded.size;
  2638. var lenStr = len.toString();
  2639. var lengthAry = new Uint8Array(lenStr.length + 1);
  2640. for (var i = 0; i < lenStr.length; i++) {
  2641. lengthAry[i] = parseInt(lenStr[i]);
  2642. }
  2643. lengthAry[lenStr.length] = 255;
  2644. if (Blob) {
  2645. var blob = new Blob([binaryIdentifier.buffer, lengthAry.buffer, encoded]);
  2646. doneCallback(null, blob);
  2647. }
  2648. });
  2649. }
  2650. map(packets, encodeOne, function(err, results) {
  2651. return callback(new Blob(results));
  2652. });
  2653. };
  2654. /*
  2655. * Decodes data when a payload is maybe expected. Strings are decoded by
  2656. * interpreting each byte as a key code for entries marked to start with 0. See
  2657. * description of encodePayloadAsBinary
  2658. *
  2659. * @param {ArrayBuffer} data, callback method
  2660. * @api public
  2661. */
  2662. exports.decodePayloadAsBinary = function (data, binaryType, callback) {
  2663. if (typeof binaryType === 'function') {
  2664. callback = binaryType;
  2665. binaryType = null;
  2666. }
  2667. var bufferTail = data;
  2668. var buffers = [];
  2669. var numberTooLong = false;
  2670. while (bufferTail.byteLength > 0) {
  2671. var tailArray = new Uint8Array(bufferTail);
  2672. var isString = tailArray[0] === 0;
  2673. var msgLength = '';
  2674. for (var i = 1; ; i++) {
  2675. if (tailArray[i] == 255) break;
  2676. if (msgLength.length > 310) {
  2677. numberTooLong = true;
  2678. break;
  2679. }
  2680. msgLength += tailArray[i];
  2681. }
  2682. if(numberTooLong) return callback(err, 0, 1);
  2683. bufferTail = sliceBuffer(bufferTail, 2 + msgLength.length);
  2684. msgLength = parseInt(msgLength);
  2685. var msg = sliceBuffer(bufferTail, 0, msgLength);
  2686. if (isString) {
  2687. try {
  2688. msg = String.fromCharCode.apply(null, new Uint8Array(msg));
  2689. } catch (e) {
  2690. // iPhone Safari doesn't let you apply to typed arrays
  2691. var typed = new Uint8Array(msg);
  2692. msg = '';
  2693. for (var i = 0; i < typed.length; i++) {
  2694. msg += String.fromCharCode(typed[i]);
  2695. }
  2696. }
  2697. }
  2698. buffers.push(msg);
  2699. bufferTail = sliceBuffer(bufferTail, msgLength);
  2700. }
  2701. var total = buffers.length;
  2702. buffers.forEach(function(buffer, i) {
  2703. callback(exports.decodePacket(buffer, binaryType, true), i, total);
  2704. });
  2705. };
  2706. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  2707. },{"./keys":18,"after":19,"arraybuffer.slice":20,"base64-arraybuffer":21,"blob":11,"utf8":22}],18:[function(_dereq_,module,exports){
  2708. /**
  2709. * Gets the keys for an object.
  2710. *
  2711. * @return {Array} keys
  2712. * @api private
  2713. */
  2714. module.exports = Object.keys || function keys (obj){
  2715. var arr = [];
  2716. var has = Object.prototype.hasOwnProperty;
  2717. for (var i in obj) {
  2718. if (has.call(obj, i)) {
  2719. arr.push(i);
  2720. }
  2721. }
  2722. return arr;
  2723. };
  2724. },{}],19:[function(_dereq_,module,exports){
  2725. module.exports = after
  2726. function after(count, callback, err_cb) {
  2727. var bail = false
  2728. err_cb = err_cb || noop
  2729. proxy.count = count
  2730. return (count === 0) ? callback() : proxy
  2731. function proxy(err, result) {
  2732. if (proxy.count <= 0) {
  2733. throw new Error('after called too many times')
  2734. }
  2735. --proxy.count
  2736. // after first error, rest are passed to err_cb
  2737. if (err) {
  2738. bail = true
  2739. callback(err)
  2740. // future error callbacks will go to error handler
  2741. callback = err_cb
  2742. } else if (proxy.count === 0 && !bail) {
  2743. callback(null, result)
  2744. }
  2745. }
  2746. }
  2747. function noop() {}
  2748. },{}],20:[function(_dereq_,module,exports){
  2749. /**
  2750. * An abstraction for slicing an arraybuffer even when
  2751. * ArrayBuffer.prototype.slice is not supported
  2752. *
  2753. * @api public
  2754. */
  2755. module.exports = function(arraybuffer, start, end) {
  2756. var bytes = arraybuffer.byteLength;
  2757. start = start || 0;
  2758. end = end || bytes;
  2759. if (arraybuffer.slice) { return arraybuffer.slice(start, end); }
  2760. if (start < 0) { start += bytes; }
  2761. if (end < 0) { end += bytes; }
  2762. if (end > bytes) { end = bytes; }
  2763. if (start >= bytes || start >= end || bytes === 0) {
  2764. return new ArrayBuffer(0);
  2765. }
  2766. var abv = new Uint8Array(arraybuffer);
  2767. var result = new Uint8Array(end - start);
  2768. for (var i = start, ii = 0; i < end; i++, ii++) {
  2769. result[ii] = abv[i];
  2770. }
  2771. return result.buffer;
  2772. };
  2773. },{}],21:[function(_dereq_,module,exports){
  2774. /*
  2775. * base64-arraybuffer
  2776. * https://github.com/niklasvh/base64-arraybuffer
  2777. *
  2778. * Copyright (c) 2012 Niklas von Hertzen
  2779. * Licensed under the MIT license.
  2780. */
  2781. (function(chars){
  2782. "use strict";
  2783. exports.encode = function(arraybuffer) {
  2784. var bytes = new Uint8Array(arraybuffer),
  2785. i, len = bytes.length, base64 = "";
  2786. for (i = 0; i < len; i+=3) {
  2787. base64 += chars[bytes[i] >> 2];
  2788. base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
  2789. base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
  2790. base64 += chars[bytes[i + 2] & 63];
  2791. }
  2792. if ((len % 3) === 2) {
  2793. base64 = base64.substring(0, base64.length - 1) + "=";
  2794. } else if (len % 3 === 1) {
  2795. base64 = base64.substring(0, base64.length - 2) + "==";
  2796. }
  2797. return base64;
  2798. };
  2799. exports.decode = function(base64) {
  2800. var bufferLength = base64.length * 0.75,
  2801. len = base64.length, i, p = 0,
  2802. encoded1, encoded2, encoded3, encoded4;
  2803. if (base64[base64.length - 1] === "=") {
  2804. bufferLength--;
  2805. if (base64[base64.length - 2] === "=") {
  2806. bufferLength--;
  2807. }
  2808. }
  2809. var arraybuffer = new ArrayBuffer(bufferLength),
  2810. bytes = new Uint8Array(arraybuffer);
  2811. for (i = 0; i < len; i+=4) {
  2812. encoded1 = chars.indexOf(base64[i]);
  2813. encoded2 = chars.indexOf(base64[i+1]);
  2814. encoded3 = chars.indexOf(base64[i+2]);
  2815. encoded4 = chars.indexOf(base64[i+3]);
  2816. bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
  2817. bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
  2818. bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
  2819. }
  2820. return arraybuffer;
  2821. };
  2822. })("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
  2823. },{}],22:[function(_dereq_,module,exports){
  2824. (function (global){
  2825. /*! http://mths.be/utf8js v2.0.0 by @mathias */
  2826. ;(function(root) {
  2827. // Detect free variables `exports`
  2828. var freeExports = typeof exports == 'object' && exports;
  2829. // Detect free variable `module`
  2830. var freeModule = typeof module == 'object' && module &&
  2831. module.exports == freeExports && module;
  2832. // Detect free variable `global`, from Node.js or Browserified code,
  2833. // and use it as `root`
  2834. var freeGlobal = typeof global == 'object' && global;
  2835. if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
  2836. root = freeGlobal;
  2837. }
  2838. /*--------------------------------------------------------------------------*/
  2839. var stringFromCharCode = String.fromCharCode;
  2840. // Taken from http://mths.be/punycode
  2841. function ucs2decode(string) {
  2842. var output = [];
  2843. var counter = 0;
  2844. var length = string.length;
  2845. var value;
  2846. var extra;
  2847. while (counter < length) {
  2848. value = string.charCodeAt(counter++);
  2849. if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
  2850. // high surrogate, and there is a next character
  2851. extra = string.charCodeAt(counter++);
  2852. if ((extra & 0xFC00) == 0xDC00) { // low surrogate
  2853. output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
  2854. } else {
  2855. // unmatched surrogate; only append this code unit, in case the next
  2856. // code unit is the high surrogate of a surrogate pair
  2857. output.push(value);
  2858. counter--;
  2859. }
  2860. } else {
  2861. output.push(value);
  2862. }
  2863. }
  2864. return output;
  2865. }
  2866. // Taken from http://mths.be/punycode
  2867. function ucs2encode(array) {
  2868. var length = array.length;
  2869. var index = -1;
  2870. var value;
  2871. var output = '';
  2872. while (++index < length) {
  2873. value = array[index];
  2874. if (value > 0xFFFF) {
  2875. value -= 0x10000;
  2876. output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
  2877. value = 0xDC00 | value & 0x3FF;
  2878. }
  2879. output += stringFromCharCode(value);
  2880. }
  2881. return output;
  2882. }
  2883. /*--------------------------------------------------------------------------*/
  2884. function createByte(codePoint, shift) {
  2885. return stringFromCharCode(((codePoint >> shift) & 0x3F) | 0x80);
  2886. }
  2887. function encodeCodePoint(codePoint) {
  2888. if ((codePoint & 0xFFFFFF80) == 0) { // 1-byte sequence
  2889. return stringFromCharCode(codePoint);
  2890. }
  2891. var symbol = '';
  2892. if ((codePoint & 0xFFFFF800) == 0) { // 2-byte sequence
  2893. symbol = stringFromCharCode(((codePoint >> 6) & 0x1F) | 0xC0);
  2894. }
  2895. else if ((codePoint & 0xFFFF0000) == 0) { // 3-byte sequence
  2896. symbol = stringFromCharCode(((codePoint >> 12) & 0x0F) | 0xE0);
  2897. symbol += createByte(codePoint, 6);
  2898. }
  2899. else if ((codePoint & 0xFFE00000) == 0) { // 4-byte sequence
  2900. symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xF0);
  2901. symbol += createByte(codePoint, 12);
  2902. symbol += createByte(codePoint, 6);
  2903. }
  2904. symbol += stringFromCharCode((codePoint & 0x3F) | 0x80);
  2905. return symbol;
  2906. }
  2907. function utf8encode(string) {
  2908. var codePoints = ucs2decode(string);
  2909. // console.log(JSON.stringify(codePoints.map(function(x) {
  2910. // return 'U+' + x.toString(16).toUpperCase();
  2911. // })));
  2912. var length = codePoints.length;
  2913. var index = -1;
  2914. var codePoint;
  2915. var byteString = '';
  2916. while (++index < length) {
  2917. codePoint = codePoints[index];
  2918. byteString += encodeCodePoint(codePoint);
  2919. }
  2920. return byteString;
  2921. }
  2922. /*--------------------------------------------------------------------------*/
  2923. function readContinuationByte() {
  2924. if (byteIndex >= byteCount) {
  2925. throw Error('Invalid byte index');
  2926. }
  2927. var continuationByte = byteArray[byteIndex] & 0xFF;
  2928. byteIndex++;
  2929. if ((continuationByte & 0xC0) == 0x80) {
  2930. return continuationByte & 0x3F;
  2931. }
  2932. // If we end up here, it’s not a continuation byte
  2933. throw Error('Invalid continuation byte');
  2934. }
  2935. function decodeSymbol() {
  2936. var byte1;
  2937. var byte2;
  2938. var byte3;
  2939. var byte4;
  2940. var codePoint;
  2941. if (byteIndex > byteCount) {
  2942. throw Error('Invalid byte index');
  2943. }
  2944. if (byteIndex == byteCount) {
  2945. return false;
  2946. }
  2947. // Read first byte
  2948. byte1 = byteArray[byteIndex] & 0xFF;
  2949. byteIndex++;
  2950. // 1-byte sequence (no continuation bytes)
  2951. if ((byte1 & 0x80) == 0) {
  2952. return byte1;
  2953. }
  2954. // 2-byte sequence
  2955. if ((byte1 & 0xE0) == 0xC0) {
  2956. var byte2 = readContinuationByte();
  2957. codePoint = ((byte1 & 0x1F) << 6) | byte2;
  2958. if (codePoint >= 0x80) {
  2959. return codePoint;
  2960. } else {
  2961. throw Error('Invalid continuation byte');
  2962. }
  2963. }
  2964. // 3-byte sequence (may include unpaired surrogates)
  2965. if ((byte1 & 0xF0) == 0xE0) {
  2966. byte2 = readContinuationByte();
  2967. byte3 = readContinuationByte();
  2968. codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3;
  2969. if (codePoint >= 0x0800) {
  2970. return codePoint;
  2971. } else {
  2972. throw Error('Invalid continuation byte');
  2973. }
  2974. }
  2975. // 4-byte sequence
  2976. if ((byte1 & 0xF8) == 0xF0) {
  2977. byte2 = readContinuationByte();
  2978. byte3 = readContinuationByte();
  2979. byte4 = readContinuationByte();
  2980. codePoint = ((byte1 & 0x0F) << 0x12) | (byte2 << 0x0C) |
  2981. (byte3 << 0x06) | byte4;
  2982. if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) {
  2983. return codePoint;
  2984. }
  2985. }
  2986. throw Error('Invalid UTF-8 detected');
  2987. }
  2988. var byteArray;
  2989. var byteCount;
  2990. var byteIndex;
  2991. function utf8decode(byteString) {
  2992. byteArray = ucs2decode(byteString);
  2993. byteCount = byteArray.length;
  2994. byteIndex = 0;
  2995. var codePoints = [];
  2996. var tmp;
  2997. while ((tmp = decodeSymbol()) !== false) {
  2998. codePoints.push(tmp);
  2999. }
  3000. return ucs2encode(codePoints);
  3001. }
  3002. /*--------------------------------------------------------------------------*/
  3003. var utf8 = {
  3004. 'version': '2.0.0',
  3005. 'encode': utf8encode,
  3006. 'decode': utf8decode
  3007. };
  3008. // Some AMD build optimizers, like r.js, check for specific condition patterns
  3009. // like the following:
  3010. if (
  3011. typeof define == 'function' &&
  3012. typeof define.amd == 'object' &&
  3013. define.amd
  3014. ) {
  3015. define(function() {
  3016. return utf8;
  3017. });
  3018. } else if (freeExports && !freeExports.nodeType) {
  3019. if (freeModule) { // in Node.js or RingoJS v0.8.0+
  3020. freeModule.exports = utf8;
  3021. } else { // in Narwhal or RingoJS v0.7.0-
  3022. var object = {};
  3023. var hasOwnProperty = object.hasOwnProperty;
  3024. for (var key in utf8) {
  3025. hasOwnProperty.call(utf8, key) && (freeExports[key] = utf8[key]);
  3026. }
  3027. }
  3028. } else { // in Rhino or a web browser
  3029. root.utf8 = utf8;
  3030. }
  3031. }(this));
  3032. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  3033. },{}],23:[function(_dereq_,module,exports){
  3034. /**
  3035. * Module dependencies.
  3036. */
  3037. var global = _dereq_('global');
  3038. /**
  3039. * Module exports.
  3040. *
  3041. * Logic borrowed from Modernizr:
  3042. *
  3043. * - https://github.com/Modernizr/Modernizr/blob/master/feature-detects/cors.js
  3044. */
  3045. try {
  3046. module.exports = 'XMLHttpRequest' in global &&
  3047. 'withCredentials' in new global.XMLHttpRequest();
  3048. } catch (err) {
  3049. // if XMLHttp support is disabled in IE then it will throw
  3050. // when trying to create
  3051. module.exports = false;
  3052. }
  3053. },{"global":24}],24:[function(_dereq_,module,exports){
  3054. /**
  3055. * Returns `this`. Execute this without a "context" (i.e. without it being
  3056. * attached to an object of the left-hand side), and `this` points to the
  3057. * "global" scope of the current JS execution.
  3058. */
  3059. module.exports = (function () { return this; })();
  3060. },{}],25:[function(_dereq_,module,exports){
  3061. var indexOf = [].indexOf;
  3062. module.exports = function(arr, obj){
  3063. if (indexOf) return arr.indexOf(obj);
  3064. for (var i = 0; i < arr.length; ++i) {
  3065. if (arr[i] === obj) return i;
  3066. }
  3067. return -1;
  3068. };
  3069. },{}],26:[function(_dereq_,module,exports){
  3070. (function (global){
  3071. /**
  3072. * JSON parse.
  3073. *
  3074. * @see Based on jQuery#parseJSON (MIT) and JSON2
  3075. * @api private
  3076. */
  3077. var rvalidchars = /^[\],:{}\s]*$/;
  3078. var rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
  3079. var rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
  3080. var rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g;
  3081. var rtrimLeft = /^\s+/;
  3082. var rtrimRight = /\s+$/;
  3083. module.exports = function parsejson(data) {
  3084. if ('string' != typeof data || !data) {
  3085. return null;
  3086. }
  3087. data = data.replace(rtrimLeft, '').replace(rtrimRight, '');
  3088. // Attempt to parse using the native JSON parser first
  3089. if (global.JSON && JSON.parse) {
  3090. return JSON.parse(data);
  3091. }
  3092. if (rvalidchars.test(data.replace(rvalidescape, '@')
  3093. .replace(rvalidtokens, ']')
  3094. .replace(rvalidbraces, ''))) {
  3095. return (new Function('return ' + data))();
  3096. }
  3097. };
  3098. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  3099. },{}],27:[function(_dereq_,module,exports){
  3100. /**
  3101. * Compiles a querystring
  3102. * Returns string representation of the object
  3103. *
  3104. * @param {Object}
  3105. * @api private
  3106. */
  3107. exports.encode = function (obj) {
  3108. var str = '';
  3109. for (var i in obj) {
  3110. if (obj.hasOwnProperty(i)) {
  3111. if (str.length) str += '&';
  3112. str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]);
  3113. }
  3114. }
  3115. return str;
  3116. };
  3117. /**
  3118. * Parses a simple querystring into an object
  3119. *
  3120. * @param {String} qs
  3121. * @api private
  3122. */
  3123. exports.decode = function(qs){
  3124. var qry = {};
  3125. var pairs = qs.split('&');
  3126. for (var i = 0, l = pairs.length; i < l; i++) {
  3127. var pair = pairs[i].split('=');
  3128. qry[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
  3129. }
  3130. return qry;
  3131. };
  3132. },{}],28:[function(_dereq_,module,exports){
  3133. /**
  3134. * Parses an URI
  3135. *
  3136. * @author Steven Levithan <stevenlevithan.com> (MIT license)
  3137. * @api private
  3138. */
  3139. var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
  3140. var parts = [
  3141. 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'
  3142. ];
  3143. module.exports = function parseuri(str) {
  3144. var src = str,
  3145. b = str.indexOf('['),
  3146. e = str.indexOf(']');
  3147. if (b != -1 && e != -1) {
  3148. str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length);
  3149. }
  3150. var m = re.exec(str || ''),
  3151. uri = {},
  3152. i = 14;
  3153. while (i--) {
  3154. uri[parts[i]] = m[i] || '';
  3155. }
  3156. if (b != -1 && e != -1) {
  3157. uri.source = src;
  3158. uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':');
  3159. uri.authority = uri.authority.replace('[', '').replace(']', '').replace(/;/g, ':');
  3160. uri.ipv6uri = true;
  3161. }
  3162. return uri;
  3163. };
  3164. },{}],29:[function(_dereq_,module,exports){
  3165. /**
  3166. * Module dependencies.
  3167. */
  3168. var global = (function() { return this; })();
  3169. /**
  3170. * WebSocket constructor.
  3171. */
  3172. var WebSocket = global.WebSocket || global.MozWebSocket;
  3173. /**
  3174. * Module exports.
  3175. */
  3176. module.exports = WebSocket ? ws : null;
  3177. /**
  3178. * WebSocket constructor.
  3179. *
  3180. * The third `opts` options object gets ignored in web browsers, since it's
  3181. * non-standard, and throws a TypeError if passed to the constructor.
  3182. * See: https://github.com/einaros/ws/issues/227
  3183. *
  3184. * @param {String} uri
  3185. * @param {Array} protocols (optional)
  3186. * @param {Object) opts (optional)
  3187. * @api public
  3188. */
  3189. function ws(uri, protocols, opts) {
  3190. var instance;
  3191. if (protocols) {
  3192. instance = new WebSocket(uri, protocols);
  3193. } else {
  3194. instance = new WebSocket(uri);
  3195. }
  3196. return instance;
  3197. }
  3198. if (WebSocket) ws.prototype = WebSocket.prototype;
  3199. },{}]},{},[1])(1)
  3200. });