tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

decompression-split-chunk.tentative.any.js (1748B)


      1 // META: global=window,worker
      2 
      3 "use strict";
      4 
      5 // The zstd-compressed bytes for the string "expected output".
      6 const compressedZstdBytes = new Uint8Array([
      7  0x28, 0xb5, 0x2f, 0xfd, 0x04, 0x58, 0x79, 0x00, 0x00, 0x65, 0x78, 0x70, 0x65,
      8  0x63, 0x74, 0x65, 0x64, 0x20, 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x5b, 0x11,
      9  0xc6, 0x85,
     10 ]);
     11 
     12 for (let chunkSize = 1; chunkSize < 16; ++chunkSize) {
     13  promise_test(async t => {
     14    const ds = new DecompressionStream("zstd");
     15 
     16    const writer = ds.writable.getWriter();
     17    const reader = ds.readable.getReader();
     18 
     19    const writePromises = [];
     20 
     21    for (
     22      let beginning = 0;
     23      beginning < compressedZstdBytes.length;
     24      beginning += chunkSize
     25    ) {
     26      writePromises.push(
     27        writer.write(
     28          compressedZstdBytes.slice(beginning, beginning + chunkSize)
     29        )
     30      );
     31    }
     32 
     33    const writerClosePromise = writer.close();
     34    const chunks = [];
     35 
     36    while (true) {
     37      const { value, done } = await reader.read();
     38      if (done) {
     39        break;
     40      }
     41      chunks.push(value);
     42    }
     43 
     44    await Promise.all(writePromises);
     45    await writerClosePromise;
     46 
     47    let totalLength = 0;
     48    for (const chunk of chunks) {
     49      totalLength += chunk.byteLength;
     50    }
     51 
     52    const combined = new Uint8Array(totalLength);
     53    let offset = 0;
     54    for (const chunk of chunks) {
     55      combined.set(chunk, offset);
     56      offset += chunk.byteLength;
     57    }
     58 
     59    const decompressedText = new TextDecoder().decode(combined);
     60    assert_equals(
     61      decompressedText,
     62      "expected output",
     63      `Decompressed text with chunkSize=${chunkSize} should match the expected output.`
     64    );
     65  }, `decompressing valid zstd input in chunks of size ${chunkSize} should yield "expected output"`);
     66 }