Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: allow Uint8Array input to methods #10236

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[squash] update typecheck error messages
  • Loading branch information
addaleax committed Dec 13, 2016
commit d2e93cb406831cbe5c73936f5216c1f2f4006222
18 changes: 11 additions & 7 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ Buffer.isBuffer = function isBuffer(b) {

Buffer.compare = function compare(a, b) {
if (!isUint8Array(a) || !isUint8Array(b)) {
throw new TypeError('Arguments must be Buffers');
throw new TypeError('Arguments must be Buffers or Uint8Arrays');
}

if (a === b) {
Expand All @@ -302,10 +302,13 @@ Buffer.isEncoding = function(encoding) {
};
Buffer[internalUtil.kIsEncodingSymbol] = Buffer.isEncoding;

const kConcatErrMsg = '"list" argument must be an Array ' +
'of Buffer or Uint8Array instances';

Buffer.concat = function(list, length) {
var i;
if (!Array.isArray(list))
throw new TypeError('"list" argument must be an Array of Buffers');
throw new TypeError(kConcatErrMsg);

if (list.length === 0)
return new FastBuffer();
Expand All @@ -323,7 +326,7 @@ Buffer.concat = function(list, length) {
for (i = 0; i < list.length; i++) {
var buf = list[i];
if (!isUint8Array(buf))
throw new TypeError('"list" argument must be an Array of Buffers');
throw new TypeError(kConcatErrMsg);
binding.copy(buf, buffer, pos);
pos += buf.length;
}
Expand Down Expand Up @@ -510,7 +513,7 @@ Buffer.prototype.toString = function() {

Buffer.prototype.equals = function equals(b) {
if (!isUint8Array(b))
throw new TypeError('Argument must be a Buffer');
throw new TypeError('Argument must be a Buffer or Uint8Array');

if (this === b)
return true;
Expand Down Expand Up @@ -539,7 +542,7 @@ Buffer.prototype.compare = function compare(target,
thisEnd) {

if (!isUint8Array(target))
throw new TypeError('Argument must be a Buffer');
throw new TypeError('Argument must be a Buffer or Uint8Array');

if (start === undefined)
start = 0;
Expand Down Expand Up @@ -609,7 +612,8 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, 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');
}


Expand Down Expand Up @@ -1037,7 +1041,7 @@ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {

function checkInt(buffer, value, offset, ext, max, min) {
if (!isUint8Array(buffer))
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)
throw new TypeError('"value" argument is out of bounds');
if (offset + ext > buffer.length)
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { isUint8Array } = process.binding('util');
// Buffer instance.
exports.transcode = function transcode(source, fromEncoding, toEncoding) {
if (!isUint8Array(source))
throw new TypeError('"source" argument must be a Buffer');
throw new TypeError('"source" argument must be a Buffer or Uint8Array');
if (source.length === 0) return Buffer.alloc(0);

fromEncoding = normalizeEncoding(fromEncoding) || fromEncoding;
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-buffer-concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ function assertWrongList(value) {
Buffer.concat(value);
}, function(err) {
return err instanceof TypeError &&
err.message === '"list" argument must be an Array of Buffers';
err.message === '"list" argument must be an Array of Buffer ' +
'or Uint8Array instances';
});
}

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-icu-transcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ for (const test in tests) {

assert.throws(
() => buffer.transcode(null, 'utf8', 'ascii'),
/^TypeError: "source" argument must be a Buffer$/
/^TypeError: "source" argument must be a Buffer or Uint8Array$/
);

assert.throws(
Expand Down