Proof of concept for a tiny Cloudflare Worker that serves Go vanity import paths with just ~25 lines of JavaScript.
Currently powering
go.carr.sh.
Go vanity imports let you use a custom domain (e.g. go.carr.sh/litmus) as the canonical import path for your Go modules, while the source code lives on GitHub. This worker handles the handshake between the Go toolchain and your repos.
When the Go toolchain fetches a module it sends a ?go-get=1 query parameter. The worker responds with an HTML page containing a <meta name="go-import"> tag that points to the corresponding GitHub repository:
$ curl 'https://go.carr.sh/litmus?go-get=1'
<!DOCTYPE html><meta name="go-import" content="go.carr.sh/litmus git https://github.com/lukecarr/litmus">If a regular browser hits the same URL (without ?go-get=1), the worker redirects to the GitHub repo instead:
$ curl -is 'https://go.carr.sh/litmus' | grep location
location: https://github.com/lukecarr/litmusModules are registered in the MODULES object in src/index.js. Each property associates a module name (i.e. go.carr.sh/<module name>) with a repo.
In the below example, the litmus module is served at go.carr.sh/litmus and points to the lukecarr/litmus GitHub repo:
const MODULES = {
litmus: "https://github.com/lukecarr/litmus",
};The only prerequisite is Bun. The specific version of Bun is pinned in the packageManager entry of the package.json file.
Install the worker's dependencies with Bun:
bun installRun the worker in development mode with hot reloading:
bun run devRun tests with coverage reporting (we strive for 100% coverage):
bun test --coverageLint and format the codebase (with auto-fix) using Biome:
bun run checkThe worker is deployed to Cloudflare via Wrangler:
bun run deployRouting is configured in wrangler.jsonc to serve requests on the go.carr.sh custom domain.
This repo is licensed under the MIT License.