From 6c1976a6de6a0471ef400b634418a43391a9454f Mon Sep 17 00:00:00 2001 From: Marco Montalbano Date: Fri, 12 Jul 2024 16:05:21 +0200 Subject: [PATCH] fix: use btoa or atob directly without globalThis (there was an issue on vercel middleware --- packages/js-auth/src/utils/base64.spec.ts | 14 ++++++-------- packages/js-auth/src/utils/base64.ts | 9 ++++----- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/packages/js-auth/src/utils/base64.spec.ts b/packages/js-auth/src/utils/base64.spec.ts index 18b9238..ca242c2 100644 --- a/packages/js-auth/src/utils/base64.spec.ts +++ b/packages/js-auth/src/utils/base64.spec.ts @@ -9,15 +9,9 @@ const stringifiedObject = JSON.stringify({ }) describe('Using `Buffer`', () => { - runTests() -}) - -describe('Using `btoa` and `atob`', () => { beforeAll(() => { - vi.stubGlobal('window', { - atob: globalThis.atob, - btoa: globalThis.btoa - }) + vi.stubGlobal('atob', undefined) + vi.stubGlobal('btoa', undefined) }) afterAll(() => { @@ -27,6 +21,10 @@ describe('Using `btoa` and `atob`', () => { runTests() }) +describe('Using `btoa` and `atob`', () => { + runTests() +}) + function runTests(): void { describe('encodeBase64UrlSafe', () => { it('should be able to create a Base64 URL safe encoded ASCII string from a binary string.', () => { diff --git a/packages/js-auth/src/utils/base64.ts b/packages/js-auth/src/utils/base64.ts index 455ae25..c39e3ba 100644 --- a/packages/js-auth/src/utils/base64.ts +++ b/packages/js-auth/src/utils/base64.ts @@ -10,10 +10,9 @@ * @returns An ASCII string containing the Base64 URL safe representation of `stringToEncode`. */ export function encodeBase64URLSafe(stringToEncode: string): string { - if (typeof globalThis.btoa !== 'undefined') { + if (typeof btoa !== 'undefined') { return ( - globalThis - .btoa(stringToEncode) + btoa(stringToEncode) // Remove padding equal characters .replaceAll('=', '') // Replace characters according to base64url specifications @@ -37,8 +36,8 @@ export function encodeBase64URLSafe(stringToEncode: string): string { * @returns An ASCII string containing decoded data from `encodedData`. */ export function decodeBase64URLSafe(encodedData: string): string { - if (typeof globalThis.atob !== 'undefined') { - return globalThis.atob( + if (typeof atob !== 'undefined') { + return atob( encodedData // Replace characters according to base64url specifications .replaceAll('-', '+')