@ng-org/web
Version:
JS/TS SDK of NextGraph for third-party web apps
161 lines (160 loc) • 4.69 kB
JavaScript
function f(s, o) {
let i = s;
for (let t = 0; t < o.length; ++t)
i = i[o[t]];
return i;
}
function g(s, o, i) {
if (o.length === 0)
throw new Error("Cannot assign target object itself");
let t = s;
for (let l = 0; l < o.length; ++l)
l === o.length - 1 ? t[o[l]] = i : t = t[o[l]];
return !0;
}
function P(s, o) {
if (o.length === 0)
throw new Error("Cannot remove target object itself");
let i = s;
for (let t = 0; t < o.length; ++t)
t === o.length - 1 ? delete i[o[t]] : i = i[o[t]];
return !0;
}
function _(s = /* @__PURE__ */ Object.create(null), o = /* @__PURE__ */ Object.create(null)) {
class i extends Function {
}
const t = {
async get(e, n) {
return f(e, n);
},
async apply(e, n, r, a) {
return Reflect.apply(f(e, n), r, a);
},
async set(e, n, r) {
g(e, n, r);
},
async deleteProperty(e, n) {
P(e, n);
},
async construct(e, n, r) {
return Reflect.construct(f(e, n), r);
},
async setPrototypeOf(e, n, r) {
Reflect.setPrototypeOf(f(e, n), r);
},
async getPrototypeOf(e, n) {
return Reflect.getPrototypeOf(f(e, n));
},
async defineProperty(e, n, r, a) {
Reflect.defineProperty(f(e, n), r, a);
},
...o
};
function l(e = []) {
return new Proxy(new i(), {
get(n, r) {
switch (r) {
case "then":
case "catch":
case "finally":
const a = (async () => await t.get(s, e))();
return a[r].bind(a);
default:
return l([...e, r]);
}
},
apply(n, r, a) {
return t.apply(s, e, r, a);
},
set(n, r, a) {
return t.set(s, [...e, r], a), !0;
},
deleteProperty(n, r) {
return t.deleteProperty(s, [...e, r]), !0;
},
construct(n, r) {
return t.construct(s, e, r);
},
setPrototypeOf(n, r) {
return t.setPrototypeOf(s, e, r), !0;
},
getPrototypeOf(n) {
return t.getPrototypeOf(s, e);
},
defineProperty(n, r, a) {
return t.defineProperty(s, e, r, a), !0;
},
has(n, r) {
throw Error('Async proxy does not support "has" handler.');
},
getOwnPropertyDescriptor(n, r) {
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();
}
let p = !1;
const y = "nextgraph.net", d = { redirect: `https://${y}/redir/#/?o=`, origin: `https://${y}` }, x = async function(s, o, i) {
if (!window) throw new Error("init(callback,..) can only be called from a browser's window");
if (window.self === window.top)
window.location.href = d.redirect + encodeURIComponent(window.location.href);
else if (p === !1) {
const { port1: t, port2: l } = new MessageChannel();
t.onmessage = async (e) => {
e.data.ok && (p = e.data.ret, s && await s({ status: "loggedin", session: p })), t.close();
}, parent.postMessage({ method: "init", origin: window.location.href, singleton: o, access_requests: i, port: l }, d.origin, [l]);
} else
throw new Error("you must call init() only once");
}, h = {
doc_subscribe: 2,
orm_start: 3,
file_get: 3,
app_request_stream: 1
};
function b(s, o) {
const { port1: i, port2: t } = new MessageChannel();
let l = h[s];
if (l !== void 0) {
let e = o[l], n = o.slice(0, -1);
return parent.postMessage({ method: s, streamed: !0, args: n, port: t }, d.origin, [t]), new Promise((a, w) => {
let u = !1;
i.onmessage = (c) => {
if (c.data.stream)
u || (a(() => {
i.close();
}), u = !0), c.data.ret !== void 0 && e(c.data.ret);
else if (!c.data.ok)
if (!u)
w(c.data.ret), u = !0;
else
throw new Error(c.data.ret);
};
});
} else
return parent.postMessage({ method: s, args: o, port: t }, d.origin, [t]), new Promise((e, n) => {
i.onmessage = (r) => {
r.data.ok ? e(r.data.ret) : (console.error("method " + s + " with args ", o, " failed with ", r.data.ret), n(new Error(r.data.ret)));
};
});
}
const E = {
apply(s, o, i, t) {
if (p === !1)
throw new Error("you must call init() first (and once)");
return b(o[0], t);
}
}, O = _({}, E);
export {
x as init,
O as ng
};