|
| 1 | +import { immutableJSONPatch } from 'immutable-json-patch'; |
| 2 | +import { camelcase, computeEnabledFeatures, matchHostname, parseFeatureSettings } from './utils.js'; |
| 3 | + |
| 4 | +export default class ConfigFeature { |
| 5 | + /** @type {import('./utils.js').RemoteConfig | undefined} */ |
| 6 | + #bundledConfig; |
| 7 | + |
| 8 | + /** @type {string} */ |
| 9 | + name; |
| 10 | + |
| 11 | + /** @type {{ debug?: boolean, desktopModeEnabled?: boolean, forcedZoomEnabled?: boolean, featureSettings?: Record<string, unknown>, assets?: import('./content-feature.js').AssetConfig | undefined, site: import('./content-feature.js').Site, messagingConfig?: import('@duckduckgo/messaging').MessagingConfig } | null} */ |
| 12 | + #args; |
| 13 | + |
| 14 | + /** |
| 15 | + * @param {string} name |
| 16 | + * @param {import('./content-scope-features.js').LoadArgs} args |
| 17 | + */ |
| 18 | + constructor(name, args) { |
| 19 | + this.name = name; |
| 20 | + const { bundledConfig, site, platform } = args; |
| 21 | + this.#bundledConfig = bundledConfig; |
| 22 | + this.#args = args; |
| 23 | + // If we have a bundled config, treat it as a regular config |
| 24 | + // This will be overriden by the remote config if it is available |
| 25 | + if (this.#bundledConfig && this.#args) { |
| 26 | + const enabledFeatures = computeEnabledFeatures(bundledConfig, site.domain, platform.version); |
| 27 | + this.#args.featureSettings = parseFeatureSettings(bundledConfig, enabledFeatures); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + get args() { |
| 32 | + return this.#args; |
| 33 | + } |
| 34 | + |
| 35 | + set args(args) { |
| 36 | + this.#args = args; |
| 37 | + } |
| 38 | + |
| 39 | + get featureSettings() { |
| 40 | + return this.#args?.featureSettings; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Given a config key, interpret the value as a list of domain overrides, and return the elements that match the current page |
| 45 | + * Consider using patchSettings instead as per `getFeatureSetting`. |
| 46 | + * @param {string} featureKeyName |
| 47 | + * @return {any[]} |
| 48 | + * @protected |
| 49 | + */ |
| 50 | + matchDomainFeatureSetting(featureKeyName) { |
| 51 | + const domain = this.args?.site.domain; |
| 52 | + if (!domain) return []; |
| 53 | + const domains = this._getFeatureSettings()?.[featureKeyName] || []; |
| 54 | + return domains.filter((rule) => { |
| 55 | + if (Array.isArray(rule.domain)) { |
| 56 | + return rule.domain.some((domainRule) => { |
| 57 | + return matchHostname(domain, domainRule); |
| 58 | + }); |
| 59 | + } |
| 60 | + return matchHostname(domain, rule.domain); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Return the settings object for a feature |
| 66 | + * @param {string} [featureName] - The name of the feature to get the settings for; defaults to the name of the feature |
| 67 | + * @returns {any} |
| 68 | + */ |
| 69 | + _getFeatureSettings(featureName) { |
| 70 | + const camelFeatureName = featureName || camelcase(this.name); |
| 71 | + return this.featureSettings?.[camelFeatureName]; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * For simple boolean settings, return true if the setting is 'enabled' |
| 76 | + * For objects, verify the 'state' field is 'enabled'. |
| 77 | + * This allows for future forwards compatibility with more complex settings if required. |
| 78 | + * For example: |
| 79 | + * ```json |
| 80 | + * { |
| 81 | + * "toggle": "enabled" |
| 82 | + * } |
| 83 | + * ``` |
| 84 | + * Could become later (without breaking changes): |
| 85 | + * ```json |
| 86 | + * { |
| 87 | + * "toggle": { |
| 88 | + * "state": "enabled", |
| 89 | + * "someOtherKey": 1 |
| 90 | + * } |
| 91 | + * } |
| 92 | + * ``` |
| 93 | + * This also supports domain overrides as per `getFeatureSetting`. |
| 94 | + * @param {string} featureKeyName |
| 95 | + * @param {string} [featureName] |
| 96 | + * @returns {boolean} |
| 97 | + */ |
| 98 | + getFeatureSettingEnabled(featureKeyName, featureName) { |
| 99 | + const result = this.getFeatureSetting(featureKeyName, featureName); |
| 100 | + if (typeof result === 'object') { |
| 101 | + return result.state === 'enabled'; |
| 102 | + } |
| 103 | + return result === 'enabled'; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Return a specific setting from the feature settings |
| 108 | + * If the "settings" key within the config has a "domains" key, it will be used to override the settings. |
| 109 | + * This uses JSONPatch to apply the patches to settings before getting the setting value. |
| 110 | + * For example.com getFeatureSettings('val') will return 1: |
| 111 | + * ```json |
| 112 | + * { |
| 113 | + * "settings": { |
| 114 | + * "domains": [ |
| 115 | + * { |
| 116 | + * "domain": "example.com", |
| 117 | + * "patchSettings": [ |
| 118 | + * { "op": "replace", "path": "/val", "value": 1 } |
| 119 | + * ] |
| 120 | + * } |
| 121 | + * ] |
| 122 | + * } |
| 123 | + * } |
| 124 | + * ``` |
| 125 | + * "domain" can either be a string or an array of strings. |
| 126 | + |
| 127 | + * For boolean states you should consider using getFeatureSettingEnabled. |
| 128 | + * @param {string} featureKeyName |
| 129 | + * @param {string} [featureName] |
| 130 | + * @returns {any} |
| 131 | + */ |
| 132 | + getFeatureSetting(featureKeyName, featureName) { |
| 133 | + let result = this._getFeatureSettings(featureName); |
| 134 | + if (featureKeyName === 'domains') { |
| 135 | + throw new Error('domains is a reserved feature setting key name'); |
| 136 | + } |
| 137 | + const domainMatch = [...this.matchDomainFeatureSetting('domains')].sort((a, b) => { |
| 138 | + return a.domain.length - b.domain.length; |
| 139 | + }); |
| 140 | + for (const match of domainMatch) { |
| 141 | + if (match.patchSettings === undefined) { |
| 142 | + continue; |
| 143 | + } |
| 144 | + try { |
| 145 | + result = immutableJSONPatch(result, match.patchSettings); |
| 146 | + } catch (e) { |
| 147 | + console.error('Error applying patch settings', e); |
| 148 | + } |
| 149 | + } |
| 150 | + return result?.[featureKeyName]; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @returns {import('./utils.js').RemoteConfig | undefined} |
| 155 | + **/ |
| 156 | + get bundledConfig() { |
| 157 | + return this.#bundledConfig; |
| 158 | + } |
| 159 | +} |
0 commit comments