123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- ;(function(){
-
- var root;
- if(typeof window !== "undefined"){ root = window }
- if(typeof global !== "undefined"){ root = global }
- root = root || {};
- var console = root.console || {log: function(){}};
- function USE(arg, req){
- return req? require(arg) : arg.slice? USE[R(arg)] : function(mod, path){
- arg(mod = {exports: {}});
- USE[R(path)] = mod.exports;
- }
- function R(p){
- return p.split('/').slice(-1).toString().replace('.js','');
- }
- }
- if(typeof module !== "undefined"){ var common = module }
-
- ;USE(function(module){
- if(typeof window !== "undefined"){ module.window = window }
- var tmp = module.window || module;
- var AXE = tmp.AXE || function(){};
- if(AXE.window = module.window){ AXE.window.AXE = AXE }
- try{ if(typeof common !== "undefined"){ common.exports = AXE } }catch(e){}
- module.exports = AXE;
- })(USE, './root');
-
- ;USE(function(module){
- var AXE = USE('./root'), Gun = (AXE.window||{}).Gun || USE('./gun', 1);
- (Gun.AXE = AXE).GUN = AXE.Gun = Gun;
- Gun.on('opt', function(at){
- start(at);
- this.to.next(at);
- });
- function start(at){
- if(at.axe){ return }
- var opt = at.opt, peers = opt.peers;
- if(false === opt.axe){ return }
- if((typeof process !== "undefined") && 'false' === ''+(process.env||{}).AXE){ return }
- var axe = at.axe = {}, tmp;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- opt.mob = opt.mob || Infinity;
- var mesh = opt.mesh = opt.mesh || Gun.Mesh(at);
- console.log("AXE enabled.");
- function verify(dht, msg) {
- var puts = Object.keys(msg.put);
- var soul = puts[0];
- var subs = dht(soul);
- if (!subs) { return; }
- var tmp = [];
- Gun.obj.map(subs.split(','), function(pid) {
- if (pid in peers) {
- tmp.push(pid);
- mesh.say(msg, peers[pid]);
- }
- });
-
- if (opt.super) {
- dht(soul, tmp.join(','));
- }
- }
- function route(get){ var tmp;
- if(!get){ return }
- if('string' != typeof (tmp = get['#'])){ return }
- return tmp;
- }
- var Rad = (Gun.window||{}).Radix || USE('./lib/radix', 1);
- at.opt.dht = Rad();
- at.on('in', function input(msg){
- var to = this.to, peer = (msg._||{}).via;
- var dht = opt.dht;
- var routes = axe.routes || (axe.routes = {});
- var get = msg.get, hash, tmp;
- //if(get && opt.super && peer){
- if(get && opt.super && peer && (tmp = route(get))){
- hash = tmp;
- (routes[hash] || (routes[hash] = {}))[peer.id] = peer;
- (peer.routes || (peer.routes = {}))[hash] = routes[hash];
-
- }
- if((tmp = msg['@']) && (tmp = at.dup.s[tmp]) && (tmp = tmp.it)){
- (tmp = (tmp._||ok)).ack = (tmp.ack || 0) + 1;
- }
- to.next(msg);
- if (opt.rtc && msg.dht) {
- Gun.obj.map(msg.dht, function(pids, soul) {
- dht(soul, pids);
- Gun.obj.map(pids.split(','), function(pid) {
-
- if (!pid || pid in opt.peers || pid === opt.pid || opt.announce[pid]) { return; }
- opt.announce[pid] = true;
- opt.announce(pid);
- });
- });
- }
- });
-
- 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 = 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) {
- if (msg.rtc) {
- if (msg.rtc.to) {
-
- var peer = (peers) ? peers[msg.rtc.to] : null;
- if (peer) { mesh.say(msg, peer); }
- return;
- }
- }
- if(msg.get){ mesh.say(msg, axe.up) }
- if(msg.get && (tmp = route(msg.get))){
- var hash = tmp;
- var routes = axe.routes || (axe.routes = {});
- var peers = routes[hash];
- function chat(peers, old){
- if(!peers){ return chat(opt.peers) }
- var ids = Object.keys(peers);
- var meta = (msg._||yes);
- clearTimeout(meta.lack);
- var id, peer, c = 1;
- while((id = ids[meta.turn || 0]) && c--){
- peer = peers[id];
- meta.turn = (meta.turn || 0) + 1;
- if((old && old[id]) || false === mesh.say(msg, peer)){ ++c }
- }
-
- if(0 < c){
- if(peers === opt.peers){ return }
- return meta.turn = 0, chat(opt.peers, peers)
- }
- var hash = msg['##'], ack = meta.ack;
- meta.lack = setTimeout(function(){
- if(ack && hash && hash === msg['##']){ return }
- if(meta.turn >= (axe.turns || 3)){ return }
-
- chat(peers, old);
- }, 25);
- }
- return chat(peers);
- }
-
- if(msg.put){
- var routes = axe.routes || (axe.routes = {});
- var peers = {};
- Gun.obj.map(msg.put, function(node, soul){
- var hash = soul;
- var to = routes[hash];
- if(!to){ return }
- Gun.obj.to(to, peers);
- });
- mesh.say(msg, peers);
- return;
- }
- mesh.say(msg, opt.peers); return;
- if (!msg.put) { mesh.say(msg); return; }
-
- verify(opt.dht, msg);
- };
- } else {
- mesh.route = function(msg) {
- if (msg.rtc) {
- }
- if (!msg.put) { mesh.say(msg); return; }
- verify(opt.dht, msg);
-
- Gun.obj.map(peers, function(peer) {
- if (peer.url) {
- mesh.say(msg, peer);
- }
- });
- };
-
- }
- axe.up = {};
- at.on('hi', function(peer){
- this.to.next(peer);
- if(!peer.url){ return }
- axe.up[peer.id] = peer;
- });
- at.on('bye', function(peer){ this.to.next(peer);
- if(peer.url){ delete axe.up[peer.id] }
- Gun.obj.map(peer.routes, function(route, hash){
- delete route[peer.id];
- if(Gun.obj.empty(route)){
- delete axe.routes[hash];
- }
- });
- });
-
- at.on('hi', function(peer){
- this.to.next(peer);
- if(peer.url){ return }
- var count = Object.keys(opt.peers).length;
- if(opt.mob >= count){ return }
- mesh.say({dam: 'mob', mob: count, peers: Object.keys(axe.up)}, peer);
-
- });
- at.on('bye', function(peer){
- this.to.next(peer);
- });
- }
- function joindht(dht, soul, pids) {
- if (!pids || !soul || !dht) { return; }
- var subs = dht(soul);
- var tmp = subs ? subs.split(',') : [];
- Gun.obj.map(pids.split(','), function(pid) {
- if (pid && tmp.indexOf(pid) === -1) { tmp.push(pid); }
- });
- tmp = tmp.join(',');
- dht(soul, tmp);
- return tmp;
- }
- var empty = {}, yes = true, u;
- module.exports = AXE;
- })(USE, './axe');
- }());
|