@@ -67,9 +67,6 @@ Object.defineProperty(exports, 'constants', {
67
67
68
68
exports . kStringMaxLength = binding . kStringMaxLength ;
69
69
70
- const kFromErrorMsg = 'First argument must be a string, Buffer, ' +
71
- 'ArrayBuffer, Array, or array-like object.' ;
72
-
73
70
Buffer . poolSize = 8 * 1024 ;
74
71
var poolSize , poolOffset , allocPool ;
75
72
@@ -146,9 +143,8 @@ function Buffer(arg, encodingOrOffset, length) {
146
143
// Common case.
147
144
if ( typeof arg === 'number' ) {
148
145
if ( typeof encodingOrOffset === 'string' ) {
149
- throw new Error (
150
- 'If encoding is specified then the first argument must be a string'
151
- ) ;
146
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'string' ,
147
+ 'string' , arg ) ;
152
148
}
153
149
return Buffer . alloc ( arg ) ;
154
150
}
@@ -177,10 +173,12 @@ Buffer.from = function(value, encodingOrOffset, length) {
177
173
return fromArrayBuffer ( value , encodingOrOffset , length ) ;
178
174
179
175
if ( value == null )
180
- throw new TypeError ( kFromErrorMsg ) ;
176
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'first argument' ,
177
+ [ 'string' , 'buffer' , 'arrayBuffer' , 'array' , 'array-like object' ] , value ) ;
181
178
182
179
if ( typeof value === 'number' )
183
- throw new TypeError ( '"value" argument must not be a number' ) ;
180
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'value' , 'not number' ,
181
+ value ) ;
184
182
185
183
const valueOf = value . valueOf && value . valueOf ( ) ;
186
184
if ( valueOf != null && valueOf !== value )
@@ -196,7 +194,8 @@ Buffer.from = function(value, encodingOrOffset, length) {
196
194
length ) ;
197
195
}
198
196
199
- throw new TypeError ( kFromErrorMsg ) ;
197
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'first argument' ,
198
+ [ 'string' , 'buffer' , 'arrayBuffer' , 'array' , 'array-like object' ] ) ;
200
199
} ;
201
200
202
201
Object . setPrototypeOf ( Buffer , Uint8Array ) ;
@@ -208,12 +207,11 @@ function assertSize(size) {
208
207
let err = null ;
209
208
210
209
if ( typeof size !== 'number' ) {
211
- err = new TypeError ( '" size" argument must be a number' ) ;
210
+ err = new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , ' size' , ' number', size ) ;
212
211
} else if ( size < 0 ) {
213
- err = new RangeError ( '"size" argument must not be negative' ) ;
212
+ err = new errors . RangeError ( 'ERR_INVALID_OPT_VALUE' , 'size' , size ) ;
214
213
} else if ( size > binding . kMaxLength ) {
215
- err = new RangeError ( '"size" argument must not be larger ' +
216
- 'than ' + binding . kMaxLength ) ;
214
+ err = new errors . RangeError ( 'ERR_INVALID_OPT_VALUE' , 'size' , size ) ;
217
215
}
218
216
219
217
if ( err ) {
@@ -300,7 +298,7 @@ function fromString(string, encoding) {
300
298
} else {
301
299
length = byteLength ( string , encoding , true ) ;
302
300
if ( length === - 1 )
303
- throw new TypeError ( '"encoding" must be a valid string encoding' ) ;
301
+ throw new errors . TypeError ( 'ERR_UNKNOWN_ENCODING' , encoding ) ;
304
302
if ( string . length === 0 )
305
303
return new FastBuffer ( ) ;
306
304
}
@@ -343,7 +341,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
343
341
const maxLength = obj . byteLength - byteOffset ;
344
342
345
343
if ( maxLength < 0 )
346
- throw new RangeError ( "'offset' is out of bounds" ) ;
344
+ throw new errors . RangeError ( 'ERR_BUFFER_OUT_OF_BOUNDS' , 'offset' ) ;
347
345
348
346
if ( length === undefined ) {
349
347
length = maxLength ;
@@ -355,7 +353,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
355
353
length = 0 ;
356
354
} else if ( length > 0 ) {
357
355
if ( length > maxLength )
358
- throw new RangeError ( "'length' is out of bounds" ) ;
356
+ throw new errors . RangeError ( 'ERR_BUFFER_OUT_OF_BOUNDS' , 'length' ) ;
359
357
} else {
360
358
length = 0 ;
361
359
}
@@ -399,7 +397,8 @@ Buffer.isBuffer = function isBuffer(b) {
399
397
400
398
Buffer . compare = function compare ( a , b ) {
401
399
if ( ! isUint8Array ( a ) || ! isUint8Array ( b ) ) {
402
- throw new TypeError ( 'Arguments must be Buffers or Uint8Arrays' ) ;
400
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , [ 'buf1' , 'buf2' ] ,
401
+ [ 'buffer' , 'uint8Array' ] ) ;
403
402
}
404
403
405
404
if ( a === b ) {
@@ -416,13 +415,13 @@ Buffer.isEncoding = function(encoding) {
416
415
} ;
417
416
Buffer [ internalUtil . kIsEncodingSymbol ] = Buffer . isEncoding ;
418
417
419
- const kConcatErrMsg = '"list" argument must be an Array ' +
420
- 'of Buffer or Uint8Array instances' ;
418
+ const kConcatErr = new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'list' ,
419
+ [ 'array' , 'buffer' , 'uint8Array' ] ) ;
421
420
422
421
Buffer . concat = function ( list , length ) {
423
422
var i ;
424
423
if ( ! Array . isArray ( list ) )
425
- throw new TypeError ( kConcatErrMsg ) ;
424
+ throw kConcatErr ;
426
425
427
426
if ( list . length === 0 )
428
427
return new FastBuffer ( ) ;
@@ -440,7 +439,7 @@ Buffer.concat = function(list, length) {
440
439
for ( i = 0 ; i < list . length ; i ++ ) {
441
440
var buf = list [ i ] ;
442
441
if ( ! isUint8Array ( buf ) )
443
- throw new TypeError ( kConcatErrMsg ) ;
442
+ throw kConcatErr ;
444
443
binding . copy ( buf , buffer , pos ) ;
445
444
pos += buf . length ;
446
445
}
@@ -475,7 +474,8 @@ function byteLength(string, encoding) {
475
474
return string . byteLength ;
476
475
}
477
476
478
- throw new TypeError ( '"string" must be a string, Buffer, or ArrayBuffer' ) ;
477
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'string' ,
478
+ [ 'string' , 'buffer' , 'arrayBuffer' ] ) ;
479
479
}
480
480
481
481
const len = string . length ;
@@ -591,7 +591,7 @@ function stringSlice(buf, encoding, start, end) {
591
591
return buf . ucs2Slice ( start , end ) ;
592
592
break ;
593
593
}
594
- throw new TypeError ( 'Unknown encoding: ' + encoding ) ;
594
+ throw new errors . TypeError ( 'ERR_UNKNOWN_ENCODING' , encoding ) ;
595
595
}
596
596
597
597
@@ -633,8 +633,8 @@ Buffer.prototype.toString = function(encoding, start, end) {
633
633
634
634
Buffer . prototype . equals = function equals ( b ) {
635
635
if ( ! isUint8Array ( b ) )
636
- throw new TypeError ( 'Argument must be a Buffer or Uint8Array' ) ;
637
-
636
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'otherBuffer' ,
637
+ [ 'buffer' , 'uint8Array' ] ) ;
638
638
if ( this === b )
639
639
return true ;
640
640
@@ -659,7 +659,8 @@ Buffer.prototype.compare = function compare(target,
659
659
thisStart ,
660
660
thisEnd ) {
661
661
if ( ! isUint8Array ( target ) )
662
- throw new TypeError ( 'Argument must be a Buffer or Uint8Array' ) ;
662
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'target' ,
663
+ [ 'buffer' , 'uint8Array' ] ) ;
663
664
if ( arguments . length === 1 )
664
665
return compare_ ( this , target ) ;
665
666
@@ -738,8 +739,8 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
738
739
return binding . indexOfNumber ( buffer , val , byteOffset , dir ) ;
739
740
}
740
741
741
- throw new TypeError ( '"val" argument must be string, number, Buffer ' +
742
- 'or Uint8Array' ) ;
742
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'val' ,
743
+ [ 'string' , 'buffer' , 'uint8Array' ] ) ;
743
744
}
744
745
745
746
@@ -765,7 +766,7 @@ function slowIndexOf(buffer, val, byteOffset, encoding, dir) {
765
766
766
767
default :
767
768
if ( loweredCase ) {
768
- throw new TypeError ( 'Unknown encoding: ' + encoding ) ;
769
+ throw new errors . TypeError ( 'ERR_UNKNOWN_ENCODING' , encoding ) ;
769
770
}
770
771
771
772
encoding = ( '' + encoding ) . toLowerCase ( ) ;
@@ -807,11 +808,11 @@ Buffer.prototype.fill = function fill(val, start, end, encoding) {
807
808
}
808
809
809
810
if ( encoding !== undefined && typeof encoding !== 'string' ) {
810
- throw new TypeError ( 'encoding must be a string' ) ;
811
+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , ' encoding' , ' string') ;
811
812
}
812
813
var normalizedEncoding = internalUtil . normalizeEncoding ( encoding ) ;
813
814
if ( normalizedEncoding === undefined ) {
814
- throw new TypeError ( 'Unknown encoding: ' + encoding ) ;
815
+ throw new errors . TypeError ( 'ERR_UNKNOWN_ENCODING' , encoding ) ;
815
816
}
816
817
817
818
if ( val . length === 0 ) {
@@ -872,13 +873,13 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
872
873
length = remaining ;
873
874
874
875
if ( string . length > 0 && ( length < 0 || offset < 0 ) )
875
- throw new RangeError ( 'Attempt to write outside buffer bounds' ) ;
876
+ throw new errors . RangeError ( 'ERR_BUFFER_OUT_OF_BOUNDS' , 'length' , true ) ;
876
877
} else {
877
878
// if someone is still calling the obsolete form of write(), tell them.
878
879
// we don't want eg buf.write("foo", "utf8", 10) to silently turn into
879
880
// buf.write("foo", "utf8"), so we can't ignore extra args
880
- throw new Error ( 'Buffer.write(string, encoding, offset[, length]) ' +
881
- 'is no longer supported ') ;
881
+ throw new errors . Error ( 'ERR_NO_LONGER_SUPPORTED' ,
882
+ 'Buffer.write(string, encoding, offset[, length]) ') ;
882
883
}
883
884
884
885
if ( ! encoding ) return this . utf8Write ( string , offset , length ) ;
@@ -925,7 +926,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
925
926
return this . hexWrite ( string , offset , length ) ;
926
927
break ;
927
928
}
928
- throw new TypeError ( 'Unknown encoding: ' + encoding ) ;
929
+ throw new errors . TypeError ( 'ERR_UNKNOWN_ENCODING' , encoding ) ;
929
930
} ;
930
931
931
932
@@ -1176,7 +1177,7 @@ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
1176
1177
1177
1178
function checkInt ( buffer , value , offset , ext , max , min ) {
1178
1179
if ( value > max || value < min )
1179
- throw new TypeError ( '"value" argument is out of bounds' ) ;
1180
+ throw new errors . RangeError ( 'ERR_INVALID_OPT_VALUE' , 'value' , value ) ;
1180
1181
if ( offset + ext > buffer . length )
1181
1182
throw new errors . RangeError ( 'ERR_INDEX_OUT_OF_RANGE' ) ;
1182
1183
}
@@ -1448,7 +1449,7 @@ Buffer.prototype.swap16 = function swap16() {
1448
1449
// dropping down to the native code is faster.
1449
1450
const len = this . length ;
1450
1451
if ( len % 2 !== 0 )
1451
- throw new RangeError ( 'Buffer size must be a multiple of 16-bits' ) ;
1452
+ throw new errors . RangeError ( 'ERR_INVALID_BUFFER_SIZE' , ' 16-bits') ;
1452
1453
if ( len < 128 ) {
1453
1454
for ( var i = 0 ; i < len ; i += 2 )
1454
1455
swap ( this , i , i + 1 ) ;
@@ -1464,7 +1465,7 @@ Buffer.prototype.swap32 = function swap32() {
1464
1465
// dropping down to the native code is faster.
1465
1466
const len = this . length ;
1466
1467
if ( len % 4 !== 0 )
1467
- throw new RangeError ( 'Buffer size must be a multiple of 32-bits' ) ;
1468
+ throw new errors . RangeError ( 'ERR_INVALID_BUFFER_SIZE' , ' 32-bits') ;
1468
1469
if ( len < 192 ) {
1469
1470
for ( var i = 0 ; i < len ; i += 4 ) {
1470
1471
swap ( this , i , i + 3 ) ;
@@ -1482,7 +1483,7 @@ Buffer.prototype.swap64 = function swap64() {
1482
1483
// dropping down to the native code is faster.
1483
1484
const len = this . length ;
1484
1485
if ( len % 8 !== 0 )
1485
- throw new RangeError ( 'Buffer size must be a multiple of 64-bits' ) ;
1486
+ throw new errors . RangeError ( 'ERR_INVALID_BUFFER_SIZE' , ' 64-bits') ;
1486
1487
if ( len < 192 ) {
1487
1488
for ( var i = 0 ; i < len ; i += 8 ) {
1488
1489
swap ( this , i , i + 7 ) ;
0 commit comments