@@ -67,9 +67,6 @@ Object.defineProperty(exports, 'constants', {
6767
6868exports . kStringMaxLength = binding . kStringMaxLength ;
6969
70- const kFromErrorMsg = 'First argument must be a string, Buffer, ' +
71- 'ArrayBuffer, Array, or array-like object.' ;
72-
7370Buffer . poolSize = 8 * 1024 ;
7471var poolSize , poolOffset , allocPool ;
7572
@@ -146,9 +143,8 @@ function Buffer(arg, encodingOrOffset, length) {
146143 // Common case.
147144 if ( typeof arg === 'number' ) {
148145 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 ) ;
152148 }
153149 return Buffer . alloc ( arg ) ;
154150 }
@@ -177,10 +173,12 @@ Buffer.from = function(value, encodingOrOffset, length) {
177173 return fromArrayBuffer ( value , encodingOrOffset , length ) ;
178174
179175 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 ) ;
181178
182179 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 ) ;
184182
185183 const valueOf = value . valueOf && value . valueOf ( ) ;
186184 if ( valueOf != null && valueOf !== value )
@@ -196,7 +194,8 @@ Buffer.from = function(value, encodingOrOffset, length) {
196194 length ) ;
197195 }
198196
199- throw new TypeError ( kFromErrorMsg ) ;
197+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'first argument' ,
198+ [ 'string' , 'buffer' , 'arrayBuffer' , 'array' , 'array-like object' ] ) ;
200199} ;
201200
202201Object . setPrototypeOf ( Buffer , Uint8Array ) ;
@@ -208,12 +207,11 @@ function assertSize(size) {
208207 let err = null ;
209208
210209 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 ) ;
212211 } 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 ) ;
214213 } 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 ) ;
217215 }
218216
219217 if ( err ) {
@@ -300,7 +298,7 @@ function fromString(string, encoding) {
300298 } else {
301299 length = byteLength ( string , encoding , true ) ;
302300 if ( length === - 1 )
303- throw new TypeError ( '"encoding" must be a valid string encoding' ) ;
301+ throw new errors . TypeError ( 'ERR_UNKNOWN_ENCODING' , encoding ) ;
304302 if ( string . length === 0 )
305303 return new FastBuffer ( ) ;
306304 }
@@ -343,7 +341,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
343341 const maxLength = obj . byteLength - byteOffset ;
344342
345343 if ( maxLength < 0 )
346- throw new RangeError ( "'offset' is out of bounds" ) ;
344+ throw new errors . RangeError ( 'ERR_BUFFER_OUT_OF_BOUNDS' , 'offset' ) ;
347345
348346 if ( length === undefined ) {
349347 length = maxLength ;
@@ -355,7 +353,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
355353 length = 0 ;
356354 } else if ( length > 0 ) {
357355 if ( length > maxLength )
358- throw new RangeError ( "'length' is out of bounds" ) ;
356+ throw new errors . RangeError ( 'ERR_BUFFER_OUT_OF_BOUNDS' , 'length' ) ;
359357 } else {
360358 length = 0 ;
361359 }
@@ -399,7 +397,8 @@ Buffer.isBuffer = function isBuffer(b) {
399397
400398Buffer . compare = function compare ( a , b ) {
401399 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' ] ) ;
403402 }
404403
405404 if ( a === b ) {
@@ -416,13 +415,13 @@ Buffer.isEncoding = function(encoding) {
416415} ;
417416Buffer [ internalUtil . kIsEncodingSymbol ] = Buffer . isEncoding ;
418417
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' ] ) ;
421420
422421Buffer . concat = function ( list , length ) {
423422 var i ;
424423 if ( ! Array . isArray ( list ) )
425- throw new TypeError ( kConcatErrMsg ) ;
424+ throw kConcatErr ;
426425
427426 if ( list . length === 0 )
428427 return new FastBuffer ( ) ;
@@ -440,7 +439,7 @@ Buffer.concat = function(list, length) {
440439 for ( i = 0 ; i < list . length ; i ++ ) {
441440 var buf = list [ i ] ;
442441 if ( ! isUint8Array ( buf ) )
443- throw new TypeError ( kConcatErrMsg ) ;
442+ throw kConcatErr ;
444443 binding . copy ( buf , buffer , pos ) ;
445444 pos += buf . length ;
446445 }
@@ -475,7 +474,8 @@ function byteLength(string, encoding) {
475474 return string . byteLength ;
476475 }
477476
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' ] ) ;
479479 }
480480
481481 const len = string . length ;
@@ -591,7 +591,7 @@ function stringSlice(buf, encoding, start, end) {
591591 return buf . ucs2Slice ( start , end ) ;
592592 break ;
593593 }
594- throw new TypeError ( 'Unknown encoding: ' + encoding ) ;
594+ throw new errors . TypeError ( 'ERR_UNKNOWN_ENCODING' , encoding ) ;
595595}
596596
597597
@@ -633,8 +633,8 @@ Buffer.prototype.toString = function(encoding, start, end) {
633633
634634Buffer . prototype . equals = function equals ( b ) {
635635 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' ] ) ;
638638 if ( this === b )
639639 return true ;
640640
@@ -659,7 +659,8 @@ Buffer.prototype.compare = function compare(target,
659659 thisStart ,
660660 thisEnd ) {
661661 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' ] ) ;
663664 if ( arguments . length === 1 )
664665 return compare_ ( this , target ) ;
665666
@@ -738,8 +739,8 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
738739 return binding . indexOfNumber ( buffer , val , byteOffset , dir ) ;
739740 }
740741
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' ] ) ;
743744}
744745
745746
@@ -765,7 +766,7 @@ function slowIndexOf(buffer, val, byteOffset, encoding, dir) {
765766
766767 default :
767768 if ( loweredCase ) {
768- throw new TypeError ( 'Unknown encoding: ' + encoding ) ;
769+ throw new errors . TypeError ( 'ERR_UNKNOWN_ENCODING' , encoding ) ;
769770 }
770771
771772 encoding = ( '' + encoding ) . toLowerCase ( ) ;
@@ -807,11 +808,11 @@ Buffer.prototype.fill = function fill(val, start, end, encoding) {
807808 }
808809
809810 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') ;
811812 }
812813 var normalizedEncoding = internalUtil . normalizeEncoding ( encoding ) ;
813814 if ( normalizedEncoding === undefined ) {
814- throw new TypeError ( 'Unknown encoding: ' + encoding ) ;
815+ throw new errors . TypeError ( 'ERR_UNKNOWN_ENCODING' , encoding ) ;
815816 }
816817
817818 if ( val . length === 0 ) {
@@ -872,13 +873,13 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
872873 length = remaining ;
873874
874875 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 ) ;
876877 } else {
877878 // if someone is still calling the obsolete form of write(), tell them.
878879 // we don't want eg buf.write("foo", "utf8", 10) to silently turn into
879880 // 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]) ') ;
882883 }
883884
884885 if ( ! encoding ) return this . utf8Write ( string , offset , length ) ;
@@ -925,7 +926,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
925926 return this . hexWrite ( string , offset , length ) ;
926927 break ;
927928 }
928- throw new TypeError ( 'Unknown encoding: ' + encoding ) ;
929+ throw new errors . TypeError ( 'ERR_UNKNOWN_ENCODING' , encoding ) ;
929930} ;
930931
931932
@@ -1176,7 +1177,7 @@ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
11761177
11771178function checkInt ( buffer , value , offset , ext , max , min ) {
11781179 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 ) ;
11801181 if ( offset + ext > buffer . length )
11811182 throw new errors . RangeError ( 'ERR_INDEX_OUT_OF_RANGE' ) ;
11821183}
@@ -1448,7 +1449,7 @@ Buffer.prototype.swap16 = function swap16() {
14481449 // dropping down to the native code is faster.
14491450 const len = this . length ;
14501451 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') ;
14521453 if ( len < 128 ) {
14531454 for ( var i = 0 ; i < len ; i += 2 )
14541455 swap ( this , i , i + 1 ) ;
@@ -1464,7 +1465,7 @@ Buffer.prototype.swap32 = function swap32() {
14641465 // dropping down to the native code is faster.
14651466 const len = this . length ;
14661467 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') ;
14681469 if ( len < 192 ) {
14691470 for ( var i = 0 ; i < len ; i += 4 ) {
14701471 swap ( this , i , i + 3 ) ;
@@ -1482,7 +1483,7 @@ Buffer.prototype.swap64 = function swap64() {
14821483 // dropping down to the native code is faster.
14831484 const len = this . length ;
14841485 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') ;
14861487 if ( len < 192 ) {
14871488 for ( var i = 0 ; i < len ; i += 8 ) {
14881489 swap ( this , i , i + 7 ) ;
0 commit comments