amark/gun · error · TypeError

First argument must be Array containing ArrayBuffer or…

Error message

First argument must be Array containing ArrayBuffer or Uint8Array instances.

What it means

SafeBuffer.concat joins an array of buffer-like items into one SeaArray. It first asserts the argument is a real Array (Array.isArray) and throws a TypeError otherwise — a non-array argument (single buffer, object, string) cannot be reduced item by item.

Solutions

  1. Wrap the value in an array: SafeBuffer.concat([buf]) instead of SafeBuffer.concat(buf)
  2. Ensure the argument is created as a real Array (array literal, Array.from(iterable)) — a TypedArray alone is not Array.isArray-true
  3. Spread iterables: SafeBuffer.concat(Array.from(items))
  4. Check for undefined/null sources upstream before calling concat

Example fix

// before
const joined = SafeBuffer.concat(part1).concat(part2);
// after
const joined = SafeBuffer.concat([part1, part2]);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!Array.isArray(parts)) {
  throw new TypeError('concat expects an array of buffer-like items');
}

Type guard

const isBufList = (v) => Array.isArray(v) && v.every((x) => x && typeof x.length === 'number');

Try / catch

try {
  const joined = SafeBuffer.concat(list);
} catch (e) {
  if (e instanceof TypeError) console.error('concat requires an array, got:', typeof list);
  throw e;
}

Prevention

When it happens

Trigger: Calling SafeBuffer.concat(buf) passing a single Uint8Array/SeaArray instead of wrapping it in an array; passing a plain object, arguments object, or iterable that is not an Array; passing an undefined variable. Note the error text mentions ArrayBuffer/Uint8Array contents, but the actual runtime check is only Array.isArray(arr).

Common situations: Translating Node Buffer.concat usage where someone forgot the array literal around a single buffer; spreading mistakes (forgetting the ... before a list of buffers); TypedArray subclass results (e.g. from subarray) that are not Array instances.

Related errors


AI-assisted analysis of amark/gun@552227599d (2026-09-02). Data as JSON: /api/errors/e0f3767ea347deb0. Report an issue: GitHub.

Appendix: source

Thrown at sea/buffer.js:71

          let buf
          if (input instanceof ArrayBuffer) {
            buf = new Uint8Array(input)
          }
          return SeaArray.from(buf || input)
        }
      },
      // This is 'safe-buffer.alloc' sans encoding support
      alloc(length, fill = 0 /*, enc*/ ) {
        return SeaArray.from(new Uint8Array(Array.from({ length: length }, () => fill)))
      },
      // This is normal UNSAFE 'buffer.alloc' or 'new Buffer(length)' - don't use!
      allocUnsafe(length) {
        return SeaArray.from(new Uint8Array(Array.from({ length : length })))
      },
      // This puts together array of array like members
      concat(arr) { // octet array
        if (!Array.isArray(arr)) {
          throw new TypeError('First argument must be Array containing ArrayBuffer or Uint8Array instances.')
        }
        return SeaArray.from(arr.reduce((ret, item) => ret.concat(Array.from(item)), []))
      }
    })
    SafeBuffer.prototype.from = SafeBuffer.from
    SafeBuffer.prototype.toString = SeaArray.prototype.toString

    module.exports = SafeBuffer;
  
}());

View on GitHub (pinned to 552227599d)