From b79829c5f5018eb99ea514ccb8e91b4eabc0f97c Mon Sep 17 00:00:00 2001 From: Sebastien Ahkrin Date: Mon, 6 Jan 2020 03:55:31 +0100 Subject: [PATCH] lib: replace String global with primordials PR-URL: https://github.com/nodejs/node/pull/35397 Reviewed-By: Luigi Pinca Reviewed-By: Jiawen Geng Reviewed-By: Pranshu Srivastava Reviewed-By: Rich Trott Reviewed-By: Anto Aravinth Reviewed-By: Ujjwal Sharma --- lib/.eslintrc.yaml | 2 ++ lib/_http_client.js | 1 + lib/assert.js | 1 + lib/events.js | 1 + lib/internal/assert/assertion_error.js | 1 + lib/internal/bootstrap/loaders.js | 1 + lib/internal/errors.js | 1 + lib/internal/event_target.js | 1 + lib/internal/http2/util.js | 6 ++++-- lib/internal/process/warning.js | 1 + lib/internal/readline/utils.js | 1 + lib/internal/url.js | 1 + lib/internal/util/inspect.js | 1 + lib/internal/validators.js | 1 + lib/internal/worker.js | 1 + lib/querystring.js | 1 + lib/tls.js | 18 ++++++++++++++---- lib/wasi.js | 1 + 18 files changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index 02d70f83f9..0f34afff08 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -49,6 +49,8 @@ rules: message: "Use `const { RegExp } = primordials;` instead of the global." - name: Set message: "Use `const { Set } = primordials;` instead of the global." + - name: String + message: "Use `const { String } = primordials;` instead of the global." - name: Symbol message: "Use `const { Symbol } = primordials;` instead of the global." - name: Uint16Array diff --git a/lib/_http_client.js b/lib/_http_client.js index 30ab901684..0aae57aed4 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -29,6 +29,7 @@ const { ObjectAssign, ObjectKeys, ObjectSetPrototypeOf, + String, Symbol } = primordials; diff --git a/lib/assert.js b/lib/assert.js index 6ad672d698..08ef182409 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -29,6 +29,7 @@ const { Map, NumberIsNaN, RegExpPrototypeTest, + String, } = primordials; const { Buffer } = require('buffer'); diff --git a/lib/events.js b/lib/events.js index c1343acbf3..7d3ee54018 100644 --- a/lib/events.js +++ b/lib/events.js @@ -35,6 +35,7 @@ const { PromiseResolve, ReflectApply, ReflectOwnKeys, + String, Symbol, SymbolFor, SymbolAsyncIterator diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index 3c2150c69e..c89219eef8 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -7,6 +7,7 @@ const { ObjectDefineProperty, ObjectGetPrototypeOf, ObjectKeys, + String, } = primordials; const { inspect } = require('internal/util/inspect'); diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js index c4ef1d12a2..127ef67c36 100644 --- a/lib/internal/bootstrap/loaders.js +++ b/lib/internal/bootstrap/loaders.js @@ -52,6 +52,7 @@ const { ObjectPrototypeHasOwnProperty, ReflectGet, SafeSet, + String, } = primordials; // Set up process.moduleLoadList. diff --git a/lib/internal/errors.js b/lib/internal/errors.js index ff4cea13b6..314c06c7b7 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -21,6 +21,7 @@ const { NumberIsInteger, ObjectDefineProperty, ObjectKeys, + String, StringPrototypeStartsWith, Symbol, SymbolFor, diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index f9dda75f04..4db6af26e3 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -9,6 +9,7 @@ const { ObjectAssign, ObjectDefineProperties, ObjectDefineProperty, + String, Symbol, SymbolFor, SymbolToStringTag, diff --git a/lib/internal/http2/util.js b/lib/internal/http2/util.js index 93994ca29e..b3fdf420c8 100644 --- a/lib/internal/http2/util.js +++ b/lib/internal/http2/util.js @@ -8,6 +8,8 @@ const { ObjectCreate, ObjectKeys, Set, + String, + StringFromCharCode, StringPrototypeToLowerCase, Symbol, } = primordials; @@ -459,8 +461,8 @@ const assertValidPseudoHeaderTrailer = hideStackFrames((key) => { }); const emptyArray = []; -const kNeverIndexFlag = String.fromCharCode(NGHTTP2_NV_FLAG_NO_INDEX); -const kNoHeaderFlags = String.fromCharCode(NGHTTP2_NV_FLAG_NONE); +const kNeverIndexFlag = StringFromCharCode(NGHTTP2_NV_FLAG_NO_INDEX); +const kNoHeaderFlags = StringFromCharCode(NGHTTP2_NV_FLAG_NONE); function mapToHeaders(map, assertValuePseudoHeader = assertValidPseudoHeader) { let ret = ''; diff --git a/lib/internal/process/warning.js b/lib/internal/process/warning.js index e2d41eac73..2822ef22f1 100644 --- a/lib/internal/process/warning.js +++ b/lib/internal/process/warning.js @@ -3,6 +3,7 @@ const { ArrayIsArray, Error, + String, } = primordials; const assert = require('internal/assert'); diff --git a/lib/internal/readline/utils.js b/lib/internal/readline/utils.js index d75b64dd46..408d12d01e 100644 --- a/lib/internal/readline/utils.js +++ b/lib/internal/readline/utils.js @@ -1,6 +1,7 @@ 'use strict'; const { + String, Symbol, } = primordials; diff --git a/lib/internal/url.js b/lib/internal/url.js index f56e006e90..3c464ffbd6 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -11,6 +11,7 @@ const { ObjectKeys, ReflectGetOwnPropertyDescriptor, ReflectOwnKeys, + String, Symbol, SymbolIterator, SymbolToStringTag, diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index a979539025..2a9bb18fdf 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -49,6 +49,7 @@ const { Set, SetPrototype, SetPrototypeValues, + String, StringPrototypeValueOf, SymbolPrototypeToString, SymbolPrototypeValueOf, diff --git a/lib/internal/validators.js b/lib/internal/validators.js index f9509a4882..2ef7d653d9 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -5,6 +5,7 @@ const { NumberIsInteger, NumberMAX_SAFE_INTEGER, NumberMIN_SAFE_INTEGER, + String, } = primordials; const { diff --git a/lib/internal/worker.js b/lib/internal/worker.js index c04484b3d8..c89c8cd4e2 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -11,6 +11,7 @@ const { ObjectEntries, Promise, PromiseResolve, + String, Symbol, SymbolFor, } = primordials; diff --git a/lib/querystring.js b/lib/querystring.js index 4d196633dc..04a21e8d07 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -29,6 +29,7 @@ const { MathAbs, ObjectCreate, ObjectKeys, + String, } = primordials; const { Buffer } = require('buffer'); diff --git a/lib/tls.js b/lib/tls.js index 2ccbe409c9..a46031ad7d 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -26,6 +26,10 @@ const { ArrayIsArray, ObjectDefineProperty, ObjectFreeze, + StringFromCharCode, + StringPrototypeCharCodeAt, + StringPrototypeReplace, + StringPrototypeSplit, } = primordials; const { @@ -137,11 +141,17 @@ function unfqdn(host) { return host.replace(/[.]$/, ''); } +// String#toLowerCase() is locale-sensitive so we use +// a conservative version that only lowercases A-Z. +function toLowerCase(c) { + return StringFromCharCode(32 + StringPrototypeCharCodeAt(c, 0)); +} + function splitHost(host) { - // String#toLowerCase() is locale-sensitive so we use - // a conservative version that only lowercases A-Z. - const replacer = (c) => String.fromCharCode(32 + c.charCodeAt(0)); - return unfqdn(host).replace(/[A-Z]/g, replacer).split('.'); + return StringPrototypeSplit( + StringPrototypeReplace(unfqdn(host), /[A-Z]/g, toLowerCase), + '.' + ); } function check(hostParts, pattern, wildcards) { diff --git a/lib/wasi.js b/lib/wasi.js index a764a1745a..072f19be92 100644 --- a/lib/wasi.js +++ b/lib/wasi.js @@ -4,6 +4,7 @@ const { ArrayPrototypePush, FunctionPrototypeBind, ObjectEntries, + String, Symbol, } = primordials;