|
1 | 1 | import { immutableJSONPatch } from 'immutable-json-patch'; |
2 | 2 | import { camelcase, computeEnabledFeatures, matchHostname, parseFeatureSettings } from './utils.js'; |
| 3 | +import { URLPattern } from 'urlpattern-polyfill'; |
3 | 4 |
|
4 | 5 | export default class ConfigFeature { |
5 | 6 | /** @type {import('./utils.js').RemoteConfig | undefined} */ |
@@ -48,19 +49,55 @@ export default class ConfigFeature { |
48 | 49 | * @protected |
49 | 50 | */ |
50 | 51 | matchDomainFeatureSetting(featureKeyName) { |
51 | | - const domain = this.args?.site.domain; |
52 | | - if (!domain) return []; |
53 | 52 | const domains = this._getFeatureSettings()?.[featureKeyName] || []; |
54 | 53 | 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); |
| 54 | + return this.matchConditionalChanges(rule); |
61 | 55 | }); |
62 | 56 | } |
63 | 57 |
|
| 58 | + /** |
| 59 | + * Used to match conditional changes for a settings feature. |
| 60 | + * @typedef {object} ConditionBlock |
| 61 | + * @property {string[] | string} domain? |
| 62 | + * @property {object} urlPattern? |
| 63 | + */ |
| 64 | + |
| 65 | + /** |
| 66 | + * Takes a conditional block and returns true if it applies. |
| 67 | + * All conditions must be met to return true. |
| 68 | + * @param {ConditionBlock} conditionBlock |
| 69 | + * @returns {boolean} |
| 70 | + */ |
| 71 | + matchConditionalChanges(conditionBlock) { |
| 72 | + // Check domain condition |
| 73 | + if (conditionBlock.domain && !this._matchDomainConditional(conditionBlock)) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + // Check URL pattern condition |
| 78 | + if (conditionBlock.urlPattern) { |
| 79 | + const pattern = new URLPattern(conditionBlock.urlPattern); |
| 80 | + if (!this.args?.site.url || !pattern.test(this.args?.site.url)) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // All conditions are met |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + _matchDomainConditional(conditionBlock) { |
| 90 | + if (!conditionBlock.domain) return false; |
| 91 | + const domain = this.args?.site.domain; |
| 92 | + if (!domain) return false; |
| 93 | + if (Array.isArray(conditionBlock.domain)) { |
| 94 | + return conditionBlock.domain.some((domainRule) => { |
| 95 | + return matchHostname(domain, domainRule); |
| 96 | + }); |
| 97 | + } |
| 98 | + return matchHostname(domain, conditionBlock.domain); |
| 99 | + } |
| 100 | + |
64 | 101 | /** |
65 | 102 | * Return the settings object for a feature |
66 | 103 | * @param {string} [featureName] - The name of the feature to get the settings for; defaults to the name of the feature |
@@ -131,13 +168,20 @@ export default class ConfigFeature { |
131 | 168 | */ |
132 | 169 | getFeatureSetting(featureKeyName, featureName) { |
133 | 170 | let result = this._getFeatureSettings(featureName); |
134 | | - if (featureKeyName === 'domains') { |
135 | | - throw new Error('domains is a reserved feature setting key name'); |
| 171 | + if (featureKeyName in ['domains', 'conditionalChanges']) { |
| 172 | + throw new Error(`${featureKeyName} is a reserved feature setting key name`); |
136 | 173 | } |
137 | | - const domainMatch = [...this.matchDomainFeatureSetting('domains')].sort((a, b) => { |
138 | | - return a.domain.length - b.domain.length; |
139 | | - }); |
140 | | - for (const match of domainMatch) { |
| 174 | + // We only support one of these keys at a time, where conditionalChanges takes precedence |
| 175 | + // TODO should we rename these? |
| 176 | + // TODO should we only support conditionalChanges to support other types of settings? |
| 177 | + let conditionalMatches = []; |
| 178 | + // Presence check using result to avoid the [] default response |
| 179 | + if (result?.conditionalChanges) { |
| 180 | + conditionalMatches = this.matchDomainFeatureSetting('conditionalChanges'); |
| 181 | + } else { |
| 182 | + conditionalMatches = this.matchDomainFeatureSetting('domains'); |
| 183 | + } |
| 184 | + for (const match of conditionalMatches) { |
141 | 185 | if (match.patchSettings === undefined) { |
142 | 186 | continue; |
143 | 187 | } |
|
0 commit comments