# quietfox42/midnight-card architecture

> Architecture diagram of the GitHub repository quietfox42/midnight-card, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/quietfox42/midnight-card
- Repository: https://github.com/quietfox42/midnight-card
- Video tour (about a minute): https://gitdiagram.com/quietfox42/midnight-card/video

## Overview

Midnight Card is a public, temporary-email service: a visitor gets a random or custom address, incoming mail is parsed and stored, and the browser polls to display messages and extracted verification codes or links. The Worker handles email events, API requests, and scheduled cleanup; D1 holds mail, while static assets serve the web app. The graph preserves both the main receive-and-view journey and address customization, extraction, and retention. Some relationships are documented in the README or source index; unsampled implementation details are not inferred.

Main entry points: [`src/web/App.tsx`](https://github.com/quietfox42/midnight-card/blob/main/src/web/App.tsx) (Web app), [`src/worker/index.ts`](https://github.com/quietfox42/midnight-card/blob/main/src/worker/index.ts) (Worker entry), [`migrations/0001_init.sql`](https://github.com/quietfox42/midnight-card/blob/main/migrations/0001_init.sql) (D1 mail database), [`src/worker/email.ts`](https://github.com/quietfox42/midnight-card/blob/main/src/worker/email.ts) (Email handler)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web experience"]
  node_web_app["Web app<br/>[App.tsx]"]
  node_address_rules["Address rules<br/>[address.ts]"]
  node_address_controls["Address controls<br/>[PrefixSheet.tsx]"]
  node_mail_card["Address card<br/>[MidnightCard.tsx]"]
  node_inbox_polling["Inbox polling<br/>[useInbox.ts]"]
  node_api_client["API client<br/>[api.ts]"]
  node_inbox_view["Inbox list<br/>[Inbox.tsx]"]
  node_mail_detail["Mail detail<br/>[MailDetail.tsx]"]
end

subgraph group_mail["Mail processing"]
  node_email_handler["Email handler<br/>[email.ts]"]
  node_extractor["Code and link extraction<br/>[extract.ts]"]
end

subgraph group_runtime["Worker runtime"]
  node_worker["Worker entry<br/>[index.ts]"]
  node_api_handler["HTTP API<br/>[api.ts]"]
  node_cleanup["Expiry cleanup<br/>[cleanup.ts]"]
  node_configuration["Runtime config<br/>[env.ts]"]
end

subgraph group_state["Mail state"]
  node_d1[("D1 mail database<br/>[0001_init.sql]")]
end

node_visitor(("Visitor"))
node_sender(("Mail sender"))
node_email_routing{{"Email Routing"}}
node_static_assets{{"Static assets"}}
node_cron["Hourly Cron"]

node_visitor -->|"opens app"| node_static_assets
node_static_assets -->|"serves"| node_web_app
node_web_app -->|"requests config"| node_api_client
node_web_app -->|"renders"| node_mail_card
node_web_app -->|"renders"| node_inbox_view
node_web_app -->|"opens"| node_address_controls
node_web_app -->|"validates prefix"| node_address_rules
node_web_app -->|"starts polling"| node_inbox_polling
node_inbox_view -->|"opens message"| node_mail_detail
node_inbox_polling -->|"fetches pages"| node_api_client
node_api_client -->|"calls /api"| node_worker
node_visitor -->|"sends mail"| node_email_routing
node_email_routing -->|"routes email"| node_worker
node_worker -->|"dispatches email"| node_email_handler
node_worker -->|"dispatches HTTP"| node_api_handler
node_worker -->|"runs scheduled cleanup"| node_cleanup
node_email_handler -->|"extracts content"| node_extractor
node_email_handler -->|"stores mail"| node_d1
node_api_handler -->|"reads mail"| node_d1
node_cleanup -->|"deletes expired mail"| node_d1
node_cron -->|"triggers hourly"| node_worker
node_worker -->|"loads settings"| node_configuration
node_configuration -->|"uses shared rules"| node_address_rules
node_extractor -->|"persists extracted fields"| node_d1

click node_web_app "https://github.com/quietfox42/midnight-card/blob/main/src/web/App.tsx"
click node_address_rules "https://github.com/quietfox42/midnight-card/blob/main/src/shared/address.ts"
click node_address_controls "https://github.com/quietfox42/midnight-card/blob/main/src/web/components/PrefixSheet.tsx"
click node_mail_card "https://github.com/quietfox42/midnight-card/blob/main/src/web/components/card/MidnightCard.tsx"
click node_inbox_polling "https://github.com/quietfox42/midnight-card/blob/main/src/web/lib/useInbox.ts"
click node_api_client "https://github.com/quietfox42/midnight-card/blob/main/src/web/lib/api.ts"
click node_inbox_view "https://github.com/quietfox42/midnight-card/blob/main/src/web/components/Inbox.tsx"
click node_mail_detail "https://github.com/quietfox42/midnight-card/blob/main/src/web/components/MailDetail.tsx"
click node_worker "https://github.com/quietfox42/midnight-card/blob/main/src/worker/index.ts"
click node_api_handler "https://github.com/quietfox42/midnight-card/blob/main/src/worker/api.ts"
click node_email_handler "https://github.com/quietfox42/midnight-card/blob/main/src/worker/email.ts"
click node_extractor "https://github.com/quietfox42/midnight-card/blob/main/src/worker/extract.ts"
click node_cleanup "https://github.com/quietfox42/midnight-card/blob/main/src/worker/cleanup.ts"
click node_configuration "https://github.com/quietfox42/midnight-card/blob/main/src/worker/env.ts"
click node_d1 "https://github.com/quietfox42/midnight-card/blob/main/migrations/0001_init.sql"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_web_app,node_address_rules,node_address_controls,node_mail_card,node_inbox_polling,node_api_client,node_inbox_view,node_mail_detail toneBlue
class node_email_handler,node_extractor toneAmber
class node_worker,node_api_handler,node_cleanup,node_configuration toneMint
class node_d1 toneRose
class node_visitor,node_sender,node_email_routing,node_static_assets,node_cron toneIndigo
```

## Components

### Web experience

- [Web app](https://github.com/quietfox42/midnight-card/blob/main/src/web/App.tsx): `src/web/App.tsx`
- [Address rules](https://github.com/quietfox42/midnight-card/blob/main/src/shared/address.ts): `src/shared/address.ts`
- [Address controls](https://github.com/quietfox42/midnight-card/blob/main/src/web/components/PrefixSheet.tsx): `src/web/components/PrefixSheet.tsx`
- [Address card](https://github.com/quietfox42/midnight-card/blob/main/src/web/components/card/MidnightCard.tsx): `src/web/components/card/MidnightCard.tsx`
- [Inbox polling](https://github.com/quietfox42/midnight-card/blob/main/src/web/lib/useInbox.ts): `src/web/lib/useInbox.ts`
- [API client](https://github.com/quietfox42/midnight-card/blob/main/src/web/lib/api.ts): `src/web/lib/api.ts`
- [Inbox list](https://github.com/quietfox42/midnight-card/blob/main/src/web/components/Inbox.tsx): `src/web/components/Inbox.tsx`
- [Mail detail](https://github.com/quietfox42/midnight-card/blob/main/src/web/components/MailDetail.tsx): `src/web/components/MailDetail.tsx`

### Mail processing

- [Email handler](https://github.com/quietfox42/midnight-card/blob/main/src/worker/email.ts): `src/worker/email.ts`
- [Code and link extraction](https://github.com/quietfox42/midnight-card/blob/main/src/worker/extract.ts): `src/worker/extract.ts`

### Worker runtime

- [Worker entry](https://github.com/quietfox42/midnight-card/blob/main/src/worker/index.ts): `src/worker/index.ts`
- [HTTP API](https://github.com/quietfox42/midnight-card/blob/main/src/worker/api.ts): `src/worker/api.ts`
- [Expiry cleanup](https://github.com/quietfox42/midnight-card/blob/main/src/worker/cleanup.ts): `src/worker/cleanup.ts`
- [Runtime config](https://github.com/quietfox42/midnight-card/blob/main/src/worker/env.ts): `src/worker/env.ts`

### Mail state

- [D1 mail database](https://github.com/quietfox42/midnight-card/blob/main/migrations/0001_init.sql): `migrations/0001_init.sql`

### Other components

- **Visitor**
- **Mail sender**
- **Email Routing**
- **Static assets**
- **Hourly Cron**

## Connections

- Visitor → Static assets: opens app (evidence: `README.md`)
- Static assets → Web app: serves (evidence: `README.md`)
- Web app → API client: requests config (evidence: `src/web/App.tsx`)
- Web app → Address card: renders (evidence: `src/web/App.tsx`)
- Web app → Inbox list: renders (evidence: `src/web/App.tsx`)
- Web app → Address controls: opens (evidence: `src/web/App.tsx`)
- Web app → Address rules: validates prefix (evidence: `src/web/App.tsx`)
- Web app → Inbox polling: starts polling (evidence: `src/web/App.tsx`)
- Inbox list → Mail detail: opens message (evidence: `src/web/components/Inbox.tsx`)
- Inbox polling → API client: fetches pages (evidence: `src/web/lib/useInbox.ts`)
- API client → Worker entry: calls /api (evidence: `README.md`)
- Visitor → Email Routing: sends mail (evidence: `README.md`)
- Email Routing → Worker entry: routes email (evidence: `README.md`)
- Worker entry → Email handler: dispatches email (evidence: `src/worker/index.ts`)
- Worker entry → HTTP API: dispatches HTTP (evidence: `src/worker/index.ts`)
- Worker entry → Expiry cleanup: runs scheduled cleanup (evidence: `src/worker/index.ts`)
- Email handler → Code and link extraction: extracts content (evidence: `src/worker/email.ts`)
- Email handler → D1 mail database: stores mail (evidence: `README.md`)
- HTTP API → D1 mail database: reads mail (evidence: `README.md`)
- Expiry cleanup → D1 mail database: deletes expired mail (evidence: `README.md`)
- Hourly Cron → Worker entry: triggers hourly (evidence: `README.md`)
- Worker entry → Runtime config: loads settings (evidence: `src/worker/index.ts`)
- Runtime config → Address rules: uses shared rules (evidence: `src/worker/env.ts`)
- Code and link extraction → D1 mail database: persists extracted fields (evidence: `README.md`)
