A library that bridges Effect-TS with Cloudflare Workers runtime. It provides effectful, type-safe, and composable patterns for building serverless applications.
This implementation is based on the design document at docs/002-implementation-guide.md with the following modifications:
- Database/Drizzle excluded: No D1 database, Drizzle ORM, or database-related code
- Simplified naming: Service implementations use
Livesuffix instead of platform-specific namesKVStoreLive(instead ofKVStoreCloudflare)ObjectStorageLive(instead ofObjectStorageR2)- Exception:
CloudflareBindingskeeps its name as it's specifically for Cloudflare env
effect-worker/
├── src/
│ ├── worker.ts # Main entry point (export default)
│ ├── app.ts # Application layer composition
│ ├── runtime.ts # Runtime factory functions
│ ├── services/ # Service abstractions
│ │ ├── bindings.ts # CloudflareBindings service
│ │ ├── config.ts # Config service
│ │ ├── kv.ts # KV Store service
│ │ ├── storage.ts # Object Storage (R2) service
│ │ └── index.ts # Re-exports all services
│ ├── handlers/ # Handler implementations
│ │ ├── fetch.ts # HTTP request handler
│ │ └── errors.ts # Error response formatting
│ └── errors/ # Error definitions
│ └── index.ts # Typed error definitions
├── test/ # Test files
│ └── fixtures/ # Test fixtures
│ └── mock-services.ts # Mock service implementations
├── docs/ # Design documentation
│ ├── 001-system-design.md # System architecture document
│ └── 002-implementation-guide.md # Implementation guide
├── wrangler.toml # Cloudflare configuration
├── package.json # Dependencies
├── tsconfig.json # TypeScript config
└── vitest.config.ts # Test configuration
- ✅
package.json- Dependencies (without drizzle) - ✅
tsconfig.json- TypeScript config for Workers + Effect - ✅
wrangler.toml- Cloudflare config (without D1 database binding) - ✅
vitest.config.ts- Test configuration
- ✅
src/services/bindings.ts- CloudflareBindings service (foundation layer) - ✅
src/services/config.ts- Config service for env vars/secrets - ✅
src/services/kv.ts- KV Store service abstraction - ✅
src/services/storage.ts- Object Storage (R2) service abstraction - ✅
src/services/index.ts- Re-exports all services - ✅
src/errors/index.ts- Error type definitions (excluding DatabaseError) - ✅
src/app.ts- Application layer composition (excluding Database) - ✅
src/runtime.ts- Runtime factory functions - ✅
src/handlers/fetch.ts- HTTP request handler - ✅
src/handlers/errors.ts- Error response formatting - ✅
src/worker.ts- Main entry point
- ✅
test/fixtures/mock-services.ts- Mock service implementations for testing
- CloudflareBindings - Foundation layer that provides access to Cloudflare's env and ExecutionContext
- Config - Type-safe configuration management with validation
- KVStore - Key-value storage with JSON support and schema validation
- ObjectStorage - R2 object storage with streaming and JSON support
All errors extend Data.TaggedError for type-safe error handling:
ConfigError- Configuration issuesKVError- KV operation failuresStorageError- R2 operation failuresValidationError- Request validation failuresAuthorizationError- Authorization failuresNotFoundError- Resource not found
The implemented fetch handler includes example endpoints:
GET /health- Health checkGET /api/kv/:key- Get a KV valuePOST /api/kv/:key- Set a KV valueGET /api/storage/:key- Get an R2 objectPOST /api/storage/:key- Upload to R2GET /api/config- Get environment info
To use this implementation:
-
Install dependencies:
pnpm install
-
Configure your Cloudflare bindings in
wrangler.toml -
Start local development:
pnpm dev
-
Run tests:
pnpm test -
Deploy to Cloudflare:
pnpm deploy
See the docs/ directory for detailed design documentation:
001-system-design.md- System architecture overview002-implementation-guide.md- Comprehensive implementation guide
- No Database/Drizzle: All database-related code has been excluded
- Simplified Naming: Service implementations use cleaner names:
KVStoreLiveinstead ofKVStoreCloudflareObjectStorageLiveinstead ofObjectStorageR2
- Clean Dependencies: Removed drizzle-orm and drizzle-kit from package.json
- Updated Env Interface: Removed DB binding from CloudflareBindings
- Updated Handlers: Fetch handler demonstrates KV and R2 usage without database operations
- The library follows Effect-TS patterns for composition, error handling, and dependency injection
- Runtime is cached per isolate for better performance
- All services are swappable through Effect's layer system
- Full type safety throughout with TypeScript strict mode