Skip to content

Commit

Permalink
lib: improve lazy requires
Browse files Browse the repository at this point in the history
* 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: #14167
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
  • Loading branch information
BridgeAR authored and jasnell committed Sep 21, 2017
1 parent f6c65e6 commit cf1fe76
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 88 deletions.
7 changes: 2 additions & 5 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
const assert = require('assert');
const errors = require('internal/errors');
const Buffer = require('buffer').Buffer;
const dns = require('dns');
const util = require('util');
const EventEmitter = require('events');
const setInitTriggerId = require('async_hooks').setInitTriggerId;
Expand All @@ -39,17 +40,13 @@ const BIND_STATE_UNBOUND = 0;
const BIND_STATE_BINDING = 1;
const BIND_STATE_BOUND = 2;

// lazily loaded
// Lazily loaded
var cluster = null;
var dns = null;

const errnoException = util._errnoException;
const exceptionWithHostPort = util._exceptionWithHostPort;

function lookup(address, family, callback) {
if (!dns)
dns = require('dns');

return dns.lookup(address, family, callback);
}

Expand Down
37 changes: 14 additions & 23 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -240,7 +232,6 @@ E('ERR_VALID_PERFORMANCE_ENTRY_TYPE',
// Add new errors from here...

function invalidArgType(name, expected, actual) {
const assert = lazyAssert();
assert(name, 'name is required');
var msg = `The "${name}" argument must be ${oneOf(expected, 'type')}`;
if (arguments.length >= 3) {
Expand Down
18 changes: 5 additions & 13 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
'use strict';

const util = require('util');

var _lazyConstants = null;

function lazyConstants() {
if (!_lazyConstants) {
_lazyConstants = process.binding('constants').os.signals;
}
return _lazyConstants;
}
const constants = process.binding('constants').os.signals;

const assert = process.assert = function(x, msg) {
if (!x) throw new Error(msg || 'assertion error');
Expand Down Expand Up @@ -177,8 +169,8 @@ function setupKillAndExit() {
err = process._kill(pid, 0);
} else {
sig = sig || 'SIGTERM';
if (lazyConstants()[sig]) {
err = process._kill(pid, lazyConstants()[sig]);
if (constants[sig]) {
err = process._kill(pid, constants[sig]);
} else {
throw new Error(`Unknown signal: ${sig}`);
}
Expand All @@ -198,7 +190,7 @@ function setupSignalHandlers() {
const signalWraps = {};

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
Expand All @@ -212,7 +204,7 @@ function setupSignalHandlers() {

wrap.onsignal = function() { process.emit(type); };

const signum = lazyConstants()[type];
const signum = constants[type];
const err = wrap.start(signum);
if (err) {
wrap.close();
Expand Down
18 changes: 4 additions & 14 deletions lib/internal/process/stdio.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
'use strict';

exports.setup = setupStdio;

var errors;
const errors = require('internal/errors');

function lazyErrors() {
if (!errors)
errors = require('internal/errors');
return errors;
}
exports.setup = setupStdio;

function setupStdio() {
var stdin;
Expand All @@ -20,8 +14,7 @@ function setupStdio() {
stdout = createWritableStdioStream(1);
stdout.destroySoon = stdout.destroy;
stdout._destroy = function(er, cb) {
// avoid errors if we already emitted
const errors = lazyErrors();
// Avoid errors if we already emitted
er = er || new errors.Error('ERR_STDOUT_CLOSE');
cb(er);
};
Expand All @@ -36,8 +29,7 @@ function setupStdio() {
stderr = createWritableStdioStream(2);
stderr.destroySoon = stderr.destroy;
stderr._destroy = function(er, cb) {
// avoid errors if we already emitted
const errors = lazyErrors();
// Avoid errors if we already emitted
er = er || new errors.Error('ERR_STDERR_CLOSE');
cb(er);
};
Expand Down Expand Up @@ -95,7 +87,6 @@ function setupStdio() {

default:
// Probably an error on in uv_guess_handle()
const errors = lazyErrors();
throw new errors.Error('ERR_UNKNOWN_STDIN_TYPE');
}

Expand Down Expand Up @@ -180,7 +171,6 @@ function createWritableStdioStream(fd) {

default:
// Probably an error on in uv_guess_handle()
const errors = lazyErrors();
throw new errors.Error('ERR_UNKNOWN_STREAM_TYPE');
}

Expand Down
26 changes: 9 additions & 17 deletions lib/internal/process/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,16 @@

const config = process.binding('config');
const prefix = `(${process.release.name}:${process.pid}) `;
const errors = require('internal/errors');

exports.setup = setupProcessWarnings;

var errors;
var fs;
var cachedFd;
var acquiringFd = false;
function nop() {}

function lazyErrors() {
if (!errors)
errors = require('internal/errors');
return errors;
}

function lazyFs() {
if (!fs)
fs = require('fs');
return fs;
}
// Lazily loaded
var fs = null;

function writeOut(message) {
if (console && typeof console.error === 'function')
Expand All @@ -31,7 +21,8 @@ function writeOut(message) {

function onClose(fd) {
return function() {
lazyFs().close(fd, nop);
if (fs === null) fs = require('fs');
fs.close(fd, nop);
};
}

Expand All @@ -53,14 +44,16 @@ function onAcquired(message) {
return function(err, fd) {
if (err)
return writeOut(message);
lazyFs().appendFile(fd, `${message}\n`, nop);
if (fs === null) fs = require('fs');
fs.appendFile(fd, `${message}\n`, nop);
};
}

function acquireFd(cb) {
if (cachedFd === undefined && !acquiringFd) {
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) {
cb(null, cachedFd);
} else {
Expand Down Expand Up @@ -112,7 +105,6 @@ function setupProcessWarnings() {
// process.emitWarning(str[, type[, code]][, ctor])
// process.emitWarning(str[, options])
process.emitWarning = function(warning, type, code, ctor, now) {
const errors = lazyErrors();
var detail;
if (type !== null && typeof type === 'object' && !Array.isArray(type)) {
ctor = type.ctor;
Expand Down
17 changes: 5 additions & 12 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ const async_id_symbol = process.binding('async_wrap').async_id_symbol;
const { newUid, setInitTriggerId } = require('async_hooks');
const nextTick = require('internal/process/next_tick').nextTick;
const errors = require('internal/errors');
const dns = require('dns');

var cluster;
var dns;
// `cluster` is only used by `listenInCluster` so for startup performance
// reasons it's lazy loaded.
var cluster = null;

const errnoException = util._errnoException;
const exceptionWithHostPort = util._exceptionWithHostPort;
Expand Down Expand Up @@ -1023,7 +1025,6 @@ Socket.prototype.connect = function(...args) {


function lookupAndConnect(self, options) {
const dns = lazyDns();
var host = options.host || 'localhost';
var port = options.port;
var localAddress = options.localAddress;
Expand Down Expand Up @@ -1371,18 +1372,11 @@ function emitListeningNT(self) {
}


function lazyDns() {
if (dns === undefined)
dns = require('dns');
return dns;
}


function listenInCluster(server, address, port, addressType,
backlog, fd, exclusive) {
exclusive = !!exclusive;

if (!cluster) cluster = require('cluster');
if (cluster === null) cluster = require('cluster');

if (cluster.isMaster || exclusive) {
// Will create a new handle
Expand Down Expand Up @@ -1491,7 +1485,6 @@ Server.prototype.listen = function(...args) {
};

function lookupAndListen(self, port, address, backlog, exclusive) {
const dns = lazyDns();
dns.lookup(address, function doListen(err, ip, addressType) {
if (err) {
self.emit('error', err);
Expand Down
6 changes: 2 additions & 4 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
'use strict';

const { debug, inherits } = require('util');
const Buffer = require('buffer').Buffer;
const { Buffer } = require('buffer');
const EventEmitter = require('events');
const { StringDecoder } = require('string_decoder');
const {
CSI,
emitKeys,
Expand Down Expand Up @@ -59,7 +60,6 @@ const ESCAPE_DECODER = Symbol('escape-decoder');
// GNU readline library - keyseq-timeout is 500ms (default)
const ESCAPE_CODE_TIMEOUT = 500;


function createInterface(input, output, completer, terminal) {
return new Interface(input, output, completer, terminal);
}
Expand Down Expand Up @@ -177,7 +177,6 @@ function Interface(input, output, completer, terminal) {
input.removeListener('data', ondata);
input.removeListener('end', onend);
});
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
this._decoder = new StringDecoder('utf8');

} else {
Expand Down Expand Up @@ -979,7 +978,6 @@ Interface.prototype._ttyWrite = function(s, key) {

function emitKeypressEvents(stream, iface) {
if (stream[KEYPRESS_DECODER]) return;
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
stream[KEYPRESS_DECODER] = new StringDecoder('utf8');

stream[ESCAPE_DECODER] = emitKeys(stream);
Expand Down

0 comments on commit cf1fe76

Please sign in to comment.