diff --git a/axe.js b/axe.js index 40b3121cd..a950e363f 100644 --- a/axe.js +++ b/axe.js @@ -47,11 +47,11 @@ // 1. If any remembered peers or from last cache or extension // 2. Fallback to use hard coded peers from dApp // 3. Or any offered peers. - if(Gun.obj.empty(p)){ - Gun.obj.map(['http://localhost:8765/gun'/*, 'https://guntest.herokuapp.com/gun'*/], function(url){ - p[url] = {url: url, axe: {}}; - }); - } + //if(Gun.obj.empty(p)){ + // Gun.obj.map(['http://localhost:8765/gun'/*, 'https://guntest.herokuapp.com/gun'*/], function(url){ + // p[url] = {url: url, axe: {}}; + // }); + //} // Our current hypothesis is that it is most optimal // to take peers in a common network, and align // them in a line, where you only have left and right @@ -61,6 +61,25 @@ // in case the p2p linear latency is high. // Or there could be plenty of other better options. console.log("axe", at.opt); + if(at.opt.super){ + function verify(msg, send, at) { + var peers = Object.keys(p), puts = Object.keys(msg.put), i, j, peer; + var soul = puts[0]; /// TODO: verify all souls in puts. Copy the msg only with subscribed souls? + for (i=0; i < peers.length; ++i) { + peer = p[peers[i]]; + //if (peer.url) {console.log('AXE do not reject superpeers'); send(msg, peer); continue;} /// always send to superpeers? + if (!peer.id) {console.log('AXE peer without id: ', peer); continue;} + if (!Gun.subscribe[soul] || !Gun.subscribe[soul][peer.id]) { console.log('AXE SAY reject msg to peer: %s, soul: %s', peer.id, soul); continue; } + send(msg, peer); + } + } + AXE.say = function(msg, send, at) { + if (!msg.put) { send(msg); return; } + console.log('AXE HOOK!! ', msg); + verify(msg, send, at); + }; + /// TODO: remove peer from all Gun.subscribe. On `mesh.bye` event? + } if(at.opt.super){ at.on('in', USE('./lib/super', 1), at); } else { @@ -77,4 +96,4 @@ module.exports = AXE; })(USE, './axe'); -}()); \ No newline at end of file +}()); diff --git a/examples/axe.html b/examples/axe.html new file mode 100644 index 000000000..40dade3bc --- /dev/null +++ b/examples/axe.html @@ -0,0 +1,40 @@ +<!DOCTYPE HTML> +<html> + +<head> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0" /> + <title>Testing AXE</title> +</head> + +<body> + <script src="../gun.js"></script> + <!-- <script src="../axe.js"></script> --> + <!-- <script src="../sea.js"></script> --> + <script> + var pid = location.hash.slice(1); + + var opt = ({ + peers: [`${location.origin}/gun`] + }); + + if (pid) { opt.pid = pid; } + + Gun.on('opt', function(ctx) { + this.to.next(ctx); + ctx.on('hi', function(opt) { + console.log('HI!! PEER', new Date(), opt.pid); + }); + if (pid) { + ctx.on('out', function(msg) { + msg.pid = pid; + this.to.next(msg); + }); + } + }); + + var gun = Gun(opt); + //var user = gun.user(); + </script> +</body> +</html> diff --git a/examples/express.js b/examples/express.js index 81e689c3a..a516e18a8 100644 --- a/examples/express.js +++ b/examples/express.js @@ -2,12 +2,16 @@ console.log("If module not found, install express globally `npm i express -g`!") var port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765; var express = require('express'); var Gun = require('..'); +require('../axe'); var app = express(); app.use(Gun.serve); app.use(express.static(__dirname)); var server = app.listen(port); -Gun({ file: 'data.json', web: server }); +var gun = Gun({ file: 'data', web: server }); + +global.Gun = Gun; /// make global to `node --inspect` - debug only +global.gun = gun; /// make global to `node --inspect` - debug only console.log('Server started on port ' + port + ' with /gun'); diff --git a/gun.js b/gun.js index d996771c2..7ac767db4 100644 --- a/gun.js +++ b/gun.js @@ -1941,6 +1941,7 @@ return; } // add hook for AXE? + if (Gun.AXE && opt && opt.super) { Gun.AXE.say(msg, mesh.say, this); return; } mesh.say(msg); } @@ -2210,4 +2211,4 @@ var noop = function(){}; })(USE, './adapters/websocket'); -}()); \ No newline at end of file +}()); diff --git a/lib/super.js b/lib/super.js index eb2f6368b..3f94ff3dc 100644 --- a/lib/super.js +++ b/lib/super.js @@ -12,10 +12,18 @@ } else { } + subscribe(soul, peer, msg); } to.next(msg); } + /// Store the subscribes + Gun.subscribe = {}; /// TODO: use Rad instead of plain object? + function subscribe(soul, peer, msg) { + if (!peer.id) { console.log('super jump peer without id: ', peer, msg); return; } /// TODO: this occurs in first subscription. Use peer reference or peer.wire.id? + Gun.subscribe[soul] = Gun.subscribe[soul] || {}; + Gun.subscribe[soul][peer.id] = 1; + } var empty = {}, u; if(Gun.window){ return } try{module.exports = input}catch(e){} -}()); \ No newline at end of file +}()); diff --git a/src/adapters/mesh.js b/src/adapters/mesh.js index 6c99a6e86..a94826f02 100644 --- a/src/adapters/mesh.js +++ b/src/adapters/mesh.js @@ -20,6 +20,7 @@ function Mesh(ctx){ return; } // add hook for AXE? + if (Gun.AXE && opt && opt.super) { Gun.AXE.say(msg, mesh.say, this); return; } mesh.say(msg); } @@ -228,5 +229,3 @@ Mesh.hash = function(s){ // via SO Object.keys = Object.keys || function(o){ return map(o, function(v,k,t){t(k)}) } try{ module.exports = Mesh }catch(e){} - - \ No newline at end of file