@@ -15,9 +15,9 @@ const kInfo = Symbol('info');
1515const messages = new Map ( ) ;
1616const codes = { } ;
1717
18- var green = '' ;
19- var red = '' ;
20- var white = '' ;
18+ let green = '' ;
19+ let red = '' ;
20+ let white = '' ;
2121
2222const {
2323 errmap,
@@ -29,16 +29,9 @@ const { kMaxLength } = process.binding('buffer');
2929const { defineProperty } = Object ;
3030
3131// Lazily loaded
32- var util_ = null ;
32+ var util ;
3333var buffer ;
3434
35- function lazyUtil ( ) {
36- if ( ! util_ ) {
37- util_ = require ( 'util' ) ;
38- }
39- return util_ ;
40- }
41-
4235var internalUtil = null ;
4336function lazyInternalUtil ( ) {
4437 if ( ! internalUtil ) {
@@ -47,6 +40,13 @@ function lazyInternalUtil() {
4740 return internalUtil ;
4841}
4942
43+ function inspectValue ( val ) {
44+ return util . inspect (
45+ val ,
46+ { compact : false , customInspect : false }
47+ ) . split ( '\n' ) ;
48+ }
49+
5050function makeNodeError ( Base ) {
5151 return class NodeError extends Base {
5252 constructor ( key , ...args ) {
@@ -210,11 +210,9 @@ function createErrDiff(actual, expected, operator) {
210210 var lastPos = 0 ;
211211 var end = '' ;
212212 var skipped = false ;
213- const util = lazyUtil ( ) ;
214- const actualLines = util
215- . inspect ( actual , { compact : false , customInspect : false } ) . split ( '\n' ) ;
216- const expectedLines = util
217- . inspect ( expected , { compact : false , customInspect : false } ) . split ( '\n' ) ;
213+ if ( util === undefined ) util = require ( 'util' ) ;
214+ const actualLines = inspectValue ( actual ) ;
215+ const expectedLines = inspectValue ( expected ) ;
218216 const msg = `Input A expected to ${ operator } input B:\n` +
219217 `${ green } + expected${ white } ${ red } - actual${ white } ` ;
220218 const skippedMsg = ' ... Lines skipped' ;
@@ -333,14 +331,20 @@ class AssertionError extends Error {
333331 if ( message != null ) {
334332 super ( message ) ;
335333 } else {
336- if ( util_ === null &&
337- process . stdout . isTTY &&
338- process . stdout . getColorDepth ( ) !== 1 ) {
339- green = '\u001b[32m' ;
340- white = '\u001b[39m' ;
341- red = '\u001b[31m' ;
334+ if ( process . stdout . isTTY ) {
335+ // Reset on each call to make sure we handle dynamically set environment
336+ // variables correct.
337+ if ( process . stdout . getColorDepth ( ) !== 1 ) {
338+ green = '\u001b[32m' ;
339+ white = '\u001b[39m' ;
340+ red = '\u001b[31m' ;
341+ } else {
342+ green = '' ;
343+ white = '' ;
344+ red = '' ;
345+ }
342346 }
343- const util = lazyUtil ( ) ;
347+ if ( util === undefined ) util = require ( 'util' ) ;
344348 if ( typeof actual === 'object' && actual !== null &&
345349 'stack' in actual && actual instanceof Error ) {
346350 actual = `${ actual . name } : ${ actual . message } ` ;
@@ -361,10 +365,7 @@ class AssertionError extends Error {
361365 } else if ( errorDiff === 1 ) {
362366 // In case the objects are equal but the operator requires unequal, show
363367 // the first object and say A equals B
364- const res = util . inspect (
365- actual ,
366- { compact : false , customInspect : false }
367- ) . split ( '\n' ) ;
368+ const res = inspectValue ( actual ) ;
368369
369370 if ( res . length > 20 ) {
370371 res [ 19 ] = '...' ;
@@ -406,10 +407,10 @@ function message(key, args) {
406407 const msg = messages . get ( key ) ;
407408 internalAssert ( msg , `An invalid error message key was used: ${ key } .` ) ;
408409 let fmt ;
410+ if ( util === undefined ) util = require ( 'util' ) ;
409411 if ( typeof msg === 'function' ) {
410412 fmt = msg ;
411413 } else {
412- const util = lazyUtil ( ) ;
413414 fmt = util . format ;
414415 if ( args === undefined || args . length === 0 )
415416 return msg ;
@@ -479,7 +480,8 @@ function errnoException(err, syscall, original) {
479480 // getSystemErrorName(err) to guard against invalid arguments from users.
480481 // This can be replaced with [ code ] = errmap.get(err) when this method
481482 // is no longer exposed to user land.
482- const code = lazyUtil ( ) . getSystemErrorName ( err ) ;
483+ if ( util === undefined ) util = require ( 'util' ) ;
484+ const code = util . getSystemErrorName ( err ) ;
483485 const message = original ?
484486 `${ syscall } ${ code } ${ original } ` : `${ syscall } ${ code } ` ;
485487
@@ -508,7 +510,8 @@ function exceptionWithHostPort(err, syscall, address, port, additional) {
508510 // getSystemErrorName(err) to guard against invalid arguments from users.
509511 // This can be replaced with [ code ] = errmap.get(err) when this method
510512 // is no longer exposed to user land.
511- const code = lazyUtil ( ) . getSystemErrorName ( err ) ;
513+ if ( util === undefined ) util = require ( 'util' ) ;
514+ const code = util . getSystemErrorName ( err ) ;
512515 let details = '' ;
513516 if ( port && port > 0 ) {
514517 details = ` ${ address } :${ port } ` ;
@@ -746,10 +749,9 @@ E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
746749E ( 'ERR_INVALID_ADDRESS_FAMILY' , 'Invalid address family: %s' , RangeError ) ;
747750E ( 'ERR_INVALID_ARG_TYPE' , invalidArgType , TypeError ) ;
748751E ( 'ERR_INVALID_ARG_VALUE' , ( name , value , reason = 'is invalid' ) => {
749- const util = lazyUtil ( ) ;
750752 let inspected = util . inspect ( value ) ;
751753 if ( inspected . length > 128 ) {
752- inspected = inspected . slice ( 0 , 128 ) + ' ...' ;
754+ inspected = ` ${ inspected . slice ( 0 , 128 ) } ...` ;
753755 }
754756 return `The argument '${ name } ' ${ reason } . Received ${ inspected } ` ;
755757} , TypeError , RangeError ) ;
0 commit comments