util: make TextEncoder/TextDecoder global
Fixes: https://github.com/nodejs/node/issues/20365 PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
efe0bbcd2f
commit
932be0164f
@ -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
|
||||
},
|
||||
};
|
||||
|
@ -794,13 +794,13 @@ The stack trace is extended to include the point in time at which the
|
||||
<a id="ERR_ENCODING_INVALID_ENCODED_DATA"></a>
|
||||
### 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.
|
||||
|
||||
<a id="ERR_ENCODING_NOT_SUPPORTED"></a>
|
||||
### 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][].
|
||||
|
||||
<a id="ERR_FALSY_VALUE_REJECTION"></a>
|
||||
|
@ -138,6 +138,25 @@ added: v0.0.1
|
||||
|
||||
[`setTimeout`] is described in the [timers][] section.
|
||||
|
||||
## TextDecoder
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
<!-- type=global -->
|
||||
|
||||
The WHATWG `TextDecoder` class. See the [`TextDecoder`][] section.
|
||||
|
||||
## TextEncoder
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
<!-- type=global -->
|
||||
|
||||
The WHATWG `TextEncoder` class. See the [`TextEncoder`][] section.
|
||||
|
||||
|
||||
## URL
|
||||
<!-- YAML
|
||||
added: v10.0.0
|
||||
@ -169,6 +188,8 @@ The WHATWG `URLSearchParams` class. See the [`URLSearchParams`][] section.
|
||||
[`setImmediate`]: timers.html#timers_setimmediate_callback_args
|
||||
[`setInterval`]: timers.html#timers_setinterval_callback_delay_args
|
||||
[`setTimeout`]: timers.html#timers_settimeout_callback_delay_args
|
||||
[`TextDecoder`]: util.html#util_class_util_textdecoder
|
||||
[`TextEncoder`]: util.html#util_class_util_textencoder
|
||||
[`URL`]: url.html#url_class_url
|
||||
[`URLSearchParams`]: url.html#url_class_urlsearchparams
|
||||
[buffer section]: buffer.html
|
||||
|
@ -878,6 +878,13 @@ The `'iso-8859-16'` encoding listed in the [WHATWG Encoding Standard][]
|
||||
is not supported.
|
||||
|
||||
### new TextDecoder([encoding[, options]])
|
||||
<!-- YAML
|
||||
added: v8.3.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: REPLACEME
|
||||
description: The class is now available on the global object.
|
||||
-->
|
||||
|
||||
* `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
|
||||
<!-- YAML
|
||||
added: v8.3.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: REPLACEME
|
||||
description: The class is now available on the global object.
|
||||
-->
|
||||
|
||||
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.
|
||||
|
@ -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');
|
||||
|
8
test/parallel/test-global-encoder.js
Normal file
8
test/parallel/test-global-encoder.js
Normal file
@ -0,0 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const { strictEqual } = require('assert');
|
||||
const util = require('util');
|
||||
|
||||
strictEqual(TextDecoder, util.TextDecoder);
|
||||
strictEqual(TextEncoder, util.TextEncoder);
|
@ -8,10 +8,6 @@ if (!common.hasIntl)
|
||||
common.skip('missing Intl');
|
||||
|
||||
const assert = require('assert');
|
||||
const {
|
||||
TextDecoder
|
||||
} = require('util');
|
||||
|
||||
|
||||
{
|
||||
[
|
||||
|
@ -5,10 +5,6 @@
|
||||
require('../common');
|
||||
|
||||
const assert = require('assert');
|
||||
const {
|
||||
TextDecoder,
|
||||
TextEncoder
|
||||
} = require('util');
|
||||
|
||||
const badStrings = [
|
||||
{
|
||||
|
@ -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' },
|
||||
|
@ -5,9 +5,6 @@
|
||||
const common = require('../common');
|
||||
|
||||
const assert = require('assert');
|
||||
const {
|
||||
TextDecoder
|
||||
} = require('util');
|
||||
|
||||
const cases = [
|
||||
{
|
||||
|
@ -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';
|
||||
|
@ -8,9 +8,6 @@ if (!common.hasIntl)
|
||||
common.skip('missing Intl');
|
||||
|
||||
const assert = require('assert');
|
||||
const {
|
||||
TextDecoder
|
||||
} = require('util');
|
||||
|
||||
const bad = [
|
||||
{
|
||||
|
@ -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,
|
||||
|
@ -5,10 +5,6 @@
|
||||
require('../common');
|
||||
|
||||
const assert = require('assert');
|
||||
const {
|
||||
TextDecoder,
|
||||
TextEncoder
|
||||
} = require('util');
|
||||
|
||||
const bad = [
|
||||
{
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user