@hadoku/prefs-client 1.1.32 → 1.2.0
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/dist/rooms.d.ts +173 -0
- package/dist/rooms.js +75 -0
- package/package.json +7 -3
package/dist/rooms.d.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Room history — the wire contract between an app's room server and the
|
|
3
|
+
* game-stats service (hadoku_site services/game-stats), shared by the client
|
|
4
|
+
* (`./rooms`) and the service's ingest, so the two cannot drift.
|
|
5
|
+
*
|
|
6
|
+
* Generic across every app on the shared room logic. The words are
|
|
7
|
+
* rooms-server's own: a room has an id (ONE opening, never reused) and a key
|
|
8
|
+
* (the address people share, which a later opening may reuse); a participant
|
|
9
|
+
* has a ParticipantId that outlives its socket; a role is whatever the app's
|
|
10
|
+
* seating says it is.
|
|
11
|
+
*
|
|
12
|
+
* Identity follows match seats (IDENTITY_MODEL R5, R7): a person is named by
|
|
13
|
+
* their KEY, which the service resolves and never stores; reads answer with
|
|
14
|
+
* names and `isMe`, never a userId.
|
|
15
|
+
*
|
|
16
|
+
* Design: hadoku_site docs/planning/self-hosted-game-stats-2026-11-23.md.
|
|
17
|
+
*/
|
|
18
|
+
/** An app id: the same shape as a gameId. */
|
|
19
|
+
declare const ROOM_APP_ID_PATTERN: RegExp;
|
|
20
|
+
/** roomId, roomKey, participantId and eventId: opaque, URL-safe. */
|
|
21
|
+
declare const ROOM_TOKEN_PATTERN: RegExp;
|
|
22
|
+
/** A mode or role: a short word. */
|
|
23
|
+
declare const ROOM_WORD_PATTERN: RegExp;
|
|
24
|
+
declare const ROOM_LIMITS: {
|
|
25
|
+
/** Events per POST. */
|
|
26
|
+
readonly eventsPerBatch: 200;
|
|
27
|
+
readonly displayNameLength: 64;
|
|
28
|
+
readonly reasonLength: 64;
|
|
29
|
+
/** `attrs` on an open: keys and string length, as a match's. */
|
|
30
|
+
readonly attrKeys: 16;
|
|
31
|
+
readonly attrStringLength: 128;
|
|
32
|
+
};
|
|
33
|
+
type RoomMemberKind = 'user' | 'guest' | 'bot';
|
|
34
|
+
/** Who joined. A user is named by key; a guest or bot by what the room showed. */
|
|
35
|
+
type RoomParticipant = {
|
|
36
|
+
participantId: string;
|
|
37
|
+
kind: 'user';
|
|
38
|
+
userKey: string;
|
|
39
|
+
} | {
|
|
40
|
+
participantId: string;
|
|
41
|
+
kind: 'guest' | 'bot';
|
|
42
|
+
displayName?: string;
|
|
43
|
+
};
|
|
44
|
+
interface EventBase {
|
|
45
|
+
/** Unique per app, minted by the reporting server: a re-sent event changes nothing. */
|
|
46
|
+
eventId: string;
|
|
47
|
+
roomId: string;
|
|
48
|
+
/** Epoch ms. */
|
|
49
|
+
at: number;
|
|
50
|
+
}
|
|
51
|
+
type RoomEvent = (EventBase & {
|
|
52
|
+
type: 'open';
|
|
53
|
+
roomKey?: string;
|
|
54
|
+
mode?: string;
|
|
55
|
+
openedBy?: RoomParticipant;
|
|
56
|
+
attrs?: Record<string, string | number | boolean>;
|
|
57
|
+
}) | (EventBase & {
|
|
58
|
+
type: 'join';
|
|
59
|
+
participant: RoomParticipant;
|
|
60
|
+
role?: string;
|
|
61
|
+
}) | (EventBase & {
|
|
62
|
+
type: 'role';
|
|
63
|
+
participantId: string;
|
|
64
|
+
role: string;
|
|
65
|
+
}) | (EventBase & {
|
|
66
|
+
type: 'leave';
|
|
67
|
+
participantId: string;
|
|
68
|
+
}) | (EventBase & {
|
|
69
|
+
type: 'close';
|
|
70
|
+
reason?: string;
|
|
71
|
+
});
|
|
72
|
+
type RoomEventType = RoomEvent['type'];
|
|
73
|
+
interface RecordRoomEventsResult {
|
|
74
|
+
/** Events applied by this call. */
|
|
75
|
+
accepted: number;
|
|
76
|
+
/** Events already applied by an earlier call (same eventId). */
|
|
77
|
+
duplicates: number;
|
|
78
|
+
}
|
|
79
|
+
/** One stay of one participant, as a reader sees it. */
|
|
80
|
+
interface RoomStayView {
|
|
81
|
+
/** Live registry name for a user, the snapshot for a guest or bot; null once erased. */
|
|
82
|
+
name: string | null;
|
|
83
|
+
kind: RoomMemberKind;
|
|
84
|
+
isMe: boolean;
|
|
85
|
+
role: string | null;
|
|
86
|
+
joinedAt: number;
|
|
87
|
+
/** null while they are still in the room. */
|
|
88
|
+
leftAt: number | null;
|
|
89
|
+
}
|
|
90
|
+
interface RoomView {
|
|
91
|
+
roomId: string;
|
|
92
|
+
roomKey: string | null;
|
|
93
|
+
mode: string | null;
|
|
94
|
+
/** null when the open event has not arrived (or was never sent). */
|
|
95
|
+
openedAt: number | null;
|
|
96
|
+
closedAt: number | null;
|
|
97
|
+
closeReason: string | null;
|
|
98
|
+
openedBy: {
|
|
99
|
+
name: string | null;
|
|
100
|
+
isMe: boolean;
|
|
101
|
+
} | null;
|
|
102
|
+
attrs: Record<string, string | number | boolean>;
|
|
103
|
+
stays: RoomStayView[];
|
|
104
|
+
}
|
|
105
|
+
interface RoomDetailView extends RoomView {
|
|
106
|
+
/**
|
|
107
|
+
* The matches reported with this roomId, newest first. A room's app and a
|
|
108
|
+
* match's game can differ (bomberman is played in watchparty's rooms).
|
|
109
|
+
*/
|
|
110
|
+
matches: {
|
|
111
|
+
gameId: string;
|
|
112
|
+
matchId: string;
|
|
113
|
+
}[];
|
|
114
|
+
}
|
|
115
|
+
interface RoomPage {
|
|
116
|
+
rooms: RoomView[];
|
|
117
|
+
/** Pass as `before` for the next page; null on the last one. */
|
|
118
|
+
nextBefore: number | null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Room history — who opened a room, when it opened and closed, and who was in
|
|
123
|
+
* it, for every app on the shared room logic. Kept by the game-stats service
|
|
124
|
+
* (`/game-stats/api/v1/_rooms/:appId/*`), beside match history.
|
|
125
|
+
*
|
|
126
|
+
* - A ROOM SERVER reports what happens with `recordRoomEvents()`, holding
|
|
127
|
+
* its service-tier key. Every event carries its own `eventId`, so a batch
|
|
128
|
+
* that failed is simply sent again.
|
|
129
|
+
* - A BROWSER reads with `createRoomHistoryClient()`: names, never userIds.
|
|
130
|
+
*
|
|
131
|
+
* Contract: ./rooms-contract.ts. Design: hadoku_site
|
|
132
|
+
* docs/planning/self-hosted-game-stats-2026-11-23.md.
|
|
133
|
+
*/
|
|
134
|
+
|
|
135
|
+
/** The game-stats mount. */
|
|
136
|
+
declare const GAME_STATS_BASE = "https://hadoku.me/game-stats";
|
|
137
|
+
interface RoomHistoryOptions {
|
|
138
|
+
appId: string;
|
|
139
|
+
/** The game-stats mount (`https://hadoku.me/game-stats` by default), without `/api/v1`. */
|
|
140
|
+
baseUrl?: string;
|
|
141
|
+
fetch?: typeof fetch;
|
|
142
|
+
}
|
|
143
|
+
declare class RoomHistoryError extends Error {
|
|
144
|
+
readonly status: number;
|
|
145
|
+
constructor(status: number, message: string);
|
|
146
|
+
}
|
|
147
|
+
interface RecordRoomEventsOptions extends RoomHistoryOptions {
|
|
148
|
+
/** The room server's own service-tier key. */
|
|
149
|
+
serviceKey: string;
|
|
150
|
+
/** Sent on the call: hadoku.me's WAF challenges a bare client user-agent. */
|
|
151
|
+
userAgent: string;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Server-side: report room events, at most 200 per call. Safe to re-send:
|
|
155
|
+
* an event whose `eventId` was already applied is counted in `duplicates`.
|
|
156
|
+
*/
|
|
157
|
+
declare function recordRoomEvents(opts: RecordRoomEventsOptions, events: readonly RoomEvent[]): Promise<RecordRoomEventsResult>;
|
|
158
|
+
interface RoomListQuery {
|
|
159
|
+
/** A registry name, or `*` for everyone; omitted is the caller. */
|
|
160
|
+
member?: string;
|
|
161
|
+
/** Only rooms still open. */
|
|
162
|
+
open?: boolean;
|
|
163
|
+
limit?: number;
|
|
164
|
+
/** The previous page's `nextBefore`. */
|
|
165
|
+
before?: number;
|
|
166
|
+
}
|
|
167
|
+
declare function createRoomHistoryClient(opts: RoomHistoryOptions): {
|
|
168
|
+
rooms: ({ member, open, limit, before }?: RoomListQuery) => Promise<RoomPage>;
|
|
169
|
+
room: (roomId: string) => Promise<RoomDetailView>;
|
|
170
|
+
};
|
|
171
|
+
type RoomHistoryClient = ReturnType<typeof createRoomHistoryClient>;
|
|
172
|
+
|
|
173
|
+
export { GAME_STATS_BASE, ROOM_APP_ID_PATTERN, ROOM_LIMITS, ROOM_TOKEN_PATTERN, ROOM_WORD_PATTERN, type RecordRoomEventsOptions, type RecordRoomEventsResult, type RoomDetailView, type RoomEvent, type RoomEventType, type RoomHistoryClient, RoomHistoryError, type RoomHistoryOptions, type RoomListQuery, type RoomMemberKind, type RoomPage, type RoomParticipant, type RoomStayView, type RoomView, createRoomHistoryClient, recordRoomEvents };
|
package/dist/rooms.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// src/rooms-contract.ts
|
|
2
|
+
var ROOM_APP_ID_PATTERN = /^[a-z][a-z0-9-]{0,31}$/;
|
|
3
|
+
var ROOM_TOKEN_PATTERN = /^[A-Za-z0-9_.:-]{1,128}$/;
|
|
4
|
+
var ROOM_WORD_PATTERN = /^[a-z][a-z0-9_-]{0,31}$/;
|
|
5
|
+
var ROOM_LIMITS = {
|
|
6
|
+
/** Events per POST. */
|
|
7
|
+
eventsPerBatch: 200,
|
|
8
|
+
displayNameLength: 64,
|
|
9
|
+
reasonLength: 64,
|
|
10
|
+
/** `attrs` on an open: keys and string length, as a match's. */
|
|
11
|
+
attrKeys: 16,
|
|
12
|
+
attrStringLength: 128
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// src/rooms.ts
|
|
16
|
+
var GAME_STATS_BASE = "https://hadoku.me/game-stats";
|
|
17
|
+
var RoomHistoryError = class extends Error {
|
|
18
|
+
constructor(status, message) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.status = status;
|
|
21
|
+
this.name = "RoomHistoryError";
|
|
22
|
+
}
|
|
23
|
+
status;
|
|
24
|
+
};
|
|
25
|
+
function appBase(opts) {
|
|
26
|
+
if (!ROOM_APP_ID_PATTERN.test(opts.appId)) throw new Error(`invalid appId: ${opts.appId}`);
|
|
27
|
+
return `${opts.baseUrl ?? GAME_STATS_BASE}/api/v1/_rooms/${opts.appId}`;
|
|
28
|
+
}
|
|
29
|
+
async function readJson(res) {
|
|
30
|
+
if (!res.ok) {
|
|
31
|
+
const body = await res.json().catch(() => null);
|
|
32
|
+
throw new RoomHistoryError(res.status, body?.message ?? `HTTP ${res.status}`);
|
|
33
|
+
}
|
|
34
|
+
return await res.json();
|
|
35
|
+
}
|
|
36
|
+
async function recordRoomEvents(opts, events) {
|
|
37
|
+
const f = opts.fetch ?? fetch;
|
|
38
|
+
const res = await f(`${appBase(opts)}/events`, {
|
|
39
|
+
method: "POST",
|
|
40
|
+
headers: {
|
|
41
|
+
"Content-Type": "application/json",
|
|
42
|
+
"X-User-Key": opts.serviceKey,
|
|
43
|
+
"User-Agent": opts.userAgent
|
|
44
|
+
},
|
|
45
|
+
body: JSON.stringify({ events })
|
|
46
|
+
});
|
|
47
|
+
return readJson(res);
|
|
48
|
+
}
|
|
49
|
+
function createRoomHistoryClient(opts) {
|
|
50
|
+
const base = appBase(opts);
|
|
51
|
+
const f = opts.fetch ?? fetch;
|
|
52
|
+
const get = (path) => f(`${base}${path}`, { credentials: "include" }).then(readJson);
|
|
53
|
+
return {
|
|
54
|
+
rooms: ({ member, open, limit, before } = {}) => {
|
|
55
|
+
const q = new URLSearchParams();
|
|
56
|
+
if (member !== void 0) q.set("member", member);
|
|
57
|
+
if (open) q.set("open", "1");
|
|
58
|
+
if (limit !== void 0) q.set("limit", String(limit));
|
|
59
|
+
if (before !== void 0) q.set("before", String(before));
|
|
60
|
+
const s = q.toString();
|
|
61
|
+
return get(`/rooms${s ? `?${s}` : ""}`);
|
|
62
|
+
},
|
|
63
|
+
room: (roomId) => get(`/rooms/${encodeURIComponent(roomId)}`)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
GAME_STATS_BASE,
|
|
68
|
+
ROOM_APP_ID_PATTERN,
|
|
69
|
+
ROOM_LIMITS,
|
|
70
|
+
ROOM_TOKEN_PATTERN,
|
|
71
|
+
ROOM_WORD_PATTERN,
|
|
72
|
+
RoomHistoryError,
|
|
73
|
+
createRoomHistoryClient,
|
|
74
|
+
recordRoomEvents
|
|
75
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/prefs-client",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"gitHead": "
|
|
5
|
-
"description": "Client SDK for the hadoku unified preferences store. Wraps /prefs/api/v1/* with localStorage caching, debounced writes, Zod-validated reads, and a usePrefs() React hook, plus match history, ratings and leaderboards for every game (./games), the one resolved avatar per user (./avatar), and the avatar cosmetics renderer (./cosmetics).",
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"gitHead": "933517dd00e778d904a43e618c66a6acd36752c6",
|
|
5
|
+
"description": "Client SDK for the hadoku unified preferences store. Wraps /prefs/api/v1/* with localStorage caching, debounced writes, Zod-validated reads, and a usePrefs() React hook, plus match history, ratings and leaderboards for every game (./games), the one resolved avatar per user (./avatar), and the avatar cosmetics renderer (./cosmetics), and room history (./rooms).",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
@@ -42,6 +42,10 @@
|
|
|
42
42
|
"./people": {
|
|
43
43
|
"types": "./dist/people.d.ts",
|
|
44
44
|
"default": "./dist/people.js"
|
|
45
|
+
},
|
|
46
|
+
"./rooms": {
|
|
47
|
+
"types": "./dist/rooms.d.ts",
|
|
48
|
+
"default": "./dist/rooms.js"
|
|
45
49
|
}
|
|
46
50
|
},
|
|
47
51
|
"files": [
|