Files
sousa-gecko/testing/web-platform/tests/compression/compression-multiple-chunks.any.js
T
Anne van Kesteren 47a27e9af8 Bug 2038779 [wpt PR 59794] - Remove ShadowRealm test coverage, a=testonly
Automatic update from web-platform-tests
Remove ShadowRealm test coverage

This intentionally does not make infrastructure or documentation changes, but without implementer support all the ShadowRealm tests add significant noise. Once there is implementer support this can be reverted.

--

wpt-commits: e4a4672e9e607fc2b28e7173b83ce4e38ef53071
wpt-pr: 59794
2026-05-13 04:24:16 +00:00

59 lines
1.9 KiB
JavaScript

// META: global=window,worker
// META: script=third_party/pako/pako_inflate.min.js
// META: script=resources/decompress.js
// META: script=resources/formats.js
// META: timeout=long
'use strict';
// This test asserts that compressing multiple chunks should work.
// Example: ('Hello', 3) => TextEncoder().encode('HelloHelloHello')
function makeExpectedChunk(input, numberOfChunks) {
const expectedChunk = input.repeat(numberOfChunks);
return new TextEncoder().encode(expectedChunk);
}
// Example: ('Hello', 3, 'deflate') => compress ['Hello', 'Hello', Hello']
async function compressMultipleChunks(input, numberOfChunks, format) {
const cs = new CompressionStream(format);
const writer = cs.writable.getWriter();
const chunk = new TextEncoder().encode(input);
for (let i = 0; i < numberOfChunks; ++i) {
writer.write(chunk);
}
const closePromise = writer.close();
const out = [];
const reader = cs.readable.getReader();
let totalSize = 0;
while (true) {
const { value, done } = await reader.read();
if (done)
break;
out.push(value);
totalSize += value.byteLength;
}
await closePromise;
const concatenated = new Uint8Array(totalSize);
let offset = 0;
for (const array of out) {
concatenated.set(array, offset);
offset += array.byteLength;
}
return concatenated;
}
const hello = 'Hello';
for (const format of formats) {
for (let numberOfChunks = 2; numberOfChunks <= 16; ++numberOfChunks) {
promise_test(async t => {
const compressedData = await compressMultipleChunks(hello, numberOfChunks, format);
const decompressedData = await decompressDataOrPako(compressedData, format);
const expectedValue = makeExpectedChunk(hello, numberOfChunks);
// check that we got the same result as our original string
assert_array_equals(decompressedData, expectedValue, 'value should match');
}, `compressing ${numberOfChunks} chunks with ${format} should work`);
}
}