Browse Source

no more hardcoding. search the directory structure for .k files and load them

ansuz 7 years ago
parent
commit
dc3b822252
2 changed files with 43 additions and 97 deletions
  1. 43 91
      index.js
  2. 0 6
      tests.js

+ 43 - 91
index.js

@@ -1,100 +1,53 @@
 var Fs = require("fs"),
-    R = function (p) {
+    Path = require("path"),
+    read = function (p) {
         var content = Fs.readFileSync(p, 'utf-8');
         if (content.charAt(content.length - 1) !== '\n') {
             throw new Error("file at " + p + " did not end with a newline character");
         }
         return JSON.parse(content);
     },
-    P = module.exports.peers = {
-        AS: {
-            hk: {
-                'hk.hub.icfreedom.net.k': R('./AS/hk/hk.hub.icfreedom.net.k'),
-            },
-            sg: {
-                singapore: {
-                    'sg.hub.icfreedom.net.k': R('./AS/sg/singapore/sg.hub.icfreedom.net.k'),
-                    'weuxel.sing.k': R('./AS/sg/singapore/weuxel.sing.k'),
-                },
-            },
-        },
-        EU: {
-            de: {
-                bavaria: {
-                    'hype.jazzanet.com.k': R('./EU/de/bavaria/hype.jazzanet.com.k'),
-                },
-            },
-            fr: {
-                'nord-pas-de-calais': {
-                    'hub.icfreedom.net.k': R('./EU/fr/nord-pas-de-calais/hub.icfreedom.net.k'),
-                    'play.fallofanempire.com.k': R('./EU/fr/nord-pas-de-calais/play.fallofanempire.com.k'),
-                },
-                strasbourg: {
-                    'magik6k.net.k': R('./EU/fr/strasbourg/magik6k.net.k'),
-                },
-            },
-            gr: {
-                rethymno: {
-                    'kaotisk.rethymno-meshnet.k': R('./EU/gr/rethymno/kaotisk.rethymno-meshnet.k'),
-                },
-            },
-            md: {
-                chisinau: {
-                    'eu-east.hub.icfreedom.net.k': R('./EU/md/chisinau/eu-east.hub.icfreedom.net.k'),
-                },
-            },
-            nl: {
-                amsterdam: {
-                    'mrowr.me.k': R('./EU/nl/amsterdam/mrowr.me.k'),
-                    'weuxel.ams.k': R('./EU/nl/amsterdam/weuxel.ams.k'),
-                },
-            },
-            ru: {
-                moscow: {
-                    'h.bunjlabs.com.k': R('./EU/ru/moscow/h.bunjlabs.com.k'),
-                },
-            },
-            se: {
-                lulea: {
-                    'bliss.willeponken.me.k': R('./EU/se/lulea/bliss.willeponken.me.k'),
-                },
-            },
-            uk: {
-                london: {
-                    'ansuz.science.k': R('./EU/uk/london/ansuz.science.k'),
-                },
-            },
-        },
-        NA: {
-            ca: {
-                quebec: {
-                    'ca.hub.icfreedom.net.k': R('./NA/ca/quebec/ca.hub.icfreedom.net.k'),
-                },
-                beauharnois: {
-                    'derp.fusion.k': R('./NA/ca/beauharnois/derp.fusion.k'),
-                },
-            },
-            us: {
-                california: {
-                    'igel-california.usa.k': R('./NA/us/california/igel-california.usa.k'),
-                },
-                newyork: {
-                    'jacobhenner.usa.k': R('./NA/us/newyork/jacobhenner.usa.k'),
-                    'weuxel.usa.k': R('./NA/us/newyork/weuxel.usa.k'),
-                },
-                northcarolina: {
-                    'igel-northcarolina.usa.k': R('./NA/us/northcarolina/igel-northcarolina.usa.k'),
+    P = module.exports.peers = (function () {
+        var pathFromArray = function (A) {
+            return A.reduce(function (a, b) { return Path.join(a, b); }, '');
+        };
 
-                },
-                oregon: {
-                    'h.us-west.hub.icfreedom.net.k': R('./NA/us/oregon/h.us-west.hub.icfreedom.net.k'),
-                },
-                pennsylvania: {
-                    'nat.usa.k': R('./NA/us/pennsylvania/nat.usa.k'),
-                },
-            },
-        },
-    },
+        var isDir = function (fullPath) {
+            return Fs.lstatSync(fullPath).isDirectory();
+        };
+
+        var getDir = function (A, f) {
+            var p = pathFromArray(A);
+            return Fs.readdirSync(p).filter(function (name) {
+                var fullPath = pathFromArray([p, name]);
+                return f(fullPath, A, name);
+            });
+        };
+
+        var find = function (map, path) {
+            /* safely search for nested values in an object via a path */
+            return (map && path.reduce(function (p, n) {
+                return typeof p[n] !== 'undefined' && p[n];
+            }, map)) || undefined;
+        }
+
+        var peers = {};
+        var walk = function (A) {
+            getDir(A, function (fullPath, A, name) {
+                if (/^\./.test(name)) {
+                    // ignore hidden files
+                } else if (isDir(fullPath)) {
+                    find(peers, A.slice(1))[name] = {};
+                    walk(A.concat(name));
+                } else if (/\.k$/.test(name)) {
+                    find(peers, A.slice(1))[name] = JSON.parse(Fs.readFileSync(fullPath, 'utf-8'));
+                }
+            });
+        };
+
+        walk([__dirname]);
+        return peers;
+    }()),
     map = module.exports.map = function (f) {
         var L = [];
 
@@ -102,7 +55,6 @@ var Fs = require("fs"),
         var isCred = function (k) {
             // creds end in .k
             return /\.k/.test(k);
-            return true;
         };
 
         var walk = function (o, p, f) {
@@ -113,7 +65,6 @@ var Fs = require("fs"),
                 Object.keys(o).forEach(function (k) {
                     var path = p.slice(0).concat(k);
 
-
                     if (isCred(k)) {
                         //console.log(o[k]);
                         L.push(f(o[k], path));
@@ -133,3 +84,4 @@ var Fs = require("fs"),
         });
         return L;
     };
+

+ 0 - 6
tests.js

@@ -57,9 +57,3 @@ if (insufficientFields.length) {
     console.log(insufficientFields);
 }
 
-/*  Credentials must be short enough *as is* that they will not trigger
-    the connectTo-overflow bug.
-*/
-
-// TODO check if this bug still exists in cjdns
-// TODO add bencoding and check length