2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2021-06-05 17:10:07 -04:00
|
|
|
const core = window.Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2022-04-27 10:03:44 -04:00
|
|
|
const { pathFromURL } = window.__bootstrap.util;
|
|
|
|
const { abortSignal } = window.__bootstrap;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
function readFileSync(path) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_readfile_sync(pathFromURL(path));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-06-22 11:45:26 -04:00
|
|
|
async function readFile(path, options) {
|
2022-04-27 10:03:44 -04:00
|
|
|
let cancelRid;
|
|
|
|
let abortHandler;
|
|
|
|
if (options?.signal) {
|
|
|
|
options.signal.throwIfAborted();
|
2022-08-11 09:56:56 -04:00
|
|
|
cancelRid = ops.op_cancel_handle();
|
2022-04-27 10:03:44 -04:00
|
|
|
abortHandler = () => core.tryClose(cancelRid);
|
|
|
|
options.signal[abortSignal.add](abortHandler);
|
|
|
|
}
|
|
|
|
|
2021-04-08 10:36:52 -04:00
|
|
|
try {
|
2022-04-27 10:03:44 -04:00
|
|
|
const read = await core.opAsync(
|
|
|
|
"op_readfile_async",
|
|
|
|
pathFromURL(path),
|
|
|
|
cancelRid,
|
|
|
|
);
|
|
|
|
return read;
|
2021-04-08 10:36:52 -04:00
|
|
|
} finally {
|
2022-04-27 10:03:44 -04:00
|
|
|
if (options?.signal) {
|
|
|
|
options.signal[abortSignal.remove](abortHandler);
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
function readTextFileSync(path) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_readfile_text_sync(pathFromURL(path));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-06-22 11:45:26 -04:00
|
|
|
async function readTextFile(path, options) {
|
2022-04-27 10:03:44 -04:00
|
|
|
let cancelRid;
|
|
|
|
let abortHandler;
|
|
|
|
if (options?.signal) {
|
|
|
|
options.signal.throwIfAborted();
|
2022-08-11 09:56:56 -04:00
|
|
|
cancelRid = ops.op_cancel_handle();
|
2022-04-27 10:03:44 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.readFile = {
|
|
|
|
readFile,
|
|
|
|
readFileSync,
|
|
|
|
readTextFileSync,
|
|
|
|
readTextFile,
|
|
|
|
};
|
|
|
|
})(this);
|