API reference@evolu/common › Fs

File system operations for Tasks.

Fs reads, writes, copies, and renames files; lists and manages directories; and provides metadata and existence checks. Each operation returns a Task.

Tasks can sequence file operations without synchronous I/O. Node.js's synchronous methods are intentionally omitted to avoid accidentally blocking the event loop.

Inject createNodeFs through runMain or createRun. Tasks declare FsDep and access the file system through run.deps.fs.

FsError includes the path, a diagnostic message, and a FsErrorReason such as NotFound or PermissionDenied. Fs.exists returns false for NotFound and preserves other errors.

Example

import {
  assertEqual,
  ok,
  type FsDep,
  type FsError,
  type Task,
} from "@evolu/common";
import { createNodeFs, runMain } from "@evolu/nodejs";
import { join } from "node:path";

const main: Task<void, FsError, FsDep> = async (run) => {
  const { fs } = run.deps;
  const temp = await run(fs.createTempDirectory({ prefix: "evolu-fs-" }));
  if (!temp.ok) return temp;

  await using directory = temp.value;
  const path = join(directory.path, "message.txt");

  const result = await run(fs.writeFile(path, "hello"));
  if (!result.ok) return result;

  const text = await run(fs.readFile(path, "utf8"));
  if (!text.ok) return text;
  assertEqual(text.value, "hello");

  return ok();
};

await runMain({ fs: createNodeFs() }, { mode: "command" })(main);

Core

NameDescription
FsAsynchronous file system operations.
FsDepDependency wrapper for Fs.
FsMetadataFile metadata as data, with Node's numeric and timestamp field names.
FsReadFileReads bytes by default, or text when an encoding is specified.
FsTempDirectoryA temporary directory removed, with its contents, on asynchronous disposal.
FsEncodingSupported text encodings.
FsEntryTypeThe kind of file system entry described by FsMetadata.
FsOpenFlagSupported file opening modes for Fs.writeFile.
FsPathA file system path, or a file: URL.

Options

InterfaceDescription
FsCopyFileOptionsOptions for Fs.copyFile.
FsCopyOptionsOptions for Fs.copy.
FsCreateDirectoryOptionsOptions for Fs.createDirectory.
FsCreateTempDirectoryOptionsOptions for Fs.createTempDirectory.
FsReadDirectoryOptionsOptions for Fs.readDirectory.
FsRemoveOptionsOptions for Fs.remove.
FsWriteFileOptionsOptions for Fs.writeFile.

Errors

NameDescription
FsErrorA failed file system operation.
FsErrorReasonWhy a file system operation failed, mapped from the platform's error code.

Testing

FunctionDescription
testCreateFsCreates a test Fs with the supplied operation overrides.