| 1 | // Stand-ins for identity, repos and events, so the packages service runs |
| 2 | // on its own with `wrangler dev` (see packages.jsonc beside it). |
| 3 | // |
| 4 | // - Identity knows one person, `dev`, an owner of the workspace `acme`, |
| 5 | // whose token is DEV_TOKEN, and one outsider, `bo` (OUTSIDER_TOKEN). |
| 6 | // - Repos knows two repositories of acme: `web` (private) and `site` |
| 7 | // (public). |
| 8 | // - Events takes every event and audit entry and logs them. |
| 9 | // - Billing says acme is free, with FREE_PRIVATE_BYTES of private package |
| 10 | // storage and 10 GB public. |
| 11 | |
| 12 | const REPOS = { |
| 13 | web: { isPrivate: true }, |
| 14 | site: { isPrivate: false }, |
| 15 | }; |
| 16 | |
| 17 | function user(env, secret) { |
| 18 | if (secret === env.DEV_TOKEN) { |
| 19 | return { |
| 20 | id: "usr_dev", |
| 21 | username: "dev", |
| 22 | kind: "user", |
| 23 | verified: true, |
| 24 | workspaces: [{ slug: "acme", role: "owner" }], |
| 25 | token: { token_id: "tok_dev" }, |
| 26 | }; |
| 27 | } |
| 28 | if (secret === env.OUTSIDER_TOKEN) { |
| 29 | return { id: "usr_bo", username: "bo", kind: "user", verified: true, workspaces: [], token: { token_id: "tok_bo" } }; |
| 30 | } |
| 31 | return null; |
| 32 | } |
| 33 | |
| 34 | export default { |
| 35 | async fetch(request, env) { |
| 36 | const method = new URL(request.url).pathname.replace(/^\/rpc\//, ""); |
| 37 | const args = await request.json().catch(() => ({})); |
| 38 | const json = (value) => Response.json(value); |
| 39 | switch (method) { |
| 40 | case "user_for_git_credentials": |
| 41 | return json(user(env, args.secret)); |
| 42 | case "get": { |
| 43 | const repo = args.path?.namespace === "acme" ? REPOS[args.path.name] : undefined; |
| 44 | if (!repo) return json({ ok: false, error: { code: "not_found", message: "Repository not found." } }); |
| 45 | return json({ |
| 46 | ok: true, |
| 47 | value: { |
| 48 | id: `rep_${args.path.name}`, |
| 49 | namespace: "acme", |
| 50 | name: args.path.name, |
| 51 | description: null, |
| 52 | isPrivate: repo.isPrivate, |
| 53 | ownerId: "usr_dev", |
| 54 | defaultBranch: "main", |
| 55 | forkOf: null, |
| 56 | createdAt: "2026-10-01T00:00:00.000Z", |
| 57 | }, |
| 58 | }); |
| 59 | } |
| 60 | case "publish": |
| 61 | for (const event of args.events ?? []) console.log(`event ${event.type} ${JSON.stringify(event.data)}`); |
| 62 | return json(null); |
| 63 | case "entitlements": |
| 64 | return json({ |
| 65 | workspace: args.workspace, |
| 66 | plan: "free", |
| 67 | hasPlan: false, |
| 68 | packagePublicFreeBytes: 10_000_000_000, |
| 69 | packagePrivateFreeBytes: Number(env.FREE_PRIVATE_BYTES), |
| 70 | }); |
| 71 | case "audit_record": |
| 72 | for (const entry of args.entries ?? []) console.log(`audit ${entry.action} by ${entry.actor} on ${entry.path}`); |
| 73 | return json((args.entries ?? []).length); |
| 74 | default: |
| 75 | return new Response(`stub: no method ${method}`, { status: 404 }); |
| 76 | } |
| 77 | }, |
| 78 | }; |