Skip to content

Commit

Permalink
Merge pull request #3 from amark/master
Browse files Browse the repository at this point in the history
update to master
  • Loading branch information
sirpy authored Nov 4, 2019
2 parents 87f69ae + d69c315 commit f15281d
Show file tree
Hide file tree
Showing 22 changed files with 321 additions and 90 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ Thanks to:<br/>
<a href="http://github.com/alanmimms">Alan Mimms</a>,
<a href="https://github.com/dfreire">Dário Freire</a>,
<a href="http://github.com/velua">John Williamson</a>,
<a href="http://github.com/finwo">Robin Bron</a>
<a href="http://github.com/finwo">Robin Bron</a>,
<a href="http://github.com/ElieMakhoul">Elie Makhoul</a>
</p>

- Join others in sponsoring code: https://www.patreon.com/gunDB !
Expand Down Expand Up @@ -232,6 +233,13 @@ now --npm amark/gun

Then visit the URL in the output of the 'now --npm' step, in your browser.

### [Unebo](https://unubo.app/)

Fork this GUN repo (Unebo only deploys from your own GitHub repo's).
Add a Node.js app, select your GUN fork, set `npm start` start as the command and deploy.

Then visit the deployed app by following the 'view app' button, in your browser.

### [Docker](https://www.docker.com/)

[![Docker Automated buil](https://img.shields.io/docker/automated/gundb/gun.svg)](https://hub.docker.com/r/gundb/gun/) [![](https://images.microbadger.com/badges/image/gundb/gun.svg)](https://microbadger.com/images/gundb/gun "Get your own image badge on microbadger.com") [![Docker Pulls](https://img.shields.io/docker/pulls/gundb/gun.svg)](https://hub.docker.com/r/gundb/gun/) [![Docker Stars](https://img.shields.io/docker/stars/gundb/gun.svg)](https://hub.docker.com/r/gundb/gun/)
Expand Down
4 changes: 2 additions & 2 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
border: none;
}
</style>
<a href="/todo/index.html"><iframe src="/todo/index.html"></iframe></a>
<a href="/todo/index.html"><iframe src="/todo/index.html" style="height: 100%;"></iframe></a>
<!-- a href="/json/index.html"><iframe src="/json/index.html"></iframe></a -->
<a href="/chat/index.html"><iframe src="/chat/index.html"></iframe></a>
<!-- a href="/chat/index.html"><iframe src="/chat/index.html"></iframe></a --> <!-- removing until DOM bug fixed -->
<!-- script src="../gun.js"></script -->
</body>
</html>
55 changes: 39 additions & 16 deletions gun.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,16 +811,20 @@
// Maybe... in case the in-memory key we have is a local write
// we still need to trigger a pull/merge from peers.
} else {
//var S = +new Date;
node = Gun.obj.copy(node);
//console.log(+new Date - S, 'copy node');
}
node = Gun.graph.node(node);
tmp = (at||empty).ack;
//var S = +new Date;
root.on('in', {
'@': msg['#'],
how: 'mem',
put: node,
$: gun
});
//console.log(+new Date - S, 'root got send');
//if(0 < tmp){ return }
root.on('get', msg);
}
Expand Down Expand Up @@ -1295,19 +1299,18 @@
if(tmp = cat.soul || cat.link || cat.dub){ return cb(tmp, as, cat) }
if(cat.jam){ return cat.jam.push([cb, as]) }
cat.jam = [[cb,as]];
gun.get(function(msg, eve){
gun.get(function go(msg, eve){
if(u === msg.put && (tmp = Object.keys(cat.root.opt.peers).length) && ++acks < tmp){
return;
}
eve.rid(msg);
var at = ((at = msg.$) && at._) || {};
tmp = cat.jam; Gun.obj.del(cat, 'jam');
Gun.obj.map(tmp, function(as, cb){
cb = as[0]; as = as[1];
if(!cb){ return }
var id = at.link || at.soul || rel.is(msg.put) || node_soul(msg.put) || at.dub;
cb(id, as, msg, eve);
});
var at = ((at = msg.$) && at._) || {}, i = 0, as;
tmp = cat.jam; delete cat.jam; // tmp = cat.jam.splice(0, 100);
//if(tmp.length){ process.nextTick(function(){ go(msg, eve) }) }
while(as = tmp[i++]){ //Gun.obj.map(tmp, function(as, cb){
var cb = as[0], id; as = as[1];
cb && cb(id = at.link || at.soul || rel.is(msg.put) || node_soul(msg.put) || at.dub, as, msg, eve);
} //);
}, {out: {get: {'.':true}}});
return gun;
}
Expand Down Expand Up @@ -1701,7 +1704,7 @@
return;
}
if(link && u === link.put && (tmp = rel.is(data))){ data = Gun.node.ify({}, tmp) }
eve.rid(msg);
eve.rid? eve.rid(msg) : eve.off();
opt.ok.call(gun || opt.$, data, msg.get);
}

