@hadoku/rooms-server 0.45.56 → 0.45.57
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/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/session-claims.d.ts +95 -0
- package/dist/session-claims.d.ts.map +1 -0
- package/dist/session-claims.js +156 -0
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Room lifecycle and membership, stable participant identity, seating, avatar
|
|
5
5
|
* claims, name persistence across reconnect, presence (heartbeat, online/offline,
|
|
6
|
-
* left-after-grace), backpressure-aware emit, per-key rate limiting, and
|
|
6
|
+
* left-after-grace), one live tab per player per room (session claims), backpressure-aware emit, per-key rate limiting, and
|
|
7
7
|
* idle/empty reaping.
|
|
8
8
|
*
|
|
9
9
|
* Carries no hosting-app vocabulary and no opinion about what a room is FOR —
|
|
@@ -19,4 +19,5 @@ export * from './emit.js';
|
|
|
19
19
|
export * from './gc.js';
|
|
20
20
|
export * from './rateLimiter.js';
|
|
21
21
|
export * from './presence.js';
|
|
22
|
+
export * from './session-claims.js';
|
|
22
23
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Room lifecycle and membership, stable participant identity, seating, avatar
|
|
5
5
|
* claims, name persistence across reconnect, presence (heartbeat, online/offline,
|
|
6
|
-
* left-after-grace), backpressure-aware emit, per-key rate limiting, and
|
|
6
|
+
* left-after-grace), one live tab per player per room (session claims), backpressure-aware emit, per-key rate limiting, and
|
|
7
7
|
* idle/empty reaping.
|
|
8
8
|
*
|
|
9
9
|
* Carries no hosting-app vocabulary and no opinion about what a room is FOR —
|
|
@@ -19,6 +19,7 @@ export * from './emit.js';
|
|
|
19
19
|
export * from './gc.js';
|
|
20
20
|
export * from './rateLimiter.js';
|
|
21
21
|
export * from './presence.js';
|
|
22
|
+
export * from './session-claims.js';
|
|
22
23
|
// NOT `export * from '@wolffm/rooms-protocol'`. Re-exporting it would give
|
|
23
24
|
// every symbol two import paths, and the first thing that drifts is which one
|
|
24
25
|
// a file happens to use. Consumers depend on both packages explicitly.
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session claims: which browser tab a player plays a room from.
|
|
3
|
+
*
|
|
4
|
+
* A player opening the same room in a second tab (or on another device) must
|
|
5
|
+
* not end up with two tabs both acting for them. The rule, shared by every
|
|
6
|
+
* room app (`@wolffm/rooms-protocol`'s `session.ts` has the wire half):
|
|
7
|
+
*
|
|
8
|
+
* - **One holder per key.** A key is whatever the app scopes a session to,
|
|
9
|
+
* normally account × room. The holder is a TAB, not a connection: one tab
|
|
10
|
+
* may speak to a room over several sockets (a lobby socket and a match
|
|
11
|
+
* socket), and a reload or reconnect of the same tab is the same holder, so
|
|
12
|
+
* it never asks.
|
|
13
|
+
* - **A live holder is asked about, never silently replaced.** A claim from
|
|
14
|
+
* another tab answers `held`; only a claim with `take` (the player said yes)
|
|
15
|
+
* replaces it, and hands back the old tab's connections for the app to close.
|
|
16
|
+
* - **A stale holder is replaced without asking.** A connection is live while
|
|
17
|
+
* it beats (`heartbeat`); a holder none of whose connections has beaten for
|
|
18
|
+
* `idleMs` is gone in all but name (a closed laptop, a dropped network, a
|
|
19
|
+
* transport that has not timed out yet), so a new claim takes from it and
|
|
20
|
+
* says so (`stale: true`). Use the same window as your `PresenceTracker`.
|
|
21
|
+
* - **The last connection closing frees the key.** `release` on a closed
|
|
22
|
+
* connection; a tab with no connection left holds nothing.
|
|
23
|
+
*
|
|
24
|
+
* Pure arithmetic over keys, tab ids and connection ids: no sockets, no
|
|
25
|
+
* timers. Staleness is decided when a claim asks, from the clock, so nothing
|
|
26
|
+
* runs while nobody is claiming. Who counts as a player (and so claims at all)
|
|
27
|
+
* is the app's to decide; watchers should never be passed in.
|
|
28
|
+
*/
|
|
29
|
+
export type SessionClaimDecision =
|
|
30
|
+
/** The tab holds the key now. `fresh`: nobody held it before; else this tab already did. */
|
|
31
|
+
{
|
|
32
|
+
type: 'granted';
|
|
33
|
+
fresh: boolean;
|
|
34
|
+
}
|
|
35
|
+
/** Another tab holds it and is live. Nothing changed; ask the player, then claim with `take`. */
|
|
36
|
+
| {
|
|
37
|
+
type: 'held';
|
|
38
|
+
holderTabId: string;
|
|
39
|
+
lastSeen: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The tab took the key from `fromTabId`. `stale`: that tab had gone silent, so nobody was
|
|
43
|
+
* asked. `retire`: its connections under this key, which no longer hold anything; close them.
|
|
44
|
+
*/
|
|
45
|
+
| {
|
|
46
|
+
type: 'taken';
|
|
47
|
+
fromTabId: string;
|
|
48
|
+
stale: boolean;
|
|
49
|
+
retire: string[];
|
|
50
|
+
};
|
|
51
|
+
export interface SessionClaimsOptions {
|
|
52
|
+
/** A holder with no heartbeat on any connection for this long is stale. */
|
|
53
|
+
idleMs: number;
|
|
54
|
+
/** Clock. Defaults to `Date.now`. */
|
|
55
|
+
now?: () => number;
|
|
56
|
+
}
|
|
57
|
+
export declare class SessionClaims<K extends string = string> {
|
|
58
|
+
private readonly idleMs;
|
|
59
|
+
private readonly now;
|
|
60
|
+
private readonly claims;
|
|
61
|
+
/** Each connection's keys, and when it last beat. A connection appears only while it holds something. */
|
|
62
|
+
private readonly conns;
|
|
63
|
+
constructor(opts: SessionClaimsOptions);
|
|
64
|
+
/**
|
|
65
|
+
* `connectionId` of tab `tabId` claims `key`. A claim counts as a heartbeat
|
|
66
|
+
* for the connection. Pass `take` only once the player has said yes.
|
|
67
|
+
*/
|
|
68
|
+
claim(key: K, tabId: string, connectionId: string, opts?: {
|
|
69
|
+
take?: boolean;
|
|
70
|
+
}): SessionClaimDecision;
|
|
71
|
+
/** The connection is alive. False when it holds nothing. */
|
|
72
|
+
heartbeat(connectionId: string): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* The connection closed. Idempotent. Returns the keys it was the holder's
|
|
75
|
+
* last connection for, which nobody holds now.
|
|
76
|
+
*/
|
|
77
|
+
release(connectionId: string): K[];
|
|
78
|
+
/** The tab holding `key`, if any. */
|
|
79
|
+
holder(key: K): string | undefined;
|
|
80
|
+
/** Whether `tabId` holds `key`. */
|
|
81
|
+
holds(key: K, tabId: string): boolean;
|
|
82
|
+
/** Whether `key`'s holder has gone silent past the idle window. False when nobody holds it. */
|
|
83
|
+
isStale(key: K): boolean;
|
|
84
|
+
/** The holder's connections under `key`. */
|
|
85
|
+
connections(key: K): string[];
|
|
86
|
+
/** The keys a connection holds. */
|
|
87
|
+
keysOf(connectionId: string): K[];
|
|
88
|
+
/** Drop `key`'s claim at once (the room was deleted). Its connections hold nothing under it after. */
|
|
89
|
+
forget(key: K): void;
|
|
90
|
+
private hold;
|
|
91
|
+
private attach;
|
|
92
|
+
private detach;
|
|
93
|
+
private lastSeenOf;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=session-claims.d.ts.map
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E,MAAM,GAAG,CAAC,EAAE;IAelC,qCAAqC;IACrC,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,MAAM,GAAG,SAAS;IAIlC,mCAAmC;IACnC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO;IAIrC,+FAA+F;IAC/F,OAAO,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO;IAKxB,4CAA4C;IAC5C,WAAW,CAAC,GAAG,EAAE,CAAC,GAAG,MAAM,EAAE;IAI7B,mCAAmC;IACnC,MAAM,CAAC,YAAY,EAAE,MAAM,GAAG,CAAC,EAAE;IAIjC,sGAAsG;IACtG,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,IAAI;IAOpB,OAAO,CAAC,IAAI;IAMZ,OAAO,CAAC,MAAM;IAWd,OAAO,CAAC,MAAM;IAQd,OAAO,CAAC,UAAU;CAKnB"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session claims: which browser tab a player plays a room from.
|
|
3
|
+
*
|
|
4
|
+
* A player opening the same room in a second tab (or on another device) must
|
|
5
|
+
* not end up with two tabs both acting for them. The rule, shared by every
|
|
6
|
+
* room app (`@wolffm/rooms-protocol`'s `session.ts` has the wire half):
|
|
7
|
+
*
|
|
8
|
+
* - **One holder per key.** A key is whatever the app scopes a session to,
|
|
9
|
+
* normally account × room. The holder is a TAB, not a connection: one tab
|
|
10
|
+
* may speak to a room over several sockets (a lobby socket and a match
|
|
11
|
+
* socket), and a reload or reconnect of the same tab is the same holder, so
|
|
12
|
+
* it never asks.
|
|
13
|
+
* - **A live holder is asked about, never silently replaced.** A claim from
|
|
14
|
+
* another tab answers `held`; only a claim with `take` (the player said yes)
|
|
15
|
+
* replaces it, and hands back the old tab's connections for the app to close.
|
|
16
|
+
* - **A stale holder is replaced without asking.** A connection is live while
|
|
17
|
+
* it beats (`heartbeat`); a holder none of whose connections has beaten for
|
|
18
|
+
* `idleMs` is gone in all but name (a closed laptop, a dropped network, a
|
|
19
|
+
* transport that has not timed out yet), so a new claim takes from it and
|
|
20
|
+
* says so (`stale: true`). Use the same window as your `PresenceTracker`.
|
|
21
|
+
* - **The last connection closing frees the key.** `release` on a closed
|
|
22
|
+
* connection; a tab with no connection left holds nothing.
|
|
23
|
+
*
|
|
24
|
+
* Pure arithmetic over keys, tab ids and connection ids: no sockets, no
|
|
25
|
+
* timers. Staleness is decided when a claim asks, from the clock, so nothing
|
|
26
|
+
* runs while nobody is claiming. Who counts as a player (and so claims at all)
|
|
27
|
+
* is the app's to decide; watchers should never be passed in.
|
|
28
|
+
*/
|
|
29
|
+
export class SessionClaims {
|
|
30
|
+
idleMs;
|
|
31
|
+
now;
|
|
32
|
+
claims = new Map();
|
|
33
|
+
/** Each connection's keys, and when it last beat. A connection appears only while it holds something. */
|
|
34
|
+
conns = new Map();
|
|
35
|
+
constructor(opts) {
|
|
36
|
+
if (!(opts.idleMs > 0))
|
|
37
|
+
throw new RangeError(`idleMs must be > 0, got ${opts.idleMs}`);
|
|
38
|
+
this.idleMs = opts.idleMs;
|
|
39
|
+
this.now = opts.now ?? Date.now;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* `connectionId` of tab `tabId` claims `key`. A claim counts as a heartbeat
|
|
43
|
+
* for the connection. Pass `take` only once the player has said yes.
|
|
44
|
+
*/
|
|
45
|
+
claim(key, tabId, connectionId, opts = {}) {
|
|
46
|
+
if (tabId.length === 0)
|
|
47
|
+
throw new RangeError('tabId must not be empty');
|
|
48
|
+
const held = this.claims.get(key);
|
|
49
|
+
if (held === undefined) {
|
|
50
|
+
this.hold(key, tabId, connectionId);
|
|
51
|
+
return { type: 'granted', fresh: true };
|
|
52
|
+
}
|
|
53
|
+
if (held.tabId === tabId) {
|
|
54
|
+
this.attach(key, held, connectionId);
|
|
55
|
+
return { type: 'granted', fresh: false };
|
|
56
|
+
}
|
|
57
|
+
const lastSeen = this.lastSeenOf(held);
|
|
58
|
+
const stale = this.now() - lastSeen > this.idleMs;
|
|
59
|
+
if (!stale && opts.take !== true)
|
|
60
|
+
return { type: 'held', holderTabId: held.tabId, lastSeen };
|
|
61
|
+
const retire = [...held.connections];
|
|
62
|
+
for (const id of retire)
|
|
63
|
+
this.detach(key, id);
|
|
64
|
+
this.claims.delete(key);
|
|
65
|
+
this.hold(key, tabId, connectionId);
|
|
66
|
+
return { type: 'taken', fromTabId: held.tabId, stale, retire };
|
|
67
|
+
}
|
|
68
|
+
/** The connection is alive. False when it holds nothing. */
|
|
69
|
+
heartbeat(connectionId) {
|
|
70
|
+
const conn = this.conns.get(connectionId);
|
|
71
|
+
if (conn === undefined)
|
|
72
|
+
return false;
|
|
73
|
+
conn.lastSeen = this.now();
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The connection closed. Idempotent. Returns the keys it was the holder's
|
|
78
|
+
* last connection for, which nobody holds now.
|
|
79
|
+
*/
|
|
80
|
+
release(connectionId) {
|
|
81
|
+
const conn = this.conns.get(connectionId);
|
|
82
|
+
if (conn === undefined)
|
|
83
|
+
return [];
|
|
84
|
+
const freed = [];
|
|
85
|
+
for (const key of [...conn.keys]) {
|
|
86
|
+
this.detach(key, connectionId);
|
|
87
|
+
const claim = this.claims.get(key);
|
|
88
|
+
if (claim !== undefined && claim.connections.size === 0) {
|
|
89
|
+
this.claims.delete(key);
|
|
90
|
+
freed.push(key);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return freed;
|
|
94
|
+
}
|
|
95
|
+
/** The tab holding `key`, if any. */
|
|
96
|
+
holder(key) {
|
|
97
|
+
return this.claims.get(key)?.tabId;
|
|
98
|
+
}
|
|
99
|
+
/** Whether `tabId` holds `key`. */
|
|
100
|
+
holds(key, tabId) {
|
|
101
|
+
return this.claims.get(key)?.tabId === tabId;
|
|
102
|
+
}
|
|
103
|
+
/** Whether `key`'s holder has gone silent past the idle window. False when nobody holds it. */
|
|
104
|
+
isStale(key) {
|
|
105
|
+
const claim = this.claims.get(key);
|
|
106
|
+
return claim !== undefined && this.now() - this.lastSeenOf(claim) > this.idleMs;
|
|
107
|
+
}
|
|
108
|
+
/** The holder's connections under `key`. */
|
|
109
|
+
connections(key) {
|
|
110
|
+
return [...(this.claims.get(key)?.connections ?? [])];
|
|
111
|
+
}
|
|
112
|
+
/** The keys a connection holds. */
|
|
113
|
+
keysOf(connectionId) {
|
|
114
|
+
return [...(this.conns.get(connectionId)?.keys ?? [])];
|
|
115
|
+
}
|
|
116
|
+
/** Drop `key`'s claim at once (the room was deleted). Its connections hold nothing under it after. */
|
|
117
|
+
forget(key) {
|
|
118
|
+
const claim = this.claims.get(key);
|
|
119
|
+
if (claim === undefined)
|
|
120
|
+
return;
|
|
121
|
+
for (const id of [...claim.connections])
|
|
122
|
+
this.detach(key, id);
|
|
123
|
+
this.claims.delete(key);
|
|
124
|
+
}
|
|
125
|
+
hold(key, tabId, connectionId) {
|
|
126
|
+
const claim = { tabId, connections: new Set() };
|
|
127
|
+
this.claims.set(key, claim);
|
|
128
|
+
this.attach(key, claim, connectionId);
|
|
129
|
+
}
|
|
130
|
+
attach(key, claim, connectionId) {
|
|
131
|
+
claim.connections.add(connectionId);
|
|
132
|
+
const at = this.now();
|
|
133
|
+
const conn = this.conns.get(connectionId);
|
|
134
|
+
if (conn === undefined)
|
|
135
|
+
this.conns.set(connectionId, { keys: new Set([key]), lastSeen: at });
|
|
136
|
+
else {
|
|
137
|
+
conn.keys.add(key);
|
|
138
|
+
conn.lastSeen = at;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
detach(key, connectionId) {
|
|
142
|
+
this.claims.get(key)?.connections.delete(connectionId);
|
|
143
|
+
const conn = this.conns.get(connectionId);
|
|
144
|
+
if (conn === undefined)
|
|
145
|
+
return;
|
|
146
|
+
conn.keys.delete(key);
|
|
147
|
+
if (conn.keys.size === 0)
|
|
148
|
+
this.conns.delete(connectionId);
|
|
149
|
+
}
|
|
150
|
+
lastSeenOf(claim) {
|
|
151
|
+
let latest = -Infinity;
|
|
152
|
+
for (const id of claim.connections)
|
|
153
|
+
latest = Math.max(latest, this.conns.get(id)?.lastSeen ?? -Infinity);
|
|
154
|
+
return latest;
|
|
155
|
+
}
|
|
156
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/rooms-server",
|
|
3
|
-
"version": "0.45.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.45.57",
|
|
4
|
+
"gitHead": "14172aa10cda73d19a6c845f77e0995a845fe5f8",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "Room lifecycle, membership, seating, identity and backpressure-aware emit for a socket.io server.",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"directory": "packages/rooms-server"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@0.45.
|
|
26
|
+
"@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@0.45.57"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"socket.io": "^4.8.3"
|