Skip to content

Commit a833c05

Browse files
committed
Localfile close
1 parent b9249c5 commit a833c05

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/localFile.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ export default class LocalFile implements GenericFilehandle {
1111

1212
public async read(length: number, position = 0) {
1313
const arr = new Uint8Array(length)
14-
const fd = await open(this.filename, 'r')
15-
const res = await fd.read(arr, 0, length, position)
16-
await fd.close()
17-
return res.buffer.subarray(0, res.bytesRead)
14+
let fd // Declare fd outside the try block so it's accessible in finally
15+
try {
16+
fd = await open(this.filename, 'r')
17+
const res = await fd.read(arr, 0, length, position)
18+
return res.buffer.subarray(0, res.bytesRead)
19+
} finally {
20+
// This block will always execute, regardless of success or error
21+
if (fd) {
22+
// Only close if the fd was successfully opened
23+
await fd.close()
24+
}
25+
}
1826
}
1927

2028
public async readFile(): Promise<Uint8Array<ArrayBuffer>>

0 commit comments

Comments
 (0)