2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 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) => {
|
|
|
|
const { stat, statSync, chmod, chmodSync } = window.__bootstrap.fs;
|
|
|
|
const { open, openSync } = window.__bootstrap.files;
|
|
|
|
const { build } = window.__bootstrap.build;
|
2021-07-03 18:17:52 -04:00
|
|
|
const {
|
|
|
|
TypedArrayPrototypeSubarray,
|
|
|
|
} = window.__bootstrap.primordials;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
function writeFileSync(
|
|
|
|
path,
|
|
|
|
data,
|
|
|
|
options = {},
|
|
|
|
) {
|
2021-08-06 13:21:29 -04:00
|
|
|
if (options?.signal?.aborted) {
|
|
|
|
throw new DOMException("The write operation was aborted.", "AbortError");
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
if (options.create !== undefined) {
|
|
|
|
const create = !!options.create;
|
|
|
|
if (!create) {
|
|
|
|
// verify that file exists
|
|
|
|
statSync(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 10:19:29 -05:00
|
|
|
const openOptions = options.append
|
2020-07-19 13:49:44 -04:00
|
|
|
? { write: true, create: true, append: true }
|
|
|
|
: { write: true, create: true, truncate: true };
|
|
|
|
const file = openSync(path, openOptions);
|
|
|
|
|
|
|
|
if (
|
|
|
|
options.mode !== undefined &&
|
|
|
|
options.mode !== null &&
|
|
|
|
build.os !== "windows"
|
|
|
|
) {
|
|
|
|
chmodSync(path, options.mode);
|
|
|
|
}
|
|
|
|
|
2021-04-05 18:05:36 -04:00
|
|
|
let nwritten = 0;
|
|
|
|
while (nwritten < data.length) {
|
2021-07-03 18:17:52 -04:00
|
|
|
nwritten += file.writeSync(TypedArrayPrototypeSubarray(data, nwritten));
|
2021-04-05 18:05:36 -04:00
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function writeFile(
|
|
|
|
path,
|
|
|
|
data,
|
|
|
|
options = {},
|
|
|
|
) {
|
|
|
|
if (options.create !== undefined) {
|
|
|
|
const create = !!options.create;
|
|
|
|
if (!create) {
|
|
|
|
// verify that file exists
|
|
|
|
await stat(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 10:19:29 -05:00
|
|
|
const openOptions = options.append
|
2020-07-19 13:49:44 -04:00
|
|
|
? { write: true, create: true, append: true }
|
|
|
|
: { write: true, create: true, truncate: true };
|
|
|
|
const file = await open(path, openOptions);
|
|
|
|
|
|
|
|
if (
|
|
|
|
options.mode !== undefined &&
|
|
|
|
options.mode !== null &&
|
|
|
|
build.os !== "windows"
|
|
|
|
) {
|
|
|
|
await chmod(path, options.mode);
|
|
|
|
}
|
|
|
|
|
2021-08-06 13:21:29 -04:00
|
|
|
const signal = options?.signal ?? null;
|
2021-04-05 18:05:36 -04:00
|
|
|
let nwritten = 0;
|
2021-08-06 13:21:29 -04:00
|
|
|
while (!signal?.aborted && nwritten < data.length) {
|
2021-07-03 18:17:52 -04:00
|
|
|
nwritten += await file.write(TypedArrayPrototypeSubarray(data, nwritten));
|
2021-04-05 18:05:36 -04:00
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
file.close();
|
2021-08-06 13:21:29 -04:00
|
|
|
|
|
|
|
if (signal?.aborted) {
|
|
|
|
throw new DOMException("The write operation was aborted.", "AbortError");
|
|
|
|
}
|
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);
|