Skip to content

Commit

Permalink
feat(shared): Introduce isomorphicBtoa helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkl committed Oct 25, 2023
1 parent a96fc0c commit b57eb53
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/shared/src/isomorphicBtoa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const isomorphicBtoa = (data: string) => {
if (typeof btoa !== 'undefined' && typeof btoa === 'function') {
return btoa(data);
} else if (typeof global !== 'undefined' && global.Buffer) {
return new global.Buffer(data).toString('base64');
}
return data;
};
3 changes: 2 additions & 1 deletion packages/shared/src/keys.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PublishableKey } from '@clerk/types';

import { isomorphicAtob } from './isomorphicAtob';
import { isomorphicBtoa } from './isomorphicBtoa';

const PUBLISHABLE_KEY_LIVE_PREFIX = 'pk_live_';
const PUBLISHABLE_KEY_TEST_PREFIX = 'pk_test_';
Expand All @@ -12,7 +13,7 @@ export function buildPublishableKey(frontendApi: string): string {
const keyPrefix = PUBLISHABLE_FRONTEND_API_DEV_REGEX.test(frontendApi)
? PUBLISHABLE_KEY_TEST_PREFIX
: PUBLISHABLE_KEY_LIVE_PREFIX;
return `${keyPrefix}${btoa(`${frontendApi}$`)}`;
return `${keyPrefix}${isomorphicBtoa(`${frontendApi}$`)}`;
}

export function parsePublishableKey(key: string | undefined): PublishableKey | null {
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './createDeferredPromise';
export { isStaging } from './instance';
export { logErrorInDevMode } from './logErrorInDevMode';
export { noop } from './noop';
export * from './runtimeEnvironment';
export * from './runWithExponentialBackOff';
export { logErrorInDevMode } from './logErrorInDevMode';
export * from './runtimeEnvironment';

0 comments on commit b57eb53

Please sign in to comment.