diff --git a/doc/api/errors.md b/doc/api/errors.md index 422d41348204bd..3edc09048fa1be 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -764,13 +764,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..c38818cc7f020c 100644 --- a/doc/api/globals.md +++ b/doc/api/globals.md @@ -156,6 +156,24 @@ added: v10.0.0 The WHATWG `URLSearchParams` class. See the [`URLSearchParams`][] section. +## TextEncoder + + + + +The WHATWG `TextEncoder` class. See the [`TextEncoder`][] section. + +## TextDecoder + + + + +The WHATWG `TextDecoder` class. See the [`TextDecoder`][] section. + [`__dirname`]: modules.html#modules_dirname [`__filename`]: modules.html#modules_filename [`clearImmediate`]: timers.html#timers_clearimmediate_immediate @@ -171,6 +189,8 @@ The WHATWG `URLSearchParams` class. See the [`URLSearchParams`][] section. [`setTimeout`]: timers.html#timers_settimeout_callback_delay_args [`URL`]: url.html#url_class_url [`URLSearchParams`]: url.html#url_class_urlsearchparams +[`TextEncoder`]: util.html#util_class_textencoder +[`TextDecoder`]: util.html#util_class_textdecoder [buffer section]: buffer.html [built-in objects]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects [module system documentation]: modules.html diff --git a/doc/api/util.md b/doc/api/util.md index b0e511c3cd4d6b..0255801a2c5f8d 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -771,9 +771,13 @@ added: v8.0.0 * {symbol} that can be used to declare custom promisified variants of functions, see [Custom promisified functions][]. -## Class: util.TextDecoder +## Class: TextDecoder An implementation of the [WHATWG Encoding Standard][] `TextDecoder` API. @@ -908,9 +912,13 @@ thrown. The value will be `true` if the decoding result will include the byte order mark. -## Class: util.TextEncoder +## Class: TextEncoder An implementation of the [WHATWG Encoding Standard][] `TextEncoder` API. All diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 5993cc71f7d404..841cae5eef2a0d 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -75,6 +75,7 @@ setupGlobalTimeouts(); setupGlobalConsole(); setupGlobalURL(); + setupGlobalTextEncoderDecoder(); } // Ensure setURLConstructor() is called before the native @@ -381,6 +382,24 @@ }); } + function setupGlobalTextEncoderDecoder() { + const { TextEncoder, TextDecoder } = NativeModule.require('internal/util'); + Object.defineProperties(global, { + TextEncoder: { + value: TextEncoder, + writable: true, + configurable: true, + enumerable: false + }, + TextDecoder: { + value: TextDecoder, + writable: true, + configurable: true, + enumerable: false + } + }); + } + function setupInspector(originalConsole, wrappedConsole, CJSModule) { if (!process.config.variables.v8_enable_inspector) { return; diff --git a/test/parallel/test-whatwg-encoding-global.js b/test/parallel/test-whatwg-encoding-global.js new file mode 100644 index 00000000000000..be9ccd3306049c --- /dev/null +++ b/test/parallel/test-whatwg-encoding-global.js @@ -0,0 +1,25 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); +const { TextEncoder, TextDecoder } = require('util'); + +assert.deepStrictEqual( + Object.getOwnPropertyDescriptor(global, 'TextEncoder'), + { + value: TextEncoder, + writable: true, + configurable: true, + enumerable: false + } +); + +assert.deepStrictEqual( + Object.getOwnPropertyDescriptor(global, 'TextDecoder'), + { + value: TextDecoder, + writable: true, + configurable: true, + enumerable: false + } +);