@pi-chamber/web 0.9.5 → 0.9.7
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/assets/DiagramView-BvrCkm_z.js +1 -0
- package/dist/assets/{DiffView-BmIVvheE.js → DiffView-DMvRLRN1.js} +1 -1
- package/dist/assets/FileSaveConflictDialog-DGa9u43J.js +3 -0
- package/dist/assets/FilesView-CNHu4RnC.js +6 -0
- package/dist/assets/{GitView-BNkzec7G.js → GitView-Cbepz-bR.js} +3 -3
- package/dist/assets/{JsonTreeView-C1Kzaybf.js → JsonTreeView-C2X5oXQm.js} +1 -1
- package/dist/assets/{MarkdownRendererImpl-qmKiFVpO.js → MarkdownRendererImpl-BanSCkPy.js} +2 -2
- package/dist/assets/{PatchDiff-D9JesZL1.js → PatchDiff-i_9BIlgo.js} +1 -1
- package/dist/assets/{SessionSwitcherContent-DplV_ApH.js → SessionSwitcherContent-DxS_rFuS.js} +1 -1
- package/dist/assets/{SessionSwitcherDropdown-DdgDKT6K.js → SessionSwitcherDropdown-AVghToww.js} +2 -2
- package/dist/assets/{SessionUnreadDot-BdSDgvhC.js → SessionUnreadDot-CDRZswx-.js} +1 -1
- package/dist/assets/SettingsView-hyAGp836.js +2 -0
- package/dist/assets/SettingsWindow-BfPUzBbq.js +1 -0
- package/dist/assets/{ToolOutputDialog-DaF1xcM0.js → ToolOutputDialog-CJ87b80H.js} +5 -5
- package/dist/assets/{ToolPartDiffPreview-Di6Uo0Dt.js → ToolPartDiffPreview-bF486Lz1.js} +1 -1
- package/dist/assets/{VirtualizedFileDiff-wqcrY0yr.js → VirtualizedFileDiff-CFpvcZ1g.js} +1 -1
- package/dist/assets/{collapsible-DBQnXTNm.js → collapsible-DesjbW8a.js} +1 -1
- package/dist/assets/{computeVirtualFileMetrics-Cr3jh0d6.js → computeVirtualFileMetrics-CsAu9rOt.js} +2 -2
- package/dist/assets/{contextFileOpenGuard-C_AOTYQD.js → contextFileOpenGuard-rILslsBJ.js} +1 -1
- package/dist/assets/{gitStatusPredicates-BvPOjeOy.js → gitStatusPredicates-D_31YElh.js} +1 -1
- package/dist/assets/index-BXgR2dKM.css +1 -0
- package/dist/assets/index-BZpvAhrX.js +8 -0
- package/dist/assets/{index-B8Wt2aN3.js → index-CXLVW_GG.js} +1 -1
- package/dist/assets/{isDiffPlainText-L9uvgz2F.js → isDiffPlainText-BX609Lau.js} +1 -1
- package/dist/assets/{isFilePlainText-C4yk5BOo.js → isFilePlainText-FgBWqu9b.js} +1 -1
- package/dist/assets/{main-DCK6lLCo.js → main-C_pL2r_k.js} +2 -2
- package/dist/assets/{main-C1OSzD-E.js → main-tv2uysSs.js} +7 -7
- package/dist/assets/{markdownCore-Czb_lINQ.js → markdownCore-D3Ibaffd.js} +1 -1
- package/dist/assets/{miniChat-CXiSlptL.js → miniChat-B0xQlBzZ.js} +2 -2
- package/dist/assets/{mobile-D0HVxiyJ.js → mobile-BmSlVuhn.js} +2 -2
- package/dist/assets/renderElectronMiniChatApp-C4xZzAnn.js +2 -0
- package/dist/assets/renderMobileApp-Bi8wTGd5.js +11 -0
- package/dist/assets/runtimeEndpointReset-B6_gFvBD.js +53 -0
- package/dist/assets/{runtimeSurface-vilE4xyj.js → runtimeSurface-egSD9pqj.js} +1 -1
- package/dist/assets/{useAppFontEffects-BP07Vl6M.js → useAppFontEffects-C9QQrBIx.js} +104 -104
- package/dist/assets/useFilesViewSearch-BTZutbn-.js +7 -0
- package/dist/assets/{usePushVisibilityBeacon-CChMvoTL.js → usePushVisibilityBeacon-BZcHxSo-.js} +1 -1
- package/dist/assets/useSwitcherItems-Dw5tHQh8.js +1 -0
- package/dist/assets/{vendor-codemirror-language-data-BJrrV83E.js → vendor-codemirror-language-data-0f_rLdj_.js} +2 -2
- package/dist/assets/{web-components-B5sMa_kC.js → web-components-CYngz9Ml.js} +1 -1
- package/dist/index.html +4 -4
- package/dist/mini-chat.html +3 -3
- package/dist/mobile.html +3 -3
- package/package.json +1 -1
- package/server/index.js +17 -3
- package/server/lib/client-auth/DOCUMENTATION.md +184 -0
- package/server/lib/client-auth/live-revocation.test.js +567 -0
- package/server/lib/client-auth/principal-tracker.js +383 -0
- package/server/lib/client-auth/principal-tracker.test.js +321 -0
- package/server/lib/client-auth/remote-clients.js +31 -0
- package/server/lib/client-auth/remote-clients.test.js +44 -0
- package/server/lib/fs/DOCUMENTATION.md +15 -3
- package/server/lib/fs/routes.js +369 -51
- package/server/lib/fs/routes.test.js +565 -3
- package/server/lib/fs/search.js +74 -29
- package/server/lib/pi/event-stream.js +269 -0
- package/server/lib/pi/event-stream.test.js +776 -0
- package/server/lib/pi/routes.js +132 -28
- package/server/lib/pi/routes.test.js +213 -4
- package/server/lib/pi/session-daemon/DOCUMENTATION.md +6 -4
- package/server/lib/pi/session-daemon/ipc-client.js +19 -15
- package/server/lib/pi/session-daemon/ipc-client.test.js +113 -1
- package/server/lib/pi/session-daemon/prompt-operations.test.js +9 -3
- package/server/lib/pi/session-daemon/prompt-preflight.test.js +556 -0
- package/server/lib/pi/session-daemon/send-operation-dedup.test.js +331 -0
- package/server/lib/pi/session-daemon/send-operation-registry.js +264 -0
- package/server/lib/pi/session-daemon/send-operation-registry.test.js +205 -0
- package/server/lib/pi/session-daemon/session-daemon-epoch.test.js +349 -0
- package/server/lib/pi/session-daemon/session-daemon-extensions.e2e.test.js +16 -5
- package/server/lib/pi/session-daemon/session-daemon-extensions.test.js +3 -1
- package/server/lib/pi/session-daemon/session-daemon.js +339 -37
- package/server/lib/pi/session-daemon/session-daemon.test.js +47 -17
- package/server/lib/pi/session-daemon/session-idle-residency.test.js +8 -1
- package/server/lib/pi/session-daemon/session-replay.test.js +11 -4
- package/server/lib/pi/session-daemon/supervisor.js +30 -5
- package/server/lib/pi/session-daemon/supervisor.test.js +4 -3
- package/server/lib/pi/ui-settings-store.js +1 -1
- package/server/lib/pi/ui-settings-store.test.js +3 -1
- package/server/lib/server/core-routes.js +11 -1
- package/server/lib/server/cors.js +6 -0
- package/server/lib/server/cors.test.js +3 -0
- package/server/lib/stt/DOCUMENTATION.md +2 -0
- package/server/lib/stt/runtime.js +39 -2
- package/server/lib/stt/runtime.test.js +53 -0
- package/server/lib/terminal/DOCUMENTATION.md +2 -0
- package/server/lib/terminal/runtime.js +48 -2
- package/server/lib/terminal/runtime.test.js +35 -0
- package/server/lib/ui-auth/DOCUMENTATION.md +13 -4
- package/server/lib/ui-auth/ui-auth.js +98 -0
- package/server/lib/ui-auth/ui-auth.test.js +249 -0
- package/server/lib/workspace/host.js +9 -3
- package/server/lib/workspace/host.test.js +15 -0
- package/dist/assets/DiagramEditor-BPDkGHsj.js +0 -1
- package/dist/assets/DiagramView-D_EfDz2d.js +0 -1
- package/dist/assets/FilesView-CJytSUAX.js +0 -6
- package/dist/assets/SettingsView-z8uf0XyM.js +0 -2
- package/dist/assets/SettingsWindow-CPDmgNg2.js +0 -1
- package/dist/assets/index-DIF8b4bh.css +0 -1
- package/dist/assets/index-jrfQYSvP.js +0 -8
- package/dist/assets/renderElectronMiniChatApp-BcG209a4.js +0 -2
- package/dist/assets/renderMobileApp-B5x_PWlc.js +0 -11
- package/dist/assets/runtimeEndpointReset-CUiPe6dQ.js +0 -53
- package/dist/assets/useFilesViewSearch-oxIKk-ea.js +0 -7
- package/dist/assets/useSwitcherItems-_RSo3F-A.js +0 -1
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live principal tracking and credential-revocation propagation.
|
|
3
|
+
*
|
|
4
|
+
* Two focused pieces:
|
|
5
|
+
*
|
|
6
|
+
* - `createPrincipalTracker` tracks live connections (SSE event streams,
|
|
7
|
+
* terminal/dictation WebSocket upgrades) under the principal derived from
|
|
8
|
+
* trusted auth at the owning boundary — never under client-supplied IDs.
|
|
9
|
+
* Closing is synchronous and immediate; entries are removed when the
|
|
10
|
+
* connection ends naturally, fails, or the owner detaches a pending one.
|
|
11
|
+
*
|
|
12
|
+
* Resource bound: at most `maxPerPrincipal` live connections per principal
|
|
13
|
+
* and `maxTotal` live connections globally (defaults 50 / 1000). A new
|
|
14
|
+
* registration beyond either bound is reject-closed synchronously
|
|
15
|
+
* (`close('over-limit')`) without evicting unrelated entries. Memory is
|
|
16
|
+
* therefore O(maxTotal) entries. After `dispose()` new registrations are
|
|
17
|
+
* reject-closed with `close('shutdown')`.
|
|
18
|
+
*
|
|
19
|
+
* - `createRevocationCoordinator` wires revocation to the tracker. Revocation
|
|
20
|
+
* happens against the shared on-disk credential store
|
|
21
|
+
* (`remote-clients.json`, guarded by a cross-process file lock), which more
|
|
22
|
+
* than one process can write (the server, the `pichamber connect-url`
|
|
23
|
+
* pairing CLI). There is no reliable cross-process revocation emitter, so
|
|
24
|
+
* propagation is defined in two layers, and the emitter layer must never be
|
|
25
|
+
* claimed as sufficient on its own:
|
|
26
|
+
*
|
|
27
|
+
* 1. In-process: the auth-owning revoke route calls `clientRevoked()` right
|
|
28
|
+
* after the store write commits, so connections are closed inside that
|
|
29
|
+
* request — immediate, bounded latency, no timers involved.
|
|
30
|
+
* 2. Cross-process: a bounded poll of `listRevokedClientIds()` (default
|
|
31
|
+
* every 15 seconds, timer unref'd) converges this process with
|
|
32
|
+
* revocations committed by any other process sharing the data directory.
|
|
33
|
+
* A failed poll tick is transient: current state is kept and the next
|
|
34
|
+
* tick retries; it is never treated as an authoritative empty result.
|
|
35
|
+
*
|
|
36
|
+
* Verify-registration barrier: bearer/URL-token validation awaits the store
|
|
37
|
+
* (and the WebSocket path additionally awaits the origin gate), so a
|
|
38
|
+
* revocation can commit and close between verification and registration. The
|
|
39
|
+
* coordinator therefore remembers every in-process revocation
|
|
40
|
+
* (`revokedClientIds`, bounded FIFO, default 2000 entries) and a monotonic
|
|
41
|
+
* global-invalidation generation (bumped by every `revokeAllLive()`).
|
|
42
|
+
* `trackLiveConnection()` checks both synchronously — revoked-ID match
|
|
43
|
+
* reject-closes with `close('credential-revoked')`, stale-generation match
|
|
44
|
+
* reject-closes with `close('global-invalidation')` — without trusting the
|
|
45
|
+
* caller's claim that verification passed. The synchronous check+insert is
|
|
46
|
+
* atomic on the event loop, so no in-process revoke can slip between them.
|
|
47
|
+
* Auth boundaries must capture `getGeneration()` BEFORE their first await
|
|
48
|
+
* and pass it as `generation` at track time; the WebSocket upgrade path must
|
|
49
|
+
* additionally capture before the origin check and must not call
|
|
50
|
+
* `handleUpgrade` when the returned entry is inactive (`active === false`).
|
|
51
|
+
*
|
|
52
|
+
* Latency guarantee (healthy storage only): a revocation observed by this
|
|
53
|
+
* process closes its tracked connections synchronously within the revoking
|
|
54
|
+
* call; a revocation committed by another process is applied within one poll
|
|
55
|
+
* interval after the next SUCCESSFUL poll. A failed poll tick provides no
|
|
56
|
+
* guarantee — already-established cross-process connections stay open until
|
|
57
|
+
* a later tick succeeds (degraded retention), while new bearer/URL-token
|
|
58
|
+
* establishment remains authoritatively gated by direct store/secret reads
|
|
59
|
+
* and is unaffected by poll health.
|
|
60
|
+
*
|
|
61
|
+
* Topology: one server process per data directory. The pairing CLI shares
|
|
62
|
+
* only `remote-clients.json` (never the JWT secret); JWT-secret rotation for
|
|
63
|
+
* browser sessions is in-process only and has no cross-process poll. See
|
|
64
|
+
* `../ui-auth/DOCUMENTATION.md` for the browser-session contract.
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
const DEFAULT_REVOCATION_POLL_INTERVAL_MS = 15_000;
|
|
68
|
+
|
|
69
|
+
export const MAX_LIVE_CONNECTIONS_PER_PRINCIPAL = 50;
|
|
70
|
+
export const MAX_LIVE_CONNECTIONS_GLOBAL = 1000;
|
|
71
|
+
export const MAX_REMEMBERED_REVOKED_CLIENTS = 2000;
|
|
72
|
+
|
|
73
|
+
export const createPrincipalTracker = ({
|
|
74
|
+
onCloseError,
|
|
75
|
+
maxPerPrincipal = MAX_LIVE_CONNECTIONS_PER_PRINCIPAL,
|
|
76
|
+
maxTotal = MAX_LIVE_CONNECTIONS_GLOBAL,
|
|
77
|
+
} = {}) => {
|
|
78
|
+
const byPrincipal = new Map();
|
|
79
|
+
let entrySequence = 0;
|
|
80
|
+
let trackerDisposed = false;
|
|
81
|
+
|
|
82
|
+
const remove = (entry) => {
|
|
83
|
+
const set = byPrincipal.get(entry.principal);
|
|
84
|
+
if (!set) return;
|
|
85
|
+
set.delete(entry);
|
|
86
|
+
if (set.size === 0) byPrincipal.delete(entry.principal);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const rejectClose = (principal, close, reason) => {
|
|
90
|
+
const entry = {
|
|
91
|
+
id: ++entrySequence,
|
|
92
|
+
principal,
|
|
93
|
+
active: false,
|
|
94
|
+
revoke: () => false,
|
|
95
|
+
end: () => {},
|
|
96
|
+
};
|
|
97
|
+
try {
|
|
98
|
+
close(reason);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
onCloseError?.({ entry, error });
|
|
101
|
+
}
|
|
102
|
+
return entry;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const tracker = {
|
|
106
|
+
/**
|
|
107
|
+
* Track one live connection under `principal`. `close(reason)` must end
|
|
108
|
+
* the underlying transport synchronously (destroy the response or socket).
|
|
109
|
+
* Returns an entry whose `revoke(reason)` closes it exactly once and
|
|
110
|
+
* whose `end()` detaches it when the transport ended by itself — both are
|
|
111
|
+
* idempotent, so a revoke racing a natural close is safe.
|
|
112
|
+
*
|
|
113
|
+
* When the tracker is disposed or either resource bound is reached, the
|
|
114
|
+
* new connection is reject-closed synchronously and an inactive entry
|
|
115
|
+
* (`active === false`) is returned without touching unrelated entries.
|
|
116
|
+
*/
|
|
117
|
+
track({ principal, close }) {
|
|
118
|
+
if (typeof principal !== 'string' || principal.length === 0) {
|
|
119
|
+
throw new Error('principal is required');
|
|
120
|
+
}
|
|
121
|
+
if (typeof close !== 'function') {
|
|
122
|
+
throw new Error('close callback is required');
|
|
123
|
+
}
|
|
124
|
+
if (trackerDisposed) {
|
|
125
|
+
return rejectClose(principal, close, 'shutdown');
|
|
126
|
+
}
|
|
127
|
+
const perPrincipal = byPrincipal.get(principal)?.size ?? 0;
|
|
128
|
+
if (perPrincipal >= maxPerPrincipal) {
|
|
129
|
+
return rejectClose(principal, close, 'over-limit');
|
|
130
|
+
}
|
|
131
|
+
if (tracker.size >= maxTotal) {
|
|
132
|
+
return rejectClose(principal, close, 'over-limit');
|
|
133
|
+
}
|
|
134
|
+
const entry = {
|
|
135
|
+
id: ++entrySequence,
|
|
136
|
+
principal,
|
|
137
|
+
active: true,
|
|
138
|
+
revoke: (reason = 'revoked') => {
|
|
139
|
+
if (!entry.active) return false;
|
|
140
|
+
entry.active = false;
|
|
141
|
+
remove(entry);
|
|
142
|
+
try {
|
|
143
|
+
close(reason);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
// One failing close must not strand the remaining connections or
|
|
146
|
+
// the caller; the failure signal stays available via onCloseError.
|
|
147
|
+
onCloseError?.({ entry, error });
|
|
148
|
+
}
|
|
149
|
+
return true;
|
|
150
|
+
},
|
|
151
|
+
end: () => {
|
|
152
|
+
if (!entry.active) return;
|
|
153
|
+
entry.active = false;
|
|
154
|
+
remove(entry);
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
let set = byPrincipal.get(principal);
|
|
158
|
+
if (!set) {
|
|
159
|
+
set = new Set();
|
|
160
|
+
byPrincipal.set(principal, set);
|
|
161
|
+
}
|
|
162
|
+
set.add(entry);
|
|
163
|
+
return entry;
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
/** Close every tracked connection of one principal. Returns the count. */
|
|
167
|
+
closePrincipal: (principal, reason = 'revoked') => {
|
|
168
|
+
const set = byPrincipal.get(principal);
|
|
169
|
+
if (!set) return 0;
|
|
170
|
+
let closed = 0;
|
|
171
|
+
for (const entry of [...set]) {
|
|
172
|
+
if (entry.revoke(reason)) closed += 1;
|
|
173
|
+
}
|
|
174
|
+
return closed;
|
|
175
|
+
},
|
|
176
|
+
|
|
177
|
+
/** Close every tracked connection. Used by global invalidation and shutdown. */
|
|
178
|
+
closeAll: (reason = 'revoked') => {
|
|
179
|
+
let closed = 0;
|
|
180
|
+
for (const principal of [...byPrincipal.keys()]) {
|
|
181
|
+
closed += tracker.closePrincipal(principal, reason);
|
|
182
|
+
}
|
|
183
|
+
return closed;
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
countPrincipal: (principal) => byPrincipal.get(principal)?.size ?? 0,
|
|
187
|
+
principals: () => [...byPrincipal.keys()],
|
|
188
|
+
get size() {
|
|
189
|
+
let total = 0;
|
|
190
|
+
for (const set of byPrincipal.values()) total += set.size;
|
|
191
|
+
return total;
|
|
192
|
+
},
|
|
193
|
+
get maxPerPrincipal() {
|
|
194
|
+
return maxPerPrincipal;
|
|
195
|
+
},
|
|
196
|
+
get maxTotal() {
|
|
197
|
+
return maxTotal;
|
|
198
|
+
},
|
|
199
|
+
get disposed() {
|
|
200
|
+
return trackerDisposed;
|
|
201
|
+
},
|
|
202
|
+
/**
|
|
203
|
+
* Mark disposed and close remaining entries synchronously. Later `track`
|
|
204
|
+
* calls reject-close with `close('shutdown')`.
|
|
205
|
+
*/
|
|
206
|
+
dispose: (reason = 'shutdown') => {
|
|
207
|
+
trackerDisposed = true;
|
|
208
|
+
return tracker.closeAll(reason);
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
return tracker;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
export const createRevocationCoordinator = ({
|
|
215
|
+
listRevokedClientIds,
|
|
216
|
+
pollIntervalMs = DEFAULT_REVOCATION_POLL_INTERVAL_MS,
|
|
217
|
+
setIntervalFn = setInterval,
|
|
218
|
+
clearIntervalFn = clearInterval,
|
|
219
|
+
maxPerPrincipal = MAX_LIVE_CONNECTIONS_PER_PRINCIPAL,
|
|
220
|
+
maxTotal = MAX_LIVE_CONNECTIONS_GLOBAL,
|
|
221
|
+
maxRevoked = MAX_REMEMBERED_REVOKED_CLIENTS,
|
|
222
|
+
onCloseError,
|
|
223
|
+
} = {}) => {
|
|
224
|
+
if (typeof listRevokedClientIds !== 'function') {
|
|
225
|
+
throw new Error('listRevokedClientIds is required');
|
|
226
|
+
}
|
|
227
|
+
const tracker = createPrincipalTracker({ maxPerPrincipal, maxTotal, onCloseError });
|
|
228
|
+
// Authoritative in-process revocation memory: every clientRevoked() and
|
|
229
|
+
// every successful poll inserts here synchronously, so a track racing the
|
|
230
|
+
// verify await is rejected even though verification passed moments earlier.
|
|
231
|
+
// Bounded FIFO: beyond maxRevoked the oldest ID is evicted and that ID
|
|
232
|
+
// falls back to establishment-time store checks plus the next poll.
|
|
233
|
+
const revokedClientIds = new Set();
|
|
234
|
+
// Monotonic global-invalidation generation for browser-secret rotation.
|
|
235
|
+
// Auth captures getGeneration() before its first await; track rejects when
|
|
236
|
+
// the captured value is stale. Never reset; a Number is sufficient.
|
|
237
|
+
let globalGeneration = 0;
|
|
238
|
+
let pollTimer = null;
|
|
239
|
+
let disposed = false;
|
|
240
|
+
let pollInFlight = false;
|
|
241
|
+
let coordinatorSequence = 0;
|
|
242
|
+
|
|
243
|
+
const rememberRevoked = (clientId) => {
|
|
244
|
+
if (typeof clientId !== 'string' || clientId.length === 0) return;
|
|
245
|
+
if (revokedClientIds.has(clientId)) return;
|
|
246
|
+
if (revokedClientIds.size >= maxRevoked) {
|
|
247
|
+
const oldest = revokedClientIds.values().next().value;
|
|
248
|
+
revokedClientIds.delete(oldest);
|
|
249
|
+
}
|
|
250
|
+
revokedClientIds.add(clientId);
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const inactiveEntry = (principal) => ({
|
|
254
|
+
id: ++coordinatorSequence,
|
|
255
|
+
principal,
|
|
256
|
+
active: false,
|
|
257
|
+
revoke: () => false,
|
|
258
|
+
end: () => {},
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
const rejectCloseQuiet = (principal, close, reason) => {
|
|
262
|
+
const entry = inactiveEntry(principal);
|
|
263
|
+
try {
|
|
264
|
+
close(reason);
|
|
265
|
+
} catch (error) {
|
|
266
|
+
onCloseError?.({ entry, error });
|
|
267
|
+
}
|
|
268
|
+
return entry;
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const closeRevokedClient = (clientId) => {
|
|
272
|
+
if (typeof clientId !== 'string' || clientId.length === 0) return 0;
|
|
273
|
+
rememberRevoked(clientId);
|
|
274
|
+
if (disposed) return 0;
|
|
275
|
+
return tracker.closePrincipal(`client:${clientId}`, 'credential-revoked');
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const pollOnce = async () => {
|
|
279
|
+
if (pollInFlight || disposed) return;
|
|
280
|
+
pollInFlight = true;
|
|
281
|
+
try {
|
|
282
|
+
const revokedIds = await listRevokedClientIds();
|
|
283
|
+
if (Array.isArray(revokedIds)) {
|
|
284
|
+
for (const clientId of revokedIds) closeRevokedClient(clientId);
|
|
285
|
+
}
|
|
286
|
+
} catch {
|
|
287
|
+
// A transient store read failure must not look like an authoritative
|
|
288
|
+
// empty result; keep current state and let the next tick retry. While
|
|
289
|
+
// polls fail, cross-process revocations of ESTABLISHED connections are
|
|
290
|
+
// retained (degraded); new establishment stays authoritatively gated by
|
|
291
|
+
// direct store/secret reads.
|
|
292
|
+
} finally {
|
|
293
|
+
pollInFlight = false;
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
return {
|
|
298
|
+
/** Close every tracked connection of a client revoked in THIS process. */
|
|
299
|
+
clientRevoked: closeRevokedClient,
|
|
300
|
+
/**
|
|
301
|
+
* Close every tracked connection (global sign-out everywhere) and bump
|
|
302
|
+
* the generation so late registrations captured before this call are
|
|
303
|
+
* rejected at track time.
|
|
304
|
+
*/
|
|
305
|
+
revokeAllLive: (reason = 'global-invalidation') => {
|
|
306
|
+
globalGeneration += 1;
|
|
307
|
+
if (disposed) return 0;
|
|
308
|
+
return tracker.closeAll(reason);
|
|
309
|
+
},
|
|
310
|
+
/**
|
|
311
|
+
* Register a verified live connection. `generation` must be the value of
|
|
312
|
+
* `getGeneration()` captured BEFORE the caller's first auth await;
|
|
313
|
+
* a stale generation reject-closes without trusting the caller's
|
|
314
|
+
* principal. A `client:<id>` whose id is in revoked memory reject-closes
|
|
315
|
+
* the same way. Resource bounds reject-close without evicting unrelated
|
|
316
|
+
* entries. After `dispose()` every registration reject-closes.
|
|
317
|
+
* Returns an inactive entry (`active === false`) on every rejection;
|
|
318
|
+
* WebSocket upgrades must not call `handleUpgrade` for those.
|
|
319
|
+
*/
|
|
320
|
+
trackLiveConnection: ({ principal, close, generation } = {}) => {
|
|
321
|
+
if (typeof principal !== 'string' || principal.length === 0) {
|
|
322
|
+
throw new Error('principal is required');
|
|
323
|
+
}
|
|
324
|
+
if (typeof close !== 'function') {
|
|
325
|
+
throw new Error('close callback is required');
|
|
326
|
+
}
|
|
327
|
+
if (disposed) {
|
|
328
|
+
return rejectCloseQuiet(principal, close, 'shutdown');
|
|
329
|
+
}
|
|
330
|
+
if (generation !== undefined && generation !== globalGeneration) {
|
|
331
|
+
return rejectCloseQuiet(principal, close, 'global-invalidation');
|
|
332
|
+
}
|
|
333
|
+
if (principal.startsWith('client:')) {
|
|
334
|
+
const clientId = principal.slice('client:'.length);
|
|
335
|
+
if (clientId.length > 0 && revokedClientIds.has(clientId)) {
|
|
336
|
+
return rejectCloseQuiet(principal, close, 'credential-revoked');
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return tracker.track({ principal, close });
|
|
340
|
+
},
|
|
341
|
+
countPrincipal: (principal) => tracker.countPrincipal(principal),
|
|
342
|
+
principals: () => tracker.principals(),
|
|
343
|
+
isClientRevoked: (clientId) =>
|
|
344
|
+
typeof clientId === 'string' && clientId.length > 0 && revokedClientIds.has(clientId),
|
|
345
|
+
getRevokedCount: () => revokedClientIds.size,
|
|
346
|
+
getGeneration: () => globalGeneration,
|
|
347
|
+
get maxPerPrincipal() {
|
|
348
|
+
return tracker.maxPerPrincipal;
|
|
349
|
+
},
|
|
350
|
+
get maxTotal() {
|
|
351
|
+
return tracker.maxTotal;
|
|
352
|
+
},
|
|
353
|
+
get maxRevoked() {
|
|
354
|
+
return maxRevoked;
|
|
355
|
+
},
|
|
356
|
+
get liveConnectionCount() {
|
|
357
|
+
return tracker.size;
|
|
358
|
+
},
|
|
359
|
+
/** Begin bounded cross-process convergence polling. Idempotent. */
|
|
360
|
+
start: () => {
|
|
361
|
+
if (disposed || pollTimer) return;
|
|
362
|
+
pollTimer = setIntervalFn(() => { void pollOnce(); }, pollIntervalMs);
|
|
363
|
+
if (typeof pollTimer?.unref === 'function') pollTimer.unref();
|
|
364
|
+
},
|
|
365
|
+
/**
|
|
366
|
+
* Stop polling and, by default, close remaining tracked connections
|
|
367
|
+
* synchronously (bounded shutdown cleanup). Later registrations are
|
|
368
|
+
* reject-closed.
|
|
369
|
+
*/
|
|
370
|
+
dispose: ({ closeConnections = true } = {}) => {
|
|
371
|
+
disposed = true;
|
|
372
|
+
let closed = 0;
|
|
373
|
+
if (pollTimer) {
|
|
374
|
+
clearIntervalFn(pollTimer);
|
|
375
|
+
pollTimer = null;
|
|
376
|
+
}
|
|
377
|
+
if (closeConnections) {
|
|
378
|
+
closed = tracker.closeAll('shutdown');
|
|
379
|
+
}
|
|
380
|
+
return closed;
|
|
381
|
+
},
|
|
382
|
+
};
|
|
383
|
+
};
|