2020-07-29 18:38:59 +02:00
|
|
|
import { assignWithDepth } from './utils';
|
2022-02-10 20:32:21 +01:00
|
|
|
import { log } from './logger'; // eslint-disable-line
|
2020-07-29 18:38:59 +02:00
|
|
|
import theme from './themes';
|
|
|
|
import config from './defaultConfig';
|
|
|
|
|
2022-02-10 20:32:21 +01:00
|
|
|
const handleThemeVariables = (value) => {
|
2020-07-29 18:38:59 +02:00
|
|
|
return theme[value] ? theme[value].getThemeVariables() : theme.default.getThemeVariables();
|
|
|
|
};
|
|
|
|
|
|
|
|
const manipulators = {
|
2022-02-10 20:32:21 +01:00
|
|
|
themeVariables: handleThemeVariables,
|
2020-07-29 18:38:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// debugger;
|
|
|
|
config.class.arrowMarkerAbsolute = config.arrowMarkerAbsolute;
|
|
|
|
config.git.arrowMarkerAbsolute = config.arrowMarkerAbsolute;
|
|
|
|
export const defaultConfig = Object.freeze(config);
|
|
|
|
|
|
|
|
const siteConfig = assignWithDepth({}, defaultConfig);
|
|
|
|
const currentConfig = assignWithDepth({}, defaultConfig);
|
|
|
|
|
|
|
|
/**
|
2022-02-10 20:32:21 +01:00
|
|
|
* ## setSiteConfig
|
|
|
|
*
|
|
|
|
* | Function | Description | Type | Values |
|
|
|
|
* | ------------- | ------------------------------------- | ----------- | --------------------------------------- |
|
|
|
|
* | setSiteConfig | Sets the siteConfig to desired values | Put Request | Any Values, except ones in secure array |
|
|
|
|
*
|
|
|
|
* **Notes:** Sets the siteConfig. The siteConfig is a protected configuration for repeat use. Calls
|
|
|
|
* to reset() will reset the currentConfig to siteConfig. Calls to reset(configApi.defaultConfig)
|
|
|
|
* will reset siteConfig and currentConfig to the defaultConfig Note: currentConfig is set in this
|
|
|
|
* function Default value: At default, will mirror Global Config
|
|
|
|
*
|
|
|
|
* @param {any} conf - The base currentConfig to use as siteConfig
|
|
|
|
* @returns {any} - The siteConfig
|
2020-07-29 18:38:59 +02:00
|
|
|
*/
|
2022-02-10 20:32:21 +01:00
|
|
|
export const setSiteConfig = (conf) => {
|
2020-07-29 18:38:59 +02:00
|
|
|
console.log('setSiteConfig');
|
|
|
|
|
2022-02-10 20:32:21 +01:00
|
|
|
Object.keys(conf).forEach((key) => {
|
2020-07-29 18:38:59 +02:00
|
|
|
const manipulator = manipulators[key];
|
|
|
|
conf[key] = manipulator ? manipulator(conf[key]) : conf[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
assignWithDepth(currentConfig, conf, { clobber: true });
|
|
|
|
// Set theme variables if user has set the theme option
|
|
|
|
assignWithDepth(siteConfig, conf);
|
|
|
|
|
|
|
|
return getSiteConfig();
|
|
|
|
};
|
|
|
|
/**
|
2022-02-10 20:32:21 +01:00
|
|
|
* ## getSiteConfig
|
|
|
|
*
|
|
|
|
* | Function | Description | Type | Values |
|
|
|
|
* | ------------- | ------------------------------------------------- | ----------- | -------------------------------- |
|
|
|
|
* | setSiteConfig | Returns the current siteConfig base configuration | Get Request | Returns Any Values in siteConfig |
|
|
|
|
*
|
|
|
|
* **Notes**: Returns **any** values in siteConfig.
|
|
|
|
*
|
|
|
|
* @returns {any}
|
2020-07-29 18:38:59 +02:00
|
|
|
*/
|
|
|
|
export const getSiteConfig = () => {
|
|
|
|
return assignWithDepth({}, siteConfig);
|
|
|
|
};
|
|
|
|
/**
|
2022-02-10 20:32:21 +01:00
|
|
|
* ## setConfig
|
|
|
|
*
|
|
|
|
* | Function | Description | Type | Values |
|
|
|
|
* | ------------- | ------------------------------------- | ----------- | --------------------------------------- |
|
|
|
|
* | setSiteConfig | Sets the siteConfig to desired values | Put Request | Any Values, except ones in secure array |
|
|
|
|
*
|
|
|
|
* **Notes**: Sets the currentConfig. The parameter conf is sanitized based on the siteConfig.secure
|
|
|
|
* keys. Any values found in conf with key found in siteConfig.secure will be replaced with the
|
|
|
|
* corresponding siteConfig value.
|
|
|
|
*
|
|
|
|
* @param {any} conf - The potential currentConfig
|
|
|
|
* @returns {any} - The currentConfig merged with the sanitized conf
|
2020-07-29 18:38:59 +02:00
|
|
|
*/
|
2022-02-10 20:32:21 +01:00
|
|
|
export const setConfig = (conf) => {
|
2020-07-29 18:38:59 +02:00
|
|
|
console.log('setConfig');
|
|
|
|
sanitize(conf);
|
2022-02-10 20:32:21 +01:00
|
|
|
Object.keys(conf).forEach((key) => {
|
2020-07-29 18:38:59 +02:00
|
|
|
const manipulator = manipulators[key];
|
|
|
|
conf[key] = manipulator ? manipulator(conf[key]) : conf[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
assignWithDepth(currentConfig, conf);
|
|
|
|
return getConfig();
|
|
|
|
};
|
|
|
|
/**
|
2022-02-10 20:32:21 +01:00
|
|
|
* ## getConfig
|
|
|
|
*
|
|
|
|
* | Function | Description | Type | Return Values |
|
|
|
|
* | --------- | ------------------------- | ----------- | ----------------------------- |
|
|
|
|
* | getConfig | Obtains the currentConfig | Get Request | Any Values from currentConfig |
|
|
|
|
*
|
|
|
|
* **Notes**: Returns **any** the currentConfig
|
|
|
|
*
|
|
|
|
* @returns {any} - The currentConfig
|
2020-07-29 18:38:59 +02:00
|
|
|
*/
|
|
|
|
export const getConfig = () => {
|
|
|
|
return assignWithDepth({}, currentConfig);
|
|
|
|
};
|
|
|
|
/**
|
2022-02-10 20:32:21 +01:00
|
|
|
* ## sanitize
|
|
|
|
*
|
|
|
|
* | Function | Description | Type | Values |
|
|
|
|
* | -------- | -------------------------------------- | ----------- | ------ |
|
|
|
|
* | sanitize | Sets the siteConfig to desired values. | Put Request | None |
|
|
|
|
*
|
|
|
|
* Ensures options parameter does not attempt to override siteConfig secure keys **Notes**: modifies
|
|
|
|
* options in-place
|
|
|
|
*
|
|
|
|
* @param {any} options - The potential setConfig parameter
|
2020-07-29 18:38:59 +02:00
|
|
|
*/
|
2022-02-10 20:32:21 +01:00
|
|
|
export const sanitize = (options) => {
|
|
|
|
Object.keys(siteConfig.secure).forEach((key) => {
|
2020-07-29 18:38:59 +02:00
|
|
|
if (typeof options[siteConfig.secure[key]] !== 'undefined') {
|
|
|
|
// DO NOT attempt to print options[siteConfig.secure[key]] within `${}` as a malicious script
|
|
|
|
// can exploit the logger's attempt to stringify the value and execute arbitrary code
|
2022-02-10 20:32:21 +01:00
|
|
|
log.trace(
|
2020-07-29 18:38:59 +02:00
|
|
|
`Denied attempt to modify a secure key ${siteConfig.secure[key]}`,
|
|
|
|
options[siteConfig.secure[key]]
|
|
|
|
);
|
|
|
|
delete options[siteConfig.secure[key]];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
/**
|
2022-02-10 20:32:21 +01:00
|
|
|
* ## reset
|
|
|
|
*
|
|
|
|
* | Function | Description | Type | Required | Values |
|
|
|
|
* | -------- | ---------------------------- | ----------- | -------- | ------ |
|
|
|
|
* | reset | Resets currentConfig to conf | Put Request | Required | None |
|
|
|
|
*
|
|
|
|
* | Parameter | Description |Type | Required | Values|
|
|
|
|
*
|
|
|
|
* | --- | --- | --- | --- | --- |
|
|
|
|
* | conf| base set of values, which currentConfig coul be **reset** to.| Dictionary | Required | Any Values, with respect to the secure Array|
|
|
|
|
*
|
|
|
|
* **Notes**: (default: current siteConfig ) (optional, default `getSiteConfig()`)
|
|
|
|
*
|
|
|
|
* @param {any} conf - The base currentConfig to reset to (default: current siteConfig )
|
2020-07-29 18:38:59 +02:00
|
|
|
*/
|
|
|
|
export const reset = (conf = getSiteConfig()) => {
|
|
|
|
console.warn('reset');
|
2022-02-10 20:32:21 +01:00
|
|
|
Object.keys(siteConfig).forEach((key) => delete siteConfig[key]);
|
|
|
|
Object.keys(currentConfig).forEach((key) => delete currentConfig[key]);
|
2020-07-29 18:38:59 +02:00
|
|
|
assignWithDepth(siteConfig, conf, { clobber: true });
|
|
|
|
assignWithDepth(currentConfig, conf, { clobber: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
const configApi = Object.freeze({
|
|
|
|
sanitize,
|
|
|
|
setSiteConfig,
|
|
|
|
getSiteConfig,
|
|
|
|
setConfig,
|
|
|
|
getConfig,
|
|
|
|
reset,
|
2022-02-10 20:32:21 +01:00
|
|
|
defaultConfig,
|
2020-07-29 18:38:59 +02:00
|
|
|
});
|
|
|
|
export default configApi;
|