# khromov/mochi architecture

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

- Interactive diagram: https://gitdiagram.com/khromov/mochi
- Repository: https://github.com/khromov/mochi

## Overview

#### Purpose and Workspace Shape
Mochi is a Bun-only SSR framework for Svelte 5, centered on islands-based selective hydration and small client bundles. This monorepo contains the framework, app scaffolder, reference/template apps, documentation, and demos. Framework public entry points are `packages/mochi/src/index.ts` and CLI entry points `packages/mochi/src/cli.ts`; development orchestration starts from `scripts/dev.ts`.

#### Framework Startup, Configuration, and Serving
`Mochi` owns application startup and request-serving integration over `Bun.serve()`. It reads framework and Svelte configuration, establishes public assets and request setup, and exposes application-facing APIs. Key boundaries are framework configuration versus user route/component code, and Bun runtime APIs versus portable framework abstractions.
- `packages/mochi/src/Mochi.ts`
- `packages/mochi/src/mochiConfig.ts`
- `packages/mochi/src/requestSetup.ts`

#### Build and Compilation Pipeline
The build stage compiles Svelte application code, tracks bundle metadata, preprocesses source, and creates server/client artifacts. It protects server-only imports from client leakage and caches compilation/preprocessing work. Build output drives runtime component resolution and island manifests.
- `packages/mochi/src/build.ts`
- `packages/mochi/src/compileCache.ts`
- `packages/mochi/src/serverOnlyScan.ts`

#### SSR Rendering and Component Registry
A request resolves to SSR rendering through the component registry, which manages compiled components and fallback/reload behavior. HTML shell and head handling separate framework-provided document structure from app page rendering; error handling supplies failure boundaries/default pages.
- `packages/mochi/src/ComponentRegistry.ts`
- `packages/mochi/src/templates/default-shell.html`
- `packages/mochi/src/errors.ts`

#### Islands and Client Runtime
Selective hydration is the defining execution boundary: server output registers island props and emits browser web components; only marked/hydratable components receive client bundles and hydration behavior. Server islands can render through isolated endpoints, with crypto protecting payloads and CSS tracked across nested island responses.
- `packages/mochi/src/islandPropsRegistry.ts`
- `packages/mochi/src/web-components/HydratableIsland.ts`
- `packages/mochi/src/web-components/ServerIsland.ts`

#### HTTP Features and Stateful Services
Framework request features include cookies, CSRF, enhanced forms, middleware, caching, queues, events, and proxy support. These operate in the Bun server process; optional persistence includes Bun SQLite and filesystem-backed cache storage. Email supports rendering Svelte emails, development outbox/viewer, and configured transports.
- `packages/mochi/src/cache.ts`
- `packages/mochi/src/forms.ts`
- `packages/mochi/src/email/mailer.ts`

#### Consumer Applications and Scaffolding
The documentation site is itself a Mochi application and the broadest integration consumer; its route assembly begins in `packages/site/src/index.ts` and `packages/site/src/routes.ts`. Standalone demos exercise framework features, while the minimal app is the template basis. `create-mochi` copies/templates this starter for npm users.
- `packages/site/src/index.ts`
- `packages/demos/src/index.ts`
- `packages/cli/src/create.ts`

#### Runtime and Deployment
Bun (minimum version pinned in `.bun-version`) is the required runtime for development, build, serving, and optional SQLite. Production docs and demos are independently containerized and deployed as separate CapRover applications via GHCR images. Docker boundaries are `Dockerfile` for the site and `packages/demos/Dockerfile.production` for demos; CI deployment is defined in `.github/workflows/build.yml`.

