diff options
author | Satya Rohith <me@satyarohith.com> | 2021-04-08 20:06:52 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-08 16:36:52 +0200 |
commit | 543080de559d997d5ff911e8a8a1625d51f2e0bd (patch) | |
tree | de2389edd09925dd55911d2eb90ca2fdae24cc0e /runtime/js | |
parent | c867c1aa476b3f00933be08cbc5f528973e37857 (diff) |
fix(runtime/readFile*): close resources on error during read (#10059)
This commit ensures readFile, readFileSync, readTextFile,
and readTextFileSync does not leak resources on error.
Diffstat (limited to 'runtime/js')
-rw-r--r-- | runtime/js/40_read_file.js | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/runtime/js/40_read_file.js b/runtime/js/40_read_file.js index 0ca8f56e9..1efd5338d 100644 --- a/runtime/js/40_read_file.js +++ b/runtime/js/40_read_file.js @@ -7,32 +7,44 @@ function readFileSync(path) { const file = openSync(path); - const contents = readAllSync(file); - file.close(); - return contents; + try { + const contents = readAllSync(file); + return contents; + } finally { + file.close(); + } } async function readFile(path) { const file = await open(path); - const contents = await readAll(file); - file.close(); - return contents; + try { + const contents = await readAll(file); + return contents; + } finally { + file.close(); + } } function readTextFileSync(path) { const file = openSync(path); - const contents = readAllSync(file); - file.close(); - const decoder = new TextDecoder(); - return decoder.decode(contents); + try { + const contents = readAllSync(file); + const decoder = new TextDecoder(); + return decoder.decode(contents); + } finally { + file.close(); + } } async function readTextFile(path) { const file = await open(path); - const contents = await readAll(file); - file.close(); - const decoder = new TextDecoder(); - return decoder.decode(contents); + try { + const contents = await readAll(file); + const decoder = new TextDecoder(); + return decoder.decode(contents); + } finally { + file.close(); + } } window.__bootstrap.readFile = { |