@eleven-am/pondsocket 0.1.200 → 0.1.202

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.
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
+ if (kind === "m") throw new TypeError("Private method is not writable");
13
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
14
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
15
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
16
+ };
17
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
18
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
19
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
20
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
+ };
22
+ var _RedisDistributedBackend_instances, _RedisDistributedBackend_publishClient, _RedisDistributedBackend_subscribeClient, _RedisDistributedBackend_keyPrefix, _RedisDistributedBackend_subject, _RedisDistributedBackend_isConnected, _RedisDistributedBackend_initialize, _RedisDistributedBackend_handleRedisMessage, _RedisDistributedBackend_isValidMessage, _RedisDistributedBackend_buildKey;
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.RedisDistributedBackend = void 0;
25
+ const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
26
+ const redis_1 = require("redis");
27
+ const types_1 = require("../types");
28
+ class RedisDistributedBackend {
29
+ constructor(options = {}) {
30
+ _RedisDistributedBackend_instances.add(this);
31
+ _RedisDistributedBackend_publishClient.set(this, void 0);
32
+ _RedisDistributedBackend_subscribeClient.set(this, void 0);
33
+ _RedisDistributedBackend_keyPrefix.set(this, void 0);
34
+ _RedisDistributedBackend_subject.set(this, void 0);
35
+ _RedisDistributedBackend_isConnected.set(this, false);
36
+ const { host = 'localhost', port = 6379, password, database = 0, url, keyPrefix = 'pondsocket', } = options;
37
+ __classPrivateFieldSet(this, _RedisDistributedBackend_keyPrefix, keyPrefix, "f");
38
+ __classPrivateFieldSet(this, _RedisDistributedBackend_subject, new pondsocket_common_1.Subject(), "f");
39
+ // Create Redis clients
40
+ const clientConfig = url
41
+ ? { url }
42
+ : { socket: { host,
43
+ port },
44
+ password,
45
+ database };
46
+ __classPrivateFieldSet(this, _RedisDistributedBackend_publishClient, (0, redis_1.createClient)(clientConfig), "f");
47
+ __classPrivateFieldSet(this, _RedisDistributedBackend_subscribeClient, __classPrivateFieldGet(this, _RedisDistributedBackend_publishClient, "f").duplicate(), "f");
48
+ void __classPrivateFieldGet(this, _RedisDistributedBackend_instances, "m", _RedisDistributedBackend_initialize).call(this);
49
+ }
50
+ /**
51
+ * Gets the subject for subscribing to distributed messages
52
+ */
53
+ get subject() {
54
+ return __classPrivateFieldGet(this, _RedisDistributedBackend_subject, "f");
55
+ }
56
+ /**
57
+ * Subscribe to messages for a specific endpoint and channel
58
+ */
59
+ subscribe(endpointName, channelName, handler) {
60
+ return this.subject.subscribe((message) => {
61
+ if (message.endpointName === endpointName && message.channelName === channelName) {
62
+ handler(message);
63
+ }
64
+ });
65
+ }
66
+ /**
67
+ * Broadcasts a message to all nodes for a specific endpoint and channel
68
+ */
69
+ broadcast(endpointName, channelName, message) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ if (!__classPrivateFieldGet(this, _RedisDistributedBackend_isConnected, "f")) {
72
+ throw new Error('Redis backend is not connected');
73
+ }
74
+ const key = __classPrivateFieldGet(this, _RedisDistributedBackend_instances, "m", _RedisDistributedBackend_buildKey).call(this, endpointName, channelName);
75
+ const serializedMessage = JSON.stringify(message);
76
+ yield __classPrivateFieldGet(this, _RedisDistributedBackend_publishClient, "f").publish(key, serializedMessage);
77
+ });
78
+ }
79
+ /**
80
+ * Cleanup and close all Redis connections
81
+ */
82
+ cleanup() {
83
+ return __awaiter(this, void 0, void 0, function* () {
84
+ this.subject.close();
85
+ if (__classPrivateFieldGet(this, _RedisDistributedBackend_subscribeClient, "f").isOpen) {
86
+ yield __classPrivateFieldGet(this, _RedisDistributedBackend_subscribeClient, "f").quit();
87
+ }
88
+ if (__classPrivateFieldGet(this, _RedisDistributedBackend_publishClient, "f").isOpen) {
89
+ yield __classPrivateFieldGet(this, _RedisDistributedBackend_publishClient, "f").quit();
90
+ }
91
+ __classPrivateFieldSet(this, _RedisDistributedBackend_isConnected, false, "f");
92
+ });
93
+ }
94
+ }
95
+ exports.RedisDistributedBackend = RedisDistributedBackend;
96
+ _RedisDistributedBackend_publishClient = new WeakMap(), _RedisDistributedBackend_subscribeClient = new WeakMap(), _RedisDistributedBackend_keyPrefix = new WeakMap(), _RedisDistributedBackend_subject = new WeakMap(), _RedisDistributedBackend_isConnected = new WeakMap(), _RedisDistributedBackend_instances = new WeakSet(), _RedisDistributedBackend_initialize = function _RedisDistributedBackend_initialize() {
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ yield Promise.all([
99
+ __classPrivateFieldGet(this, _RedisDistributedBackend_publishClient, "f").connect(),
100
+ __classPrivateFieldGet(this, _RedisDistributedBackend_subscribeClient, "f").connect(),
101
+ ]);
102
+ const pattern = `${__classPrivateFieldGet(this, _RedisDistributedBackend_keyPrefix, "f")}:*`;
103
+ yield __classPrivateFieldGet(this, _RedisDistributedBackend_subscribeClient, "f").pSubscribe(pattern, (message) => {
104
+ __classPrivateFieldGet(this, _RedisDistributedBackend_instances, "m", _RedisDistributedBackend_handleRedisMessage).call(this, message);
105
+ });
106
+ __classPrivateFieldSet(this, _RedisDistributedBackend_isConnected, true, "f");
107
+ });
108
+ }, _RedisDistributedBackend_handleRedisMessage = function _RedisDistributedBackend_handleRedisMessage(message) {
109
+ try {
110
+ const parsedMessage = JSON.parse(message);
111
+ if (__classPrivateFieldGet(this, _RedisDistributedBackend_instances, "m", _RedisDistributedBackend_isValidMessage).call(this, parsedMessage)) {
112
+ this.subject.publish(parsedMessage);
113
+ }
114
+ }
115
+ catch (_a) {
116
+ // Silently ignore invalid messages
117
+ }
118
+ }, _RedisDistributedBackend_isValidMessage = function _RedisDistributedBackend_isValidMessage(message) {
119
+ return (typeof message === 'object' &&
120
+ typeof message.type === 'string' &&
121
+ Object.values(types_1.DistributedMessageType).includes(message.type) &&
122
+ typeof message.endpointName === 'string' &&
123
+ typeof message.channelName === 'string');
124
+ }, _RedisDistributedBackend_buildKey = function _RedisDistributedBackend_buildKey(endpointName, channelName) {
125
+ return `${__classPrivateFieldGet(this, _RedisDistributedBackend_keyPrefix, "f")}:${endpointName}:${channelName}`;
126
+ };
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
12
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
13
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
@@ -64,6 +73,26 @@ class Middleware {
64
73
  };
