buffer: allow Uint8Array input to methods · nodejs/node@beca324
@@ -2,7 +2,8 @@
22'use strict';
3344const 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}
252253253254function fromObject(obj) {
254-if (obj instanceof Buffer) {
255+if (isUint8Array(obj)) {
255256const b = allocate(obj.length);
256257257258if (b.length === 0)
258259return b;
259260260-obj.copy(b, 0, 0, obj.length);
261+binding.copy(obj, b, 0, 0, obj.length);
261262return b;
262263}
263264@@ -287,9 +288,8 @@ Buffer.isBuffer = function isBuffer(b) {
287288288289289290Buffer.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}
294294295295if (a === b) {
@@ -306,10 +306,13 @@ Buffer.isEncoding = function(encoding) {
306306};
307307Buffer[internalUtil.kIsEncodingSymbol] = Buffer.isEncoding;
308308309+const kConcatErrMsg = '"list" argument must be an Array ' +
310+'of Buffer or Uint8Array instances';
311+309312Buffer.concat = function(list, length) {
310313var i;
311314if (!Array.isArray(list))
312-throw new TypeError('"list" argument must be an Array of Buffers');
315+throw new TypeError(kConcatErrMsg);
313316314317if (list.length === 0)
315318return new FastBuffer();
@@ -326,9 +329,9 @@ Buffer.concat = function(list, length) {
326329var pos = 0;
327330for (i = 0; i < list.length; i++) {
328331var 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);
332335pos += 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+};
498504499505Buffer.prototype.toString = function() {
500506let result;
@@ -510,8 +516,8 @@ Buffer.prototype.toString = function() {
510516511517512518Buffer.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');
515521516522if (this === b)
517523return true;
@@ -539,8 +545,8 @@ Buffer.prototype.compare = function compare(target,
539545thisStart,
540546thisEnd) {
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');
544550545551if (start === undefined)
546552start = 0;
@@ -604,13 +610,14 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
604610return binding.indexOfString(buffer, val, byteOffset, encoding, dir);
605611}
606612return slowIndexOf(buffer, val, byteOffset, encoding, dir);
607-} else if (val instanceof Buffer) {
613+} else if (isUint8Array(val)) {
608614return binding.indexOfBuffer(buffer, val, byteOffset, encoding, dir);
609615} else if (typeof val === 'number') {
610616return 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) {
103710441038104510391046function 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');
10421049if (value > max || value < min)
10431050throw new TypeError('"value" argument is out of bounds');
10441051if (offset + ext > buffer.length)