All files utils.ts

100% Statements 29/29
100% Branches 2/2
100% Functions 3/3
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 4410x 10x     7x 7x 7x 35x   7x       7x 7x 7x 35x   7x       14x 14x 14x   14x 28x 28x 14x 14x     14x 14x   14x 14x 14x     14x    
const enc = new TextEncoder();
const dec = new TextDecoder();
 
export function base64Encode(value: string): string {
    const bytes = enc.encode(value);
    let binary = "";
    for (let i = 0; i < bytes.length; i++) {
        binary += String.fromCharCode(bytes[i]!);
    }
    return btoa(binary);
}
 
export function base64Decode(encoded: string): string {
    const binary = atob(encoded);
    const bytes = new Uint8Array(binary.length);
    for (let i = 0; i < binary.length; i++) {
        bytes[i] = binary.charCodeAt(i);
    }
    return dec.decode(bytes);
}
 
export async function readStream(value: ReadableStream): Promise<Uint8Array> {
    const reader = value.getReader();
    const chunks: Uint8Array[] = [];
    let total = 0;
 
    for (; ;) {
        const { value: chunk, done } = await reader.read();
        if (done) break;
        chunks.push(chunk);
        total += chunk.byteLength;
    }
 
    const out = new Uint8Array(total);
    let off = 0;
 
    for (const c of chunks) {
        out.set(c, off);
        off += c.byteLength;
    }
 
    return out;
}