tor-browser

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

compression-including-empty-chunk.any.js (1779B)


      1 // META: global=window,worker,shadowrealm
      2 // META: script=third_party/pako/pako_inflate.min.js
      3 // META: script=resources/decompress.js
      4 // META: script=resources/formats.js
      5 // META: timeout=long
      6 
      7 'use strict';
      8 
      9 // This test asserts that compressing '' doesn't affect the compressed data.
     10 // Example: compressing ['Hello', '', 'Hello'] results in 'HelloHello'
     11 
     12 async function compressChunkList(chunkList, format) {
     13  const cs = new CompressionStream(format);
     14  const writer = cs.writable.getWriter();
     15  for (const chunk of chunkList) {
     16    const chunkByte = new TextEncoder().encode(chunk);
     17    writer.write(chunkByte);
     18  }
     19  const closePromise = writer.close();
     20  const out = [];
     21  const reader = cs.readable.getReader();
     22  let totalSize = 0;
     23  while (true) {
     24    const { value, done } = await reader.read();
     25    if (done)
     26      break;
     27    out.push(value);
     28    totalSize += value.byteLength;
     29  }
     30  await closePromise;
     31  const concatenated = new Uint8Array(totalSize);
     32  let offset = 0;
     33  for (const array of out) {
     34    concatenated.set(array, offset);
     35    offset += array.byteLength;
     36  }
     37  return concatenated;
     38 }
     39 
     40 const chunkLists = [
     41  ['', 'Hello', 'Hello'],
     42  ['Hello', '', 'Hello'],
     43  ['Hello', 'Hello', '']
     44 ];
     45 const expectedValue = new TextEncoder().encode('HelloHello');
     46 
     47 for (const format of formats) {
     48  for (const chunkList of chunkLists) {
     49    promise_test(async t => {
     50      const compressedData = await compressChunkList(chunkList, format);
     51      const decompressedData = await decompressDataOrPako(compressedData, format);
     52      // check that we got the same result as our original string
     53      assert_array_equals(expectedValue, decompressedData, 'value should match');
     54    }, `the result of compressing [${chunkList}] with ${format} should be 'HelloHello'`);
     55  }
     56 }