Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 64bf2f2

Browse filesBrowse files
aduh95danielleadams
authored andcommitted
util: refactor to use more primordials
PR-URL: #36265 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 857b98e commit 64bf2f2
Copy full SHA for 64bf2f2

File tree

Expand file treeCollapse file tree

6 files changed

+162
-97
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

6 files changed

+162
-97
lines changed
Open diff view settings
Collapse file

‎lib/internal/util.js‎

Copy file name to clipboardExpand all lines: lib/internal/util.js
+33-22Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
const {
44
ArrayFrom,
55
ArrayIsArray,
6+
ArrayPrototypePop,
7+
ArrayPrototypePush,
8+
ArrayPrototypeSlice,
9+
ArrayPrototypeSort,
610
Error,
7-
Map,
811
ObjectCreate,
912
ObjectDefineProperties,
1013
ObjectDefineProperty,
@@ -13,8 +16,14 @@ const {
1316
ObjectGetPrototypeOf,
1417
ObjectSetPrototypeOf,
1518
Promise,
19+
ReflectApply,
1620
ReflectConstruct,
17-
Set,
21+
RegExpPrototypeTest,
22+
SafeMap,
23+
SafeSet,
24+
StringPrototypeReplace,
25+
StringPrototypeToLowerCase,
26+
StringPrototypeToUpperCase,
1827
Symbol,
1928
SymbolFor,
2029
} = primordials;
@@ -40,12 +49,12 @@ const { isNativeError } = internalBinding('types');
4049

4150
const noCrypto = !process.versions.openssl;
4251

43-
const experimentalWarnings = new Set();
52+
const experimentalWarnings = new SafeSet();
4453

4554
const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex
4655

4756
function removeColors(str) {
48-
return str.replace(colorRegExp, '');
57+
return StringPrototypeReplace(str, colorRegExp, '');
4958
}
5059

5160
function isError(e) {
@@ -57,7 +66,7 @@ function isError(e) {
5766

5867
// Keep a list of deprecation codes that have been warned on so we only warn on
5968
// each one once.
60-
const codesWarned = new Set();
69+
const codesWarned = new SafeSet();
6170

6271
// Mark that a method should not be used.
6372
// Returns a modified function which warns once by default.
@@ -86,7 +95,7 @@ function deprecate(fn, msg, code) {
8695
if (new.target) {
8796
return ReflectConstruct(fn, args, new.target);
8897
}
89-
return fn.apply(this, args);
98+
return ReflectApply(fn, this, args);
9099
}
91100

92101
// The wrapper will keep the same prototype as fn to maintain prototype chain
@@ -132,12 +141,13 @@ function slowCases(enc) {
132141
case 4:
133142
if (enc === 'UTF8') return 'utf8';
134143
if (enc === 'ucs2' || enc === 'UCS2') return 'utf16le';
135-
enc = `${enc}`.toLowerCase();
144+
enc = StringPrototypeToLowerCase(`${enc}`);
136145
if (enc === 'utf8') return 'utf8';
137146
if (enc === 'ucs2') return 'utf16le';
138147
break;
139148
case 3:
140-
if (enc === 'hex' || enc === 'HEX' || `${enc}`.toLowerCase() === 'hex')
149+
if (enc === 'hex' || enc === 'HEX' ||
150+
StringPrototypeToLowerCase(`${enc}`) === 'hex')
141151
return 'hex';
142152
break;
143153
case 5:
@@ -146,7 +156,7 @@ function slowCases(enc) {
146156
if (enc === 'UTF-8') return 'utf8';
147157
if (enc === 'ASCII') return 'ascii';
148158
if (enc === 'UCS-2') return 'utf16le';
149-
enc = `${enc}`.toLowerCase();
159+
enc = StringPrototypeToLowerCase(`${enc}`);
150160
if (enc === 'utf-8') return 'utf8';
151161
if (enc === 'ascii') return 'ascii';
152162
if (enc === 'ucs-2') return 'utf16le';
@@ -156,18 +166,18 @@ function slowCases(enc) {
156166
if (enc === 'latin1' || enc === 'binary') return 'latin1';
157167
if (enc === 'BASE64') return 'base64';
158168
if (enc === 'LATIN1' || enc === 'BINARY') return 'latin1';
159-
enc = `${enc}`.toLowerCase();
169+
enc = StringPrototypeToLowerCase(`${enc}`);
160170
if (enc === 'base64') return 'base64';
161171
if (enc === 'latin1' || enc === 'binary') return 'latin1';
162172
break;
163173
case 7:
164174
if (enc === 'utf16le' || enc === 'UTF16LE' ||
165-
`${enc}`.toLowerCase() === 'utf16le')
175+
StringPrototypeToLowerCase(`${enc}`) === 'utf16le')
166176
return 'utf16le';
167177
break;
168178
case 8:
169179
if (enc === 'utf-16le' || enc === 'UTF-16LE' ||
170-
`${enc}`.toLowerCase() === 'utf-16le')
180+
StringPrototypeToLowerCase(`${enc}`) === 'utf-16le')
171181
return 'utf16le';
172182
break;
173183
default:
@@ -184,25 +194,25 @@ function emitExperimentalWarning(feature) {
184194
}
185195

186196
function filterDuplicateStrings(items, low) {
187-
const map = new Map();
197+
const map = new SafeMap();
188198
for (let i = 0; i < items.length; i++) {
189199
const item = items[i];
190-
const key = item.toLowerCase();
200+
const key = StringPrototypeToLowerCase(item);
191201
if (low) {
192202
map.set(key, key);
193203
} else {
194204
map.set(key, item);
195205
}
196206
}
197-
return ArrayFrom(map.values()).sort();
207+
return ArrayPrototypeSort(ArrayFrom(map.values()));
198208
}
199209

200210
function cachedResult(fn) {
201211
let result;
202212
return () => {
203213
if (result === undefined)
204214
result = fn();
205-
return result.slice();
215+
return ArrayPrototypeSlice(result);
206216
};
207217
}
208218

@@ -244,7 +254,7 @@ function convertToValidSignal(signal) {
244254
return signal;
245255

246256
if (typeof signal === 'string') {
247-
const signalName = signals[signal.toUpperCase()];
257+
const signalName = signals[StringPrototypeToUpperCase(signal)];
248258
if (signalName) return signalName;
249259
}
250260

@@ -294,7 +304,7 @@ function promisify(original) {
294304

295305
function fn(...args) {
296306
return new Promise((resolve, reject) => {
297-
original.call(this, ...args, (err, ...values) => {
307+
ArrayPrototypePush(args, (err, ...values) => {
298308
if (err) {
299309
return reject(err);
300310
}
@@ -307,6 +317,7 @@ function promisify(original) {
307317
resolve(values[0]);
308318
}
309319
});
320+
ReflectApply(original, this, args);
310321
});
311322
}
312323

@@ -343,7 +354,7 @@ function join(output, separator) {
343354
function spliceOne(list, index) {
344355
for (; index + 1 < list.length; index++)
345356
list[index] = list[index + 1];
346-
list.pop();
357+
ArrayPrototypePop(list);
347358
}
348359

349360
const kNodeModulesRE = /^(.*)[\\/]node_modules[\\/]/;
@@ -376,9 +387,9 @@ function isInsideNodeModules() {
376387
const filename = frame.getFileName();
377388
// If a filename does not start with / or contain \,
378389
// it's likely from Node.js core.
379-
if (!/^\/|\\/.test(filename))
390+
if (!RegExpPrototypeTest(/^\/|\\/, filename))
380391
continue;
381-
return kNodeModulesRE.test(filename);
392+
return RegExpPrototypeTest(kNodeModulesRE, filename);
382393
}
383394
}
384395
return false;
@@ -389,7 +400,7 @@ function once(callback) {
389400
return function(...args) {
390401
if (called) return;
391402
called = true;
392-
callback.apply(this, args);
403+
ReflectApply(callback, this, args);
393404
};
394405
}
395406

Collapse file

‎lib/internal/util/comparisons.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/comparisons.js
+20-14Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
const {
44
ArrayIsArray,
5+
ArrayPrototypeFilter,
6+
ArrayPrototypePush,
57
BigIntPrototypeValueOf,
68
BooleanPrototypeValueOf,
79
DatePrototypeGetTime,
810
Error,
9-
Map,
1011
NumberIsNaN,
1112
NumberPrototypeValueOf,
1213
ObjectGetOwnPropertySymbols,
@@ -16,10 +17,11 @@ const {
1617
ObjectPrototypeHasOwnProperty,
1718
ObjectPrototypePropertyIsEnumerable,
1819
ObjectPrototypeToString,
19-
Set,
20+
SafeMap,
21+
SafeSet,
2022
StringPrototypeValueOf,
2123
SymbolPrototypeValueOf,
22-
SymbolToStringTag,
24+
TypedArrayPrototypeGetSymbolToStringTag,
2325
Uint8Array,
2426
} = primordials;
2527

@@ -126,7 +128,7 @@ function isEqualBoxedPrimitive(val1, val2) {
126128

127129
function isIdenticalTypedArrayType(a, b) {
128130
// Fast path to reduce type checks in the common case.
129-
const check = types[`is${a[SymbolToStringTag]}`];
131+
const check = types[`is${TypedArrayPrototypeGetSymbolToStringTag(a)}`];
130132
if (check !== undefined && check(a)) {
131133
return check(b);
132134
}
@@ -150,8 +152,9 @@ function isIdenticalTypedArrayType(a, b) {
150152
}
151153
/* c8 ignore next 4 */
152154
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}`
155158
);
156159
}
157160

@@ -291,7 +294,10 @@ function innerDeepEqual(val1, val2, strict, memos) {
291294
}
292295

293296
function getEnumerables(val, keys) {
294-
return keys.filter((k) => ObjectPrototypePropertyIsEnumerable(val, k));
297+
return ArrayPrototypeFilter(
298+
keys,
299+
(k) => ObjectPrototypePropertyIsEnumerable(val, k)
300+
);
295301
}
296302

297303
function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {
@@ -330,7 +336,7 @@ function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {
330336
if (!ObjectPrototypePropertyIsEnumerable(val2, key)) {
331337
return false;
332338
}
333-
aKeys.push(key);
339+
ArrayPrototypePush(aKeys, key);
334340
count++;
335341
} else if (ObjectPrototypePropertyIsEnumerable(val2, key)) {
336342
return false;
@@ -360,8 +366,8 @@ function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {
360366
// Use memos to handle cycles.
361367
if (memos === undefined) {
362368
memos = {
363-
val1: new Map(),
364-
val2: new Map(),
369+
val1: new SafeMap(),
370+
val2: new SafeMap(),
365371
position: 0
366372
};
367373
} else {
@@ -458,7 +464,7 @@ function setEquiv(a, b, strict, memo) {
458464
// to check this improves the worst case scenario instead.
459465
if (typeof val === 'object' && val !== null) {
460466
if (set === null) {
461-
set = new Set();
467+
set = new SafeSet();
462468
}
463469
// If the specified value doesn't exist in the second set its an not null
464470
// object (or non strict only: a not matching primitive) we'll need to go
@@ -475,7 +481,7 @@ function setEquiv(a, b, strict, memo) {
475481
}
476482

477483
if (set === null) {
478-
set = new Set();
484+
set = new SafeSet();
479485
}
480486
set.add(val);
481487
}
@@ -521,7 +527,7 @@ function mapEquiv(a, b, strict, memo) {
521527
for (const [key, item1] of a) {
522528
if (typeof key === 'object' && key !== null) {
523529
if (set === null) {
524-
set = new Set();
530+
set = new SafeSet();
525531
}
526532
set.add(key);
527533
} else {
@@ -537,7 +543,7 @@ function mapEquiv(a, b, strict, memo) {
537543
if (!mapMightHaveLoosePrim(a, b, key, item1, memo))
538544
return false;
539545
if (set === null) {
540-
set = new Set();
546+
set = new SafeSet();
541547
}
542548
set.add(key);
543549
}
Collapse file

‎lib/internal/util/debuglog.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/debuglog.js
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
'use strict';
22

33
const {
4+
FunctionPrototype,
45
FunctionPrototypeBind,
56
ObjectCreate,
67
ObjectDefineProperty,
78
RegExp,
89
RegExpPrototypeTest,
910
SafeArrayIterator,
10-
StringPrototypeToUpperCase
11+
StringPrototypeToLowerCase,
12+
StringPrototypeToUpperCase,
1113
} = primordials;
1214

1315
const { inspect, format, formatWithOptions } = require('internal/util/inspect');
@@ -37,13 +39,13 @@ function initializeDebugEnv(debugEnv) {
3739
function emitWarningIfNeeded(set) {
3840
if ('HTTP' === set || 'HTTP2' === set) {
3941
process.emitWarning('Setting the NODE_DEBUG environment variable ' +
40-
'to \'' + set.toLowerCase() + '\' can expose sensitive ' +
42+
'to \'' + StringPrototypeToLowerCase(set) + '\' can expose sensitive ' +
4143
'data (such as passwords, tokens and authentication headers) ' +
4244
'in the resulting log.');
4345
}
4446
}
4547

46-
function noop() {}
48+
const noop = FunctionPrototype;
4749

4850
function debuglogImpl(enabled, set) {
4951
if (debugImpls[set] === undefined) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.