waveterm/frontend/util/util.ts

26 lines
736 B
TypeScript
Raw Normal View History

2024-05-14 21:29:41 +02:00
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0s
import base64 from "base64-js";
function base64ToString(b64: string): string {
const stringBytes = base64.toByteArray(b64);
return new TextDecoder().decode(stringBytes);
}
function stringToBase64(input: string): string {
const stringBytes = new TextEncoder().encode(input);
return base64.fromByteArray(stringBytes);
}
function base64ToArray(b64: string): Uint8Array {
const rawStr = atob(b64);
const rtnArr = new Uint8Array(new ArrayBuffer(rawStr.length));
for (let i = 0; i < rawStr.length; i++) {
rtnArr[i] = rawStr.charCodeAt(i);
}
return rtnArr;
}
2024-05-28 21:12:28 +02:00
export { base64ToArray, base64ToString, stringToBase64 };