Using `Deno.FsFile.prototype.readable` with `Response`

I was trying to stream a file into a response with the snippet:
const file = await Deno.open(path);
const resp = new Response(file.readable);
file.close();
return resp;
const file = await Deno.open(path);
const resp = new Response(file.readable);
file.close();
return resp;
but it throws a BadResource error. This makes sense, because closing the file would destroy the associated stream, but if I have to close the file at some point, how can I put it in a Response? afaict any way I do it will either error or leave a leak. I guess I could add a finalizer for the file but that seems silly.
csjh
csjh•18d ago
fwiw new Response(createReadStream(path)) also works but I would assume the runtime-specific implementation is better in some way oh I guess I could just fetch(path) i forgot file:// is supported
Deno AI Helper
Deno AI Helper•18d ago
You're correct that closing the file before the response is fully sent would result in a BadResource error, as the stream would no longer be valid. The key here is to ensure that the file is closed only after the response has been fully sent and not before. According to the documentation, when you use file.readable as the body of a Response, Deno takes care of closing the file handle automatically once the response is done sending. This means you don't need to manually close the file handle in your code. Here's the example from the documentation:
async function handler(_req) {
// Open the README.md file available at the root of the repository.
const file = await Deno.open("./README.md");

// Use the `readable` property, which is a `ReadableStream`. This will
// automatically close the file handle when the response is done sending.
return new Response(file.readable);
}

Deno.serve(handler);
async function handler(_req) {
// Open the README.md file available at the root of the repository.
const file = await Deno.open("./README.md");

// Use the `readable` property, which is a `ReadableStream`. This will
// automatically close the file handle when the response is done sending.
return new Response(file.readable);
}

Deno.serve(handler);
In this example, the file.readable is passed directly to the Response constructor, and there is no explicit call to file.close(). The Deno runtime ensures that the file is closed after the stream is consumed. So, in your case, you should be able to simply return the Response with the file.readable stream and not worry about closing the file handle manually. Deno will handle the closing of the file for you once the response has been fully sent. 🦕
csjh
csjh•18d ago
oop I'm silly
Doctor 🤖
Doctor 🤖•17d ago
Good bot On a side note, if Deno didn't handle auto closing after the readable stream was finished being consumed you could detect that it's done via piping through a transform stream calling file.close() when the flush function is called.
Esente
Esente•17d ago
That's the same way I would do. But I think using will become a thing soon, and the FileFS will support that.
Doctor 🤖
Doctor 🤖•17d ago
Using wouldn't work in this context as the dispose function would be called when the object scope ends. Which is the same as calling file.close after placing it in the response and not after it's done sending.
More Posts
react-helmet-async missing HelmetProvider exportI'm on the canary version testing out jsxImportSourceTypes and ran into an issue when switching to uWhere has std/hash/sha3.ts gone?Basically what the title says. This was removed from std/ at some point, but I can't find the currenLoading a module with `.load_main_es_module_from_code()` and TS module loader doesn't transpileWhile working on a script runtime I ran into a curious issue where the main module I load doesn't geStaticModuleLoader: why must the ModuleSpecifier be a URL?hey everyone, I'm trying to pass a `StaticModuleLoader` to my runtime to enable specific imports. EaWriting CSS in FreshAre there any alternatives in how we write CSS other than Tailwind-like (inline & abbreviated)? I pPassing C enum to function and read in TypeScriptFirst of all I am not a trained software developper, so please forgive me if I am missing some basicintergrating Flowbite with freshI want to intergrate Flowbite with Fresh. Any idea on the best way to go about this? And will I be lget file path from FsFilegiven a `Deno.FsFile`, is there a way to get its path (or at least a filename)? i can't really see bulr to rtransparent gradientI am working on reactjs project in the project it should have blur to transperant bg take a look at Streaming video from browser to deno serverTrying to stream video from Chrome to my local Deno server but having trouble understanding how to pMigrating from Deno.run to Deno.command, how to readLines?At the moment I have this: ```ts const command = new Deno.command('...'); for await (const line of rHow to abstract tests to allow running different functions against the same test suite?I've set up two suites of tests like this: https://github.com/gustwindjs/gustwind/tree/develop/htmliFresh tabler icon classHello I want to use the fresh tabler icon lib without using the npm version like this: ```ts { "iRedirection in Testing is Always Valued FalseThis is my first test in full stack framework, especially for Fresh. I'm not sure why the `redirecteNeovim setup with LSP formatting & lintinghello, I am trying to set up the fmt & linting using nvim-lspconfig with neovim Anyone know how i sblock js with #[op2] fn without blocking rust threadI'm using deno_core and I'm looking for a way to block the js main thread without using a sync functConnect to managed databases from "INSIDE" of Deno DeployHi I am trying to connect to a managed database from INSIDE another application in deno Deploy. But something wrong with deno i thinkidk but sh1t wont work?