@eleven-am/pondsocket 0.1.167 → 0.1.168

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.
@@ -18,9 +18,11 @@ describe('AbstractRequest', () => {
18
18
  expect(request).toBeTruthy();
19
19
  expect(request.channelName).toBe('test');
20
20
  expect(request.assigns).toEqual({});
21
+ expect(yield request.getPresence()).toEqual({});
21
22
  }));
22
23
  it('should be able to parse queries', () => {
23
24
  const request = new abstractRequest_1.AbstractRequest('/1234?choke=balls', createMockChannelEngine(), {});
25
+ expect(() => request.event).toThrowError('Event was not parsed');
24
26
  expect(request['_parseQueries']('/:id')).toBe(true);
25
27
  expect(request.event).toEqual({
26
28
  event: '/1234?choke=balls',
@@ -1,13 +1,4 @@
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
- };
11
2
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
3
  if (kind === "m") throw new TypeError("Private method is not writable");
13
4
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
@@ -30,7 +21,7 @@ var __rest = (this && this.__rest) || function (s, e) {
30
21
  }
31
22
  return t;
32
23
  };
33
- var _ChannelEngine_instances, _ChannelEngine_receiver, _ChannelEngine_presenceEngine, _ChannelEngine_users, _ChannelEngine_parentEngine, _ChannelEngine_subscribe, _ChannelEngine_getUsersFromRecipients, _ChannelEngine_buildSubscription, _ChannelEngine_removeUser, _ChannelEngine_initPubSub, _Channel_engine;
24
+ var _ChannelEngine_instances, _ChannelEngine_receiver, _ChannelEngine_presenceEngine, _ChannelEngine_users, _ChannelEngine_parentEngine, _ChannelEngine_subscribe, _ChannelEngine_getUsersFromRecipients, _ChannelEngine_buildSubscription, _ChannelEngine_removeUser, _Channel_engine;
34
25
  Object.defineProperty(exports, "__esModule", { value: true });
35
26
  exports.Channel = exports.ChannelEngine = void 0;
36
27
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
@@ -49,8 +40,6 @@ class ChannelEngine {
49
40
  __classPrivateFieldSet(this, _ChannelEngine_parentEngine, parent, "f");
50
41
  __classPrivateFieldSet(this, _ChannelEngine_users, new Map(), "f");
51
42
  __classPrivateFieldSet(this, _ChannelEngine_receiver, new pondsocket_common_1.Subject(), "f");
52
- this.pubSubClient = parent.parent.getPubSubClient();
53
- __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_initPubSub).call(this);
54
43
  }
55
44
  /**
56
45
  * @desc Gets the presence engine for the channel
@@ -178,7 +167,6 @@ class ChannelEngine {
178
167
  event,
179
168
  payload,
180
169
  };
181
- this.pubSubClient.publish(recipient, channelEvent);
182
170
  const recipients = __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_getUsersFromRecipients).call(this, recipient, sender);
183
171
  __classPrivateFieldGet(this, _ChannelEngine_receiver, "f").publish(Object.assign(Object.assign({}, channelEvent), { recipients }));
184
172
  }
@@ -224,15 +212,6 @@ class ChannelEngine {
224
212
  unsubscribeFrom(userId, channel) {
225
213
  __classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").parent.unsubscribeFrom(userId, channel);
226
214
  }
227
- /**
228
- * @desc Gets the presence from the pubSub client
229
- */
230
- getPubSubPresence() {
231
- return __awaiter(this, void 0, void 0, function* () {
232
- const presence = yield this.pubSubClient.getPresence(this.name);
233
- return presence.reduce((acc, value) => Object.assign(acc, value), {});
234
- });
235
- }
236
215
  }
237
216
  exports.ChannelEngine = ChannelEngine;
238
217
  _ChannelEngine_receiver = new WeakMap(), _ChannelEngine_presenceEngine = new WeakMap(), _ChannelEngine_users = new WeakMap(), _ChannelEngine_parentEngine = new WeakMap(), _ChannelEngine_instances = new WeakSet(), _ChannelEngine_subscribe = function _ChannelEngine_subscribe(userId, onMessage) {
@@ -298,21 +277,6 @@ _ChannelEngine_receiver = new WeakMap(), _ChannelEngine_presenceEngine = new Wea
298
277
  }
299
278
  unsubscribe();
300
279
  cachedUser.subscriptions.delete(this.name);
301
- }, _ChannelEngine_initPubSub = function _ChannelEngine_initPubSub() {
302
- this.pubSubClient.subscribeToPresence(this.name, () => {
303
- if (__classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) {
304
- return __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f").getPresence();
305
- }
306
- return {};
307
- });
308
- this.pubSubClient.subscribe(this.name, (recipients, data) => {
309
- try {
310
- this.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, recipients, data.action, data.event, data.payload, data.requestId);
311
- }
312
- catch (error) {
313
- // noop
314
- }
315
- });
316
280
  };
317
281
  class Channel {
318
282
  constructor(engine) {
@@ -373,12 +337,12 @@ class Channel {
373
337
  return this;
374
338
  }
375
339
  upsertPresence(userId, presence) {
376
- const oldPresence = __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getInternalPresence()[userId];
340
+ const oldPresence = __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getPresence()[userId];
377
341
  if (oldPresence) {
378
- void __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.updatePresence(userId, presence);
342
+ __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.updatePresence(userId, presence);
379
343
  }
380
344
  else {
381
- void __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
345
+ __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
382
346
  }
383
347
  return this;
384
348
  }
@@ -1,13 +1,4 @@
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
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.createParentEngine = void 0;
13
4
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
@@ -116,7 +107,7 @@ describe('ChannelEngine', () => {
116
107
  test2: { test: 2 },
117
108
  });
118
109
  });
