Skip to content

Commit

Permalink
lib: move encodingsMap to internal/util
Browse files Browse the repository at this point in the history
PR-URL: #51044
Refs: nodejs/performance#136
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
  • Loading branch information
joyeecheung authored and marco-ippolito committed Feb 29, 2024
1 parent 4ebb944 commit 5a2d2da
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
6 changes: 1 addition & 5 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const {
normalizeEncoding,
kIsEncodingSymbol,
defineLazyProperties,
encodingsMap,
} = require('internal/util');
const {
isAnyArrayBuffer,
Expand All @@ -95,7 +96,6 @@ const {
const {
inspect: utilInspect,
} = require('internal/util/inspect');
const { encodings } = internalBinding('string_decoder');

const {
codes: {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -872,6 +877,7 @@ module.exports = {
defineReplaceableLazyAttribute,
deprecate,
emitExperimentalWarning,
encodingsMap,
exposeInterface,
exposeLazyInterfaces,
exposeNamespace,
Expand Down
15 changes: 7 additions & 8 deletions lib/string_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 5a2d2da

Please sign in to comment.