Skip to content

Commit 932be01

Browse files
jasnelladdaleax
authored andcommitted
util: make TextEncoder/TextDecoder global
Fixes: #20365 PR-URL: #22281 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent efe0bbc commit 932be01

15 files changed

+68
-29
lines changed

.eslintrc.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ module.exports = {
271271
DTRACE_HTTP_SERVER_REQUEST: false,
272272
DTRACE_HTTP_SERVER_RESPONSE: false,
273273
DTRACE_NET_SERVER_CONNECTION: false,
274-
DTRACE_NET_STREAM_END: false
274+
DTRACE_NET_STREAM_END: false,
275+
TextEncoder: false,
276+
TextDecoder: false
275277
},
276278
};

doc/api/errors.md

+2-2
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

+21
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

+15
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,13 @@ The `'iso-8859-16'` encoding listed in the [WHATWG Encoding Standard][]
878878
is not supported.
879879

880880
### new TextDecoder([encoding[, options]])
881+
<!-- YAML
882+
added: v8.3.0
883+
changes:
884+
- version: REPLACEME
885+
pr-url: REPLACEME
886+
description: The class is now available on the global object.
887+
-->
881888

882889
* `encoding` {string} Identifies the `encoding` that this `TextDecoder` instance
883890
supports. **Default:** `'utf-8'`.
@@ -893,6 +900,8 @@ is not supported.
893900
Creates an new `TextDecoder` instance. The `encoding` may specify one of the
894901
supported encodings or an alias.
895902

903+
The `TextDecoder` class is also available on the global object.
904+
896905
### textDecoder.decode([input[, options]])
897906

898907
* `input` {ArrayBuffer|DataView|TypedArray} An `ArrayBuffer`, `DataView` or
@@ -932,6 +941,10 @@ mark.
932941
## Class: util.TextEncoder
933942
<!-- YAML
934943
added: v8.3.0
944+
changes:
945+
- version: REPLACEME
946+
pr-url: REPLACEME
947+
description: The class is now available on the global object.
935948
-->
936949

937950
An implementation of the [WHATWG Encoding Standard][] `TextEncoder` API. All
@@ -942,6 +955,8 @@ const encoder = new TextEncoder();
942955
const uint8array = encoder.encode('this is some data');
943956
```
944957

958+
The `TextEncoder` class is also available on the global object.
959+
945960
### textEncoder.encode([input])
946961

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

lib/internal/bootstrap/node.js

+19
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) {
@@ -476,6 +477,24 @@
476477
});
477478
}
478479

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

test/parallel/test-global-encoder.js

+8
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

-4
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

-4
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

-3
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

-3
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
{

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

-3
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 string =
1310
'\x00123ABCabc\x80\xFF\u0100\u1000\uFFFD\uD800\uDC00\uDBFF\uDFFF';

test/parallel/test-whatwg-encoding-textdecoder-utf16-surrogates.js

-3
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
{

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

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
const common = require('../common');
55

66
const assert = require('assert');
7-
const { TextDecoder, TextEncoder } = require('util');
87
const { customInspectSymbol: inspect } = require('internal/util');
98

109
const buf = Buffer.from([0xef, 0xbb, 0xbf, 0x74, 0x65,

test/parallel/test-whatwg-encoding-textencoder-utf16-surrogates.js

-4
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 bad = [
1410
{

test/parallel/test-whatwg-encoding-textencoder.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
const common = require('../common');
55

66
const assert = require('assert');
7-
const { TextDecoder, TextEncoder } = require('util');
87
const { customInspectSymbol: inspect } = require('internal/util');
98

109
const encoded = Buffer.from([0xef, 0xbb, 0xbf, 0x74, 0x65,

0 commit comments

Comments
 (0)