GeezerVison is showing github.com — util: refactor to use more primordials · nodejs/node@64bf2f2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js +33-22Lines changed: 33 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | const { | 4 | 4 | ArrayFrom, | 5 | 5 | ArrayIsArray, | | 6 | + ArrayPrototypePop, | | 7 | + ArrayPrototypePush, | | 8 | + ArrayPrototypeSlice, | | 9 | + ArrayPrototypeSort, | 6 | 10 | Error, | 7 | | - Map, | 8 | 11 | ObjectCreate, | 9 | 12 | ObjectDefineProperties, | 10 | 13 | ObjectDefineProperty, | @@ -13,8 +16,14 @@ const { | 13 | 16 | ObjectGetPrototypeOf, | 14 | 17 | ObjectSetPrototypeOf, | 15 | 18 | Promise, | | 19 | + ReflectApply, | 16 | 20 | ReflectConstruct, | 17 | | - Set, | | 21 | + RegExpPrototypeTest, | | 22 | + SafeMap, | | 23 | + SafeSet, | | 24 | + StringPrototypeReplace, | | 25 | + StringPrototypeToLowerCase, | | 26 | + StringPrototypeToUpperCase, | 18 | 27 | Symbol, | 19 | 28 | SymbolFor, | 20 | 29 | } = primordials; | @@ -40,12 +49,12 @@ const { isNativeError } = internalBinding('types'); | 40 | 49 |
| 41 | 50 | const noCrypto = !process.versions.openssl; | 42 | 51 |
| 43 | | -const experimentalWarnings = new Set(); | | 52 | +const experimentalWarnings = new SafeSet(); | 44 | 53 |
| 45 | 54 | const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex | 46 | 55 |
| 47 | 56 | function removeColors(str) { | 48 | | - return str.replace(colorRegExp, ''); | | 57 | + return StringPrototypeReplace(str, colorRegExp, ''); | 49 | 58 | } | 50 | 59 |
| 51 | 60 | function isError(e) { | @@ -57,7 +66,7 @@ function isError(e) { | 57 | 66 |
| 58 | 67 | // Keep a list of deprecation codes that have been warned on so we only warn on | 59 | 68 | // each one once. | 60 | | -const codesWarned = new Set(); | | 69 | +const codesWarned = new SafeSet(); | 61 | 70 |
| 62 | 71 | // Mark that a method should not be used. | 63 | 72 | // Returns a modified function which warns once by default. | @@ -86,7 +95,7 @@ function deprecate(fn, msg, code) { | 86 | 95 | if (new.target) { | 87 | 96 | return ReflectConstruct(fn, args, new.target); | 88 | 97 | } | 89 | | - return fn.apply(this, args); | | 98 | + return ReflectApply(fn, this, args); | 90 | 99 | } | 91 | 100 |
| 92 | 101 | // The wrapper will keep the same prototype as fn to maintain prototype chain | @@ -132,12 +141,13 @@ function slowCases(enc) { | 132 | 141 | case 4: | 133 | 142 | if (enc === 'UTF8') return 'utf8'; | 134 | 143 | if (enc === 'ucs2' || enc === 'UCS2') return 'utf16le'; | 135 | | - enc = `${enc}`.toLowerCase(); | | 144 | + enc = StringPrototypeToLowerCase(`${enc}`); | 136 | 145 | if (enc === 'utf8') return 'utf8'; | 137 | 146 | if (enc === 'ucs2') return 'utf16le'; | 138 | 147 | break; | 139 | 148 | case 3: | 140 | | - if (enc === 'hex' || enc === 'HEX' || `${enc}`.toLowerCase() === 'hex') | | 149 | + if (enc === 'hex' || enc === 'HEX' || | | 150 | + StringPrototypeToLowerCase(`${enc}`) === 'hex') | 141 | 151 | return 'hex'; | 142 | 152 | break; | 143 | 153 | case 5: | @@ -146,7 +156,7 @@ function slowCases(enc) { | 146 | 156 | if (enc === 'UTF-8') return 'utf8'; | 147 | 157 | if (enc === 'ASCII') return 'ascii'; | 148 | 158 | if (enc === 'UCS-2') return 'utf16le'; | 149 | | - enc = `${enc}`.toLowerCase(); | | 159 | + enc = StringPrototypeToLowerCase(`${enc}`); | 150 | 160 | if (enc === 'utf-8') return 'utf8'; | 151 | 161 | if (enc === 'ascii') return 'ascii'; | 152 | 162 | if (enc === 'ucs-2') return 'utf16le'; | @@ -156,18 +166,18 @@ function slowCases(enc) { | 156 | 166 | if (enc === 'latin1' || enc === 'binary') return 'latin1'; | 157 | 167 | if (enc === 'BASE64') return 'base64'; | 158 | 168 | if (enc === 'LATIN1' || enc === 'BINARY') return 'latin1'; | 159 | | - enc = `${enc}`.toLowerCase(); | | 169 | + enc = StringPrototypeToLowerCase(`${enc}`); | 160 | 170 | if (enc === 'base64') return 'base64'; | 161 | 171 | if (enc === 'latin1' || enc === 'binary') return 'latin1'; | 162 | 172 | break; | 163 | 173 | case 7: | 164 | 174 | if (enc === 'utf16le' || enc === 'UTF16LE' || | 165 | | - `${enc}`.toLowerCase() === 'utf16le') | | 175 | + StringPrototypeToLowerCase(`${enc}`) === 'utf16le') | 166 | 176 | return 'utf16le'; | 167 | 177 | break; | 168 | 178 | case 8: | 169 | 179 | if (enc === 'utf-16le' || enc === 'UTF-16LE' || | 170 | | - `${enc}`.toLowerCase() === 'utf-16le') | | 180 | + StringPrototypeToLowerCase(`${enc}`) === 'utf-16le') | 171 | 181 | return 'utf16le'; | 172 | 182 | break; | 173 | 183 | default: | @@ -184,25 +194,25 @@ function emitExperimentalWarning(feature) { | 184 | 194 | } | 185 | 195 |
| 186 | 196 | function filterDuplicateStrings(items, low) { | 187 | | - const map = new Map(); | | 197 | + const map = new SafeMap(); | 188 | 198 | for (let i = 0; i < items.length; i++) { | 189 | 199 | const item = items[i]; | 190 | | - const key = item.toLowerCase(); | | 200 | + const key = StringPrototypeToLowerCase(item); | 191 | 201 | if (low) { | 192 | 202 | map.set(key, key); | 193 | 203 | } else { | 194 | 204 | map.set(key, item); | 195 | 205 | } | 196 | 206 | } | 197 | | - return ArrayFrom(map.values()).sort(); | | 207 | + return ArrayPrototypeSort(ArrayFrom(map.values())); | 198 | 208 | } | 199 | 209 |
| 200 | 210 | function cachedResult(fn) { | 201 | 211 | let result; | 202 | 212 | return () => { | 203 | 213 | if (result === undefined) | 204 | 214 | result = fn(); | 205 | | - return result.slice(); | | 215 | + return ArrayPrototypeSlice(result); | 206 | 216 | }; | 207 | 217 | } | 208 | 218 |
| @@ -244,7 +254,7 @@ function convertToValidSignal(signal) { | 244 | 254 | return signal; | 245 | 255 |
| 246 | 256 | if (typeof signal === 'string') { | 247 | | - const signalName = signals[signal.toUpperCase()]; | | 257 | + const signalName = signals[StringPrototypeToUpperCase(signal)]; | 248 | 258 | if (signalName) return signalName; | 249 | 259 | } | 250 | 260 |
| @@ -294,7 +304,7 @@ function promisify(original) { | 294 | 304 |
| 295 | 305 | function fn(...args) { | 296 | 306 | return new Promise((resolve, reject) => { | 297 | | - original.call(this, ...args, (err, ...values) => { | | 307 | + ArrayPrototypePush(args, (err, ...values) => { | 298 | 308 | if (err) { | 299 | 309 | return reject(err); | 300 | 310 | } | @@ -307,6 +317,7 @@ function promisify(original) { | 307 | 317 | resolve(values[0]); | 308 | 318 | } | 309 | 319 | }); | | 320 | + ReflectApply(original, this, args); | 310 | 321 | }); | 311 | 322 | } | 312 | 323 |
| @@ -343,7 +354,7 @@ function join(output, separator) { | 343 | 354 | function spliceOne(list, index) { | 344 | 355 | for (; index + 1 < list.length; index++) | 345 | 356 | list[index] = list[index + 1]; | 346 | | - list.pop(); | | 357 | + ArrayPrototypePop(list); | 347 | 358 | } | 348 | 359 |
| 349 | 360 | const kNodeModulesRE = /^(.*)[\\/]node_modules[\\/]/; | @@ -376,9 +387,9 @@ function isInsideNodeModules() { | 376 | 387 | const filename = frame.getFileName(); | 377 | 388 | // If a filename does not start with / or contain \, | 378 | 389 | // it's likely from Node.js core. | 379 | | - if (!/^\/|\\/.test(filename)) | | 390 | + if (!RegExpPrototypeTest(/^\/|\\/, filename)) | 380 | 391 | continue; | 381 | | - return kNodeModulesRE.test(filename); | | 392 | + return RegExpPrototypeTest(kNodeModulesRE, filename); | 382 | 393 | } | 383 | 394 | } | 384 | 395 | return false; | @@ -389,7 +400,7 @@ function once(callback) { | 389 | 400 | return function(...args) { | 390 | 401 | if (called) return; | 391 | 402 | called = true; | 392 | | - callback.apply(this, args); | | 403 | + ReflectApply(callback, this, args); | 393 | 404 | }; | 394 | 405 | } | 395 | 406 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util/comparisons.js +20-14Lines changed: 20 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { | 4 | 4 | ArrayIsArray, | | 5 | + ArrayPrototypeFilter, | | 6 | + ArrayPrototypePush, | 5 | 7 | BigIntPrototypeValueOf, | 6 | 8 | BooleanPrototypeValueOf, | 7 | 9 | DatePrototypeGetTime, | 8 | 10 | Error, | 9 | | - Map, | 10 | 11 | NumberIsNaN, | 11 | 12 | NumberPrototypeValueOf, | 12 | 13 | ObjectGetOwnPropertySymbols, | @@ -16,10 +17,11 @@ const { | 16 | 17 | ObjectPrototypeHasOwnProperty, | 17 | 18 | ObjectPrototypePropertyIsEnumerable, | 18 | 19 | ObjectPrototypeToString, | 19 | | - Set, | | 20 | + SafeMap, | | 21 | + SafeSet, | 20 | 22 | StringPrototypeValueOf, | 21 | 23 | SymbolPrototypeValueOf, | 22 | | - SymbolToStringTag, | | 24 | + TypedArrayPrototypeGetSymbolToStringTag, | 23 | 25 | Uint8Array, | 24 | 26 | } = primordials; | 25 | 27 |
| @@ -126,7 +128,7 @@ function isEqualBoxedPrimitive(val1, val2) { | 126 | 128 |
| 127 | 129 | function isIdenticalTypedArrayType(a, b) { | 128 | 130 | // Fast path to reduce type checks in the common case. | 129 | | - const check = types[`is${a[SymbolToStringTag]}`]; | | 131 | + const check = types[`is${TypedArrayPrototypeGetSymbolToStringTag(a)}`]; | 130 | 132 | if (check !== undefined && check(a)) { | 131 | 133 | return check(b); | 132 | 134 | } | @@ -150,8 +152,9 @@ function isIdenticalTypedArrayType(a, b) { | 150 | 152 | } | 151 | 153 | /* c8 ignore next 4 */ | 152 | 154 | assert.fail( | 153 | | - `Unknown TypedArray type checking ${a[SymbolToStringTag]} ${a}\n` + | 154 | | - `and ${b[SymbolToStringTag]} ${b}` | | 155 | + 'Unknown TypedArray type checking ' + | | 156 | + `${TypedArrayPrototypeGetSymbolToStringTag(a)} ${a}\n` + | | 157 | + `and ${TypedArrayPrototypeGetSymbolToStringTag(b)} ${b}` | 155 | 158 | ); | 156 | 159 | } | 157 | 160 |
| @@ -291,7 +294,10 @@ function innerDeepEqual(val1, val2, strict, memos) { | 291 | 294 | } | 292 | 295 |
| 293 | 296 | function getEnumerables(val, keys) { | 294 | | - return keys.filter((k) => ObjectPrototypePropertyIsEnumerable(val, k)); | | 297 | + return ArrayPrototypeFilter( | | 298 | + keys, | | 299 | + (k) => ObjectPrototypePropertyIsEnumerable(val, k) | | 300 | + ); | 295 | 301 | } | 296 | 302 |
| 297 | 303 | function keyCheck(val1, val2, strict, memos, iterationType, aKeys) { | @@ -330,7 +336,7 @@ function keyCheck(val1, val2, strict, memos, iterationType, aKeys) { | 330 | 336 | if (!ObjectPrototypePropertyIsEnumerable(val2, key)) { | 331 | 337 | return false; | 332 | 338 | } | 333 | | - aKeys.push(key); | | 339 | + ArrayPrototypePush(aKeys, key); | 334 | 340 | count++; | 335 | 341 | } else if (ObjectPrototypePropertyIsEnumerable(val2, key)) { | 336 | 342 | return false; | @@ -360,8 +366,8 @@ function keyCheck(val1, val2, strict, memos, iterationType, aKeys) { | 360 | 366 | // Use memos to handle cycles. | 361 | 367 | if (memos === undefined) { | 362 | 368 | memos = { | 363 | | - val1: new Map(), | 364 | | - val2: new Map(), | | 369 | + val1: new SafeMap(), | | 370 | + val2: new SafeMap(), | 365 | 371 | position: 0 | 366 | 372 | }; | 367 | 373 | } else { | @@ -458,7 +464,7 @@ function setEquiv(a, b, strict, memo) { | 458 | 464 | // to check this improves the worst case scenario instead. | 459 | 465 | if (typeof val === 'object' && val !== null) { | 460 | 466 | if (set === null) { | 461 | | - set = new Set(); | | 467 | + set = new SafeSet(); | 462 | 468 | } | 463 | 469 | // If the specified value doesn't exist in the second set its an not null | 464 | 470 | // object (or non strict only: a not matching primitive) we'll need to go | @@ -475,7 +481,7 @@ function setEquiv(a, b, strict, memo) { | 475 | 481 | } | 476 | 482 |
| 477 | 483 | if (set === null) { | 478 | | - set = new Set(); | | 484 | + set = new SafeSet(); | 479 | 485 | } | 480 | 486 | set.add(val); | 481 | 487 | } | @@ -521,7 +527,7 @@ function mapEquiv(a, b, strict, memo) { | 521 | 527 | for (const [key, item1] of a) { | 522 | 528 | if (typeof key === 'object' && key !== null) { | 523 | 529 | if (set === null) { | 524 | | - set = new Set(); | | 530 | + set = new SafeSet(); | 525 | 531 | } | 526 | 532 | set.add(key); | 527 | 533 | } else { | @@ -537,7 +543,7 @@ function mapEquiv(a, b, strict, memo) { | 537 | 543 | if (!mapMightHaveLoosePrim(a, b, key, item1, memo)) | 538 | 544 | return false; | 539 | 545 | if (set === null) { | 540 | | - set = new Set(); | | 546 | + set = new SafeSet(); | 541 | 547 | } | 542 | 548 | set.add(key); | 543 | 549 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util/debuglog.js +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const { | | 4 | + FunctionPrototype, | 4 | 5 | FunctionPrototypeBind, | 5 | 6 | ObjectCreate, | 6 | 7 | ObjectDefineProperty, | 7 | 8 | RegExp, | 8 | 9 | RegExpPrototypeTest, | 9 | 10 | SafeArrayIterator, | 10 | | - StringPrototypeToUpperCase | | 11 | + StringPrototypeToLowerCase, | | 12 | + StringPrototypeToUpperCase, | 11 | 13 | } = primordials; | 12 | 14 |
| 13 | 15 | const { inspect, format, formatWithOptions } = require('internal/util/inspect'); | @@ -37,13 +39,13 @@ function initializeDebugEnv(debugEnv) { | 37 | 39 | function emitWarningIfNeeded(set) { | 38 | 40 | if ('HTTP' === set || 'HTTP2' === set) { | 39 | 41 | process.emitWarning('Setting the NODE_DEBUG environment variable ' + | 40 | | - 'to \'' + set.toLowerCase() + '\' can expose sensitive ' + | | 42 | + 'to \'' + StringPrototypeToLowerCase(set) + '\' can expose sensitive ' + | 41 | 43 | 'data (such as passwords, tokens and authentication headers) ' + | 42 | 44 | 'in the resulting log.'); | 43 | 45 | } | 44 | 46 | } | 45 | 47 |
| 46 | | -function noop() {} | | 48 | +const noop = FunctionPrototype; | 47 | 49 |
| 48 | 50 | function debuglogImpl(enabled, set) { | 49 | 51 | if (debugImpls[set] === undefined) { | |
|