@jusbid/redis 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -9,7 +9,8 @@ const {
9
9
  } = require('./socketRedisClient');
10
10
 
11
11
  const { initSocketManager, getIO, emitEvent } = require("./socket-manager");
12
-
12
+ const { SOCKET_EVENTS } = require('./redis-client');
13
+ const { Server } = require("socket.io");
13
14
 
14
15
  const redisOptions = {};
15
16
 
@@ -886,6 +887,56 @@ const deleteByPattern = async (pattern) => {
886
887
  return keys.length;
887
888
  }
888
889
 
890
+
891
+ /**
892
+ * Create Socket.IO server
893
+ */
894
+ const createSocketServer = ({
895
+ httpServer,
896
+ cors = {
897
+ origin: process.env.CORS_ORIGIN,
898
+ methods: ["GET", "POST"],
899
+ },
900
+ path = "/socket.io",
901
+ verifyToken, // (token, socket) => Promise<payload> | payload
902
+ } = {}) => {
903
+ if (!httpServer) {
904
+ throw new Error("httpServer is required to create socket server");
905
+ }
906
+
907
+ const socketServer = new Server(httpServer, {
908
+ cors,
909
+ path,
910
+ });
911
+
912
+ // Token handshake middleware
913
+ socketServer.use(async (socket, next) => {
914
+ try {
915
+ const token =
916
+ socket.handshake.auth?.token ||
917
+ socket.handshake.headers?.authorization?.replace("Bearer ", "");
918
+
919
+ if (!token) {
920
+ return next(new Error("AUTH_TOKEN_MISSING"));
921
+ }
922
+
923
+ if (typeof verifyToken !== "function") {
924
+ throw new Error("verifyToken function is required to authenticate sockets");
925
+ }
926
+
927
+ const decoded = await verifyToken(token, socket);
928
+ socket.data.user = decoded; // attach identity for later use
929
+
930
+ next();
931
+ } catch (err) {
932
+ next(new Error("AUTH_TOKEN_INVALID"));
933
+ }
934
+ });
935
+
936
+ return socketServer;
937
+ };
938
+
939
+
889
940
  module.exports = {
890
941
  redisClient,
891
942
  get,
@@ -917,6 +968,7 @@ module.exports = {
917
968
  publish,
918
969
  subscribe,
919
970
 
920
- initSocketManager, getIO, emitEvent
921
-
971
+ initSocketManager, getIO, emitEvent,
972
+ createSocketServer,
973
+ SOCKET_EVENTS
922
974
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jusbid/redis",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "main": "index.js",
5
5
  "private": false,
6
6
  "dependencies": {
package/redis-client.js CHANGED
@@ -13,4 +13,32 @@ const createRedisClients = async ({ url = process.env.REDIS_URL } = {}) => {
13
13
  return { pubClient, subClient };
14
14
  };
15
15
 
16
- module.exports = { createRedisClients };
16
+ /**
17
+ * Socket events
18
+ */
19
+ const SOCKET_EVENTS = Object.freeze({
20
+ CONNECT: "connect",
21
+ DISCONNECT: "disconnect",
22
+ ERROR: "error",
23
+
24
+ BOOKING_CREATED: "booking.created",
25
+ BOOKING_UPDATED: "booking.updated",
26
+ BOOKING_CANCELLED: "booking.cancelled",
27
+
28
+ PROPERTY_CREATED: "property.created",
29
+ PROPERTY_UPDATED: "property.updated",
30
+
31
+ ROOM_CREATED: "room.created",
32
+ ROOM_UPDATED: "room.updated",
33
+
34
+ INVENTORY_UPDATED: "inventory.updated",
35
+
36
+ PAYMENT_SUCCESS: "payment.success",
37
+ PAYMENT_FAILED: "payment.failed",
38
+
39
+ NOTIFICATION: "notification",
40
+ });
41
+
42
+
43
+
44
+ module.exports = { createRedisClients, SOCKET_EVENTS };