D
Deno

help

Closing a file

Zzidan29998/23/2023
How do I close a file after opening and streaming it? My handler function code is as follows:
return new Promise(
(grant) => {
Deno.open(
asset.path
).then(
(file) => {
grant(
new Response(
file.readable,
{
status: 200,
headers: {
"content-type": asset.type,
"content-length": file.stat.size.toString()
}
}
)
)
}
)
}
);
return new Promise(
(grant) => {
Deno.open(
asset.path
).then(
(file) => {
grant(
new Response(
file.readable,
{
status: 200,
headers: {
"content-type": asset.type,
"content-length": file.stat.size.toString()
}
}
)
)
}
)
}
);
Is there a good way of closing the "file" after returning the "Response" object?
IioB8/23/2023
If you use .readable, it's closed after the whole body is read automatically no need to manually close it
Zzidan29998/23/2023
oh, that's convenient got it, thanks 👍 file.stat appears to be a function that returns a promise now.. is there a way to get the file size in the same .then() or do i have to chain another .then?
IioB8/23/2023
I would have personally written this like
return new Promise(
async (grant) => {
const file = await Deno.open(
asset.path
);
const stat = await file.stat();

grant(
new Response(
file.readable,
{
status: 200,
headers: {
"content-type": asset.type,
"content-length": file.stat.size.toString()
}
}
)
)
}
);
return new Promise(
async (grant) => {
const file = await Deno.open(
asset.path
);
const stat = await file.stat();

grant(
new Response(
file.readable,
{
status: 200,
headers: {
"content-type": asset.type,
"content-length": file.stat.size.toString()
}
}
)
)
}
);
async/await syntax is your friend!
Zzidan29998/23/2023
and then check only the file open for erros i think?
IioB8/23/2023
if you want to throw a better error, yes
Zzidan29998/23/2023
got it, thanks a lot

Looking for more? Join the community!

Recommended Posts
puppeteer makes script to hang for a while at the endI am using `npm:puppeteer` in my script. Everything is working fine. But often the script hangs at tIn Fresh, how do I return a JSON response in a middleware?In Fresh, how do I return a JSON response in a middleware?get length for KV list via prefix?Is there a fast way to get just the count of items for a prefix?Property 'params' does not exist on type 'Context<State, Record<string, any>> ... what to do?I want to access params in oak, but typescript complains. What can I do about that? Thanks.What is the std library for making a guessing game like shown in rust?```rust use std::io; fn main() { println!("Guess the number!"); println!("Please input youData must be valid JSONA simple project, after successful deployment, reports an error "Data must be valid JSON".FFI deno_bindgen, how to see build logs?Hi, using deno_bindgen for rust, is there anyway i can view the build logs? --verbose or -v do not sTypeError: str.split is not a functionHey Gang. Im following along this tutorial: https://deno.com/blog/build-a-blog-with-fresh But I am Can't sign up/sign in to Deno DeployI'm tired of this error. Does anybody know the reason or at least how to solve it?CSP Middleware for OAK?Just wondering what the best module is for this? I noticed SNELM but that hasn't been updated in abExternal dependencies with npm specifier in combination with vite does not work.Hi, I am new to Deno and want to build an app with deno, vite and react. Unfortunately the dependencQuestion about for loopsWhy does the following code log numbers from 0 to 9 instead of 1 to 9? `i` is assigned as zero in thDockerfile saaskit deploymentI created an entry in `deno.json` starting a script named `db:init` to initialize my KV DB with an uDeno TypesWhat is the Deno.ServeHandler type? ```No overload matches this call. Overload 1 of 3, '(handler: I use Deno KV for my application, and I'm planning to use Deno Deploy. Can I allow deploy to use...I use Deno KV for my application, and I'm planning to use Deno Deploy. Can I allow deploy to use KVbad resource id with node:crypto```ts function test() { const key = randomBytes(32); const iv = randomBytes(16); const cipher Close a Deno.serve() connection?Is it possible to close a Deno.serve() connection?Relative import path "preact" not prefixed with / or ./ or ../ at https://deno.land/x/fresh@1.4.Please help. ``` deno 1.36.1 (release, aarch64-apple-darwin) v8 11.6.189.12 typescript 5.1.6 ```Problems with Deno KV```ts import InputLoop from "https://deno.land/x/input@2.0.3/index.ts"; const kv = await Deno.openKFile differs only by case errorI'm getting a strange error from TypeScript. For some reason it thinks the case of my file name is