You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Deno中FsFile.readable流耗尽时是否自动关闭文件?

Deno中FsFile.readable流耗尽时是否自动关闭文件?

先看你给出的代码:

const file = await Deno.open(fsPathname, { read: true })
return new Response(file.readable, { status: 200 })

答案:不会自动关闭文件,这点当前Deno官方文档确实未明确提及。

如果不想做额外复杂操作,有两种简洁的解决方式:

  1. 利用流的回调自动关闭:
const file = await Deno.open(fsPathname, { read: true });
const res = new Response(file.readable, { status: 200 });
// 流结束或取消时关闭文件
res.body?.cancel().finally(() => file.close());
return res;
  1. 使用Deno 1.28+支持的using声明(推荐):
await using file = await Deno.open(fsPathname, { read: true });
return new Response(file.readable, { status: 200 });

using会在代码块执行完毕后自动调用file.close(),无需手动处理,语法更简洁。

内容的提问来源于stack exchange,提问作者Chris_F

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 10:09:52