UNPKG

8.58 kBJavaScriptView Raw
1/// <reference types="./full.min.d.ts" />
2((r,t)=>{"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((r="undefined"!=typeof globalThis?globalThis:r||self).AsyncCall={})})(this,(function(r){"use strict";class t extends Error{constructor(r,t,e,n){super(t),this.name=r,this.code=e,this.stack=n}}const e={},n={},o={},s={},i=[e,n,o,s];function a(r,t){const e=i.indexOf(r);return t.message+=`Error ${e}: https://github.com/Jack-Works/async-call-rpc/wiki/Errors#`+e,t}const c={__proto__:null,Error,EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError},l="DOMException:",u=r=>(r+"").replace(/^.+\n.+\n/,""),f=()=>{try{return DOMException}catch(r){}},d=r=>"string"==typeof r,y=r=>"boolean"==typeof r,p=r=>"function"==typeof r,h=r=>"object"==typeof r&&null!==r,m="Error",g=void 0,w=Object.setPrototypeOf,b=r=>Promise.reject(r),E=r=>Promise.resolve(r),k=Array.isArray,_=()=>"() => replay()",$="2.0",S=(r,t,e,n)=>{r===g&&(r=null),Number.isNaN(t=Math.floor(t))&&(t=-1);const o={jsonrpc:$,id:r,error:{code:t,message:e,data:n}};return M(o.error,"data"),o},P=(r,t,e)=>{const{id:n}=r,{code:o,message:s,data:i}=e(t,r);return S(n,o,s,i)},v=(r="",t=-1)=>e=>{let n=O("",(()=>e.message)),o=O(m,((r=e.constructor)=>p(r)&&r.name));const s=f();return s&&e instanceof s&&(o=l+e.name),(d(e)||"number"==typeof e||y(e)||"bigint"==typeof e)&&(o=m,n=e+""),{code:t,message:n,data:r?{stack:r,type:o}:{type:o}}},x=r=>{if(!h(r))return!1;if(!j(r,"jsonrpc"))return!1;if(r.jsonrpc!==$)return!1;if(j(r,"params")){const t=r.params;if(!k(t)&&!h(t))return!1}return!0},j=(r,t)=>t in r,O=(r,t)=>{try{const e=t();return e===g?r:e+""}catch(t){return r}},M=(r,t)=>{r[t]===g&&delete r[t]},z={serialization:r=>r,deserialization:r=>r},A="AsyncCall/",C=Symbol.for(A+"ignored"),N=Symbol.for(A+"notify"),R=Symbol.for(A+"batch"),T=(r,t)=>r[t][N],I=()=>Math.random().toString(36).slice(2),W=r=>void 0===r||r,J=r=>{if(!y(r)){const{methodNotFound:t,unknownMessage:e}=r;return[t,e]}return[r,r]};function G(r,e){let n=!0,i=g,w=g;const O=async()=>{try{i=await r}catch(r){w=r,or("AsyncCall failed to start",r)}finally{n=!1}},{serializer:A=z,key:T="rpc",strict:G=!0,log:q=!0,parameterStructures:D="by-position",preferLocalImplementation:F=!1,idGenerator:L=I,mapError:U,logger:B,channel:H,thenable:K}=e;r instanceof Promise?O():(i=r,n=!1);const[Q,V]=J(G),[X,Y,Z,rr,tr,er]=(r=>{if("all"===r)return[!0,!0,!0,!0,!0,!0];if(!y(r)){const{beCalled:t,localError:e,remoteError:n,type:o,requestReplay:s,sendLocalStack:i}=r;return[W(t),W(e),W(n),"basic"!==o,s,i]}return r?[!0,!0,!0,!0]:[]})(q),{log:nr,error:or=nr,debug:sr=nr,groupCollapsed:ir=nr,groupEnd:ar=nr,warn:cr=nr}=B||console,lr=new Map,ur=async r=>{let t;try{if(t=await yr(r),x(t))return await gr(t);if(k(t)&&t.every(x)&&0!==t.length)return Promise.all(t.map(gr));if(V){let r=t.id;return r===g&&(r=null),(r=>S(r,-32600,"Invalid Request"))(r)}return g}catch(r){return Y&&or(r,t,void 0),((r,t)=>{const e=P({},r,t),n=e.error;return n.code=-32700,n.message="Parse error",e})(r,U||v(r&&r.stack))}},fr=async r=>{if(r){if(k(r)){const t=r.filter((r=>r&&j(r,"id")));if(0===t.length)return;return dr(t)}return dr(r)}},dr=r=>A.serialization(r),yr=r=>A.deserialization(r);var pr;if(j(pr=H,"setup")&&p(pr.setup)&&H.setup((r=>ur(r).then(fr)),(r=>{const t=yr(r);return!!x(t)||E(t).then(x)})),(r=>j(r,"send")&&p(r.send))(H)){const r=H;r.on&&r.on((t=>ur(t).then(fr).then((t=>t&&r.send(t)))))}function hr(r,t,e){return h(r)&&j(r,"stack")&&(r.stack=t.split("\n").reduce(((r,t)=>r.replace(t+"\n","")),""+r.stack)),Y&&or(r),P(e,r,U||v(er?r.stack:g))}async function mr(r,t=!1){t&&(r=[...r]);const e=await dr(r);return H.send(e)}const gr=async r=>{if(j(r,"method")){const t=(async r=>{if(n)await O();else if(w)return hr(w,"",r);let t="";try{const{params:e,method:n,id:o,remoteStack:s}=r,a=n.startsWith("rpc.")?Symbol.for(n):n,c=i&&i[a];if(!p(c))return Q?S(o,-32601,"Method not found"):void(Y&&sr("Missing method",a,r));const l=k(e)?e:[e];t=u(Error().stack);const f=new Promise((r=>r(c.apply(i,l))));if(X)if(rr){const r=[`${T}.%c${n}%c(${l.map((()=>"%o")).join(", ")}%c)\n%o %c@${o}`,"color: #d2c057","",...l,"",f,"color: gray; font-style: italic;"];if(tr){const t=()=>{debugger;return c.apply(i,l)};t.toString=_,r.push(t)}s?(ir(...r),nr(s),ar()):nr(...r)}else nr(`${T}.${n}(${""+[...l]}) @${o}`);if(await f===C)return;return((r,t)=>{const e={jsonrpc:$,id:r,result:t};return M(e,"id"),e})(o,await f)}catch(e){return hr(e,t,r)}})(r);if(j(r,"id"))return t;try{await t}catch(r){}return g}return(async r=>{let e="",n="",o=0,s=m;if(j(r,"error")){const t=r.error;e=t.message,o=t.code;const i=t.data;n=h(i)&&j(i,"stack")&&d(i.stack)?i.stack:"<remote stack not available>",s=h(i)&&j(i,"type")&&d(i.type)?i.type:m,Z&&(rr?or(`${s}: ${e}(${o}) %c@${r.id}\n%c${n}`,"color: gray",""):or(`${s}: ${e}(${o}) @${r.id}\n${n}`))}if(null===r.id||r.id===g)return;const{f:[i,a]=[null,null],stack:u=""}=lr.get(r.id)||{};i&&a&&(lr.delete(r.id),j(r,"error")?a(((r,e,n,o)=>{try{const s=f();if(r.startsWith(l)&&s)return new s(e,r.slice(13));if(r in c){const t=new c[r](e);return t.stack=o,t.code=n,t}return new t(r,e,n,o)}catch(t){return Error(`E${n} ${r}: ${e}\n${o}`)}})(s,e,o,n+"\n аt AsyncCall (rpc) \n"+u)):i(r.result))})(r)};return new Proxy({__proto__:null},{get(r,t){if("then"===t&&(K===g&&cr(a(s,new TypeError("RPC used as Promise: "))),!0!==K))return g;if(d(t)&&r[t])return r[t];const e=r=>(...e)=>{let s=u(Error().stack),c=g;if(t===R&&(c=e.shift(),t=e.shift()),"symbol"==typeof t){const r=Symbol.keyFor(t)||t.description;if(r){if(!r.startsWith("rpc."))return b(new TypeError("Not start with rpc."));t=r}}else if(t.startsWith("rpc."))return b(a(o,new TypeError));return new Promise(((o,a)=>{if(F&&!n&&d(t)){const r=i&&i[t];if(p(r))return o(r(...e))}const l=L(),[u]=e,f=er?s:"",y="by-name"===D&&1===e.length&&h(u)?u:e,m=((r,t,e,n)=>{const o={jsonrpc:$,id:r,method:t,params:e,remoteStack:n};return M(o,"id"),((r,t)=>{r[t]||delete r[t]})(o,"remoteStack"),o})(r?g:l,t,y,f);if(c?(c.push(m),c.r||(c.r=[()=>mr(c,!0),r=>((r,t)=>{for(const e of r)if(j(e,"id")){const r=lr.get(e.id);r&&r.f[1](t)}})(c,r)])):mr(m).catch(a),r)return o();lr.set(l,{f:[o,a],stack:s})}))},c=e(!1);return c[N]=e(!0),c[N][N]=c[N],d(t)&&Object.defineProperty(r,t,{value:c,configurable:!0}),c}})}const q="rpc.async-iterator.",D=Symbol.for(q+"start"),F=Symbol.for(q+"next"),L=Symbol.for(q+"return"),U=Symbol.for(q+"throw");class B{__init(){this.d=!1}__init2(){this.c=async r=>(await Q(r,(()=>this.d=!0)),r)}constructor(r,t){this.r=r,this.i=t,B.prototype.__init.call(this),B.prototype.__init2.call(this)}async return(r){return this.d?V(!0,r):this.c(this.r[L](await this.i,r))}async next(r){return this.d?V(!0):this.c(this.r[F](await this.i,r))}async throw(r){if(!this.d)return this.c(this.r[U](await this.i,r));throw r}}const H=async function*(){};w(B,H.constructor.prototype);const K=Object.getPrototypeOf(H());w(B.prototype,K);const Q=async(r,t)=>{try{const e=await r;e&&e.done&&t()}catch(r){}},V=(r,t)=>({done:r,value:t});r.AsyncCall=G,r.AsyncGeneratorCall=(r,t)=>{const o=new Map,[s]=J(null==(i=t.strict)||i);var i;const{idGenerator:c=I}=t,l=(r,t)=>{const n=o.get(r);if(!n){if(s)throw a(e,Error(`Iterator ${r}, `));return C}const i=t(n);return Q(i,(()=>o.delete(r))),i},u=G({async[D](t,e){const n=(await r)[t];if(!p(n)){if(s)throw new TypeError(t+" is not a function");return C}const i=n(...e),a=c();return o.set(a,i),E(a)},[F]:(r,t)=>l(r,(r=>r.next(t))),[L]:(r,t)=>l(r,(r=>p(r.return)&&r.return(t))),[U]:(r,t)=>l(r,(r=>p(r.throw)&&r.throw(t)))},t);return new Proxy({__proto__:null},{get:(r,t)=>{if(!d(t))throw a(n,new TypeError(""));if(r[t])return r[t];const e=(...r)=>{const e=u[D](t,r);return new B(u,e)};return Object.defineProperty(r,t,{value:e,configurable:!0}),e}})},r.JSONSerialization=(r=[g,g],t,e="null")=>({serialization(n){if(e&&h(n)&&j(n,"result")&&n.result===g){const r={...n};r.result=null,"keep"===e&&(r.undef=!0),n=r}return JSON.stringify(n,r[0],t)},deserialization(t){const e=JSON.parse(t,r[1]);return h(e)&&j(e,"result")&&null===e.result&&j(e,"undef")&&!0===e.undef&&(e.result=g,delete e.undef),e}}),r.NoSerialization=z,r.batch=r=>{let t=[];return[new Proxy({__proto__:null},{get(e,n){if(d(n)&&e[n])return e[n];const o=(...e)=>r[R](t,n,...e);return(o[N]=(...e)=>r[R][N](t,n,...e))[N]=o[N],d(n)&&Object.defineProperty(e,n,{value:o,configurable:!0}),o}}),(r=t.r)=>r&&r[0](),(r=Error("Aborted"),e=t.r)=>{e&&e[1](r),t=[]}]},r.notify=r=>p(r)?r[N]:new Proxy(r,{get:T}),Object.defineProperty(r,"__esModule",{value:!0})}));
3//# sourceMappingURL=full.min.js.map
4
\No newline at end of file