Skip to content

Commit dd0904d

Browse files
authored
fix: Use js-base64 polyfill instead of Buffer for browser env (#159)
* fix: Use base64 polyfill instead of Buffer * version bump
1 parent 0587b36 commit dd0904d

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@eppo/js-client-sdk-common",
3-
"version": "4.5.3",
3+
"version": "4.5.4",
44
"description": "Eppo SDK for client-side JavaScript applications (base for both web and react native)",
55
"main": "dist/index.js",
66
"files": [

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import SdkKeyDecoder from './sdk-key-decoder';
22

33
describe('SdkKeyDecoder', () => {
4+
const decoder = new SdkKeyDecoder();
45
it('should decode the event ingestion hostname from the SDK key', () => {
5-
const decoder = new SdkKeyDecoder();
66
const hostname = decoder.decodeEventIngestionHostName(
77
'zCsQuoHJxVPp895.ZWg9MTIzNDU2LmUudGVzdGluZy5lcHBvLmNsb3Vk',
88
);
99
expect(hostname).toEqual('123456.e.testing.eppo.cloud');
1010
});
1111

12+
it('should decode strings with non URL-safe characters', () => {
13+
// this is not a really valid ingestion URL, but it's useful for testing the decoder
14+
const invalidUrl = 'eh=12+3456/.e.testing.eppo.cloud';
15+
const encoded = Buffer.from(invalidUrl).toString('base64url');
16+
const hostname = decoder.decodeEventIngestionHostName(`zCsQuoHJxVPp895.${encoded}`);
17+
expect(hostname).toEqual('12 3456/.e.testing.eppo.cloud');
18+
});
19+
1220
it("should return null if the SDK key doesn't contain the event ingestion hostname", () => {
13-
const decoder = new SdkKeyDecoder();
1421
expect(decoder.decodeEventIngestionHostName('zCsQuoHJxVPp895')).toBeNull();
1522
expect(decoder.decodeEventIngestionHostName('zCsQuoHJxVPp895.xxxxxx')).toBeNull();
1623
});

src/events/sdk-key-decoder.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Base64 } from 'js-base64';
2+
13
export default class SdkKeyDecoder {
24
/**
35
* Decodes and returns the event ingestion hostname from the provided Eppo SDK key string.
@@ -7,7 +9,7 @@ export default class SdkKeyDecoder {
79
const encodedPayload = sdkKey.split('.')[1];
810
if (!encodedPayload) return null;
911

10-
const decodedPayload = Buffer.from(encodedPayload, 'base64url').toString('utf-8');
12+
const decodedPayload = Base64.decode(encodedPayload);
1113
const params = new URLSearchParams(decodedPayload);
1214
return params.get('eh');
1315
}

0 commit comments

Comments
 (0)