summaryrefslogtreecommitdiff
path: root/test/parallel/test-whatwg-webstreams-encoding.js
blob: 97061650496c0db7923b995eab45237b0a57fb23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Flags: --no-warnings
'use strict';

const common = require('../common');
const assert = require('assert');

const {
  TextEncoderStream,
  TextDecoderStream,
} = require('stream/web');

const kEuroBytes = Buffer.from([0xe2, 0x82, 0xac]);
const kEuro = Buffer.from([0xe2, 0x82, 0xac]).toString();

[1, false, [], {}, 'hello'].forEach((i) => {
  assert.throws(() => new TextDecoderStream(i), {
    code: 'ERR_ENCODING_NOT_SUPPORTED',
  });
});

[1, false, 'hello'].forEach((i) => {
  assert.throws(() => new TextDecoderStream(undefined, i), {
    code: 'ERR_INVALID_ARG_TYPE',
  });
});

{
  const tds = new TextDecoderStream();
  const writer = tds.writable.getWriter();
  const reader = tds.readable.getReader();
  reader.read().then(common.mustCall(({ value, done }) => {
    assert(!done);
    assert.strictEqual(kEuro, value);
    reader.read().then(common.mustCall(({ done }) => {
      assert(done);
    }));
  }));
  Promise.all([
    writer.write(kEuroBytes.slice(0, 1)),
    writer.write(kEuroBytes.slice(1, 2)),
    writer.write(kEuroBytes.slice(2, 3)),
    writer.close(),
  ]).then(common.mustCall());

  assert.strictEqual(tds.encoding, 'utf-8');
  assert.strictEqual(tds.fatal, false);
  assert.strictEqual(tds.ignoreBOM, false);

  assert.throws(
    () => Reflect.get(TextDecoderStream.prototype, 'encoding', {}), {
      code: 'ERR_INVALID_THIS',
    });
  assert.throws(
    () => Reflect.get(TextDecoderStream.prototype, 'fatal', {}), {
      code: 'ERR_INVALID_THIS',
    });
  assert.throws(
    () => Reflect.get(TextDecoderStream.prototype, 'ignoreBOM', {}), {
      code: 'ERR_INVALID_THIS',
    });
  assert.throws(
    () => Reflect.get(TextDecoderStream.prototype, 'readable', {}), {
      code: 'ERR_INVALID_THIS',
    });
  assert.throws(
    () => Reflect.get(TextDecoderStream.prototype, 'writable', {}), {
      code: 'ERR_INVALID_THIS',
    });
}

{
  const tds = new TextEncoderStream();
  const writer = tds.writable.getWriter();
  const reader = tds.readable.getReader();
  reader.read().then(common.mustCall(({ value, done }) => {
    assert(!done);
    const buf = Buffer.from(value.buffer, value.byteOffset, value.byteLength);
    assert.deepStrictEqual(kEuroBytes, buf);
    reader.read().then(common.mustCall(({ done }) => {
      assert(done);
    }));
  }));
  Promise.all([
    writer.write(kEuro),
    writer.close(),
  ]).then(common.mustCall());

  assert.strictEqual(tds.encoding, 'utf-8');

  assert.throws(
    () => Reflect.get(TextEncoderStream.prototype, 'encoding', {}), {
      code: 'ERR_INVALID_THIS',
    });
  assert.throws(
    () => Reflect.get(TextEncoderStream.prototype, 'readable', {}), {
      code: 'ERR_INVALID_THIS',
    });
  assert.throws(
    () => Reflect.get(TextEncoderStream.prototype, 'writable', {}), {
      code: 'ERR_INVALID_THIS',
    });
}