{"record":{"id":"2072b81526dd8283","repo":"grafana/k6","slug":"blob-arraybuffer-return-is-not-a-promise","errorCode":null,"errorMessage":"Blob.[arrayBuffer] return is not a Promise","messagePattern":"Blob\\.\\[arrayBuffer\\] return is not a Promise","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/websockets/helpers.go","lineNumber":56,"sourceCode":"\nfunc isObject(val sobek.Value) bool {\n\treturn val != nil && val.ExportType() != nil && val.ExportType().Kind() == reflect.Map\n}\n\nfunc extractBytes(o *sobek.Object, rt *sobek.Runtime) []byte {\n\tarrayBuffer, ok := sobek.AssertFunction(o.Get(\"arrayBuffer\"))\n\tif !ok {\n\t\tcommon.Throw(rt, errors.New(\"Blob.[arrayBuffer] is not a function\"))\n\t}\n\n\tbuffer, err := arrayBuffer(sobek.Undefined())\n\tif err != nil {\n\t\tcommon.Throw(rt, fmt.Errorf(\"call to Blob.[arrayBuffer] failed: %w\", err))\n\t}\n\n\tp, ok := buffer.Export().(*sobek.Promise)\n\tif !ok {\n\t\tcommon.Throw(rt, errors.New(\"Blob.[arrayBuffer] return is not a Promise\"))\n\t}\n\n\tab, ok := p.Result().Export().(sobek.ArrayBuffer)\n\tif !ok {\n\t\tcommon.Throw(rt, errors.New(\"Blob.[arrayBuffer] promise's return is not an ArrayBuffer\"))\n\t}\n\n\treturn ab.Bytes()\n}\n","sourceCodeStart":38,"sourceCodeEnd":66,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/websockets/helpers.go#L38-L66","documentation":"After calling arrayBuffer() on a Blob-like object, k6 requires the return value to be a Promise, because k6's own Blob.arrayBuffer resolves to an ArrayBuffer. extractBytes (internal/js/modules/k6/websockets/helpers.go:56) throws this when a Blob-shaped object's arrayBuffer() returns synchronously (raw bytes, a Uint8Array, or a plain value) instead of a Promise.","triggerScenarios":"ws.send(obj) or new Blob([obj]) where obj's class overrides arrayBuffer as a plain method returning the bytes directly, e.g. arrayBuffer() { return this.buf; } — Node Buffer-style synchronous semantics instead of the Web Blob contract.","commonSituations":"A custom Blob shim written for older environments where arrayBuffer was synchronous; a subclass that 'optimizes away' the Promise; test doubles of Blob that forgot the async contract.","solutions":["Make the override async (or return Promise.resolve(...)) so arrayBuffer() yields a Promise","Send a real k6 Blob: ws.send(new Blob([data]))","Extract the bytes to a Uint8Array in script code and send that directly"],"exampleFix":"// before\nclass MyBlob {\n  constructor(buf) { this.buf = buf; }\n  arrayBuffer() { return this.buf; } // synchronous — throws\n}\nws.send(new MyBlob(bytes));\n\n// after\nclass MyBlob {\n  constructor(buf) { this.buf = buf; }\n  async arrayBuffer() { return this.buf; } // returns a Promise\n}\nws.send(new MyBlob(bytes));","handlingStrategy":"type-guard","validationCode":"function checkBlobContract(v) {\n  if (!(v instanceof Blob)) return v;\n  const ret = v.arrayBuffer();\n  if (!(ret instanceof Promise)) {\n    throw new TypeError('arrayBuffer() must return a Promise — make the override async');\n  }\n  return v;\n}\n\nws.send(checkBlobContract(value));","typeGuard":"function blobHasAsyncArrayBuffer(v) {\n  return v instanceof Blob && typeof v.arrayBuffer === 'function';\n}","tryCatchPattern":null,"preventionTips":["Model Blob overrides on the Web contract: async arrayBuffer() resolving to an ArrayBuffer","Run a one-line smoke check of any custom Blob (await blob.arrayBuffer()) before using it in sends","Prefer k6's built-in Blob over shims from other runtimes"],"tags":["websocket","blob","promise","duck-typing","type-validation"],"backgroundTag":"invalid-blob-object","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}