@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,174 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Scheduled events and attached Rally forms (`@hadoku/shared`'s `events.ts`): what the front
|
|
3
|
+
* page's calendar and polls list. One JSON file, read at boot and rewritten whole on each change,
|
|
4
|
+
* like `tournaments.ts` — a friend group schedules a handful at a time.
|
|
5
|
+
*
|
|
6
|
+
* An event keeps its room's id and its schedule, nothing else: what the room is called, plays and
|
|
7
|
+
* how are the lobby's (`Lobby.create` with `scheduledFor`), read from it each time. A form keeps
|
|
8
|
+
* only its Rally id and who attached it; its title and status are Rally's.
|
|
9
|
+
*/
|
|
10
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
11
|
+
import { dirname } from 'node:path';
|
|
12
|
+
import { randomBytes } from 'node:crypto';
|
|
13
|
+
import { z } from 'zod';
|
|
14
|
+
import { replaceIds } from '../erase/anonymise.js';
|
|
15
|
+
const EventRecordSchema = z.object({
|
|
16
|
+
id: z.string(),
|
|
17
|
+
roomId: z.string(),
|
|
18
|
+
startsAt: z.number(),
|
|
19
|
+
organizerId: z.string(),
|
|
20
|
+
/** Who organized it, by registry userId (R12): their name is looked up when the event is shown. null when the edge named nobody. */
|
|
21
|
+
organizerUserId: z.string().nullable().optional(),
|
|
22
|
+
/** Read only, from a record written before `organizerUserId`: never written again. */
|
|
23
|
+
organizerName: z.string().optional(),
|
|
24
|
+
/** The Rally sign-up form it was made from: Rally counts who can make it (`EventView.signedUp`). */
|
|
25
|
+
formId: z.string().optional(),
|
|
26
|
+
createdAt: z.number(),
|
|
27
|
+
});
|
|
28
|
+
const FormRecordSchema = z.object({
|
|
29
|
+
formId: z.string(),
|
|
30
|
+
attachedById: z.string(),
|
|
31
|
+
/** Who attached it, by registry userId (R12), as `EventRecord.organizerUserId`. */
|
|
32
|
+
attachedByUserId: z.string().nullable().optional(),
|
|
33
|
+
/** Read only, from a record written before `attachedByUserId`: never written again. */
|
|
34
|
+
attachedByName: z.string().optional(),
|
|
35
|
+
attachedAt: z.number(),
|
|
36
|
+
});
|
|
37
|
+
/** An event stays listed this long after it starts: it is still on while people play it. */
|
|
38
|
+
export const EVENT_LISTED_AFTER_START_MS = 6 * 60 * 60_000;
|
|
39
|
+
/** Forgotten this long after it starts. */
|
|
40
|
+
const EVENT_KEPT_AFTER_START_MS = 7 * 24 * 60 * 60_000;
|
|
41
|
+
const mintEventId = () => `e-${randomBytes(6).toString('hex')}`;
|
|
42
|
+
export async function openEvents(file, log, now = Date.now) {
|
|
43
|
+
let events = [];
|
|
44
|
+
let forms = [];
|
|
45
|
+
try {
|
|
46
|
+
const parsed = z.object({ events: z.array(z.unknown()), forms: z.array(z.unknown()) }).parse(JSON.parse(await readFile(file, 'utf8')));
|
|
47
|
+
events = parsed.events.flatMap((e) => EventRecordSchema.safeParse(e).data ?? []);
|
|
48
|
+
forms = parsed.forms.flatMap((f) => FormRecordSchema.safeParse(f).data ?? []);
|
|
49
|
+
const dropped = parsed.events.length - events.length + parsed.forms.length - forms.length;
|
|
50
|
+
if (dropped > 0)
|
|
51
|
+
log({ event: 'events-dropped', dropped });
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
if (err.code !== 'ENOENT')
|
|
55
|
+
log({ event: 'events-unreadable', err: String(err) });
|
|
56
|
+
}
|
|
57
|
+
// One write at a time, each of the whole store as it then is. Resolves false, logged, when it failed.
|
|
58
|
+
let writing = Promise.resolve(true);
|
|
59
|
+
const save = () => {
|
|
60
|
+
const cutoff = now() - EVENT_KEPT_AFTER_START_MS;
|
|
61
|
+
events = events.filter((e) => e.startsAt > cutoff);
|
|
62
|
+
const body = JSON.stringify({ events, forms });
|
|
63
|
+
writing = writing.then(async () => {
|
|
64
|
+
try {
|
|
65
|
+
await mkdir(dirname(file), { recursive: true });
|
|
66
|
+
await writeFile(`${file}.tmp`, body);
|
|
67
|
+
await rename(`${file}.tmp`, file);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
log({ event: 'events-write-failed', err: String(err) });
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
return writing;
|
|
76
|
+
};
|
|
77
|
+
const owned = (id, callerId) => {
|
|
78
|
+
const event = events.find((e) => e.id === id);
|
|
79
|
+
if (event === undefined)
|
|
80
|
+
return { ok: false, reason: 'no-event' };
|
|
81
|
+
if (event.organizerId !== callerId)
|
|
82
|
+
return { ok: false, reason: 'not-organizer' };
|
|
83
|
+
return { ok: true, event };
|
|
84
|
+
};
|
|
85
|
+
return {
|
|
86
|
+
upcoming(at) {
|
|
87
|
+
const cutoff = at - EVENT_LISTED_AFTER_START_MS;
|
|
88
|
+
return events.filter((e) => e.startsAt > cutoff).sort((a, b) => a.startsAt - b.startsAt || (a.id < b.id ? -1 : 1));
|
|
89
|
+
},
|
|
90
|
+
get(id) {
|
|
91
|
+
return events.find((e) => e.id === id);
|
|
92
|
+
},
|
|
93
|
+
create(input, at) {
|
|
94
|
+
let id = mintEventId();
|
|
95
|
+
while (events.some((e) => e.id === id))
|
|
96
|
+
id = mintEventId();
|
|
97
|
+
const event = { id, ...input, createdAt: at };
|
|
98
|
+
events = [...events, event];
|
|
99
|
+
log({ event: 'event-created', id, roomId: event.roomId });
|
|
100
|
+
void save();
|
|
101
|
+
return event;
|
|
102
|
+
},
|
|
103
|
+
reschedule(id, callerId, startsAt) {
|
|
104
|
+
const found = owned(id, callerId);
|
|
105
|
+
if (!found.ok)
|
|
106
|
+
return found;
|
|
107
|
+
const event = { ...found.event, startsAt };
|
|
108
|
+
events = events.map((e) => (e.id === id ? event : e));
|
|
109
|
+
log({ event: 'event-updated', id });
|
|
110
|
+
void save();
|
|
111
|
+
return { ok: true, event };
|
|
112
|
+
},
|
|
113
|
+
cancel(id, callerId) {
|
|
114
|
+
const found = owned(id, callerId);
|
|
115
|
+
if (!found.ok)
|
|
116
|
+
return found;
|
|
117
|
+
events = events.filter((e) => e.id !== id);
|
|
118
|
+
log({ event: 'event-cancelled', id });
|
|
119
|
+
void save();
|
|
120
|
+
return found;
|
|
121
|
+
},
|
|
122
|
+
forms() {
|
|
123
|
+
return forms;
|
|
124
|
+
},
|
|
125
|
+
attachForm(input, at) {
|
|
126
|
+
if (forms.some((f) => f.formId === input.formId))
|
|
127
|
+
return false;
|
|
128
|
+
forms = [...forms, { ...input, attachedAt: at }];
|
|
129
|
+
log({ event: 'event-form-attached', formId: input.formId });
|
|
130
|
+
void save();
|
|
131
|
+
return true;
|
|
132
|
+
},
|
|
133
|
+
detachForm(formId, callerId) {
|
|
134
|
+
const form = forms.find((f) => f.formId === formId);
|
|
135
|
+
if (form === undefined)
|
|
136
|
+
return { ok: false, reason: 'no-form' };
|
|
137
|
+
if (form.attachedById !== callerId)
|
|
138
|
+
return { ok: false, reason: 'not-attacher' };
|
|
139
|
+
forms = forms.filter((f) => f !== form);
|
|
140
|
+
log({ event: 'event-form-detached', formId });
|
|
141
|
+
void save();
|
|
142
|
+
return { ok: true };
|
|
143
|
+
},
|
|
144
|
+
forgetForms(formIds) {
|
|
145
|
+
const gone = new Set(formIds);
|
|
146
|
+
if (!forms.some((f) => gone.has(f.formId)))
|
|
147
|
+
return;
|
|
148
|
+
forms = forms.filter((f) => !gone.has(f.formId));
|
|
149
|
+
log({ event: 'event-forms-forgotten', formIds });
|
|
150
|
+
void save();
|
|
151
|
+
},
|
|
152
|
+
async anonymise(ids, to) {
|
|
153
|
+
let changedEvents = 0;
|
|
154
|
+
for (const e of events) {
|
|
155
|
+
if (replaceIds(e, ids, to) === 0)
|
|
156
|
+
continue;
|
|
157
|
+
if (e.organizerId === to)
|
|
158
|
+
e.organizerName = '';
|
|
159
|
+
changedEvents++;
|
|
160
|
+
}
|
|
161
|
+
let changedForms = 0;
|
|
162
|
+
for (const f of forms) {
|
|
163
|
+
if (replaceIds(f, ids, to) === 0)
|
|
164
|
+
continue;
|
|
165
|
+
if (f.attachedById === to)
|
|
166
|
+
f.attachedByName = '';
|
|
167
|
+
changedForms++;
|
|
168
|
+
}
|
|
169
|
+
if (changedEvents + changedForms > 0 && !(await save()))
|
|
170
|
+
throw new Error('events: the anonymised store was not saved');
|
|
171
|
+
return { events: changedEvents, forms: changedForms };
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minting a `ParticipantId` — now a thin binding over the fleet's derivation.
|
|
3
|
+
*
|
|
4
|
+
* The hashing used to live here: `@wolffm/rooms-protocol` is zero-dependency
|
|
5
|
+
* and isomorphic, so it shipped the *format* and stopped, and `node:crypto`'s
|
|
6
|
+
* `createHash` was ours to own. `@wolffm/rooms-server@0.39.4` publishes the
|
|
7
|
+
* other half — `createParticipantIdDeriver(domain)` — built from the scheme
|
|
8
|
+
* asked for in `docs/requests/P5_SERVICE_PARTICIPANT.md` §7.2, so this file is
|
|
9
|
+
* the domain string and the header plumbing and nothing else.
|
|
10
|
+
*
|
|
11
|
+
* The package hashes `` `${domain}\u001f${tier}\u001f${material}` `` with
|
|
12
|
+
* SHA-256, hex, truncated to `PARTICIPANT_ID_DIGEST_LENGTH`. `identity.test.ts`
|
|
13
|
+
* pins a known userId to the exact `fk:` id it derives, so a future bump that
|
|
14
|
+
* re-identifies everybody arrives as a red test rather than as a room full of
|
|
15
|
+
* strangers.
|
|
16
|
+
*
|
|
17
|
+
* ## The material is the userId, never the key
|
|
18
|
+
*
|
|
19
|
+
* The id is minted from `X-User-Id`, the registry's userId, passed as the
|
|
20
|
+
* deriver's `accountId` (the `account` tier). It used to be minted from
|
|
21
|
+
* `X-User-Key`, the credential, and a rotated key then made the same person a
|
|
22
|
+
* stranger to every room, pod and recording that named them (cardtable#2,
|
|
23
|
+
* hadoku_site IDENTITY_MODEL R7/R12, cardtable#25). The key is not an input at
|
|
24
|
+
* all now. Stores written under the key-derived ids are refused rather than
|
|
25
|
+
* remapped (`@wolffm/rooms-server/store`'s `STORE_VERSION` 2).
|
|
26
|
+
*
|
|
27
|
+
* ## Derived, never accepted
|
|
28
|
+
*
|
|
29
|
+
* The input comes from the EDGE HEADERS and from nowhere else. A caller the
|
|
30
|
+
* edge named nobody gets an `ephemeral` id tied to the socket. A browser that
|
|
31
|
+
* puts a `participantId` in a join payload is a parse error one layer up
|
|
32
|
+
* (`@hadoku/shared`'s strict client schemas), not a value anything here reads.
|
|
33
|
+
*
|
|
34
|
+
* `X-User-Id` is only believable after `sanitizeHeaders` has run — it
|
|
35
|
+
* overwrites tier and DELETES `x-user-id` on anything the edge did not vouch
|
|
36
|
+
* for. So this must run after `edgeProvenance`, which for a socket means after
|
|
37
|
+
* `io.engine.use(edgeProvenance)`. Handed unsanitized headers it would happily
|
|
38
|
+
* mint an `fk:` identity for a forged userId.
|
|
39
|
+
*
|
|
40
|
+
* The id is a domain-separated digest, so it can be logged, put on the wire and
|
|
41
|
+
* shown in a seat row; there is exactly one id per participant rather than an
|
|
42
|
+
* internal id plus a wire handle to keep in sync.
|
|
43
|
+
*/
|
|
44
|
+
import type { IncomingHttpHeaders } from 'node:http';
|
|
45
|
+
import type { ParticipantId } from '@wolffm/rooms-protocol';
|
|
46
|
+
/**
|
|
47
|
+
* Domain separation for the digest.
|
|
48
|
+
*
|
|
49
|
+
* Present so that a friend key hashed here can never collide with the same
|
|
50
|
+
* bytes hashed for some other purpose, and versioned so that changing the
|
|
51
|
+
* scheme is a visible change rather than a silent re-identification of
|
|
52
|
+
* everybody.
|
|
53
|
+
*
|
|
54
|
+
* Per-TIER separation is NOT spelled here any more: the package hashes the tier
|
|
55
|
+
* in alongside the domain, and its own header explains why asking the caller
|
|
56
|
+
* for a second string was the wrong shape. There is nowhere to pass one.
|
|
57
|
+
*/
|
|
58
|
+
export declare const PARTICIPANT_ID_DOMAIN = "hadoku.cardtable.participant.v1";
|
|
59
|
+
/**
|
|
60
|
+
* A short, wire-safe handle for a seat row — `@wolffm/rooms-protocol`'s, so the
|
|
61
|
+
* truncation length is one number for the fleet rather than one per app.
|
|
62
|
+
*
|
|
63
|
+
* Re-exported rather than imported at each call site because this module is
|
|
64
|
+
* where the rest of the server already looks for anything identity-shaped, and
|
|
65
|
+
* because the import that used to be here was the same function.
|
|
66
|
+
*/
|
|
67
|
+
export { participantHandle } from '@wolffm/rooms-protocol';
|
|
68
|
+
interface IdentityInputs {
|
|
69
|
+
/** `X-User-Id`, post-sanitize: the registry userId. The `account` tier's source material. */
|
|
70
|
+
accountId?: string | null;
|
|
71
|
+
/** Fallback for a caller the edge could not name at all. */
|
|
72
|
+
socketId: string;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Mint an id for one connection.
|
|
76
|
+
*
|
|
77
|
+
* The deriver returns `{ id, tier }`; callers here have only ever wanted the
|
|
78
|
+
* id, and the tier they act on is the EDGE tier (`x-hadoku-tier`), which is a
|
|
79
|
+
* different question from which material the id was minted from.
|
|
80
|
+
*/
|
|
81
|
+
export declare function deriveParticipantId(input: IdentityInputs): ParticipantId;
|
|
82
|
+
/**
|
|
83
|
+
* The identity of a handshake, from its SANITIZED headers.
|
|
84
|
+
*
|
|
85
|
+
* `socketId` is only ever reached when the edge named nobody — it is the
|
|
86
|
+
* ephemeral tier's body, and an ephemeral participant cannot hold a seat across
|
|
87
|
+
* a reconnect because there is nothing durable to hold it against.
|
|
88
|
+
*/
|
|
89
|
+
export declare function identityFromHeaders(headers: IncomingHttpHeaders, socketId: string): ParticipantId;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minting a `ParticipantId` — now a thin binding over the fleet's derivation.
|
|
3
|
+
*
|
|
4
|
+
* The hashing used to live here: `@wolffm/rooms-protocol` is zero-dependency
|
|
5
|
+
* and isomorphic, so it shipped the *format* and stopped, and `node:crypto`'s
|
|
6
|
+
* `createHash` was ours to own. `@wolffm/rooms-server@0.39.4` publishes the
|
|
7
|
+
* other half — `createParticipantIdDeriver(domain)` — built from the scheme
|
|
8
|
+
* asked for in `docs/requests/P5_SERVICE_PARTICIPANT.md` §7.2, so this file is
|
|
9
|
+
* the domain string and the header plumbing and nothing else.
|
|
10
|
+
*
|
|
11
|
+
* The package hashes `` `${domain}\u001f${tier}\u001f${material}` `` with
|
|
12
|
+
* SHA-256, hex, truncated to `PARTICIPANT_ID_DIGEST_LENGTH`. `identity.test.ts`
|
|
13
|
+
* pins a known userId to the exact `fk:` id it derives, so a future bump that
|
|
14
|
+
* re-identifies everybody arrives as a red test rather than as a room full of
|
|
15
|
+
* strangers.
|
|
16
|
+
*
|
|
17
|
+
* ## The material is the userId, never the key
|
|
18
|
+
*
|
|
19
|
+
* The id is minted from `X-User-Id`, the registry's userId, passed as the
|
|
20
|
+
* deriver's `accountId` (the `account` tier). It used to be minted from
|
|
21
|
+
* `X-User-Key`, the credential, and a rotated key then made the same person a
|
|
22
|
+
* stranger to every room, pod and recording that named them (cardtable#2,
|
|
23
|
+
* hadoku_site IDENTITY_MODEL R7/R12, cardtable#25). The key is not an input at
|
|
24
|
+
* all now. Stores written under the key-derived ids are refused rather than
|
|
25
|
+
* remapped (`@wolffm/rooms-server/store`'s `STORE_VERSION` 2).
|
|
26
|
+
*
|
|
27
|
+
* ## Derived, never accepted
|
|
28
|
+
*
|
|
29
|
+
* The input comes from the EDGE HEADERS and from nowhere else. A caller the
|
|
30
|
+
* edge named nobody gets an `ephemeral` id tied to the socket. A browser that
|
|
31
|
+
* puts a `participantId` in a join payload is a parse error one layer up
|
|
32
|
+
* (`@hadoku/shared`'s strict client schemas), not a value anything here reads.
|
|
33
|
+
*
|
|
34
|
+
* `X-User-Id` is only believable after `sanitizeHeaders` has run — it
|
|
35
|
+
* overwrites tier and DELETES `x-user-id` on anything the edge did not vouch
|
|
36
|
+
* for. So this must run after `edgeProvenance`, which for a socket means after
|
|
37
|
+
* `io.engine.use(edgeProvenance)`. Handed unsanitized headers it would happily
|
|
38
|
+
* mint an `fk:` identity for a forged userId.
|
|
39
|
+
*
|
|
40
|
+
* The id is a domain-separated digest, so it can be logged, put on the wire and
|
|
41
|
+
* shown in a seat row; there is exactly one id per participant rather than an
|
|
42
|
+
* internal id plus a wire handle to keep in sync.
|
|
43
|
+
*/
|
|
44
|
+
import { createParticipantIdDeriver } from '@wolffm/rooms-server';
|
|
45
|
+
/**
|
|
46
|
+
* Domain separation for the digest.
|
|
47
|
+
*
|
|
48
|
+
* Present so that a friend key hashed here can never collide with the same
|
|
49
|
+
* bytes hashed for some other purpose, and versioned so that changing the
|
|
50
|
+
* scheme is a visible change rather than a silent re-identification of
|
|
51
|
+
* everybody.
|
|
52
|
+
*
|
|
53
|
+
* Per-TIER separation is NOT spelled here any more: the package hashes the tier
|
|
54
|
+
* in alongside the domain, and its own header explains why asking the caller
|
|
55
|
+
* for a second string was the wrong shape. There is nowhere to pass one.
|
|
56
|
+
*/
|
|
57
|
+
export const PARTICIPANT_ID_DOMAIN = 'hadoku.cardtable.participant.v1';
|
|
58
|
+
/**
|
|
59
|
+
* A short, wire-safe handle for a seat row — `@wolffm/rooms-protocol`'s, so the
|
|
60
|
+
* truncation length is one number for the fleet rather than one per app.
|
|
61
|
+
*
|
|
62
|
+
* Re-exported rather than imported at each call site because this module is
|
|
63
|
+
* where the rest of the server already looks for anything identity-shaped, and
|
|
64
|
+
* because the import that used to be here was the same function.
|
|
65
|
+
*/
|
|
66
|
+
export { participantHandle } from '@wolffm/rooms-protocol';
|
|
67
|
+
/**
|
|
68
|
+
* Bound once, at module scope. The factory shape is deliberate upstream: a
|
|
69
|
+
* `(input, domain)` function would let a call site mint a second identity space
|
|
70
|
+
* for the same person by passing a different string.
|
|
71
|
+
*/
|
|
72
|
+
const derive = createParticipantIdDeriver(PARTICIPANT_ID_DOMAIN);
|
|
73
|
+
/**
|
|
74
|
+
* Mint an id for one connection.
|
|
75
|
+
*
|
|
76
|
+
* The deriver returns `{ id, tier }`; callers here have only ever wanted the
|
|
77
|
+
* id, and the tier they act on is the EDGE tier (`x-hadoku-tier`), which is a
|
|
78
|
+
* different question from which material the id was minted from.
|
|
79
|
+
*/
|
|
80
|
+
export function deriveParticipantId(input) {
|
|
81
|
+
return derive(input).id;
|
|
82
|
+
}
|
|
83
|
+
function headerValue(headers, name) {
|
|
84
|
+
const raw = headers[name];
|
|
85
|
+
if (typeof raw === 'string' && raw !== '')
|
|
86
|
+
return raw;
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The identity of a handshake, from its SANITIZED headers.
|
|
91
|
+
*
|
|
92
|
+
* `socketId` is only ever reached when the edge named nobody — it is the
|
|
93
|
+
* ephemeral tier's body, and an ephemeral participant cannot hold a seat across
|
|
94
|
+
* a reconnect because there is nothing durable to hold it against.
|
|
95
|
+
*/
|
|
96
|
+
export function identityFromHeaders(headers, socketId) {
|
|
97
|
+
return deriveParticipantId({ accountId: headerValue(headers, 'x-user-id'), socketId });
|
|
98
|
+
}
|