mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-27 22:49:56 -05:00
bd597db829
- This is part of the quest to separate the frontend and server & get rid of all the places where there are cross-requires - At the moment the settings cache is one big shared cache used by the frontend and server liberally - This change doesn't really solve the fundamental problems, as we still depend on events, and requires from inside frontend - However it allows us to control the misuse slightly better by getting rid of restricted requires and turning on that eslint ruleset
75 lines
2.4 KiB
JavaScript
75 lines
2.4 KiB
JavaScript
const _ = require('lodash');
|
|
const Promise = require('bluebird');
|
|
const SafeString = require('../../frontend/services/theme-engine/engine').SafeString;
|
|
const errors = require('@tryghost/errors');
|
|
const i18n = require('../../shared/i18n');
|
|
const logging = require('@tryghost/logging');
|
|
const settingsCache = require('../../shared/settings-cache');
|
|
const config = require('../../shared/config');
|
|
|
|
// NOTE: this allowlist is meant to be used to filter out any unexpected
|
|
// input for the "labs" setting value
|
|
const BETA_FEATURES = [
|
|
'activitypub',
|
|
'matchHelper'
|
|
];
|
|
|
|
const ALPHA_FEATURES = [
|
|
'multipleProducts',
|
|
'featureImageMeta',
|
|
'psmRedesign'
|
|
];
|
|
|
|
module.exports.WRITABLE_KEYS_ALLOWLIST = [...BETA_FEATURES, ...ALPHA_FEATURES];
|
|
|
|
module.exports.getAll = () => {
|
|
const labs = _.cloneDeep(settingsCache.get('labs')) || {};
|
|
|
|
ALPHA_FEATURES.forEach((alphaKey) => {
|
|
if (labs[alphaKey] && !(config.get('enableDeveloperExperiments') || process.env.NODE_ENV.match(/^testing/))) {
|
|
delete labs[alphaKey];
|
|
}
|
|
});
|
|
|
|
labs.members = settingsCache.get('members_signup_access') !== 'none';
|
|
|
|
return labs;
|
|
};
|
|
|
|
/**
|
|
* @param {string} flag
|
|
* @returns {boolean}
|
|
*/
|
|
module.exports.isSet = function isSet(flag) {
|
|
const labsConfig = module.exports.getAll();
|
|
|
|
return !!(labsConfig && labsConfig[flag] && labsConfig[flag] === true);
|
|
};
|
|
|
|
module.exports.enabledHelper = function enabledHelper(options, callback) {
|
|
const errDetails = {};
|
|
let errString;
|
|
|
|
if (module.exports.isSet(options.flagKey) === true) {
|
|
// helper is active, use the callback
|
|
return callback();
|
|
}
|
|
|
|
// Else, the helper is not active and we need to handle this as an error
|
|
errDetails.message = i18n.t(options.errMessagePath || 'warnings.helpers.helperNotAvailable', {helperName: options.helperName}),
|
|
errDetails.context = i18n.t(options.errContextPath || 'warnings.helpers.flagMustBeEnabled', {
|
|
helperName: options.helperName,
|
|
flagName: options.flagName
|
|
});
|
|
errDetails.help = i18n.t(options.errHelpPath || 'warnings.helpers.seeLink', {url: options.helpUrl});
|
|
|
|
logging.error(new errors.DisabledFeatureError(errDetails));
|
|
|
|
errString = new SafeString(`<script>console.error("${_.values(errDetails).join(' ')}");</script>`);
|
|
|
|
if (options.async) {
|
|
return Promise.resolve(errString);
|
|
}
|
|
|
|
return errString;
|
|
};
|