dev -> master, master ->

This commit is contained in:
Mark Nadal 2020-01-21 15:38:31 -08:00
parent f8f9cddafe
commit b5fd757db4
7 changed files with 50 additions and 33 deletions

2
gun.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -2,7 +2,7 @@
var Gun = require('../gun'), u; var Gun = require('../gun'), u;
Gun.serve = require('./serve'); Gun.serve = require('./serve');
//process.env.GUN_ENV = process.env.GUN_ENV || 'debug'; //process.env.GUN_ENV = process.env.GUN_ENV || 'debug';
console.LOG = true; // only temporarily, REVERT THIS IN FUTURE! //console.LOG = true; // only do this for dev.
Gun.on('opt', function(root){ Gun.on('opt', function(root){
if(u === root.opt.super){ root.opt.super = true } if(u === root.opt.super){ root.opt.super = true }
if(u === root.opt.faith){ root.opt.faith = true } // HNPERF: This should probably be off, but we're testing performance improvements, please audit. if(u === root.opt.faith){ root.opt.faith = true } // HNPERF: This should probably be off, but we're testing performance improvements, please audit.

2
sea.js
View File

@ -659,7 +659,7 @@
SEA.verify = USE('./verify'); SEA.verify = USE('./verify');
SEA.encrypt = USE('./encrypt'); SEA.encrypt = USE('./encrypt');
SEA.decrypt = USE('./decrypt'); SEA.decrypt = USE('./decrypt');
SEA.aeskey = USE('./aeskey'); SEA.opt.aeskey = USE('./aeskey'); // not official!
SEA.random = SEA.random || shim.random; SEA.random = SEA.random || shim.random;

View File

@ -11,6 +11,7 @@ function Mesh(root){
var dup = root.dup; var dup = root.dup;
// TODO: somewhere in here caused a out-of-memory crash! How? It is inbound!
mesh.hear = function(raw, peer){ mesh.hear = function(raw, peer){
if(!raw){ return } if(!raw){ return }
var msg, id, hash, tmp = raw[0]; var msg, id, hash, tmp = raw[0];
@ -39,11 +40,11 @@ function Mesh(root){
if(!(id = msg['#'])){ id = msg['#'] = Type.text.random(9) } if(!(id = msg['#'])){ id = msg['#'] = Type.text.random(9) }
if(msg.DBG_s){ opt.log(+new Date - msg.DBG_s, 'to hear', id) } if(msg.DBG_s){ opt.log(+new Date - msg.DBG_s, 'to hear', id) }
if(dup.check(id)){ return } 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? dup.track(id, true).it = 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 = 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(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 } 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? dup.track(tmp+hash, true).it = it(msg); // GUN core also dedups, so `true` is needed. // Does GUN core need to dedup anymore?
} }
(msg._ = function(){}).via = peer; (msg._ = function(){}).via = peer;
if(tmp = msg['><']){ (msg._).to = Type.obj.map(tmp.split(','), tomap) } if(tmp = msg['><']){ (msg._).to = Type.obj.map(tmp.split(','), tomap) }
@ -53,9 +54,9 @@ function Mesh(root){
} }
return; return;
} }
var S; LOG && (S = +new Date); var S, ST; LOG && (S = +new Date);
root.on('in', msg); root.on('in', msg);
LOG && !msg.nts && opt.log(S, +new Date - S, 'msg', msg['#']); LOG && !msg.nts && (ST = +new Date - S) > 9 && opt.log(S, ST, 'msg', msg['#']);
return; return;
} }
} }
@ -63,6 +64,7 @@ function Mesh(root){
mesh.hear.c = mesh.hear.d = 0; mesh.hear.c = mesh.hear.d = 0;
;(function(){ ;(function(){
var SMIA = 0;
var message; var message;
function each(peer){ mesh.say(message, peer) } function each(peer){ mesh.say(message, peer) }
mesh.say = function(msg, peer){ mesh.say = function(msg, peer){
@ -74,24 +76,28 @@ function Mesh(root){
if(!(id = msg['#'])){ id = msg['#'] = Type.text.random(9) } if(!(id = msg['#'])){ id = msg['#'] = Type.text.random(9) }
if(!(hash = msg['##']) && u !== msg.put){ hash = msg['##'] = Type.obj.hash(msg.put) } if(!(hash = msg['##']) && u !== msg.put){ hash = msg['##'] = Type.obj.hash(msg.put) }
if(!(raw = meta.raw)){ if(!(raw = meta.raw)){
raw = meta.raw = mesh.raw(msg); raw = mesh.raw(msg);
if(hash && (tmp = msg['@'])){ if(hash && (tmp = msg['@'])){
dup.track(tmp+hash).it = msg; dup.track(tmp+hash).it = it(msg);
if(tmp = (dup.s[tmp]||ok).it){ if(tmp = (dup.s[tmp]||ok).it){
if(hash === tmp['##']){ return false } if(hash === tmp['##']){ return false }
tmp['##'] = hash; tmp['##'] = hash;
} }
} }
} }
LOG && opt.log(S, +new Date - S, 'say prep'); //LOG && opt.log(S, +new Date - S, 'say prep');
dup.track(id).it = msg; // track for 9 seconds, default. Earth<->Mars would need more! dup.track(id).it = 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){ peer = (tmp = dup.s[msg['@']]) && (tmp = tmp.it) && (tmp = tmp._) && (tmp = tmp.via) }
if(!peer && msg['@']){
LOG && opt.log(+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 && mesh.way){ return mesh.way(msg) }
if(!peer || !peer.id){ message = msg; if(!peer || !peer.id){ message = msg;
if(!Type.obj.is(peer || opt.peers)){ return false } if(!Type.obj.is(peer || opt.peers)){ return false }
var S; LOG && (S = +new Date); //var S; LOG && (S = +new Date);
Type.obj.map(peer || opt.peers, each); // in case peer is a peer list. Type.obj.map(peer || opt.peers, each); // in case peer is a peer list.
LOG && opt.log(S, +new Date - S, 'say loop'); //LOG && opt.log(S, +new Date - S, 'say loop');
return; return;
} }
if(!peer.wire && mesh.wire){ mesh.wire(peer) } if(!peer.wire && mesh.wire){ mesh.wire(peer) }
@ -128,20 +134,21 @@ function Mesh(root){
// for now - find better place later. // for now - find better place later.
function send(raw, peer){ try{ function send(raw, peer){ try{
var wire = peer.wire; var wire = peer.wire;
var S; LOG && (S = +new Date); var S, ST; LOG && (S = +new Date);
if(peer.say){ if(peer.say){
peer.say(raw); peer.say(raw);
} else } else
if(wire.send){ if(wire.send){
wire.send(raw); wire.send(raw);
} }
LOG && opt.log(S, +new Date - S, 'wire send', raw.length); LOG && (ST = +new Date - S) > 9 && opt.log(S, ST, 'wire send', raw.length);
mesh.say.d += raw.length||0; ++mesh.say.c; // STATS! mesh.say.d += raw.length||0; ++mesh.say.c; // STATS!
}catch(e){ }catch(e){
(peer.queue = peer.queue || []).push(raw); (peer.queue = peer.queue || []).push(raw);
}} }}
;(function(){ ;(function(){
// TODO: this caused a out-of-memory crash!
mesh.raw = function(msg){ // TODO: Clean this up / delete it / move logic out! mesh.raw = function(msg){ // TODO: Clean this up / delete it / move logic out!
if(!msg){ return '' } if(!msg){ return '' }
var meta = (msg._) || {}, put, hash, tmp; var meta = (msg._) || {}, put, hash, tmp;
@ -149,7 +156,7 @@ function Mesh(root){
if(typeof msg === 'string'){ return msg } if(typeof msg === 'string'){ return msg }
if(!msg.dam){ if(!msg.dam){
var i = 0, to = []; Type.obj.map(opt.peers, function(p){ 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. 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() } }); if(i > 1){ msg['><'] = to.join() }
} }
var raw = $(msg); // optimize by reusing put = the JSON.stringify from .hash? var raw = $(msg); // optimize by reusing put = the JSON.stringify from .hash?
@ -158,7 +165,7 @@ function Mesh(root){
raw = raw.slice(0, tmp-1) + put + raw.slice(tmp + _.length + 1); 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 //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 } if(meta && (raw||'').length < (1000 * 100)){ meta.raw = raw } // HNPERF: If string too big, don't keep in memory.
return raw; return raw;
} }
var $ = JSON.stringify, _ = ':])([:'; var $ = JSON.stringify, _ = ':])([:';
@ -260,6 +267,8 @@ function Mesh(root){
} }
}()); }());
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; var empty = {}, ok = true, u;
var LOG = console.LOG; var LOG = console.LOG;

View File

@ -2,7 +2,7 @@
var Type = require('./type'); var Type = require('./type');
function Dup(opt){ function Dup(opt){
var dup = {s:{}}; var dup = {s:{}};
opt = opt || {max: 1000, age: 1000 * 9};//1000 * 60 * 2}; opt = opt || {max: 1000, age: /*1000 * 9};//*/ 1000 * 9 * 3};
dup.check = function(id){ var tmp; dup.check = function(id){ var tmp;
if(!(tmp = dup.s[id])){ return false } if(!(tmp = dup.s[id])){ return false }
if(tmp.pass){ return tmp.pass = false } if(tmp.pass){ return tmp.pass = false }
@ -12,17 +12,16 @@ function Dup(opt){
var it = dup.s[id] || (dup.s[id] = {}); var it = dup.s[id] || (dup.s[id] = {});
it.was = time_is(); it.was = time_is();
if(pass){ it.pass = true } if(pass){ it.pass = true }
if(!dup.to){ if(!dup.to){ dup.to = setTimeout(dup.drop, opt.age + 9) }
dup.to = setTimeout(function(){ return it;
}
dup.drop = function(age){
var now = time_is(); var now = time_is();
Type.obj.map(dup.s, function(it, id){ Type.obj.map(dup.s, function(it, id){
if(it && opt.age > (now - it.was)){ return } if(it && (age || opt.age) > (now - it.was)){ return }
Type.obj.del(dup.s, id); Type.obj.del(dup.s, id);
}); });
dup.to = null; dup.to = null;
}, opt.age + 9);
}
return it;
} }
return dup; return dup;
} }

View File

@ -53,7 +53,8 @@ Gun.dup = require('./dup');
return; return;
} }
dup.track(tmp); dup.track(tmp);
if(!at.ask(msg['@'], msg)){ if(tmp = msg['@']){ dup.track(tmp) } // HNPERF: Bump original request's liveliness.
if(!at.ask(tmp, msg)){
if(msg.get){ if(msg.get){
Gun.on.get(msg, gun); //at.on('get', get(msg)); Gun.on.get(msg, gun); //at.on('get', get(msg));
} }
@ -72,18 +73,21 @@ Gun.dup = require('./dup');
;(function(){ ;(function(){
Gun.on.put = function(msg, gun){ Gun.on.put = function(msg, gun){
var at = gun._, ctx = {$: gun, graph: at.graph, put: {}, map: {}, souls: {}, machine: Gun.state(), ack: msg['@'], cat: at, stop: {}}; var at = gun._, ctx = {$: gun, graph: at.graph, put: {}, map: {}, souls: {}, machine: Gun.state(), ack: msg['@'], cat: at, stop: {}};
if(!Gun.obj.map(msg.put, perf, ctx)){ return } // HNPERF: performance test, not core code, do not port.
if(!Gun.graph.is(msg.put, null, verify, ctx)){ ctx.err = "Error: Invalid graph!" } if(!Gun.graph.is(msg.put, null, verify, ctx)){ ctx.err = "Error: Invalid graph!" }
if(ctx.err){ return at.on('in', {'@': msg['#'], err: Gun.log(ctx.err) }) } if(ctx.err){ return at.on('in', {'@': msg['#'], err: Gun.log(ctx.err) }) }
obj_map(ctx.put, merge, ctx); obj_map(ctx.put, merge, ctx);
if(!ctx.async){ obj_map(ctx.map, map, ctx) } if(!ctx.async){ obj_map(ctx.map, map, ctx) }
if(u !== ctx.defer){ if(u !== ctx.defer){
var to = ctx.defer - ctx.machine;
setTimeout(function(){ setTimeout(function(){
Gun.on.put(msg, gun); Gun.on.put(msg, gun);
}, ctx.defer - ctx.machine); }, to > MD? MD : to ); // setTimeout Max Defer 32bit :(
} }
if(!ctx.diff){ return } if(!ctx.diff){ return }
at.on('put', obj_to(msg, {put: ctx.diff})); at.on('put', obj_to(msg, {put: ctx.diff}));
}; };
var MD = 2147483647;
function verify(val, key, node, soul){ var ctx = this; function verify(val, key, node, soul){ var ctx = this;
var state = Gun.state.is(node, key), tmp; var state = Gun.state.is(node, key), tmp;
if(!state){ return ctx.err = "Error: No state on '"+key+"' in node '"+soul+"'!" } if(!state){ return ctx.err = "Error: No state on '"+key+"' in node '"+soul+"'!" }
@ -149,6 +153,7 @@ Gun.dup = require('./dup');
(msg.$._).on('in', msg); (msg.$._).on('in', msg);
this.cat.stop = null; // temporary fix till a better solution? this.cat.stop = null; // temporary fix till a better solution?
} }
function perf(node, soul){ if(node !== this.graph[soul]){ return true } } // HNPERF: do not port!
Gun.on.get = function(msg, gun){ Gun.on.get = function(msg, gun){
var root = gun._, get = msg.get, soul = get[_soul], node = root.graph[soul], has = get[_has], tmp; var root = gun._, get = msg.get, soul = get[_soul], node = root.graph[soul], has = get[_has], tmp;
@ -162,15 +167,17 @@ Gun.dup = require('./dup');
// Maybe... in case the in-memory key we have is a local write // Maybe... in case the in-memory key we have is a local write
// we still need to trigger a pull/merge from peers. // we still need to trigger a pull/merge from peers.
} else { } else {
node = Gun.obj.copy(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
} }
node = Gun.graph.node(node); node = Gun.graph.node(node);
tmp = (at||empty).ack; tmp = (at||empty).ack;
var faith = function(){}; faith.faith = true; // HNPERF: We're testing performance improvement by skipping going through security again, but this should be audited.
root.on('in', { root.on('in', {
'@': msg['#'], '@': msg['#'],
how: 'mem', how: 'mem',
put: node, put: node,
$: gun $: gun,
_: faith
}); });
//if(0 < tmp){ return } //if(0 < tmp){ return }
root.on('get', msg); root.on('get', msg);

View File

@ -38,7 +38,9 @@ describe('SEA', function(){
var pub; var pub;
describe('Utility', function(){ describe('Utility', function(){
it('generates aeskey from jwk', function(done) { it('generates aeskey from jwk', function(done) {
SEA.aeskey('x','x').then(k => { console.log("WARNING: THIS DOES NOT WORK IN BROWSER!!!! NEEDS FIX");
SEA.opt.aeskey('x','x').then(k => {
//console.log("DATA", k.data);
expect(k.data.toString('base64')).to.be('Xd6JaIf2dUybFb/jpEGuSAbfL96UABMR4IvxEGIuC74=') expect(k.data.toString('base64')).to.be('Xd6JaIf2dUybFb/jpEGuSAbfL96UABMR4IvxEGIuC74=')
done() done()
}) })