{"record":{"id":"4e9bbafbf38c01b3","repo":"denoland/deno","slug":"a-chunk-may-only-be-either-a-string-or-an-uint8arr","errorCode":null,"errorMessage":"A chunk may only be either a string or an Uint8Array","messagePattern":"A chunk may only be either a string or an Uint8Array","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/websocket/02_websocketstream.js","lineNumber":224,"sourceCode":"                const err = new WebSocketError(\"Closed while connecting\");\n                this[_opened].reject(err);\n                this[_closed].reject(err);\n              },\n            );\n          } else {\n            this[_rid] = create.rid;\n\n            const writable = new WritableStream({\n              write: async (chunk) => {\n                if (typeof chunk === \"string\") {\n                  await op_ws_send_text_async(this[_rid], chunk);\n                } else if (\n                  TypedArrayPrototypeGetSymbolToStringTag(chunk) ===\n                    \"Uint8Array\"\n                ) {\n                  await op_ws_send_binary_async(this[_rid], chunk);\n                } else {\n                  throw new TypeError(\n                    \"A chunk may only be either a string or an Uint8Array\",\n                  );\n                }\n              },\n              close: async () => {\n                this.close();\n                await this.closed;\n              },\n              abort: async (reason) => {\n                let closeCode = null;\n                let reasonString = \"\";\n\n                if (\n                  ObjectPrototypeIsPrototypeOf(WebSocketErrorPrototype, reason)\n                ) {\n                  closeCode = reason.closeCode;\n                  reasonString = reason.reason;\n                }","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/websocket/02_websocketstream.js#L206-L242","documentation":"The writable side of a WebSocketStream only accepts strings and Uint8Arrays: the write() implementation checks typeof chunk === 'string' or the typed-array tag 'Uint8Array'. Anything else - ArrayBuffer, DataView, other typed arrays, Blob, plain objects, numbers - throws a plain TypeError from inside the stream.","triggerScenarios":"writer.write(new ArrayBuffer(8)), writer.write(new Uint16Array(4)), writer.write(blob) (allowed on WebSocket.send but not here), writer.write({ data }) without JSON.stringify, writer.write(42).","commonSituations":"Assuming ws.send()-compatible types carry over to WebSocketStream; forgetting JSON.stringify for objects; passing a raw ArrayBuffer from file/socket reads instead of a Uint8Array view.","solutions":["Encode text to bytes: new TextEncoder().encode(str)","Wrap buffers: new Uint8Array(arrayBuffer)","Stringify structured data: JSON.stringify(value)","Convert Blob: new Uint8Array(await blob.arrayBuffer())"],"exampleFix":"// before\nawait writer.write({ type: 'msg' });\nawait writer.write(await file.arrayBuffer()); // ArrayBuffer\n\n// after\nawait writer.write(JSON.stringify({ type: 'msg' }));\nawait writer.write(new Uint8Array(await file.arrayBuffer()));","handlingStrategy":"type-guard","validationCode":"function toChunk(v: unknown): string | Uint8Array {\n  if (typeof v === 'string') return v;\n  if (v instanceof Uint8Array) return v;\n  if (v instanceof ArrayBuffer) return new Uint8Array(v);\n  if (typeof v === 'object' && v !== null) return JSON.stringify(v);\n  throw new TypeError('unsupported chunk type');\n}\nawait writer.write(toChunk(data));","typeGuard":"const isWsChunk = (v: unknown): v is string | Uint8Array =>\n  typeof v === 'string' || v instanceof Uint8Array;","tryCatchPattern":null,"preventionTips":["Writable chunks must be string or Uint8Array - nothing else","Blob works on WebSocket.send but NOT on WebSocketStream.writable","Put one normalize/encode helper at the write boundary and use it everywhere"],"tags":["websocketstream","writable-stream","uint8array","typeerror","chunk-type"],"backgroundTag":"websocket-invalid-message-type","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}