65
74
  next();
66
75
  }
76
+ runAsync(req, res, final) {
77
+ let index = 0;
78
+ const next = (err) => __awaiter(this, void 0, void 0, function* () {
79
+ if (err) {
80
+ return final(__classPrivateFieldGet(this, _Middleware_instances, "m", _Middleware_handleError).call(this, err));
81
+ }
82
+ if (index >= __classPrivateFieldGet(this, _Middleware_stack, "f").length) {
83
+ return final();
84
+ }
85
+ const middleware = __classPrivateFieldGet(this, _Middleware_stack, "f")[index];
86
+ index++;
87
+ try {
88
+ yield middleware(req, res, next);
89
+ }
90
+ catch (error) {
91
+ return final(__classPrivateFieldGet(this, _Middleware_instances, "m", _Middleware_handleError).call(this, error));
92
+ }
93
+ });
94
+ return next();
95
+ }
67
96
  }
68
97
  exports.Middleware = Middleware;
69
98
  _Middleware_stack = new WeakMap(), _Middleware_instances = new WeakSet(), _Middleware_handleError = function _Middleware_handleError(error) {
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DistributedMessageType = void 0;
4
+ var DistributedMessageType;
5
+ (function (DistributedMessageType) {
6
+ DistributedMessageType["STATE_REQUEST"] = "STATE_REQUEST";
7
+ DistributedMessageType["STATE_RESPONSE"] = "STATE_RESPONSE";
8
+ DistributedMessageType["USER_JOINED"] = "USER_JOINED";
9
+ DistributedMessageType["USER_LEFT"] = "USER_LEFT";
10
+ DistributedMessageType["USER_MESSAGE"] = "USER_MESSAGE";
11
+ DistributedMessageType["PRESENCE_UPDATE"] = "PRESENCE_UPDATE";
12
+ DistributedMessageType["PRESENCE_REMOVED"] = "PRESENCE_REMOVED";
13
+ DistributedMessageType["ASSIGNS_UPDATE"] = "ASSIGNS_UPDATE";
14
+ DistributedMessageType["ASSIGNS_REMOVED"] = "ASSIGNS_REMOVED";
15
+ DistributedMessageType["EVICT_USER"] = "EVICT_USER";
16
+ })(DistributedMessageType || (exports.DistributedMessageType = DistributedMessageType = {}));
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _BaseContext_engine, _BaseContext_params, _BaseContext_event, _BaseContext_sender, _BaseContext_payload;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.BaseContext = void 0;
16
+ const channel_1 = require("../wrappers/channel");
17
+ class BaseContext {
18
+ constructor(engine, params, event, payload, sender) {
19
+ _BaseContext_engine.set(this, void 0);
20
+ _BaseContext_params.set(this, void 0);
21
+ _BaseContext_event.set(this, void 0);
22
+ _BaseContext_sender.set(this, void 0);
23
+ _BaseContext_payload.set(this, void 0);
24
+ __classPrivateFieldSet(this, _BaseContext_engine, engine, "f");
25
+ __classPrivateFieldSet(this, _BaseContext_params, params, "f");
26
+ __classPrivateFieldSet(this, _BaseContext_event, event, "f");
27
+ __classPrivateFieldSet(this, _BaseContext_payload, payload, "f");
28
+ __classPrivateFieldSet(this, _BaseContext_sender, sender, "f");
29
+ }
30
+ /**
31
+ * The event information
32
+ */
33
+ get event() {
34
+ return {
35
+ event: __classPrivateFieldGet(this, _BaseContext_event, "f"),
36
+ query: __classPrivateFieldGet(this, _BaseContext_params, "f").query,
37
+ params: __classPrivateFieldGet(this, _BaseContext_params, "f").params,
38
+ payload: __classPrivateFieldGet(this, _BaseContext_payload, "f"),
39
+ };
40
+ }
41
+ /**
42
+ * The channel name
43
+ */
44
+ get channelName() {
45
+ return __classPrivateFieldGet(this, _BaseContext_engine, "f").name;
46
+ }
47
+ /**
48
+ * The channel instance
49
+ */
50
+ get channel() {
51
+ return new channel_1.Channel(__classPrivateFieldGet(this, _BaseContext_engine, "f"));
52
+ }
53
+ /**
54
+ * All current presences in the channel
55
+ */
56
+ get presences() {
57
+ return __classPrivateFieldGet(this, _BaseContext_engine, "f").getPresence();
58
+ }
59
+ /**
60
+ * All current assigns in the channel
61
+ */
62
+ get assigns() {
63
+ return __classPrivateFieldGet(this, _BaseContext_engine, "f").getAssigns();
64
+ }
65
+ /**
66
+ * The user who sent the request
67
+ */
68
+ get user() {
69
+ return this.channel.getUserData(__classPrivateFieldGet(this, _BaseContext_sender, "f"));
70
+ }
71
+ /**
72
+ * Gets the channel engine instance
73
+ * @protected
74
+ */
75
+ get engine() {
76
+ return __classPrivateFieldGet(this, _BaseContext_engine, "f");
77
+ }
78
+ /**
79
+ * Updates the event parameters.
80
+ */
81
+ updateParams(params) {
82
+ __classPrivateFieldSet(this, _BaseContext_params, params, "f");
83
+ }
84
+ }
85
+ exports.BaseContext = BaseContext;
86
+ _BaseContext_engine = new WeakMap(), _BaseContext_params = new WeakMap(), _BaseContext_event = new WeakMap(), _BaseContext_sender = new WeakMap(), _BaseContext_payload = new WeakMap();
@@ -10,80 +10,36 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _EventContext_instances, _EventContext_event, _EventContext_engine, _EventContext_params, _EventContext_requestId, _EventContext_sendMessage;
13
+ var _EventContext_instances, _EventContext_event, _EventContext_requestId, _EventContext_sendMessage;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.EventContext = void 0;
16
16
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
17
- const channel_1 = require("../wrappers/channel");
17
+ const baseContext_1 = require("./baseContext");
18
18
  /**
19
19
  * EventContext combines the functionality of EventRequest and EventResponse
20
20
  * to provide a unified interface for handling events in a channel.
21
21
  */
22
- class EventContext {
22
+ class EventContext extends baseContext_1.BaseContext {
23
23
  constructor(event, params, engine) {
24
+ super(engine, params, event.event, event.payload, event.sender);
24
25
  _EventContext_instances.add(this);
25
26
  _EventContext_event.set(this, void 0);
26
- _EventContext_engine.set(this, void 0);
27
- _EventContext_params.set(this, void 0);
28
27
  _EventContext_requestId.set(this, void 0);
29
28
  __classPrivateFieldSet(this, _EventContext_event, event, "f");
30
- __classPrivateFieldSet(this, _EventContext_params, params, "f");
31
- __classPrivateFieldSet(this, _EventContext_engine, engine, "f");
32
29
  __classPrivateFieldSet(this, _EventContext_requestId, event.requestId, "f");
33
30
  }
34
- /**
35
- * The event information
36
- */
37
- get event() {
38
- return {
39
- event: __classPrivateFieldGet(this, _EventContext_event, "f").event,
40
- query: __classPrivateFieldGet(this, _EventContext_params, "f").query,
41
- params: __classPrivateFieldGet(this, _EventContext_params, "f").params,
42
- payload: __classPrivateFieldGet(this, _EventContext_event, "f").payload,
43
- };
44
- }
45
- /**
46
- * The channel name
47
- */
48
- get channelName() {
49
- return __classPrivateFieldGet(this, _EventContext_engine, "f").name;
50
- }
51
- /**
52
- * The channel instance
53
- */
54
- get channel() {
55
- return new channel_1.Channel(__classPrivateFieldGet(this, _EventContext_engine, "f"));
56
- }
57
- /**
58
- * All current presences in the channel
59
- */
60
- get presences() {
61
- return __classPrivateFieldGet(this, _EventContext_engine, "f").getPresence();
62
- }
63
- /**
64
- * All current assigns in the channel
65
- */
66
- get assigns() {
67
- return __classPrivateFieldGet(this, _EventContext_engine, "f").getAssigns();
68
- }
69
- /**
70
- * The user who sent the request
71
- */
72
- get user() {
73
- return __classPrivateFieldGet(this, _EventContext_engine, "f").getUserData(__classPrivateFieldGet(this, _EventContext_event, "f").sender);
74
- }
75
31
  /**
76
32
  * Assigns data to the client
77
33
  */
78
34
  assign(assigns) {
79
- __classPrivateFieldGet(this, _EventContext_engine, "f").updateAssigns(__classPrivateFieldGet(this, _EventContext_event, "f").sender, assigns);
35
+ this.channel.updateAssigns(__classPrivateFieldGet(this, _EventContext_event, "f").sender, assigns);
80
36
  return this;
81
37
  }
82
38
  /**
83
39
  * Sends a direct reply to the client
84
40
  */
85
41
  reply(event, payload) {
86
- __classPrivateFieldGet(this, _EventContext_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, [__classPrivateFieldGet(this, _EventContext_event, "f").sender], pondsocket_common_1.ServerActions.SYSTEM, event, payload, __classPrivateFieldGet(this, _EventContext_requestId, "f"));
42
+ this.engine.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, [__classPrivateFieldGet(this, _EventContext_event, "f").sender], pondsocket_common_1.ServerActions.SYSTEM, event, payload, __classPrivateFieldGet(this, _EventContext_requestId, "f"));
87
43
  return this;
88
44
  }
89
45
  /**
@@ -112,32 +68,32 @@ class EventContext {
112
68
  * Tracks a user's presence
113
69
  */
114
70
  trackPresence(presence, userId = __classPrivateFieldGet(this, _EventContext_event, "f").sender) {
115
- __classPrivateFieldGet(this, _EventContext_engine, "f").trackPresence(userId, presence);
71
+ this.channel.trackPresence(userId, presence);
116
72
  return this;
117
73
  }
118
74
  /**
119
75
  * Updates a user's presence
120
76
  */
121
77
  updatePresence(presence, userId = __classPrivateFieldGet(this, _EventContext_event, "f").sender) {
122
- __classPrivateFieldGet(this, _EventContext_engine, "f").updatePresence(userId, presence);
78
+ this.channel.updatePresence(userId, presence);
123
79
  return this;
124
80
  }
125
81
  /**
126
82
  * Kicks a user from the channel
127
83
  */
128
84
  evictUser(reason, userId = __classPrivateFieldGet(this, _EventContext_event, "f").sender) {
129
- __classPrivateFieldGet(this, _EventContext_engine, "f").kickUser(userId, reason);
85
+ this.channel.evictUser(userId, reason);
130
86
  return this;
131
87
  }
132
88
  /**
133
89
  * Removes a user's presence
134
90
  */
135
91
  removePresence(userId = __classPrivateFieldGet(this, _EventContext_event, "f").sender) {
136
- __classPrivateFieldGet(this, _EventContext_engine, "f").removePresence(userId);
92
+ this.channel.removePresence(userId);
137
93
  return this;
138
94
  }
139
95
  }
140
96
  exports.EventContext = EventContext;
141
- _EventContext_event = new WeakMap(), _EventContext_engine = new WeakMap(), _EventContext_params = new WeakMap(), _EventContext_requestId = new WeakMap(), _EventContext_instances = new WeakSet(), _EventContext_sendMessage = function _EventContext_sendMessage(recipient, event, payload) {
142
- __classPrivateFieldGet(this, _EventContext_engine, "f").sendMessage(__classPrivateFieldGet(this, _EventContext_event, "f").sender, recipient, pondsocket_common_1.ServerActions.BROADCAST, event, payload, __classPrivateFieldGet(this, _EventContext_requestId, "f"));
97
+ _EventContext_event = new WeakMap(), _EventContext_requestId = new WeakMap(), _EventContext_instances = new WeakSet(), _EventContext_sendMessage = function _EventContext_sendMessage(recipient, event, payload) {
98
+ this.engine.sendMessage(__classPrivateFieldGet(this, _EventContext_event, "f").sender, recipient, pondsocket_common_1.ServerActions.BROADCAST, event, payload, __classPrivateFieldGet(this, _EventContext_requestId, "f"));
143
99
  };
@@ -10,67 +10,31 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _JoinContext_instances, _JoinContext_options, _JoinContext_engine, _JoinContext_user, _JoinContext_newAssigns, _JoinContext_executed, _JoinContext_accepted, _JoinContext_sendMessage, _JoinContext_directMessage, _JoinContext_performChecks;
13
+ var _JoinContext_instances, _JoinContext_options, _JoinContext_user, _JoinContext_newAssigns, _JoinContext_executed, _JoinContext_accepted, _JoinContext_sendMessage, _JoinContext_directMessage, _JoinContext_performChecks;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.JoinContext = void 0;
16
16
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
17
+ const baseContext_1 = require("./baseContext");
17
18
  const httpError_1 = require("../errors/httpError");
18
- const channel_1 = require("../wrappers/channel");
19
19
  /**
20
20
  * JoinContext combines the functionality of JoinRequest and JoinResponse
21
21
  * to provide a unified interface for handling join events in a channel.
22
22
  */
23
- class JoinContext {
23
+ class JoinContext extends baseContext_1.BaseContext {
24
24
  constructor(options, engine, user) {
25
+ super(engine, options.params, engine.name, options.joinParams, user.clientId);
25
26
  _JoinContext_instances.add(this);
26
27
  _JoinContext_options.set(this, void 0);
27
- _JoinContext_engine.set(this, void 0);
28
28
  _JoinContext_user.set(this, void 0);
29
29
  _JoinContext_newAssigns.set(this, void 0);
30
30
  _JoinContext_executed.set(this, void 0);
31
31
  _JoinContext_accepted.set(this, void 0);
32
32
  __classPrivateFieldSet(this, _JoinContext_options, options, "f");
33
- __classPrivateFieldSet(this, _JoinContext_engine, engine, "f");
34
33
  __classPrivateFieldSet(this, _JoinContext_user, user, "f");
35
34
  __classPrivateFieldSet(this, _JoinContext_executed, false, "f");
36
35
  __classPrivateFieldSet(this, _JoinContext_accepted, false, "f");
37
36
  __classPrivateFieldSet(this, _JoinContext_newAssigns, Object.assign({}, user.assigns), "f");
38
37
  }
39
- /**
40
- * The event information
41
- */
42
- get event() {
43
- return {
44
- event: __classPrivateFieldGet(this, _JoinContext_engine, "f").name,
45
- query: __classPrivateFieldGet(this, _JoinContext_options, "f").params.query,
46
- params: __classPrivateFieldGet(this, _JoinContext_options, "f").params.params,
47
- payload: __classPrivateFieldGet(this, _JoinContext_options, "f").joinParams,
48
- };
49
- }
50
- /**
51
- * The channel name
52
- */
53
- get channelName() {
54
- return __classPrivateFieldGet(this, _JoinContext_engine, "f").name;
55
- }
56
- /**
57
- * The channel instance
58
- */
59
- get channel() {
60
- return new channel_1.Channel(__classPrivateFieldGet(this, _JoinContext_engine, "f"));
61
- }
62
- /**
63
- * All current presences in the channel
64
- */
65
- get presences() {
66
- return __classPrivateFieldGet(this, _JoinContext_engine, "f").getPresence();
67
- }
68
- /**
69
- * All current assigns in the channel
70
- */
71
- get assigns() {
72
- return __classPrivateFieldGet(this, _JoinContext_engine, "f").getAssigns();
73
- }
74
38
  /**
75
39
  * The user who sent the request
76
40
  */
@@ -98,8 +62,8 @@ class JoinContext {
98
62
  */
99
63
  accept() {
100
64
  __classPrivateFieldGet(this, _JoinContext_instances, "m", _JoinContext_performChecks).call(this);
101
- const onMessage = __classPrivateFieldGet(this, _JoinContext_engine, "f").parent.parent.sendMessage.bind(__classPrivateFieldGet(this, _JoinContext_engine, "f").parent.parent, __classPrivateFieldGet(this, _JoinContext_user, "f").socket);
102
- const subscription = __classPrivateFieldGet(this, _JoinContext_engine, "f").addUser(__classPrivateFieldGet(this, _JoinContext_user, "f").clientId, __classPrivateFieldGet(this, _JoinContext_newAssigns, "f"), onMessage);
65
+ const onMessage = this.engine.parent.parent.sendMessage.bind(this.engine.parent.parent, __classPrivateFieldGet(this, _JoinContext_user, "f").socket);
66
+ const subscription = this.engine.addUser(__classPrivateFieldGet(this, _JoinContext_user, "f").clientId, __classPrivateFieldGet(this, _JoinContext_newAssigns, "f"), onMessage);
103
67
  __classPrivateFieldGet(this, _JoinContext_user, "f").subscriptions.add(subscription);
104
68
  __classPrivateFieldSet(this, _JoinContext_accepted, true, "f");
105
69
  return this;
@@ -115,7 +79,7 @@ class JoinContext {
115
79
  message: message || 'Unauthorized connection',
116
80
  code: errorCode || 401,
117
81
  },
118
- channelName: __classPrivateFieldGet(this, _JoinContext_engine, "f").name,
82
+ channelName: this.engine.name,
119
83
  action: pondsocket_common_1.ServerActions.ERROR,
120
84
  requestId: __classPrivateFieldGet(this, _JoinContext_user, "f").requestId,
121
85
  };
@@ -127,7 +91,7 @@ class JoinContext {
127
91
  */
128
92
  assign(assigns) {
129
93
  if (__classPrivateFieldGet(this, _JoinContext_accepted, "f")) {
130
- __classPrivateFieldGet(this, _JoinContext_engine, "f").updateAssigns(__classPrivateFieldGet(this, _JoinContext_user, "f").clientId, assigns);
94
+ this.engine.updateAssigns(__classPrivateFieldGet(this, _JoinContext_user, "f").clientId, assigns);
131
95
  }
132
96
  else {
133
97
  __classPrivateFieldSet(this, _JoinContext_newAssigns, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _JoinContext_newAssigns, "f")), assigns), "f");
@@ -140,7 +104,7 @@ class JoinContext {
140
104
  reply(event, payload) {
141
105
  const message = {
142
106
  action: pondsocket_common_1.ServerActions.SYSTEM,
143
- channelName: __classPrivateFieldGet(this, _JoinContext_engine, "f").name,
107
+ channelName: this.engine.name,
144
108
  requestId: __classPrivateFieldGet(this, _JoinContext_user, "f").requestId,
145
109
  payload,
146
110
  event,
@@ -174,18 +138,18 @@ class JoinContext {
174
138
  * Tracks the user's presence
175
139
  */
176
140
  trackPresence(presence) {
177
- __classPrivateFieldGet(this, _JoinContext_engine, "f").trackPresence(__classPrivateFieldGet(this, _JoinContext_user, "f").clientId, presence);
141
+ this.engine.trackPresence(__classPrivateFieldGet(this, _JoinContext_user, "f").clientId, presence);
178
142
  return this;
179
143
  }
180
144
  }
181
145
  exports.JoinContext = JoinContext;
182
- _JoinContext_options = new WeakMap(), _JoinContext_engine = new WeakMap(), _JoinContext_user = new WeakMap(), _JoinContext_newAssigns = new WeakMap(), _JoinContext_executed = new WeakMap(), _JoinContext_accepted = new WeakMap(), _JoinContext_instances = new WeakSet(), _JoinContext_sendMessage = function _JoinContext_sendMessage(recipient, event, payload) {
183
- __classPrivateFieldGet(this, _JoinContext_engine, "f").sendMessage(__classPrivateFieldGet(this, _JoinContext_user, "f").clientId, recipient, pondsocket_common_1.ServerActions.BROADCAST, event, payload, __classPrivateFieldGet(this, _JoinContext_user, "f").requestId);
146
+ _JoinContext_options = new WeakMap(), _JoinContext_user = new WeakMap(), _JoinContext_newAssigns = new WeakMap(), _JoinContext_executed = new WeakMap(), _JoinContext_accepted = new WeakMap(), _JoinContext_instances = new WeakSet(), _JoinContext_sendMessage = function _JoinContext_sendMessage(recipient, event, payload) {
147
+ this.engine.sendMessage(__classPrivateFieldGet(this, _JoinContext_user, "f").clientId, recipient, pondsocket_common_1.ServerActions.BROADCAST, event, payload, __classPrivateFieldGet(this, _JoinContext_user, "f").requestId);
184
148
  }, _JoinContext_directMessage = function _JoinContext_directMessage(event) {
185
- __classPrivateFieldGet(this, _JoinContext_engine, "f").parent.parent.sendMessage(__classPrivateFieldGet(this, _JoinContext_user, "f").socket, event);
149
+ this.engine.parent.parent.sendMessage(__classPrivateFieldGet(this, _JoinContext_user, "f").socket, event);
186
150
  }, _JoinContext_performChecks = function _JoinContext_performChecks() {
187
151
  if (__classPrivateFieldGet(this, _JoinContext_executed, "f")) {
188
- const message = `Request to join channel ${__classPrivateFieldGet(this, _JoinContext_engine, "f").name} rejected: Request already executed`;
152
+ const message = `Request to join channel ${this.engine.name} rejected: Request already executed`;
189
153
  throw new httpError_1.HttpError(403, message);
190
154
  }
191
155
  __classPrivateFieldSet(this, _JoinContext_executed, true, "f");
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _OutgoingContext_payload, _OutgoingContext_isBlocked;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.OutgoingContext = void 0;
16
+ const baseContext_1 = require("./baseContext");
17
+ class OutgoingContext extends baseContext_1.BaseContext {
18
+ constructor(event, params, engine, userid) {
19
+ super(engine, params, event.event, event.payload, userid);
20
+ _OutgoingContext_payload.set(this, void 0);
21
+ _OutgoingContext_isBlocked.set(this, void 0);
22
+ __classPrivateFieldSet(this, _OutgoingContext_payload, event.payload, "f");
23
+ __classPrivateFieldSet(this, _OutgoingContext_isBlocked, false, "f");
24
+ }
25
+ get payload() {
26
+ return __classPrivateFieldGet(this, _OutgoingContext_payload, "f");
27
+ }
28
+ /**
29
+ * Blocks the outgoing context, preventing further processing of the event.
30
+ */
31
+ block() {
32
+ __classPrivateFieldSet(this, _OutgoingContext_isBlocked, true, "f");
33
+ return this;
34
+ }
35
+ /**
36
+ * Checks if the outgoing context is blocked.
37
+ * @returns {boolean} - True if blocked, false otherwise.
38
+ */
39
+ isBlocked() {
40
+ return __classPrivateFieldGet(this, _OutgoingContext_isBlocked, "f");
41
+ }
42
+ /**
43
+ * Transforms the outgoing context with a new payload.
44
+ * @param payload - The new payload to set for the context.
45
+ */
46
+ transform(payload) {
47
+ __classPrivateFieldSet(this, _OutgoingContext_payload, payload, "f");
48
+ return this;
49
+ }
50
+ /**
51
+ * Updates the parameters of the outgoing context.
52
+ * @param params - The new parameters to set for the context.
53
+ */
54
+ updateParams(params) {
55
+ super.updateParams(params);
56
+ }
57
+ }
58
+ exports.OutgoingContext = OutgoingContext;
59
+ _OutgoingContext_payload = new WeakMap(), _OutgoingContext_isBlocked = new WeakMap();