2
2
'use strict' ;
3
3
4
4
const binding = process . binding ( 'buffer' ) ;
5
- const { isArrayBuffer, isSharedArrayBuffer } = process . binding ( 'util' ) ;
5
+ const { isArrayBuffer, isSharedArrayBuffer, isUint8Array } =
6
+ process . binding ( 'util' ) ;
6
7
const bindingObj = { } ;
7
8
const internalUtil = require ( 'internal/util' ) ;
8
9
@@ -251,13 +252,13 @@ function fromArrayBuffer(obj, byteOffset, length) {
251
252
}
252
253
253
254
function fromObject ( obj ) {
254
- if ( obj instanceof Buffer ) {
255
+ if ( isUint8Array ( obj ) ) {
255
256
const b = allocate ( obj . length ) ;
256
257
257
258
if ( b . length === 0 )
258
259
return b ;
259
260
260
- obj . copy ( b , 0 , 0 , obj . length ) ;
261
+ binding . copy ( obj , b , 0 , 0 , obj . length ) ;
261
262
return b ;
262
263
}
263
264
@@ -287,9 +288,8 @@ Buffer.isBuffer = function isBuffer(b) {
287
288
288
289
289
290
Buffer . compare = function compare ( a , b ) {
290
- if ( ! ( a instanceof Buffer ) ||
291
- ! ( b instanceof Buffer ) ) {
292
- throw new TypeError ( 'Arguments must be Buffers' ) ;
291
+ if ( ! isUint8Array ( a ) || ! isUint8Array ( b ) ) {
292
+ throw new TypeError ( 'Arguments must be Buffers or Uint8Arrays' ) ;
293
293
}
294
294
295
295
if ( a === b ) {
@@ -306,10 +306,13 @@ Buffer.isEncoding = function(encoding) {
306
306
} ;
307
307
Buffer [ internalUtil . kIsEncodingSymbol ] = Buffer . isEncoding ;
308
308
309
+ const kConcatErrMsg = '"list" argument must be an Array ' +
310
+ 'of Buffer or Uint8Array instances' ;
311
+
309
312
Buffer . concat = function ( list , length ) {
310
313
var i ;
311
314
if ( ! Array . isArray ( list ) )
312
- throw new TypeError ( '"list" argument must be an Array of Buffers' ) ;
315
+ throw new TypeError ( kConcatErrMsg ) ;
313
316
314
317
if ( list . length === 0 )
315
318
return new FastBuffer ( ) ;
@@ -326,9 +329,9 @@ Buffer.concat = function(list, length) {
326
329
var pos = 0 ;
327
330
for ( i = 0 ; i < list . length ; i ++ ) {
328
331
var buf = list [ i ] ;
329
- if ( ! Buffer . isBuffer ( buf ) )
330
- throw new TypeError ( '"list" argument must be an Array of Buffers' ) ;
331
- buf . copy ( buffer , pos ) ;
332
+ if ( ! isUint8Array ( buf ) )
333
+ throw new TypeError ( kConcatErrMsg ) ;
334
+ binding . copy ( buf , buffer , pos ) ;
332
335
pos += buf . length ;
333
336
}
334
337
@@ -495,6 +498,9 @@ function slowToString(encoding, start, end) {
495
498
}
496
499
}
497
500
501
+ Buffer . prototype . copy = function ( target , targetStart , sourceStart , sourceEnd ) {
502
+ return binding . copy ( this , target , targetStart , sourceStart , sourceEnd ) ;
503
+ } ;
498
504
499
505
Buffer . prototype . toString = function ( ) {
500
506
let result ;
@@ -510,8 +516,8 @@ Buffer.prototype.toString = function() {
510
516
511
517
512
518
Buffer . prototype . equals = function equals ( b ) {
513
- if ( ! ( b instanceof Buffer ) )
514
- throw new TypeError ( 'Argument must be a Buffer' ) ;
519
+ if ( ! isUint8Array ( b ) )
520
+ throw new TypeError ( 'Argument must be a Buffer or Uint8Array ' ) ;
515
521
516
522
if ( this === b )
517
523
return true ;
@@ -539,8 +545,8 @@ Buffer.prototype.compare = function compare(target,
539
545
thisStart ,
540
546
thisEnd ) {
541
547
542
- if ( ! ( target instanceof Buffer ) )
543
- throw new TypeError ( 'Argument must be a Buffer' ) ;
548
+ if ( ! isUint8Array ( target ) )
549
+ throw new TypeError ( 'Argument must be a Buffer or Uint8Array ' ) ;
544
550
545
551
if ( start === undefined )
546
552
start = 0 ;
@@ -604,13 +610,14 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
604
610
return binding . indexOfString ( buffer , val , byteOffset , encoding , dir ) ;
605
611
}
606
612
return slowIndexOf ( buffer , val , byteOffset , encoding , dir ) ;
607
- } else if ( val instanceof Buffer ) {
613
+ } else if ( isUint8Array ( val ) ) {
608
614
return binding . indexOfBuffer ( buffer , val , byteOffset , encoding , dir ) ;
609
615
} else if ( typeof val === 'number' ) {
610
616
return binding . indexOfNumber ( buffer , val , byteOffset , dir ) ;
611
617
}
612
618
613
- throw new TypeError ( '"val" argument must be string, number or Buffer' ) ;
619
+ throw new TypeError ( '"val" argument must be string, number, Buffer ' +
620
+ 'or Uint8Array' ) ;
614
621
}
615
622
616
623
@@ -1037,8 +1044,8 @@ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
1037
1044
1038
1045
1039
1046
function checkInt ( buffer , value , offset , ext , max , min ) {
1040
- if ( ! ( buffer instanceof Buffer ) )
1041
- throw new TypeError ( '"buffer" argument must be a Buffer instance ' ) ;
1047
+ if ( ! isUint8Array ( buffer ) )
1048
+ throw new TypeError ( '"buffer" argument must be a Buffer or Uint8Array ' ) ;
1042
1049
if ( value > max || value < min )
1043
1050
throw new TypeError ( '"value" argument is out of bounds' ) ;
1044
1051
if ( offset + ext > buffer . length )
0 commit comments