-
Notifications
You must be signed in to change notification settings - Fork 2
refactor: Move non-data-intensive operations from Express backend to Supabase Edge Functions #2750
Description
Context
The Express backend currently handles both data-intensive operations (mining, extracting, cleaning, signature extraction) and lightweight operations. Data-intensive operations need to stay in the Express backend due to:
- Heavy processing requirements
- Long-running tasks with Redis streams
- Worker queue management
However, lightweight operations that don't involve intensive processing should be moved to Supabase Edge Functions for better scalability, lower latency, and reduced infrastructure cost.
Current Architecture
Express Backend Routes
Data-Intensive Operations (Keep in Express):
| Endpoint | Purpose |
|---|---|
POST /api/imap/mine/email/:userId |
Email mining (fetch + extract + clean + signature extraction) |
POST /api/imap/mine/file/:userId |
File mining with extraction |
POST /api/imap/mine/pst/:userId |
PST mining with signature extraction |
GET /api/imap/mine/:type/:id/progress/ |
SSE progress streaming |
GET /api/imap/mine/:userId/ |
Get task status (requires Redis pub/sub) |
POST /api/imap/mine/:type/:userId/:id |
Stop task (requires Redis stream cleanup) |
Non-Data-Intensive Operations (Move to Edge Functions):
| Endpoint | Purpose | Priority |
|---|---|---|
POST /api/enrich/person |
Single contact enrichment | High |
POST /api/enrich/person/bulk |
Bulk enrichment | High |
POST /api/enrich/webhook/:id |
Async enrichment webhook | High |
GET / |
Health check | Low |
DELETE /api/auth/users |
Delete user account | High |
POST /api/imap/boxes |
Fetch IMAP folder tree | High |
POST /api/imap/mine/sources/imap |
Create IMAP source with manual credentials | High |
POST /api/imap/mine/sources/:provider |
OAuth URL generation | High |
GET /api/imap/mine/sources/:provider/callback |
OAuth callback | High |
POST /api/contacts/export/:exportType |
Export contacts | Medium |
Why Task Management Stays in Express
The task status and stop endpoints require Redis for:
- Pub/Sub: Real-time progress updates to clients via SSE
- Redis Streams: Message queues between fetch → extract → clean workers
- In-memory Map: Active tasks stored in memory for fast access
Moving these would require replacing Redis with database-based queues, which is complex and would lose real-time SSE progress updates.
Already in Edge Functions
The following edge functions already exist:
email-campaigns- Email campaign managementpassive-mining- Passive mining operationsfetch-mining-source- OAuth handlers for Google/Microsoftadd-mining-source- Add mining sourcedelete-mining-source- Delete mining sourcemail/*- Email notificationsemail-templates- Email template managementimap- IMAP operations
Migration Plan
Phase 1: High Priority
-
Enrichment (
/api/enrich/*)- External API calls + lightweight JSON processing
- Can run efficiently in edge functions
-
OAuth Flow (
/api/imap/mine/sources/:provider,/callback)- Currently partly in edge functions (
fetch-mining-source) - Need to consolidate and remove Express dependencies
- Currently partly in edge functions (
-
IMAP Boxes (
POST /api/imap/boxes)- Fetch folder tree - simple DB + IMAP query
- Can be handled by existing
imapedge function
Phase 2: Medium Priority
-
Export Contacts (
POST /api/contacts/export/:exportType)- Requires billing integration
- Generate file and return download URL
-
Delete User (
DELETE /api/auth/users)- Cleanup user data across tables
Phase 3: Low Priority
- Health Check (
GET /)- Simple ping endpoint
- Could use Supabase's built-in health check
Requirements
- All edge functions must use the shared middleware for auth validation
- Billing integration needs to work from edge functions
- Proper error handling and logging (using shared logger)
- CORS handled via shared cors middleware
Benefits
- Scalability: Edge functions scale automatically
- Latency: Lower latency for users worldwide
- Cost: Reduce Express backend load and costs
- Maintenance: Smaller Express codebase to maintain
Todo
- Audit all Express routes to confirm classification
- Create new edge functions for missing endpoints
- Migrate high-priority endpoints (enrichment, OAuth, IMAP boxes)
- Update frontend to call edge functions directly
- Remove deprecated Express routes
- Test all migrated endpoints
/cc @ankaboot-source