nextgraphweb
Version:
TS SDK of NextGraph for web apps
56 lines (47 loc) • 5.92 kB
JavaScript
(function(p,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(p=typeof globalThis<"u"?globalThis:p||self,u(p.nextgraphweb={}))})(this,function(p){"use strict";function u(r,e){let s=r;for(let o=0;o<e.length;++o)s=s[e[o]];return s}function m(r,e,s){if(e.length===0)throw new Error("Cannot assign target object itself");let o=r;for(let l=0;l<e.length;++l)l===e.length-1?o[e[l]]=s:o=o[e[l]];return!0}function b(r,e){if(e.length===0)throw new Error("Cannot remove target object itself");let s=r;for(let o=0;o<e.length;++o)o===e.length-1?delete s[e[o]]:s=s[e[o]];return!0}function _(r=Object.create(null),e=Object.create(null)){class s extends Function{}const o={async get(t,n){return u(t,n)},async apply(t,n,a,i){return Reflect.apply(u(t,n),a,i)},async set(t,n,a){m(t,n,a)},async deleteProperty(t,n){b(t,n)},async construct(t,n,a){return Reflect.construct(u(t,n),a)},async setPrototypeOf(t,n,a){Reflect.setPrototypeOf(u(t,n),a)},async getPrototypeOf(t,n){return Reflect.getPrototypeOf(u(t,n))},async defineProperty(t,n,a,i){Reflect.defineProperty(u(t,n),a,i)},...e};function l(t=[]){return new Proxy(new s,{get(n,a){switch(a){case"then":case"catch":case"finally":const i=(async()=>await o.get(r,t))();return i[a].bind(i);default:return l([...t,a])}},apply(n,a,i){return o.apply(r,t,a,i)},set(n,a,i){return o.set(r,[...t,a],i),!0},deleteProperty(n,a){return o.deleteProperty(r,[...t,a]),!0},construct(n,a){return o.construct(r,t,a)},setPrototypeOf(n,a){return o.setPrototypeOf(r,t,a),!0},getPrototypeOf(n){return o.getPrototypeOf(r,t)},defineProperty(n,a,i){return o.defineProperty(r,t,a,i),!0},has(n,a){throw Error('Async proxy does not support "has" handler.')},getOwnPropertyDescriptor(n,a){throw Error('Async proxy does not support "getOwnPropertyDescriptor" handler.')},ownKeys(n){throw Error('Async proxy does not support "ownKeys" handler.')},preventExtensions(n){throw Error('Async proxy does not support "preventExtensions" handler.')},isExtensible(n){throw Error('Async proxy does not support "isExtensible" handler.')}})}return l()}function x(r){return new ReadableStream(new v(r))}class v{constructor(e){this._port=e,this._port.onmessage=s=>this._onMessage(s.data)}start(e){this._controller=e}pull(e){const s={type:"pull"};this._port.postMessage(s)}cancel(e){const s={type:"error",reason:e};this._port.postMessage(s),this._port.close()}_onMessage(e){switch(e.type){case"write":this._controller.enqueue(e.chunk);break;case"abort":this._controller.error(e.reason),this._port.close();break;case"close":this._controller.close(),this._port.close();break}}}class y{constructor(){const e=new MessageChannel;this.writablePort=e.port1,this.readable=x(e.port2)}}let c=null;const P=`.nextgraph-auth-modal {
visibility: hidden;
background-color: rgba(0, 0, 0, 0.4);
display: grid;
place-items: center;
height: 100vh;
width: 100vw;
position: fixed;
left: 0;
top: 0;
}
.nextgraph-auth-modal.nextgraph-auth-modal--fade {
opacity: 0;
transition: 0.2s;
}
.nextgraph-auth-modal.nextgraph-auth-modal--active {
visibility: visible;
z-index: 9999;
opacity: 1;
}
.nextgraph-auth-modal__content {
width: 100%;
height: 100%;
background-color: #fff;
}
.nextgraph-auth-modal.nextgraph-auth-modal--active.nextgraph-auth-modal--fade {
opacity: 1;
animation-name: fadeInOpacity;
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-duration: 0.5s;
}
@keyframes fadeInOpacity {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}`,E=`
<div id="nextgraph-auth" class="nextgraph-auth-modal nextgraph-auth-modal--fade">
<div class="nextgraph-auth-modal__content">
<iframe id="nextgraph-auth-iframe" scrolling="auto" frameborder="0"
style="position: relative; height: 100%; width: 100%; overflow:auto;">
</iframe>
</div>
</div>`;function O(){const r=window.document.createElement("style");r.textContent=P,window.document.head.append(r),document.getElementsByTagName("body")[0].insertAdjacentHTML("afterbegin",E)}const h={src:"https://nextgraph.net/auth/?o=",origin:"https://nextgraph.net"},R=async function(r,e,s){if(c===null){if(!window)throw new Error("init(callback,..) can only be called from a browser's window");let o=location.origin,l=encodeURIComponent(o);O();let t=window.document.getElementById("nextgraph-auth-iframe");if(t)return new Promise(async n=>{t.addEventListener("load",async function(){c=this.contentWindow;const{readable:a,writablePort:i}=new y;c==null||c.postMessage({method:"init",singleton:e,access_requests:s,port:i},h.origin,[i]);const d=a.getReader();n(!0);for(var f;(f=await d.read())&&!f.done;)f.value.status=="error"?console.error(f.value.error):(f.value.status=="cancelled"||f.value.status=="loggedin")&&g(),await r(f.value)}),t.src=`${h.src}${l}`})}};function k(){var r;(r=window.document.getElementById("nextgraph-auth"))==null||r.classList.add("nextgraph-auth-modal--active")}function g(){var r;(r=window.document.getElementById("nextgraph-auth"))==null||r.classList.remove("nextgraph-auth-modal--active")}async function w(r,e){const{readable:s,writablePort:o}=new y;if(r==="doc_subscribe"){let l=e[2],t=[e[0],e[1]];c==null||c.postMessage({method:r,args:t,port:o},h.origin,[o]);const n=s.getReader();return new Promise(async i=>{i(()=>{});for(var d;(d=await n.read())&&!d.done;){if(d.value.error)throw new Error(d.value.ret);d.value.stream&&l(d.value.ret)}})}else{c==null||c.postMessage({method:r,args:e,port:o},h.origin,[o]);const l=s.getReader();let t=await l.read();if(await l.read(),t.value.ok)return t.value.ret;throw new Error(t.value.ret)}}const M=_({},{async apply(r,e,s,o){if(c===null)throw new Error("you must call init() first (and once)");return e[0]==="login"?await w("login")!==!0?(k(),!1):!0:await w(e[0],o)}});p.default=M,p.init=R,Object.defineProperties(p,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});