From: Feross Aboukhadijeh Date: Fri, 16 Feb 2018 07:09:57 +0000 (-0800) Subject: Buffer.concat() accepts Uint8Array instances X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=42201a7dbf66c2ac45270c85285d1b00a06fd58a;p=buffer.git Buffer.concat() accepts Uint8Array instances Fix https://github.com/feross/buffer/issues/173 --- diff --git a/index.js b/index.js index a5b9ef5..19a9978 100644 --- a/index.js +++ b/index.js @@ -369,6 +369,9 @@ Buffer.concat = function concat (list, length) { var pos = 0 for (i = 0; i < list.length; ++i) { var buf = list[i] + if (isArrayBufferView(buf)) { + buf = Buffer.from(buf) + } if (!Buffer.isBuffer(buf)) { throw new TypeError('"list" argument must be an Array of Buffers') } diff --git a/test/methods.js b/test/methods.js index 3696437..a63e4c4 100644 --- a/test/methods.js +++ b/test/methods.js @@ -60,6 +60,13 @@ test('concat() a varying number of buffers', function (t) { t.end() }) +test('concat() works on Uint8Array instances', function (t) { + var result = B.concat([new Uint8Array([1, 2]), new Uint8Array([3, 4])]) + var expected = Buffer.from([1, 2, 3, 4]) + t.deepEqual(result, expected) + t.end() +}) + test('fill', function (t) { var b = new B(10) b.fill(2)