diff --git a/gun.js b/gun.js index a8dba928..6767381f 100644 --- a/gun.js +++ b/gun.js @@ -886,6 +886,7 @@ if(!at.gun){ at.gun = gun; } + this.to.next(at); if(get = at.get){ if(tmp = get[_soul]){ tmp = (root.get(tmp)._); @@ -966,7 +967,7 @@ proxy.at.on('in', proxy.at); }); } - if(cat.ack){ + if(cat.ack >= 0){ if(!obj_has(cat, 'put')){ // u !== cat.put instead? //if(u !== cat.put){ return; diff --git a/lib/bye.js b/lib/bye.js new file mode 100644 index 00000000..e8ba0315 --- /dev/null +++ b/lib/bye.js @@ -0,0 +1,38 @@ +if(typeof window !== "undefined"){ + var Gun = window.Gun; +} else { + var Gun = require('../gun'); +} + +Gun.on('opt', function(ctx){ + this.to.next(ctx); + if(ctx.once){ return } + ctx.on('in', function(msg){ + if(!msg.peer || !msg.BYE){ return this.to.next(msg) } + var peer = msg.peer(); + (peer.bye = peer.bye || []).push(msg.BYE); + }) + ctx.on('bye', function(peer){ + if(!peer.bye){ return } + var gun = ctx.gun; + Gun.obj.map(peer.bye, function(data){ + Gun.obj.map(data, function(put, soul){ + gun.get(soul).put(put); + }); + }); + peer.bye = []; + }); +}); + +Gun.chain.bye = function(){ + var gun = this, bye = gun.chain(), root = gun.back(-1), put = bye.put; + bye.put = function(data){ + gun.back(function(at){ + if(!at.get){ return } + var tmp = data; + (data = {})[at.get] = tmp; + }); + root.on('out', {BYE: data}); + } + return bye; +} \ No newline at end of file diff --git a/lib/file.js b/lib/file.js index ed57ebaa..f89bc552 100644 --- a/lib/file.js +++ b/lib/file.js @@ -40,6 +40,7 @@ Gun.on('opt', function(ctx){ //setTimeout(function(){ if(!lex || !(soul = lex[Gun._.soul])){ return } //if(0 >= at.cap){ return } + if(Gun.obj.is(soul)){ return match(at) } var field = lex['.']; data = disk[soul] || u; if(data && field){ @@ -74,4 +75,14 @@ Gun.on('opt', function(ctx){ if(1 < tmp){ flush() } }); } + + function match(at){ + var rgx = at.get['#'], has = at.get['.']; + Gun.obj.map(disk, function(node, soul, put){ + if(!Gun.text.match(soul, rgx)){ return } + if(has){ node = Gun.state.to(node, has) } + (put = {})[soul] = node; + ctx.on('in', {put: put, '@': at['#']}); + }); + } }); \ No newline at end of file diff --git a/lib/open.js b/lib/open.js index ca9029e8..254f110b 100644 --- a/lib/open.js +++ b/lib/open.js @@ -9,7 +9,7 @@ Gun.chain.open = function(cb, opt, at){ opt.doc = opt.doc || {}; opt.ids = opt.ids || {}; return this.on(function(data, key){ - delete (data = Gun.obj.copy(data))._; + delete ((data = Gun.obj.copy(data))||{})._; clearTimeout(opt.to); opt.to = setTimeout(function(){ cb(opt.doc); diff --git a/lib/server.js b/lib/server.js index d27ab989..ca88b96f 100644 --- a/lib/server.js +++ b/lib/server.js @@ -6,5 +6,6 @@ try{require('./ws');}catch(e){require('./wsp/server');} require('./verify'); require('./file'); + require('./bye'); module.exports = Gun; }()); diff --git a/lib/shim.js b/lib/shim.js index 0be7da63..cfc6e0bc 100644 --- a/lib/shim.js +++ b/lib/shim.js @@ -12,4 +12,7 @@ Gun.chain.on = function(a,b,c){ return this.open(b,c); } return _on.call(this, a,b,c); -} \ No newline at end of file +} + +Gun.chain.bye || require('gun/lib/bye'); +Gun.chain.onDisconnect = Gun.chain.bye; \ No newline at end of file diff --git a/lib/uws.js b/lib/uws.js index 03ac4b83..c6fa8619 100644 --- a/lib/uws.js +++ b/lib/uws.js @@ -18,17 +18,20 @@ Gun.on('opt', function mount(ctx){ ws.web = new WebSocket.Server(ws); - ws.web.on('connection', function(peer){ - peer.upgradeReq = peer.upgradeReq || {}; - peer.url = url.parse(peer.upgradeReq.url||'', true); - peer.id = peer.id || Gun.text.random(6); - opt.peers[peer.id] = {wire: peer}; - peer.on('message', function(msg){ + ws.web.on('connection', function(wire){ + wire.upgradeReq = wire.upgradeReq || {}; + wire.url = url.parse(wire.upgradeReq.url||'', true); + wire.id = wire.id || Gun.text.random(6); + var peer = opt.peers[wire.id] = {wire: wire}; + peer.wire = function(){ return peer }; + ctx.on('hi', peer); + wire.on('message', function(msg){ //console.log("MESSAGE", msg); - receive(msg, peer, ctx); // diff: peer is wire. + receive(msg, wire, ctx); // diff: wire is wire. }); - peer.on('close', function(){ - Gun.obj.del(opt.peers, peer.id); + wire.on('close', function(){ + ctx.on('bye', peer); + Gun.obj.del(opt.peers, wire.id); }); }); @@ -73,6 +76,7 @@ Gun.on('opt', function mount(ctx){ } return; } + msg.peer = wire.peer; ctx.on('in', msg); } function open(peer, as){ diff --git a/lib/ws.js b/lib/ws.js index 6879f0ec..0742b66c 100644 --- a/lib/ws.js +++ b/lib/ws.js @@ -16,17 +16,20 @@ Gun.on('opt', function mount(ctx){ ws.web = new WebSocket.Server(ws); - ws.web.on('connection', function(peer){ - peer.upgradeReq = peer.upgradeReq || {}; - peer.url = url.parse(peer.upgradeReq.url||'', true); - peer.id = peer.id || Gun.text.random(6); - opt.peers[peer.id] = {wire: peer}; - peer.on('message', function(msg){ + ws.web.on('connection', function(wire){ + wire.upgradeReq = wire.upgradeReq || {}; + wire.url = url.parse(wire.upgradeReq.url||'', true); + wire.id = wire.id || Gun.text.random(6); + var peer = opt.peers[wire.id] = {wire: wire}; + wire.peer = function(){ return peer }; + ctx.on('hi', peer); + wire.on('message', function(msg){ //console.log("MESSAGE", msg); - receive(msg, peer, ctx); // diff: peer is wire. + receive(msg, wire, ctx); // diff: wire is wire. }); - peer.on('close', function(){ - Gun.obj.del(opt.peers, peer.id); + wire.on('close', function(){ + ctx.on('bye', peer); + Gun.obj.del(opt.peers, wire.id); }); }); @@ -71,6 +74,7 @@ Gun.on('opt', function mount(ctx){ } return; } + msg.peer = wire.peer; ctx.on('in', msg); } function open(peer, as){ diff --git a/package.json b/package.json index 9ccc028a..b99382c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gun", - "version": "0.8.3", + "version": "0.8.4", "description": "Graph engine", "main": "index.js", "browser": "gun.min.js", diff --git a/test/panic/s2s-all.js b/test/panic/s2s-all.js index d7c9597f..fe752de2 100644 --- a/test/panic/s2s-all.js +++ b/test/panic/s2s-all.js @@ -105,7 +105,7 @@ describe("Sync all data from one server to another!", function(){ test.async(); var raw = require('fs').readFileSync(env.i+'alldata'); var json = JSON.parse(raw); - var graph = json.graph; + var graph = json; console.log("Bob's graph on disk:", json); if(!graph.a1 || graph.a1.a !== 1){