@ -2,7 +2,8 @@
'use strict' ;
'use strict' ;
const binding = process . binding ( 'buffer' ) ;
const binding = process . binding ( 'buffer' ) ;
const { isArrayBuffer , isSharedArrayBuffer } = process . binding ( 'util' ) ;
const { isArrayBuffer , isSharedArrayBuffer , isUint8Array } =
process . binding ( 'util' ) ;
const bindingObj = { } ;
const bindingObj = { } ;
const internalUtil = require ( 'internal/util' ) ;
const internalUtil = require ( 'internal/util' ) ;
@ -251,13 +252,13 @@ function fromArrayBuffer(obj, byteOffset, length) {
}
}
function fromObject ( obj ) {
function fromObject ( obj ) {
if ( obj instanceof Buffer ) {
if ( isUint8Array ( obj ) ) {
const b = allocate ( obj . length ) ;
const b = allocate ( obj . length ) ;
if ( b . length === 0 )
if ( b . length === 0 )
return b ;
return b ;
obj . copy ( b , 0 , 0 , obj . length ) ;
binding . copy ( obj , b , 0 , 0 , obj . length ) ;
return b ;
return b ;
}
}
@ -287,9 +288,8 @@ Buffer.isBuffer = function isBuffer(b) {
Buffer . compare = function compare ( a , b ) {
Buffer . compare = function compare ( a , b ) {
if ( ! ( a instanceof Buffer ) ||
if ( ! isUint8Array ( a ) || ! isUint8Array ( b ) ) {
! ( b instanceof Buffer ) ) {
throw new TypeError ( 'Arguments must be Buffers or Uint8Arrays' ) ;
throw new TypeError ( 'Arguments must be Buffers' ) ;
}
}
if ( a === b ) {
if ( a === b ) {
@ -306,10 +306,13 @@ Buffer.isEncoding = function(encoding) {
} ;
} ;
Buffer [ internalUtil . kIsEncodingSymbol ] = Buffer . isEncoding ;
Buffer [ internalUtil . kIsEncodingSymbol ] = Buffer . isEncoding ;
const kConcatErrMsg = '"list" argument must be an Array ' +
'of Buffer or Uint8Array instances' ;
Buffer . concat = function ( list , length ) {
Buffer . concat = function ( list , length ) {
var i ;
var i ;
if ( ! Array . isArray ( list ) )
if ( ! Array . isArray ( list ) )
throw new TypeError ( '"list" argument must be an Array of Buffers' ) ;
throw new TypeError ( kConcatErrMsg ) ;
if ( list . length === 0 )
if ( list . length === 0 )
return new FastBuffer ( ) ;
return new FastBuffer ( ) ;
@ -326,9 +329,9 @@ Buffer.concat = function(list, length) {
var pos = 0 ;
var pos = 0 ;
for ( i = 0 ; i < list . length ; i ++ ) {
for ( i = 0 ; i < list . length ; i ++ ) {
var buf = list [ i ] ;
var buf = list [ i ] ;
if ( ! Buffer . isBuffer ( buf ) )
if ( ! isUint8Array ( buf ) )
throw new TypeError ( '"list" argument must be an Array of Buffers' ) ;
throw new TypeError ( kConcatErrMsg ) ;
buf . copy ( buffer , pos ) ;
binding . copy ( buf , buffer , pos ) ;
pos += buf . length ;
pos += buf . length ;
}
}
@ -495,6 +498,9 @@ function slowToString(encoding, start, end) {
}
}
}
}
Buffer . prototype . copy = function ( target , targetStart , sourceStart , sourceEnd ) {
return binding . copy ( this , target , targetStart , sourceStart , sourceEnd ) ;
} ;
Buffer . prototype . toString = function ( ) {
Buffer . prototype . toString = function ( ) {
let result ;
let result ;
@ -510,8 +516,8 @@ Buffer.prototype.toString = function() {
Buffer . prototype . equals = function equals ( b ) {
Buffer . prototype . equals = function equals ( b ) {
if ( ! ( b instanceof Buffer ) )
if ( ! isUint8Array ( b ) )
throw new TypeError ( 'Argument must be a Buffer' ) ;
throw new TypeError ( 'Argument must be a Buffer or Uint8Array ' ) ;
if ( this === b )
if ( this === b )
return true ;
return true ;
@ -539,8 +545,8 @@ Buffer.prototype.compare = function compare(target,
thisStart ,
thisStart ,
thisEnd ) {
thisEnd ) {
if ( ! ( target instanceof Buffer ) )
if ( ! isUint8Array ( target ) )
throw new TypeError ( 'Argument must be a Buffer' ) ;
throw new TypeError ( 'Argument must be a Buffer or Uint8Array ' ) ;
if ( start === undefined )
if ( start === undefined )
start = 0 ;
start = 0 ;
@ -604,13 +610,14 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
return binding . indexOfString ( buffer , val , byteOffset , encoding , dir ) ;
return binding . indexOfString ( buffer , val , byteOffset , encoding , dir ) ;
}
}
return slowIndexOf ( buffer , val , byteOffset , encoding , dir ) ;
return slowIndexOf ( buffer , val , byteOffset , encoding , dir ) ;
} else if ( val instanceof Buffer ) {
} else if ( isUint8Array ( val ) ) {
return binding . indexOfBuffer ( buffer , val , byteOffset , encoding , dir ) ;
return binding . indexOfBuffer ( buffer , val , byteOffset , encoding , dir ) ;
} else if ( typeof val === 'number' ) {
} else if ( typeof val === 'number' ) {
return binding . indexOfNumber ( buffer , val , byteOffset , dir ) ;
return binding . indexOfNumber ( buffer , val , byteOffset , dir ) ;
}
}
throw new TypeError ( '"val" argument must be string, number or Buffer' ) ;
throw new TypeError ( '"val" argument must be string, number, Buffer ' +
'or Uint8Array' ) ;
}
}
@ -1037,8 +1044,8 @@ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
function checkInt ( buffer , value , offset , ext , max , min ) {
function checkInt ( buffer , value , offset , ext , max , min ) {
if ( ! ( buffer instanceof B uffer) )
if ( ! isUint8Array ( b uffer) )
throw new TypeError ( '"buffer" argument must be a Buffer instance ' ) ;
throw new TypeError ( '"buffer" argument must be a Buffer or Uint8Array ' ) ;
if ( value > max || value < min )
if ( value > max || value < min )
throw new TypeError ( '"value" argument is out of bounds' ) ;
throw new TypeError ( '"value" argument is out of bounds' ) ;
if ( offset + ext > buffer . length )
if ( offset + ext > buffer . length )