Skip to content

Commit 75f1128

Browse files
committed
util: make TextEncoder/TextDecoder global
Fixes: #20365
1 parent 9392535 commit 75f1128

15 files changed

+68
-29
lines changed

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ module.exports = {
266266
DTRACE_HTTP_SERVER_REQUEST: false,
267267
DTRACE_HTTP_SERVER_RESPONSE: false,
268268
DTRACE_NET_SERVER_CONNECTION: false,
269-
DTRACE_NET_STREAM_END: false
269+
DTRACE_NET_STREAM_END: false,
270+
TextEncoder: false,
271+
TextDecoder: false
270272
},
271273
};

doc/api/errors.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,13 +794,13 @@ The stack trace is extended to include the point in time at which the
794794
<a id="ERR_ENCODING_INVALID_ENCODED_DATA"></a>
795795
### ERR_ENCODING_INVALID_ENCODED_DATA
796796

797-
Data provided to `util.TextDecoder()` API was invalid according to the encoding
797+
Data provided to `TextDecoder()` API was invalid according to the encoding
798798
provided.
799799

800800
<a id="ERR_ENCODING_NOT_SUPPORTED"></a>
801801
### ERR_ENCODING_NOT_SUPPORTED
802802

803-
Encoding provided to `util.TextDecoder()` API was not one of the
803+
Encoding provided to `TextDecoder()` API was not one of the
804804
[WHATWG Supported Encodings][].
805805

806806
<a id="ERR_FALSY_VALUE_REJECTION"></a>

doc/api/globals.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,25 @@ added: v0.0.1
138138

139139
[`setTimeout`] is described in the [timers][] section.
140140

141+
## TextDecoder
142+
<!-- YAML
143+
added: REPLACEME
144+
-->
145+
146+
<!-- type=global -->
147+
148+
The WHATWG `TextDecoder` class. See the [`TextDecoder`][] section.
149+
150+
## TextEncoder
151+
<!-- YAML
152+
added: REPLACEME
153+
-->
154+
155+
<!-- type=global -->
156+
157+
The WHATWG `TextEncoder` class. See the [`TextEncoder`][] section.
158+
159+
141160
## URL
142161
<!-- YAML
143162
added: v10.0.0
@@ -169,6 +188,8 @@ The WHATWG `URLSearchParams` class. See the [`URLSearchParams`][] section.
169188
[`setImmediate`]: timers.html#timers_setimmediate_callback_args
170189
[`setInterval`]: timers.html#timers_setinterval_callback_delay_args
171190
[`setTimeout`]: timers.html#timers_settimeout_callback_delay_args
191+
[`TextDecoder`]: util.html#util_class_util_textdecoder
192+
[`TextEncoder`]: util.html#util_class_util_textencoder
172193
[`URL`]: url.html#url_class_url
173194
[`URLSearchParams`]: url.html#url_class_urlsearchparams
174195
[buffer section]: buffer.html

doc/api/util.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,13 @@ The `'iso-8859-16'` encoding listed in the [WHATWG Encoding Standard][]
841841
is not supported.
842842

843843
### new TextDecoder([encoding[, options]])
844+
<!-- YAML
845+
added: v8.3.0
846+
changes:
847+
- version: REPLACEME
848+
pr-url: REPLACEME
849+
description: The class is now available on the global object.
850+
-->
844851

845852
* `encoding` {string} Identifies the `encoding` that this `TextDecoder` instance
846853
supports. **Default:** `'utf-8'`.
@@ -856,6 +863,8 @@ is not supported.
856863
Creates an new `TextDecoder` instance. The `encoding` may specify one of the
857864
supported encodings or an alias.
858865

866+
The `TextDecoder` class is also available on the global object.
867+
859868
### textDecoder.decode([input[, options]])
860869

861870
* `input` {ArrayBuffer|DataView|TypedArray} An `ArrayBuffer`, `DataView` or
@@ -895,6 +904,10 @@ mark.
895904
## Class: util.TextEncoder
896905
<!-- YAML
897906
added: v8.3.0
907+
changes:
908+
- version: REPLACEME
909+
pr-url: REPLACEME
910+
description: The class is now available on the global object.
898911
-->
899912

900913
An implementation of the [WHATWG Encoding Standard][] `TextEncoder` API. All
@@ -905,6 +918,8 @@ const encoder = new TextEncoder();
905918
const uint8array = encoder.encode('this is some data');
906919
```
907920

921+
The `TextEncoder` class is also available on the global object.
922+
908923
### textEncoder.encode([input])
909924

910925
* `input` {string} The text to encode. **Default:** an empty string.

lib/internal/bootstrap/node.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
setupGlobalTimeouts();
128128
setupGlobalConsole();
129129
setupGlobalURL();
130+
setupGlobalEncoding();
130131
}
131132

132133
if (process.binding('config').experimentalWorker) {
@@ -475,6 +476,24 @@
475476
});
476477
}
477478

479+
function setupGlobalEncoding() {
480+
const { TextEncoder, TextDecoder } = NativeModule.require('util');
481+
Object.defineProperties(global, {
482+
TextEncoder: {
483+
value: TextEncoder,
484+
writable: true,
485+
configurable: true,
486+
enumerable: false
487+
},
488+
TextDecoder: {
489+
value: TextDecoder,
490+
writable: true,
491+
configurable: true,
492+
enumerable: false
493+
}
494+
});
495+
}
496+
478497
function setupDOMException() {
479498
// Registers the constructor with C++.
480499
NativeModule.require('internal/domexception');

test/parallel/test-global-encoder.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
require('../common');
4+
const { strictEqual } = require('assert');
5+
const util = require('util');
6+
7+
strictEqual(TextDecoder, util.TextDecoder);
8+
strictEqual(TextEncoder, util.TextEncoder);

test/parallel/test-whatwg-encoding-fatal-streaming.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ if (!common.hasIntl)
88
common.skip('missing Intl');
99

1010
const assert = require('assert');
11-
const {
12-
TextDecoder
13-
} = require('util');
14-
1511

1612
{
1713
[

test/parallel/test-whatwg-encoding-surrogates-utf8.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
require('../common');
66

77
const assert = require('assert');
8-
const {
9-
TextDecoder,
10-
TextEncoder
11-
} = require('util');
128

139
const badStrings = [
1410
{

test/parallel/test-whatwg-encoding-textdecoder-fatal.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ if (!common.hasIntl)
88
common.skip('missing Intl');
99

1010
const assert = require('assert');
11-
const {
12-
TextDecoder
13-
} = require('util');
1411

1512
const bad = [
1613
{ encoding: 'utf-8', input: [0xFF], name: 'invalid code' },

test/parallel/test-whatwg-encoding-textdecoder-ignorebom.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
const common = require('../common');
66

77
const assert = require('assert');
8-
const {
9-
TextDecoder
10-
} = require('util');
118

129
const cases = [
1310
{

0 commit comments

Comments
 (0)