diff --git a/lib/load-config.js b/lib/load-config.js new file mode 100644 index 000000000..7b9f73251 --- /dev/null +++ b/lib/load-config.js @@ -0,0 +1,12 @@ +var config; +try { + config = require("../config/config"); + if (config.adminEmail === 'i.did.not.read.my.config@cryptpad.fr') { + console.log("You can configure the administrator email (adminEmail) in your config/config.js file"); + } +} catch (e) { + console.log("You can customize the configuration by copying config/config.example.js to config/config.js"); + config = require("../config/config.example"); +} +module.exports = config; + diff --git a/lib/pins.js b/lib/pins.js new file mode 100644 index 000000000..4e0791d05 --- /dev/null +++ b/lib/pins.js @@ -0,0 +1,43 @@ +/*jshint esversion: 6 */ + +var Pins = module.exports; + +/* + takes contents of a pinFile (UTF8 string) + and the pin file's name + returns an array of of channel ids which are pinned + + throw errors on pin logs with invalid pin data +*/ +Pins.calculateFromLog = function (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: + // FIXME logging + // 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); +}; + +// TODO refactor to include a streaming version for use in rpc.js as well diff --git a/scripts/check-account-deletion.js b/scripts/check-account-deletion.js index 459fad4e9..020e3c254 100644 --- a/scripts/check-account-deletion.js +++ b/scripts/check-account-deletion.js @@ -4,31 +4,8 @@ const nThen = require('nthen'); const Pinned = require('./pinned'); const Nacl = require('tweetnacl'); const Path = require('path'); -const Config = require('./load-config'); - -const hashesFromPinFile = (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: throw new Error(JSON.stringify(l) + ' ' + fileName); - } - }); - return Object.keys(pins); -}; +const Pins = require('../lib/pins'); +const Config = require('../lib/load-config'); var escapeKeyCharacters = function (key) { return key && key.replace && key.replace(/\//g, '-'); @@ -61,7 +38,7 @@ nThen((waitFor) => { let f = Path.join(pinPath, edPublic.slice(0, 2), edPublic + '.ndjson'); Fs.readFile(f, waitFor((err, content) => { if (err) { throw err; } - pinned = hashesFromPinFile(content.toString('utf8'), f); + pinned = Pins.calculateFromLog(content.toString('utf8'), f); })); }).nThen((waitFor) => { Pinned.load(waitFor((d) => { diff --git a/scripts/check-accounts.js b/scripts/check-accounts.js index 604e9d224..4d0067d43 100644 --- a/scripts/check-accounts.js +++ b/scripts/check-accounts.js @@ -1,6 +1,6 @@ /* globals Buffer */ var Https = require('https'); -var Config = require("../config/config.js"); +var Config = require("../lib/load-config"); var Package = require("../package.json"); var body = JSON.stringify({ diff --git a/scripts/delete-inactive.js b/scripts/delete-inactive.js index bf64509ed..9dbfb0707 100644 --- a/scripts/delete-inactive.js +++ b/scripts/delete-inactive.js @@ -3,12 +3,7 @@ const Fs = require("fs"); const nThen = require("nthen"); const Saferphore = require("saferphore"); const PinnedData = require('./pinneddata'); -let config; -try { - config = require('./config/config'); -} catch (e) { - config = require('./config/config.example'); -} +const config = require("../lib/load-config"); if (!config.inactiveTime || typeof(config.inactiveTime) !== "number") { return; } diff --git a/scripts/expire-channels.js b/scripts/expire-channels.js index 38e0c10c1..5c87c7cea 100644 --- a/scripts/expire-channels.js +++ b/scripts/expire-channels.js @@ -3,7 +3,7 @@ var Path = require("path"); var nThen = require("nthen"); -var config = require("./load-config"); +var config = require("../lib/load-config"); var FileStorage = require('../' + config.storage || './storage/file'); var root = Path.resolve('../' + config.taskPath || './tasks'); diff --git a/scripts/load-config.js b/scripts/load-config.js deleted file mode 100644 index d32f9d31e..000000000 --- a/scripts/load-config.js +++ /dev/null @@ -1,7 +0,0 @@ -var config; -try { - config = require("../config/config"); -} catch (e) { - config = require("../config/config.example"); -} -module.exports = config; diff --git a/scripts/pinned.js b/scripts/pinned.js index 2c00310a4..f42b4bf8c 100644 --- a/scripts/pinned.js +++ b/scripts/pinned.js @@ -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; }); @@ -106,6 +75,6 @@ if (!module.parent) { console.log(x + ' ' + JSON.stringify(data[x])); }); }, { - pinPath: require("../config/config").pinPath + pinPath: require("../lib/load-config").pinPath }); } diff --git a/scripts/pinneddata.js b/scripts/pinneddata.js index 5f3d4e1ec..e87aaf971 100644 --- a/scripts/pinneddata.js +++ b/scripts/pinneddata.js @@ -3,36 +3,7 @@ const Fs = require('fs'); const Semaphore = require('saferphore'); const nThen = require('nthen'); const Path = require('path'); - -/* - takes contents of a pinFile (UTF8 string) - and the pin file's name - returns an array of of channel ids which are pinned - - throw errors on pin logs with invalid pin data -*/ -const hashesFromPinFile = (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; }); } - break; - } - case 'PIN': { - l[1].forEach((x) => { pins[x] = 1; }); - break; - } - case 'UNPIN': { - l[1].forEach((x) => { delete pins[x]; }); - break; - } - default: throw new Error(JSON.stringify(l) + ' ' + fileName); - } - }); - return Object.keys(pins); -}; +const Pins = require('../lib/pins'); /* takes an array of pinned file names @@ -152,7 +123,7 @@ module.exports.load = function (config, cb) { Fs.readFile(f, waitFor(returnAfter((err, content) => { if (err) { throw err; } // get the list of channels pinned by this log - const hashes = hashesFromPinFile(content.toString('utf8'), f); + const hashes = Pins.calculateFromLog(content.toString('utf8'), f); if (config.unpinned) { hashes.forEach((x) => { pinned[x] = 1; }); } else { diff --git a/server.js b/server.js index 24d7fecc3..7d64195c3 100644 --- a/server.js +++ b/server.js @@ -11,17 +11,7 @@ var Package = require('./package.json'); var Path = require("path"); var nThen = require("nthen"); -var config; -try { - config = require('./config/config'); -} catch (e) { - console.log("You can customize the configuration by copying config/config.example.js to config/config.js"); - config = require('./config/config.example'); -} - -if (config.adminEmail === 'i.did.not.read.my.config@cryptpad.fr') { - console.log("You can configure the administrator email (adminEmail) in your config/config.js file"); -} +var config = require("./lib/load-config"); var websocketPort = config.websocketPort || config.httpPort; var useSecureWebsockets = config.useSecureWebsockets || false;