Skip to content

Commit

Permalink
feat(user): added reservedIds
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiyo5hi committed Mar 27, 2024
1 parent 8134c1c commit e42d097
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
15 changes: 1 addition & 14 deletions errors.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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}`);
Expand Down
36 changes: 31 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -34,6 +40,8 @@ async function processResponse(
request: RequestBase<string, Record<string, unknown>>,
responsePromise: MaybePromise<ResponseBase<string, Record<string, unknown>>>,
) {
// TODO: handle ReservedUserId

try {
const response = await responsePromise;
const to = response.to;
Expand Down Expand Up @@ -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",
Expand All @@ -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}`);
Expand Down
7 changes: 7 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,10 @@ export type HandlerEntry<

// biome-ignore lint/suspicious/noExplicitAny: Elysia has complex types
export type Connection = ElysiaWS<any>;

export enum ReservedUserId {
ALL = "all",
GROUP = "group",
SYSTEM = "system",
UNKNOWN = "unknown",
}

0 comments on commit e42d097

Please sign in to comment.