How bulwarkmail/webmail works
Purpose and Runtime
Bulwark Webmail is a self-hosted Next.js 16/TypeScript webmail suite for Stalwart Mail Server, using JMAP as its primary backend protocol. It delivers mail, calendar, contacts, files, settings, PWA support, plugins, and administration. It runs as a Node/Next server, commonly in Docker: Dockerfile, docker-compose.yml, next.config.ts.
Application Entry and Routing
The App Router is the UI and server entry surface. The main shell, locale-scoped user workspace, setup flow, admin console, and sandboxed plugin pages are separated by route groups:
• app/(main)/layout.tsx, app/(main)/[locale]/layout.tsx
• app/(main)/setup/page.tsx, app/(main)/admin/layout.tsx
• app/(sandbox)/plugin-sandbox/page.tsx
Locale detection/routing and translation assembly form a cross-cutting boundary around user routes: i18n/routing.ts, i18n/request.ts, locales/en/common.json.
Client State and JMAP Data Plane
Zustand stores own client-side application state for accounts, auth, mail, calendars, contacts, files, settings, and plugins. UI components consume these stores; stores use the custom JMAP client and account-state coordination to operate across one or multiple accounts.
• stores/account-store.ts, stores/email-store.ts, stores/calendar-store.ts
• lib/jmap/client.ts, lib/account-state-manager.ts, lib/unified-mailbox.ts
The JMAP boundary isolates RFC 8620 calls, capability types, search, and Stalwart-specific passthrough. Stalwart integration also exposes JMAP through a server route where needed: lib/jmap/client-interface.ts, lib/stalwart/jmap-passthrough.ts, app/api/account/stalwart/jmap/route.ts.
User Workspace Modules
The locale workspace hosts the primary mail experience plus dedicated calendar, contacts, and files pages. Feature components render data managed by the corresponding stores and JMAP capabilities.
• app/(main)/[locale]/page.tsx, components/email/email-composer.tsx, components/email/email-viewer.tsx
• app/(main)/[locale]/calendar/page.tsx, components/calendar/calendar-month-view.tsx
• app/(main)/[locale]/contacts/page.tsx, app/(main)/[locale]/files/page.tsx, components/files/file-browser.tsx
Cross-feature protocols include CalDAV discovery/subscriptions, WebDAV/file operations, iCalendar fetching, and browser mailto:/webcal: handling: app/api/caldav/discover/route.ts, app/api/webdav/route.ts, app/(main)/protocol/mailto/page.tsx.
Authentication and Security Boundary
Authentication supports JMAP credentials, OAuth/OIDC discovery and PKCE/token exchange, SSO reauthentication, pairing, session management, impersonation, and TOTP-related exchange. Session credentials are encrypted and cookie-backed; external identity-provider interaction remains server-side API logic.
• app/api/auth/session/route.ts, app/api/auth/sso/start/route.ts, app/api/auth/token/route.ts
• lib/auth/crypto.ts, lib/auth/session-cookie.ts, lib/oauth/pkce-server.ts
Setup and Administrative Control Plane
First-launch setup persists administrator-managed configuration, branding, security, and JMAP server configuration; environment values override this managed state. The separate admin UI/API controls policy, audit, marketplace, plugins, themes, telemetry, and version checks.
• app/api/setup/step/route.ts, lib/setup/state.ts, lib/admin/config-manager.ts
• app/(main)/admin/page.tsx, app/api/admin/config/route.ts, app/api/admin/plugins/route.ts
Persistent deployment state is filesystem-backed under configured admin/state/settings directories, not a database. External services may include JMAP/Stalwart, OAuth/OIDC issuers, an extension directory, and web-push endpoints.
Plugin and Theme Extension Boundary
Plugins are installed and governed by admin APIs, validated/signed, then projected into UI slots. Third-party execution is isolated through sandbox pages, iframe bridging, consent, integrity/signature checks, tiering, and restricted host APIs.
• lib/plugin-loader.ts, lib/plugin-validator.ts, lib/plugin-sandbox/runtime.tsx
• components/plugins/plugin-slot-renderer.tsx, app/(sandbox)/plugin-sandbox-privileged/page.tsx
• lib/theme-compiler.ts, lib/theme-loader.ts
Operational Interfaces
Health, runtime configuration, update status, PWA assets, push-preview, telemetry, and error instrumentation are server-facing operational surfaces. Static service-worker and manifest support installation/offline browser integration.
• app/api/health/route.ts, app/api/config/route.ts, app/api/system/update-status/route.ts
• app/manifest.ts, public/sw.js, instrumentation.node.ts
Main entry points: layout.tsx App shell & locale workspace · account-store.ts Application stores · route.ts Admin APIs · plugin-loader.ts Plugin & theme lifecycle
Components 21
App routes
- App shell & locale workspace · Main layout, localized workspace, and setup entry.
app/(main)/[locale]/layout.tsx - Mail workspace · Mail views and composer.
app/(main)/[locale]/page.tsx - Calendar workspace
app/(main)/[locale]/calendar/page.tsx - Contacts & files · Dedicated contacts and file browser pages.
app/(main)/[locale]/files/page.tsx - Protocol handlers · mailto/webcal, CalDAV discovery, and WebDAV.
app/(main)/protocol/mailto/page.tsx - Admin console
app/(main)/admin/page.tsx - Plugin sandbox · Iframe-hosted third-party plugin execution.
app/(sandbox)/plugin-sandbox/page.tsx
Client data plane
- Locale routing & translations
i18n/routing.ts - Application stores · Account, mail, calendar, contacts, files, settings, and plugins.
stores/account-store.ts - Account coordination · Coordinates multi-account state and unified mailboxes.
lib/account-state-manager.ts - JMAP client · RFC 8620 calls, capabilities, search, and Stalwart passthrough.
lib/jmap/client.ts
Server control plane
- Auth & sessions · Credentials, OAuth/OIDC PKCE, SSO, pairing, and encrypted cookies.
app/api/auth/session/route.ts - Setup & managed config · First-launch, branding, security, and JMAP configuration.
lib/setup/state.ts - Admin APIs · Policy, audit, marketplace, plugins, telemetry, and updates.
app/api/admin/config/route.ts - Filesystem state · Configured admin, state, and settings directories.
- Operational interfaces · Health, runtime config, updates, push preview, and instrumentation.
app/api/health/route.ts - Stalwart JMAP route · Server-side JMAP passthrough.
app/api/account/stalwart/jmap/route.ts
Extensions
- Plugin & theme lifecycle · Validation, signing, installation, projection, and theme loading.
lib/plugin-loader.ts
External systems
- Stalwart Mail Server
- OAuth/OIDC issuers
- Extension & push services · Extension directory and web-push endpoints.
Connections 23
- App shell & locale workspace → Locale routing & translations: localizes
- App shell & locale workspace → Mail workspace: hosts
- App shell & locale workspace → Calendar workspace: hosts
- App shell & locale workspace → Contacts & files: hosts
- Mail workspace → Application stores: uses
- Calendar workspace → Application stores: uses
- Contacts & files → Application stores: uses
- Protocol handlers → JMAP client: uses
- Application stores → Account coordination: coordinates
- Account coordination → JMAP client: operates
- JMAP client → Stalwart Mail Server: JMAP
- Stalwart JMAP route → Stalwart Mail Server: passthrough
- Auth & sessions → OAuth/OIDC issuers: OAuth/OIDC
- App shell & locale workspace → Auth & sessions: authenticates via
- Setup & managed config → Filesystem state: persists
- Admin console → Admin APIs: manages through
- Admin APIs → Setup & managed config: updates
- Admin APIs → Filesystem state: persists
- Admin APIs → Plugin & theme lifecycle: governs
- Plugin & theme lifecycle → Plugin sandbox: loads isolated plugins
- Plugin sandbox → Application stores: restricted host bridge
- Plugin & theme lifecycle → Extension & push services: directory & push
- Operational interfaces → Filesystem state: reads runtime state