mirror of
https://github.com/amark/gun.git
synced 2025-03-30 15:08:33 +00:00
1803 lines
64 KiB
JavaScript
1803 lines
64 KiB
JavaScript
;(function(){
|
|
|
|
/* UNBUILD */
|
|
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 }
|
|
/* UNBUILD */
|
|
|
|
;USE(function(module){
|
|
// Shim for generic javascript utilities.
|
|
String.random = function(l, c){
|
|
var s = '';
|
|
l = l || 24; // you are not going to make a 0 length random number, so no need to check type
|
|
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['>'] || 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){ // via SO
|
|
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 has = 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(has.call(o, k) && k !== n){ return false } }
|
|
return true;
|
|
}
|
|
Object.keys = Object.keys || function(o){
|
|
var l = [];
|
|
for(var k in o){ if(has.call(o, k)){ l.push(k) } }
|
|
return l;
|
|
}
|
|
;(function(){ // max ~1ms or before stack overflow
|
|
var u, l = 0, c = 0, sI = (typeof setImmediate !== ''+u && setImmediate) || setTimeout;
|
|
setTimeout.poll = function(f){
|
|
if((1 >= (+new Date - l)) && c++ < 3333){ f(); return }
|
|
sI(function(){ l = +new Date; f() },c=0)
|
|
}
|
|
}());
|
|
;(function(){ // Too many polls block, this "threads" them in turns over a single thread in time.
|
|
var p = setTimeout.poll, s = [], i = 0, fn;
|
|
setTimeout.turn = function(f){ 1 == s.push(f) && p(turn) };
|
|
function turn(){
|
|
if(fn = s[i++]){ fn() }
|
|
if(i == s.length || 99 == i){
|
|
s = s.slice(i);
|
|
i = 0;
|
|
}
|
|
if(s.length){ p(turn) }
|
|
}
|
|
}());
|
|
// JSON should not block CPU, this should be a standard. But browsers do not have it, so we are stuck polyfilling with the blocking version.
|
|
//JSON.parseAsync = JSON.parseAsync || function(t,cb,r){ var u; try{ cb(u, JSON.parse(t,r)) }catch(e){ cb(e) } }
|
|
//JSON.stringifyAsync = JSON.stringifyAsync || function(v,cb,r,s){ var u; try{ cb(u, JSON.stringify(v,r,s)) }catch(e){ cb(e) } }
|
|
})(USE, './shim');
|
|
|
|
;USE(function(module){
|
|
// On event emitter generic javascript utility.
|
|
module.exports = function onto(tag, arg, as){
|
|
if(!tag){ return {to: onto} }
|
|
var u, tag = (this.tag || (this.tag = {}))[tag] ||
|
|
(this.tag[tag] = {tag: tag, to: onto._ = {
|
|
next: function(arg){ var tmp;
|
|
if((tmp = this.to)){
|
|
tmp.next(arg);
|
|
}}
|
|
}});
|
|
if('function' == typeof arg){
|
|
var be = {
|
|
off: onto.off ||
|
|
(onto.off = function(){
|
|
if(this.next === onto._.next){ return !0 }
|
|
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: 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(module){
|
|
USE('./shim');
|
|
module.exports = function(v){ // Valid values are a subset of JSON: null, binary, number (!Infinity), text, or a soul relation. Arrays need special algorithms to handle concurrency, so they are not supported directly. Use an extension that supports them if needed but research their problems first.
|
|
if(v === undefined){ return false }
|
|
if(v === null){ return true } // "deletes", nulling out keys.
|
|
if(v === Infinity){ return false } // we want this to be, but JSON does not support it, sad face.
|
|
if('string' == typeof v
|
|
|| 'boolean' == typeof v // by "binary" we mean boolean.
|
|
|| 'number' == typeof v){ // by "number" we mean integers or decimals.
|
|
return true; // simple values are valid.
|
|
}
|
|
if(v && ('string' == typeof (v['#']||0)) && Object.empty(v, '#')){ return v['#'] } // is link
|
|
return false; // If not, everything else remaining is an invalid data type. Custom extensions can be built on top of these primitives to support other types.
|
|
}
|
|
})(USE, './valid');
|
|
|
|
;USE(function(module){
|
|
USE('./shim');
|
|
function State(){
|
|
var t = +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; // WARNING! In the future, on machines that are D times faster than 2016AD machines, you will want to increase D by another several orders of magnitude so the processing speed never out paces the decimal resolution (increasing an integer effects the state accuracy).
|
|
State.is = function(n, k, o){ // convenience function to get the state on a key on a node and return it.
|
|
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){ // put a key's state on a node.
|
|
if(!n || !n['_']){ // reject if it is not node-like.
|
|
if(!soul){ return } // unless they passed a soul
|
|
((n = n || {})._ = n._ || {})['#'] = soul || n._['#']; // then make it so!
|
|
}
|
|
var tmp = n['_']['>'] || (n['_']['>'] = {}); // grab the states data.
|
|
if(u !== k && k !== '_'){
|
|
if('number' == typeof s){
|
|
tmp[k] = s; // add the valid state.
|
|
}
|
|
if(u !== v){ // Note: Not its job to check for valid values!
|
|
n[k] = v;
|
|
}
|
|
}
|
|
return n;
|
|
}
|
|
module.exports = State;
|
|
})(USE, './state');
|
|
|
|
;USE(function(module){
|
|
USE('./shim');
|
|
function Dup(opt){
|
|
var dup = {s:{}}, s = dup.s;
|
|
opt = opt || {max: 999, age: /*1000 * 9};//*/ 1000 * 9 * 3};
|
|
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 = +new Date;
|
|
if(!dup.to){ dup.to = setTimeout(dup.drop, opt.age + 9) }
|
|
return it;
|
|
}
|
|
dup.drop = function(age){
|
|
var now = +new Date;
|
|
Object.keys(s).forEach(function(id){ var it = s[id];
|
|
if(it && (age || opt.age) > (now - it.was)){ return }
|
|
delete s[id];
|
|
});
|
|
dup.to = null;
|
|
console.STAT && (age = +new Date - now) > 9 && console.STAT(now, age, 'dup drop');
|
|
}
|
|
return dup;
|
|
}
|
|
module.exports = Dup;
|
|
})(USE, './dup');
|
|
|
|
;USE(function(module){
|
|
// request / response module, for asking and acking messages.
|
|
USE('./onto'); // depends upon onto!
|
|
module.exports = function ask(cb, as){
|
|
if(!this.on){ return }
|
|
if(!('function' == typeof cb)){
|
|
if(!cb || !as){ return }
|
|
var id = cb['#'] || cb, tmp = (this.tag||'')[id];
|
|
if(!tmp){ return }
|
|
tmp = this.on(id, as);
|
|
clearTimeout(tmp.err);
|
|
return true;
|
|
}
|
|
var id = (as && as['#']) || Math.random().toString(36).slice(2);
|
|
if(!cb){ return id }
|
|
var to = this.on(id, cb, as);
|
|
to.err = to.err || setTimeout(function(){
|
|
to.next({err: "Error: No ACK yet.", lack: true});
|
|
to.off();
|
|
}, (this.opt||{}).lack || 9000);
|
|
return id;
|
|
}
|
|
})(USE, './ask');
|
|
|
|
;USE(function(module){
|
|
|
|
function Gun(o){
|
|
if(o instanceof Gun){ return (this._ = {$: this}).$ }
|
|
if(!(this instanceof Gun)){ return new Gun(o) }
|
|
return Gun.create(this._ = {$: this, opt: o});
|
|
}
|
|
|
|
Gun.is = function($){ return ($ instanceof Gun) || ($ && $._ && ($ === $._.$)) || false }
|
|
|
|
Gun.version = 0.2020;
|
|
|
|
Gun.chain = Gun.prototype;
|
|
Gun.chain.toJSON = function(){};
|
|
|
|
USE('./shim');
|
|
Gun.valid = USE('./valid');
|
|
Gun.state = USE('./state');
|
|
Gun.on = USE('./onto');
|
|
Gun.dup = USE('./dup');
|
|
Gun.ask = USE('./ask');
|
|
|
|
;(function(){
|
|
Gun.create = function(at){
|
|
at.root = at.root || at;
|
|
at.graph = at.graph || {};
|
|
at.on = at.on || Gun.on;
|
|
at.ask = at.ask || Gun.ask;
|
|
at.dup = at.dup || Gun.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);
|
|
Gun.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(!at.ask(msg['@'], msg)){ // is this machine listening for an ack?
|
|
DBG && (DBG.u = +new Date);
|
|
if(msg.get){ Gun.on._get(msg, gun) }
|
|
if(msg.put){ put(msg); return }
|
|
}
|
|
DBG && (DBG.uc = +new Date);
|
|
eve.to.next(msg);
|
|
DBG && (DBG.ua = +new Date);
|
|
msg.out = universe; at.on('out', msg);
|
|
DBG && (DBG.ue = +new Date);
|
|
}
|
|
function put(msg){
|
|
if(!msg){ return }
|
|
var ctx = msg._||'', root = ctx.root = ((ctx.$ = msg.$||'')._||'').root;
|
|
if(msg['@'] && !ctx.me){ // TODO: WARNING! This will prevent relays from caching!
|
|
msg.out = universe;
|
|
root.on('out', msg);
|
|
return;
|
|
}
|
|
var put = msg.put, id = msg['#'];
|
|
var DBG = ctx.DBG = msg.DBG;
|
|
if(put['#'] && put['.']){ root.on('put', msg); return } // TODO: BUG! This needs to call HAM instead.
|
|
DBG && (DBG.p = S);
|
|
ctx.msg = msg;
|
|
ctx.stun = 1;
|
|
var nl = Object.keys(put), ni = 0, nj, kl, soul, node, states, err, tmp;
|
|
(function pop(o){
|
|
if(nj != ni){ nj = ni;
|
|
if(!(soul = nl[ni])){
|
|
ctx.stun--; // TODO: 'forget' feature in SEA tied to this, bad approach, but hacked in for now. Any changes here must update there.
|
|
console.log('done!', ctx.stun);
|
|
fire(ctx);
|
|
return;
|
|
}
|
|
if(!(node = put[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){
|
|
console.log("handle error!") // handle!
|
|
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);
|
|
}
|
|
if((kl = kl.slice(i)).length){ turn(pop); return }
|
|
++ni; kl = null; pop(o);
|
|
}());
|
|
|
|
/*DBG && (DBG.pe = +new Date);
|
|
if(console.STAT){ console.STAT(S, +new Date - S, 'mix');console.STAT(S, ctx.lot.s, 'mix #') }
|
|
if(ctx.err = err){ root.on('in', {'@': id, err: Gun.log(err)}); return }
|
|
if(!(--ctx.lot.more)){ fire(ctx) } // if synchronous.
|
|
if(!ctx.stun && !msg['@']){ root.on('in', {'@': id, ok: -1}) } // in case no diff sent to storage, etc., still ack.*/
|
|
} Gun.on.put = put;
|
|
function ham(val, key, soul, state, msg){
|
|
var ctx = msg._||'', root = ctx.root, graph = root.graph, lot;
|
|
var vertex = graph[soul] || empty, was = state_is(vertex, key, 1), known = vertex[key];
|
|
var now = State(),u;
|
|
if(state > now){ setTo; return }
|
|
if(state < was){ old; if(!ctx.miss){ return } } // but some chains have a cache miss that need to re-fire. // TODO: Improve in future.
|
|
if(state === was && (val === known || L(val) <= L(known))){ same; return }
|
|
/*if(!is.incoming){
|
|
if(is.defer){
|
|
var to = state - machine;
|
|
setTimeout(function(){
|
|
ham(val, key, soul, state, msg);
|
|
}, to > MD? MD : to); // setTimeout Max Defer 32bit :(
|
|
if(!ctx.to){ root.on('in', {'@': msg['#'], err: to}) } ctx.to = 1; // TODO: This causes too many problems unless sending peers auto-retry.
|
|
return to;
|
|
}
|
|
return;
|
|
}*/
|
|
//(lot = ctx.lot||'').s++; lot.more++;
|
|
//(ctx.stun || (ctx.stun = {}))[soul+key] = 1;
|
|
ctx.stun++; // TODO: 'forget' feature in SEA tied to this, bad approach, but hacked in for now. Any changes here must update there.
|
|
var DBG = ctx.DBG; DBG && (DBG.ph = DBG.ph || +new Date);
|
|
root.on('put', {'#': msg['#'], '@': msg['@'], put: {'#': soul, '.': key, ':': val, '>': state}, _: ctx});
|
|
}
|
|
function map(msg){
|
|
var DBG; if(DBG = (msg._||'').DBG){ DBG.pa = +new Date; DBG.pm = DBG.pm || +new Date}
|
|
var eve = this, root = eve.as, graph = root.graph, ctx = msg._, put = msg.put, soul = put['#'], key = put['.'], val = put[':'], state = put['>'], id = msg['#'], tmp;
|
|
graph[soul] = state_ify(graph[soul], key, state, val, soul); // TODO: Only put in graph if subscribed? Relays vs Browsers?
|
|
chain(ctx, soul, key, (u !== (tmp = put['=']))? tmp : val, state); // TODO: This should NOT be how the API works, this should be done at an extension layer, but hacky solution to migrate with old code for now.
|
|
if((tmp = ctx.out) && (tmp = tmp.put)){
|
|
tmp[soul] = state_ify(tmp[soul], key, state, val, soul); // TODO: Hacky, fix & come back later, for actual pushing messages.
|
|
}
|
|
//if(!(--ctx.lot.more)){ fire(ctx) }
|
|
if(!(--ctx.stun)){ fire(ctx) }
|
|
eve.to.next(msg);
|
|
}
|
|
function chain(ctx, soul, key,val, state){
|
|
var root = ctx.root, put, tmp;
|
|
(root.opt||'').super && root.$.get(soul); // I think we need super for now, but since we are rewriting, should consider getting rid of it.
|
|
if(!root || !(tmp = root.next) || !(tmp = tmp[soul]) || !tmp.$){ return }
|
|
(put = ctx.put || (ctx.put = {}))[soul] = state_ify(put[soul], key, state, val, soul);
|
|
tmp.put = state_ify(tmp.put, key, state, val, soul);
|
|
}
|
|
function fire(ctx){
|
|
if(ctx.err){ return }
|
|
var stop = {};
|
|
var root = ((ctx.$||'')._||'').root, next = (root||'').next||'', put = ctx.put, tmp;
|
|
var S = +new Date;
|
|
//Gun.graph.is(put, function(node, soul){
|
|
for(var soul in put){ var node = put[soul]; // Gun.obj.native() makes this safe.
|
|
if(!(tmp = next[soul]) || !tmp.$){ continue }
|
|
root.stop = stop; // temporary fix till a better solution?
|
|
tmp.on('in', {$: tmp.$, get: soul, put: node});
|
|
root.stop = null; // temporary fix till a better solution?
|
|
}
|
|
//console.log("fire:", +new Date - S);
|
|
console.STAT && console.STAT(S, +new Date - S, 'fire');
|
|
ctx.DBG && (ctx.DBG.f = +new Date);
|
|
if(!(tmp = ctx.msg)){ return }
|
|
tmp.out = universe;
|
|
//console.log("PUT:", +new Date - ctx.START, ctx.lot); ctx.START = null;
|
|
console.log('out', tmp);
|
|
root.on('out', tmp);
|
|
}
|
|
var ERR = "Error: Invalid graph!";
|
|
var cut = function(s){ return " '"+(''+s).slice(0,9)+"...' " }
|
|
var L = JSON.stringify, MD = 2147483647, State = Gun.state;
|
|
}());
|
|
|
|
;(function(){
|
|
Gun.on._get = function(msg, gun){
|
|
var root = gun._, get = msg.get, soul = get['#'], node = root.graph[soul], has = get['.'], tmp;
|
|
var next = root.next || (root.next = {}), at = next[soul];
|
|
// queue concurrent GETs?
|
|
var ctx = msg._||'', DBG = ctx.DBG = msg.DBG;
|
|
DBG && (DBG.g = +new Date);
|
|
//console.log("GET", get);
|
|
console.only(1, 'GET', node);
|
|
if(!node){ return root.on('get', msg) }
|
|
if(has){
|
|
if('string' != typeof has || !obj_has(node, has)){ return root.on('get', msg) }
|
|
node = Gun.state.to(node, has);
|
|
// If we have a key in-memory, do we really need to fetch?
|
|
// Maybe... in case the in-memory key we have is a local write
|
|
// we still need to trigger a pull/merge from peers.
|
|
} else {
|
|
//node = node;//Gun.window? Gun.obj.copy(node) : node; // HNPERF: If !browser bump Performance? Is this too dangerous to reference root graph? Copy / shallow copy too expensive for big nodes. Gun.obj.to(node); // 1 layer deep copy // Gun.obj.copy(node); // too slow on big nodes
|
|
var ack = msg['#'], id = text_rand(9), keys = Object.keys(node||{}), soul = node._['#'];
|
|
(function got(){
|
|
var i = 0, k, put = {};
|
|
while(i < 9 && (k = keys[i++])){
|
|
state_ify(put, k, state_is(node, k), node[k], soul);
|
|
}
|
|
keys = keys.slice(i);
|
|
(tmp = {})[soul] = put; put = tmp;
|
|
var faith = function(){}; faith.ram = faith.faith = true; // HNPERF: We're testing performance improvement by skipping going through security again, but this should be audited.
|
|
tmp = keys.length;
|
|
root.on('in', {'@': ack, '#': id, put: put, '%': (tmp? (id = text_rand(9)) : u), ram: 1, $: gun, _: faith});
|
|
if(!tmp){ return }
|
|
setTimeout.turn(got);
|
|
}());
|
|
//root.on('get', msg);
|
|
return;
|
|
}
|
|
//console.log("GOT", Object.keys(node).length);
|
|
(tmp = {})[node._['#']] = node; node = tmp;
|
|
tmp = (at||empty).ack;
|
|
var faith = function(){}; faith.ram = faith.faith = true; // HNPERF: We're testing performance improvement by skipping going through security again, but this should be audited.
|
|
faith.$ = msg.$;
|
|
DBG && (DBG.ga = +new Date);
|
|
console.only(2, 'GOT:', msg['#'], node);
|
|
root.on('in', {
|
|
'@': msg['#'],
|
|
put: node,
|
|
ram: 1,
|
|
$: gun,
|
|
_: faith
|
|
});
|
|
DBG && (DBG.gm = +new Date);
|
|
//if(0 < tmp){ return }
|
|
root.on('get', msg);
|
|
DBG && (DBG.gd = +new Date);
|
|
}
|
|
}());
|
|
|
|
;(function(){
|
|
Gun.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(tmp instanceof Array){
|
|
if(!Object.plain(at.opt.peers)){ at.opt.peers = {}}
|
|
tmp.forEach(function(url){
|
|
var p = {}; p.id = p.url = url;
|
|
at.opt.peers[url] = at.opt.peers[url] || p;
|
|
})
|
|
}
|
|
at.opt.peers = at.opt.peers || {};
|
|
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);
|
|
});
|
|
Gun.on('opt', at);
|
|
return gun;
|
|
}
|
|
}());
|
|
|
|
var obj_each = function(o,f){ Object.keys(o).forEach(f,o) }, text_rand = String.random, turn = setTimeout.turn, valid = Gun.valid, state_is = Gun.state.is, state_ify = Gun.state.ify, u, empty = {}, C;
|
|
|
|
Gun.log = function(){ return (!Gun.log.off && C.log.apply(C, arguments)), [].slice.call(arguments).join(' ') };
|
|
Gun.log.once = function(w,s,o){ return (o = Gun.log.once)[w] = o[w] || 0, o[w]++ || Gun.log(s) };
|
|
|
|
if(typeof window !== "undefined"){ (window.GUN = window.Gun = Gun).window = window }
|
|
try{ if(typeof MODULE !== "undefined"){ MODULE.exports = Gun } }catch(e){}
|
|
module.exports = Gun;
|
|
|
|
(Gun.window||{}).console = (Gun.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!";
|
|
Gun.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(module){
|
|
var Gun = USE('./root');
|
|
Gun.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(module){
|
|
// WARNING: GUN is very simple, but the JavaScript chaining API around GUN
|
|
// is complicated and was extremely hard to build. If you port GUN to another
|
|
// language, consider implementing an easier API to build.
|
|
var Gun = USE('./root');
|
|
Gun.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 = Gun.on;
|
|
cat.on('in', input, cat); // For 'in' if I add my own listeners to each then I MUST do it before in gets called. If I listen globally for all incoming data instead though, regardless of individual listeners, I can transform the data there and then as well.
|
|
cat.on('out', output, cat); // However for output, there isn't really the global option. I must listen by adding my own listener individually BEFORE this one is ever called.
|
|
return chain;
|
|
}
|
|
|
|
function output(msg){
|
|
var put, get, at = this.as, back = at.back, root = at.root, tmp;
|
|
if(!msg.$){ msg.$ = at.$ }
|
|
this.to.next(msg);
|
|
if(get = msg.get){
|
|
/*if(u !== at.put){
|
|
at.on('in', at);
|
|
return;
|
|
}*/
|
|
if(at.lex){ msg.get = obj_to(at.lex, msg.get) }
|
|
if(get['#'] || at.soul){
|
|
get['#'] = get['#'] || at.soul;
|
|
msg['#'] || (msg['#'] = text_rand(9));
|
|
back = (root.$.get(get['#'])._);
|
|
if(!(get = get['.'])){
|
|
tmp = back.ack;
|
|
if(!tmp){ back.ack = -1 }
|
|
if(obj_has(back, 'put')){
|
|
back.on('in', back);
|
|
}
|
|
if(tmp && u !== back.put){ return } //if(tmp){ return }
|
|
msg.$ = back.$;
|
|
} else
|
|
if(obj_has(back.put, get)){ // TODO: support #LEX !
|
|
put = (back.$.get(get)._);
|
|
if(!(tmp = put.ack)){ put.ack = -1 }
|
|
back.on('in', {
|
|
$: back.$,
|
|
put: Gun.state.to(back.put, get),
|
|
get: back.get
|
|
});
|
|
if(tmp){ return }
|
|
} else
|
|
if('string' != typeof get){
|
|
var put = {}, meta = (back.put||{})._;
|
|
Gun.obj.map(back.put, function(v,k){
|
|
if(!Gun.text.match(k, get)){ return }
|
|
put[k] = v;
|
|
})
|
|
if(!Gun.obj.empty(put)){
|
|
put._ = meta;
|
|
back.on('in', {$: back.$, put: put, get: back.get})
|
|
}
|
|
if(tmp = at.lex){
|
|
tmp = (tmp._) || (tmp._ = function(){});
|
|
if(back.ack < tmp.ask){ tmp.ask = back.ack }
|
|
if(tmp.ask){ return }
|
|
tmp.ask = 1;
|
|
}
|
|
}
|
|
root.ask(ack, msg);
|
|
return root.on('in', msg);
|
|
}
|
|
if(root.now){ root.now[at.id] = root.now[at.id] || true; at.pass = {} }
|
|
if(get['.']){
|
|
if(at.get){
|
|
msg = {get: {'.': at.get}, $: at.$};
|
|
//if(back.ask || (back.ask = {})[at.get]){ return }
|
|
(back.ask || (back.ask = {}));
|
|
back.ask[at.get] = msg.$._; // TODO: PERFORMANCE? More elegant way?
|
|
return back.on('out', msg);
|
|
}
|
|
msg = {get: {}, $: at.$};
|
|
return back.on('out', msg);
|
|
}
|
|
at.ack = at.ack || -1;
|
|
if(at.get){
|
|
msg.$ = at.$;
|
|
get['.'] = at.get;
|
|
(back.ask || (back.ask = {}))[at.get] = msg.$._; // TODO: PERFORMANCE? More elegant way?
|
|
return back.on('out', msg);
|
|
}
|
|
}
|
|
return back.on('out', msg);
|
|
}
|
|
|
|
function input(msg){
|
|
Gun.window == msg && console.log("!!!!!!!", msg);
|
|
var eve = this, cat = eve.as, root = cat.root, gun = msg.$, at = (gun||empty)._ || empty, change = msg.put, rel, tmp;
|
|
if(cat.get && msg.get !== cat.get){
|
|
msg = obj_to(msg, {get: cat.get});
|
|
}
|
|
if(cat.has && at !== cat){
|
|
msg = obj_to(msg, {$: cat.$});
|
|
if(at.ack){
|
|
cat.ack = at.ack;
|
|
//cat.ack = cat.ack || at.ack;
|
|
}
|
|
}
|
|
if(u === change){
|
|
tmp = at.put;
|
|
eve.to.next(msg);
|
|
if(cat.soul){ return } // TODO: BUG, I believee the fresh input refactor caught an edge case that a `gun.get('soul').get('key')` that points to a soul that doesn't exist will not trigger val/get etc.
|
|
if(u === tmp && u !== at.put){ return }
|
|
echo(cat, msg, eve);
|
|
if(cat.has){
|
|
not(cat, msg);
|
|
}
|
|
obj_del(at.echo, cat.id);
|
|
obj_del(cat.map, at.id);
|
|
return;
|
|
}
|
|
if(cat.soul){
|
|
eve.to.next(msg);
|
|
echo(cat, msg, eve);
|
|
if(cat.next){ Object.keys(change).forEach(map, {msg: msg, cat: cat}) }
|
|
return;
|
|
}
|
|
//if(!(rel = Gun.val.link.is(change))){
|
|
if('string' != (rel = valid(change))){
|
|
if(valid(change)){
|
|
if(cat.has || cat.soul){
|
|
not(cat, msg);
|
|
} else
|
|
if(at.has || at.soul){
|
|
(at.echo || (at.echo = {}))[cat.id] = at.echo[at.id] || cat;
|
|
(cat.map || (cat.map = {}))[at.id] = cat.map[at.id] || {at: at};
|
|
//if(u === at.put){ return } // Not necessary but improves performance. If we have it but at does not, that means we got things out of order and at will get it. Once at gets it, it will tell us again.
|
|
}
|
|
eve.to.next(msg);
|
|
echo(cat, msg, eve);
|
|
return;
|
|
}
|
|
if(cat.has && at !== cat && obj_has(at, 'put')){
|
|
cat.put = at.put;
|
|
};
|
|
if((rel = Gun.node.soul(change)) && at.has){
|
|
at.put = (cat.root.$.get(rel)._).put;
|
|
}
|
|
tmp = (root.stop || {})[at.id];
|
|
//if(tmp && tmp[cat.id]){ } else {
|
|
eve.to.next(msg);
|
|
//}
|
|
relate(cat, msg, at, rel);
|
|
echo(cat, msg, eve);
|
|
if(cat.next){ obj_map(change, map, {msg: msg, cat: cat}) }
|
|
return;
|
|
}
|
|
var was = root.stop;
|
|
tmp = root.stop || {};
|
|
tmp = tmp[at.id] || (tmp[at.id] = {});
|
|
//if(tmp[cat.id]){ return }
|
|
tmp.is = tmp.is || at.put;
|
|
tmp[cat.id] = at.put || true;
|
|
//if(root.stop){
|
|
eve.to.next(msg)
|
|
//}
|
|
relate(cat, msg, at, rel);
|
|
echo(cat, msg, eve);
|
|
}
|
|
|
|
function relate(at, msg, from, rel){
|
|
if(!rel || node_ === at.get){ return }
|
|
var tmp = (at.root.$.get(rel)._);
|
|
if(at.has){
|
|
from = tmp;
|
|
} else
|
|
if(from.has){
|
|
relate(from, msg, from, rel);
|
|
}
|
|
if(from === at){ return }
|
|
if(!from.$){ from = {} }
|
|
(from.echo || (from.echo = {}))[at.id] = from.echo[at.id] || at;
|
|
if(at.has && !(at.map||empty)[from.id]){ // if we haven't seen this before.
|
|
not(at, msg);
|
|
}
|
|
tmp = from.id? ((at.map || (at.map = {}))[from.id] = at.map[from.id] || {at: from}) : {};
|
|
if(rel === tmp.link){
|
|
if(!(tmp.pass || at.pass)){
|
|
return;
|
|
}
|
|
}
|
|
if(at.pass){
|
|
Gun.obj.map(at.map, function(tmp){ tmp.pass = true })
|
|
obj_del(at, 'pass');
|
|
}
|
|
if(tmp.pass){ obj_del(tmp, 'pass') }
|
|
if(at.has){ at.link = rel }
|
|
ask(at, tmp.link = rel);
|
|
}
|
|
function echo(at, msg, ev){
|
|
if(!at.echo){ return } // || node_ === at.get ?
|
|
//if(at.has){ msg = obj_to(msg, {event: ev}) }
|
|
Object.keys(at.echo).forEach(function(k){ var to = at.echo[k];
|
|
if(!to || !to.on){ return }
|
|
to.on('in', msg);
|
|
})
|
|
}
|
|
function map(key){ // Map over only the changes on every update.
|
|
var cat = this.cat, next = cat.next || empty, via = this.msg, data = via.put[key], chain, at, tmp;
|
|
if('_' === key && !next[key]){ return }
|
|
if(!(at = next[key])){
|
|
return;
|
|
}
|
|
//if(data && data[_soul] && (tmp = Gun.val.link.is(data)) && (tmp = (cat.root.$.get(tmp)._)) && obj_has(tmp, 'put')){
|
|
// data = tmp.put;
|
|
//}
|
|
if(at.has){
|
|
//if(!(data && data[_soul] && Gun.val.link.is(data) === Gun.node.soul(at.put))){
|
|
if(u === at.put || 'string' != typeof valid(data)){ // not a link!
|
|
at.put = data;
|
|
}
|
|
chain = at.$;
|
|
} else
|
|
if(tmp = via.$){
|
|
tmp = (chain = via.$.get(key))._;
|
|
if(u === tmp.put || 'string' != typeof valid(data)){
|
|
tmp.put = data;
|
|
}
|
|
}
|
|
at.on('in', {
|
|
put: data,
|
|
get: key,
|
|
$: chain,
|
|
via: via
|
|
});
|
|
}
|
|
function not(at, msg){
|
|
if(!(at.has || at.soul)){ return }
|
|
var tmp = at.map, root = at.root;
|
|
at.map = null;
|
|
if(at.has){
|
|
if(at.dub && at.root.stop){ at.dub = null }
|
|
at.link = null;
|
|
}
|
|
//if(!root.now || !root.now[at.id]){
|
|
if(!at.pass){
|
|
if((!msg['@']) && null === tmp){ return }
|
|
//obj_del(at, 'pass');
|
|
}
|
|
//if(u === tmp && Gun.val.link.is(at.put)){ return } // This prevents the very first call of a thing from triggering a "clean up" call. // TODO: link.is(at.put) || !val.is(at.put) ?
|
|
if(u === tmp && 'string' == typeof valid(at.put)){ return } // This prevents the very first call of a thing from triggering a "clean up" call. // TODO: link.is(at.put) || !val.is(at.put) ?
|
|
Object.keys(tmp||{}).forEach(function(k){ var proxy = tmp[k];
|
|
if(!(proxy = proxy.at)){ return }
|
|
delete proxy.echo[at.id];
|
|
});
|
|
tmp = at.put;
|
|
//obj_map(at.next, function(neat, key){
|
|
Object.keys(at.next||{}).forEach(function(key){ var neat = at.next[key];
|
|
if(u === tmp && u !== at.put){ return true }
|
|
neat.put = u;
|
|
if(neat.ack){
|
|
neat.ack = -1; // Shouldn't this be reset to 0? If we do that, SEA test `set user ref should be found` fails, odd.
|
|
}
|
|
neat.on('in', {
|
|
get: key,
|
|
$: neat.$,
|
|
put: u
|
|
});
|
|
});
|
|
}
|
|
function ask(at, soul){
|
|
var tmp = (at.root.$.get(soul)._), lex = at.lex;
|
|
if(at.ack || lex){
|
|
(lex = lex||{})['#'] = soul;
|
|
tmp.on('out', {get: lex});
|
|
if(!at.ask){ return } // TODO: PERFORMANCE? More elegant way?
|
|
}
|
|
tmp = at.ask; delete at['ask'];
|
|
Object.keys(tmp = tmp || at.next || {}).forEach(function(neat, key){ var neat = tmp[key];
|
|
var lex = neat.lex || {}; lex['#'] = soul; lex['.'] = lex['.'] || key;
|
|
neat.on('out', {get: lex});
|
|
});
|
|
delete at['ask']; // TODO: PERFORMANCE? More elegant way?
|
|
}
|
|
function ack(msg, ev){
|
|
var as = this.as, get = as.get||'', at = as.$._, tmp = (msg.put||'')[get['#']];
|
|
(msg._||{}).me = 1;
|
|
if(at.ack){ at.ack = (at.ack + 1) || 1; }
|
|
if(!msg.put || ('string' == typeof get['.'] && !obj_has(tmp, at.get))){
|
|
if(at.put !== u){ return }
|
|
at.on('in', {
|
|
get: at.get,
|
|
put: at.put = u,
|
|
$: at.$,
|
|
'@': msg['@']
|
|
});
|
|
return;
|
|
}
|
|
if('_' == get['.']){ // is this a security concern?
|
|
at.on('in', {get: at.get, put: Gun.val.link.ify(get['#']), $: at.$, '@': msg['@']});
|
|
return;
|
|
}
|
|
if(at.$ === (msg._||'').$){ // replying to self, for perf, skip ham/security tho needs audit.
|
|
(msg._).miss = (at.put === u);
|
|
}
|
|
Gun.on.put(msg);
|
|
}
|
|
var empty = {}, u, text_rand = String.random, valid = Gun.valid, obj_has = function(o, k){ return o && Object.prototype.hasOwnProperty.call(o, k) }
|
|
})(USE, './chain');
|
|
|
|
;USE(function(module){
|
|
var Gun = USE('./root');
|
|
Gun.chain.get = function(key, cb, as){
|
|
var gun, tmp;
|
|
if(typeof key === 'string'){
|
|
var back = this, cat = back._;
|
|
var next = cat.next || empty;
|
|
if(!(gun = next[key])){
|
|
gun = cache(key, back);
|
|
}
|
|
gun = gun.$;
|
|
} else
|
|
if('function' == typeof key){
|
|
if(true === cb){ return soul(this, key, cb, as), this }
|
|
gun = this;
|
|
var at = gun._, root = at.root, tmp = root.now, ev;
|
|
as = cb || {};
|
|
as.at = at;
|
|
as.use = key;
|
|
as.out = as.out || {};
|
|
as.out.get = as.out.get || {};
|
|
(ev = at.on('in', use, as)).rid = rid;
|
|
(root.now = {$:1})[as.now = at.id] = ev;
|
|
var mum = root.mum; root.mum = {};
|
|
at.on('out', as.out);
|
|
root.mum = mum;
|
|
root.now = tmp;
|
|
return gun;
|
|
} else
|
|
if('number' == typeof key){
|
|
return this.get(''+key, cb, as);
|
|
} else
|
|
if(tmp = rel.is(key)){
|
|
return this.get(tmp, cb, as);
|
|
} else
|
|
if(Object.plain(key)){
|
|
gun = this;
|
|
if(tmp = ((tmp = key['#'])||empty)['='] || tmp){ gun = gun.get(tmp) }
|
|
gun._.lex = key;
|
|
return gun;
|
|
} else {
|
|
(as = this.chain())._.err = {err: Gun.log('Invalid get request!', key)}; // CLEAN UP
|
|
if(cb){ cb.call(as, as._.err) }
|
|
return as;
|
|
}
|
|
if(tmp = this._.stun){ // TODO: Refactor?
|
|
gun._.stun = gun._.stun || tmp;
|
|
}
|
|
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;
|
|
//if(obj_has(cat.put, key)){
|
|
//at.put = cat.put[key];
|
|
//}
|
|
}
|
|
return at;
|
|
}
|
|
function soul(gun, cb, opt, as){
|
|
var cat = gun._, acks = 0, tmp;
|
|
if(tmp = cat.soul || cat.link || cat.dub){ 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){ // TODO: super should not be in core code, bring AXE up into core instead to fix?
|
|
return;
|
|
}
|
|
eve.rid(msg);
|
|
var at = ((at = msg.$) && at._) || {}, i = 0, as;
|
|
tmp = cat.jam; delete cat.jam; // tmp = cat.jam.splice(0, 100);
|
|
//if(tmp.length){ process.nextTick(function(){ go(msg, eve) }) }
|
|
while(as = tmp[i++]){ //Gun.obj.map(tmp, function(as, cb){
|
|
var cb = as[0], id; as = as[1];
|
|
cb && cb(id = at.link || at.soul || rel.is(msg.put) || node_soul(msg.put) || at.dub, as, msg, eve);
|
|
} //);
|
|
}, {out: {get: {'.':true}}});
|
|
return gun;
|
|
}
|
|
function use(msg){
|
|
var eve = this, as = eve.as, cat = as.at, root = cat.root, gun = msg.$, at = (gun||{})._ || {}, data = msg.put || at.put, tmp;
|
|
if((tmp = root.now) && eve !== tmp[as.now]){ return eve.to.next(msg) }
|
|
//if(at.async && msg.root){ return }
|
|
//if(at.async === 1 && cat.async !== true){ return }
|
|
//if(root.stop && root.stop[at.id]){ return } root.stop && (root.stop[at.id] = true);
|
|
//if(!at.async && !cat.async && at.put && msg.put === at.put){ return }
|
|
//else if(!cat.async && msg.put !== at.put && root.stop && root.stop[at.id]){ return } root.stop && (root.stop[at.id] = true);
|
|
|
|
|
|
//root.stop && (root.stop.id = root.stop.id || Gun.text.random(2));
|
|
//if((tmp = root.stop) && (tmp = tmp[at.id] || (tmp[at.id] = {})) && tmp[cat.id]){ return } tmp && (tmp[cat.id] = true);
|
|
if(eve.seen && at.id && eve.seen[at.id]){ return eve.to.next(msg) }
|
|
//if((tmp = root.stop)){ if(tmp[at.id]){ return } tmp[at.id] = msg.root; } // temporary fix till a better solution?
|
|
if((tmp = data) && tmp['#'] && 'string' == (tmp = valid(tmp))){
|
|
tmp = ((msg.$$ = at.root.$.get(tmp))._);
|
|
if(u !== tmp.put){
|
|
//msg = obj_to(msg, {put: data = tmp.put});
|
|
var o = {}; Object.keys(msg, function(k){ o[k] = msg[k] }); o.put = data = tmp.put;
|
|
msg = o;
|
|
}
|
|
}
|
|
if((tmp = root.mum) && at.id){ // TODO: can we delete mum entirely now?
|
|
var id = at.id + (eve.id || (eve.id = String.random(9)));
|
|
if(tmp[id]){ return }
|
|
if(u !== data && 'string' != valid(data)){ tmp[id] = true; }
|
|
}
|
|
as.use(msg, eve);
|
|
if(eve.stun){
|
|
eve.stun = null;
|
|
return;
|
|
}
|
|
eve.to.next(msg);
|
|
}
|
|
function rid(at){
|
|
var cat = 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(!map || !(tmp = map[at]) || !(tmp = tmp.at)){ return }
|
|
if(tmp = (seen = this.seen || (this.seen = {}))[at]){ return true }
|
|
seen[at] = true;
|
|
return;
|
|
//tmp.echo[cat.id] = {}; // TODO: Warning: This unsubscribes ALL of this chain's listeners from this link, not just the one callback event.
|
|
//obj.del(map, at); // TODO: Warning: This unsubscribes ALL of this chain's listeners from this link, not just the one callback event.
|
|
return;
|
|
}
|
|
var empty = {}, u;
|
|
})(USE, './get');
|
|
|
|
;USE(function(module){
|
|
var Gun = USE('./root');
|
|
Gun.chain.put = function(data, cb, as){
|
|
var gun = this, at = (gun._), root = at.root.$, ctx = root._, M = 99, tmp;
|
|
as = as || {};
|
|
as.data = data;
|
|
as.via = as.$ = as.via || as.$ || gun;
|
|
if(typeof cb === 'string'){
|
|
as.soul = cb;
|
|
} else {
|
|
as.ack = as.ack || cb;
|
|
}
|
|
if(at.soul){
|
|
as.soul = at.soul;
|
|
}
|
|
if(as.soul || root === gun){
|
|
if(!obj_is(as.data)){
|
|
(as.ack||noop).call(as, as.out = {err: Gun.log("Data saved to the root level of the graph must be a node (an object), not a", (typeof as.data), 'of "' + as.data + '"!')});
|
|
if(as.res){ as.res() }
|
|
return gun;
|
|
}
|
|
as.soul = as.soul || (as.not = Gun.node.soul(as.data) || (as.via.back('opt.uuid') || Gun.text.random)());
|
|
as.via._.stun = {};
|
|
if(!as.soul){ // polyfill async uuid for SEA
|
|
as.via.back('opt.uuid')(function(err, soul){ // TODO: improve perf without anonymous callback
|
|
if(err){ return Gun.log(err) } // TODO: Handle error!
|
|
(as.ref||as.$).put(as.data, as.soul = soul, as);
|
|
});
|
|
return gun;
|
|
}
|
|
as.$ = root.get(as.soul);
|
|
as.ref = as.$;
|
|
ify(as);
|
|
return gun;
|
|
}
|
|
as.via._.stun = {};
|
|
if(Gun.is(data)){
|
|
data.get(function(soul, o, msg){
|
|
if(!soul){
|
|
delete as.via._.stun;
|
|
return Gun.log("The reference you are saving is a", typeof msg.put, '"'+ msg.put +'", not a node (object)!');
|
|
}
|
|
gun.put(Gun.val.link.ify(soul), cb, as);
|
|
}, true);
|
|
return gun;
|
|
}
|
|
if(at.has && (tmp = Gun.val.link.is(data))){ at.dub = tmp }
|
|
as.ref = as.ref || (root._ === (tmp = at.back))? gun : tmp.$;
|
|
if(as.ref._.soul && Gun.val.is(as.data) && at.get){
|
|
as.data = obj_put({}, at.get, as.data);
|
|
as.ref.put(as.data, as.soul, as);
|
|
return gun;
|
|
}
|
|
as.ref.get(any, true, {as: as});
|
|
if(!as.out){
|
|
// TODO: Perf idea! Make a global lock, that blocks everything while it is on, but if it is on the lock it does the expensive lookup to see if it is a dependent write or not and if not then it proceeds full speed. Meh? For write heavy async apps that would be terrible.
|
|
as.res = as.res || stun; // Gun.on.stun(as.ref); // TODO: BUG! Deal with locking?
|
|
as.$._.stun = as.ref._.stun;
|
|
}
|
|
return gun;
|
|
};
|
|
/*Gun.chain.put = function(data, cb, as){ // don't rewrite! :(
|
|
var gun = this, at = gun._;
|
|
as = as || {};
|
|
as.soul || (as.soul = at.soul || ('string' == typeof cb && cb));
|
|
if(!as.soul){ return get(data, cb, as) }
|
|
|
|
return gun;
|
|
}*/
|
|
|
|
function ify(as){
|
|
as.batch = batch;
|
|
var opt = as.opt||{}, env = as.env = Gun.state.map(map, opt.state);
|
|
env.soul = as.soul;
|
|
as.graph = Gun.graph.ify(as.data, env, as);
|
|
if(env.err){
|
|
(as.ack||noop).call(as, as.out = {err: Gun.log(env.err)});
|
|
if(as.res){ as.res() }
|
|
return;
|
|
}
|
|
as.batch();
|
|
}
|
|
|
|
function stun(cb){
|
|
if(cb){ cb() }
|
|
return;
|
|
var as = this;
|
|
if(!as.ref){ return }
|
|
if(cb){
|
|
as.after = as.ref._.tag;
|
|
as.now = as.ref._.tag = {};
|
|
cb();
|
|
return;
|
|
}
|
|
if(as.after){
|
|
as.ref._.tag = as.after;
|
|
}
|
|
}
|
|
|
|
function batch(){ var as = this;
|
|
if(!as.graph || !obj_empty(as.stun)){ return }
|
|
as.res = as.res || function(cb){ if(cb){ cb() } };
|
|
as.res(function(){
|
|
delete as.via._.stun;
|
|
var cat = (as.$.back(-1)._), ask = cat.ask(function(ack){
|
|
cat.root.on('ack', ack);
|
|
if(ack.err){ Gun.log(ack) }
|
|
if(++acks > (as.acks || 0)){ this.off() } // Adjustable ACKs! Only 1 by default.
|
|
if(!as.ack){ return }
|
|
as.ack(ack, this);
|
|
//--C;
|
|
}, as.opt), acks = 0;
|
|
//C++;
|
|
// NOW is a hack to get synchronous replies to correctly call.
|
|
// and STOP is a hack to get async behavior to correctly call.
|
|
// neither of these are ideal, need to be fixed without hacks,
|
|
// but for now, this works for current tests. :/
|
|
var tmp = cat.root.now; obj.del(cat.root, 'now');
|
|
var mum = cat.root.mum; cat.root.mum = {};
|
|
(as.ref._).on('out', {
|
|
$: as.ref, put: as.out = as.env.graph, opt: as.opt, '#': ask
|
|
});
|
|
cat.root.mum = mum? obj.to(mum, cat.root.mum) : mum;
|
|
cat.root.now = tmp;
|
|
as.via._.on('res', {}); delete as.via._.tag.res; // emitting causes mem leak?
|
|
}, as);
|
|
if(as.res){ as.res() }
|
|
} function no(v,k){ if(v){ return true } }
|
|
|
|
function map(v,k,n, at){ var as = this;
|
|
var is = Gun.is(v);
|
|
if(k || !at.path.length){ return }
|
|
(as.res||iife)(function(){
|
|
var path = at.path, ref = as.ref, opt = as.opt;
|
|
var i = 0, l = path.length;
|
|
for(i; i < l; i++){
|
|
ref = ref.get(path[i]);
|
|
}
|
|
if(is){ ref = v }
|
|
//if(as.not){ (ref._).dub = Gun.text.random() } // This might optimize stuff? Maybe not needed anymore. Make sure it doesn't introduce bugs.
|
|
var id = (ref._).dub;
|
|
if(id || (id = Gun.node.soul(at.obj))){
|
|
ref.back(-1).get(id);
|
|
at.soul(id);
|
|
return;
|
|
}
|
|
(as.stun = as.stun || {})[path] = 1;
|
|
ref.get(soul, true, {as: {at: at, as: as, p:path, ref: ref}});
|
|
}, {as: as, at: at});
|
|
//if(is){ return {} }
|
|
}
|
|
var G = String.fromCharCode(31);
|
|
function soul(id, as, msg, eve){
|
|
var as = as.as, path = as.p, ref = as.ref, cat = as.at, pat = [], sat; as = as.as;
|
|
ref.back(function(at){
|
|
if(sat = at.soul || at.link || at.dub){ return sat }
|
|
pat.push(at.has || at.get);
|
|
});
|
|
pat = [sat || as.soul].concat(pat.reverse());
|
|
var at = ((msg || {}).$ || {})._ || {};
|
|
id = at.dub = at.dub || id || Gun.node.soul(cat.obj) || Gun.node.soul(msg.put || at.put) || Gun.val.link.is(msg.put || at.put) || pat.join('/') /* || (function(){
|
|
return (as.soul+'.')+Gun.text.hash(path.join(G)).toString(32);
|
|
})(); // TODO: BUG!? Do we really want the soul of the object given to us? Could that be dangerous? What about copy operations? */
|
|
if(eve){ eve.stun = true }
|
|
if(!id){ // polyfill async uuid for SEA
|
|
as.via.back('opt.uuid')(function(err, id){ // TODO: improve perf without anonymous callback
|
|
if(err){ return Gun.log(err) } // TODO: Handle error.
|
|
solve(at, at.dub = at.dub || id, cat, as);
|
|
});
|
|
return;
|
|
}
|
|
solve(at, at.dub = id, cat, as);
|
|
}
|
|
|
|
function solve(at, id, cat, as){
|
|
at.$.back(-1).get(id);
|
|
cat.soul(id);
|
|
delete as.stun[cat.path];
|
|
as.batch();
|
|
}
|
|
|
|
function any(soul, as, msg, eve){
|
|
as = as.as;
|
|
if(!msg.$ || !msg.$._){ return } // TODO: Handle
|
|
if(msg.err){ // TODO: Handle
|
|
delete as.via._.stun;
|
|
Gun.log("Please report this as an issue! Put.any.err");
|
|
return;
|
|
}
|
|
var at = (msg.$._), data = at.put, opt = as.opt||{}, root, tmp;
|
|
if((tmp = as.ref) && tmp._.now){ return }
|
|
if(eve){ eve.stun = true }
|
|
if(as.ref !== as.$){
|
|
tmp = (as.$._).get || at.get;
|
|
if(!tmp){ // TODO: Handle
|
|
Gun.log("Please report this as an issue! Put.no.get"); // TODO: BUG!??
|
|
return;
|
|
}
|
|
as.data = obj_put({}, tmp, as.data);
|
|
tmp = null;
|
|
}
|
|
if(u === data){
|
|
if(!at.get){ delete as.via._.stun; return } // TODO: Handle
|
|
if(!soul){
|
|
tmp = at.$.back(function(at){
|
|
if(at.link || at.soul){ return at.link || at.soul }
|
|
as.data = obj_put({}, at.get, as.data);
|
|
});
|
|
as.not = true; // maybe consider this?
|
|
}
|
|
tmp = tmp || at.soul || at.link || at.dub;// || at.get;
|
|
at = tmp? (at.root.$.get(tmp)._) : at;
|
|
as.soul = tmp;
|
|
data = as.data;
|
|
}
|
|
if(!as.not && !(as.soul = as.soul || soul)){
|
|
if(as.path && obj_is(as.data)){
|
|
as.soul = (opt.uuid || as.via.back('opt.uuid') || Gun.text.random)();
|
|
} else {
|
|
//as.data = obj_put({}, as.$._.get, as.data);
|
|
if(node_ == at.get){
|
|
as.soul = (at.put||empty)['#'] || at.dub;
|
|
}
|
|
as.soul = as.soul || at.soul || at.link || (opt.uuid || as.via.back('opt.uuid') || Gun.text.random)();
|
|
}
|
|
if(!as.soul){ // polyfill async uuid for SEA
|
|
as.via.back('opt.uuid')(function(err, soul){ // TODO: improve perf without anonymous callback
|
|
if(err){ delete as.via._.stun; return Gun.log(err) } // Handle error.
|
|
as.ref.put(as.data, as.soul = soul, as);
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
as.ref.put(as.data, as.soul, as);
|
|
}
|
|
var u, empty = {}, noop = function(){}, iife = function(fn,as){fn.call(as||empty)};
|
|
})(USE, './put');
|
|
|
|
;USE(function(module){
|
|
var Gun = USE('./root');
|
|
USE('./chain');
|
|
USE('./back');
|
|
USE('./put');
|
|
USE('./get');
|
|
module.exports = Gun;
|
|
})(USE, './index');
|
|
|
|
;USE(function(module){
|
|
var Gun = USE('./index');
|
|
Gun.chain.on = function(tag, arg, eas, as){
|
|
var gun = this, at = gun._, tmp, act, off;
|
|
if(typeof tag === 'string'){
|
|
if(!arg){ return at.on(tag) }
|
|
act = at.on(tag, arg, eas || at, as);
|
|
if(eas && eas.$){
|
|
(eas.subs || (eas.subs = [])).push(act);
|
|
}
|
|
return gun;
|
|
}
|
|
var opt = arg;
|
|
opt = (true === opt)? {change: true} : opt || {};
|
|
opt.at = at;
|
|
opt.ok = tag;
|
|
//opt.last = {};
|
|
gun.get(ok, opt); // TODO: PERF! Event listener leak!!!?
|
|
return gun;
|
|
}
|
|
|
|
function ok(msg, ev){ var opt = this;
|
|
var gun = msg.$, at = (gun||{})._ || {}, data = at.put || msg.put, cat = opt.at, tmp;
|
|
if(u === data){
|
|
return;
|
|
}
|
|
if(tmp = msg.$$){
|
|
tmp = (msg.$$._);
|
|
if(u === tmp.put){
|
|
return;
|
|
}
|
|
data = tmp.put;
|
|
}
|
|
if(opt.change){ // TODO: BUG? Move above the undef checks?
|
|
data = msg.put;
|
|
}
|
|
// DEDUPLICATE // TODO: NEEDS WORK! BAD PROTOTYPE
|
|
//if(tmp.put === data && tmp.get === id && !Gun.node.soul(data)){ return }
|
|
//tmp.put = data;
|
|
//tmp.get = id;
|
|
// DEDUPLICATE // TODO: NEEDS WORK! BAD PROTOTYPE
|
|
//at.last = data;
|
|
if(opt.as){
|
|
opt.ok.call(opt.as, msg, ev);
|
|
} else {
|
|
opt.ok.call(gun, data, msg.get, msg, ev);
|
|
}
|
|
}
|
|
Gun.chain.once = function(cb, opt){
|
|
var gun = this, at = gun._, data = at.put;
|
|
if(0 < at.ack && u !== data){
|
|
(cb || noop).call(gun, data, at.get);
|
|
return gun;
|
|
}
|
|
if(cb){
|
|
(opt = opt || {}).ok = cb;
|
|
opt.at = at;
|
|
opt.out = {'#': String.random(9)};
|
|
gun.get(val, {as: opt});
|
|
opt.async = true; //opt.async = at.stun? 1 : true;
|
|
} else {
|
|
Gun.log.once("valonce", "Chainable once is experimental, its behavior and API may change moving forward. Please play with it and report bugs and ideas on how to improve it.");
|
|
var chain = gun.chain();
|
|
chain._.nix = gun.once(function(){
|
|
chain._.on('in', gun._);
|
|
});
|
|
return chain;
|
|
}
|
|
return gun;
|
|
}
|
|
|
|
function val(msg, eve, to){
|
|
if(!msg.$){ eve.off(); return }
|
|
var opt = this.as, cat = opt.at, gun = msg.$, at = gun._, data = at.put || msg.put, link, tmp;
|
|
if(tmp = msg.$$){
|
|
link = tmp = (msg.$$._);
|
|
if(u !== link.put){
|
|
data = link.put;
|
|
}
|
|
}
|
|
if((tmp = eve.wait) && (tmp = tmp[at.id])){ clearTimeout(tmp) }
|
|
eve.ack = (eve.ack||0)+1;
|
|
// TODO: BUG! If NodeJS && people connect to it, these peer counts will be wrong + see other ref of this bug: // TODO: super should not be in core code, bring AXE up into core instead to fix?
|
|
if(!to && u === data && eve.ack <= (opt.acks || Object.keys(at.root.opt.peers).length)){ return }
|
|
if((!to && (u === data || at.soul || at.link || (link && !(0 < link.ack))))
|
|
|| (u === data && !at.root.opt.super && (tmp = Object.keys(at.root.opt.peers).length) && (!to && (link||at).ack < tmp))){
|
|
tmp = (eve.wait = {})[at.id] = setTimeout(function(){
|
|
val.call({as:opt}, msg, eve, tmp || 1);
|
|
}, opt.wait || 99);
|
|
return;
|
|
}
|
|
//if(link && u === link.put && (tmp = rel.is(data))){ data = Gun.node.ify({}, tmp) }
|
|
if(link && u === link.put && 'string' == (tmp = Gun.valid(data))){ (data = {_:{},'>':{}})['#'] = tmp; }
|
|
eve.rid? eve.rid(msg) : eve.off();
|
|
opt.ok.call(gun || opt.$, data, msg.get);
|
|
}
|
|
|
|
Gun.chain.off = function(){
|
|
// make off more aggressive. Warning, it might backfire!
|
|
var gun = this, at = gun._, tmp;
|
|
var cat = at.back;
|
|
if(!cat){ return }
|
|
at.ack = 0; // so can resubscribe.
|
|
if(tmp = cat.next){
|
|
if(tmp[at.get]){
|
|
obj_del(tmp, at.get);
|
|
} else {
|
|
|
|
}
|
|
}
|
|
if(tmp = cat.ask){
|
|
obj_del(tmp, at.get);
|
|
}
|
|
if(tmp = cat.put){
|
|
obj_del(tmp, at.get);
|
|
}
|
|
if(tmp = at.soul){
|
|
obj_del(cat.root.graph, tmp);
|
|
}
|
|
if(tmp = at.map){
|
|
obj_map(tmp, function(at){
|
|
if(at.link){
|
|
cat.root.$.get(at.link).off();
|
|
}
|
|
});
|
|
}
|
|
if(tmp = at.next){
|
|
obj_map(tmp, function(neat){
|
|
neat.$.off();
|
|
});
|
|
}
|
|
at.on('off', {});
|
|
return gun;
|
|
}
|
|
var empty = {}, noop = function(){}, u;
|
|
})(USE, './on');
|
|
|
|
;USE(function(module){
|
|
var Gun = USE('./index');
|
|
Gun.chain.map = function(cb, opt, t){
|
|
var gun = this, cat = gun._, chain;
|
|
if(!cb){
|
|
if(chain = cat.each){ return chain }
|
|
cat.each = chain = gun.chain();
|
|
chain._.nix = gun.back('nix');
|
|
gun.on('in', map, chain._);
|
|
return chain;
|
|
}
|
|
Gun.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.");
|
|
chain = gun.chain();
|
|
gun.map().on(function(data, key, at, ev){
|
|
var next = (cb||noop).call(this, data, key, at, ev);
|
|
if(u === next){ return }
|
|
if(data === next){ return chain._.on('in', at) }
|
|
if(Gun.is(next)){ return chain._.on('in', next._) }
|
|
chain._.on('in', {get: key, put: next});
|
|
});
|
|
return chain;
|
|
}
|
|
function map(msg){
|
|
if(!msg.put || Gun.valid(msg.put)){ return this.to.next(msg) }
|
|
if(this.as.nix){ this.off() } // TODO: Ugly hack!
|
|
Object.keys(msg.put).forEach(each, {at: this.as, msg: msg});
|
|
this.to.next(msg);
|
|
}
|
|
function each(k){
|
|
if('_' === k){ return }
|
|
var msg = this.msg, v = msg.put[k], gun = msg.$, at = gun._, cat = this.at, tmp = at.lex;
|
|
if(tmp && !Gun.text.match(k, tmp['.'] || tmp['#'] || tmp)){ return } // review?
|
|
((tmp = gun.get(k)._).echo || (tmp.echo = {}))[cat.id] = tmp.echo[cat.id] || cat;
|
|
}
|
|
var noop = function(){}, event = {stun: noop, off: noop}, u;
|
|
})(USE, './map');
|
|
|
|
;USE(function(module){
|
|
var Gun = USE('./index');
|
|
Gun.chain.set = function(item, cb, opt){
|
|
var gun = this, soul;
|
|
cb = cb || function(){};
|
|
opt = opt || {}; opt.item = opt.item || item;
|
|
if(soul = Gun.node.soul(item)){ item = Gun.obj.put({}, soul, Gun.val.link.ify(soul)) }
|
|
if(!Gun.is(item)){
|
|
if(Gun.obj.is(item)){
|
|
//item = gun.back(-1).get(soul = soul || Gun.node.soul(item) || (gun.back('opt.uuid') || uuid)()).put(item);
|
|
soul = soul || Gun.node.soul(item) || uuid(); // this just key now, not a soul.
|
|
}
|
|
return gun.get(soul || uuid()).put(item, cb, opt);
|
|
}
|
|
item.get(function(soul, o, msg){
|
|
if(!soul){ return cb.call(gun, {err: Gun.log('Only a node can be linked! Not "' + msg.put + '"!')}) }
|
|
gun.put(Gun.obj.put({}, soul, Gun.val.link.ify(soul)), cb, opt);
|
|
},true);
|
|
return item;
|
|
}
|
|
function uuid(){ return Gun.state.lex() + Gun.text.random(7) }
|
|
})(USE, './set');
|
|
|
|
;USE(function(module){
|
|
USE('./shim');
|
|
|
|
function Mesh(root){
|
|
var mesh = function(){};
|
|
var opt = root.opt || {};
|
|
opt.log = opt.log || console.log;
|
|
opt.gap = opt.gap || opt.wait || 0;
|
|
opt.pack = opt.pack || (opt.memory? (opt.memory * 999 * 999) : 300000000) * 0.3;
|
|
opt.puff = opt.puff || 9; // IDEA: do a start/end benchmark, divide ops/result.
|
|
var puff = setTimeout.turn || setTimeout;
|
|
var parse = JSON.parseAsync || function(t,cb,r){ var u; try{ cb(u, JSON.parse(t,r)) }catch(e){ cb(e) } }
|
|
var json = JSON.stringifyAsync || function(v,cb,r,s){ var u; try{ cb(u, JSON.stringify(v,r,s)) }catch(e){ cb(e) } }
|
|
|
|
var dup = root.dup, dup_check = dup.check, dup_track = dup.track;
|
|
|
|
var hear = mesh.hear = function(raw, peer){
|
|
if(!raw){ return }
|
|
if(opt.pack <= raw.length){ return mesh.say({dam: '!', err: "Message too big!"}, peer) }
|
|
var msg, id, hash, tmp = raw[0], ash, DBG;
|
|
if(mesh === this){ hear.d += raw.length||0 ; ++hear.c } // STATS!
|
|
if('[' === tmp){
|
|
try{msg = JSON.parse(raw)}catch(e){opt.log('DAM JSON parse error', e)}
|
|
raw = '';
|
|
if(!msg){ return }
|
|
console.STAT && console.STAT(+new Date, msg.length, '# on hear batch');
|
|
var P = opt.puff;
|
|
(function go(){
|
|
var S = +new Date;
|
|
//var P = peer.puff || opt.puff, s = +new Date; // TODO: For future, but in mix?
|
|
var i = 0, m; while(i < P && (m = msg[i++])){ hear(m, peer) }
|
|
//peer.puff = Math.ceil((+new Date - s)? P * 1.1 : P * 0.9);
|
|
msg = msg.slice(i); // slicing after is faster than shifting during.
|
|
console.STAT && console.STAT(S, +new Date - S, 'hear loop');
|
|
flush(peer); // force send all synchronously batched acks.
|
|
if(!msg.length){ return }
|
|
puff(go, 0);
|
|
}());
|
|
return;
|
|
}
|
|
if('{' === tmp || ((raw['#'] || obj_is(raw)) && (msg = raw))){
|
|
try{msg = msg || JSON.parse(raw);
|
|
}catch(e){return opt.log('DAM JSON parse error', e)}
|
|
if(!msg){ return }
|
|
if(msg.DBG){ msg.DBG = DBG = {DBG: msg.DBG} }
|
|
DBG && (DBG.hp = +new Date);
|
|
if(!(id = msg['#'])){ id = msg['#'] = String.random(9) }
|
|
if(tmp = dup_check(id)){ return }
|
|
// DAM logic:
|
|
if(!(hash = msg['##']) && false && u !== msg.put){ /*hash = msg['##'] = Type.obj.hash(msg.put)*/ } // disable hashing for now // TODO: impose warning/penalty instead (?)
|
|
if(hash && (tmp = msg['@'] || (msg.get && id)) && dup.check(ash = tmp+hash)){ return } // Imagine A <-> B <=> (C & D), C & D reply with same ACK but have different IDs, B can use hash to dedup. Or if a GET has a hash already, we shouldn't ACK if same.
|
|
(msg._ = function(){}).via = mesh.leap = peer;
|
|
if((tmp = msg['><']) && 'string' == typeof tmp){ tmp.slice(0,99).split(',').forEach(function(k){ this[k] = 1 }, (msg._).yo = {}) } // Peers already sent to, do not resend.
|
|
// DAM ^
|
|
if(tmp = msg.dam){
|
|
if(tmp = mesh.hear[tmp]){
|
|
tmp(msg, peer, root);
|
|
}
|
|
dup_track(id);
|
|
return;
|
|
}
|
|
var S = +new Date, ST;
|
|
DBG && (DBG.is = S);
|
|
root.on('in', mesh.last = msg);
|
|
//ECHO = msg.put || ECHO; !(msg.ok !== -3740) && mesh.say({ok: -3740, put: ECHO, '@': msg['#']}, peer);
|
|
DBG && (DBG.hd = +new Date);
|
|
console.STAT && (ST = +new Date - S) > 9 && console.STAT(S, ST, 'msg'); // TODO: PERF: caught one > 1.5s on tgif
|
|
(tmp = dup_track(id)).via = peer;
|
|
if(msg.get){ tmp.it = msg }
|
|
if(ash){ dup_track(ash) } //dup.track(tmp+hash, true).it = it(msg);
|
|
mesh.leap = mesh.last = null; // warning! mesh.leap could be buggy.
|
|
}
|
|
}
|
|
var tomap = function(k,i,m){m(k,true)};
|
|
var noop = function(){};
|
|
hear.c = hear.d = 0;
|
|
|
|
;(function(){
|
|
var SMIA = 0;
|
|
var message, loop;
|
|
mesh.hash = function(msg, cb){ var data;
|
|
if(!cb){ data = msg.put }
|
|
console.log("HASH:", data);
|
|
json(data, function(err, text){
|
|
console.log("STRINGIFIED!", text);
|
|
})
|
|
}
|
|
function each(peer){ mesh.say(message, peer) }
|
|
var say = mesh.say = function(msg, peer){ var tmp;
|
|
if((tmp = this) && (tmp = tmp.to) && tmp.next){ tmp.next(msg) } // compatible with middleware adapters.
|
|
if(!msg){ return false }
|
|
var id, hash, raw, ack = msg['@'];
|
|
var DBG = msg.DBG, S; if(!peer){ S = +new Date ; DBG && (DBG.y = S) }
|
|
var meta = msg._||(msg._=function(){});
|
|
if(!(id = msg['#'])){ id = msg['#'] = String.random(9) }
|
|
!loop && dup_track(id);//.it = it(msg); // track for 9 seconds, default. Earth<->Mars would need more! // always track, maybe move this to the 'after' logic if we split function.
|
|
if(!(hash = msg['##']) && u !== msg.put && !meta.via && ack){ console.log('HASH:', msg); mesh.hash(msg); return } // TODO: Should broadcasts be hashed?
|
|
if(!(raw = meta.raw)){
|
|
raw = mesh.raw(msg);
|
|
if(hash && ack){
|
|
dup_track(ack+hash);//.it = it(msg);
|
|
if((tmp = (dup.s[ack]||{}).it) || ((tmp = mesh.last) && ack === tmp['#'])){
|
|
if(hash === tmp['##']){ return false }
|
|
tmp['##'] = hash;
|
|
}
|
|
}
|
|
}
|
|
S && console.STAT && console.STAT(S, +new Date - S, 'say prep');
|
|
//console.log("SEND!", JSON.parse(JSON.stringify(msg)));
|
|
if(!peer && ack){ peer = ((tmp = dup.s[ack]) && (tmp.via || ((tmp = tmp.it) && (tmp = tmp._) && tmp.via))) || mesh.leap } // warning! mesh.leap could be buggy!
|
|
if(!peer && ack){
|
|
console.STAT && console.STAT(+new Date, ++SMIA, 'total no peer to ack to');
|
|
return false;
|
|
} // TODO: Temporary? If ack via trace has been lost, acks will go to all peers, which trashes browser bandwidth. Not relaying the ack will force sender to ask for ack again. Note, this is technically wrong for mesh behavior.
|
|
if(!peer && mesh.way){ return mesh.way(msg) }
|
|
if(!peer || !peer.id){ message = msg;
|
|
if(!Object.plain(peer || opt.peers)){ return false }
|
|
var P = opt.puff, ps = opt.peers, pl = Object.keys(peer || opt.peers || {});
|
|
;(function go(){
|
|
var S = +new Date;
|
|
//Type.obj.map(peer || opt.peers, each); // in case peer is a peer list.
|
|
loop = 1; var wr = meta.raw; meta.raw = raw; // quick perf hack
|
|
var i = 0, p; while(i < 9 && (p = (pl||'')[i++])){
|
|
if(!(p = ps[p])){ continue }
|
|
say(msg, p);
|
|
}
|
|
meta.raw = wr; loop = 0;
|
|
pl = pl.slice(i); // slicing after is faster than shifting during.
|
|
console.STAT && console.STAT(S, +new Date - S, 'say loop');
|
|
if(!pl.length){ return }
|
|
puff(go, 0);
|
|
dup_track(ack); // keep for later
|
|
}());
|
|
return;
|
|
}
|
|
// TODO: PERF: consider splitting function here, so say loops do less work.
|
|
if(!peer.wire && mesh.wire){ mesh.wire(peer) }
|
|
if(id === peer.last){ return } peer.last = id; // was it just sent?
|
|
if(peer === meta.via){ return false } // don't send back to self.
|
|
if((tmp = meta.yo) && (tmp[peer.url] || tmp[peer.pid] || tmp[peer.id]) /*&& !o*/){ return false }
|
|
if(peer.batch){
|
|
peer.tail = (tmp = peer.tail || 0) + raw.length;
|
|
if(peer.tail <= opt.pack){
|
|
//peer.batch.push(raw);
|
|
peer.batch += (tmp?',':'')+raw;
|
|
return;
|
|
}
|
|
flush(peer);
|
|
}
|
|
//peer.batch = [];
|
|
peer.batch = '['; // Prevents double JSON!
|
|
var S = +new Date, ST;
|
|
setTimeout(function(){
|
|
console.STAT && (ST = +new Date - S) > 9 && console.STAT(S, ST, '0ms TO', id, peer.id);
|
|
flush(peer);
|
|
}, opt.gap);
|
|
send(raw, peer);
|
|
}
|
|
mesh.say.c = mesh.say.d = 0;
|
|
}());
|
|
|
|
function flush(peer){
|
|
var tmp = peer.batch, t = 'string' == typeof tmp, l;
|
|
if(t){ tmp += ']' }// TODO: Prevent double JSON!
|
|
peer.batch = peer.tail = null;
|
|
if(!tmp){ return }
|
|
if(t? 3 > tmp.length : !tmp.length){ return } // TODO: ^
|
|
if(!t){try{tmp = (1 === tmp.length? tmp[0] : JSON.stringify(tmp));
|
|
}catch(e){return opt.log('DAM JSON stringify error', e)}}
|
|
if(!tmp){ return }
|
|
send(tmp, peer);
|
|
}
|
|
// for now - find better place later.
|
|
function send(raw, peer){ try{
|
|
var wire = peer.wire;
|
|
if(peer.say){
|
|
peer.say(raw);
|
|
} else
|
|
if(wire.send){
|
|
wire.send(raw);
|
|
}
|
|
mesh.say.d += raw.length||0; ++mesh.say.c; // STATS!
|
|
}catch(e){
|
|
(peer.queue = peer.queue || []).push(raw);
|
|
}}
|
|
|
|
;(function(){
|
|
// TODO: this caused a out-of-memory crash!
|
|
mesh.raw = function(msg){ // TODO: Clean this up / delete it / move logic out!
|
|
if(!msg){ return '' }
|
|
var meta = (msg._) || {}, put, hash, tmp;
|
|
if(tmp = meta.raw){ return tmp }
|
|
if('string' == typeof msg){ return msg }
|
|
/*if(!msg.dam){ // TOOD: COME BACK TO THIS LATER!!! IMPORTANT MESH STUFF!!
|
|
var i = 0, to = []; Type.obj.map(opt.peers, function(p){
|
|
to.push(p.url || p.pid || p.id); if(++i > 3){ return true } // limit server, fast fix, improve later! // For "tower" peer, MUST include 6 surrounding ids. // REDUCED THIS TO 3 for temporary relay peer performance, towers still should list neighbors.
|
|
}); if(i > 1){ msg['><'] = to.join() }
|
|
}*/ // TOOD: COME BACK TO THIS LATER!!! IMPORTANT MESH STUFF!!
|
|
var raw = $(msg); // optimize by reusing put = the JSON.stringify from .hash?
|
|
/*if(u !== put){
|
|
tmp = raw.indexOf(_, raw.indexOf('put'));
|
|
raw = raw.slice(0, tmp-1) + put + raw.slice(tmp + _.length + 1);
|
|
//raw = raw.replace('"'+ _ +'"', put); // NEVER USE THIS! ALSO NEVER DELETE IT TO NOT MAKE SAME MISTAKE! https://github.com/amark/gun/wiki/@$$ Heisenbug
|
|
}*/
|
|
// TODO: PERF: tgif, CPU way too much on re-JSONifying ^ it.
|
|
/*
|
|
// NOTE TO SELF: Switch NTS to DAM now.
|
|
*/
|
|
if(meta && (raw||'').length < (999 * 99)){ meta.raw = raw } // HNPERF: If string too big, don't keep in memory.
|
|
return raw;
|
|
}
|
|
var $ = JSON.stringify, _ = ':])([:';
|
|
|
|
}());
|
|
|
|
mesh.hi = function(peer){
|
|
var tmp = peer.wire || {};
|
|
if(peer.id){
|
|
opt.peers[peer.url || peer.id] = peer;
|
|
} else {
|
|
tmp = peer.id = peer.id || String.random(9);
|
|
mesh.say({dam: '?', pid: root.opt.pid}, opt.peers[tmp] = peer);
|
|
delete dup.s[peer.last]; // IMPORTANT: see https://gun.eco/docs/DAM#self
|
|
}
|
|
peer.met = peer.met || +(new Date);
|
|
if(!tmp.hied){ root.on(tmp.hied = 'hi', peer) }
|
|
// @rogowski I need this here by default for now to fix go1dfish's bug
|
|
tmp = peer.queue; peer.queue = [];
|
|
(tmp||[]).forEach(function(msg){
|
|
send(msg, peer);
|
|
});
|
|
//Type.obj.native && Type.obj.native(); // dirty place to check if other JS polluted.
|
|
}
|
|
mesh.bye = function(peer){
|
|
root.on('bye', peer);
|
|
var tmp = +(new Date); tmp = (tmp - (peer.met||tmp));
|
|
mesh.bye.time = ((mesh.bye.time || tmp) + tmp) / 2;
|
|
}
|
|
mesh.hear['!'] = function(msg, peer){ opt.log('Error:', msg.err) }
|
|
mesh.hear['?'] = function(msg, peer){
|
|
if(msg.pid){
|
|
if(!peer.pid){ peer.pid = msg.pid }
|
|
if(msg['@']){ return }
|
|
}
|
|
mesh.say({dam: '?', pid: opt.pid, '@': msg['#']}, peer);
|
|
delete dup.s[peer.last]; // IMPORTANT: see https://gun.eco/docs/DAM#self
|
|
}
|
|
|
|
root.on('create', function(root){
|
|
root.opt.pid = root.opt.pid || String.random(9);
|
|
this.to.next(root);
|
|
root.on('out', mesh.say);
|
|
});
|
|
|
|
root.on('bye', function(peer, tmp){
|
|
peer = opt.peers[peer.id || peer] || peer;
|
|
this.to.next(peer);
|
|
peer.bye? peer.bye() : (tmp = peer.wire) && tmp.close && tmp.close();
|
|
delete opt.peers[peer.id];
|
|
peer.wire = null;
|
|
});
|
|
|
|
var gets = {};
|
|
root.on('bye', function(peer, tmp){ this.to.next(peer);
|
|
if(!(tmp = peer.url)){ return } gets[tmp] = true;
|
|
setTimeout(function(){ delete gets[tmp] },opt.lack || 9000);
|
|
});
|
|
root.on('hi', function(peer, tmp){ this.to.next(peer);
|
|
if(!(tmp = peer.url) || !gets[tmp]){ return } delete gets[tmp];
|
|
if(opt.super){ return } // temporary (?) until we have better fix/solution?
|
|
Object.keys(root.next).forEach(function(soul){ var node = root.next[soul];
|
|
tmp = {}; tmp[soul] = root.graph[soul];
|
|
mesh.say({'##': String.hash(tmp), get: {'#': soul}}, peer);
|
|
})
|
|
});
|
|
|
|
return mesh;
|
|
}
|
|
|
|
;(function(){
|
|
var $ = JSON.stringify, u;
|
|
|
|
Object.hash = function(obj, hash){
|
|
if(!hash && u === (obj = $(obj, sort))){ return }
|
|
return String.hash(hash || obj || '');
|
|
}
|
|
|
|
function sort(k, v){ var tmp;
|
|
if(!(v instanceof Object)){ return v }
|
|
var S = +new Date;
|
|
Object.keys(v).sort().forEach(map, {to: tmp = {}, on: v});
|
|
console.STAT && console.STAT(S, +new Date - S, 'sort');
|
|
return tmp;
|
|
}
|
|
Object.hash.sort = sort;
|
|
|
|
function map(k){
|
|
this.to[k] = this.on[k];
|
|
}
|
|
}());
|
|
|
|
function it(msg){ return msg || {_: msg._, '##': msg['##']} } // HNPERF: Only need some meta data, not full reference (took up too much memory). // HNPERF: Garrrgh! We add meta data to msg over time, copying the object happens to early.
|
|
|
|
var empty = {}, ok = true, u;
|
|
|
|
try{ module.exports = Mesh }catch(e){}
|
|
|
|
})(USE, './mesh');
|
|
|
|
;USE(function(module){
|
|
var Gun = USE('../index');
|
|
Gun.Mesh = USE('./mesh');
|
|
|
|
Gun.on('opt', function(root){
|
|
this.to.next(root);
|
|
if(root.once){ return }
|
|
var opt = root.opt;
|
|
if(false === opt.WebSocket){ return }
|
|
|
|
var env = Gun.window || {};
|
|
var websocket = opt.WebSocket || env.WebSocket || env.webkitWebSocket || env.mozWebSocket;
|
|
if(!websocket){ return }
|
|
opt.WebSocket = websocket;
|
|
|
|
var mesh = opt.mesh = opt.mesh || Gun.Mesh(root);
|
|
|
|
var wire = mesh.wire || opt.wire;
|
|
mesh.wire = opt.wire = open;
|
|
function open(peer){ try{
|
|
if(!peer || !peer.url){ return wire && wire(peer) }
|
|
var url = peer.url.replace('http', 'ws');
|
|
var wire = peer.wire = new opt.WebSocket(url);
|
|
wire.onclose = function(){
|
|
opt.mesh.bye(peer);
|
|
reconnect(peer);
|
|
};
|
|
wire.onerror = function(error){
|
|
reconnect(peer);
|
|
};
|
|
wire.onopen = function(){
|
|
opt.mesh.hi(peer);
|
|
}
|
|
wire.onmessage = function(msg){
|
|
if(!msg){ return }
|
|
opt.mesh.hear(msg.data || msg, peer);
|
|
};
|
|
return wire;
|
|
}catch(e){}}
|
|
|
|
setTimeout(function(){ !opt.super && root.on('out', {dam:'hi'}) },1); // it can take a while to open a socket, so maybe no longer lazy load for perf reasons?
|
|
|
|
var wait = 2 * 999;
|
|
function reconnect(peer){
|
|
clearTimeout(peer.defer);
|
|
if(doc && peer.retry <= 0){ return } peer.retry = (peer.retry || opt.retry || 60) - 1;
|
|
peer.defer = setTimeout(function to(){
|
|
if(doc && doc.hidden){ return setTimeout(to,wait) }
|
|
open(peer);
|
|
}, wait);
|
|
}
|
|
var doc = 'undefined' !== typeof document && document;
|
|
});
|
|
var noop = function(){};
|
|
})(USE, './websocket');
|
|
|
|
}()); |