@@ -12,7 +12,6 @@ const kCode = Symbol('code');
1212const messages = new Map ( ) ;
1313
1414// Lazily loaded
15- var assert = null ;
1615var util = null ;
1716
1817function makeNodeError ( Base ) {
@@ -60,11 +59,22 @@ class AssertionError extends Error {
6059 }
6160}
6261
62+ // This is defined here instead of using the assert module to avoid a
63+ // circular dependency. The effect is largely the same.
64+ function internalAssert ( condition , message ) {
65+ if ( ! condition ) {
66+ throw new AssertionError ( {
67+ message,
68+ actual : false ,
69+ expected : true ,
70+ operator : '=='
71+ } ) ;
72+ }
73+ }
74+
6375function message ( key , args ) {
64- if ( assert === null ) assert = require ( 'assert' ) ;
65- assert . strictEqual ( typeof key , 'string' ) ;
6676 const msg = messages . get ( key ) ;
67- assert ( msg , `An invalid error message key was used: ${ key } .` ) ;
77+ internalAssert ( msg , `An invalid error message key was used: ${ key } .` ) ;
6878 let fmt ;
6979 if ( typeof msg === 'function' ) {
7080 fmt = msg ;
@@ -184,6 +194,11 @@ E('ERR_HTTP2_UNSUPPORTED_PROTOCOL',
184194 ( protocol ) => `protocol "${ protocol } " is unsupported.` ) ;
185195E ( 'ERR_INDEX_OUT_OF_RANGE' , 'Index out of range' ) ;
186196E ( 'ERR_INVALID_ARG_TYPE' , invalidArgType ) ;
197+ E ( 'ERR_INVALID_ARRAY_LENGTH' ,
198+ ( name , len , actual ) => {
199+ internalAssert ( typeof actual === 'number' , 'actual must be a number' ) ;
200+ return `The array "${ name } " (length ${ actual } ) must be of length ${ len } .` ;
201+ } ) ;
187202E ( 'ERR_INVALID_ASYNC_ID' , ( type , id ) => `Invalid ${ type } value: ${ id } ` ) ;
188203E ( 'ERR_INVALID_CALLBACK' , 'callback must be a function' ) ;
189204E ( 'ERR_INVALID_FD' , ( fd ) => `"fd" must be a positive integer: ${ fd } ` ) ;
@@ -239,7 +254,7 @@ E('ERR_VALID_PERFORMANCE_ENTRY_TYPE',
239254// Add new errors from here...
240255
241256function invalidArgType ( name , expected , actual ) {
242- assert ( name , 'name is required' ) ;
257+ internalAssert ( name , 'name is required' ) ;
243258 var msg = `The "${ name } " argument must be ${ oneOf ( expected , 'type' ) } ` ;
244259 if ( arguments . length >= 3 ) {
245260 msg += `. Received type ${ actual !== null ? typeof actual : 'null' } ` ;
@@ -248,7 +263,7 @@ function invalidArgType(name, expected, actual) {
248263}
249264
250265function missingArgs ( ...args ) {
251- assert ( args . length > 0 , 'At least one arg needs to be specified' ) ;
266+ internalAssert ( args . length > 0 , 'At least one arg needs to be specified' ) ;
252267 let msg = 'The ' ;
253268 const len = args . length ;
254269 args = args . map ( ( a ) => `"${ a } "` ) ;
@@ -268,11 +283,12 @@ function missingArgs(...args) {
268283}
269284
270285function oneOf ( expected , thing ) {
271- assert ( expected , 'expected is required' ) ;
272- assert ( typeof thing === 'string' , 'thing is required' ) ;
286+ internalAssert ( expected , 'expected is required' ) ;
287+ internalAssert ( typeof thing === 'string' , 'thing is required' ) ;
273288 if ( Array . isArray ( expected ) ) {
274289 const len = expected . length ;
275- assert ( len > 0 , 'At least one expected value needs to be specified' ) ;
290+ internalAssert ( len > 0 ,
291+ 'At least one expected value needs to be specified' ) ;
276292 expected = expected . map ( ( i ) => String ( i ) ) ;
277293 if ( len > 2 ) {
278294 return `one of ${ thing } ${ expected . slice ( 0 , len - 1 ) . join ( ', ' ) } , or ` +
0 commit comments