@hadoku/game-server 0.0.0-stage → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/base-path.d.ts +29 -0
- package/dist/base-path.js +30 -0
- package/dist/busy.d.ts +40 -0
- package/dist/busy.js +23 -0
- package/dist/capacity.d.ts +9 -0
- package/dist/capacity.js +6 -0
- package/dist/config.d.ts +247 -0
- package/dist/config.js +271 -0
- package/dist/edge-auth.d.ts +43 -0
- package/dist/edge-auth.js +47 -0
- package/dist/erase/anonymise.d.ts +7 -0
- package/dist/erase/anonymise.js +7 -0
- package/dist/erase/index.d.ts +18 -0
- package/dist/erase/index.js +20 -0
- package/dist/erase/legacy-ids.d.ts +7 -0
- package/dist/erase/legacy-ids.js +24 -0
- package/dist/erase/recordings.d.ts +26 -0
- package/dist/erase/recordings.js +128 -0
- package/dist/erase/rooms.d.ts +63 -0
- package/dist/erase/rooms.js +67 -0
- package/dist/gate.d.ts +27 -0
- package/dist/gate.js +15 -0
- package/dist/http/client-events.d.ts +25 -0
- package/dist/http/client-events.js +15 -0
- package/dist/http/page.d.ts +26 -0
- package/dist/http/page.js +35 -0
- package/dist/http/prepared.d.ts +14 -0
- package/dist/http/prepared.js +16 -0
- package/dist/http/request.d.ts +59 -0
- package/dist/http/request.js +37 -0
- package/dist/http/room-core.d.ts +160 -0
- package/dist/http/room-core.js +278 -0
- package/dist/http/trim.d.ts +23 -0
- package/dist/http/trim.js +76 -0
- package/dist/http/usage.d.ts +22 -0
- package/dist/http/usage.js +16 -0
- package/dist/kernel.d.ts +4 -0
- package/dist/kernel.js +26 -0
- package/dist/log.d.ts +9 -0
- package/dist/log.js +9 -0
- package/dist/people.d.ts +105 -0
- package/dist/people.js +145 -0
- package/dist/play/clock.d.ts +39 -0
- package/dist/play/clock.js +81 -0
- package/dist/play/diagnostics.d.ts +21 -0
- package/dist/play/diagnostics.js +35 -0
- package/dist/play/engine-runtime.d.ts +200 -0
- package/dist/play/engine-runtime.js +309 -0
- package/dist/play/game-adapter.d.ts +158 -0
- package/dist/play/game-adapter.js +1 -0
- package/dist/play/host.d.ts +482 -0
- package/dist/play/host.js +950 -0
- package/dist/play/jvm-transport.d.ts +25 -0
- package/dist/play/jvm-transport.js +1 -0
- package/dist/play/local-jvm.d.ts +325 -0
- package/dist/play/local-jvm.js +829 -0
- package/dist/play/match/hosted.d.ts +92 -0
- package/dist/play/match/hosted.js +151 -0
- package/dist/play/match/outcome.d.ts +26 -0
- package/dist/play/match/outcome.js +21 -0
- package/dist/play/match/shell.d.ts +378 -0
- package/dist/play/match/shell.js +613 -0
- package/dist/play/recording/retention.d.ts +36 -0
- package/dist/play/recording/retention.js +89 -0
- package/dist/play/recording/writer.d.ts +185 -0
- package/dist/play/recording/writer.js +90 -0
- package/dist/play/relay-socket.d.ts +128 -0
- package/dist/play/relay-socket.js +194 -0
- package/dist/play/runtimes/in-process.d.ts +47 -0
- package/dist/play/runtimes/in-process.js +43 -0
- package/dist/play/runtimes/relay-match.d.ts +166 -0
- package/dist/play/runtimes/relay-match.js +240 -0
- package/dist/play/runtimes/relay.d.ts +128 -0
- package/dist/play/runtimes/relay.js +183 -0
- package/dist/play/runtimes/streamed.d.ts +116 -0
- package/dist/play/runtimes/streamed.js +148 -0
- package/dist/play/start-queue.d.ts +65 -0
- package/dist/play/start-queue.js +187 -0
- package/dist/room/avatar.d.ts +1 -0
- package/dist/room/avatar.js +28 -0
- package/dist/room/caller.d.ts +21 -0
- package/dist/room/caller.js +1 -0
- package/dist/room/constants.d.ts +27 -0
- package/dist/room/constants.js +27 -0
- package/dist/room/events.d.ts +59 -0
- package/dist/room/events.js +174 -0
- package/dist/room/identity.d.ts +89 -0
- package/dist/room/identity.js +98 -0
- package/dist/room/lobby.d.ts +426 -0
- package/dist/room/lobby.js +880 -0
- package/dist/room/membership.d.ts +64 -0
- package/dist/room/membership.js +76 -0
- package/dist/room/provisioning.d.ts +22 -0
- package/dist/room/provisioning.js +46 -0
- package/dist/room/record.d.ts +63 -0
- package/dist/room/record.js +39 -0
- package/dist/room/room-list.d.ts +67 -0
- package/dist/room/room-list.js +66 -0
- package/dist/room/seats.d.ts +39 -0
- package/dist/room/seats.js +58 -0
- package/dist/room/session.d.ts +91 -0
- package/dist/room/session.js +156 -0
- package/dist/room/socket-auth.d.ts +2 -0
- package/dist/room/socket-auth.js +10 -0
- package/dist/room/watching.d.ts +35 -0
- package/dist/room/watching.js +72 -0
- package/dist/room/watchparty-link.d.ts +1 -0
- package/dist/room/watchparty-link.js +17 -0
- package/dist/stream/alloc.d.ts +115 -0
- package/dist/stream/alloc.js +368 -0
- package/dist/stream/index.d.ts +66 -0
- package/dist/stream/index.js +293 -0
- package/dist/telemetry.d.ts +46 -0
- package/dist/telemetry.js +25 -0
- package/dist/testPort.d.ts +1 -0
- package/dist/testPort.js +28 -0
- package/package.json +36 -4
- package/README.md +0 -3
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account erase's part in the live table (the operator's ruling, 2026-11-05):
|
|
3
|
+
*
|
|
4
|
+
* - A match they hold a seat in ends as their forfeit: conceded for their seat, so the opponent
|
|
5
|
+
* wins as they would from any concede (`endForfeitedGame`), or stopped when the seat cannot.
|
|
6
|
+
* - A room where they are the only member is closed, as its owner closing it would.
|
|
7
|
+
* - Any other room they are in, they leave by the room's own leave (leadership passes on; a
|
|
8
|
+
* tournament entrant's match is a drop).
|
|
9
|
+
* - Every room that still names them afterwards (results, series, pairings) names their
|
|
10
|
+
* placeholder instead (`Lobby.anonymise`).
|
|
11
|
+
*
|
|
12
|
+
* The steps run in that order, so a room is never anonymised while they still sit in it.
|
|
13
|
+
*/
|
|
14
|
+
import type { ParticipantId } from '@wolffm/rooms-protocol';
|
|
15
|
+
import type { EraseStep } from './index.js';
|
|
16
|
+
export interface LiveMatch {
|
|
17
|
+
seatOf(participantId: ParticipantId): number | null;
|
|
18
|
+
}
|
|
19
|
+
/** What this file reads off a room — `FileLobby.list()`'s own `RoomView[]` satisfies this structurally. */
|
|
20
|
+
interface RoomSummary {
|
|
21
|
+
id: string;
|
|
22
|
+
members: readonly {
|
|
23
|
+
id: string;
|
|
24
|
+
}[];
|
|
25
|
+
}
|
|
26
|
+
/** `room/lobby.ts`'s own `LeaveResult`/`CloseRoomResult`, narrowed to what this file reads; `FileLobby` satisfies both structurally. */
|
|
27
|
+
type RoomsLeaveResult = {
|
|
28
|
+
ok: true;
|
|
29
|
+
forfeited?: {
|
|
30
|
+
tableId: string;
|
|
31
|
+
matchIds: string[];
|
|
32
|
+
};
|
|
33
|
+
} | {
|
|
34
|
+
ok: false;
|
|
35
|
+
};
|
|
36
|
+
type RoomsCloseResult = {
|
|
37
|
+
ok: true;
|
|
38
|
+
matches: string[];
|
|
39
|
+
} | {
|
|
40
|
+
ok: false;
|
|
41
|
+
};
|
|
42
|
+
/** A narrow slice of `room/lobby.ts`'s `Lobby` — declared here, not imported, so this platform file carries no edge into the tangled lobby. */
|
|
43
|
+
interface RoomsLobby {
|
|
44
|
+
list(): readonly RoomSummary[];
|
|
45
|
+
leave(roomId: string, memberId: string): RoomsLeaveResult;
|
|
46
|
+
close(roomId: string, callerId: string): RoomsCloseResult;
|
|
47
|
+
anonymise(ids: ReadonlySet<string>, to: string): number;
|
|
48
|
+
removed(): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
export interface RoomsEraseDeps<M extends LiveMatch> {
|
|
51
|
+
lobby: RoomsLobby;
|
|
52
|
+
/** Every match still being played. */
|
|
53
|
+
liveMatches(): readonly M[];
|
|
54
|
+
/** End a live match as `loser`'s forfeit (`table-actions.ts`'s `endForfeitedGame`). */
|
|
55
|
+
forfeit(match: M, loser: ParticipantId): void;
|
|
56
|
+
/** A room just closed: stop its matches and drop its draft (`index.ts`'s `roomClosed`). */
|
|
57
|
+
closed(roomId: string, matches: readonly string[]): void;
|
|
58
|
+
/** A tournament entrant's leave dropped them from a match: end its games (`http/rooms.ts`'s `forfeited`). */
|
|
59
|
+
forfeited(roomId: string, tableId: string, loser: string, matches: readonly string[]): void;
|
|
60
|
+
}
|
|
61
|
+
/** The steps, in the order the module header gives. */
|
|
62
|
+
export declare function roomsEraseSteps<M extends LiveMatch>(deps: RoomsEraseDeps<M>): EraseStep[];
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account erase's part in the live table (the operator's ruling, 2026-11-05):
|
|
3
|
+
*
|
|
4
|
+
* - A match they hold a seat in ends as their forfeit: conceded for their seat, so the opponent
|
|
5
|
+
* wins as they would from any concede (`endForfeitedGame`), or stopped when the seat cannot.
|
|
6
|
+
* - A room where they are the only member is closed, as its owner closing it would.
|
|
7
|
+
* - Any other room they are in, they leave by the room's own leave (leadership passes on; a
|
|
8
|
+
* tournament entrant's match is a drop).
|
|
9
|
+
* - Every room that still names them afterwards (results, series, pairings) names their
|
|
10
|
+
* placeholder instead (`Lobby.anonymise`).
|
|
11
|
+
*
|
|
12
|
+
* The steps run in that order, so a room is never anonymised while they still sit in it.
|
|
13
|
+
*/
|
|
14
|
+
/** The steps, in the order the module header gives. */
|
|
15
|
+
export function roomsEraseSteps(deps) {
|
|
16
|
+
return [
|
|
17
|
+
{
|
|
18
|
+
what: 'matchesForfeited',
|
|
19
|
+
erase: ({ participantId }) => {
|
|
20
|
+
let n = 0;
|
|
21
|
+
for (const match of deps.liveMatches()) {
|
|
22
|
+
if (match.seatOf(participantId) === null)
|
|
23
|
+
continue;
|
|
24
|
+
deps.forfeit(match, participantId);
|
|
25
|
+
n++;
|
|
26
|
+
}
|
|
27
|
+
return n;
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
what: 'roomsClosed',
|
|
32
|
+
erase: async ({ participantId }) => {
|
|
33
|
+
let n = 0;
|
|
34
|
+
for (const room of deps.lobby.list()) {
|
|
35
|
+
if (room.members.length !== 1 || room.members[0]?.id !== participantId)
|
|
36
|
+
continue;
|
|
37
|
+
const closed = deps.lobby.close(room.id, participantId);
|
|
38
|
+
if (!closed.ok)
|
|
39
|
+
continue;
|
|
40
|
+
deps.closed(room.id, closed.matches);
|
|
41
|
+
n++;
|
|
42
|
+
}
|
|
43
|
+
// A closed room's file names them until it is deleted: answer only once it is.
|
|
44
|
+
await deps.lobby.removed();
|
|
45
|
+
return n;
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
what: 'roomsLeft',
|
|
50
|
+
erase: ({ participantId }) => {
|
|
51
|
+
let n = 0;
|
|
52
|
+
for (const room of deps.lobby.list()) {
|
|
53
|
+
if (!room.members.some((m) => m.id === participantId))
|
|
54
|
+
continue;
|
|
55
|
+
const left = deps.lobby.leave(room.id, participantId);
|
|
56
|
+
if (!left.ok)
|
|
57
|
+
continue;
|
|
58
|
+
if (left.forfeited !== undefined)
|
|
59
|
+
deps.forfeited(room.id, left.forfeited.tableId, participantId, left.forfeited.matchIds);
|
|
60
|
+
n++;
|
|
61
|
+
}
|
|
62
|
+
return n;
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
{ what: 'roomsAnonymised', erase: ({ ids, placeholder }) => deps.lobby.anonymise(ids, placeholder) },
|
|
66
|
+
];
|
|
67
|
+
}
|
package/dist/gate.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one handshake gate — cardtable's binding of the platform kernel
|
|
3
|
+
* (`@hadoku/service-kernel/http`): each of three copies of "sanitize, rank,
|
|
4
|
+
* refuse, report" (the room socket, the play socket, the stream's raw
|
|
5
|
+
* upgrade) folded into one verdict (`floor`) and one refusal report.
|
|
6
|
+
* HTTP's own copy is `requireMinTier`, bound in `edge-auth.ts` — both files
|
|
7
|
+
* bind the one shared `createGate` instance in `kernel.ts` (w93-Z).
|
|
8
|
+
*/
|
|
9
|
+
/** HTTP gates itself via `requireMinTier` (`edge-auth.ts`), which calls {@link floor} directly. */
|
|
10
|
+
export type GateSurface = 'draft-socket' | 'play-socket' | 'stream';
|
|
11
|
+
/** `minTier`: `CONFIG.minTier` in production. `surface`: the `reportSealRefused` label. */
|
|
12
|
+
export interface GateOptions {
|
|
13
|
+
minTier: string;
|
|
14
|
+
surface: GateSurface;
|
|
15
|
+
}
|
|
16
|
+
/** Sanitize `headers` in place against edge provenance, then rank the result against `minTier`. */
|
|
17
|
+
export declare const floor: (headers: Record<string, unknown>, minTier: string) => {
|
|
18
|
+
ok: boolean;
|
|
19
|
+
tier: string;
|
|
20
|
+
};
|
|
21
|
+
/** Provenance + the floor on a socket.io handshake. `next(new Error('not found'))` is the client's `connect_error`. */
|
|
22
|
+
export declare const gateSocketIo: (io: import("socket.io").Server, opts: import("@wolffm/service-kernel/http").GateOptions) => void;
|
|
23
|
+
/** The floor for a raw upgrade: no middleware chain runs, so the caller answers the refusal itself. */
|
|
24
|
+
export declare const gateUpgrade: (req: import("http").IncomingMessage, opts: import("@wolffm/service-kernel/http").GateOptions) => {
|
|
25
|
+
ok: boolean;
|
|
26
|
+
tier: string;
|
|
27
|
+
};
|
package/dist/gate.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one handshake gate — cardtable's binding of the platform kernel
|
|
3
|
+
* (`@hadoku/service-kernel/http`): each of three copies of "sanitize, rank,
|
|
4
|
+
* refuse, report" (the room socket, the play socket, the stream's raw
|
|
5
|
+
* upgrade) folded into one verdict (`floor`) and one refusal report.
|
|
6
|
+
* HTTP's own copy is `requireMinTier`, bound in `edge-auth.ts` — both files
|
|
7
|
+
* bind the one shared `createGate` instance in `kernel.ts` (w93-Z).
|
|
8
|
+
*/
|
|
9
|
+
import { gate } from './kernel.js';
|
|
10
|
+
/** Sanitize `headers` in place against edge provenance, then rank the result against `minTier`. */
|
|
11
|
+
export const floor = gate.floor;
|
|
12
|
+
/** Provenance + the floor on a socket.io handshake. `next(new Error('not found'))` is the client's `connect_error`. */
|
|
13
|
+
export const gateSocketIo = gate.gateSocketIo;
|
|
14
|
+
/** The floor for a raw upgrade: no middleware chain runs, so the caller answers the refusal itself. */
|
|
15
|
+
export const gateUpgrade = gate.gateUpgrade;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter: cardtable's binding of `@hadoku/service-kernel/relays`'s `attachClientEvents`
|
|
3
|
+
* (w93-K3). `POST /api/client-events`: client telemetry from a screen with no match
|
|
4
|
+
* (`@hadoku/platform-wire/telemetry`'s `PageEventSchema`). A match's screen sends its telemetry over the match
|
|
5
|
+
* socket and it lands in the play log (`play/socket.ts`, `forge:client`); a page outside a match
|
|
6
|
+
* has no such socket, so it sends the same flat facts here, and they land in the same stdout log
|
|
7
|
+
* as `client` events, under `svc: 'cardtable-client'`, keyed by page and user. Nothing is stored
|
|
8
|
+
* and nothing is answered but 204.
|
|
9
|
+
*
|
|
10
|
+
* Every export and signature below is unchanged from before this moved into the kernel:
|
|
11
|
+
* `index.ts` and `play/socket.ts` import this path exactly as they did. The cardtable-specific
|
|
12
|
+
* bits are `@hadoku/platform-wire/telemetry`'s schema, path and per-second cap, bound here — the kernel itself
|
|
13
|
+
* never imports it.
|
|
14
|
+
*
|
|
15
|
+
* Read them with `grep -a '"svc":"cardtable-client"' ~/.pm2/logs/cardtable-ppe-out.log` (PPE) or
|
|
16
|
+
* `cardtable-out.log` (production).
|
|
17
|
+
*/
|
|
18
|
+
import type { Router } from 'express';
|
|
19
|
+
import { type JsonLog } from '@wolffm/service-kernel/log';
|
|
20
|
+
export { malformedFacts } from '@wolffm/service-kernel/relays';
|
|
21
|
+
export interface ClientEventsDeps {
|
|
22
|
+
log?: JsonLog;
|
|
23
|
+
now?: () => number;
|
|
24
|
+
}
|
|
25
|
+
export declare function attachClientEvents(api: Router, deps?: ClientEventsDeps): void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CLIENT_EVENT_MAX_PER_SECOND, PAGE_EVENT_PATH, PageEventSchema } from '@wolffm/platform-wire/telemetry';
|
|
2
|
+
import { attachClientEvents as kernelAttachClientEvents } from '@wolffm/service-kernel/relays';
|
|
3
|
+
import { userIdOf } from '../edge-auth.js';
|
|
4
|
+
import { jsonLogger } from '@wolffm/service-kernel/log';
|
|
5
|
+
export { malformedFacts } from '@wolffm/service-kernel/relays';
|
|
6
|
+
export function attachClientEvents(api, deps = {}) {
|
|
7
|
+
kernelAttachClientEvents(api, {
|
|
8
|
+
schema: PageEventSchema,
|
|
9
|
+
path: PAGE_EVENT_PATH,
|
|
10
|
+
maxPerSecond: CLIENT_EVENT_MAX_PER_SECOND,
|
|
11
|
+
userIdOf,
|
|
12
|
+
log: deps.log ?? jsonLogger('cardtable-client'),
|
|
13
|
+
now: deps.now,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page: every client-routed path serves the same index.html.
|
|
3
|
+
*
|
|
4
|
+
* Scoped to GET/HEAD: a POST to a path that does not exist is a bug in the
|
|
5
|
+
* caller, and answering it with an HTML page hides that. Mounted LAST, below
|
|
6
|
+
* every route that owns a path, or it answers for them.
|
|
7
|
+
*
|
|
8
|
+
* Per caller, a page load is a costly act: each one boots the whole app, about
|
|
9
|
+
* eight requests, each a line in this server's log and a row in the edge's. A
|
|
10
|
+
* browser reloading in a loop (a proof script once did, 120 loads a minute for
|
|
11
|
+
* eight hours) is refused with a 429 and `rate-limited` telemetry naming it,
|
|
12
|
+
* while the busiest real caller (20 a minute) never meets the ceiling.
|
|
13
|
+
*/
|
|
14
|
+
import type { RateLimitConfig } from '@wolffm/rooms-server';
|
|
15
|
+
import type { Express } from 'express';
|
|
16
|
+
import { type BasePathVerdict } from '../base-path.js';
|
|
17
|
+
export declare const PAGE_LOAD_RATE_LIMIT: RateLimitConfig;
|
|
18
|
+
export interface PageDeps {
|
|
19
|
+
indexHtml: string;
|
|
20
|
+
basePath: BasePathVerdict;
|
|
21
|
+
basePrefix: string;
|
|
22
|
+
/** `null` is no limit (the e2e stacks); omitted is {@link PAGE_LOAD_RATE_LIMIT}. */
|
|
23
|
+
rateLimit?: RateLimitConfig | null;
|
|
24
|
+
now?: () => number;
|
|
25
|
+
}
|
|
26
|
+
export declare function attachPage(app: Express, deps: PageDeps): void;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page: every client-routed path serves the same index.html.
|
|
3
|
+
*
|
|
4
|
+
* Scoped to GET/HEAD: a POST to a path that does not exist is a bug in the
|
|
5
|
+
* caller, and answering it with an HTML page hides that. Mounted LAST, below
|
|
6
|
+
* every route that owns a path, or it answers for them.
|
|
7
|
+
*
|
|
8
|
+
* Per caller, a page load is a costly act: each one boots the whole app, about
|
|
9
|
+
* eight requests, each a line in this server's log and a row in the edge's. A
|
|
10
|
+
* browser reloading in a loop (a proof script once did, 120 loads a minute for
|
|
11
|
+
* eight hours) is refused with a 429 and `rate-limited` telemetry naming it,
|
|
12
|
+
* while the busiest real caller (20 a minute) never meets the ceiling.
|
|
13
|
+
*/
|
|
14
|
+
import { diagnosticPage, sendIndex } from '../base-path.js';
|
|
15
|
+
import { perCallerLimit } from './request.js';
|
|
16
|
+
export const PAGE_LOAD_RATE_LIMIT = {
|
|
17
|
+
capacity: 30,
|
|
18
|
+
refillAmount: 1,
|
|
19
|
+
refillIntervalMs: 2_000,
|
|
20
|
+
};
|
|
21
|
+
export function attachPage(app, deps) {
|
|
22
|
+
const limit = perCallerLimit(deps.rateLimit === undefined ? PAGE_LOAD_RATE_LIMIT : deps.rateLimit, 'too many page loads; wait a moment and reload', {
|
|
23
|
+
act: 'page-load',
|
|
24
|
+
now: deps.now,
|
|
25
|
+
});
|
|
26
|
+
app.get(/.*/, (req, res, next) => {
|
|
27
|
+
if (!deps.basePath.ok) {
|
|
28
|
+
res.status(503).type('html').send(diagnosticPage(deps.basePath, deps.basePrefix));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (!limit(req, res))
|
|
32
|
+
return;
|
|
33
|
+
sendIndex(res, deps.indexHtml, (err) => err && next(err));
|
|
34
|
+
});
|
|
35
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A JSON answer serialized once and sent many times, for a body read far more often than it
|
|
3
|
+
* changes: Express would stringify the value and hash the whole body again for its ETag on every
|
|
4
|
+
* request. The ETag is the body's own, so a revalidation still answers 304 — `res.send` keeps an
|
|
5
|
+
* ETag already set and checks the request's freshness against it.
|
|
6
|
+
*/
|
|
7
|
+
import type { Response } from 'express';
|
|
8
|
+
export interface PreparedJson {
|
|
9
|
+
readonly body: string;
|
|
10
|
+
readonly etag: string;
|
|
11
|
+
}
|
|
12
|
+
/** `value` as the bytes `sendPrepared` answers with; `tag` names the route in the ETag. */
|
|
13
|
+
export declare function prepareJson(value: unknown, tag: string): PreparedJson;
|
|
14
|
+
export declare function sendPrepared(res: Response, answer: PreparedJson): void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A JSON answer serialized once and sent many times, for a body read far more often than it
|
|
3
|
+
* changes: Express would stringify the value and hash the whole body again for its ETag on every
|
|
4
|
+
* request. The ETag is the body's own, so a revalidation still answers 304 — `res.send` keeps an
|
|
5
|
+
* ETag already set and checks the request's freshness against it.
|
|
6
|
+
*/
|
|
7
|
+
import { createHash } from 'node:crypto';
|
|
8
|
+
/** `value` as the bytes `sendPrepared` answers with; `tag` names the route in the ETag. */
|
|
9
|
+
export function prepareJson(value, tag) {
|
|
10
|
+
const body = JSON.stringify(value);
|
|
11
|
+
return { body, etag: `"${tag}-${createHash('sha1').update(body).digest('base64url').slice(0, 16)}"` };
|
|
12
|
+
}
|
|
13
|
+
export function sendPrepared(res, answer) {
|
|
14
|
+
res.setHeader('ETag', answer.etag);
|
|
15
|
+
res.type('json').send(answer.body);
|
|
16
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter: cardtable's binding of `@hadoku/service-kernel/limits` (w93-K3). Every export and
|
|
3
|
+
* signature below is unchanged from before this moved into the kernel — this path's ~20 importers
|
|
4
|
+
* (routes, pools, play/socket.ts) need no edit.
|
|
5
|
+
*
|
|
6
|
+
* `httpCaller` binds the kernel's injected caller-id deriver to this app's own identity scheme
|
|
7
|
+
* (`room/identity.ts`). `callerOf` builds the full `Caller` the room layer wants: it is NOT moved
|
|
8
|
+
* into the kernel (`room/lobby.ts`'s `Caller` and `UNNAMED` stay app-side, by the wave's own
|
|
9
|
+
* ruling), and `UNNAMED` is copied here rather than imported from `room/lobby.ts` — this file is
|
|
10
|
+
* `platform` and `room/lobby.ts` is `tangled` (`scripts/layers/layers.json`), so importing it back
|
|
11
|
+
* would restore the very `platform→tangled` edge this wave cuts
|
|
12
|
+
* (`scripts/layers/ratchet.test.ts`, the `http/request.ts→../room/lobby.js` edge). The object
|
|
13
|
+
* `callerOf` builds matches `room/lobby.ts`'s `Caller` shape structurally, which is all a caller
|
|
14
|
+
* of `callerOf` needs — nothing here imports that type either.
|
|
15
|
+
*/
|
|
16
|
+
import type { ParticipantId } from '@wolffm/rooms-protocol';
|
|
17
|
+
import type { RateLimitConfig } from '@wolffm/rooms-server';
|
|
18
|
+
import type { Request, Response } from 'express';
|
|
19
|
+
import { bodyErrors, parseBody, readBody, type BodySchema } from '@wolffm/service-kernel/limits';
|
|
20
|
+
export { bodyErrors, parseBody, readBody };
|
|
21
|
+
export type { BodySchema };
|
|
22
|
+
/**
|
|
23
|
+
* The caller, by the edge's headers. A request the edge named nobody on gets a
|
|
24
|
+
* fresh ephemeral id, which matches no seat and no member — the right answer
|
|
25
|
+
* for someone the edge could not name.
|
|
26
|
+
*/
|
|
27
|
+
export declare const httpCaller: (req: Request) => ParticipantId;
|
|
28
|
+
/**
|
|
29
|
+
* Who is asking, for a lobby call: the id, the DISPLAY NAME (`X-User-Name`, never the opaque
|
|
30
|
+
* userId — a roster is for humans; a key the edge sent no name for gets `UNNAMED`), and the userId
|
|
31
|
+
* a saved room keeps instead of the name (R12), and whether the operator verifies them.
|
|
32
|
+
*/
|
|
33
|
+
export declare function callerOf(req: Request): {
|
|
34
|
+
id: string;
|
|
35
|
+
name: string;
|
|
36
|
+
userId: string | null;
|
|
37
|
+
verified: boolean;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* A per-caller token bucket for a costly step: each pass reports `costly-act` and each refusal
|
|
41
|
+
* `rate-limited`, both named by `act`, so telemetry shows who is spending the expensive steps and
|
|
42
|
+
* who hit the ceiling (`telemetry.ts` throttles the posts). `headers` are the caller's own, for
|
|
43
|
+
* the report. `null` is no limit — the e2e stacks' `CARDTABLE_E2E_NO_RATE_LIMITS`, where one test
|
|
44
|
+
* identity makes every room and starts every match of a run.
|
|
45
|
+
*/
|
|
46
|
+
export declare function costlyActLimit(cfg: RateLimitConfig | null, opts: {
|
|
47
|
+
act: string;
|
|
48
|
+
now?: () => number;
|
|
49
|
+
onRefused?: () => void;
|
|
50
|
+
}): (caller: ParticipantId, headers: Request['headers']) => boolean;
|
|
51
|
+
/**
|
|
52
|
+
* {@link costlyActLimit} for a route: false once the 429 (with `Retry-After`) is sent. A route
|
|
53
|
+
* asks AFTER reading its body, so only a request that would do the work spends a token.
|
|
54
|
+
*/
|
|
55
|
+
export declare function perCallerLimit(cfg: RateLimitConfig | null, refusal: string, opts: {
|
|
56
|
+
act: string;
|
|
57
|
+
now?: () => number;
|
|
58
|
+
onRefused?: () => void;
|
|
59
|
+
}): (req: Request, res: Response) => boolean;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { bodyErrors, makeCallerId, parseBody, readBody, costlyActLimit as kernelCostlyActLimit, perCallerLimit as kernelPerCallerLimit, } from '@wolffm/service-kernel/limits';
|
|
2
|
+
import { accountIdOf, userNameOf, verifiedOf } from '../edge-auth.js';
|
|
3
|
+
import { identityFromHeaders } from '../room/identity.js';
|
|
4
|
+
export { bodyErrors, parseBody, readBody };
|
|
5
|
+
/**
|
|
6
|
+
* The caller, by the edge's headers. A request the edge named nobody on gets a
|
|
7
|
+
* fresh ephemeral id, which matches no seat and no member — the right answer
|
|
8
|
+
* for someone the edge could not name.
|
|
9
|
+
*/
|
|
10
|
+
export const httpCaller = makeCallerId(identityFromHeaders);
|
|
11
|
+
/** `room/lobby.ts`'s own word for an unnamed member. Kept in sync by hand — see this file's header. */
|
|
12
|
+
const UNNAMED = 'friend';
|
|
13
|
+
/**
|
|
14
|
+
* Who is asking, for a lobby call: the id, the DISPLAY NAME (`X-User-Name`, never the opaque
|
|
15
|
+
* userId — a roster is for humans; a key the edge sent no name for gets `UNNAMED`), and the userId
|
|
16
|
+
* a saved room keeps instead of the name (R12), and whether the operator verifies them.
|
|
17
|
+
*/
|
|
18
|
+
export function callerOf(req) {
|
|
19
|
+
return { id: httpCaller(req), name: userNameOf(req) ?? UNNAMED, userId: accountIdOf(req), verified: verifiedOf(req) };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A per-caller token bucket for a costly step: each pass reports `costly-act` and each refusal
|
|
23
|
+
* `rate-limited`, both named by `act`, so telemetry shows who is spending the expensive steps and
|
|
24
|
+
* who hit the ceiling (`telemetry.ts` throttles the posts). `headers` are the caller's own, for
|
|
25
|
+
* the report. `null` is no limit — the e2e stacks' `CARDTABLE_E2E_NO_RATE_LIMITS`, where one test
|
|
26
|
+
* identity makes every room and starts every match of a run.
|
|
27
|
+
*/
|
|
28
|
+
export function costlyActLimit(cfg, opts) {
|
|
29
|
+
return kernelCostlyActLimit(cfg, opts);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* {@link costlyActLimit} for a route: false once the 429 (with `Retry-After`) is sent. A route
|
|
33
|
+
* asks AFTER reading its body, so only a request that would do the work spends a token.
|
|
34
|
+
*/
|
|
35
|
+
export function perCallerLimit(cfg, refusal, opts) {
|
|
36
|
+
return kernelPerCallerLimit(cfg, refusal, { ...opts, callerId: httpCaller });
|
|
37
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/api/rooms`'s GENERIC surface: list, create, join, ready, leave, close, an
|
|
3
|
+
* admin's testing-room reset, the watchparty bind, "the room that started this
|
|
4
|
+
* match", and the `{ gameType }` arm of `PATCH /:id`. `rooms.ts` stays Magic:
|
|
5
|
+
* formats, pods, drafts, tournaments, and the `{ format }`/`{ settings }`/
|
|
6
|
+
* `{ mode }` arms of the same PATCH, mounted on this file's OWN router after it.
|
|
7
|
+
*
|
|
8
|
+
* This file imports no `Lobby`, no `RoomView`, no `@hadoku/shared` (the ratchet
|
|
9
|
+
* resolves `@hadoku/shared` as one package, magic even through `/platform`) —
|
|
10
|
+
* every real Magic type is a type PARAMETER instead (`V`, `C`, `P`, `G` below),
|
|
11
|
+
* and `rooms.ts` passes the real `Lobby`/schemas in structurally. `LobbyCore`'s
|
|
12
|
+
* methods use method syntax on purpose, for TypeScript's bivariant parameter
|
|
13
|
+
* checking — it's what lets the real, narrower-typed `Lobby` satisfy this
|
|
14
|
+
* interface with no cast.
|
|
15
|
+
*
|
|
16
|
+
* ## The PATCH seam
|
|
17
|
+
*
|
|
18
|
+
* ONE registration of `PATCH /:id` (the frozen `route-table.json` admits each
|
|
19
|
+
* method+path once): this file parses the body with `deps.wire.patchRoomBody`
|
|
20
|
+
* and answers `{ gameType }` itself; anything else goes to `opts.onPatch`, a
|
|
21
|
+
* hook `rooms.ts` passes in — never a second route, never `res.locals`.
|
|
22
|
+
*/
|
|
23
|
+
import { Router, type NextFunction, type Request, type Response } from 'express';
|
|
24
|
+
import { type RateLimitConfig } from '@wolffm/rooms-server';
|
|
25
|
+
import { bodyErrors, callerOf, type BodySchema } from './request.js';
|
|
26
|
+
/** Who is asking — exactly what `callerOf` returns, never a redeclared shape. */
|
|
27
|
+
type CallerLike = ReturnType<typeof callerOf>;
|
|
28
|
+
/** The minimal room shape every route here touches; the real `RoomView` satisfies it and rides through untouched as `V`. */
|
|
29
|
+
export interface RoomViewCore {
|
|
30
|
+
id: string;
|
|
31
|
+
leader: string;
|
|
32
|
+
members: readonly {
|
|
33
|
+
id: string;
|
|
34
|
+
ready: boolean;
|
|
35
|
+
}[];
|
|
36
|
+
testing?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/** The slice of `room/lobby.ts`'s `Lobby` this file calls, typed against `V` and `C` alone — never the real types. */
|
|
39
|
+
export interface LobbyCore<V extends RoomViewCore, C> {
|
|
40
|
+
get(roomId: string): V | undefined;
|
|
41
|
+
offered(): V[];
|
|
42
|
+
isMember(roomId: string, callerId: string): boolean;
|
|
43
|
+
create(caller: CallerLike, body: C): {
|
|
44
|
+
ok: true;
|
|
45
|
+
room: V;
|
|
46
|
+
} | {
|
|
47
|
+
ok: false;
|
|
48
|
+
};
|
|
49
|
+
join(roomId: string, caller: CallerLike, password?: string): {
|
|
50
|
+
ok: true;
|
|
51
|
+
room: V;
|
|
52
|
+
} | {
|
|
53
|
+
ok: false;
|
|
54
|
+
reason: 'no-match' | 'not-durable';
|
|
55
|
+
};
|
|
56
|
+
setReady(roomId: string, callerId: string, ready: boolean): boolean;
|
|
57
|
+
touch(roomId: string, callerId: string): void;
|
|
58
|
+
leave(roomId: string, callerId: string): {
|
|
59
|
+
ok: true;
|
|
60
|
+
room: V;
|
|
61
|
+
forfeited?: {
|
|
62
|
+
tableId: string;
|
|
63
|
+
matchIds: readonly string[];
|
|
64
|
+
};
|
|
65
|
+
} | {
|
|
66
|
+
ok: false;
|
|
67
|
+
};
|
|
68
|
+
close(roomId: string, callerId: string): {
|
|
69
|
+
ok: true;
|
|
70
|
+
matches: readonly string[];
|
|
71
|
+
} | {
|
|
72
|
+
ok: false;
|
|
73
|
+
reason: string;
|
|
74
|
+
};
|
|
75
|
+
bindWatchparty(caller: CallerLike, watchpartyRoom: string): {
|
|
76
|
+
ok: true;
|
|
77
|
+
room: V;
|
|
78
|
+
} | {
|
|
79
|
+
ok: false;
|
|
80
|
+
};
|
|
81
|
+
roomOfMatch(matchId: string): V | undefined;
|
|
82
|
+
setGameType(roomId: string, callerId: string, gameType: string): {
|
|
83
|
+
ok: true;
|
|
84
|
+
room: V;
|
|
85
|
+
} | {
|
|
86
|
+
ok: false;
|
|
87
|
+
reason: string;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/** The request-body schemas and the room-id shape, injected rather than imported: the real values live in a Magic-resolved package. */
|
|
91
|
+
export interface RoomCoreWire<C, P extends object> {
|
|
92
|
+
roomIdRe: RegExp;
|
|
93
|
+
createRoomBody: BodySchema<C>;
|
|
94
|
+
joinRoomBody: BodySchema<{
|
|
95
|
+
password?: string;
|
|
96
|
+
}>;
|
|
97
|
+
readyBody: BodySchema<{
|
|
98
|
+
ready: boolean;
|
|
99
|
+
}>;
|
|
100
|
+
emptyBody: BodySchema<unknown>;
|
|
101
|
+
watchpartyBindBody: BodySchema<{
|
|
102
|
+
room: string;
|
|
103
|
+
}>;
|
|
104
|
+
patchRoomBody: BodySchema<P>;
|
|
105
|
+
}
|
|
106
|
+
/** The single answer for "no such room" and "wrong password". Byte-identical by construction. */
|
|
107
|
+
export declare const NO_MATCH: {
|
|
108
|
+
readonly error: "no room with that id and password";
|
|
109
|
+
};
|
|
110
|
+
/** Join attempts per caller: a burst of 10, then one more every 30 s. Only NON-member attempts spend a token. */
|
|
111
|
+
export declare const JOIN_RATE_LIMIT: RateLimitConfig;
|
|
112
|
+
/** Rooms made per caller: a burst of 10, then one a minute. `rooms.ts`'s own `/challenge` shares this SAME limiter instance. */
|
|
113
|
+
export declare const ROOM_CREATE_RATE_LIMIT: RateLimitConfig;
|
|
114
|
+
export interface RoomCoreDeps<V extends RoomViewCore, C, P extends object, G = never> {
|
|
115
|
+
lobby: LobbyCore<V, C>;
|
|
116
|
+
wire: RoomCoreWire<C, P>;
|
|
117
|
+
/** Structured log lines. Never carries a password, hash or salt. */
|
|
118
|
+
log?: (entry: Record<string, unknown>) => void;
|
|
119
|
+
closed?: (roomId: string, matches: readonly string[]) => void;
|
|
120
|
+
/** A tournament entrant's drop (`Lobby.leave`): their match's games to end and its seats' decks to restore. */
|
|
121
|
+
forfeited?: (roomId: string, tableId: string, loser: string, matches: readonly string[]) => void;
|
|
122
|
+
/** "Reset = Close": runs AFTER `closed`, same as `index.ts`'s own `seedTestingRooms` boot call. */
|
|
123
|
+
reseedTesting?: () => void;
|
|
124
|
+
arrived?: (roomId: string, caller: CallerLike) => void;
|
|
125
|
+
/** Each table's game, by table id (`play/table-game.ts`). A hook: this file never inspects `G`'s shape. */
|
|
126
|
+
tableGames?(room: V): Record<string, G>;
|
|
127
|
+
featured?(caller: string): {
|
|
128
|
+
matchId: string;
|
|
129
|
+
roomId: string;
|
|
130
|
+
} | null;
|
|
131
|
+
joinRateLimit?: RateLimitConfig;
|
|
132
|
+
createRateLimit?: RateLimitConfig | null;
|
|
133
|
+
now?: () => number;
|
|
134
|
+
}
|
|
135
|
+
/** The room, as this caller is told it: `V` untouched plus their own row. */
|
|
136
|
+
export type RoomBody<V extends RoomViewCore, G = never> = V & {
|
|
137
|
+
you: {
|
|
138
|
+
id: string;
|
|
139
|
+
leader: boolean;
|
|
140
|
+
ready: boolean;
|
|
141
|
+
};
|
|
142
|
+
tableGames?: Record<string, G>;
|
|
143
|
+
};
|
|
144
|
+
export declare function badRequest(res: Response, error: string): void;
|
|
145
|
+
export declare function noMatch(res: Response): void;
|
|
146
|
+
export declare function notDurable(res: Response, verb?: string): void;
|
|
147
|
+
/** A testing room, to a non-admin, is as if it did not exist — shared by this file's router and `rooms.ts`'s. */
|
|
148
|
+
export declare function hideTestingRoomsParam<V extends RoomViewCore>(lobby: Pick<LobbyCore<V, never>, 'get'>): (req: Request, res: Response, next: NextFunction, id: string) => void;
|
|
149
|
+
export interface RoomCoreHandle<V extends RoomViewCore, G = never> {
|
|
150
|
+
router: Router;
|
|
151
|
+
roomBody(room: V, callerId: string): RoomBody<V, G>;
|
|
152
|
+
/** The SAME per-caller limiter `POST /` spends from — `rooms.ts`'s `/challenge` too, never a second budget. */
|
|
153
|
+
createLimit(req: Request, res: Response): boolean;
|
|
154
|
+
}
|
|
155
|
+
export declare function attachRoomCore<V extends RoomViewCore, C, P extends object, G = never>(api: Router, deps: RoomCoreDeps<V, C, P, G>, opts?: {
|
|
156
|
+
beforeWildcard?(router: Router): void;
|
|
157
|
+
/** `{ format }`/`{ settings }`/`{ mode }` (Magic's three other PATCH variants) — method syntax for bivariance, like `LobbyCore`. */
|
|
158
|
+
onPatch?(req: Request, res: Response, body: P, caller: CallerLike, roomId: string): void;
|
|
159
|
+
}): RoomCoreHandle<V, G>;
|
|
160
|
+
export { bodyErrors };
|