2017-12-06 00:48:30 +08:00
|
|
|
/* jshint esversion: 6, node: true */
|
|
|
|
const Fs = require('fs');
|
2019-04-11 16:27:52 +08:00
|
|
|
const Path = require("path");
|
2017-12-06 00:48:30 +08:00
|
|
|
const Semaphore = require('saferphore');
|
2019-04-11 16:27:52 +08:00
|
|
|
const Once = require("../lib/once");
|
2017-12-06 00:48:30 +08:00
|
|
|
const nThen = require('nthen');
|
|
|
|
|
|
|
|
const sema = Semaphore.create(20);
|
|
|
|
|
|
|
|
let dirList;
|
|
|
|
const fileList = [];
|
|
|
|
const pinned = {};
|
|
|
|
|
2019-04-12 23:16:32 +08:00
|
|
|
// FIXME this seems to be duplicated in a few places.
|
|
|
|
// make it a library and put it in ./lib/
|
2019-04-11 16:27:52 +08:00
|
|
|
const checkPinStatus = (pinFile, fileName) => {
|
2017-12-06 00:48:30 +08:00
|
|
|
var pins = {};
|
|
|
|
pinFile.split('\n').filter((x)=>(x)).map((l) => JSON.parse(l)).forEach((l) => {
|
|
|
|
switch (l[0]) {
|
|
|
|
case 'RESET': {
|
|
|
|
pins = {};
|
2018-04-04 18:06:59 +08:00
|
|
|
if (l[1] && l[1].length) { l[1].forEach((x) => { pins[x] = 1; }); }
|
2017-12-06 00:48:30 +08:00
|
|
|
//jshint -W086
|
|
|
|
// fallthrough
|
|
|
|
}
|
|
|
|
case 'PIN': {
|
|
|
|
l[1].forEach((x) => { pins[x] = 1; });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'UNPIN': {
|
|
|
|
l[1].forEach((x) => { delete pins[x]; });
|
|
|
|
break;
|
|
|
|
}
|
2019-04-11 16:41:07 +08:00
|
|
|
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));
|
2017-12-06 00:48:30 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return Object.keys(pins);
|
|
|
|
};
|
|
|
|
|
2018-04-04 18:06:59 +08:00
|
|
|
module.exports.load = function (cb, config) {
|
2019-04-11 16:27:52 +08:00
|
|
|
var pinPath = config.pinPath || './pins';
|
|
|
|
var done = Once(cb);
|
|
|
|
|
2017-12-06 00:48:30 +08:00
|
|
|
nThen((waitFor) => {
|
2019-04-11 16:27:52 +08:00
|
|
|
// recurse over the configured pinPath, or the default
|
|
|
|
Fs.readdir(pinPath, waitFor((err, list) => {
|
2017-12-20 01:21:29 +08:00
|
|
|
if (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
dirList = [];
|
2019-04-11 16:27:52 +08:00
|
|
|
return; // this ends up calling back with an empty object
|
2017-12-20 01:21:29 +08:00
|
|
|
}
|
2019-04-11 16:27:52 +08:00
|
|
|
waitFor.abort();
|
|
|
|
return void done(err);
|
2017-12-20 01:21:29 +08:00
|
|
|
}
|
2017-12-06 00:48:30 +08:00
|
|
|
dirList = list;
|
|
|
|
}));
|
|
|
|
}).nThen((waitFor) => {
|
|
|
|
dirList.forEach((f) => {
|
|
|
|
sema.take((returnAfter) => {
|
2019-04-11 16:27:52 +08:00
|
|
|
// iterate over all the subdirectories in the pin store
|
|
|
|
Fs.readdir(Path.join(pinPath, f), waitFor(returnAfter((err, list2) => {
|
|
|
|
if (err) {
|
|
|
|
waitFor.abort();
|
|
|
|
return void done(err);
|
|
|
|
}
|
2018-04-04 18:06:59 +08:00
|
|
|
list2.forEach((ff) => {
|
|
|
|
if (config && config.exclude && config.exclude.indexOf(ff) > -1) { return; }
|
2019-04-11 16:27:52 +08:00
|
|
|
fileList.push(Path.join(pinPath, f, ff));
|
2018-04-04 18:06:59 +08:00
|
|
|
});
|
2017-12-06 00:48:30 +08:00
|
|
|
})));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).nThen((waitFor) => {
|
|
|
|
fileList.forEach((f) => {
|
|
|
|
sema.take((returnAfter) => {
|
|
|
|
Fs.readFile(f, waitFor(returnAfter((err, content) => {
|
2019-04-11 16:27:52 +08:00
|
|
|
if (err) {
|
|
|
|
waitFor.abort();
|
|
|
|
return void done(err);
|
|
|
|
}
|
|
|
|
const hashes = checkPinStatus(content.toString('utf8'), f);
|
2017-12-06 00:48:30 +08:00
|
|
|
hashes.forEach((x) => {
|
|
|
|
(pinned[x] = pinned[x] || {})[f.replace(/.*\/([^/]*).ndjson$/, (x, y)=>y)] = 1;
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).nThen(() => {
|
2019-04-11 16:27:52 +08:00
|
|
|
done(void 0, pinned);
|
2017-12-06 00:48:30 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!module.parent) {
|
2019-04-11 16:27:52 +08:00
|
|
|
module.exports.load(function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return void console.error(err);
|
|
|
|
}
|
|
|
|
|
2017-12-06 00:48:30 +08:00
|
|
|
Object.keys(data).forEach(function (x) {
|
|
|
|
console.log(x + ' ' + JSON.stringify(data[x]));
|
|
|
|
});
|
2019-04-11 16:27:52 +08:00
|
|
|
}, {
|
|
|
|
pinPath: require("../config/config").pinPath
|
2017-12-06 00:48:30 +08:00
|
|
|
});
|
2018-04-04 18:06:59 +08:00
|
|
|
}
|