UNPKG

@speckle/objectsender

Version:

Simple API helper to serialize and send objects to the server.

514 lines (513 loc) 15.8 kB
var yt = Object.defineProperty; var Y = (t) => { throw TypeError(t); }; var gt = (t, e, r) => e in t ? yt(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r; var l = (t, e, r) => gt(t, typeof e != "symbol" ? e + "" : e, r), H = (t, e, r) => e.has(t) || Y("Cannot " + r); var c = (t, e, r) => (H(t, e, "read from private field"), r ? r.call(t) : e.get(t)), p = (t, e, r) => e.has(t) ? Y("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, r), f = (t, e, r, n) => (H(t, e, "write to private field"), n ? n.call(t, r) : e.set(t, r), r), h = (t, e, r) => (H(t, e, "access private method"), r); import "reflect-metadata"; import { md5 as tt, TIME_MS as B, retry as _t, timeoutAt as bt } from "@speckle/shared"; var mt = typeof global == "object" && global && global.Object === Object && global, wt = typeof self == "object" && self && self.Object === Object && self, J = mt || wt || Function("return this")(), v = J.Symbol, st = Object.prototype, St = st.hasOwnProperty, Tt = st.toString, k = v ? v.toStringTag : void 0; function jt(t) { var e = St.call(t, k), r = t[k]; try { t[k] = void 0; var n = !0; } catch { } var a = Tt.call(t); return n && (e ? t[k] = r : delete t[k]), a; } var Ot = Object.prototype, vt = Ot.toString; function Ct(t) { return vt.call(t); } var zt = "[object Null]", $t = "[object Undefined]", et = v ? v.toStringTag : void 0; function ot(t) { return t == null ? t === void 0 ? $t : zt : et && et in Object(t) ? jt(t) : Ct(t); } function ct(t) { return t != null && typeof t == "object"; } var kt = "[object Symbol]"; function V(t) { return typeof t == "symbol" || ct(t) && ot(t) == kt; } function Pt(t, e) { for (var r = -1, n = t == null ? 0 : t.length, a = Array(n); ++r < n; ) a[r] = e(t[r], r, t); return a; } var W = Array.isArray, rt = v ? v.prototype : void 0, nt = rt ? rt.toString : void 0; function ht(t) { if (typeof t == "string") return t; if (W(t)) return Pt(t, ht) + ""; if (V(t)) return nt ? nt.call(t) : ""; var e = t + ""; return e == "0" && 1 / t == -1 / 0 ? "-0" : e; } function ut(t) { var e = typeof t; return t != null && (e == "object" || e == "function"); } var It = "[object AsyncFunction]", xt = "[object Function]", Et = "[object GeneratorFunction]", At = "[object Proxy]"; function Dt(t) { if (!ut(t)) return !1; var e = ot(t); return e == xt || e == Et || e == It || e == At; } var K = J["__core-js_shared__"], at = function() { var t = /[^.]+$/.exec(K && K.keys && K.keys.IE_PROTO || ""); return t ? "Symbol(src)_1." + t : ""; }(); function Rt(t) { return !!at && at in t; } var Mt = Function.prototype, Ft = Mt.toString; function Lt(t) { if (t != null) { try { return Ft.call(t); } catch { } try { return t + ""; } catch { } } return ""; } var Nt = /[\\^$.*+?()[\]{}|]/g, Ut = /^\[object .+?Constructor\]$/, Gt = Function.prototype, Ht = Object.prototype, Kt = Gt.toString, Bt = Ht.hasOwnProperty, qt = RegExp( "^" + Kt.call(Bt).replace(Nt, "\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, "$1.*?") + "$" ); function Jt(t) { if (!ut(t) || Rt(t)) return !1; var e = Dt(t) ? qt : Ut; return e.test(Lt(t)); } function Vt(t, e) { return t == null ? void 0 : t[e]; } function ft(t, e) { var r = Vt(t, e); return Jt(r) ? r : void 0; } function Wt(t, e) { return t === e || t !== t && e !== e; } var Xt = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/, Zt = /^\w*$/; function Qt(t, e) { if (W(t)) return !1; var r = typeof t; return r == "number" || r == "symbol" || r == "boolean" || t == null || V(t) ? !0 : Zt.test(t) || !Xt.test(t) || e != null && t in Object(e); } var x = ft(Object, "create"); function Yt() { this.__data__ = x ? x(null) : {}, this.size = 0; } function te(t) { var e = this.has(t) && delete this.__data__[t]; return this.size -= e ? 1 : 0, e; } var ee = "__lodash_hash_undefined__", re = Object.prototype, ne = re.hasOwnProperty; function ae(t) { var e = this.__data__; if (x) { var r = e[t]; return r === ee ? void 0 : r; } return ne.call(e, t) ? e[t] : void 0; } var ie = Object.prototype, se = ie.hasOwnProperty; function oe(t) { var e = this.__data__; return x ? e[t] !== void 0 : se.call(e, t); } var ce = "__lodash_hash_undefined__"; function he(t, e) { var r = this.__data__; return this.size += this.has(t) ? 0 : 1, r[t] = x && e === void 0 ? ce : e, this; } function _(t) { var e = -1, r = t == null ? 0 : t.length; for (this.clear(); ++e < r; ) { var n = t[e]; this.set(n[0], n[1]); } } _.prototype.clear = Yt; _.prototype.delete = te; _.prototype.get = ae; _.prototype.has = oe; _.prototype.set = he; function ue() { this.__data__ = [], this.size = 0; } function N(t, e) { for (var r = t.length; r--; ) if (Wt(t[r][0], e)) return r; return -1; } var fe = Array.prototype, le = fe.splice; function de(t) { var e = this.__data__, r = N(e, t); if (r < 0) return !1; var n = e.length - 1; return r == n ? e.pop() : le.call(e, r, 1), --this.size, !0; } function pe(t) { var e = this.__data__, r = N(e, t); return r < 0 ? void 0 : e[r][1]; } function ye(t) { return N(this.__data__, t) > -1; } function ge(t, e) { var r = this.__data__, n = N(r, t); return n < 0 ? (++this.size, r.push([t, e])) : r[n][1] = e, this; } function C(t) { var e = -1, r = t == null ? 0 : t.length; for (this.clear(); ++e < r; ) { var n = t[e]; this.set(n[0], n[1]); } } C.prototype.clear = ue; C.prototype.delete = de; C.prototype.get = pe; C.prototype.has = ye; C.prototype.set = ge; var _e = ft(J, "Map"); function be() { this.size = 0, this.__data__ = { hash: new _(), map: new (_e || C)(), string: new _() }; } function me(t) { var e = typeof t; return e == "string" || e == "number" || e == "symbol" || e == "boolean" ? t !== "__proto__" : t === null; } function U(t, e) { var r = t.__data__; return me(e) ? r[typeof e == "string" ? "string" : "hash"] : r.map; } function we(t) { var e = U(this, t).delete(t); return this.size -= e ? 1 : 0, e; } function Se(t) { return U(this, t).get(t); } function Te(t) { return U(this, t).has(t); } function je(t, e) { var r = U(this, t), n = r.size; return r.set(t, e), this.size += r.size == n ? 0 : 1, this; } function b(t) { var e = -1, r = t == null ? 0 : t.length; for (this.clear(); ++e < r; ) { var n = t[e]; this.set(n[0], n[1]); } } b.prototype.clear = be; b.prototype.delete = we; b.prototype.get = Se; b.prototype.has = Te; b.prototype.set = je; var Oe = "Expected a function"; function X(t, e) { if (typeof t != "function" || e != null && typeof e != "function") throw new TypeError(Oe); var r = function() { var n = arguments, a = e ? e.apply(this, n) : n[0], i = r.cache; if (i.has(a)) return i.get(a); var d = t.apply(this, n); return r.cache = i.set(a, d) || i, d; }; return r.cache = new (X.Cache || b)(), r; } X.Cache = b; var ve = 500; function Ce(t) { var e = X(t, function(n) { return r.size === ve && r.clear(), n; }), r = e.cache; return e; } var ze = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g, $e = /\\(\\)?/g, ke = Ce(function(t) { var e = []; return t.charCodeAt(0) === 46 && e.push(""), t.replace(ze, function(r, n, a, i) { e.push(a ? i.replace($e, "$1") : n || r); }), e; }); function Pe(t) { return t == null ? "" : ht(t); } function Ie(t, e) { return W(t) ? t : Qt(t, e) ? [t] : ke(Pe(t)); } function xe(t) { if (typeof t == "string" || V(t)) return t; var e = t + ""; return e == "0" && 1 / t == -1 / 0 ? "-0" : e; } function Ee(t, e) { e = Ie(e, t); for (var r = 0, n = e.length; t != null && r < n; ) t = t[xe(e[r++])]; return r && r == n ? t : void 0; } function Ae(t, e, r) { var n = t == null ? void 0 : Ee(t, e); return n === void 0 ? r : n; } const lt = Symbol("detach"), Z = Symbol("chunkable"); function Ke() { return Reflect.metadata(lt, !0); } function Be(t) { return Reflect.metadata(Z, t); } function De(t, e) { return !!Reflect.getMetadata(lt, t, e); } function Re(t, e) { return !!Reflect.getMetadata(Z, t, e); } function Me(t, e) { return Reflect.getMetadata(Z, t, e); } const Fe = (t) => ct(t) && !!Ae(t, "speckle_type"); var s, P, I, L, q, dt; class Le { constructor(e, r = 1e3, n = tt) { p(this, s); l(this, "chunkSize"); l(this, "detachLineage"); l(this, "lineage"); l(this, "familyTree"); l(this, "closureTable"); l(this, "transport"); l(this, "uniqueId"); l(this, "hashingFunction"); this.chunkSize = r, this.detachLineage = [!0], this.lineage = [], this.familyTree = {}, this.closureTable = {}, this.transport = e, this.uniqueId = 0, this.hashingFunction = n || tt; } async write(e) { return await h(this, s, P).call(this, e, !0); } dispose() { this.detachLineage = [], this.lineage = [], this.familyTree = {}, this.closureTable = {}, this.transport = null; } } s = new WeakSet(), P = async function(e, r) { const n = `${this.uniqueId++}-obj`; this.lineage.push(n); const a = { speckle_type: e.speckle_type || "Base" }; for (const o in e) { const u = e[o]; if (u === void 0 || o === "id" || o.startsWith("_")) continue; if (u === null || typeof u != "object") { a[o] = u; continue; } const z = o.startsWith("@") || De(e, o), Q = Array.isArray(u), w = Q ? Re(e, o) || o.match(/^@\((\d*)\)/) : !1; if (Q && w && u.length !== 0 && typeof u[0] != "object") { let S = this.chunkSize; typeof w == "boolean" ? S = Me(e, o) : S = w[1] !== "" ? parseInt(w[1]) : this.chunkSize; const F = []; let $ = new it(), G = 0; for (const pt of u) G === S && (F.push(await h(this, s, q).call(this, $)), $ = new it(), G = 0), $.data.push(pt), G++; $.data.length !== 0 && F.push(await h(this, s, q).call(this, $)), typeof w == "boolean" ? a[o] = F : a[o.replace(w[0], "")] = F; continue; } if (u.speckle_type) { const S = await h(this, s, I).call(this, { value: u, isDetached: z }); a[o] = z ? h(this, s, L).call(this, S.id) : S; continue; } a[o] = await h(this, s, I).call(this, { value: u, isDetached: z }); } const i = this.detachLineage.pop(), d = this.lineage.pop(); if (this.familyTree[d]) { const o = {}; Object.entries(this.familyTree[d]).forEach(([u, z]) => { o[u] = z - this.detachLineage.length; }), a.totalChildrenCount = Object.keys(o).length, a.totalChildrenCount && (a.__closure = o); } const { hash: m, serializedObject: R, size: M } = h(this, s, dt).call(this, a); return a.id = m, (i || r) && this.transport && await this.transport.write(R, M, m), r && this.transport && await this.transport.flush(), { hash: m, traversed: a }; }, I = async function({ value: e, isDetached: r = !1 }) { if (typeof e != "object") return e; if (Array.isArray(e)) { const n = e; if (n.length === 0) return e; if (typeof n[0] != "object") return n; if (!r) return Promise.all( e.map(async (i) => await h(this, s, I).call(this, { value: i })) ); const a = []; for (const i of e) if (Fe(i)) { this.detachLineage.push(r); const { hash: d } = await h(this, s, P).call(this, i, !1); a.push(h(this, s, L).call(this, d)); } else a.push(await h(this, s, I).call(this, { value: i, isDetached: r })); return a; } if (!e.speckle_type) return e; if (e.speckle_type) return this.detachLineage.push(r), (await h(this, s, P).call(this, e, !1)).traversed; throw new Error(`Unsupported type '${typeof e}': ${e}.`); }, L = function(e) { return this.lineage.forEach((r) => { this.familyTree[r] || (this.familyTree[r] = {}), (!this.familyTree[r][e] || this.familyTree[r][e] > this.detachLineage.length) && (this.familyTree[r][e] = this.detachLineage.length); }), { referencedId: e, speckle_type: "reference" }; }, q = async function(e) { this.detachLineage.push(!0); const { hash: r } = await h(this, s, P).call(this, e, !1); return h(this, s, L).call(this, r); }, dt = function(e) { const r = JSON.stringify(e), n = this.hashingFunction(r), a = r.substring(0, 1) + `"id":"${n}",` + r.substring(1); return { hash: n, serializedObject: a, size: r.length // approx, good enough as we're just limiting artificially batch sizes based on this }; }; class it { constructor() { l(this, "speckle_type"); l(this, "data"); this.data = [], this.speckle_type = "Speckle.Core.Models.DataChunk"; } } var y, E, g, T, j, O, A, D; class Ne { constructor(e, r, n, a) { p(this, y); p(this, E); p(this, g); p(this, T); p(this, j); p(this, O); p(this, A); p(this, D); f(this, E, (a == null ? void 0 : a.maxSize) || 2e5), f(this, A, (a == null ? void 0 : a.flushRetryCount) || 3), f(this, D, (a == null ? void 0 : a.flushTimeout) || 2 * B.minute), f(this, g, 0), f(this, T, e), f(this, j, r), f(this, O, n), f(this, y, []); } async write(e, r, n) { c(this, y).push([n, e]), f(this, g, c(this, g) + r), !(c(this, g) < c(this, E)) && await this.flush(); } async flush() { if (c(this, y).length === 0) return; const e = await this.diff(), r = new FormData(), n = "[" + e.join(",") + "]"; r.append("object-batch", new Blob([n], { type: "application/json" })); const a = new URL(`/objects/${c(this, j)}`, c(this, T)), i = await _t( async () => await Promise.race([ fetch(a, { method: "POST", headers: { Authorization: `Bearer ${c(this, O)}` }, body: r }), bt(c(this, D), "Object sender flush timed out") ]), c(this, A), (d) => d * B.second ); if (i.status !== 201) throw new Error( `Unexpected error when sending data. Expected status 200, got ${i.status}` ); f(this, y, []), f(this, g, 0); } async diff() { const e = c(this, y).map(([i]) => i), r = new URL(`/api/diff/${c(this, j)}`, c(this, T)), n = await fetch(r, { method: "POST", headers: { Authorization: `Bearer ${c(this, O)}`, "Content-Type": "application/json" }, body: JSON.stringify({ objects: JSON.stringify(e) }) }); if (!n.ok) { const i = await n.json(); throw new Error( `Unexpected error when sending data. Received ${i.error.message}` ); } const a = await n.json(); return c(this, y).filter(([i]) => !a[i]).map(([, i]) => i); } dispose() { f(this, y, []); } } y = new WeakMap(), E = new WeakMap(), g = new WeakMap(), T = new WeakMap(), j = new WeakMap(), O = new WeakMap(), A = new WeakMap(), D = new WeakMap(); class qe { constructor(e) { l(this, "speckle_type"); if (this.speckle_type = this.constructor.name, e) for (const r in e) this[r] = e[r]; } } const Je = async (t, { serverUrl: e = "https://app.speckle.systems", projectId: r, token: n, logger: a = console, options: i = void 0 }) => { const d = performance.now(); a == null || a.log("Starting to send"); const m = new Ne(e, r, n, i == null ? void 0 : i.transport), R = new Le( m, i == null ? void 0 : i.chunkSize, i == null ? void 0 : i.hashingFunction ); let M; try { M = await R.write(t); } finally { m.dispose(), R.dispose(); } const o = performance.now(); return a.log(`Finished sending in ${(o - d) / B.second}s.`), M; }; export { qe as Base, Be as Chunkable, Ke as Detach, Je as send }; //# sourceMappingURL=objectsender.js.map