Architecture
Updated
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.
On this page
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#
- Read. In the extension, a service worker runs a reader function in the page you clicked on (
chrome.scripting.executeScript). It returns aPost(see The Post schema) and a list of videos found. A small panel (the "island") is drawn on the page to show it. - Send. The extension calls
POST /capturewith its capture token. Videos go first throughPOST /capture/uploadsin 16 MB pieces, or for videos over the limit through the big-upload routes, and the post refers to them by upload id. - Validate. The API checks the body against the shared
zodschema. Every URL the server fetches itself goes throughfetchPublic, which refuses private addresses. - Write.
BundleServicedownloads the pictures and files, joins streamed videos with ffmpeg, rendersindex.html,post.txtandpost.md, and writespost.jsonwith the SHA-256 of every file. The bundle is stored under the key<platform>/<post id>in the bundle storage. - Index. A
bookmarksrow 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. - 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, thebundlesvolume). - 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.jsonin each bundle says which files are there. Reads (withRange) 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.
The web app is a proxy#
The browser never sees the API token.
- On sign-in,
POST /api/sessionsends your password to the API and stores the returned web token in anhttpOnly,sameSite=laxcookie (securein 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 theAuthorizationheader, and forwards the response with the range headers a video needs. - Anything that changes data must pass a same-origin check: the
Originheader must match the host. proxy.tsredirects 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 asx-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 for the tables.
- API routes and scopes for every route.
- Security notes.
- Build, test and extend to change the code.