◐ Shell
clean mode source ↗

buffer: allow Uint8Array input to methods · nodejs/node@beca324

@@ -2,7 +2,8 @@

22

'use strict';

3344

const binding = process.binding('buffer');

5-

const { isArrayBuffer, isSharedArrayBuffer } = process.binding('util');

5+

const { isArrayBuffer, isSharedArrayBuffer, isUint8Array } =

6+

process.binding('util');

67

const bindingObj = {};

78

const internalUtil = require('internal/util');

89

@@ -251,13 +252,13 @@ function fromArrayBuffer(obj, byteOffset, length) {

251252

}

252253253254

function fromObject(obj) {

254-

if (obj instanceof Buffer) {

255+

if (isUint8Array(obj)) {

255256

const b = allocate(obj.length);

256257257258

if (b.length === 0)

258259

return b;

259260260-

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) {

287288288289289290

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');

293293

}

294294295295

if (a === b) {

@@ -306,10 +306,13 @@ Buffer.isEncoding = function(encoding) {

306306

};

307307

Buffer[internalUtil.kIsEncodingSymbol] = Buffer.isEncoding;

308308309+

const kConcatErrMsg = '"list" argument must be an Array ' +

310+

'of Buffer or Uint8Array instances';

311+309312

Buffer.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);

313316314317

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

}

497500501+

Buffer.prototype.copy = function(target, targetStart, sourceStart, sourceEnd) {

502+

return binding.copy(this, target, targetStart, sourceStart, sourceEnd);

503+

};

498504499505

Buffer.prototype.toString = function() {

500506

let result;

@@ -510,8 +516,8 @@ Buffer.prototype.toString = function() {

510516511517512518

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');

515521516522

if (this === b)

517523

return true;

@@ -539,8 +545,8 @@ Buffer.prototype.compare = function compare(target,

539545

thisStart,

540546

thisEnd) {

541547542-

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');

544550545551

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

}

612618613-

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

}

615622616623

@@ -1037,8 +1044,8 @@ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {

103710441038104510391046

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');

10421049

if (value > max || value < min)

10431050

throw new TypeError('"value" argument is out of bounds');

10441051

if (offset + ext > buffer.length)