From a9b1aec5e2f40366bbc8edf360cd2f9582203934 Mon Sep 17 00:00:00 2001 From: bugwheels94 Date: Mon, 6 May 2024 08:53:18 +0000 Subject: [PATCH] fix: nanoid based id --- package.json | 1 + src/client.ts | 14 ++++++++------ src/index.ts | 17 +++++++++-------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 077211a..db43e6d 100644 --- a/package.json +++ b/package.json @@ -90,6 +90,7 @@ "husky": "^7.0.4", "jest": "^29.6.4", "lint-staged": "^12.3.7", + "nanoid": "^5.0.7", "npm-run-all": "^4.1.5", "prettier": "2.6.0", "replace": "^1.2.1", diff --git a/src/client.ts b/src/client.ts index 4d58fc8..da26a66 100644 --- a/src/client.ts +++ b/src/client.ts @@ -6,7 +6,7 @@ import EventEmitter from 'events'; import { AllowedType, DataMapping, JsonObject, Serialize } from './utils'; import { SoxtendServer } from '.'; import { GROUPS_BY_CONNECTION_ID, SERVERS_HAVING_GROUP } from './constants'; - +import { nanoid } from 'nanoid'; export type ClientResponse = { _id: number; status: HttpStatusCode; @@ -33,7 +33,7 @@ export type ClientPromiseStore = Record< >; export class Socket extends EventEmitter { - public readonly id: string = crypto.randomUUID(); + public readonly id: string; private serialize: Serialize>; storage: Record = {}; mode?: string | Uint8Array; @@ -49,12 +49,12 @@ export class Socket extends Eve public async initialize() { const id = this.id; this.server.individualSocketConnectionStore.add(this); - return this.server.distributor.set(`i:${id}`, this.server.serverId); + return this.server.distributor.set(`i:${id}`, this.server.id); } public async clear() { const id = this.id; this.server.individualSocketConnectionStore.remove(id); - return this.server.distributor.set(`i:${id}`, this.server.serverId); + return this.server.distributor.set(`i:${id}`, this.server.id); } constructor( socket: WebSocket, @@ -74,6 +74,8 @@ export class Socket extends Eve this.mode = mode; this.rawSocket = socket; this.server = server; + this.id = this.server.id + nanoid(); + // this.store = store; this.initialize(); } @@ -83,14 +85,14 @@ export class Socket extends Eve this.server.socketGroupStore.add(this, groupId); return Promise.all([ this.server.distributor.addListItem(`${GROUPS_BY_CONNECTION_ID}${this.id}`, groupId), - this.server.distributor.addListItem(`${SERVERS_HAVING_GROUP}${groupId}`, this.server.serverId), + this.server.distributor.addListItem(`${SERVERS_HAVING_GROUP}${groupId}`, this.server.id), ]); } async joinGroups(groupdIds: Iterable) { for (let groupId of groupdIds) { this.server.socketGroupStore.add(this, groupId); - this.server.distributor.addListItem(`${SERVERS_HAVING_GROUP}${groupId}`, this.server.serverId); + this.server.distributor.addListItem(`${SERVERS_HAVING_GROUP}${groupId}`, this.server.id); } this.server.distributor.addListItems(`${GROUPS_BY_CONNECTION_ID}${this.id}`, groupdIds); } diff --git a/src/index.ts b/src/index.ts index 848752e..19b5f07 100644 --- a/src/index.ts +++ b/src/index.ts @@ -75,7 +75,7 @@ const encoder = new TextEncoder(); const decoder = new TextDecoder(); export class SoxtendServer extends EventEmitter { - serverId: string; + id: string; rawWebSocketServer: WebSocket.Server; distributor: MessageDistributor; eventStore: Record< @@ -100,14 +100,14 @@ export class SoxtendServer extends E } if (!this.distributor) return; - const server = await this.distributor.get(`i:${id}`); + const serverId = id.slice(0, 21); const groupArray = encoder.encode(id); const messageWithGroupId = new Uint8Array(serializedMessage.length + groupArray.length + 1); messageWithGroupId[0] = groupArray.length; messageWithGroupId.set(groupArray, 1); messageWithGroupId.set(serializedMessage, 1 + groupArray.length); // @ts-ignore - this.distributor.enqueue(`${server}`, messageWithGroupId); + this.distributor.enqueue(`${serverId}`, messageWithGroupId); } private async sendMessageAsBufferToGroup(id: string, message: JsonObject) { // this.socketGroupStore.find(id)?.forEach((socket) => { @@ -132,11 +132,12 @@ export class SoxtendServer extends E } if (!this.distributor) return; - const server = await this.distributor.get(`i:${id}`); + const serverId = id.slice(0, 21); + const messageWithGroupId = id + ':' + serializedMessage; //@ts-ignore - this.distributor.enqueue(`${server}`, messageWithGroupId); + this.distributor.enqueue(`${serverId}`, messageWithGroupId); } private async sendMessageAsStringToGroup(id: string, message: JsonObject) { const serializedMessage = this.serialize(message) as string; @@ -230,21 +231,21 @@ export class SoxtendServer extends E this.sendToGroup = this.sendMessageAsStringToGroup; } this.distributor = distributor; - this.serverId = crypto.randomUUID(); + this.id = crypto.randomUUID(); this.individualSocketConnectionStore = new IndividualSocketConnectionStore(); this.socketGroupStore = new SocketGroupStore(); this.rawWebSocketServer = new WebSocket.Server(options); Promise.all([ this.distributor - ? this.distributor.initialize(this.serverId, { + ? this.distributor.initialize(this.id, { messageType, }) : undefined, // options.messageStore ? options.messageStore.initialize(this.serverId) : undefined, ]) .then(() => { - this.listenToIndividualQueue(`${this.serverId}`); + this.listenToIndividualQueue(`${this.id}`); this.listenToGroupQueue(`broadcast`); this.emit('ready'); this.rawWebSocketServer.on('connection', (rawSocket: WebSocket) => {