{"record":{"id":"ada910ed9ac88ac7","repo":"denoland/deno","slug":"err-invalid-arg-type-ada910","errorCode":"ERR_INVALID_ARG_TYPE","errorMessage":"The \"${name}\" argument must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, or DataView. Received ${actual}","messagePattern":"The \"(.+?)\" argument must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, or DataView\\. Received (.+?)","errorType":"error_code","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/crypto/keys.ts","lineNumber":130,"sourceCode":"    | Uint32Array => {\n    if (isAnyArrayBuffer(buffer)) {\n      return new Uint8Array(buffer);\n    }\n    if (typeof buffer === \"string\") {\n      if (encoding === \"buffer\") {\n        encoding = \"utf8\";\n      }\n      return Buffer.from(buffer, encoding);\n    }\n    if (ObjectPrototypeIsPrototypeOf(DataViewPrototype, buffer)) {\n      return new Uint8Array(\n        DataViewPrototypeGetBuffer(buffer),\n        DataViewPrototypeGetByteOffset(buffer),\n        DataViewPrototypeGetByteLength(buffer),\n      );\n    }\n    if (!isArrayBufferView(buffer)) {\n      throw new ERR_INVALID_ARG_TYPE(\n        name,\n        [\n          \"string\",\n          \"ArrayBuffer\",\n          \"Buffer\",\n          \"TypedArray\",\n          \"DataView\",\n        ],\n        buffer,\n      );\n    }\n    return buffer;\n  },\n);\n\nconst kConsumePublic = 0;\nconst kConsumePrivate = 1;\nconst kCreatePublic = 2;","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/crypto/keys.ts#L112-L148","documentation":"The shared getArrayBufferOrView helper normalizes raw crypto material (keys, passwords, salts, passphrases) for many node:crypto APIs. When the value is neither a string, ArrayBuffer, Buffer, TypedArray, nor DataView, it throws ERR_INVALID_ARG_TYPE; the reported argument name varies by call site ('key', 'key.key', 'key.passphrase', 'password', 'salt', 'spkac').","triggerScenarios":"crypto.scrypt(12345, 'salt', 64, cb) with a numeric password; crypto.createSecretKey(null); Certificate.exportChallenge(spkac) with a non-buffer; createPublicKey({ key: { key: 42, format: 'pem' } }) where nested key.key is not string/bytes; passing a plain object where key material is expected.","commonSituations":"Reading key material from JSON/env/config and forgetting to Buffer.from or fs.readFileSync it; undefined caused by typo'd property names in destructuring; assuming numbers or objects are coerced automatically because old scripts did so.","solutions":["Convert the value to a Buffer or string before the call: Buffer.from(str), fs.readFileSync(path)","Fix the upstream source of undefined/null (typo'd key name, missing env var, wrong JSON path)","Pass strings or Buffers explicitly; never numbers or plain objects, as crypto material"],"exampleFix":"// before\nconst key = config.apiSecret; // number 98765...\ncrypto.createSecretKey(key);\n// after\nconst key = Buffer.from(String(config.apiSecret), 'utf8');\ncrypto.createSecretKey(key);","handlingStrategy":"type-guard","validationCode":"function toKeyMaterial(v) {\n  if (typeof v === 'string') return Buffer.from(v, 'utf8');\n  if (Buffer.isBuffer(v) || v instanceof Uint8Array || v instanceof ArrayBuffer) return v;\n  throw new TypeError(`Expected key material, got ${typeof v}`);\n}\ncrypto.createSecretKey(toKeyMaterial(config.secret));","typeGuard":"function isStringOrBinary(v) {\n  return typeof v === 'string' || v instanceof ArrayBuffer || ArrayBuffer.isView(v) || Buffer.isBuffer(v);\n}","tryCatchPattern":"try {\n  crypto.scrypt(password, salt, 64, cb);\n} catch (e) {\n  if (e.code === 'ERR_INVALID_ARG_TYPE') throw new Error('password/salt must be string or Buffer');\n  throw e;\n}","preventionTips":["Decode all config/env values to Buffer at the boundary","Destructure with defaults and fail fast on undefined","Never pass numbers as passwords, salts, or keys"],"tags":["crypto","argument-validation","buffer","node-compat"],"backgroundTag":"invalid-argument-type","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}