@speckle/objectsender
Version:
Simple API helper to serialize and send objects to the server.
515 lines (514 loc) • 15.8 kB
JavaScript
var yt = Object.defineProperty;
var Q = (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) || Q("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) ? Q("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, It = 1 / 0, 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 == -It ? "-0" : e;
}
function ut(t) {
var e = typeof t;
return t != null && (e == "object" || e == "function");
}
var xt = "[object AsyncFunction]", Et = "[object Function]", At = "[object GeneratorFunction]", Dt = "[object Proxy]";
function Ft(t) {
if (!ut(t))
return !1;
var e = ot(t);
return e == Et || e == At || e == xt || e == Dt;
}
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, Nt = Mt.toString;
function Lt(t) {
if (t != null) {
try {
return Nt.call(t);
} catch {
}
try {
return t + "";
} catch {
}
}
return "";
}
var Ut = /[\\^$.*+?()[\]{}|]/g, Gt = /^\[object .+?Constructor\]$/, Ht = Function.prototype, Kt = Object.prototype, Bt = Ht.toString, qt = Kt.hasOwnProperty, Jt = RegExp(
"^" + Bt.call(qt).replace(Ut, "\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, "$1.*?") + "$"
);
function Vt(t) {
if (!ut(t) || Rt(t))
return !1;
var e = Ft(t) ? Jt : Gt;
return e.test(Lt(t));
}
function Wt(t, e) {
return t == null ? void 0 : t[e];
}
function ft(t, e) {
var r = Wt(t, e);
return Vt(r) ? r : void 0;
}
function Xt(t, e) {
return t === e || t !== t && e !== e;
}
var Yt = /\.|\[(?:[^[\]]*|(["'])(?:(?!\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) || !Yt.test(t) || e != null && t in Object(e);
}
var x = ft(Object, "create");
function te() {
this.__data__ = x ? x(null) : {}, this.size = 0;
}
function ee(t) {
var e = this.has(t) && delete this.__data__[t];
return this.size -= e ? 1 : 0, e;
}
var re = "__lodash_hash_undefined__", ne = Object.prototype, ae = ne.hasOwnProperty;
function ie(t) {
var e = this.__data__;
if (x) {
var r = e[t];
return r === re ? void 0 : r;
}
return ae.call(e, t) ? e[t] : void 0;
}
var se = Object.prototype, oe = se.hasOwnProperty;
function ce(t) {
var e = this.__data__;
return x ? e[t] !== void 0 : oe.call(e, t);
}
var he = "__lodash_hash_undefined__";
function ue(t, e) {
var r = this.__data__;
return this.size += this.has(t) ? 0 : 1, r[t] = x && e === void 0 ? he : 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 = te;
_.prototype.delete = ee;
_.prototype.get = ie;
_.prototype.has = ce;
_.prototype.set = ue;
function fe() {
this.__data__ = [], this.size = 0;
}
function L(t, e) {
for (var r = t.length; r--; )
if (Xt(t[r][0], e))
return r;
return -1;
}
var le = Array.prototype, de = le.splice;
function pe(t) {
var e = this.__data__, r = L(e, t);
if (r < 0)
return !1;
var n = e.length - 1;
return r == n ? e.pop() : de.call(e, r, 1), --this.size, !0;
}
function ye(t) {
var e = this.__data__, r = L(e, t);
return r < 0 ? void 0 : e[r][1];
}
function ge(t) {
return L(this.__data__, t) > -1;
}
function _e(t, e) {
var r = this.__data__, n = L(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 = fe;
C.prototype.delete = pe;
C.prototype.get = ye;
C.prototype.has = ge;
C.prototype.set = _e;
var be = ft(J, "Map");
function me() {
this.size = 0, this.__data__ = {
hash: new _(),
map: new (be || C)(),
string: new _()
};
}
function we(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 we(e) ? r[typeof e == "string" ? "string" : "hash"] : r.map;
}
function Se(t) {
var e = U(this, t).delete(t);
return this.size -= e ? 1 : 0, e;
}
function Te(t) {
return U(this, t).get(t);
}
function je(t) {
return U(this, t).has(t);
}
function Oe(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 = me;
b.prototype.delete = Se;
b.prototype.get = Te;
b.prototype.has = je;
b.prototype.set = Oe;
var ve = "Expected a function";
function X(t, e) {
if (typeof t != "function" || e != null && typeof e != "function")
throw new TypeError(ve);
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 Ce = 500;
function ze(t) {
var e = X(t, function(n) {
return r.size === Ce && r.clear(), n;
}), r = e.cache;
return e;
}
var $e = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g, ke = /\\(\\)?/g, Pe = ze(function(t) {
var e = [];
return t.charCodeAt(0) === 46 && e.push(""), t.replace($e, function(r, n, a, i) {
e.push(a ? i.replace(ke, "$1") : n || r);
}), e;
});
function Ie(t) {
return t == null ? "" : ht(t);
}
function xe(t, e) {
return W(t) ? t : Qt(t, e) ? [t] : Pe(Ie(t));
}
var Ee = 1 / 0;
function Ae(t) {
if (typeof t == "string" || V(t))
return t;
var e = t + "";
return e == "0" && 1 / t == -Ee ? "-0" : e;
}
function De(t, e) {
e = xe(e, t);
for (var r = 0, n = e.length; t != null && r < n; )
t = t[Ae(e[r++])];
return r && r == n ? t : void 0;
}
function Fe(t, e, r) {
var n = t == null ? void 0 : De(t, e);
return n === void 0 ? r : n;
}
const lt = Symbol("detach"), Y = Symbol("chunkable");
function qe() {
return Reflect.metadata(lt, !0);
}
function Je(t) {
return Reflect.metadata(Y, t);
}
function Re(t, e) {
return !!Reflect.getMetadata(lt, t, e);
}
function Me(t, e) {
return !!Reflect.getMetadata(Y, t, e);
}
function Ne(t, e) {
return Reflect.getMetadata(Y, t, e);
}
const Le = (t) => ct(t) && !!Fe(t, "speckle_type");
var s, P, I, N, q, dt;
class Ue {
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("@") || Re(e, o), Z = Array.isArray(u), w = Z ? Me(e, o) || o.match(/^@\((\d*)\)/) : !1;
if (Z && w && u.length !== 0 && typeof u[0] != "object") {
let S = this.chunkSize;
typeof w == "boolean" ? S = Ne(e, o) : S = w[1] !== "" ? parseInt(w[1]) : this.chunkSize;
const M = [];
let $ = new it(), G = 0;
for (const pt of u)
G === S && (M.push(await h(this, s, q).call(this, $)), $ = new it(), G = 0), $.data.push(pt), G++;
$.data.length !== 0 && M.push(await h(this, s, q).call(this, $)), typeof w == "boolean" ? a[o] = M : a[o.replace(w[0], "")] = M;
continue;
}
if (u.speckle_type) {
const S = await h(this, s, I).call(this, {
value: u,
isDetached: z
});
a[o] = z ? h(this, s, N).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: F, size: R } = h(this, s, dt).call(this, a);
return a.id = m, (i || r) && this.transport && await this.transport.write(F, R, 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 (Le(i)) {
this.detachLineage.push(r);
const { hash: d } = await h(this, s, P).call(this, i, !1);
a.push(h(this, s, N).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}.`);
}, N = 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, N).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 Ge {
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 Ve {
constructor(e) {
l(this, "speckle_type");
if (this.speckle_type = this.constructor.name, e)
for (const r in e) this[r] = e[r];
}
}
const We = 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 Ge(e, r, n, i == null ? void 0 : i.transport), F = new Ue(
m,
i == null ? void 0 : i.chunkSize,
i == null ? void 0 : i.hashingFunction
);
let R;
try {
R = await F.write(t);
} finally {
m.dispose(), F.dispose();
}
const o = performance.now();
return a.log(`Finished sending in ${(o - d) / B.second}s.`), R;
};
export {
Ve as Base,
Je as Chunkable,
qe as Detach,
We as send
};
//# sourceMappingURL=objectsender.js.map