-
Notifications
You must be signed in to change notification settings - Fork 1
infra: add support for decoding configuration hostnames from SDK keys… #252
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,70 @@ | ||
import { Base64 } from 'js-base64'; | ||
|
||
const PATH = 'v0/i'; | ||
const EVENT_INGESTION_HOSTNAME_KEY = 'eh'; | ||
const CONFIGURATION_HOSTNAME_KEY = 'ch'; | ||
|
||
const EVENT_INGESTION_PATH = 'v0/i'; | ||
const ASSIGNMENT_CONFIG_PATH = 'assignment'; | ||
const EDGE_CONFIG_PATH = 'edge'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems this is intended to follow the same pattern used for event ingestion. The |
||
|
||
export default class SdkKeyDecoder { | ||
/** | ||
* Decodes and returns the event ingestion hostname from the provided Eppo SDK key string. | ||
* If the SDK key doesn't contain the event ingestion hostname, or it's invalid, it returns null. | ||
*/ | ||
decodeEventIngestionUrl(sdkKey: string): string | null { | ||
return this.decodeHostnames(sdkKey, EVENT_INGESTION_PATH).eventIngestionHostname; | ||
} | ||
|
||
/** | ||
* Decodes and returns the configuration hostname from the provided Eppo SDK key string. | ||
* If the SDK key doesn't contain the configuration hostname, or it's invalid, it returns null. | ||
*/ | ||
decodeAssignmentConfigurationUrl(sdkKey: string): string | null { | ||
return this.decodeHostnames(sdkKey, ASSIGNMENT_CONFIG_PATH).configurationHostname; | ||
} | ||
|
||
/** | ||
* Decodes and returns the edge configuration hostname from the provided Eppo SDK key string. | ||
* If the SDK key doesn't contain the edge configuration hostname, or it's invalid, it returns null. | ||
*/ | ||
decodeEdgeConfigurationUrl(sdkKey: string): string | null { | ||
return this.decodeHostnames(sdkKey, EDGE_CONFIG_PATH).configurationHostname; | ||
} | ||
Comment on lines
+19
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking into the future a bit: the clients allow for a |
||
|
||
private decodeHostnames( | ||
sdkKey: string, | ||
path: string, | ||
): { | ||
eventIngestionHostname: string | null; | ||
configurationHostname: string | null; | ||
} { | ||
const encodedPayload = sdkKey.split('.')[1]; | ||
if (!encodedPayload) return null; | ||
if (!encodedPayload) return { eventIngestionHostname: null, configurationHostname: null }; | ||
|
||
const decodedPayload = Base64.decode(encodedPayload); | ||
const params = new URLSearchParams(decodedPayload); | ||
const hostname = params.get('eh'); | ||
if (!hostname) return null; | ||
|
||
const hostAndPath = hostname.endsWith('/') ? `${hostname}${PATH}` : `${hostname}/${PATH}`; | ||
if (!hostAndPath.startsWith('http://') && !hostAndPath.startsWith('https://')) { | ||
// prefix hostname with https scheme if none present | ||
return `https://${hostAndPath}`; | ||
} else { | ||
return hostAndPath; | ||
const eventIngestionHostname = params.get(EVENT_INGESTION_HOSTNAME_KEY); | ||
const configurationHostname = params.get(CONFIGURATION_HOSTNAME_KEY); | ||
|
||
return { | ||
eventIngestionHostname: eventIngestionHostname | ||
? this.ensureHttps(this.ensurePath(eventIngestionHostname, path)) | ||
: null, | ||
configurationHostname: configurationHostname | ||
? this.ensureHttps(this.ensurePath(configurationHostname, path)) | ||
: null, | ||
}; | ||
} | ||
|
||
private ensureHttps(hostname: string): string { | ||
if (!hostname.startsWith('http://') && !hostname.startsWith('https://')) { | ||
return `https://${hostname}`; | ||
} | ||
return hostname; | ||
} | ||
|
||
private ensurePath(hostname: string, path: string): string { | ||
return hostname.endsWith('/') ? `${hostname}${path}` : `${hostname}/${path}`; | ||
} | ||
} |
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.
@typotter Since I got here first 😉 , I selected
ch
(forconfiguration host
). Please reflect this with the server changes.