pr_01m47d24b0e6n91zwymwxg0vpx/apps/web/app/lib/registration.server.ts
| 1 | import type { RegistrationMode } from "@g1t/contracts"; |
| 2 | |
| 3 | import { identity } from "./services.server"; |
| 4 | |
| 5 | /** How long one isolate trusts the answer before asking identity again. */ |
| 6 | const TTL_MS = 60_000; |
| 7 | |
| 8 | let known: { mode: RegistrationMode; at: number } | null = null; |
| 9 | |
| 10 | /** |
| 11 | * Whether g1t.sh is invite-only, as identity's REGISTRATION_MODE says. |
| 12 | * Asked at most once a minute per isolate; when identity cannot answer, |
| 13 | * the site says invite-only, the same as identity does with no setting. |
| 14 | */ |
| 15 | export async function registrationMode(): Promise<RegistrationMode> { |
| 16 | const now = Date.now(); |
| 17 | if (known && now - known.at < TTL_MS) return known.mode; |
| 18 | try { |
| 19 | const mode = await identity.registration(); |
| 20 | known = { mode: mode === "open" ? "open" : "invite", at: now }; |
| 21 | return known.mode; |
| 22 | } catch { |
| 23 | return "invite"; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /** Who is asking, for identity's rate limits: the visitor's address. */ |
| 28 | export function clientKey(request: Request): string | null { |
| 29 | return request.headers.get("cf-connecting-ip") ?? request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ?? null; |
| 30 | } |