grafana/k6 · error

invalid type %T, expected string, []byte or ArrayBuffer

Error message

invalid type %T, expected string, []byte or ArrayBuffer

What it means

Returned by the webcrypto ToBytes helper when its argument is none of the three accepted types. The switch accepts []byte (internal), string, and sobek.ArrayBuffer; any other JS value — numbers, plain objects, TypedArrays that are not ArrayBuffers, null — hits the default branch and the actual Go type is reported. Typically fires when crypto APIs receive an unsupported data representation.

Solutions

  1. Pass a string or an ArrayBuffer; for TypedArrays use .buffer to obtain the underlying ArrayBuffer
  2. Convert other types explicitly, e.g. String(x) or new TextEncoder().encode(x).buffer
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at internal/js/modules/k6/webcrypto/types.go:35 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at internal/js/modules/k6/webcrypto/types.go:35

// byteLength is a type alias for the length of a byte slice.
type byteLength int

// asBitLength returns the length of the byte slice in bits.
func (b byteLength) asBitLength() bitLength {
	return bitLength(b) * 8
}

// ToBytes tries to return a byte slice from compatible types.
func ToBytes(data any) ([]byte, error) {
	switch dt := data.(type) {
	case []byte:
		return dt, nil
	case string:
		return []byte(dt), nil
	case sobek.ArrayBuffer:
		return dt.Bytes(), nil
	default:
		return nil, fmt.Errorf("invalid type %T, expected string, []byte or ArrayBuffer", data)
	}
}

View on GitHub (pinned to 01ffac6f24)