mirror of
https://github.com/amark/gun.git
synced 2025-03-30 15:08:33 +00:00
GUN: -peer, once fix, +multicast; AXE +peer +email
This commit is contained in:
parent
0c718c8658
commit
ab36283b2e
2
as.js
2
as.js
@ -226,6 +226,6 @@
|
||||
gun.user().auth(a,b, cb,o);
|
||||
}
|
||||
gun.on('auth', function(ack){
|
||||
console.log("Your namespace is", ack.soul);
|
||||
console.log("Your namespace is publicly available at", ack.soul);
|
||||
})
|
||||
});
|
27
axe.js
27
axe.js
@ -42,7 +42,7 @@
|
||||
if(at.axe){ return }
|
||||
var opt = at.opt, peers = opt.peers;
|
||||
if(false === opt.axe){ return }
|
||||
if(false === process.env.NO_AXE){ return }
|
||||
if((typeof process !== "undefined") && 'false' === ''+(process.env||{}).AXE){ return }
|
||||
var axe = at.axe = {}, tmp;
|
||||
// 1. If any remembered peers or from last cache or extension
|
||||
// 2. Fallback to use hard coded peers from dApp
|
||||
@ -132,6 +132,31 @@
|
||||
}
|
||||
});
|
||||
|
||||
//try{console.log(req.connection.remoteAddress)}catch(e){};
|
||||
mesh.hear['opt'] = function(msg, peer){
|
||||
if(msg.ok){ return opt.log(msg) }
|
||||
var tmp = msg.opt;
|
||||
if(!tmp){ return }
|
||||
tmp = tmp.peers;
|
||||
if(!tmp || !Gun.text.is(tmp)){ return }
|
||||
if(axe.up[tmp] || 6 <= Object.keys(axe.up).length){ return }
|
||||
var o = {peers: tmp};
|
||||
at.$.opt(o);
|
||||
o = peers[tmp];
|
||||
if(!o){ return }
|
||||
o.retry = 9;
|
||||
mesh.wire(o);
|
||||
if(peer){ mesh.say({dam: 'opt', ok: 1, '@': msg['#']}, peer) }
|
||||
}
|
||||
setInterval(function(tmp){
|
||||
if(!(tmp = at.stats && at.stats.stay)){ return }
|
||||
(tmp.axe = tmp.axe || {}).up = Object.keys(axe.up||{});
|
||||
},1000 * 60)
|
||||
setTimeout(function(tmp){
|
||||
if(!(tmp = at.stats && at.stats.stay)){ return }
|
||||
Gun.obj.map((tmp.axe||{}).up, function(url){ mesh.hear.opt({opt: {peers: url}}) })
|
||||
},1000);
|
||||
|
||||
if(at.opt.super){
|
||||
var rotate = 0;
|
||||
mesh.way = function(msg) {
|
||||
|
30
gun.js
30
gun.js
@ -115,8 +115,9 @@
|
||||
} t.r = t.r || [];
|
||||
t.r.push(k);
|
||||
};
|
||||
var keys = Object.keys;
|
||||
Type.obj.map = function(l, c, _){
|
||||
var keys = Object.keys, map;
|
||||
Object.keys = Object.keys || function(o){ return map(o, function(v,k,t){t(k)}) }
|
||||
Type.obj.map = map = function(l, c, _){
|
||||
var u, i = 0, x, r, ll, lle, f = fn_is(c);
|
||||
t.r = null;
|
||||
if(keys && obj_is(l)){
|
||||
@ -839,7 +840,10 @@
|
||||
at.opt.peers = obj_to(tmp, at.opt.peers);
|
||||
}
|
||||
at.opt.peers = at.opt.peers || {};
|
||||
obj_to(opt, at.opt); // copies options on to `at.opt` only if not already taken.
|
||||
obj_map(opt, function each(v,k){
|
||||
if(!obj_has(this, k) || text.is(v) || obj.empty(v)){ this[k] = v ; return }
|
||||
obj_map(v, each, this[k]);
|
||||
}, at.opt);
|
||||
Gun.on('opt', at);
|
||||
at.opt.uuid = at.opt.uuid || function(){ return state_lex() + text_rand(12) }
|
||||
return gun;
|
||||
@ -1288,7 +1292,7 @@
|
||||
var cat = gun._, acks = 0, tmp;
|
||||
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){
|
||||
if(u === msg.put && (tmp = Object.keys(cat.root.opt.peers).length) && ++acks < tmp){
|
||||
return;
|
||||
}
|
||||
ev.rid(msg);
|
||||
@ -1678,7 +1682,7 @@
|
||||
}
|
||||
if((tmp = eve.wait) && (tmp = tmp[at.id])){ clearTimeout(tmp) }
|
||||
if((!to && (u === data || at.soul || at.link || (link && !(0 < link.ack))))
|
||||
|| (u === data && (tmp = (obj_map(at.root.opt.peers, function(v,k,t){t(k)})||[]).length) && (!to && (link||at).ack <= tmp))){
|
||||
|| (u === data && (tmp = Object.keys(at.root.opt.peers).length) && (!to && (link||at).ack < tmp))){
|
||||
tmp = (eve.wait = {})[at.id] = setTimeout(function(){
|
||||
val.call({as:opt}, msg, eve, tmp || 1);
|
||||
}, opt.wait || 99);
|
||||
@ -2101,7 +2105,6 @@
|
||||
});
|
||||
}
|
||||
mesh.bye = function(peer){
|
||||
Type.obj.del(opt.peers, peer.id); // assume if peer.url then reconnect
|
||||
root.on('bye', peer);
|
||||
var tmp = +(new Date); tmp = (tmp - (peer.met||tmp));
|
||||
mesh.bye.time = ((mesh.bye.time || tmp) + tmp) / 2;
|
||||
@ -2128,6 +2131,14 @@
|
||||
root.on('out', mesh.say);
|
||||
});
|
||||
|
||||
root.on('bye', function(peer, tmp){
|
||||
peer = opt.peers[peer.id || peer] || peer;
|
||||
this.to.next(peer);
|
||||
peer.bye? peer.bye() : (tmp = peer.wire) && tmp.close && tmp.close();
|
||||
Type.obj.del(opt.peers, peer.id);
|
||||
peer.wire = null;
|
||||
});
|
||||
|
||||
var gets = {};
|
||||
root.on('bye', function(peer, tmp){ this.to.next(peer);
|
||||
if(!(tmp = peer.url)){ return } gets[tmp] = true;
|
||||
@ -2177,7 +2188,6 @@
|
||||
}());
|
||||
|
||||
var empty = {}, ok = true, u;
|
||||
Object.keys = Object.keys || function(o){ return map(o, function(v,k,t){t(k)}) }
|
||||
|
||||
try{ module.exports = Mesh }catch(e){}
|
||||
|
||||
@ -2215,11 +2225,7 @@
|
||||
reconnect(peer);
|
||||
};
|
||||
wire.onerror = function(error){
|
||||
reconnect(peer); // placement?
|
||||
if(!error){ return }
|
||||
if(error.code === 'ECONNREFUSED'){
|
||||
//reconnect(peer, as);
|
||||
}
|
||||
reconnect(peer);
|
||||
};
|
||||
wire.onopen = function(){
|
||||
opt.mesh.hi(peer);
|
||||
|
12
lib/email.js
Normal file
12
lib/email.js
Normal file
@ -0,0 +1,12 @@
|
||||
;(function(){
|
||||
var email, fail = {send: function(opt, cb){ cb && cb("You do not have email installed.") } };
|
||||
if(!process.env.EMAIL){ return module.exports = fail }
|
||||
try{ email = require('emailjs') }catch(e){};
|
||||
if(!email){ return module.exports = fail }
|
||||
return module.exports = email.server.connect({
|
||||
user: process.env.EMAIL,
|
||||
password: process.env.EMAIL_KEY,
|
||||
host: process.env.EMAIL_HOST || "smtp.gmail.com",
|
||||
ssl: process.env.EMAIL_SSL || true
|
||||
});
|
||||
}());
|
@ -4,22 +4,26 @@ Gun.on('create', function(root){
|
||||
this.to.next(root);
|
||||
var opt = root.opt;
|
||||
if(false === opt.multicast){ return }
|
||||
if(true !== opt.multicast){ return } // disable multicast by default for now.
|
||||
if((typeof process !== "undefined") && 'false' === ''+(process.env||{}).MULTICAST){ return }
|
||||
//if(true !== opt.multicast){ return } // disable multicast by default for now.
|
||||
|
||||
var udp = opt.multicast = opt.multicast || {};
|
||||
udp.address = udp.address || '233.255.255.255';
|
||||
udp.pack = udp.pack || 50000; // UDP messages limited to 65KB.
|
||||
udp.port = udp.port || 23456;
|
||||
udp.port = udp.port || 8765;
|
||||
|
||||
var noop = function(){}, port;
|
||||
var noop = function(){}, u;
|
||||
var pid = '2'+Math.random().toString().slice(-8);
|
||||
var mesh = opt.mesh = opt.mesh || Gun.Mesh(root);
|
||||
var dgram;
|
||||
|
||||
var dgram = require("dgram");
|
||||
try{ dgram = require("dgram") }catch(e){ return }
|
||||
var socket = dgram.createSocket({type: "udp4", reuseAddr: true});
|
||||
socket.bind(udp.port);
|
||||
|
||||
socket.on("listening", function() {
|
||||
socket.on("listening", function(){
|
||||
socket.addMembership(udp.address);
|
||||
udp.peer = {url: udp.address + ':' + udp.port, wire: socket};
|
||||
udp.peer = {id: udp.address + ':' + udp.port, wire: socket};
|
||||
|
||||
udp.peer.say = function(raw){
|
||||
var buf = Buffer.from(raw, 'utf8');
|
||||
@ -28,9 +32,8 @@ Gun.on('create', function(root){
|
||||
}
|
||||
socket.send(buf, 0, buf.length, udp.port, udp.address, noop);
|
||||
}
|
||||
opt.mesh.hi(udp.peer);
|
||||
|
||||
console.log('multicasting on', udp.peer.url);
|
||||
console.log('Multicast on', udp.peer.id);
|
||||
return; // below code only needed for when WebSocket connections desired!
|
||||
setInterval(function broadcast(){
|
||||
port = port || (opt.web && opt.web.address()||{}).port;
|
||||
@ -42,6 +45,7 @@ Gun.on('create', function(root){
|
||||
socket.on("message", function(raw, info) { try {
|
||||
if(!raw){ return }
|
||||
raw = raw.toString('utf8');
|
||||
if('2'===raw[0]){ return check(raw, info) }
|
||||
opt.mesh.hear(raw, udp.peer);
|
||||
|
||||
return; // below code only needed for when WebSocket connections desired!
|
||||
@ -53,12 +57,32 @@ Gun.on('create', function(root){
|
||||
var url = 'http://' + info.address + ':' + (port || (opt.web && opt.web.address()||{}).port) + '/gun';
|
||||
if(root.opt.peers[url]){ return }
|
||||
|
||||
console.log('discovered', url, message, info);
|
||||
//console.log('discovered', url, message, info);
|
||||
root.$.opt(url);
|
||||
|
||||
} catch(e){
|
||||
console.log('multicast error', e, raw);
|
||||
//console.log('multicast error', e, raw);
|
||||
return;
|
||||
} });
|
||||
|
||||
function say(msg){
|
||||
this.to.next(msg);
|
||||
if(!udp.peer){ return }
|
||||
mesh.say(msg, udp.peer);
|
||||
}
|
||||
|
||||
function check(id, info){ var tmp;
|
||||
if(!udp.peer){ return }
|
||||
if(!id){
|
||||
id = check.id = check.id || Buffer.from(pid, 'utf8');
|
||||
socket.send(id, 0, id.length, udp.port, udp.address, noop);
|
||||
return;
|
||||
}
|
||||
if((tmp = root.stats) && (tmp = tmp.gap) && info){ (tmp.near || (tmp.near = {}))[info.address] = info.port || 1 } // STATS!
|
||||
if(check.on || id === pid){ return }
|
||||
root.on('out', check.on = say);
|
||||
}
|
||||
|
||||
setInterval(check, 1000 * 1);
|
||||
|
||||
});
|
||||
|
@ -9,8 +9,8 @@ function CDN(dir){
|
||||
if(serve(req, res)){ return } // filters GUN requests!
|
||||
fs.createReadStream(path.join(dir, req.url)).on('error',function(tmp){ // static files!
|
||||
try{ tmp = fs.readFileSync(path.join(dir, 'index.html')) }catch(e){}
|
||||
res.writeHead(200, {'Content-Type': 'text/html'});
|
||||
res.end(tmp+''); // or default to index
|
||||
try{ res.writeHead(200, {'Content-Type': 'text/html'});
|
||||
res.end(tmp+''); }catch(e){} // or default to index
|
||||
}).pipe(res); // stream
|
||||
}
|
||||
}
|
||||
|
@ -19,10 +19,13 @@ Gun.on('opt', function(root){
|
||||
os.loadavg = os.loadavg || noop;
|
||||
os.cpus = os.cpus || noop;
|
||||
setTimeout(function(){
|
||||
root.opt.file += (process.argv[2]||'');
|
||||
root.stats = Gun.obj.ify((fs.existsSync(__dirname+'/../stats.'+root.opt.file) && fs.readFileSync(__dirname+'/../stats.'+root.opt.file).toString())) || {};
|
||||
root.stats.up = root.stats.up || {};
|
||||
root.stats.up.start = root.stats.up.start || +(new Date);
|
||||
root.stats.up.count = (root.stats.up.count || 0) + 1;
|
||||
root.stats.stay = root.stats.stay || {};
|
||||
root.stats.gap = {};
|
||||
},1);
|
||||
setInterval(function(){
|
||||
if(!root.stats){ root.stats = {} }
|
||||
@ -50,6 +53,7 @@ Gun.on('opt', function(root){
|
||||
}
|
||||
|
||||
fs.writeFile(__dirname+'/../stats.'+root.opt.file, JSON.stringify(stats, null, 2), function(err){});
|
||||
stats.gap = {};
|
||||
}, 1000 * 15);
|
||||
Object.keys = Object.keys || function(o){ return Gun.obj.map(o, function(v,k,t){t(k)}) }
|
||||
});
|
47
package-lock.json
generated
47
package-lock.json
generated
@ -1,9 +1,15 @@
|
||||
{
|
||||
"name": "gun",
|
||||
"version": "0.2019.627",
|
||||
"version": "0.2019.711",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"addressparser": {
|
||||
"version": "0.3.2",
|
||||
"resolved": "https://registry.npmjs.org/addressparser/-/addressparser-0.3.2.tgz",
|
||||
"integrity": "sha1-WYc/Nej89sc2HBAjkmHXbhU0i7I=",
|
||||
"optional": true
|
||||
},
|
||||
"after": {
|
||||
"version": "0.8.2",
|
||||
"resolved": "https://registry.npmjs.org/after/-/after-0.8.2.tgz",
|
||||
@ -165,6 +171,33 @@
|
||||
"integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==",
|
||||
"dev": true
|
||||
},
|
||||
"emailjs": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/emailjs/-/emailjs-2.2.0.tgz",
|
||||
"integrity": "sha1-ulsj5KSwpFEPZS6HOxVOlAe2ygM=",
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"addressparser": "^0.3.2",
|
||||
"emailjs-mime-codec": "^2.0.7"
|
||||
}
|
||||
},
|
||||
"emailjs-base64": {
|
||||
"version": "1.1.4",
|
||||
"resolved": "https://registry.npmjs.org/emailjs-base64/-/emailjs-base64-1.1.4.tgz",
|
||||
"integrity": "sha512-4h0xp1jgVTnIQBHxSJWXWanNnmuc5o+k4aHEpcLXSToN8asjB5qbXAexs7+PEsUKcEyBteNYsSvXUndYT2CGGA==",
|
||||
"optional": true
|
||||
},
|
||||
"emailjs-mime-codec": {
|
||||
"version": "2.0.9",
|
||||
"resolved": "https://registry.npmjs.org/emailjs-mime-codec/-/emailjs-mime-codec-2.0.9.tgz",
|
||||
"integrity": "sha512-7qJo4pFGcKlWh/kCeNjmcgj34YoJWY0ekZXEHYtluWg4MVBnXqGM4CRMtZQkfYwitOhUgaKN5EQktJddi/YIDQ==",
|
||||
"optional": true,
|
||||
"requires": {
|
||||
"emailjs-base64": "^1.1.4",
|
||||
"ramda": "^0.26.1",
|
||||
"text-encoding": "^0.7.0"
|
||||
}
|
||||
},
|
||||
"engine.io": {
|
||||
"version": "1.8.5",
|
||||
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-1.8.5.tgz",
|
||||
@ -644,6 +677,12 @@
|
||||
"integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=",
|
||||
"dev": true
|
||||
},
|
||||
"ramda": {
|
||||
"version": "0.26.1",
|
||||
"resolved": "https://registry.npmjs.org/ramda/-/ramda-0.26.1.tgz",
|
||||
"integrity": "sha512-hLWjpy7EnsDBb0p+Z3B7rPi3GDeRG5ZtiI33kJhTt+ORCd38AbAIjB/9zRIUoeTbE/AVX5ZkU7m6bznsvrf8eQ==",
|
||||
"optional": true
|
||||
},
|
||||
"sax": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.1.tgz",
|
||||
@ -869,9 +908,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"ws": {
|
||||
"version": "7.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-7.0.0.tgz",
|
||||
"integrity": "sha512-cknCal4k0EAOrh1SHHPPWWh4qm93g1IuGGGwBjWkXmCG7LsDtL8w9w+YVfaF+KSVwiHQKDIMsSLBVftKf9d1pg==",
|
||||
"version": "7.1.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-7.1.0.tgz",
|
||||
"integrity": "sha512-Swie2C4fs7CkwlHu1glMePLYJJsWjzhl1vm3ZaLplD0h7OMkZyZ6kLTB/OagiU923bZrPFXuDTeEqaEN4NWG4g==",
|
||||
"requires": {
|
||||
"async-limiter": "^1.0.0"
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "gun",
|
||||
"version": "0.2019.627",
|
||||
"version": "0.2019.711",
|
||||
"description": "A realtime, decentralized, offline-first, graph data synchronization engine.",
|
||||
"main": "index.js",
|
||||
"browser": "gun.js",
|
||||
"scripts": {
|
||||
"start": "node examples/http.js 8765",
|
||||
"start": "node examples/http.js",
|
||||
"https": "HTTPS_KEY=test/https/server.key HTTPS_CERT=test/https/server.crt npm start",
|
||||
"prepublishOnly": "npm run unbuild",
|
||||
"test": "mocha",
|
||||
@ -51,11 +51,12 @@
|
||||
"node": ">=0.8.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"ws": "~>7.0.0"
|
||||
"ws": "~>7.1.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"text-encoding": "^0.7.0",
|
||||
"node-webcrypto-ossl": "^1.0.47"
|
||||
"node-webcrypto-ossl": "^1.0.47",
|
||||
"emailjs": "^2.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"aws-sdk": ">=2.153.0",
|
||||
|
Loading…
x
Reference in New Issue
Block a user