1// Flags: --no-warnings
2'use strict';
3
4const common = require('../common');
5
6const {
7  CompressionStream,
8  DecompressionStream,
9} = require('stream/web');
10
11const assert = require('assert');
12const dec = new TextDecoder();
13
14async function test(format) {
15  const gzip = new CompressionStream(format);
16  const gunzip = new DecompressionStream(format);
17
18  gzip.readable.pipeTo(gunzip.writable).then(common.mustCall());
19
20  const reader = gunzip.readable.getReader();
21  const writer = gzip.writable.getWriter();
22
23  const compressed_data = [];
24  const reader_function = ({ value, done }) => {
25    if (value)
26      compressed_data.push(value);
27    if (!done)
28      return reader.read().then(reader_function);
29    assert.strictEqual(dec.decode(Buffer.concat(compressed_data)), 'hello');
30  };
31  const reader_promise = reader.read().then(reader_function);
32
33  await Promise.all([
34    reader_promise,
35    reader_promise.then(() => reader.read().then(({ done }) => assert(done))),
36    writer.write('hello'),
37    writer.close(),
38  ]);
39}
40
41Promise.all(['gzip', 'deflate'].map((i) => test(i))).then(common.mustCall());
42
43[1, 'hello', false, {}].forEach((i) => {
44  assert.throws(() => new CompressionStream(i), {
45    code: 'ERR_INVALID_ARG_VALUE',
46  });
47  assert.throws(() => new DecompressionStream(i), {
48    code: 'ERR_INVALID_ARG_VALUE',
49  });
50});
51
52assert.throws(
53  () => Reflect.get(CompressionStream.prototype, 'readable', {}), {
54    code: 'ERR_INVALID_THIS',
55  });
56assert.throws(
57  () => Reflect.get(CompressionStream.prototype, 'writable', {}), {
58    code: 'ERR_INVALID_THIS',
59  });
60assert.throws(
61  () => Reflect.get(DecompressionStream.prototype, 'readable', {}), {
62    code: 'ERR_INVALID_THIS',
63  });
64assert.throws(
65  () => Reflect.get(DecompressionStream.prototype, 'writable', {}), {
66    code: 'ERR_INVALID_THIS',
67  });
68