grafana/k6 · error

data must be an ArrayBuffer, TypedArray, or DataView

Error message

data must be an ArrayBuffer, TypedArray, or DataView

What it means

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.

Solutions

  1. Encode strings first: crypto.subtle.digest('SHA-256', new TextEncoder().encode('hello'))
  2. Pass existing binary data as a TypedArray/view: new Uint8Array(buf, byteOffset, length)
  3. For a Blob, await blob.arrayBuffer() and pass the result

Example fix

// before
const digest = await crypto.subtle.digest('SHA-256', 'hello');

// after
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode('hello'));
Defensive patterns

Strategy: type-guard

Validate before calling

function toBufferSource(data) {
  if (typeof data === 'string') return new TextEncoder().encode(data);
  if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) return data;
  throw new TypeError('digest data must be a string (encoded) or a buffer source');
}

await crypto.subtle.digest('SHA-256', toBufferSource(input));

Type guard

function isBufferSource(v) {
  return v instanceof ArrayBuffer || ArrayBuffer.isView(v);
}

Try / catch

try {
  digest = await crypto.subtle.digest('SHA-256', data);
} catch (err) {
  if (/must be an ArrayBuffer, TypedArray, or DataView/.test(String(err))) {
    digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(String(data)));
  } else {
    throw err;
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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().

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/72d1ab13d4a773b5. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/webcrypto/subtle_crypto.go:423

//   - SHA-512
//
// The `data` parameter should contain the data to be digested.
func (sc *SubtleCrypto) Digest(algorithm sobek.Value, data sobek.Value) (*sobek.Promise, error) {
	rt := sc.vu.Runtime()

	var (
		hashFn func() hash.Hash
		bytes  []byte
	)

	err := func() error {
		var err error

		// Validate that the value we received is either an ArrayBuffer, TypedArray, or DataView
		// This uses the technique described in https://github.com/dop251/goja/issues/379#issuecomment-1164441879
		if !IsInstanceOf(sc.vu.Runtime(), data, ArrayBufferConstructor, DataViewConstructor) &&
			!IsTypedArray(sc.vu.Runtime(), data) {
			return errors.New("data must be an ArrayBuffer, TypedArray, or DataView")
		}

		bytes, err = exportArrayBuffer(rt, data)
		if err != nil {
			return err
		}

		normalized, err := normalizeAlgorithm(rt, algorithm, OperationIdentifierDigest)
		if err != nil {
			return err
		}

		var ok bool
		hashFn, ok = getHashFn(normalized.Name)
		if !ok {
			return NewError(
				NotSupportedError,
				"unsupported digest algorithm '"+normalized.Name+"', "+

View on GitHub (pinned to 01ffac6f24)