Skip to content

ext_fs

The ext_fs crate provides filesystem operations for Forge applications through the host:fs module.

Overview

ext_fs handles:

  • File reading/writing - Text and binary file operations
  • Directory operations - Create, read, and remove directories
  • File watching - Monitor files and directories for changes
  • File metadata - Size, modification time, file type
  • Capability-based security - Path-based permission checks

Module: host:fs

import {
readTextFile,
writeTextFile,
readDir,
watch
} from "host:fs";

Key Types

Error Types

enum FsErrorCode {
Io = 3000,
PermissionDenied = 3001,
NotFound = 3002,
AlreadyExists = 3003,
InvalidPath = 3004,
NotAFile = 3005,
NotADirectory = 3006,
}
struct FsError {
code: FsErrorCode,
message: String,
}

Data Types

struct FileStat {
is_file: bool,
is_directory: bool,
is_symlink: bool,
size: u64,
modified: Option<u64>,
accessed: Option<u64>,
created: Option<u64>,
}
struct DirEntry {
name: String,
path: String,
is_file: bool,
is_directory: bool,
is_symlink: bool,
}
struct FileEvent {
kind: WatchEventKind,
paths: Vec<String>,
}
enum WatchEventKind {
Create,
Modify,
Remove,
Other,
}

Capability Types

struct FsCapabilities {
read_patterns: Vec<String>,
write_patterns: Vec<String>,
}
trait FsCapabilityChecker {
fn check_read(&self, path: &str) -> bool;
fn check_write(&self, path: &str) -> bool;
}

Operations

OpTypeScriptDescription
op_fs_read_textreadTextFile(path)Read file as UTF-8 string
op_fs_read_bytesreadFile(path)Read file as bytes
op_fs_write_textwriteTextFile(path, content)Write string to file
op_fs_write_byteswriteFile(path, data)Write bytes to file
op_fs_read_dirreadDir(path)List directory entries
op_fs_mkdirmkdir(path, opts?)Create directory
op_fs_removeremove(path, opts?)Remove file or directory
op_fs_statstat(path)Get file metadata
op_fs_existsexists(path)Check if path exists
op_fs_watchwatch(path)Watch for file changes
op_fs_watch_recv(internal)Receive watch events

File Structure

crates/ext_fs/
├── src/
│ └── lib.rs # Extension implementation
├── ts/
│ └── init.ts # TypeScript module shim
├── build.rs # forge-weld build configuration
└── Cargo.toml

Build Configuration

build.rs
use forge_weld::ExtensionBuilder;
fn main() {
ExtensionBuilder::new("host_fs", "host:fs")
.ts_path("ts/init.ts")
.ops(&[
"op_fs_read_text",
"op_fs_write_text",
"op_fs_read_dir",
// ...
])
.generate_sdk_types("sdk")
.dts_generator(generate_host_fs_types)
.build()
.expect("Failed to build host_fs extension");
}

Dependencies

DependencyPurpose
deno_coreOp definitions
tokioAsync filesystem operations
notifyFile watching
globsetPattern matching for capabilities
tracingLogging
forge-weldBuild-time code generation