import { op_file_chmod_sync as chmodOp, op_file_chown_sync as chownOp, op_file_copy_sync as copyOp, op_file_lchown_sync as lchownOp, op_file_link_sync as linkOp, op_file_lutime_sync as lutimeOp, op_file_mkdir_sync as mkdirOp, op_file_open_sync as openOp, op_file_path_stat_sync as pathStatOp, op_file_path_truncate_sync as truncatePathOp, op_file_read_dir_sync as readDirOp, op_file_read_link_sync as readLinkOp, op_file_real_path_sync as realPathOp, op_file_remove_sync as removeOp, op_file_rename_sync as renameOp, op_file_symlink_sync as symlinkOp, op_file_utime_sync as utimeOp, } from "ext:core/ops"; import { dictionary, required } from "ext:web/idl.js"; import { RangeError, TypeError } from "ext:core/primordials"; import { args, fileHandle, integer, metadata, mode, openArgs, owner, path, stamp, } from "./main.js"; import { result } from "./errors.js"; export { errors, FileHandle, SeekMode } from "./main.js"; export function open(pathname: unknown, options: unknown = undefined) { required(arguments.length, 1); return fileHandle(result(openOp(openArgs(pathname, options)))); } export function create(pathname: unknown) { required(arguments.length, 1); return open(pathname, { read: true, write: true, create: true, truncate: true, }); } export function stat(pathname: unknown) { required(arguments.length, 1); return metadata( result(pathStatOp(args(path(pathname), true))), ); } export function lstat(pathname: unknown) { required(arguments.length, 1); return metadata( result(pathStatOp(args(path(pathname), false))), ); } export function* readDir( pathname: unknown, ): Generator< { name: string; isFile: boolean; isDirectory: boolean; isSymlink: boolean } > { required(arguments.length, 1); const entries = result(readDirOp(path(pathname))); for (let i = 0; i < entries.length; i++) { const row = entries[i]; yield { name: row[0], isFile: row[1], isDirectory: row[2], isSymlink: row[3], }; } } export function mkdir(pathname: unknown, options: unknown = undefined): void { required(arguments.length, 1); const filename = path(pathname), mode = dictionary(options).mode; if (mode !== undefined && integer(mode) > 0o7777) { throw new RangeError("Invalid directory mode"); } result( mkdirOp(args(filename, mode === undefined ? null : mode as number)), ); } export function remove(pathname: unknown, options: unknown = undefined): void { required(arguments.length, 1); const filename = path(pathname), recursive = dictionary(options).recursive; if (recursive !== undefined && typeof recursive !== "boolean") { throw new TypeError("recursive must be boolean"); } result(removeOp(args(filename, recursive === true))); } export function rename(source: unknown, destination: unknown): void { required(arguments.length, 2); result(renameOp(args(path(source), path(destination)))); } export function copyFile(source: unknown, destination: unknown): void { required(arguments.length, 2); result(copyOp(args(path(source), path(destination)))); } export function symlink( target: unknown, linkPath: unknown, options: unknown = undefined, ): void { required(arguments.length, 2); const contents = path(target), link = path(linkPath), kind = dictionary(options).type; if (kind !== undefined && kind !== "file" && kind !== "dir") { throw new TypeError("Symlink type must be file or dir"); } result( symlinkOp(args(contents, link, kind === undefined ? null : kind)), ); } export function readLink(pathname: unknown): string { required(arguments.length, 1); return result(readLinkOp(path(pathname))); } export function realPath(pathname: unknown): string { required(arguments.length, 1); return result(realPathOp(path(pathname))); } export function chmod(pathname: unknown, fileMode: number): void { required(arguments.length, 2); result(chmodOp(args(path(pathname), mode(fileMode)))); } export function chown( pathname: unknown, uid: number | null | undefined, gid: number | null | undefined, ): void { required(arguments.length, 3); result( chownOp(args(path(pathname), owner(uid), owner(gid))), ); } export function lchown( pathname: unknown, uid: number | null | undefined, gid: number | null | undefined, ): void { required(arguments.length, 3); result( lchownOp(args(path(pathname), owner(uid), owner(gid))), ); } export function utime( pathname: unknown, atime: number | Date, mtime: number | Date, ): void { required(arguments.length, 3); const before = stamp(atime), after = stamp(mtime); result( utimeOp( args(path(pathname), before[0], before[1], after[0], after[1]), ), ); } export function lutime( pathname: unknown, atime: number | Date, mtime: number | Date, ): void { required(arguments.length, 3); const before = stamp(atime), after = stamp(mtime); result( lutimeOp( args(path(pathname), before[0], before[1], after[0], after[1]), ), ); } export function link(oldpath: unknown, newpath: unknown): void { required(arguments.length, 2); result(linkOp(args(path(oldpath), path(newpath)))); } export function truncate(pathname: unknown, length = 0): void { required(arguments.length, 1); result( truncatePathOp(args(path(pathname), integer(length))), ); }