grafana/k6 · error

failed to process [blobParts]: %w

Error message

failed to process [blobParts]: %w

What it means

The WebSocket module's Blob constructor (new ws.Blob(...)) exports its first argument into a []any via sobek's ExportTo. If that argument is not an array (string, number, object, null, undefined-with-extra-args), the export fails and k6 throws this error, wrapping sobek's conversion message.

Source

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

)

type blob struct {
	typ  string
	data *bytes.Buffer
}

func (b *blob) text() string {
	return b.data.String()
}

func (r *WebSocketsAPI) blob(call sobek.ConstructorCall) *sobek.Object {
	rt := r.vu.Runtime()

	b := &blob{data: new(bytes.Buffer)}
	var blobParts []any
	if len(call.Arguments) > 0 {
		if err := rt.ExportTo(call.Arguments[0], &blobParts); err != nil {
			common.Throw(rt, fmt.Errorf("failed to process [blobParts]: %w", err))
		}
	}

	if len(blobParts) > 0 {
		r.fillData(b, blobParts, call)
	}

	if len(call.Arguments) > 1 && !sobek.IsUndefined(call.Arguments[1]) {
		opts := call.Arguments[1]
		if !isObject(opts) {
			common.Throw(rt, errors.New("[options] must be an object"))
		}

		typeOpt := opts.ToObject(rt).Get("type")
		if !sobek.IsUndefined(typeOpt) {
			b.typ = typeOpt.String()
		}
	}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Always wrap parts in an array literal: new Blob([value])
  2. If parts come from a variable, normalize first: const parts = Array.isArray(v) ? v : [v]
  3. Check for undefined before constructing and pass an explicit empty array for an empty Blob

Example fix

// before
const b = new ws.Blob(someString); // TypeError: failed to process [blobParts]

// after
const b = new ws.Blob([someString]);
Defensive patterns

Strategy: validation

Validate before calling

function makeBlob(parts) {
  const arr = parts === undefined ? [] : parts;
  if (!Array.isArray(arr)) throw new TypeError('blobParts must be an array');
  return new WS.Blob(arr);
}

Type guard

const isPartsArray = v => v === undefined || Array.isArray(v);

Try / catch

try { b = new WS.Blob(parts); } catch (e) { if (/blobParts/.test(e.message)) throw new TypeError('wrap parts in an array: new Blob([value])'); throw e; }

Prevention

When it happens

Trigger: Calling new WebSocket Blob with a non-array blobParts: new Blob('hello'), new Blob({text:'hello'}), new Blob(42). Only new Blob([...]) with an actual JS array (or no first argument at all) is valid; a TypedArray of parts is also rejected because it is not a plain array.

Common situations: Porting browser code where a string shorthand was never valid anyway but the error is unfamiliar; passing a single Buffer-like value instead of wrapping it in an array; a variable that is undefined after a failed lookup being spread into the constructor; migrating scripts from the experimental websockets API with changed signatures.

Related errors


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