summaryrefslogtreecommitdiff
path: root/runtime/js/40_read_file.js
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/js/40_read_file.js')
-rw-r--r--runtime/js/40_read_file.js73
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 = {