mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
lib: improve lazy requires
* internal/errors - assert should already be in place when calling any of the message generating functions. * No lazy load if not necessary. * Replace function calls with `if`s. PR-URL: https://github.com/nodejs/node/pull/14167 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
This commit is contained in:
1 parent
7bc666be17
commit
b55ab01b45
7 files changed
+42
-93
No files matched your search
+15
-29
@@ -9,19 +9,9 @@
|
||||
const kCode = Symbol('code');
|
||||
const messages = new Map();
|
||||
|
||||
var util;
|
||||
function lazyUtil() {
|
||||
if (!util)
|
||||
util = require('util');
|
||||
return util;
|
||||
}
|
||||
|
||||
var assert;
|
||||
function lazyAssert() {
|
||||
if (!assert)
|
||||
assert = require('assert');
|
||||
return assert;
|
||||
}
|
||||
// Lazily loaded
|
||||
var assert = null;
|
||||
var util = null;
|
||||
|
||||
function makeNodeError(Base) {
|
||||
return class NodeError extends Base {
|
||||
@@ -45,13 +35,14 @@ class AssertionError extends Error {
|
||||
if (typeof options !== 'object' || options === null) {
|
||||
throw new exports.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
|
||||
}
|
||||
const util = lazyUtil();
|
||||
const message = options.message ||
|
||||
`${util.inspect(options.actual).slice(0, 128)} ` +
|
||||
`${options.operator} ` +
|
||||
util.inspect(options.expected).slice(0, 128);
|
||||
if (options.message) {
|
||||
super(options.message);
|
||||
} else {
|
||||
if (util === null) util = require('util');
|
||||
super(`${util.inspect(options.actual).slice(0, 128)} ` +
|
||||
`${options.operator} ${util.inspect(options.expected).slice(0, 128)}`);
|
||||
}
|
||||
|
||||
super(message);
|
||||
this.generatedMessage = !options.message;
|
||||
this.name = 'AssertionError [ERR_ASSERTION]';
|
||||
this.code = 'ERR_ASSERTION';
|
||||
@@ -63,15 +54,16 @@ class AssertionError extends Error {
|
||||
}
|
||||
|
||||
function message(key, args) {
|
||||
const assert = lazyAssert();
|
||||
if (assert === null) assert = require('assert');
|
||||
assert.strictEqual(typeof key, 'string');
|
||||
const util = lazyUtil();
|
||||
const msg = messages.get(key);
|
||||
assert(msg, `An invalid error message key was used: ${key}.`);
|
||||
let fmt = util.format;
|
||||
let fmt;
|
||||
if (typeof msg === 'function') {
|
||||
fmt = msg;
|
||||
} else {
|
||||
if (util === null) util = require('util');
|
||||
fmt = util.format;
|
||||
if (args === undefined || args.length === 0)
|
||||
return msg;
|
||||
args.unshift(msg);
|
||||
@@ -123,7 +115,6 @@ E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
|
||||
E('ERR_INVALID_ARG_TYPE', invalidArgType);
|
||||
E('ERR_INVALID_ARRAY_LENGTH',
|
||||
(name, length, actual) => {
|
||||
const assert = lazyAssert();
|
||||
assert.strictEqual(typeof actual, 'number');
|
||||
return `The "${name}" array must have a length of ${
|
||||
length}. Received length ${actual}`;
|
||||
@@ -152,10 +143,7 @@ E('ERR_INVALID_THIS', 'Value of "this" must be of type %s');
|
||||
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple');
|
||||
E('ERR_INVALID_URL', 'Invalid URL: %s');
|
||||
E('ERR_INVALID_URL_SCHEME',
|
||||
(expected) => {
|
||||
lazyAssert();
|
||||
return `The URL must be ${oneOf(expected, 'scheme')}`;
|
||||
});
|
||||
(expected) => `The URL must be ${oneOf(expected, 'scheme')}`);
|
||||
E('ERR_IPC_CHANNEL_CLOSED', 'Channel closed');
|
||||
E('ERR_IPC_DISCONNECTED', 'IPC channel is already disconnected');
|
||||
E('ERR_IPC_ONE_PIPE', 'Child process can have only one IPC pipe');
|
||||
@@ -191,7 +179,6 @@ E('ERR_V8BREAKITERATOR', 'Full ICU data not installed. ' +
|
||||
// Add new errors from here...
|
||||
|
||||
function invalidArgType(name, expected, actual) {
|
||||
const assert = lazyAssert();
|
||||
assert(name, 'name is required');
|
||||
|
||||
// determiner: 'must be' or 'must not be'
|
||||
@@ -223,7 +210,6 @@ function invalidArgType(name, expected, actual) {
|
||||
}
|
||||
|
||||
function missingArgs(...args) {
|
||||
const assert = lazyAssert();
|
||||
assert(args.length > 0, 'At least one arg needs to be specified');
|
||||
let msg = 'The ';
|
||||
const len = args.length;
|
||||
|
||||
Reference in new issue
Block a user