How exerra/cf-imap works
Purpose and runtime
cf-imap is a TypeScript IMAP4 client designed specifically for Cloudflare Workers. It opens and manages IMAP sessions over Workers-compatible sockets, supports IMAP4rev1/rev2 behavior, and exposes mailbox, message, flag, and IDLE operations. Its public entry point is src/index.ts.
Public client and session lifecycle
CFImap in src/index.ts is the primary API boundary. Callers construct it with server, TLS, authentication, and timeout options; then invoke connect() from a Worker request handler. The lifecycle is:
1. Open socket and establish implicit TLS or negotiate STARTTLS.
2. Discover capabilities and negotiate IMAP4rev2 where supported.
3. Authenticate, execute commands, and consume server responses.
4. End the session through logout() / close() and socket shutdown.
Related exported request/result types are centralized in src/types/emails.ts.
IMAP command transport and response framing
The client’s control plane is tag-based IMAP command execution: commands are serialized onto one socket, then correlated with tagged completion responses while collecting untagged data and continuations. src/utils/imapStream.ts is the streaming/framing layer for reading IMAP response lines and literals, including long message payloads without treating the protocol as a single buffered response. src/utils/imapList.ts parses structured IMAP LIST-style responses.
This transport boundary is responsible for command failures becoming ImapError, socket drops becoming regular errors, and applying the configured read timeout.
Capability-dependent protocol behavior
Connection setup and command construction in src/index.ts adapt to server capability advertisements. Important branches include implicit TLS on port 993 versus STARTTLS elsewhere, AUTHENTICATE PLAIN before conditional LOGIN, enabling IMAP4rev2 when advertised, and refusing rev1-only/deprecated semantics where rev2 removes them.
Mailbox naming is also protocol-version dependent: rev2 uses UTF-8 names, while rev1 sessions require modified UTF-7 conversion. Namespace discovery, folder listing, select/examine, status, close/unselect, copy/move, append, expunge, and IDLE all pass through this same session-aware command boundary.
Search and message retrieval
src/utils/search.ts translates high-level search criteria into IMAP SEARCH commands and parses both legacy SEARCH and rev2 ESEARCH results. Search can operate on sequence numbers or UIDs and supplies UTF-8 charset handling for non-ASCII criteria.
Fetching is orchestrated by src/index.ts: it requests headers or complete RFC message content, associates IMAP metadata such as sequence number, UID, flags, size, and internal date, and returns the normalized email model defined in src/types/emails.ts.
MIME decoding pipeline
Raw fetched messages cross into a local parsing pipeline rather than an external email service. src/utils/mime.ts parses MIME structure, multipart bodies, text/HTML parts, attachments, content IDs, encodings, and raw message representation. src/utils/decodeMime.ts decodes encoded headers/content and supports normalized fields such as subject, address lists, headers, body variants, and attachment base64/content.
The resulting application-facing email object is the boundary between IMAP wire responses and consumer code: src/types/emails.ts, src/utils/mime.ts, and src/utils/decodeMime.ts.
Main entry points: index.ts CFImap client · imapStream.ts IMAP stream framing · search.ts Search translator · mime.ts MIME parser
Components 13
Public API
- Worker caller · Creates and uses IMAP sessions.
- CFImap client · Session-aware IMAP API and command orchestrator.
src/index.ts - Email models · Application-facing request, result, and normalized email types.
src/types/emails.ts
IMAP Session
- Worker socket · Workers-compatible IMAP connection.
- Connection setup · TLS, capabilities, rev2 negotiation, authentication, and shutdown.
src/index.ts - Tagged command transport · Serializes commands and correlates tagged, untagged, and continuation responses.
src/index.ts - Mailbox operations · Namespace, folders, select, status, copy/move, append, expunge, and IDLE.
src/index.ts - Message retrieval · Collects message metadata, headers, and RFC content.
src/index.ts
Response & Message Parsing
- IMAP stream framing · Reads response lines and literals with timeout handling.
src/utils/imapStream.ts - LIST parser · Parses structured mailbox listing responses.
src/utils/imapList.ts - Search translator · Builds criteria and parses sequence or UID results.
src/utils/search.ts - MIME parser · Parses multipart structure, bodies, attachments, and raw content.
src/utils/mime.ts - MIME decoder · Decodes encoded fields into normalized message content.
src/utils/decodeMime.ts
Connections 19
- Worker caller → CFImap client: constructs / connects
- CFImap client → Email models: returns
- CFImap client → Connection setup: manages lifecycle
- Connection setup → Worker socket: opens TLS or STARTTLS
- Connection setup → Tagged command transport: negotiates and authenticates
- CFImap client → Tagged command transport: executes commands
- Tagged command transport → Worker socket: writes commands
- Worker socket → IMAP stream framing: server bytes
- IMAP stream framing → Tagged command transport: framed responses
- CFImap client → Mailbox operations: exposes
- Mailbox operations → Tagged command transport: session-aware commands
- CFImap client → Search translator: search requests
- Search translator → Tagged command transport: SEARCH / ESEARCH
- CFImap client → Message retrieval: fetch requests
- Message retrieval → Tagged command transport: FETCH
- IMAP stream framing → LIST parser: LIST responses
- Message retrieval → MIME parser: raw RFC message
- MIME parser → MIME decoder: parsed parts
- MIME decoder → Email models: normalized email