{"record":{"id":"3f9d09c33734bae3","repo":"denoland/deno","slug":"expected-arraybuffer-or-arraybufferview-for-write","errorCode":null,"errorMessage":"Expected ArrayBuffer or ArrayBufferView for write chunk","messagePattern":"Expected ArrayBuffer or ArrayBufferView for write chunk","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/web/06_streams.js","lineNumber":459,"sourceCode":"function bufferSourceAsUint8Array(O) {\n  if (isTypedArray(O)) {\n    return new Uint8Array(\n      TypedArrayPrototypeGetBuffer(/** @type {Uint8Array} */ (O)),\n      TypedArrayPrototypeGetByteOffset(/** @type {Uint8Array} */ (O)),\n      TypedArrayPrototypeGetByteLength(/** @type {Uint8Array} */ (O)),\n    );\n  }\n  if (ArrayBufferIsView(O)) {\n    return new Uint8Array(\n      DataViewPrototypeGetBuffer(/** @type {DataView} */ (O)),\n      DataViewPrototypeGetByteOffset(/** @type {DataView} */ (O)),\n      DataViewPrototypeGetByteLength(/** @type {DataView} */ (O)),\n    );\n  }\n  if (isAnyArrayBuffer(O)) {\n    return new Uint8Array(/** @type {ArrayBuffer} */ (O));\n  }\n  throw new TypeError(\n    \"Expected ArrayBuffer or ArrayBufferView for write chunk\",\n  );\n}\n\n/**\n * Byte length of an ArrayBuffer or ArrayBufferView. Throws TypeError otherwise.\n * @param {unknown} O\n * @returns {number}\n */\nfunction bufferSourceByteLength(O) {\n  if (isTypedArray(O)) {\n    return TypedArrayPrototypeGetByteLength(/** @type {Uint8Array} */ (O));\n  }\n  if (ArrayBufferIsView(O)) {\n    return DataViewPrototypeGetByteLength(/** @type {DataView} */ (O));\n  }\n  if (isAnyArrayBuffer(O)) {\n    return ArrayBufferPrototypeGetByteLength(/** @type {ArrayBuffer} */ (O));","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/06_streams.js#L441-L477","documentation":"Resource-backed WritableStreams (Deno.stdout/stderr .writable, file .writable, connection writables) coerce every chunk with bufferSourceAsUint8Array inside their write() sink (ext/web/06_streams.js:1524-1527 -> 441-462). Only TypedArrays, DataView, and (Shared)ArrayBuffers are accepted; a string, number, Blob, or plain object throws TypeError 'Expected ArrayBuffer or ArrayBufferView for write chunk', rejecting the writer.write() promise. Generic WritableStreams accept any chunk — this guard is specific to rid-backed byte streams.","triggerScenarios":"await Deno.stdout.writable.getWriter().write('hello'); writing a Blob or plain object chunk to file.writable or a connection's writable; code ported from Node streams that writes strings with an assumed encoding.","commonSituations":"Logging pipelines writing formatted strings directly to stdout/stderr writables; saving JSON.stringify output to a file stream; feeding fetch/socket bodies with strings or Blobs; refactors where a transform upstream silently changes chunk type from bytes to text.","solutions":["Encode text chunks before writing: writer.write(new TextEncoder().encode(text)).","Convert Blobs first: writer.write(new Uint8Array(await blob.arrayBuffer())).","Pipe string producers through a TextEncoderStream so the byte stream receives Uint8Array chunks.","Type stream chunks as Uint8Array at the boundary to catch regressions at compile time."],"exampleFix":"// before\nconst w = Deno.stdout.writable.getWriter();\nawait w.write('hello\\n'); // TypeError: Expected ArrayBuffer or ArrayBufferView for write chunk\n\n// after\nconst encoder = new TextEncoder();\nawait w.write(encoder.encode('hello\\n'));","handlingStrategy":"type-guard","validationCode":"const toChunk = (d) =>\n  typeof d === 'string' ? new TextEncoder().encode(d) : d;\nconst chunk = toChunk(data);\nif (chunk instanceof ArrayBuffer || ArrayBuffer.isView(chunk)) {\n  await writer.write(chunk);\n} else {\n  throw new TypeError(`unsupported chunk type: ${typeof chunk}`);\n}","typeGuard":"function isBufferSource(v) {\n  return v instanceof ArrayBuffer || ArrayBuffer.isView(v);\n}","tryCatchPattern":"try {\n  await writer.write(chunk);\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('write chunk')) {\n    await writer.write(new TextEncoder().encode(chunk));\n  } else throw e;\n}","preventionTips":["Encode strings with TextEncoder before writing to stdout/file/socket writables","Pipe text sources through a TextEncoderStream","Type writer chunks as Uint8Array at the boundary"],"tags":["streams","writablestream","uint8array","stdio"],"backgroundTag":"invalid-stream-chunk-type","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}