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.)
DAH
Deno AI Helper22d 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.
D
Doctor 🤖22d 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'))
C
CodyC21d 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.
D
Doctor 🤖21d ago
What makes you think the contents in the blob aren't all stored in memory?
C
CodyC21d 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.
C
CodyC21d 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 ...
D
Doctor 🤖21d 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.
C
CodyC21d 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.
D
Doctor 🤖21d ago
I see. I didn't read the second link until now
More Posts
Deno deploy running my code multiple timesIm hosting a small discord bot on deno deploy but for some reason its firing the events multiple timvalue too large in kv (30kb)I'm getting a value too large error when setting a value in KV (on deploy) even though the value is How to work without a tsconfig.json in VSCode? Getting typescript errors due to React.``` Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compilupdate deno version on Alpineon alpine the version of deno is 1.38.1 https://pkgs.alpinelinux.org/packages?name=deno is there a rDeno blog deployment failureHi all, recently tried to make a new blog post on personal blog, however now the deployment fails (nFresh 1.6 - Deno Deploy - TailwindDeploying my project on deno deploy is not working. Does anyone have the same issue ? I am using taAnalyze dependencies for deno compileIs there a way to analyze the dependencies that is added when running deno compile? My compiled binaHow to install jupyter using deno v1.41.xAccording to the documentation https://docs.deno.com/runtime/manual/tools/jupyter the command shouldHelp with TypeScript namespaces - is this pattern supported?Hi! I am attempting to model some classes in a Namespace for organization purposes. I would expect tfont regenerator for copying restriction purposes on DenoI am developing a web reader PWA primarily for reading CJK character text. If I do not want users examples of Deno+LlamacppAre there examples of Deno and Ollama/Llamacpp setups in typescript? Its should t be hard since itIs there a way to bundle multiple files into one file using `esbuild_deno_loader` and `esbuild`?I'm trying to bundle multiple files in a single folder into one JS file that needs to end up in the Deno.stdin raw map?Does anyone have a map to which key is what raw buffer gotten from `Deno.stdin`? Like i can just seaHave the Deno deploy templates disappeared?Good morning, I can't find https://dash.deno.com/new from the account dashboard panel. Are the templIPC (ZeroMQ)I want to create a service with Deno that should be callable by other (non-deno) processes running oRead child process I/OI want to write a simple Deno CLI script using `Deno.Command` or `dx` that can read output from a shCannot find name 'Deno'.ts(2304)Hello, I am using VS Code and Typescript and I have this error. I have downloaded the latest extenDeno Fresh: TypeError: 'import', and 'export' cannot be outside of moduleDeno Fresh: ```tsx // @deno-types="npm:@arcgis/core/interfaces.d.ts" import config from "npm:@arcgisEcosystem: @arcgis/corehttps://github.com/Esri/jsapi-resources/tree/main/esm-samples/jsapi-deno I've found an example imporKill Deno.CommandIs it possible to kill/end a `Deno.command`-instance? Example code: ```ts (async () => { let ffm