[relay] Relay: The league relay from Ludus, byte for byte, with the slice of the schema it imports
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* What a relay will accept. Policy, not contract.
|
||||
*
|
||||
* The submission *shape* is `@ludus/schema`'s and is the same everywhere. These
|
||||
* limits are this relay's opinion, and a self-hoster is entitled to a different
|
||||
* one — which is the point of the service being ~400 lines and self-hostable.
|
||||
*
|
||||
* Every value here is a CHEAP defence. Read `SIGNATURE_PROVES` in
|
||||
* `@ludus/schema` before adding an expensive one: a local-first client computes
|
||||
* its own score, so it can lie, and a defence that implies otherwise is worse
|
||||
* than no defence at all (CLAUDE.md §6.3).
|
||||
*/
|
||||
|
||||
export interface RelayConfig {
|
||||
/**
|
||||
* Hard ceiling on the XP one participant may hold in one week.
|
||||
*
|
||||
* Submissions above it are CLAMPED, not rejected. Rejecting would punish an
|
||||
* honest heavy user with a lost week; clamping bounds what a dishonest one
|
||||
* gains, which is all a ceiling can do.
|
||||
*
|
||||
* The number is derived from the session contract rather than picked: six to
|
||||
* eight items a day (`SESSION_MIN_ITEMS`/`SESSION_MAX_ITEMS`) at roughly 20 XP
|
||||
* an item is ~160 XP on a full day, ~1 120 in a week. 5 000 leaves room for
|
||||
* bonuses, catch-up days and a scoring change, and still makes "I earned
|
||||
* 400 000 XP this week" impossible to submit.
|
||||
*/
|
||||
readonly xpCeiling: number;
|
||||
|
||||
/**
|
||||
* Longest display name stored, in UTF-16 code units.
|
||||
*
|
||||
* A relay serves this string back to every other participant in the league.
|
||||
* It is untrusted text from an anonymous device, so it is bounded here and
|
||||
* refused — not truncated — above the bound: truncating changes the bytes that
|
||||
* were signed, and the row would then disagree with its own signature.
|
||||
*/
|
||||
readonly displayNameMaxChars: number;
|
||||
|
||||
/** Longest league id accepted. Ids are chosen by whoever runs the league. */
|
||||
readonly leagueIdMaxChars: number;
|
||||
|
||||
/**
|
||||
* Largest request body read, in bytes.
|
||||
*
|
||||
* A submission is a few hundred bytes. The cap exists so an unauthenticated
|
||||
* POST cannot make the process buffer a gigabyte; the connection is closed as
|
||||
* soon as the limit is passed rather than after the body finishes arriving.
|
||||
*/
|
||||
readonly maxBodyBytes: number;
|
||||
|
||||
/**
|
||||
* Most rows one board read returns.
|
||||
*
|
||||
* A league is a cohort of ~30 by design — an empty-looking global board with
|
||||
* eleven people on it is worse than no board (CLAUDE.md §8). The cap is
|
||||
* generous rather than exact so that a self-hoster running larger cohorts is
|
||||
* not silently truncated at a number they never chose.
|
||||
*/
|
||||
readonly boardMaxRows: number;
|
||||
}
|
||||
|
||||
export const DEFAULT_RELAY_CONFIG: RelayConfig = {
|
||||
xpCeiling: 5_000,
|
||||
displayNameMaxChars: 32,
|
||||
leagueIdMaxChars: 64,
|
||||
maxBodyBytes: 8 * 1024,
|
||||
boardMaxRows: 200
|
||||
};
|
||||
|
||||
export function relayConfig(overrides: Partial<RelayConfig> = {}): RelayConfig {
|
||||
return { ...DEFAULT_RELAY_CONFIG, ...overrides };
|
||||
}
|
||||
Reference in New Issue
Block a user