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