Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
sammacbeth committed Dec 30, 2024
1 parent c3b2a2c commit f4e03ea
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion shared/js/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const components = {
trackers: new TrackersGlobal({ tds }),
debugger: new DebuggerConnection({ tds, devtools }),
devtools,
config: new RemoteConfig({ tds, settings })
config: new RemoteConfig({ tds, settings }),
};

// Chrome-only components
Expand Down
73 changes: 36 additions & 37 deletions shared/js/background/components/remote-config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

/**
* @typedef {import('../settings.js')} Settings
* @typedef {import('./tds').default} TDSStorage
* @typedef {import('@duckduckgo/privacy-configuration/schema/config.js').GenericV4Config} Config

Check failure on line 4 in shared/js/background/components/remote-config.js

View workflow job for this annotation

GitHub Actions / lint

Cannot find module '@duckduckgo/privacy-configuration/schema/config.js' or its corresponding type declarations.
*/

import { getFeatureSettings, isFeatureEnabled, satisfiesMinVersion } from '../utils'
import { getExtensionVersion } from '../wrapper'
import { getFeatureSettings, isFeatureEnabled, satisfiesMinVersion } from '../utils';
import { getExtensionVersion } from '../wrapper';

export default class RemoteConfig {
/**
Expand All @@ -17,92 +16,92 @@ export default class RemoteConfig {
*/
constructor({ tds, settings }) {
/** @type {Config?} */
this.config = null
this.settings = settings
this.config = null;
this.settings = settings;
this.ready = new Promise((resolve) => {
tds?.config.onUpdate(async (_, etag, v) => {
this.updateConfig(v)
resolve(null)
})
})
this.updateConfig(v);
resolve(null);
});
});
}

/**
*
* @param {Config} configValue
*
* @param {Config} configValue
*/
updateConfig(configValue) {
// copy config value before modification
const configCopy = structuredClone(configValue)
this.config = processRawConfig(configCopy, this.settings)
const configCopy = structuredClone(configValue);
this.config = processRawConfig(configCopy, this.settings);
}

/**
*
* @param {string} featureName
*
* @param {string} featureName
* @returns {boolean}
*/
isFeatureEnabled(featureName) {
return isFeatureEnabled(featureName, this.config || undefined)
return isFeatureEnabled(featureName, this.config || undefined);
}

/**
*
* @param {string} featureName
*
* @param {string} featureName
* @returns {object}
*/
getFeatureSettings(featureName) {
return getFeatureSettings(featureName, this.config || undefined)
return getFeatureSettings(featureName, this.config || undefined);
}

isSubFeatureEnabled(featureName, subFeatureName) {
if (this.config) {
return isSubFeatureEnabled(featureName, subFeatureName, this.config)
return isSubFeatureEnabled(featureName, subFeatureName, this.config);
} else {
return false
return false;
}
}
}

/**
*
* @param {Config} configValue
*
* @param {Config} configValue
* @param {Settings} settings
* @returns {Config}
*/
export function processRawConfig(configValue, settings) {
Object.entries(configValue.features).forEach(([featureName, feature]) => {
Object.entries(feature.features || {}).forEach(([name, subfeature]) => {
if (subfeature.rollout && subfeature.state === 'enabled') {
/* Handle a rollout: Dice roll is stored in settings and used that to decide
/* Handle a rollout: Dice roll is stored in settings and used that to decide
* whether the feature is set as 'enabled' or not.
*/
const rolloutSettingsKey = `rollouts.${featureName}.${name}.roll`
const validSteps = subfeature.rollout.steps.filter((v) => v.percent > 0 && v.percent <= 100)
const rolloutPercent = validSteps.length > 0 ? validSteps.reverse()[0].percent : 0.0
const rolloutSettingsKey = `rollouts.${featureName}.${name}.roll`;
const validSteps = subfeature.rollout.steps.filter((v) => v.percent > 0 && v.percent <= 100);
const rolloutPercent = validSteps.length > 0 ? validSteps.reverse()[0].percent : 0.0;
if (!settings.getSetting(rolloutSettingsKey)) {
settings.updateSetting(rolloutSettingsKey, Math.random() * 100)
settings.updateSetting(rolloutSettingsKey, Math.random() * 100);
}
const dieRoll = settings.getSetting(rolloutSettingsKey)
subfeature.state = rolloutPercent >= dieRoll ? 'enabled' : 'disabled'
const dieRoll = settings.getSetting(rolloutSettingsKey);
subfeature.state = rolloutPercent >= dieRoll ? 'enabled' : 'disabled';
}
})
})
return configValue
});
});
return configValue;
}

/**
*
* @param {string} featureName
* @param {string} subFeatureName
*
* @param {string} featureName
* @param {string} subFeatureName
* @param {Config} config
* @returns {boolean}
*/
export function isSubFeatureEnabled(featureName, subFeatureName, config) {
const feature = config.features[featureName];
const subFeature = (feature?.features || {})[subFeatureName];
if (!feature || !subFeature) {
return false
return false;
}
if (subFeature.minSupportedVersion) {
const extensionVersionString = getExtensionVersion();
Expand Down

0 comments on commit f4e03ea

Please sign in to comment.