{"record":{"id":"a87ea721284b90c7","repo":"amark/gun","slug":"first-argument-must-be-a-string-buffer-arraybuff","errorCode":null,"errorMessage":"First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.","messagePattern":"First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sea/buffer.js","lineNumber":19,"sourceCode":";(function(){\n\n    require('./base64');\n    // This is Buffer implementation used in SEA. Functionality is mostly\n    // compatible with NodeJS 'safe-buffer' and is used for encoding conversions\n    // between binary and 'hex' | 'utf8' | 'base64'\n    // See documentation and validation for safe implementation in:\n    // https://github.com/feross/safe-buffer#update\n    var SeaArray = require('./array');\n    function SafeBuffer(...props) {\n      console.warn('new SafeBuffer() is depreciated, please use SafeBuffer.from()')\n      return SafeBuffer.from(...props)\n    }\n    SafeBuffer.prototype = Object.create(Array.prototype)\n    Object.assign(SafeBuffer, {\n      // (data, enc) where typeof data === 'string' then enc === 'utf8'|'hex'|'base64'\n      from() {\n        if (!Object.keys(arguments).length || arguments[0]==null) {\n          throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')\n        }\n        const input = arguments[0]\n        let buf\n        if (typeof input === 'string') {\n          const enc = arguments[1] || 'utf8'\n          if (enc === 'hex') {\n            const bytes = input.match(/([\\da-fA-F]{2})/g)\n            .map((byte) => parseInt(byte, 16))\n            if (!bytes || !bytes.length) {\n              throw new TypeError('Invalid first argument for type \\'hex\\'.')\n            }\n            buf = SeaArray.from(bytes)\n          } else if (enc === 'utf8' || 'binary' === enc) { // EDIT BY MARK: I think this is safe, tested it against a couple \"binary\" strings. This lets SafeBuffer match NodeJS Buffer behavior more where it safely btoas regular strings.\n            const length = input.length\n            const words = new Uint16Array(length)\n            Array.from({ length: length }, (_, i) => words[i] = input.charCodeAt(i))\n            buf = SeaArray.from(words)\n          } else if (enc === 'base64') {","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/amark/gun/blob/552227599d47ba0493824b7fcf8a00c8cd6404ba/sea/buffer.js#L1-L37","documentation":"This sea/buffer.js SafeBuffer.from polyfill mimics Node's Buffer.from. It throws a TypeError when called with no arguments or with a null/undefined first argument, because there is no data to construct a buffer-like SeaArray from.","triggerScenarios":"Calling SafeBuffer.from() with zero arguments; calling SafeBuffer.from(null) or SafeBuffer.from(undefined); passing a variable that is null/undefined, e.g. from a failed lookup or empty JSON.parse result — note objects like {} or numbers do NOT throw here, only missing/nullish input does.","commonSituations":"Decoding a SEA token/proof where the field is absent; passing the result of sessionStorage/localStorage.getItem (null when missing) directly to Buffer.from; integration code assuming Node's Buffer exists when the code actually runs Gun's SafeBuffer polyfill (e.g. in browsers or sea builds).","solutions":["Check the value is a non-null string/ArrayBuffer/array before calling from()","Guard optional data sources (localStorage, config, JWT fields) before conversion","If you need an empty buffer, pass an empty string or array instead of null","Verify which Buffer implementation is loaded (sea/buffer.js polyfill vs Node Buffer) to know which validations apply"],"exampleFix":"// before\nconst key = SafeBuffer.from(localStorage.getItem('key'), 'utf8');\n// after\nconst stored = localStorage.getItem('key');\nif (typeof stored !== 'string') throw new Error('key not configured');\nconst key = SafeBuffer.from(stored, 'utf8');","handlingStrategy":"validation","validationCode":"function isBufFromInput(v) {\n  return v != null && (typeof v === 'string' || ArrayBuffer.isView(v) || v instanceof ArrayBuffer || Array.isArray(v));\n}\nif (!isBufFromInput(input)) throw new TypeError('from() needs non-null string/ArrayBuffer/array');","typeGuard":"const isNonNullData = (v) => v !== null && v !== undefined;","tryCatchPattern":"try {\n  const buf = SafeBuffer.from(input, enc);\n} catch (e) {\n  if (e instanceof TypeError) {\n    console.error('no data to buffer:', input);\n  }\n  throw e;\n}","preventionTips":["Null-check data from localStorage/config/network before from()","Never call from() with zero arguments","Default missing values to '' or [] explicitly if an empty buffer is acceptable"],"tags":["buffer","typeerror","input-validation","sea"],"backgroundTag":"invalid-buffer-input","analyzedSha":"552227599d47ba0493824b7fcf8a00c8cd6404ba","analyzedAt":"2026-09-02T18:40:58.370Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}