Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading from a file seems rather unlike existing prior art like fetch? #433

Open
benlesh opened this issue Nov 29, 2023 · 1 comment
Open

Comments

@benlesh
Copy link
Contributor

benlesh commented Nov 29, 2023

While I understand that fetch generally has fewer ways of getting http responses than something dealing with opening and reading files, this API -- with regards to reading files in particular -- is surprisingly divergent, IMO. I personally found the initial example to be a bit hard to follow, where I was bouncing around inside the code to figure out what was being read, what happens when it's read, and what's initializing the read.

I guess I would have expected something like fetch and how you read the body:

const response = await fetch('/some/image.png');

if (!response.ok) {
  throw new Error('Not good');
}

const reader = await response.body.getReader();

for await (const data of reader) {
  // do things with binary data here.
}

Where a file read that aligns more with the existing API might look like:

const files = await showOpenFilePicker(/* options */);

for (const file of files) {
  console.log(`Reading ${file.name}`);
  
  // Get a reader
  const fileReader = await file.content.getReader();
  
  // Read the binary data
  for await (const data of fileReader) {
    // do something with binary data here
  }
}
@a-sully
Copy link
Collaborator

a-sully commented Nov 30, 2023

One thing I'll mention before addressing your feedback...

I personally found the initial example to be a bit hard to follow, where I was bouncing around inside the code to figure out what was being read, what happens when it's read, and what's initializing the read.

The getFile() method is specified over in https://github.com/whatwg/fs (here). So

  • if things are hard to follow.... yeah sorry about that. I wish things weren't split across two specs, too
  • if you have feedback about that method in particular, please consider filing an issue on that spec (and we can close this one)

While I understand that fetch generally has fewer ways of getting http responses than something dealing with opening and reading files, this API -- with regards to reading files in particular -- is surprisingly divergent, IMO.

The prior art this API builds on is FileSystemFileEntry.file() which returns (via a callback, since it predates promises) a File. File extends Blob, from which you can (among other things) get a ReadableStream (which is what response.body gets you)

const readableStream = (await fileHandle.getFile()).stream()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@benlesh @a-sully and others