119
- it('should be able to track presence', () => __awaiter(void 0, void 0, void 0, function* () {
110
+ it('should be able to track presence', () => {
120
111
  const onMessage = jest.fn();
121
112
  const { parentEngine } = (0, exports.createParentEngine)();
122
113
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
@@ -126,31 +117,32 @@ describe('ChannelEngine', () => {
126
117
  presence: {},
127
118
  id: 'test',
128
119
  });
129
- yield channelEngine.presenceEngine.trackPresence('test', { test: 2 });
120
+ channelEngine.presenceEngine.trackPresence('test', { test: 2 });
130
121
  expect(channelEngine.presenceEngine).toBeDefined();
131
122
  expect(channelEngine.getUserData('test')).toEqual({
132
123
  assigns: { test: 1 },
133
124
  presence: { test: 2 },
134
125
  id: 'test',
135
126
  });
136
- }));
137
- it('should throw error if channel is already tracking the users presence', () => __awaiter(void 0, void 0, void 0, function* () {
127
+ });
128
+ it('should throw error if channel is already tracking the users presence', () => {
138
129
  const onMessage = jest.fn();
139
130
  const { parentEngine } = (0, exports.createParentEngine)();
140
131
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
141
132
  channelEngine.addUser('test', { test: 1 }, onMessage);
142
- yield channelEngine.presenceEngine.trackPresence('test', { test: 2 });
143
- yield expect(channelEngine.presenceEngine.trackPresence('test', { test: 2 }))
144
- .rejects.toThrow('PresenceEngine: Presence with key test already exists');
145
- }));
146
- it('should be able to list presence', () => __awaiter(void 0, void 0, void 0, function* () {
133
+ channelEngine.presenceEngine.trackPresence('test', { test: 2 });
134
+ expect(() => {
135
+ channelEngine.presenceEngine.trackPresence('test', { test: 2 });
136
+ }).toThrow('PresenceEngine: Presence with key test already exists');
137
+ });
138
+ it('should be able to list presence', () => {
147
139
  var _a;
148
140
  const onMessage = jest.fn();
149
141
  const { parentEngine } = (0, exports.createParentEngine)();
150
142
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
151
143
  channelEngine.addUser('test', { test: 1 }, onMessage);
152
144
  channelEngine.addUser('test2', { test: 2 }, onMessage);
153
- yield channelEngine.presenceEngine.trackPresence('test', { test: 2 });
145
+ channelEngine.presenceEngine.trackPresence('test', { test: 2 });
154
146
  expect(onMessage).toHaveBeenCalledWith(expect.objectContaining({
155
147
  channelName: 'test',
156
148
  action: pondsocket_common_1.ServerActions.PRESENCE,
@@ -162,31 +154,32 @@ describe('ChannelEngine', () => {
162
154
  },
163
155
  },
164
156
  }));
165
- yield channelEngine.presenceEngine.trackPresence('test2', { test: 3 });
166
- expect(yield ((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence())).toEqual({
157
+ channelEngine.presenceEngine.trackPresence('test2', { test: 3 });
158
+ expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
167
159
  test: { test: 2 },
168
160
  test2: { test: 3 },
169
161
  });
170
162
  expect(onMessage).toHaveBeenCalledTimes(5);
171
- }));
172
- it('should update a users presence', () => __awaiter(void 0, void 0, void 0, function* () {
163
+ });
164
+ it('should update a users presence', () => {
165
+ var _a;
173
166
  const onMessage = jest.fn();
174
167
  const { parentEngine } = (0, exports.createParentEngine)();
175
168
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
176
169
  channelEngine.addUser('test', { test: 1 }, onMessage);
177
- yield channelEngine.presenceEngine.trackPresence('test', { test: 2 });
170
+ channelEngine.presenceEngine.trackPresence('test', { test: 2 });
178
171
  expect(channelEngine.getUserData('test')).toEqual({
179
172
  assigns: { test: 1 },
180
173
  presence: { test: 2 },
181
174
  id: 'test',
182
175
  });
183
- yield channelEngine.presenceEngine.updatePresence('test', { test: 3 });
176
+ (_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.updatePresence('test', { test: 3 });
184
177
  expect(channelEngine.getUserData('test')).toEqual({
185
178
  assigns: { test: 1 },
186
179
  presence: { test: 3 },
187
180
  id: 'test',
188
181
  });
189
- }));
182
+ });
190
183
  it('should remove user from channel', () => {
191
184
  const onMessage = jest.fn();
192
185
  const { parentEngine, socket } = (0, exports.createParentEngine)();
@@ -199,23 +192,23 @@ describe('ChannelEngine', () => {
199
192
  expect(channelEngine.size).toEqual(0);
200
193
  expect(channelEngine.getUserData('test')).not.toBeDefined();
201
194
  });
202
- it('should untrack presence when user is removed', () => __awaiter(void 0, void 0, void 0, function* () {
203
- var _a, _b;
195
+ it('should untrack presence when user is removed', () => {
196
+ var _a;
204
197
  const onMessage = jest.fn();
205
198
  const { parentEngine, socket } = (0, exports.createParentEngine)();
206
199
  const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
207
200
  const unsub = channelEngine.addUser('test1', { test: 1 }, onMessage);
208
201
  socket.subscriptions.set('test', unsub);
209
- yield channelEngine.presenceEngine.trackPresence('test1', { test: 2 });
202
+ channelEngine.presenceEngine.trackPresence('test1', { test: 2 });
210
203
  channelEngine.addUser('test2', { test: 1 }, onMessage);
211
- yield channelEngine.presenceEngine.trackPresence('test2', { test: 2 });
212
- expect(yield ((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence())).toEqual({
204
+ channelEngine.presenceEngine.trackPresence('test2', { test: 2 });
205
+ expect(channelEngine.presenceEngine.getPresence()).toEqual({
213
206
  test1: { test: 2 },
214
207
  test2: { test: 2 },
215
208
  });
216
209
  onMessage.mockClear();
217
210
  channelEngine.kickUser('test1', 'test reason');
218
- expect(yield ((_b = channelEngine.presenceEngine) === null || _b === void 0 ? void 0 : _b.getPresence())).toEqual({
211
+ expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
219
212
  test2: { test: 2 },
220
213
  });
221
214
  expect(onMessage).toHaveBeenCalledWith(expect.objectContaining({
@@ -229,18 +222,22 @@ describe('ChannelEngine', () => {
229
222
  },
230
223
  },
231
224
  }));
232
- }));
233
- it('should throw error if user is not in channel and isPond is false', () => __awaiter(void 0, void 0, void 0, function* () {
234
- var _a;
225
+ });
226
+ it('should throw error if user is not in channel and isPond is false', () => {
235
227
  const { parentEngine } = (0, exports.createParentEngine)();
236
228
  const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
237
229
  // add a user to the channel
238
230
  channelEngine.addUser('test', { test: 1 }, jest.fn());
239
231
  // track the added user's presence
240
- yield channelEngine.presenceEngine.trackPresence('test', { test: 2 });
241
- yield expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.removePresence('test1'))
242
- .rejects.toThrow('PresenceEngine: Presence with key test1 does not exist');
243
- }));
232
+ channelEngine.presenceEngine.trackPresence('test', { test: 2 });
233
+ // we do this because we want to initialize the presence engine
234
+ // the engine is not initialized by default when a channel is created
235
+ expect(() => {
236
+ var _a;
237
+ // now we try to untrack the presence of a user that is not in the channel
238
+ (_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.removePresence('test1');
239
+ }).toThrow('PresenceEngine: Presence with key test1 does not exist');
240
+ });
244
241
  it('should not throw error if user is not in channel and isPond is true', () => {
245
242
  const { parentEngine } = (0, exports.createParentEngine)();
246
243
  const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
@@ -76,7 +76,7 @@ class EventResponse {
76
76
  * @param userId - the id of the user to track
77
77
  */
78
78
  trackPresence(presence, userId = __classPrivateFieldGet(this, _EventResponse_event, "f").sender) {
79
- void __classPrivateFieldGet(this, _EventResponse_engine, "f").presenceEngine.trackPresence(userId, presence);
79
+ __classPrivateFieldGet(this, _EventResponse_engine, "f").presenceEngine.trackPresence(userId, presence);
80
80
  return this;
81
81
  }
82
82
  /**
@@ -1,13 +1,4 @@
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
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.createChannelEvent = exports.createChannelEngine = void 0;
13
4
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
@@ -123,54 +114,62 @@ describe('ChannelResponse', () => {
123
114
  response.trackPresence({ status: 'online' });
124
115
  expect(channelEngine.presenceEngine.trackPresence).toHaveBeenCalledWith('sender', { status: 'online' });
125
116
  });
126
- it('should throw an error if trackPresence is called twice', () => __awaiter(void 0, void 0, void 0, function* () {
117
+ it('should throw an error if trackPresence is called twice', () => {
127
118
  const { response, channelEngine } = createChannelResponse();
128
119
  jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
129
120
  expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
130
121
  response.trackPresence({ status: 'online' });
131
122
  expect(channelEngine.presenceEngine.trackPresence).toHaveBeenCalledWith('sender', { status: 'online' });
132
- }));
123
+ expect(() => response.trackPresence({ status: 'online' })).toThrow('PresenceEngine: Presence with key sender already exists');
124
+ });
133
125
  it('should throw an error if trackPresence is called for a non existing user', () => {
134
- const { channelEngine } = createChannelResponse();
126
+ const { response, channelEngine } = createChannelResponse();
135
127
  jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
136
128
  expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
129
+ expect(() => response.trackPresence({ status: 'online' }, 'non_existent_user'))
130
+ .toThrow('ChannelEngine: Invalid recipients non_existent_user some users do not exist in channel test');
137
131
  });
138
- it('should update the presence of a user that is already tracked', () => __awaiter(void 0, void 0, void 0, function* () {
132
+ it('should update the presence of a user that is already tracked', () => {
139
133
  var _a, _b;
140
134
  const { response, channelEngine } = createChannelResponse();
141
135
  jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
142
136
  expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
143
137
  response.trackPresence({ status: 'online' });
144
138
  expect(channelEngine.presenceEngine.trackPresence).toHaveBeenCalledWith('sender', { status: 'online' });
145
- expect(yield ((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence())).toEqual({
139
+ expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
146
140
  sender: { status: 'online' },
147
141
  });
148
142
  response.updatePresence({ status: 'offline' });
149
- expect(yield ((_b = channelEngine.presenceEngine) === null || _b === void 0 ? void 0 : _b.getPresence())).toEqual({
143
+ expect((_b = channelEngine.presenceEngine) === null || _b === void 0 ? void 0 : _b.getPresence()).toEqual({
150
144
  sender: { status: 'offline' },
151
145
  });
152
- }));
146
+ });
153
147
  it('should throw an error if updatePresence is called for a non existing user', () => {
154
148
  const { response, channelEngine } = createChannelResponse();
155
149
  jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
156
150
  expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
157
151
  response.trackPresence({ status: 'online' });
152
+ expect(() => response.updatePresence({ status: 'online' }, 'non_existent_user'))
153
+ .toThrow('PresenceEngine: Presence with key non_existent_user does not exist');
158
154
  });
159
- it('should unTrack a trackPresence', () => __awaiter(void 0, void 0, void 0, function* () {
155
+ it('should unTrack a trackPresence', () => {
156
+ var _a, _b;
160
157
  const { response, channelEngine } = createChannelResponse();
161
158
  response.trackPresence({ status: 'online' });
162
- expect(yield channelEngine.presenceEngine.getPresence()).toEqual({
159
+ expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
163
160
  sender: { status: 'online' },
164
161
  });
165
162
  response.removePresence();
166
- expect(yield channelEngine.presenceEngine.getPresence()).toEqual({});
167
- }));
168
- it('should throw an error if unTrackPresence is called for a non existing user', () => __awaiter(void 0, void 0, void 0, function* () {
163
+ expect((_b = channelEngine.presenceEngine) === null || _b === void 0 ? void 0 : _b.getPresence()).toEqual({});
164
+ });
165
+ it('should throw an error if unTrackPresence is called for a non existing user', () => {
169
166
  var _a;
170
167
  const { response, channelEngine } = createChannelResponse();
171
168
  response.trackPresence({ status: 'online' });
172
- expect(yield ((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence())).toEqual({
169
+ expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
173
170
  sender: { status: 'online' },
174
171
  });
175
- }));
172
+ expect(() => response.removePresence('non_existent_user'))
173
+ .toThrow('PresenceEngine: Presence with key non_existent_user does not exist');
174
+ });
176
175
  });
@@ -10,7 +10,7 @@ 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 _EndpointEngine_instances, _EndpointEngine_middleware, _EndpointEngine_lobbyEngines, _EndpointEngine_channels, _EndpointEngine_sockets, _EndpointEngine_parentEngine, _EndpointEngine_client, _EndpointEngine_joinChannel, _EndpointEngine_broadcastMessage, _EndpointEngine_retrieveChannel, _EndpointEngine_handleMessage, _EndpointEngine_readMessage, _EndpointEngine_buildError, _EndpointEngine_leaveChannel, _Endpoint_engine;
13
+ var _EndpointEngine_instances, _EndpointEngine_middleware, _EndpointEngine_lobbyEngines, _EndpointEngine_channels, _EndpointEngine_sockets, _EndpointEngine_parentEngine, _EndpointEngine_joinChannel, _EndpointEngine_broadcastMessage, _EndpointEngine_retrieveChannel, _EndpointEngine_handleMessage, _EndpointEngine_readMessage, _EndpointEngine_buildError, _EndpointEngine_leaveChannel, _Endpoint_engine;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.Endpoint = exports.EndpointEngine = void 0;
16
16
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
@@ -22,20 +22,18 @@ const joinResponse_1 = require("../lobby/joinResponse");
22
22
  const lobby_1 = require("../lobby/lobby");
23
23
  const matcher_1 = require("../matcher/matcher");
24
24
  class EndpointEngine {
25
- constructor(parent, client) {
25
+ constructor(parent) {
26
26
  _EndpointEngine_instances.add(this);
27
27
  _EndpointEngine_middleware.set(this, void 0);
28
28
  _EndpointEngine_lobbyEngines.set(this, void 0);
29
29
  _EndpointEngine_channels.set(this, void 0);
30
30
  _EndpointEngine_sockets.set(this, void 0);
31
31
  _EndpointEngine_parentEngine.set(this, void 0);
32
- _EndpointEngine_client.set(this, void 0);
33
32
  __classPrivateFieldSet(this, _EndpointEngine_sockets, new Map(), "f");
34
33
  __classPrivateFieldSet(this, _EndpointEngine_middleware, new middleware_1.Middleware(), "f");
35
34
  __classPrivateFieldSet(this, _EndpointEngine_lobbyEngines, new Map(), "f");
36
35
  __classPrivateFieldSet(this, _EndpointEngine_parentEngine, parent, "f");
37
36
  __classPrivateFieldSet(this, _EndpointEngine_channels, new Map(), "f");
38
- __classPrivateFieldSet(this, _EndpointEngine_client, client, "f");
39
37
  }
40
38
  get parent() {
41
39
  return __classPrivateFieldGet(this, _EndpointEngine_parentEngine, "f");
@@ -164,15 +162,9 @@ class EndpointEngine {
164
162
  sendMessage(socket, message) {
165
163
  socket.send(JSON.stringify(message));
166
164
  }
167
- /**
168
- * @desc Gets the PubSubClient for this endpoint
169
- */
170
- getPubSubClient() {
171
- return __classPrivateFieldGet(this, _EndpointEngine_client, "f");
172
- }
173
165
  }
174
166
  exports.EndpointEngine = EndpointEngine;
175
- _EndpointEngine_middleware = new WeakMap(), _EndpointEngine_lobbyEngines = new WeakMap(), _EndpointEngine_channels = new WeakMap(), _EndpointEngine_sockets = new WeakMap(), _EndpointEngine_parentEngine = new WeakMap(), _EndpointEngine_client = new WeakMap(), _EndpointEngine_instances = new WeakSet(), _EndpointEngine_joinChannel = function _EndpointEngine_joinChannel(channel, socket, joinParams, requestId) {
167
+ _EndpointEngine_middleware = new WeakMap(), _EndpointEngine_lobbyEngines = new WeakMap(), _EndpointEngine_channels = new WeakMap(), _EndpointEngine_sockets = new WeakMap(), _EndpointEngine_parentEngine = new WeakMap(), _EndpointEngine_instances = new WeakSet(), _EndpointEngine_joinChannel = function _EndpointEngine_joinChannel(channel, socket, joinParams, requestId) {
176
168
  const cache = Object.assign(Object.assign({}, socket), { requestId, channelName: channel });
177
169
  __classPrivateFieldGet(this, _EndpointEngine_middleware, "f").run(cache, joinParams, () => {
178
170
  throw new pondError_1.EndpointError(`GatewayEngine: Channel ${channel} does not exist`, 404);
@@ -16,7 +16,6 @@ exports.createEndpointEngine = void 0;
16
16
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
17
17
  const superwstest_1 = __importDefault(require("superwstest"));
18
18
  const pondSocket_1 = require("../server/pondSocket");
19
- /* eslint-disable line-comment-position, no-inline-comments */
20
19
  const createEndpointEngine = (socket) => ({
21
20
  createChannel: jest.fn(),
22
21
  listConnections: jest.fn(),
@@ -95,7 +94,7 @@ describe('endpoint', () => {
95
94
  });
96
95
  yield (0, superwstest_1.default)(server)
97
96
  .ws('/api/socket')
98
- .expectUpgrade((res) => expect(res.statusCode).toBe(101)) // the connection is still upgraded, so we can send error messages
97
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
99
98
  .expectMessage(expect.objectContaining({
100
99
  action: pondsocket_common_1.ServerActions.ERROR,
101
100
  event: pondsocket_common_1.ErrorTypes.UNAUTHORIZED_CONNECTION,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.167",
3
+ "version": "0.1.168",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -35,7 +35,6 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@eleven-am/pondsocket-common": "^0.0.23",
38
- "ioredis": "^5.4.1",
39
38
  "ws": "^8.18.0"
40
39
  },
41
40
  "devDependencies": {
@@ -1,13 +1,4 @@
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
- };
11
2
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
3
  if (kind === "m") throw new TypeError("Private method is not writable");
13
4
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
@@ -19,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
19
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");
20
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
12
  };
22
- var _PresenceEngine_instances, _PresenceEngine_presenceMap, _PresenceEngine_channel, _PresenceEngine_publish, _PresenceEngine_getAllPresenceMap;
13
+ var _PresenceEngine_instances, _PresenceEngine_presenceMap, _PresenceEngine_channel, _PresenceEngine_publish;
23
14
  Object.defineProperty(exports, "__esModule", { value: true });
24
15
  exports.PresenceEngine = void 0;
25
16
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
@@ -33,28 +24,15 @@ class PresenceEngine {
33
24
  __classPrivateFieldSet(this, _PresenceEngine_presenceMap, new Map(), "f");
34
25
  }
35
26
  /**
36
- * @desc Lists all the presence of the internal users
27
+ * @desc Lists all the presence of the users
37
28
  */
38
- getInternalPresence() {
29
+ getPresence() {
39
30
  return Array.from(__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").entries())
40
31
  .reduce((acc, [key, value]) => {
41
32
  acc[key] = value;
42
33
  return acc;
43
34
  }, {});
44
35
  }
45
- /**
46
- * @desc Lists all the presence of all users
47
- */
48
- getPresence() {
49
- return __awaiter(this, void 0, void 0, function* () {
50
- const presenceMap = yield __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_getAllPresenceMap).call(this);
51
- return Array.from(presenceMap.entries())
52
- .reduce((acc, [key, value]) => {
53
- acc[key] = value;
54
- return acc;
55
- }, {});
56
- });
57
- }
58
36
  /**
59
37
  * @desc Returns the presence of a user
60
38
  * @param userId - The id of the user
@@ -68,47 +46,39 @@ class PresenceEngine {
68
46
  * @param presence - The presence
69
47
  */
70
48
  trackPresence(presenceKey, presence) {
71
- return __awaiter(this, void 0, void 0, function* () {
72
- const presenceMap = yield __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_getAllPresenceMap).call(this);
73
- if (!presenceMap.has(presenceKey)) {
74
- __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").set(presenceKey, presence);
75
- presenceMap.set(presenceKey, presence);
76
- __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_publish).call(this, pondsocket_common_1.PresenceEventTypes.JOIN, {
77
- changed: presence,
78
- presence: Array.from(presenceMap.values()),
79
- });
80
- }
81
- else {
82
- const code = 400;
83
- const message = `PresenceEngine: Presence with key ${presenceKey} already exists`;
84
- throw new pondError_1.PresenceError(message, code, __classPrivateFieldGet(this, _PresenceEngine_channel, "f").name, pondsocket_common_1.PresenceEventTypes.JOIN);
85
- }
86
- });
49
+ if (!__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").has(presenceKey)) {
50
+ __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").set(presenceKey, presence);
51
+ __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_publish).call(this, pondsocket_common_1.PresenceEventTypes.JOIN, {
52
+ changed: presence,
53
+ presence: Array.from(__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").values()),
54
+ });
55
+ }
56
+ else {
57
+ const code = 400;
58
+ const message = `PresenceEngine: Presence with key ${presenceKey} already exists`;
59
+ throw new pondError_1.PresenceError(message, code, __classPrivateFieldGet(this, _PresenceEngine_channel, "f").name, pondsocket_common_1.PresenceEventTypes.JOIN);
60
+ }
87
61
  }
88
62
  /**
89
63
  * @desc Removes a presence from the presence engine
90
64
  * @param presenceKey - The key of the presence
91
65
  * @param safe - If true, it will not throw an error if the presence does not exist
92
66
  */
93
- removePresence(presenceKey_1) {
94
- return __awaiter(this, arguments, void 0, function* (presenceKey, safe = false) {
95
- const presenceMap = yield __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_getAllPresenceMap).call(this);
96
- const presence = presenceMap.get(presenceKey);
97
- if (presence) {
98
- __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").delete(presenceKey);
99
- presenceMap.delete(presenceKey);
100
- __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_publish).call(this, pondsocket_common_1.PresenceEventTypes.LEAVE, {
101
- changed: presence,
102
- presence: Array.from(presenceMap.values()),
103
- });
104
- }
105
- else if (!safe) {
106
- const code = 404;
107
- const message = `PresenceEngine: Presence with key ${presenceKey} does not exist`;
108
- throw new pondError_1.PresenceError(message, code, __classPrivateFieldGet(this, _PresenceEngine_channel, "f").name, pondsocket_common_1.PresenceEventTypes.LEAVE);
109
- }
110
- return presence;
111
- });
67
+ removePresence(presenceKey, safe = false) {
68
+ const presence = __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").get(presenceKey);
69
+ if (presence) {
70
+ __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").delete(presenceKey);
71
+ __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_publish).call(this, pondsocket_common_1.PresenceEventTypes.LEAVE, {
72
+ changed: presence,
73
+ presence: Array.from(__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").values()),
74
+ });
75
+ }
76
+ else if (!safe) {
77
+ const code = 404;
78
+ const message = `PresenceEngine: Presence with key ${presenceKey} does not exist`;
79
+ throw new pondError_1.PresenceError(message, code, __classPrivateFieldGet(this, _PresenceEngine_channel, "f").name, pondsocket_common_1.PresenceEventTypes.LEAVE);
80
+ }
81
+ return presence;
112
82
  }
113
83
  /**
114
84
  * @desc Updates a presence
@@ -116,24 +86,20 @@ class PresenceEngine {
116
86
  * @param presence - The new presence
117
87
  */
118
88
  updatePresence(presenceKey, presence) {
119
- return __awaiter(this, void 0, void 0, function* () {
120
- const presenceMap = yield __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_getAllPresenceMap).call(this);
121
- const oldPresence = presenceMap.get(presenceKey);
122
- if (oldPresence) {
123
- const newPresence = Object.assign(Object.assign({}, oldPresence), presence);
124
- __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").set(presenceKey, newPresence);
125
- presenceMap.set(presenceKey, newPresence);
126
- __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_publish).call(this, pondsocket_common_1.PresenceEventTypes.UPDATE, {
127
- changed: newPresence,
128
- presence: Array.from(presenceMap.values()),
129
- });
130
- }
131
- else {
132
- const code = 404;
133
- const message = `PresenceEngine: Presence with key ${presenceKey} does not exist`;
134
- throw new pondError_1.PresenceError(message, code, __classPrivateFieldGet(this, _PresenceEngine_channel, "f").name, pondsocket_common_1.PresenceEventTypes.UPDATE);
135
- }
136
- });
89
+ const oldPresence = __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").get(presenceKey);
90
+ if (oldPresence) {
91
+ const newPresence = Object.assign(Object.assign({}, oldPresence), presence);
92
+ __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").set(presenceKey, newPresence);
93
+ __classPrivateFieldGet(this, _PresenceEngine_instances, "m", _PresenceEngine_publish).call(this, pondsocket_common_1.PresenceEventTypes.UPDATE, {
94
+ changed: newPresence,
95
+ presence: Array.from(__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").values()),
96
+ });
97
+ }
98
+ else {
99
+ const code = 404;
100
+ const message = `PresenceEngine: Presence with key ${presenceKey} does not exist`;
101
+ throw new pondError_1.PresenceError(message, code, __classPrivateFieldGet(this, _PresenceEngine_channel, "f").name, pondsocket_common_1.PresenceEventTypes.UPDATE);
102
+ }
137
103
  }
138
104
  }
139
105
  exports.PresenceEngine = PresenceEngine;
@@ -143,12 +109,4 @@ _PresenceEngine_presenceMap = new WeakMap(), _PresenceEngine_channel = new WeakM
143
109
  return;
144
110
  }
145
111
  __classPrivateFieldGet(this, _PresenceEngine_channel, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, recipients, pondsocket_common_1.ServerActions.PRESENCE, event, payload);
146
- }, _PresenceEngine_getAllPresenceMap = function _PresenceEngine_getAllPresenceMap() {
147
- return __awaiter(this, void 0, void 0, function* () {
148
- const otherInstancesPresences = yield __classPrivateFieldGet(this, _PresenceEngine_channel, "f").pubSubClient.getPresence(__classPrivateFieldGet(this, _PresenceEngine_channel, "f").name);
149
- const newPresenceMap = new Map(__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f"));
150
- Object.entries(otherInstancesPresences)
151
- .forEach(([key, value]) => newPresenceMap.set(key, value));
152
- return newPresenceMap;
153
- });
154
112
  };
@@ -1,13 +1,4 @@
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
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
13
4
  const presence_1 = require("./presence");
@@ -33,10 +24,10 @@ describe('PresenceEngine', () => {
33
24
  presenceKey = 'presenceKey';
34
25
  });
35
26
  describe('trackPresence', () => {
36
- it('should insert a presence into the presence engine', () => __awaiter(void 0, void 0, void 0, function* () {
27
+ it('should insert a presence into the presence engine', () => {
37
28
  // spy on the channel sendMessage method
38
29
  const sendMessage = jest.spyOn(channel, 'sendMessage');
39
- yield presenceEngine.trackPresence(presenceKey, presence);
30
+ presenceEngine.trackPresence(presenceKey, presence);
40
31
  // get all the params
41
32
  const params = sendMessage.mock.calls[0];
42
33
  // remove the first element as it contains the request id which is random
@@ -50,15 +41,15 @@ describe('PresenceEngine', () => {
50
41
  presence: [presence],
51
42
  },
52
43
  ]);
53
- }));
54
- it('should throw an error if the presence already exists', () => __awaiter(void 0, void 0, void 0, function* () {
55
- yield presenceEngine.trackPresence(presenceKey, presence);
56
- yield expect(presenceEngine.trackPresence(presenceKey, presence)).rejects.toThrow(`PresenceEngine: Presence with key ${presenceKey} already exists`);
57
- }));
44
+ });
45
+ it('should throw an error if the presence already exists', () => {
46
+ presenceEngine.trackPresence(presenceKey, presence);
47
+ expect(() => presenceEngine.trackPresence(presenceKey, presence)).toThrowError(`PresenceEngine: Presence with key ${presenceKey} already exists`);
48
+ });
58
49
  });
59
50
  describe('updatePresence', () => {
60
- it('should update a presence', () => __awaiter(void 0, void 0, void 0, function* () {
61
- yield presenceEngine.trackPresence(presenceKey, presence);
51
+ it('should update a presence', () => {
52
+ presenceEngine.trackPresence(presenceKey, presence);
62
53
  const newPresence = {
63
54
  id: 'id',
64
55
  name: 'name',
@@ -67,7 +58,7 @@ describe('PresenceEngine', () => {
67
58
  location: 'location',
68
59
  };
69
60
  const sendMessage = jest.spyOn(channel, 'sendMessage');
70
- yield presenceEngine.updatePresence(presenceKey, newPresence);
61
+ presenceEngine.updatePresence(presenceKey, newPresence);
71
62
  // get all the params
72
63
  const params = sendMessage.mock.calls[0];
73
64
  // remove the first element as it contains the request id which is random
@@ -81,25 +72,25 @@ describe('PresenceEngine', () => {
81
72
  presence: [newPresence],
82
73
  },
83
74
  ]);
84
- }));
85
- it('should throw an error if the presence does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
86
- yield expect(presenceEngine.updatePresence(presenceKey, presence)).rejects.toThrow(`PresenceEngine: Presence with key ${presenceKey} does not exist`);
87
- }));
75
+ });
76
+ it('should throw an error if the presence does not exist', () => {
77
+ expect(() => presenceEngine.updatePresence(presenceKey, presence)).toThrowError(`PresenceEngine: Presence with key ${presenceKey} does not exist`);
78
+ });
88
79
  });
89
80
  describe('removePresence', () => {
90
- it('should remove a presence from the presence engine', () => __awaiter(void 0, void 0, void 0, function* () {
81
+ it('should remove a presence from the presence engine', () => {
91
82
  const listener = jest.spyOn(channel, 'sendMessage');
92
83
  // before we can track a presence, we need make sure the user is in the channel
93
84
  channel.addUser('presenceKey2', { assign: 'assign' }, () => {
94
85
  // do nothing
95
86
  });
96
87
  listener.mockClear();
97
- yield presenceEngine.trackPresence(presenceKey, presence);
98
- yield presenceEngine.trackPresence('presenceKey2', Object.assign(Object.assign({}, presence), { key: 'presence2' }));
88
+ presenceEngine.trackPresence(presenceKey, presence);
89
+ presenceEngine.trackPresence('presenceKey2', Object.assign(Object.assign({}, presence), { key: 'presence2' }));
99
90
  expect(listener).toHaveBeenCalledTimes(2);
100
91
  // clear the mock
101
92
  listener.mockClear();
102
- yield presenceEngine.removePresence(presenceKey);
93
+ presenceEngine.removePresence(presenceKey);
103
94
  expect(listener).toHaveBeenCalledTimes(1);
104
95
  // get all the params
105
96
  const params = listener.mock.calls[0];
@@ -117,22 +108,22 @@ describe('PresenceEngine', () => {
117
108
  },
118
109
  ]);
119
110
  listener.mockClear();
120
- yield presenceEngine.removePresence('presenceKey2');
111
+ presenceEngine.removePresence('presenceKey2');
121
112
  expect(listener).toHaveBeenCalledTimes(0);
122
- }));
123
- it('should throw an error if the presence does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
124
- yield expect(presenceEngine.removePresence(presenceKey)).rejects.toThrow(`PresenceEngine: Presence with key ${presenceKey} does not exist`);
125
- }));
126
- it('should not throw an error if the safe flag is true', () => __awaiter(void 0, void 0, void 0, function* () {
127
- yield expect(presenceEngine.removePresence(presenceKey, true)).resolves.not.toThrow();
128
- }));
113
+ });
114
+ it('should throw an error if the presence does not exist', () => {
115
+ expect(() => presenceEngine.removePresence(presenceKey)).toThrowError(`PresenceEngine: Presence with key ${presenceKey} does not exist`);
116
+ });
117
+ it('should not throw an error if the safe flag is true', () => {
118
+ expect(() => presenceEngine.removePresence(presenceKey, true)).not.toThrow();
119
+ });
129
120
  });
130
121
  describe('getPresence', () => {
131
- it('should return the presence', () => __awaiter(void 0, void 0, void 0, function* () {
132
- yield presenceEngine.trackPresence(presenceKey, presence);
122
+ it('should return the presence', () => {
123
+ presenceEngine.trackPresence(presenceKey, presence);
133
124
  const data = {};
134
125
  data[presenceKey] = presence;
135
- expect(yield presenceEngine.getPresence()).toEqual(data);
136
- }));
126
+ expect(presenceEngine.getPresence()).toEqual(data);
127
+ });
137
128
  });
138
129
  });
@@ -10,7 +10,7 @@ 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 _PondSocket_instances, _PondSocket_server, _PondSocket_pubSubEngine, _PondSocket_socketServer, _PondSocket_middleware, _PondSocket_manageHeartbeat, _PondSocket_init;
13
+ var _PondSocket_instances, _PondSocket_server, _PondSocket_socketServer, _PondSocket_middleware, _PondSocket_manageHeartbeat, _PondSocket_init;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.PondSocket = void 0;
16
16
  const http_1 = require("http");
@@ -20,26 +20,15 @@ const middleware_1 = require("../abstracts/middleware");
20
20
  const endpoint_1 = require("../endpoint/endpoint");
21
21
  const response_1 = require("../endpoint/response");
22
22
  const matcher_1 = require("../matcher/matcher");
23
- const pubSubEngine_1 = require("../pubSub/pubSubEngine");
24
23
  class PondSocket {
25
- constructor({ server, socketServer, db, redisUrl } = {}) {
24
+ constructor({ server, socketServer } = {}) {
26
25
  _PondSocket_instances.add(this);
27
26
  _PondSocket_server.set(this, void 0);
28
- _PondSocket_pubSubEngine.set(this, void 0);
29
27
  _PondSocket_socketServer.set(this, void 0);
30
28
  _PondSocket_middleware.set(this, void 0);
31
29
  __classPrivateFieldSet(this, _PondSocket_server, server !== null && server !== void 0 ? server : new http_1.Server(), "f");
32
30
  __classPrivateFieldSet(this, _PondSocket_socketServer, socketServer !== null && socketServer !== void 0 ? socketServer : new ws_1.WebSocketServer({ noServer: true }), "f");
33
31
  __classPrivateFieldSet(this, _PondSocket_middleware, new middleware_1.Middleware(), "f");
34
- if (redisUrl && db !== undefined) {
35
- __classPrivateFieldSet(this, _PondSocket_pubSubEngine, new pubSubEngine_1.PubSubEngine({
36
- redisUrl,
37
- db,
38
- }), "f");
39
- }
40
- else {
41
- __classPrivateFieldSet(this, _PondSocket_pubSubEngine, new pubSubEngine_1.PubSubEngine(), "f");
42
- }
43
32
  __classPrivateFieldGet(this, _PondSocket_instances, "m", _PondSocket_init).call(this);
44
33
  }
45
34
  /**
@@ -73,8 +62,7 @@ class PondSocket {
73
62
  * })
74
63
  */
75
64
  createEndpoint(path, handler) {
76
- const client = __classPrivateFieldGet(this, _PondSocket_pubSubEngine, "f").buildClient(path);
77
- const endpoint = new endpoint_1.EndpointEngine(this, client);
65
+ const endpoint = new endpoint_1.EndpointEngine(this);
78
66
  __classPrivateFieldGet(this, _PondSocket_middleware, "f").use((req, socket, next) => {
79
67
  const event = (0, matcher_1.parseAddress)(path, req.address);
80
68
  if (event) {
@@ -88,7 +76,7 @@ class PondSocket {
88
76
  }
89
77
  }
90
78
  exports.PondSocket = PondSocket;
91
- _PondSocket_server = new WeakMap(), _PondSocket_pubSubEngine = new WeakMap(), _PondSocket_socketServer = new WeakMap(), _PondSocket_middleware = new WeakMap(), _PondSocket_instances = new WeakSet(), _PondSocket_manageHeartbeat = function _PondSocket_manageHeartbeat() {
79
+ _PondSocket_server = new WeakMap(), _PondSocket_socketServer = new WeakMap(), _PondSocket_middleware = new WeakMap(), _PondSocket_instances = new WeakSet(), _PondSocket_manageHeartbeat = function _PondSocket_manageHeartbeat() {
92
80
  __classPrivateFieldGet(this, _PondSocket_socketServer, "f").on('connection', (socket) => {
93
81
  socket.on('pong', () => {
94
82
  socket.isAlive = true;
@@ -107,12 +95,10 @@ _PondSocket_server = new WeakMap(), _PondSocket_pubSubEngine = new WeakMap(), _P
107
95
  const timeout = __classPrivateFieldGet(this, _PondSocket_instances, "m", _PondSocket_manageHeartbeat).call(this);
108
96
  __classPrivateFieldGet(this, _PondSocket_server, "f").on('error', (error) => {
109
97
  clearInterval(timeout);
110
- __classPrivateFieldGet(this, _PondSocket_pubSubEngine, "f").close();
111
98
  throw new Error(error.message);
112
99
  });
113
100
  __classPrivateFieldGet(this, _PondSocket_server, "f").on('close', () => {
114
101
  clearInterval(timeout);
115
- __classPrivateFieldGet(this, _PondSocket_pubSubEngine, "f").close();
116
102
  });
117
103
  __classPrivateFieldGet(this, _PondSocket_server, "f").on('upgrade', (req, socket, head) => {
118
104
  const clientId = req.headers['sec-websocket-key'];
package/types.d.ts CHANGED
@@ -196,7 +196,7 @@ export declare class Channel<EventType extends PondEventMap = PondEventMap, Pres
196
196
  /**
197
197
  * @desc Gets the current presence data for the channel.
198
198
  */
199
- getPresences(): Promise<Record<string, PresenceType>>;
199
+ getPresences(): Record<string, PresenceType>;
200
200
 
201
201
  /**
202
202
  * @desc Gets the assign date for a specific user.
@@ -290,7 +290,7 @@ export declare class AbstractRequest<Path extends string, PresenceType extends P
290
290
 
291
291
  assigns: Record<string, AssignType>;
292
292
 
293
- getPresence(): Promise<Record<string, PresenceType>>;
293
+ getPresence(): Record<string, PresenceType>;
294
294
  }
295
295
 
296
296
  export declare class JoinRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
@@ -1,150 +0,0 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
23
- return (mod && mod.__esModule) ? mod : { "default": mod };
24
- };
25
- var _PubSubEngine_instances, _PubSubEngine_subscriber, _PubSubEngine_redis, _PubSubEngine_pubSub, _PubSubEngine_id, _PubSubEngine_expirationTime, _PubSubEngine_heartbeatInterval, _PubSubEngine_interval, _PubSubEngine_getPresence, _PubSubEngine_registerInstance, _PubSubEngine_getActiveInstances, _PubSubEngine_subscribeToChannel, _PubSubEngine_getPresenceFromInstance, _PubSubEngine_subscribeToPresence, _PubSubEngine_subscribe, _PubSubEngine_publish;
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.PubSubEngine = void 0;
28
- const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
29
- const ioredis_1 = __importDefault(require("ioredis"));
30
- class PubSubEngine {
31
- constructor(options = null) {
32
- _PubSubEngine_instances.add(this);
33
- _PubSubEngine_subscriber.set(this, void 0);
34
- _PubSubEngine_redis.set(this, void 0);
35
- _PubSubEngine_pubSub.set(this, void 0);
36
- _PubSubEngine_id.set(this, (0, pondsocket_common_1.uuid)());
37
- _PubSubEngine_expirationTime.set(this, 60);
38
- _PubSubEngine_heartbeatInterval.set(this, 5000);
39
- _PubSubEngine_interval.set(this, void 0);
40
- __classPrivateFieldSet(this, _PubSubEngine_subscriber, new pondsocket_common_1.Subject(), "f");
41
- __classPrivateFieldSet(this, _PubSubEngine_redis, options ? new ioredis_1.default(options.redisUrl, { db: options.db }) : null, "f");
42
- __classPrivateFieldSet(this, _PubSubEngine_pubSub, options ? new ioredis_1.default(options.redisUrl, { db: options.db }) : null, "f");
43
- __classPrivateFieldGet(this, _PubSubEngine_instances, "m", _PubSubEngine_subscribeToChannel).call(this);
44
- __classPrivateFieldSet(this, _PubSubEngine_interval, setInterval(() => __classPrivateFieldGet(this, _PubSubEngine_instances, "m", _PubSubEngine_registerInstance).call(this), __classPrivateFieldGet(this, _PubSubEngine_heartbeatInterval, "f")), "f");
45
- }
46
- /**
47
- * @desc Builds a client for the given endpoint
48
- * @param endpoint - the endpoint to build the client for
49
- */
50
- buildClient(endpoint) {
51
- const subscribeToPresence = __classPrivateFieldGet(this, _PubSubEngine_instances, "m", _PubSubEngine_subscribeToPresence).bind(this, endpoint);
52
- const getPresence = __classPrivateFieldGet(this, _PubSubEngine_instances, "m", _PubSubEngine_getPresence).bind(this, endpoint);
53
- const subscribe = __classPrivateFieldGet(this, _PubSubEngine_instances, "m", _PubSubEngine_subscribe).bind(this, endpoint);
54
- const publish = __classPrivateFieldGet(this, _PubSubEngine_instances, "m", _PubSubEngine_publish).bind(this, endpoint);
55
- const client = {
56
- getPresence,
57
- publish,
58
- subscribeToPresence,
59
- subscribe,
60
- };
61
- return client;
62
- }
63
- /**
64
- * @desc Closes the connection
65
- */
66
- close() {
67
- var _a, _b;
68
- clearInterval(__classPrivateFieldGet(this, _PubSubEngine_interval, "f"));
69
- (_a = __classPrivateFieldGet(this, _PubSubEngine_redis, "f")) === null || _a === void 0 ? void 0 : _a.quit();
70
- (_b = __classPrivateFieldGet(this, _PubSubEngine_pubSub, "f")) === null || _b === void 0 ? void 0 : _b.quit();
71
- }
72
- }
73
- exports.PubSubEngine = PubSubEngine;
74
- _PubSubEngine_subscriber = new WeakMap(), _PubSubEngine_redis = new WeakMap(), _PubSubEngine_pubSub = new WeakMap(), _PubSubEngine_id = new WeakMap(), _PubSubEngine_expirationTime = new WeakMap(), _PubSubEngine_heartbeatInterval = new WeakMap(), _PubSubEngine_interval = new WeakMap(), _PubSubEngine_instances = new WeakSet(), _PubSubEngine_getPresence = function _PubSubEngine_getPresence(endpoint, channel) {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- const instances = yield __classPrivateFieldGet(this, _PubSubEngine_instances, "m", _PubSubEngine_getActiveInstances).call(this);
77
- return Promise.all(instances.map((instance) => __classPrivateFieldGet(this, _PubSubEngine_instances, "m", _PubSubEngine_getPresenceFromInstance).call(this, endpoint, channel, instance)));
78
- });
79
- }, _PubSubEngine_registerInstance = function _PubSubEngine_registerInstance() {
80
- if (!__classPrivateFieldGet(this, _PubSubEngine_redis, "f")) {
81
- return;
82
- }
83
- __classPrivateFieldGet(this, _PubSubEngine_redis, "f").setex(`app:instance:${__classPrivateFieldGet(this, _PubSubEngine_id, "f")}`, __classPrivateFieldGet(this, _PubSubEngine_expirationTime, "f"), 'active');
84
- }, _PubSubEngine_getActiveInstances = function _PubSubEngine_getActiveInstances() {
85
- return __awaiter(this, void 0, void 0, function* () {
86
- if (!__classPrivateFieldGet(this, _PubSubEngine_redis, "f")) {
87
- return [];
88
- }
89
- const keys = yield __classPrivateFieldGet(this, _PubSubEngine_redis, "f").keys('app:instance:*');
90
- return keys.map((key) => key.replace('app:instance:', ''))
91
- .filter((key) => key !== __classPrivateFieldGet(this, _PubSubEngine_id, "f"));
92
- });
93
- }, _PubSubEngine_subscribeToChannel = function _PubSubEngine_subscribeToChannel() {
94
- if (!__classPrivateFieldGet(this, _PubSubEngine_pubSub, "f")) {
95
- return;
96
- }
97
- __classPrivateFieldGet(this, _PubSubEngine_pubSub, "f").subscribe('app:messages', (err) => {
98
- if (err) {
99
- console.error('Failed to subscribe: ', err);
100
- }
101
- });
102
- __classPrivateFieldGet(this, _PubSubEngine_pubSub, "f").on('message', (channel, message) => {
103
- const data = pondsocket_common_1.pubSubEventSchema.parse(JSON.parse(message));
104
- if (data.pubSubId === __classPrivateFieldGet(this, _PubSubEngine_id, "f")) {
105
- return;
106
- }
107
- __classPrivateFieldGet(this, _PubSubEngine_subscriber, "f").publish(data);
108
- });
109
- }, _PubSubEngine_getPresenceFromInstance = function _PubSubEngine_getPresenceFromInstance(endpoint, channel, instance) {
110
- return new Promise((resolve) => {
111
- const unsubscribe = __classPrivateFieldGet(this, _PubSubEngine_subscriber, "f").subscribe((data) => {
112
- if (data.endpoint === endpoint && data.channel === channel && data.event === pondsocket_common_1.PubSubEvents.PRESENCE && data.pubSubId === instance) {
113
- resolve(data.presence);
114
- unsubscribe();
115
- }
116
- });
117
- });
118
- }, _PubSubEngine_subscribeToPresence = function _PubSubEngine_subscribeToPresence(endpoint, channel, handler) {
119
- return __classPrivateFieldGet(this, _PubSubEngine_subscriber, "f").subscribe((data) => {
120
- var _a;
121
- if (data.endpoint === endpoint && data.channel === channel && data.event === pondsocket_common_1.PubSubEvents.GET_PRESENCE) {
122
- const presence = handler();
123
- const message = {
124
- event: pondsocket_common_1.PubSubEvents.PRESENCE,
125
- pubSubId: __classPrivateFieldGet(this, _PubSubEngine_id, "f"),
126
- endpoint,
127
- channel,
128
- presence,
129
- };
130
- (_a = __classPrivateFieldGet(this, _PubSubEngine_pubSub, "f")) === null || _a === void 0 ? void 0 : _a.publish('app:messages', JSON.stringify(message));
131
- }
132
- });
133
- }, _PubSubEngine_subscribe = function _PubSubEngine_subscribe(endpoint, channel, handler) {
134
- return __classPrivateFieldGet(this, _PubSubEngine_subscriber, "f").subscribe((data) => {
135
- if (data.endpoint === endpoint && data.channel === channel && data.event === pondsocket_common_1.PubSubEvents.MESSAGE) {
136
- handler(data.recipient, data.message);
137
- }
138
- });
139
- }, _PubSubEngine_publish = function _PubSubEngine_publish(endpoint, recipients, data) {
140
- var _a;
141
- const message = {
142
- event: pondsocket_common_1.PubSubEvents.MESSAGE,
143
- pubSubId: __classPrivateFieldGet(this, _PubSubEngine_id, "f"),
144
- endpoint,
145
- channel: data.channelName,
146
- message: data,
147
- recipient: recipients,
148
- };
149
- (_a = __classPrivateFieldGet(this, _PubSubEngine_redis, "f")) === null || _a === void 0 ? void 0 : _a.publish('app:messages', JSON.stringify(message));
150
- };