tor-browser

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

compression-output-length.any.js (1564B)


      1 // META: global=window,worker,shadowrealm
      2 // META: script=resources/formats.js
      3 
      4 'use strict';
      5 
      6 // This test asserts that compressed data length is shorter than the original
      7 // data length. If the input is extremely small, the compressed data may be
      8 // larger than the original data.
      9 
     10 const LARGE_FILE = '/media/test-av-384k-44100Hz-1ch-320x240-30fps-10kfr.webm';
     11 
     12 async function compressArrayBuffer(input, format) {
     13  const cs = new CompressionStream(format);
     14  const writer = cs.writable.getWriter();
     15  writer.write(input);
     16  const closePromise = writer.close();
     17  const out = [];
     18  const reader = cs.readable.getReader();
     19  let totalSize = 0;
     20  while (true) {
     21    const { value, done } = await reader.read();
     22    if (done)
     23      break;
     24    out.push(value);
     25    totalSize += value.byteLength;
     26  }
     27  await closePromise;
     28  const concatenated = new Uint8Array(totalSize);
     29  let offset = 0;
     30  for (const array of out) {
     31    concatenated.set(array, offset);
     32    offset += array.byteLength;
     33  }
     34  return concatenated;
     35 }
     36 
     37 for (const format of formats) {
     38  promise_test(async () => {
     39    const response = await fetch(LARGE_FILE);
     40    const buffer = await response.arrayBuffer();
     41    const bufferView = new Uint8Array(buffer);
     42    const originalLength = bufferView.length;
     43    const compressedData = await compressArrayBuffer(bufferView, format);
     44    const compressedLength = compressedData.length;
     45    assert_less_than(compressedLength, originalLength, 'output should be smaller');
     46  }, `the length of ${format} data should be shorter than that of the original data`);
     47 }