From c931b91bdde76d278de74639ad20b208dd54806f 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 --- 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 b32c0e8472b320..a159ed1c0e8827 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; @@ -860,6 +861,10 @@ class WeakReference { } } +const encodingsMap = { __proto__: null }; +for (let i = 0; i < encodings.length; ++i) + encodingsMap[encodings[i]] = i; + module.exports = { getLazy, assertCrypto, @@ -873,6 +878,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