22'use strict' ;
33
44const binding = process . binding ( 'buffer' ) ;
5- const { isArrayBuffer, isSharedArrayBuffer } = process . binding ( 'util' ) ;
5+ const { isArrayBuffer, isSharedArrayBuffer, isUint8Array } =
6+ process . binding ( 'util' ) ;
67const bindingObj = { } ;
78const internalUtil = require ( 'internal/util' ) ;
89
@@ -251,13 +252,13 @@ function fromArrayBuffer(obj, byteOffset, length) {
251252}
252253
253254function fromObject ( obj ) {
254- if ( obj instanceof Buffer ) {
255+ if ( isUint8Array ( obj ) ) {
255256 const b = allocate ( obj . length ) ;
256257
257258 if ( b . length === 0 )
258259 return b ;
259260
260- obj . copy ( b , 0 , 0 , obj . length ) ;
261+ binding . copy ( obj , b , 0 , 0 , obj . length ) ;
261262 return b ;
262263 }
263264
@@ -287,9 +288,8 @@ Buffer.isBuffer = function isBuffer(b) {
287288
288289
289290Buffer . 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' ) ;
293293 }
294294
295295 if ( a === b ) {
@@ -306,10 +306,13 @@ Buffer.isEncoding = function(encoding) {
306306} ;
307307Buffer [ internalUtil . kIsEncodingSymbol ] = Buffer . isEncoding ;
308308
309+ const kConcatErrMsg = '"list" argument must be an Array ' +
310+ 'of Buffer or Uint8Array instances' ;
311+
309312Buffer . concat = function ( list , length ) {
310313 var i ;
311314 if ( ! Array . isArray ( list ) )
312- throw new TypeError ( '"list" argument must be an Array of Buffers' ) ;
315+ throw new TypeError ( kConcatErrMsg ) ;
313316
314317 if ( list . length === 0 )
315318 return new FastBuffer ( ) ;
@@ -326,9 +329,9 @@ Buffer.concat = function(list, length) {
326329 var pos = 0 ;
327330 for ( i = 0 ; i < list . length ; i ++ ) {
328331 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 ) ;
332335 pos += buf . length ;
333336 }
334337
@@ -495,6 +498,9 @@ function slowToString(encoding, start, end) {
495498 }
496499}
497500
501+ Buffer . prototype . copy = function ( target , targetStart , sourceStart , sourceEnd ) {
502+ return binding . copy ( this , target , targetStart , sourceStart , sourceEnd ) ;
503+ } ;
498504
499505Buffer . prototype . toString = function ( ) {
500506 let result ;
@@ -510,8 +516,8 @@ Buffer.prototype.toString = function() {
510516
511517
512518Buffer . 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 ' ) ;
515521
516522 if ( this === b )
517523 return true ;
@@ -539,8 +545,8 @@ Buffer.prototype.compare = function compare(target,
539545 thisStart ,
540546 thisEnd ) {
541547
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 ' ) ;
544550
545551 if ( start === undefined )
546552 start = 0 ;
@@ -604,13 +610,14 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
604610 return binding . indexOfString ( buffer , val , byteOffset , encoding , dir ) ;
605611 }
606612 return slowIndexOf ( buffer , val , byteOffset , encoding , dir ) ;
607- } else if ( val instanceof Buffer ) {
613+ } else if ( isUint8Array ( val ) ) {
608614 return binding . indexOfBuffer ( buffer , val , byteOffset , encoding , dir ) ;
609615 } else if ( typeof val === 'number' ) {
610616 return binding . indexOfNumber ( buffer , val , byteOffset , dir ) ;
611617 }
612618
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' ) ;
614621}
615622
616623
@@ -1037,8 +1044,8 @@ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
10371044
10381045
10391046function 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 ' ) ;
10421049 if ( value > max || value < min )
10431050 throw new TypeError ( '"value" argument is out of bounds' ) ;
10441051 if ( offset + ext > buffer . length )
0 commit comments