mirror of
https://github.com/zitadel/zitadel.git
synced 2025-01-06 14:55:35 +00:00
fix(console): url safe base64 to array buffer (#6019)
fix: console base64 to array buffer Co-authored-by: Livio Spring <livio.a@gmail.com>
This commit is contained in:
parent
5693e40930
commit
e39d1b7d82
@ -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);
|
||||
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;
|
||||
}
|
||||
return bytes.buffer;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user