/affine-bun-workers

Workers for self-hosted instances of AFFiNE, made with Bun.

Primary LanguageTypeScript

AFFiNE Bun Workers

Linting Testing Publish image on Docker Hub

AFFiNE workers but for self-hosted Docker instances, using Bun and Elysia.

Content

Why ?

This repository serves for the development of workers for the self-hosted version of AFFiNE until a proper solution is added to the main repository.

The workers are basically a Bun server running and handling requests for proxying images and fetching metadata from a link.

This repository is NOT affiliated with AFFiNE in any way, it is just a temporary solution for self-hosted users. It tries to replicate the workers from AFFiNE as closely as possible.

Currently supported workers

  • Image proxy (/api/worker/image-proxy)
  • Link preview (/api/worker/link-preview)

Thanks

A big thanks to the AFFiNE team for creating such a great tool and to eikaramba for heavily inspiring this project with their work. Also a big thanks to Bun for simply existing.

Usage

Note

This section assumes you are using the Docker image of AFFiNE and have an instance running.

Use this Docker image to run the workers. Example in a classic compose file:

services:
  workers:
    image: yummyume/affine-bun-workers
    restart: unless-stopped
    container_name: affine_workers

  # Other services, AFFiNE, Postgres, etc.

The workers will now be available on the port 3000 of the container. The next step is to proxy requests from /api/worker/* to the workers container.

You can do this however you want, but here is an example using Caddy as a reverse proxy:

https://my-website.com {
  reverse_proxy /api/worker/* workers:3000
  reverse_proxy * affine:3010
}

Configuration

The following environment variables can be used on the service to configure the workers:

Variable Description Default Example
NODE_ENV The environment the workers are running in. You generally don't need to change this. production development
ELYSIA_PORT The port the workers will run on. 3000 4000
ELYSIA_ALLOWED_ORIGINS The allowed origins for the workers (CORS). You can probably leave this empty if you use the same domain for everything. You can add multiple origins separated by a comma. affine.my-website.com
ELYSIA_PREFIX The prefix for the workers. The server will prefix every route with this prefix. /api/worker /
ELYSIA_LOGGER_LEVEL The log levels to use for the workers. See the pino documentation. debug info

Stripping or rewriting the prefix

It's possible to tell your proxy to strip or rewrite the prefix before sending the request to the workers. In this case, you should set the ELYSIA_PREFIX to whatever the prefix will be after the rewrite. Example with Caddy and a full strip:

https://my-website.com {
  # handle_path will strip the prefix (in this case /api/worker) before sending the request to the workers
  handle_path /api/worker/* {
    reverse_proxy workers:3000
  }

  reverse_proxy * affine:3010
}

In this case, the ELYSIA_PREFIX should be set empty.

Development

  • (Optional) Change the .env values if needed with a .env.local and compose.override.yml file
  • Run docker compose up -d (or make up) to start the AFFiNE app and the workers
  • Go to http://localhost to access the AFFiNE app
  • Create a new dummy account, create a new doc and try pasting a link or an image to see results