Skip to content

Lightpanda: the headless browser designed for AI and automation

[de]compression stream validation

Karl Seguin4330a05ccbc1
diff --git a/src/browser/tests/compression/compression_stream.html b/src/browser/tests/compression/compression_stream.html
index a758a8b1a..560e89440 100644
--- a/src/browser/tests/compression/compression_stream.html
+++ b/src/browser/tests/compression/compression_stream.html
@@ -1,8 +1,21 @@
<!DOCTYPE html>
+<meta name=lp-mode content=test>
<script src="../testing.js"></script>
<script>
- const FORMATS = ['gzip', 'deflate', 'deflate-raw', 'brotli'];
+ let SUPPORTS_BROTLI = true;
+ try {
+ new CompressionStream('brotli');
+ } catch (e) {
+ SUPPORTS_BROTLI = false;
+ }
+</script>
+
+<script>
+ const FORMATS = ['gzip', 'deflate', 'deflate-raw'];
+ if (SUPPORTS_BROTLI) {
+ FORMATS.push('brotli');
+ }
async function readAll(readable) {
return readRest(readable.getReader(), []);
@@ -64,7 +77,7 @@
try {
await promise;
} catch (e) {
- return e.name;
+ return e?.name ?? e;
}
return 'resolved';
}
@@ -154,7 +167,9 @@
testing.expectEqual(20, results['gzip'].length);
testing.expectEqual(8, results['deflate'].length);
testing.expectEqual(2, results['deflate-raw'].length);
- testing.expectEqual(1, results['brotli'].length);
+ if (SUPPORTS_BROTLI) {
+ testing.expectEqual(1, results['brotli'].length);
+ }
for (const format of FORMATS) {
testing.expectEqual([], results[format].decompressed);
}
@@ -187,10 +202,13 @@
</script>
<script id=bad_chunks type=module>
+ const formats = ['gzip'];
+ if (SUPPORTS_BROTLI) {
+ formats.push('brotli');
+ }
const state = await testing.async();
-
const results = [];
- for (const format of ['gzip', 'brotli']) {
+ for (const format of formats) {
for (const chunk of ['hello', undefined, null, 42, {}, [1, 2, 3]]) {
const cs = new CompressionStream(format);
const writer = cs.writable.getWriter();
@@ -232,9 +250,13 @@
state.resolve();
await state.done(() => {
- testing.expectEqual('TypeError', write);
- testing.expectEqual('TypeError', next_write);
- testing.expectEqual('TypeError', close);
+ // Known bug: cancelling the readable should error the writable with the
+ // cancel reason (undefined here); our writes still run and fail to enqueue.
+ testing.diverges("cancelling a TransformStream's readable doesn't error its writable", () => {
+ testing.expectEqual('TypeError', write);
+ testing.expectEqual('TypeError', next_write);
+ testing.expectEqual('TypeError', close);
+ });
});
</script>
@@ -386,79 +408,97 @@
</script>
<script id=brotli_matches_cli type=module>
+ // testing.async() comes first, even when skipping: the runner waits for
+ // every module that has one.
const state = await testing.async();
+ if (SUPPORTS_BROTLI == false) {
+ state.resolve();
+ await state.done(() => {});
+ } else {
+ const encode = (text) => new TextEncoder().encode(text);
+ const results = {
+ hello: Array.from(await run(new CompressionStream('brotli'), [encode('hello')])),
+ repeated: Array.from(await run(new CompressionStream('brotli'), [encode('hello brotli, '.repeat(8))])),
+ // Only empty chunks: still a complete, valid stream.
+ empty: Array.from(await run(new CompressionStream('brotli'), [new Uint8Array(0), new ArrayBuffer(0)])),
+ };
- const encode = (text) => new TextEncoder().encode(text);
- const results = {
- hello: Array.from(await run(new CompressionStream('brotli'), [encode('hello')])),
- repeated: Array.from(await run(new CompressionStream('brotli'), [encode('hello brotli, '.repeat(8))])),
- // Only empty chunks: still a complete, valid stream.
- empty: Array.from(await run(new CompressionStream('brotli'), [new Uint8Array(0), new ArrayBuffer(0)])),
- };
-
- state.resolve();
- await state.done(() => {
- // Byte-for-byte what `brotli -q 5 -w 22` (v1.2.0) gives, the quality we
- // use. 'hello' is too short to compress, so it's stored as is.
- testing.expectEqual([11, 2, 128, 104, 101, 108, 108, 111, 3], results.hello);
- testing.expectEqual([
- 27, 111, 0, 0, 196, 113, 108, 221, 252, 238, 234, 69, 68, 170, 65, 112,
- 171, 199, 115, 122, 14, 173, 143, 9,
- ], results.repeated);
- testing.expectEqual([59], results.empty);
- });
+ state.resolve();
+ await state.done(() => {
+ // Byte-for-byte what `brotli -q 5 -w 22` (v1.2.0) gives, the quality we
+ // use. 'hello' is too short to compress, so it's stored as is.
+ testing.expectEqual([11, 2, 128, 104, 101, 108, 108, 111, 3], results.hello);
+ testing.expectEqual([
+ 27, 111, 0, 0, 196, 113, 108, 221, 252, 238, 234, 69, 68, 170, 65, 112,
+ 171, 199, 115, 122, 14, 173, 143, 9,
+ ], results.repeated);
+ testing.expectEqual([59], results.empty);
+ });
+ }
</script>
<script id=brotli_round_trip type=module>
+ // testing.async() comes first, even when skipping: the runner waits for
+ // every module that has one.
const state = await testing.async();
+ if (SUPPORTS_BROTLI == false) {
+ state.resolve();
+ await state.done(() => {});
+ } else {
+ const noisy = noise(200 * 1024);
+ const tiny = noise(4096);
+ const text = new TextEncoder().encode('hello world '.repeat(1000));
+ const cases = {
+ noise: [noisy, [noisy.subarray(0, 1), noisy.subarray(1, 70000), noisy.subarray(70000)]],
+ tiny_writes: [tiny, Array.from(tiny, (_, i) => tiny.subarray(i, i + 1))],
+ text: [text, [text]],
+ // Closed without a single write.
+ empty: [new Uint8Array(0), []],
+ };
- const noisy = noise(200 * 1024);
- const tiny = noise(4096);
- const text = new TextEncoder().encode('hello world '.repeat(1000));
- const cases = {
- noise: [noisy, [noisy.subarray(0, 1), noisy.subarray(1, 70000), noisy.subarray(70000)]],
- tiny_writes: [tiny, Array.from(tiny, (_, i) => tiny.subarray(i, i + 1))],
- text: [text, [text]],
- // Closed without a single write.
- empty: [new Uint8Array(0), []],
- };
+ const results = {};
+ for (const [name, [input, chunks]] of Object.entries(cases)) {
+ const compressed = await run(new CompressionStream('brotli'), chunks);
+ results[name] = equal(await unbrotli(compressed), input);
+ }
- const results = {};
- for (const [name, [input, chunks]] of Object.entries(cases)) {
- const compressed = await run(new CompressionStream('brotli'), chunks);
- results[name] = equal(await unbrotli(compressed), input);
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual({ noise: true, tiny_writes: true, text: true, empty: true }, results);
+ });
}
-
- state.resolve();
- await state.done(() => {
- testing.expectEqual({ noise: true, tiny_writes: true, text: true, empty: true }, results);
- });
</script>
<script id=brotli_output_before_close type=module>
+ // testing.async() comes first, even when skipping: the runner waits for
+ // every module that has one.
const state = await testing.async();
+ if (SUPPORTS_BROTLI == false) {
+ state.resolve();
+ await state.done(() => {});
+ } else {
+ // The encoder holds input back until it has a whole meta-block; for this
+ // noise that's after 1 MiB, so the write produces output by itself. It
+ // then stops taking input until that output is taken, so the rest of the
+ // write needs another call into the encoder.
+ const input = noise(1664 * 1024);
+ const cs = new CompressionStream('brotli');
+ const writer = cs.writable.getWriter();
+ const reader = cs.readable.getReader();
- // The encoder holds input back until it has a whole meta-block; for this
- // noise that's after 1 MiB, so the write produces output by itself. It
- // then stops taking input until that output is taken, so the rest of the
- // write needs another call into the encoder.
- const input = noise(1664 * 1024);
- const cs = new CompressionStream('brotli');
- const writer = cs.writable.getWriter();
- const reader = cs.readable.getReader();
-
- let first = null;
- reader.read().then(({ value }) => {
- first = value;
- });
- await writer.write(input);
- const before_close = first !== null;
- writer.close();
- const compressed = await readRest(reader, before_close ? [first] : []);
- const same = equal(await unbrotli(compressed), input);
+ let first = null;
+ reader.read().then(({ value }) => {
+ first = value;
+ });
+ await writer.write(input);
+ const before_close = first !== null;
+ writer.close();
+ const compressed = await readRest(reader, before_close ? [first] : []);
+ const same = equal(await unbrotli(compressed), input);
- state.resolve();
- await state.done(() => {
- testing.expectEqual({ before_close: true, same: true }, { before_close, same });
- });
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual({ before_close: true, same: true }, { before_close, same });
+ });
+ }
</script>
diff --git a/src/browser/tests/compression/decompression_stream.html b/src/browser/tests/compression/decompression_stream.html
index 479eece63..91ca4e5e4 100644
--- a/src/browser/tests/compression/decompression_stream.html
+++ b/src/browser/tests/compression/decompression_stream.html
@@ -1,8 +1,21 @@
<!DOCTYPE html>
+<meta name=lp-mode content=test>
<script src="../testing.js"></script>
<script>
- const FORMATS = ['gzip', 'deflate', 'deflate-raw', 'brotli'];
+ let SUPPORTS_BROTLI = true;
+ try {
+ new CompressionStream('brotli');
+ } catch (e) {
+ SUPPORTS_BROTLI = false;
+ }
+</script>
+
+<script>
+ const FORMATS = ['gzip', 'deflate', 'deflate-raw'];
+ if (SUPPORTS_BROTLI) {
+ FORMATS.push('brotli');
+ }
// 'hello', compressed by Python's zlib/gzip modules and the brotli CLI
// (v1.2.0).
@@ -88,13 +101,19 @@
async function outcome(format, chunks) {
const ds = new DecompressionStream(format);
const writer = ds.writable.getWriter();
+ // Writes wait for reads (backpressure), so drain the readable meanwhile.
+ // `read` is how it ends.
+ const reader = ds.readable.getReader();
+ const read = rejection((async () => {
+ while ((await reader.read()).done == false);
+ })());
const writes = [];
for (const chunk of chunks) writes.push(rejection(writer.write(new Uint8Array(chunk))));
const close = rejection(writer.close());
return {
writes: await Promise.all(writes),
close: await close,
- read: await rejection(ds.readable.getReader().read()),
+ read: await read,
};
}
</script>
@@ -193,7 +212,9 @@
// A reserved window size (RFC 7932, section 9.1). brotli has no checksum, so
// flipped bytes often go unnoticed.
- results['brotli'] = await outcome('brotli', [[17, 0, 0, 0]]);
+ if (SUPPORTS_BROTLI) {
+ results['brotli'] = await outcome('brotli', [[17, 0, 0, 0]]);
+ }
state.resolve();
await state.done(() => {
@@ -227,10 +248,15 @@
</script>
<script id=bad_chunks type=module>
+ const formats = ['gzip'];
+ if (SUPPORTS_BROTLI) {
+ formats.push('brotli');
+ }
+
const state = await testing.async();
const results = [];
- for (const format of ['gzip', 'brotli']) {
+ for (const format of formats) {
for (const chunk of ['hello', undefined, null, 42, {}, [1, 2, 3]]) {
const ds = new DecompressionStream(format);
const writer = ds.writable.getWriter();
@@ -343,48 +369,60 @@
['deflate', 'brotli'],
['deflate-raw', 'brotli'],
]) {
- results[`${data} as ${format}`] = await outcome(format, [HELLO[data]]);
+ if (FORMATS.includes(format)) {
+ results[`${data} as ${format}`] = await outcome(format, [HELLO[data]]);
+ }
}
// brotli has no magic number: these bytes start a valid-looking stream that
// never ends, so only close() notices.
- const raw_as_brotli = await outcome('brotli', [HELLO['deflate-raw']]);
+ const raw_as_brotli = SUPPORTS_BROTLI ? await outcome('brotli', [HELLO['deflate-raw']]) : null;
state.resolve();
await state.done(() => {
for (const result of Object.values(results)) {
testing.expectEqual({ writes: ['TypeError'], close: 'TypeError', read: 'TypeError' }, result);
}
- testing.expectEqual({ writes: ['resolved'], close: 'TypeError', read: 'TypeError' }, raw_as_brotli);
+ if (SUPPORTS_BROTLI) {
+ testing.expectEqual({ writes: ['resolved'], close: 'TypeError', read: 'TypeError' }, raw_as_brotli);
+ }
});
</script>
<script id=brotli_small_window type=module>
+ // testing.async() comes first, even when skipping: the runner waits for
+ // every module that has one.
const state = await testing.async();
+ if (SUPPORTS_BROTLI == false) {
+ state.resolve();
+ await state.done(() => {});
+ } else {
+
+ // `brotli -q 11 -w 10` (v1.2.0) of 'hello world ' x 1000: with a 1 KiB
+ // window the decoder's ring buffer fills a dozen times within one write.
+ const compressed = [
+ 161, 248, 118, 193, 111, 164, 116, 243, 38, 170, 50, 180, 4, 97, 123, 171, 89, 30, 171, 5, 50, 160, 218, 142, 38, 0,
+ 0,
+ ];
+ const expected = 'hello world '.repeat(1000);
+ const results = {
+ whole: (await decompress('brotli', [compressed])) === expected,
+ split: (await decompress('brotli', compressed.map((b) => [b]))) === expected,
+ };
- // `brotli -q 11 -w 10` (v1.2.0) of 'hello world ' x 1000: with a 1 KiB
- // window the decoder's ring buffer fills a dozen times within one write.
- const compressed = [
- 161, 248, 118, 193, 111, 164, 116, 243, 38, 170, 50, 180, 4, 97, 123, 171, 89, 30, 171, 5, 50, 160, 218, 142, 38, 0,
- 0,
- ];
- const expected = 'hello world '.repeat(1000);
- const results = {
- whole: (await decompress('brotli', [compressed])) === expected,
- split: (await decompress('brotli', compressed.map((b) => [b]))) === expected,
- };
-
- state.resolve();
- await state.done(() => {
- testing.expectEqual({ whole: true, split: true }, results);
- });
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual({ whole: true, split: true }, results);
+ });
+ }
</script>
<script id=stored_type_error type=module>
const state = await testing.async();
// A stream errored by a bad chunk rejects with one TypeError object, on
- // both sides, starting with the write that failed.
+ // both sides, starting with the write that failed. close() is the
+ // exception: on an errored stream it rejects with a new TypeError.
const ds = new DecompressionStream('gzip');
const writer = ds.writable.getWriter();
const reader = ds.readable.getReader();
@@ -401,7 +439,8 @@
state.resolve();
await state.done(() => {
testing.expectEqual(true, errors[0] instanceof TypeError);
- testing.expectEqual([true, true, true, true, true, true], errors.map((e) => e === errors[0]));
+ testing.expectEqual([true, true, true, true, true, false], errors.map((e) => e === errors[0]));
+ testing.expectEqual(true, errors[5] instanceof TypeError);
});
</script>
@@ -409,20 +448,26 @@
const state = await testing.async();
// Truncated input only fails in close(), which rejects with the TypeError
- // that both sides errored with.
+ // that both sides errored with. A second close() gets a new TypeError.
const ds = new DecompressionStream('gzip');
const writer = ds.writable.getWriter();
+ const reader = ds.readable.getReader();
+ // The write waits for a read (backpressure)
+ const first_read = reader.read();
await writer.write(new Uint8Array(HELLO.gzip.slice(0, -1)));
+ const first = new TextDecoder().decode((await first_read).value);
const close_error = await writer.close().catch((e) => e);
const errors = [
- await ds.readable.getReader().read().catch((e) => e),
+ await reader.read().catch((e) => e),
await writer.write(new Uint8Array(1)).catch((e) => e),
await writer.close().catch((e) => e),
];
state.resolve();
await state.done(() => {
+ testing.expectEqual('hello', first);
testing.expectEqual(true, close_error instanceof TypeError);
- testing.expectEqual([true, true, true], errors.map((e) => e === close_error));
+ testing.expectEqual([true, true, false], errors.map((e) => e === close_error));
+ testing.expectEqual(true, errors[2] instanceof TypeError);
});
</script>
diff --git a/src/browser/tests/streams/readable_stream.html b/src/browser/tests/streams/readable_stream.html
index ebad2c459..19bca0f49 100644
--- a/src/browser/tests/streams/readable_stream.html
+++ b/src/browser/tests/streams/readable_stream.html
@@ -491,8 +491,8 @@
<script id=stored_error type=module>
const state = await testing.async();
- // read(), cancel(), write(), close() and closed reject with whatever
- // controller.error() was given.
+ // read(), cancel(), write() and closed reject with whatever
+ // controller.error() was given; close() with a new TypeError.
const errored = new ReadableStream({ start(c) { c.error('err!'); } }).getReader();
const no_reason = new ReadableStream({ start(c) { c.error(); } }).getReader();
const errored_sink = new WritableStream({ start(c) { c.error('err!'); } }).getWriter();
@@ -502,13 +502,13 @@
await errored.cancel().catch((e) => e),
await no_reason.read().then(() => 'resolved', (e) => e),
await errored_sink.write('a').catch((e) => e),
- await errored_sink.close().catch((e) => e),
- await no_reason_sink.close().then(() => 'resolved', (e) => e),
+ await errored_sink.close().catch((e) => e.name),
+ await no_reason_sink.close().then(() => 'resolved', (e) => e.name),
await errored_sink.closed.catch((e) => e),
];
state.resolve();
await state.done(() => {
- testing.expectEqual(['err!', 'err!', undefined, 'err!', 'err!', undefined, 'err!'], results);
+ testing.expectEqual(['err!', 'err!', undefined, 'err!', 'TypeError', 'TypeError', 'err!'], results);
});
</script>
diff --git a/src/browser/tests/streams/transform_stream.html b/src/browser/tests/streams/transform_stream.html
index ee8b2cecd..8b390346c 100644
--- a/src/browser/tests/streams/transform_stream.html
+++ b/src/browser/tests/streams/transform_stream.html
@@ -167,6 +167,7 @@
const state = await testing.async();
// controller.error(e) errors the readable and the writable side with e.
+ // close() on the errored writable rejects with a new TypeError.
const err = new Error('boom');
const ts = new TransformStream({ transform(chunk, controller) { controller.error(err); } });
const writer = ts.writable.getWriter();
@@ -180,6 +181,7 @@
state.resolve();
await state.done(() => {
- testing.expectEqual([true, true, true], errors.map((e) => e === err));
+ testing.expectEqual([true, true, false], errors.map((e) => e === err));
+ testing.expectEqual(true, errors[2] instanceof TypeError);
});
</script>
diff --git a/src/browser/webapi/streams/WritableStreamDefaultWriter.zig b/src/browser/webapi/streams/WritableStreamDefaultWriter.zig
index cc5ef0cec..276ee4a5e 100644
--- a/src/browser/webapi/streams/WritableStreamDefaultWriter.zig
+++ b/src/browser/webapi/streams/WritableStreamDefaultWriter.zig
@@ -55,8 +55,9 @@ pub fn close(self: *WritableStreamDefaultWriter, exec: *const Execution) !js.Pro
return local.typeError("Writer has been released");
};
+ // Unlike write(), an errored stream rejects with a new TypeError.
if (stream._state != .writable) {
- return rejectNotWritable(stream, local);
+ return local.typeError("Stream is not writable");
}
stream.closeStream(exec) catch |err| switch (err) {