UNPKG

nextgraphweb

Version:

TS SDK of NextGraph for web apps

272 lines (263 loc) 7.29 kB
function p(r, e) { let s = r; for (let o = 0; o < e.length; ++o) s = s[e[o]]; return s; } function g(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 m(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 b(r = /* @__PURE__ */ Object.create(null), e = /* @__PURE__ */ Object.create(null)) { class s extends Function { } const o = { async get(t, n) { return p(t, n); }, async apply(t, n, a, i) { return Reflect.apply(p(t, n), a, i); }, async set(t, n, a) { g(t, n, a); }, async deleteProperty(t, n) { m(t, n); }, async construct(t, n, a) { return Reflect.construct(p(t, n), a); }, async setPrototypeOf(t, n, a) { Reflect.setPrototypeOf(p(t, n), a); }, async getPrototypeOf(t, n) { return Reflect.getPrototypeOf(p(t, n)); }, async defineProperty(t, n, a, i) { Reflect.defineProperty(p(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 _(r) { return new ReadableStream(new x(r)); } class x { 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 w { constructor() { const e = new MessageChannel(); this.writablePort = e.port1, this.readable = _(e.port2); } } let c = null; const v = `.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; } }`, P = ` <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 E() { const r = window.document.createElement("style"); r.textContent = v, window.document.head.append(r), document.getElementsByTagName("body")[0].insertAdjacentHTML("afterbegin", P); } const h = { src: "https://nextgraph.net/auth/?o=", origin: "https://nextgraph.net" }, k = 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); E(); 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 w(); c == null || c.postMessage({ method: "init", singleton: e, access_requests: s, port: i }, h.origin, [i]); const u = a.getReader(); n(!0); for (var d; (d = await u.read()) && !d.done; ) d.value.status == "error" ? console.error(d.value.error) : (d.value.status == "cancelled" || d.value.status == "loggedin") && f(), await r(d.value); }), t.src = `${h.src}${l}`; }); } }; function O() { var r; (r = window.document.getElementById("nextgraph-auth")) == null || r.classList.add("nextgraph-auth-modal--active"); } function f() { var r; (r = window.document.getElementById("nextgraph-auth")) == null || r.classList.remove("nextgraph-auth-modal--active"); } async function y(r, e) { const { readable: s, writablePort: o } = new w(); 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 u; (u = await n.read()) && !u.done; ) { if (u.value.error) throw new Error(u.value.ret); u.value.stream && l(u.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 R = { async apply(r, e, s, o) { if (c === null) throw new Error("you must call init() first (and once)"); return e[0] === "login" ? await y("login") !== !0 ? (O(), !1) : !0 : await y(e[0], o); } }, M = b({}, R); export { M as default, k as init };