From 30c6b3e809610b720cb7d48ea9cc1c6319fbe685 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 | 1 + 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, 32 insertions(+), 4 deletions(-) diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index 02d70f83f9c62f..0f34afff0814a6 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 320ab7dcb996ba..a54cfb2d1b2146 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 6ad672d698602d..08ef18240925ba 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 48341c0b20f5b3..8570c0a8fbaea3 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 3c2150c69e4e09..c89219eef82224 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 c4ef1d12a2974c..127ef67c36eb64 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 656cb374905f3f..59172ba7e41c5a 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 8f0657c8d5b193..afaadc564396ff 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -8,6 +8,7 @@ const { NumberIsInteger, Object, ObjectDefineProperty, + String, Symbol, SymbolFor, SymbolToStringTag, diff --git a/lib/internal/http2/util.js b/lib/internal/http2/util.js index 8d47dbd53c0ca3..00561e0895d1b0 100644 --- a/lib/internal/http2/util.js +++ b/lib/internal/http2/util.js @@ -8,6 +8,7 @@ const { ObjectCreate, ObjectKeys, Set, + String, Symbol, } = primordials; diff --git a/lib/internal/process/warning.js b/lib/internal/process/warning.js index 877a58bbf53fa5..f10e07f6210c30 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 d75b64dd46beac..408d12d01e151d 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 25d107bacd5ce0..0a5be32e467715 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 aa45bc5d4f6b3a..b5315fc1527246 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 71726f700518e3..0a4c8b6b576af2 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 59434c91e59e3a..e521064fb33ea2 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 4d196633dcf2c7..04a21e8d07f24f 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 2ccbe409c96c2d..a46031ad7da90e 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 a764a1745ab0a9..072f19be9235ae 100644 --- a/lib/wasi.js +++ b/lib/wasi.js @@ -4,6 +4,7 @@ const { ArrayPrototypePush, FunctionPrototypeBind, ObjectEntries, + String, Symbol, } = primordials;