standardize pin calculation routine

This commit is contained in:
ansuz
2019-04-17 14:46:46 +02:00
parent d5a9eacef0
commit 5c9cd5ffc6
4 changed files with 49 additions and 89 deletions

View File

@@ -4,6 +4,7 @@ const Path = require("path");
const Semaphore = require('saferphore');
const Once = require("../lib/once");
const nThen = require('nthen');
const Pins = require("../lib/pins");
const sema = Semaphore.create(20);
@@ -11,38 +12,6 @@ let dirList;
const fileList = [];
const pinned = {};
// FIXME this seems to be duplicated in a few places.
// make it a library and put it in ./lib/
const checkPinStatus = (pinFile, fileName) => {
var pins = {};
pinFile.split('\n').filter((x)=>(x)).map((l) => JSON.parse(l)).forEach((l) => {
switch (l[0]) {
case 'RESET': {
pins = {};
if (l[1] && l[1].length) { l[1].forEach((x) => { pins[x] = 1; }); }
//jshint -W086
// fallthrough
}
case 'PIN': {
l[1].forEach((x) => { pins[x] = 1; });
break;
}
case 'UNPIN': {
l[1].forEach((x) => { delete pins[x]; });
break;
}
default:
// TODO write to the error log
/* Log.error('CORRUPTED_PIN_LOG', {
line: JSON.stringify(l),
fileName: fileName,
}); */
console.error(new Error (JSON.stringify(l) + ' ' + fileName));
}
});
return Object.keys(pins);
};
module.exports.load = function (cb, config) {
var pinPath = config.pinPath || './pins';
var done = Once(cb);
@@ -84,7 +53,7 @@ module.exports.load = function (cb, config) {
waitFor.abort();
return void done(err);
}
const hashes = checkPinStatus(content.toString('utf8'), f);
const hashes = Pins.calculateFromLog(content.toString('utf8'), f);
hashes.forEach((x) => {
(pinned[x] = pinned[x] || {})[f.replace(/.*\/([^/]*).ndjson$/, (x, y)=>y)] = 1;
});