2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-06-27 02:18:22 -04:00
|
|
|
|
|
|
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
|
|
|
// deno-lint-ignore-file prefer-primordials
|
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
import { Encodings } from "ext:deno_node/_utils.ts";
|
2023-06-16 06:43:59 -04:00
|
|
|
import { pathFromURL } from "ext:deno_web/00_infra.js";
|
2023-07-02 14:19:30 -04:00
|
|
|
import { Buffer } from "node:buffer";
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
|
|
|
CallbackWithError,
|
|
|
|
checkEncoding,
|
|
|
|
getEncoding,
|
|
|
|
getOpenOptions,
|
|
|
|
isFileOptions,
|
|
|
|
WriteFileOptions,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/_fs/_fs_common.ts";
|
|
|
|
import { isWindows } from "ext:deno_node/_util/os.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
|
|
|
AbortError,
|
|
|
|
denoErrorToNodeError,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/internal/errors.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
|
|
|
validateStringAfterArrayBufferView,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/internal/fs/utils.mjs";
|
|
|
|
import { promisify } from "ext:deno_node/internal/util.mjs";
|
2024-09-11 05:08:40 -04:00
|
|
|
import { FileHandle } from "ext:deno_node/internal/fs/handle.ts";
|
2024-01-24 07:36:35 -05:00
|
|
|
import { FsFile } from "ext:deno_fs/30_fs.js";
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
interface Writer {
|
|
|
|
write(p: Uint8Array): Promise<number>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function writeFile(
|
2024-09-11 05:08:40 -04:00
|
|
|
pathOrRid: string | number | URL | FileHandle,
|
2024-06-11 07:41:44 -04:00
|
|
|
data: string | Uint8Array,
|
2023-02-14 11:38:45 -05:00
|
|
|
optOrCallback: Encodings | CallbackWithError | WriteFileOptions | undefined,
|
|
|
|
callback?: CallbackWithError,
|
|
|
|
) {
|
|
|
|
const callbackFn: CallbackWithError | undefined =
|
|
|
|
optOrCallback instanceof Function ? optOrCallback : callback;
|
|
|
|
const options: Encodings | WriteFileOptions | undefined =
|
|
|
|
optOrCallback instanceof Function ? undefined : optOrCallback;
|
|
|
|
|
|
|
|
if (!callbackFn) {
|
|
|
|
throw new TypeError("Callback must be a function.");
|
|
|
|
}
|
|
|
|
|
2023-06-16 06:43:59 -04:00
|
|
|
pathOrRid = pathOrRid instanceof URL ? pathFromURL(pathOrRid) : pathOrRid;
|
2024-09-11 05:08:40 -04:00
|
|
|
pathOrRid = pathOrRid instanceof FileHandle ? pathOrRid.fd : pathOrRid;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
const flag: string | undefined = isFileOptions(options)
|
|
|
|
? options.flag
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
const mode: number | undefined = isFileOptions(options)
|
|
|
|
? options.mode
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
const encoding = checkEncoding(getEncoding(options)) || "utf8";
|
|
|
|
const openOptions = getOpenOptions(flag || "w");
|
|
|
|
|
|
|
|
if (!ArrayBuffer.isView(data)) {
|
|
|
|
validateStringAfterArrayBufferView(data, "data");
|
2024-06-11 07:41:44 -04:00
|
|
|
data = Buffer.from(data, encoding);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const isRid = typeof pathOrRid === "number";
|
|
|
|
let file;
|
|
|
|
|
|
|
|
let error: Error | null = null;
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
file = isRid
|
2024-01-25 17:51:29 -05:00
|
|
|
? new FsFile(pathOrRid as number, Symbol.for("Deno.internal.FsFile"))
|
2023-02-14 11:38:45 -05:00
|
|
|
: await Deno.open(pathOrRid as string, openOptions);
|
|
|
|
|
|
|
|
// ignore mode because it's not supported on windows
|
|
|
|
// TODO(@bartlomieju): remove `!isWindows` when `Deno.chmod` is supported
|
|
|
|
if (!isRid && mode && !isWindows) {
|
|
|
|
await Deno.chmod(pathOrRid as string, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
const signal: AbortSignal | undefined = isFileOptions(options)
|
|
|
|
? options.signal
|
|
|
|
: undefined;
|
|
|
|
await writeAll(file, data as Uint8Array, { signal });
|
|
|
|
} catch (e) {
|
|
|
|
error = e instanceof Error
|
|
|
|
? denoErrorToNodeError(e, { syscall: "write" })
|
|
|
|
: new Error("[non-error thrown]");
|
|
|
|
} finally {
|
|
|
|
// Make sure to close resource
|
|
|
|
if (!isRid && file) file.close();
|
|
|
|
callbackFn(error);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
export const writeFilePromise = promisify(writeFile) as (
|
|
|
|
pathOrRid: string | number | URL,
|
2024-06-11 07:41:44 -04:00
|
|
|
data: string | Uint8Array,
|
2023-02-14 11:38:45 -05:00
|
|
|
options?: Encodings | WriteFileOptions,
|
|
|
|
) => Promise<void>;
|
|
|
|
|
|
|
|
export function writeFileSync(
|
|
|
|
pathOrRid: string | number | URL,
|
2024-06-11 07:41:44 -04:00
|
|
|
data: string | Uint8Array,
|
2023-02-14 11:38:45 -05:00
|
|
|
options?: Encodings | WriteFileOptions,
|
|
|
|
) {
|
2023-06-16 06:43:59 -04:00
|
|
|
pathOrRid = pathOrRid instanceof URL ? pathFromURL(pathOrRid) : pathOrRid;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
const flag: string | undefined = isFileOptions(options)
|
|
|
|
? options.flag
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
const mode: number | undefined = isFileOptions(options)
|
|
|
|
? options.mode
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
const encoding = checkEncoding(getEncoding(options)) || "utf8";
|
|
|
|
const openOptions = getOpenOptions(flag || "w");
|
|
|
|
|
|
|
|
if (!ArrayBuffer.isView(data)) {
|
|
|
|
validateStringAfterArrayBufferView(data, "data");
|
2024-06-11 07:41:44 -04:00
|
|
|
data = Buffer.from(data, encoding);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const isRid = typeof pathOrRid === "number";
|
|
|
|
let file;
|
|
|
|
|
|
|
|
let error: Error | null = null;
|
|
|
|
try {
|
|
|
|
file = isRid
|
2024-01-25 17:51:29 -05:00
|
|
|
? new FsFile(pathOrRid as number, Symbol.for("Deno.internal.FsFile"))
|
2023-02-14 11:38:45 -05:00
|
|
|
: Deno.openSync(pathOrRid as string, openOptions);
|
|
|
|
|
|
|
|
// ignore mode because it's not supported on windows
|
|
|
|
// TODO(@bartlomieju): remove `!isWindows` when `Deno.chmod` is supported
|
|
|
|
if (!isRid && mode && !isWindows) {
|
|
|
|
Deno.chmodSync(pathOrRid as string, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(crowlKats): duplicate from runtime/js/13_buffer.js
|
|
|
|
let nwritten = 0;
|
|
|
|
while (nwritten < (data as Uint8Array).length) {
|
|
|
|
nwritten += file.writeSync((data as Uint8Array).subarray(nwritten));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
error = e instanceof Error
|
|
|
|
? denoErrorToNodeError(e, { syscall: "write" })
|
|
|
|
: new Error("[non-error thrown]");
|
|
|
|
} finally {
|
|
|
|
// Make sure to close resource
|
|
|
|
if (!isRid && file) file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface WriteAllOptions {
|
|
|
|
offset?: number;
|
|
|
|
length?: number;
|
|
|
|
signal?: AbortSignal;
|
|
|
|
}
|
|
|
|
async function writeAll(
|
|
|
|
w: Writer,
|
|
|
|
arr: Uint8Array,
|
|
|
|
options: WriteAllOptions = {},
|
|
|
|
) {
|
|
|
|
const { offset = 0, length = arr.byteLength, signal } = options;
|
|
|
|
checkAborted(signal);
|
|
|
|
|
|
|
|
const written = await w.write(arr.subarray(offset, offset + length));
|
|
|
|
|
|
|
|
if (written === length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await writeAll(w, arr, {
|
|
|
|
offset: offset + written,
|
|
|
|
length: length - written,
|
|
|
|
signal,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkAborted(signal?: AbortSignal) {
|
|
|
|
if (signal?.aborted) {
|
|
|
|
throw new AbortError();
|
|
|
|
}
|
|
|
|
}
|