ocaml-jmap — JMAP for OCaml #
An implementation of the JSON Meta Application Protocol (JMAP) as specified in RFC 8620 (core) and RFC 8621 (mail), with a native client built on Eio.
Libraries #
The jmap package provides:
jmap— protocol types,jsontcodecs, andJmap.Chain, a typed request builder that turns a chain of method calls into one JMAP request using RFC 8620 §3.7 result references, and reads the response back into a list of the same shape.jmap.eio— the client:Jmap_eio.Authfor the credential,Jmap_eio.Transportfor the HTTP stack,Jmap_eio.Clientfor the session, requests and blobs,Jmap_eio.Syncfor paging, change draining and batching,Jmap_eio.Pushfor the EventSource endpoint, andJmap_eio.Clifor cmdliner terms and environment configuration.jmap.top— toplevel pretty-printers.
A second package, jmap-mosaic, builds on them: a terminal mail client that
reads, labels, files and replies to mail, written on the
Mosaic UI library. See mosaic/README.md.
The design is Eio-only. There is no second concurrency runtime and no callback or promise layer to bridge: every client call is a direct-style function, and concurrency is the caller's to arrange with fibers and switches.
The Eio client #
Jmap_eio.Client runs over fetch with the
fetch-httpz backend, so TLS, redirects and cookies come from fetch, and
credentials are scoped to an origin rather than attached to every request.
Three modules make a client: Auth (who), Transport (how the HTTP is done)
and Client.connect (the session).
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
let client =
Jmap_eio.Client.connect_env ~sw ~timeout:30.
~auth:(Jmap_eio.Auth.bearer token) env
"https://api.fastmail.com/.well-known/jmap"
|> Result.get_ok
connect_env builds the default stack; Client.connect ~sw ?auth transport url
takes a Jmap_eio.Transport.t instead, which is how a program pins a
certificate, sets its own retry policy or paces requests per origin
(Transport.v ?https ?retry ?max_concurrent ?min_interval env), and how a test
puts a mock underneath (Transport.of_fetch).
Authentication is named by scheme, not by header:
- Bearer —
Jmap_eio.Auth.bearer token(RFC 6750 §2.1), for services such as Fastmail that issue API tokens. - Basic —
Jmap_eio.Auth.basic ~user ~password(RFC 7617), for servers that predate bearer tokens, such as the Cyrus test server. Basic credentials over plainhttp://need~allow_insecure:true; otherwise the client refuses to send them. Auth.bearer_from_file/Auth.basic_from_fileread the secret from the first line of a file when a request needs it,Auth.refreshing ~refreshcalls a thunk per request (OAuth), andAuth.of_env ()readsJMAP_API_KEY,JMAP_API_KEY_FILEandJMAP_AUTH.Auth.ppredacts.
Credentials are attached only to the origins the session resource names, and
refresh_session re-scopes them when the session changes. A response whose
sessionState disagrees with the cached session (RFC 8620 §3.4) refetches it
once under a mutex; Client.on_session_change and Client.session_changed
report that, and Client.concurrency_limits reports the
maxConcurrentRequests / maxConcurrentUpload the client then obeys.
~timeout bounds each exchange the client reads to its end — the session
fetch, a request, an upload, a download — with the transport's clock, and gives
Client.Timeout. Everything else the network can do arrives as
Client.Transport (Fetch.error, text), so a caller can tell a retryable
Connection_failure from a Denied that will never succeed.
Blobs without a copy in memory #
Client.upload/download take and give a string; the streaming pair does
not (RFC 8620 §6):
Eio.Path.with_open_in path @@ fun file ->
let length = Optint.Int63.to_int64 (Eio.File.size file) in
Jmap_eio.Client.upload_flow client ~account_id
~content_type:"message/rfc822" ~length file
Client.download_to client ~account_id ~blob_id sink copies the response body
to any Eio.Flow.sink as it arrives and returns the media type the server
served it as; the client's body limit does not apply, because the bytes are the
caller's to place.
Push #
Jmap_eio.Push.subscribe ~sw client () forks a daemon fiber that keeps the
event source of RFC 8620 §7.3 connected, reconnects with Last-Event-ID and
exponential backoff (?backoff_initial/?backoff_max), and delivers into a
bounded Eio.Stream.t:
let sub = Jmap_eio.Push.subscribe ~sw client ~types:[ "Email" ] ~ping:30 () in
match Eio.Stream.take (Push.events sub) with
| `Event ev -> Fmt.pr "%a@." Jmap_eio.Push.pp_event ev
| `End -> ()
~poll:seconds closes and reopens the connection instead of holding it open,
which is what Cyrus needs: it holds a per-account lock for as long as an event
source is open, so while a stream is held that user's LMTP deliveries and JMAP
API calls both wait for it (another user is unaffected). Pair ~poll with
~last_event_id, which is what makes each connection answer at once with what
changed while it was away (RFC 8620 §7.3); Push.last_event_id is the value to
persist across runs. Push.listen is the raw connection underneath.
Sync: paging, changes and batching #
Jmap_eio.Sync is the three loops the server's own limits force:
Sync.pageswalks a/querybyposition/limit(RFC 8620 §5.5), lazily, using the limit the server reports;Sync.all_ids ~maxconcatenates them.Sync.changes(andemail_changes,mailbox_changes,thread_changes) drainshasMoreChanges(§5.2) and reportscannotCalculateChangesas a value rather than an error, since it means "resync from scratch".Sync.get_allsplits an id list overmaxObjectsInGet(§5.1) and runs the batches in parallel, bounded by the session'smaxConcurrentRequests.
Jmap_eio.Cli wires the same choices to a command line
(--url, --api-key, --api-key-file, --account-id, --debug) and to the
environment (JMAP_SESSION_URL, JMAP_API_KEY, JMAP_API_KEY_FILE,
JMAP_AUTH = bearer | basic, JMAP_ACCOUNT_ID). Cli.create_client ~sw
and Cli.account_id are the two calls every example starts with, under an
Eio.Switch.run.
Chaining calls #
Jmap.Chain builds several method calls into a single request, with each call
able to refer to an earlier one's response. A chain ends in the handles whose
responses are wanted, and Client.run_exn sends it and hands back their
decoded responses in the same order:
let (Results.[ query; got ]) =
Jmap_eio.Client.run_exn client
Jmap.Chain.(
let* q =
email_query ~account_id
~filter:(Proto.Email.filter ~in_mailbox:inbox ())
~sort:[ Proto.Email.sort ~ascending:false `Received_at ]
~limit:10L ()
in
let+ g =
email_get ~account_id ~ids:(from_query q)
~properties:[ `Id; `Received_at; `From; `Subject ] ()
in
Handles.[ q; g ])
That list is typed, so the compiler knows how many responses come back and
what each one is. query.ids is a list of ids and got.list a list of
Proto.Email.t. A chain ending in a single handle goes to Client.call_exn,
which is that one response, and Client.chain_exn hands back the raw
Proto.Response.t for a caller that wants to print it. Client.call and
Client.run are the same two as results, reporting a server-side
Method_error (RFC 8620 §3.6.2) rather than raising; Jmap.Chain.attempt
turns one call's response into a result of its own, so a call that may
legitimately fail does not fail the read of the calls beside it.
Command-line tools #
jmap is a general-purpose client (mailboxes, search, read, send), and jmapq
holds specialist workflows. Both take their configuration from Jmap_eio.Cli,
so the flags and environment variables above apply.
Examples #
examples/ is a step-by-step tutorial in the shape of
Dream's: sixteen numbered directories, 1-session to g-raw, each a short
program that introduces one idea, with a README that shows the program, walks
through it, links the RFC section behind each idea and shows a real run.
After the tutorial come nine larger programs, each a whole mail-client task
(sync, search, reading, threads, sending, import and parse, filing, push,
blobs). Every example exits non-zero on an unexpected response, so the set
doubles as a coverage check:
dune exec -- examples/1-session/session.exe
scripts/run-examples.sh # run them all against a server
Testing against a real server #
test/ holds hermetic unit tests against JSON fixtures. test/oracle/ holds
tests that run against a real Cyrus IMAP server:
where the RFCs are ambiguous, what a shipping server does is the tiebreaker.
Nothing in test/oracle/ runs unless JMAP_ORACLE_URL is set, so a plain
dune runtest stays offline and hermetic — the oracle cases report as skipped.
scripts/oracle-up.sh # docker run, waits for /jmap to answer
eval "$(scripts/oracle-env.sh)" # exports JMAP_ORACLE_URL and _LMTP
dune build @test/oracle/runtest --force
scripts/oracle-down.sh # stop and remove the container
--force matters: without it dune caches the last successful run.
The oracle listens on localhost:18080 (HTTP/JMAP), 18024 (LMTP, used to
inject test messages) and 18001 (Cyrus master). Users user1 … user5 exist
in example.com and any password is accepted, which is why the oracle tests
use Basic auth with ~allow_insecure:true.
| variable | default | meaning |
|---|---|---|
JMAP_ORACLE_URL |
unset — tests skip | session or well-known URL |
JMAP_ORACLE_USER |
user1 |
login name |
JMAP_ORACLE_PASSWORD |
x |
password |
JMAP_ORACLE_DOMAIN |
example.com |
mail domain |
JMAP_ORACLE_LMTP |
localhost:18024 |
host:port of the LMTP listener |
JMAP_ORACLE_HTTP_PORT |
18080 |
read by oracle-up.sh only |
JMAP_ORACLE_IMAGE |
ghcr.io/cyrusimap/cyrus-docker-test-server:bookworm |
image to run |
scripts/run-examples.sh points the examples at the same server. It defaults
to the oracle and Basic auth, takes --seed N to deliver N messages over LMTP
first so the read-only examples have something to show, and reports pass/fail
per example:
scripts/oracle-up.sh
scripts/run-examples.sh --seed 2
See test/oracle/README.md for the harness and the
recorded Cyrus behaviour, and examples/README.md for
the examples themselves.
Documentation #
dune build @doc
The generated pages are doc/index.mld (the package index), doc/tutorial.mld
(a guided introduction whose examples are checked by mdx), and
doc/coverage.mld — a matrix of every RFC 8620 and RFC 8621 method against the
codecs, Chain builders, unit tests, oracle tests and examples that exercise
it, so gaps are visible rather than implied.
Installation #
opam install jmap
License #
ISC — see LICENSE.md.