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-09-05 07:50:48 -04:00
|
|
|
ops.op_write_file_sync(
|
|
|
|
pathFromURL(path),
|
|
|
|
options.mode,
|
|
|
|
options.append ?? false,
|
|
|
|
options.create ?? true,
|
2022-12-12 23:12:49 -05:00
|
|
|
options.createNew ?? false,
|
2022-04-18 18:00:14 -04:00
|
|
|
data,
|
2022-09-05 07:50:48 -04:00
|
|
|
);
|
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-09-05 07:50:48 -04:00
|
|
|
await core.opAsync(
|
|
|
|
"op_write_file_async",
|
|
|
|
pathFromURL(path),
|
|
|
|
options.mode,
|
|
|
|
options.append ?? false,
|
|
|
|
options.create ?? true,
|
2022-12-12 23:12:49 -05:00
|
|
|
options.createNew ?? false,
|
2022-04-18 18:00:14 -04:00
|
|
|
data,
|
|
|
|
cancelRid,
|
2022-09-05 07:50:48 -04:00
|
|
|
);
|
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);
|