grafana/k6 · error · TypeError

typedArray parameter is required

Error message

typedArray parameter is required

What it means

crypto.getRandomValues() requires a TypedArray argument per the WebCrypto spec. k6 rejects a missing/nullish argument with an ECMAScript TypeError before dereferencing it, matching browser and Node behavior.

Solutions

  1. Always pass a TypedArray: crypto.getRandomValues(new Uint8Array(16)).
  2. Create the buffer first and verify it exists before the call.
  3. If the argument comes from elsewhere, default it to a fresh Uint8Array when nullish.

Example fix

// before
const bytes = crypto.getRandomValues();
// after
const bytes = crypto.getRandomValues(new Uint8Array(16));
Defensive patterns

Strategy: validation

Validate before calling

function secureRandom(n) {
  if (!Number.isInteger(n) || n <= 0) throw new TypeError('need a positive length');
  return crypto.getRandomValues(new Uint8Array(n));
}

Type guard

function isTypedArray(v) {
  return ArrayBuffer.isView(v) && !(v instanceof DataView);
}

Try / catch

try {
  crypto.getRandomValues(buf);
} catch (e) {
  buf = crypto.getRandomValues(new Uint8Array(16)); // fallback buffer
}

Prevention

When it happens

Trigger: Calling crypto.getRandomValues() with no arguments, with undefined, or with null.

Common situations: Forgetting the argument entirely; passing the result of a lookup that returned undefined (e.g. new Uint8Array(N) typo'd); porting code where the array creation was removed.

Related errors


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-09-20). Data as JSON: /api/errors/5e0a2ac1b63c27b9. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/webcrypto/crypto.go:45

//
// The array given as the parameter is filled with random numbers (random in
// its cryptographic sense, not in its statistical sense).
//
// To guarantee enough performance, this implementation is not using a truly
// random number generator, but is using a pseudo-random number generator
// seeded with a value with enough entropy. We are using the golang
// crypto/rand package, which uses the operating system's random number
// generator.
//
// [specification]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues
func (c *Crypto) GetRandomValues(typedArray sobek.Value) sobek.Value {
	// The typedArray parameter is not optional in the specification's WebIDL
	// definition, so reject a missing or nullish one before IsInstanceOf gets
	// to dereference it. This is an ECMAScript TypeError rather than one of the
	// WebCrypto error names, which is what browsers and Node throw here, and
	// what a nullish argument already produced before this check existed.
	if common.IsNullish(typedArray) {
		panic(c.vu.Runtime().NewTypeError("typedArray parameter is required"))
	}

	acceptedTypes := []JSType{
		Int8ArrayConstructor,
		Uint8ArrayConstructor,
		Uint8ClampedArrayConstructor,
		Int16ArrayConstructor,
		Uint16ArrayConstructor,
		Int32ArrayConstructor,
		Uint32ArrayConstructor,
	}

	// 1.
	if !IsInstanceOf(c.vu.Runtime(), typedArray, acceptedTypes...) {
		common.Throw(c.vu.Runtime(), NewError(TypeMismatchError, "typedArray parameter isn't a TypedArray instance"))
	}

	// 2.

View on GitHub (pinned to 3fcf5388d7)