import { validateCallback } from "../internal/validators.mjs";import type { ErrnoException } from "../_global.d.ts";import { BinaryEncodings, Encodings, notImplemented, TextEncodings,} from "../_utils.ts";
export type CallbackWithError = (err: ErrnoException | null) => void;
export interface FileOptions { encoding?: Encodings; flag?: string; signal?: AbortSignal;}
export type TextOptionsArgument = | TextEncodings | ({ encoding: TextEncodings } & FileOptions);export type BinaryOptionsArgument = | BinaryEncodings | ({ encoding: BinaryEncodings } & FileOptions);export type FileOptionsArgument = Encodings | FileOptions;
export interface WriteFileOptions extends FileOptions { mode?: number;}
export function isFileOptions( fileOptions: string | WriteFileOptions | undefined,): fileOptions is FileOptions { if (!fileOptions) return false;
return ( (fileOptions as FileOptions).encoding != undefined || (fileOptions as FileOptions).flag != undefined || (fileOptions as FileOptions).signal != undefined || (fileOptions as WriteFileOptions).mode != undefined );}
export function getEncoding( optOrCallback?: | FileOptions | WriteFileOptions | ((...args: any[]) => any) | Encodings | null,): Encodings | null { if (!optOrCallback || typeof optOrCallback === "function") { return null; }
const encoding = typeof optOrCallback === "string" ? optOrCallback : optOrCallback.encoding; if (!encoding) return null; return encoding;}
export function checkEncoding(encoding: Encodings | null): Encodings | null { if (!encoding) return null;
encoding = encoding.toLowerCase() as Encodings; if (["utf8", "hex", "base64"].includes(encoding)) return encoding;
if (encoding === "utf-8") { return "utf8"; } if (encoding === "binary") { return "binary"; }
const notImplementedEncodings = ["utf16le", "latin1", "ascii", "ucs2"];
if (notImplementedEncodings.includes(encoding as string)) { notImplemented(`"${encoding}" encoding`); }
throw new Error(`The value "${encoding}" is invalid for option "encoding"`);}
export function getOpenOptions(flag: string | undefined): Deno.OpenOptions { if (!flag) { return { create: true, append: true }; }
let openOptions: Deno.OpenOptions; switch (flag) { case "a": { openOptions = { create: true, append: true }; break; } case "ax": { openOptions = { createNew: true, write: true, append: true }; break; } case "a+": { openOptions = { read: true, create: true, append: true }; break; } case "ax+": { openOptions = { read: true, createNew: true, append: true }; break; } case "r": { openOptions = { read: true }; break; } case "r+": { openOptions = { read: true, write: true }; break; } case "w": { openOptions = { create: true, write: true, truncate: true }; break; } case "wx": { openOptions = { createNew: true, write: true }; break; } case "w+": { openOptions = { create: true, write: true, truncate: true, read: true }; break; } case "wx+": { openOptions = { createNew: true, write: true, read: true }; break; } case "as": { openOptions = { create: true, append: true }; break; } case "as+": { openOptions = { create: true, read: true, append: true }; break; } case "rs+": { openOptions = { create: true, read: true, write: true }; break; } default: { throw new Error(`Unrecognized file system flag: ${flag}`); } }
return openOptions;}
export { isUint32 as isFd } from "../internal/validators.mjs";
export function maybeCallback(cb: unknown) { validateCallback(cb);
return cb as CallbackWithError;}
export function makeCallback( this: unknown, cb?: (err: Error | null, result?: unknown) => void,) { validateCallback(cb);
return (...args: unknown[]) => Reflect.apply(cb!, this, args);}