Expand Down Expand Up @@ -1954,6 +1957,7 @@

;USE(function(module){
var Type = USE('../type');
var puff = (typeof setImmediate !== "undefined")? setImmediate : setTimeout;

function Mesh(root){
var mesh = function(){};
Expand All @@ -1972,19 +1976,26 @@
if('[' === tmp){
try{msg = JSON.parse(raw);}catch(e){opt.log('DAM JSON parse error', e)}
if(!msg){ return }
var i = 0, m;
var S = +new Date; // STATS!
while(m = msg[i++]){
mesh.hear(m, peer);
}
(mesh.hear.long || (mesh.hear.long = [])).push(+new Date - S);
//console.log('hear batch length of', msg.length);
(function go(){
var S = +new Date; // STATS!
var m, c = 100; // hardcoded for now?
while(c-- && (m = msg.shift())){
mesh.hear(m, peer);
}
//console.log(+new Date - S, 'hear batch');
(mesh.hear.long || (mesh.hear.long = [])).push(+new Date - S);
if(!msg.length){ return }
puff(go, 0);
}());
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(msg.DBG_s){ console.log(+new Date - msg.DBG_s, 'to hear', id) }
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) }
Expand All @@ -2000,7 +2011,9 @@
}
return;
}
//var S = +new Date;
root.on('in', msg);
//!msg.nts && console.log(+new Date - S, 'msg', msg['#']);
return;
}
}
Expand All @@ -2014,6 +2027,7 @@
if(this.to){ this.to.next(msg) } // compatible with middleware adapters.
if(!msg){ return false }
var id, hash, tmp, raw;
//var S = +new Date; //msg.DBG_s = msg.DBG_s || +new Date;
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) }
Expand All @@ -2027,12 +2041,15 @@
}
}
}
//console.log(+new Date - S, 'mesh say prep');
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 }
//var S = +new Date;
Type.obj.map(peer || opt.peers, each); // in case peer is a peer list.
//console.log(+new Date - S, 'mesh say loop');
return;
}
if(!peer.wire && mesh.wire){ mesh.wire(peer) }
Expand All @@ -2056,8 +2073,10 @@
peer.batch = peer.tail = null;
if(!tmp){ return }
if(!tmp.length){ return } // if(3 > tmp.length){ return } // TODO: ^
//var S = +new Date;
try{tmp = (1 === tmp.length? tmp[0] : JSON.stringify(tmp));
}catch(e){return opt.log('DAM JSON stringify error', e)}
//console.log(+new Date - S, 'mesh flush', tmp.length);
if(!tmp){ return }
send(tmp, peer);
}
Expand All @@ -2067,12 +2086,14 @@
// for now - find better place later.
function send(raw, peer){ try{
var wire = peer.wire;
//var S = +new Date;
if(peer.say){
peer.say(raw);
} else
if(wire.send){
wire.send(raw);
}
//console.log(+new Date - S, 'wire send', raw.length);
mesh.say.d += raw.length||0; ++mesh.say.c; // STATS!
}catch(e){
(peer.queue = peer.queue || []).push(raw);
Expand Down Expand Up @@ -2251,6 +2272,8 @@
return wire;
}catch(e){}}

