2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-05 03:48:32 +05:30
|
|
|
"use strict";
|
2020-07-19 19:49:44 +02:00
|
|
|
|
|
|
|
((window) => {
|
2021-06-05 23:10:07 +02:00
|
|
|
const core = window.Deno.core;
|
2022-04-27 07:03:44 -07:00
|
|
|
const { pathFromURL } = window.__bootstrap.util;
|
|
|
|
const { abortSignal } = window.__bootstrap;
|
2020-07-19 19:49:44 +02:00
|
|
|
|
|
|
|
function readFileSync(path) {
|
2022-04-27 07:03:44 -07:00
|
|
|
return core.opSync("op_readfile_sync", pathFromURL(path));
|
2020-07-19 19:49:44 +02:00
|
|
|
}
|
|
|
|
|
2021-06-22 18:45:26 +03:00
|
|
|
async function readFile(path, options) {
|
2022-04-27 07:03:44 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-04-08 20:06:52 +05:30
|
|
|
try {
|
2022-04-27 07:03:44 -07:00
|
|
|
const read = await core.opAsync(
|
|
|
|
"op_readfile_async",
|
|
|
|
pathFromURL(path),
|
|
|
|
cancelRid,
|
|
|
|
);
|
|
|
|
return read;
|
2021-04-08 20:06:52 +05:30
|
|
|
} finally {
|
2022-04-27 07:03:44 -07:00
|
|
|
if (options?.signal) {
|
|
|
|
options.signal[abortSignal.remove](abortHandler);
|
|
|
|
|
|
|
|
// always throw the abort error when aborted
|
|
|
|
options.signal.throwIfAborted();
|
|
|
|
}
|
2021-04-08 20:06:52 +05:30
|
|
|
}
|
2020-07-19 19:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function readTextFileSync(path) {
|
2022-04-27 07:03:44 -07:00
|
|
|
return core.opSync("op_readfile_text_sync", pathFromURL(path));
|
2020-07-19 19:49:44 +02:00
|
|
|
}
|
|
|
|
|
2021-06-22 18:45:26 +03:00
|
|
|
async function readTextFile(path, options) {
|
2022-04-27 07:03:44 -07:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 19:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.readFile = {
|
|
|
|
readFile,
|
|
|
|
readFileSync,
|
|
|
|
readTextFileSync,
|
|
|
|
readTextFile,
|
|
|
|
};
|
|
|
|
})(this);
|