Skip to content
Merged
Changes from 3 commits
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
46 changes: 36 additions & 10 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,31 +608,57 @@ Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
Buffer.concat = function concat(list, length) {
validateArray(list, 'list');

if (list.length === 0)
const listLen = list.length;
if (listLen === 0)
return new FastBuffer();

if (length === undefined) {
length = 0;
for (let i = 0; i < list.length; i++) {
if (list[i].length) {
length += list[i].length;
for (let i = 0; i < listLen; i++) {
const buf = list[i];
if (!isUint8Array(buf)) {
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
// Instead, find the proper error code for this.
throw new ERR_INVALID_ARG_TYPE(
`list[${i}]`, ['Buffer', 'Uint8Array'], buf);
}
length += buf.length;
}
} else {
validateOffset(length, 'length');

const buffer = allocate(length);
let pos = 0;
for (let i = 0; i < listLen; i++) {
const buf = list[i];
TypedArrayPrototypeSet(buffer, buf, pos);
pos += buf.length;
}

if (pos < length) {
TypedArrayPrototypeFill(buffer, 0, pos, length);
}
return buffer;
}

const buffer = Buffer.allocUnsafe(length);
validateOffset(length, 'length');
const buffer = allocate(length);
let pos = 0;
for (let i = 0; i < list.length; i++) {
for (let i = 0; i < listLen; i++) {
const buf = list[i];
if (!isUint8Array(buf)) {
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
// Instead, find the proper error code for this.
throw new ERR_INVALID_ARG_TYPE(
`list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
`list[${i}]`, ['Buffer', 'Uint8Array'], buf);
}
if (pos + buf.length > length) {
TypedArrayPrototypeSet(buffer,
TypedArrayPrototypeSlice(buf, 0, length - pos),
pos);
pos = length;
break;
}
pos += _copyActual(buf, buffer, pos, 0, buf.length, true);
TypedArrayPrototypeSet(buffer, buf, pos);
pos += buf.length;
}

// Note: `length` is always equal to `buffer.length` at this point
Expand Down
Loading