{"record":{"id":"05f0d3698957102b","repo":"grafana/k6","slug":"call-to-blob-arraybuffer-failed-w","errorCode":null,"errorMessage":"call to Blob.[arrayBuffer] failed: %w","messagePattern":"call to Blob\\.\\[arrayBuffer\\] failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/websockets/helpers.go","lineNumber":51,"sourceCode":"}\n\nfunc isBlob(o *sobek.Object, blobConstructor sobek.Value) bool {\n\treturn o.Prototype().Get(\"constructor\") == blobConstructor\n}\n\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":33,"sourceCodeEnd":66,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/websockets/helpers.go#L33-L66","documentation":"When a Blob is nested inside another Blob's parts, k6 reads its bytes by invoking the inner object's arrayBuffer() method. If that call itself throws (the method was replaced with a throwing stub, or the object is a fake Blob whose method errors), this wrapped error is thrown from inside fillData, typically surfacing as 'failed to process [blobParts]' upstream.","triggerScenarios":"new ws.Blob([fakeBlob]) where fakeBlob is a hand-made object whose arrayBuffer is not a real function that succeeds, e.g. { arrayBuffer: () => { throw new Error('boom') } }, or a real Blob whose arrayBuffer was monkey-patched. extractBytes calls the method synchronously and propagates any failure with %w.","commonSituations":"Test doubles/mocks standing in for Blob during unit tests of message building; partial reimplementation of the Blob API in userland helpers; subclass-like wrappers that forward methods but throw on unsupported states. Real Blob instances produced by the same constructor essentially never trigger this.","solutions":["Only nest Blobs created by the WebSocket module's own Blob constructor","Replace mocks with real small Blobs: new ws.Blob(['fixture'])","If you wrap a Blob, keep the original instance and expose it directly rather than reimplementing arrayBuffer()","Inspect the wrapped cause after the colon — it is the exception your fake arrayBuffer raised"],"exampleFix":"// before\nconst fake = { size: 3, arrayBuffer: async () => { throw new Error('not implemented'); } };\nconst b = new ws.Blob([fake]); // -> call to Blob.[arrayBuffer] failed\n\n// after\nconst real = new ws.Blob(['fix']);\nconst b = new ws.Blob([real]);","handlingStrategy":"type-guard","validationCode":"const isRealBlob = (p, BlobCtor) => p instanceof BlobCtor;\n// before nesting:\nconst parts = rawParts.map(p => (p && p.arrayBuffer && !isRealBlob(p, WS.Blob)) ? new WS.Blob([String(p)]) : p);\nconst b = new WS.Blob(parts);","typeGuard":"const isNestableBlob = (p, BlobCtor) => p instanceof BlobCtor && typeof p.arrayBuffer === 'function';","tryCatchPattern":"try { b = new WS.Blob(parts); } catch (e) { if (/arrayBuffer. failed/.test(e.message)) throw new Error('nested fake Blob detected - use real WS.Blob instances'); throw e; }","preventionTips":["Never hand-roll Blob doubles in tests; construct real ones with fixture data","If you wrap Blobs, store and expose the original instance rather than reimplementing methods","Keep mocks at the data level (strings/buffers), not at the Blob-API level"],"tags":["websockets","blob","mocking","internal"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}