@eleven-am/pondsocket 0.1.43 → 0.1.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/enums.d.ts ADDED
@@ -0,0 +1,48 @@
1
+ export enum PresenceEventTypes {
2
+ JOIN = 'JOIN',
3
+ LEAVE = 'LEAVE',
4
+ UPDATE = 'UPDATE'
5
+ }
6
+
7
+ export enum ServerActions {
8
+ PRESENCE = 'PRESENCE',
9
+ SYSTEM = 'SYSTEM',
10
+ BROADCAST = 'BROADCAST',
11
+ ERROR = 'ERROR',
12
+ }
13
+
14
+ export enum ClientActions {
15
+ JOIN_CHANNEL = 'JOIN_CHANNEL',
16
+ LEAVE_CHANNEL = 'LEAVE_CHANNEL',
17
+ BROADCAST = 'BROADCAST',
18
+ }
19
+
20
+ export enum PondState {
21
+ CONNECTING = 'CONNECTING',
22
+ OPEN = 'OPEN',
23
+ CLOSING = 'CLOSING',
24
+ CLOSED = 'CLOSED',
25
+ }
26
+
27
+ export enum ChannelState {
28
+ IDLE = 'IDLE',
29
+ JOINING = 'JOINING',
30
+ JOINED = 'JOINED',
31
+ STALLED = 'STALLED',
32
+ CLOSED = 'CLOSED',
33
+ }
34
+
35
+ export enum ErrorTypes {
36
+ UNAUTHORIZED_JOIN_REQUEST = 'UNAUTHORIZED_JOIN_REQUEST',
37
+ UNAUTHORIZED_BROADCAST = 'UNAUTHORIZED_BROADCAST',
38
+ INVALID_MESSAGE = 'INVALID_MESSAGE',
39
+ HANDLER_NOT_FOUND = 'HANDLER_NOT_FOUND',
40
+ PRESENCE_LEAVE_FAILED = 'PRESENCE_LEAVE_FAILED',
41
+ INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR',
42
+ }
43
+
44
+ export enum SystemSender {
45
+ SERVER = 'SERVER',
46
+ ENDPOINT = 'ENDPOINT',
47
+ CHANNEL = 'CHANNEL',
48
+ }
package/enums.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PondState = exports.ClientActions = exports.ServerActions = exports.PresenceEventTypes = void 0;
3
+ exports.SystemSender = exports.ErrorTypes = exports.ChannelState = exports.PondState = exports.ClientActions = exports.ServerActions = exports.PresenceEventTypes = void 0;
4
4
  var PresenceEventTypes;
5
5
  (function (PresenceEventTypes) {
6
6
  PresenceEventTypes["JOIN"] = "JOIN";
@@ -27,3 +27,26 @@ var PondState;
27
27
  PondState["CLOSING"] = "CLOSING";
28
28
  PondState["CLOSED"] = "CLOSED";
29
29
  })(PondState = exports.PondState || (exports.PondState = {}));
