-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move decodeBase64 to utils and remove atob usage
- Loading branch information
Showing
2 changed files
with
26 additions
and
12 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
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,24 @@ | ||
export function decodeBase64(b64: string): Uint8Array { | ||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
let base64 = b64.replace(/-/g, '+').replace(/_/g, '/'); | ||
|
||
while (base64.length % 4) { | ||
base64 += '='; | ||
} | ||
|
||
let bitString = ''; | ||
for (let i = 0; i < base64.length; i++) { | ||
const char = base64.charAt(i); | ||
if (char !== '=') { | ||
const charIndex = chars.indexOf(char); | ||
bitString += charIndex.toString(2).padStart(6, '0'); | ||
} | ||
} | ||
|
||
const bytes = new Uint8Array(bitString.length / 8); | ||
for (let i = 0; i < bytes.length; i++) { | ||
bytes[i] = parseInt(bitString.substring(8 * i, 8 * (i + 1)), 2); | ||
} | ||
|
||
return bytes; | ||
} |