UNPKG

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