index.js 12 KB

1
  1. module.exports=function(t){function e(o){if(n[o])return n[o].exports;var i=n[o]={i:o,l:!1,exports:{}};return t[o].call(i.exports,i,i.exports,e),i.l=!0,i.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,o){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:o})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=7)}([function(t,e){t.exports=require("debug")},function(t,e){t.exports=require("socket.io-parser")},function(t,e,n){function o(t,e){if(!(this instanceof o))return new o(t,e);t&&"object"==typeof t&&(e=t,t=void 0),(e=e||{}).path=e.path||"/socket.io",this.nsps={},this.subs=[],this.opts=e,this.reconnection(!1!==e.reconnection),this.reconnectionAttempts(e.reconnectionAttempts||1/0),this.reconnectionDelay(e.reconnectionDelay||1e3),this.reconnectionDelayMax(e.reconnectionDelayMax||5e3),this.randomizationFactor(e.randomizationFactor||.5),this.backoff=new f({min:this.reconnectionDelay(),max:this.reconnectionDelayMax(),jitter:this.randomizationFactor()}),this.timeout(null==e.timeout?2e4:e.timeout),this.readyState="closed",this.uri=t,this.connecting=[],this.lastPing=null,this.encoding=!1,this.packetBuffer=[];var n=e.parser||r;this.encoder=new n.Encoder,this.decoder=new n.Decoder,this.autoConnect=!1!==e.autoConnect,this.autoConnect&&this.open()}var i=n(12),s=n(3),c=n(4),r=n(1),p=n(5),h=n(6),a=n(0)("socket.io-client:manager"),u=n(15),f=n(16),d=Object.prototype.hasOwnProperty;t.exports=o,o.prototype.emitAll=function(){this.emit.apply(this,arguments);for(var t in this.nsps)d.call(this.nsps,t)&&this.nsps[t].emit.apply(this.nsps[t],arguments)},o.prototype.updateSocketIds=function(){for(var t in this.nsps)d.call(this.nsps,t)&&(this.nsps[t].id=this.generateId(t))},o.prototype.generateId=function(t){return("/"===t?"":t+"#")+this.engine.id},c(o.prototype),o.prototype.reconnection=function(t){return arguments.length?(this._reconnection=!!t,this):this._reconnection},o.prototype.reconnectionAttempts=function(t){return arguments.length?(this._reconnectionAttempts=t,this):this._reconnectionAttempts},o.prototype.reconnectionDelay=function(t){return arguments.length?(this._reconnectionDelay=t,this.backoff&&this.backoff.setMin(t),this):this._reconnectionDelay},o.prototype.randomizationFactor=function(t){return arguments.length?(this._randomizationFactor=t,this.backoff&&this.backoff.setJitter(t),this):this._randomizationFactor},o.prototype.reconnectionDelayMax=function(t){return arguments.length?(this._reconnectionDelayMax=t,this.backoff&&this.backoff.setMax(t),this):this._reconnectionDelayMax},o.prototype.timeout=function(t){return arguments.length?(this._timeout=t,this):this._timeout},o.prototype.maybeReconnectOnOpen=function(){!this.reconnecting&&this._reconnection&&0===this.backoff.attempts&&this.reconnect()},o.prototype.open=o.prototype.connect=function(t,e){if(a("readyState %s",this.readyState),~this.readyState.indexOf("open"))return this;a("opening %s",this.uri),this.engine=i(this.uri,this.opts);var n=this.engine,o=this;this.readyState="opening",this.skipReconnect=!1;var s=p(n,"open",function(){o.onopen(),t&&t()}),c=p(n,"error",function(e){if(a("connect_error"),o.cleanup(),o.readyState="closed",o.emitAll("connect_error",e),t){var n=new Error("Connection error");n.data=e,t(n)}else o.maybeReconnectOnOpen()});if(!1!==this._timeout){var r=this._timeout;a("connect attempt will timeout after %d",r);var h=setTimeout(function(){a("connect attempt timed out after %d",r),s.destroy(),n.close(),n.emit("error","timeout"),o.emitAll("connect_timeout",r)},r);this.subs.push({destroy:function(){clearTimeout(h)}})}return this.subs.push(s),this.subs.push(c),this},o.prototype.onopen=function(){a("open"),this.cleanup(),this.readyState="open",this.emit("open");var t=this.engine;this.subs.push(p(t,"data",h(this,"ondata"))),this.subs.push(p(t,"ping",h(this,"onping"))),this.subs.push(p(t,"pong",h(this,"onpong"))),this.subs.push(p(t,"error",h(this,"onerror"))),this.subs.push(p(t,"close",h(this,"onclose"))),this.subs.push(p(this.decoder,"decoded",h(this,"ondecoded")))},o.prototype.onping=function(){this.lastPing=new Date,this.emitAll("ping")},o.prototype.onpong=function(){this.emitAll("pong",new Date-this.lastPing)},o.prototype.ondata=function(t){this.decoder.add(t)},o.prototype.ondecoded=function(t){this.emit("packet",t)},o.prototype.onerror=function(t){a("error",t),this.emitAll("error",t)},o.prototype.socket=function(t,e){function n(){~u(i.connecting,o)||i.connecting.push(o)}var o=this.nsps[t];if(!o){o=new s(this,t,e),this.nsps[t]=o;var i=this;o.on("connecting",n),o.on("connect",function(){o.id=i.generateId(t)}),this.autoConnect&&n()}return o},o.prototype.destroy=function(t){var e=u(this.connecting,t);~e&&this.connecting.splice(e,1),this.connecting.length||this.close()},o.prototype.packet=function(t){a("writing packet %j",t);var e=this;t.query&&0===t.type&&(t.nsp+="?"+t.query),e.encoding?e.packetBuffer.push(t):(e.encoding=!0,this.encoder.encode(t,function(n){for(var o=0;o<n.length;o++)e.engine.write(n[o],t.options);e.encoding=!1,e.processPacketQueue()}))},o.prototype.processPacketQueue=function(){if(this.packetBuffer.length>0&&!this.encoding){var t=this.packetBuffer.shift();this.packet(t)}},o.prototype.cleanup=function(){a("cleanup");for(var t=this.subs.length,e=0;e<t;e++){this.subs.shift().destroy()}this.packetBuffer=[],this.encoding=!1,this.lastPing=null,this.decoder.destroy()},o.prototype.close=o.prototype.disconnect=function(){a("disconnect"),this.skipReconnect=!0,this.reconnecting=!1,"opening"===this.readyState&&this.cleanup(),this.backoff.reset(),this.readyState="closed",this.engine&&this.engine.close()},o.prototype.onclose=function(t){a("onclose"),this.cleanup(),this.backoff.reset(),this.readyState="closed",this.emit("close",t),this._reconnection&&!this.skipReconnect&&this.reconnect()},o.prototype.reconnect=function(){if(this.reconnecting||this.skipReconnect)return this;var t=this;if(this.backoff.attempts>=this._reconnectionAttempts)a("reconnect failed"),this.backoff.reset(),this.emitAll("reconnect_failed"),this.reconnecting=!1;else{var e=this.backoff.duration();a("will wait %dms before reconnect attempt",e),this.reconnecting=!0;var n=setTimeout(function(){t.skipReconnect||(a("attempting reconnect"),t.emitAll("reconnect_attempt",t.backoff.attempts),t.emitAll("reconnecting",t.backoff.attempts),t.skipReconnect||t.open(function(e){e?(a("reconnect attempt error"),t.reconnecting=!1,t.reconnect(),t.emitAll("reconnect_error",e.data)):(a("reconnect success"),t.onreconnect())}))},e);this.subs.push({destroy:function(){clearTimeout(n)}})}},o.prototype.onreconnect=function(){var t=this.backoff.attempts;this.reconnecting=!1,this.backoff.reset(),this.updateSocketIds(),this.emitAll("reconnect",t)}},function(t,e,n){function o(t,e,n){this.io=t,this.nsp=e,this.json=this,this.ids=0,this.acks={},this.receiveBuffer=[],this.sendBuffer=[],this.connected=!1,this.disconnected=!0,n&&n.query&&(this.query=n.query),this.io.autoConnect&&this.open()}var i=n(1),s=n(4),c=n(13),r=n(5),p=n(6),h=n(0)("socket.io-client:socket"),a=n(14);t.exports=o;var u={connect:1,connect_error:1,connect_timeout:1,connecting:1,disconnect:1,error:1,reconnect:1,reconnect_attempt:1,reconnect_failed:1,reconnect_error:1,reconnecting:1,ping:1,pong:1},f=s.prototype.emit;s(o.prototype),o.prototype.subEvents=function(){if(!this.subs){var t=this.io;this.subs=[r(t,"open",p(this,"onopen")),r(t,"packet",p(this,"onpacket")),r(t,"close",p(this,"onclose"))]}},o.prototype.open=o.prototype.connect=function(){return this.connected?this:(this.subEvents(),this.io.open(),"open"===this.io.readyState&&this.onopen(),this.emit("connecting"),this)},o.prototype.send=function(){var t=c(arguments);return t.unshift("message"),this.emit.apply(this,t),this},o.prototype.emit=function(t){if(u.hasOwnProperty(t))return f.apply(this,arguments),this;var e=c(arguments),n={type:i.EVENT,data:e};return n.options={},n.options.compress=!this.flags||!1!==this.flags.compress,"function"==typeof e[e.length-1]&&(h("emitting packet with ack id %d",this.ids),this.acks[this.ids]=e.pop(),n.id=this.ids++),this.connected?this.packet(n):this.sendBuffer.push(n),delete this.flags,this},o.prototype.packet=function(t){t.nsp=this.nsp,this.io.packet(t)},o.prototype.onopen=function(){if(h("transport is open - connecting"),"/"!==this.nsp)if(this.query){var t="object"==typeof this.query?a.encode(this.query):this.query;h("sending connect packet with query %s",t),this.packet({type:i.CONNECT,query:t})}else this.packet({type:i.CONNECT})},o.prototype.onclose=function(t){h("close (%s)",t),this.connected=!1,this.disconnected=!0,delete this.id,this.emit("disconnect",t)},o.prototype.onpacket=function(t){if(t.nsp===this.nsp)switch(t.type){case i.CONNECT:this.onconnect();break;case i.EVENT:case i.BINARY_EVENT:this.onevent(t);break;case i.ACK:case i.BINARY_ACK:this.onack(t);break;case i.DISCONNECT:this.ondisconnect();break;case i.ERROR:this.emit("error",t.data)}},o.prototype.onevent=function(t){var e=t.data||[];h("emitting event %j",e),null!=t.id&&(h("attaching ack callback to event"),e.push(this.ack(t.id))),this.connected?f.apply(this,e):this.receiveBuffer.push(e)},o.prototype.ack=function(t){var e=this,n=!1;return function(){if(!n){n=!0;var o=c(arguments);h("sending ack %j",o),e.packet({type:i.ACK,id:t,data:o})}}},o.prototype.onack=function(t){var e=this.acks[t.id];"function"==typeof e?(h("calling ack %s with %j",t.id,t.data),e.apply(this,t.data),delete this.acks[t.id]):h("bad ack %s",t.id)},o.prototype.onconnect=function(){this.connected=!0,this.disconnected=!1,this.emit("connect"),this.emitBuffered()},o.prototype.emitBuffered=function(){var t;for(t=0;t<this.receiveBuffer.length;t++)f.apply(this,this.receiveBuffer[t]);for(this.receiveBuffer=[],t=0;t<this.sendBuffer.length;t++)this.packet(this.sendBuffer[t]);this.sendBuffer=[]},o.prototype.ondisconnect=function(){h("server disconnect (%s)",this.nsp),this.destroy(),this.onclose("io server disconnect")},o.prototype.destroy=function(){if(this.subs){for(var t=0;t<this.subs.length;t++)this.subs[t].destroy();this.subs=null}this.io.destroy(this)},o.prototype.close=o.prototype.disconnect=function(){return this.connected&&(h("performing disconnect (%s)",this.nsp),this.packet({type:i.DISCONNECT})),this.destroy(),this.connected&&this.onclose("io client disconnect"),this},o.prototype.compress=function(t){return this.flags=this.flags||{},this.flags.compress=t,this}},function(t,e){t.exports=require("component-emitter")},function(t,e){t.exports=function(t,e,n){return t.on(e,n),{destroy:function(){t.removeListener(e,n)}}}},function(t,e){t.exports=require("component-bind")},function(t,e,n){t.exports=n(8)},function(t,e,n){function o(t,e){"object"==typeof t&&(e=t,t=void 0),e=e||{};var n,o=i(t),s=o.source,h=o.id,a=o.path,u=p[h]&&a in p[h].nsps;return e.forceNew||e["force new connection"]||!1===e.multiplex||u?(r("ignoring socket cache for %s",s),n=c(s,e)):(p[h]||(r("new io instance for %s",s),p[h]=c(s,e)),n=p[h]),o.query&&!e.query&&(e.query=o.query),n.socket(o.path,e)}var i=n(9),s=n(1),c=n(2),r=n(0)("socket.io-client");t.exports=e=o;var p=e.managers={};e.protocol=s.protocol,e.connect=o,e.Manager=n(2),e.Socket=n(3)},function(t,e,n){(function(e){var o=n(11),i=n(0)("socket.io-client:url");t.exports=function(t,n){var s=t;n=n||e.location,null==t&&(t=n.protocol+"//"+n.host),"string"==typeof t&&("/"===t.charAt(0)&&(t="/"===t.charAt(1)?n.protocol+t:n.host+t),/^(https?|wss?):\/\//.test(t)||(i("protocol-less url %s",t),t=void 0!==n?n.protocol+"//"+t:"https://"+t),i("parse %s",t),s=o(t)),s.port||(/^(http|ws)$/.test(s.protocol)?s.port="80":/^(http|ws)s$/.test(s.protocol)&&(s.port="443")),s.path=s.path||"/";var c=-1!==s.host.indexOf(":")?"["+s.host+"]":s.host;return s.id=s.protocol+"://"+c+":"+s.port,s.href=s.protocol+"://"+c+(n&&n.port===s.port?"":":"+s.port),s}}).call(e,n(10))},function(t,e){var n;n=function(){return this}();try{n=n||Function("return this")()||(0,eval)("this")}catch(t){"object"==typeof window&&(n=window)}t.exports=n},function(t,e){t.exports=require("parseuri")},function(t,e){t.exports=require("engine.io-mp-client")},function(t,e){t.exports=require("to-array")},function(t,e){t.exports=require("parseqs")},function(t,e){t.exports=require("indexof")},function(t,e){t.exports=require("backo2")}]);