Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: load internal/errors eagerly in events.js and perf_hooks.js #26771

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

var spliceOne;

const {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE,
ERR_UNHANDLED_ERROR
} = require('internal/errors').codes;

function EventEmitter() {
EventEmitter.init.call(this);
}
Expand All @@ -42,17 +48,9 @@ EventEmitter.prototype._maxListeners = undefined;
// added to it. This is a useful default which helps finding memory leaks.
var defaultMaxListeners = 10;

var errors;
function lazyErrors() {
if (errors === undefined)
errors = require('internal/errors').codes;
return errors;
}

function checkListener(listener) {
if (typeof listener !== 'function') {
const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
throw new ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
}
}

Expand All @@ -63,10 +61,9 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
},
set: function(arg) {
if (typeof arg !== 'number' || arg < 0 || Number.isNaN(arg)) {
const errors = lazyErrors();
throw new errors.ERR_OUT_OF_RANGE('defaultMaxListeners',
'a non-negative number',
arg);
throw new ERR_OUT_OF_RANGE('defaultMaxListeners',
'a non-negative number',
arg);
}
defaultMaxListeners = arg;
}
Expand All @@ -87,8 +84,7 @@ EventEmitter.init = function() {
// that to be increased. Set to zero for unlimited.
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
if (typeof n !== 'number' || n < 0 || Number.isNaN(n)) {
const errors = lazyErrors();
throw new errors.ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
}
this._maxListeners = n;
return this;
Expand Down Expand Up @@ -183,8 +179,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
}

// At least give some kind of context to the user
const errors = lazyErrors();
const err = new errors.ERR_UNHANDLED_ERROR(stringifiedEr);
const err = new ERR_UNHANDLED_ERROR(stringifiedEr);
err.context = er;
throw err; // Unhandled 'error' event
}
Expand Down
52 changes: 22 additions & 30 deletions lib/perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ const { AsyncResource } = require('async_hooks');
const L = require('internal/linkedlist');
const kInspect = require('internal/util').customInspectSymbol;

const {
ERR_INVALID_CALLBACK,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_OPT_VALUE,
ERR_VALID_PERFORMANCE_ENTRY_TYPE,
ERR_INVALID_PERFORMANCE_MARK
} = require('internal/errors').codes;

const kHandle = Symbol('handle');
const kMap = Symbol('map');
const kCallback = Symbol('callback');
Expand Down Expand Up @@ -125,14 +134,6 @@ function collectHttp2Stats(entry) {
}
}


let errors;
function lazyErrors() {
if (errors === undefined)
errors = require('internal/errors').codes;
return errors;
}

function now() {
const hr = process.hrtime();
return hr[0] * 1000 + hr[1] / 1e6;
Expand Down Expand Up @@ -282,8 +283,7 @@ let gcTrackingIsEnabled = false;
class PerformanceObserver extends AsyncResource {
constructor(callback) {
if (typeof callback !== 'function') {
const errors = lazyErrors();
throw new errors.ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK();
}
super('PerformanceObserver');
Object.defineProperties(this, {
Expand Down Expand Up @@ -329,16 +329,15 @@ class PerformanceObserver extends AsyncResource {
}

observe(options) {
const errors = lazyErrors();
if (typeof options !== 'object' || options == null) {
throw new errors.ERR_INVALID_ARG_TYPE('options', 'Object', options);
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
if (!Array.isArray(options.entryTypes)) {
throw new errors.ERR_INVALID_OPT_VALUE('entryTypes', options);
throw new ERR_INVALID_OPT_VALUE('entryTypes', options);
}
const entryTypes = options.entryTypes.filter(filterTypes).map(mapTypes);
if (entryTypes.length === 0) {
throw new errors.ERR_VALID_PERFORMANCE_ENTRY_TYPE();
throw new ERR_VALID_PERFORMANCE_ENTRY_TYPE();
}
if (entryTypes.includes(NODE_PERFORMANCE_ENTRY_TYPE_GC) &&
!gcTrackingIsEnabled) {
Expand Down Expand Up @@ -391,8 +390,7 @@ class Performance {
startMark = startMark !== undefined ? `${startMark}` : '';
const marks = this[kIndex][kMarks];
if (!marks.has(endMark) && !(endMark in nodeTiming)) {
const errors = lazyErrors();
throw new errors.ERR_INVALID_PERFORMANCE_MARK(endMark);
throw new ERR_INVALID_PERFORMANCE_MARK(endMark);
}
_measure(name, startMark, endMark);
}
Expand All @@ -410,8 +408,7 @@ class Performance {

timerify(fn) {
if (typeof fn !== 'function') {
const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
}
if (fn[kTimerified])
return fn[kTimerified];
Expand Down Expand Up @@ -565,13 +562,11 @@ class ELDHistogram {
get stddev() { return this[kHandle].stddev(); }
percentile(percentile) {
if (typeof percentile !== 'number') {
const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('percentile', 'number', percentile);
throw new ERR_INVALID_ARG_TYPE('percentile', 'number', percentile);
}
if (percentile <= 0 || percentile > 100) {
const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_VALUE.RangeError('percentile',
percentile);
throw new ERR_INVALID_ARG_VALUE.RangeError('percentile',
percentile);
}
return this[kHandle].percentile(percentile);
}
Expand All @@ -595,18 +590,15 @@ class ELDHistogram {

function monitorEventLoopDelay(options = {}) {
if (typeof options !== 'object' || options === null) {
const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('options', 'Object', options);
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}
const { resolution = 10 } = options;
if (typeof resolution !== 'number') {
const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('options.resolution',
'number', resolution);
throw new ERR_INVALID_ARG_TYPE('options.resolution',
'number', resolution);
}
if (resolution <= 0 || !Number.isSafeInteger(resolution)) {
const errors = lazyErrors();
throw new errors.ERR_INVALID_OPT_VALUE.RangeError('resolution', resolution);
throw new ERR_INVALID_OPT_VALUE.RangeError('resolution', resolution);
}
return new ELDHistogram(new _ELDHistogram(resolution));
}
Expand Down