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:
Ruben Bridgewater 2017-07-11 09:23:38 -04:00 committed by Refael Ackermann
parent 7bc666be17
commit b55ab01b45
No known key found for this signature in database
GPG Key ID: CD704BD80FDDDB64
7 changed files with 42 additions and 93 deletions

View File

@ -24,6 +24,7 @@
const assert = require('assert'); const assert = require('assert');
const errors = require('internal/errors'); const errors = require('internal/errors');
const Buffer = require('buffer').Buffer; const Buffer = require('buffer').Buffer;
const dns = require('dns');
const util = require('util'); const util = require('util');
const EventEmitter = require('events'); const EventEmitter = require('events');
const setInitTriggerId = require('async_hooks').setInitTriggerId; const setInitTriggerId = require('async_hooks').setInitTriggerId;
@ -39,17 +40,13 @@ const BIND_STATE_UNBOUND = 0;
const BIND_STATE_BINDING = 1; const BIND_STATE_BINDING = 1;
const BIND_STATE_BOUND = 2; const BIND_STATE_BOUND = 2;
// lazily loaded // Lazily loaded
var cluster = null; var cluster = null;
var dns = null;
const errnoException = util._errnoException; const errnoException = util._errnoException;
const exceptionWithHostPort = util._exceptionWithHostPort; const exceptionWithHostPort = util._exceptionWithHostPort;
function lookup(address, family, callback) { function lookup(address, family, callback) {
if (!dns)
dns = require('dns');
return dns.lookup(address, family, callback); return dns.lookup(address, family, callback);
} }

View File

