Skip to content

Commit 3fa20c5

Browse files
committed
Use async emitter for sockets stuff
1 parent dd683c0 commit 3fa20c5

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

src/SocketServer.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,12 @@ class SocketServer {
512512
/**
513513
* Get a LostConnection for a user, if one exists.
514514
*
515-
* @param {User} user
515+
* @param {string} sessionID
516516
* @private
517517
*/
518-
getLostConnection(user) {
518+
getLostConnection(sessionID) {
519519
return this.#connections.find((connection) => (
520-
connection instanceof LostConnection && connection.user.id === user.id
520+
connection instanceof LostConnection && connection.sessionID === sessionID
521521
));
522522
}
523523

@@ -534,7 +534,7 @@ class SocketServer {
534534
connection.on('close', () => {
535535
this.remove(connection);
536536
});
537-
connection.on('authenticate', async (user, sessionID, lastEventID) => {
537+
connection.on('authenticate', async ({ user, sessionID, lastEventID }) => {
538538
const isReconnect = await connection.isReconnect(sessionID);
539539
this.#logger.info({ userId: user.id, isReconnect, lastEventID }, 'authenticated socket');
540540
if (isReconnect) {
@@ -575,11 +575,7 @@ class SocketServer {
575575
});
576576
connection.on(
577577
'command',
578-
/**
579-
* @param {string} command
580-
* @param {import('type-fest').JsonValue} data
581-
*/
582-
(command, data) => {
578+
({ command, data }) => {
583579
this.#logger.trace({ userId: user.id, command, data }, 'command');
584580
if (has(this.#clientActions, command)) {
585581
// Ignore incorrect input

src/sockets/AuthedConnection.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import EventEmitter from 'node:events';
1+
import Emittery from 'emittery';
22
import Ultron from 'ultron';
33
import WebSocket from 'ws';
44
import sjson from 'secure-json-parse';
@@ -8,7 +8,13 @@ import { fromJson, json } from '../utils/sqlite.js';
88
const PING_TIMEOUT = 5_000;
99
const DEAD_TIMEOUT = 30_000;
1010

11-
class AuthedConnection extends EventEmitter {
11+
/**
12+
* @augments {Emittery<{
13+
* command: { command: string, data: import('type-fest').JsonValue },
14+
* close: { banned: boolean, lastEventID: string | null },
15+
* }>}
16+
*/
17+
class AuthedConnection extends Emittery {
1218
#events;
1319

1420
#logger;
@@ -20,6 +26,8 @@ class AuthedConnection extends EventEmitter {
2026
/** @type {string|null} */
2127
#lastEventID = null;
2228

29+
banned = false;
30+
2331
/**
2432
* @param {import('../Uwave.js').default} uw
2533
* @param {import('ws').WebSocket} socket
@@ -97,7 +105,7 @@ class AuthedConnection extends EventEmitter {
97105
this.#lastMessage = Date.now();
98106
const { command, data } = sjson.safeParse(raw) ?? {};
99107
if (command) {
100-
this.emit('command', command, data);
108+
this.emit('command', { command, data });
101109
}
102110
}
103111

src/sockets/GuestConnection.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import EventEmitter from 'node:events';
1+
import Emittery from 'emittery';
22
import { ulid } from 'ulid';
33
import Ultron from 'ultron';
44
import WebSocket from 'ws';
55

66
const PING_TIMEOUT = 5_000;
77
const DEAD_TIMEOUT = 30_000;
88

9-
class GuestConnection extends EventEmitter {
9+
/**
10+
* @augments {Emittery<{
11+
* close: undefined,
12+
* authenticate: { user: import('../schema.js').User, sessionID: string, lastEventID: string | null }
13+
* }>}
14+
*/
15+
class GuestConnection extends Emittery {
1016
#events;
1117

1218
#logger;
@@ -69,7 +75,7 @@ class GuestConnection extends EventEmitter {
6975
throw new Error('You have been banned');
7076
}
7177

72-
this.emit('authenticate', userModel, sessionID, null);
78+
await this.emit('authenticate', { user: userModel, sessionID, lastEventID: null });
7379
}
7480

7581
/**

src/sockets/LostConnection.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import EventEmitter from 'node:events';
1+
import Emittery from 'emittery';
22

3-
class LostConnection extends EventEmitter {
3+
/**
4+
* @augments {Emittery<{ close: undefined }>}
5+
*/
6+
class LostConnection extends Emittery {
47
#logger;
58

69
#expiresAt;

0 commit comments

Comments
 (0)