You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
Currently, Deno provides Deno.readFile(), which reads an entire file into a buffer, and Deno.FsFile.read(), which reads a portion of a file into a buffer. However, Deno.FsFile.read() may return before reading the full requested number of bytes. This creates a challenge when attempting to reliably read exactly a specified number of bytes, requiring users to write custom logic to ensure they handle partial reads correctly.
Describe the solution you'd like
I propose adding a utility function to the standard library that abstracts away the complexity of handling partial reads, ensuring that the desired number of bytes is read into a buffer. This function might behave similarly to the following code:
This function would ensure that the buffer is filled (if possible) or gracefully handle end-of-file conditions without requiring developers to write custom loops.
Describe alternatives you've considered
Alternatives include manually implementing this logic in every project, as illustrated above. However, this approach leads to code duplication and potential bugs across codebases.
The text was updated successfully, but these errors were encountered:
You can achieve the same goal by using a ReadableStreamBYOBReader and specifying a min amount. This way the entire buffer will be guaranteed to be filled, unless the stream ends.
constreader=(awaitDeno.open('path')).readable.getReader({mode: 'byob'})const{ done, value }=awaitreader.read(newUint8Array(100),{min: 100})// Remember to `reader.releaseLock()` or `reader.cancel()` when finished so the resource can be cleaned up.
Yes, but ReadableStreamBYOBReader doesn't support seeking so it's not relevant if you want to perform random access reads from a file without reading the whole thing into a buffer.
Is your feature request related to a problem? Please describe.
Currently, Deno provides
Deno.readFile()
, which reads an entire file into a buffer, andDeno.FsFile.read()
, which reads a portion of a file into a buffer. However,Deno.FsFile.read()
may return before reading the full requested number of bytes. This creates a challenge when attempting to reliably read exactly a specified number of bytes, requiring users to write custom logic to ensure they handle partial reads correctly.Describe the solution you'd like
I propose adding a utility function to the standard library that abstracts away the complexity of handling partial reads, ensuring that the desired number of bytes is read into a buffer. This function might behave similarly to the following code:
This function would ensure that the buffer is filled (if possible) or gracefully handle end-of-file conditions without requiring developers to write custom loops.
Describe alternatives you've considered
Alternatives include manually implementing this logic in every project, as illustrated above. However, this approach leads to code duplication and potential bugs across codebases.
The text was updated successfully, but these errors were encountered: