diff --git a/.eslintrc.js b/.eslintrc.js
index e4192c3539925c..99cef88669ba79 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -271,6 +271,8 @@ module.exports = {
DTRACE_HTTP_SERVER_REQUEST: false,
DTRACE_HTTP_SERVER_RESPONSE: false,
DTRACE_NET_SERVER_CONNECTION: false,
- DTRACE_NET_STREAM_END: false
+ DTRACE_NET_STREAM_END: false,
+ TextEncoder: false,
+ TextDecoder: false
},
};
diff --git a/doc/api/errors.md b/doc/api/errors.md
index 34d85d0ceb7934..7e77d84ed08665 100644
--- a/doc/api/errors.md
+++ b/doc/api/errors.md
@@ -794,13 +794,13 @@ The stack trace is extended to include the point in time at which the
### ERR_ENCODING_INVALID_ENCODED_DATA
-Data provided to `util.TextDecoder()` API was invalid according to the encoding
+Data provided to `TextDecoder()` API was invalid according to the encoding
provided.
### ERR_ENCODING_NOT_SUPPORTED
-Encoding provided to `util.TextDecoder()` API was not one of the
+Encoding provided to `TextDecoder()` API was not one of the
[WHATWG Supported Encodings][].
diff --git a/doc/api/globals.md b/doc/api/globals.md
index a9aaefeefa1203..af0bf3ee674c16 100644
--- a/doc/api/globals.md
+++ b/doc/api/globals.md
@@ -138,6 +138,25 @@ added: v0.0.1
[`setTimeout`] is described in the [timers][] section.
+## TextDecoder
+
+
+
+
+The WHATWG `TextDecoder` class. See the [`TextDecoder`][] section.
+
+## TextEncoder
+
+
+
+
+The WHATWG `TextEncoder` class. See the [`TextEncoder`][] section.
+
+
## URL
* `encoding` {string} Identifies the `encoding` that this `TextDecoder` instance
supports. **Default:** `'utf-8'`.
@@ -893,6 +900,8 @@ is not supported.
Creates an new `TextDecoder` instance. The `encoding` may specify one of the
supported encodings or an alias.
+The `TextDecoder` class is also available on the global object.
+
### textDecoder.decode([input[, options]])
* `input` {ArrayBuffer|DataView|TypedArray} An `ArrayBuffer`, `DataView` or
@@ -932,6 +941,10 @@ mark.
## Class: util.TextEncoder
An implementation of the [WHATWG Encoding Standard][] `TextEncoder` API. All
@@ -942,6 +955,8 @@ const encoder = new TextEncoder();
const uint8array = encoder.encode('this is some data');
```
+The `TextEncoder` class is also available on the global object.
+
### textEncoder.encode([input])
* `input` {string} The text to encode. **Default:** an empty string.
diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js
index 7af3a8ad4ade07..fc8d8a2809b76d 100644
--- a/lib/internal/bootstrap/node.js
+++ b/lib/internal/bootstrap/node.js
@@ -127,6 +127,7 @@
setupGlobalTimeouts();
setupGlobalConsole();
setupGlobalURL();
+ setupGlobalEncoding();
}
if (process.binding('config').experimentalWorker) {
@@ -476,6 +477,24 @@
});
}
+ function setupGlobalEncoding() {
+ const { TextEncoder, TextDecoder } = NativeModule.require('util');
+ Object.defineProperties(global, {
+ TextEncoder: {
+ value: TextEncoder,
+ writable: true,
+ configurable: true,
+ enumerable: false
+ },
+ TextDecoder: {
+ value: TextDecoder,
+ writable: true,
+ configurable: true,
+ enumerable: false
+ }
+ });
+ }
+
function setupDOMException() {
// Registers the constructor with C++.
NativeModule.require('internal/domexception');
diff --git a/test/parallel/test-global-encoder.js b/test/parallel/test-global-encoder.js
new file mode 100644
index 00000000000000..0e98bc806ca5bc
--- /dev/null
+++ b/test/parallel/test-global-encoder.js
@@ -0,0 +1,8 @@
+'use strict';
+
+require('../common');
+const { strictEqual } = require('assert');
+const util = require('util');
+
+strictEqual(TextDecoder, util.TextDecoder);
+strictEqual(TextEncoder, util.TextEncoder);
diff --git a/test/parallel/test-whatwg-encoding-fatal-streaming.js b/test/parallel/test-whatwg-encoding-fatal-streaming.js
index 0b510126affe3a..ddedb483624331 100644
--- a/test/parallel/test-whatwg-encoding-fatal-streaming.js
+++ b/test/parallel/test-whatwg-encoding-fatal-streaming.js
@@ -8,10 +8,6 @@ if (!common.hasIntl)
common.skip('missing Intl');
const assert = require('assert');
-const {
- TextDecoder
-} = require('util');
-
{
[
diff --git a/test/parallel/test-whatwg-encoding-surrogates-utf8.js b/test/parallel/test-whatwg-encoding-surrogates-utf8.js
index 5fbdd0d83b944d..fd66a1e02e50a8 100644
--- a/test/parallel/test-whatwg-encoding-surrogates-utf8.js
+++ b/test/parallel/test-whatwg-encoding-surrogates-utf8.js
@@ -5,10 +5,6 @@
require('../common');
const assert = require('assert');
-const {
- TextDecoder,
- TextEncoder
-} = require('util');
const badStrings = [
{
diff --git a/test/parallel/test-whatwg-encoding-textdecoder-fatal.js b/test/parallel/test-whatwg-encoding-textdecoder-fatal.js
index cfb595e78e6b40..be37f5097c504e 100644
--- a/test/parallel/test-whatwg-encoding-textdecoder-fatal.js
+++ b/test/parallel/test-whatwg-encoding-textdecoder-fatal.js
@@ -8,9 +8,6 @@ if (!common.hasIntl)
common.skip('missing Intl');
const assert = require('assert');
-const {
- TextDecoder
-} = require('util');
const bad = [
{ encoding: 'utf-8', input: [0xFF], name: 'invalid code' },
diff --git a/test/parallel/test-whatwg-encoding-textdecoder-ignorebom.js b/test/parallel/test-whatwg-encoding-textdecoder-ignorebom.js
index 0e3cd3025d040a..72fe498cd056d9 100644
--- a/test/parallel/test-whatwg-encoding-textdecoder-ignorebom.js
+++ b/test/parallel/test-whatwg-encoding-textdecoder-ignorebom.js
@@ -5,9 +5,6 @@
const common = require('../common');
const assert = require('assert');
-const {
- TextDecoder
-} = require('util');
const cases = [
{
diff --git a/test/parallel/test-whatwg-encoding-textdecoder-streaming.js b/test/parallel/test-whatwg-encoding-textdecoder-streaming.js
index e446d56ffd2abb..69186accb5770d 100644
--- a/test/parallel/test-whatwg-encoding-textdecoder-streaming.js
+++ b/test/parallel/test-whatwg-encoding-textdecoder-streaming.js
@@ -5,9 +5,6 @@
const common = require('../common');
const assert = require('assert');
-const {
- TextDecoder
-} = require('util');
const string =
'\x00123ABCabc\x80\xFF\u0100\u1000\uFFFD\uD800\uDC00\uDBFF\uDFFF';
diff --git a/test/parallel/test-whatwg-encoding-textdecoder-utf16-surrogates.js b/test/parallel/test-whatwg-encoding-textdecoder-utf16-surrogates.js
index fcf6a82e90fd85..163cccf9bb2b88 100644
--- a/test/parallel/test-whatwg-encoding-textdecoder-utf16-surrogates.js
+++ b/test/parallel/test-whatwg-encoding-textdecoder-utf16-surrogates.js
@@ -8,9 +8,6 @@ if (!common.hasIntl)
common.skip('missing Intl');
const assert = require('assert');
-const {
- TextDecoder
-} = require('util');
const bad = [
{
diff --git a/test/parallel/test-whatwg-encoding-textdecoder.js b/test/parallel/test-whatwg-encoding-textdecoder.js
index e87364de1e91f7..37fbe12757134e 100644
--- a/test/parallel/test-whatwg-encoding-textdecoder.js
+++ b/test/parallel/test-whatwg-encoding-textdecoder.js
@@ -4,7 +4,6 @@
const common = require('../common');
const assert = require('assert');
-const { TextDecoder, TextEncoder } = require('util');
const { customInspectSymbol: inspect } = require('internal/util');
const buf = Buffer.from([0xef, 0xbb, 0xbf, 0x74, 0x65,
diff --git a/test/parallel/test-whatwg-encoding-textencoder-utf16-surrogates.js b/test/parallel/test-whatwg-encoding-textencoder-utf16-surrogates.js
index 9ef3c0c2360b67..808acf31eb134b 100644
--- a/test/parallel/test-whatwg-encoding-textencoder-utf16-surrogates.js
+++ b/test/parallel/test-whatwg-encoding-textencoder-utf16-surrogates.js
@@ -5,10 +5,6 @@
require('../common');
const assert = require('assert');
-const {
- TextDecoder,
- TextEncoder
-} = require('util');
const bad = [
{
diff --git a/test/parallel/test-whatwg-encoding-textencoder.js b/test/parallel/test-whatwg-encoding-textencoder.js
index 5514b714bd9b6e..9022477229c0de 100644
--- a/test/parallel/test-whatwg-encoding-textencoder.js
+++ b/test/parallel/test-whatwg-encoding-textencoder.js
@@ -4,7 +4,6 @@
const common = require('../common');
const assert = require('assert');
-const { TextDecoder, TextEncoder } = require('util');
const { customInspectSymbol: inspect } = require('internal/util');
const encoded = Buffer.from([0xef, 0xbb, 0xbf, 0x74, 0x65,