tor-browser

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

compression-stream.any.js (1984B)


      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 const SMALL_FILE = "/media/foo.vtt";
     10 const LARGE_FILE = "/media/test-av-384k-44100Hz-1ch-320x240-30fps-10kfr.webm";
     11 
     12 let dataPromiseList = [
     13  ["empty data", Promise.resolve(new Uint8Array(0))],
     14  ["small amount data", fetch(SMALL_FILE).then(response => response.bytes())],
     15  ["large amount data", fetch(LARGE_FILE).then(response => response.bytes())],
     16 ];
     17 
     18 async function compressArrayBuffer(input, format) {
     19  const cs = new CompressionStream(format);
     20  const writer = cs.writable.getWriter();
     21  writer.write(input);
     22  const closePromise = writer.close();
     23  const out = [];
     24  const reader = cs.readable.getReader();
     25  let totalSize = 0;
     26  while (true) {
     27    const { value, done } = await reader.read();
     28    if (done)
     29      break;
     30    out.push(value);
     31    totalSize += value.byteLength;
     32  }
     33  await closePromise;
     34  const concatenated = new Uint8Array(totalSize);
     35  let offset = 0;
     36  for (const array of out) {
     37    concatenated.set(array, offset);
     38    offset += array.byteLength;
     39  }
     40  return concatenated;
     41 }
     42 
     43 test(() => {
     44  assert_throws_js(TypeError, () => {
     45    const transformer = new CompressionStream("nonvalid");
     46  }, "non supported format should throw");
     47 }, "CompressionStream constructor should throw on invalid format");
     48 
     49 for (const format of formats) {
     50  for (const [label, dataPromise] of dataPromiseList) {
     51    promise_test(async () => {
     52      const bufferView = await dataPromise;
     53      const compressedData = await compressArrayBuffer(bufferView, format);
     54      const decompressedData = await decompressDataOrPako(compressedData, format);
     55      // check that we got the same result as our original string
     56      assert_array_equals(decompressedData, bufferView, 'value should match');
     57    }, `${format} ${label} should be reinflated back to its origin`);
     58  }
     59 }