-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(27254): implement new remote-feature-flag-controller
- Loading branch information
1 parent
1e7d70e
commit 82f7997
Showing
16 changed files
with
929 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## [0.1.0] | ||
|
||
### Added | ||
|
||
- Initial release | ||
|
||
[Unreleased]: https://github.com/MetaMask/core/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 MetaMask | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# `@metamask/example-controllers` | ||
|
||
This package is designed to illustrate best practices for controller packages and controller files, including tests. | ||
|
||
## Installation | ||
|
||
`yarn add @metamask/example-controllers` | ||
|
||
or | ||
|
||
`npm install @metamask/example-controllers` | ||
|
||
## Contributing | ||
|
||
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* For a detailed explanation regarding each configuration property and type check, visit: | ||
* https://jestjs.io/docs/configuration | ||
*/ | ||
|
||
const merge = require('deepmerge'); | ||
const path = require('path'); | ||
|
||
const baseConfig = require('../../jest.config.packages'); | ||
|
||
const displayName = path.basename(__dirname); | ||
|
||
module.exports = merge(baseConfig, { | ||
// The display name when running multiple projects | ||
displayName, | ||
|
||
// An object that configures minimum threshold enforcement for coverage results | ||
coverageThreshold: { | ||
global: { | ||
branches: 100, | ||
functions: 100, | ||
lines: 100, | ||
statements: 100, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
{ | ||
"name": "@metamask/remote-feature-flag-controller", | ||
"version": "0.0.1", | ||
"private": true, | ||
"description": "Controller with caching, fallback, and privacy for managing feature flags via ClientConfigAPI", | ||
"keywords": [ | ||
"MetaMask", | ||
"Ethereum" | ||
], | ||
"homepage": "https://github.com/MetaMask/core/tree/main/packages/remote-feature-flag-controller#readme", | ||
"bugs": { | ||
"url": "https://github.com/MetaMask/core/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/MetaMask/core.git" | ||
}, | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./dist/index.d.mts", | ||
"default": "./dist/index.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/index.d.cts", | ||
"default": "./dist/index.cjs" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"main": "./dist/index.cjs", | ||
"types": "./dist/index.d.cts", | ||
"files": [ | ||
"dist/" | ||
], | ||
"scripts": { | ||
"build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references", | ||
"build:docs": "typedoc", | ||
"changelog:update": "../../scripts/update-changelog.sh @metamask/remote-feature-flag-controllers", | ||
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/remote-feature-flag-controllers", | ||
"publish:preview": "yarn npm publish --tag preview", | ||
"since-latest-release": "../../scripts/since-latest-release.sh", | ||
"test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", | ||
"test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache", | ||
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose", | ||
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch" | ||
}, | ||
"dependencies": { | ||
"@lavamoat/allow-scripts": "^3.3.0", | ||
"@metamask/base-controller": "^7.0.2", | ||
"@metamask/utils": "^10.0.0" | ||
}, | ||
"devDependencies": { | ||
"@metamask/auto-changelog": "^3.4.4", | ||
"@metamask/controller-utils": "^11.4.3", | ||
"@types/jest": "^27.4.1", | ||
"deepmerge": "^4.2.2", | ||
"jest": "^27.5.1", | ||
"nock": "^13.3.1", | ||
"ts-jest": "^27.1.4", | ||
"typedoc": "^0.24.8", | ||
"typedoc-plugin-missing-exports": "^2.0.0", | ||
"typescript": "~5.2.2" | ||
}, | ||
"engines": { | ||
"node": "^18.18 || >=20" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org/" | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ature-flag-controller/src/client-config-api-service/abstract-client-config-api-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { PublicInterface } from '@metamask/utils'; | ||
|
||
import type { ClientConfigApiService } from './client-config-api-service'; | ||
|
||
/** | ||
* A service object responsible for fetching feature flags. | ||
*/ | ||
export type AbstractClientConfigApiService = | ||
PublicInterface<ClientConfigApiService>; |
192 changes: 192 additions & 0 deletions
192
...e-feature-flag-controller/src/client-config-api-service/client-config-api-service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import type { FeatureFlags } from '../remote-feature-flag-controller-types'; | ||
import { | ||
ClientType, | ||
DistributionType, | ||
EnvironmentType, | ||
} from '../remote-feature-flag-controller-types'; | ||
import { ClientConfigApiService } from './client-config-api-service'; | ||
|
||
const BASE_URL = 'https://client-config.api.cx.metamask.io/v1'; | ||
|
||
describe('ClientConfigApiService', () => { | ||
let originalConsoleError: typeof console.error; | ||
let clientConfigApiService: ClientConfigApiService; | ||
let mockFetch: jest.Mock; | ||
|
||
const mockFeatureFlags: FeatureFlags = { | ||
feature1: false, | ||
feature2: { chrome: '<109' }, | ||
}; | ||
|
||
const networkError = new Error('Network error'); | ||
Object.assign(networkError, { | ||
response: { | ||
status: 503, | ||
statusText: 'Service Unavailable', | ||
}, | ||
}); | ||
|
||
beforeEach(() => { | ||
mockFetch = jest.fn(); | ||
clientConfigApiService = new ClientConfigApiService({ fetch: mockFetch }); | ||
}); | ||
|
||
beforeAll(() => { | ||
originalConsoleError = console.error; | ||
console.error = jest | ||
.spyOn(console, 'error') | ||
.mockImplementation() as unknown as typeof console.error; | ||
}); | ||
|
||
afterAll(() => { | ||
console.error = originalConsoleError; | ||
}); | ||
|
||
describe('fetchFlags', () => { | ||
it('should successfully fetch and return feature flags', async () => { | ||
mockFetch.mockResolvedValueOnce({ | ||
ok: true, | ||
status: 200, | ||
statusText: 'OK', | ||
json: async () => mockFeatureFlags, | ||
}); | ||
|
||
const result = await clientConfigApiService.fetchFlags( | ||
ClientType.Extension, | ||
DistributionType.Main, | ||
EnvironmentType.Production, | ||
); | ||
|
||
expect(mockFetch).toHaveBeenCalledWith( | ||
`${BASE_URL}/flags?client=extension&distribution=main&environment=prod`, | ||
{ cache: 'no-cache' }, | ||
); | ||
|
||
expect(result).toStrictEqual({ | ||
error: false, | ||
message: 'Success', | ||
statusCode: '200', | ||
statusText: 'OK', | ||
cachedData: mockFeatureFlags, | ||
cacheTimestamp: expect.any(Number), | ||
}); | ||
}); | ||
|
||
it('should return cached data when API request fails and cached data is available', async () => { | ||
const cachedData = { feature3: true }; | ||
const cacheTimestamp = Date.now(); | ||
|
||
mockFetch.mockRejectedValueOnce(networkError); | ||
|
||
const result = await clientConfigApiService.fetchFlags( | ||
ClientType.Extension, | ||
DistributionType.Main, | ||
EnvironmentType.Production, | ||
cachedData, | ||
cacheTimestamp, | ||
); | ||
|
||
expect(result).toStrictEqual({ | ||
error: true, | ||
message: 'Network error', | ||
statusCode: '503', | ||
statusText: 'Service Unavailable', | ||
cachedData, | ||
cacheTimestamp, | ||
}); | ||
}); | ||
|
||
it('should return empty object when API request fails and cached data is not available', async () => { | ||
mockFetch.mockRejectedValueOnce(networkError); | ||
const result = await clientConfigApiService.fetchFlags( | ||
ClientType.Extension, | ||
DistributionType.Main, | ||
EnvironmentType.Production, | ||
); | ||
const currentTime = Date.now(); | ||
expect(result).toStrictEqual({ | ||
error: true, | ||
message: 'Network error', | ||
statusCode: '503', | ||
statusText: 'Service Unavailable', | ||
cachedData: {}, | ||
cacheTimestamp: currentTime, | ||
}); | ||
}); | ||
|
||
it('should handle non-200 responses without cache data', async () => { | ||
mockFetch.mockResolvedValueOnce({ | ||
ok: false, | ||
status: 404, | ||
statusText: 'Not Found', | ||
}); | ||
|
||
const result = await clientConfigApiService.fetchFlags( | ||
ClientType.Extension, | ||
DistributionType.Main, | ||
EnvironmentType.Production, | ||
); | ||
const currentTime = Date.now(); | ||
expect(result).toStrictEqual({ | ||
error: true, | ||
message: 'Failed to fetch flags', | ||
statusCode: '404', | ||
statusText: 'Not Found', | ||
cachedData: {}, | ||
cacheTimestamp: currentTime, | ||
}); | ||
}); | ||
|
||
it('should handle non-200 responses with cache data', async () => { | ||
const cachedData = { feature3: true }; | ||
const cacheTimestamp = Date.now(); | ||
mockFetch.mockResolvedValueOnce({ | ||
ok: false, | ||
status: 404, | ||
statusText: 'Not Found', | ||
}); | ||
|
||
const result = await clientConfigApiService.fetchFlags( | ||
ClientType.Extension, | ||
DistributionType.Main, | ||
EnvironmentType.Production, | ||
cachedData, | ||
cacheTimestamp, | ||
); | ||
const currentTime = Date.now(); | ||
expect(result).toStrictEqual({ | ||
error: true, | ||
message: 'Failed to fetch flags', | ||
statusCode: '404', | ||
statusText: 'Not Found', | ||
cachedData, | ||
cacheTimestamp: currentTime, | ||
}); | ||
}); | ||
|
||
it('should handle invalid API responses', async () => { | ||
mockFetch.mockResolvedValueOnce({ | ||
ok: true, | ||
status: 200, | ||
statusText: 'OK', | ||
json: async () => null, // Invalid response | ||
}); | ||
|
||
const result = await clientConfigApiService.fetchFlags( | ||
ClientType.Extension, | ||
DistributionType.Main, | ||
EnvironmentType.Production, | ||
); | ||
|
||
const currentTime = Date.now(); | ||
expect(result).toStrictEqual({ | ||
error: true, | ||
message: 'Invalid API response', | ||
statusCode: null, | ||
statusText: null, | ||
cachedData: {}, | ||
cacheTimestamp: currentTime, | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.