Diffinery

homeflare/celldpublic

21 lines · 851 BRaw History
1// The default fetch routes by path: /alice → the "alice" Counter, /bob → "bob".
2// On a multi-node cluster those cells may live on different nodes; env.COUNTER
3// .get(id) transparently proxies to the owner. This fixture is plain DO code —
4// it runs identically on Cloudflare.
5export class Counter {
6 constructor(state, env) { this.state = state; }
7 async fetch(request) {
8 const name = new URL(request.url).pathname.slice(1) || "default";
9 let n = (await this.state.storage.get("n")) ?? 0;
10 n++;
11 await this.state.storage.put("n", n);
12 return new Response(JSON.stringify({ name, n }), { status: 200 });
13 }
14}
15export default {
16 async fetch(request, env) {
17 const name = new URL(request.url).pathname.slice(1) || "default";
18 const id = env.COUNTER.idFromName(name);
19 return env.COUNTER.get(id).fetch(request);
20 }
21};