30
+ var ChannelState;
31
+ (function (ChannelState) {
32
+ ChannelState["IDLE"] = "IDLE";
33
+ ChannelState["JOINING"] = "JOINING";
34
+ ChannelState["JOINED"] = "JOINED";
35
+ ChannelState["STALLED"] = "STALLED";
36
+ ChannelState["CLOSED"] = "CLOSED";
37
+ })(ChannelState = exports.ChannelState || (exports.ChannelState = {}));
38
+ var ErrorTypes;
39
+ (function (ErrorTypes) {
40
+ ErrorTypes["UNAUTHORIZED_JOIN_REQUEST"] = "UNAUTHORIZED_JOIN_REQUEST";
41
+ ErrorTypes["UNAUTHORIZED_BROADCAST"] = "UNAUTHORIZED_BROADCAST";
42
+ ErrorTypes["INVALID_MESSAGE"] = "INVALID_MESSAGE";
43
+ ErrorTypes["HANDLER_NOT_FOUND"] = "HANDLER_NOT_FOUND";
44
+ ErrorTypes["PRESENCE_LEAVE_FAILED"] = "PRESENCE_LEAVE_FAILED";
45
+ ErrorTypes["INTERNAL_SERVER_ERROR"] = "INTERNAL_SERVER_ERROR";
46
+ })(ErrorTypes = exports.ErrorTypes || (exports.ErrorTypes = {}));
47
+ var SystemSender;
48
+ (function (SystemSender) {
49
+ SystemSender["SERVER"] = "SERVER";
50
+ SystemSender["ENDPOINT"] = "ENDPOINT";
51
+ SystemSender["CHANNEL"] = "CHANNEL";
52
+ })(SystemSender = exports.SystemSender || (exports.SystemSender = {}));
package/node.js CHANGED
@@ -13,14 +13,14 @@ class PondClient extends client_1.default {
13
13
  connect(backoff = 1) {
14
14
  const socket = new WebSocket(this._address.toString());
15
15
  socket.onopen = () => {
16
- this._connectionState.publish(enums_1.PondState.OPEN);
16
+ this._connectionState.next(enums_1.PondState.OPEN);
17
17
  };
18
18
  socket.onmessage = (message) => {
19
19
  const data = JSON.parse(message.data);
20
- this._broadcaster.publish(data);
20
+ this._broadcaster.next(data);
21
21
  };
22
22
  socket.onerror = () => {
23
- this._connectionState.publish(enums_1.PondState.CLOSED);
23
+ this._connectionState.next(enums_1.PondState.CLOSED);
24
24
  setTimeout(() => {
25
25
  this.connect(backoff * 2);
26
26
  }, backoff * 1000);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.43",
3
+ "version": "0.1.46",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -21,8 +21,8 @@
21
21
  },
22
22
  "author": "Roy OSSAI",
23
23
  "license": "GPL-3.0",
24
- "main": "index.js",
25
- "types": "index.d.ts",
24
+ "main": "dist/index.js",
25
+ "types": "dist/index.d.ts",
26
26
  "repository": {
27
27
  "type": "git",
28
28
  "url": "git+https://github.com/Eleven-am/pondSocket.git"
@@ -14,6 +14,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.ChannelEngine = exports.ServerActions = void 0;
15
15
  const eventRequest_1 = require("./eventRequest");
16
16
  const eventResponse_1 = require("./eventResponse");
17
+ const enums_1 = require("../../enums");
17
18
  const presenceEngine_1 = require("../presence/presenceEngine");
18
19
  const subjectUtils_1 = require("../utils/subjectUtils");
19
20
  var ServerActions;
@@ -69,12 +70,12 @@ class ChannelEngine {
69
70
  * @param reason - The reason for kicking the user
70
71
  */
71
72
  kickUser(userId, reason) {
72
- this.sendMessage('channel', [userId], ServerActions.SYSTEM, 'kicked_out', {
73
+ this.sendMessage(enums_1.SystemSender.CHANNEL, [userId], ServerActions.SYSTEM, 'kicked_out', {
73
74
  message: 'You have been kicked out of the channel',
74
75
  reason,
75
76
  });
76
77
  this.removeUser(userId);
77
- this.sendMessage('channel', 'all_users', ServerActions.SYSTEM, 'kicked', {
78
+ this.sendMessage(enums_1.SystemSender.CHANNEL, 'all_users', ServerActions.SYSTEM, 'kicked', {
78
79
  userId,
79
80
  reason,
80
81
  });
@@ -84,7 +85,7 @@ class ChannelEngine {
84
85
  * @param reason - The reason for self-destructing the channel
85
86
  */
86
87
  destroy(reason) {
87
- this.sendMessage('channel', 'all_users', ServerActions.ERROR, 'destroyed', {
88
+ this.sendMessage(enums_1.SystemSender.CHANNEL, 'all_users', ServerActions.ERROR, 'destroyed', {
88
89
  message: 'Channel has been destroyed',
89
90
  reason,
90
91
  });
@@ -106,7 +107,7 @@ class ChannelEngine {
106
107
  }
107
108
  this._presenceEngine.trackPresence(userId, presence, (change) => {
108
109
  const { type } = change, rest = __rest(change, ["type"]);
109
- this.sendMessage('channel', [userId], ServerActions.PRESENCE, type, rest);
110
+ this.sendMessage(enums_1.SystemSender.CHANNEL, [userId], ServerActions.PRESENCE, type, rest);
110
111
  });
111
112
  }
112
113
  /**
@@ -198,7 +199,7 @@ class ChannelEngine {
198
199
  * @private
199
200
  */
200
201
  sendMessage(sender, recipient, action, event, payload) {
201
- if (!this._users.has(sender) && sender !== 'channel') {
202
+ if (!this._users.has(sender) && sender !== enums_1.SystemSender.CHANNEL) {
202
203
  throw new Error(`ChannelEngine: User with id ${sender} does not exist in channel ${this.name}`);
203
204
  }
204
205
  this._receiver.next({
@@ -228,7 +229,7 @@ class ChannelEngine {
228
229
  const request = new eventRequest_1.EventRequest(responseEvent, this);
229
230
  const response = new eventResponse_1.EventResponse(responseEvent, this);
230
231
  this._parentEngine.execute(request, response, () => {
231
- this.sendMessage('channel', [userId], ServerActions.ERROR, 'error_no_handler', {
232
+ this.sendMessage(enums_1.SystemSender.CHANNEL, [userId], ServerActions.ERROR, enums_1.ErrorTypes.HANDLER_NOT_FOUND, {
232
233
  message: 'A handler did not respond to the event',
233
234
  code: 404,
234
235
  });
@@ -260,7 +261,7 @@ class ChannelEngine {
260
261
  users = allUsers;
261
262
  break;
262
263
  case 'all_except_sender':
263
- if (sender === 'channel') {
264
+ if (sender === enums_1.SystemSender.CHANNEL) {
264
265
  throw new Error('ChannelEngine: Cannot send to all users except sender when sender is channel');
265
266
  }
266
267
  users = allUsers.filter((user) => user !== sender);
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createParentEngine = void 0;
4
4
  const channelEngine_1 = require("./channelEngine");
5
- const presenceEngine_1 = require("../presence/presenceEngine");
5
+ const enums_1 = require("../../enums");
6
6
  const createParentEngine = () => {
7
7
  const parentEngine = {
8
8
  destroyChannel: jest.fn(),
@@ -122,7 +122,7 @@ describe('ChannelEngine', () => {
122
122
  expect(onMessage).toHaveBeenCalledWith({
123
123
  channelName: 'test',
124
124
  action: channelEngine_1.ServerActions.PRESENCE,
125
- event: presenceEngine_1.PresenceEventTypes.JOIN,
125
+ event: enums_1.PresenceEventTypes.JOIN,
126
126
  payload: {
127
127
  presence: [{ test: 2 }],
128
128
  changed: {
@@ -208,7 +208,7 @@ describe('ChannelEngine', () => {
208
208
  });
209
209
  expect(onMessage).toHaveBeenCalledWith({
210
210
  channelName: 'test',
211
- event: presenceEngine_1.PresenceEventTypes.LEAVE,
211
+ event: enums_1.PresenceEventTypes.LEAVE,
212
212
  action: channelEngine_1.ServerActions.PRESENCE,
213
213
  payload: {
214
214
  presence: [{ test: 2 }],
@@ -290,7 +290,7 @@ describe('ChannelEngine', () => {
290
290
  const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
291
291
  channelEngine.addUser('test', { test: 1 }, onMessage);
292
292
  channelEngine.addUser('test2', { test: 1 }, onMessage);
293
- channelEngine.sendMessage('channel', 'all_users', channelEngine_1.ServerActions.BROADCAST, 'test', { test: 2 });
293
+ channelEngine.sendMessage(enums_1.SystemSender.CHANNEL, 'all_users', channelEngine_1.ServerActions.BROADCAST, 'test', { test: 2 });
294
294
  expect(onMessage.mock.calls[0][0]).toStrictEqual({
295
295
  action: channelEngine_1.ServerActions.BROADCAST,
296
296
  channelName: 'test',
@@ -334,7 +334,7 @@ describe('ChannelEngine', () => {
334
334
  }).toThrow('ChannelEngine: User with id test3 does not exist in channel test');
335
335
  // when sender is channel itself it throws an error
336
336
  expect(() => {
337
- channelEngine.sendMessage('channel', 'all_except_sender', channelEngine_1.ServerActions.BROADCAST, 'test', { test: 3 });
337
+ channelEngine.sendMessage(enums_1.SystemSender.CHANNEL, 'all_except_sender', channelEngine_1.ServerActions.BROADCAST, 'test', { test: 3 });
338
338
  }).toThrow('ChannelEngine: Cannot send to all users except sender when sender is channel');
339
339
  });
340
340
  it('should broadcast a message to specific users', () => {
@@ -4,6 +4,7 @@ exports.createChannelEvent = exports.createChannelEngine = void 0;
4
4
  const channelEngine_1 = require("./channelEngine");
5
5
  const channelEngine_test_1 = require("./channelEngine.test");
6
6
  const eventResponse_1 = require("./eventResponse");
7
+ const enums_1 = require("../../enums");
7
8
  const createChannelEngine = () => {
8
9
  const parentEngine = (0, channelEngine_test_1.createParentEngine)();
9
10
  return new channelEngine_1.ChannelEngine('test', parentEngine);
@@ -26,10 +27,13 @@ const createChannelResponse = () => {
26
27
  const channelEngine = (0, exports.createChannelEngine)();
27
28
  const event = (0, exports.createChannelEvent)();
28
29
  channelEngine.addUser(event.sender, { assign: 'assign' }, () => { });
30
+ channelEngine.addUser(event.recipients[0], { assign: 'assign' }, () => { });
29
31
  const response = new eventResponse_1.EventResponse(event, channelEngine);
30
- return { channelEngine,
32
+ return {
33
+ channelEngine,
31
34
  event,
32
- response };
35
+ response,
36
+ };
33
37
  };
34
38
  describe('ChannelResponse', () => {
35
39
  it('should create a new ChannelResponse', () => {
@@ -50,7 +54,7 @@ describe('ChannelResponse', () => {
50
54
  jest.spyOn(channelEngine, 'sendMessage');
51
55
  response.reject();
52
56
  expect(response.responseSent).toEqual(true);
53
- expect(channelEngine.sendMessage).toHaveBeenCalledWith('channel', [event.sender], channelEngine_1.ServerActions.ERROR, 'error_channel', { message: 'Unauthorized request',
57
+ expect(channelEngine.sendMessage).toHaveBeenCalledWith(enums_1.SystemSender.CHANNEL, [event.sender], channelEngine_1.ServerActions.ERROR, enums_1.ErrorTypes.UNAUTHORIZED_BROADCAST, { message: 'Unauthorized request',
54
58
  code: 403 });
55
59
  });
56
60
  it('should send a direct message', () => {
@@ -58,7 +62,7 @@ describe('ChannelResponse', () => {
58
62
  jest.spyOn(channelEngine, 'sendMessage');
59
63
  response.send('event', { payload: 'payload' });
60
64
  expect(response.responseSent).toEqual(true);
61
- expect(channelEngine.sendMessage).toHaveBeenCalledWith('channel', [event.sender], channelEngine_1.ServerActions.SYSTEM, 'event', { payload: 'payload' });
65
+ expect(channelEngine.sendMessage).toHaveBeenCalledWith(enums_1.SystemSender.CHANNEL, [event.sender], channelEngine_1.ServerActions.SYSTEM, 'event', { payload: 'payload' });
62
66
  });
63
67
  it('should broadcast a message', () => {
64
68
  const { response, channelEngine } = createChannelResponse();
@@ -75,10 +79,18 @@ describe('ChannelResponse', () => {
75
79
  it('should sendToUsers a message', () => {
76
80
  const { response, channelEngine } = createChannelResponse();
77
81
  jest.spyOn(channelEngine, 'sendMessage');
78
- channelEngine.addUser('recipient', { assign: 'assign' }, () => { });
79
82
  response.sendToUsers('event', { payload: 'payload' }, ['recipient']);
80
83
  expect(channelEngine.sendMessage).toHaveBeenCalledWith('sender', ['recipient'], channelEngine_1.ServerActions.BROADCAST, 'event', { payload: 'payload' });
81
84
  });
85
+ it('should fail to send to non existing users', () => {
86
+ const { event, response, channelEngine } = createChannelResponse();
87
+ jest.spyOn(channelEngine, 'sendMessage');
88
+ event.recipients = ['non_existing_user'];
89
+ expect(() => response.accept()).toThrowError(new Error('ChannelEngine: Users non_existing_user are not in channel test'));
90
+ expect(channelEngine.sendMessage).toHaveBeenCalledWith(event.sender, ['non_existing_user'], channelEngine_1.ServerActions.BROADCAST, event.event, event.payload);
91
+ expect(() => response.sendToUsers('event', { payload: 'payload' }, ['non_existing_user']))
92
+ .toThrowError(new Error('ChannelEngine: Users non_existing_user are not in channel test'));
93
+ });
82
94
  it('should track a trackPresence', () => {
83
95
  const { response, channelEngine } = createChannelResponse();
84
96
  jest.spyOn(channelEngine, 'trackPresence');
@@ -99,7 +111,7 @@ describe('ChannelResponse', () => {
99
111
  response.trackPresence({ status: 'online' });
100
112
  response.unTrackPresence();
101
113
  response.unTrackPresence();
102
- expect(channelEngine.sendMessage).toHaveBeenCalledWith('channel', ['sender'], channelEngine_1.ServerActions.ERROR, 'error_channel', {
114
+ expect(channelEngine.sendMessage).toHaveBeenCalledWith(enums_1.SystemSender.CHANNEL, ['sender'], channelEngine_1.ServerActions.ERROR, enums_1.ErrorTypes.PRESENCE_LEAVE_FAILED, {
103
115
  message: 'PresenceEngine: Presence with key sender does not exist',
104
116
  code: 500,
105
117
  });
@@ -131,4 +143,22 @@ describe('ChannelResponse', () => {
131
143
  expect(channelEngine.destroy).toHaveBeenCalledWith('recipient');
132
144
  expect(response.responseSent).toEqual(true);
133
145
  });
146
+ it('should set hasResponseSent to true when calling send, accept, reject, end', () => {
147
+ const { response } = createChannelResponse();
148
+ expect(response.responseSent).toEqual(false);
149
+ response.send('event', { payload: 'payload' });
150
+ expect(response.responseSent).toEqual(true);
151
+ const { response: response2 } = createChannelResponse();
152
+ expect(response2.responseSent).toEqual(false);
153
+ response2.accept();
154
+ expect(response2.responseSent).toEqual(true);
155
+ const { response: response3 } = createChannelResponse();
156
+ expect(response3.responseSent).toEqual(false);
157
+ response3.reject();
158
+ expect(response3.responseSent).toEqual(true);
159
+ const { response: response4 } = createChannelResponse();
160
+ expect(response4.responseSent).toEqual(false);
161
+ response4.end();
162
+ expect(response4.responseSent).toEqual(true);
163
+ });
134
164
  });
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EventResponse = void 0;
4
4
  const channelEngine_1 = require("./channelEngine");
5
+ const enums_1 = require("../../enums");
5
6
  const abstractResponse_1 = require("../abstracts/abstractResponse");
6
7
  class EventResponse extends abstractResponse_1.PondResponse {
7
8
  constructor(event, engine) {
@@ -22,7 +23,7 @@ class EventResponse extends abstractResponse_1.PondResponse {
22
23
  */
23
24
  accept(assigns) {
24
25
  this._manageAssigns(assigns);
25
- this._engine.sendMessage('channel', [this._event.sender], this._event.action, this._event.event, this._event.payload);
26
+ this._engine.sendMessage(this._event.sender, this._event.recipients, this._event.action, this._event.event, this._event.payload);
26
27
  this._hasExecuted = true;
27
28
  return this;
28
29
  }
@@ -35,8 +36,10 @@ class EventResponse extends abstractResponse_1.PondResponse {
35
36
  reject(message, errorCode, assigns) {
36
37
  this._manageAssigns(assigns);
37
38
  const text = message || 'Unauthorized request';
38
- this._engine.sendMessage('channel', [this._event.sender], channelEngine_1.ServerActions.ERROR, 'error_channel', { message: text,
39
- code: errorCode || 403 });
39
+ this._engine.sendMessage(enums_1.SystemSender.CHANNEL, [this._event.sender], channelEngine_1.ServerActions.ERROR, enums_1.ErrorTypes.UNAUTHORIZED_BROADCAST, {
40
+ message: text,
41
+ code: errorCode || 403,
42
+ });
40
43
  this._hasExecuted = true;
41
44
  return this;
42
45
  }
@@ -47,7 +50,7 @@ class EventResponse extends abstractResponse_1.PondResponse {
47
50
  * @param assigns - the data to assign to the client
48
51
  */
49
52
  send(event, payload, assigns) {
50
- this._engine.sendMessage('channel', [this._event.sender], channelEngine_1.ServerActions.SYSTEM, event, payload);
53
+ this._engine.sendMessage(enums_1.SystemSender.CHANNEL, [this._event.sender], channelEngine_1.ServerActions.SYSTEM, event, payload);
51
54
  return this.accept(assigns);
52
55
  }
53
56
  /**
@@ -106,8 +109,10 @@ class EventResponse extends abstractResponse_1.PondResponse {
106
109
  this._engine.unTrackPresence(userId);
107
110
  }
108
111
  catch (e) {
109
- this._engine.sendMessage('channel', [userId], channelEngine_1.ServerActions.ERROR, 'error_channel', { message: e.message,
110
- code: 500 });
112
+ this._engine.sendMessage(enums_1.SystemSender.CHANNEL, [userId], channelEngine_1.ServerActions.ERROR, enums_1.ErrorTypes.PRESENCE_LEAVE_FAILED, {
113
+ message: e.message,
114
+ code: 500,
115
+ });
111
116
  }
112
117
  return this;
113
118
  }
@@ -1,15 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Endpoint = exports.ClientActions = void 0;
3
+ exports.Endpoint = void 0;
4
4
  const connectionResponse_1 = require("./connectionResponse");
5
+ const enums_1 = require("../../enums");
5
6
  const channelEngine_1 = require("../channel/channelEngine");
6
7
  const matchPattern_1 = require("../utils/matchPattern");
7
- var ClientActions;
8
- (function (ClientActions) {
9
- ClientActions["JOIN_CHANNEL"] = "JOIN_CHANNEL";
10
- ClientActions["LEAVE_CHANNEL"] = "LEAVE_CHANNEL";
11
- ClientActions["BROADCAST"] = "BROADCAST";
12
- })(ClientActions = exports.ClientActions || (exports.ClientActions = {}));
13
8
  class Endpoint {
14
9
  constructor(server) {
15
10
  this._server = server;
@@ -53,7 +48,7 @@ class Endpoint {
53
48
  event,
54
49
  payload,
55
50
  action: channelEngine_1.ServerActions.BROADCAST,
56
- channelName: 'SERVER',
51
+ channelName: enums_1.SystemSender.ENDPOINT,
57
52
  };
58
53
  this._sendMessage(socket, message);
59
54
  });
@@ -99,7 +94,7 @@ class Endpoint {
99
94
  if (data.message) {
100
95
  const newMessage = {
101
96
  event: data.message.event,
102
- channelName: 'SERVER',
97
+ channelName: enums_1.SystemSender.ENDPOINT,
103
98
  payload: data.message.payload,
104
99
  action: channelEngine_1.ServerActions.SYSTEM,
105
100
  };
@@ -176,15 +171,15 @@ class Endpoint {
176
171
  */
177
172
  _handleMessage(cache, message) {
178
173
  switch (message.action) {
179
- case ClientActions.JOIN_CHANNEL:
174
+ case enums_1.ClientActions.JOIN_CHANNEL:
180
175
  this._joinChannel(message.channelName, cache, message.payload);
181
176
  break;
182
- case ClientActions.LEAVE_CHANNEL:
177
+ case enums_1.ClientActions.LEAVE_CHANNEL:
183
178
  this._execute(message.channelName, (channel) => {
184
179
  channel.removeUser(cache.clientId);
185
180
  });
186
181
  break;
187
- case ClientActions.BROADCAST:
182
+ case enums_1.ClientActions.BROADCAST:
188
183
  this._execute(message.channelName, (channel) => {
189
184
  channel.broadcastMessage(cache.clientId, message);
190
185
  });
@@ -201,9 +196,9 @@ class Endpoint {
201
196
  */
202
197
  _readMessage(cache, message) {
203
198
  const errorMessage = {
204
- event: 'error',
199
+ event: enums_1.ErrorTypes.INVALID_MESSAGE,
205
200
  action: channelEngine_1.ServerActions.ERROR,
206
- channelName: 'ENDPOINT',
201
+ channelName: enums_1.SystemSender.ENDPOINT,
207
202
  payload: {},
208
203
  };
209
204
  try {
@@ -238,6 +233,7 @@ class Endpoint {
238
233
  this._sendMessage(cache.socket, errorMessage);
239
234
  }
240
235
  else if (e instanceof Error) {
236
+ errorMessage.event = enums_1.ErrorTypes.INTERNAL_SERVER_ERROR;
241
237
  errorMessage.payload = {
242
238
  message: e.message,
243
239
  };
@@ -13,14 +13,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const superwstest_1 = __importDefault(require("superwstest"));
16
- const endpoint_1 = require("./endpoint");
17
16
  const enums_1 = require("../../enums");
18
17
  const pondChannel_1 = require("../pondChannel/pondChannel");
19
18
  const pondSocket_1 = require("../server/pondSocket");
20
19
  const createPondSocket = () => {
21
20
  const mock = jest.fn();
22
21
  const socket = new pondSocket_1.PondSocket();
23
- const server = socket.listen(3001, mock);
22
+ // GET RANDOM PORT FROM 6000 - 50000
23
+ const port = Math.floor(Math.random() * (50000 - 6000 + 1) + 6000);
24
+ const server = socket.listen(port, mock);
24
25
  const createPondChannel = () => new pondChannel_1.PondChannel();
25
26
  return { socket,
26
27
  server,
@@ -93,7 +94,7 @@ describe('endpoint', () => {
93
94
  .expectUpgrade((res) => expect(res.statusCode).toBe(101))
94
95
  .expectJson({
95
96
  event: 'Hello',
96
- channelName: 'SERVER',
97
+ channelName: enums_1.SystemSender.ENDPOINT,
97
98
  action: enums_1.ServerActions.SYSTEM,
98
99
  payload: {
99
100
  room: 'socket',
@@ -101,7 +102,7 @@ describe('endpoint', () => {
101
102
  })
102
103
  .expectJson({
103
104
  event: 'TEST',
104
- channelName: 'SERVER',
105
+ channelName: enums_1.SystemSender.ENDPOINT,
105
106
  action: enums_1.ServerActions.BROADCAST,
106
107
  payload: {
107
108
  message: 'Hello everyone',
@@ -114,7 +115,7 @@ describe('endpoint', () => {
114
115
  .expectUpgrade((res) => expect(res.statusCode).toBe(101))
115
116
  .expectJson({
116
117
  event: 'Hello',
117
- channelName: 'SERVER',
118
+ channelName: enums_1.SystemSender.ENDPOINT,
118
119
  action: enums_1.ServerActions.SYSTEM,
119
120
  payload: {
120
121
  room: 'secondSocket',
@@ -122,7 +123,7 @@ describe('endpoint', () => {
122
123
  })
123
124
  .expectJson({
124
125
  event: 'TEST',
125
- channelName: 'SERVER',
126
+ channelName: enums_1.SystemSender.ENDPOINT,
126
127
  action: enums_1.ServerActions.BROADCAST,
127
128
  payload: {
128
129
  message: 'Hello everyone',
@@ -134,7 +135,7 @@ describe('endpoint', () => {
134
135
  }));
135
136
  it('should be able to accept connections on this handler', () => __awaiter(void 0, void 0, void 0, function* () {
136
137
  const message = {
137
- action: endpoint_1.ClientActions.JOIN_CHANNEL,
138
+ action: enums_1.ClientActions.JOIN_CHANNEL,
138
139
  channelName: '/test/socket',
139
140
  event: 'TEST',
140
141
  payload: {},
@@ -196,7 +197,7 @@ describe('endpoint', () => {
196
197
  });
197
198
  endpoint.addChannel('/test/:room', testPond);
198
199
  const message = {
199
- action: endpoint_1.ClientActions.JOIN_CHANNEL,
200
+ action: enums_1.ClientActions.JOIN_CHANNEL,
200
201
  channelName: '/test/socket',
201
202
  event: 'TEST',
202
203
  payload: {},
@@ -212,8 +213,8 @@ describe('endpoint', () => {
212
213
  .expectUpgrade((res) => expect(res.statusCode).toBe(101))
213
214
  .sendJson(Object.assign(Object.assign({}, message), { channelName: '/socket/socket' }))
214
215
  .expectJson({
215
- event: 'error',
216
- channelName: 'ENDPOINT',
216
+ event: enums_1.ErrorTypes.INTERNAL_SERVER_ERROR,
217
+ channelName: enums_1.SystemSender.ENDPOINT,
217
218
  action: enums_1.ServerActions.ERROR,
218
219
  payload: {
219
220
  message: 'GatewayEngine: Channel /socket/socket does not exist',
@@ -233,7 +234,8 @@ describe('endpoint', () => {
233
234
  const channel = createPondChannel();
234
235
  channel.onEvent(':room', (req, res) => {
235
236
  if (req.event.params.room === 'TEST') {
236
- res.accept().broadcast(req.event.event, req.event.payload);
237
+ res.accept()
238
+ .broadcast(req.event.event, req.event.payload);
237
239
  }
238
240
  else if (req.event.params.room === 'TEST2') {
239
241
  res.reject();
@@ -247,7 +249,7 @@ describe('endpoint', () => {
247
249
  });
248
250
  endpoint.addChannel('/test/:room', channel);
249
251
  const message = {
250
- action: endpoint_1.ClientActions.JOIN_CHANNEL,
252
+ action: enums_1.ClientActions.JOIN_CHANNEL,
251
253
  channelName: '/test/socket',
252
254
  event: 'TEST',
253
255
  payload: {},
@@ -255,33 +257,39 @@ describe('endpoint', () => {
255
257
  yield (0, superwstest_1.default)(server)
256
258
  .ws('/api/socket')
257
259
  .expectUpgrade((res) => expect(res.statusCode).toBe(101))
258
- .sendJson(message)
259
- .sendJson(Object.assign(Object.assign({}, message), { event: 'TEST2', action: endpoint_1.ClientActions.BROADCAST }))
260
+ .sendJson(message) // Join the channel
261
+ .sendJson(Object.assign(Object.assign({}, message), { channelName: '/test/socket', action: enums_1.ClientActions.BROADCAST }))
260
262
  .expectJson({
261
- event: 'error_channel',
263
+ payload: {},
264
+ event: 'TEST',
262
265
  channelName: '/test/socket',
263
- action: enums_1.ServerActions.ERROR,
264
- payload: {
265
- message: 'Unauthorized request',
266
- code: 403,
267
- },
266
+ action: enums_1.ServerActions.BROADCAST,
268
267
  })
269
- .sendJson(Object.assign(Object.assign({}, message), { channelName: '/test/socket', action: endpoint_1.ClientActions.BROADCAST }))
270
268
  .expectJson({
271
269
  payload: {},
272
270
  event: 'TEST',
273
271
  channelName: '/test/socket',
274
272
  action: enums_1.ServerActions.BROADCAST,
275
273
  })
276
- .sendJson(Object.assign(Object.assign({}, message), { event: 'TEST3', action: endpoint_1.ClientActions.BROADCAST }))
274
+ .sendJson(Object.assign(Object.assign({}, message), { event: 'TEST2', channelName: '/test/socket', action: enums_1.ClientActions.BROADCAST }))
277
275
  .expectJson({
278
- event: 'error_channel',
276
+ action: enums_1.ServerActions.ERROR,
277
+ event: enums_1.ErrorTypes.UNAUTHORIZED_BROADCAST,
278
+ payload: {
279
+ message: 'Unauthorized request',
280
+ code: 403,
281
+ },
279
282
  channelName: '/test/socket',
283
+ })
284
+ .sendJson(Object.assign(Object.assign({}, message), { event: 'TEST3', channelName: '/test/socket', action: enums_1.ClientActions.BROADCAST }))
285
+ .expectJson({
280
286
  action: enums_1.ServerActions.ERROR,
287
+ event: enums_1.ErrorTypes.UNAUTHORIZED_BROADCAST,
281
288
  payload: {
282
289
  message: 'choke on my balls',
283
290
  code: 403,
284
291
  },
292
+ channelName: '/test/socket',
285
293
  })
286
294
  .close()
287
295
  .expectClosed();
@@ -302,7 +310,7 @@ describe('endpoint', () => {
302
310
  });
303
311
  endpoint.addChannel('/test/:room', channel);
304
312
  const message = {
305
- action: endpoint_1.ClientActions.JOIN_CHANNEL,
313
+ action: enums_1.ClientActions.JOIN_CHANNEL,
306
314
  channelName: '/test/socket',
307
315
  event: 'TEST',
308
316
  payload: {},
@@ -343,7 +351,7 @@ describe('endpoint', () => {
343
351
  });
344
352
  endpoint.addChannel('/test/:room', channel);
345
353
  const message = {
346
- action: endpoint_1.ClientActions.LEAVE_CHANNEL,
354
+ action: enums_1.ClientActions.LEAVE_CHANNEL,
347
355
  channelName: '/test/socket',
348
356
  event: 'TEST',
349
357
  payload: {},
@@ -353,7 +361,7 @@ describe('endpoint', () => {
353
361
  .expectUpgrade((res) => expect(res.statusCode).toBe(101))
354
362
  .sendJson(message)
355
363
  .expectJson({
356
- event: 'error',
364
+ event: enums_1.ErrorTypes.INTERNAL_SERVER_ERROR,
357
365
  channelName: 'ENDPOINT',
358
366
  action: enums_1.ServerActions.ERROR,
359
367
  payload: {
@@ -361,7 +369,7 @@ describe('endpoint', () => {
361
369
  },
362
370
  })
363
371
  // now join the channel
364
- .sendJson(Object.assign(Object.assign({}, message), { action: endpoint_1.ClientActions.JOIN_CHANNEL }))
372
+ .sendJson(Object.assign(Object.assign({}, message), { action: enums_1.ClientActions.JOIN_CHANNEL }))
365
373
  .expectJson({
366
374
  event: 'TEST',
367
375
  channelName: '/test/socket',
@@ -396,7 +404,7 @@ describe('endpoint', () => {
396
404
  .expectUpgrade((res) => expect(res.statusCode).toBe(101))
397
405
  .expectJson({
398
406
  event: 'TEST',
399
- channelName: 'SERVER',
407
+ channelName: enums_1.SystemSender.ENDPOINT,
400
408
  action: enums_1.ServerActions.SYSTEM,
401
409
  payload: {
402
410
  test: 'test',
@@ -408,12 +416,13 @@ describe('endpoint', () => {
408
416
  .expectUpgrade((res) => expect(res.statusCode).toBe(101))
409
417
  .expectJson({
410
418
  event: 'TEST',
411
- channelName: 'SERVER',
419
+ channelName: enums_1.SystemSender.ENDPOINT,
412
420
  action: enums_1.ServerActions.SYSTEM,
413
421
  payload: {
414
422
  test: 'test',
415
423
  },
416
424
  })
417
425
  .expectClosed();
426
+ server.close();
418
427
  }));
419
428
  });