Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close file on readAll exception #682

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export {
} from "jsr:@std/http@^1.0/negotiation";
export { UserAgent } from "jsr:@std/http@^1.0/user-agent";
export { LimitedReader } from "jsr:@std/io@0.224/limited-reader";
export { readAll } from "jsr:@std/io@0.224/read-all";
export { contentType } from "jsr:@std/media-types@^1.0/content-type";
export { typeByExtension } from "jsr:@std/media-types@^1.0/type-by-extension";
export {
Expand Down
6 changes: 2 additions & 4 deletions send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
ifNoneMatch,
parse,
range,
readAll,
responseRange,
Status,
} from "./deps.ts";
Expand Down Expand Up @@ -142,12 +141,11 @@ async function getEntity(
let body: Uint8Array | Deno.FsFile;
let entity: Uint8Array | FileInfo;
const fileInfo = { mtime: new Date(mtime), size: stats.size };
const file = await Deno.open(path, { read: true });
if (stats.size < maxbuffer) {
const buffer = await readAll(file);
file.close();
const buffer = await Deno.readFile(path);
Copy link
Contributor Author

@dsherret dsherret Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of handling closing the file on readAll, we can just use the readFile api directly and remove the readAll dependency (I think the code should also work faster or have the potential to be faster by doing this too)

body = entity = buffer;
} else {
const file = await Deno.open(path, { read: true });
response.addResource(file);
body = file;
entity = fileInfo;
Expand Down