Files
dandelion/static/dataUtils.js
bobbydigitales d0fd041f7e Add deno-only bootstrap peer implementation. Uses libdatachannel for the RTCDatachannel implementation.
Fix Typescript sourcemap serving
PeerManager: more robust when RTCPeerConnection fails or is not present
Separate source maps so the main files arent bloated
2025-06-10 20:23:19 -07:00

72 lines
2.5 KiB
JavaScript

export async function bytesToBase64DataUrl(bytes, type = "application/octet-stream") {
return await new Promise((resolve, reject) => {
const reader = Object.assign(new FileReader(), {
onload: () => resolve(reader.result),
onerror: () => reject(reader.error),
});
reader.readAsDataURL(new File([bytes], "", { type }));
});
}
export async function arrayBufferToBase64(buffer) {
var bytes = new Uint8Array(buffer);
return (await bytesToBase64DataUrl(bytes)).replace("data:application/octet-stream;base64,", "");
}
// async function base64ToArrayBuffer(base64String: string) {
// let response = await fetch("data:application/octet-stream;base64," + base64String);
// let arrayBuffer = await response.arrayBuffer();
// return arrayBuffer;
// }
export async function compressString(input) {
// Convert the string to a Uint8Array
const textEncoder = new TextEncoder();
const inputArray = textEncoder.encode(input);
// Create a CompressionStream
const compressionStream = new CompressionStream('gzip');
const writer = compressionStream.writable.getWriter();
// Write the data and close the stream
writer.write(inputArray);
writer.close();
// Read the compressed data from the stream
const compressedArray = await new Response(compressionStream.readable).arrayBuffer();
// Convert the compressed data to a Uint8Array
return new Uint8Array(compressedArray);
}
// Base58 character set
// const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
// Base58 encoding
// Base58 encoding
// function encodeBase58(buffer: Uint8Array): string {
// let carry;
// const digits = [0];
// for (const byte of buffer) {
// carry = byte;
// for (let i = 0; i < digits.length; i++) {
// carry += digits[i] << 8;
// digits[i] = carry % 58;
// carry = Math.floor(carry / 58);
// }
// while (carry > 0) {
// digits.push(carry % 58);
// carry = Math.floor(carry / 58);
// }
// }
// let result = '';
// for (const digit of digits.reverse()) {
// result += BASE58_ALPHABET[digit];
// }
// // Handle leading zero bytes
// for (const byte of buffer) {
// if (byte === 0x00) {
// result = BASE58_ALPHABET[0] + result;
// } else {
// break;
// }
// }
// return result;
// }
// Convert UUID v4 to Base58
// function uuidToBase58(uuid: string): string {
// const bytes = uuidToBytes(uuid);
// return encodeBase58(bytes);
// }
//# sourceMappingURL=dataUtils.js.map