From e42d09766a6da6934cee03d5c77c8568d7838a91 Mon Sep 17 00:00:00 2001 From: Kiyo5hi <44930252+Kiyo5hi@users.noreply.github.com> Date: Wed, 27 Mar 2024 13:51:52 -0700 Subject: [PATCH] feat(user): added reservedIds --- errors.ts | 15 +-------------- index.ts | 36 +++++++++++++++++++++++++++++++----- types.ts | 7 +++++++ 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/errors.ts b/errors.ts index f394350..ac5c0d4 100644 --- a/errors.ts +++ b/errors.ts @@ -1,7 +1,7 @@ import { Type } from "@sinclair/typebox"; import { eventNames } from "./handlers"; -import { ResponseBase, type Connection, type UserId } from "./types"; +import { ResponseBase, type UserId } from "./types"; export const ServerErrorSchema = ResponseBase( "error", @@ -39,19 +39,6 @@ export class EventNotFoundError extends ServerError { } } -export class UserConflictError extends ServerError { - conn: Connection; - - constructor(userId: UserId, conn: Connection) { - super(`user already connected: ${userId}`); - this.name = "UserConflictError"; - this.conn = conn; - this.details = { - userId, - }; - } -} - export class ConnectionClosedError extends ServerError { constructor(userId: UserId) { super(`connection closed for user: ${userId}`); diff --git a/index.ts b/index.ts index df5e5a1..a553842 100644 --- a/index.ts +++ b/index.ts @@ -10,7 +10,13 @@ import { ServerErrorSchema, UserIdMismatchError, } from "./errors"; -import type { Connection, RequestBase, ResponseBase, UserId } from "./types"; +import { + ReservedUserId, + type Connection, + type RequestBase, + type ResponseBase, + type UserId, +} from "./types"; import { loggerMiddleware } from "./setup"; import { getLogger } from "./loggers"; @@ -34,6 +40,8 @@ async function processResponse( request: RequestBase>, responsePromise: MaybePromise>>, ) { + // TODO: handle ReservedUserId + try { const response = await responsePromise; const to = response.to; @@ -77,10 +85,27 @@ const app = new Elysia().use(loggerMiddleware).ws("/:userId", { userId: Type.String(), }), open: async (ws) => { + if (ws.data.params.userId in ReservedUserId) { + ws.send({ + requestId: "0", + to: ReservedUserId.UNKNOWN, + event: "error", + data: { + name: "ReservedUserIdError", + message: `userId is reserved: ${ws.data.params.userId}`, + details: { + userId: ws.data.params.userId, + }, + }, + }); + ws.close(); + return; + } + if (connectionMap.has(ws.data.params.userId)) { ws.send({ requestId: "0", - to: ws.data.params.userId, + to: ReservedUserId.UNKNOWN, event: "error", data: { name: "UserConflictError", @@ -91,10 +116,11 @@ const app = new Elysia().use(loggerMiddleware).ws("/:userId", { }, }); ws.close(); - } else { - connectionLogger.info(`connection opened: ${ws.data.params.userId}`); - connectionMap.set(ws.data.params.userId, ws as Connection); + return; } + + connectionLogger.info(`connection opened: ${ws.data.params.userId}`); + connectionMap.set(ws.data.params.userId, ws as Connection); }, close: async (ws) => { connectionLogger.info(`connection closed: ${ws.data.params.userId}`); diff --git a/types.ts b/types.ts index a776825..518fc65 100644 --- a/types.ts +++ b/types.ts @@ -76,3 +76,10 @@ export type HandlerEntry< // biome-ignore lint/suspicious/noExplicitAny: Elysia has complex types export type Connection = ElysiaWS; + +export enum ReservedUserId { + ALL = "all", + GROUP = "group", + SYSTEM = "system", + UNKNOWN = "unknown", +}