2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
|
|
|
const ops = core.ops;
|
|
|
|
import { pathFromURL } from "internal:runtime/js/06_util.js";
|
2023-02-07 16:09:50 -05:00
|
|
|
import * as abortSignal from "internal:deno_web/03_abort_signal.js";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function readFileSync(path) {
|
|
|
|
return ops.op_readfile_sync(pathFromURL(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function readFile(path, options) {
|
|
|
|
let cancelRid;
|
|
|
|
let abortHandler;
|
|
|
|
if (options?.signal) {
|
|
|
|
options.signal.throwIfAborted();
|
|
|
|
cancelRid = ops.op_cancel_handle();
|
|
|
|
abortHandler = () => core.tryClose(cancelRid);
|
|
|
|
options.signal[abortSignal.add](abortHandler);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
|
|
|
const read = await core.opAsync(
|
|
|
|
"op_readfile_async",
|
|
|
|
pathFromURL(path),
|
|
|
|
cancelRid,
|
|
|
|
);
|
|
|
|
return read;
|
|
|
|
} finally {
|
2022-04-27 10:03:44 -04:00
|
|
|
if (options?.signal) {
|
2023-02-07 14:22:46 -05:00
|
|
|
options.signal[abortSignal.remove](abortHandler);
|
2022-04-27 10:03:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// always throw the abort error when aborted
|
|
|
|
options.signal.throwIfAborted();
|
2021-04-08 10:36:52 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function readTextFileSync(path) {
|
|
|
|
return ops.op_readfile_text_sync(pathFromURL(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function readTextFile(path, options) {
|
|
|
|
let cancelRid;
|
|
|
|
let abortHandler;
|
|
|
|
if (options?.signal) {
|
|
|
|
options.signal.throwIfAborted();
|
|
|
|
cancelRid = ops.op_cancel_handle();
|
|
|
|
abortHandler = () => core.tryClose(cancelRid);
|
|
|
|
options.signal[abortSignal.add](abortHandler);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
|
|
|
const read = await core.opAsync(
|
|
|
|
"op_readfile_text_async",
|
|
|
|
pathFromURL(path),
|
|
|
|
cancelRid,
|
|
|
|
);
|
|
|
|
return read;
|
|
|
|
} finally {
|
2022-04-27 10:03:44 -04:00
|
|
|
if (options?.signal) {
|
2023-02-07 14:22:46 -05:00
|
|
|
options.signal[abortSignal.remove](abortHandler);
|
2022-04-27 10:03:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// always throw the abort error when aborted
|
|
|
|
options.signal.throwIfAborted();
|
2022-04-27 10:03:44 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { readFile, readFileSync, readTextFile, readTextFileSync };
|