rough rate limiting for websockets

This commit is contained in:
dakkar 2024-08-15 11:35:51 +01:00
parent 01958da57f
commit 311a31da58
2 changed files with 55 additions and 0 deletions

View file

@ -25,6 +25,7 @@ import type Channel from './channel.js';
export default class Connection {
public user?: MiUser;
public token?: MiAccessToken;
private rateLimiter?: () => Promise<boolean>;
private wsConnection: WebSocket.WebSocket;
public subscriber: StreamEventEmitter;
private channels: Channel[] = [];
@ -48,9 +49,11 @@ export default class Connection {
user: MiUser | null | undefined,
token: MiAccessToken | null | undefined,
rateLimiter: () => Promise<boolean>,
) {
if (user) this.user = user;
if (token) this.token = token;
if (rateLimiter) this.rateLimiter = rateLimiter;
}
@bindThis
@ -103,6 +106,10 @@ export default class Connection {
private async onWsConnectionMessage(data: WebSocket.RawData) {
let obj: Record<string, any>;
if (this.rateLimiter && await this.rateLimiter()) {
return;
}
try {
obj = JSON.parse(data.toString());
} catch (e) {