Skip to content

Commit

Permalink
fix: use btoa or atob directly without globalThis (there was an issue…
Browse files Browse the repository at this point in the history
… on vercel middleware
  • Loading branch information
marcomontalbano committed Jul 12, 2024
1 parent 61356f9 commit 6c1976a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
14 changes: 6 additions & 8 deletions packages/js-auth/src/utils/base64.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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.', () => {
Expand Down
9 changes: 4 additions & 5 deletions packages/js-auth/src/utils/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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('-', '+')
Expand Down

0 comments on commit 6c1976a

Please sign in to comment.