5
0
mirror of https://github.com/cwinfo/hyperboria-peers.git synced 2024-11-09 16:40:27 +00:00

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

This commit is contained in:
ansuz 2016-06-19 03:44:08 +02:00
parent 682ed17833
commit dc3b822252
2 changed files with 43 additions and 97 deletions

134
index.js
View File

@ -1,100 +1,53 @@
var Fs = require("fs"), var Fs = require("fs"),
R = function (p) { Path = require("path"),
read = function (p) {
var content = Fs.readFileSync(p, 'utf-8'); var content = Fs.readFileSync(p, 'utf-8');
if (content.charAt(content.length - 1) !== '\n') { if (content.charAt(content.length - 1) !== '\n') {
throw new Error("file at " + p + " did not end with a newline character"); throw new Error("file at " + p + " did not end with a newline character");
} }
return JSON.parse(content); return JSON.parse(content);
}, },
P = module.exports.peers = { P = module.exports.peers = (function () {
AS: { var pathFromArray = function (A) {
hk: { return A.reduce(function (a, b) { return Path.join(a, b); }, '');
'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'),
}, var isDir = function (fullPath) {
oregon: { return Fs.lstatSync(fullPath).isDirectory();
'h.us-west.hub.icfreedom.net.k': R('./NA/us/oregon/h.us-west.hub.icfreedom.net.k'), };
},
pennsylvania: { var getDir = function (A, f) {
'nat.usa.k': R('./NA/us/pennsylvania/nat.usa.k'), 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) { map = module.exports.map = function (f) {
var L = []; var L = [];
@ -102,7 +55,6 @@ var Fs = require("fs"),
var isCred = function (k) { var isCred = function (k) {
// creds end in .k // creds end in .k
return /\.k/.test(k); return /\.k/.test(k);
return true;
}; };
var walk = function (o, p, f) { var walk = function (o, p, f) {
@ -113,7 +65,6 @@ var Fs = require("fs"),
Object.keys(o).forEach(function (k) { Object.keys(o).forEach(function (k) {
var path = p.slice(0).concat(k); var path = p.slice(0).concat(k);
if (isCred(k)) { if (isCred(k)) {
//console.log(o[k]); //console.log(o[k]);
L.push(f(o[k], path)); L.push(f(o[k], path));
@ -133,3 +84,4 @@ var Fs = require("fs"),
}); });
return L; return L;
}; };

View File

@ -57,9 +57,3 @@ if (insufficientFields.length) {
console.log(insufficientFields); 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