diff --git a/auxilary.ts b/subscriptionMap.ts similarity index 100% rename from auxilary.ts rename to subscriptionMap.ts diff --git a/utility.ts b/utility.ts new file mode 100644 index 0000000..ffaef1c --- /dev/null +++ b/utility.ts @@ -0,0 +1,4 @@ +export function guardStringNotEmpty(data: any, cb: (data: string) => void): void { + if (typeof data != "string" || data != "") return; + cb(data); +} diff --git a/websocket-handler.ts b/websocket-handler.ts index 5408cb7..61037ad 100644 --- a/websocket-handler.ts +++ b/websocket-handler.ts @@ -1,7 +1,8 @@ // TYPES -import { SubscriptionMap } from "./auxilary"; +import { SubscriptionMap } from "./subscriptionMap"; import { WS } from "./index"; +import { guardStringNotEmpty } from "./utility"; // requests export interface IncomingMessage { @@ -38,16 +39,16 @@ export function forgetConnection(ws: WS): void { export function processMessage(ws: WS, messageString: string): void { const messageObject: IncomingMessage = parseMessage(messageString); - if (typeof messageObject.subscribeChannel == "string") { - trackSubscription(ws, messageObject.subscribeChannel); - } else if (typeof messageObject.unsubscribeChannel == "string") { - removeSubscription(ws, messageObject.unsubscribeChannel); - } else if ( - typeof messageObject.messageChannel == "string" && - typeof messageObject.messageBody == "string" - ) { - sendMessage(messageObject.messageChannel, messageObject.messageBody); - } + guardStringNotEmpty(messageObject.subscribeChannel, (subscribeChannel) => { + trackSubscription(ws, subscribeChannel); + }); + guardStringNotEmpty(messageObject.unsubscribeChannel, (unsubscribeChannel) => { + removeSubscription(ws, unsubscribeChannel); + }); + guardStringNotEmpty(messageObject.messageChannel == "string", (messageChannel) => { + if (!messageObject.messageBody) return; + sendMessage(messageChannel, messageObject.messageBody); + }); } // MESSAGING @@ -73,5 +74,6 @@ function confirmSubscription(ws: WS, channel: string): void { subscribed: subscriptions.getChannelList(ws)?.has(channel), }; const messageString = stringifyMessage(messageObject); + console.log(messageString); ws.send(messageString); }