-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(console): url safe base64 to array buffer (#6019)
fix: console base64 to array buffer Co-authored-by: Livio Spring <livio.a@gmail.com>
- Loading branch information
1 parent
5693e40
commit e39d1b7
Showing
1 changed file
with
30 additions
and
8 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 |
---|---|---|
@@ -1,9 +1,31 @@ | ||
export function _base64ToArrayBuffer(base64: string): any { | ||
const binaryString = atob(base64); | ||
const len = binaryString.length; | ||
const bytes = new Uint8Array(len); | ||
for (let i = 0; i < len; i++) { | ||
bytes[i] = binaryString.charCodeAt(i); | ||
} | ||
return bytes.buffer; | ||
export function _base64ToArrayBuffer(thing: any) { | ||
if (typeof thing === 'string') { | ||
// base64url to base64 | ||
thing = thing.replace(/-/g, '+').replace(/_/g, '/'); | ||
|
||
// base64 to Uint8Array | ||
var str = window.atob(thing); | ||
var bytes = new Uint8Array(str.length); | ||
for (var i = 0; i < str.length; i++) { | ||
bytes[i] = str.charCodeAt(i); | ||
} | ||
thing = bytes; | ||
} | ||
|
||
// Array to Uint8Array | ||
if (Array.isArray(thing)) { | ||
thing = new Uint8Array(thing); | ||
} | ||
|
||
// Uint8Array to ArrayBuffer | ||
if (thing instanceof Uint8Array) { | ||
thing = thing.buffer; | ||
} | ||
|
||
// error if none of the above worked | ||
if (!(thing instanceof ArrayBuffer)) { | ||
throw new TypeError('could not coerce to ArrayBuffer'); | ||
} | ||
|
||
return thing; | ||
} |
e39d1b7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./
zitadel-docs.vercel.app
docs-git-main-zitadel.vercel.app
docs-zitadel.vercel.app