From 5f3b850da5fdfa83d48b56f46592aff80dc6e796 Mon Sep 17 00:00:00 2001 From: dnlup Date: Tue, 19 Mar 2019 17:13:20 +0100 Subject: [PATCH] lib: reduce usage of require('util') Replace `require('util').inspect` and `require('util').format` with `require('util/internal/inspect').inspect` and `require('util/internal/inspect').format` in `lib/internal/errors.js`. PR-URL: https://github.com/nodejs/node/pull/26782 Refs: https://github.com/nodejs/node/issues/26546 Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell --- lib/internal/errors.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index a80cf015242bfd..30af8f18f4ca2a 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -30,6 +30,14 @@ function lazyInternalUtil() { return internalUtil; } +let internalUtilInspect = null; +function lazyInternalUtilInspect() { + if (!internalUtilInspect) { + internalUtilInspect = require('internal/util/inspect'); + } + return internalUtilInspect; +} + let buffer; function lazyBuffer() { if (buffer === undefined) @@ -220,7 +228,6 @@ function E(sym, val, def, ...otherClasses) { function getMessage(key, args, self) { const msg = messages.get(key); - if (util === undefined) util = require('util'); if (assert === undefined) assert = require('internal/assert'); if (typeof msg === 'function') { @@ -242,7 +249,7 @@ function getMessage(key, args, self) { return msg; args.unshift(msg); - return util.format.apply(null, args); + return lazyInternalUtilInspect().format.apply(null, args); } let uvBinding; @@ -773,7 +780,7 @@ E('ERR_INVALID_ARG_TYPE', return msg; }, TypeError); E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => { - let inspected = util.inspect(value); + let inspected = lazyInternalUtilInspect().inspect(value); if (inspected.length > 128) { inspected = `${inspected.slice(0, 128)}...`; }