Skip to content

Commit 32a9e2f

Browse files
authored
Potential helper to fix EBADF errors (#10)
1 parent 52134a9 commit 32a9e2f

File tree

3 files changed

+728
-699
lines changed

3 files changed

+728
-699
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@
4949
"genomics"
5050
],
5151
"devDependencies": {
52-
"@types/node": "^22.15.3",
52+
"@types/node": "^24.3.1",
5353
"@types/range-parser": "^1.2.7",
5454
"@vitest/coverage-v8": "^3.0.1",
5555
"eslint": "^9.16.0",
5656
"eslint-plugin-import": "^2.31.0",
57-
"eslint-plugin-unicorn": "^59.0.0",
57+
"eslint-plugin-unicorn": "^60.0.0",
5858
"prettier": "^3.4.1",
5959
"range-parser": "^1.2.1",
6060
"rimraf": "^6.0.0",

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)