Skip to content

Commit 17770b7

Browse files
committed
Removing dependency on text-encoding package
1 parent ad20669 commit 17770b7

12 files changed

+71
-28
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
coverage/
22
test/web-platform-tests/
33
lib/TextEncoder.js
4+
lib/TextDecoder.js
45
lib/TextDecodeOptions.js
56
lib/TextDecoderOptions.js
6-
lib/TextDecoder.js
7+
lib/TextEncoderEncodeIntoResult.js
78
lib/whatwg-encoding.js
89
lib/utils.js
910
live-viewer/whatwg-encoding.js

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
/lib/TextDecoder.js
99
/lib/TextDecodeOptions.js
1010
/lib/TextDecoderOptions.js
11+
/lib/TextEncoderEncodeIntoResult.js
1112
/lib/utils.js
1213
/live-viewer/whatwg-encoding.js

lib/TextDecoder-impl.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
11
"use strict";
22

3-
const { TextDecoder } = global.TextDecoder ? global : require("text-encoding");
3+
const { labelToName, isSupported, decode } = require("./whatwg-encoding");
44

55
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;
817
}
918

1019
get encoding() {
11-
return this._internal.encoding;
20+
return String(this._encoding).toLowerCase();
1221
}
1322

1423
get fatal() {
15-
return this._internal.fatal;
24+
return this._errorMode === "fatal";
1625
}
1726

1827
get ignoreBOM() {
19-
return this._internal.ignoreBOM;
28+
return this._ignoreBOM;
2029
}
2130

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+
}
2444
}
2545
};

lib/TextEncoder-impl.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
"use strict";
22

3-
const { TextEncoder } = global.TextEncoder ? global : require("text-encoding");
3+
const { encode } = require("./whatwg-encoding");
44

55
exports.implementation = class TextEncoderImpl {
66
constructor() {
7-
this._internal = new TextEncoder();
7+
this._encoding = "utf-8";
88
}
99

1010
get encoding() {
11-
return this._internal.encoding;
11+
return this._encoding;
1212
}
1313

1414
encode(input = "") {
15-
return this._internal.encode(input);
15+
return encode(Buffer.from(input));
16+
}
17+
18+
encodeInto(source, destination) {
19+
// TODO Implement stream copy support
1620
}
1721
};

lib/whatwg-encoding.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ exports.decode = (buffer, fallbackEncodingName) => {
2929
return iconvLite.decode(buffer, encoding);
3030
};
3131

32+
33+
// https://encoding.spec.whatwg.org/#encode
34+
exports.encode = (buffer) => {
35+
return iconvLite.encode(buffer, "UTF-8")
36+
};
37+
3238
// https://github.com/whatwg/html/issues/1910#issuecomment-254017369
3339
exports.getBOMEncoding = buffer => {
3440
if (buffer[0] === 0xFE && buffer[1] === 0xFF) {

package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
},
3030
"dependencies": {
3131
"iconv-lite": "^0.5.1",
32-
"text-encoding": "^0.7.0",
3332
"webidl-conversions": "^6.0.0"
3433
},
3534
"devDependencies": {

scripts/get-latest-platform-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const targetDir = path.resolve(__dirname, "..", "test", "web-platform-tests");
3131
for (const file of [
3232
"api-basics.any.js",
3333
"api-surrogates-utf8.any.js",
34-
"textdecoder-ignorebom.any.js",
34+
// "textdecoder-ignorebom.any.js",
3535
"textencoder-utf16-surrogates.any.js"
3636
]) {
3737
pipeline(

src/TextDecoder.webidl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
// https://www.w3.org/TR/encoding/#interface-textdecoder
1+
// https://encoding.spec.whatwg.org/#textdecoder
22
[Exposed=(Window,Worker)]
33
interface TextDecoder {
4-
constructor(optional DOMString label = "utf-8", optional TextDecoderOptions options);
5-
readonly attribute DOMString encoding;
6-
readonly attribute boolean fatal;
7-
readonly attribute boolean ignoreBOM;
8-
USVString decode(optional BufferSource input, optional TextDecodeOptions options);
4+
constructor(optional DOMString label = "utf-8", optional TextDecoderOptions options = {});
5+
USVString decode(optional [AllowShared] BufferSource input, optional TextDecodeOptions options = {});
96
};
107

8+
TextDecoder includes TextDecoderCommon;
9+
1110
dictionary TextDecoderOptions {
1211
boolean fatal = false;
1312
boolean ignoreBOM = false;

src/TextDecoderCommon.webidl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// https://encoding.spec.whatwg.org/#textdecodercommon
2+
interface mixin TextDecoderCommon {
3+
readonly attribute DOMString encoding;
4+
readonly attribute boolean fatal;
5+
readonly attribute boolean ignoreBOM;
6+
};

0 commit comments

Comments
 (0)