Skip to content

Part 3: A Fork per User

src/Api.ts
const userSandbox = Effect.fn("userSandbox")(function* (user: string) {
return (yield* sandboxes.get(`user-${user}`)) ?? (yield* sandboxes.create({ name: `user-${user}` }));
});

A user’s first request forks Golden, with the repository already cloned. The fork is named after the user, so sandboxes.get finds it again on their next request.

src/Api.ts
const match = /^\/users\/([a-z0-9-]+)\/files\/(.+)$/.exec(url.pathname);
if (match !== null) {
const sandbox = yield* userSandbox(match[1]!);
const path = `/work/is/${match[2]}`;
if (request.method === "PUT") yield* sandbox.files.write(path, yield* request.text);
return yield* HttpServerResponse.json({ content: yield* sandbox.files.readText(path) });
}

Each user edits their own copy of the repository. Golden never changes, so the next fork doesn’t see their edits. A fork pauses when it’s idle, keeping its memory, and resumes on the user’s next request.

Terminal window
pnpm alchemy deploy
curl -X PUT "$URL/users/ana/files/notes.md" -d 'hello'
curl "$URL/users/ana/files/notes.md"
src/Api.ts
const user = /^\/users\/([a-z0-9-]+)$/.exec(url.pathname)?.[1];
if (request.method === "DELETE" && user !== undefined) {
yield* sandboxes.delete(`user-${user}`);
return HttpServerResponse.empty({ status: 204 });
}

Forks the program creates carry its name prefix and aren’t deleted with the Stack, so delete them when their owner goes away.

Part 4 changes the sandbox.