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:
Max Peintner 2023-06-13 07:37:22 +02:00 committed by GitHub
parent 5693e40930
commit e39d1b7d82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
}