-
Notifications
You must be signed in to change notification settings - Fork 1
feat: server overrides #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,141 @@ | ||
import { MemoryOnlyConfigurationStore } from '../configuration-store/memory.store'; | ||
import { Flag, FormatEnum, ObfuscatedFlag, VariationType } from '../interfaces'; | ||
import * as overrideValidatorModule from '../override-validator'; | ||
|
||
import EppoClient from './eppo-client'; | ||
|
||
describe('EppoClient', () => { | ||
const storage = new MemoryOnlyConfigurationStore<Flag | ObfuscatedFlag>(); | ||
|
||
function setUnobfuscatedFlagEntries( | ||
entries: Record<string, Flag | ObfuscatedFlag>, | ||
): Promise<boolean> { | ||
storage.setFormat(FormatEnum.SERVER); | ||
return storage.setEntries(entries); | ||
} | ||
|
||
const flagKey = 'mock-flag'; | ||
|
||
const variationA = { | ||
key: 'a', | ||
value: 'variation-a', | ||
}; | ||
|
||
const variationB = { | ||
key: 'b', | ||
value: 'variation-b', | ||
}; | ||
|
||
const mockFlag: Flag = { | ||
key: flagKey, | ||
enabled: true, | ||
variationType: VariationType.STRING, | ||
variations: { a: variationA, b: variationB }, | ||
allocations: [ | ||
{ | ||
key: 'allocation-a', | ||
rules: [], | ||
splits: [ | ||
{ | ||
shards: [], | ||
variationKey: 'a', | ||
}, | ||
], | ||
doLog: true, | ||
}, | ||
], | ||
totalShards: 10000, | ||
}; | ||
|
||
let client: EppoClient; | ||
let subjectKey: string; | ||
|
||
beforeEach(async () => { | ||
await setUnobfuscatedFlagEntries({ [flagKey]: mockFlag }); | ||
subjectKey = 'subject-10'; | ||
client = new EppoClient({ flagConfigurationStore: storage }); | ||
}); | ||
|
||
describe('parseOverrides', () => { | ||
it('should parse a valid payload', async () => { | ||
jest.spyOn(overrideValidatorModule, 'sendValidationRequest').mockResolvedValue(undefined); | ||
const result = await client.parseOverrides( | ||
JSON.stringify({ | ||
browserExtensionKey: 'my-key', | ||
overrides: { [flagKey]: variationB }, | ||
}), | ||
); | ||
expect(result).toEqual({ [flagKey]: variationB }); | ||
}); | ||
|
||
it('should throw an error if the key is missing', async () => { | ||
jest.spyOn(overrideValidatorModule, 'sendValidationRequest').mockResolvedValue(undefined); | ||
expect(() => | ||
client.parseOverrides( | ||
JSON.stringify({ | ||
overrides: { [flagKey]: variationB }, | ||
}), | ||
), | ||
).rejects.toThrow(); | ||
}); | ||
|
||
it('should throw an error if the key is not a string', async () => { | ||
jest.spyOn(overrideValidatorModule, 'sendValidationRequest').mockResolvedValue(undefined); | ||
expect(() => | ||
client.parseOverrides( | ||
JSON.stringify({ | ||
browserExtensionKey: 123, | ||
overrides: { [flagKey]: variationB }, | ||
}), | ||
), | ||
).rejects.toThrow(); | ||
}); | ||
|
||
it('should throw an error if the overrides are not parseable', async () => { | ||
jest.spyOn(overrideValidatorModule, 'sendValidationRequest').mockResolvedValue(undefined); | ||
expect(() => | ||
client.parseOverrides(`{ | ||
browserExtensionKey: 'my-key', | ||
overrides: { [flagKey]: , | ||
}`), | ||
).rejects.toThrow(); | ||
}); | ||
|
||
it('should throw an error if overrides is not an object', async () => { | ||
jest.spyOn(overrideValidatorModule, 'sendValidationRequest').mockResolvedValue(undefined); | ||
expect(() => | ||
client.parseOverrides( | ||
JSON.stringify({ | ||
browserExtensionKey: 'my-key', | ||
overrides: false, | ||
}), | ||
), | ||
).rejects.toThrow(); | ||
}); | ||
|
||
it('should throw an error if an invalid key is supplied', async () => { | ||
jest.spyOn(overrideValidatorModule, 'sendValidationRequest').mockImplementation(async () => { | ||
throw new Error(`Unable to authorize key`); | ||
}); | ||
expect(() => | ||
client.parseOverrides( | ||
JSON.stringify({ | ||
browserExtensionKey: 'my-key', | ||
overrides: { [flagKey]: variationB }, | ||
}), | ||
), | ||
).rejects.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('withOverrides', () => { | ||
it('should create a new instance of EppoClient with specified overrides without affecting the original instance', () => { | ||
const clientWithOverrides = client.withOverrides({ [flagKey]: variationB }); | ||
|
||
expect(client.getStringAssignment(flagKey, subjectKey, {}, 'default')).toBe('variation-a'); | ||
expect(clientWithOverrides.getStringAssignment(flagKey, subjectKey, {}, 'default')).toBe( | ||
'variation-b', | ||
); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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
This file contains hidden or 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,73 @@ | ||
import { TLRUCache } from './cache/tlru-cache'; | ||
import { Variation } from './interfaces'; | ||
import { FlagKey } from './types'; | ||
|
||
const FIVE_MINUTES_IN_MS = 5 * 3600 * 1000; | ||
const KEY_VALIDATION_URL = 'https://eppo.cloud/api/flag-overrides/v1/validate-key'; | ||
|
||
export interface OverridePayload { | ||
browserExtensionKey: string; | ||
overrides: Record<FlagKey, Variation>; | ||
} | ||
|
||
export const sendValidationRequest = async (browserExtensionKey: string) => { | ||
const response = await fetch(KEY_VALIDATION_URL, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
key: browserExtensionKey, | ||
}), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
if (response.status !== 200) { | ||
throw new Error(`Unable to authorize key: ${response.statusText}`); | ||
} | ||
}; | ||
|
||
export class OverrideValidator { | ||
private validKeyCache = new TLRUCache(100, FIVE_MINUTES_IN_MS); | ||
|
||
parseOverridePayload(overridePayload: string): OverridePayload { | ||
const errorMsg = (msg: string) => `Unable to parse overridePayload: ${msg}`; | ||
try { | ||
const parsed = JSON.parse(overridePayload); | ||
this.validateParsedOverridePayload(parsed); | ||
return parsed as OverridePayload; | ||
} catch (err: unknown) { | ||
const message: string = (err as any)?.message ?? 'unknown error'; | ||
throw new Error(errorMsg(message)); | ||
} | ||
} | ||
|
||
private validateParsedOverridePayload(parsed: any) { | ||
if (typeof parsed !== 'object') { | ||
throw new Error(`Expected object, but received ${typeof parsed}`); | ||
} | ||
const keys = Object.keys(parsed); | ||
if (!keys.includes('browserExtensionKey')) { | ||
throw new Error(`Missing required field: 'browserExtensionKey'`); | ||
} | ||
if (!keys.includes('overrides')) { | ||
throw new Error(`Missing required field: 'overrides'`); | ||
} | ||
if (typeof parsed['browserExtensionKey'] !== 'string') { | ||
throw new Error( | ||
`Invalid type for 'browserExtensionKey'. Expected string, but received ${typeof parsed['browserExtensionKey']}`, | ||
); | ||
} | ||
if (typeof parsed['overrides'] !== 'object') { | ||
throw new Error( | ||
`Invalid type for 'overrides'. Expected object, but received ${typeof parsed['overrides']}.`, | ||
); | ||
} | ||
} | ||
|
||
async validateKey(browserExtensionKey: string) { | ||
if (this.validKeyCache.get(browserExtensionKey) === 'true') { | ||
return true; | ||
} | ||
await sendValidationRequest(browserExtensionKey); | ||
this.validKeyCache.set(browserExtensionKey, 'true'); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool idea to do this!