# Architecture

> How Social Crawl is put together: the four apps and packages, how a post travels from a page to the library, where files and data live, and how the parts talk.

Source: https://crawl-docs.pdhq.win/advanced/architecture/
Last updated: 2026-12-02

Social Crawl is a Yarn monorepo. One owner signs in with one password, and everything is built around that.

## The parts

| Path | What it is | Runs where |
| --- | --- | --- |
| `apps/api` | NestJS 12 API with PostgreSQL (TypeORM). Writes bundles, serves files and ZIPs, runs the background jobs. | A container, port 4000 |
| `apps/web` | Next.js 16 app: the library UI. It is also a **backend for the frontend**: the login token never reaches browser JavaScript. | A container, port 3000 |
| `apps/extension` | Browser extension (Chrome, Manifest V3) written in plain TypeScript. Reads the page you are on and uploads it. | The browser |
| `apps/docs` | These docs: a Next.js static export built from Markdown. | Static files |
| `packages/schema` | The shared `zod` schemas: `Post`, the bundle manifest, the capture request, tags, restricted-site rules. | Imported by all |
| `packages/render` | Dependency-free renderer that turns a post into `index.html`, `post.txt` and Markdown, and the layout language. | Imported by api, web, extension tests |
| `deploy/` | The Dockerfiles. `docker-compose.yml` at the repository root is what Dokploy runs. | |

## The picture

```text
 browser extension ──(Bearer capture token)──▶ api :4000 ──▶ PostgreSQL (posts, tags, shares, settings)
        │                                        │  ▲
        │                                        │  └── events (server-sent) ──┐
        │                                        ▼                            │
        │                                  bundle storage: server disk │ S3/R2 │ + WebDAV tier
        │                                        ▲
 you in a browser ─▶ web :3000 (cookie ─ Bearer web token) ─ /api/... allowlisted proxy ─┘
                         └─ /s/<secret> pages ─▶ api public routes /share/... (no token)
```

## How a post gets in

1. **Read.** In the extension, a service worker runs a reader function in the page you clicked on (`chrome.scripting.executeScript`). It returns a `Post` (see [The Post schema](https://crawl-docs.pdhq.win/advanced/post-schema.md)) and a list of videos found. A small panel (the "island") is drawn on the page to show it.
2. **Send.** The extension calls `POST /capture` with its **capture** token. Videos go first through `POST /capture/uploads` in 16 MB pieces, or for videos over the limit through the big-upload routes, and the post refers to them by upload id.
3. **Validate.** The API checks the body against the shared `zod` schema. Every URL the server fetches itself goes through `fetchPublic`, which refuses private addresses.
4. **Write.** `BundleService` downloads the pictures and files, joins streamed videos with ffmpeg, renders `index.html`, `post.txt` and `post.md`, and writes `post.json` with the SHA-256 of every file. The bundle is stored under the key `<platform>/<post id>` in the **bundle storage**.
5. **Index.** A `bookmarks` row is written with the card data (cover, counts, size, author key, tags). If the post's site or account matches a restricted-part rule it is marked restricted.
6. **Announce.** An event goes out on `GET /events` (server-sent events), so every open library updates by itself.

## Storage layers

`BundleStorage` is an abstract class with three pieces behind it:

- **Local**: a folder on disk (`STORAGE_DIR`, the `bundles` volume).
- **S3-compatible**: Cloudflare R2 or any bucket (`STORAGE_DRIVER=s3`).
- **Tiered**: wraps either of them and puts large files on **WebDAV** when you set it up. A small `.webdav.json` in each bundle says which files are there. Reads (with `Range`) are served from whichever side holds the file, and a read cache keeps watched parts on disk.

Posts, tags, notes, share links, settings, big-upload state and layouts live in PostgreSQL. See [Data model](https://crawl-docs.pdhq.win/advanced/data-model.md).

## The web app is a proxy

The browser never sees the API token.

- On sign-in, `POST /api/session` sends your password to the API and stores the returned **web** token in an `httpOnly`, `sameSite=lax` cookie (`secure` in production), for 7 days.
- Every other call goes through `/api/[...path]`, a route handler that checks the request against an **allowlist** of method and path patterns (`apps/web/src/lib/proxy-allowlist.ts`), requires the cookie, adds the `Authorization` header, and forwards the response with the range headers a video needs.
- Anything that changes data must pass a same-origin check: the `Origin` header must match the host.
- `proxy.ts` redirects visitors without the cookie to `/login`, except for `/s/` (the share links, where the secret is the key).
- The restricted part has its own cookie, `sc_r`, kept only for the browser session, and sent to the API as `x-restricted-token`.

## Background work

The API runs long jobs itself, in the same process:

| Job | Where | Limits |
| --- | --- | --- |
| Smaller sizes for the player and facts about a video (`MediaJobsService`) | `bookmarks/media-jobs.ts` | A few at a time, the rest wait. |
| Conversion of big videos (`ConvertService`) | `big-uploads/` | One at a time, at low priority, at most 2 encoder threads. |
| Speech recognition | `bundles/speech-tools.ts` | One video at a time. |
| WebDAV copy and move | `webdav/` | Reports progress by events. |
| Database copy and delete | `migration/` | One at a time. |
| Recount of big-video sizes | `big-uploads/big-sizes.ts` | At start and every 10 minutes. |

`GET /jobs` lists them all for the queue in the top bar.

## Where to read next

- [Data model](https://crawl-docs.pdhq.win/advanced/data-model.md) for the tables.
- [API routes and scopes](https://crawl-docs.pdhq.win/advanced/api-routes.md) for every route.
- [Security notes](https://crawl-docs.pdhq.win/advanced/security.md).
- [Build, test and extend](https://crawl-docs.pdhq.win/advanced/build-test-extend.md) to change the code.
