"fix" holy grail, unbuild.

This commit is contained in:
Mark Nadal 2019-05-28 00:14:23 -07:00
parent 4aac986e94
commit cfd7d1042d
10 changed files with 179 additions and 178 deletions

3
gun.js
View File

@ -1821,13 +1821,14 @@
});
});
setTimeout(function(){
// TODO: Holy Grail dangling by this thread! If gap / offline resync doesn't trigger, it doesn't work. Ouch, and this is a localStorage specific adapter. :(
root.on('out', {put: send, '#': root.ask(ack)});
},1);
}
root.on('out', function(msg){
if(msg.lS){ return } // TODO: for IndexedDB and others, shouldn't send to peers ACKs to our own GETs.
if(Gun.is(msg.$) && msg.put && !msg['@'] && !empty(opt.peers)){
if(Gun.is(msg.$) && msg.put && !msg['@']){
id = msg['#'];
Gun.graph.is(msg.put, null, map);
if(!to){ to = setTimeout(flush, opt.wait || 1) }

2
gun.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -30,13 +30,14 @@ Gun.on('create', function(root){
});
});
setTimeout(function(){
// TODO: Holy Grail dangling by this thread! If gap / offline resync doesn't trigger, it doesn't work. Ouch, and this is a localStorage specific adapter. :(
root.on('out', {put: send, '#': root.ask(ack)});
},1);
}
root.on('out', function(msg){
if(msg.lS){ return }
if(Gun.is(msg.$) && msg.put && !msg['@'] && !empty(opt.peers)){
if(msg.lS){ return } // TODO: for IndexedDB and others, shouldn't send to peers ACKs to our own GETs.
if(Gun.is(msg.$) && msg.put && !msg['@']){
id = msg['#'];
Gun.graph.is(msg.put, null, map);
if(!to){ to = setTimeout(flush, opt.wait || 1) }
@ -108,11 +109,9 @@ Gun.on('create', function(root){
if(data && has){
data = Gun.state.to(data, has);
}
if(!data && !Gun.obj.empty(opt.peers)){ // if data not found, don't ack if there are peers.
return; // Hmm, what if we have peers but we are disconnected?
}
//if(!data && !Gun.obj.empty(opt.peers)){ return } // if data not found, don't ack if there are peers. // Hmm, what if we have peers but we are disconnected?
//console.log("lS get", lex, data);
root.on('in', {'@': msg['#'], put: Gun.graph.node(data), how: 'lS', lS: msg.$ || root.$});
root.on('in', {'@': msg['#'], put: Gun.graph.node(data), how: 'lS', lS: msg.$});// || root.$});
};
Gun.debug? setTimeout(to,1) : to();
});

View File

@ -1,67 +1,20 @@
var Gun = require('../index');
var Type = require('../type');
function Mesh(ctx){
function Mesh(root){
var mesh = function(){};
var opt = ctx.opt || {};
var opt = root.opt || {};
opt.log = opt.log || console.log;
opt.gap = opt.gap || opt.wait || 1;
opt.pack = opt.pack || (opt.memory? (opt.memory * 1000 * 1000) : 1399000000) * 0.3; // max_old_space_size defaults to 1400 MB.
mesh.out = function(msg){ var tmp;
if(this.to){ this.to.next(msg) }
//if(mesh.last != msg['#']){ return mesh.last = msg['#'], this.to.next(msg) }
if((tmp = msg['@'])
&& (tmp = ctx.dup.s[tmp])
&& (tmp = tmp.it)
&& tmp._){
mesh.say(msg, (tmp._).via, 1);
tmp['##'] = msg['##'];
return;
}
// add hook for AXE?
if (Gun.AXE) { Gun.AXE.say(msg, mesh.say, this); return; }
mesh.say(msg);
}
ctx.on('create', function(root){
root.opt.pid = root.opt.pid || Type.text.random(9);
this.to.next(root);
ctx.on('out', mesh.out);
});
var dup = root.dup;
mesh.hear = function(raw, peer){
if(!raw){ return }
var dup = ctx.dup, id, hash, msg, tmp = raw[0];
if(opt.pack <= raw.length){ return mesh.say({dam: '!', err: "Message too big!"}, peer) }
if('{' === tmp){
try{msg = JSON.parse(raw);}catch(e){opt.log('DAM JSON parse error', e)}
if(!msg){ return }
mesh.hear.d += raw.length; ++mesh.hear.c; // STATS!
if(dup.check(id = msg['#'])){ return }
dup.track(id, true).it = msg; // GUN core also dedups, so `true` is needed.
if((tmp = msg['@']) && msg.put){
hash = msg['##'] || (msg['##'] = mesh.hash(msg));
if((tmp = tmp + hash) != id){
if(dup.check(tmp)){ return }
(tmp = dup.s)[hash] = tmp[id];
}
}
(msg._ = function(){}).via = peer;
if((tmp = msg['><'])){
(msg._).to = Type.obj.map(tmp.split(','), tomap);
}
if(msg.dam){
if(tmp = mesh.hear[msg.dam]){
tmp(msg, peer, ctx);
}
return;
}
ctx.on('in', msg);
return;
} else
var msg, id, hash, tmp = raw[0];
if(opt.pack <= raw.length){ return mesh.say({dam: '!', err: "Message too big!"}, peer) }
if('{' != raw[2]){ mesh.hear.d += raw.length||0; ++mesh.hear.c; } // STATS! // ugh, stupid double JSON encoding
if('[' === tmp){
try{msg = JSON.parse(raw);}catch(e){opt.log('DAM JSON parse error', e)}
if(!msg){ return }
@ -69,179 +22,222 @@ function Mesh(ctx){
while(m = msg[i++]){
mesh.hear(m, peer);
}
return;
}
if('{' === tmp || (Type.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(!(id = msg['#'])){ id = msg['#'] = Type.text.random(9) }
if(dup.check(id)){ return }
dup.track(id, true).it = msg; // GUN core also dedups, so `true` is needed. // Does GUN core need to dedup anymore?
if(!(hash = msg['##']) && u !== msg.put){ hash = msg['##'] = Type.obj.hash(msg.put) }
if(hash && (tmp = msg['@'] || (msg.get && id))){ // Reduces backward daisy in case varying hashes at different daisy depths are the same.
if(dup.check(tmp+hash)){ return }
dup.track(tmp+hash, true).it = msg; // GUN core also dedups, so `true` is needed. // Does GUN core need to dedup anymore?
}
(msg._ = function(){}).via = peer;
if(tmp = msg['><']){ (msg._).to = Type.obj.map(tmp.split(','), tomap) }
if(msg.dam){
if(tmp = mesh.hear[msg.dam]){
tmp(msg, peer, root);
}
return;
}
root.on('in', msg);
return;
}
}
mesh.hear.c = mesh.hear.d = 0;
var tomap = function(k,i,m){m(k,true)};
mesh.hear.c = mesh.hear.d = 0;
;(function(){
var message;
function each(peer){ mesh.say(message, peer) }
mesh.say = function(msg, peer, o){
/*
TODO: Plenty of performance optimizations
that can be made just based off of ordering,
and reducing function calls for cached writes.
*/
if(!peer){ message = msg;
Type.obj.map(opt.peers, each);
return;
}
var tmp, wire = peer.wire || ((opt.wire) && opt.wire(peer)), msh, raw;// || open(peer, ctx); // TODO: Reopen!
if(!wire){ return }
msh = (msg._) || empty;
if(peer === msh.via){ return }
if(!(raw = msh.raw)){ raw = mesh.raw(msg) }
if((tmp = msg['@'])
&& (tmp = ctx.dup.s[tmp])
&& (tmp = tmp.it)){
if(tmp.get && tmp['##'] && tmp['##'] === msg['##']){ // PERF: move this condition outside say?
return; // TODO: this still needs to be tested in the browser!
mesh.say = function(msg, peer){
if(this.to){ this.to.next(msg) } // compatible with middleware adapters.
if(!msg){ return false }
var id, hash, tmp, raw;
var meta = msg._||(msg._=function(){});
if(!(id = msg['#'])){ id = msg['#'] = Type.text.random(9) }
if(!(hash = msg['##']) && u !== msg.put){ hash = msg['##'] = Type.obj.hash(msg.put) }
if(!(raw = meta.raw)){
raw = meta.raw = mesh.raw(msg);
if(hash && (tmp = msg['@'])){
dup.track(tmp+hash).it = msg;
if(tmp = (dup.s[tmp]||ok).it){
if(hash === tmp['##']){ return false }
tmp['##'] = hash;
}
}
}
if((tmp = msh.to) && (tmp[peer.url] || tmp[peer.id]) && !o){ return } // TODO: still needs to be tested
dup.track(id).it = msg; // track for 9 seconds, default. Earth<->Mars would need more!
if(!peer){ peer = (tmp = dup.s[msg['@']]) && (tmp = tmp.it) && (tmp = tmp._) && (tmp = tmp.via) }
if(!peer && mesh.way){ return mesh.way(msg) }
if(!peer || !peer.id){ message = msg;
if(!Type.obj.is(peer || opt.peers)){ return false }
Type.obj.map(peer || opt.peers, each); // in case peer is a peer list.
return;
}
if(!peer.wire && mesh.wire){ mesh.wire(peer) }
if(peer === meta.via){ return false }
if((tmp = meta.to) && (tmp[peer.url] || tmp[peer.pid] || tmp[peer.id]) /*&& !o*/){ return false }
if(peer.batch){
peer.tail = (peer.tail || 0) + raw.length;
peer.tail = (tmp = peer.tail || 0) + raw.length;
if(peer.tail <= opt.pack){
peer.batch.push(raw);
peer.batch.push(raw); // peer.batch += (tmp?'':',')+raw; // TODO: Prevent double JSON! // FOR v1.0 !?
return;
}
flush(peer);
}
peer.batch = [];
peer.batch = []; // peer.batch = '['; // TODO: Prevent double JSON!
setTimeout(function(){flush(peer)}, opt.gap);
send(raw, peer);
}
function flush(peer){
var tmp = peer.batch;
if(!tmp){ return }
var tmp = peer.batch; // var tmp = peer.batch + ']'; // TODO: Prevent double JSON!
peer.batch = peer.tail = null;
if(!tmp.length){ return }
try{send(JSON.stringify(tmp), peer);
}catch(e){opt.log('DAM JSON stringify error', e)}
}
function send(raw, peer){
var wire = peer.wire;
try{
if(peer.say){
peer.say(raw);
} else
if(wire.send){
wire.send(raw);
}
mesh.say.d += raw.length; ++mesh.say.c; // STATS!
}catch(e){
(peer.queue = peer.queue || []).push(raw);
}
if(!tmp){ return }
if(!tmp.length){ return } // if(3 > tmp.length){ return } // TODO: ^
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);
}
mesh.say.c = mesh.say.d = 0;
}());
// 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(){
mesh.raw = function(msg){
mesh.raw = function(msg){ // TODO: Clean this up / delete it / move logic out!
if(!msg){ return '' }
var dup = ctx.dup, msh = (msg._) || {}, put, hash, tmp;
if(tmp = msh.raw){ return tmp }
var meta = (msg._) || {}, put, hash, tmp;
if(tmp = meta.raw){ return tmp }
if(typeof msg === 'string'){ return msg }
if(msg['@'] && (tmp = msg.put)){
if(!(hash = msg['##'])){
put = $(tmp, sort) || '';
hash = mesh.hash(msg, put);
msg['##'] = hash;
}
(tmp = dup.s)[hash = msg['@']+hash] = tmp[msg['#']];
msg['#'] = hash || msg['#'];
if(put){ (msg = Type.obj.to(msg)).put = _ }
if(!msg.dam){
var i = 0, to = []; Type.obj.map(opt.peers, function(p){
to.push(p.url || p.pid || p.id); if(++i > 9){ return true } // limit server, fast fix, improve later! // For "tower" peer, MUST include 6 surrounding ids.
}); if(i > 1){ msg['><'] = to.join() }
}
var i = 0, to = []; Type.obj.map(opt.peers, function(p){
to.push(p.url || p.id); if(++i > 9){ return true } // limit server, fast fix, improve later!
}); msg['><'] = to.join();
var raw = $(msg);
if(u !== put){
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); // https://github.com/amark/gun/wiki/@$$ Heisenbug
}
if(msh){
msh.raw = raw;
}
//raw = raw.replace('"'+ _ +'"', put); // NEVER USE THIS! ALSO NEVER DELETE IT TO NOT MAKE SAME MISTAKE! https://github.com/amark/gun/wiki/@$$ Heisenbug
}*/
if(meta){ meta.raw = raw }
return raw;
}
mesh.hash = function(msg, hash){
return Mesh.hash(hash || $(msg.put, sort) || '') || msg['#'] || Type.text.random(9);
}
function sort(k, v){ var tmp;
if(!(v instanceof Object)){ return v }
Type.obj.map(Object.keys(v).sort(), map, {to: tmp = {}, on: v});
return tmp;
}
function map(k){
this.to[k] = this.on[k];
}
var $ = JSON.stringify, _ = ':])([:';
}());
mesh.hi = function(peer){
var tmp = peer.wire || {};
if(peer.id || peer.url){
if(peer.id){
opt.peers[peer.url || peer.id] = peer;
} else {
tmp = peer.id = tmp.pid = peer.id || Type.text.random(9);
tmp = peer.id = peer.id || Type.text.random(9);
mesh.say({dam: '?'}, opt.peers[tmp] = peer);
}
if(!tmp.hied){ ctx.on(tmp.hied = 'hi', peer) }
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 = [];
Type.obj.map(tmp, function(msg){
mesh.say(msg, peer);
send(msg, peer);
});
}
mesh.bye = function(peer){
Type.obj.del(opt.peers, peer.id); // assume if peer.url then reconnect
ctx.on('bye', 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){
mesh.say({dam: '?', pid: opt.pid, '@': msg['#']}, peer);
// @rogowski I want to re-enable this AXE logic with some fix/merge later.
// var tmp = peer.queue; peer.queue = [];
// Type.obj.map(tmp, function(msg){
// mesh.say(msg, peer);
// });
/* var tmp = peer.queue; peer.queue = [];
Type.obj.map(tmp, function(msg){
mesh.say(msg, peer);
}); */
// @rogowski 2: I think with my PID fix we can delete this and use the original.
return;
}
if(!peer.wire){ return }
if(!peer.wire.pid){ return } // only run code below if wire.pid exists
Type.obj.del(opt.peers, peer.wire.pid || peer.id);
delete peer.wire.pid;
peer.id = msg.pid;
mesh.hi(peer);
if(peer.pid){ return }
peer.pid = msg.pid;
}
root.on('create', function(root){
root.opt.pid = root.opt.pid || Type.text.random(9);
this.to.next(root);
root.on('out', mesh.say);
});
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];
Type.obj.map(root.next, function(node, soul){
tmp = {}; tmp[soul] = root.graph[soul];
mesh.say({'##': Type.obj.hash(tmp), get: {'#': soul}}, peer);
})
});
return mesh;
}
Mesh.hash = function(s){ // via SO
if(typeof s !== 'string'){ return {err: 1} }
var 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; // Math.abs(c);
}
;(function(){
Type.text.hash = function(s){ // via SO
if(typeof s !== 'string'){ return {err: 1} }
var 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; // Math.abs(c);
}
var $ = JSON.stringify, u;
var empty = {}, u;
Type.obj.hash = function(obj, hash){
if(!hash && u === (obj = $(obj, sort))){ return }
return Type.text.hash(hash || obj || '');
}
function sort(k, v){ var tmp;
if(!(v instanceof Object)){ return v }
Type.obj.map(Object.keys(v).sort(), map, {to: tmp = {}, on: v});
return tmp;
}
Type.obj.hash.sort = sort;
function map(k){
this.to[k] = this.on[k];
}
}());
var empty = {}, ok = true, u;
Object.keys = Object.keys || function(o){ return map(o, function(v,k,t){t(k)}) }
try{ module.exports = Mesh }catch(e){}

View File

@ -19,8 +19,8 @@ Gun.on('opt', function(root){
var mesh = opt.mesh = opt.mesh || Gun.Mesh(root);
var wire = opt.wire;
opt.wire = open;
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');

View File

@ -247,7 +247,7 @@ function not(at, msg){
if(u === tmp && u !== at.put){ return true }
neat.put = u;
if(neat.ack){
neat.ack = -1;
neat.ack = -1; // TODO: BUG? Should this be 0?
}
neat.on('in', {
get: key,

View File

@ -101,6 +101,7 @@ Gun.chain.off = function(){
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);

View File

@ -107,11 +107,11 @@ function batch(){ var as = this;
var cat = (as.$.back(-1)._), ask = cat.ask(function(ack){
cat.root.on('ack', ack);
if(ack.err){ Gun.log(ack) }
if(!ack.lack){ this.off() } // One response is good enough for us currently. Later we may want to adjust this.
if(++acks > (as.acks || 0)){ this.off() } // Adjustable ACKs! Only 1 by default.
if(!as.ack){ return }
as.ack(ack, this);
//--C;
}, as.opt);
}, 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.

View File

@ -185,7 +185,7 @@ Gun.dup = require('./dup');
if(text_is(tmp)){ tmp = [tmp] }
if(list_is(tmp)){
tmp = obj_map(tmp, function(url, i, map){
map(url, {url: url});
i = {}; i.id = i.url = url; map(url, i);
});
if(!obj_is(at.opt.peers)){ at.opt.peers = {}}
at.opt.peers = obj_to(tmp, at.opt.peers);

View File

@ -50,6 +50,8 @@ describe("The Holy Grail Test!", function(){
test.async();
try{ require('fs').unlinkSync(env.i+'data') }catch(e){}
try{ require('fs').unlinkSync((env.i+1)+'data') }catch(e){}
try{ require('gun/lib/fsrm')(env.i+'data') }catch(e){}
try{ require('gun/lib/fsrm')((env.i+1)+'data') }catch(e){}
var port = env.config.port + env.i;
var server = require('http').createServer(function(req, res){
res.end("I am "+ env.i +"!");
@ -104,7 +106,8 @@ describe("The Holy Grail Test!", function(){
return server.run(function(test){
console.log(3);
var env = test.props;
try{ require('fs').unlinkSync(env.i+'data'); }catch(e){}
try{ require('fs').unlinkSync(env.i+'data') }catch(e){}
try{ require('gun/lib/fsrm')(env.i+'data') }catch(e){}
process.exit(0);
}, {i: 1, config: config})
});
@ -176,6 +179,7 @@ describe("The Holy Grail Test!", function(){
var env = test.props;
test.async();
try{ require('fs').unlinkSync(env.i+'data') }catch(e){}
try{ require('gun/lib/fsrm')(env.i+'data') }catch(e){}
var port = env.config.port + env.i;
var server = require('http').createServer(function(req, res){
res.end("I am "+ env.i +"!");