diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2022-04-27 07:03:44 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-27 16:03:44 +0200 |
commit | 8b8b21b553fba03124934363e77e636adaeb4745 (patch) | |
tree | dc21d5b6df8dbbd9f38980e00d76dcfec5921863 /runtime/js/40_read_file.js | |
parent | 9853c96cc4686a6cd1ffa1e9081c012b8df72ff7 (diff) |
perf(runtime): read entire files in single ops (#14261)
Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
Diffstat (limited to 'runtime/js/40_read_file.js')
-rw-r--r-- | runtime/js/40_read_file.js | 73 |
1 files changed, 49 insertions, 24 deletions
diff --git a/runtime/js/40_read_file.js b/runtime/js/40_read_file.js index 5862454db..8a52e4f70 100644 --- a/runtime/js/40_read_file.js +++ b/runtime/js/40_read_file.js @@ -3,44 +3,69 @@ ((window) => { const core = window.Deno.core; - const { open, openSync } = window.__bootstrap.files; - const { readAllSync, readAll, readAllSyncSized, readAllInnerSized } = - window.__bootstrap.io; + const { pathFromURL } = window.__bootstrap.util; + const { abortSignal } = window.__bootstrap; function readFileSync(path) { - const file = openSync(path); - try { - const { size } = file.statSync(); - if (size === 0) { - return readAllSync(file); - } else { - return readAllSyncSized(file, size); - } - } finally { - file.close(); - } + return core.opSync("op_readfile_sync", pathFromURL(path)); } async function readFile(path, options) { - const file = await open(path); + let cancelRid; + let abortHandler; + if (options?.signal) { + options.signal.throwIfAborted(); + cancelRid = core.opSync("op_cancel_handle"); + abortHandler = () => core.tryClose(cancelRid); + options.signal[abortSignal.add](abortHandler); + } + try { - const { size } = await file.stat(); - if (size === 0) { - return await readAll(file); - } else { - return await readAllInnerSized(file, size, options); - } + const read = await core.opAsync( + "op_readfile_async", + pathFromURL(path), + cancelRid, + ); + return read; } finally { - file.close(); + if (options?.signal) { + options.signal[abortSignal.remove](abortHandler); + + // always throw the abort error when aborted + options.signal.throwIfAborted(); + } } } function readTextFileSync(path) { - return core.decode(readFileSync(path)); + return core.opSync("op_readfile_text_sync", pathFromURL(path)); } async function readTextFile(path, options) { - return core.decode(await readFile(path, options)); + let cancelRid; + let abortHandler; + if (options?.signal) { + options.signal.throwIfAborted(); + cancelRid = core.opSync("op_cancel_handle"); + abortHandler = () => core.tryClose(cancelRid); + options.signal[abortSignal.add](abortHandler); + } + + try { + const read = await core.opAsync( + "op_readfile_text_async", + pathFromURL(path), + cancelRid, + ); + return read; + } finally { + if (options?.signal) { + options.signal[abortSignal.remove](abortHandler); + + // always throw the abort error when aborted + options.signal.throwIfAborted(); + } + } } window.__bootstrap.readFile = { |