From a96a80ce661359f4ef4d37874fd84c5e7a929c89 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Wed, 30 Jan 2019 23:41:30 -0800 Subject: [PATCH 1/3] set gun ref with SEA causes race condition while crypto op happen, cache dub --- examples/basic/user.html | 2 +- gun.js | 11 +++++++---- package.json | 2 +- test/common.js | 20 +++++++++++++++++--- test/sea/sea.js | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 9 deletions(-) diff --git a/examples/basic/user.html b/examples/basic/user.html index 9ae9b009a..873d517c0 100644 --- a/examples/basic/user.html +++ b/examples/basic/user.html @@ -5,7 +5,7 @@

User

- + diff --git a/gun.js b/gun.js index 52e8d3ac4..0e8e55081 100644 --- a/gun.js +++ b/gun.js @@ -1160,7 +1160,10 @@ if(!(at.has || at.soul)){ return } var tmp = at.map, root = at.root; at.map = null; - if(at.has){ at.link = 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 } @@ -1286,8 +1289,7 @@ } function soul(gun, cb, opt, as){ var cat = gun._, acks = 0, tmp; - if(tmp = cat.soul){ return cb(tmp, as, cat), gun } - if(tmp = cat.link){ return cb(tmp, as, cat), gun } + if(tmp = cat.soul || cat.link || cat.dub){ return cb(tmp, as, cat), gun } gun.get(function(msg, ev){ if(u === msg.put && (tmp = (obj_map(cat.root.opt.peers, function(v,k,t){t(k)})||[]).length) && ++acks < tmp){ return; @@ -1397,6 +1399,7 @@ }, 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); @@ -1551,7 +1554,7 @@ if(node_ == at.get){ as.soul = (at.put||empty)['#'] || at.dub; } - as.soul = as.soul || at.soul || at.soul || (opt.uuid || as.via.back('opt.uuid') || Gun.text.random)(); + 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 diff --git a/package.json b/package.json index f3c57aa06..2cf8e82d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gun", - "version": "0.9.999999", + "version": "0.9.9999991", "description": "A realtime, decentralized, offline-first, graph data synchronization engine.", "main": "index.js", "browser": "gun.min.js", diff --git a/test/common.js b/test/common.js index 4b754270a..5254bbcde 100644 --- a/test/common.js +++ b/test/common.js @@ -1265,13 +1265,14 @@ describe('Gun', function(){ describe('API', function(){ var gopt = {wire:{put:function(n,cb){cb()},get:function(k,cb){cb()}}}; if(Gun.window && location.search){ - console.log("LOCALHOST PEER MUST BE ON!"); + /*console.log("LOCALHOST PEER MUST BE ON!"); + var peer = {url: 'http://localhost:8765/gun'}; Gun.on('opt', function(root){ if(root.opt.test_no_peer){ return this.to.next(root) } root.opt.peers = root.opt.peers || {}; - root.opt.peers['http://localhost:8765/gun'] = {url: 'http://localhost:8765/gun'}; + root.opt.peers['http://localhost:8765/gun'] = peer; this.to.next(root); - }); + });*/ } var gun = Gun(); @@ -3755,6 +3756,19 @@ describe('Gun', function(){ }); //}); }); + + it('Set a ref should be found', function(done){ + var gun = Gun(); + var msg = {what: 'hello world'}; + //var ref = user.get('who').get('all').set(msg); + //user.get('who').get('said').set(ref); + var ref = gun.get('who').get('all').set(msg); + gun.get('who').get('said').set(ref); + gun.get('who').get('said').map().once(function(data){ + expect(data.what).to.be.ok(); + done(); + }) + }); return; it('Nested listener should be called', function(done){ diff --git a/test/sea/sea.js b/test/sea/sea.js index 4a6836f7b..52dbbcb20 100644 --- a/test/sea/sea.js +++ b/test/sea/sea.js @@ -317,6 +317,45 @@ describe('SEA', function(){ done(); }); }) + + it('set user ref should be found', function(done){ + var gun = Gun(); + var user = gun.user(); + var msg = {what: 'hello world'}; + user.create('zach', 'password'); + gun.on('auth', function(){ + var ref = user.get('who').get('all').set(msg); + user.get('who').get('said').set(ref); + user.get('who').get('said').map().once(function(data){ + expect(data.what).to.be.ok(); + done(); + }) + }) + }); + + it('set user ref null override', function(done){ + var gun = Gun(); + var user = gun.user(); + var msg = {what: 'hello world'}; + user.create('xavier', 'password'); + gun.on('auth', function(){ + var ref = user.get('who').get('all').set(msg); + var tmp = ref._.dub || ref._.link; + setTimeout(function(){ + user.get('who').put(1); + setTimeout(function(){ + user.get('who').get('all').get(tmp).put({boom: 'ah'}); + setTimeout(function(){ + user.get('who').get('all').map().once(function(data){ + expect(data).to.be.ok(); + expect(data.what).to.not.be.ok(); + done(); + }); + },9); + },9); + },9); + }); + }); }); }) From b9edb1de1529a028f6bbb0bc13ea039e9b8e44b7 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Thu, 31 Jan 2019 04:46:34 -0800 Subject: [PATCH 2/3] party --- examples/party.html | 160 +++++++++++++++++++++++++++----------------- lib/upload.js | 3 +- 2 files changed, 99 insertions(+), 64 deletions(-) diff --git a/examples/party.html b/examples/party.html index 56573277b..e466da702 100644 --- a/examples/party.html +++ b/examples/party.html @@ -38,10 +38,12 @@ ;(() => { function S(){}; window.S = S; + try{localStorage.clear();//sessionStorage.clear(); + }catch(e){} S.gun = Gun(location.host? location.origin+'/gun' : 'http://localhost:8765/gun'); //S.gun = Gun('http://localhost:8765/gun'); //S.gun = Gun(); - S.app = S.gun.get('examples/social/1'); + S.app = S.gun.get('examples/social/2'); S.user = S.gun.user(); S.tell = (what, n) => { var e = $('#tell').find('p'); @@ -530,7 +532,7 @@
  • -
    +
  • @@ -547,23 +549,33 @@ }); window.user = S.user; $('#speak').on('submit', (e) => { - /*var say = normalize($('#speak .draft')); - console.log(1, say.html()); - return;*/ - var say = $('#speak .draft').text(); + var say = $('#speak .draft').text(); //.text(); // NO NO NO NO NO if(!say){ return } - var ref = S.user.get('who').get('all').set({what: say}); - ref.get('by').put(S.user.get('who')); - S.user.get('who').get('said').time(ref); - S.gun.get('@').time(ref); + console.log('save!', say); + var ref = S.user.get('who').get('all').set({what: say, when: Gun.state()}); + //ref.get('by').put(S.user.get('who')); + //S.user.get('who').get('said').time(ref); + S.user.get('who').get('said').set(ref); + //S.gun.get('@').time(ref); $('#speak .draft').text(''); }); - S.gun.get('@').time(async (data, key, time) => { - var ref = S.gun.get(data), tmp; - var said = await ref.then(); + //S.gun.get('@').time(async (data, key, time) => { + S.user.get('who').get('said').map().once(async (data, key, time) => { + //var ref = S.gun.get(data), tmp; + //var said = await ref.then(); + key = key.replace(/[^A-Za-z]/ig,''); + var tmp, said = data, time = said.when; var $li = $($('#'+key)[0] || $('#draft .model .spoke').clone(true,true).attr('id', key)[(tmp = $.as.sort(time, $('#draft ul').children('li').first()))[0]?'insertBefore':'appendTo'](tmp[0] || '#draft ul')); - $li.find('.what').text(said.what); - var by = ref.get('by'); + tmp = said.what; + if(tmp && tmp.ct){ + tmp = JSON.stringify(tmp); + setTimeout(async function(){ + tmp = await SEA.decrypt(said.what, S.user._.sea); + $li.find('.what').text(tmp); + }, 750); + } + $li.find('.what').text(tmp); // NORMALIAZE!!! + var by = S.user.get('who');// ref.get('by'); by.get('face').get('small').on(data => { $li.find('.face').attr('src', data).removeClass('none'); }); @@ -579,7 +591,7 @@ if(face){ $li.find('.face').attr('src', face).removeClass('none'); } - }, 10); + }); $(document).on('click', '#speak .act.face', (eve) => { }); @@ -587,61 +599,80 @@ -
    +
    - + -
    @@ -673,6 +704,7 @@

    Hello world!

    + - + + + \ No newline at end of file diff --git a/lib/upload.js b/lib/upload.js index 6cdadd212..aaacba294 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -25,9 +25,10 @@ if(!e){ return cb && cb({err: "No file!"}) } if(e.err){ return } var file = (((e.event || e).target || e).result || e), img = new Image(); + img.crossOrigin = "Anonymous"; img.src = file; img.onload = function(){ - if(img.width < w && img.height < (h||Infinity)){ + if(img.width < (w = w || 1000) && img.height < (h||Infinity) && "data:" == file.slice(0,5)){ e.base64 = file; return cb(e || file); } From 2aae735960f35aa39f3efd7205d333bee9a33f01 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Thu, 31 Jan 2019 04:57:44 -0800 Subject: [PATCH 3/3] bump --- test/sea/sea.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/sea/sea.js b/test/sea/sea.js index 52dbbcb20..4f7fe2455 100644 --- a/test/sea/sea.js +++ b/test/sea/sea.js @@ -334,6 +334,7 @@ describe('SEA', function(){ }); it('set user ref null override', function(done){ + this.timeout(9000); var gun = Gun(); var user = gun.user(); var msg = {what: 'hello world'};