setTimeout(function(){ root.on('out', {dam:'hi'}) },1); // it can take a while to open a socket, so maybe no longer lazy load for perf reasons?

var wait = 2 * 1000;
function reconnect(peer){
clearTimeout(peer.defer);
Expand Down
2 changes: 1 addition & 1 deletion gun.min.js

Large diffs are not rendered by default.

24 changes: 11 additions & 13 deletions lib/bye.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');

Gun.on('opt', function(root){
Gun.on('create', function(root){
this.to.next(root);
if(root.once){ return }
root.on('in', function(msg){
//Msg did not have a peer property saved before, so nothing ever went further
if(!msg._ || !msg.BYE){ return this.to.next(msg) }
var peer = msg._.via;
(peer.bye = peer.bye || []).push(msg.BYE);
})
var mesh = root.opt.mesh;
if(!mesh){ return }
mesh.hear['bye'] = function(msg, peer){
(peer.byes = peer.byes || []).push(msg.bye);
}
root.on('bye', function(peer){
this.to.next(peer);
if(!peer.bye){ return }
var gun = root.gun;
Gun.obj.map(peer.bye, function(data){
if(!peer.byes){ return }
var gun = root.$;
Gun.obj.map(peer.byes, function(data){
Gun.obj.map(data, function(put, soul){
gun.get(soul).put(put);
});
});
peer.bye = [];
peer.byes = [];
});
});

Expand All @@ -30,7 +28,7 @@ Gun.chain.bye = function(){
var tmp = data;
(data = {})[at.get] = tmp;
});
root.on('out', {BYE: data});
root.on('out', {bye: data});
return gun;
}
return bye;
Expand Down
4 changes: 2 additions & 2 deletions lib/evict.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
function GC(){
var souls = Object.keys(root.graph||empty);
var toss = Math.ceil(souls.length * 0.01);
//var start = Gun.state(), i = toss;
//var S = +new Date;
Gun.list.map(souls, function(soul){
if(--toss < 0){ return }
root.gun.get(soul).off();
});
//console.log("evicted", i, 'nodes in', ((Gun.state() - start)/1000).toFixed(2), 'sec.');
//console.log(+new Date - S, 'gc');
}
/*
root.on('in', function(msg){
Expand Down
6 changes: 5 additions & 1 deletion lib/multicast.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ Gun.on('create', function(root){

try{ dgram = require("dgram") }catch(e){ return }
var socket = dgram.createSocket({type: "udp4", reuseAddr: true});
socket.bind(udp.port);
socket.bind({port: udp.port, exclusive: true}, function(){
socket.setBroadcast(true);
socket.setMulticastTTL(128);
socket.addMembership(udp.address);
});

socket.on("listening", function(){
try { socket.addMembership(udp.address) }catch(e){ return }
Expand Down
4 changes: 2 additions & 2 deletions lib/promise.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Promise Library v1.0 for GUN DB
/* Promise Library v1.1 for GUN DB
* Turn any part of a gun chain into a promise, that you can then use
* .then().catch() pattern.
* In normal gun doing var item = gun.get('someKey'), gun returns a reference
Expand Down Expand Up @@ -67,7 +67,7 @@ Gun.chain.promPut = async function (item, opt) {
var gun = this;
return (new Promise((res, rej)=>{
gun.put(item, function(ack) {
if(ack.err){rej(ack.err)}
if(ack.err){console.log(ack.err); ack.ok=-1; res({ref:gun, ack:ack})}
res({ref:gun, ack:ack});
}, opt);
}))
Expand Down
Loading

0 comments on commit f15281d

Please sign in to comment.