From 5a2d2daad55a313e0c30add79a03bac762a98eea Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 4 Dec 2023 02:46:11 +0100 Subject: [PATCH] lib: move encodingsMap to internal/util MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/51044 Refs: https://github.com/nodejs/performance/issues/136 Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Yagiz Nizipli --- lib/buffer.js | 6 +----- lib/internal/util.js | 6 ++++++ lib/string_decoder.js | 15 +++++++-------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 59a125960c276f..a8d07342e15eaa 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -85,6 +85,7 @@ const { normalizeEncoding, kIsEncodingSymbol, defineLazyProperties, + encodingsMap, } = require('internal/util'); const { isAnyArrayBuffer, @@ -95,7 +96,6 @@ const { const { inspect: utilInspect, } = require('internal/util/inspect'); -const { encodings } = internalBinding('string_decoder'); const { codes: { @@ -149,10 +149,6 @@ const constants = ObjectDefineProperties({}, { Buffer.poolSize = 8 * 1024; let poolSize, poolOffset, allocPool; -const encodingsMap = { __proto__: null }; -for (let i = 0; i < encodings.length; ++i) - encodingsMap[encodings[i]] = i; - function createPool() { poolSize = Buffer.poolSize; allocPool = createUnsafeBuffer(poolSize).buffer; diff --git a/lib/internal/util.js b/lib/internal/util.js index 603f8164a14405..226a57ddda55b3 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -65,6 +65,7 @@ const { } = internalBinding('util'); const { isNativeError, isPromise } = internalBinding('types'); const { getOptionValue } = require('internal/options'); +const { encodings } = internalBinding('string_decoder'); const noCrypto = !process.versions.openssl; @@ -859,6 +860,10 @@ class WeakReference { } } +const encodingsMap = { __proto__: null }; +for (let i = 0; i < encodings.length; ++i) + encodingsMap[encodings[i]] = i; + module.exports = { getLazy, assertCrypto, @@ -872,6 +877,7 @@ module.exports = { defineReplaceableLazyAttribute, deprecate, emitExperimentalWarning, + encodingsMap, exposeInterface, exposeLazyInterfaces, exposeNamespace, diff --git a/lib/string_decoder.js b/lib/string_decoder.js index 2ed2de60de6ee5..c0dbfe2b5e92a8 100644 --- a/lib/string_decoder.js +++ b/lib/string_decoder.js @@ -38,15 +38,18 @@ const { kSize, decode, flush, - encodings, } = internalBinding('string_decoder'); -const internalUtil = require('internal/util'); +const { + kIsEncodingSymbol, + encodingsMap, + normalizeEncoding: _normalizeEncoding, +} = require('internal/util'); const { ERR_INVALID_ARG_TYPE, ERR_INVALID_THIS, ERR_UNKNOWN_ENCODING, } = require('internal/errors').codes; -const isEncoding = Buffer[internalUtil.kIsEncodingSymbol]; +const isEncoding = Buffer[kIsEncodingSymbol]; const kNativeDecoder = Symbol('kNativeDecoder'); @@ -60,7 +63,7 @@ const kNativeDecoder = Symbol('kNativeDecoder'); * @throws {TypeError} Throws an error when encoding is invalid */ function normalizeEncoding(enc) { - const nenc = internalUtil.normalizeEncoding(enc); + const nenc = _normalizeEncoding(enc); if (nenc === undefined) { if (Buffer.isEncoding === isEncoding || !Buffer.isEncoding(enc)) throw new ERR_UNKNOWN_ENCODING(enc); @@ -69,10 +72,6 @@ function normalizeEncoding(enc) { return nenc; } -const encodingsMap = {}; -for (let i = 0; i < encodings.length; ++i) - encodingsMap[encodings[i]] = i; - /** * StringDecoder provides an interface for efficiently splitting a series of * buffers into a series of JS strings without breaking apart multi-byte