{"record":{"id":"b52d6c8b55c83e76","repo":"gchq/CyberChef","slug":"protobuf-input-must-be-a-bytearray-or-uint8array","errorCode":null,"errorMessage":"Protobuf input must be a byteArray or Uint8Array","messagePattern":"Protobuf input must be a byteArray or Uint8Array","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/lib/Protobuf.mjs","lineNumber":27,"sourceCode":" * integers (varint).\n *\n * @author GCHQ Contributor [3]\n * @copyright Crown Copyright 2019\n * @license Apache-2.0\n */\nclass Protobuf {\n\n    /**\n     * Protobuf constructor\n     *\n     * @param {byteArray|Uint8Array} data\n     */\n    constructor(data) {\n        // Check we have a byteArray or Uint8Array\n        if (data instanceof Array || data instanceof Uint8Array) {\n            this.data = data;\n        } else {\n            throw new Error(\"Protobuf input must be a byteArray or Uint8Array\");\n        }\n\n        // Set up masks\n        this.TYPE = 0x07;\n        this.NUMBER = 0x78;\n        this.MSB = 0x80;\n        this.VALUE = 0x7f;\n\n        // Declare offset, length, and field type object\n        this.offset = 0;\n        this.LENGTH = data.length;\n        this.fieldTypes = {};\n    }\n\n    // Public Functions\n\n    /**\n     * Encode a varint from a number","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/Protobuf.mjs#L9-L45","documentation":"Thrown by the Protobuf constructor when `data` is neither a plain Array nor a Uint8Array. It is a plain `Error` (not OperationError), so callers must catch it themselves. Note that ArrayBuffer, TypedArrays other than Uint8Array, and Buffer (Node) will fail this check.","triggerScenarios":"Constructing `new Protobuf(data)` with a string, number, plain object, ArrayBuffer, Int8Array/Float32Array, or null/undefined. Also a Node Buffer is not a Uint8Array in some environments and can fail.","commonSituations":"Passing an ArrayBuffer from a FileReader without wrapping in a Uint8Array view; passing raw string input from a recipe; feeding a JSON-parsed object; receiving data in a different TypedArray variant.","solutions":["Convert to a Uint8Array before construction: `new Protobuf(new Uint8Array(arrayBuffer))`.","If you have a byte list, ensure it is a true Array of 0-255 integers.","For Node Buffers, pass `new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)`."],"exampleFix":"// before\nconst pb = new Protobuf(arrayBuffer);\n// after\nconst pb = new Protobuf(Array.from(new Uint8Array(arrayBuffer)));","handlingStrategy":"type-guard","validationCode":"if (!(data instanceof Array) && !(data instanceof Uint8Array)) {\n  data = new Uint8Array(data instanceof ArrayBuffer ? data : new TextEncoder().encode(String(data)));\n}\nconst pb = new Protobuf(data);","typeGuard":"function isByteArrayOrUint8Array(v) {\n  return Array.isArray(v) || (v instanceof Uint8Array);\n}","tryCatchPattern":"try {\n  return new Protobuf(data);\n} catch (e) {\n  if (/must be a byteArray or Uint8Array/.test(e.message)) {\n    return new Protobuf(new Uint8Array(data));\n  }\n  throw e;\n}","preventionTips":["Always wrap ArrayBuffer in a Uint8Array view before constructing.","Coerce incoming data with `Array.from(new Uint8Array(buf))` at the boundary.","Do not pass strings or plain objects to the Protobuf constructor."],"tags":["protobuf","type-check","constructor","bytearray"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}