The Role of @cloudflare/dofs in Cloudflare Computer: Core Data Layer and Virtual Filesystem
@cloudflare/dofs is the core data layer of Cloudflare Computer, providing a Durable Object-backed SQLite virtual filesystem that powers storage, POSIX operations, and state synchronization across the entire platform.
Cloudflare Computer is a cloud development environment built on Cloudflare's edge infrastructure. At its foundation lies @cloudflare/dofs, a TypeScript package that abstracts Durable Object storage into a SQLite-backed virtual filesystem according to the cloudflare/computer source code. This package serves as the authoritative storage layer, enabling everything from basic file operations to real-time synchronization between edge and client.
Database Wrapper and Schema Initialization
The foundation of @cloudflare/dofs is the Database class implemented in packages/dofs/src/storage.ts. This wrapper encapsulates a Durable Object's storage interface and exposes it as a SQLite database connection.
When initializing a Durable Object, you must create the virtual filesystem schema using the initializeSchema function exported from packages/dofs/src/index.ts. This function creates the internal vfs_* tables required for filesystem operations and accepts a clock function—typically Date.now—for revision tracking.
import { Database, initializeSchema } from "@cloudflare/dofs";
export class WorkspaceDO extends DurableObject {
private readonly db: Database;
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
this.db = new Database(ctx.storage);
// Create the vfs_* tables; Date.now supplies a clock for rev tracking
initializeSchema(this.db, Date.now);
}
}
The Database instance becomes the single source of truth for all subsequent filesystem and synchronization operations within that Durable Object.
POSIX-Style Filesystem Primitives
Beyond database management, @cloudflare/dofs implements complete POSIX-style filesystem operations in packages/dofs/src/fs/. These functions operate directly on the Database instance without requiring external storage mechanisms.
Available primitives include mkdir, writeFile, readFile, rm, readdir, stat, chmod, symlink, readlink, find, and grep. Each function accepts the database instance as its first parameter, followed by standard filesystem arguments.
import { Database, readFile, writeFile, mkdir, stat } from "@cloudflare/dofs";
async function demo(db: Database) {
await mkdir(db, "/example");
await writeFile(db, "/example/hello.txt", "Hello, Cloudflare!", { mode: 0o644 });
const contents = await readFile(db, "/example/hello.txt", "utf8");
console.log(contents); // → "Hello, Cloudflare!"
console.log(await stat(db, "/example/hello.txt"));
}
These primitives enable direct manipulation of the virtual filesystem within Durable Objects or testing environments.
SQLiteWorkspaceProvider and FUSE Integration
To bridge the gap between the SQLite-backed filesystem and host-side applications, @cloudflare/dofs provides SQLiteWorkspaceProvider in packages/dofs/src/provider.ts. This class implements a @platformatic/vfs-compatible adapter that exposes the WorkspaceFilesystem through a Node-style fs API.
The computerd daemon consumes this provider to mount the virtual filesystem via FUSE (Filesystem in Userspace), allowing local development tools to interact with the cloud workspace as if it were a local directory.
import { Database, initializeSchema, SQLiteWorkspaceProvider } from "@cloudflare/dofs";
async function startComputerd(storage: DurableObjectStorage) {
const db = new Database(storage);
await initializeSchema(db, Date.now);
const provider = new SQLiteWorkspaceProvider({ db });
// Provider implements @platformatic/vfs interface; computerd mounts via FUSE
await mountFUSE(provider);
}
This architecture allows seamless integration between edge storage and local development environments.
Sync Protocol Building Blocks
Real-time collaboration in Cloudflare Computer relies on the synchronization primitives defined in packages/dofs/src/sync/. These functions manage the bidirectional flow of data between the Durable Object state and connected clients.
Key functions include applyChanges for applying local modifications, stageBlob for preparing binary data, fetchChanges for retrieving remote updates, pushObjects for uploading state, buildManifest for generating state snapshots, and currentRev for tracking revision watermarks.
These building blocks are wired into @cloudflare/computer-rpc to enable efficient state synchronization. The sync layer operates entirely on the same Database instance used by the filesystem primitives, ensuring consistency between file contents and metadata during sync operations.
Developer Utilities and Testing Support
@cloudflare/dofs includes comprehensive testing infrastructure in packages/dofs/src/testing.ts. The package exports SQLiteTestStorage, a Node-only utility for creating test database instances, and RecordingStorage, an in-process storage implementation for unit tests.
Type definitions for Database, DurableObjectStorageLike, and SQLCursorLike are also exported from packages/dofs/src/index.ts, enabling type-safe development across the Cloudflare Computer ecosystem.
Source Code Structure and Preview Status
The package is organized into distinct modules within packages/dofs/:
- README.md – Contains the preview-only warning noting that the API may evolve
- src/index.ts – Export hub re-exporting database, schema, provider, filesystem primitives, and sync helpers
- src/storage.ts –
Databasewrapper implementation around Durable Object storage - src/provider.ts –
SQLiteWorkspaceProviderimplementing the@platformatic/vfsinterface - src/fs/ – Individual filesystem primitive implementations
- src/sync/ – Synchronization protocol functions
- src/testing.ts – Node-only testing utilities including
SQLiteTestStorageandRecordingStorage
According to the package README, @cloudflare/dofs is intentionally marked as preview-only and subject to change, though the core architecture—Durable Object → SQLite → virtual FS → sync → client—remains stable.
Summary
- @cloudflare/dofs provides the authoritative storage and filesystem layer for Cloudflare Computer, implementing a SQLite-backed virtual filesystem inside Durable Objects.
- The
Databaseclass insrc/storage.tswraps Durable Object storage, whileinitializeSchemacreates the requiredvfs_*tables for revision tracking. - POSIX-style operations (
mkdir,writeFile,readFile,stat, etc.) operate directly on the database instance through functions exported fromsrc/fs/. SQLiteWorkspaceProviderinsrc/provider.tsadapts the virtual filesystem to the@platformatic/vfsinterface, enabling FUSE mounting by thecomputerddaemon.- Synchronization primitives in
src/sync/(applyChanges,stageBlob,fetchChanges, etc.) manage state consistency between edge and client. - Testing utilities in
src/testing.tsprovideSQLiteTestStorageandRecordingStoragefor unit testing outside the Workers environment.
Frequently Asked Questions
What is @cloudflare/dofs used for in Cloudflare Computer?
@cloudflare/dofs serves as the core data layer that provides a Durable Object-backed SQLite virtual filesystem. It handles all storage operations, file manipulation, and state synchronization required by the Cloudflare Computer development environment.
How does @cloudflare/dofs store filesystem data?
The package stores filesystem metadata and content in SQLite tables within a Durable Object's storage using the Database class defined in src/storage.ts. The initializeSchema function creates internal vfs_* tables that track files, directories, permissions, and revision history using a clock function like Date.now.
Can I use @cloudflare/dofs outside of Cloudflare Computer?
While the package is published and functional, the README explicitly marks it as preview-only with APIs that may evolve. The architecture is designed specifically for Cloudflare Computer's needs, though the primitives could theoretically support any application requiring a SQLite-backed virtual filesystem in Durable Objects.
What testing utilities does @cloudflare/dofs provide?
The package exports SQLiteTestStorage for Node.js-based integration testing and RecordingStorage for in-process unit testing from src/testing.ts. These utilities allow developers to simulate Durable Object storage behavior without deploying to the Cloudflare Workers environment.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →