All concepts
07 — Concept

Email and file storage

Mail that logs instead of failing when no provider is set, and file storage on R2 behind a small interface with a table in front of it.

Email: one fetch call to Resend, no SDK

Sending mail is one function over plain fetch against Resend's HTTP API. There is no vendor SDK in the bundle, because the whole surface being used is a single POST with a JSON body — and an SDK would be a dependency, a bundle-size cost and a Node-compatibility risk in exchange for nothing.

Templates share a shell and a call-to-action helper, and cover the magic link, the organisation invitation, the invitation-accepted notice and the access-request decision. The from address and the branding come from configuration, so changing them is not a code change.

With no API key, mail is logged rather than failed

With no mail provider configured, a message is not an error. It is written to the log — loudly, with any link printed on its own line — and reported as delivered-false. Nothing throws, nothing retries, and no code path branches on whether email "works".

Why degrade instead of fail. This is what makes a fresh clone signable-in with zero external credentials. Sign-in is a magic link; a magic link is an email; an email with no provider would normally be a hard stop before the first screen. Treating the absent provider as a delivery channel of last resort — your terminal — removes the only credential that would otherwise be mandatory on day one.

Which emails are queued, and which are sent inline

Invitation mail — created, bulk, resent — and the access-request decision notice are queued. Those routes answer as soon as the row exists, and a provider outage becomes a retry with backoff rather than a failed request.

The magic link is sent inline, on purpose. Somebody is sitting in front of the login form waiting for it, and moving it to a queue trades a latency they can feel for a durability they would never notice. When the work has a human waiting on it, latency wins.

The queued payload carries the link explicitly, so the console fallback still prints a usable accept URL when no provider is configured. The development path and the production path go through the same handler.

File storage: a small interface over R2

File storage sits behind a small service interface — put, get, head, delete, list — whose one implementation wraps the native R2 binding. Keys are built by a helper and always take the shape tenants/{tenantId}/{scope}/{uuid}-{filename}, so a single prefix scopes one tenant, or one scope within it, for listing or bulk deletion. The UUID means every key is unique whatever the client happened to call the file.

Downloads stream through the Worker, so every download passes the same organisation and visibility checks as every other route. Uploads do not have to: the next section is how a file gets in.

Uploads: ask, put, confirm

An upload takes three requests. The client first asks POST /api/uploads for a place to put the file, naming the kind of upload, the file's name, type and size. Every check that kind makes runs here, before a byte moves: permission, type, size, an empty file, and whether storage (and, for a document, conversion and embeddings) is configured at all. The answer is a PUT URL that lasts 10 minutes, recorded in a pending_uploads row.

The client then PUTs the bytes with the type and length it declared, and asks POST /api/uploads/:id/confirm. Confirm checks the stored object's size and type against the declaration — a mismatch is refused and the object deleted — and only then writes the files row, or the document. An upload belongs to its organisation and the person who started it: anyone else's upload id is a 404, and so is a second confirm. A nightly task deletes the objects of uploads nobody confirmed.

Where the PUT goes depends on one setting, and the client cannot tell which. With an R2 API token's key pair set, the Worker presigns a URL on R2's S3 API (the binding itself cannot presign), signing the type, the length and write-once, and the file goes straight to the bucket. Without the key pair the URL is the Worker's own, which holds the same rules itself; deployed, it takes at most 10 MB a file. Provisioning writes the bucket's CORS rule, and local development needs no R2 credentials.

Why direct uploads are opt-in. The FILES binding cannot presign, so a direct URL is signed against R2's S3 API and needs an R2 API token. Without one, every upload goes through the Worker's own URL, which holds the same rules, up to 10 MB a file deployed.

A direct upload is a single PUT, so 5 GiB is the ceiling, with no multipart upload and no resume. A presigned URL is not spent by the confirm: for its 10 minutes it can write the key again once the confirmed object is gone. The nightly task deletes such an object for the kit's own targets, and a plugin owns that cleanup for its own.

The files table is the only thing the browser can name

A files table records id, tenant, uploader, scope, key, filename, content type, size and creation time. Rows are immutable: there is no updated_at, because a file is not edited, it is replaced by another file. The browser only ever references a file by that row's id — never by a storage key.

Writes go object first and row second; if the insert fails, the object is deleted. Downloads are tenant-scoped lookups — another tenant's file is a 404, not a 403 — and carry an ETag from R2 so a repeat request is a 304.

Upload targets, and the download default

Each kind of upload is a target with its own rules: the kit's are avatars, uploads and documents, and a plugin adds its own. An avatar must be one of a short list of image types, every target is capped by size, and an empty file is rejected outright. Apart from the Worker's own upload URL, no core route takes a file in its request body.

Why almost everything downloads as an attachment. Only the avatar image types are served inline. Everything else — SVG very much included — is sent with a Content-Disposition: attachment header, so a stored HTML page or a scripted SVG cannot execute on the application's own origin against a live session. Rendering user-supplied markup inline is a stored-XSS primitive; the attachment header removes it, and the cost is that one class of file downloads instead of previewing.

A missing storage binding is a loud 503 rather than a silent no-op. Realtime can degrade quietly because nothing is lost; storage cannot, because an upload that appears to succeed and stores nothing is data loss.