File tree Expand file tree Collapse file tree 1 file changed +12
-4
lines changed
Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Original file line number Diff line number Diff 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 > >
You can’t perform that action at this time.
0 commit comments