{"record":{"id":"816d4da49455d01d","repo":"denoland/deno","slug":"cannot-use-a-byob-reader-with-a-non-byte-stream","errorCode":null,"errorMessage":"Cannot use a BYOB reader with a non-byte stream","messagePattern":"Cannot use a BYOB reader with a non-byte stream","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/web/06_streams.js","lineNumber":4518,"sourceCode":"  );\n}\n\n/**\n * @template R\n * @param {ReadableStreamBYOBReader} reader\n * @param {ReadableStream<R>} stream\n */\nfunction setUpReadableStreamBYOBReader(reader, stream) {\n  if (isReadableStreamLocked(stream)) {\n    throw new TypeError(\"ReadableStream is locked\");\n  }\n  if (\n    !(ObjectPrototypeIsPrototypeOf(\n      ReadableByteStreamControllerPrototype,\n      stream[_controller],\n    ))\n  ) {\n    throw new TypeError(\"Cannot use a BYOB reader with a non-byte stream\");\n  }\n  readableStreamReaderGenericInitialize(reader, stream);\n  reader[_readIntoRequests] = new Queue();\n}\n\n/**\n * @template R\n * @param {ReadableStreamDefaultReader<R>} reader\n * @param {ReadableStream<R>} stream\n */\nfunction setUpReadableStreamDefaultReader(reader, stream) {\n  if (isReadableStreamLocked(stream)) {\n    throw new TypeError(\"ReadableStream is locked\");\n  }\n  readableStreamReaderGenericInitialize(reader, stream);\n  reader[_readRequests] = new Queue();\n}\n","sourceCodeStart":4500,"sourceCodeEnd":4536,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/06_streams.js#L4500-L4536","documentation":"A BYOB reader was attached to a ReadableStream that is not a byte stream: its controller is a ReadableStreamDefaultController because it was constructed without { type: 'bytes' }, so it fails the ObjectPrototypeIsPrototypeOf(ReadableByteStreamControllerPrototype, ...) check. read-into (BYOB) semantics only exist for byte streams, whose controller can fill caller-provided buffers.","triggerScenarios":"stream.getReader({ mode: 'byob' }) or new ReadableStreamBYOBReader(stream) on a stream created via new ReadableStream() without type: 'bytes'; applying a BYOB reader to the readable side of a TransformStream; applying it to framework-produced value streams (fetch bodies, helpers).","commonSituations":"Generic utility functions that assume all streams are byte streams; adapting a value stream and attempting zero-copy reads; library code that must work on both kinds and probes BYOB support by trying it.","solutions":["Create the source as a byte stream: new ReadableStream({ type: 'bytes', ... }).","Use a default reader (getReader()) when the stream is a value stream.","If you control the pipeline, funnel value-stream chunks into a byte stream you construct yourself."],"exampleFix":"// before\nconst stream = new ReadableStream({ start(c) { c.enqueue(new Uint8Array(1)); } });\nconst reader = stream.getReader({ mode: 'byob' }); // TypeError: non-byte stream\n\n// after\nconst stream = new ReadableStream({ type: 'bytes', start(c) { c.enqueue(new Uint8Array(1)); } });\nconst reader = stream.getReader({ mode: 'byob' }); // ok","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isByteStream(s) {\n  if (s.locked) return false; // cannot probe while locked\n  try {\n    const r = s.getReader({ mode: 'byob' });\n    r.releaseLock();\n    return true;\n  } catch {\n    return false; // not a byte stream\n  }\n}\nconst reader = isByteStream(stream)\n  ? stream.getReader({ mode: 'byob' })\n  : stream.getReader();","tryCatchPattern":"try {\n  reader = stream.getReader({ mode: 'byob' });\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('non-byte stream')) {\n    reader = stream.getReader(); // fall back to a default reader\n  } else throw e;\n}","preventionTips":["BYOB requires the producer's cooperation: { type: 'bytes' } at construction.","Design helpers to accept a reader and let the caller choose the mode.","Test generic stream utilities against both byte and value streams."],"tags":["readable-stream","byob-reader","byte-stream","type-mismatch"],"backgroundTag":"byob-reader-type-mismatch","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}