text: (function(){'use strict';const a="object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global||{},b=["ws"],c=(()=>{const c=a.WebSocket;if(void 0!==c)return c;for(const a of b)try{return require(a)}catch(a){}})(),d=(()=>{if(void 0===c)return;let a=c.Server;if(void 0!==a)return a;for(const c of b)try{if(a=require(c).Server,void 0!==a)return a}catch(a){}})(),e=(()=>{try{return require("weak")}catch(b){return a.weak}})(),f=function(a,b){return Promise.resolve(a).then(a=>Promise.resolve(a._enter()).then(c=>{let d;try{d=b(c)}catch(b){return Promise.resolve(a._exit(b)).then(a=>{if(!a)throw b})}return Promise.resolve(d).then(b=>Promise.resolve(a._exit()).then(()=>b),b=>Promise.resolve(a._exit(b)).then(a=>{if(!a)throw b}))}))},g=void 0===Symbol.asyncIterator?Symbol("Symbol.asyncIterator"):Symbol.asyncIterator,h="__*__",i=Object.create(null);["AttributeError","CancelledError","DisconnectedError","IndexError","LookupError","RuntimeError"].forEach(a=>{class b extends Error{}b.prototype.name=a,i[a]=b}),i.Exception=Error;const j=40,k=30,l=20,m=10,n={[40]:"error",[k]:"warning",[l]:"info",[m]:"debug"},o=function(a){return"_"==a.substring(0,1)||"constructor"==a},p=Object.prototype.hasOwnProperty;class q{constructor(a,b,c,d=!0){this.name=a,this.encode=b,this.decode=c,this.inband=d,q.registry[a]=this}static byname(a){if(null===a)return null;const b=q.registry[a];if(void 0===b)throw new i.LookupError("Unknown format: "+a);return b}}q.registry=Object.create(null),new q("json",(a,b)=>{return JSON.stringify(b,function(b,c){switch(typeof c){case"boolean":case"number":case"string":return c;}if(void 0===c||null===c)return c;if("function"==typeof c._marshal)return c._marshal();if(Array.isArray(c))return c;const d=Object.getPrototypeOf(c).constructor;return[Object,Boolean,Number,String].includes(d)?c:a.lsessions[-1].marshal(c,"function"==typeof c?"call":null)})},JSON.parse);const r=(()=>{try{return require("msgpack-lite")}catch(b){return a.msgpack}})();if(void 0!==r){const a=r.decode([212,0,0]).constructor,b=r.createCodec({binarraybuffer:!0}),c={codec:b};b.addExtUnpacker(0,a=>new s(r.decode(a,c))),new q("msgpack",(b,d)=>{const e=function(c){switch(typeof c){case"boolean":case"number":case"string":return c;}if(void 0===c||null===c)return c;if("function"==typeof c._marshal)return new a(r.encode(c._marshal()),0);if(Array.isArray(c))return c.map(e);if(c instanceof ArrayBuffer||"[object ArrayBuffer]"==={}.toString.call(c))return c;if(ArrayBuffer.isView(c))return c.buffer;const d=Object.getPrototypeOf(c).constructor;return[Boolean,Number,String].includes(d)?c.valueOf():d===Object?Object.keys(c).reduce((a,b)=>{const d=e(c[b]);return void 0!==d&&(a[b]=d),a},{}):new a(r.encode(b.lsessions[-1].marshal(c,"function"==typeof c?"call":null)),0)};return r.encode(e(d),c)},a=>r.decode(a,c),!1)}class s{constructor(a){this.ref=a}_marshal(){return this.ref}}class t{constructor(a,b=null){this.conn=a,this.lcodec=q.byname(b)}unmarshalAll(a,b,c=null){return a.inband?this.unmarshalAllInBand(b,c):this.unmarshalAllOutOfBand(b,c)}unmarshalAllInBand(a,b=null){switch(typeof a){case"boolean":case"number":case"string":return a;}return null===a?a:Array.isArray(a)?a.map(a=>this.unmarshalAllInBand(a,b)):p.call(a,h)?this.unmarshal(a,b):Object.keys(a).reduce((c,d)=>(c[d]=this.unmarshalAllInBand(a[d],b),c),{})}unmarshalAllOutOfBand(a,b=null){switch(typeof a){case"boolean":case"number":case"string":return a;}return null===a?a:Array.isArray(a)?a.map(a=>this.unmarshalAllOutOfBand(a,b)):a instanceof s?this.unmarshal(a.ref,b):a instanceof ArrayBuffer||"[object ArrayBuffer]"==={}.toString.call(a)?a:Object.keys(a).reduce((c,d)=>(c[d]=this.unmarshalAllOutOfBand(a[d],b),c),{})}unmarshal(b,c){const d=this.unmarshalObj(b,c);if(!p.call(b,"method"))return d;const e=b.method;if(o(e))throw new i.AttributeError("Cannot access private attribute '"+e+"' of '"+d.constructor.name+"' object");const f=d[e];if(void 0===f)throw new i.AttributeError("'"+d.constructor.name+"' object has no attribute '"+e+"'");if(f===Object.prototype[e])throw new i.AttributeError("Cannot access forbidden attribute '"+e+"' of '"+d.constructor.name+"' object");return f.bind(d)}_enter(){return this.root()}_exit(a){return this.close()}}class u extends t{constructor(a,b,c=null,d=null){if(b in a.lsessions)throw new i.RuntimeError("Session ID in use: "+b);super(a,d),this.lsid=b,this.nextloid=0,this.objects=Object.create(null),a.lsessions[b]=this;try{this.objects[null]=(c||a.rootFactory)(this)}catch(c){throw delete a.lsessions[b],c}}root(){return this.objects[null]}add(a){const b=this.nextloid++;return this.objects[b]=a,b}unmarshalObj(a,b){const c=a[h];if(p.call(a,"lsid")){const d=new D(this.conn,a.lsid,null,b);return d.lcodec=this.lcodec,d.unmarshalId(c)}const d=this.conn.unmarshalLsession(a.rsid);return d.unmarshalId(c)}unmarshalId(a){const b=this.objects[a];if(void 0===b)throw new i.LookupError("Unknown object: "+a);return b}createBridge(a,b="json",c="json"){return a.createSession(null,c).then(a=>new H(this,a,b))}close(){this.root().release()}destroy(){Object.keys(this.objects).forEach(a=>{this.objects[a]._close()}),delete this.conn.lsessions[this.lsid]}free(a){const b=this.unmarshalId(a);b.release()}}class v extends u{marshal(a,b=null){const c=this.nextloid++;this.objects[c]=a;const d={[h]:c,lsid:this.lsid};return null!==b&&(d.method=b),d}unmarshalId(a){const b=super.unmarshalId(a);return"function"==typeof b?{call:{bind:()=>b}}:b}destroy(){delete this.conn.lsessions[this.lsid]}free(a){if(null===a)return super.free(null);if(!(a in this.objects))throw new i.LookupError("Unknown object: "+a);delete this.objects[a]}}class w{constructor(a,b){return this._lsession=a,this._loid=b,this._lref={lsid:a.lsid,[h]:b},new Proxy(this,{get:(a,b)=>{const c=a[b];return"function"==typeof c?new Proxy(c,{get:(a,d)=>{const e=a[d];return"bind"===d?function(a,...d){const f=e.call(this,a,...d);return f._marshal=()=>(++a._nref,{lsid:a._lsession.lsid,[h]:a._loid,method:b}),f.help=c.help,f}:e}}):c}})}_marshal(){return++this._nref,this._lref}addref(){++this._nref}release(){0>=--this._nref&&this._release()}help(a=null){return null===a?this.constructor.help||null:a.help||null}dir(){let a=this,b=[];for(;a!==Object.prototype;)b=b.concat(Object.getOwnPropertyNames(a)),a=Object.getPrototypeOf(a);return b.sort().filter((a,b,c)=>a!==c[b+1]&&!o(a)&&"function"==typeof this[a])}taxa(){let a=Object.getPrototypeOf(this);const b=[];for(;null!==a&&a.constructor!==x&&a.constructor!==z;)b.push(a.constructor.name),a=Object.getPrototypeOf(a);return b}signature(a){const b=[];for(let c=0;c<a.length;++c)b.push(["abcdefghijklmnopqrstuvwxyz".charAt(c),null]);return b.push(["*args",null]),{params:b,defaults:{},return:null}}_release(){delete this._lsession.objects[this._loid],this._close()}_close(){}_enter(){return++this._nref,this}_exit(a){this.release()}}w.prototype.addref.help="Increment the object's reference count.",w.prototype.release.help="Decrement the object's reference count.  When the reference count reaches zero,\nit will be removed from memory.",w.prototype.help.help="Get documentation for the object or one of its methods.",w.prototype.dir.help="Get a list of names of the object's methods.",w.prototype.taxa.help="Get a list of names of the object's base classes.",w.prototype.signature.help="Get method type signature.";class x extends w{constructor(a){super(a,null),this._nref=1}_close(){this._lsession.destroy()}static setNewables(a,b){b.forEach(b=>{let c;p.call(b,"_new")?c=b._new:(c=(...a)=>new b(...a),c.help=b.help),(a.prototype["new_"+b.name]=function(...a){return c(this._lsession,...a)}).help=c.help})}}class y extends x{open(a,b=null){this._lsession.conn.createLocalSession(a,null,b)}free(a,b){const c=this._lsession.conn.unmarshalLsession(a);c.free(b)}}y.prototype.open.help="Open a new session.",y.prototype.free.help="Release the specified object, which may be a native object.";class z extends w{constructor(a){const b=a.nextloid++;super(a,b),a.objects[b]=this,this._nref=0}}const A=a=>new Promise((b,c)=>{a.closed?b():a.rsession.closed()||a.rsession.conn.closed()?(a.closed=!0,b()):(a.closed=!0,a.rsession.call(null,"free",[a.rref.rsid,a.rref[h]]).then(b,a=>{a instanceof i.LookupError||a instanceof i.DisconnectedError?b():c(a)}))});class B{constructor(a,b){const c={rsession:a,rref:{rsid:a.rsid,[h]:b},closed:!1};return this._rdata=c,void 0!==e&&e(this,A.bind(void 0,c)),new Proxy(this,{get:(b,c)=>{const d=b[c];if(void 0!==d)return d;if("string"==typeof c&&"inspect"!==c&&"then"!==c&&"toJSON"!==c){const b=function(...b){return a.call(this,c,b)};return b.bind=function(a,...b){const d=Function.prototype.bind.call(this,a,...b);return d._marshal=()=>({rsid:a._rdata.rref.rsid,[h]:a._rdata.rref[h],method:c}),d.close=()=>a._close(),d._enter=()=>d,d._exit=()=>d.close(),d.help=()=>a.help(d),d.signature=()=>a.signature(d),d},b}}})}_marshal(){return this._rdata.rref}_close(){return A(this._rdata)}_enter(){return this}_exit(a){return this._close()}[g](){return new C(this)}}class C{constructor(a){this.robj=a,this.iter=-1}[g](){return this}next(){const a=this.iter;if(null===a)return Promise.resolve({value:void 0,done:!0});const b=a=>this.robj.get(a).then(b=>(this.iter=a+1,{value:b,done:!1}),a=>{if(a instanceof i.IndexError)return this.iter=null,{value:void 0,done:!0};throw this.iter=null,a}),c=a=>a.next().then(b=>b.done?(this.iter=null,a._close(),{value:void 0,done:!0}):{value:b.value,done:b.done},b=>{throw this.iter=null,a._close(),b});return-1===a?this.robj.iter().then(a=>(this.iter=a,c(a)),a=>{if(a instanceof i.AttributeError)return this.iter=0,b(0);throw this.iter=null,a}):"number"==typeof a?b(a):c(a)}close(){const a=this.iter;if(this.iter=null,a instanceof B)return a._close()}_enter(){return this}_exit(a){return this.close()}}class D extends t{constructor(a,b,c=null,d=null){super(a,c),this.rsid=b,this.dstid=d}call(a,b,c=[]){const d=this.conn.nextrcid++;this.conn.sendcall(this.lcodec,this.dstid,d,a,b,c);const e=new Promise((a,b)=>{this.conn.rcalls[d]={rsession:this,resolve:a,reject:b}});return e.cancel=()=>{if(d in this.conn.rcalls){const a=this.conn.rcalls[d];delete this.conn.rcalls[d];const b={cancel:d};null!==this.dstid&&(b.dst=this.dstid),this.conn.send(b),a.reject(new i.CancelledError)}},e}unmarshalObj(a){const b=a[h];if(p.call(a,"rsid")){const c=this.conn.unmarshalLsession(a.rsid);return c.unmarshalId(b)}const c=a.lsid;let d;c===this.rsid?d=this:(d=this.createExternalSession(this.conn,c,null,this.dstid),d.lcodec=this.lcodec);const e=d.unmarshalId(b);return p.call(a,"bridge")?d.unmarshalBridge(e,a.bridge):e}unmarshalBridge(a,b){return new G(a,b)}unmarshalId(a){return new B(this,a)}closed(){return!1}createExternalSession(...a){return new D(...a)}}class E extends D{constructor(a,b,c=null,d=null){super(a,b,c,d),this._root=this.unmarshalId(null)}root(){return this._root}closed(){return this._root._rdata._closed}}class F extends E{close(){return this._root._close()}}class G extends E{constructor(a,b){super(a._rdata.rsession.conn,b.rsid,b.lformat,b.dst),this.bridge=a,this._root._closed=!0}close(){return this.bridge._close()}closed(){return this.bridge._rdata._closed}}class H extends z{constructor(a,b,c){super(a),this._rsession=b,this._lref.bridge={dst:b.conn.id,rsid:b.rsid,lformat:c}}_close(){this._rsession.close()}}H.help="Bridge between clients.  Allows one client to call methods exposed by another client.";class I{constructor(){this.objects=Object.create(null),this.nextid=0}add(a){const b=this.nextid++;return this.objects[b]=a,b}get(a){return this.objects[a]}remove(a){delete this.objects[a]}}const J=new I;class K{constructor(a,b={}){if(b=Object.assign(Object.create(null),b),"rootFactory"in b)this.rootFactory=b.rootFactory;else{const a=b.root||x;this.rootFactory=b=>new a(b)}this.eventHandlers={open:[],close:[]};const d=Object.assign(Object.create(null),b.on||{});"open"in d&&this.on("open",d.open),"close"in d&&this.on("close",d.close),this.logfn=b.log||((a,...b)=>{console.log(n[a]||"Unknown",...b)}),this.logLevel=b.logLevel||k,this.lencode=q.registry[b.lformat||"json"].encode,this.rcodec=q.registry[b.rformat||"json"],this.rcodecBin=q.registry[b.rformatBin||"msgpack"],this.lsessions=Object.create(null),this.lcalls=Object.create(null),this.rcalls=Object.create(null),this.bcalls=Object.create(null),this.nextlsid=-2,this.nextrsid=0,this.nextrcid=0,this.header=null,this.headerRcodec=null,new u(this,null,a=>new y(a)),new v(this,-1,a=>new x(a)),this.registry=b.registry||J,this.id=this.registry.add(this);const e="string"==typeof a?new c(a):a;let f;this.opened=new Promise(a=>{f=a}),3===e.readyState?(f(!0),this.ws=null,this.registry.remove(this.id),setTimeout(()=>this.emit("close"),0)):(this.ws=e,1===e.readyState?(f(!0),setTimeout(()=>this.emit("open"),0)):e.onopen=()=>{f(!0),this.emit("open")},e.onclose=()=>{this.ws=null,this.lclose(),this.rclose(),f(!1),this.emit("close")},e.onmessage=a=>{let b=a.data;if(b instanceof ArrayBuffer&&(b=new Uint8Array(b)),this.log(m,"recv",b),null===this.header){const a="string"==typeof b?this.rcodec:this.rcodecBin;let c;try{c=a.decode(b)}catch(a){return this.log(j,"Invalid data received on Eider WebSocket connection:",a),void this.close()}p.call(c,"format")&&null!==c.format?(this.header=c,this.headerRcodec=a):this.dispatch(a,c)}else this.dispatch(this.headerRcodec,this.header,b),this.header=null})}closed(){return null===this.ws||1!==this.ws.readyState}createLocalSession(a=null,b=null,c=null){return null===a&&(a=this.nextlsid--),new u(this,a,b,c)}createSession(a=null,b=null){const c=this.nextrsid++,d=new F(this,c,a);return d.call(null,"open",[c,b]).then(()=>d)}dispatch(a,b,c=null){const d=p.call(b,"dst")?b.dst:null,e=p.call(b,"method")?b.method:null;if(e){const f=p.call(b,"id")?b.id:null;if(null===d){const d=p.call(b,"src")?b.src:null;try{if(o(e))throw new i.AttributeError("Cannot call private method '"+e+"'");let g;null===c?g=b:(a=q.byname(b.format),g=a.decode(c));const[h,j]=this.applyBegin(a,d,e,g),k=h.lcodec;try{const b=this.applyFinish(a,d,e,h,j,g);null!==f&&b&&"function"==typeof b.then&&"function"==typeof b.cancel&&(this.lcalls[f]=b),Promise.resolve(b).then(a=>{null!==f&&(delete this.lcalls[f],this.respond(d,f,a,k))}).catch(a=>{delete this.lcalls[f],this.onError(d,f,a,k)})}catch(a){this.onError(d,f,a,k)}}catch(a){this.onError(d,f,a)}}else this.bridgeCall(d,f,b,c)}else{const e=p.call(b,"cancel")?b.cancel:null;if(!(null!==e)){const e=b.id;if(!(null===d))this.bridgeResponse(d,e,b,c);else if(e in this.rcalls){const d=this.rcalls[e];delete this.rcalls[e];try{let e;null===c?e=b:(a=q.byname(b.format),e=a.decode(c)),d.resolve(this.getresult(a,d.rsession,e))}catch(a){d.reject(a)}}}else if(null!==d)this.bridgeCall(d,null,b,c);else if(e in this.lcalls){const a=this.lcalls[e];delete this.lcalls[e],a.cancel()}}}bridgeCall(a,b,c,d){const e=this.registry.get(a);void 0===e?this.onError(null,b,new i.DisconnectedError("Unknown connection: "+a)):(delete c.dst,c.src=this.id,e.send(c,d),null!==b&&(!(this.id in e.bcalls)&&(e.bcalls[this.id]=Object.create(null)),e.bcalls[this.id][b]=1))}bridgeResponse(a,b,c,d){const e=this.registry.get(a);void 0!==e&&(a in this.bcalls&&delete this.bcalls[a][b],delete c.dst,e.send(c,d))}unmarshalLsession(a){if(!(a in this.lsessions))throw new i.LookupError("Unknown session: "+a);return this.lsessions[a]}applyBegin(a,b,c,d){let e=null,f=null;if(p.call(d,"this")){let a=d.this;if(Array.isArray(a))throw new TypeError("Malformed this object");else a instanceof s&&(a=a.ref),p.call(a,h)&&(e=a[h]),p.call(a,"rsid")&&(f=a.rsid)}return[this.unmarshalLsession(f),e]}applyFinish(b,c,d,e,g,h){const j=e.unmarshalId(g),k=p.call(h,"params")?e.unmarshalAll(b,h.params,c):[];let l=j[d];if(void 0===l){if("set_"==d.substring(0,4)&&1==k.length){const a=d.substring(4);if(o(a))throw new i.AttributeError("Cannot assign to private attribute '"+a+"'");if(l=j[a],void 0!==l){if(l===Object.prototype[a])throw new i.AttributeError("Cannot assign to forbidden attribute '"+a+"' of '"+j.constructor.name+"' object");let b=!0;try{l.bind(j)}catch(a){if(a instanceof TypeError)b=!1;else throw a}if(b)throw new i.AttributeError("Cannot assign to method '"+a+"'")}return void(j[a]=k[0])}throw new i.AttributeError("'"+j.constructor.name+"' object has no attribute '"+d+"'")}if(l===Object.prototype[d])throw new i.AttributeError("Cannot access forbidden attribute '"+d+"' of '"+j.constructor.name+"' object");let m;try{m=l.bind(j)}catch(a){if(a instanceof TypeError&&!k.length)return l;throw a}return m(...k)}getresult(b,c,d){if(p.call(d,"result"))return c.unmarshalAll(b,d.result);if(!p.call(d,"error"))throw new Error("Unspecified error");const e=d.error;let f="";p.call(e,"message")&&(f=""+e.message),""==f&&(f="Unspecified error");let g;if(p.call(e,"name")){const b=e.name;b in i?g=i[b]:(g=a[b],!("function"==typeof g&&(g===Error||g.prototype instanceof Error))&&(g=Error,b&&(f=b+": "+f)))}const h=new g(f);if(p.call(e,"stack")){const a=e.stack;"string"==typeof a&&(p.call(h,"stack")&&"string"==typeof h.stack?h.stack=a.trim()+"\n\nThe above exception was the direct cause of the following exception:\n\n"+h.stack:h.stack=a)}throw h}onError(a,b,c,d=null){null===b?this.log(j,c):(!(c instanceof Error)&&(c=new Error(c)),this.error(a,b,c,d))}close(){null!==this.ws&&(this.ws.close(),this.ws=null,this.registry.remove(this.id))}lclose(){Object.keys(this.lsessions).forEach(a=>{this.lsessions[a].objects[null]._release()})}rclose(){this.registry.remove(this.id),Object.keys(this.lcalls).forEach(a=>{this.lcalls[a].cancel()}),Object.keys(this.rcalls).forEach(a=>{this.rcalls[a].reject(new i.DisconnectedError("Connection lost"))}),Object.keys(this.bcalls).forEach(a=>{const b=this.registry.get(a);void 0!==b&&Object.keys(this.bcalls[a]).forEach(a=>{b.error(null,a,new i.DisconnectedError("Bridged connection lost"))})}),Object.keys(this.registry.objects).forEach(a=>{const b=this.registry.objects[a];if(this.id in b.bcalls){const a=b.bcalls[this.id];delete b.bcalls[this.id],b.closed()||Object.keys(a).forEach(a=>{b.send({cancel:a})})}})}sendcall(a,b,c,d,e,f=[]){if(this.closed())throw new i.DisconnectedError("Connection closed");const g={id:c,method:e};null!==b&&(g.dst=b);let h;null===a?(h=null,null!==d&&(g.this=d),f.length&&(g.params=f)):(h={},null!==d&&(h.this=d),f.length&&(h.params=f),h=a.encode(this,h),g.format=a.name),this.send(g,h)}respond(a,b,c,d){void 0===c&&(c=null);const e={id:b};null!==a&&(e.dst=a);let f;if(null===d)f=null,e.result=c;else{try{f=d.encode(this,{result:c})}catch(c){return void this.error(a,b,c,d)}e.format=d.name}this.send(e,f)}error(a,b,c,d=null){const e={id:b};null!==a&&(e.dst=a);const f={name:c.name,message:c.message};"string"==typeof c.stack&&(f.stack=c.stack);let g;if(null===d)g=null,e.error=f;else try{g=d.encode(this,{error:f}),e.format=d.name}catch(a){g=null,e.error=f}this.send(e,g)}send(a,b=null){this.closed()||(this.sendData(this.lencode(this,a)),null!==b&&this.sendData(b))}sendData(a){this.log(m,"send",a),this.ws.send(a)}on(a,b){this.eventHandlers[a].push(b)}emit(a){this.eventHandlers[a].forEach(a=>{a(this)})}log(a,...b){a>=this.logLevel&&this.logfn(a,...b)}_enter(){return this.opened.then(a=>{if(this.closed())throw new i.DisconnectedError(a?"Connection closed":"Could not connect");return this})}_exit(a){return new Promise(a=>{this.closed()?a():(this.on("close",()=>{a()}),this.close())})}}const L=function(a,b={}){const c=new K(a,b);return c._enter()},M={asyncIterator:g,Bridge:H,Codec:q,connect:L,Connection:K,Errors:i,forAwait:(a,b)=>f(a[g](),a=>{const c=d=>void 0===d?a.next().then(a=>{if(!a.done)return Promise.resolve(b(a.value)).then(c)}):d;return c()}),LocalObject:z,LocalRoot:x,LOG_ERROR:40,LOG_WARNING:k,LOG_INFO:l,LOG_DEBUG:m,Reference:s,Registry:I,serve:function(a,b={}){const c=p.call(b,"Server")?b.Server:d,e=new c({port:a});return e.on("connection",a=>L(a,b)),e},using:f,VERSION:"1.0.0"};"undefined"!=typeof module&&!module.nodeType&&module.exports?module.exports=M:a.Eider=M})();