|
1 | 1 | "use strict";
|
2 | 2 |
|
3 |
| -const { TextDecoder } = global.TextDecoder ? global : require("text-encoding"); |
| 3 | +const { labelToName, isSupported, decode } = require("./whatwg-encoding"); |
4 | 4 |
|
5 | 5 | exports.implementation = class TextDecoderImpl {
|
6 |
| - constructor(globalObject, args = []) { |
7 |
| - this._internal = new TextDecoder(...args); |
| 6 | + constructor(globalObject, constructorArgs = []) { |
| 7 | + const label = constructorArgs[0]; |
| 8 | + const options = constructorArgs[1] || {}; |
| 9 | + |
| 10 | + const encoding = labelToName(label); |
| 11 | + if (!isSupported(encoding) || encoding === "replacement") { |
| 12 | + throw new RangeError(`"${encoding}" is not a supported encoding name`); |
| 13 | + } |
| 14 | + this._encoding = encoding; |
| 15 | + this._errorMode = options._fatal ? "fatal" : "replacement"; |
| 16 | + this._ignoreBOM = options._ignoreBOM || false; |
8 | 17 | }
|
9 | 18 |
|
10 | 19 | get encoding() {
|
11 |
| - return this._internal.encoding; |
| 20 | + return String(this._encoding).toLowerCase(); |
12 | 21 | }
|
13 | 22 |
|
14 | 23 | get fatal() {
|
15 |
| - return this._internal.fatal; |
| 24 | + return this._errorMode === "fatal"; |
16 | 25 | }
|
17 | 26 |
|
18 | 27 | get ignoreBOM() {
|
19 |
| - return this._internal.ignoreBOM; |
| 28 | + return this._ignoreBOM; |
20 | 29 | }
|
21 | 30 |
|
22 |
| - decode(input, options) { |
23 |
| - return this._internal.decode(input, options); |
| 31 | + decode(input, options = {}) { |
| 32 | + if (options.steam === true) { |
| 33 | + // TODO: Implement stream support |
| 34 | + } |
| 35 | + try { |
| 36 | + // TODO: Implement ignoreBOM support |
| 37 | + return decode(Buffer.from(input), this._encoding); |
| 38 | + } catch (exception) { |
| 39 | + if (this._errorMode === "fatal") { |
| 40 | + throw new TypeError(exception); |
| 41 | + } |
| 42 | + return exception; |
| 43 | + } |
24 | 44 | }
|
25 | 45 | };
|
0 commit comments