2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 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) => {
|
2022-04-18 18:00:14 -04:00
|
|
|
const core = window.__bootstrap.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2022-04-18 18:00:14 -04:00
|
|
|
const { abortSignal } = window.__bootstrap;
|
|
|
|
const { pathFromURL } = window.__bootstrap.util;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
function writeFileSync(
|
|
|
|
path,
|
|
|
|
data,
|
|
|
|
options = {},
|
|
|
|
) {
|
2021-12-16 06:57:26 -05:00
|
|
|
options.signal?.throwIfAborted();
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_write_file_sync({
|
2022-04-18 18:00:14 -04:00
|
|
|
path: pathFromURL(path),
|
|
|
|
data,
|
|
|
|
mode: options.mode,
|
|
|
|
append: options.append ?? false,
|
|
|
|
create: options.create ?? true,
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function writeFile(
|
|
|
|
path,
|
|
|
|
data,
|
|
|
|
options = {},
|
|
|
|
) {
|
2022-04-18 18:00:14 -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-18 18:00:14 -04:00
|
|
|
abortHandler = () => core.tryClose(cancelRid);
|
|
|
|
options.signal[abortSignal.add](abortHandler);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-12-16 06:57:26 -05:00
|
|
|
try {
|
2022-04-18 18:00:14 -04:00
|
|
|
await core.opAsync("op_write_file_async", {
|
|
|
|
path: pathFromURL(path),
|
|
|
|
data,
|
|
|
|
mode: options.mode,
|
|
|
|
append: options.append ?? false,
|
|
|
|
create: options.create ?? true,
|
|
|
|
cancelRid,
|
|
|
|
});
|
2021-12-16 06:57:26 -05:00
|
|
|
} finally {
|
2022-04-18 18:00:14 -04:00
|
|
|
if (options.signal) {
|
|
|
|
options.signal[abortSignal.remove](abortHandler);
|
|
|
|
|
|
|
|
// always throw the abort error when aborted
|
|
|
|
options.signal.throwIfAborted();
|
|
|
|
}
|
2021-08-06 13:21:29 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function writeTextFileSync(
|
|
|
|
path,
|
|
|
|
data,
|
|
|
|
options = {},
|
|
|
|
) {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
return writeFileSync(path, encoder.encode(data), options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeTextFile(
|
|
|
|
path,
|
|
|
|
data,
|
|
|
|
options = {},
|
|
|
|
) {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
return writeFile(path, encoder.encode(data), options);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.writeFile = {
|
|
|
|
writeTextFile,
|
|
|
|
writeTextFileSync,
|
|
|
|
writeFile,
|
|
|
|
writeFileSync,
|
|
|
|
};
|
|
|
|
})(this);
|