Source
PR #191 review threads:
Problem
The open and message handlers in ws.get.ts duplicate authentication logic:
- Create h3 headers object
- Call
getUserIdACL with notifications:listen
- Call
fetchAllACLs
- Call
notificationSystem.listen
- Send
unauthenticated on failure
Fix
Extract a shared authenticatePeer helper:
async function authenticatePeer(
peer: Parameters<typeof defineWebSocketHandler>[0]['open'] extends (p: infer P) => any ? P : never,
headers: Headers,
): Promise<boolean> {
const h3 = { headers };
const userId = await aclManager.getUserIdACL(h3, ["notifications:listen"]);
if (!userId) return false;
const acls = await aclManager.fetchAllACLs(h3);
if (!acls) return false;
socketSessions.set(peer.id, userId);
notificationSystem.listen(userId, acls, peer.id, (notification) => {
peer.send(JSON.stringify(notification));
});
return true;
}
Both open and message handlers call authenticatePeer instead of duplicating.
Relates: #191
Source
PR #191 review threads:
Problem
The
openandmessagehandlers inws.get.tsduplicate authentication logic:getUserIdACLwithnotifications:listenfetchAllACLsnotificationSystem.listenunauthenticatedon failureFix
Extract a shared
authenticatePeerhelper:Both
openandmessagehandlers callauthenticatePeerinstead of duplicating.Relates: #191