@ -9,19 +9,9 @@
const kCode = Symbol('code'); const kCode = Symbol('code');
const messages = new Map(); const messages = new Map();
var util; // Lazily loaded
function lazyUtil() { var assert = null;
if (!util) var util = null;
util = require('util');
return util;
}
var assert;
function lazyAssert() {
if (!assert)
assert = require('assert');
return assert;
}
function makeNodeError(Base) { function makeNodeError(Base) {
return class NodeError extends Base { return class NodeError extends Base {
@ -45,13 +35,14 @@ class AssertionError extends Error {
if (typeof options !== 'object' || options === null) { if (typeof options !== 'object' || options === null) {
throw new exports.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object'); throw new exports.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
} }
const util = lazyUtil(); if (options.message) {
const message = options.message || super(options.message);
`${util.inspect(options.actual).slice(0, 128)} ` + } else {
`${options.operator} ` + if (util === null) util = require('util');
util.inspect(options.expected).slice(0, 128); super(`${util.inspect(options.actual).slice(0, 128)} ` +
`${options.operator} ${util.inspect(options.expected).slice(0, 128)}`);
}
super(message);
this.generatedMessage = !options.message; this.generatedMessage = !options.message;
this.name = 'AssertionError [ERR_ASSERTION]'; this.name = 'AssertionError [ERR_ASSERTION]';
this.code = 'ERR_ASSERTION'; this.code = 'ERR_ASSERTION';
@ -63,15 +54,16 @@ class AssertionError extends Error {
} }
function message(key, args) { function message(key, args) {
const assert = lazyAssert(); if (assert === null) assert = require('assert');
assert.strictEqual(typeof key, 'string'); assert.strictEqual(typeof key, 'string');
const util = lazyUtil();
const msg = messages.get(key); const msg = messages.get(key);
assert(msg, `An invalid error message key was used: ${key}.`); assert(msg, `An invalid error message key was used: ${key}.`);
let fmt = util.format; let fmt;
if (typeof msg === 'function') { if (typeof msg === 'function') {
fmt = msg; fmt = msg;
} else { } else {
if (util === null) util = require('util');
fmt = util.format;
if (args === undefined || args.length === 0) if (args === undefined || args.length === 0)
return msg; return msg;
args.unshift(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_ARG_TYPE', invalidArgType);
E('ERR_INVALID_ARRAY_LENGTH', E('ERR_INVALID_ARRAY_LENGTH',
(name, length, actual) => { (name, length, actual) => {
const assert = lazyAssert();
assert.strictEqual(typeof actual, 'number'); assert.strictEqual(typeof actual, 'number');
return `The "${name}" array must have a length of ${ return `The "${name}" array must have a length of ${
length}. Received length ${actual}`; 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_TUPLE', '%s must be an iterable %s tuple');
E('ERR_INVALID_URL', 'Invalid URL: %s'); E('ERR_INVALID_URL', 'Invalid URL: %s');
E('ERR_INVALID_URL_SCHEME', E('ERR_INVALID_URL_SCHEME',
(expected) => { (expected) => `The URL must be ${oneOf(expected, 'scheme')}`);
lazyAssert();
return `The URL must be ${oneOf(expected, 'scheme')}`;
});
E('ERR_IPC_CHANNEL_CLOSED', 'Channel closed'); E('ERR_IPC_CHANNEL_CLOSED', 'Channel closed');
E('ERR_IPC_DISCONNECTED', 'IPC channel is already disconnected'); E('ERR_IPC_DISCONNECTED', 'IPC channel is already disconnected');
E('ERR_IPC_ONE_PIPE', 'Child process can have only one IPC pipe'); 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... // Add new errors from here...
function invalidArgType(name, expected, actual) { function invalidArgType(name, expected, actual) {
const assert = lazyAssert();
assert(name, 'name is required'); assert(name, 'name is required');
// determiner: 'must be' or 'must not be' // determiner: 'must be' or 'must not be'
@ -223,7 +210,6 @@ function invalidArgType(name, expected, actual) {
} }
function missingArgs(...args) { function missingArgs(...args) {
const assert = lazyAssert();
assert(args.length > 0, 'At least one arg needs to be specified'); assert(args.length > 0, 'At least one arg needs to be specified');
let msg = 'The '; let msg = 'The ';
const len = args.length; const len = args.length;

View File

@ -1,14 +1,7 @@
'use strict'; 'use strict';
const errors = require('internal/errors'); const errors = require('internal/errors');
var _lazyConstants = null; const constants = process.binding('constants').os.signals;
function lazyConstants() {
if (!_lazyConstants) {
_lazyConstants = process.binding('constants').os.signals;
}
return _lazyConstants;
}
const assert = process.assert = function(x, msg) { const assert = process.assert = function(x, msg) {
if (!x) throw new errors.Error('ERR_ASSERTION', msg || 'assertion error'); if (!x) throw new errors.Error('ERR_ASSERTION', msg || 'assertion error');
@ -178,8 +171,8 @@ function setupKillAndExit() {
err = process._kill(pid, 0); err = process._kill(pid, 0);
} else { } else {
sig = sig || 'SIGTERM'; sig = sig || 'SIGTERM';
if (lazyConstants()[sig]) { if (constants[sig]) {
err = process._kill(pid, lazyConstants()[sig]); err = process._kill(pid, constants[sig]);
} else { } else {
throw new errors.TypeError('ERR_UNKNOWN_SIGNAL', sig); throw new errors.TypeError('ERR_UNKNOWN_SIGNAL', sig);
} }
@ -201,7 +194,7 @@ function setupSignalHandlers() {
const signalWraps = {}; const signalWraps = {};
function isSignal(event) { function isSignal(event) {
return typeof event === 'string' && lazyConstants()[event] !== undefined; return typeof event === 'string' && constants[event] !== undefined;
} }
// Detect presence of a listener for the special signal types // Detect presence of a listener for the special signal types
@ -215,7 +208,7 @@ function setupSignalHandlers() {
wrap.onsignal = function() { process.emit(type); }; wrap.onsignal = function() { process.emit(type); };
const signum = lazyConstants()[type]; const signum = constants[type];
const err = wrap.start(signum); const err = wrap.start(signum);
if (err) { if (err) {
wrap.close(); wrap.close();

View File

@ -1,15 +1,9 @@
'use strict'; 'use strict';
const errors = require('internal/errors');
exports.setup = setupStdio; exports.setup = setupStdio;
var errors;
function lazyErrors() {
if (!errors)
errors = require('internal/errors');
return errors;
}
function setupStdio() { function setupStdio() {
var stdin; var stdin;
var stdout; var stdout;
@ -20,8 +14,7 @@ function setupStdio() {
stdout = createWritableStdioStream(1); stdout = createWritableStdioStream(1);
stdout.destroySoon = stdout.destroy; stdout.destroySoon = stdout.destroy;
stdout._destroy = function(er, cb) { stdout._destroy = function(er, cb) {
// avoid errors if we already emitted // Avoid errors if we already emitted
const errors = lazyErrors();
er = er || new errors.Error('ERR_STDOUT_CLOSE'); er = er || new errors.Error('ERR_STDOUT_CLOSE');
cb(er); cb(er);
}; };
@ -36,8 +29,7 @@ function setupStdio() {
stderr = createWritableStdioStream(2); stderr = createWritableStdioStream(2);
stderr.destroySoon = stderr.destroy; stderr.destroySoon = stderr.destroy;
stderr._destroy = function(er, cb) { stderr._destroy = function(er, cb) {
// avoid errors if we already emitted // Avoid errors if we already emitted
const errors = lazyErrors();
er = er || new errors.Error('ERR_STDERR_CLOSE'); er = er || new errors.Error('ERR_STDERR_CLOSE');
cb(er); cb(er);
}; };
@ -95,7 +87,6 @@ function setupStdio() {
default: default:
// Probably an error on in uv_guess_handle() // Probably an error on in uv_guess_handle()
const errors = lazyErrors();
throw new errors.Error('ERR_UNKNOWN_STDIN_TYPE'); throw new errors.Error('ERR_UNKNOWN_STDIN_TYPE');
} }
@ -180,7 +171,6 @@ function createWritableStdioStream(fd) {
default: default:
// Probably an error on in uv_guess_handle() // Probably an error on in uv_guess_handle()
const errors = lazyErrors();
throw new errors.Error('ERR_UNKNOWN_STREAM_TYPE'); throw new errors.Error('ERR_UNKNOWN_STREAM_TYPE');
} }

View File

@ -2,26 +2,16 @@
const config = process.binding('config'); const config = process.binding('config');
const prefix = `(${process.release.name}:${process.pid}) `; const prefix = `(${process.release.name}:${process.pid}) `;
const errors = require('internal/errors');
exports.setup = setupProcessWarnings; exports.setup = setupProcessWarnings;
var errors;
var fs;
var cachedFd; var cachedFd;
var acquiringFd = false; var acquiringFd = false;
function nop() {} function nop() {}
function lazyErrors() { // Lazily loaded
if (!errors) var fs = null;
errors = require('internal/errors');
return errors;
}
function lazyFs() {
if (!fs)
fs = require('fs');
return fs;
}
function writeOut(message) { function writeOut(message) {
if (console && typeof console.error === 'function') if (console && typeof console.error === 'function')
@ -31,7 +21,8 @@ function writeOut(message) {
function onClose(fd) { function onClose(fd) {
return function() { return function() {
lazyFs().close(fd, nop); if (fs === null) fs = require('fs');
fs.close(fd, nop);
}; };
} }
@ -53,14 +44,16 @@ function onAcquired(message) {
return function(err, fd) { return function(err, fd) {
if (err) if (err)
return writeOut(message); return writeOut(message);
lazyFs().appendFile(fd, `${message}\n`, nop); if (fs === null) fs = require('fs');
fs.appendFile(fd, `${message}\n`, nop);
}; };
} }
function acquireFd(cb) { function acquireFd(cb) {
if (cachedFd === undefined && !acquiringFd) { if (cachedFd === undefined && !acquiringFd) {
acquiringFd = true; acquiringFd = true;
lazyFs().open(config.warningFile, 'a', onOpen(cb)); if (fs === null) fs = require('fs');
fs.open(config.warningFile, 'a', onOpen(cb));
} else if (cachedFd !== undefined && !acquiringFd) { } else if (cachedFd !== undefined && !acquiringFd) {
cb(null, cachedFd); cb(null, cachedFd);
} else { } else {
@ -112,7 +105,6 @@ function setupProcessWarnings() {
// process.emitWarning(str[, type[, code]][, ctor]) // process.emitWarning(str[, type[, code]][, ctor])
// process.emitWarning(str[, options]) // process.emitWarning(str[, options])
process.emitWarning = function(warning, type, code, ctor, now) { process.emitWarning = function(warning, type, code, ctor, now) {
const errors = lazyErrors();
var detail; var detail;
if (type !== null && typeof type === 'object' && !Array.isArray(type)) { if (type !== null && typeof type === 'object' && !Array.isArray(type)) {
ctor = type.ctor; ctor = type.ctor;

View File

@ -43,9 +43,11 @@ const async_id_symbol = process.binding('async_wrap').async_id_symbol;
const { newUid, setInitTriggerId } = require('async_hooks'); const { newUid, setInitTriggerId } = require('async_hooks');
const nextTick = require('internal/process/next_tick').nextTick; const nextTick = require('internal/process/next_tick').nextTick;
const errors = require('internal/errors'); const errors = require('internal/errors');
const dns = require('dns');
var cluster; // `cluster` is only used by `listenInCluster` so for startup performance
var dns; // reasons it's lazy loaded.
var cluster = null;
const errnoException = util._errnoException; const errnoException = util._errnoException;
const exceptionWithHostPort = util._exceptionWithHostPort; const exceptionWithHostPort = util._exceptionWithHostPort;
@ -999,7 +1001,6 @@ Socket.prototype.connect = function() {
function lookupAndConnect(self, options) { function lookupAndConnect(self, options) {
const dns = lazyDns();
var host = options.host || 'localhost'; var host = options.host || 'localhost';
var port = options.port; var port = options.port;
var localAddress = options.localAddress; var localAddress = options.localAddress;
@ -1347,18 +1348,11 @@ function emitListeningNT(self) {
} }
function lazyDns() {
if (dns === undefined)
dns = require('dns');
return dns;
}
function listenInCluster(server, address, port, addressType, function listenInCluster(server, address, port, addressType,
backlog, fd, exclusive) { backlog, fd, exclusive) {
exclusive = !!exclusive; exclusive = !!exclusive;
if (!cluster) cluster = require('cluster'); if (cluster === null) cluster = require('cluster');
if (cluster.isMaster || exclusive) { if (cluster.isMaster || exclusive) {
// Will create a new handle // Will create a new handle
@ -1484,7 +1478,6 @@ Server.prototype.listen = function() {
}; };
function lookupAndListen(self, port, address, backlog, exclusive) { function lookupAndListen(self, port, address, backlog, exclusive) {
const dns = lazyDns();
dns.lookup(address, function doListen(err, ip, addressType) { dns.lookup(address, function doListen(err, ip, addressType) {
if (err) { if (err) {
self.emit('error', err); self.emit('error', err);

View File

@ -29,8 +29,9 @@
const errors = require('internal/errors'); const errors = require('internal/errors');
const { debug, inherits } = require('util'); const { debug, inherits } = require('util');
const Buffer = require('buffer').Buffer; const { Buffer } = require('buffer');
const EventEmitter = require('events'); const EventEmitter = require('events');
const { StringDecoder } = require('string_decoder');
const { const {
CSI, CSI,
emitKeys, emitKeys,
@ -59,7 +60,6 @@ const ESCAPE_DECODER = Symbol('escape-decoder');
// GNU readline library - keyseq-timeout is 500ms (default) // GNU readline library - keyseq-timeout is 500ms (default)
const ESCAPE_CODE_TIMEOUT = 500; const ESCAPE_CODE_TIMEOUT = 500;
function createInterface(input, output, completer, terminal) { function createInterface(input, output, completer, terminal) {
return new Interface(input, output, completer, terminal); return new Interface(input, output, completer, terminal);
} }
@ -181,7 +181,6 @@ function Interface(input, output, completer, terminal) {
input.removeListener('data', ondata); input.removeListener('data', ondata);
input.removeListener('end', onend); input.removeListener('end', onend);
}); });
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
this._decoder = new StringDecoder('utf8'); this._decoder = new StringDecoder('utf8');
} else { } else {
@ -989,7 +988,6 @@ Interface.prototype._ttyWrite = function(s, key) {
function emitKeypressEvents(stream, iface) { function emitKeypressEvents(stream, iface) {
if (stream[KEYPRESS_DECODER]) return; if (stream[KEYPRESS_DECODER]) return;
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
stream[KEYPRESS_DECODER] = new StringDecoder('utf8'); stream[KEYPRESS_DECODER] = new StringDecoder('utf8');
stream[ESCAPE_DECODER] = emitKeys(stream); stream[ESCAPE_DECODER] = emitKeys(stream);