@@ -14,9 +14,9 @@ const kCode = Symbol('code');
1414const messages = new Map ( ) ;
1515const codes = { } ;
1616
17- var green = '' ;
18- var red = '' ;
19- var white = '' ;
17+ let green = '' ;
18+ let red = '' ;
19+ let white = '' ;
2020
2121const {
2222 UV_EAI_MEMORY ,
@@ -27,13 +27,7 @@ const { kMaxLength } = process.binding('buffer');
2727const { defineProperty } = Object ;
2828
2929// Lazily loaded
30- var util_ = null ;
31- function lazyUtil ( ) {
32- if ( ! util_ ) {
33- util_ = require ( 'util' ) ;
34- }
35- return util_ ;
36- }
30+ var util ;
3731
3832var internalUtil = null ;
3933function lazyInternalUtil ( ) {
@@ -43,6 +37,13 @@ function lazyInternalUtil() {
4337 return internalUtil ;
4438}
4539
40+ function inspectValue ( val ) {
41+ return util . inspect (
42+ val ,
43+ { compact : false , customInspect : false }
44+ ) . split ( '\n' ) ;
45+ }
46+
4647function makeNodeError ( Base ) {
4748 return class NodeError extends Base {
4849 constructor ( key , ...args ) {
@@ -89,11 +90,9 @@ function createErrDiff(actual, expected, operator) {
8990 var lastPos = 0 ;
9091 var end = '' ;
9192 var skipped = false ;
92- const util = lazyUtil ( ) ;
93- const actualLines = util
94- . inspect ( actual , { compact : false , customInspect : false } ) . split ( '\n' ) ;
95- const expectedLines = util
96- . inspect ( expected , { compact : false , customInspect : false } ) . split ( '\n' ) ;
93+ if ( util === undefined ) util = require ( 'util' ) ;
94+ const actualLines = inspectValue ( actual ) ;
95+ const expectedLines = inspectValue ( expected ) ;
9796 const msg = `Input A expected to ${ operator } input B:\n` +
9897 `${ green } + expected${ white } ${ red } - actual${ white } ` ;
9998 const skippedMsg = ' ... Lines skipped' ;
@@ -260,14 +259,20 @@ class AssertionError extends Error {
260259 if ( message != null ) {
261260 super ( message ) ;
262261 } else {
263- if ( util_ === null &&
264- process . stdout . isTTY &&
265- process . stdout . getColorDepth ( ) !== 1 ) {
266- green = '\u001b[32m' ;
267- white = '\u001b[39m' ;
268- red = '\u001b[31m' ;
262+ if ( process . stdout . isTTY ) {
263+ // Reset on each call to make sure we handle dynamically set environment
264+ // variables correct.
265+ if ( process . stdout . getColorDepth ( ) !== 1 ) {
266+ green = '\u001b[32m' ;
267+ white = '\u001b[39m' ;
268+ red = '\u001b[31m' ;
269+ } else {
270+ green = '' ;
271+ white = '' ;
272+ red = '' ;
273+ }
269274 }
270- const util = lazyUtil ( ) ;
275+ if ( util === undefined ) util = require ( 'util' ) ;
271276 if ( typeof actual === 'object' && actual !== null &&
272277 'stack' in actual && actual instanceof Error ) {
273278 actual = `${ actual . name } : ${ actual . message } ` ;
@@ -288,10 +293,7 @@ class AssertionError extends Error {
288293 } else if ( errorDiff === 1 ) {
289294 // In case the objects are equal but the operator requires unequal, show
290295 // the first object and say A equals B
291- const res = util . inspect (
292- actual ,
293- { compact : false , customInspect : false }
294- ) . split ( '\n' ) ;
296+ const res = inspectValue ( actual ) ;
295297
296298 if ( res . length > 20 ) {
297299 res [ 19 ] = '...' ;
@@ -333,10 +335,10 @@ function message(key, args) {
333335 const msg = messages . get ( key ) ;
334336 internalAssert ( msg , `An invalid error message key was used: ${ key } .` ) ;
335337 let fmt ;
338+ if ( util === undefined ) util = require ( 'util' ) ;
336339 if ( typeof msg === 'function' ) {
337340 fmt = msg ;
338341 } else {
339- const util = lazyUtil ( ) ;
340342 fmt = util . format ;
341343 if ( args === undefined || args . length === 0 )
342344 return msg ;
@@ -358,7 +360,8 @@ function errnoException(err, syscall, original) {
358360 // getSystemErrorName(err) to guard against invalid arguments from users.
359361 // This can be replaced with [ code ] = errmap.get(err) when this method
360362 // is no longer exposed to user land.
361- const code = lazyUtil ( ) . getSystemErrorName ( err ) ;
363+ if ( util === undefined ) util = require ( 'util' ) ;
364+ const code = util . getSystemErrorName ( err ) ;
362365 const message = original ?
363366 `${ syscall } ${ code } ${ original } ` : `${ syscall } ${ code } ` ;
364367
@@ -386,7 +389,8 @@ function exceptionWithHostPort(err, syscall, address, port, additional) {
386389 // getSystemErrorName(err) to guard against invalid arguments from users.
387390 // This can be replaced with [ code ] = errmap.get(err) when this method
388391 // is no longer exposed to user land.
389- const code = lazyUtil ( ) . getSystemErrorName ( err ) ;
392+ if ( util === undefined ) util = require ( 'util' ) ;
393+ const code = util . getSystemErrorName ( err ) ;
390394 let details = '' ;
391395 if ( port && port > 0 ) {
392396 details = ` ${ address } :${ port } ` ;
@@ -626,10 +630,9 @@ E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
626630E ( 'ERR_INVALID_ADDRESS_FAMILY' , 'Invalid address family: %s' , RangeError ) ;
627631E ( 'ERR_INVALID_ARG_TYPE' , invalidArgType , TypeError ) ;
628632E ( 'ERR_INVALID_ARG_VALUE' , ( name , value , reason = 'is invalid' ) => {
629- const util = lazyUtil ( ) ;
630633 let inspected = util . inspect ( value ) ;
631634 if ( inspected . length > 128 ) {
632- inspected = inspected . slice ( 0 , 128 ) + ' ...' ;
635+ inspected = ` ${ inspected . slice ( 0 , 128 ) } ...` ;
633636 }
634637 return `The argument '${ name } ' ${ reason } . Received ${ inspected } ` ;
635638} , TypeError , RangeError ) ; // Some are currently falsy implemented as "Error"
0 commit comments