{"record":{"id":"a8df0a14cf7097a2","repo":"denoland/deno","slug":"err-invalid-arg-type-a8df0a","errorCode":"ERR_INVALID_ARG_TYPE","errorMessage":"The \"data\" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received ${data}","messagePattern":"The \"data\" argument must be of type string or an instance of Buffer, TypedArray, or DataView\\. Received (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/crypto/cipher.ts","lineNumber":163,"sourceCode":"    cipher === \"aes256-wrap\" || cipher === \"id-aes128-wrap-pad\" ||\n    cipher === \"id-aes192-wrap-pad\" || cipher === \"id-aes256-wrap-pad\";\n}\n\nfunction isStringOrBuffer(\n  val: unknown,\n): val is string | Buffer | ArrayBuffer | ArrayBufferView {\n  return typeof val === \"string\" ||\n    isArrayBufferView(val) ||\n    isAnyArrayBuffer(val) ||\n    Buffer.isBuffer(val);\n}\n\n// Matches Node's `ArrayBuffer.isView(data)` check in\n// `lib/internal/crypto/cipher.js`: accepts string, Buffer, TypedArray\n// or DataView, but rejects raw ArrayBuffer / SharedArrayBuffer.\nfunction validateCipherUpdateData(data: unknown): void {\n  if (typeof data !== \"string\" && !ArrayBufferIsView(data)) {\n    throw new ERR_INVALID_ARG_TYPE(\n      \"data\",\n      [\"string\", \"Buffer\", \"TypedArray\", \"DataView\"],\n      data,\n    );\n  }\n}\n\nconst NO_TAG = new Uint8Array();\n\nfunction toU8(\n  input: string | Uint8Array | KeyObject | null,\n): Uint8Array {\n  if (input == null) {\n    return new Uint8Array(0);\n  }\n  if (isKeyObject(input)) {\n    return op_node_export_secret_key(input[kHandle]);\n  }","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/crypto/cipher.ts#L145-L181","documentation":"Cipheriv/Decipheriv .update() accepts only a string, Buffer/TypedArray, or DataView. The polyfill deliberately mirrors Node's `ArrayBuffer.isView(data)` check (see validateCipherUpdateData), so a raw ArrayBuffer or SharedArrayBuffer is rejected even though other crypto APIs (WebCrypto, toU8 helpers) accept array buffers. There is no implicit conversion: numbers, null, and plain objects fail the same check.","triggerScenarios":"cipher.update(new ArrayBuffer(16)); decipher.update(42); cipher.update({ data: 'x' }); passing a SharedArrayBuffer that is not wrapped in a typed-array view.","commonSituations":"Feeding an ArrayBuffer from fetch()/WebSocket responses directly into a node:crypto cipher; passing deserialized JSON values or numbers; refactors that swapped Buffer for ArrayBuffer when adopting WebCrypto-style code.","solutions":["Wrap raw ArrayBuffer/SharedArrayBuffer in a view: new Uint8Array(buf) or Buffer.from(buf).","For strings, pass them directly as data with inputEncoding, or Buffer.from(str, enc) explicitly.","Route all update() calls through a helper that applies the type guard first."],"exampleFix":"// before\nconst ab: ArrayBuffer = await res.arrayBuffer();\ncipher.update(ab); // ERR_INVALID_ARG_TYPE\n\n// after\nconst ab: ArrayBuffer = await res.arrayBuffer();\ncipher.update(new Uint8Array(ab)); // TypedArray view is accepted","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isCipherUpdateData(v: unknown): v is string | Buffer | ArrayBufferView {\n  return typeof v === 'string' || ArrayBuffer.isView(v) || Buffer.isBuffer(v);\n}\n// usage\nif (!isCipherUpdateData(data)) data = Buffer.from(data as string); // or wrap/reject","tryCatchPattern":"try { cipher.update(data); } catch (e) { if (e.code === 'ERR_INVALID_ARG_TYPE' && /\"data\"/.test(e.message)) { cipher.update(new Uint8Array(data as ArrayBuffer)); } else throw e; }","preventionTips":["Always view-wrap ArrayBuffers: new Uint8Array(ab) before update().","Type function parameters as string | Buffer | ArrayBufferView so invalid inputs fail at compile time.","Keep one shared encode/encrypt helper so conversion happens in exactly one place."],"tags":["crypto","cipher","type-validation","buffer","node-compat"],"backgroundTag":"invalid-argument-type","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}