holosphere
Version:
Holonic Geospatial Communication Infrastructure
1,339 lines (1,337 loc) • 1.25 MB
JavaScript
/**
* HoloSphere Bundle v1.1.20
* Holonic Geospatial Communication Infrastructure
*
* Includes:
* - HoloSphere core
* - Gun.js (real-time database)
* - H3.js (geospatial indexing)
* - Ajv (JSON schema validation)
*
* Usage:
* <script src="https://unpkg.com/holosphere@1.1.20/holosphere-bundle.js"></script>
* <script>
* const hs = new HoloSphere('myapp');
* </script>
*
* @author Roberto Valenti
* @license GPL-3.0-or-later
*/
var HoloSphere = (() => {
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined") return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});
var __commonJS = (cb, mod) => function __require2() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// node_modules/gun/gun.js
var require_gun = __commonJS({
"node_modules/gun/gun.js"(exports, module) {
(function() {
function USE(arg, req) {
return req ? __require(arg) : arg.slice ? USE[R(arg)] : function(mod, path) {
arg(mod = { exports: {} });
USE[R(path)] = mod.exports;
};
function R(p) {
return p.split("/").slice(-1).toString().replace(".js", "");
}
}
if (typeof module !== "undefined") {
var MODULE = module;
}
;
USE(function(module2) {
String.random = function(l, c) {
var s = "";
l = l || 24;
c = c || "0123456789ABCDEFGHIJKLMNOPQRSTUVWXZabcdefghijklmnopqrstuvwxyz";
while (l-- > 0) {
s += c.charAt(Math.floor(Math.random() * c.length));
}
return s;
};
String.match = function(t, o) {
var tmp, u;
if ("string" !== typeof t) {
return false;
}
if ("string" == typeof o) {
o = { "=": o };
}
o = o || {};
tmp = o["="] || o["*"] || o[">"] || o["<"];
if (t === tmp) {
return true;
}
if (u !== o["="]) {
return false;
}
tmp = o["*"] || o[">"];
if (t.slice(0, (tmp || "").length) === tmp) {
return true;
}
if (u !== o["*"]) {
return false;
}
if (u !== o[">"] && u !== o["<"]) {
return t >= o[">"] && t <= o["<"] ? true : false;
}
if (u !== o[">"] && t >= o[">"]) {
return true;
}
if (u !== o["<"] && t <= o["<"]) {
return true;
}
return false;
};
String.hash = function(s, c) {
if (typeof s !== "string") {
return;
}
c = c || 0;
if (!s.length) {
return c;
}
for (var i = 0, l = s.length, n; i < l; ++i) {
n = s.charCodeAt(i);
c = (c << 5) - c + n;
c |= 0;
}
return c;
};
var has2 = Object.prototype.hasOwnProperty;
Object.plain = function(o) {
return o ? o instanceof Object && o.constructor === Object || Object.prototype.toString.call(o).match(/^\[object (\w+)\]$/)[1] === "Object" : false;
};
Object.empty = function(o, n) {
for (var k in o) {
if (has2.call(o, k) && (!n || -1 == n.indexOf(k))) {
return false;
}
}
return true;
};
Object.keys = Object.keys || function(o) {
var l = [];
for (var k in o) {
if (has2.call(o, k)) {
l.push(k);
}
}
return l;
};
(function() {
var u, sT = setTimeout, l = 0, c = 0, sI = typeof setImmediate !== "" + u && setImmediate || function(c2, f) {
if (typeof MessageChannel == "" + u) {
return sT;
}
(c2 = new MessageChannel()).port1.onmessage = function(e) {
"" == e.data && f();
};
return function(q) {
f = q;
c2.port2.postMessage("");
};
}(), check = sT.check = sT.check || typeof performance !== "" + u && performance || { now: function() {
return +/* @__PURE__ */ new Date();
} };
sT.hold = sT.hold || 9;
sT.poll = sT.poll || function(f) {
if (sT.hold >= check.now() - l && c++ < 3333) {
f();
return;
}
sI(function() {
l = check.now();
f();
}, c = 0);
};
})();
;
(function() {
var sT = setTimeout, t = sT.turn = sT.turn || function(f2) {
1 == s.push(f2) && p(T);
}, s = t.s = [], p = sT.poll, i = 0, f, T = function() {
if (f = s[i++]) {
f();
}
if (i == s.length || 99 == i) {
s = t.s = s.slice(i);
i = 0;
}
if (s.length) {
p(T);
}
};
})();
;
(function() {
var u, sT = setTimeout, T = sT.turn;
(sT.each = sT.each || function(l, f, e, S) {
S = S || 9;
(function t(s, L, r) {
if (L = (s = (l || []).splice(0, S)).length) {
for (var i = 0; i < L; i++) {
if (u !== (r = f(s[i]))) {
break;
}
}
if (u === r) {
T(t);
return;
}
}
e && e(r);
})();
})();
})();
})(USE, "./shim");
;
USE(function(module2) {
module2.exports = function onto(tag, arg, as) {
if (!tag) {
return { to: onto };
}
var u, f = "function" == typeof arg, tag = (this.tag || (this.tag = {}))[tag] || f && (this.tag[tag] = { tag, to: onto._ = { next: function(arg2) {
var tmp;
if (tmp = this.to) {
tmp.next(arg2);
}
} } });
if (f) {
var be = {
off: onto.off || (onto.off = function() {
if (this.next === onto._.next) {
return true;
}
if (this === this.the.last) {
this.the.last = this.back;
}
this.to.back = this.back;
this.next = onto._.next;
this.back.to = this.to;
if (this.the.last === this.the) {
delete this.on.tag[this.the.tag];
}
}),
to: onto._,
next: arg,
the: tag,
on: this,
as
};
(be.back = tag.last || tag).to = be;
return tag.last = be;
}
if ((tag = tag.to) && u !== arg) {
tag.next(arg);
}
return tag;
};
})(USE, "./onto");
;
USE(function(module2) {
module2.exports = function(v) {
return v === null || "string" === typeof v || "boolean" === typeof v || // we want +/- Infinity to be, but JSON does not support it, sad face.
// can you guess what v === v checks for? ;)
"number" === typeof v && v != Infinity && v != -Infinity && v === v || !!v && "string" == typeof v["#"] && Object.keys(v).length === 1 && v["#"];
};
})(USE, "./valid");
;
USE(function(module2) {
USE("./shim");
function State() {
var t = +/* @__PURE__ */ new Date();
if (last < t) {
return N = 0, last = t + State.drift;
}
return last = t + (N += 1) / D + State.drift;
}
State.drift = 0;
var NI = -Infinity, N = 0, D = 999, last = NI, u;
State.is = function(n, k, o) {
var tmp = k && n && n._ && n._[">"] || o;
if (!tmp) {
return;
}
return "number" == typeof (tmp = tmp[k]) ? tmp : NI;
};
State.ify = function(n, k, s, v, soul) {
(n = n || {})._ = n._ || {};
if (soul) {
n._["#"] = soul;
}
var tmp = n._[">"] || (n._[">"] = {});
if (u !== k && k !== "_") {
if ("number" == typeof s) {
tmp[k] = s;
}
if (u !== v) {
n[k] = v;
}
}
return n;
};
module2.exports = State;
})(USE, "./state");
;
USE(function(module2) {
USE("./shim");
function Dup(opt) {
var dup = { s: {} }, s = dup.s;
opt = opt || { max: 999, age: 1e3 * 9 };
dup.check = function(id) {
if (!s[id]) {
return false;
}
return dt(id);
};
var dt = dup.track = function(id) {
var it = s[id] || (s[id] = {});
it.was = dup.now = +/* @__PURE__ */ new Date();
if (!dup.to) {
dup.to = setTimeout(dup.drop, opt.age + 9);
}
if (dt.ed) {
dt.ed(id);
}
return it;
};
dup.drop = function(age) {
dup.to = null;
dup.now = +/* @__PURE__ */ new Date();
var l = Object.keys(s);
console.STAT && console.STAT(dup.now, +/* @__PURE__ */ new Date() - dup.now, "dup drop keys");
setTimeout.each(l, function(id) {
var it = s[id];
if (it && (age || opt.age) > dup.now - it.was) {
return;
}
delete s[id];
}, 0, 99);
};
return dup;
}
module2.exports = Dup;
})(USE, "./dup");
;
USE(function(module2) {
USE("./onto");
module2.exports = function ask(cb, as) {
if (!this.on) {
return;
}
var lack = (this.opt || {}).lack || 9e3;
if (!("function" == typeof cb)) {
if (!cb) {
return;
}
var id = cb["#"] || cb, tmp = (this.tag || "")[id];
if (!tmp) {
return;
}
if (as) {
tmp = this.on(id, as);
clearTimeout(tmp.err);
tmp.err = setTimeout(function() {
tmp.off();
}, lack);
}
return true;
}
var id = as && as["#"] || random(9);
if (!cb) {
return id;
}
var to = this.on(id, cb, as);
to.err = to.err || setTimeout(function() {
to.off();
to.next({ err: "Error: No ACK yet.", lack: true });
}, lack);
return id;
};
var random = String.random || function() {
return Math.random().toString(36).slice(2);
};
})(USE, "./ask");
;
USE(function(module2) {
function Gun3(o) {
if (o instanceof Gun3) {
return (this._ = { $: this }).$;
}
if (!(this instanceof Gun3)) {
return new Gun3(o);
}
return Gun3.create(this._ = { $: this, opt: o });
}
Gun3.is = function($) {
return $ instanceof Gun3 || $ && $._ && $ === $._.$ || false;
};
Gun3.version = 0.202;
Gun3.chain = Gun3.prototype;
Gun3.chain.toJSON = function() {
};
USE("./shim");
Gun3.valid = USE("./valid");
Gun3.state = USE("./state");
Gun3.on = USE("./onto");
Gun3.dup = USE("./dup");
Gun3.ask = USE("./ask");
;
(function() {
Gun3.create = function(at) {
at.root = at.root || at;
at.graph = at.graph || {};
at.on = at.on || Gun3.on;
at.ask = at.ask || Gun3.ask;
at.dup = at.dup || Gun3.dup();
var gun = at.$.opt(at.opt);
if (!at.once) {
at.on("in", universe, at);
at.on("out", universe, at);
at.on("put", map, at);
Gun3.on("create", at);
at.on("create", at);
}
at.once = 1;
return gun;
};
function universe(msg) {
if (!msg) {
return;
}
if (msg.out === universe) {
this.to.next(msg);
return;
}
var eve = this, as = eve.as, at = as.at || as, gun = at.$, dup = at.dup, tmp, DBG = msg.DBG;
(tmp = msg["#"]) || (tmp = msg["#"] = text_rand(9));
if (dup.check(tmp)) {
return;
}
dup.track(tmp);
tmp = msg._;
msg._ = "function" == typeof tmp ? tmp : function() {
};
msg.$ && msg.$ === (msg.$._ || "").$ || (msg.$ = gun);
if (msg["@"] && !msg.put) {
ack(msg);
}
if (!at.ask(msg["@"], msg)) {
DBG && (DBG.u = +/* @__PURE__ */ new Date());
if (msg.put) {
put2(msg);
return;
} else if (msg.get) {
Gun3.on.get(msg, gun);
}
}
DBG && (DBG.uc = +/* @__PURE__ */ new Date());
eve.to.next(msg);
DBG && (DBG.ua = +/* @__PURE__ */ new Date());
if (msg.nts || msg.NTS) {
return;
}
msg.out = universe;
at.on("out", msg);
DBG && (DBG.ue = +/* @__PURE__ */ new Date());
}
function put2(msg) {
if (!msg) {
return;
}
var ctx = msg._ || "", root = ctx.root = ((ctx.$ = msg.$ || "")._ || "").root;
if (msg["@"] && ctx.faith && !ctx.miss) {
msg.out = universe;
root.on("out", msg);
return;
}
ctx.latch = root.hatch;
ctx.match = root.hatch = [];
var put3 = msg.put;
var DBG = ctx.DBG = msg.DBG, S = +/* @__PURE__ */ new Date();
CT = CT || S;
if (put3["#"] && put3["."]) {
return;
}
DBG && (DBG.p = S);
ctx["#"] = msg["#"];
ctx.msg = msg;
ctx.all = 0;
ctx.stun = 1;
var nl = Object.keys(put3);
console.STAT && console.STAT(S, ((DBG || ctx).pk = +/* @__PURE__ */ new Date()) - S, "put sort");
var ni = 0, nj, kl, soul, node, states, err, tmp;
(function pop(o) {
if (nj != ni) {
nj = ni;
if (!(soul = nl[ni])) {
console.STAT && console.STAT(S, ((DBG || ctx).pd = +/* @__PURE__ */ new Date()) - S, "put");
fire(ctx);
return;
}
if (!(node = put3[soul])) {
err = ERR + cut(soul) + "no node.";
} else if (!(tmp = node._)) {
err = ERR + cut(soul) + "no meta.";
} else if (soul !== tmp["#"]) {
err = ERR + cut(soul) + "soul not same.";
} else if (!(states = tmp[">"])) {
err = ERR + cut(soul) + "no state.";
}
kl = Object.keys(node || {});
}
if (err) {
msg.err = ctx.err = err;
fire(ctx);
return;
}
var i = 0, key;
o = o || 0;
while (o++ < 9 && (key = kl[i++])) {
if ("_" === key) {
continue;
}
var val = node[key], state = states[key];
if (u === state) {
err = ERR + cut(key) + "on" + cut(soul) + "no state.";
break;
}
if (!valid(val)) {
err = ERR + cut(key) + "on" + cut(soul) + "bad " + typeof val + cut(val);
break;
}
ham(val, key, soul, state, msg);
++C2;
}
if ((kl = kl.slice(i)).length) {
turn(pop);
return;
}
++ni;
kl = null;
pop(o);
})();
}
Gun3.on.put = put2;
function ham(val, key, soul, state, msg) {
var ctx = msg._ || "", root = ctx.root, graph = root.graph, lot, tmp;
var vertex = graph[soul] || empty, was = state_is(vertex, key, 1), known = vertex[key];
var DBG = ctx.DBG;
if (tmp = console.STAT) {
if (!graph[soul] || !known) {
tmp.has = (tmp.has || 0) + 1;
}
}
var now = State(), u2;
if (state > now) {
setTimeout(function() {
ham(val, key, soul, state, msg);
}, (tmp = state - now) > MD ? MD : tmp);
console.STAT && console.STAT((DBG || ctx).Hf = +/* @__PURE__ */ new Date(), tmp, "future");
return;
}
if (state < was) {
if (true) {
return;
}
}
if (!ctx.faith) {
if (state === was && (val === known || L(val) <= L(known))) {
if (!ctx.miss) {
return;
}
}
}
ctx.stun++;
var aid = msg["#"] + ctx.all++, id = { toString: function() {
return aid;
}, _: ctx };
id.toJSON = id.toString;
root.dup.track(id)["#"] = msg["#"];
DBG && (DBG.ph = DBG.ph || +/* @__PURE__ */ new Date());
root.on("put", { "#": id, "@": msg["@"], put: { "#": soul, ".": key, ":": val, ">": state }, ok: msg.ok, _: ctx });
}
function map(msg) {
var DBG;
if (DBG = (msg._ || "").DBG) {
DBG.pa = +/* @__PURE__ */ new Date();
DBG.pm = DBG.pm || +/* @__PURE__ */ new Date();
}
var eve = this, root = eve.as, graph = root.graph, ctx = msg._, put3 = msg.put, soul = put3["#"], key = put3["."], val = put3[":"], state = put3[">"], id = msg["#"], tmp;
if ((tmp = ctx.msg) && (tmp = tmp.put) && (tmp = tmp[soul])) {
state_ify(tmp, key, state, val, soul);
}
graph[soul] = state_ify(graph[soul], key, state, val, soul);
if (tmp = (root.next || "")[soul]) {
tmp.on("in", msg);
}
fire(ctx);
eve.to.next(msg);
}
function fire(ctx, msg) {
var root;
if (ctx.stop) {
return;
}
if (!ctx.err && 0 < --ctx.stun) {
return;
}
ctx.stop = 1;
if (!(root = ctx.root)) {
return;
}
var tmp = ctx.match;
tmp.end = 1;
if (tmp === root.hatch) {
if (!(tmp = ctx.latch) || tmp.end) {
delete root.hatch;
} else {
root.hatch = tmp;
}
}
ctx.hatch && ctx.hatch();
setTimeout.each(ctx.match, function(cb) {
cb && cb();
});
if (!(msg = ctx.msg) || ctx.err || msg.err) {
return;
}
msg.out = universe;
ctx.root.on("out", msg);
CF();
}
function ack(msg) {
var id = msg["@"] || "", ctx, ok, tmp;
if (!(ctx = id._)) {
var dup = (dup = msg.$) && (dup = dup._) && (dup = dup.root) && (dup = dup.dup);
if (!(dup = dup.check(id))) {
return;
}
msg["@"] = dup["#"] || msg["@"];
return;
}
ctx.acks = (ctx.acks || 0) + 1;
if (ctx.err = msg.err) {
msg["@"] = ctx["#"];
fire(ctx);
}
ctx.ok = msg.ok || ctx.ok;
if (!ctx.stop && !ctx.crack) {
ctx.crack = ctx.match && ctx.match.push(function() {
back(ctx);
});
}
back(ctx);
}
function back(ctx) {
if (!ctx || !ctx.root) {
return;
}
if (ctx.stun || ctx.acks !== ctx.all) {
return;
}
ctx.root.on("in", { "@": ctx["#"], err: ctx.err, ok: ctx.err ? u : ctx.ok || { "": 1 } });
}
var ERR = "Error: Invalid graph!";
var cut = function(s) {
return " '" + ("" + s).slice(0, 9) + "...' ";
};
var L = JSON.stringify, MD = 2147483647, State = Gun3.state;
var C2 = 0, CT, CF = function() {
if (C2 > 999 && C2 / -(CT - (CT = +/* @__PURE__ */ new Date())) > 1) {
Gun3.window && console.log("Warning: You're syncing 1K+ records a second, faster than DOM can update - consider limiting query.");
CF = function() {
C2 = 0;
};
}
};
})();
;
(function() {
Gun3.on.get = function(msg, gun) {
var root = gun._, get2 = msg.get, soul = get2["#"], node = root.graph[soul], has2 = get2["."];
var next = root.next || (root.next = {}), at = next[soul];
var ctx = msg._ || {}, DBG = ctx.DBG = msg.DBG;
DBG && (DBG.g = +/* @__PURE__ */ new Date());
if (!node) {
return root.on("get", msg);
}
if (has2) {
if ("string" != typeof has2 || u === node[has2]) {
if (!((at || "").next || "")[has2]) {
root.on("get", msg);
return;
}
}
node = state_ify({}, has2, state_is(node, has2), node[has2], soul);
}
node && ack(msg, node);
root.on("get", msg);
};
function ack(msg, node) {
var S = +/* @__PURE__ */ new Date(), ctx = msg._ || {}, DBG = ctx.DBG = msg.DBG;
var to = msg["#"], id = text_rand(9), keys = Object.keys(node || "").sort(), soul = ((node || "")._ || "")["#"], kl = keys.length, j = 0, root = msg.$._.root, F = node === root.graph[soul];
console.STAT && console.STAT(S, ((DBG || ctx).gk = +/* @__PURE__ */ new Date()) - S, "got keys");
node && function go() {
S = +/* @__PURE__ */ new Date();
var i = 0, k, put2 = {}, tmp;
while (i < 9 && (k = keys[i++])) {
state_ify(put2, k, state_is(node, k), node[k], soul);
}
keys = keys.slice(i);
(tmp = {})[soul] = put2;
put2 = tmp;
var faith;
if (F) {
faith = function() {
};
faith.ram = faith.faith = true;
}
tmp = keys.length;
console.STAT && console.STAT(S, -(S - (S = +/* @__PURE__ */ new Date())), "got copied some");
DBG && (DBG.ga = +/* @__PURE__ */ new Date());
root.on("in", { "@": to, "#": id, put: put2, "%": tmp ? id = text_rand(9) : u, $: root.$, _: faith, DBG, FOO: 1 });
console.STAT && console.STAT(S, +/* @__PURE__ */ new Date() - S, "got in");
if (!tmp) {
return;
}
setTimeout.turn(go);
}();
if (!node) {
root.on("in", { "@": msg["#"] });
}
}
Gun3.on.get.ack = ack;
})();
;
(function() {
Gun3.chain.opt = function(opt) {
opt = opt || {};
var gun = this, at = gun._, tmp = opt.peers || opt;
if (!Object.plain(opt)) {
opt = {};
}
if (!Object.plain(at.opt)) {
at.opt = opt;
}
if ("string" == typeof tmp) {
tmp = [tmp];
}
if (!Object.plain(at.opt.peers)) {
at.opt.peers = {};
}
if (tmp instanceof Array) {
opt.peers = {};
tmp.forEach(function(url) {
var p = {};
p.id = p.url = url;
opt.peers[url] = at.opt.peers[url] = at.opt.peers[url] || p;
});
}
obj_each(opt, function each(k) {
var v = this[k];
if (this && this.hasOwnProperty(k) || "string" == typeof v || Object.empty(v)) {
this[k] = v;
return;
}
if (v && v.constructor !== Object && !(v instanceof Array)) {
return;
}
obj_each(v, each);
});
at.opt.from = opt;
Gun3.on("opt", at);
at.opt.uuid = at.opt.uuid || function uuid(l) {
return Gun3.state().toString(36).replace(".", "") + String.random(l || 12);
};
return gun;
};
})();
var obj_each = function(o, f) {
Object.keys(o).forEach(f, o);
}, text_rand = String.random, turn = setTimeout.turn, valid = Gun3.valid, state_is = Gun3.state.is, state_ify = Gun3.state.ify, u, empty = {}, C;
Gun3.log = function() {
return !Gun3.log.off && C.log.apply(C, arguments), [].slice.call(arguments).join(" ");
};
Gun3.log.once = function(w, s, o) {
return (o = Gun3.log.once)[w] = o[w] || 0, o[w]++ || Gun3.log(s);
};
if (typeof window !== "undefined") {
(window.GUN = window.Gun = Gun3).window = window;
}
try {
if (typeof MODULE !== "undefined") {
MODULE.exports = Gun3;
}
} catch (e) {
}
module2.exports = Gun3;
(Gun3.window || {}).console = (Gun3.window || {}).console || { log: function() {
} };
(C = console).only = function(i, s) {
return C.only.i && i === C.only.i && C.only.i++ && (C.log.apply(C, arguments) || s);
};
;
"Please do not remove welcome log unless you are paying for a monthly sponsorship, thanks!";
Gun3.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!");
})(USE, "./root");
;
USE(function(module2) {
var Gun3 = USE("./root");
Gun3.chain.back = function(n, opt) {
var tmp;
n = n || 1;
if (-1 === n || Infinity === n) {
return this._.root.$;
} else if (1 === n) {
return (this._.back || this._).$;
}
var gun = this, at = gun._;
if (typeof n === "string") {
n = n.split(".");
}
if (n instanceof Array) {
var i = 0, l = n.length, tmp = at;
for (i; i < l; i++) {
tmp = (tmp || empty)[n[i]];
}
if (u !== tmp) {
return opt ? gun : tmp;
} else if (tmp = at.back) {
return tmp.$.back(n, opt);
}
return;
}
if ("function" == typeof n) {
var yes, tmp = { back: at };
while ((tmp = tmp.back) && u === (yes = n(tmp, opt))) {
}
return yes;
}
if ("number" == typeof n) {
return (at.back || at).$.back(n - 1);
}
return this;
};
var empty = {}, u;
})(USE, "./back");
;
USE(function(module2) {
var Gun3 = USE("./root");
Gun3.chain.chain = function(sub) {
var gun = this, at = gun._, chain = new (sub || gun).constructor(gun), cat = chain._, root;
cat.root = root = at.root;
cat.id = ++root.once;
cat.back = gun._;
cat.on = Gun3.on;
cat.on("in", Gun3.on.in, cat);
cat.on("out", Gun3.on.out, cat);
return chain;
};
function output(msg) {
var put2, get2, at = this.as, back = at.back, root = at.root, tmp;
if (!msg.$) {
msg.$ = at.$;
}
this.to.next(msg);
if (at.err) {
at.on("in", { put: at.put = u, $: at.$ });
return;
}
if (get2 = msg.get) {
if (root.pass) {
root.pass[at.id] = at;
}
if (at.lex) {
Object.keys(at.lex).forEach(function(k) {
tmp[k] = at.lex[k];
}, tmp = msg.get = msg.get || {});
}
if (get2["#"] || at.soul) {
get2["#"] = get2["#"] || at.soul;
msg["#"] || (msg["#"] = text_rand(9));
back = root.$.get(get2["#"])._;
if (!(get2 = get2["."])) {
tmp = back.ask && back.ask[""];
(back.ask || (back.ask = {}))[""] = back;
if (u !== back.put) {
back.on("in", back);
if (tmp) {
return;
}
}
msg.$ = back.$;
} else if (obj_has(back.put, get2)) {
tmp = back.ask && back.ask[get2];
(back.ask || (back.ask = {}))[get2] = back.$.get(get2)._;
back.on("in", { get: get2, put: { "#": back.soul, ".": get2, ":": back.put[get2], ">": state_is(root.graph[back.soul], get2) } });
if (tmp) {
return;
}
}
root.ask(ack, msg);
return root.on("in", msg);
}
if (get2["."]) {
if (at.get) {
msg = { get: { ".": at.get }, $: at.$ };
(back.ask || (back.ask = {}))[at.get] = msg.$._;
return back.on("out", msg);
}
msg = { get: at.lex ? msg.get : {}, $: at.$ };
return back.on("out", msg);
}
(at.ask || (at.ask = {}))[""] = at;
if (at.get) {
get2["."] = at.get;
(back.ask || (back.ask = {}))[at.get] = msg.$._;
return back.on("out", msg);
}
}
return back.on("out", msg);
}
;
Gun3.on.out = output;
function input(msg, cat) {
cat = cat || this.as;
var root = cat.root, gun = msg.$ || (msg.$ = cat.$), at = (gun || "")._ || empty, tmp = msg.put || "", soul = tmp["#"], key = tmp["."], change = u !== tmp["="] ? tmp["="] : tmp[":"], state2 = tmp[">"] || -Infinity, sat;
if (u !== msg.put && (u === tmp["#"] || u === tmp["."] || u === tmp[":"] && u === tmp["="] || u === tmp[">"])) {
if (!valid(tmp)) {
if (!(soul = ((tmp || "")._ || "")["#"])) {
console.log("chain not yet supported for", tmp, "...", msg, cat);
return;
}
gun = cat.root.$.get(soul);
return setTimeout.each(Object.keys(tmp).sort(), function(k) {
if ("_" == k || u === (state2 = state_is(tmp, k))) {
return;
}
cat.on("in", { $: gun, put: { "#": soul, ".": k, "=": tmp[k], ">": state2 }, VIA: msg });
});
}
cat.on("in", { $: at.back.$, put: { "#": soul = at.back.soul, ".": key = at.has || at.get, "=": tmp, ">": state_is(at.back.put, key) }, via: msg });
return;
}
if ((msg.seen || "")[cat.id]) {
return;
}
(msg.seen || (msg.seen = function() {
}))[cat.id] = cat;
if (cat !== at) {
Object.keys(msg).forEach(function(k) {
tmp[k] = msg[k];
}, tmp = {});
tmp.get = cat.get || tmp.get;
if (!cat.soul && !cat.has) {
tmp.$$$ = tmp.$$$ || cat.$;
} else if (at.soul) {
tmp.$ = cat.$;
tmp.$$ = tmp.$$ || at.$;
}
msg = tmp;
}
unlink(msg, cat);
if ((cat.soul || msg.$$) && state2 >= state_is(root.graph[soul], key)) {
(tmp = root.$.get(soul)._).put = state_ify(tmp.put, key, state2, change, soul);
}
if (!at.soul && state2 >= state_is(root.graph[soul], key) && (sat = (root.$.get(soul)._.next || "")[key])) {
sat.put = change;
if ("string" == typeof (tmp = valid(change))) {
sat.put = root.$.get(tmp)._.put || change;
}
}
this.to && this.to.next(msg);
cat.any && setTimeout.each(Object.keys(cat.any), function(any) {
(any = cat.any[any]) && any(msg);
}, 0, 99);
cat.echo && setTimeout.each(Object.keys(cat.echo), function(lat) {
(lat = cat.echo[lat]) && lat.on("in", msg);
}, 0, 99);
if (((msg.$$ || "")._ || at).soul) {
if ((sat = cat.next) && (sat = sat[key])) {
tmp = {};
Object.keys(msg).forEach(function(k) {
tmp[k] = msg[k];
});
tmp.$ = (msg.$$ || msg.$).get(tmp.get = key);
delete tmp.$$;
delete tmp.$$$;
sat.on("in", tmp);
}
}
link(msg, cat);
}
;
Gun3.on.in = input;
function link(msg, cat) {
cat = cat || this.as || msg.$._;
if (msg.$$ && this !== Gun3.on) {
return;
}
if (!msg.put || cat.soul) {
return;
}
var put2 = msg.put || "", link2 = put2["="] || put2[":"], tmp;
var root = cat.root, tat = root.$.get(put2["#"]).get(put2["."])._;
if ("string" != typeof (link2 = valid(link2))) {
if (this === Gun3.on) {
(tat.echo || (tat.echo = {}))[cat.id] = cat;
}
return;
}
if ((tat.echo || (tat.echo = {}))[cat.id] && !(root.pass || "")[cat.id]) {
return;
}
if (tmp = root.pass) {
if (tmp[link2 + cat.id]) {
return;
}
tmp[link2 + cat.id] = 1;
}
(tat.echo || (tat.echo = {}))[cat.id] = cat;
if (cat.has) {
cat.link = link2;
}
var sat = root.$.get(tat.link = link2)._;
(sat.echo || (sat.echo = {}))[tat.id] = tat;
var tmp = cat.ask || "";
if (tmp[""] || cat.lex) {
sat.on("out", { get: { "#": link2 } });
}
setTimeout.each(Object.keys(tmp), function(get2, sat2) {
if (!get2 || !(sat2 = tmp[get2])) {
return;
}
sat2.on("out", { get: { "#": link2, ".": get2 } });
}, 0, 99);
}
;
Gun3.on.link = link;
function unlink(msg, cat) {
var put2 = msg.put || "", change = u !== put2["="] ? put2["="] : put2[":"], root = cat.root, link2, tmp;
if (u === change) {
if (cat.soul && u !== cat.put) {
return;
}
tmp = (msg.$$ || msg.$ || "")._ || "";
if (msg["@"] && (u !== tmp.put || u !== cat.put)) {
return;
}
if (link2 = cat.link || msg.linked) {
delete (root.$.get(link2)._.echo || "")[cat.id];
}
if (cat.has) {
cat.link = null;
}
cat.put = u;
setTimeout.each(Object.keys(cat.next || ""), function(get2, sat) {
if (!(sat = cat.next[get2])) {
return;
}
if (link2) {
delete (root.$.get(link2).get(get2)._.echo || "")[sat.id];
}
sat.on("in", { get: get2, put: u, $: sat.$ });
}, 0, 99);
return;
}
if (cat.soul) {
return;
}
if (msg.$$) {
return;
}
link2 = valid(change);
tmp = msg.$._ || "";
if (link2 === tmp.link || cat.has && !tmp.link) {
if ((root.pass || "")[cat.id] && "string" !== typeof link2) {
} else {
return;
}
}
delete (tmp.echo || "")[cat.id];
unlink({ get: cat.get, put: u, $: msg.$, linked: msg.linked = msg.linked || tmp.link }, cat);
}
;
Gun3.on.unlink = unlink;
function ack(msg, ev) {
var as = this.as, at = as.$._, root = at.root, get2 = as.get || "", tmp = (msg.put || "")[get2["#"]] || "";
if (!msg.put || "string" == typeof get2["."] && u === tmp[get2["."]]) {
if (u !== at.put) {
return;
}
if (!at.soul && !at.has) {
return;
}
at.ack = (at.ack || 0) + 1;
at.on("in", {
get: at.get,
put: at.put = u,
$: at.$,
"@": msg["@"]
});
return;
}
(msg._ || {}).miss = 1;
Gun3.on.put(msg);
return;
}
var empty = {}, u, text_rand = String.random, valid = Gun3.valid, obj_has = function(o, k) {
return o && Object.prototype.hasOwnProperty.call(o, k);
}, state = Gun3.state, state_is = state.is, state_ify = state.ify;
})(USE, "./chain");
;
USE(function(module2) {
var Gun3 = USE("./root");
Gun3.chain.get = function(key, cb, as) {
var gun, tmp;
if (typeof key === "string") {
if (key.length == 0) {
(gun = this.chain())._.err = { err: Gun3.log("0 length key!", key) };
if (cb) {
cb.call(gun, gun._.err);
}
return gun;
}
var back = this, cat = back._;
var next = cat.next || empty;
if (!(gun = next[key])) {
gun = key && cache(key, back);
}
gun = gun && gun.$;
} else if ("function" == typeof key) {
let any2 = function(msg, eve, f) {
if (any2.stun) {
return;
}
if ((tmp2 = root.pass) && !tmp2[id]) {
return;
}
var at = msg.$._, sat = (msg.$$ || "")._, data = (sat || at).put, odd = !at.has && !at.soul, test = {}, link, tmp2;
if (odd || u === data) {
data = u === ((tmp2 = msg.put) || "")["="] ? u === (tmp2 || "")[":"] ? tmp2 : tmp2[":"] : tmp2["="];
}
if (link = "string" == typeof (tmp2 = Gun3.valid(data))) {
data = u === (tmp2 = root.$.get(tmp2)._.put) ? opt.not ? u : data : tmp2;
}
if (opt.not && u === data) {
return;
}
if (u === opt.stun) {
if ((tmp2 = root.stun) && tmp2.on) {
cat.$.back(function(a) {
tmp2.on("" + a.id, test = {});
if ((test.run || 0) < any2.id) {
return test;
}
});
!test.run && tmp2.on("" + at.id, test = {});
!test.run && sat && tmp2.on("" + sat.id, test = {});
if (any2.id > test.run) {
if (!test.stun || test.stun.end) {
test.stun = tmp2.on("stun");
test.stun = test.stun && test.stun.last;
}
if (test.stun && !test.stun.end) {
(test.stun.add || (test.stun.add = {}))[id] = function() {
any2(msg, eve, 1);
};
return;
}
}
}
if (
/*odd &&*/
u === data
) {
f = 0;
}
if ((tmp2 = root.hatch) && !tmp2.end && u === opt.hatch && !f) {
if (wait[at.$._.id]) {
return;
}
wait[at.$._.id] = 1;
tmp2.push(function() {
any2(msg, eve, 1);
});
return;
}
;
wait = {};
}
if (root.pass) {
if (root.pass[id + at.id]) {
return;
}
root.pass[id + at.id] = 1;
}
if (opt.on) {
opt.ok.call(at.$, data, at.get, msg, eve || any2);
return;
}
if (opt.v2020) {
opt.ok(msg, eve || any2);
return;
}
Object.keys(msg).forEach(function(k) {
tmp2[k] = msg[k];
}, tmp2 = {});
msg = tmp2;
msg.put = data;
opt.ok.call(opt.as, msg, eve || any2);
};
var any = any2;
if (true === cb) {
return soul(this, key, cb, as), this;
}
gun = this;
var cat = gun._, opt = cb || {}, root = cat.root, id;
opt.at = cat;
opt.ok = key;
var wait = {};
;
any2.at = cat;
(cat.any || (cat.any = {}))[id = String.random(7)] = any2;
any2.off = function() {
any2.stun = 1;
if (!cat.any) {
return;
}
delete cat.any[id];
};
any2.rid = rid;
any2.id = opt.run || ++root.once;
tmp = root.pass;
(root.pass = {})[id] = 1;
opt.out = opt.out || { get: {} };
cat.on("out", opt.out);
root.pass = tmp;
return gun;
} else if ("number" == typeof key) {
return this.get("" + key, cb, as);
} else if ("string" == typeof (tmp = valid(key))) {
return this.get(tmp, cb, as);
} else if (tmp = this.get.next) {
gun = tmp(this, key);
}
if (!gun) {
(gun = this.chain())._.err = { err: Gun3.log("Invalid get request!", key) };
if (cb) {
cb.call(gun, gun._.err);
}
return gun;
}
if (cb && "function" == typeof cb) {
gun.get(cb, as);
}
return gun;
};
function cache(key, back) {
var cat = back._, next = cat.next, gun = back.chain(), at = gun._;
if (!next) {
next = cat.next = {};
}
next[at.get = key] = at;
if (back === cat.root.$) {
at.soul = key;
} else if (cat.soul || cat.has) {
at.has = key;
}
return at;
}
function soul(gun, cb, opt, as) {
var cat = gun._, acks = 0, tmp;
if (tmp = cat.soul || cat.link) {
return cb(tmp, as, cat);
}
if (cat.jam) {
return cat.jam.push([cb, as]);
}
cat.jam = [[cb, as]];
gun.get(function go(msg, eve) {
if (u === msg.put && !cat.root.opt.super && (tmp = Object.keys(cat.root.opt.peers).length) && ++acks <= tmp) {
return;
}
eve.rid(msg);
var at = (at = msg.$) && at._ || {}, i = 0, as2;
tmp = cat.jam;
delete cat.jam;
while (as2 = tmp[i++]) {
var cb2 = as2[0], id;
as2 = as2[1];
cb2 && cb2(id = at.link || at.soul || Gun3.valid(msg.put) || ((msg.put || {})._ || {})["#"], as2, msg, eve);
}
}, { out: { get: { ".": true } } });
return gun;
}
function rid(at) {
var cat = this.at || this.on;
if (!at || cat.soul || cat.has) {
return this.off();
}
if (!(at = (at = (at = at.$ || at)._ || at).id)) {
return;
}
var map = cat.map, tmp, seen;
if (tmp = (seen = this.seen || (this.seen = {}))[at]) {
return true;
}
seen[at] = true;
return;
return;
}
var empty = {}, valid = Gun3.valid, u;
})(USE, "./get");
;
USE(function(module2) {
var Gun3 = USE("./root");
Gun3.chain.put = function(data, cb, as) {
var gun = this, at = gun._, root = at.root;
as = as || {};
as.root = at.root;
as.run || (as.run = root.once);
stun(as, at.id);
as.ack = as.ack || cb;
as.via = as.via || gun;
as.data = as.data || data;
as.soul || (as.soul = at.soul || "string" == typeof cb && cb);
var s = as.state = as.state || Gun3.state();
if ("function" == typeof data) {
data(function(d) {
as.data = d;
gun.put(u, u, as);
});
return gun;