async function textToGzipBase64(text) {
const stream = new Blob([text]).stream();
const compressed = stream.pipeThrough(new CompressionStream('gzip'));
- const buffer = new Uint8Array(await new Response(compressed).arrayBuffer());
- let encoded = '';
- buffer.forEach(byte => encoded += String.fromCharCode(byte));
- const b64 = btoa(encoded);
- return b64;
+ const buffer = await new Response(compressed).bytes();
+ return buffer.toBase64();
}
async function gzipBase64ToText(b64) {
- const encoded = atob(b64);
- const buffer = Uint8Array.from(encoded, c => c.charCodeAt(0));
+ const buffer = Uint8Array.fromBase64(b64);
const stream = new Blob([buffer]).stream();
const decompressed = stream.pipeThrough(new DecompressionStream('gzip'));
const text = await new Response(decompressed).text();