{"record":{"id":"1db839d881ca9e53","repo":"grafana/k6","slug":"failed-to-create-uint8array-w","errorCode":null,"errorMessage":"failed to create Uint8Array: %w","messagePattern":"failed to create Uint8Array: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/websockets/blob.go","lineNumber":74,"sourceCode":"\tmust(rt, obj.DefineAccessorProperty(\"type\", rt.ToValue(func() sobek.Value {\n\t\treturn rt.ToValue(b.typ)\n\t}), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE))\n\n\tmust(rt, obj.Set(\"arrayBuffer\", func(_ sobek.FunctionCall) sobek.Value {\n\t\tpromise, resolve, _ := rt.NewPromise()\n\t\terr := resolve(rt.NewArrayBuffer(b.data.Bytes()))\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\treturn rt.ToValue(promise)\n\t}))\n\tmust(rt, obj.Set(\"bytes\", func(_ sobek.FunctionCall) sobek.Value {\n\t\tpromise, resolve, reject := rt.NewPromise()\n\t\tdata, err := rt.New(rt.Get(\"Uint8Array\"), rt.ToValue(b.data.Bytes()))\n\t\tif err == nil {\n\t\t\terr = resolve(data)\n\t\t} else {\n\t\t\terr = reject(fmt.Errorf(\"failed to create Uint8Array: %w\", err))\n\t\t}\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\treturn rt.ToValue(promise)\n\t}))\n\tmust(rt, obj.Set(\"slice\", func(call sobek.FunctionCall) sobek.Value {\n\t\treturn r.slice(call, b, rt)\n\t}))\n\tmust(rt, obj.Set(\"text\", func(_ sobek.FunctionCall) sobek.Value {\n\t\tpromise, resolve, _ := rt.NewPromise()\n\t\terr := resolve(b.text())\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\treturn rt.ToValue(promise)\n\t}))\n\tmust(rt, obj.Set(\"stream\", func(_ sobek.FunctionCall) sobek.Value {","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/websockets/blob.go#L56-L92","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove or scope any code that reassigns/deletes Uint8Array on globalThis","Save the original constructor and restore it before calling bytes(): globalThis.Uint8Array = SavedUint8Array","Use blob.arrayBuffer() or blob.text() instead, which do not depend on the Uint8Array global","Await the returned promise inside try/catch so the rejection surfaces with the wrapped cause"],"exampleFix":"// before\nUint8Array = myPolyfill; // ... later\nconst bytes = await blob.bytes(); // rejects: failed to create Uint8Array\n\n// after\nglobalThis.Uint8Array = SavedUint8Array; // restore the builtin\nconst bytes = await blob.bytes();\n// or avoid it entirely:\nconst ab = await blob.arrayBuffer();","handlingStrategy":"try-catch","validationCode":"if (typeof Uint8Array !== 'function') throw new Error('Uint8Array global missing; blob.bytes() unavailable');\nconst bytes = await blob.bytes();","typeGuard":"const canBytes = () => typeof globalThis.Uint8Array === 'function';","tryCatchPattern":"try { bytes = await blob.bytes(); }\ncatch (e) { if (/Uint8Array/.test(e.message)) bytes = new Uint8Array(await blob.arrayBuffer()); else throw e; }","preventionTips":["Never reassign or delete typed-array globals (Uint8Array and friends) in k6 scripts","Scope polyfills inside functions instead of patching globalThis","Prefer blob.arrayBuffer()/blob.text() — they avoid the Uint8Array dependency entirely","Keep an untouched reference (const NativeUA = Uint8Array) if you must experiment, and restore before use"],"tags":["websockets","blob","uint8array","runtime","global-pollution"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}