@hadoku/ui-shell 0.1.137 → 0.1.138
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/session/index.d.ts +72 -0
- package/dist/session/index.js +176 -0
- package/package.json +7 -3
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a page does when its session is gone: stop asking, and go back through the edge's sign-in
|
|
3
|
+
* wall. A signed-out caller gets a 401 from the edge on every gated path, a page load too, and
|
|
4
|
+
* every socket's handshake. Without this a tab left open across a lost session retries every socket
|
|
5
|
+
* forever (reconnects never more than a second apart) and every poller polls on: one browser sent
|
|
6
|
+
* 45k refused requests in 22 hours (2026-11-23), its page dead to its player the whole time.
|
|
7
|
+
*
|
|
8
|
+
* ONE watch per page, two ways in:
|
|
9
|
+
* - every `fetch` the page makes under `scope` (one wrapper, installed at start, so no client has
|
|
10
|
+
* to remember to): a 401 is a suspicion;
|
|
11
|
+
* - every socket handed to {@link watchSocket}: a `connect_error` is a suspicion (the socket
|
|
12
|
+
* library never says why a handshake failed).
|
|
13
|
+
* A suspicion is checked by ONE probe, `probeUrl` (a gated GET under `scope`), at most once per
|
|
14
|
+
* {@link PROBE_GAP_MS}: only the edge's 401 there means the session is gone. Anything else (a
|
|
15
|
+
* restart's 502, a 404, a network error, a backend's own 401 on some other path) is not a lost
|
|
16
|
+
* session, and the page carries on.
|
|
17
|
+
*
|
|
18
|
+
* A lost session stops everything at once: every watched socket is disconnected (which ends its
|
|
19
|
+
* reconnecting), every later `fetch` under `scope` fails without leaving the page, and the page
|
|
20
|
+
* reloads. The edge answers that load with its sign-in wall, which gives a signed-out visitor an
|
|
21
|
+
* account and sends them on. A second loss within {@link RELOAD_GUARD_MS} of one this tab already
|
|
22
|
+
* reloaded for does not reload again: the page stays stopped rather than loop.
|
|
23
|
+
*/
|
|
24
|
+
/** The least time between two probes. */
|
|
25
|
+
export declare const PROBE_GAP_MS = 10000;
|
|
26
|
+
/** A loss inside this long after the last reload for one stays stopped instead of reloading. */
|
|
27
|
+
export declare const RELOAD_GUARD_MS = 60000;
|
|
28
|
+
/** Where this tab notes when it last reloaded for a lost session. */
|
|
29
|
+
export declare const RELOADED_KEY = "hadoku:session-lost-reload";
|
|
30
|
+
/** What the watch needs of a socket (socket.io's `Socket` is one): its connect errors and its end, and a way to end it. */
|
|
31
|
+
export interface WatchedSocket {
|
|
32
|
+
on(event: 'connect_error' | 'disconnect', listener: (reason?: unknown) => void): unknown;
|
|
33
|
+
off(event: 'connect_error' | 'disconnect', listener: (reason?: unknown) => void): unknown;
|
|
34
|
+
disconnect(): unknown;
|
|
35
|
+
}
|
|
36
|
+
/** The page, as far as the watch touches it. */
|
|
37
|
+
export interface SessionHost {
|
|
38
|
+
fetch: typeof fetch;
|
|
39
|
+
location: {
|
|
40
|
+
origin: string;
|
|
41
|
+
reload(): void;
|
|
42
|
+
};
|
|
43
|
+
sessionStorage?: Pick<Storage, 'getItem' | 'setItem'>;
|
|
44
|
+
}
|
|
45
|
+
export interface SessionWatchOptions {
|
|
46
|
+
/** The path prefix the edge gates for this app, `/cardtable/`: a 401 under it means signed out. */
|
|
47
|
+
scope: string;
|
|
48
|
+
/** A gated GET under `scope`, asked to confirm a suspicion. */
|
|
49
|
+
probeUrl: string;
|
|
50
|
+
host?: SessionHost;
|
|
51
|
+
now?: () => number;
|
|
52
|
+
/** Told once, when a probe confirms the loss. */
|
|
53
|
+
onLost?: () => void;
|
|
54
|
+
}
|
|
55
|
+
export interface SessionWatch {
|
|
56
|
+
/** Something looked like a lost session: probe, unless one was asked lately. */
|
|
57
|
+
suspect(): void;
|
|
58
|
+
lost(): boolean;
|
|
59
|
+
/** Takes the fetch wrapper off and forgets every socket (an embed's unmount). */
|
|
60
|
+
stop(): void;
|
|
61
|
+
}
|
|
62
|
+
/** Signed out: the request a stopped page did not send. */
|
|
63
|
+
export declare class SessionLostError extends Error {
|
|
64
|
+
constructor();
|
|
65
|
+
}
|
|
66
|
+
/** Start the page's one watch. A second start replaces the first. */
|
|
67
|
+
export declare function startSessionWatch(options: SessionWatchOptions): SessionWatch;
|
|
68
|
+
/**
|
|
69
|
+
* Hand a socket to the page's watch: its connect errors are suspicions, and a lost session ends it.
|
|
70
|
+
* The page's own `disconnect()` lets go of it. Nothing, with no watch started (a test, a harness page).
|
|
71
|
+
*/
|
|
72
|
+
export declare function watchSocket(socket: WatchedSocket): void;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a page does when its session is gone: stop asking, and go back through the edge's sign-in
|
|
3
|
+
* wall. A signed-out caller gets a 401 from the edge on every gated path, a page load too, and
|
|
4
|
+
* every socket's handshake. Without this a tab left open across a lost session retries every socket
|
|
5
|
+
* forever (reconnects never more than a second apart) and every poller polls on: one browser sent
|
|
6
|
+
* 45k refused requests in 22 hours (2026-11-23), its page dead to its player the whole time.
|
|
7
|
+
*
|
|
8
|
+
* ONE watch per page, two ways in:
|
|
9
|
+
* - every `fetch` the page makes under `scope` (one wrapper, installed at start, so no client has
|
|
10
|
+
* to remember to): a 401 is a suspicion;
|
|
11
|
+
* - every socket handed to {@link watchSocket}: a `connect_error` is a suspicion (the socket
|
|
12
|
+
* library never says why a handshake failed).
|
|
13
|
+
* A suspicion is checked by ONE probe, `probeUrl` (a gated GET under `scope`), at most once per
|
|
14
|
+
* {@link PROBE_GAP_MS}: only the edge's 401 there means the session is gone. Anything else (a
|
|
15
|
+
* restart's 502, a 404, a network error, a backend's own 401 on some other path) is not a lost
|
|
16
|
+
* session, and the page carries on.
|
|
17
|
+
*
|
|
18
|
+
* A lost session stops everything at once: every watched socket is disconnected (which ends its
|
|
19
|
+
* reconnecting), every later `fetch` under `scope` fails without leaving the page, and the page
|
|
20
|
+
* reloads. The edge answers that load with its sign-in wall, which gives a signed-out visitor an
|
|
21
|
+
* account and sends them on. A second loss within {@link RELOAD_GUARD_MS} of one this tab already
|
|
22
|
+
* reloaded for does not reload again: the page stays stopped rather than loop.
|
|
23
|
+
*/
|
|
24
|
+
/** The least time between two probes. */
|
|
25
|
+
export const PROBE_GAP_MS = 10_000;
|
|
26
|
+
/** A loss inside this long after the last reload for one stays stopped instead of reloading. */
|
|
27
|
+
export const RELOAD_GUARD_MS = 60_000;
|
|
28
|
+
/** Where this tab notes when it last reloaded for a lost session. */
|
|
29
|
+
export const RELOADED_KEY = 'hadoku:session-lost-reload';
|
|
30
|
+
/** socket.io's reason when the page itself closed a socket: the watch lets go of it. */
|
|
31
|
+
const CLOSED_BY_PAGE = 'io client disconnect';
|
|
32
|
+
let current = null;
|
|
33
|
+
/** Signed out: the request a stopped page did not send. */
|
|
34
|
+
export class SessionLostError extends Error {
|
|
35
|
+
constructor() {
|
|
36
|
+
super('signed out');
|
|
37
|
+
this.name = 'SessionLostError';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function urlOf(input) {
|
|
41
|
+
if (typeof input === 'string')
|
|
42
|
+
return input;
|
|
43
|
+
if (input instanceof URL)
|
|
44
|
+
return input.href;
|
|
45
|
+
return input.url;
|
|
46
|
+
}
|
|
47
|
+
/** Start the page's one watch. A second start replaces the first. */
|
|
48
|
+
export function startSessionWatch(options) {
|
|
49
|
+
current?.stop();
|
|
50
|
+
const host = options.host ?? window;
|
|
51
|
+
const now = options.now ?? Date.now;
|
|
52
|
+
const scopeUrl = new URL(options.scope, host.location.origin);
|
|
53
|
+
const pageFetch = host.fetch;
|
|
54
|
+
/** Each watched socket, with its own two listeners. */
|
|
55
|
+
const sockets = new Map();
|
|
56
|
+
const release = (socket) => {
|
|
57
|
+
const listeners = sockets.get(socket);
|
|
58
|
+
if (listeners === undefined)
|
|
59
|
+
return;
|
|
60
|
+
sockets.delete(socket);
|
|
61
|
+
socket.off('connect_error', listeners.onError);
|
|
62
|
+
socket.off('disconnect', listeners.onClose);
|
|
63
|
+
};
|
|
64
|
+
let isLost = false;
|
|
65
|
+
let probing = false;
|
|
66
|
+
let lastProbe = -Infinity;
|
|
67
|
+
const inScope = (input) => {
|
|
68
|
+
try {
|
|
69
|
+
const url = new URL(urlOf(input), host.location.origin);
|
|
70
|
+
return url.origin === scopeUrl.origin && url.pathname.startsWith(scopeUrl.pathname);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
/** When this tab last reloaded for a lost session, noting now as the next one's answer. */
|
|
77
|
+
const swapLastReload = () => {
|
|
78
|
+
const store = host.sessionStorage;
|
|
79
|
+
try {
|
|
80
|
+
const raw = store?.getItem(RELOADED_KEY) ?? null;
|
|
81
|
+
store?.setItem(RELOADED_KEY, String(now()));
|
|
82
|
+
return raw === null ? null : Number(raw);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// Storage refused (a private window): reload anyway; the edge's wall is what ends a loop.
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
const reload = () => {
|
|
90
|
+
const last = swapLastReload();
|
|
91
|
+
if (last !== null && Number.isFinite(last) && now() - last < RELOAD_GUARD_MS)
|
|
92
|
+
return;
|
|
93
|
+
host.location.reload();
|
|
94
|
+
};
|
|
95
|
+
const lose = () => {
|
|
96
|
+
if (isLost)
|
|
97
|
+
return;
|
|
98
|
+
isLost = true;
|
|
99
|
+
for (const socket of [...sockets.keys()]) {
|
|
100
|
+
release(socket);
|
|
101
|
+
socket.disconnect();
|
|
102
|
+
}
|
|
103
|
+
options.onLost?.();
|
|
104
|
+
reload();
|
|
105
|
+
};
|
|
106
|
+
const probe = async () => {
|
|
107
|
+
probing = true;
|
|
108
|
+
lastProbe = now();
|
|
109
|
+
try {
|
|
110
|
+
const res = await pageFetch.call(undefined, options.probeUrl, { credentials: 'same-origin', cache: 'no-store', headers: { accept: 'application/json' } });
|
|
111
|
+
if (res.status === 401)
|
|
112
|
+
lose();
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
// No answer is not "signed out": the next suspicion asks again.
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
probing = false;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const suspect = () => {
|
|
122
|
+
if (isLost || probing || now() - lastProbe < PROBE_GAP_MS)
|
|
123
|
+
return;
|
|
124
|
+
void probe();
|
|
125
|
+
};
|
|
126
|
+
const watched = async (input, init) => {
|
|
127
|
+
if (!inScope(input))
|
|
128
|
+
return pageFetch.call(undefined, input, init);
|
|
129
|
+
if (isLost)
|
|
130
|
+
throw new SessionLostError();
|
|
131
|
+
const res = await pageFetch.call(undefined, input, init);
|
|
132
|
+
if (res.status === 401)
|
|
133
|
+
suspect();
|
|
134
|
+
return res;
|
|
135
|
+
};
|
|
136
|
+
host.fetch = watched;
|
|
137
|
+
const watch = {
|
|
138
|
+
suspect,
|
|
139
|
+
lost: () => isLost,
|
|
140
|
+
add(socket) {
|
|
141
|
+
if (isLost) {
|
|
142
|
+
socket.disconnect();
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (sockets.has(socket))
|
|
146
|
+
return;
|
|
147
|
+
const listeners = {
|
|
148
|
+
onError: () => suspect(),
|
|
149
|
+
onClose: (reason) => {
|
|
150
|
+
if (reason === CLOSED_BY_PAGE)
|
|
151
|
+
release(socket);
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
sockets.set(socket, listeners);
|
|
155
|
+
socket.on('connect_error', listeners.onError);
|
|
156
|
+
socket.on('disconnect', listeners.onClose);
|
|
157
|
+
},
|
|
158
|
+
stop() {
|
|
159
|
+
if (host.fetch === watched)
|
|
160
|
+
host.fetch = pageFetch;
|
|
161
|
+
for (const socket of [...sockets.keys()])
|
|
162
|
+
release(socket);
|
|
163
|
+
if (current === watch)
|
|
164
|
+
current = null;
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
current = watch;
|
|
168
|
+
return watch;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Hand a socket to the page's watch: its connect errors are suspicions, and a lost session ends it.
|
|
172
|
+
* The page's own `disconnect()` lets go of it. Nothing, with no watch started (a test, a harness page).
|
|
173
|
+
*/
|
|
174
|
+
export function watchSocket(socket) {
|
|
175
|
+
current?.add(socket);
|
|
176
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/ui-shell",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.1.138",
|
|
4
|
+
"gitHead": "5ce3e147c511684342d92920c160be7aaa7a44d8",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"description": "The parts of a game's UI any game keeps: chrome, trim, room shell, embed mount, theme and settings frame, UI scale, usage counting, the sound mixer, motion and generic components. One appId parameterizes its storage keys, events and chat prefix. MIT; imports only react, react-dom, platform-wire and @wolffm/*.",
|
|
@@ -42,6 +42,10 @@
|
|
|
42
42
|
"types": "./dist/usage/index.d.ts",
|
|
43
43
|
"import": "./dist/usage/index.js"
|
|
44
44
|
},
|
|
45
|
+
"./session": {
|
|
46
|
+
"types": "./dist/session/index.d.ts",
|
|
47
|
+
"import": "./dist/session/index.js"
|
|
48
|
+
},
|
|
45
49
|
"./sound": {
|
|
46
50
|
"types": "./dist/sound/index.d.ts",
|
|
47
51
|
"import": "./dist/sound/index.js"
|
|
@@ -64,7 +68,7 @@
|
|
|
64
68
|
"react-dom": "^19.2.8"
|
|
65
69
|
},
|
|
66
70
|
"dependencies": {
|
|
67
|
-
"@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.
|
|
71
|
+
"@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.138",
|
|
68
72
|
"@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.2.2",
|
|
69
73
|
"@wolffm/themes": "npm:@hadoku/themes@^5.14.19",
|
|
70
74
|
"zod": "4.5.4"
|