grafana/k6 · error

failed to create Uint8Array: %w

Error message

failed to create Uint8Array: %w

What it means

Blob.bytes() resolves its promise by calling rt.New(rt.Get('Uint8Array'), ...), i.e. it looks up the global Uint8Array constructor to wrap the blob's bytes. If that constructor was deleted, shadowed, or replaced by the script, or the runtime cannot allocate the typed array, the promise rejects with this wrapped failure instead of the bytes.

Source

Thrown at internal/js/modules/k6/websockets/blob.go:74

	must(rt, obj.DefineAccessorProperty("type", rt.ToValue(func() sobek.Value {
		return rt.ToValue(b.typ)
	}), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE))

	must(rt, obj.Set("arrayBuffer", func(_ sobek.FunctionCall) sobek.Value {
		promise, resolve, _ := rt.NewPromise()
		err := resolve(rt.NewArrayBuffer(b.data.Bytes()))
		if err != nil {
			panic(err)
		}
		return rt.ToValue(promise)
	}))
	must(rt, obj.Set("bytes", func(_ sobek.FunctionCall) sobek.Value {
		promise, resolve, reject := rt.NewPromise()
		data, err := rt.New(rt.Get("Uint8Array"), rt.ToValue(b.data.Bytes()))
		if err == nil {
			err = resolve(data)
		} else {
			err = reject(fmt.Errorf("failed to create Uint8Array: %w", err))
		}
		if err != nil {
			panic(err)
		}
		return rt.ToValue(promise)
	}))
	must(rt, obj.Set("slice", func(call sobek.FunctionCall) sobek.Value {
		return r.slice(call, b, rt)
	}))
	must(rt, obj.Set("text", func(_ sobek.FunctionCall) sobek.Value {
		promise, resolve, _ := rt.NewPromise()
		err := resolve(b.text())
		if err != nil {
			panic(err)
		}
		return rt.ToValue(promise)
	}))
	must(rt, obj.Set("stream", func(_ sobek.FunctionCall) sobek.Value {

View on GitHub (pinned to 93accf6570)

Solutions

  1. Remove or scope any code that reassigns/deletes Uint8Array on globalThis
  2. Save the original constructor and restore it before calling bytes(): globalThis.Uint8Array = SavedUint8Array
  3. Use blob.arrayBuffer() or blob.text() instead, which do not depend on the Uint8Array global
  4. Await the returned promise inside try/catch so the rejection surfaces with the wrapped cause

Example fix

// before
Uint8Array = myPolyfill; // ... later
const bytes = await blob.bytes(); // rejects: failed to create Uint8Array

// after
globalThis.Uint8Array = SavedUint8Array; // restore the builtin
const bytes = await blob.bytes();
// or avoid it entirely:
const ab = await blob.arrayBuffer();
Defensive patterns

Strategy: try-catch

Validate before calling

if (typeof Uint8Array !== 'function') throw new Error('Uint8Array global missing; blob.bytes() unavailable');
const bytes = await blob.bytes();

Type guard

const canBytes = () => typeof globalThis.Uint8Array === 'function';

Try / catch

try { bytes = await blob.bytes(); }
catch (e) { if (/Uint8Array/.test(e.message)) bytes = new Uint8Array(await blob.arrayBuffer()); else throw e; }

Prevention

When it happens

Trigger: Calling blob.bytes() after script code has overwritten globalThis.Uint8Array (e.g. Uint8Array = () => {} or delete globalThis.Uint8Array), or in an environment where the sobek runtime lacks the typed-array builtins. arrayBuffer() and text() are unaffected because they do not touch Uint8Array.

Common situations: Importing a JS library or k6 script that monkey-patches typed-array globals for polyfill/testing purposes; mock frameworks replacing built-ins; defensive 'hide typed arrays' snippets copied from the web. Very rare otherwise — if Uint8Array is intact this code path essentially cannot fail.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/1db839d881ca9e53. Report an issue: GitHub.