-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove Buffer usage for browser environments (#423)
Closes #418
- Loading branch information
Showing
3 changed files
with
26 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Base64 string encoding and decoding module. | ||
// Uses Buffer for Node.js and btoa/atob for browser environments. | ||
// We use TextEncoder/TextDecoder for browser environments because | ||
// they can handle non-ASCII characters, unlike btoa/atob. | ||
|
||
export const stringToBase64 = | ||
typeof Buffer !== 'undefined' | ||
? (str: string) => Buffer.from(str).toString('base64') | ||
: (str: string) => | ||
btoa( | ||
new TextEncoder() | ||
.encode(str) | ||
.reduce((acc, byte) => acc + String.fromCharCode(byte), ''), | ||
); | ||
|
||
export const base64ToString = | ||
typeof Buffer !== 'undefined' | ||
? (str: string) => Buffer.from(str, 'base64').toString() | ||
: (str: string) => | ||
new TextDecoder().decode(Uint8Array.from(atob(str), (c) => c.charCodeAt(0))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters