/** * The fake Google as a Worker of its own (src/fake_google/fake.ts), deployed to dev alone * (`npx wrangler deploy -c wrangler.fake-google.jsonc`): one Durable Object holds every invented account, grant and * event, so the dev business Worker's calls (GOOGLE_BASE) and a proof's admin calls see one Google. Never live. */ import { DurableObject } from "cloudflare:workers"; import { empty, FakeGoogle, type Client, type State } from "./fake.ts"; interface Env { FAKE_GOOGLE: DurableObjectNamespace; /** The OAuth clients this Google knows, JSON: { "": { "secret": "…", "redirects": ["https://…/v1/google/callback"] } }. */ FAKE_GOOGLE_CLIENTS: string; /** The token a proof sends to /fake/admin/* (Google Calendar's own web page, in effect). */ FAKE_GOOGLE_ADMIN?: string; } export class FakeGoogleState extends DurableObject { async fetch(request: Request): Promise { const state = ((await this.ctx.storage.get("state")) ?? empty()) as State; const clients = JSON.parse(this.env.FAKE_GOOGLE_CLIENTS || "{}") as Record; const random = () => { const bytes = crypto.getRandomValues(new Uint8Array(24)); return btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); }; const fake = new FakeGoogle(state, clients, () => Date.now(), random, this.env.FAKE_GOOGLE_ADMIN ?? null); const answer = await fake.handle(request); await this.ctx.storage.put("state", fake.state); return answer; } } export default { async fetch(request: Request, env: Env): Promise { return env.FAKE_GOOGLE.get(env.FAKE_GOOGLE.idFromName("google")).fetch(request); }, } satisfies ExportedHandler;