decompression-split-chunk.any.js (1262B)
1 // META: global=window,worker,shadowrealm 2 // META: script=resources/decompression-input.js 3 4 'use strict'; 5 6 async function decompressArrayBuffer(input, format, chunkSize) { 7 const ds = new DecompressionStream(format); 8 const reader = ds.readable.getReader(); 9 const writer = ds.writable.getWriter(); 10 for (let beginning = 0; beginning < input.length; beginning += chunkSize) { 11 writer.write(input.slice(beginning, beginning + chunkSize)); 12 } 13 writer.close(); 14 const out = []; 15 let totalSize = 0; 16 while (true) { 17 const { value, done } = await reader.read(); 18 if (done) break; 19 out.push(value); 20 totalSize += value.byteLength; 21 } 22 const concatenated = new Uint8Array(totalSize); 23 let offset = 0; 24 for (const array of out) { 25 concatenated.set(array, offset); 26 offset += array.byteLength; 27 } 28 return concatenated; 29 } 30 31 for (const [format, bytes] of compressedBytes) { 32 for (let chunkSize = 1; chunkSize < 16; ++chunkSize) { 33 promise_test(async t => { 34 const decompressedData = await decompressArrayBuffer(bytes, format, chunkSize); 35 assert_array_equals(decompressedData, expectedChunkValue, "value should match"); 36 }, `decompressing splitted chunk into pieces of size ${chunkSize} should work in ${format}`); 37 } 38 }