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) => {
|
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;
|
2023-01-11 21:37:23 -05:00
|
|
|
const { open } = window.__bootstrap.files;
|
|
|
|
const { ReadableStreamPrototype } = window.__bootstrap.streams;
|
|
|
|
const { ObjectPrototypeIsPrototypeOf } = window.__bootstrap.primordials;
|
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 {
|
2023-01-11 21:37:23 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, data)) {
|
|
|
|
const file = await open(path, {
|
|
|
|
mode: options.mode,
|
|
|
|
append: options.append ?? false,
|
|
|
|
create: options.create ?? true,
|
|
|
|
createNew: options.createNew ?? false,
|
|
|
|
write: true,
|
|
|
|
});
|
|
|
|
await data.pipeTo(file.writable, {
|
|
|
|
signal: options.signal,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await core.opAsync(
|
|
|
|
"op_write_file_async",
|
|
|
|
pathFromURL(path),
|
|
|
|
options.mode,
|
|
|
|
options.append ?? false,
|
|
|
|
options.create ?? true,
|
|
|
|
options.createNew ?? false,
|
|
|
|
data,
|
|
|
|
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 = {},
|
|
|
|
) {
|
2023-01-11 21:37:23 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, data)) {
|
|
|
|
return writeFile(
|
|
|
|
path,
|
|
|
|
data.pipeThrough(new TextEncoderStream()),
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
return writeFile(path, encoder.encode(data), options);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.writeFile = {
|
|
|
|
writeTextFile,
|
|
|
|
writeTextFileSync,
|
|
|
|
writeFile,
|
|
|
|
writeFileSync,
|
|
|
|
};
|
|
|
|
})(this);
|