fs
import { fs } from "@tynd/core/client";Cross-OS filesystem operations. Binary I/O uses the zero-copy binary channel.
Text I/O
await fs.writeText("data.json", JSON.stringify(state), { createDirs: true });
const text = await fs.readText("data.json");readText(path): Promise<string>
Reads the file as UTF-8.
writeText(path, content, opts?): Promise<void>
Writes UTF-8 text.
createDirs?: boolean— create parent directories if missing.
Binary I/O
const bytes = await fs.readBinary("image.png"); // Uint8Array
await fs.writeBinary("copy.png", bytes, { createDirs: true });Uses the tynd-bin:// channel — no base64, zero-copy. Accepts Uint8Array | ArrayBuffer. See the Binary Data guide.
Metadata
exists(path): Promise<boolean>
if (await fs.exists("data.json")) { /* … */ }stat(path): Promise<FileStat>
interface FileStat {
size: number;
isFile: boolean;
isDir: boolean;
isSymlink: boolean;
mtime: number | null; // ms since epoch; null if unsupported
}
const info = await fs.stat("data.json");Directories
readDir(path): Promise<DirEntry[]>
interface DirEntry {
name: string;
isFile: boolean;
isDir: boolean;
isSymlink: boolean;
}
const entries = await fs.readDir(".");Join with the parent path for an absolute path: await path.join(dir, entry.name).
mkdir(path, opts?): Promise<void>
await fs.mkdir("./nested/dir", { recursive: true });remove(path, opts?): Promise<void>
Works on files and directories.
await fs.remove("./old.json");
await fs.remove("./dir", { recursive: true });rename(from, to): Promise<void>
await fs.rename("./a.txt", "./b.txt");copy(from, to): Promise<void>
await fs.copy("./src.json", "./dst.json");Watcher
interface FsChangeEvent {
id: number;
kind: "create" | "modify" | "delete" | "rename" | "error" | "other";
path?: string;
error?: string;
}
const watcher = await fs.watch("./notes", { recursive: true }, (event) => {
console.log(event.kind, event.path);
});
// later
await watcher.unwatch();- Uses
ReadDirectoryChangesW(Windows),FSEvents(macOS),inotify(Linux). - Event coalescing behavior differs by OS —
modifymay fire multiple times for a single logical save. Debounce on your side if needed. kind: "error"events include anerrorfield; watcher remains active.
Notes
- Paths accept both absolute and relative. Relative paths resolve against the app’s current working directory at launch. Prefer absolute paths built from
os.dataDir(),os.configDir(), etc. - All methods run on a fresh Rust thread — JS event loop is never blocked.
Related
- path — path helpers.
- os —
dataDir,configDir,cacheDir,tmpDir. - Binary Data guide.
Last updated on