···1+# Project: Hop
2+3+Cloudflare Workers project built with Bun.
4+5+## Stack
6+7+- **Runtime**: Cloudflare Workers
8+- **Package Manager**: Bun
9+- **Language**: TypeScript
10+11+## Commands
12+13+```sh
14+bun run dev # Start local dev server (wrangler dev)
15+bun run deploy # Deploy to Cloudflare
16+bun run types # Generate Cloudflare Workers types
17+bun test # Run tests
18+```
19+20+## Architecture
21+22+- Worker entry point: `src/index.ts`
23+- Configuration: `wrangler.toml`
24+- Compatibility date: `2024-12-11`
25+26+## Bun Preferences
27+28+Default to using Bun instead of Node.js.
29+30+- Use `bun <file>` instead of `node <file>` or `ts-node <file>`
31+- Use `bun test` instead of `jest` or `vitest`
32+- Use `bun build <file.html|file.ts|file.css>` instead of `webpack` or `esbuild`
33+- Use `bun install` instead of `npm install` or `yarn install` or `pnpm install`
34+- Use `bun run <script>` instead of `npm run <script>` or `yarn run <script>` or `pnpm run <script>`
35+- Use `bunx <package> <command>` instead of `npx <package> <command>`
36+- Bun automatically loads .env, so don't use dotenv.
37+38+## Bun APIs
39+40+- `Bun.serve()` supports WebSockets, HTTPS, and routes. Don't use `express`.
41+- `bun:sqlite` for SQLite. Don't use `better-sqlite3`.
42+- `Bun.redis` for Redis. Don't use `ioredis`.
43+- `Bun.sql` for Postgres. Don't use `pg` or `postgres.js`.
44+- `WebSocket` is built-in. Don't use `ws`.
45+- Prefer `Bun.file` over `node:fs`'s readFile/writeFile
46+- Bun.$`ls` instead of execa.
47+48+**Note**: When building for Cloudflare Workers, use Web Standard APIs (Request, Response, fetch) instead of Bun-specific APIs, as they won't be available in the Workers runtime.
49+50+## Cloudflare Workers Development
51+52+Workers use the standard Fetch API handler:
53+54+```ts
55+export default {
56+ async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
57+ return new Response('Hello World');
58+ },
59+} satisfies ExportedHandler<Env>;
60+```
61+62+### Environment Variables
63+64+Define bindings in `wrangler.toml` and access via the `env` parameter:
65+66+```toml
67+[vars]
68+MY_VAR = "value"
69+70+[[kv_namespaces]]
71+binding = "MY_KV"
72+id = "..."
73+```
74+75+```ts
76+interface Env {
77+ MY_VAR: string;
78+ MY_KV: KVNamespace;
79+}
80+```
81+82+### Testing
83+84+Use `bun test` with Cloudflare Workers types:
85+86+```ts
87+import { test, expect } from "bun:test";
88+89+test("worker responds", async () => {
90+ const request = new Request("http://localhost/");
91+ const response = await worker.fetch(request, {}, {});
92+ expect(response.status).toBe(200);
93+});
94+```
95+96+For more information, read the Bun API docs in `node_modules/bun-types/docs/**.mdx`.