diff --git a/gun.js b/gun.js index 3467a56d..4f506c7f 100644 --- a/gun.js +++ b/gun.js @@ -923,7 +923,7 @@ if(cat.jam){ return cat.jam.push([cb, as]) } cat.jam = [[cb,as]]; gun.get(function go(msg, eve){ - if(u === msg.put && (tmp = Object.keys(cat.root.opt.peers).length) && ++acks <= tmp){ + if(u === msg.put && (tmp = Object.keys(cat.root.opt.peers).length) && ++acks <= tmp){ // TODO: BUG! If NodeJS && people connec to it, this peer count will be wrong. See other ref of this bug. return; } eve.rid(msg); @@ -1311,6 +1311,7 @@ } if((tmp = eve.wait) && (tmp = tmp[at.id])){ clearTimeout(tmp) } eve.ack = (eve.ack||0)+1; + // TODO: BUG! If NodeJS && people connec to it, these peer counts will be wrong + see other ref of this bug: 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 && (tmp = Object.keys(at.root.opt.peers).length) && (!to && (link||at).ack < tmp))){ @@ -1436,6 +1437,8 @@ 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; @@ -1503,16 +1506,23 @@ ;(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){ - if(this.to){ this.to.next(msg) } // compatible with middleware adapters. + if(this && this.to){ this.to.next(msg) } // compatible with middleware adapters. if(!msg){ return false } var id, hash, tmp, 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){ say.hash(msg); return } // TODO: Should broadcasts be hashed? + 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){ diff --git a/lib/yson.js b/lib/yson.js index 946399e6..5ae7785b 100644 --- a/lib/yson.js +++ b/lib/yson.js @@ -148,6 +148,66 @@ function value(s){ } } -module.exports = yson; +yson.stringifyAsync = function(data, done, replacer, space, ctx){ + ctx = ctx || {}; + ctx.text = ctx.text || ""; + ctx.up = [ctx.at = {d: data}]; + ctx.done = done; + ctx.i = 0; + var j = 0; + ify(); + function ify(){ + var at = ctx.at, data = at.d, tmp; + //console.log(at.k, typeof data, data); + if(at.i > 0){ ctx.text += ',' } + if(u !== (tmp = at.k)){ ctx.text += '"'+tmp+'":' } + switch(typeof data){ + case 'boolean': + ctx.text += ''+data; + break; + case 'string': + //ctx.text += JSON.stringify(data); + ctx.text += '"'+data+'"';//JSON.stringify(data); + break; + case 'number': + ctx.text += data; + break; + case 'object': + if(!data){ + ctx.text += 'null'; + break; + } + if(data instanceof Array){ + ctx.text += '['; + at = {i: -1, as: data, up: at}; + at.l = data.length; + ctx.up.push(ctx.at = at); + break; + } + ctx.text += '{'; + at = {i: -1, ok: Object.keys(data).sort(), as: data, up: at}; + at.l = at.ok.length; + ctx.up.push(ctx.at = at); + break; + } + while(1+at.i >= at.l){ + ctx.text += (at.ok? '}' : ']'); + at = ctx.at = at.up; + } + if(++at.i < at.l){ + if(tmp = at.ok){ + at.d = at.as[at.k = tmp[at.i]]; + } else { + at.d = at.as[at.i]; + } + if(++j < 9){ return ify() } else { j = 0 } + sI(ify); + return; + } + ctx.done(u, ctx.text); + } +} +if(typeof window != ''+u){ window.YSON = yson } +try{ if(typeof module != ''+u){ module.exports = yson } }catch(e){} }()); \ No newline at end of file diff --git a/sea.js b/sea.js index fbf76363..2ede139c 100644 --- a/sea.js +++ b/sea.js @@ -1426,8 +1426,6 @@ SEA.opt.shuffle_attack = 1546329600000; // Jan 1, 2019 var noop = function(){}, u; var fl = Math.floor; // TODO: Still need to fix inconsistent state issue. - var rel_is = Gun.val.rel.is; - var obj_ify = Gun.obj.ify; // TODO: Potential bug? If pub/priv key starts with `-`? IDK how possible. })(USE, './index'); diff --git a/test/mocha.html b/test/mocha.html index 2e8e039a..42893ede 100644 --- a/test/mocha.html +++ b/test/mocha.html @@ -23,9 +23,11 @@ + +