Skip to content

Commit 9077b48

Browse files
committed
lib: refactor internal/util
* Use the more efficient module.exports = {} approach * Eliminate some uses of arguments PR-URL: #11404 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Brian White <[email protected]
1 parent 5e095f6 commit 9077b48

File tree

1 file changed

+50
-37
lines changed

1 file changed

+50
-37
lines changed

lib/internal/util.js

+50-37
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@ const signals = process.binding('constants').os.signals;
55

66
const kArrowMessagePrivateSymbolIndex = binding['arrow_message_private_symbol'];
77
const kDecoratedPrivateSymbolIndex = binding['decorated_private_symbol'];
8+
const noCrypto = !process.versions.openssl;
89

9-
// The `buffer` module uses this. Defining it here instead of in the public
10-
// `util` module makes it accessible without having to `require('util')` there.
11-
exports.customInspectSymbol = Symbol('util.inspect.custom');
10+
function isError(e) {
11+
return objectToString(e) === '[object Error]' || e instanceof Error;
12+
}
13+
14+
function objectToString(o) {
15+
return Object.prototype.toString.call(o);
16+
}
1217

1318
// Mark that a method should not be used.
1419
// Returns a modified function which warns once by default.
1520
// If --no-deprecation is set, then it is a no-op.
16-
exports.deprecate = function deprecate(fn, msg, code) {
21+
function deprecate(fn, msg, code) {
1722
// Allow for deprecating things in the process of starting up.
1823
if (global.process === undefined) {
19-
return function() {
20-
return exports.deprecate(fn, msg, code).apply(this, arguments);
24+
return function(...args) {
25+
return deprecate(fn, msg).apply(this, args);
2126
};
2227
}
2328

@@ -29,7 +34,7 @@ exports.deprecate = function deprecate(fn, msg, code) {
2934
throw new TypeError('`code` argument must be a string');
3035

3136
var warned = false;
32-
function deprecated() {
37+
function deprecated(...args) {
3338
if (!warned) {
3439
warned = true;
3540
if (code !== undefined) {
@@ -39,9 +44,9 @@ exports.deprecate = function deprecate(fn, msg, code) {
3944
}
4045
}
4146
if (new.target) {
42-
return Reflect.construct(fn, arguments, new.target);
47+
return Reflect.construct(fn, args, new.target);
4348
}
44-
return fn.apply(this, arguments);
49+
return fn.apply(this, args);
4550
}
4651

4752
// The wrapper will keep the same prototype as fn to maintain prototype chain
@@ -54,10 +59,10 @@ exports.deprecate = function deprecate(fn, msg, code) {
5459
}
5560

5661
return deprecated;
57-
};
62+
}
5863

59-
exports.decorateErrorStack = function decorateErrorStack(err) {
60-
if (!(exports.isError(err) && err.stack) ||
64+
function decorateErrorStack(err) {
65+
if (!(isError(err) && err.stack) ||
6166
binding.getHiddenValue(err, kDecoratedPrivateSymbolIndex) === true)
6267
return;
6368

@@ -67,30 +72,19 @@ exports.decorateErrorStack = function decorateErrorStack(err) {
6772
err.stack = arrow + err.stack;
6873
binding.setHiddenValue(err, kDecoratedPrivateSymbolIndex, true);
6974
}
70-
};
71-
72-
exports.isError = function isError(e) {
73-
return exports.objectToString(e) === '[object Error]' || e instanceof Error;
74-
};
75-
76-
exports.objectToString = function objectToString(o) {
77-
return Object.prototype.toString.call(o);
78-
};
75+
}
7976

80-
const noCrypto = !process.versions.openssl;
81-
exports.assertCrypto = function() {
77+
function assertCrypto() {
8278
if (noCrypto)
8379
throw new Error('Node.js is not compiled with openssl crypto support');
84-
};
85-
86-
exports.kIsEncodingSymbol = Symbol('node.isEncoding');
80+
}
8781

8882
// The loop should only run at most twice, retrying with lowercased enc
8983
// if there is no match in the first pass.
9084
// We use a loop instead of branching to retry with a helper
9185
// function in order to avoid the performance hit.
9286
// Return undefined if there is no match.
93-
exports.normalizeEncoding = function normalizeEncoding(enc) {
87+
function normalizeEncoding(enc) {
9488
if (!enc) return 'utf8';
9589
var retried;
9690
while (true) {
@@ -116,11 +110,9 @@ exports.normalizeEncoding = function normalizeEncoding(enc) {
116110
retried = true;
117111
}
118112
}
119-
};
113+
}
120114

121-
// Filters duplicate strings. Used to support functions in crypto and tls
122-
// modules. Implemented specifically to maintain existing behaviors in each.
123-
exports.filterDuplicateStrings = function filterDuplicateStrings(items, low) {
115+
function filterDuplicateStrings(items, low) {
124116
const map = new Map();
125117
for (var i = 0; i < items.length; i++) {
126118
const item = items[i];
@@ -132,24 +124,24 @@ exports.filterDuplicateStrings = function filterDuplicateStrings(items, low) {
132124
}
133125
}
134126
return Array.from(map.values()).sort();
135-
};
127+
}
136128

137-
exports.cachedResult = function cachedResult(fn) {
129+
function cachedResult(fn) {
138130
var result;
139131
return () => {
140132
if (result === undefined)
141133
result = fn();
142134
return result.slice();
143135
};
144-
};
136+
}
145137

146138
// Useful for Wrapping an ES6 Class with a constructor Function that
147139
// does not require the new keyword. For instance:
148140
// class A { constructor(x) {this.x = x;}}
149141
// const B = createClassWrapper(A);
150142
// B() instanceof A // true
151143
// B() instanceof B // true
152-
exports.createClassWrapper = function createClassWrapper(type) {
144+
function createClassWrapper(type) {
153145
const fn = function(...args) {
154146
return Reflect.construct(type, args, new.target || type);
155147
};
@@ -161,7 +153,7 @@ exports.createClassWrapper = function createClassWrapper(type) {
161153
Object.setPrototypeOf(fn, type);
162154
fn.prototype = type.prototype;
163155
return fn;
164-
};
156+
}
165157

166158
let signalsToNamesMapping;
167159
function getSignalsToNamesMapping() {
@@ -176,7 +168,7 @@ function getSignalsToNamesMapping() {
176168
return signalsToNamesMapping;
177169
}
178170

179-
exports.convertToValidSignal = function convertToValidSignal(signal) {
171+
function convertToValidSignal(signal) {
180172
if (typeof signal === 'number' && getSignalsToNamesMapping()[signal])
181173
return signal;
182174

@@ -186,4 +178,25 @@ exports.convertToValidSignal = function convertToValidSignal(signal) {
186178
}
187179

188180
throw new Error('Unknown signal: ' + signal);
181+
}
182+
183+
module.exports = exports = {
184+
assertCrypto,
185+
cachedResult,
186+
convertToValidSignal,
187+
createClassWrapper,
188+
decorateErrorStack,
189+
deprecate,
190+
filterDuplicateStrings,
191+
isError,
192+
normalizeEncoding,
193+
objectToString,
194+
195+
// Symbol used to provide a custom inspect function for an object as an
196+
// alternative to using 'inspect'
197+
customInspectSymbol: Symbol('util.inspect.custom'),
198+
199+
// Used by the buffer module to capture an internal reference to the
200+
// default isEncoding implementation, just in case userland overrides it.
201+
kIsEncodingSymbol: Symbol('node.isEncoding')
189202
};

0 commit comments

Comments
 (0)