• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// From: https://github.com/w3c/web-platform-tests/blob/d74324b53c/encoding/textdecoder-fatal-streaming.html
4// With the twist that we specifically test for Node.js error codes
5
6const common = require('../common');
7const assert = require('assert');
8
9if (!common.hasIntl)
10  common.skip('missing Intl');
11
12{
13  [
14    { encoding: 'utf-8', sequence: [0xC0] },
15    { encoding: 'utf-16le', sequence: [0x00] },
16    { encoding: 'utf-16be', sequence: [0x00] },
17  ].forEach((testCase) => {
18    const data = new Uint8Array([testCase.sequence]);
19    assert.throws(
20      () => {
21        const decoder = new TextDecoder(testCase.encoding, { fatal: true });
22        decoder.decode(data);
23      }, {
24        code: 'ERR_ENCODING_INVALID_ENCODED_DATA',
25        name: 'TypeError',
26        message:
27          `The encoded data was not valid for encoding ${testCase.encoding}`
28      }
29    );
30  });
31}
32
33{
34  const decoder = new TextDecoder('utf-16le', { fatal: true });
35  const odd = new Uint8Array([0x00]);
36  const even = new Uint8Array([0x00, 0x00]);
37
38  assert.throws(
39    () => {
40      decoder.decode(even, { stream: true });
41      decoder.decode(odd);
42    }, {
43      code: 'ERR_ENCODING_INVALID_ENCODED_DATA',
44      name: 'TypeError',
45      message:
46        'The encoded data was not valid for encoding utf-16le'
47    }
48  );
49
50  assert.throws(
51    () => {
52      decoder.decode(odd, { stream: true });
53      decoder.decode(even);
54    }, {
55      code: 'ERR_ENCODING_INVALID_ENCODED_DATA',
56      name: 'TypeError',
57      message:
58        'The encoded data was not valid for encoding utf-16le'
59    }
60  );
61}
62