From ffd137a31613e4105bb2f5fcfa3b8292c939fc4e Mon Sep 17 00:00:00 2001 From: Mark Tiedemann Date: Thu, 10 May 2018 19:14:48 +0200 Subject: [PATCH 1/6] util: expose the WHATWG Encoding API globally This commit makes `util.TextEncoder` and `util.TextDecoder` available on the global object. --- doc/api/errors.md | 4 ++-- doc/api/globals.md | 20 ++++++++++++++++ doc/api/util.md | 12 ++++++++-- lib/internal/bootstrap/node.js | 19 +++++++++++++++ test/common/index.mjs | 9 +++++++ test/parallel/test-whatwg-encoding-global.js | 25 ++++++++++++++++++++ 6 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-whatwg-encoding-global.js 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..9291859dd0b117 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..b02f286ad44226 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/common/index.mjs b/test/common/index.mjs index 49b9e56bd9a10b..afc0570b810938 100644 --- a/test/common/index.mjs +++ b/test/common/index.mjs @@ -73,6 +73,15 @@ export function leakedGlobals() { knownGlobals.push(Symbol); } + // WHATWG APIs + if (global.TextEncoder) { + knownGlobals.push(TextEncoder); + } + + if (global.TextDecoder) { + knownGlobals.push(TextDecoder) + } + const leaked = []; for (const val in global) { diff --git a/test/parallel/test-whatwg-encoding-global.js b/test/parallel/test-whatwg-encoding-global.js new file mode 100644 index 00000000000000..4c2478af2ffa68 --- /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 + } +); \ No newline at end of file From 651ad3480a986e6e3bd41f8bfa0addb207f57335 Mon Sep 17 00:00:00 2001 From: Mark Tiedemann Date: Sun, 13 May 2018 14:22:41 +0200 Subject: [PATCH 2/6] Add missing new-line (see: https://github.com/nodejs/node/pull/20662#discussion_r187405915) --- test/parallel/test-whatwg-encoding-global.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-whatwg-encoding-global.js b/test/parallel/test-whatwg-encoding-global.js index 4c2478af2ffa68..be9ccd3306049c 100644 --- a/test/parallel/test-whatwg-encoding-global.js +++ b/test/parallel/test-whatwg-encoding-global.js @@ -22,4 +22,4 @@ assert.deepStrictEqual( configurable: true, enumerable: false } -); \ No newline at end of file +); From 702099510b9f988b1a4fd1e3f9cd62e559de45f7 Mon Sep 17 00:00:00 2001 From: Mark Tiedemann Date: Sun, 13 May 2018 14:23:32 +0200 Subject: [PATCH 3/6] Replace PR-number placeholder (see: https://github.com/nodejs/node/pull/20662#discussion_r187435446) --- doc/api/util.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index b02f286ad44226..72346f6c36dd8f 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -776,7 +776,7 @@ see [Custom promisified functions][]. added: v8.3.0 changes: - version: v11.0.0 - pr-url: https://github.com/nodejs/node/pull/TO_DO + pr-url: https://github.com/nodejs/node/pull/20662 description: The class is now available on the global object. --> @@ -917,7 +917,7 @@ mark. added: v8.3.0 changes: - version: v11.0.0 - pr-url: https://github.com/nodejs/node/pull/TO_DO + pr-url: https://github.com/nodejs/node/pull/20662 description: The class is now available on the global object. --> From 5820e161214ba315ab70a1239063bf90f090d6e7 Mon Sep 17 00:00:00 2001 From: Mark Tiedemann Date: Sun, 13 May 2018 14:29:55 +0200 Subject: [PATCH 4/6] Replace version number with REPLACEME placeholder (see: https://github.com/nodejs/node/pull/20662#discussion_r187435637) --- doc/api/globals.md | 4 ++-- doc/api/util.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/api/globals.md b/doc/api/globals.md index 9291859dd0b117..c38818cc7f020c 100644 --- a/doc/api/globals.md +++ b/doc/api/globals.md @@ -158,7 +158,7 @@ The WHATWG `URLSearchParams` class. See the [`URLSearchParams`][] section. ## TextEncoder @@ -167,7 +167,7 @@ The WHATWG `TextEncoder` class. See the [`TextEncoder`][] section. ## TextDecoder diff --git a/doc/api/util.md b/doc/api/util.md index 72346f6c36dd8f..0255801a2c5f8d 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -775,7 +775,7 @@ see [Custom promisified functions][]. @@ -916,7 +916,7 @@ mark. From 98b1684869919af34d838fadb9fb3cd7becdc6b2 Mon Sep 17 00:00:00 2001 From: Mark Tiedemann Date: Sun, 13 May 2018 23:19:01 +0200 Subject: [PATCH 5/6] Add TextDecoder and TextEncoder to known test globals (see: https://github.com/nodejs/node/pull/20662#discussion_r187808371) --- test/common/index.js | 4 +++- test/common/index.mjs | 13 +++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/test/common/index.js b/test/common/index.js index 07c0992d65e8d2..ca011dd66d5030 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -316,7 +316,9 @@ let knownGlobals = [ process, setImmediate, setInterval, - setTimeout + setTimeout, + TextDecoder, + TextEncoder ]; if (global.gc) { diff --git a/test/common/index.mjs b/test/common/index.mjs index afc0570b810938..bb6fa0230a8a12 100644 --- a/test/common/index.mjs +++ b/test/common/index.mjs @@ -14,7 +14,9 @@ let knownGlobals = [ process, setImmediate, setInterval, - setTimeout + setTimeout, + TextDecoder, + TextEncoder ]; if (process.env.NODE_TEST_KNOWN_GLOBALS) { @@ -73,15 +75,6 @@ export function leakedGlobals() { knownGlobals.push(Symbol); } - // WHATWG APIs - if (global.TextEncoder) { - knownGlobals.push(TextEncoder); - } - - if (global.TextDecoder) { - knownGlobals.push(TextDecoder) - } - const leaked = []; for (const val in global) { From a6637f0233ab81685a4f866be2c4c9c9aa7043d2 Mon Sep 17 00:00:00 2001 From: Mark Tiedemann Date: Fri, 18 May 2018 19:21:54 +0200 Subject: [PATCH 6/6] Reverse common test file changes (see: https://github.com/nodejs/node/pull/20662#discussion_r189142132) --- test/common/index.js | 4 +--- test/common/index.mjs | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/test/common/index.js b/test/common/index.js index ca011dd66d5030..07c0992d65e8d2 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -316,9 +316,7 @@ let knownGlobals = [ process, setImmediate, setInterval, - setTimeout, - TextDecoder, - TextEncoder + setTimeout ]; if (global.gc) { diff --git a/test/common/index.mjs b/test/common/index.mjs index bb6fa0230a8a12..49b9e56bd9a10b 100644 --- a/test/common/index.mjs +++ b/test/common/index.mjs @@ -14,9 +14,7 @@ let knownGlobals = [ process, setImmediate, setInterval, - setTimeout, - TextDecoder, - TextEncoder + setTimeout ]; if (process.env.NODE_TEST_KNOWN_GLOBALS) {