@metamask-previews/remote-feature-flag-controller 7.0.0-preview-01e391e → 7.0.0-preview-a26804c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -25
- package/dist/client-config-api-service/abstract-client-config-api-service.d.ts +17 -0
- package/dist/client-config-api-service/abstract-client-config-api-service.d.ts.map +1 -0
- package/dist/client-config-api-service/abstract-client-config-api-service.js +2 -0
- package/dist/client-config-api-service/abstract-client-config-api-service.js.map +1 -0
- package/dist/client-config-api-service/client-config-api-service.d.ts +75 -65
- package/dist/client-config-api-service/client-config-api-service.d.ts.map +1 -1
- package/dist/client-config-api-service/client-config-api-service.js +49 -67
- package/dist/client-config-api-service/client-config-api-service.js.map +1 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/remote-feature-flag-controller.d.ts +5 -9
- package/dist/remote-feature-flag-controller.d.ts.map +1 -1
- package/dist/remote-feature-flag-controller.js +6 -2
- package/dist/remote-feature-flag-controller.js.map +1 -1
- package/package.json +1 -3
- package/dist/client-config-api-service/client-config-api-service-method-action-types.d.ts +0 -23
- package/dist/client-config-api-service/client-config-api-service-method-action-types.d.ts.map +0 -1
- package/dist/client-config-api-service/client-config-api-service-method-action-types.js +0 -6
- package/dist/client-config-api-service/client-config-api-service-method-action-types.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,39 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Added
|
|
11
11
|
|
|
12
|
-
- Add `ClientConfigApiServiceInvalidateQueriesAction`, `ClientConfigApiServiceCacheUpdatedEvent` and `ClientConfigApiServiceGranularCacheUpdatedEvent`, also included in the new `ClientConfigApiServiceActions` and `ClientConfigApiServiceEvents` types ([#11217](https://github.com/MetaMask/core/pull/11217))
|
|
13
|
-
- Add `ClientConfigApiServiceMessenger`, `ClientConfigApiServiceFetchRemoteFeatureFlagsAction` and `ClientConfigApiServiceMethodActions` exports ([#11217](https://github.com/MetaMask/core/pull/11217))
|
|
14
12
|
- Add support for Node.js 26 ([#10981](https://github.com/MetaMask/core/pull/10981))
|
|
15
13
|
|
|
16
14
|
### Changed
|
|
17
15
|
|
|
18
|
-
- **BREAKING:** `ClientConfigApiService` now extends `BaseDataService` from `@metamask/base-data-service` ([#11217](https://github.com/MetaMask/core/pull/11217))
|
|
19
|
-
- The constructor now requires a `messenger` (`ClientConfigApiServiceMessenger`). Messengers built from `ClientConfigApiServiceActions` and `ClientConfigApiServiceEvents` pick up the new `ClientConfigApiService:invalidateQueries` action and `ClientConfigApiService:cacheUpdated` events automatically.
|
|
20
|
-
- The `retries`, `maximumConsecutiveFailures`, `circuitBreakDuration`, `onBreak` and `onDegraded` constructor options are replaced by `policyOptions` (`maxRetries`, `maxConsecutiveFailures`, `circuitBreakDuration`). The defaults are unchanged.
|
|
21
|
-
- `fetch` and `config` are still required.
|
|
22
|
-
- **BREAKING:** `RemoteFeatureFlagController` no longer takes a `clientConfigApiService` option, and now calls `ClientConfigApiService:fetchRemoteFeatureFlags` through its messenger ([#11217](https://github.com/MetaMask/core/pull/11217))
|
|
23
|
-
- `RemoteFeatureFlagControllerMessenger` now requires that action to be delegated to it. Construct a `ClientConfigApiService` with its own messenger and delegate the action to the controller's messenger.
|
|
24
|
-
- Concurrent calls to `ClientConfigApiService.fetchRemoteFeatureFlags` now share a single request ([#11217](https://github.com/MetaMask/core/pull/11217))
|
|
25
|
-
- Every call still hits the network, so a sequential call always refetches.
|
|
26
|
-
- `ClientConfigApiService.fetchRemoteFeatureFlags` now treats a non 2xx response as a failure inside its request policy ([#11217](https://github.com/MetaMask/core/pull/11217))
|
|
27
|
-
- Previously the status was checked after the policy ran, so a 4xx or 5xx response was not retried and did not count toward the circuit breaker. Now it throws an `HttpError` (`Failed to fetch remote feature flags: <status>`), so 5xx responses are retried and repeated failures open the circuit breaker.
|
|
28
|
-
- `ClientConfigApiService.fetchRemoteFeatureFlags` now validates the response with a struct ([#11217](https://github.com/MetaMask/core/pull/11217))
|
|
29
|
-
- A response that is not an array of objects throws `Query function for "ClientConfigApiService:fetchRemoteFeatureFlags" returned an unexpected response: ...` instead of `Feature flags api did not return an array`. Arrays containing non object items, which used to be ignored, now fail too.
|
|
30
|
-
- Add dependency `@metamask/base-data-service` ([#11217](https://github.com/MetaMask/core/pull/11217))
|
|
31
|
-
- Add dependency `@metamask/superstruct` ([#11217](https://github.com/MetaMask/core/pull/11217))
|
|
32
16
|
- Bump `uuid` from `^8.3.2` to `^14.0.0` ([#10117](https://github.com/MetaMask/core/pull/10117), [#10243](https://github.com/MetaMask/core/pull/10243), [#10941](https://github.com/MetaMask/core/pull/10941))
|
|
33
17
|
- Bump `@metamask/utils` from `^11.12.0` to `^12.0.1` ([#10192](https://github.com/MetaMask/core/pull/10192), [#11107](https://github.com/MetaMask/core/pull/11107))
|
|
34
18
|
- Bump `@metamask/base-controller` from `^10.0.0` to `^10.1.0` ([#10864](https://github.com/MetaMask/core/pull/10864))
|
|
35
19
|
|
|
36
|
-
### Removed
|
|
37
|
-
|
|
38
|
-
- **BREAKING:** Remove `onBreak` and `onDegraded` from `ClientConfigApiService` ([#11217](https://github.com/MetaMask/core/pull/11217))
|
|
39
|
-
- **BREAKING:** Remove the `AbstractClientConfigApiService` type ([#11217](https://github.com/MetaMask/core/pull/11217))
|
|
40
|
-
|
|
41
|
-
### Fixed
|
|
42
|
-
|
|
43
|
-
- Correct the `ClientConfigApiService.fetchRemoteFeatureFlags` documentation, which claimed a structured error object and a cached data fallback. The method throws on failure, and `RemoteFeatureFlagController` keeps its existing state when it does ([#11217](https://github.com/MetaMask/core/pull/11217))
|
|
44
|
-
|
|
45
20
|
## [7.0.0]
|
|
46
21
|
|
|
47
22
|
### Changed
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ServicePolicy } from '@metamask/controller-utils';
|
|
2
|
+
import type { ServiceResponse } from '../remote-feature-flag-controller-types.js';
|
|
3
|
+
/**
|
|
4
|
+
* A service object responsible for fetching feature flags.
|
|
5
|
+
*/
|
|
6
|
+
export type AbstractClientConfigApiService = Partial<Pick<ServicePolicy, 'onBreak' | 'onDegraded'>> & {
|
|
7
|
+
/**
|
|
8
|
+
* Fetches feature flags from the API with specific client, distribution, and
|
|
9
|
+
* environment parameters. Provides structured error handling, including
|
|
10
|
+
* fallback to cached data if available.
|
|
11
|
+
*
|
|
12
|
+
* @returns An object of feature flags and their boolean values or a
|
|
13
|
+
* structured error object.
|
|
14
|
+
*/
|
|
15
|
+
fetchRemoteFeatureFlags(): Promise<ServiceResponse>;
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=abstract-client-config-api-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abstract-client-config-api-service.d.ts","sourceRoot":"","sources":["../../src/client-config-api-service/abstract-client-config-api-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAEhE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4CAA4C,CAAC;AAElF;;GAEG;AACH,MAAM,MAAM,8BAA8B,GAAG,OAAO,CAClD,IAAI,CAAC,aAAa,EAAE,SAAS,GAAG,YAAY,CAAC,CAC9C,GAAG;IACF;;;;;;;OAOG;IACH,uBAAuB,IAAI,OAAO,CAAC,eAAe,CAAC,CAAC;CACrD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abstract-client-config-api-service.js","sourceRoot":"","sources":["../../src/client-config-api-service/abstract-client-config-api-service.ts"],"names":[],"mappings":"","sourcesContent":["import type { ServicePolicy } from '@metamask/controller-utils';\n\nimport type { ServiceResponse } from '../remote-feature-flag-controller-types.js';\n\n/**\n * A service object responsible for fetching feature flags.\n */\nexport type AbstractClientConfigApiService = Partial<\n Pick<ServicePolicy, 'onBreak' | 'onDegraded'>\n> & {\n /**\n * Fetches feature flags from the API with specific client, distribution, and\n * environment parameters. Provides structured error handling, including\n * fallback to cached data if available.\n *\n * @returns An object of feature flags and their boolean values or a\n * structured error object.\n */\n fetchRemoteFeatureFlags(): Promise<ServiceResponse>;\n};\n"]}
|
|
@@ -1,68 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
import type { Messenger } from '@metamask/messenger';
|
|
1
|
+
import type { ServicePolicy } from '@metamask/controller-utils';
|
|
2
|
+
import type { IDisposable } from 'cockatiel';
|
|
4
3
|
import type { ClientType, DistributionType, EnvironmentType, ServiceResponse } from '../remote-feature-flag-controller-types.js';
|
|
5
|
-
import type {
|
|
4
|
+
import type { AbstractClientConfigApiService } from './abstract-client-config-api-service.js';
|
|
6
5
|
/**
|
|
7
|
-
*
|
|
8
|
-
* service's actions and events.
|
|
6
|
+
* This service is responsible for fetching feature flags from the ClientConfig API.
|
|
9
7
|
*/
|
|
10
|
-
export declare
|
|
11
|
-
/**
|
|
12
|
-
* Invalidates cached queries for {@link ClientConfigApiService}.
|
|
13
|
-
*/
|
|
14
|
-
export type ClientConfigApiServiceInvalidateQueriesAction = DataServiceInvalidateQueriesAction<typeof serviceName>;
|
|
15
|
-
/**
|
|
16
|
-
* Actions that {@link ClientConfigApiService} exposes to other consumers.
|
|
17
|
-
*/
|
|
18
|
-
export type ClientConfigApiServiceActions = ClientConfigApiServiceMethodActions | ClientConfigApiServiceInvalidateQueriesAction;
|
|
19
|
-
/**
|
|
20
|
-
* Actions from other messengers that {@link ClientConfigApiServiceMessenger}
|
|
21
|
-
* calls.
|
|
22
|
-
*/
|
|
23
|
-
type AllowedActions = never;
|
|
24
|
-
/**
|
|
25
|
-
* Published when {@link ClientConfigApiService}'s cache is updated.
|
|
26
|
-
*/
|
|
27
|
-
export type ClientConfigApiServiceCacheUpdatedEvent = DataServiceCacheUpdatedEvent<typeof serviceName>;
|
|
28
|
-
/**
|
|
29
|
-
* Published when a key within {@link ClientConfigApiService}'s cache is
|
|
30
|
-
* updated.
|
|
31
|
-
*/
|
|
32
|
-
export type ClientConfigApiServiceGranularCacheUpdatedEvent = DataServiceGranularCacheUpdatedEvent<typeof serviceName>;
|
|
33
|
-
/**
|
|
34
|
-
* Events that {@link ClientConfigApiService} exposes to other consumers.
|
|
35
|
-
*/
|
|
36
|
-
export type ClientConfigApiServiceEvents = ClientConfigApiServiceCacheUpdatedEvent | ClientConfigApiServiceGranularCacheUpdatedEvent;
|
|
37
|
-
/**
|
|
38
|
-
* Events from other messengers that {@link ClientConfigApiService} subscribes
|
|
39
|
-
* to.
|
|
40
|
-
*/
|
|
41
|
-
type AllowedEvents = never;
|
|
42
|
-
/**
|
|
43
|
-
* The messenger restricted to actions and events accessed by
|
|
44
|
-
* {@link ClientConfigApiService}.
|
|
45
|
-
*/
|
|
46
|
-
export type ClientConfigApiServiceMessenger = Messenger<typeof serviceName, ClientConfigApiServiceActions | AllowedActions, ClientConfigApiServiceEvents | AllowedEvents>;
|
|
47
|
-
/**
|
|
48
|
-
* This service is responsible for fetching feature flags from the ClientConfig
|
|
49
|
-
* API.
|
|
50
|
-
*
|
|
51
|
-
* Its {@link ClientConfigApiService.fetchRemoteFeatureFlags} method is
|
|
52
|
-
* automatically registered on the messenger so that
|
|
53
|
-
* `RemoteFeatureFlagController` can call it.
|
|
54
|
-
*/
|
|
55
|
-
export declare class ClientConfigApiService extends BaseDataService<typeof serviceName, ClientConfigApiServiceMessenger> {
|
|
8
|
+
export declare class ClientConfigApiService implements AbstractClientConfigApiService {
|
|
56
9
|
#private;
|
|
57
10
|
/**
|
|
58
11
|
* Constructs a new ClientConfigApiService object.
|
|
59
12
|
*
|
|
60
13
|
* @param args - The arguments.
|
|
61
|
-
* @param args.messenger - The messenger suited for this service.
|
|
62
14
|
* @param args.fetch - A function that can be used to make an HTTP request.
|
|
63
15
|
* If your JavaScript environment supports `fetch` natively, you'll probably
|
|
64
16
|
* want to pass that; otherwise you can pass an equivalent (such as `fetch`
|
|
65
17
|
* via `node-fetch`).
|
|
18
|
+
* @param args.retries - Number of retry attempts for each fetch request.
|
|
19
|
+
* @param args.maximumConsecutiveFailures - The maximum number of consecutive
|
|
20
|
+
* failures allowed before breaking the circuit and pausing further fetch
|
|
21
|
+
* attempts.
|
|
22
|
+
* @param args.circuitBreakDuration - The amount of time to wait when the
|
|
23
|
+
* circuit breaks from too many consecutive failures.
|
|
66
24
|
* @param args.config - The configuration object, includes client,
|
|
67
25
|
* distribution, and environment.
|
|
68
26
|
* @param args.config.client - The client type (e.g., 'extension', 'mobile').
|
|
@@ -70,29 +28,81 @@ export declare class ClientConfigApiService extends BaseDataService<typeof servi
|
|
|
70
28
|
* 'flask').
|
|
71
29
|
* @param args.config.environment - The environment type (e.g., 'prod', 'rc',
|
|
72
30
|
* 'dev').
|
|
73
|
-
* @param args.policyOptions - Options to pass to `createServicePolicy`, which
|
|
74
|
-
* is used to wrap each request. See {@link CreateServicePolicyOptions}.
|
|
75
31
|
*/
|
|
76
|
-
constructor(
|
|
77
|
-
messenger: ClientConfigApiServiceMessenger;
|
|
32
|
+
constructor(args: {
|
|
78
33
|
fetch: typeof fetch;
|
|
34
|
+
retries?: number;
|
|
35
|
+
maximumConsecutiveFailures?: number;
|
|
36
|
+
circuitBreakDuration?: number;
|
|
79
37
|
config: {
|
|
80
38
|
client: ClientType;
|
|
81
39
|
distribution: DistributionType;
|
|
82
40
|
environment: EnvironmentType;
|
|
83
41
|
};
|
|
84
|
-
policyOptions?: CreateServicePolicyOptions;
|
|
85
42
|
});
|
|
86
43
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
44
|
+
* Constructs a new ClientConfigApiService object.
|
|
45
|
+
*
|
|
46
|
+
* @deprecated This signature is deprecated; please use the `onBreak` and
|
|
47
|
+
* `onDegraded` methods instead.
|
|
48
|
+
* @param args - The arguments.
|
|
49
|
+
* @param args.fetch - A function that can be used to make an HTTP request.
|
|
50
|
+
* If your JavaScript environment supports `fetch` natively, you'll probably
|
|
51
|
+
* want to pass that; otherwise you can pass an equivalent (such as `fetch`
|
|
52
|
+
* via `node-fetch`).
|
|
53
|
+
* @param args.retries - Number of retry attempts for each fetch request.
|
|
54
|
+
* @param args.maximumConsecutiveFailures - The maximum number of consecutive
|
|
55
|
+
* failures allowed before breaking the circuit and pausing further fetch
|
|
56
|
+
* attempts.
|
|
57
|
+
* @param args.circuitBreakDuration - The amount of time to wait when the
|
|
58
|
+
* circuit breaks from too many consecutive failures.
|
|
59
|
+
* @param args.onBreak - Callback for when the circuit breaks, useful
|
|
60
|
+
* for capturing metrics about network failures.
|
|
61
|
+
* @param args.onDegraded - Callback for when the API responds successfully
|
|
62
|
+
* but takes too long to respond (5 seconds or more).
|
|
63
|
+
* @param args.config - The configuration object, includes client,
|
|
64
|
+
* distribution, and environment.
|
|
65
|
+
* @param args.config.client - The client type (e.g., 'extension', 'mobile').
|
|
66
|
+
* @param args.config.distribution - The distribution type (e.g., 'main',
|
|
67
|
+
* 'flask').
|
|
68
|
+
* @param args.config.environment - The environment type (e.g., 'prod', 'rc',
|
|
69
|
+
* 'dev').
|
|
70
|
+
*/
|
|
71
|
+
constructor(args: {
|
|
72
|
+
fetch: typeof fetch;
|
|
73
|
+
retries?: number;
|
|
74
|
+
maximumConsecutiveFailures?: number;
|
|
75
|
+
circuitBreakDuration?: number;
|
|
76
|
+
onBreak?: () => void;
|
|
77
|
+
onDegraded?: () => void;
|
|
78
|
+
config: {
|
|
79
|
+
client: ClientType;
|
|
80
|
+
distribution: DistributionType;
|
|
81
|
+
environment: EnvironmentType;
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
/**
|
|
85
|
+
* Listens for when the request to the API fails too many times in a row.
|
|
86
|
+
*
|
|
87
|
+
* @param args - The same arguments that {@link ServicePolicy.onBreak}
|
|
88
|
+
* takes.
|
|
89
|
+
* @returns What {@link ServicePolicy.onBreak} returns.
|
|
90
|
+
*/
|
|
91
|
+
onBreak(...args: Parameters<ServicePolicy['onBreak']>): IDisposable;
|
|
92
|
+
/**
|
|
93
|
+
* Listens for when the API is degraded.
|
|
94
|
+
*
|
|
95
|
+
* @param args - The same arguments that {@link ServicePolicy.onDegraded}
|
|
96
|
+
* takes.
|
|
97
|
+
* @returns What {@link ServicePolicy.onDegraded} returns.
|
|
98
|
+
*/
|
|
99
|
+
onDegraded(...args: Parameters<ServicePolicy['onDegraded']>): IDisposable;
|
|
100
|
+
/**
|
|
101
|
+
* Fetches feature flags from the API with specific client, distribution, and environment parameters.
|
|
102
|
+
* Provides structured error handling, including fallback to cached data if available.
|
|
91
103
|
*
|
|
92
|
-
* @returns
|
|
93
|
-
* were fetched.
|
|
104
|
+
* @returns An object of feature flags and their boolean values or a structured error object.
|
|
94
105
|
*/
|
|
95
106
|
fetchRemoteFeatureFlags(): Promise<ServiceResponse>;
|
|
96
107
|
}
|
|
97
|
-
export {};
|
|
98
108
|
//# sourceMappingURL=client-config-api-service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client-config-api-service.d.ts","sourceRoot":"","sources":["../../src/client-config-api-service/client-config-api-service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client-config-api-service.d.ts","sourceRoot":"","sources":["../../src/client-config-api-service/client-config-api-service.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAG7C,OAAO,KAAK,EAEV,UAAU,EACV,gBAAgB,EAChB,eAAe,EACf,eAAe,EAEhB,MAAM,4CAA4C,CAAC;AACpD,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,yCAAyC,CAAC;AAE9F;;GAEG;AACH,qBAAa,sBAAuB,YAAW,8BAA8B;;IAW3E;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,YAAY,IAAI,EAAE;QAChB,KAAK,EAAE,OAAO,KAAK,CAAC;QACpB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,0BAA0B,CAAC,EAAE,MAAM,CAAC;QACpC,oBAAoB,CAAC,EAAE,MAAM,CAAC;QAC9B,MAAM,EAAE;YACN,MAAM,EAAE,UAAU,CAAC;YACnB,YAAY,EAAE,gBAAgB,CAAC;YAC/B,WAAW,EAAE,eAAe,CAAC;SAC9B,CAAC;KACH,EAAE;IAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IAEH,YAAY,IAAI,EAAE;QAChB,KAAK,EAAE,OAAO,KAAK,CAAC;QACpB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,0BAA0B,CAAC,EAAE,MAAM,CAAC;QACpC,oBAAoB,CAAC,EAAE,MAAM,CAAC;QAC9B,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;QACrB,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;QACxB,MAAM,EAAE;YACN,MAAM,EAAE,UAAU,CAAC;YACnB,YAAY,EAAE,gBAAgB,CAAC;YAC/B,WAAW,EAAE,eAAe,CAAC;SAC9B,CAAC;KACH,EAAE;IAyCH;;;;;;OAMG;IACH,OAAO,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,GAAG,WAAW,CAElE;IAED;;;;;;OAMG;IACH,UAAU,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,GAAG,WAAW,CAExE;IAED;;;;;OAKG;IACU,uBAAuB,IAAI,OAAO,CAAC,eAAe,CAAC,CAyB/D;CAgBF"}
|
|
@@ -1,88 +1,70 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { HttpError } from '@metamask/controller-utils';
|
|
3
|
-
import { array, record, string } from '@metamask/superstruct';
|
|
4
|
-
import { JsonStruct } from '@metamask/utils';
|
|
1
|
+
import { createServicePolicy, DEFAULT_CIRCUIT_BREAK_DURATION, DEFAULT_MAX_CONSECUTIVE_FAILURES, DEFAULT_MAX_RETRIES, } from '@metamask/controller-utils';
|
|
5
2
|
import { BASE_URL } from '../constants.js';
|
|
6
|
-
// === GENERAL ===
|
|
7
3
|
/**
|
|
8
|
-
*
|
|
9
|
-
* service's actions and events.
|
|
4
|
+
* This service is responsible for fetching feature flags from the ClientConfig API.
|
|
10
5
|
*/
|
|
11
|
-
export
|
|
12
|
-
/**
|
|
13
|
-
* Describes the shape of the ClientConfig API response: an array of objects,
|
|
14
|
-
* each holding feature flag key value pairs.
|
|
15
|
-
*/
|
|
16
|
-
const FeatureFlagsResponseStruct = array(record(string(), JsonStruct));
|
|
17
|
-
// === MESSENGER ===
|
|
18
|
-
const MESSENGER_EXPOSED_METHODS = ['fetchRemoteFeatureFlags'];
|
|
19
|
-
// === SERVICE DEFINITION ===
|
|
20
|
-
/**
|
|
21
|
-
* This service is responsible for fetching feature flags from the ClientConfig
|
|
22
|
-
* API.
|
|
23
|
-
*
|
|
24
|
-
* Its {@link ClientConfigApiService.fetchRemoteFeatureFlags} method is
|
|
25
|
-
* automatically registered on the messenger so that
|
|
26
|
-
* `RemoteFeatureFlagController` can call it.
|
|
27
|
-
*/
|
|
28
|
-
export class ClientConfigApiService extends BaseDataService {
|
|
6
|
+
export class ClientConfigApiService {
|
|
29
7
|
#fetch;
|
|
8
|
+
#policy;
|
|
30
9
|
#client;
|
|
31
10
|
#distribution;
|
|
32
11
|
#environment;
|
|
33
|
-
|
|
34
|
-
* Constructs a new ClientConfigApiService object.
|
|
35
|
-
*
|
|
36
|
-
* @param args - The arguments.
|
|
37
|
-
* @param args.messenger - The messenger suited for this service.
|
|
38
|
-
* @param args.fetch - A function that can be used to make an HTTP request.
|
|
39
|
-
* If your JavaScript environment supports `fetch` natively, you'll probably
|
|
40
|
-
* want to pass that; otherwise you can pass an equivalent (such as `fetch`
|
|
41
|
-
* via `node-fetch`).
|
|
42
|
-
* @param args.config - The configuration object, includes client,
|
|
43
|
-
* distribution, and environment.
|
|
44
|
-
* @param args.config.client - The client type (e.g., 'extension', 'mobile').
|
|
45
|
-
* @param args.config.distribution - The distribution type (e.g., 'main',
|
|
46
|
-
* 'flask').
|
|
47
|
-
* @param args.config.environment - The environment type (e.g., 'prod', 'rc',
|
|
48
|
-
* 'dev').
|
|
49
|
-
* @param args.policyOptions - Options to pass to `createServicePolicy`, which
|
|
50
|
-
* is used to wrap each request. See {@link CreateServicePolicyOptions}.
|
|
51
|
-
*/
|
|
52
|
-
constructor({ messenger, fetch: fetchFunction, config, policyOptions = {}, }) {
|
|
53
|
-
super({ name: serviceName, messenger, policyOptions });
|
|
12
|
+
constructor({ fetch: fetchFunction, retries = DEFAULT_MAX_RETRIES, maximumConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES, circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION, onBreak, onDegraded, config, }) {
|
|
54
13
|
this.#fetch = fetchFunction;
|
|
55
14
|
this.#client = config.client;
|
|
56
15
|
this.#distribution = config.distribution;
|
|
57
16
|
this.#environment = config.environment;
|
|
58
|
-
this
|
|
17
|
+
this.#policy = createServicePolicy({
|
|
18
|
+
maxRetries: retries,
|
|
19
|
+
maxConsecutiveFailures: maximumConsecutiveFailures,
|
|
20
|
+
circuitBreakDuration,
|
|
21
|
+
});
|
|
22
|
+
if (onBreak) {
|
|
23
|
+
this.#policy.onBreak(onBreak);
|
|
24
|
+
}
|
|
25
|
+
if (onDegraded) {
|
|
26
|
+
this.#policy.onDegraded(onDegraded);
|
|
27
|
+
}
|
|
59
28
|
}
|
|
60
29
|
/**
|
|
61
|
-
*
|
|
62
|
-
* environment parameters. Always requests fresh data from the network, and
|
|
63
|
-
* concurrent calls share a single request. Throws if the request fails or
|
|
64
|
-
* the response is not an array of flag objects.
|
|
30
|
+
* Listens for when the request to the API fails too many times in a row.
|
|
65
31
|
*
|
|
66
|
-
* @
|
|
67
|
-
*
|
|
32
|
+
* @param args - The same arguments that {@link ServicePolicy.onBreak}
|
|
33
|
+
* takes.
|
|
34
|
+
* @returns What {@link ServicePolicy.onBreak} returns.
|
|
35
|
+
*/
|
|
36
|
+
onBreak(...args) {
|
|
37
|
+
return this.#policy.onBreak(...args);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Listens for when the API is degraded.
|
|
41
|
+
*
|
|
42
|
+
* @param args - The same arguments that {@link ServicePolicy.onDegraded}
|
|
43
|
+
* takes.
|
|
44
|
+
* @returns What {@link ServicePolicy.onDegraded} returns.
|
|
45
|
+
*/
|
|
46
|
+
onDegraded(...args) {
|
|
47
|
+
return this.#policy.onDegraded(...args);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Fetches feature flags from the API with specific client, distribution, and environment parameters.
|
|
51
|
+
* Provides structured error handling, including fallback to cached data if available.
|
|
52
|
+
*
|
|
53
|
+
* @returns An object of feature flags and their boolean values or a structured error object.
|
|
68
54
|
*/
|
|
69
55
|
async fetchRemoteFeatureFlags() {
|
|
70
56
|
const url = `${BASE_URL}/flags?client=${this.#client}&distribution=${this.#distribution}&environment=${this.#environment}`;
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
return (await response.json());
|
|
82
|
-
},
|
|
83
|
-
});
|
|
57
|
+
const response = await this.#policy.execute(() => this.#fetch(url, { cache: 'no-cache' }));
|
|
58
|
+
if (!response.ok) {
|
|
59
|
+
throw new Error('Failed to fetch remote feature flags');
|
|
60
|
+
}
|
|
61
|
+
const data = await response.json();
|
|
62
|
+
if (!Array.isArray(data)) {
|
|
63
|
+
throw new Error('Feature flags api did not return an array');
|
|
64
|
+
}
|
|
65
|
+
const remoteFeatureFlags = this.#flattenFeatureFlags(data);
|
|
84
66
|
return {
|
|
85
|
-
remoteFeatureFlags
|
|
67
|
+
remoteFeatureFlags,
|
|
86
68
|
cacheTimestamp: Date.now(),
|
|
87
69
|
};
|
|
88
70
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client-config-api-service.js","sourceRoot":"","sources":["../../src/client-config-api-service/client-config-api-service.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"client-config-api-service.js","sourceRoot":"","sources":["../../src/client-config-api-service/client-config-api-service.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,8BAA8B,EAC9B,gCAAgC,EAChC,mBAAmB,GACpB,MAAM,4BAA4B,CAAC;AAIpC,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAW3C;;GAEG;AACH,MAAM,OAAO,sBAAsB;IACxB,MAAM,CAAe;IAErB,OAAO,CAAgB;IAEvB,OAAO,CAAa;IAEpB,aAAa,CAAmB;IAEhC,YAAY,CAAkB;IA+EvC,YAAY,EACV,KAAK,EAAE,aAAa,EACpB,OAAO,GAAG,mBAAmB,EAC7B,0BAA0B,GAAG,gCAAgC,EAC7D,oBAAoB,GAAG,8BAA8B,EACrD,OAAO,EACP,UAAU,EACV,MAAM,GAaP;QACC,IAAI,CAAC,MAAM,GAAG,aAAa,CAAC;QAC5B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC,YAAY,CAAC;QACzC,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,WAAW,CAAC;QAEvC,IAAI,CAAC,OAAO,GAAG,mBAAmB,CAAC;YACjC,UAAU,EAAE,OAAO;YACnB,sBAAsB,EAAE,0BAA0B;YAClD,oBAAoB;SACrB,CAAC,CAAC;QACH,IAAI,OAAO,EAAE,CAAC;YACZ,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAChC,CAAC;QACD,IAAI,UAAU,EAAE,CAAC;YACf,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;QACtC,CAAC;IACH,CAA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,IAAI,EAAE,CAAC;QAC7B,CAAC,EAAE,EAAE,CAAC,CAAC;IACT,CAAC;CACF","sourcesContent":["import {\n createServicePolicy,\n DEFAULT_CIRCUIT_BREAK_DURATION,\n DEFAULT_MAX_CONSECUTIVE_FAILURES,\n DEFAULT_MAX_RETRIES,\n} from '@metamask/controller-utils';\nimport type { ServicePolicy } from '@metamask/controller-utils';\nimport type { IDisposable } from 'cockatiel';\n\nimport { BASE_URL } from '../constants.js';\nimport type {\n FeatureFlags,\n ClientType,\n DistributionType,\n EnvironmentType,\n ServiceResponse,\n ApiDataResponse,\n} from '../remote-feature-flag-controller-types.js';\nimport type { AbstractClientConfigApiService } from './abstract-client-config-api-service.js';\n\n/**\n * This service is responsible for fetching feature flags from the ClientConfig API.\n */\nexport class ClientConfigApiService implements AbstractClientConfigApiService {\n readonly #fetch: typeof fetch;\n\n readonly #policy: ServicePolicy;\n\n readonly #client: ClientType;\n\n readonly #distribution: DistributionType;\n\n readonly #environment: EnvironmentType;\n\n /**\n * Constructs a new ClientConfigApiService object.\n *\n * @param args - The arguments.\n * @param args.fetch - A function that can be used to make an HTTP request.\n * If your JavaScript environment supports `fetch` natively, you'll probably\n * want to pass that; otherwise you can pass an equivalent (such as `fetch`\n * via `node-fetch`).\n * @param args.retries - Number of retry attempts for each fetch request.\n * @param args.maximumConsecutiveFailures - The maximum number of consecutive\n * failures allowed before breaking the circuit and pausing further fetch\n * attempts.\n * @param args.circuitBreakDuration - The amount of time to wait when the\n * circuit breaks from too many consecutive failures.\n * @param args.config - The configuration object, includes client,\n * distribution, and environment.\n * @param args.config.client - The client type (e.g., 'extension', 'mobile').\n * @param args.config.distribution - The distribution type (e.g., 'main',\n * 'flask').\n * @param args.config.environment - The environment type (e.g., 'prod', 'rc',\n * 'dev').\n */\n constructor(args: {\n fetch: typeof fetch;\n retries?: number;\n maximumConsecutiveFailures?: number;\n circuitBreakDuration?: number;\n config: {\n client: ClientType;\n distribution: DistributionType;\n environment: EnvironmentType;\n };\n });\n\n /**\n * Constructs a new ClientConfigApiService object.\n *\n * @deprecated This signature is deprecated; please use the `onBreak` and\n * `onDegraded` methods instead.\n * @param args - The arguments.\n * @param args.fetch - A function that can be used to make an HTTP request.\n * If your JavaScript environment supports `fetch` natively, you'll probably\n * want to pass that; otherwise you can pass an equivalent (such as `fetch`\n * via `node-fetch`).\n * @param args.retries - Number of retry attempts for each fetch request.\n * @param args.maximumConsecutiveFailures - The maximum number of consecutive\n * failures allowed before breaking the circuit and pausing further fetch\n * attempts.\n * @param args.circuitBreakDuration - The amount of time to wait when the\n * circuit breaks from too many consecutive failures.\n * @param args.onBreak - Callback for when the circuit breaks, useful\n * for capturing metrics about network failures.\n * @param args.onDegraded - Callback for when the API responds successfully\n * but takes too long to respond (5 seconds or more).\n * @param args.config - The configuration object, includes client,\n * distribution, and environment.\n * @param args.config.client - The client type (e.g., 'extension', 'mobile').\n * @param args.config.distribution - The distribution type (e.g., 'main',\n * 'flask').\n * @param args.config.environment - The environment type (e.g., 'prod', 'rc',\n * 'dev').\n */\n // eslint-disable-next-line @typescript-eslint/unified-signatures\n constructor(args: {\n fetch: typeof fetch;\n retries?: number;\n maximumConsecutiveFailures?: number;\n circuitBreakDuration?: number;\n onBreak?: () => void;\n onDegraded?: () => void;\n config: {\n client: ClientType;\n distribution: DistributionType;\n environment: EnvironmentType;\n };\n });\n\n constructor({\n fetch: fetchFunction,\n retries = DEFAULT_MAX_RETRIES,\n maximumConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES,\n circuitBreakDuration = DEFAULT_CIRCUIT_BREAK_DURATION,\n onBreak,\n onDegraded,\n config,\n }: {\n fetch: typeof fetch;\n retries?: number;\n maximumConsecutiveFailures?: number;\n circuitBreakDuration?: number;\n onBreak?: () => void;\n onDegraded?: () => void;\n config: {\n client: ClientType;\n distribution: DistributionType;\n environment: EnvironmentType;\n };\n }) {\n this.#fetch = fetchFunction;\n this.#client = config.client;\n this.#distribution = config.distribution;\n this.#environment = config.environment;\n\n this.#policy = createServicePolicy({\n maxRetries: retries,\n maxConsecutiveFailures: maximumConsecutiveFailures,\n circuitBreakDuration,\n });\n if (onBreak) {\n this.#policy.onBreak(onBreak);\n }\n if (onDegraded) {\n this.#policy.onDegraded(onDegraded);\n }\n }\n\n /**\n * Listens for when the request to the API fails too many times in a row.\n *\n * @param args - The same arguments that {@link ServicePolicy.onBreak}\n * takes.\n * @returns What {@link ServicePolicy.onBreak} returns.\n */\n onBreak(...args: Parameters<ServicePolicy['onBreak']>): IDisposable {\n return this.#policy.onBreak(...args);\n }\n\n /**\n * Listens for when the API is degraded.\n *\n * @param args - The same arguments that {@link ServicePolicy.onDegraded}\n * takes.\n * @returns What {@link ServicePolicy.onDegraded} returns.\n */\n onDegraded(...args: Parameters<ServicePolicy['onDegraded']>): IDisposable {\n return this.#policy.onDegraded(...args);\n }\n\n /**\n * Fetches feature flags from the API with specific client, distribution, and environment parameters.\n * Provides structured error handling, including fallback to cached data if available.\n *\n * @returns An object of feature flags and their boolean values or a structured error object.\n */\n public async fetchRemoteFeatureFlags(): Promise<ServiceResponse> {\n const url = `${BASE_URL}/flags?client=${this.#client}&distribution=${\n this.#distribution\n }&environment=${this.#environment}`;\n\n const response = await this.#policy.execute(() =>\n this.#fetch(url, { cache: 'no-cache' }),\n );\n\n if (!response.ok) {\n throw new Error('Failed to fetch remote feature flags');\n }\n\n const data = await response.json();\n\n if (!Array.isArray(data)) {\n throw new Error('Feature flags api did not return an array');\n }\n\n const remoteFeatureFlags = this.#flattenFeatureFlags(data);\n\n return {\n remoteFeatureFlags,\n cacheTimestamp: Date.now(),\n };\n }\n\n /**\n * Flattens an array of feature flag objects into a single feature flags object.\n *\n * @param responseData - Array of objects containing feature flag key-value pairs\n * @returns A single object containing all feature flags merged together\n * @example\n * // Input: [{ flag1: true }, { flag2: [] }]\n * // Output: { flag1: true, flag2: [] }\n */\n #flattenFeatureFlags(responseData: ApiDataResponse): FeatureFlags {\n return responseData.reduce((acc, curr) => {\n return { ...acc, ...curr };\n }, {});\n }\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,5 @@ export type { RemoteFeatureFlagControllerClearAllFlagOverridesAction, RemoteFeat
|
|
|
4
4
|
export { ClientType, DistributionType, EnvironmentType, ThresholdVersion, } from './remote-feature-flag-controller-types.js';
|
|
5
5
|
export type { FeatureFlags } from './remote-feature-flag-controller-types.js';
|
|
6
6
|
export { ClientConfigApiService } from './client-config-api-service/client-config-api-service.js';
|
|
7
|
-
export type { ClientConfigApiServiceActions, ClientConfigApiServiceCacheUpdatedEvent, ClientConfigApiServiceEvents, ClientConfigApiServiceGranularCacheUpdatedEvent, ClientConfigApiServiceInvalidateQueriesAction, ClientConfigApiServiceMessenger, } from './client-config-api-service/client-config-api-service.js';
|
|
8
|
-
export type { ClientConfigApiServiceFetchRemoteFeatureFlagsAction, ClientConfigApiServiceMethodActions, } from './client-config-api-service/client-config-api-service-method-action-types.js';
|
|
9
7
|
export { generateDeterministicRandomNumber } from './utils/user-segmentation-utils.js';
|
|
10
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,qCAAqC,CAAC;AAClF,YAAY,EACV,gCAAgC,EAChC,oCAAoC,EACpC,kCAAkC,EAClC,yCAAyC,EACzC,iCAAiC,EACjC,2CAA2C,GAC5C,MAAM,qCAAqC,CAAC;AAC7C,YAAY,EACV,sDAAsD,EACtD,wCAAwC,EACxC,uCAAuC,EACvC,wCAAwC,EACxC,mDAAmD,EACnD,gDAAgD,EAChD,yDAAyD,GAC1D,MAAM,yDAAyD,CAAC;AACjE,OAAO,EACL,UAAU,EACV,gBAAgB,EAChB,eAAe,EACf,gBAAgB,GACjB,MAAM,2CAA2C,CAAC;AAEnD,YAAY,EAAE,YAAY,EAAE,MAAM,2CAA2C,CAAC;AAC9E,OAAO,EAAE,sBAAsB,EAAE,MAAM,0DAA0D,CAAC;AAClG,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,qCAAqC,CAAC;AAClF,YAAY,EACV,gCAAgC,EAChC,oCAAoC,EACpC,kCAAkC,EAClC,yCAAyC,EACzC,iCAAiC,EACjC,2CAA2C,GAC5C,MAAM,qCAAqC,CAAC;AAC7C,YAAY,EACV,sDAAsD,EACtD,wCAAwC,EACxC,uCAAuC,EACvC,wCAAwC,EACxC,mDAAmD,EACnD,gDAAgD,EAChD,yDAAyD,GAC1D,MAAM,yDAAyD,CAAC;AACjE,OAAO,EACL,UAAU,EACV,gBAAgB,EAChB,eAAe,EACf,gBAAgB,GACjB,MAAM,2CAA2C,CAAC;AAEnD,YAAY,EAAE,YAAY,EAAE,MAAM,2CAA2C,CAAC;AAC9E,OAAO,EAAE,sBAAsB,EAAE,MAAM,0DAA0D,CAAC;AAClG,OAAO,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,CAAC"}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,qCAAqC,CAAC;AAkBlF,OAAO,EACL,UAAU,EACV,gBAAgB,EAChB,eAAe,EACf,gBAAgB,GACjB,MAAM,2CAA2C,CAAC;AAGnD,OAAO,EAAE,sBAAsB,EAAE,MAAM,0DAA0D,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,qCAAqC,CAAC;AAkBlF,OAAO,EACL,UAAU,EACV,gBAAgB,EAChB,eAAe,EACf,gBAAgB,GACjB,MAAM,2CAA2C,CAAC;AAGnD,OAAO,EAAE,sBAAsB,EAAE,MAAM,0DAA0D,CAAC;AAClG,OAAO,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,CAAC","sourcesContent":["export { RemoteFeatureFlagController } from './remote-feature-flag-controller.js';\nexport type {\n RemoteFeatureFlagControllerState,\n RemoteFeatureFlagControllerMessenger,\n RemoteFeatureFlagControllerActions,\n RemoteFeatureFlagControllerGetStateAction,\n RemoteFeatureFlagControllerEvents,\n RemoteFeatureFlagControllerStateChangeEvent,\n} from './remote-feature-flag-controller.js';\nexport type {\n RemoteFeatureFlagControllerClearAllFlagOverridesAction,\n RemoteFeatureFlagControllerDisableAction,\n RemoteFeatureFlagControllerEnableAction,\n RemoteFeatureFlagControllerMethodActions,\n RemoteFeatureFlagControllerRemoveFlagOverrideAction,\n RemoteFeatureFlagControllerSetFlagOverrideAction,\n RemoteFeatureFlagControllerUpdateRemoteFeatureFlagsAction,\n} from './remote-feature-flag-controller-method-action-types.js';\nexport {\n ClientType,\n DistributionType,\n EnvironmentType,\n ThresholdVersion,\n} from './remote-feature-flag-controller-types.js';\n\nexport type { FeatureFlags } from './remote-feature-flag-controller-types.js';\nexport { ClientConfigApiService } from './client-config-api-service/client-config-api-service.js';\nexport { generateDeterministicRandomNumber } from './utils/user-segmentation-utils.js';\n"]}
|
|
@@ -2,7 +2,7 @@ import { BaseController, ControllerGetStateAction } from '@metamask/base-control
|
|
|
2
2
|
import type { ControllerStateChangeEvent } from '@metamask/base-controller';
|
|
3
3
|
import type { Messenger } from '@metamask/messenger';
|
|
4
4
|
import type { Json } from '@metamask/utils';
|
|
5
|
-
import type {
|
|
5
|
+
import type { AbstractClientConfigApiService } from './client-config-api-service/abstract-client-config-api-service.js';
|
|
6
6
|
import type { RemoteFeatureFlagControllerMethodActions } from './remote-feature-flag-controller-method-action-types.js';
|
|
7
7
|
import type { FeatureFlags } from './remote-feature-flag-controller-types.js';
|
|
8
8
|
export declare const controllerName = "RemoteFeatureFlagController";
|
|
@@ -19,12 +19,7 @@ export type RemoteFeatureFlagControllerGetStateAction = ControllerGetStateAction
|
|
|
19
19
|
export type RemoteFeatureFlagControllerActions = RemoteFeatureFlagControllerGetStateAction | RemoteFeatureFlagControllerMethodActions;
|
|
20
20
|
export type RemoteFeatureFlagControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, RemoteFeatureFlagControllerState>;
|
|
21
21
|
export type RemoteFeatureFlagControllerEvents = RemoteFeatureFlagControllerStateChangeEvent;
|
|
22
|
-
|
|
23
|
-
* Actions from other messengers that {@link RemoteFeatureFlagControllerMessenger}
|
|
24
|
-
* calls.
|
|
25
|
-
*/
|
|
26
|
-
type AllowedActions = ClientConfigApiServiceFetchRemoteFeatureFlagsAction;
|
|
27
|
-
export type RemoteFeatureFlagControllerMessenger = Messenger<typeof controllerName, RemoteFeatureFlagControllerActions | AllowedActions, RemoteFeatureFlagControllerEvents>;
|
|
22
|
+
export type RemoteFeatureFlagControllerMessenger = Messenger<typeof controllerName, RemoteFeatureFlagControllerActions, RemoteFeatureFlagControllerEvents>;
|
|
28
23
|
/**
|
|
29
24
|
* Returns the default state for the RemoteFeatureFlagController.
|
|
30
25
|
*
|
|
@@ -45,6 +40,7 @@ export declare class RemoteFeatureFlagController extends BaseController<typeof c
|
|
|
45
40
|
* @param options - The controller options.
|
|
46
41
|
* @param options.messenger - The messenger used for communication.
|
|
47
42
|
* @param options.state - The initial state of the controller.
|
|
43
|
+
* @param options.clientConfigApiService - The service instance to fetch remote feature flags.
|
|
48
44
|
* @param options.fetchInterval - The interval in milliseconds before cached flags expire. Defaults to 1 day.
|
|
49
45
|
* @param options.disabled - Determines if the controller should be disabled initially. Defaults to false.
|
|
50
46
|
* @param options.getMetaMetricsId - Returns metaMetricsId.
|
|
@@ -54,9 +50,10 @@ export declare class RemoteFeatureFlagController extends BaseController<typeof c
|
|
|
54
50
|
* @param options.prevClientVersion - The previous client version for feature flag cache invalidation.
|
|
55
51
|
* @param options.defaultFeatureFlags - Client-side default feature flags used as the lowest-precedence layer under processed remote flags and local overrides. Not persisted.
|
|
56
52
|
*/
|
|
57
|
-
constructor({ messenger, state, fetchInterval, disabled, getMetaMetricsId, getCanonicalProfileId, metaMetricsFlags, clientVersion, prevClientVersion, defaultFeatureFlags, }: {
|
|
53
|
+
constructor({ messenger, state, clientConfigApiService, fetchInterval, disabled, getMetaMetricsId, getCanonicalProfileId, metaMetricsFlags, clientVersion, prevClientVersion, defaultFeatureFlags, }: {
|
|
58
54
|
messenger: RemoteFeatureFlagControllerMessenger;
|
|
59
55
|
state?: Partial<RemoteFeatureFlagControllerState>;
|
|
56
|
+
clientConfigApiService: AbstractClientConfigApiService;
|
|
60
57
|
getMetaMetricsId: () => string;
|
|
61
58
|
getCanonicalProfileId: () => string;
|
|
62
59
|
metaMetricsFlags?: readonly string[];
|
|
@@ -113,5 +110,4 @@ export declare class RemoteFeatureFlagController extends BaseController<typeof c
|
|
|
113
110
|
*/
|
|
114
111
|
clearAllFlagOverrides(): void;
|
|
115
112
|
}
|
|
116
|
-
export {};
|
|
117
113
|
//# sourceMappingURL=remote-feature-flag-controller.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote-feature-flag-controller.d.ts","sourceRoot":"","sources":["../src/remote-feature-flag-controller.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,wBAAwB,EACzB,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AAC5E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAErD,OAAO,KAAK,EAAE,IAAI,EAAiB,MAAM,iBAAiB,CAAC;AAE3D,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"remote-feature-flag-controller.d.ts","sourceRoot":"","sources":["../src/remote-feature-flag-controller.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,wBAAwB,EACzB,MAAM,2BAA2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{
|
|
|
101
101
|
export class RemoteFeatureFlagController extends BaseController {
|
|
102
102
|
#fetchInterval;
|
|
103
103
|
#disabled;
|
|
104
|
+
#clientConfigApiService;
|
|
104
105
|
#inProgressFlagUpdate;
|
|
105
106
|
#getMetaMetricsId;
|
|
106
107
|
#getCanonicalProfileId;
|
|
@@ -114,6 +115,7 @@ export class RemoteFeatureFlagController extends BaseController {
|
|
|
114
115
|
* @param options - The controller options.
|
|
115
116
|
* @param options.messenger - The messenger used for communication.
|
|
116
117
|
* @param options.state - The initial state of the controller.
|
|
118
|
+
* @param options.clientConfigApiService - The service instance to fetch remote feature flags.
|
|
117
119
|
* @param options.fetchInterval - The interval in milliseconds before cached flags expire. Defaults to 1 day.
|
|
118
120
|
* @param options.disabled - Determines if the controller should be disabled initially. Defaults to false.
|
|
119
121
|
* @param options.getMetaMetricsId - Returns metaMetricsId.
|
|
@@ -123,7 +125,7 @@ export class RemoteFeatureFlagController extends BaseController {
|
|
|
123
125
|
* @param options.prevClientVersion - The previous client version for feature flag cache invalidation.
|
|
124
126
|
* @param options.defaultFeatureFlags - Client-side default feature flags used as the lowest-precedence layer under processed remote flags and local overrides. Not persisted.
|
|
125
127
|
*/
|
|
126
|
-
constructor({ messenger, state, fetchInterval = DEFAULT_CACHE_DURATION, disabled = false, getMetaMetricsId, getCanonicalProfileId, metaMetricsFlags = [], clientVersion, prevClientVersion, defaultFeatureFlags = {}, }) {
|
|
128
|
+
constructor({ messenger, state, clientConfigApiService, fetchInterval = DEFAULT_CACHE_DURATION, disabled = false, getMetaMetricsId, getCanonicalProfileId, metaMetricsFlags = [], clientVersion, prevClientVersion, defaultFeatureFlags = {}, }) {
|
|
127
129
|
if (!isValidSemVerVersion(clientVersion)) {
|
|
128
130
|
throw new Error(`Invalid clientVersion: "${clientVersion}". Must be a valid 3-part SemVer version string`);
|
|
129
131
|
}
|
|
@@ -152,6 +154,7 @@ export class RemoteFeatureFlagController extends BaseController {
|
|
|
152
154
|
this.#processedRemoteFeatureFlags = initialState.remoteFeatureFlags;
|
|
153
155
|
this.#fetchInterval = fetchInterval;
|
|
154
156
|
this.#disabled = disabled;
|
|
157
|
+
this.#clientConfigApiService = clientConfigApiService;
|
|
155
158
|
this.#getMetaMetricsId = getMetaMetricsId;
|
|
156
159
|
this.#getCanonicalProfileId = getCanonicalProfileId;
|
|
157
160
|
this.#metaMetricsFlags = new Set(metaMetricsFlags);
|
|
@@ -202,7 +205,8 @@ export class RemoteFeatureFlagController extends BaseController {
|
|
|
202
205
|
return;
|
|
203
206
|
}
|
|
204
207
|
try {
|
|
205
|
-
this.#inProgressFlagUpdate =
|
|
208
|
+
this.#inProgressFlagUpdate =
|
|
209
|
+
this.#clientConfigApiService.fetchRemoteFeatureFlags();
|
|
206
210
|
serverData = await this.#inProgressFlagUpdate;
|
|
207
211
|
}
|
|
208
212
|
finally {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote-feature-flag-controller.js","sourceRoot":"","sources":["../src/remote-feature-flag-controller.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAEf,MAAM,2BAA2B,CAAC;AAGnC,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAUpE,OAAO,EACL,yBAAyB,EACzB,2BAA2B,GAC5B,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAE1E,kBAAkB;AAElB,MAAM,CAAC,MAAM,cAAc,GAAG,6BAA6B,CAAC;AAC5D,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,QAAQ;AAanE,MAAM,mCAAmC,GAAG;IAC1C,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,cAAc,EAAE;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AAc;gBAC5B,CAAC,QAAQ,CAAC,EAAE,KAAK;aAClB,CAAC;YAEF,OAAO;gBACL,GAAG,IAAI,CAAC,KAAK;gBACb,cAAc;gBACd,kBAAkB,EAAE,IAAI,CAAC,yBAAyB,CAAC,EAAE,cAAc,EAAE,CAAC;aACvE,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,kBAAkB,CAAC,QAAgB;QACjC,MAAM,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,cAAc,EAAE,CAAC;QAC3D,OAAO,iBAAiB,CAAC,QAAQ,CAAC,CAAC;QAEnC,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;YACf,OAAO;gBACL,GAAG,IAAI,CAAC,KAAK;gBACb,cAAc,EAAE,iBAAiB;gBACjC,kBAAkB,EAAE,IAAI,CAAC,yBAAyB,CAAC;oBACjD,cAAc,EAAE,iBAAiB;iBAClC,CAAC;aACH,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,qBAAqB;QACnB,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;YACf,OAAO;gBACL,GAAG,IAAI,CAAC,KAAK;gBACb,cAAc,EAAE,EAAE;gBAClB,kBAAkB,EAAE,IAAI,CAAC,yBAAyB,CAAC;oBACjD,cAAc,EAAE,EAAE;iBACnB,CAAC;aACH,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import {\n BaseController,\n ControllerGetStateAction,\n} from '@metamask/base-controller';\nimport type { ControllerStateChangeEvent } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport { hasProperty, isValidSemVerVersion } from '@metamask/utils';\nimport type { Json, SemVerVersion } from '@metamask/utils';\n\nimport type { ClientConfigApiServiceFetchRemoteFeatureFlagsAction } from './client-config-api-service/client-config-api-service-method-action-types.js';\nimport type { RemoteFeatureFlagControllerMethodActions } from './remote-feature-flag-controller-method-action-types.js';\nimport type {\n FeatureFlags,\n ServiceResponse,\n FeatureFlagScopeValue,\n} from './remote-feature-flag-controller-types.js';\nimport {\n calculateThresholdForFlag,\n isFeatureFlagWithScopeValue,\n} from './utils/user-segmentation-utils.js';\nimport { isVersionFeatureFlag, getVersionData } from './utils/version.js';\n\n// === GENERAL ===\n\nexport const controllerName = 'RemoteFeatureFlagController';\nexport const DEFAULT_CACHE_DURATION = 24 * 60 * 60 * 1000; // 1 day\n\n// === STATE ===\n\nexport type RemoteFeatureFlagControllerState = {\n remoteFeatureFlags: FeatureFlags;\n localOverrides?: FeatureFlags;\n rawRemoteFeatureFlags?: FeatureFlags;\n cacheTimestamp: number;\n thresholdCache?: Record<string, number>;\n featureFlagThresholdGroups?: Record<string, string>;\n};\n\nconst remoteFeatureFlagControllerMetadata = {\n remoteFeatureFlags: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n localOverrides: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n rawRemoteFeatureFlags: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n cacheTimestamp: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n thresholdCache: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: false,\n },\n featureFlagThresholdGroups: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n};\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'clearAllFlagOverrides',\n 'disable',\n 'enable',\n 'removeFlagOverride',\n 'setFlagOverride',\n 'updateRemoteFeatureFlags',\n] as const;\n\nexport type RemoteFeatureFlagControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n RemoteFeatureFlagControllerState\n >;\n\nexport type RemoteFeatureFlagControllerActions =\n | RemoteFeatureFlagControllerGetStateAction\n | RemoteFeatureFlagControllerMethodActions;\n\nexport type RemoteFeatureFlagControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n RemoteFeatureFlagControllerState\n >;\n\nexport type RemoteFeatureFlagControllerEvents =\n RemoteFeatureFlagControllerStateChangeEvent;\n\n/**\n * Actions from other messengers that {@link RemoteFeatureFlagControllerMessenger}\n * calls.\n */\ntype AllowedActions = ClientConfigApiServiceFetchRemoteFeatureFlagsAction;\n\nexport type RemoteFeatureFlagControllerMessenger = Messenger<\n typeof controllerName,\n RemoteFeatureFlagControllerActions | AllowedActions,\n RemoteFeatureFlagControllerEvents\n>;\n\n/**\n * Returns the default state for the RemoteFeatureFlagController.\n *\n * @returns The default controller state.\n */\nexport function getDefaultRemoteFeatureFlagControllerState(): RemoteFeatureFlagControllerState {\n return {\n remoteFeatureFlags: {},\n localOverrides: {},\n rawRemoteFeatureFlags: {},\n cacheTimestamp: 0,\n };\n}\n\n/**\n * Searches threshold entries for an explicit MetaMetrics ID match.\n * Returns the first entry whose `metaMetricsIds` list contains the given\n * normalized ID. Entries with malformed `metaMetricsIds` (not an array) are\n * skipped without throwing.\n *\n * @param entries - The array of raw threshold entries for a feature flag.\n * @param normalizedId - The current user's MetaMetrics ID, already trimmed and\n * lower-cased.\n * @returns The first matching entry, or `undefined` if none match.\n */\nfunction findExplicitIdMatch(\n entries: Json[],\n normalizedId: string,\n): FeatureFlagScopeValue | undefined {\n for (const entry of entries) {\n if (!isFeatureFlagWithScopeValue(entry)) {\n continue;\n }\n const { metaMetricsIds } = entry;\n if (!Array.isArray(metaMetricsIds)) {\n continue;\n }\n const hasMatch = metaMetricsIds.some(\n (id) =>\n typeof id === 'string' && id.trim().toLowerCase() === normalizedId,\n );\n if (hasMatch) {\n return entry;\n }\n }\n return undefined;\n}\n\n/**\n * The RemoteFeatureFlagController manages the retrieval and caching of remote feature flags.\n * It fetches feature flags from a remote API, caches them, and provides methods to access\n * and manage these flags. The controller ensures that feature flags are refreshed based on\n * a specified interval and handles cases where the controller is disabled or the network is unavailable.\n */\nexport class RemoteFeatureFlagController extends BaseController<\n typeof controllerName,\n RemoteFeatureFlagControllerState,\n RemoteFeatureFlagControllerMessenger\n> {\n readonly #fetchInterval: number;\n\n #disabled: boolean;\n\n #inProgressFlagUpdate?: Promise<ServiceResponse>;\n\n readonly #getMetaMetricsId: () => string;\n\n readonly #getCanonicalProfileId: () => string;\n\n readonly #metaMetricsFlags: ReadonlySet<string>;\n\n readonly #clientVersion: SemVerVersion;\n\n readonly #defaultFeatureFlags: FeatureFlags;\n\n #processedRemoteFeatureFlags: FeatureFlags;\n\n /**\n * Constructs a new RemoteFeatureFlagController instance.\n *\n * @param options - The controller options.\n * @param options.messenger - The messenger used for communication.\n * @param options.state - The initial state of the controller.\n * @param options.fetchInterval - The interval in milliseconds before cached flags expire. Defaults to 1 day.\n * @param options.disabled - Determines if the controller should be disabled initially. Defaults to false.\n * @param options.getMetaMetricsId - Returns metaMetricsId.\n * @param options.getCanonicalProfileId - Returns the canonical profile identifier used for threshold flags by default. Must return an empty string, rather than throwing, when the identifier is unavailable.\n * @param options.metaMetricsFlags - Names of feature flags that should use MetaMetrics ID for threshold assignment.\n * @param options.clientVersion - The current client version for version-based feature flag filtering. Must be a valid 3-part SemVer version string.\n * @param options.prevClientVersion - The previous client version for feature flag cache invalidation.\n * @param options.defaultFeatureFlags - Client-side default feature flags used as the lowest-precedence layer under processed remote flags and local overrides. Not persisted.\n */\n constructor({\n messenger,\n state,\n fetchInterval = DEFAULT_CACHE_DURATION,\n disabled = false,\n getMetaMetricsId,\n getCanonicalProfileId,\n metaMetricsFlags = [],\n clientVersion,\n prevClientVersion,\n defaultFeatureFlags = {},\n }: {\n messenger: RemoteFeatureFlagControllerMessenger;\n state?: Partial<RemoteFeatureFlagControllerState>;\n getMetaMetricsId: () => string;\n getCanonicalProfileId: () => string;\n metaMetricsFlags?: readonly string[];\n fetchInterval?: number;\n disabled?: boolean;\n clientVersion: string;\n prevClientVersion?: string;\n defaultFeatureFlags?: FeatureFlags;\n }) {\n if (!isValidSemVerVersion(clientVersion)) {\n throw new Error(\n `Invalid clientVersion: \"${clientVersion}\". Must be a valid 3-part SemVer version string`,\n );\n }\n\n const initialState: RemoteFeatureFlagControllerState = {\n ...getDefaultRemoteFeatureFlagControllerState(),\n ...state,\n };\n\n const hasClientVersionChanged =\n isValidSemVerVersion(prevClientVersion) &&\n prevClientVersion !== clientVersion;\n\n super({\n name: controllerName,\n metadata: remoteFeatureFlagControllerMetadata,\n messenger,\n state: {\n ...initialState,\n cacheTimestamp: hasClientVersionChanged\n ? 0\n : initialState.cacheTimestamp,\n },\n });\n\n this.#defaultFeatureFlags = defaultFeatureFlags;\n // Last session's effective flags stand in for the remote layer until\n // `init` re-derives it from the persisted raw flags, or a fetch replaces\n // it. Overrides are layered on top rather than subtracted out, so a remote\n // flag that happens to share an override's value is not lost.\n this.#processedRemoteFeatureFlags = initialState.remoteFeatureFlags;\n this.#fetchInterval = fetchInterval;\n this.#disabled = disabled;\n this.#getMetaMetricsId = getMetaMetricsId;\n this.#getCanonicalProfileId = getCanonicalProfileId;\n this.#metaMetricsFlags = new Set(metaMetricsFlags);\n this.#clientVersion = clientVersion;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Computes effective feature flags with precedence:\n * defaults < processed remote < local overrides.\n *\n * @param options - The layers to merge. Each defaults to the current layer.\n * @param options.processedRemoteFeatureFlags - The processed remote feature\n * flags. Defaults to the currently resolved remote layer.\n * @param options.localOverrides - Local overrides. Defaults to current state\n * overrides.\n * @returns The effective feature flags.\n */\n #getEffectiveFeatureFlags({\n processedRemoteFeatureFlags = this.#processedRemoteFeatureFlags,\n localOverrides = this.state.localOverrides,\n }: {\n processedRemoteFeatureFlags?: FeatureFlags;\n localOverrides?: FeatureFlags;\n } = {}): FeatureFlags {\n return {\n ...this.#defaultFeatureFlags,\n ...processedRemoteFeatureFlags,\n ...localOverrides,\n };\n }\n\n /**\n * Checks if the cached feature flags are expired based on the fetch interval.\n *\n * @returns Whether the cache is expired (`true`) or still valid (`false`).\n */\n #isCacheExpired(): boolean {\n return Date.now() - this.state.cacheTimestamp > this.#fetchInterval;\n }\n\n /**\n * Retrieves the remote feature flags, fetching from the API if necessary.\n * Uses caching to prevent redundant API calls and handles concurrent fetches.\n *\n * @param force - When `true`, fetch even if the cache has not expired.\n * Defaults to `false`. Has no effect when the controller is disabled.\n * @returns A promise that resolves to the current set of feature flags.\n */\n async updateRemoteFeatureFlags(force = false): Promise<void> {\n if (this.#disabled || (!force && !this.#isCacheExpired())) {\n return;\n }\n\n let serverData;\n\n if (this.#inProgressFlagUpdate) {\n await this.#inProgressFlagUpdate;\n return;\n }\n\n try {\n this.#inProgressFlagUpdate = this.messenger.call(\n 'ClientConfigApiService:fetchRemoteFeatureFlags',\n );\n\n serverData = await this.#inProgressFlagUpdate;\n } finally {\n this.#inProgressFlagUpdate = undefined;\n }\n\n await this.#updateCache(serverData.remoteFeatureFlags);\n }\n\n /**\n * Computes the effective feature flags, re-deriving the remote layer from the\n * raw flags already in state. Threshold selection needs to await a hash and\n * so cannot run in the constructor, which is why this cannot be part of\n * construction. Clients must call this once after constructing the\n * controller.\n *\n * When there are no persisted raw flags, as on a fresh install or for state\n * persisted before raw flags were stored, the previous session's flags stand\n * in for the remote layer so that nothing is lost.\n */\n async init(): Promise<void> {\n const { rawRemoteFeatureFlags } = this.state;\n const hasRawRemoteFeatureFlags =\n rawRemoteFeatureFlags && Object.keys(rawRemoteFeatureFlags).length > 0;\n\n const resolved = hasRawRemoteFeatureFlags\n ? await this.#processRemoteFeatureFlags(rawRemoteFeatureFlags)\n : undefined;\n\n this.#processedRemoteFeatureFlags =\n resolved?.processedFlags ?? this.state.remoteFeatureFlags;\n\n this.update(() => {\n return {\n ...this.state,\n remoteFeatureFlags: this.#getEffectiveFeatureFlags(),\n ...(resolved && {\n thresholdCache: resolved.thresholdCache,\n featureFlagThresholdGroups: resolved.featureFlagThresholdGroups,\n }),\n };\n });\n }\n\n /**\n * Updates the controller's state with new feature flags and resets the cache timestamp.\n *\n * @param remoteFeatureFlags - The new feature flags to cache.\n */\n async #updateCache(remoteFeatureFlags: FeatureFlags): Promise<void> {\n const resolved = await this.#processRemoteFeatureFlags(remoteFeatureFlags);\n\n this.#processedRemoteFeatureFlags = resolved.processedFlags;\n\n // Single state update with all changes batched together\n this.update(() => {\n return {\n ...this.state,\n remoteFeatureFlags: this.#getEffectiveFeatureFlags({\n processedRemoteFeatureFlags: resolved.processedFlags,\n }),\n rawRemoteFeatureFlags: remoteFeatureFlags,\n cacheTimestamp: Date.now(),\n thresholdCache: resolved.thresholdCache,\n featureFlagThresholdGroups: resolved.featureFlagThresholdGroups,\n };\n });\n }\n\n /**\n * Processes a version-based feature flag to get the appropriate value for the current client version.\n *\n * @param flagValue - The feature flag value to process\n * @returns The processed value, or null if no version qualifies (skip this flag)\n */\n #processVersionBasedFlag(flagValue: Json): Json | null {\n if (!isVersionFeatureFlag(flagValue)) {\n return flagValue;\n }\n\n return getVersionData(flagValue, this.#clientVersion);\n }\n\n /**\n * Selects the identifier used to bucket a threshold flag. Flags named in\n * `metaMetricsFlags` segment by MetaMetrics ID; all others segment by\n * canonical profile ID.\n *\n * @param featureFlagName - The name of the feature flag being processed.\n * @returns The segmentation identifier, which may be empty when unavailable.\n */\n #getSegmentationId(featureFlagName: string): string {\n if (this.#metaMetricsFlags.has(featureFlagName)) {\n return this.#getMetaMetricsId();\n }\n return this.#getCanonicalProfileId();\n }\n\n /**\n * Resolves raw feature flags into the values that apply to this client and\n * user, selecting version and threshold entries and reconciling the\n * threshold cache against the flags the server currently serves.\n *\n * @param remoteFeatureFlags - The unprocessed feature flags.\n * @returns The processed flags, the updated threshold cache, and the\n * selected threshold group names.\n */\n async #processRemoteFeatureFlags(remoteFeatureFlags: FeatureFlags): Promise<{\n processedFlags: FeatureFlags;\n thresholdCache: Record<string, number>;\n featureFlagThresholdGroups: Record<string, string>;\n }> {\n const processedFlags: FeatureFlags = {};\n const metaMetricsId = this.#getMetaMetricsId();\n const thresholdCacheUpdates: Record<string, number> = {};\n const featureFlagThresholdGroups: Record<string, string> = {};\n\n for (const [\n remoteFeatureFlagName,\n remoteFeatureFlagValue,\n ] of Object.entries(remoteFeatureFlags)) {\n let processedValue = this.#processVersionBasedFlag(\n remoteFeatureFlagValue,\n );\n if (processedValue === null) {\n continue;\n }\n\n if (Array.isArray(processedValue)) {\n // Validate array has valid threshold items before doing expensive crypto operation\n const hasValidThresholds = processedValue.some(\n isFeatureFlagWithScopeValue,\n );\n\n if (!hasValidThresholds) {\n // Not a threshold array - preserve as-is\n processedFlags[remoteFeatureFlagName] = processedValue;\n continue;\n }\n\n // Explicit-ID matching: check before hash-based threshold, bypasses cache\n const normalizedMetaMetricsId = metaMetricsId.trim().toLowerCase();\n const explicitMatch = normalizedMetaMetricsId\n ? findExplicitIdMatch(processedValue, normalizedMetaMetricsId)\n : undefined;\n\n if (explicitMatch) {\n processedValue = explicitMatch.value;\n if (explicitMatch.name) {\n featureFlagThresholdGroups[remoteFeatureFlagName] =\n explicitMatch.name;\n }\n } else {\n const segmentationId = this.#getSegmentationId(remoteFeatureFlagName);\n\n if (!segmentationId) {\n // The identifier needed to group this flag is unavailable, as it\n // is when `init` runs before whatever backs these getters is\n // ready. Exposing the unresolved threshold array would\n // flip the flag for the end user until the next fetch.\n if (\n hasProperty(\n this.#processedRemoteFeatureFlags,\n remoteFeatureFlagName,\n )\n ) {\n processedFlags[remoteFeatureFlagName] =\n this.#processedRemoteFeatureFlags[remoteFeatureFlagName];\n // Keep the group name alongside the value it was selected with,\n // so consumers segmenting on it do not see a value with no group.\n const previousGroup =\n this.state.featureFlagThresholdGroups?.[remoteFeatureFlagName];\n if (previousGroup) {\n featureFlagThresholdGroups[remoteFeatureFlagName] =\n previousGroup;\n }\n continue;\n }\n\n // No previous value to preserve, as on a fresh install.\n processedFlags[remoteFeatureFlagName] = processedValue;\n continue;\n }\n\n // Fall back to hash-based threshold selection with cache\n const cacheKey =\n `${segmentationId}:${remoteFeatureFlagName}` as const;\n let thresholdValue = this.state.thresholdCache?.[cacheKey];\n\n if (thresholdValue === undefined) {\n thresholdValue = await calculateThresholdForFlag(\n segmentationId,\n remoteFeatureFlagName,\n );\n\n // Collect new threshold for batched state update\n thresholdCacheUpdates[cacheKey] = thresholdValue;\n }\n\n const threshold = thresholdValue;\n const selectedGroup = processedValue.find(\n (featureFlag): featureFlag is FeatureFlagScopeValue => {\n if (!isFeatureFlagWithScopeValue(featureFlag)) {\n return false;\n }\n\n return threshold <= featureFlag.scope.value;\n },\n );\n\n if (selectedGroup) {\n processedValue = selectedGroup.value;\n if (selectedGroup.name) {\n featureFlagThresholdGroups[remoteFeatureFlagName] =\n selectedGroup.name;\n }\n }\n }\n }\n\n processedFlags[remoteFeatureFlagName] = processedValue;\n }\n\n const thresholdCache = {\n ...this.state.thresholdCache,\n ...thresholdCacheUpdates,\n };\n\n // Drop cached thresholds for flags this user is no longer served, under\n // either identifier they may have been bucketed by.\n const canonicalProfileId = this.#getCanonicalProfileId();\n const currentFlagNames = Object.keys(remoteFeatureFlags);\n for (const cacheKey of Object.keys(thresholdCache)) {\n const [cachedSegmentationId, ...cachedFlagNameParts] =\n cacheKey.split(':');\n const cachedFlagName = cachedFlagNameParts.join(':');\n if (\n (cachedSegmentationId === metaMetricsId ||\n cachedSegmentationId === canonicalProfileId) &&\n !currentFlagNames.includes(cachedFlagName)\n ) {\n delete thresholdCache[cacheKey];\n }\n }\n\n return {\n processedFlags,\n thresholdCache,\n featureFlagThresholdGroups,\n };\n }\n\n /**\n * Enables the controller, allowing it to make network requests.\n */\n enable(): void {\n this.#disabled = false;\n }\n\n /**\n * Disables the controller, preventing it from making network requests.\n */\n disable(): void {\n this.#disabled = true;\n }\n\n /**\n * Sets a local override for a specific feature flag.\n *\n * @param flagName - The name of the feature flag to override.\n * @param value - The override value for the feature flag.\n */\n setFlagOverride(flagName: string, value: Json): void {\n this.update(() => {\n const localOverrides = {\n ...this.state.localOverrides,\n [flagName]: value,\n };\n\n return {\n ...this.state,\n localOverrides,\n remoteFeatureFlags: this.#getEffectiveFeatureFlags({ localOverrides }),\n };\n });\n }\n\n /**\n * Clears the local override for a specific feature flag.\n *\n * @param flagName - The name of the feature flag to clear.\n */\n removeFlagOverride(flagName: string): void {\n const newLocalOverrides = { ...this.state.localOverrides };\n delete newLocalOverrides[flagName];\n\n this.update(() => {\n return {\n ...this.state,\n localOverrides: newLocalOverrides,\n remoteFeatureFlags: this.#getEffectiveFeatureFlags({\n localOverrides: newLocalOverrides,\n }),\n };\n });\n }\n\n /**\n * Clears all local feature flag overrides.\n */\n clearAllFlagOverrides(): void {\n this.update(() => {\n return {\n ...this.state,\n localOverrides: {},\n remoteFeatureFlags: this.#getEffectiveFeatureFlags({\n localOverrides: {},\n }),\n };\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"remote-feature-flag-controller.js","sourceRoot":"","sources":["../src/remote-feature-flag-controller.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAEf,MAAM,2BAA2B,CAAC;AAGnC,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAUpE,OAAO,EACL,yBAAyB,EACzB,2BAA2B,GAC5B,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAE1E,kBAAkB;AAElB,MAAM,CAAC,MAAM,cAAc,GAAG,6BAA6B,CAAC;AAC5D,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,QAAQ;AAanE,MAAM,mCAAmC,GAAG;IAC1C,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IAC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AC,MAAM,CAAC,GAAG,EAAE;YACf,MAAM,cAAc,GAAG;gBACrB,GAAG,IAAI,CAAC,KAAK,CAAC,cAAc;gBAC5B,CAAC,QAAQ,CAAC,EAAE,KAAK;aAClB,CAAC;YAEF,OAAO;gBACL,GAAG,IAAI,CAAC,KAAK;gBACb,cAAc;gBACd,kBAAkB,EAAE,IAAI,CAAC,yBAAyB,CAAC,EAAE,cAAc,EAAE,CAAC;aACvE,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,kBAAkB,CAAC,QAAgB;QACjC,MAAM,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,cAAc,EAAE,CAAC;QAC3D,OAAO,iBAAiB,CAAC,QAAQ,CAAC,CAAC;QAEnC,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;YACf,OAAO;gBACL,GAAG,IAAI,CAAC,KAAK;gBACb,cAAc,EAAE,iBAAiB;gBACjC,kBAAkB,EAAE,IAAI,CAAC,yBAAyB,CAAC;oBACjD,cAAc,EAAE,iBAAiB;iBAClC,CAAC;aACH,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,qBAAqB;QACnB,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;YACf,OAAO;gBACL,GAAG,IAAI,CAAC,KAAK;gBACb,cAAc,EAAE,EAAE;gBAClB,kBAAkB,EAAE,IAAI,CAAC,yBAAyB,CAAC;oBACjD,cAAc,EAAE,EAAE;iBACnB,CAAC;aACH,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import {\n BaseController,\n ControllerGetStateAction,\n} from '@metamask/base-controller';\nimport type { ControllerStateChangeEvent } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport { hasProperty, isValidSemVerVersion } from '@metamask/utils';\nimport type { Json, SemVerVersion } from '@metamask/utils';\n\nimport type { AbstractClientConfigApiService } from './client-config-api-service/abstract-client-config-api-service.js';\nimport type { RemoteFeatureFlagControllerMethodActions } from './remote-feature-flag-controller-method-action-types.js';\nimport type {\n FeatureFlags,\n ServiceResponse,\n FeatureFlagScopeValue,\n} from './remote-feature-flag-controller-types.js';\nimport {\n calculateThresholdForFlag,\n isFeatureFlagWithScopeValue,\n} from './utils/user-segmentation-utils.js';\nimport { isVersionFeatureFlag, getVersionData } from './utils/version.js';\n\n// === GENERAL ===\n\nexport const controllerName = 'RemoteFeatureFlagController';\nexport const DEFAULT_CACHE_DURATION = 24 * 60 * 60 * 1000; // 1 day\n\n// === STATE ===\n\nexport type RemoteFeatureFlagControllerState = {\n remoteFeatureFlags: FeatureFlags;\n localOverrides?: FeatureFlags;\n rawRemoteFeatureFlags?: FeatureFlags;\n cacheTimestamp: number;\n thresholdCache?: Record<string, number>;\n featureFlagThresholdGroups?: Record<string, string>;\n};\n\nconst remoteFeatureFlagControllerMetadata = {\n remoteFeatureFlags: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n localOverrides: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n rawRemoteFeatureFlags: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n cacheTimestamp: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n thresholdCache: {\n includeInStateLogs: false,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: false,\n },\n featureFlagThresholdGroups: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n};\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'clearAllFlagOverrides',\n 'disable',\n 'enable',\n 'removeFlagOverride',\n 'setFlagOverride',\n 'updateRemoteFeatureFlags',\n] as const;\n\nexport type RemoteFeatureFlagControllerGetStateAction =\n ControllerGetStateAction<\n typeof controllerName,\n RemoteFeatureFlagControllerState\n >;\n\nexport type RemoteFeatureFlagControllerActions =\n | RemoteFeatureFlagControllerGetStateAction\n | RemoteFeatureFlagControllerMethodActions;\n\nexport type RemoteFeatureFlagControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n RemoteFeatureFlagControllerState\n >;\n\nexport type RemoteFeatureFlagControllerEvents =\n RemoteFeatureFlagControllerStateChangeEvent;\n\nexport type RemoteFeatureFlagControllerMessenger = Messenger<\n typeof controllerName,\n RemoteFeatureFlagControllerActions,\n RemoteFeatureFlagControllerEvents\n>;\n\n/**\n * Returns the default state for the RemoteFeatureFlagController.\n *\n * @returns The default controller state.\n */\nexport function getDefaultRemoteFeatureFlagControllerState(): RemoteFeatureFlagControllerState {\n return {\n remoteFeatureFlags: {},\n localOverrides: {},\n rawRemoteFeatureFlags: {},\n cacheTimestamp: 0,\n };\n}\n\n/**\n * Searches threshold entries for an explicit MetaMetrics ID match.\n * Returns the first entry whose `metaMetricsIds` list contains the given\n * normalized ID. Entries with malformed `metaMetricsIds` (not an array) are\n * skipped without throwing.\n *\n * @param entries - The array of raw threshold entries for a feature flag.\n * @param normalizedId - The current user's MetaMetrics ID, already trimmed and\n * lower-cased.\n * @returns The first matching entry, or `undefined` if none match.\n */\nfunction findExplicitIdMatch(\n entries: Json[],\n normalizedId: string,\n): FeatureFlagScopeValue | undefined {\n for (const entry of entries) {\n if (!isFeatureFlagWithScopeValue(entry)) {\n continue;\n }\n const { metaMetricsIds } = entry;\n if (!Array.isArray(metaMetricsIds)) {\n continue;\n }\n const hasMatch = metaMetricsIds.some(\n (id) =>\n typeof id === 'string' && id.trim().toLowerCase() === normalizedId,\n );\n if (hasMatch) {\n return entry;\n }\n }\n return undefined;\n}\n\n/**\n * The RemoteFeatureFlagController manages the retrieval and caching of remote feature flags.\n * It fetches feature flags from a remote API, caches them, and provides methods to access\n * and manage these flags. The controller ensures that feature flags are refreshed based on\n * a specified interval and handles cases where the controller is disabled or the network is unavailable.\n */\nexport class RemoteFeatureFlagController extends BaseController<\n typeof controllerName,\n RemoteFeatureFlagControllerState,\n RemoteFeatureFlagControllerMessenger\n> {\n readonly #fetchInterval: number;\n\n #disabled: boolean;\n\n readonly #clientConfigApiService: AbstractClientConfigApiService;\n\n #inProgressFlagUpdate?: Promise<ServiceResponse>;\n\n readonly #getMetaMetricsId: () => string;\n\n readonly #getCanonicalProfileId: () => string;\n\n readonly #metaMetricsFlags: ReadonlySet<string>;\n\n readonly #clientVersion: SemVerVersion;\n\n readonly #defaultFeatureFlags: FeatureFlags;\n\n #processedRemoteFeatureFlags: FeatureFlags;\n\n /**\n * Constructs a new RemoteFeatureFlagController instance.\n *\n * @param options - The controller options.\n * @param options.messenger - The messenger used for communication.\n * @param options.state - The initial state of the controller.\n * @param options.clientConfigApiService - The service instance to fetch remote feature flags.\n * @param options.fetchInterval - The interval in milliseconds before cached flags expire. Defaults to 1 day.\n * @param options.disabled - Determines if the controller should be disabled initially. Defaults to false.\n * @param options.getMetaMetricsId - Returns metaMetricsId.\n * @param options.getCanonicalProfileId - Returns the canonical profile identifier used for threshold flags by default. Must return an empty string, rather than throwing, when the identifier is unavailable.\n * @param options.metaMetricsFlags - Names of feature flags that should use MetaMetrics ID for threshold assignment.\n * @param options.clientVersion - The current client version for version-based feature flag filtering. Must be a valid 3-part SemVer version string.\n * @param options.prevClientVersion - The previous client version for feature flag cache invalidation.\n * @param options.defaultFeatureFlags - Client-side default feature flags used as the lowest-precedence layer under processed remote flags and local overrides. Not persisted.\n */\n constructor({\n messenger,\n state,\n clientConfigApiService,\n fetchInterval = DEFAULT_CACHE_DURATION,\n disabled = false,\n getMetaMetricsId,\n getCanonicalProfileId,\n metaMetricsFlags = [],\n clientVersion,\n prevClientVersion,\n defaultFeatureFlags = {},\n }: {\n messenger: RemoteFeatureFlagControllerMessenger;\n state?: Partial<RemoteFeatureFlagControllerState>;\n clientConfigApiService: AbstractClientConfigApiService;\n getMetaMetricsId: () => string;\n getCanonicalProfileId: () => string;\n metaMetricsFlags?: readonly string[];\n fetchInterval?: number;\n disabled?: boolean;\n clientVersion: string;\n prevClientVersion?: string;\n defaultFeatureFlags?: FeatureFlags;\n }) {\n if (!isValidSemVerVersion(clientVersion)) {\n throw new Error(\n `Invalid clientVersion: \"${clientVersion}\". Must be a valid 3-part SemVer version string`,\n );\n }\n\n const initialState: RemoteFeatureFlagControllerState = {\n ...getDefaultRemoteFeatureFlagControllerState(),\n ...state,\n };\n\n const hasClientVersionChanged =\n isValidSemVerVersion(prevClientVersion) &&\n prevClientVersion !== clientVersion;\n\n super({\n name: controllerName,\n metadata: remoteFeatureFlagControllerMetadata,\n messenger,\n state: {\n ...initialState,\n cacheTimestamp: hasClientVersionChanged\n ? 0\n : initialState.cacheTimestamp,\n },\n });\n\n this.#defaultFeatureFlags = defaultFeatureFlags;\n // Last session's effective flags stand in for the remote layer until\n // `init` re-derives it from the persisted raw flags, or a fetch replaces\n // it. Overrides are layered on top rather than subtracted out, so a remote\n // flag that happens to share an override's value is not lost.\n this.#processedRemoteFeatureFlags = initialState.remoteFeatureFlags;\n this.#fetchInterval = fetchInterval;\n this.#disabled = disabled;\n this.#clientConfigApiService = clientConfigApiService;\n this.#getMetaMetricsId = getMetaMetricsId;\n this.#getCanonicalProfileId = getCanonicalProfileId;\n this.#metaMetricsFlags = new Set(metaMetricsFlags);\n this.#clientVersion = clientVersion;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Computes effective feature flags with precedence:\n * defaults < processed remote < local overrides.\n *\n * @param options - The layers to merge. Each defaults to the current layer.\n * @param options.processedRemoteFeatureFlags - The processed remote feature\n * flags. Defaults to the currently resolved remote layer.\n * @param options.localOverrides - Local overrides. Defaults to current state\n * overrides.\n * @returns The effective feature flags.\n */\n #getEffectiveFeatureFlags({\n processedRemoteFeatureFlags = this.#processedRemoteFeatureFlags,\n localOverrides = this.state.localOverrides,\n }: {\n processedRemoteFeatureFlags?: FeatureFlags;\n localOverrides?: FeatureFlags;\n } = {}): FeatureFlags {\n return {\n ...this.#defaultFeatureFlags,\n ...processedRemoteFeatureFlags,\n ...localOverrides,\n };\n }\n\n /**\n * Checks if the cached feature flags are expired based on the fetch interval.\n *\n * @returns Whether the cache is expired (`true`) or still valid (`false`).\n */\n #isCacheExpired(): boolean {\n return Date.now() - this.state.cacheTimestamp > this.#fetchInterval;\n }\n\n /**\n * Retrieves the remote feature flags, fetching from the API if necessary.\n * Uses caching to prevent redundant API calls and handles concurrent fetches.\n *\n * @param force - When `true`, fetch even if the cache has not expired.\n * Defaults to `false`. Has no effect when the controller is disabled.\n * @returns A promise that resolves to the current set of feature flags.\n */\n async updateRemoteFeatureFlags(force = false): Promise<void> {\n if (this.#disabled || (!force && !this.#isCacheExpired())) {\n return;\n }\n\n let serverData;\n\n if (this.#inProgressFlagUpdate) {\n await this.#inProgressFlagUpdate;\n return;\n }\n\n try {\n this.#inProgressFlagUpdate =\n this.#clientConfigApiService.fetchRemoteFeatureFlags();\n\n serverData = await this.#inProgressFlagUpdate;\n } finally {\n this.#inProgressFlagUpdate = undefined;\n }\n\n await this.#updateCache(serverData.remoteFeatureFlags);\n }\n\n /**\n * Computes the effective feature flags, re-deriving the remote layer from the\n * raw flags already in state. Threshold selection needs to await a hash and\n * so cannot run in the constructor, which is why this cannot be part of\n * construction. Clients must call this once after constructing the\n * controller.\n *\n * When there are no persisted raw flags, as on a fresh install or for state\n * persisted before raw flags were stored, the previous session's flags stand\n * in for the remote layer so that nothing is lost.\n */\n async init(): Promise<void> {\n const { rawRemoteFeatureFlags } = this.state;\n const hasRawRemoteFeatureFlags =\n rawRemoteFeatureFlags && Object.keys(rawRemoteFeatureFlags).length > 0;\n\n const resolved = hasRawRemoteFeatureFlags\n ? await this.#processRemoteFeatureFlags(rawRemoteFeatureFlags)\n : undefined;\n\n this.#processedRemoteFeatureFlags =\n resolved?.processedFlags ?? this.state.remoteFeatureFlags;\n\n this.update(() => {\n return {\n ...this.state,\n remoteFeatureFlags: this.#getEffectiveFeatureFlags(),\n ...(resolved && {\n thresholdCache: resolved.thresholdCache,\n featureFlagThresholdGroups: resolved.featureFlagThresholdGroups,\n }),\n };\n });\n }\n\n /**\n * Updates the controller's state with new feature flags and resets the cache timestamp.\n *\n * @param remoteFeatureFlags - The new feature flags to cache.\n */\n async #updateCache(remoteFeatureFlags: FeatureFlags): Promise<void> {\n const resolved = await this.#processRemoteFeatureFlags(remoteFeatureFlags);\n\n this.#processedRemoteFeatureFlags = resolved.processedFlags;\n\n // Single state update with all changes batched together\n this.update(() => {\n return {\n ...this.state,\n remoteFeatureFlags: this.#getEffectiveFeatureFlags({\n processedRemoteFeatureFlags: resolved.processedFlags,\n }),\n rawRemoteFeatureFlags: remoteFeatureFlags,\n cacheTimestamp: Date.now(),\n thresholdCache: resolved.thresholdCache,\n featureFlagThresholdGroups: resolved.featureFlagThresholdGroups,\n };\n });\n }\n\n /**\n * Processes a version-based feature flag to get the appropriate value for the current client version.\n *\n * @param flagValue - The feature flag value to process\n * @returns The processed value, or null if no version qualifies (skip this flag)\n */\n #processVersionBasedFlag(flagValue: Json): Json | null {\n if (!isVersionFeatureFlag(flagValue)) {\n return flagValue;\n }\n\n return getVersionData(flagValue, this.#clientVersion);\n }\n\n /**\n * Selects the identifier used to bucket a threshold flag. Flags named in\n * `metaMetricsFlags` segment by MetaMetrics ID; all others segment by\n * canonical profile ID.\n *\n * @param featureFlagName - The name of the feature flag being processed.\n * @returns The segmentation identifier, which may be empty when unavailable.\n */\n #getSegmentationId(featureFlagName: string): string {\n if (this.#metaMetricsFlags.has(featureFlagName)) {\n return this.#getMetaMetricsId();\n }\n return this.#getCanonicalProfileId();\n }\n\n /**\n * Resolves raw feature flags into the values that apply to this client and\n * user, selecting version and threshold entries and reconciling the\n * threshold cache against the flags the server currently serves.\n *\n * @param remoteFeatureFlags - The unprocessed feature flags.\n * @returns The processed flags, the updated threshold cache, and the\n * selected threshold group names.\n */\n async #processRemoteFeatureFlags(remoteFeatureFlags: FeatureFlags): Promise<{\n processedFlags: FeatureFlags;\n thresholdCache: Record<string, number>;\n featureFlagThresholdGroups: Record<string, string>;\n }> {\n const processedFlags: FeatureFlags = {};\n const metaMetricsId = this.#getMetaMetricsId();\n const thresholdCacheUpdates: Record<string, number> = {};\n const featureFlagThresholdGroups: Record<string, string> = {};\n\n for (const [\n remoteFeatureFlagName,\n remoteFeatureFlagValue,\n ] of Object.entries(remoteFeatureFlags)) {\n let processedValue = this.#processVersionBasedFlag(\n remoteFeatureFlagValue,\n );\n if (processedValue === null) {\n continue;\n }\n\n if (Array.isArray(processedValue)) {\n // Validate array has valid threshold items before doing expensive crypto operation\n const hasValidThresholds = processedValue.some(\n isFeatureFlagWithScopeValue,\n );\n\n if (!hasValidThresholds) {\n // Not a threshold array - preserve as-is\n processedFlags[remoteFeatureFlagName] = processedValue;\n continue;\n }\n\n // Explicit-ID matching: check before hash-based threshold, bypasses cache\n const normalizedMetaMetricsId = metaMetricsId.trim().toLowerCase();\n const explicitMatch = normalizedMetaMetricsId\n ? findExplicitIdMatch(processedValue, normalizedMetaMetricsId)\n : undefined;\n\n if (explicitMatch) {\n processedValue = explicitMatch.value;\n if (explicitMatch.name) {\n featureFlagThresholdGroups[remoteFeatureFlagName] =\n explicitMatch.name;\n }\n } else {\n const segmentationId = this.#getSegmentationId(remoteFeatureFlagName);\n\n if (!segmentationId) {\n // The identifier needed to group this flag is unavailable, as it\n // is when `init` runs before whatever backs these getters is\n // ready. Exposing the unresolved threshold array would\n // flip the flag for the end user until the next fetch.\n if (\n hasProperty(\n this.#processedRemoteFeatureFlags,\n remoteFeatureFlagName,\n )\n ) {\n processedFlags[remoteFeatureFlagName] =\n this.#processedRemoteFeatureFlags[remoteFeatureFlagName];\n // Keep the group name alongside the value it was selected with,\n // so consumers segmenting on it do not see a value with no group.\n const previousGroup =\n this.state.featureFlagThresholdGroups?.[remoteFeatureFlagName];\n if (previousGroup) {\n featureFlagThresholdGroups[remoteFeatureFlagName] =\n previousGroup;\n }\n continue;\n }\n\n // No previous value to preserve, as on a fresh install.\n processedFlags[remoteFeatureFlagName] = processedValue;\n continue;\n }\n\n // Fall back to hash-based threshold selection with cache\n const cacheKey =\n `${segmentationId}:${remoteFeatureFlagName}` as const;\n let thresholdValue = this.state.thresholdCache?.[cacheKey];\n\n if (thresholdValue === undefined) {\n thresholdValue = await calculateThresholdForFlag(\n segmentationId,\n remoteFeatureFlagName,\n );\n\n // Collect new threshold for batched state update\n thresholdCacheUpdates[cacheKey] = thresholdValue;\n }\n\n const threshold = thresholdValue;\n const selectedGroup = processedValue.find(\n (featureFlag): featureFlag is FeatureFlagScopeValue => {\n if (!isFeatureFlagWithScopeValue(featureFlag)) {\n return false;\n }\n\n return threshold <= featureFlag.scope.value;\n },\n );\n\n if (selectedGroup) {\n processedValue = selectedGroup.value;\n if (selectedGroup.name) {\n featureFlagThresholdGroups[remoteFeatureFlagName] =\n selectedGroup.name;\n }\n }\n }\n }\n\n processedFlags[remoteFeatureFlagName] = processedValue;\n }\n\n const thresholdCache = {\n ...this.state.thresholdCache,\n ...thresholdCacheUpdates,\n };\n\n // Drop cached thresholds for flags this user is no longer served, under\n // either identifier they may have been bucketed by.\n const canonicalProfileId = this.#getCanonicalProfileId();\n const currentFlagNames = Object.keys(remoteFeatureFlags);\n for (const cacheKey of Object.keys(thresholdCache)) {\n const [cachedSegmentationId, ...cachedFlagNameParts] =\n cacheKey.split(':');\n const cachedFlagName = cachedFlagNameParts.join(':');\n if (\n (cachedSegmentationId === metaMetricsId ||\n cachedSegmentationId === canonicalProfileId) &&\n !currentFlagNames.includes(cachedFlagName)\n ) {\n delete thresholdCache[cacheKey];\n }\n }\n\n return {\n processedFlags,\n thresholdCache,\n featureFlagThresholdGroups,\n };\n }\n\n /**\n * Enables the controller, allowing it to make network requests.\n */\n enable(): void {\n this.#disabled = false;\n }\n\n /**\n * Disables the controller, preventing it from making network requests.\n */\n disable(): void {\n this.#disabled = true;\n }\n\n /**\n * Sets a local override for a specific feature flag.\n *\n * @param flagName - The name of the feature flag to override.\n * @param value - The override value for the feature flag.\n */\n setFlagOverride(flagName: string, value: Json): void {\n this.update(() => {\n const localOverrides = {\n ...this.state.localOverrides,\n [flagName]: value,\n };\n\n return {\n ...this.state,\n localOverrides,\n remoteFeatureFlags: this.#getEffectiveFeatureFlags({ localOverrides }),\n };\n });\n }\n\n /**\n * Clears the local override for a specific feature flag.\n *\n * @param flagName - The name of the feature flag to clear.\n */\n removeFlagOverride(flagName: string): void {\n const newLocalOverrides = { ...this.state.localOverrides };\n delete newLocalOverrides[flagName];\n\n this.update(() => {\n return {\n ...this.state,\n localOverrides: newLocalOverrides,\n remoteFeatureFlags: this.#getEffectiveFeatureFlags({\n localOverrides: newLocalOverrides,\n }),\n };\n });\n }\n\n /**\n * Clears all local feature flag overrides.\n */\n clearAllFlagOverrides(): void {\n this.update(() => {\n return {\n ...this.state,\n localOverrides: {},\n remoteFeatureFlags: this.#getEffectiveFeatureFlags({\n localOverrides: {},\n }),\n };\n });\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/remote-feature-flag-controller",
|
|
3
|
-
"version": "7.0.0-preview-
|
|
3
|
+
"version": "7.0.0-preview-a26804c",
|
|
4
4
|
"description": "The RemoteFeatureFlagController manages the retrieval and caching of remote feature flags",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ethereum",
|
|
@@ -51,10 +51,8 @@
|
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"@metamask/base-controller": "^10.1.0",
|
|
54
|
-
"@metamask/base-data-service": "^2.1.0",
|
|
55
54
|
"@metamask/controller-utils": "^13.0.0",
|
|
56
55
|
"@metamask/messenger": "^3.0.0",
|
|
57
|
-
"@metamask/superstruct": "^3.4.1",
|
|
58
56
|
"@metamask/utils": "^12.0.1",
|
|
59
57
|
"uuid": "^14.0.0"
|
|
60
58
|
},
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is auto generated.
|
|
3
|
-
* Do not edit manually.
|
|
4
|
-
*/
|
|
5
|
-
import type { ClientConfigApiService } from './client-config-api-service.js';
|
|
6
|
-
/**
|
|
7
|
-
* Fetches feature flags from the API with specific client, distribution, and
|
|
8
|
-
* environment parameters. Always requests fresh data from the network, and
|
|
9
|
-
* concurrent calls share a single request. Throws if the request fails or
|
|
10
|
-
* the response is not an array of flag objects.
|
|
11
|
-
*
|
|
12
|
-
* @returns The feature flags merged into a single object, and the time they
|
|
13
|
-
* were fetched.
|
|
14
|
-
*/
|
|
15
|
-
export type ClientConfigApiServiceFetchRemoteFeatureFlagsAction = {
|
|
16
|
-
type: `ClientConfigApiService:fetchRemoteFeatureFlags`;
|
|
17
|
-
handler: ClientConfigApiService['fetchRemoteFeatureFlags'];
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* Union of all ClientConfigApiService action types.
|
|
21
|
-
*/
|
|
22
|
-
export type ClientConfigApiServiceMethodActions = ClientConfigApiServiceFetchRemoteFeatureFlagsAction;
|
|
23
|
-
//# sourceMappingURL=client-config-api-service-method-action-types.d.ts.map
|
package/dist/client-config-api-service/client-config-api-service-method-action-types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client-config-api-service-method-action-types.d.ts","sourceRoot":"","sources":["../../src/client-config-api-service/client-config-api-service-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AAE7E;;;;;;;;GAQG;AACH,MAAM,MAAM,mDAAmD,GAAG;IAChE,IAAI,EAAE,gDAAgD,CAAC;IACvD,OAAO,EAAE,sBAAsB,CAAC,yBAAyB,CAAC,CAAC;CAC5D,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAC7C,mDAAmD,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client-config-api-service-method-action-types.js","sourceRoot":"","sources":["../../src/client-config-api-service/client-config-api-service-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { ClientConfigApiService } from './client-config-api-service.js';\n\n/**\n * Fetches feature flags from the API with specific client, distribution, and\n * environment parameters. Always requests fresh data from the network, and\n * concurrent calls share a single request. Throws if the request fails or\n * the response is not an array of flag objects.\n *\n * @returns The feature flags merged into a single object, and the time they\n * were fetched.\n */\nexport type ClientConfigApiServiceFetchRemoteFeatureFlagsAction = {\n type: `ClientConfigApiService:fetchRemoteFeatureFlags`;\n handler: ClientConfigApiService['fetchRemoteFeatureFlags'];\n};\n\n/**\n * Union of all ClientConfigApiService action types.\n */\nexport type ClientConfigApiServiceMethodActions =\n ClientConfigApiServiceFetchRemoteFeatureFlagsAction;\n"]}
|