mirror of
https://github.com/nodejs/node.git
synced 2025-05-10 15:29:15 +00:00

Fix Buffer.concat() so a copy is always returned regardless of the number of buffers that were passed. Previously if the array length was one then the same same buffer was returned. This created a special case for the user where there was a chance mutating the buffer returned by .concat() could mutate the buffer passed in. Also fixes an inconsistency when throwing if an array member was not a Buffer instance. For example: Buffer.concat([42]); // Returns 42 Buffer.concat([42, 1]); // Throws a TypeError Now .concat() will always throw if an array member is not a Buffer instance. See: https://github.com/nodejs/io.js/issues/1891 PR-URL: https://github.com/nodejs/io.js/pull/1937 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
28 lines
814 B
JavaScript
28 lines
814 B
JavaScript
'use strict';
|
|
var common = require('../common');
|
|
var assert = require('assert');
|
|
|
|
var zero = [];
|
|
var one = [ new Buffer('asdf') ];
|
|
var long = [];
|
|
for (var i = 0; i < 10; i++) long.push(new Buffer('asdf'));
|
|
|
|
var flatZero = Buffer.concat(zero);
|
|
var flatOne = Buffer.concat(one);
|
|
var flatLong = Buffer.concat(long);
|
|
var flatLongLen = Buffer.concat(long, 40);
|
|
|
|
assert(flatZero.length === 0);
|
|
assert(flatOne.toString() === 'asdf');
|
|
// A special case where concat used to return the first item,
|
|
// if the length is one. This check is to make sure that we don't do that.
|
|
assert(flatOne !== one[0]);
|
|
assert(flatLong.toString() === (new Array(10 + 1).join('asdf')));
|
|
assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf')));
|
|
|
|
assert.throws(function() {
|
|
Buffer.concat([42]);
|
|
}, TypeError);
|
|
|
|
console.log('ok');
|