Skip to content

Commit b3fac13

Browse files
authored
fix(events): Prefix event ingestion URL with scheme if none provided (#170)
1 parent c040140 commit b3fac13

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

src/events/default-event-dispatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export function newDefaultEventDispatcher(
137137
config: Omit<EventDispatcherConfig, 'ingestionUrl'> = DEFAULT_EVENT_DISPATCHER_CONFIG,
138138
): EventDispatcher {
139139
const sdkKeyDecoder = new SdkKeyDecoder();
140-
const ingestionUrl = sdkKeyDecoder.decodeEventIngestionHostName(sdkKey);
140+
const ingestionUrl = sdkKeyDecoder.decodeEventIngestionUrl(sdkKey);
141141
if (!ingestionUrl) {
142142
logger.debug(
143143
'Unable to parse Event ingestion URL from SDK key, falling back to no-op event dispatcher',

src/events/sdk-key-decoder.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ import SdkKeyDecoder from './sdk-key-decoder';
33
describe('SdkKeyDecoder', () => {
44
const decoder = new SdkKeyDecoder();
55
it('should decode the event ingestion hostname from the SDK key', () => {
6-
const hostname = decoder.decodeEventIngestionHostName(
6+
const hostname = decoder.decodeEventIngestionUrl(
77
'zCsQuoHJxVPp895.ZWg9MTIzNDU2LmUudGVzdGluZy5lcHBvLmNsb3Vk',
88
);
9-
expect(hostname).toEqual('123456.e.testing.eppo.cloud');
9+
expect(hostname).toEqual('https://123456.e.testing.eppo.cloud');
1010
});
1111

1212
it('should decode strings with non URL-safe characters', () => {
1313
// this is not a really valid ingestion URL, but it's useful for testing the decoder
1414
const invalidUrl = 'eh=12+3456/.e.testing.eppo.cloud';
1515
const encoded = Buffer.from(invalidUrl).toString('base64url');
16-
const hostname = decoder.decodeEventIngestionHostName(`zCsQuoHJxVPp895.${encoded}`);
17-
expect(hostname).toEqual('12 3456/.e.testing.eppo.cloud');
16+
const hostname = decoder.decodeEventIngestionUrl(`zCsQuoHJxVPp895.${encoded}`);
17+
expect(hostname).toEqual('https://12 3456/.e.testing.eppo.cloud');
1818
});
1919

2020
it("should return null if the SDK key doesn't contain the event ingestion hostname", () => {
21-
expect(decoder.decodeEventIngestionHostName('zCsQuoHJxVPp895')).toBeNull();
22-
expect(decoder.decodeEventIngestionHostName('zCsQuoHJxVPp895.xxxxxx')).toBeNull();
21+
expect(decoder.decodeEventIngestionUrl('zCsQuoHJxVPp895')).toBeNull();
22+
expect(decoder.decodeEventIngestionUrl('zCsQuoHJxVPp895.xxxxxx')).toBeNull();
2323
});
2424
});

src/events/sdk-key-decoder.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ export default class SdkKeyDecoder {
55
* Decodes and returns the event ingestion hostname from the provided Eppo SDK key string.
66
* If the SDK key doesn't contain the event ingestion hostname, or it's invalid, it returns null.
77
*/
8-
decodeEventIngestionHostName(sdkKey: string): string | null {
8+
decodeEventIngestionUrl(sdkKey: string): string | null {
99
const encodedPayload = sdkKey.split('.')[1];
1010
if (!encodedPayload) return null;
1111

1212
const decodedPayload = Base64.decode(encodedPayload);
1313
const params = new URLSearchParams(decodedPayload);
14-
return params.get('eh');
14+
const hostname = params.get('eh');
15+
if (!hostname) return null;
16+
17+
if (!hostname.startsWith('http://') && !hostname.startsWith('https://')) {
18+
// prefix hostname with https scheme if none present
19+
return `https://${hostname}`;
20+
} else {
21+
return hostname;
22+
}
1523
}
1624
}

0 commit comments

Comments
 (0)