-
Notifications
You must be signed in to change notification settings - Fork 16
/
bundle.js
1 lines (1 loc) · 18.1 KB
/
bundle.js
1
(function(){function b(d,e,g){function a(j,i){if(!e[j]){if(!d[j]){var f="function"==typeof require&&require;if(!i&&f)return f(j,!0);if(h)return h(j,!0);var c=new Error("Cannot find module '"+j+"'");throw c.code="MODULE_NOT_FOUND",c}var k=e[j]={exports:{}};d[j][0].call(k.exports,function(b){var c=d[j][1][b];return a(c||b)},k,k.exports,b,d,e,g)}return e[j].exports}for(var h="function"==typeof require&&require,c=0;c<g.length;c++)a(g[c]);return a}return b})()({1:[function(a){"use strict";function b(a){if("function"!=typeof WeakMap)return null;var c=new WeakMap,d=new WeakMap;return(b=function(a){return a?d:c})(a)}function c(a,c){if(!c&&a&&a.__esModule)return a;if(null===a||"object"!=typeof a&&"function"!=typeof a)return{default:a};var d=b(c);if(d&&d.has(a))return d.get(a);var e={},f=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var g in a)if("default"!=g&&Object.prototype.hasOwnProperty.call(a,g)){var h=f?Object.getOwnPropertyDescriptor(a,g):null;h&&(h.get||h.set)?Object.defineProperty(e,g,h):e[g]=a[g]}return e.default=a,d&&d.set(a,e),e}function d(a){h.innerHTML+=a+"\n"}function e(a){console.log(a),window.alert(a)}function f(a){const b=document.getElementById("input"),c=document.getElementById("sendBtn");c.disabled=!1,c.onclick=function(){const c=b.value;b.value="",a.emit("chat",c),d("Me: "+c)}}var g=c(a("neffos.js")),h=document.getElementById("output");(async function a(){try{const a=await g.dial("/echo",{default:{_OnNamespaceConnected:function(a,b){a.conn.wasReconnected()&&d("re-connected after "+a.conn.reconnectTries.toString()+" trie(s)"),d("connected to namespace: "+b.Namespace),f(a)},_OnNamespaceDisconnect:function(a,b){d("disconnected from namespace: "+b.Namespace)},_OnAnyEvent:function(a,b){g.isSystemEvent(b.Event)||console.log(b)},chat:function(a,b){return"test_reply"==b.Body?g.reply("same namespace-event but new body"):void d(b.Body)}}},{reconnect:5e3});a.connect("default")}catch(a){console.log("try-catch on await neffos.dial."),e(a)}})()},{"neffos.js":2}],2:[function(a,b,c){(function(b,d){(function(){"use strict";function e(a){if("function"!=typeof WeakMap)return null;var b=new WeakMap,c=new WeakMap;return(e=function(a){return a?c:b})(a)}function f(a,b){if(!b&&a&&a.__esModule)return a;if(null===a||"object"!=typeof a&&"function"!=typeof a)return{default:a};var c=e(b);if(c&&c.has(a))return c.get(a);var d={},f=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var g in a)if("default"!=g&&Object.prototype.hasOwnProperty.call(a,g)){var h=f?Object.getOwnPropertyDescriptor(a,g):null;h&&(h.get||h.set)?Object.defineProperty(d,g,h):d[g]=a[g]}return d.default=a,c&&c.set(a,d),d}function g(a){return!(a!=="_OnNamespaceConnect"&&a!=="_OnNamespaceConnected"&&a!=="_OnNamespaceDisconnect"&&a!=="_OnRoomJoin"&&a!=="_OnRoomJoined"&&a!=="_OnRoomLeave"&&a!=="_OnRoomLeft")}function h(a){return!(void 0!==a)||!(null!==a)||(""==a||"string"==typeof a||a instanceof String?0===a.length||""===a:!!(a instanceof Error)&&h(a.message))}function i(a){return JSON.stringify(a)}function j(a){return h(a)?"":a.replace(aa,Y)}function k(a){return h(a)?"":a.replace(ba,X)}function l(a){return new ca(a)}function m(a){return a instanceof ca}function n(a){if(a.IsNative&&h(a.wait))return a.Body;let b=_,c=_,d=a.Body||"";h(a.Err)||(d=a.Err.message,!m(a.Err)&&(b=$)),a.isNoOp&&(c=$);let e=[a.wait||"",j(a.Namespace),j(a.Room),j(a.Event),b,c,""].join(X);if(a.SetBinary){let a=da.encode(e);e=new Uint8Array(a.length+d.length),e.set(a,0),e.set(d,a.length)}else d instanceof Uint8Array&&(d=ea.decode(d,{stream:!1})),e+=d;return e}function o(a,b,c){if(0==c)return[a];var d=a.split(b,c);if(d.length==c){let c=d.join(b)+b;return d.push(a.substr(c.length)),d}return[a]}function p(a,b){var c=new W;if(0==a.length)return c.isInvalid=!0,c;var d,e=a instanceof ArrayBuffer;if(e){const b=new Uint8Array(a);let e=1,g=0;for(var f=0;f<b.length&&!(b[f]==fa&&(e++,g=f,e==Z));f++);if(e!=Z)return c.isInvalid=!0,c;d=o(ea.decode(b.slice(0,g),{stream:!1}),X,5),d.push(a.slice(g+1,a.length)),c.SetBinary=!0}else d=o(a,X,6);if(d.length!=Z)return b?(c.Event=Q,c.Body=a):c.isInvalid=!0,c;c.wait=d[0],c.Namespace=k(d[1]),c.Room=k(d[2]),c.Event=k(d[3]),c.isError=d[4]==$||!1,c.isNoOp=d[5]==$||!1;var g=d[6];return h(g)?c.Body="":c.isError?c.Err=new Error(g):c.Body=g,c.isInvalid=!1,c.IsForced=!1,c.IsLocal=!1,c.IsNative=b&&c.Event==Q||!1,c}function q(){if(!F){let a=b.hrtime();return V+1e9*a[0]+a[1]}else{let a=window.performance.now();return V+a.toString()}}function r(a){return U+a}function s(a){return a+X.repeat(6)}function t(a,b){return a.events.has(b.Event)?a.events.get(b.Event)(a,b):a.events.has(P)?a.events.get(P)(a,b):null}function u(a){return null===a||a===void 0||"undefined"==typeof a}function v(a,b){if(u(a))return u(b)||b("connHandler is empty."),null;let c=new Map,d=new Map,e=0;if(Object.keys(a).forEach(function(b,f){e++;let g=a[b];if(g instanceof Function)d.set(b,g);else if(g instanceof Map)c.set(b,g);else{let a=new Map;Object.keys(g).forEach(function(b,c){a.set(b,g[b])}),c.set(b,a)}}),0<d.size){if(e!=d.size)return u(b)||b("all keys of connHandler should be events, mix of namespaces and event callbacks is not supported "+d.size+" vs total "+e),null;c.set("",d)}return c}function w(a,b){return a.has(b)?a.get(b):null}function x(a,b){if(u(a))return b;for(let c in a)if(a.hasOwnProperty(c)){let d=a[c];c=encodeURIComponent(ia+c),d=encodeURIComponent(d);const e=c+"="+d;b=-1==b.indexOf("?")?-1==b.indexOf("#")?b+"?"+e:b.split("#")[0]+"?"+e+"#"+b.split("#")[1]:b.split("?")[0]+"?"+e+"&"+b.split("?")[1]}return b}function y(a,b,c){return z(a,b,0,c)}function z(a,b,c,d){if(F&&0==a.indexOf("/")){const b="https:"==document.location.protocol?"wss":"ws",c=document.location.port?":"+document.location.port:"";a=b+"://"+document.location.hostname+c+a}return-1==a.indexOf("ws")&&(a="ws://"+a),new Promise((e,f)=>{H||f("WebSocket is not accessible through this browser.");let g=v(b,f);if(u(g))return;u(d)&&(d={}),u(d.headers)&&(d.headers={});const i=d.reconnect?d.reconnect:0;0<c&&0<i?d.headers[ja]=c.toString():!u(d.headers[ja])&&delete d.headers[ja];const j=A(a,d);let k=new pa(j,g);k.reconnectTries=c,j.binaryType="arraybuffer",j.onmessage=a=>{let b=k.handle(a);return h(b)?void(k.isAcknowledged()&&e(k)):void f(b)},j.onopen=a=>{j.send(R)},j.onerror=a=>{k.close(),f(a)},j.onclose=c=>{if(k.isClosed());else{if(j.onmessage=void 0,j.onopen=void 0,j.onerror=void 0,j.onclose=void 0,0>=i)return k.close(),null;let c=new Map;k.connectedNamespaces.forEach((a,b)=>{let d=[];!u(a.rooms)&&0<a.rooms.size&&a.rooms.forEach((a,b)=>{d.push(b)}),c.set(b,d)}),k.close(),B(a,i,g=>{z(a,b,g,d).then(a=>u(e)||"function () { [native code] }"==e.toString()?void c.forEach((b,c)=>{let d=a=>b=>{a.forEach(a=>{b.joinRoom(a)})};a.connect(c).then(d(b))}):void e(a)).catch(f)})}return null}})}function A(a,b){return F&&!u(b)?(b.headers&&(a=x(b.headers,a)),b.protocols?new H(a,b.protocols):new H(a)):new H(a,b)}function B(a,b,c){let d=a.replace(/(ws)(s)?\:\/\//,"http$2://"),e=1;const f={method:"HEAD",mode:"no-cors"};let g=()=>{G(d,f).then(()=>{c(e)}).catch(()=>{e++,setTimeout(()=>{g()},b)})};setTimeout(g,b)}function C(a){return!(!a||h(a.message))&&0<=a.message.indexOf("[-1] write closed")}Object.defineProperty(c,"__esModule",{value:!0}),c.Room=c.OnRoomLeft=c.OnRoomLeave=c.OnRoomJoined=c.OnRoomJoin=c.OnNativeMessage=c.OnNamespaceDisconnect=c.OnNamespaceConnected=c.OnNamespaceConnect=c.OnAnyEvent=c.NSConn=c.Message=c.ErrWrite=c.ErrInvalidPayload=c.ErrClosed=c.ErrBadRoom=c.ErrBadNamespace=c.Conn=void 0,c.dial=y,c.isCloseError=C,c.isSystemEvent=g,c.marshal=i,c.reply=l;var D=f(a("ws")),E=function(a,b,c,d){function e(a){return a instanceof c?a:new c(function(b){b(a)})}return new(c||(c=Promise))(function(c,f){function g(a){try{i(d.next(a))}catch(a){f(a)}}function h(a){try{i(d["throw"](a))}catch(a){f(a)}}function i(a){a.done?c(a.value):e(a.value).then(g,h)}i((d=d.apply(a,b||[])).next())})};const F="undefined"!=typeof window;var G="undefined"==typeof fetch?void 0:fetch,H="undefined"==typeof H?void 0:H;H=F?window.WebSocket:D.WebSocket;const I="_OnNamespaceConnect";c.OnNamespaceConnect="_OnNamespaceConnect";const J="_OnNamespaceConnected";c.OnNamespaceConnected="_OnNamespaceConnected";const K="_OnNamespaceDisconnect";c.OnNamespaceDisconnect="_OnNamespaceDisconnect";const L="_OnRoomJoin";c.OnRoomJoin="_OnRoomJoin";const M="_OnRoomJoined";c.OnRoomJoined="_OnRoomJoined";const N="_OnRoomLeave";c.OnRoomLeave="_OnRoomLeave";const O="_OnRoomLeft";c.OnRoomLeft="_OnRoomLeft";const P="_OnAnyEvent";c.OnAnyEvent="_OnAnyEvent";const Q="_OnNativeMessage";c.OnNativeMessage="_OnNativeMessage";const R="M",S="A",T="H",U="#",V="$";class W{isConnect(){return this.Event==I||!1}isDisconnect(){return this.Event==K||!1}isRoomJoin(){return this.Event==L||!1}isRoomLeft(){return this.Event==O||!1}isWait(){return!h(this.wait)&&(!(this.wait[0]!=U)||this.wait[0]==V||!1)}unmarshal(){return JSON.parse(this.Body)}}c.Message=W;const X=";",Y="@%!semicolon@%!",Z=7,$="1",_="0",aa=/;/g,ba=new RegExp(Y,"g");class ca extends Error{constructor(a){super(a),this.name="replyError",Error.captureStackTrace(this,ca),Object.setPrototypeOf(this,ca.prototype)}}var da=new TextEncoder,ea=new TextDecoder("utf-8"),fa=";".charCodeAt(0);class ga{constructor(a,b){this.nsConn=a,this.name=b}emit(a,b){let c=new W;return c.Namespace=this.nsConn.namespace,c.Room=this.name,c.Event=a,c.Body=b,this.nsConn.conn.write(c)}leave(){let a=new W;return a.Namespace=this.nsConn.namespace,a.Room=this.name,a.Event=N,this.nsConn.askRoomLeave(a)}}c.Room=ga;class ha{constructor(a,b,c){this.conn=a,this.namespace=b,this.events=c,this.rooms=new Map}emit(a,b){let c=new W;return c.Namespace=this.namespace,c.Event=a,c.Body=b,this.conn.write(c)}emitBinary(a,b){let c=new W;return c.Namespace=this.namespace,c.Event=a,c.Body=b,c.SetBinary=!0,this.conn.write(c)}ask(a,b){let c=new W;return c.Namespace=this.namespace,c.Event=a,c.Body=b,this.conn.ask(c)}joinRoom(a){return E(this,void 0,void 0,function*(){return yield this.askRoomJoin(a)})}room(a){return this.rooms.get(a)}leaveAll(){return E(this,void 0,void 0,function*(){let a=new W;return a.Namespace=this.namespace,a.Event=O,a.IsLocal=!0,this.rooms.forEach((b,c)=>E(this,void 0,void 0,function*(){a.Room=c;try{yield this.askRoomLeave(a)}catch(a){return a}})),null})}forceLeaveAll(a){let b=new W;b.Namespace=this.namespace,b.Event=N,b.IsForced=!0,b.IsLocal=a,this.rooms.forEach((a,c)=>{b.Room=c,t(this,b),this.rooms.delete(c),b.Event=O,t(this,b),b.Event=N})}disconnect(){let a=new W;return a.Namespace=this.namespace,a.Event=K,this.conn.askDisconnect(a)}askRoomJoin(a){return new Promise((b,c)=>E(this,void 0,void 0,function*(){let d=this.rooms.get(a);if(void 0!==d)return void b(d);let e=new W;e.Namespace=this.namespace,e.Room=a,e.Event=L,e.IsLocal=!0;try{yield this.conn.ask(e)}catch(a){return void c(a)}let f=t(this,e);return h(f)?void(d=new ga(this,a),this.rooms.set(a,d),e.Event=M,t(this,e),b(d)):void c(f)}))}askRoomLeave(a){return E(this,void 0,void 0,function*(){if(!this.rooms.has(a.Room))return ma;try{yield this.conn.ask(a)}catch(a){return a}let b=t(this,a);return h(b)?(this.rooms.delete(a.Room),a.Event=O,t(this,a),null):b})}replyRoomJoin(a){if(!(h(a.wait)||a.isNoOp)){if(!this.rooms.has(a.Room)){let b=t(this,a);if(!h(b))return a.Err=b,void this.conn.write(a);this.rooms.set(a.Room,new ga(this,a.Room)),a.Event=M,t(this,a)}this.conn.writeEmptyReply(a.wait)}}replyRoomLeave(a){return h(a.wait)||a.isNoOp?void 0:this.rooms.has(a.Room)?void(t(this,a),this.rooms.delete(a.Room),this.conn.writeEmptyReply(a.wait),a.Event=O,t(this,a)):void this.conn.writeEmptyReply(a.wait)}}c.NSConn=ha;const ia="X-Websocket-Header-",ja="X-Websocket-Reconnect",ka=new Error("invalid payload");c.ErrInvalidPayload=ka;const la=new Error("bad namespace");c.ErrBadNamespace=la;const ma=new Error("bad room");c.ErrBadRoom=ma;const na=new Error("use of closed connection");c.ErrClosed=na;const oa=new Error("write closed");c.ErrWrite=oa;class pa{constructor(a,b){this.conn=a,this.reconnectTries=0,this._isAcknowledged=!1,this.namespaces=b;let c=b.has("");this.allowNativeMessages=c&&b.get("").has(Q),this.queue=[],this.waitingMessages=new Map,this.connectedNamespaces=new Map,this.closed=!1}wasReconnected(){return 0<this.reconnectTries}isAcknowledged(){return this._isAcknowledged}handle(a){if(!this._isAcknowledged){let b=this.handleAck(a.data);return null==b?(this._isAcknowledged=!0,this.handleQueue()):this.conn.close(),b}return this.handleMessage(a.data)}handleAck(a){let b=a[0];switch(b){case S:let b=a.slice(1);this.ID=b;break;case T:let c=a.slice(1);return new Error(c);default:return this.queue.push(a),null;}}handleQueue(){null==this.queue||0==this.queue.length||this.queue.forEach((a,b)=>{this.queue.splice(b,1),this.handleMessage(a)})}handleMessage(a){let b=p(a,this.allowNativeMessages);if(b.isInvalid)return ka;if(b.IsNative&&this.allowNativeMessages){let a=this.namespace("");return t(a,b)}if(b.isWait()){let a=this.waitingMessages.get(b.wait);if(null!=a)return void a(b)}const c=this.namespace(b.Namespace);switch(b.Event){case I:this.replyConnect(b);break;case K:this.replyDisconnect(b);break;case L:if(c!==void 0){c.replyRoomJoin(b);break}case N:if(c!==void 0){c.replyRoomLeave(b);break}default:if(c===void 0)return la;b.IsLocal=!1;const a=t(c,b);if(!h(a))return b.Err=a,this.write(b),a;}return null}connect(a){return this.askConnect(a)}waitServerConnect(a){return u(this.waitServerConnectNotifiers)&&(this.waitServerConnectNotifiers=new Map),new Promise((b,c)=>E(this,void 0,void 0,function*(){this.waitServerConnectNotifiers.set(a,()=>{this.waitServerConnectNotifiers.delete(a),b(this.namespace(a))})}))}namespace(a){return this.connectedNamespaces.get(a)}replyConnect(a){if(h(a.wait)||a.isNoOp)return;let b=this.namespace(a.Namespace);if(void 0!==b)return void this.writeEmptyReply(a.wait);let c=w(this.namespaces,a.Namespace);return u(c)?(a.Err=la,void this.write(a)):void(b=new ha(this,a.Namespace,c),this.connectedNamespaces.set(a.Namespace,b),this.writeEmptyReply(a.wait),a.Event=J,t(b,a),!u(this.waitServerConnectNotifiers)&&0<this.waitServerConnectNotifiers.size&&this.waitServerConnectNotifiers.has(a.Namespace)&&this.waitServerConnectNotifiers.get(a.Namespace)())}replyDisconnect(a){if(!(h(a.wait)||a.isNoOp)){let b=this.namespace(a.Namespace);return void 0===b?void this.writeEmptyReply(a.wait):void(b.forceLeaveAll(!0),this.connectedNamespaces.delete(a.Namespace),this.writeEmptyReply(a.wait),t(b,a))}}ask(a){return new Promise((b,c)=>this.isClosed()?void c(na):(a.wait=q(),this.waitingMessages.set(a.wait,a=>a.isError?void c(a.Err):void b(a)),!this.write(a))?void c(oa):void 0)}askConnect(a){return new Promise((b,c)=>E(this,void 0,void 0,function*(){let d=this.namespace(a);if(void 0!==d)return void b(d);let e=w(this.namespaces,a);if(u(e))return void c(la);let f=new W;f.Namespace=a,f.Event=I,f.IsLocal=!0,d=new ha(this,a,e);let g=t(d,f);if(!h(g))return void c(g);try{yield this.ask(f)}catch(a){return void c(a)}this.connectedNamespaces.set(a,d),f.Event=J,t(d,f),b(d)}))}askDisconnect(a){return E(this,void 0,void 0,function*(){let b=this.namespace(a.Namespace);if(void 0===b)return la;try{yield this.ask(a)}catch(a){return a}return b.forceLeaveAll(!0),this.connectedNamespaces.delete(a.Namespace),a.IsLocal=!0,t(b,a)})}isClosed(){return this.closed}write(a){if(this.isClosed())return!1;if(!a.isConnect()&&!a.isDisconnect()){let b=this.namespace(a.Namespace);if(void 0===b)return!1;if(!h(a.Room)&&!a.isRoomJoin()&&!a.isRoomLeft()&&!b.rooms.has(a.Room))return!1}return this.conn.send(n(a)),!0}writeEmptyReply(a){this.conn.send(s(a))}close(){if(this.closed)return;let a=new W;a.Event=K,a.IsForced=!0,a.IsLocal=!0,this.connectedNamespaces.forEach(b=>{b.forceLeaveAll(!0),a.Namespace=b.namespace,t(b,a),this.connectedNamespaces.delete(b.namespace)}),this.waitingMessages.clear(),this.closed=!0,this.conn.readyState===this.conn.OPEN&&this.conn.close()}}c.Conn=pa;const qa={dial:y,isSystemEvent:g,OnNamespaceConnect:"_OnNamespaceConnect",OnNamespaceConnected:"_OnNamespaceConnected",OnNamespaceDisconnect:"_OnNamespaceDisconnect",OnRoomJoin:"_OnRoomJoin",OnRoomJoined:"_OnRoomJoined",OnRoomLeave:"_OnRoomLeave",OnRoomLeft:"_OnRoomLeft",OnAnyEvent:"_OnAnyEvent",OnNativeMessage:"_OnNativeMessage",Message:W,Room:ga,NSConn:ha,Conn:pa,ErrInvalidPayload:ka,ErrBadNamespace:la,ErrBadRoom:ma,ErrClosed:na,ErrWrite:oa,isCloseError:C,reply:l,marshal:i};var ra="object"==typeof self&&self.self===self&&self||"object"==typeof d&&d.global===d&&d;ra.neffos=qa}).call(this)}).call(this,a("_process"),"undefined"==typeof global?"undefined"==typeof self?"undefined"==typeof window?{}:window:self:global)},{_process:3,ws:4}],3:[function(a,b,c){function d(){throw new Error("setTimeout has not been defined")}function f(){throw new Error("clearTimeout has not been defined")}function g(a){if(m===setTimeout)return setTimeout(a,0);if((m===d||!m)&&setTimeout)return m=setTimeout,setTimeout(a,0);try{return m(a,0)}catch(b){try{return m.call(null,a,0)}catch(b){return m.call(this,a,0)}}}function h(a){if(n===clearTimeout)return clearTimeout(a);if((n===f||!n)&&clearTimeout)return n=clearTimeout,clearTimeout(a);try{return n(a)}catch(b){try{return n.call(null,a)}catch(b){return n.call(this,a)}}}function i(){r&&p&&(r=!1,p.length?q=p.concat(q):s=-1,q.length&&j())}function j(){if(!r){var a=g(i);r=!0;for(var b=q.length;b;){for(p=q,q=[];++s<b;)p&&p[s].run();s=-1,b=q.length}p=null,r=!1,h(a)}}function k(a,b){this.fun=a,this.array=b}function l(){}var m,n,o=b.exports={};(function(){try{m="function"==typeof setTimeout?setTimeout:d}catch(a){m=d}try{n="function"==typeof clearTimeout?clearTimeout:f}catch(a){n=f}})();var p,q=[],r=!1,s=-1;o.nextTick=function(a){var b=Array(arguments.length-1);if(1<arguments.length)for(var c=1;c<arguments.length;c++)b[c-1]=arguments[c];q.push(new k(a,b)),1!==q.length||r||g(j)},k.prototype.run=function(){this.fun.apply(null,this.array)},o.title="browser",o.browser=!0,o.env={},o.argv=[],o.version="",o.versions={},o.on=l,o.addListener=l,o.once=l,o.off=l,o.removeListener=l,o.removeAllListeners=l,o.emit=l,o.prependListener=l,o.prependOnceListener=l,o.listeners=function(a){return[]},o.binding=function(a){throw new Error("process.binding is not supported")},o.cwd=function(){return"/"},o.chdir=function(a){throw new Error("process.chdir is not supported")},o.umask=function(){return 0}},{}],4:[function(a,b,c){"use strict";b.exports=function(){throw new Error("ws does not work in the browser. Browser clients must use the native WebSocket object")}},{}]},{},[1]);