axe.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ;(function(){
  2. /* UNBUILD */
  3. var root;
  4. if(typeof window !== "undefined"){ root = window }
  5. if(typeof global !== "undefined"){ root = global }
  6. root = root || {};
  7. var console = root.console || {log: function(){}};
  8. function USE(arg, req){
  9. return req? require(arg) : arg.slice? USE[R(arg)] : function(mod, path){
  10. arg(mod = {exports: {}});
  11. USE[R(path)] = mod.exports;
  12. }
  13. function R(p){
  14. return p.split('/').slice(-1).toString().replace('.js','');
  15. }
  16. }
  17. if(typeof module !== "undefined"){ var common = module }
  18. /* UNBUILD */
  19. ;USE(function(module){
  20. if(typeof window !== "undefined"){ module.window = window }
  21. var tmp = module.window || module;
  22. var AXE = tmp.AXE || function(){};
  23. if(AXE.window = module.window){ try{
  24. AXE.window.AXE = AXE;
  25. tmp = document.createEvent('CustomEvent');
  26. tmp.initCustomEvent('extension', false, false, {type: "AXE"});
  27. (window.dispatchEvent || window.fireEvent)(tmp);
  28. window.postMessage({type: "AXE"}, '*');
  29. } catch(e){} }
  30. try{ if(typeof common !== "undefined"){ common.exports = AXE } }catch(e){}
  31. module.exports = AXE;
  32. })(USE, './root');
  33. ;USE(function(module){
  34. var AXE = USE('./root'), Gun = (AXE.window||{}).Gun || USE('./gun', 1);
  35. (Gun.AXE = AXE).GUN = AXE.Gun = Gun;
  36. Gun.on('opt', function(at){
  37. if(!at.axe){
  38. at.axe = {};
  39. var p = at.opt.peers, tmp;
  40. // 1. If any remembered peers or from last cache or extension
  41. // 2. Fallback to use hard coded peers from dApp
  42. // 3. Or any offered peers.
  43. //if(Gun.obj.empty(p)){
  44. // Gun.obj.map(['http://localhost:8765/gun'/*, 'https://guntest.herokuapp.com/gun'*/], function(url){
  45. // p[url] = {url: url, axe: {}};
  46. // });
  47. //}
  48. // Our current hypothesis is that it is most optimal
  49. // to take peers in a common network, and align
  50. // them in a line, where you only have left and right
  51. // peers, so messages propagate left and right in
  52. // a linear manner with reduced overlap, and
  53. // with one common superpeer (with ready failovers)
  54. // in case the p2p linear latency is high.
  55. // Or there could be plenty of other better options.
  56. console.log("axe", at.opt);
  57. if(at.opt.super){
  58. function verify(msg, send, at) {
  59. var peers = Object.keys(p), puts = Object.keys(msg.put), i, j, peer;
  60. var soul = puts[0]; /// TODO: verify all souls in puts. Copy the msg only with subscribed souls?
  61. for (i=0; i < peers.length; ++i) {
  62. peer = p[peers[i]];
  63. //if (peer.url) {console.log('AXE do not reject superpeers'); send(msg, peer); continue;} /// always send to superpeers?
  64. if (!peer.id) {console.log('AXE peer without id: ', peer); continue;}
  65. if (!Gun.subscribe[soul] || !Gun.subscribe[soul][peer.id]) { console.log('AXE SAY reject msg to peer: %s, soul: %s', peer.id, soul); continue; }
  66. send(msg, peer);
  67. }
  68. }
  69. AXE.say = function(msg, send, at) {
  70. if (!msg.put) { send(msg); return; }
  71. console.log('AXE HOOK!! ', msg);
  72. verify(msg, send, at);
  73. };
  74. /// TODO: remove peer from all Gun.subscribe. On `mesh.bye` event?
  75. }
  76. if(at.opt.super){
  77. at.on('in', USE('./lib/super', 1), at);
  78. } else {
  79. //at.on('in', input, at);
  80. }
  81. }
  82. this.to.next(at); // make sure to call the "next" middleware adapter.
  83. });
  84. function input(msg){
  85. var at = this.as, to = this.to;
  86. }
  87. module.exports = AXE;
  88. })(USE, './axe');
  89. }());