grafana/k6 · error
Blob.[arrayBuffer] promise's return is not an ArrayBuffer
Error message
Blob.[arrayBuffer] promise's return is not an ArrayBuffer
What it means
The last step of Blob byte extraction in k6: after arrayBuffer() returns a Promise, its settled result must be an ArrayBuffer. extractBytes (internal/js/modules/k6/websockets/helpers.go:61) throws this when the Promise resolves to a Uint8Array, a string, or any other value — a Uint8Array is not an ArrayBuffer even though it wraps one.
Solutions
- Resolve with the underlying buffer: async arrayBuffer() { return this.bytes.buffer; }
- Send a genuine k6 Blob built from the data: ws.send(new Blob([data]))
- Pre-convert the object to an ArrayBuffer/Uint8Array in the script and pass that to send()
Example fix
// before
class MyBlob {
async arrayBuffer() { return new Uint8Array([1, 2, 3]); } // resolves to a view
}
// after
class MyBlob {
async arrayBuffer() { return new Uint8Array([1, 2, 3]).buffer; } // resolves to an ArrayBuffer
} Defensive patterns
Strategy: type-guard
Validate before calling
async function assertBlobResolvesArrayBuffer(v) {
if (!(v instanceof Blob)) return v;
const out = await v.arrayBuffer();
if (!(out instanceof ArrayBuffer)) {
throw new TypeError('arrayBuffer() must resolve to an ArrayBuffer, not a typed array');
}
return v;
}
ws.send(await assertBlobResolvesArrayBuffer(value)); Type guard
async function resolvesArrayBuffer(v) {
if (!(v instanceof Blob)) return false;
const out = await v.arrayBuffer();
return out instanceof ArrayBuffer;
} Prevention
- Return this.bytes.buffer (not this.bytes) from custom arrayBuffer() overrides
- Remember a Uint8Array is a view, not an ArrayBuffer — the extraction checks the exact type
- Unit-test custom Blob shims against: await blob.arrayBuffer() instanceof ArrayBuffer
When it happens
Trigger: ws.send(obj) or new Blob([obj]) where the object's async arrayBuffer() resolves to new Uint8Array(n) rather than new Uint8Array(n).buffer.
Common situations: Blob shims that resolve with typed arrays (common in Node-era polyfills); forgetting the .buffer accessor when adapting view types; test doubles returning already-copied byte arrays.
Related errors
- Blob.[arrayBuffer] return is not a Promise
- Blob.[arrayBuffer] is not a function
- [options] must be an object
- data must be an ArrayBuffer, TypedArray, or DataView
- last argument to ws.connect must be a function
AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18).
Data as JSON: /api/errors/22feeeb4d16277c2.
Report an issue: GitHub.
Appendix: source
Thrown at internal/js/modules/k6/websockets/helpers.go:61
func extractBytes(o *sobek.Object, rt *sobek.Runtime) []byte {
arrayBuffer, ok := sobek.AssertFunction(o.Get("arrayBuffer"))
if !ok {
common.Throw(rt, errors.New("Blob.[arrayBuffer] is not a function"))
}
buffer, err := arrayBuffer(sobek.Undefined())
if err != nil {
common.Throw(rt, fmt.Errorf("call to Blob.[arrayBuffer] failed: %w", err))
}
p, ok := buffer.Export().(*sobek.Promise)
if !ok {
common.Throw(rt, errors.New("Blob.[arrayBuffer] return is not a Promise"))
}
ab, ok := p.Result().Export().(sobek.ArrayBuffer)
if !ok {
common.Throw(rt, errors.New("Blob.[arrayBuffer] promise's return is not an ArrayBuffer"))
}
return ab.Bytes()
}
View on GitHub (pinned to 01ffac6f24)