@eleven-am/pondsocket 0.1.43 → 0.1.45

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.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.JoinResponse = void 0;
4
+ const enums_1 = require("../../enums");
4
5
  const abstractResponse_1 = require("../abstracts/abstractResponse");
5
6
  const channelEngine_1 = require("../channel/channelEngine");
6
7
  class JoinResponse extends abstractResponse_1.PondResponse {
@@ -34,7 +35,7 @@ class JoinResponse extends abstractResponse_1.PondResponse {
34
35
  this._hasExecuted = true;
35
36
  const text = `Request to join channel ${this._engine.name} rejected: ${message || 'Unauthorized request'}`;
36
37
  const errorMessage = {
37
- event: 'POND_ERROR',
38
+ event: enums_1.ErrorTypes.UNAUTHORIZED_JOIN_REQUEST,
38
39
  payload: {
39
40
  message: text,
40
41
  code: errorCode || 403,
@@ -53,7 +54,7 @@ class JoinResponse extends abstractResponse_1.PondResponse {
53
54
  */
54
55
  send(event, payload, assigns) {
55
56
  this.accept(assigns);
56
- this._engine.sendMessage('channel', [this._user.clientId], channelEngine_1.ServerActions.SYSTEM, event, payload);
57
+ this._engine.sendMessage(enums_1.SystemSender.CHANNEL, [this._user.clientId], channelEngine_1.ServerActions.SYSTEM, event, payload);
57
58
  return this;
58
59
  }
59
60
  /**
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const joinResponse_1 = require("./joinResponse");
4
+ const enums_1 = require("../../enums");
4
5
  const channelEngine_1 = require("../channel/channelEngine");
5
6
  const channelResponse_test_1 = require("../channel/channelResponse.test");
6
7
  const createPondResponse = () => {
@@ -49,7 +50,7 @@ describe('pondChannelResponse', () => {
49
50
  expect(channelEngine.getUserData(socket.clientId)).toBeUndefined();
50
51
  // also check if the socket was sent a message
51
52
  expect(socket.socket.send).toHaveBeenCalledWith(JSON.stringify({
52
- event: 'POND_ERROR',
53
+ event: enums_1.ErrorTypes.UNAUTHORIZED_JOIN_REQUEST,
53
54
  payload: {
54
55
  message: 'Request to join channel test rejected: Unauthorized request',
55
56
  code: 403,
@@ -1,13 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PresenceEngine = exports.PresenceEventTypes = void 0;
3
+ exports.PresenceEngine = void 0;
4
+ const enums_1 = require("../../enums");
4
5
  const subjectUtils_1 = require("../utils/subjectUtils");
5
- var PresenceEventTypes;
6
- (function (PresenceEventTypes) {
7
- PresenceEventTypes["JOIN"] = "JOIN";
8
- PresenceEventTypes["LEAVE"] = "LEAVE";
9
- PresenceEventTypes["UPDATE"] = "UPDATE";
10
- })(PresenceEventTypes = exports.PresenceEventTypes || (exports.PresenceEventTypes = {}));
11
6
  class PresenceEngine {
12
7
  constructor() {
13
8
  this._presence = new subjectUtils_1.BehaviorSubject();
@@ -41,7 +36,7 @@ class PresenceEngine {
41
36
  this._presenceMap.delete(presenceKey);
42
37
  if (this._presenceMap.size > 0) {
43
38
  this._presence.next({
44
- type: PresenceEventTypes.LEAVE,
39
+ type: enums_1.PresenceEventTypes.LEAVE,
45
40
  changed: presence,
46
41
  presence: Array.from(this._presenceMap.values()),
47
42
  });
@@ -61,7 +56,7 @@ class PresenceEngine {
61
56
  if (oldPresence) {
62
57
  this._presenceMap.set(presenceKey, presence);
63
58
  this._presence.next({
64
- type: PresenceEventTypes.UPDATE,
59
+ type: enums_1.PresenceEventTypes.UPDATE,
65
60
  changed: Object.assign(Object.assign({}, oldPresence), presence),
66
61
  presence: Array.from(this._presenceMap.values()),
67
62
  });
@@ -90,7 +85,7 @@ class PresenceEngine {
90
85
  if (!this._presenceMap.has(presenceKey)) {
91
86
  this._presenceMap.set(presenceKey, presence);
92
87
  this._presence.next({
93
- type: PresenceEventTypes.JOIN,
88
+ type: enums_1.PresenceEventTypes.JOIN,
94
89
  changed: presence,
95
90
  presence: Array.from(this._presenceMap.values()),
96
91
  });
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const presenceEngine_1 = require("./presenceEngine");
4
+ const enums_1 = require("../../enums");
4
5
  /* eslint-disable @typescript-eslint/ban-ts-comment */
5
6
  describe('PresenceEngine', () => {
6
7
  let presenceEngine;
@@ -28,7 +29,7 @@ describe('PresenceEngine', () => {
28
29
  // @ts-ignore
29
30
  expect(presenceEngine._insertPresence).toHaveBeenCalledWith(presenceKey, presence);
30
31
  expect(onPresenceChange).toHaveBeenCalledWith({
31
- type: presenceEngine_1.PresenceEventTypes.JOIN,
32
+ type: enums_1.PresenceEventTypes.JOIN,
32
33
  changed: presence,
33
34
  presence: [presence],
34
35
  });
@@ -41,7 +42,7 @@ describe('PresenceEngine', () => {
41
42
  // @ts-ignore
42
43
  expect(presenceEngine._subscribe).toHaveBeenCalledWith(presenceKey, onPresenceChange);
43
44
  expect(onPresenceChange).toHaveBeenCalledWith({
44
- type: presenceEngine_1.PresenceEventTypes.JOIN,
45
+ type: enums_1.PresenceEventTypes.JOIN,
45
46
  changed: presence,
46
47
  presence: [presence],
47
48
  });
@@ -63,7 +64,7 @@ describe('PresenceEngine', () => {
63
64
  };
64
65
  presenceEngine.updatePresence(presenceKey, newPresence);
65
66
  expect(onPresenceChange).toHaveBeenCalledWith({
66
- type: presenceEngine_1.PresenceEventTypes.UPDATE,
67
+ type: enums_1.PresenceEventTypes.UPDATE,
67
68
  changed: Object.assign(Object.assign({}, presence), newPresence),
68
69
  presence: [newPresence],
69
70
  });
@@ -78,7 +79,7 @@ describe('PresenceEngine', () => {
78
79
  presenceEngine.trackPresence('presenceKey2', Object.assign(Object.assign({}, presence), { key: 'presence2' }), onPresenceChange);
79
80
  presenceEngine.removePresence(presenceKey);
80
81
  expect(onPresenceChange).toHaveBeenCalledWith({
81
- type: presenceEngine_1.PresenceEventTypes.LEAVE,
82
+ type: enums_1.PresenceEventTypes.LEAVE,
82
83
  changed: presence,
83
84
  presence: [
84
85
  Object.assign(Object.assign({}, presence), { key: 'presence2' }),
@@ -16,6 +16,7 @@ const http_1 = require("http");
16
16
  const superwstest_1 = __importDefault(require("superwstest"));
17
17
  const ws_1 = require("ws");
18
18
  const pondSocket_1 = require("./pondSocket");
19
+ const enums_1 = require("../../enums");
19
20
  const channelEngine_1 = require("../channel/channelEngine");
20
21
  const endpoint_1 = require("../endpoint/endpoint");
21
22
  describe('server', () => {
@@ -109,7 +110,7 @@ describe('server', () => {
109
110
  .expectUpgrade((res) => expect(res.statusCode).toBe(101))
110
111
  .expectJson({
111
112
  event: 'testEvent',
112
- channelName: 'SERVER',
113
+ channelName: enums_1.SystemSender.ENDPOINT,
113
114
  payload: { test: 'test' },
114
115
  action: channelEngine_1.ServerActions.SYSTEM,
115
116
  })
@@ -61,4 +61,16 @@ describe('BaseClass', () => {
61
61
  const unMatchingString = 'pondocket2hello?test=5&test2=6';
62
62
  expect(baseClass.parseEvent(pattern, unMatchingString)).toEqual(null);
63
63
  });
64
+ it('should match any string if the path is *', () => {
65
+ const pattern = '*';
66
+ const string = 'pondSockethello?test=5&test2=6';
67
+ expect(baseClass.parseEvent(pattern, string)).toEqual({
68
+ address: string,
69
+ params: {},
70
+ query: {
71
+ test: '5',
72
+ test2: '6',
73
+ },
74
+ });
75
+ });
64
76
  });
@@ -40,7 +40,7 @@ class SimpleSubject {
40
40
  this._observers.add(observer);
41
41
  return () => this._observers.delete(observer);
42
42
  }
43
- publish(message) {
43
+ next(message) {
44
44
  this._observers.forEach((observer) => observer(message));
45
45
  }
46
46
  }
@@ -54,9 +54,9 @@ class SimpleBehaviorSubject extends SimpleSubject {
54
54
  get value() {
55
55
  return this._lastMessage;
56
56
  }
57
- publish(message) {
57
+ next(message) {
58
58
  this._lastMessage = message;
59
- super.publish(message);
59
+ super.next(message);
60
60
  }
61
61
  subscribe(observer) {
62
62
  if (this._lastMessage) {
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const subjectUtils_1 = require("./subjectUtils");
4
+ describe('Subject', () => {
5
+ let testSubject;
6
+ let observer1;
7
+ let observer2;
8
+ beforeEach(() => {
9
+ testSubject = new subjectUtils_1.Subject();
10
+ observer1 = jest.fn();
11
+ observer2 = jest.fn();
12
+ testSubject.subscribe('observer1', observer1);
13
+ testSubject.subscribe('observer2', observer2);
14
+ });
15
+ afterEach(() => {
16
+ testSubject.unsubscribe('observer1');
17
+ testSubject.unsubscribe('observer2');
18
+ });
19
+ it('should notify all subscribers when next is called', () => {
20
+ const message = 10;
21
+ testSubject.next(message);
22
+ expect(observer1).toHaveBeenCalledWith(message);
23
+ expect(observer2).toHaveBeenCalledWith(message);
24
+ });
25
+ it('should unsubscribe an observer when unsubscribe is called', () => {
26
+ const identifier = 'observer1';
27
+ testSubject.unsubscribe(identifier);
28
+ expect(testSubject['_observers'].has(identifier)).toBe(false);
29
+ });
30
+ });
31
+ describe('BehaviorSubject', () => {
32
+ let testSubject;
33
+ let observer1;
34
+ let observer2;
35
+ beforeEach(() => {
36
+ testSubject = new subjectUtils_1.BehaviorSubject();
37
+ observer1 = jest.fn();
38
+ observer2 = jest.fn();
39
+ testSubject.subscribe('observer1', observer1);
40
+ testSubject.subscribe('observer2', observer2);
41
+ });
42
+ afterEach(() => {
43
+ testSubject.unsubscribe('observer1');
44
+ testSubject.unsubscribe('observer2');
45
+ });
46
+ it('should notify all subscribers when next is called', () => {
47
+ const message = 10;
48
+ testSubject.next(message);
49
+ expect(observer1).toHaveBeenCalledWith(message);
50
+ expect(observer2).toHaveBeenCalledWith(message);
51
+ });
52
+ it('should unsubscribe an observer when unsubscribe is called', () => {
53
+ const identifier = 'observer1';
54
+ testSubject.unsubscribe(identifier);
55
+ expect(testSubject['_observers'].has(identifier)).toBe(false);
56
+ });
57
+ it('should notify new subscribers with the last message when subscribe is called', () => {
58
+ const message = 10;
59
+ testSubject.next(message);
60
+ const newObserver = jest.fn();
61
+ testSubject.subscribe('newObserver', newObserver);
62
+ expect(newObserver).toHaveBeenCalledWith(message);
63
+ });
64
+ });
65
+ describe('SimpleSubject', () => {
66
+ let testSubject;
67
+ let observer1;
68
+ let observer2;
69
+ beforeEach(() => {
70
+ testSubject = new subjectUtils_1.SimpleSubject();
71
+ observer1 = jest.fn();
72
+ observer2 = jest.fn();
73
+ testSubject.subscribe(observer1);
74
+ testSubject.subscribe(observer2);
75
+ });
76
+ afterEach(() => {
77
+ testSubject.next(0);
78
+ });
79
+ it('should notify all subscribers when publish is called', () => {
80
+ const message = 10;
81
+ testSubject.next(message);
82
+ expect(observer1).toHaveBeenCalledWith(message);
83
+ expect(observer2).toHaveBeenCalledWith(message);
84
+ });
85
+ it('should unsubscribe an observer when unsubscribe is called', () => {
86
+ const unsubscribe = testSubject.subscribe(observer1);
87
+ unsubscribe();
88
+ expect(testSubject['_observers'].size).toBe(1);
89
+ });
90
+ });
91
+ describe('SimpleBehaviorSubject', () => {
92
+ let testSubject;
93
+ let observer1;
94
+ let observer2;
95
+ beforeEach(() => {
96
+ testSubject = new subjectUtils_1.SimpleBehaviorSubject(0);
97
+ observer1 = jest.fn();
98
+ observer2 = jest.fn();
99
+ testSubject.subscribe(observer1);
100
+ testSubject.subscribe(observer2);
101
+ });
102
+ afterEach(() => {
103
+ testSubject.next(0);
104
+ });
105
+ it('should notify all subscribers when publish is called', () => {
106
+ const message = 10;
107
+ testSubject.next(message);
108
+ expect(observer1).toHaveBeenCalledWith(message);
109
+ expect(observer2).toHaveBeenCalledWith(message);
110
+ });
111
+ it('should unsubscribe an observer when unsubscribe is called', () => {
112
+ const unsubscribe = testSubject.subscribe(observer1);
113
+ unsubscribe();
114
+ expect(testSubject['_observers'].size).toBe(1);
115
+ });
116
+ it('should notify new subscribers with the last message when subscribe is called', () => {
117
+ const message = 10;
118
+ testSubject.next(message);
119
+ const newObserver = jest.fn();
120
+ testSubject.subscribe(newObserver);
121
+ expect(newObserver).toHaveBeenCalledWith(message);
122
+ });
123
+ it('should return the last message when value is called', () => {
124
+ const message = 10;
125
+ testSubject.next(message);
126
+ expect(testSubject.value).toBe(message);
127
+ });
128
+ });
package/types.d.ts CHANGED
@@ -5,7 +5,7 @@ import internal from 'stream';
5
5
  import { Express } from 'express';
6
6
  import { WebSocketServer, WebSocket } from 'ws';
7
7
 
8
- import { ClientActions, PresenceEventTypes } from './enums';
8
+ import { ClientActions, PresenceEventTypes, ChannelState } from './enums';
9
9
 
10
10
  export type PondPath = string | RegExp;
11
11
  type NextFunction = () => void;
@@ -140,7 +140,7 @@ export declare class Channel {
140
140
  * @desc Monitors the connection state of the channel.
141
141
  * @param callback - The callback to call when the connection state changes.
142
142
  */
143
- public onConnectionChange(callback: (connected: boolean) => void): Unsubscribe;
143
+ public onConnectionChange(callback: (connected: ChannelState) => void): Unsubscribe;
144
144
 
145
145
  /**
146
146
  * @desc Detects when clients join the channel.
@@ -185,12 +185,7 @@ export declare class Channel {
185
185
  /**
186
186
  * @desc Gets the current connection state of the channel.
187
187
  */
188
- public isConnected(): boolean;
189
-
190
- /**
191
- * @desc check is the channel has been closed.
192
- */
193
- public hasClosed(): boolean;
188
+ public channelState: ChannelState;
194
189
 
195
190
  /**
196
191
  * @desc Gets the current presence of the channel.