本页还没有译文
本页 17 段还没译成繁體中文,下面显示的是英文原文。ReadableStream BYOBReader
还没翻译译自英文(developers.cloudflare.com)本页 17 段都还没译
Background
BYOB is an abbreviation of bring your own buffer. A ReadableStreamBYOBReader allows reading into a developer-supplied buffer, thus minimizing copies.
An instance of ReadableStreamBYOBReader is functionally identical to ReadableStreamDefaultReader with the exception of the read method.
A ReadableStreamBYOBReader is not instantiated via its constructor. Rather, it is retrieved from a ReadableStream:
const { readable, writable } = new TransformStream();
const reader = readable.getReader({ mode: 'byob' });Methods
-
read(bufferArrayBufferView): Promise<ReadableStreamBYOBReadResult>- Returns a promise with the next available chunk of data read into a passed-in buffer.
-
readAtLeast(minElements, bufferArrayBufferView): Promise<ReadableStreamBYOBReadResult>-
Returns a promise with the next available chunk of data read into a passed-in buffer. The promise will not resolve until at least
minElementselements have been read. The element size is determined bybufferArrayBufferView, for example 4 bytes per element for aUint32Array. However, fewer thanminElementselements may be returned if the end of the stream is reached or the underlying stream is closed. Specifically:- If
minElementsor more elements are available, the promise resolves with{ value: <buffer view sized to bytes read>, done: false }. - If the stream ends after some data has been read but fewer than
minElementselements, the promise resolves with the partial data:{ value: <buffer view sized to bytes actually read>, done: false }. The next call toreadorreadAtLeastwill then return{ value: undefined, done: true }. - If the stream ends with zero bytes available (that is, the stream is already at EOF), the promise resolves with
{ value: <zero-length view>, done: true }. - If the stream errors, the promise rejects.
minElementsmust be at least 1, andminElements * elementSizemust not exceed the byte length ofbufferArrayBufferView, or the promise rejects with aTypeError. For aUint8Array, element size is 1, sominElementsis effectively a byte count.
- If
-
Common issues
Warning
read provides no control over the minimum number of bytes that should be read into the buffer. Even if you allocate a 1 MiB buffer, the kernel is perfectly within its rights to fulfill this read with a single byte, whether or not an EOF immediately follows.
In practice, the Workers team has found that read typically fills only 1% of the provided buffer.
readAtLeast is a non-standard extension to the Streams API which allows users to specify that at least minElements elements must be read into the buffer before resolving the read. For a Uint8Array (the most common case), each element is one byte, so minElements is effectively a byte count. If the stream ends before minElements elements are available, the partial data that was read is still returned rather than throwing an error — refer to the readAtLeast method documentation above for the full details.