{"record":{"id":"72d1ab13d4a773b5","repo":"grafana/k6","slug":"data-must-be-an-arraybuffer-typedarray-or-datavi","errorCode":null,"errorMessage":"data must be an ArrayBuffer, TypedArray, or DataView","messagePattern":"data must be an ArrayBuffer, TypedArray, or DataView","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/webcrypto/subtle_crypto.go","lineNumber":423,"sourceCode":"//   - SHA-512\n//\n// The `data` parameter should contain the data to be digested.\nfunc (sc *SubtleCrypto) Digest(algorithm sobek.Value, data sobek.Value) (*sobek.Promise, error) {\n\trt := sc.vu.Runtime()\n\n\tvar (\n\t\thashFn func() hash.Hash\n\t\tbytes  []byte\n\t)\n\n\terr := func() error {\n\t\tvar err error\n\n\t\t// Validate that the value we received is either an ArrayBuffer, TypedArray, or DataView\n\t\t// This uses the technique described in https://github.com/dop251/goja/issues/379#issuecomment-1164441879\n\t\tif !IsInstanceOf(sc.vu.Runtime(), data, ArrayBufferConstructor, DataViewConstructor) &&\n\t\t\t!IsTypedArray(sc.vu.Runtime(), data) {\n\t\t\treturn errors.New(\"data must be an ArrayBuffer, TypedArray, or DataView\")\n\t\t}\n\n\t\tbytes, err = exportArrayBuffer(rt, data)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tnormalized, err := normalizeAlgorithm(rt, algorithm, OperationIdentifierDigest)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tvar ok bool\n\t\thashFn, ok = getHashFn(normalized.Name)\n\t\tif !ok {\n\t\t\treturn NewError(\n\t\t\t\tNotSupportedError,\n\t\t\t\t\"unsupported digest algorithm '\"+normalized.Name+\"', \"+","sourceCodeStart":405,"sourceCodeEnd":441,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/webcrypto/subtle_crypto.go#L405-L441","documentation":"Thrown by crypto.subtle.digest when the data argument is not an ArrayBuffer, TypedArray, or DataView. SubtleCrypto.Digest (internal/js/modules/k6/webcrypto/subtle_crypto.go:423) validates the value's constructor against ArrayBuffer/DataView or a typed-array check before hashing. This matches browsers: digest never accepts a plain string, so text must be encoded first.","triggerScenarios":"crypto.subtle.digest('SHA-256', 'hello') with a raw string, or digest('SHA-256', { data: bytes }) with a wrapper object. Passing null or a number fails the same check.","commonSituations":"Porting Node.js crypto.createHash().update(str) code that accepts strings; assuming k6 auto-converts strings to UTF-8 bytes; passing a Blob directly instead of awaiting blob.arrayBuffer().","solutions":["Encode strings first: crypto.subtle.digest('SHA-256', new TextEncoder().encode('hello'))","Pass existing binary data as a TypedArray/view: new Uint8Array(buf, byteOffset, length)","For a Blob, await blob.arrayBuffer() and pass the result"],"exampleFix":"// before\nconst digest = await crypto.subtle.digest('SHA-256', 'hello');\n\n// after\nconst digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode('hello'));","handlingStrategy":"type-guard","validationCode":"function toBufferSource(data) {\n  if (typeof data === 'string') return new TextEncoder().encode(data);\n  if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) return data;\n  throw new TypeError('digest data must be a string (encoded) or a buffer source');\n}\n\nawait crypto.subtle.digest('SHA-256', toBufferSource(input));","typeGuard":"function isBufferSource(v) {\n  return v instanceof ArrayBuffer || ArrayBuffer.isView(v);\n}","tryCatchPattern":"try {\n  digest = await crypto.subtle.digest('SHA-256', data);\n} catch (err) {\n  if (/must be an ArrayBuffer, TypedArray, or DataView/.test(String(err))) {\n    digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(String(data)));\n  } else {\n    throw err;\n  }\n}","preventionTips":["Encode all text once at the boundary: const enc = new TextEncoder()","Route every digest input through an isBufferSource check","Remember Blob is not a buffer source — await blob.arrayBuffer() first"],"tags":["webcrypto","digest","arraybuffer","typedarray","type-validation"],"backgroundTag":"typedarray-expected","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}