@@ -12,7 +12,6 @@ const kCode = Symbol('code');
12
12
const messages = new Map ( ) ;
13
13
14
14
// Lazily loaded
15
- var assert = null ;
16
15
var util = null ;
17
16
18
17
function makeNodeError ( Base ) {
@@ -60,11 +59,22 @@ class AssertionError extends Error {
60
59
}
61
60
}
62
61
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
+
63
75
function message ( key , args ) {
64
- if ( assert === null ) assert = require ( 'assert' ) ;
65
- assert . strictEqual ( typeof key , 'string' ) ;
66
76
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 } .` ) ;
68
78
let fmt ;
69
79
if ( typeof msg === 'function' ) {
70
80
fmt = msg ;
@@ -184,6 +194,11 @@ E('ERR_HTTP2_UNSUPPORTED_PROTOCOL',
184
194
( protocol ) => `protocol "${ protocol } " is unsupported.` ) ;
185
195
E ( 'ERR_INDEX_OUT_OF_RANGE' , 'Index out of range' ) ;
186
196
E ( '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
+ } ) ;
187
202
E ( 'ERR_INVALID_ASYNC_ID' , ( type , id ) => `Invalid ${ type } value: ${ id } ` ) ;
188
203
E ( 'ERR_INVALID_CALLBACK' , 'callback must be a function' ) ;
189
204
E ( 'ERR_INVALID_FD' , ( fd ) => `"fd" must be a positive integer: ${ fd } ` ) ;
@@ -239,7 +254,7 @@ E('ERR_VALID_PERFORMANCE_ENTRY_TYPE',
239
254
// Add new errors from here...
240
255
241
256
function invalidArgType ( name , expected , actual ) {
242
- assert ( name , 'name is required' ) ;
257
+ internalAssert ( name , 'name is required' ) ;
243
258
var msg = `The "${ name } " argument must be ${ oneOf ( expected , 'type' ) } ` ;
244
259
if ( arguments . length >= 3 ) {
245
260
msg += `. Received type ${ actual !== null ? typeof actual : 'null' } ` ;
@@ -248,7 +263,7 @@ function invalidArgType(name, expected, actual) {
248
263
}
249
264
250
265
function 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' ) ;
252
267
let msg = 'The ' ;
253
268
const len = args . length ;
254
269
args = args . map ( ( a ) => `"${ a } "` ) ;
@@ -268,11 +283,12 @@ function missingArgs(...args) {
268
283
}
269
284
270
285
function 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' ) ;
273
288
if ( Array . isArray ( expected ) ) {
274
289
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' ) ;
276
292
expected = expected . map ( ( i ) => String ( i ) ) ;
277
293
if ( len > 2 ) {
278
294
return `one of ${ thing } ${ expected . slice ( 0 , len - 1 ) . join ( ', ' ) } , or ` +
0 commit comments