UNPKG

gun-es

Version:

ES version of GunJS Database

1,004 lines 121 kB
var V = typeof globalThis < "u" ? globalThis : typeof window < "u" ? window : typeof global < "u" ? global : typeof self < "u" ? self : {}; function et(K) { return K && K.__esModule && Object.prototype.hasOwnProperty.call(K, "default") ? K.default : K; } function nt(K) { throw new Error('Could not dynamically require "' + K + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'); } var Y = { exports: {} }; Y.exports; var Z; function rt() { return Z || (Z = 1, function(K) { (function() { function b(E, n) { return n ? nt(E) : E.slice ? b[f(E)] : function(s, v) { E(s = { exports: {} }), b[f(v)] = s.exports; }; function f(s) { return s.split("/").slice(-1).toString().replace(".js", ""); } } var B = K; b(function(E) { String.random = function(f, s) { var v = ""; for (f = f || 24, s = s || "0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz"; f-- > 0; ) v += s.charAt(Math.floor(Math.random() * s.length)); return v; }, String.match = function(f, s) { var v, l; return typeof f != "string" ? !1 : (typeof s == "string" && (s = { "=": s }), s = s || {}, v = s["="] || s["*"] || s[">"] || s["<"], f === v ? !0 : l !== s["="] ? !1 : (v = s["*"] || s[">"], f.slice(0, (v || "").length) === v ? !0 : l !== s["*"] ? !1 : l !== s[">"] && l !== s["<"] ? f >= s[">"] && f <= s["<"] : l !== s[">"] && f >= s[">"] || l !== s["<"] && f <= s["<"])); }, String.hash = function(f, s) { if (typeof f == "string") { if (s = s || 0, !f.length) return s; for (var v = 0, l = f.length, u; v < l; ++v) u = f.charCodeAt(v), s = (s << 5) - s + u, s |= 0; return s; } }; var n = Object.prototype.hasOwnProperty; Object.plain = function(f) { return f ? f instanceof Object && f.constructor === Object || Object.prototype.toString.call(f).match(/^\[object (\w+)\]$/)[1] === "Object" : !1; }, Object.empty = function(f, s) { for (var v in f) if (n.call(f, v) && (!s || s.indexOf(v) == -1)) return !1; return !0; }, Object.keys = Object.keys || function(f) { var s = []; for (var v in f) n.call(f, v) && s.push(v); return s; }, function() { var f, s = setTimeout, v = 0, l = 0, u = typeof setImmediate != "" + f && setImmediate || function(i, p) { return typeof MessageChannel == "" + f ? s : ((i = new MessageChannel()).port1.onmessage = function(_) { _.data == "" && p(); }, function(_) { p = _, i.port2.postMessage(""); }); }(), o = s.check = s.check || typeof performance != "" + f && performance || { now: function() { return +/* @__PURE__ */ new Date(); } }; s.hold = s.hold || 9, s.poll = s.poll || function(i) { if (s.hold >= o.now() - v && l++ < 3333) { i(); return; } u(function() { v = o.now(), i(); }, l = 0); }; }(), function() { var f = setTimeout, s = f.turn = f.turn || function(p) { v.push(p) == 1 && l(i); }, v = s.s = [], l = f.poll, u = 0, o, i = function() { (o = v[u++]) && o(), (u == v.length || u == 99) && (v = s.s = v.slice(u), u = 0), v.length && l(i); }; }(), function() { var f, s = setTimeout, v = s.turn; (s.each = s.each || function(l, u, o, i) { i = i || 9, function p(_, e, h) { if (e = (_ = (l || []).splice(0, i)).length) { for (var w = 0; w < e && f === (h = u(_[w])); w++) ; if (f === h) { v(p); return; } } o && o(h); }(); })(); }(); })(b, "./shim"), b(function(E) { E.exports = function n(o, s, v) { if (!o) return { to: n }; var l, u = typeof s == "function", o = (this.tag || (this.tag = {}))[o] || u && (this.tag[o] = { tag: o, to: n._ = { next: function(p) { var _; (_ = this.to) && _.next(p); } } }); if (u) { var i = { off: n.off || (n.off = function() { if (this.next === n._.next) return !0; this === this.the.last && (this.the.last = this.back), this.to.back = this.back, this.next = n._.next, this.back.to = this.to, this.the.last === this.the && delete this.on.tag[this.the.tag]; }), to: n._, next: s, the: o, on: this, as: v }; return (i.back = o.last || o).to = i, o.last = i; } return (o = o.to) && l !== s && o.next(s), o; }; })(b, "./onto"), b(function(E) { E.exports = function(n) { return n === null || typeof n == "string" || typeof n == "boolean" || // we want +/- Infinity to be, but JSON does not support it, sad face. // can you guess what v === v checks for? ;) typeof n == "number" && n != 1 / 0 && n != -1 / 0 && n === n || !!n && typeof n["#"] == "string" && Object.keys(n).length === 1 && n["#"]; }; })(b, "./valid"), b(function(E) { b("./shim"); function n() { var o = +/* @__PURE__ */ new Date(); return l < o ? (s = 0, l = o + n.drift) : l = o + (s += 1) / v + n.drift; } n.drift = 0; var f = -1 / 0, s = 0, v = 999, l = f, u; n.is = function(o, i, p) { var _ = i && o && o._ && o._[">"] || p; if (_) return typeof (_ = _[i]) == "number" ? _ : f; }, n.ify = function(o, i, p, _, e) { (o = o || {})._ = o._ || {}, e && (o._["#"] = e); var h = o._[">"] || (o._[">"] = {}); return u !== i && i !== "_" && (typeof p == "number" && (h[i] = p), u !== _ && (o[i] = _)), o; }, E.exports = n; })(b, "./state"), b(function(E) { b("./shim"); function n(f) { var s = { s: {} }, v = s.s; f = f || { max: 999, age: 1e3 * 9 }, s.check = function(u) { return v[u] ? l(u) : !1; }; var l = s.track = function(u) { var o = v[u] || (v[u] = {}); return o.was = s.now = +/* @__PURE__ */ new Date(), s.to || (s.to = setTimeout(s.drop, f.age + 9)), l.ed && l.ed(u), o; }; return s.drop = function(u) { s.to = null, s.now = +/* @__PURE__ */ new Date(); var o = Object.keys(v); console.STAT && console.STAT(s.now, +/* @__PURE__ */ new Date() - s.now, "dup drop keys"), setTimeout.each(o, function(i) { var p = v[i]; p && (u || f.age) > s.now - p.was || delete v[i]; }, 0, 99); }, s; } E.exports = n; })(b, "./dup"), b(function(E) { b("./onto"), E.exports = function(s, v) { if (this.on) { var l = (this.opt || {}).lack || 9e3; if (typeof s != "function") { if (!s) return; var o = s["#"] || s, u = (this.tag || "")[o]; return u ? (v && (u = this.on(o, v), clearTimeout(u.err), u.err = setTimeout(function() { u.off(); }, l)), !0) : void 0; } var o = v && v["#"] || n(9); if (!s) return o; var i = this.on(o, s, v); return i.err = i.err || setTimeout(function() { i.off(), i.next({ err: "Error: No ACK yet.", lack: !0 }); }, l), o; } }; var n = String.random || function() { return Math.random().toString(36).slice(2); }; })(b, "./ask"), b(function(E) { function n(e) { return e instanceof n ? (this._ = { $: this }).$ : this instanceof n ? n.create(this._ = { $: this, opt: e }) : new n(e); } n.is = function(e) { return e instanceof n || e && e._ && e === e._.$ || !1; }, n.version = 0.202, n.chain = n.prototype, n.chain.toJSON = function() { }, b("./shim"), n.valid = b("./valid"), n.state = b("./state"), n.on = b("./onto"), n.dup = b("./dup"), n.ask = b("./ask"), function() { n.create = function(r) { r.root = r.root || r, r.graph = r.graph || {}, r.on = r.on || n.on, r.ask = r.ask || n.ask, r.dup = r.dup || n.dup(); var k = r.$.opt(r.opt); return r.once || (r.on("in", e, r), r.on("out", e, r), r.on("put", m, r), n.on("create", r), r.on("create", r)), r.once = 1, k; }; function e(r) { if (r) { if (r.out === e) { this.to.next(r); return; } var k = this, A = k.as, D = A.at || A, O = D.$, T = D.dup, G, C = r.DBG; if ((G = r["#"]) || (G = r["#"] = s(9)), !T.check(G)) { if (T.track(G), G = r._, r._ = typeof G == "function" ? G : function() { }, r.$ && r.$ === (r.$._ || "").$ || (r.$ = O), r["@"] && !r.put && t(r), !D.ask(r["@"], r)) if (C && (C.u = +/* @__PURE__ */ new Date()), r.put) { h(r); return; } else r.get && n.on.get(r, O); C && (C.uc = +/* @__PURE__ */ new Date()), k.to.next(r), C && (C.ua = +/* @__PURE__ */ new Date()), !(r.nts || r.NTS) && (r.out = e, D.on("out", r), C && (C.ue = +/* @__PURE__ */ new Date())); } } } function h(r) { if (r) { var k = r._ || "", A = k.root = ((k.$ = r.$ || "")._ || "").root; if (r["@"] && k.faith && !k.miss) { r.out = e, A.on("out", r); return; } k.latch = A.hatch, k.match = A.hatch = []; var D = r.put, O = k.DBG = r.DBG, T = +/* @__PURE__ */ new Date(); if ($ = $ || T, !(D["#"] && D["."])) { O && (O.p = T), k["#"] = r["#"], k.msg = r, k.all = 0, k.stun = 1; var G = Object.keys(D); console.STAT && console.STAT(T, ((O || k).pk = +/* @__PURE__ */ new Date()) - T, "put sort"); var C = 0, U, I, P, J, M, R, W; (function H(q) { if (U != C) { if (U = C, !(P = G[C])) { console.STAT && console.STAT(T, ((O || k).pd = +/* @__PURE__ */ new Date()) - T, "put"), a(k); return; } (J = D[P]) ? (W = J._) ? P !== W["#"] ? R = y + S(P) + "soul not same." : (M = W[">"]) || (R = y + S(P) + "no state.") : R = y + S(P) + "no meta." : R = y + S(P) + "no node.", I = Object.keys(J || {}); } if (R) { r.err = k.err = R, a(k); return; } var Q = 0, F; for (q = q || 0; q++ < 9 && (F = I[Q++]); ) if (F !== "_") { var L = J[F], X = M[F]; if (i === X) { R = y + S(F) + "on" + S(P) + "no state."; break; } if (!l(L)) { R = y + S(F) + "on" + S(P) + "bad " + typeof L + S(L); break; } w(L, F, P, X, r), ++N; } if ((I = I.slice(Q)).length) { v(H); return; } ++C, I = null, H(q); })(); } } } n.on.put = h; function w(r, k, A, D, O) { var T = O._ || "", G = T.root, C = G.graph, U, I = C[A] || p, P = u(I, k, 1), J = I[k], M = T.DBG; (U = console.STAT) && (!C[A] || !J) && (U.has = (U.has || 0) + 1); var R = j(); if (D > R) { setTimeout(function() { w(r, k, A, D, O); }, (U = D - R) > x ? x : U), console.STAT && console.STAT((M || T).Hf = +/* @__PURE__ */ new Date(), U, "future"); return; } if (!(D < P) && !(!T.faith && D === P && (r === J || g(r) <= g(J)) && !T.miss)) { T.stun++; var W = O["#"] + T.all++, H = { toString: function() { return W; }, _: T }; H.toJSON = H.toString, G.dup.track(H)["#"] = O["#"], M && (M.ph = M.ph || +/* @__PURE__ */ new Date()), G.on("put", { "#": H, "@": O["@"], put: { "#": A, ".": k, ":": r, ">": D }, ok: O.ok, _: T }); } } function m(r) { var k; (k = (r._ || "").DBG) && (k.pa = +/* @__PURE__ */ new Date(), k.pm = k.pm || +/* @__PURE__ */ new Date()); var A = this, D = A.as, O = D.graph, T = r._, G = r.put, C = G["#"], U = G["."], I = G[":"], P = G[">"]; r["#"]; var J; (J = T.msg) && (J = J.put) && (J = J[C]) && o(J, U, P, I, C), O[C] = o(O[C], U, P, I, C), (J = (D.next || "")[C]) && J.on("in", r), a(T), A.to.next(r); } function a(r, k) { var A; if (!r.stop && !(!r.err && 0 < --r.stun) && (r.stop = 1, !!(A = r.root))) { var D = r.match; D.end = 1, D === A.hatch && (!(D = r.latch) || D.end ? delete A.hatch : A.hatch = D), r.hatch && r.hatch(), setTimeout.each(r.match, function(O) { O && O(); }), !(!(k = r.msg) || r.err || k.err) && (k.out = e, r.root.on("out", k), d()); } } function t(r) { var k = r["@"] || "", A; if (!(A = k._)) { var D = (D = r.$) && (D = D._) && (D = D.root) && (D = D.dup); if (!(D = D.check(k))) return; r["@"] = D["#"] || r["@"]; return; } A.acks = (A.acks || 0) + 1, (A.err = r.err) && (r["@"] = A["#"], a(A)), A.ok = r.ok || A.ok, !A.stop && !A.crack && (A.crack = A.match && A.match.push(function() { c(A); })), c(A); } function c(r) { !r || !r.root || r.stun || r.acks !== r.all || r.root.on("in", { "@": r["#"], err: r.err, ok: r.err ? i : r.ok || { "": 1 } }); } var y = "Error: Invalid graph!", S = function(r) { return " '" + ("" + r).slice(0, 9) + "...' "; }, g = JSON.stringify, x = 2147483647, j = n.state, N = 0, $, d = function() { N > 999 && N / -($ - ($ = +/* @__PURE__ */ new Date())) > 1 && (n.window && console.log("Warning: You're syncing 1K+ records a second, faster than DOM can update - consider limiting query."), d = function() { N = 0; }); }; }(), function() { n.on.get = function(h, w) { var m = w._, a = h.get, t = a["#"], c = m.graph[t], y = a["."], S = m.next || (m.next = {}), g = S[t], x = h._ || {}, j = x.DBG = h.DBG; if (j && (j.g = +/* @__PURE__ */ new Date()), !c) return m.on("get", h); if (y) { if ((typeof y != "string" || i === c[y]) && !((g || "").next || "")[y]) { m.on("get", h); return; } c = o({}, y, u(c, y), c[y], t); } c && e(h, c), m.on("get", h); }; function e(h, w) { var m = +/* @__PURE__ */ new Date(), a = h._ || {}, t = a.DBG = h.DBG, c = h["#"], y = s(9), S = Object.keys(w || "").sort(), g = ((w || "")._ || "")["#"]; S.length; var x = h.$._.root, j = w === x.graph[g]; console.STAT && console.STAT(m, ((t || a).gk = +/* @__PURE__ */ new Date()) - m, "got keys"), w && function N() { m = +/* @__PURE__ */ new Date(); for (var $ = 0, d, r = {}, k; $ < 9 && (d = S[$++]); ) o(r, d, u(w, d), w[d], g); S = S.slice($), (k = {})[g] = r, r = k; var A; j && (A = function() { }, A.ram = A.faith = !0), k = S.length, console.STAT && console.STAT(m, -(m - (m = +/* @__PURE__ */ new Date())), "got copied some"), t && (t.ga = +/* @__PURE__ */ new Date()), x.on("in", { "@": c, "#": y, put: r, "%": k ? y = s(9) : i, $: x.$, _: A, DBG: t, FOO: 1 }), console.STAT && console.STAT(m, +/* @__PURE__ */ new Date() - m, "got in"), k && setTimeout.turn(N); }(), w || x.on("in", { "@": h["#"] }); } n.on.get.ack = e; }(), function() { n.chain.opt = function(e) { e = e || {}; var h = this, w = h._, m = e.peers || e; return Object.plain(e) || (e = {}), Object.plain(w.opt) || (w.opt = e), typeof m == "string" && (m = [m]), Object.plain(w.opt.peers) || (w.opt.peers = {}), m instanceof Array && (e.peers = {}, m.forEach(function(a) { var t = {}; t.id = t.url = a, e.peers[a] = w.opt.peers[a] = w.opt.peers[a] || t; })), f(e, function a(t) { var c = this[t]; if (this && this.hasOwnProperty(t) || typeof c == "string" || Object.empty(c)) { this[t] = c; return; } c && c.constructor !== Object && !(c instanceof Array) || f(c, a); }), w.opt.from = e, n.on("opt", w), w.opt.uuid = w.opt.uuid || function(t) { return n.state().toString(36).replace(".", "") + String.random(t || 12); }, h; }; }(); var f = function(e, h) { Object.keys(e).forEach(h, e); }, s = String.random, v = setTimeout.turn, l = n.valid, u = n.state.is, o = n.state.ify, i, p = {}, _; n.log = function() { return !n.log.off && _.log.apply(_, arguments), [].slice.call(arguments).join(" "); }, n.log.once = function(e, h, w) { return (w = n.log.once)[e] = w[e] || 0, w[e]++ || n.log(h); }, typeof window < "u" && ((window.GUN = window.Gun = n).window = window); try { typeof B < "u" && (B.exports = n); } catch { } E.exports = n, (n.window || {}).console = (n.window || {}).console || { log: function() { } }, (_ = console).only = function(e, h) { return _.only.i && e === _.only.i && _.only.i++ && (_.log.apply(_, arguments) || h); }, n.log.once("welcome", "Hello wonderful person! :) Thanks for using GUN, please ask for help on http://chat.gun.eco if anything takes you longer than 5min to figure out!"); })(b, "./root"), b(function(E) { var n = b("./root"); n.chain.back = function(v, l) { var u; if (v = v || 1, v === -1 || v === 1 / 0) return this._.root.$; if (v === 1) return (this._.back || this._).$; var o = this, i = o._; if (typeof v == "string" && (v = v.split(".")), v instanceof Array) { var p = 0, _ = v.length, u = i; for (p; p < _; p++) u = (u || f)[v[p]]; return s !== u ? l ? o : u : (u = i.back) ? u.$.back(v, l) : void 0; } if (typeof v == "function") { for (var e, u = { back: i }; (u = u.back) && s === (e = v(u, l)); ) ; return e; } return typeof v == "number" ? (i.back || i).$.back(v - 1) : this; }; var f = {}, s; })(b, "./back"), b(function(E) { var n = b("./root"); n.chain.chain = function(a) { var t = this, c = t._, y = new (a || t).constructor(t), S = y._, g; return S.root = g = c.root, S.id = ++g.once, S.back = t._, S.on = n.on, S.on("in", n.on.in, S), S.on("out", n.on.out, S), y; }; function f(a) { var t, c = this.as, y = c.back, S = c.root, g; if (a.$ || (a.$ = c.$), this.to.next(a), c.err) { c.on("in", { put: c.put = i, $: c.$ }); return; } if (t = a.get) { if (S.pass && (S.pass[c.id] = c), c.lex && Object.keys(c.lex).forEach(function(x) { g[x] = c.lex[x]; }, g = a.get = a.get || {}), t["#"] || c.soul) { if (t["#"] = t["#"] || c.soul, a["#"] || (a["#"] = p(9)), y = S.$.get(t["#"])._, t = t["."]) { if (e(y.put, t) && (g = y.ask && y.ask[t], (y.ask || (y.ask = {}))[t] = y.$.get(t)._, y.on("in", { get: t, put: { "#": y.soul, ".": t, ":": y.put[t], ">": w(S.graph[y.soul], t) } }), g)) return; } else { if (g = y.ask && y.ask[""], (y.ask || (y.ask = {}))[""] = y, i !== y.put && (y.on("in", y), g)) return; a.$ = y.$; } return S.ask(u, a), S.on("in", a); } if (t["."]) return c.get ? (a = { get: { ".": c.get }, $: c.$ }, (y.ask || (y.ask = {}))[c.get] = a.$._, y.on("out", a)) : (a = { get: c.lex ? a.get : {}, $: c.$ }, y.on("out", a)); if ((c.ask || (c.ask = {}))[""] = c, c.get) return t["."] = c.get, (y.ask || (y.ask = {}))[c.get] = a.$._, y.on("out", a); } return y.on("out", a); } n.on.out = f; function s(a, t) { t = t || this.as; var c = t.root, y = a.$ || (a.$ = t.$), S = (y || "")._ || o, g = a.put || "", x = g["#"], j = g["."], N = i !== g["="] ? g["="] : g[":"], $ = g[">"] || -1 / 0, d; if (i !== a.put && (i === g["#"] || i === g["."] || i === g[":"] && i === g["="] || i === g[">"])) { if (!_(g)) { if (!(x = ((g || "")._ || "")["#"])) { console.log("chain not yet supported for", g, "...", a, t); return; } return y = t.root.$.get(x), setTimeout.each(Object.keys(g).sort(), function(r) { r == "_" || i === ($ = w(g, r)) || t.on("in", { $: y, put: { "#": x, ".": r, "=": g[r], ">": $ }, VIA: a }); }); } t.on("in", { $: S.back.$, put: { "#": x = S.back.soul, ".": j = S.has || S.get, "=": g, ">": w(S.back.put, j) }, via: a }); return; } (a.seen || "")[t.id] || ((a.seen || (a.seen = function() { }))[t.id] = t, t !== S && (Object.keys(a).forEach(function(r) { g[r] = a[r]; }, g = {}), g.get = t.get || g.get, !t.soul && !t.has ? g.$$$ = g.$$$ || t.$ : S.soul && (g.$ = t.$, g.$$ = g.$$ || S.$), a = g), l(a, t), (t.soul || a.$$) && $ >= w(c.graph[x], j) && ((g = c.$.get(x)._).put = m(g.put, j, $, N, x)), !S.soul && $ >= w(c.graph[x], j) && (d = (c.$.get(x)._.next || "")[j]) && (d.put = N, typeof (g = _(N)) == "string" && (d.put = c.$.get(g)._.put || N)), this.to && this.to.next(a), t.any && setTimeout.each(Object.keys(t.any), function(r) { (r = t.any[r]) && r(a); }, 0, 99), t.echo && setTimeout.each(Object.keys(t.echo), function(r) { (r = t.echo[r]) && r.on("in", a); }, 0, 99), ((a.$$ || "")._ || S).soul && (d = t.next) && (d = d[j]) && (g = {}, Object.keys(a).forEach(function(r) { g[r] = a[r]; }), g.$ = (a.$$ || a.$).get(g.get = j), delete g.$$, delete g.$$$, d.on("in", g)), v(a, t)); } n.on.in = s; function v(a, t) { if (t = t || this.as || a.$._, !(a.$$ && this !== n.on) && !(!a.put || t.soul)) { var c = a.put || "", y = c["="] || c[":"], j, S = t.root, g = S.$.get(c["#"]).get(c["."])._; if (typeof (y = _(y)) != "string") { this === n.on && ((g.echo || (g.echo = {}))[t.id] = t); return; } if (!((g.echo || (g.echo = {}))[t.id] && !(S.pass || "")[t.id])) { if (j = S.pass) { if (j[y + t.id]) return; j[y + t.id] = 1; } (g.echo || (g.echo = {}))[t.id] = t, t.has && (t.link = y); var x = S.$.get(g.link = y)._; (x.echo || (x.echo = {}))[g.id] = g; var j = t.ask || ""; (j[""] || t.lex) && x.on("out", { get: { "#": y } }), setTimeout.each(Object.keys(j), function(N, $) { !N || !($ = j[N]) || $.on("out", { get: { "#": y, ".": N } }); }, 0, 99); } } } n.on.link = v; function l(a, t) { var c = a.put || "", y = i !== c["="] ? c["="] : c[":"], S = t.root, g, x; if (i === y) { if (t.soul && i !== t.put || (x = (a.$$ || a.$ || "")._ || "", a["@"] && (i !== x.put || i !== t.put))) return; (g = t.link || a.linked) && delete (S.$.get(g)._.echo || "")[t.id], t.has && (t.link = null), t.put = i, setTimeout.each(Object.keys(t.next || ""), function(j, N) { (N = t.next[j]) && (g && delete (S.$.get(g).get(j)._.echo || "")[N.id], N.on("in", { get: j, put: i, $: N.$ })); }, 0, 99); return; } t.soul || a.$$ || (g = _(y), x = a.$._ || "", !((g === x.link || t.has && !x.link) && !((S.pass || "")[t.id] && typeof g != "string")) && (delete (x.echo || "")[t.id], l({ get: t.get, put: i, $: a.$, linked: a.linked = a.linked || x.link }, t))); } n.on.unlink = l; function u(a, t) { var c = this.as, y = c.$._; y.root; var S = c.get || "", g = (a.put || "")[S["#"]] || ""; if (!a.put || typeof S["."] == "string" && i === g[S["."]]) { if (i !== y.put || !y.soul && !y.has) return; y.ack = (y.ack || 0) + 1, y.on("in", { get: y.get, put: y.put = i, $: y.$, "@": a["@"] }); return; } (a._ || {}).miss = 1, n.on.put(a); } var o = {}, i, p = String.random, _ = n.valid, e = function(a, t) { return a && Object.prototype.hasOwnProperty.call(a, t); }, h = n.state, w = h.is, m = h.ify; })(b, "./chain"), b(function(E) { var n = b("./root"); n.chain.get = function(i, p, _) { var e, h; if (typeof i == "string") { if (i.length == 0) return (e = this.chain())._.err = { err: n.log("0 length key!", i) }, p && p.call(e, e._.err), e; var w = this, m = w._, a = m.next || l; (e = a[i]) || (e = i && f(i, w)), e = e && e.$; } else if (typeof i == "function") { let x = function(j, N, $) { if (!x.stun && !((O = c.pass) && !O[y])) { var d = j.$._, r = (j.$$ || "")._, k = (r || d).put, A = !d.has && !d.soul, D = {}, O; if ((A || o === k) && (k = o === ((O = j.put) || "")["="] ? o === (O || "")[":"] ? O : O[":"] : O["="]), typeof (O = n.valid(k)) == "string" && (k = o === (O = c.$.get(O)._.put) ? t.not ? o : k : O), !(t.not && o === k)) { if (o === t.stun) { if ((O = c.stun) && O.on && (m.$.back(function(T) { if (O.on("" + T.id, D = {}), (D.run || 0) < x.id) return D; }), !D.run && O.on("" + d.id, D = {}), !D.run && r && O.on("" + r.id, D = {}), x.id > D.run && ((!D.stun || D.stun.end) && (D.stun = O.on("stun"), D.stun = D.stun && D.stun.last), D.stun && !D.stun.end))) { (D.stun.add || (D.stun.add = {}))[y] = function() { x(j, N, 1); }; return; } if ( /*odd &&*/ o === k && ($ = 0), (O = c.hatch) && !O.end && o === t.hatch && !$ ) { if (S[d.$._.id]) return; S[d.$._.id] = 1, O.push(function() { x(j, N, 1); }); return; } S = {}; } if (c.pass) { if (c.pass[y + d.id]) return; c.pass[y + d.id] = 1; } if (t.on) { t.ok.call(d.$, k, d.get, j, N || x); return; } if (t.v2020) { t.ok(j, N || x); return; } Object.keys(j).forEach(function(T) { O[T] = j[T]; }, O = {}), j = O, j.put = k, t.ok.call(t.as, j, N || x); } } }; if (p === !0) return s(this, i, p, _), this; e = this; var m = e._, t = p || {}, c = m.root, y; t.at = m, t.ok = i; var S = {}; return x.at = m, (m.any || (m.any = {}))[y = String.random(7)] = x, x.off = function() { x.stun = 1, m.any && delete m.any[y]; }, x.rid = v, x.id = t.run || ++c.once, h = c.pass, (c.pass = {})[y] = 1, t.out = t.out || { get: {} }, m.on("out", t.out), c.pass = h, e; } else { if (typeof i == "number") return this.get("" + i, p, _); if (typeof (h = u(i)) == "string") return this.get(h, p, _); (h = this.get.next) && (e = h(this, i)); } return e ? (p && typeof p == "function" && e.get(p, _), e) : ((e = this.chain())._.err = { err: n.log("Invalid get request!", i) }, p && p.call(e, e._.err), e); }; function f(i, p) { var _ = p._, e = _.next, h = p.chain(), w = h._; return e || (e = _.next = {}), e[w.get = i] = w, p === _.root.$ ? w.soul = i : (_.soul || _.has) && (w.has = i), w; } function s(i, p, _, e) { var h = i._, w = 0, m; return (m = h.soul || h.link) ? p(m, e, h) : h.jam ? h.jam.push([p, e]) : (h.jam = [[p, e]], i.get(function(t, c) { if (!(o === t.put && !h.root.opt.super && (m = Object.keys(h.root.opt.peers).length) && ++w <= m)) { c.rid(t); var y = (y = t.$) && y._ || {}, S = 0, g; for (m = h.jam, delete h.jam; g = m[S++]; ) { var x = g[0]; g = g[1], x && x(y.link || y.soul || n.valid(t.put) || ((t.put || {})._ || {})["#"], g, t, c); } } }, { out: { get: { ".": !0 } } }), i); } function v(i) { var p = this.at || this.on; if (!i || p.soul || p.has) return this.off(); if (i = (i = (i = i.$ || i)._ || i).id) { p.map; var _; if ((_ = this.seen || (this.seen = {}))[i]) return !0; _[i] = !0; } } var l = {}, u = n.valid, o; })(b, "./get"), b(function(E) { var n = b("./root"); n.chain.put = function(e, h, w) { var m = this, a = m._, t = a.root; w = w || {}, w.root = a.root, w.run || (w.run = t.once), f(w, a.id), w.ack = w.ack || h, w.via = w.via || m, w.data = w.data || e, w.soul || (w.soul = a.soul || typeof h == "string" && h); var c = w.state = w.state || n.state(); return typeof e == "function" ? (e(function(y) { w.data = y, m.put(u, u, w); }), m) : w.soul ? (w.$ = t.$.get(w.soul), w.todo = [{ it: w.data, ref: w.$ }], w.turn = w.turn || i, w.ran = w.ran || s, function y() { var S = w.todo, g = S.pop(), x = g.it; g.ref && g.ref._.id; var j, N, $, d, r; if (f(w, g.ref), (d = g.todo) && (N = d.pop(), x = x[N], d.length && S.push(g)), N && (S.path || (S.path = [])).push(N), !(j = p(x)) && !(r = n.is(x))) { if (!Object.plain(x)) { s.err(w, "Invalid data: " + l(x) + " at " + (w.via.back(function(O) { O.get && d.push(O.get); }, d = []) || d.join(".")) + "." + (S.path || []).join(".")); return; } for (var k = w.seen || (w.seen = []), A = k.length; A--; ) if (x === (d = k[A]).it) { j = x = d.link; break; } } if (N && j) g.node = _(g.node, N, c, x); else { let O = function(T, G) { var C = $.link["#"]; G && (G.off(), G.rid(T)); var U = C || T.soul || (d = (T.$$ || T.$)._ || "").soul || d.link || ((d = d.put || "")._ || "")["#"] || d["#"] || ((d = T.put || "") && T.$$ ? d["#"] : (d["="] || d[":"] || "")["#"]); if (!C && f(w, T.$), !U && !g.link["#"]) { (g.wait || (g.wait = [])).push(function() { O(T, G); }); return; } U || (U = [], (T.$$ || T.$).back(function(I) { if (d = I.soul || I.link) return U.push(d); U.push(I.get); }), U = U.reverse().join("/")), $.link["#"] = U, !r && (((w.graph || (w.graph = {}))[U] = $.node || ($.node = { _: {} }))._["#"] = U), delete w.wait[D], $.wait && setTimeout.each($.wait, function(I) { I && I(); }), w.ran(w); }; if (!w.seen) { s.err(w, "Data at root of graph must be a node (an object)."); return; } w.seen.push($ = { it: x, link: {}, todo: r ? [] : Object.keys(x).sort().reverse(), path: (S.path || []).slice(), up: g }), g.node = _(g.node, N, c, $.link), !r && $.todo.length && S.push($); var D = w.seen.length; (w.wait || (w.wait = {}))[D] = "", d = ($.ref = r ? x : N ? g.ref.get(N) : g.ref)._, (d = x && (x._ || "")["#"] || d.soul || d.link) ? O({ soul: d }) : $.ref.get(O, { run: w.run, /*hatch: 0,*/ v2020: 1, out: { get: { ".": " " } } }); } if (!S.length) return w.ran(w); w.turn(y); }(), m) : (v(w), m); }; function f(e, h) { if (h) { h = (h._ || "").id || h; var w = e.root.stun || (e.root.stun = { on: n.on }), m = {}, a; e.stun || (e.stun = w.on("stun", function() { })), (a = w.on("" + h)) && a.the.last.next(m), !(m.run >= e.run) && w.on("" + h, function(t) { if (e.stun.end) { this.off(), this.to.next(t); return; } t.run = t.run || e.run, t.stun = t.stun || e.stun; }); } } function s(e) { if (e.err) { s.end(e.stun, e.root); return; } if (!(e.todo.length || e.end || !Object.empty(e.wait))) { e.end = 1; var h = e.$.back(-1)._, w = h.root, m = h.ask(function(y) { w.on("ack", y), y.err && !y.lack && n.log(y), ++a > (e.acks || 0) && this.off(), e.ack && e.ack(y, this); }, e.opt), a = 0, t = e.stun, c; (c = function() { t && (s.end(t, w), setTimeout.each(Object.keys(t = t.add || ""), function(y) { (y = t[y]) && y(); })); }).hatch = c, e.ack && !e.ok && (e.ok = e.acks || 9), e.via._.on("out", { put: e.out = e.graph, ok: e.ok && { "@": e.ok + 1 }, opt: e.opt, "#": m, _: c }); } } s.end = function(e, h) { e.end = o, e.the.to === e && e === e.the.last && delete h.stun, e.off(); }, s.err = function(e, h) { (e.ack || o).call(e, e.out = { err: e.err = n.log(h) }), e.ran(e); }; function v(e) { var h = e.via._, w; e.via = e.via.back(function(m) { if (m.soul || !m.get) return m.$; w = e.data, (e.data = {})[m.get] = w; }), (!e.via || !e.via._.soul) && (e.via = h.root.$.get(((e.data || "")._ || "")["#"] || h.$.back("opt.uuid")())), e.via.put(e.data, e.ack, e); } function l(e, h) { return e && (h = e.constructor) && h.name || typeof e; } var u, o = function() { }, i = setTimeout.turn, p = n.valid, _ = n.state.ify; })(b, "./put"), b(function(E) { var n = b("./root"); b("./chain"), b("./back"), b("./put"), b("./get"), E.exports = n; })(b, "./index"), b(function(E) { var n = b("./index"); n.chain.on = function(v, l, u, o) { var i = this, p = i._; p.root; var _; if (typeof v == "string") return l ? (_ = p.on(v, l, u || p, o), u && u.$ && (u.subs || (u.subs = [])).push(_), i) : p.on(v); var e = l; return (e = e === !0 ? { change: !0 } : e || {}).not = 1, e.on = 1, i.get(v, e), i; }, n.chain.once = function(v, l) { if (l = l || {}, !v) return f(this); var u = this, o = u._, i = o.root; o.put; var p = String.random(7), _; return u.get(function(e, h, w, m) { var a = this, t = a._, c = t.one || (t.one = {}); if (m.stun || c[p] === "") return; if ((_ = n.valid(e)) === !0) { y(); return; } if (typeof _ == "string") return; clearTimeout((o.one || "")[p]), clearTimeout(c[p]), c[p] = setTimeout(y, l.wait || 99); function y(S) { if (!t.has && !t.soul && (t = { put: e, get: h }), s === (_ = t.put) && (_ = ((w.$$ || "")._ || "").put), typeof n.valid(_) == "string" && (_ = i.$.get(_)._.put, _ === s && !S)) { c[p] = setTimeout(function() { y(1); }, l.wait || 99); return; } m.stun || c[p] !== "" && (c[p] = "", (o.soul || o.has) && m.off(), v.call(a, _, t.get), clearTimeout(c[p])); } }, { on: 1 }), u; }; function f(v, l, u) { return n.log.once("valonce", "Chainable val is experimental, its behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it."), (u = v.chain())._.nix = v.once(function(o, i) { u._.on("in", this._); }), u._.lex = v._.lex, u; } n.chain.off = function() { var v = this, l = v._, u, o = l.back; if (o) return l.ack = 0, (u = o.next) && u[l.get] && delete u[l.get], (u = o.any) && (delete o.any, o.any = {}), (u = o.ask) && delete u[l.get], (u = o.put) && delete u[l.get], (u = l.soul) && delete o.root.graph[u], (u = l.map) && Object.keys(u).forEach(function(i, p) { p = u[i], p.link && o.root.$.get(p.link).off(); }), (u = l.next) && Object.keys(u).forEach(function(i, p) { p = u[i], p.$.off(); }), l.on("off", {}), v; }; var s; })(b, "./on"), b(function(E) { var n = b("./index"), f = n.chain.get.next; n.chain.get.next = function(u, o) { var i; return Object.plain(o) ? (i = ((i = o["#"]) || "")["="] || i) ? u.get(i) : ((i = u.chain()._).lex = o, u.on("in", function(p) { String.match(p.get || (p.put || "")["."], o["."] || o["#"] || o) && i.on("in", p), this.to.next(p); }), i.$) : (f || v)(u, o); }, n.chain.map = function(u, o, i) { var p = this, _ = p._, e, h; return Object.plain(u) && (e = u["."] ? u : { ".": u }, u = l), u ? (n.log.once("mapfn", "Map functions are experimental, their behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it."), h = p.chain(), p.map().on(function(w, m, a, t) { var c = (u || v).call(this, w, m, a, t); if (l !== c) { if (w === c) return h._.on("in", a); if (n.is(c)) return h._.on("in", c._); var y = {}; Object.keys(a.put).forEach(function(S) { y[S] = a.put[S]; }, y), y["="] = c, h._.on("in", { get: m, put: y }); } }), h) : ((h = _.each) || ((_.each = h = p.chain())._.lex = e || h._.lex || _.lex, h._.nix = p.back("nix"), p.on("in", s, h._)), h); }; function s(u) { this.to.next(u); var o = this.as, i = u.$, p = i._, _ = u.put, e; !p.soul && !u.$$ || (e = o.lex) && !String.match(u.get || (_ || "")["."], e["."] || e["#"] || e) || n.on.link(u, o); } var v = function() { }, l; })(b, "./map"), b(function(E) { var n = b("./index"); n.chain.set = function(f, s, v) { var l = this, u = l.back(-1), o, i; return s = s || function() { }, v = v || {}, v.item = v.item || f, (o = ((f || "")._ || "")["#"]) && ((f = {})["#"] = o), typeof (i = n.valid(f)) == "string" ? l.get(o = i).put(f, s, v) : n.is(f) ? (l.put(function(p) { f.get(function(_, e, h) { if (!_) return s.call(l, { err: n.log('Only a node can be linked! Not "' + h.put + '"!') }); (i = {})[_] = { "#": _ }, p(i); }, !0); }), f) : (Object.plain(f) && (f = u.get(o = l.back("opt.uuid")()).put(f)), l.get(o || u.back("opt.uuid")(7)).put(f, s, v)); }; })(b, "./set"), b(function(E) { b("./shim"); var n = function() { }, f = JSON.parseAsync || function(u, o, i) { var p, _ = +/* @__PURE__ */ new Date(); try { o(p, JSON.parse(u, i), s.sucks(+/* @__PURE__ */ new Date() - _)); } catch (e) { o(e); } }, s = JSON.stringifyAsync || function(u, o, i, p) { var _, e = +/* @__PURE__ */ new Date(); try { o(_, JSON.stringify(u, i, p), s.sucks(+/* @__PURE__ */ new Date() - e)); } catch (h) { o(h); } }; s.sucks = function(u) { u > 99 && (console.log("Warning: JSON blocking CPU detected. Add `gun/lib/yson.js` to fix."), s.sucks = n); }; function v(u) { var o = function() { }, i = u.opt || {}; i.log = i.log || console.log, i.gap = i.gap || i.wait || 0, i.max = i.max || (i.memory ? i.memory * 999 * 999 : 3e8) * 0.3, i.pack = i.pack || i.max * 0.01 * 0.01, i.puff = i.puff || 9; var p = setTimeout.turn || setTimeout, _ = u.dup, e = _.check, h = _.track, w = o.hear = function(t, c) { if (t) { if (i.max <= t.length) return o.say({ dam: "!", err: "Message too big!" }, c); o === this && (w.d += t.length || 0, ++w.c); var y = c.SH = +/* @__PURE__ */ new Date(), S = t[0], g; if (S === "[") { f(t, function(x, j) { if (x || !j) return o.say({ dam: "!", err: "DAM JSON parse error." }, c); console.STAT && console.STAT(+/* @__PURE__ */ new Date(), j.length, "# on hear batch"); var N = i.puff; (function $() { for (var d = +/* @__PURE__ */ new Date(), r = 0, k; r < N && (k = j[r++]); ) o.hear(k, c); j = j.slice(r), console.STAT && console.STAT(d, +/* @__PURE__ */ new Date() - d, "hear loop"), m(c), j.length && p($, 0); })(); }), t = ""; return; } if (S === "{" || (t["#"] || Object.plain(t)) && (g = t)) { if (g) return w.one(g, c, y); f(t, function(x, j) { if (x || !j) return o.say({ dam: "!", err: "DAM JSON parse error." }, c); w.one(j, c, y); }); return; } } }; w.one = function(t, c, $) { var S, g, x, j, N; if (t.DBG && (t.DBG = N = { DBG: t.DBG }), N && (N.h = $), N && (N.hp = +/* @__PURE__ */ new Date()), (S = t["#"]) || (S = t["#"] = String.random(9)), !(x = e(S)) && (g = t["##"], !(g && (x = t["@"] || t.get && S) && _.check(j = x + g)))) { if ((t._ = function() { }).via = o.leap = c, (x = t["><"]) && typeof x == "string" && x.slice(0, 99).split(",").forEach(function(d) { this[d] = 1; }, t._.yo = {}), x = t.dam) { (x = o.hear[x]) && x(t, c, u), h(S); return; } (x = t.ok) && (t._.near = x["/"]); var $ = +/* @__PURE__ */ new Date(); N && (N.is = $), c.SI = S, h.ed = function(d) { S === d && (h.ed = 0, (d = _.s[S]) && (d.via = c, t.get && (d.it = t))); }, u.on("in", o.last = t), N && (N.hd = +/* @__PURE__ */ new Date()), console.STAT && console.STAT($, +/* @__PURE__ */ new Date() - $, t.get ? "msg get" : t.put ? "msg put" : "msg"), h(S), j && h(j), o.leap = o.last = null; } }, w.c = w.d = 0, function() { var t = 0, c; o.hash = function(g, x) { var j, N, $, d = +/* @__PURE__ */ new Date(); s(g.put, function r(k, A) { var D = (N || (N = $ = A || "")).slice(0, 32768); if (j = String.hash(D, j), N = N.slice(32768), N) { p(r, 0); return; } console.STAT && console.STAT(d, +/* @__PURE__ */ new Date() - d, "say json+hash"), g._.$put = $, g["##"] = j, o.say(g, x), delete g._.$put; }, y); }; function y(g, x) { var j; return x instanceof Object ? (Object.keys(x).sort().forEach(S, { to: j = {}, on: x }), j) : x; } function S(g) { this.to[g] = this.on[g]; } o.say = function(g, x) { var j; if ((j = this) && (j = j.to) && j.next && j.next(g), !g) return !1; var N, $, d, r = g["@"], k = g._ || (g._ = function() { }), A = g.DBG, D = +/* @__PURE__ */ new Date(); if (k.y = k.y || D, x || A && (A.y = D), (N = g["#"]) || (N = g["#"] = String.random(9)), !c && h(N), !($ = g["##"]) && l !== g.put && !k.via && r) { o.hash(g, x); return; } if (!x && r && (x = (j = _.s[r]) && (j.via || (j = j.it) && (j = j._) && j.via) || (j = o.last) && r === j["#"] && o.leap), !x && r) return _.s[r] ? void 0 : (console.STAT && console.STAT(+/* @__PURE__ */ new Date(), ++t, "total no peer to ack to"), !1); if (r && !g.put && !$ && ((_.s[r] || "").it || "")["##"]) return !1; if (!x && o.way) return o.way(g); if (A && (A.yh = +/* @__PURE__ */ new Date()), !(d = k.raw)) { o.raw(g, x); return; } if (A && (A.yr = +/* @__PURE__ */ new Date()), !x || !x.id) { if (!Object.plain(x || i.peers)) return !1; var D = +/* @__PURE__ */ new Date(); i.puff; var O = i.peers, T = Object.keys(x || i.peers || {}); console.STAT && console.STAT(D, +/* @__PURE__ */ new Date() - D, "peer keys"), function U() { var I = +/* @__PURE__ */ new Date(); c = 1; var P = k.raw; k.raw = d; for (var J = 0, M; J < 9 && (M = (T || "")[J++]); ) (M = O[M] || (x || "")[M]) && o.say(g, M); k.raw = P, c = 0, T = T.slice(J), console.STAT && console.STAT(I, +/* @__PURE__ */ new Date() - I, "say loop"), T.length && (p(U, 0), r && h(r)); }(); return; } if (!x.wire && o.wire && o.wire(x), N !== x.last) { if (x.last = N, x === k.via || (j = k.yo) && (j[x.url] || j[x.pid] || j[x.id])) return !1; if (console.STAT && console.STAT(D, ((A || k).yp = +/* @__PURE__ */ new Date()) - (k.y || D), "say prep"), !c && r && h(r), x.batch) { if (x.tail = (j = x.tail || 0) + d.length, x.tail <= i.pack) { x.batch += (j ? "," : "") + d; return; } m(x); } x.batch = "["; var G = +/* @__PURE__ */ new Date(); setTimeout(function() { console.STAT && console.STAT(G, +/* @__PURE__ */ new Date() - G, "0ms TO"), m(x); }, i.gap), a(d, x), console.STAT && r === x.SI && console.STAT(D, +/* @__PURE__ */ new Date() - x.SH, "say ack"); } }, o.say.c = o.say.d = 0, o.raw = function(g, x) { if (!g) return ""; var j = g._ || {}, N, $; if ($ = j.raw) return $; if (typeof g == "string") return g; var d = g["##"], r