stream: making DecompressionStream spec compilent for trailing junk · nodejs/node@e8a07f2
1+'use strict';
2+require('../common');
3+const assert = require('assert').strict;
4+const test = require('node:test');
5+const { DecompressionStream } = require('stream/web');
6+7+async function expectTypeError(promise) {
8+let threw = false;
9+try {
10+await promise;
11+} catch (err) {
12+threw = true;
13+assert(err instanceof TypeError, `Expected TypeError, got ${err}`);
14+}
15+assert(threw, 'Expected promise to reject');
16+}
17+18+test('DecompressStream deflat emits error on trailing data', async () => {
19+const valid = new Uint8Array([120, 156, 75, 4, 0, 0, 98, 0, 98]); // deflate('a')
20+const empty = new Uint8Array(1);
21+const invalid = new Uint8Array([...valid, ...empty]);
22+const double = new Uint8Array([...valid, ...valid]);
23+24+for (const chunk of [[invalid], [valid, empty], [valid, valid], [valid, double]]) {
25+await expectTypeError(
26+Array.fromAsync(
27+new Blob([chunk]).stream().pipeThrough(new DecompressionStream('deflate'))
28+)
29+);
30+}
31+});
32+33+test('DecompressStream gzip emits error on trailing data', async () => {
34+const valid = new Uint8Array([31, 139, 8, 0, 0, 0, 0, 0, 0, 19, 75, 4,
35+0, 67, 190, 183, 232, 1, 0, 0, 0]); // gzip('a')
36+const empty = new Uint8Array(1);
37+const invalid = new Uint8Array([...valid, ...empty]);
38+const double = new Uint8Array([...valid, ...valid]);
39+for (const chunk of [[invalid], [valid, empty], [valid, valid], [double]]) {
40+await expectTypeError(
41+Array.fromAsync(
42+new Blob([chunk]).stream().pipeThrough(new DecompressionStream('gzip'))
43+)
44+);
45+}
46+});