@@ -15,9 +15,9 @@ const kInfo = Symbol('info');
15
15
const messages = new Map ( ) ;
16
16
const codes = { } ;
17
17
18
- var green = '' ;
19
- var red = '' ;
20
- var white = '' ;
18
+ let green = '' ;
19
+ let red = '' ;
20
+ let white = '' ;
21
21
22
22
const {
23
23
errmap,
@@ -29,16 +29,9 @@ const { kMaxLength } = process.binding('buffer');
29
29
const { defineProperty } = Object ;
30
30
31
31
// Lazily loaded
32
- var util_ = null ;
32
+ var util ;
33
33
var buffer ;
34
34
35
- function lazyUtil ( ) {
36
- if ( ! util_ ) {
37
- util_ = require ( 'util' ) ;
38
- }
39
- return util_ ;
40
- }
41
-
42
35
var internalUtil = null ;
43
36
function lazyInternalUtil ( ) {
44
37
if ( ! internalUtil ) {
@@ -47,6 +40,13 @@ function lazyInternalUtil() {
47
40
return internalUtil ;
48
41
}
49
42
43
+ function inspectValue ( val ) {
44
+ return util . inspect (
45
+ val ,
46
+ { compact : false , customInspect : false }
47
+ ) . split ( '\n' ) ;
48
+ }
49
+
50
50
function makeNodeError ( Base ) {
51
51
return class NodeError extends Base {
52
52
constructor ( key , ...args ) {
@@ -210,11 +210,9 @@ function createErrDiff(actual, expected, operator) {
210
210
var lastPos = 0 ;
211
211
var end = '' ;
212
212
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 ) ;
218
216
const msg = `Input A expected to ${ operator } input B:\n` +
219
217
`${ green } + expected${ white } ${ red } - actual${ white } ` ;
220
218
const skippedMsg = ' ... Lines skipped' ;
@@ -333,14 +331,20 @@ class AssertionError extends Error {
333
331
if ( message != null ) {
334
332
super ( message ) ;
335
333
} 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
+ }
342
346
}
343
- const util = lazyUtil ( ) ;
347
+ if ( util === undefined ) util = require ( 'util' ) ;
344
348
if ( typeof actual === 'object' && actual !== null &&
345
349
'stack' in actual && actual instanceof Error ) {
346
350
actual = `${ actual . name } : ${ actual . message } ` ;
@@ -361,10 +365,7 @@ class AssertionError extends Error {
361
365
} else if ( errorDiff === 1 ) {
362
366
// In case the objects are equal but the operator requires unequal, show
363
367
// 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 ) ;
368
369
369
370
if ( res . length > 20 ) {
370
371
res [ 19 ] = '...' ;
@@ -406,10 +407,10 @@ function message(key, args) {
406
407
const msg = messages . get ( key ) ;
407
408
internalAssert ( msg , `An invalid error message key was used: ${ key } .` ) ;
408
409
let fmt ;
410
+ if ( util === undefined ) util = require ( 'util' ) ;
409
411
if ( typeof msg === 'function' ) {
410
412
fmt = msg ;
411
413
} else {
412
- const util = lazyUtil ( ) ;
413
414
fmt = util . format ;
414
415
if ( args === undefined || args . length === 0 )
415
416
return msg ;
@@ -479,7 +480,8 @@ function errnoException(err, syscall, original) {
479
480
// getSystemErrorName(err) to guard against invalid arguments from users.
480
481
// This can be replaced with [ code ] = errmap.get(err) when this method
481
482
// 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 ) ;
483
485
const message = original ?
484
486
`${ syscall } ${ code } ${ original } ` : `${ syscall } ${ code } ` ;
485
487
@@ -508,7 +510,8 @@ function exceptionWithHostPort(err, syscall, address, port, additional) {
508
510
// getSystemErrorName(err) to guard against invalid arguments from users.
509
511
// This can be replaced with [ code ] = errmap.get(err) when this method
510
512
// 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 ) ;
512
515
let details = '' ;
513
516
if ( port && port > 0 ) {
514
517
details = ` ${ address } :${ port } ` ;
@@ -768,10 +771,9 @@ E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
768
771
E ( 'ERR_INVALID_ADDRESS_FAMILY' , 'Invalid address family: %s' , RangeError ) ;
769
772
E ( 'ERR_INVALID_ARG_TYPE' , invalidArgType , TypeError ) ;
770
773
E ( 'ERR_INVALID_ARG_VALUE' , ( name , value , reason = 'is invalid' ) => {
771
- const util = lazyUtil ( ) ;
772
774
let inspected = util . inspect ( value ) ;
773
775
if ( inspected . length > 128 ) {
774
- inspected = inspected . slice ( 0 , 128 ) + ' ...' ;
776
+ inspected = ` ${ inspected . slice ( 0 , 128 ) } ...` ;
775
777
}
776
778
return `The argument '${ name } ' ${ reason } . Received ${ inspected } ` ;
777
779
} , TypeError , RangeError ) ;
0 commit comments