Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eppo/js-client-sdk-common",
"version": "4.5.3",
"version": "4.5.4",
"description": "Eppo SDK for client-side JavaScript applications (base for both web and react native)",
"main": "dist/index.js",
"files": [
Expand Down
11 changes: 9 additions & 2 deletions src/events/sdk-key-decoder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import SdkKeyDecoder from './sdk-key-decoder';

describe('SdkKeyDecoder', () => {
const decoder = new SdkKeyDecoder();
it('should decode the event ingestion hostname from the SDK key', () => {
const decoder = new SdkKeyDecoder();
const hostname = decoder.decodeEventIngestionHostName(
'zCsQuoHJxVPp895.ZWg9MTIzNDU2LmUudGVzdGluZy5lcHBvLmNsb3Vk',
);
expect(hostname).toEqual('123456.e.testing.eppo.cloud');
});

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

it("should return null if the SDK key doesn't contain the event ingestion hostname", () => {
const decoder = new SdkKeyDecoder();
expect(decoder.decodeEventIngestionHostName('zCsQuoHJxVPp895')).toBeNull();
expect(decoder.decodeEventIngestionHostName('zCsQuoHJxVPp895.xxxxxx')).toBeNull();
});
Expand Down
4 changes: 3 additions & 1 deletion src/events/sdk-key-decoder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Base64 } from 'js-base64';

export default class SdkKeyDecoder {
/**
* Decodes and returns the event ingestion hostname from the provided Eppo SDK key string.
Expand All @@ -7,7 +9,7 @@ export default class SdkKeyDecoder {
const encodedPayload = sdkKey.split('.')[1];
if (!encodedPayload) return null;

const decodedPayload = Buffer.from(encodedPayload, 'base64url').toString('utf-8');
const decodedPayload = Base64.decode(encodedPayload);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

const params = new URLSearchParams(decodedPayload);
return params.get('eh');
}
Expand Down
Loading