Repository: nodejs/node
File: lib/buffer.js (JavaScript)

Code before:
  return _compare(buf1, buf2);
};

Buffer.isEncoding = function isEncoding(encoding) {
  return typeof encoding === 'string' && encoding.length !== 0 &&
         normalizeEncoding(encoding) !== undefined;
};
Buffer[kIsEncodingSymbol] = Buffer.isEncoding;

Buffer.concat = function concat(list, length) {
  validateArray(list, 'list');

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

  if (length === undefined) {
    length = 0;
    for (let i = 0; i < list.length; 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;
    }

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

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

  validateOffset(length, 'length');
  const buffer = allocate(length);
  let pos = 0;
  for (let i = 0; i < list.length; 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(

Review comment (suggestion):
I thought of moving the type check here in #60399, but it was a breaking change somehow in the way this function behaved, though I forgot how, will take another look

But I do prefer moving it here before allocating

Code after:

  return _compare(buf1, buf2);
};

Buffer.isEncoding = function isEncoding(encoding) {
  return typeof encoding === 'string' && encoding.length !== 0 &&
         normalizeEncoding(encoding) !== undefined;
};
Buffer[kIsEncodingSymbol] = Buffer.isEncoding;

Buffer.concat = function concat(list, length) {
  validateArray(list, 'list');

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

  if (length === undefined) {
    length = 0;
    for (let i = 0; i < list.length; 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 += TypedArrayPrototypeGetByteLength(buf);
    }

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

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

  validateOffset(length, 'length');
  for (let i = 0; i < list.length; i++) {
    if (!isUint8Array(list[i])) {
      // 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]);