Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: make TextEncoder/TextDecoder global #22281

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,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
},
};
4 changes: 2 additions & 2 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
21 changes: 21 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,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'`.
Expand All @@ -856,6 +863,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
Expand Down Expand Up @@ -895,6 +904,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
Expand All @@ -905,6 +918,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.
Expand Down
19 changes: 19 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
setupGlobalTimeouts();
setupGlobalConsole();
setupGlobalURL();
setupGlobalEncoding();
}

if (process.binding('config').experimentalWorker) {
Expand Down Expand Up @@ -475,6 +476,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');
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-global-encoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

require('../common');
const { strictEqual } = require('assert');
const util = require('util');

strictEqual(TextDecoder, util.TextDecoder);
strictEqual(TextEncoder, util.TextEncoder);
4 changes: 0 additions & 4 deletions test/parallel/test-whatwg-encoding-fatal-streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ if (!common.hasIntl)
common.skip('missing Intl');

const assert = require('assert');
const {
TextDecoder
} = require('util');


{
[
Expand Down
4 changes: 0 additions & 4 deletions test/parallel/test-whatwg-encoding-surrogates-utf8.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
require('../common');

const assert = require('assert');
const {
TextDecoder,
TextEncoder
} = require('util');

const badStrings = [
{
Expand Down
3 changes: 0 additions & 3 deletions test/parallel/test-whatwg-encoding-textdecoder-fatal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
3 changes: 0 additions & 3 deletions test/parallel/test-whatwg-encoding-textdecoder-ignorebom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
const common = require('../common');

const assert = require('assert');
const {
TextDecoder
} = require('util');

const cases = [
{
Expand Down
3 changes: 0 additions & 3 deletions test/parallel/test-whatwg-encoding-textdecoder-streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ if (!common.hasIntl)
common.skip('missing Intl');

const assert = require('assert');
const {
TextDecoder
} = require('util');

const bad = [
{
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-whatwg-encoding-textdecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
require('../common');

const assert = require('assert');
const {
TextDecoder,
TextEncoder
} = require('util');

const bad = [
{
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-whatwg-encoding-textencoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down