1// Flags: --no-warnings
2'use strict';
3
4const common = require('../common');
5const assert = require('assert');
6
7const {
8  TextEncoderStream,
9  TextDecoderStream,
10} = require('stream/web');
11
12const kEuroBytes = Buffer.from([0xe2, 0x82, 0xac]);
13const kEuro = Buffer.from([0xe2, 0x82, 0xac]).toString();
14
15[1, false, [], {}, 'hello'].forEach((i) => {
16  assert.throws(() => new TextDecoderStream(i), {
17    code: 'ERR_ENCODING_NOT_SUPPORTED',
18  });
19});
20
21[1, false, 'hello'].forEach((i) => {
22  assert.throws(() => new TextDecoderStream(undefined, i), {
23    code: 'ERR_INVALID_ARG_TYPE',
24  });
25});
26
27{
28  const tds = new TextDecoderStream();
29  const writer = tds.writable.getWriter();
30  const reader = tds.readable.getReader();
31  reader.read().then(common.mustCall(({ value, done }) => {
32    assert(!done);
33    assert.strictEqual(kEuro, value);
34    reader.read().then(common.mustCall(({ done }) => {
35      assert(done);
36    }));
37  }));
38  Promise.all([
39    writer.write(kEuroBytes.slice(0, 1)),
40    writer.write(kEuroBytes.slice(1, 2)),
41    writer.write(kEuroBytes.slice(2, 3)),
42    writer.close(),
43  ]).then(common.mustCall());
44
45  assert.strictEqual(tds.encoding, 'utf-8');
46  assert.strictEqual(tds.fatal, false);
47  assert.strictEqual(tds.ignoreBOM, false);
48
49  assert.throws(
50    () => Reflect.get(TextDecoderStream.prototype, 'encoding', {}), {
51      code: 'ERR_INVALID_THIS',
52    });
53  assert.throws(
54    () => Reflect.get(TextDecoderStream.prototype, 'fatal', {}), {
55      code: 'ERR_INVALID_THIS',
56    });
57  assert.throws(
58    () => Reflect.get(TextDecoderStream.prototype, 'ignoreBOM', {}), {
59      code: 'ERR_INVALID_THIS',
60    });
61  assert.throws(
62    () => Reflect.get(TextDecoderStream.prototype, 'readable', {}), {
63      code: 'ERR_INVALID_THIS',
64    });
65  assert.throws(
66    () => Reflect.get(TextDecoderStream.prototype, 'writable', {}), {
67      code: 'ERR_INVALID_THIS',
68    });
69}
70
71{
72  const tds = new TextEncoderStream();
73  const writer = tds.writable.getWriter();
74  const reader = tds.readable.getReader();
75  reader.read().then(common.mustCall(({ value, done }) => {
76    assert(!done);
77    const buf = Buffer.from(value.buffer, value.byteOffset, value.byteLength);
78    assert.deepStrictEqual(kEuroBytes, buf);
79    reader.read().then(common.mustCall(({ done }) => {
80      assert(done);
81    }));
82  }));
83  Promise.all([
84    writer.write(kEuro),
85    writer.close(),
86  ]).then(common.mustCall());
87
88  assert.strictEqual(tds.encoding, 'utf-8');
89
90  assert.throws(
91    () => Reflect.get(TextEncoderStream.prototype, 'encoding', {}), {
92      code: 'ERR_INVALID_THIS',
93    });
94  assert.throws(
95    () => Reflect.get(TextEncoderStream.prototype, 'readable', {}), {
96      code: 'ERR_INVALID_THIS',
97    });
98  assert.throws(
99    () => Reflect.get(TextEncoderStream.prototype, 'writable', {}), {
100      code: 'ERR_INVALID_THIS',
101    });
102}
103