From efaa4b795cde2a0cd0abf308ea5f9469420ef7ee Mon Sep 17 00:00:00 2001 From: Florent Chehab Date: Mon, 11 May 2020 14:18:59 +0200 Subject: [PATCH] refacto(backend): regrouped config related handling --- scripts/WhiteboardServerSideInfo.js | 2 +- scripts/{ => config}/config-schema.json | 0 scripts/{ => config}/config.js | 10 +-- scripts/config/utils.js | 92 +++++++++++++++++++++++++ scripts/{ => config}/utils.test.js | 0 scripts/server-backend.js | 2 +- scripts/utils.js | 90 ------------------------ 7 files changed, 97 insertions(+), 99 deletions(-) rename scripts/{ => config}/config-schema.json (100%) rename scripts/{ => config}/config.js (94%) create mode 100644 scripts/config/utils.js rename scripts/{ => config}/utils.test.js (100%) diff --git a/scripts/WhiteboardServerSideInfo.js b/scripts/WhiteboardServerSideInfo.js index 8b148a3..04bf79f 100644 --- a/scripts/WhiteboardServerSideInfo.js +++ b/scripts/WhiteboardServerSideInfo.js @@ -1,4 +1,4 @@ -const config = require("./config"); +const config = require("./config/config"); class WhiteboardServerSideInfo { static defaultScreenResolution = { w: 1000, h: 1000 }; diff --git a/scripts/config-schema.json b/scripts/config/config-schema.json similarity index 100% rename from scripts/config-schema.json rename to scripts/config/config-schema.json diff --git a/scripts/config.js b/scripts/config/config.js similarity index 94% rename from scripts/config.js rename to scripts/config/config.js index 2e94c1e..c1f6266 100644 --- a/scripts/config.js +++ b/scripts/config/config.js @@ -1,12 +1,8 @@ const util = require("util"); -const { - getArgs, - getDefaultConfig, - getConfig, - deepMergeConfigs, - isConfigValid, -} = require("./utils"); +const { getDefaultConfig, getConfig, deepMergeConfigs, isConfigValid } = require("./utils"); + +const { getArgs } = require("./../utils"); const defaultConfig = getDefaultConfig(); diff --git a/scripts/config/utils.js b/scripts/config/utils.js new file mode 100644 index 0000000..e05702f --- /dev/null +++ b/scripts/config/utils.js @@ -0,0 +1,92 @@ +const path = require("path"); +const fs = require("fs"); +const yaml = require("js-yaml"); + +const Ajv = require("ajv"); +const ajv = new Ajv({ allErrors: true }); + +const configSchema = require("./config-schema.json"); + +/** + * Load a yaml config file from a given path. + * + * @param path + * @return {Object} + */ +function getConfig(path) { + return yaml.safeLoad(fs.readFileSync(path, "utf8")); +} + +/** + * Check that a config object is valid. + * + * @param {Object} config Config object + * @param {boolean} warn Should we warn in console for errors + * @return {boolean} + */ +function isConfigValid(config, warn = true) { + const validate = ajv.compile(configSchema); + const isValidAgainstSchema = validate(config); + + if (!isValidAgainstSchema && warn) console.warn(validate.errors); + + let structureIsValid = false; + try { + structureIsValid = config.frontend.performance.pointerEventsThrottling.some( + (item) => item.fromNbUser === 0 + ); + } catch (e) { + if (!e instanceof TypeError) { + throw e; + } + } + + if (!structureIsValid && warn) + console.warn( + "At least one item under frontend.performance.pointerEventsThrottling" + + "must have fromNbUser set to 0" + ); + + return isValidAgainstSchema && structureIsValid; +} + +/** + * Load the default project config + * @return {Object} + */ +function getDefaultConfig() { + const defaultConfigPath = path.join(__dirname, "..", "..", "config.default.yml"); + return getConfig(defaultConfigPath); +} + +/** + * Deep merge of project config + * + * Objects are merged, not arrays + * + * @param baseConfig + * @param overrideConfig + * @return {Object} + */ +function deepMergeConfigs(baseConfig, overrideConfig) { + const out = {}; + + Object.entries(baseConfig).forEach(([key, val]) => { + out[key] = val; + if (overrideConfig.hasOwnProperty(key)) { + const overrideVal = overrideConfig[key]; + if (typeof val === "object" && !Array.isArray(val) && val !== null) { + out[key] = deepMergeConfigs(val, overrideVal); + } else { + out[key] = overrideVal; + } + } + }); + + return out; +} + +module.exports.getConfig = getConfig; +module.exports.getDefaultConfig = getDefaultConfig; +module.exports.deepMergeConfigs = deepMergeConfigs; +module.exports.isConfigValid = isConfigValid; diff --git a/scripts/utils.test.js b/scripts/config/utils.test.js similarity index 100% rename from scripts/utils.test.js rename to scripts/config/utils.test.js diff --git a/scripts/server-backend.js b/scripts/server-backend.js index d24a961..54d14f7 100644 --- a/scripts/server-backend.js +++ b/scripts/server-backend.js @@ -1,6 +1,6 @@ const path = require("path"); -const config = require("./config"); +const config = require("./config/config"); const WhiteboardServerSideInfo = require("./WhiteboardServerSideInfo"); function startBackendServer(port) { diff --git a/scripts/utils.js b/scripts/utils.js index 754a3f2..d18a01b 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -1,12 +1,3 @@ -const path = require("path"); -const fs = require("fs"); -const yaml = require("js-yaml"); - -const Ajv = require("ajv"); -const ajv = new Ajv({ allErrors: true }); - -const configSchema = require("./config-schema.json"); - function getArgs() { const args = {}; process.argv.slice(2, process.argv.length).forEach((arg) => { @@ -26,85 +17,4 @@ function getArgs() { return args; } -/** - * TODO - * - * @param path - * @return {any} - */ -function getConfig(path) { - return yaml.safeLoad(fs.readFileSync(path, "utf8")); -} - -/** - * TODO - * @param config - * @param warn - * @return {*} - */ -function isConfigValid(config, warn = true) { - const validate = ajv.compile(configSchema); - const isValidAgainstSchema = validate(config); - - if (!isValidAgainstSchema && warn) console.warn(validate.errors); - - let structureIsValid = false; - try { - structureIsValid = config.frontend.performance.pointerEventsThrottling.some( - (item) => item.fromNbUser === 0 - ); - } catch (e) { - if (!e instanceof TypeError) { - throw e; - } - } - - if (!structureIsValid && warn) - console.warn( - "At least one item under frontend.performance.pointerEventsThrottling" + - "must have fromNbUser set to 0" - ); - - return isValidAgainstSchema && structureIsValid; -} - -/** - * TODO - * @return {*} - */ -function getDefaultConfig() { - const defaultConfigPath = path.join(__dirname, "..", "config.default.yml"); - return getConfig(defaultConfigPath); -} - -/** - * TODO - * Deep merges objects, not arrays. - * - * @param baseConfig - * @param overrideConfig - * @return {{}} - */ -function deepMergeConfigs(baseConfig, overrideConfig) { - const out = {}; - - Object.entries(baseConfig).forEach(([key, val]) => { - out[key] = val; - if (overrideConfig.hasOwnProperty(key)) { - const overrideVal = overrideConfig[key]; - if (typeof val === "object" && !Array.isArray(val) && val !== null) { - out[key] = deepMergeConfigs(val, overrideVal); - } else { - out[key] = overrideVal; - } - } - }); - - return out; -} - module.exports.getArgs = getArgs; -module.exports.getConfig = getConfig; -module.exports.getDefaultConfig = getDefaultConfig; -module.exports.deepMergeConfigs = deepMergeConfigs; -module.exports.isConfigValid = isConfigValid;