Main entry points: [`packages/mochi/src/Mochi.ts`](https://github.com/khromov/mochi/blob/main/packages/mochi/src/Mochi.ts) (Mochi server), [`packages/mochi/src/ComponentRegistry.ts`](https://github.com/khromov/mochi/blob/main/packages/mochi/src/ComponentRegistry.ts) (Component registry), [`packages/mochi/src/build.ts`](https://github.com/khromov/mochi/blob/main/packages/mochi/src/build.ts) (Build pipeline), [`packages/mochi/src/index.ts`](https://github.com/khromov/mochi/blob/main/packages/mochi/src/index.ts) (Framework public API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_framework["Mochi framework"]
  node_public_api["Framework public API<br/>package entry<br/>[index.ts]"]
  node_mochi["Mochi server<br/>application runtime<br/>[Mochi.ts]"]
  node_config["Framework configuration<br/>configuration loader<br/>[mochiConfig.ts]"]
  node_request_setup["Request setup<br/>request pipeline<br/>[requestSetup.ts]"]
  node_build["Build pipeline<br/>Svelte compiler<br/>[build.ts]"]
  node_compile_cache[("Compilation cache<br/>build cache<br/>[compileCache.ts]")]
  node_server_scan["Server-only import scan<br/>build guard<br/>[serverOnlyScan.ts]"]
  node_registry["Component registry<br/>SSR resolver"]
  node_shell["Default HTML shell<br/>SSR template<br/>[default-shell.html]"]
  node_errors["Error boundaries<br/>SSR errors<br/>[errors.ts]"]
  node_island_props[("Island props registry<br/>island manifest")]
  node_hydration["Hydratable islands<br/>browser web component"]
  node_server_islands["Server islands<br/>isolated SSR endpoint<br/>[ServerIsland.ts]"]
  node_http_features["HTTP and state features<br/>server services<br/>[forms.ts]"]
  node_email["Email service<br/>mailer<br/>[mailer.ts]"]
end

subgraph group_apps["Consumers and tooling"]
  node_dev["Development orchestration<br/>dev script<br/>[dev.ts]"]
  node_cli["create-mochi<br/>scaffolding CLI<br/>[create.ts]"]
  node_site["Documentation site<br/>Mochi application<br/>[index.ts]"]
  node_demos["Feature demos<br/>Mochi application<br/>[index.ts]"]
end

subgraph group_deployment["Runtime and deployment"]
  node_bun{{"Bun runtime<br/>required runtime<br/>[.bun-version]"}}
  node_site_container["Site container<br/>production image"]
  node_demos_container["Demos container<br/>production image"]
  node_deploy["CI deployment<br/>CapRover deployment<br/>[build.yml]"]
end

node_dev -->|"runs on"| node_bun
node_public_api -->|"exposes"| node_mochi
node_mochi -->|"loads"| node_config
node_mochi -->|"initializes"| node_request_setup
node_mochi -->|"resolves requests through"| node_registry
node_mochi -->|"hosts"| node_http_features
node_mochi -->|"serves via Bun.serve"| node_bun
node_build -->|"caches compilation"| node_compile_cache
node_build -->|"validates imports"| node_server_scan
node_build -->|"supplies compiled components"| node_registry
node_build -->|"creates island metadata"| node_island_props
node_registry -->|"renders inside"| node_shell
node_registry -->|"uses failure boundaries"| node_errors
node_registry -->|"registers island props"| node_island_props
node_island_props -->|"drives browser hydration"| node_hydration
node_registry -->|"renders isolated islands"| node_server_islands
node_http_features -->|"supports email workflows"| node_email
node_cli -.->|"targets framework"| node_public_api
node_site -->|"consumes"| node_public_api
node_demos -->|"exercises"| node_public_api
node_site -->|"packaged as"| node_site_container
node_demos -->|"packaged as"| node_demos_container
node_deploy -->|"builds and deploys"| node_site_container
node_deploy -->|"builds and deploys"| node_demos_container

click node_dev "https://github.com/khromov/mochi/blob/main/scripts/dev.ts"
click node_public_api "https://github.com/khromov/mochi/blob/main/packages/mochi/src/index.ts"
click node_mochi "https://github.com/khromov/mochi/blob/main/packages/mochi/src/Mochi.ts"
click node_config "https://github.com/khromov/mochi/blob/main/packages/mochi/src/mochiConfig.ts"
click node_request_setup "https://github.com/khromov/mochi/blob/main/packages/mochi/src/requestSetup.ts"
click node_build "https://github.com/khromov/mochi/blob/main/packages/mochi/src/build.ts"
click node_compile_cache "https://github.com/khromov/mochi/blob/main/packages/mochi/src/compileCache.ts"
click node_server_scan "https://github.com/khromov/mochi/blob/main/packages/mochi/src/serverOnlyScan.ts"
click node_registry "https://github.com/khromov/mochi/blob/main/packages/mochi/src/ComponentRegistry.ts"
click node_shell "https://github.com/khromov/mochi/blob/main/packages/mochi/src/templates/default-shell.html"
click node_errors "https://github.com/khromov/mochi/blob/main/packages/mochi/src/errors.ts"
click node_island_props "https://github.com/khromov/mochi/blob/main/packages/mochi/src/islandPropsRegistry.ts"
click node_hydration "https://github.com/khromov/mochi/blob/main/packages/mochi/src/web-components/HydratableIsland.ts"
click node_server_islands "https://github.com/khromov/mochi/blob/main/packages/mochi/src/web-components/ServerIsland.ts"
click node_http_features "https://github.com/khromov/mochi/blob/main/packages/mochi/src/forms.ts"
click node_email "https://github.com/khromov/mochi/blob/main/packages/mochi/src/email/mailer.ts"
click node_cli "https://github.com/khromov/mochi/blob/main/packages/cli/src/create.ts"
click node_site "https://github.com/khromov/mochi/blob/main/packages/site/src/index.ts"
click node_demos "https://github.com/khromov/mochi/blob/main/packages/demos/src/index.ts"
click node_bun "https://github.com/khromov/mochi/blob/main/.bun-version"
click node_site_container "https://github.com/khromov/mochi/tree/main/Dockerfile"
click node_demos_container "https://github.com/khromov/mochi/blob/main/packages/demos/Dockerfile.production"
click node_deploy "https://github.com/khromov/mochi/blob/main/.github/workflows/build.yml"

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_public_api,node_mochi,node_config,node_request_setup,node_build,node_compile_cache,node_server_scan,node_registry,node_shell,node_errors,node_island_props,node_hydration,node_server_islands,node_http_features,node_email toneBlue
class node_dev,node_cli,node_site,node_demos toneAmber
class node_bun,node_site_container,node_demos_container,node_deploy toneMint
```

## Components

### Mochi framework

Bun-only SSR framework and runtime features.

- [Framework public API](https://github.com/khromov/mochi/blob/main/packages/mochi/src/index.ts): package entry · `packages/mochi/src/index.ts`
- [Mochi server](https://github.com/khromov/mochi/blob/main/packages/mochi/src/Mochi.ts): application runtime · Owns startup and Bun request serving. · `packages/mochi/src/Mochi.ts`
- [Framework configuration](https://github.com/khromov/mochi/blob/main/packages/mochi/src/mochiConfig.ts): configuration loader · `packages/mochi/src/mochiConfig.ts`
- [Request setup](https://github.com/khromov/mochi/blob/main/packages/mochi/src/requestSetup.ts): request pipeline · `packages/mochi/src/requestSetup.ts`
- [Build pipeline](https://github.com/khromov/mochi/blob/main/packages/mochi/src/build.ts): Svelte compiler · Produces server/client artifacts and island metadata. · `packages/mochi/src/build.ts`
- [Compilation cache](https://github.com/khromov/mochi/blob/main/packages/mochi/src/compileCache.ts): build cache · `packages/mochi/src/compileCache.ts`
- [Server-only import scan](https://github.com/khromov/mochi/blob/main/packages/mochi/src/serverOnlyScan.ts): build guard · Prevents server imports from leaking into client bundles. · `packages/mochi/src/serverOnlyScan.ts`
- [Component registry](https://github.com/khromov/mochi/blob/main/packages/mochi/src/ComponentRegistry.ts): SSR resolver · Manages compiled components, fallback, and reload behavior. · `packages/mochi/src/ComponentRegistry.ts`
- [Default HTML shell](https://github.com/khromov/mochi/blob/main/packages/mochi/src/templates/default-shell.html): SSR template · `packages/mochi/src/templates/default-shell.html`
- [Error boundaries](https://github.com/khromov/mochi/blob/main/packages/mochi/src/errors.ts): SSR errors · `packages/mochi/src/errors.ts`
- [Island props registry](https://github.com/khromov/mochi/blob/main/packages/mochi/src/islandPropsRegistry.ts): island manifest · Registers props for browser island execution. · `packages/mochi/src/islandPropsRegistry.ts`
- [Hydratable islands](https://github.com/khromov/mochi/blob/main/packages/mochi/src/web-components/HydratableIsland.ts): browser web component · Loads client bundles only for marked components. · `packages/mochi/src/web-components/HydratableIsland.ts`
- [Server islands](https://github.com/khromov/mochi/blob/main/packages/mochi/src/web-components/ServerIsland.ts): isolated SSR endpoint · Protects payloads and tracks nested-island CSS. · `packages/mochi/src/web-components/ServerIsland.ts`
- [HTTP and state features](https://github.com/khromov/mochi/blob/main/packages/mochi/src/forms.ts): server services · Forms, cookies, CSRF, middleware, cache, queues, events, and proxy support. · `packages/mochi/src/forms.ts`
- [Email service](https://github.com/khromov/mochi/blob/main/packages/mochi/src/email/mailer.ts): mailer · Renders Svelte emails with outbox/viewer and transports. · `packages/mochi/src/email/mailer.ts`

### Consumers and tooling

Applications, scaffolding, and development entry points.

- [Development orchestration](https://github.com/khromov/mochi/blob/main/scripts/dev.ts): dev script · `scripts/dev.ts`
- [create-mochi](https://github.com/khromov/mochi/blob/main/packages/cli/src/create.ts): scaffolding CLI · Copies/templates the starter application. · `packages/cli/src/create.ts`
- [Documentation site](https://github.com/khromov/mochi/blob/main/packages/site/src/index.ts): Mochi application · Broad integration consumer and route assembly. · `packages/site/src/index.ts`
- [Feature demos](https://github.com/khromov/mochi/blob/main/packages/demos/src/index.ts): Mochi application · `packages/demos/src/index.ts`

### Runtime and deployment

Bun execution and independently deployed applications.

- [Bun runtime](https://github.com/khromov/mochi/blob/main/.bun-version): required runtime · Runs development, builds, serving, and optional SQLite. · `.bun-version`
- [Site container](https://github.com/khromov/mochi/tree/main/Dockerfile): production image · `Dockerfile`
- [Demos container](https://github.com/khromov/mochi/blob/main/packages/demos/Dockerfile.production): production image · `packages/demos/Dockerfile.production`
- [CI deployment](https://github.com/khromov/mochi/blob/main/.github/workflows/build.yml): CapRover deployment · Builds GHCR images and deploys site and demos separately. · `.github/workflows/build.yml`

## Connections

- Development orchestration → Bun runtime: runs on
- Framework public API → Mochi server: exposes
- Mochi server → Framework configuration: loads
- Mochi server → Request setup: initializes
- Mochi server → Component registry: resolves requests through
- Mochi server → HTTP and state features: hosts
- Mochi server → Bun runtime: serves via Bun.serve
- Build pipeline → Compilation cache: caches compilation
- Build pipeline → Server-only import scan: validates imports
- Build pipeline → Component registry: supplies compiled components
- Build pipeline → Island props registry: creates island metadata
- Component registry → Default HTML shell: renders inside
- Component registry → Error boundaries: uses failure boundaries
- Component registry → Island props registry: registers island props
- Island props registry → Hydratable islands: drives browser hydration
- Component registry → Server islands: renders isolated islands
- HTTP and state features → Email service: supports email workflows
- create-mochi → Framework public API: targets framework
- Documentation site → Framework public API: consumes
- Feature demos → Framework public API: exercises
- Documentation site → Site container: packaged as
- Feature demos → Demos container: packaged as
- CI deployment → Site container: builds and deploys
- CI deployment → Demos container: builds and deploys
