You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3551 lines
78 KiB

  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);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.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":15}],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":15,"indexof":23,"parsejson":24,"parseqs":25,"parseuri":26}],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":15}],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. });
  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);
  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":15,"parseqs":25,"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":15,"parseqs":25,"ws":27}],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":21}],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. * Expose `debug()` as the module.
  1857. */
  1858. module.exports = debug;
  1859. /**
  1860. * Create a debugger with the given `name`.
  1861. *
  1862. * @param {String} name
  1863. * @return {Type}
  1864. * @api public
  1865. */
  1866. function debug(name) {
  1867. if (!debug.enabled(name)) return function(){};
  1868. return function(fmt){
  1869. fmt = coerce(fmt);
  1870. var curr = new Date;
  1871. var ms = curr - (debug[name] || curr);
  1872. debug[name] = curr;
  1873. fmt = name
  1874. + ' '
  1875. + fmt
  1876. + ' +' + debug.humanize(ms);
  1877. // This hackery is required for IE8
  1878. // where `console.log` doesn't have 'apply'
  1879. window.console
  1880. && console.log
  1881. && Function.prototype.apply.call(console.log, console, arguments);
  1882. }
  1883. }
  1884. /**
  1885. * The currently active debug mode names.
  1886. */
  1887. debug.names = [];
  1888. debug.skips = [];
  1889. /**
  1890. * Enables a debug mode by name. This can include modes
  1891. * separated by a colon and wildcards.
  1892. *
  1893. * @param {String} name
  1894. * @api public
  1895. */
  1896. debug.enable = function(name) {
  1897. try {
  1898. localStorage.debug = name;
  1899. } catch(e){}
  1900. var split = (name || '').split(/[\s,]+/)
  1901. , len = split.length;
  1902. for (var i = 0; i < len; i++) {
  1903. name = split[i].replace('*', '.*?');
  1904. if (name[0] === '-') {
  1905. debug.skips.push(new RegExp('^' + name.substr(1) + '$'));
  1906. }
  1907. else {
  1908. debug.names.push(new RegExp('^' + name + '$'));
  1909. }
  1910. }
  1911. };
  1912. /**
  1913. * Disable debug output.
  1914. *
  1915. * @api public
  1916. */
  1917. debug.disable = function(){
  1918. debug.enable('');
  1919. };
  1920. /**
  1921. * Humanize the given `ms`.
  1922. *
  1923. * @param {Number} m
  1924. * @return {String}
  1925. * @api private
  1926. */
  1927. debug.humanize = function(ms) {
  1928. var sec = 1000
  1929. , min = 60 * 1000
  1930. , hour = 60 * min;
  1931. if (ms >= hour) return (ms / hour).toFixed(1) + 'h';
  1932. if (ms >= min) return (ms / min).toFixed(1) + 'm';
  1933. if (ms >= sec) return (ms / sec | 0) + 's';
  1934. return ms + 'ms';
  1935. };
  1936. /**
  1937. * Returns true if the given mode name is enabled, false otherwise.
  1938. *
  1939. * @param {String} name
  1940. * @return {Boolean}
  1941. * @api public
  1942. */
  1943. debug.enabled = function(name) {
  1944. for (var i = 0, len = debug.skips.length; i < len; i++) {
  1945. if (debug.skips[i].test(name)) {
  1946. return false;
  1947. }
  1948. }
  1949. for (var i = 0, len = debug.names.length; i < len; i++) {
  1950. if (debug.names[i].test(name)) {
  1951. return true;
  1952. }
  1953. }
  1954. return false;
  1955. };
  1956. /**
  1957. * Coerce `val`.
  1958. */
  1959. function coerce(val) {
  1960. if (val instanceof Error) return val.stack || val.message;
  1961. return val;
  1962. }
  1963. // persist
  1964. try {
  1965. if (window.localStorage) debug.enable(localStorage.debug);
  1966. } catch(e){}
  1967. },{}],15:[function(_dereq_,module,exports){
  1968. (function (global){
  1969. /**
  1970. * Module dependencies.
  1971. */
  1972. var keys = _dereq_('./keys');
  1973. var sliceBuffer = _dereq_('arraybuffer.slice');
  1974. var base64encoder = _dereq_('base64-arraybuffer');
  1975. var after = _dereq_('after');
  1976. var utf8 = _dereq_('utf8');
  1977. /**
  1978. * Check if we are running an android browser. That requires us to use
  1979. * ArrayBuffer with polling transports...
  1980. *
  1981. * http://ghinda.net/jpeg-blob-ajax-android/
  1982. */
  1983. var isAndroid = navigator.userAgent.match(/Android/i);
  1984. /**
  1985. * Current protocol version.
  1986. */
  1987. exports.protocol = 3;
  1988. /**
  1989. * Packet types.
  1990. */
  1991. var packets = exports.packets = {
  1992. open: 0 // non-ws
  1993. , close: 1 // non-ws
  1994. , ping: 2
  1995. , pong: 3
  1996. , message: 4
  1997. , upgrade: 5
  1998. , noop: 6
  1999. };
  2000. var packetslist = keys(packets);
  2001. /**
  2002. * Premade error packet.
  2003. */
  2004. var err = { type: 'error', data: 'parser error' };
  2005. /**
  2006. * Create a blob api even for blob builder when vendor prefixes exist
  2007. */
  2008. var Blob = _dereq_('blob');
  2009. /**
  2010. * Encodes a packet.
  2011. *
  2012. * <packet type id> [ <data> ]
  2013. *
  2014. * Example:
  2015. *
  2016. * 5hello world
  2017. * 3
  2018. * 4
  2019. *
  2020. * Binary is encoded in an identical principle
  2021. *
  2022. * @api private
  2023. */
  2024. exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {
  2025. if ('function' == typeof supportsBinary) {
  2026. callback = supportsBinary;
  2027. supportsBinary = false;
  2028. }
  2029. if ('function' == typeof utf8encode) {
  2030. callback = utf8encode;
  2031. utf8encode = null;
  2032. }
  2033. var data = (packet.data === undefined)
  2034. ? undefined
  2035. : packet.data.buffer || packet.data;
  2036. if (global.ArrayBuffer && data instanceof ArrayBuffer) {
  2037. return encodeArrayBuffer(packet, supportsBinary, callback);
  2038. } else if (Blob && data instanceof global.Blob) {
  2039. return encodeBlob(packet, supportsBinary, callback);
  2040. }
  2041. // Sending data as a utf-8 string
  2042. var encoded = packets[packet.type];
  2043. // data fragment is optional
  2044. if (undefined !== packet.data) {
  2045. encoded += utf8encode ? utf8.encode(String(packet.data)) : String(packet.data);
  2046. }
  2047. return callback('' + encoded);
  2048. };
  2049. /**
  2050. * Encode packet helpers for binary types
  2051. */
  2052. function encodeArrayBuffer(packet, supportsBinary, callback) {
  2053. if (!supportsBinary) {
  2054. return exports.encodeBase64Packet(packet, callback);
  2055. }
  2056. var data = packet.data;
  2057. var contentArray = new Uint8Array(data);
  2058. var resultBuffer = new Uint8Array(1 + data.byteLength);
  2059. resultBuffer[0] = packets[packet.type];
  2060. for (var i = 0; i < contentArray.length; i++) {
  2061. resultBuffer[i+1] = contentArray[i];
  2062. }
  2063. return callback(resultBuffer.buffer);
  2064. }
  2065. function encodeBlobAsArrayBuffer(packet, supportsBinary, callback) {
  2066. if (!supportsBinary) {
  2067. return exports.encodeBase64Packet(packet, callback);
  2068. }
  2069. var fr = new FileReader();
  2070. fr.onload = function() {
  2071. packet.data = fr.result;
  2072. exports.encodePacket(packet, supportsBinary, true, callback);
  2073. };
  2074. return fr.readAsArrayBuffer(packet.data);
  2075. }
  2076. function encodeBlob(packet, supportsBinary, callback) {
  2077. if (!supportsBinary) {
  2078. return exports.encodeBase64Packet(packet, callback);
  2079. }
  2080. if (isAndroid) {
  2081. return encodeBlobAsArrayBuffer(packet, supportsBinary, callback);
  2082. }
  2083. var length = new Uint8Array(1);
  2084. length[0] = packets[packet.type];
  2085. var blob = new Blob([length.buffer, packet.data]);
  2086. return callback(blob);
  2087. }
  2088. /**
  2089. * Encodes a packet with binary data in a base64 string
  2090. *
  2091. * @param {Object} packet, has `type` and `data`
  2092. * @return {String} base64 encoded message
  2093. */
  2094. exports.encodeBase64Packet = function(packet, callback) {
  2095. var message = 'b' + exports.packets[packet.type];
  2096. if (Blob && packet.data instanceof Blob) {
  2097. var fr = new FileReader();
  2098. fr.onload = function() {
  2099. var b64 = fr.result.split(',')[1];
  2100. callback(message + b64);
  2101. };
  2102. return fr.readAsDataURL(packet.data);
  2103. }
  2104. var b64data;
  2105. try {
  2106. b64data = String.fromCharCode.apply(null, new Uint8Array(packet.data));
  2107. } catch (e) {
  2108. // iPhone Safari doesn't let you apply with typed arrays
  2109. var typed = new Uint8Array(packet.data);
  2110. var basic = new Array(typed.length);
  2111. for (var i = 0; i < typed.length; i++) {
  2112. basic[i] = typed[i];
  2113. }
  2114. b64data = String.fromCharCode.apply(null, basic);
  2115. }
  2116. message += global.btoa(b64data);
  2117. return callback(message);
  2118. };
  2119. /**
  2120. * Decodes a packet. Changes format to Blob if requested.
  2121. *
  2122. * @return {Object} with `type` and `data` (if any)
  2123. * @api private
  2124. */
  2125. exports.decodePacket = function (data, binaryType, utf8decode) {
  2126. // String data
  2127. if (typeof data == 'string' || data === undefined) {
  2128. if (data.charAt(0) == 'b') {
  2129. return exports.decodeBase64Packet(data.substr(1), binaryType);
  2130. }
  2131. if (utf8decode) {
  2132. try {
  2133. data = utf8.decode(data);
  2134. } catch (e) {
  2135. return err;
  2136. }
  2137. }
  2138. var type = data.charAt(0);
  2139. if (Number(type) != type || !packetslist[type]) {
  2140. return err;
  2141. }
  2142. if (data.length > 1) {
  2143. return { type: packetslist[type], data: data.substring(1) };
  2144. } else {
  2145. return { type: packetslist[type] };
  2146. }
  2147. }
  2148. var asArray = new Uint8Array(data);
  2149. var type = asArray[0];
  2150. var rest = sliceBuffer(data, 1);
  2151. if (Blob && binaryType === 'blob') {
  2152. rest = new Blob([rest]);
  2153. }
  2154. return { type: packetslist[type], data: rest };
  2155. };
  2156. /**
  2157. * Decodes a packet encoded in a base64 string
  2158. *
  2159. * @param {String} base64 encoded message
  2160. * @return {Object} with `type` and `data` (if any)
  2161. */
  2162. exports.decodeBase64Packet = function(msg, binaryType) {
  2163. var type = packetslist[msg.charAt(0)];
  2164. if (!global.ArrayBuffer) {
  2165. return { type: type, data: { base64: true, data: msg.substr(1) } };
  2166. }
  2167. var data = base64encoder.decode(msg.substr(1));
  2168. if (binaryType === 'blob' && Blob) {
  2169. data = new Blob([data]);
  2170. }
  2171. return { type: type, data: data };
  2172. };
  2173. /**
  2174. * Encodes multiple messages (payload).
  2175. *
  2176. * <length>:data
  2177. *
  2178. * Example:
  2179. *
  2180. * 11:hello world2:hi
  2181. *
  2182. * If any contents are binary, they will be encoded as base64 strings. Base64
  2183. * encoded strings are marked with a b before the length specifier
  2184. *
  2185. * @param {Array} packets
  2186. * @api private
  2187. */
  2188. exports.encodePayload = function (packets, supportsBinary, callback) {
  2189. if (typeof supportsBinary == 'function') {
  2190. callback = supportsBinary;
  2191. supportsBinary = null;
  2192. }
  2193. if (supportsBinary) {
  2194. if (Blob && !isAndroid) {
  2195. return exports.encodePayloadAsBlob(packets, callback);
  2196. }
  2197. return exports.encodePayloadAsArrayBuffer(packets, callback);
  2198. }
  2199. if (!packets.length) {
  2200. return callback('0:');
  2201. }
  2202. function setLengthHeader(message) {
  2203. return message.length + ':' + message;
  2204. }
  2205. function encodeOne(packet, doneCallback) {
  2206. exports.encodePacket(packet, supportsBinary, true, function(message) {
  2207. doneCallback(null, setLengthHeader(message));
  2208. });
  2209. }
  2210. map(packets, encodeOne, function(err, results) {
  2211. return callback(results.join(''));
  2212. });
  2213. };
  2214. /**
  2215. * Async array map using after
  2216. */
  2217. function map(ary, each, done) {
  2218. var result = new Array(ary.length);
  2219. var next = after(ary.length, done);
  2220. var eachWithIndex = function(i, el, cb) {
  2221. each(el, function(error, msg) {
  2222. result[i] = msg;
  2223. cb(error, result);
  2224. });
  2225. };
  2226. for (var i = 0; i < ary.length; i++) {
  2227. eachWithIndex(i, ary[i], next);
  2228. }
  2229. }
  2230. /*
  2231. * Decodes data when a payload is maybe expected. Possible binary contents are
  2232. * decoded from their base64 representation
  2233. *
  2234. * @param {String} data, callback method
  2235. * @api public
  2236. */
  2237. exports.decodePayload = function (data, binaryType, callback) {
  2238. if (typeof data != 'string') {
  2239. return exports.decodePayloadAsBinary(data, binaryType, callback);
  2240. }
  2241. if (typeof binaryType === 'function') {
  2242. callback = binaryType;
  2243. binaryType = null;
  2244. }
  2245. var packet;
  2246. if (data == '') {
  2247. // parser error - ignoring payload
  2248. return callback(err, 0, 1);
  2249. }
  2250. var length = ''
  2251. , n, msg;
  2252. for (var i = 0, l = data.length; i < l; i++) {
  2253. var chr = data.charAt(i);
  2254. if (':' != chr) {
  2255. length += chr;
  2256. } else {
  2257. if ('' == length || (length != (n = Number(length)))) {
  2258. // parser error - ignoring payload
  2259. return callback(err, 0, 1);
  2260. }
  2261. msg = data.substr(i + 1, n);
  2262. if (length != msg.length) {
  2263. // parser error - ignoring payload
  2264. return callback(err, 0, 1);
  2265. }
  2266. if (msg.length) {
  2267. packet = exports.decodePacket(msg, binaryType, true);
  2268. if (err.type == packet.type && err.data == packet.data) {
  2269. // parser error in individual packet - ignoring payload
  2270. return callback(err, 0, 1);
  2271. }
  2272. var ret = callback(packet, i + n, l);
  2273. if (false === ret) return;
  2274. }
  2275. // advance cursor
  2276. i += n;
  2277. length = '';
  2278. }
  2279. }
  2280. if (length != '') {
  2281. // parser error - ignoring payload
  2282. return callback(err, 0, 1);
  2283. }
  2284. };
  2285. /**
  2286. * Encodes multiple messages (payload) as binary.
  2287. *
  2288. * <1 = binary, 0 = string><number from 0-9><number from 0-9>[...]<number
  2289. * 255><data>
  2290. *
  2291. * Example:
  2292. * 1 3 255 1 2 3, if the binary contents are interpreted as 8 bit integers
  2293. *
  2294. * @param {Array} packets
  2295. * @return {ArrayBuffer} encoded payload
  2296. * @api private
  2297. */
  2298. exports.encodePayloadAsArrayBuffer = function(packets, callback) {
  2299. if (!packets.length) {
  2300. return callback(new ArrayBuffer(0));
  2301. }
  2302. function encodeOne(packet, doneCallback) {
  2303. exports.encodePacket(packet, true, true, function(data) {
  2304. return doneCallback(null, data);
  2305. });
  2306. }
  2307. map(packets, encodeOne, function(err, encodedPackets) {
  2308. var totalLength = encodedPackets.reduce(function(acc, p) {
  2309. var len;
  2310. if (typeof p === 'string'){
  2311. len = p.length;
  2312. } else {
  2313. len = p.byteLength;
  2314. }
  2315. return acc + len.toString().length + len + 2; // string/binary identifier + separator = 2
  2316. }, 0);
  2317. var resultArray = new Uint8Array(totalLength);
  2318. var bufferIndex = 0;
  2319. encodedPackets.forEach(function(p) {
  2320. var isString = typeof p === 'string';
  2321. var ab = p;
  2322. if (isString) {
  2323. var view = new Uint8Array(p.length);
  2324. for (var i = 0; i < p.length; i++) {
  2325. view[i] = p.charCodeAt(i);
  2326. }
  2327. ab = view.buffer;
  2328. }
  2329. if (isString) { // not true binary
  2330. resultArray[bufferIndex++] = 0;
  2331. } else { // true binary
  2332. resultArray[bufferIndex++] = 1;
  2333. }
  2334. var lenStr = ab.byteLength.toString();
  2335. for (var i = 0; i < lenStr.length; i++) {
  2336. resultArray[bufferIndex++] = parseInt(lenStr[i]);
  2337. }
  2338. resultArray[bufferIndex++] = 255;
  2339. var view = new Uint8Array(ab);
  2340. for (var i = 0; i < view.length; i++) {
  2341. resultArray[bufferIndex++] = view[i];
  2342. }
  2343. });
  2344. return callback(resultArray.buffer);
  2345. });
  2346. };
  2347. /**
  2348. * Encode as Blob
  2349. */
  2350. exports.encodePayloadAsBlob = function(packets, callback) {
  2351. function encodeOne(packet, doneCallback) {
  2352. exports.encodePacket(packet, true, true, function(encoded) {
  2353. var binaryIdentifier = new Uint8Array(1);
  2354. binaryIdentifier[0] = 1;
  2355. if (typeof encoded === 'string') {
  2356. var view = new Uint8Array(encoded.length);
  2357. for (var i = 0; i < encoded.length; i++) {
  2358. view[i] = encoded.charCodeAt(i);
  2359. }
  2360. encoded = view.buffer;
  2361. binaryIdentifier[0] = 0;
  2362. }
  2363. var len = (encoded instanceof ArrayBuffer)
  2364. ? encoded.byteLength
  2365. : encoded.size;
  2366. var lenStr = len.toString();
  2367. var lengthAry = new Uint8Array(lenStr.length + 1);
  2368. for (var i = 0; i < lenStr.length; i++) {
  2369. lengthAry[i] = parseInt(lenStr[i]);
  2370. }
  2371. lengthAry[lenStr.length] = 255;
  2372. if (Blob) {
  2373. var blob = new Blob([binaryIdentifier.buffer, lengthAry.buffer, encoded]);
  2374. doneCallback(null, blob);
  2375. }
  2376. });
  2377. }
  2378. map(packets, encodeOne, function(err, results) {
  2379. return callback(new Blob(results));
  2380. });
  2381. };
  2382. /*
  2383. * Decodes data when a payload is maybe expected. Strings are decoded by
  2384. * interpreting each byte as a key code for entries marked to start with 0. See
  2385. * description of encodePayloadAsBinary
  2386. *
  2387. * @param {ArrayBuffer} data, callback method
  2388. * @api public
  2389. */
  2390. exports.decodePayloadAsBinary = function (data, binaryType, callback) {
  2391. if (typeof binaryType === 'function') {
  2392. callback = binaryType;
  2393. binaryType = null;
  2394. }
  2395. var bufferTail = data;
  2396. var buffers = [];
  2397. var numberTooLong = false;
  2398. while (bufferTail.byteLength > 0) {
  2399. var tailArray = new Uint8Array(bufferTail);
  2400. var isString = tailArray[0] === 0;
  2401. var msgLength = '';
  2402. for (var i = 1; ; i++) {
  2403. if (tailArray[i] == 255) break;
  2404. if (msgLength.length > 310) {
  2405. numberTooLong = true;
  2406. break;
  2407. }
  2408. msgLength += tailArray[i];
  2409. }
  2410. if(numberTooLong) return callback(err, 0, 1);
  2411. bufferTail = sliceBuffer(bufferTail, 2 + msgLength.length);
  2412. msgLength = parseInt(msgLength);
  2413. var msg = sliceBuffer(bufferTail, 0, msgLength);
  2414. if (isString) {
  2415. try {
  2416. msg = String.fromCharCode.apply(null, new Uint8Array(msg));
  2417. } catch (e) {
  2418. // iPhone Safari doesn't let you apply to typed arrays
  2419. var typed = new Uint8Array(msg);
  2420. msg = '';
  2421. for (var i = 0; i < typed.length; i++) {
  2422. msg += String.fromCharCode(typed[i]);
  2423. }
  2424. }
  2425. }
  2426. buffers.push(msg);
  2427. bufferTail = sliceBuffer(bufferTail, msgLength);
  2428. }
  2429. var total = buffers.length;
  2430. buffers.forEach(function(buffer, i) {
  2431. callback(exports.decodePacket(buffer, binaryType, true), i, total);
  2432. });
  2433. };
  2434. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  2435. },{"./keys":16,"after":17,"arraybuffer.slice":18,"base64-arraybuffer":19,"blob":11,"utf8":20}],16:[function(_dereq_,module,exports){
  2436. /**
  2437. * Gets the keys for an object.
  2438. *
  2439. * @return {Array} keys
  2440. * @api private
  2441. */
  2442. module.exports = Object.keys || function keys (obj){
  2443. var arr = [];
  2444. var has = Object.prototype.hasOwnProperty;
  2445. for (var i in obj) {
  2446. if (has.call(obj, i)) {
  2447. arr.push(i);
  2448. }
  2449. }
  2450. return arr;
  2451. };
  2452. },{}],17:[function(_dereq_,module,exports){
  2453. module.exports = after
  2454. function after(count, callback, err_cb) {
  2455. var bail = false
  2456. err_cb = err_cb || noop
  2457. proxy.count = count
  2458. return (count === 0) ? callback() : proxy
  2459. function proxy(err, result) {
  2460. if (proxy.count <= 0) {
  2461. throw new Error('after called too many times')
  2462. }
  2463. --proxy.count
  2464. // after first error, rest are passed to err_cb
  2465. if (err) {
  2466. bail = true
  2467. callback(err)
  2468. // future error callbacks will go to error handler
  2469. callback = err_cb
  2470. } else if (proxy.count === 0 && !bail) {
  2471. callback(null, result)
  2472. }
  2473. }
  2474. }
  2475. function noop() {}
  2476. },{}],18:[function(_dereq_,module,exports){
  2477. /**
  2478. * An abstraction for slicing an arraybuffer even when
  2479. * ArrayBuffer.prototype.slice is not supported
  2480. *
  2481. * @api public
  2482. */
  2483. module.exports = function(arraybuffer, start, end) {
  2484. var bytes = arraybuffer.byteLength;
  2485. start = start || 0;
  2486. end = end || bytes;
  2487. if (arraybuffer.slice) { return arraybuffer.slice(start, end); }
  2488. if (start < 0) { start += bytes; }
  2489. if (end < 0) { end += bytes; }
  2490. if (end > bytes) { end = bytes; }
  2491. if (start >= bytes || start >= end || bytes === 0) {
  2492. return new ArrayBuffer(0);
  2493. }
  2494. var abv = new Uint8Array(arraybuffer);
  2495. var result = new Uint8Array(end - start);
  2496. for (var i = start, ii = 0; i < end; i++, ii++) {
  2497. result[ii] = abv[i];
  2498. }
  2499. return result.buffer;
  2500. };
  2501. },{}],19:[function(_dereq_,module,exports){
  2502. /*
  2503. * base64-arraybuffer
  2504. * https://github.com/niklasvh/base64-arraybuffer
  2505. *
  2506. * Copyright (c) 2012 Niklas von Hertzen
  2507. * Licensed under the MIT license.
  2508. */
  2509. (function(chars){
  2510. "use strict";
  2511. exports.encode = function(arraybuffer) {
  2512. var bytes = new Uint8Array(arraybuffer),
  2513. i, len = bytes.length, base64 = "";
  2514. for (i = 0; i < len; i+=3) {
  2515. base64 += chars[bytes[i] >> 2];
  2516. base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
  2517. base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
  2518. base64 += chars[bytes[i + 2] & 63];
  2519. }
  2520. if ((len % 3) === 2) {
  2521. base64 = base64.substring(0, base64.length - 1) + "=";
  2522. } else if (len % 3 === 1) {
  2523. base64 = base64.substring(0, base64.length - 2) + "==";
  2524. }
  2525. return base64;
  2526. };
  2527. exports.decode = function(base64) {
  2528. var bufferLength = base64.length * 0.75,
  2529. len = base64.length, i, p = 0,
  2530. encoded1, encoded2, encoded3, encoded4;
  2531. if (base64[base64.length - 1] === "=") {
  2532. bufferLength--;
  2533. if (base64[base64.length - 2] === "=") {
  2534. bufferLength--;
  2535. }
  2536. }
  2537. var arraybuffer = new ArrayBuffer(bufferLength),
  2538. bytes = new Uint8Array(arraybuffer);
  2539. for (i = 0; i < len; i+=4) {
  2540. encoded1 = chars.indexOf(base64[i]);
  2541. encoded2 = chars.indexOf(base64[i+1]);
  2542. encoded3 = chars.indexOf(base64[i+2]);
  2543. encoded4 = chars.indexOf(base64[i+3]);
  2544. bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
  2545. bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
  2546. bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
  2547. }
  2548. return arraybuffer;
  2549. };
  2550. })("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
  2551. },{}],20:[function(_dereq_,module,exports){
  2552. (function (global){
  2553. /*! http://mths.be/utf8js v2.0.0 by @mathias */
  2554. ;(function(root) {
  2555. // Detect free variables `exports`
  2556. var freeExports = typeof exports == 'object' && exports;
  2557. // Detect free variable `module`
  2558. var freeModule = typeof module == 'object' && module &&
  2559. module.exports == freeExports && module;
  2560. // Detect free variable `global`, from Node.js or Browserified code,
  2561. // and use it as `root`
  2562. var freeGlobal = typeof global == 'object' && global;
  2563. if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
  2564. root = freeGlobal;
  2565. }
  2566. /*--------------------------------------------------------------------------*/
  2567. var stringFromCharCode = String.fromCharCode;
  2568. // Taken from http://mths.be/punycode
  2569. function ucs2decode(string) {
  2570. var output = [];
  2571. var counter = 0;
  2572. var length = string.length;
  2573. var value;
  2574. var extra;
  2575. while (counter < length) {
  2576. value = string.charCodeAt(counter++);
  2577. if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
  2578. // high surrogate, and there is a next character
  2579. extra = string.charCodeAt(counter++);
  2580. if ((extra & 0xFC00) == 0xDC00) { // low surrogate
  2581. output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
  2582. } else {
  2583. // unmatched surrogate; only append this code unit, in case the next
  2584. // code unit is the high surrogate of a surrogate pair
  2585. output.push(value);
  2586. counter--;
  2587. }
  2588. } else {
  2589. output.push(value);
  2590. }
  2591. }
  2592. return output;
  2593. }
  2594. // Taken from http://mths.be/punycode
  2595. function ucs2encode(array) {
  2596. var length = array.length;
  2597. var index = -1;
  2598. var value;
  2599. var output = '';
  2600. while (++index < length) {
  2601. value = array[index];
  2602. if (value > 0xFFFF) {
  2603. value -= 0x10000;
  2604. output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
  2605. value = 0xDC00 | value & 0x3FF;
  2606. }
  2607. output += stringFromCharCode(value);
  2608. }
  2609. return output;
  2610. }
  2611. /*--------------------------------------------------------------------------*/
  2612. function createByte(codePoint, shift) {
  2613. return stringFromCharCode(((codePoint >> shift) & 0x3F) | 0x80);
  2614. }
  2615. function encodeCodePoint(codePoint) {
  2616. if ((codePoint & 0xFFFFFF80) == 0) { // 1-byte sequence
  2617. return stringFromCharCode(codePoint);
  2618. }
  2619. var symbol = '';
  2620. if ((codePoint & 0xFFFFF800) == 0) { // 2-byte sequence
  2621. symbol = stringFromCharCode(((codePoint >> 6) & 0x1F) | 0xC0);
  2622. }
  2623. else if ((codePoint & 0xFFFF0000) == 0) { // 3-byte sequence
  2624. symbol = stringFromCharCode(((codePoint >> 12) & 0x0F) | 0xE0);
  2625. symbol += createByte(codePoint, 6);
  2626. }
  2627. else if ((codePoint & 0xFFE00000) == 0) { // 4-byte sequence
  2628. symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xF0);
  2629. symbol += createByte(codePoint, 12);
  2630. symbol += createByte(codePoint, 6);
  2631. }
  2632. symbol += stringFromCharCode((codePoint & 0x3F) | 0x80);
  2633. return symbol;
  2634. }
  2635. function utf8encode(string) {
  2636. var codePoints = ucs2decode(string);
  2637. // console.log(JSON.stringify(codePoints.map(function(x) {
  2638. // return 'U+' + x.toString(16).toUpperCase();
  2639. // })));
  2640. var length = codePoints.length;
  2641. var index = -1;
  2642. var codePoint;
  2643. var byteString = '';
  2644. while (++index < length) {
  2645. codePoint = codePoints[index];
  2646. byteString += encodeCodePoint(codePoint);
  2647. }
  2648. return byteString;
  2649. }
  2650. /*--------------------------------------------------------------------------*/
  2651. function readContinuationByte() {
  2652. if (byteIndex >= byteCount) {
  2653. throw Error('Invalid byte index');
  2654. }
  2655. var continuationByte = byteArray[byteIndex] & 0xFF;
  2656. byteIndex++;
  2657. if ((continuationByte & 0xC0) == 0x80) {
  2658. return continuationByte & 0x3F;
  2659. }
  2660. // If we end up here, it’s not a continuation byte
  2661. throw Error('Invalid continuation byte');
  2662. }
  2663. function decodeSymbol() {
  2664. var byte1;
  2665. var byte2;
  2666. var byte3;
  2667. var byte4;
  2668. var codePoint;
  2669. if (byteIndex > byteCount) {
  2670. throw Error('Invalid byte index');
  2671. }
  2672. if (byteIndex == byteCount) {
  2673. return false;
  2674. }
  2675. // Read first byte
  2676. byte1 = byteArray[byteIndex] & 0xFF;
  2677. byteIndex++;
  2678. // 1-byte sequence (no continuation bytes)
  2679. if ((byte1 & 0x80) == 0) {
  2680. return byte1;
  2681. }
  2682. // 2-byte sequence
  2683. if ((byte1 & 0xE0) == 0xC0) {
  2684. var byte2 = readContinuationByte();
  2685. codePoint = ((byte1 & 0x1F) << 6) | byte2;
  2686. if (codePoint >= 0x80) {
  2687. return codePoint;
  2688. } else {
  2689. throw Error('Invalid continuation byte');
  2690. }
  2691. }
  2692. // 3-byte sequence (may include unpaired surrogates)
  2693. if ((byte1 & 0xF0) == 0xE0) {
  2694. byte2 = readContinuationByte();
  2695. byte3 = readContinuationByte();
  2696. codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3;
  2697. if (codePoint >= 0x0800) {
  2698. return codePoint;
  2699. } else {
  2700. throw Error('Invalid continuation byte');
  2701. }
  2702. }
  2703. // 4-byte sequence
  2704. if ((byte1 & 0xF8) == 0xF0) {
  2705. byte2 = readContinuationByte();
  2706. byte3 = readContinuationByte();
  2707. byte4 = readContinuationByte();
  2708. codePoint = ((byte1 & 0x0F) << 0x12) | (byte2 << 0x0C) |
  2709. (byte3 << 0x06) | byte4;
  2710. if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) {
  2711. return codePoint;
  2712. }
  2713. }
  2714. throw Error('Invalid UTF-8 detected');
  2715. }
  2716. var byteArray;
  2717. var byteCount;
  2718. var byteIndex;
  2719. function utf8decode(byteString) {
  2720. byteArray = ucs2decode(byteString);
  2721. byteCount = byteArray.length;
  2722. byteIndex = 0;
  2723. var codePoints = [];
  2724. var tmp;
  2725. while ((tmp = decodeSymbol()) !== false) {
  2726. codePoints.push(tmp);
  2727. }
  2728. return ucs2encode(codePoints);
  2729. }
  2730. /*--------------------------------------------------------------------------*/
  2731. var utf8 = {
  2732. 'version': '2.0.0',
  2733. 'encode': utf8encode,
  2734. 'decode': utf8decode
  2735. };
  2736. // Some AMD build optimizers, like r.js, check for specific condition patterns
  2737. // like the following:
  2738. if (
  2739. typeof define == 'function' &&
  2740. typeof define.amd == 'object' &&
  2741. define.amd
  2742. ) {
  2743. define(function() {
  2744. return utf8;
  2745. });
  2746. } else if (freeExports && !freeExports.nodeType) {
  2747. if (freeModule) { // in Node.js or RingoJS v0.8.0+
  2748. freeModule.exports = utf8;
  2749. } else { // in Narwhal or RingoJS v0.7.0-
  2750. var object = {};
  2751. var hasOwnProperty = object.hasOwnProperty;
  2752. for (var key in utf8) {
  2753. hasOwnProperty.call(utf8, key) && (freeExports[key] = utf8[key]);
  2754. }
  2755. }
  2756. } else { // in Rhino or a web browser
  2757. root.utf8 = utf8;
  2758. }
  2759. }(this));
  2760. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  2761. },{}],21:[function(_dereq_,module,exports){
  2762. /**
  2763. * Module dependencies.
  2764. */
  2765. var global = _dereq_('global');
  2766. /**
  2767. * Module exports.
  2768. *
  2769. * Logic borrowed from Modernizr:
  2770. *
  2771. * - https://github.com/Modernizr/Modernizr/blob/master/feature-detects/cors.js
  2772. */
  2773. try {
  2774. module.exports = 'XMLHttpRequest' in global &&
  2775. 'withCredentials' in new global.XMLHttpRequest();
  2776. } catch (err) {
  2777. // if XMLHttp support is disabled in IE then it will throw
  2778. // when trying to create
  2779. module.exports = false;
  2780. }
  2781. },{"global":22}],22:[function(_dereq_,module,exports){
  2782. /**
  2783. * Returns `this`. Execute this without a "context" (i.e. without it being
  2784. * attached to an object of the left-hand side), and `this` points to the
  2785. * "global" scope of the current JS execution.
  2786. */
  2787. module.exports = (function () { return this; })();
  2788. },{}],23:[function(_dereq_,module,exports){
  2789. var indexOf = [].indexOf;
  2790. module.exports = function(arr, obj){
  2791. if (indexOf) return arr.indexOf(obj);
  2792. for (var i = 0; i < arr.length; ++i) {
  2793. if (arr[i] === obj) return i;
  2794. }
  2795. return -1;
  2796. };
  2797. },{}],24:[function(_dereq_,module,exports){
  2798. (function (global){
  2799. /**
  2800. * JSON parse.
  2801. *
  2802. * @see Based on jQuery#parseJSON (MIT) and JSON2
  2803. * @api private
  2804. */
  2805. var rvalidchars = /^[\],:{}\s]*$/;
  2806. var rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
  2807. var rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
  2808. var rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g;
  2809. var rtrimLeft = /^\s+/;
  2810. var rtrimRight = /\s+$/;
  2811. module.exports = function parsejson(data) {
  2812. if ('string' != typeof data || !data) {
  2813. return null;
  2814. }
  2815. data = data.replace(rtrimLeft, '').replace(rtrimRight, '');
  2816. // Attempt to parse using the native JSON parser first
  2817. if (global.JSON && JSON.parse) {
  2818. return JSON.parse(data);
  2819. }
  2820. if (rvalidchars.test(data.replace(rvalidescape, '@')
  2821. .replace(rvalidtokens, ']')
  2822. .replace(rvalidbraces, ''))) {
  2823. return (new Function('return ' + data))();
  2824. }
  2825. };
  2826. }).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  2827. },{}],25:[function(_dereq_,module,exports){
  2828. /**
  2829. * Compiles a querystring
  2830. * Returns string representation of the object
  2831. *
  2832. * @param {Object}
  2833. * @api private
  2834. */
  2835. exports.encode = function (obj) {
  2836. var str = '';
  2837. for (var i in obj) {
  2838. if (obj.hasOwnProperty(i)) {
  2839. if (str.length) str += '&';
  2840. str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]);
  2841. }
  2842. }
  2843. return str;
  2844. };
  2845. /**
  2846. * Parses a simple querystring into an object
  2847. *
  2848. * @param {String} qs
  2849. * @api private
  2850. */
  2851. exports.decode = function(qs){
  2852. var qry = {};
  2853. var pairs = qs.split('&');
  2854. for (var i = 0, l = pairs.length; i < l; i++) {
  2855. var pair = pairs[i].split('=');
  2856. qry[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
  2857. }
  2858. return qry;
  2859. };
  2860. },{}],26:[function(_dereq_,module,exports){
  2861. /**
  2862. * Parses an URI
  2863. *
  2864. * @author Steven Levithan <stevenlevithan.com> (MIT license)
  2865. * @api private
  2866. */
  2867. var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
  2868. var parts = [
  2869. 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'
  2870. ];
  2871. module.exports = function parseuri(str) {
  2872. var src = str,
  2873. b = str.indexOf('['),
  2874. e = str.indexOf(']');
  2875. if (b != -1 && e != -1) {
  2876. str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length);
  2877. }
  2878. var m = re.exec(str || ''),
  2879. uri = {},
  2880. i = 14;
  2881. while (i--) {
  2882. uri[parts[i]] = m[i] || '';
  2883. }
  2884. if (b != -1 && e != -1) {
  2885. uri.source = src;
  2886. uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':');
  2887. uri.authority = uri.authority.replace('[', '').replace(']', '').replace(/;/g, ':');
  2888. uri.ipv6uri = true;
  2889. }
  2890. return uri;
  2891. };
  2892. },{}],27:[function(_dereq_,module,exports){
  2893. /**
  2894. * Module dependencies.
  2895. */
  2896. var global = (function() { return this; })();
  2897. /**
  2898. * WebSocket constructor.
  2899. */
  2900. var WebSocket = global.WebSocket || global.MozWebSocket;
  2901. /**
  2902. * Module exports.
  2903. */
  2904. module.exports = WebSocket ? ws : null;
  2905. /**
  2906. * WebSocket constructor.
  2907. *
  2908. * The third `opts` options object gets ignored in web browsers, since it's
  2909. * non-standard, and throws a TypeError if passed to the constructor.
  2910. * See: https://github.com/einaros/ws/issues/227
  2911. *
  2912. * @param {String} uri
  2913. * @param {Array} protocols (optional)
  2914. * @param {Object) opts (optional)
  2915. * @api public
  2916. */
  2917. function ws(uri, protocols, opts) {
  2918. var instance;
  2919. if (protocols) {
  2920. instance = new WebSocket(uri, protocols);
  2921. } else {
  2922. instance = new WebSocket(uri);
  2923. }
  2924. return instance;
  2925. }
  2926. if (WebSocket) ws.prototype = WebSocket.prototype;
  2927. },{}]},{},[1])
  2928. (1)
  2929. });