CodyC
CodyCโ€ข8mo ago

How to open a file as blob?

Docs for Blob (https://deno.land/api@v1.42.0?s=Blob) say that File implements Blob. But the docs for Deno.File (https://deno.land/api@v1.42.0?s=Deno.File) say that it's deprecated and you should use FsFile instead. But FsFile (https://deno.land/api@v1.42.0?s=Deno.FsFile) doesn't implement Blob. I could probably write my own wrapper that will make FsFile conform to Blob, but that feels like something that might already be in std somewhere? (update: the important part here is that I want to wrap a file-like object, not read the entire file's contents into memory, since I'm working with potentially large files.)
9 Replies
Deno AI Helper
Deno AI Helperโ€ข8mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the information you've provided, it seems like you're looking for a way to work with files as Blob objects in Deno. While I cannot provide a solution based on the official documentation, I can suggest that if you need to create a Blob from a file, you might consider reading the file into an Uint8Array and then using that to create a Blob object. Here's a general idea of how you might do it:
const data = await Deno.readFile("path/to/your/file");
const blob = new Blob([data]);
const data = await Deno.readFile("path/to/your/file");
const blob = new Blob([data]);
Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of this approach. If you're looking for a more integrated solution or if this doesn't meet your requirements, you might want to explore the Deno standard library or third-party modules to see if someone has already created a wrapper or utility for this purpose.
Doctor ๐Ÿค–
Doctor ๐Ÿค–โ€ข8mo ago
The simplest way I can think of is to pass it through a response instance.
const blob = await new Response(readable).blob()
const blob = await new Response(readable).blob()
That's if you're using Deno.FsFile which is returned from Deno.open. Deno.readFile returns a Uint8Array, and you can just place that Uint8Array into a Blob constructor. new Blob(await Deno.readFile('path'))
CodyC
CodyCOPโ€ข8mo ago
Yep. I should've stated that I was looking for a solution that didn't require reading the entire file into memory, because that's trivially easy, and I'm looking to use the Blob API so that I don't have to hold the whole thing in memory. ๐Ÿ˜… The Blob API supports asynchronously reading slices of the underlying file with a very nice API: * any Blob can synchronously get a blob.slice() which itself is a Blob. * Only when reading the bytes is async required, to wait for the bytes from the underlying store. NBD, I'm writing a wrapper for it. Maybe I'll publish it to jsr when I'm satisfied with it.
Doctor ๐Ÿค–
Doctor ๐Ÿค–โ€ข8mo ago
What makes you think the contents in the blob aren't all stored in memory?
CodyC
CodyCOPโ€ข8mo ago
https://developer.mozilla.org/en-US/docs/Web/API/Blob
Blobs can represent data that isn't necessarily in a JavaScript-native format. The File interface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system.
Plus its API. You wouldn't need arrayBuffer() to return a Promise if it weren't potentially going to do I/O. It can be backed by in-memory data, but the API does not reqiure it.
CodyC
CodyCOPโ€ข8mo ago
see this conversation for more details: https://github.com/denoland/deno/discussions/10539
GitHub
Blob and Files ยท denoland deno ยท Discussion #10539
Hi, I'm coming from the NodeJS world and help maintain node-fetch and fetch-blob I have also helped and reviewed NodeJS buffer.Blob implementations so it follow the spec more correctly. NodeJS ...
Doctor ๐Ÿค–
Doctor ๐Ÿค–โ€ข8mo ago
Not being in a native JavaScript format doesn't mean not being in JavaScript memory. The File interface isn't supported in Deno so I'd guess your next best bet would be to learn how to use streams instead or maybe use the seek method if you plan on jumping all over the file. I wouldn't say simply that a conversion type returning a promise means there are underlying I/O operations. Just that whatever is happening goes out of the JavaScript context to happen and then comes back in. For example. How does the blob know its size if it hasn't pulled all the data in? Looking at the mdn docs I don't get the impression that the contents of the blob are anything but in memory.
CodyC
CodyCOPโ€ข8mo ago
Having worked with Blobs in browsers, I can tell you that they're not in memory. (also corroberated by the last link I shared.) anyway, This thread isn't to argue about that. My question is whether there's a way to get a file as a Blob in Deno (without just reading it into memory). Seems like there's not, so I'll write my own.
Doctor ๐Ÿค–
Doctor ๐Ÿค–โ€ข8mo ago
I see. I didn't read the second link until now