tor-browser

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

decompression-buffersource.any.js (2594B)


      1 // META: global=window,worker,shadowrealm
      2 
      3 'use strict';
      4 
      5 const compressedBytes = [
      6  ["deflate", [120, 156, 75, 52, 48, 52, 50, 54, 49, 53, 3, 0, 8, 136, 1, 199]],
      7  ["gzip", [31, 139, 8, 0, 0, 0, 0, 0, 0, 3, 75, 52, 48, 52, 2, 0, 216, 252, 63, 136, 4, 0, 0, 0]],
      8  ["deflate-raw", [
      9    0x00, 0x06, 0x00, 0xf9, 0xff, 0x41, 0x42, 0x43,
     10    0x44, 0x45, 0x46, 0x01, 0x00, 0x00, 0xff, 0xff,
     11  ]],
     12  ["brotli", [0x21, 0x08, 0x00, 0x04, 0x66, 0x6F, 0x6F, 0x03]]
     13 ];
     14 // These chunk values below were chosen to make the length of the compressed
     15 // output be a multiple of 8 bytes.
     16 const expectedChunkValue = new Map(Object.entries({
     17  "deflate": new TextEncoder().encode('a0123456'),
     18  "gzip": new TextEncoder().encode('a012'),
     19  "deflate-raw": new TextEncoder().encode('ABCDEF'),
     20  "brotli": new TextEncoder().encode('foo'),
     21 }));
     22 
     23 const bufferSourceChunks = compressedBytes.map(([format, bytes]) => [format, [
     24  {
     25    name: 'ArrayBuffer',
     26    value: new Uint8Array(bytes).buffer
     27  },
     28  {
     29    name: 'Int8Array',
     30    value: new Int8Array(new Uint8Array(bytes).buffer)
     31  },
     32  {
     33    name: 'Uint8Array',
     34    value: new Uint8Array(new Uint8Array(bytes).buffer)
     35  },
     36  {
     37    name: 'Uint8ClampedArray',
     38    value: new Uint8ClampedArray(new Uint8Array(bytes).buffer)
     39  },
     40  {
     41    name: 'Int16Array',
     42    value: new Int16Array(new Uint8Array(bytes).buffer)
     43  },
     44  {
     45    name: 'Uint16Array',
     46    value: new Uint16Array(new Uint8Array(bytes).buffer)
     47  },
     48  {
     49    name: 'Int32Array',
     50    value: new Int32Array(new Uint8Array(bytes).buffer)
     51  },
     52  {
     53    name: 'Uint32Array',
     54    value: new Uint32Array(new Uint8Array(bytes).buffer)
     55  },
     56  {
     57    name: 'Float16Array',
     58    value: () => new Float16Array(new Uint8Array(bytes).buffer)
     59  },
     60  {
     61    name: 'Float32Array',
     62    value: new Float32Array(new Uint8Array(bytes).buffer)
     63  },
     64  {
     65    name: 'Float64Array',
     66    value: new Float64Array(new Uint8Array(bytes).buffer)
     67  },
     68  {
     69    name: 'DataView',
     70    value: new DataView(new Uint8Array(bytes).buffer)
     71  },
     72 ]]);
     73 
     74 for (const [format, chunks] of bufferSourceChunks) {
     75  for (const chunk of chunks) {
     76    promise_test(async t => {
     77      const ds = new DecompressionStream(format);
     78      const reader = ds.readable.getReader();
     79      const writer = ds.writable.getWriter();
     80      const writePromise = writer.write(typeof chunk.value === 'function' ? chunk.value() : chunk.value);
     81      writer.close();
     82      const { value } = await reader.read();
     83      assert_array_equals(Array.from(value), expectedChunkValue.get(format), 'value should match');
     84    }, `chunk of type ${chunk.name} should work for ${format}`);
     85  }
     86 }