@eleven-am/pondsocket 0.1.168 → 0.1.170
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/abstracts/middleware.js +40 -17
- package/abstracts/redisClient.js +278 -0
- package/engines/channelEngine.js +163 -0
- package/engines/endpointEngine.js +163 -0
- package/engines/lobbyEngine.js +79 -0
- package/errors/httpError.js +11 -0
- package/index.js +2 -2
- package/managers/distributedManager.js +115 -0
- package/managers/localManager.js +41 -0
- package/managers/manager.js +219 -0
- package/managers/managerFactory.js +31 -0
- package/package.json +24 -14
- package/requests/eventRequest.js +51 -0
- package/requests/joinRequest.js +56 -0
- package/responses/connectionResponse.js +89 -0
- package/{channel → responses}/eventResponse.js +8 -79
- package/{lobby → responses}/joinResponse.js +25 -84
- package/server/{pondSocket.js → server.js} +55 -55
- package/types.d.ts +33 -106
- package/wrappers/channel.js +70 -0
- package/wrappers/endpoint.js +32 -0
- package/wrappers/pondChannel.js +48 -0
- package/abstracts/abstractRequest.js +0 -57
- package/abstracts/abstractRequest.test.js +0 -48
- package/abstracts/middleware.test.js +0 -70
- package/channel/channel.js +0 -351
- package/channel/channel.test.js +0 -435
- package/channel/eventRequest.js +0 -25
- package/channel/eventRequest.test.js +0 -30
- package/channel/eventResponse.test.js +0 -175
- package/endpoint/endpoint.js +0 -299
- package/endpoint/endpoint.test.js +0 -299
- package/endpoint/response.js +0 -129
- package/errors/pondError.js +0 -28
- package/lobby/JoinRequest.test.js +0 -50
- package/lobby/JoinResponse.test.js +0 -142
- package/lobby/joinRequest.js +0 -43
- package/lobby/lobby.js +0 -186
- package/matcher/matcher.test.js +0 -90
- package/presence/presence.js +0 -112
- package/presence/presenceEngine.test.js +0 -129
|
@@ -1,48 +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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const abstractRequest_1 = require("./abstractRequest");
|
|
13
|
-
const eventResponse_test_1 = require("../channel/eventResponse.test");
|
|
14
|
-
const createMockChannelEngine = () => (0, eventResponse_test_1.createChannelEngine)().channelEngine;
|
|
15
|
-
describe('AbstractRequest', () => {
|
|
16
|
-
it('should be able to be instantiated', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
-
const request = new abstractRequest_1.AbstractRequest('/test', createMockChannelEngine(), {});
|
|
18
|
-
expect(request).toBeTruthy();
|
|
19
|
-
expect(request.channelName).toBe('test');
|
|
20
|
-
expect(request.assigns).toEqual({});
|
|
21
|
-
expect(yield request.getPresence()).toEqual({});
|
|
22
|
-
}));
|
|
23
|
-
it('should be able to parse queries', () => {
|
|
24
|
-
const request = new abstractRequest_1.AbstractRequest('/1234?choke=balls', createMockChannelEngine(), {});
|
|
25
|
-
expect(() => request.event).toThrowError('Event was not parsed');
|
|
26
|
-
expect(request['_parseQueries']('/:id')).toBe(true);
|
|
27
|
-
expect(request.event).toEqual({
|
|
28
|
-
event: '/1234?choke=balls',
|
|
29
|
-
params: { id: '1234' },
|
|
30
|
-
query: { choke: 'balls' },
|
|
31
|
-
payload: {},
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
it('should be return the value of the payload', () => {
|
|
35
|
-
const request = new abstractRequest_1.AbstractRequest('/test', createMockChannelEngine(), { test: 'test' });
|
|
36
|
-
expect(request['_parseQueries']('/:id')).toBe(true);
|
|
37
|
-
expect(request.event.payload).toEqual({ test: 'test' });
|
|
38
|
-
const request2 = new abstractRequest_1.AbstractRequest('/test', createMockChannelEngine(), {
|
|
39
|
-
test: 'test',
|
|
40
|
-
test2: 'test2',
|
|
41
|
-
});
|
|
42
|
-
expect(request2['_parseQueries']('/:id')).toBe(true);
|
|
43
|
-
expect(request2.event.payload).toEqual({
|
|
44
|
-
test: 'test',
|
|
45
|
-
test2: 'test2',
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
});
|
|
@@ -1,70 +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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const middleware_1 = require("./middleware");
|
|
13
|
-
describe('Middleware', () => {
|
|
14
|
-
it('should be able to add middleware to the stack', () => {
|
|
15
|
-
const middleware = new middleware_1.Middleware();
|
|
16
|
-
middleware.use(() => { });
|
|
17
|
-
expect(middleware.length).toBe(1);
|
|
18
|
-
});
|
|
19
|
-
it('should be able to run middleware', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
-
const middleware = new middleware_1.Middleware();
|
|
21
|
-
const mock = jest.fn();
|
|
22
|
-
middleware.use(mock);
|
|
23
|
-
yield middleware.run({}, {}, () => { });
|
|
24
|
-
expect(mock).toHaveBeenCalled();
|
|
25
|
-
}));
|
|
26
|
-
it('should be able to run multiple middleware', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
-
const middleware = new middleware_1.Middleware();
|
|
28
|
-
const mock = jest.fn();
|
|
29
|
-
middleware.use((_, __, next) => {
|
|
30
|
-
mock('first');
|
|
31
|
-
next();
|
|
32
|
-
});
|
|
33
|
-
middleware.use((_, __, next) => {
|
|
34
|
-
mock('second');
|
|
35
|
-
next();
|
|
36
|
-
});
|
|
37
|
-
yield middleware.run({}, {}, () => { });
|
|
38
|
-
expect(mock).toHaveBeenCalledTimes(2);
|
|
39
|
-
expect(mock.mock.calls[0][2]).toBe(mock.mock.calls[1][2]);
|
|
40
|
-
}));
|
|
41
|
-
it('should be merge middleware in the correct order', () => {
|
|
42
|
-
const middleware = new middleware_1.Middleware();
|
|
43
|
-
const mock = jest.fn();
|
|
44
|
-
middleware.use(mock);
|
|
45
|
-
middleware.use(mock);
|
|
46
|
-
const middleware2 = new middleware_1.Middleware(middleware);
|
|
47
|
-
expect(middleware2.length).toBe(2);
|
|
48
|
-
});
|
|
49
|
-
it('should call the final function when the middleware stack is empty', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
-
const middleware = new middleware_1.Middleware();
|
|
51
|
-
const mock = jest.fn();
|
|
52
|
-
yield middleware.run({}, {}, mock);
|
|
53
|
-
expect(mock).toHaveBeenCalled();
|
|
54
|
-
}));
|
|
55
|
-
it('should be able to run middleware in the correct order when using a second middleware', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
56
|
-
const middleware = new middleware_1.Middleware();
|
|
57
|
-
const mock = jest.fn();
|
|
58
|
-
middleware.use((_, __, next) => {
|
|
59
|
-
mock('first');
|
|
60
|
-
next();
|
|
61
|
-
});
|
|
62
|
-
middleware.use((_, __, next) => {
|
|
63
|
-
mock('second');
|
|
64
|
-
next();
|
|
65
|
-
});
|
|
66
|
-
const middleware2 = new middleware_1.Middleware(middleware);
|
|
67
|
-
yield middleware2.run({}, {}, () => { });
|
|
68
|
-
expect(mock.mock.calls[0][2]).toBe(mock.mock.calls[1][2]);
|
|
69
|
-
}));
|
|
70
|
-
});
|
package/channel/channel.js
DELETED
|
@@ -1,351 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
-
};
|
|
8
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
-
};
|
|
13
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
14
|
-
var t = {};
|
|
15
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
16
|
-
t[p] = s[p];
|
|
17
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
18
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
19
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
20
|
-
t[p[i]] = s[p[i]];
|
|
21
|
-
}
|
|
22
|
-
return t;
|
|
23
|
-
};
|
|
24
|
-
var _ChannelEngine_instances, _ChannelEngine_receiver, _ChannelEngine_presenceEngine, _ChannelEngine_users, _ChannelEngine_parentEngine, _ChannelEngine_subscribe, _ChannelEngine_getUsersFromRecipients, _ChannelEngine_buildSubscription, _ChannelEngine_removeUser, _Channel_engine;
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.Channel = exports.ChannelEngine = void 0;
|
|
27
|
-
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
28
|
-
const eventRequest_1 = require("./eventRequest");
|
|
29
|
-
const eventResponse_1 = require("./eventResponse");
|
|
30
|
-
const pondError_1 = require("../errors/pondError");
|
|
31
|
-
const presence_1 = require("../presence/presence");
|
|
32
|
-
class ChannelEngine {
|
|
33
|
-
constructor(name, parent) {
|
|
34
|
-
_ChannelEngine_instances.add(this);
|
|
35
|
-
_ChannelEngine_receiver.set(this, void 0);
|
|
36
|
-
_ChannelEngine_presenceEngine.set(this, void 0);
|
|
37
|
-
_ChannelEngine_users.set(this, void 0);
|
|
38
|
-
_ChannelEngine_parentEngine.set(this, void 0);
|
|
39
|
-
this.name = name;
|
|
40
|
-
__classPrivateFieldSet(this, _ChannelEngine_parentEngine, parent, "f");
|
|
41
|
-
__classPrivateFieldSet(this, _ChannelEngine_users, new Map(), "f");
|
|
42
|
-
__classPrivateFieldSet(this, _ChannelEngine_receiver, new pondsocket_common_1.Subject(), "f");
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* @desc Gets the presence engine for the channel
|
|
46
|
-
*/
|
|
47
|
-
get presenceEngine() {
|
|
48
|
-
var _a;
|
|
49
|
-
const presenceEngine = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) !== null && _a !== void 0 ? _a : new presence_1.PresenceEngine(this);
|
|
50
|
-
__classPrivateFieldSet(this, _ChannelEngine_presenceEngine, presenceEngine, "f");
|
|
51
|
-
return presenceEngine;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* @desc Gets the number of users in the channel
|
|
55
|
-
*/
|
|
56
|
-
get size() {
|
|
57
|
-
return __classPrivateFieldGet(this, _ChannelEngine_users, "f").size;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* @desc Gets the parent engine
|
|
61
|
-
*/
|
|
62
|
-
get parent() {
|
|
63
|
-
return __classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f");
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* @desc Adds a user to the channel
|
|
67
|
-
* @param userId - The id of the user to add
|
|
68
|
-
* @param assigns - The assigns to add to the user
|
|
69
|
-
* @param onMessage - The callback to call when a message is received
|
|
70
|
-
*/
|
|
71
|
-
addUser(userId, assigns, onMessage) {
|
|
72
|
-
const oldUser = __classPrivateFieldGet(this, _ChannelEngine_users, "f").get(userId);
|
|
73
|
-
if (oldUser) {
|
|
74
|
-
const message = `ChannelEngine: User with id ${userId} already exists in channel ${this.name}`;
|
|
75
|
-
const code = 400;
|
|
76
|
-
throw new pondError_1.ChannelError(message, code, this.name);
|
|
77
|
-
}
|
|
78
|
-
__classPrivateFieldGet(this, _ChannelEngine_users, "f").set(userId, assigns);
|
|
79
|
-
const subscription = __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_subscribe).call(this, userId, onMessage);
|
|
80
|
-
this.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, [userId], pondsocket_common_1.ServerActions.SYSTEM, pondsocket_common_1.Events.ACKNOWLEDGE, {});
|
|
81
|
-
return subscription;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* @desc Kicks a user from the channel
|
|
85
|
-
* @param userId - The id of the user to kick
|
|
86
|
-
* @param reason - The reason for kicking the user
|
|
87
|
-
*/
|
|
88
|
-
kickUser(userId, reason) {
|
|
89
|
-
this.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, [userId], pondsocket_common_1.ServerActions.SYSTEM, 'kicked_out', {
|
|
90
|
-
message: reason,
|
|
91
|
-
code: 403,
|
|
92
|
-
});
|
|
93
|
-
__classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_removeUser).call(this, userId);
|
|
94
|
-
this.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.SYSTEM, 'kicked', {
|
|
95
|
-
userId,
|
|
96
|
-
reason,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* @desc Self destructs the channel
|
|
101
|
-
* @param reason - The reason for self-destructing the channel
|
|
102
|
-
*/
|
|
103
|
-
destroy(reason) {
|
|
104
|
-
this.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.ERROR, 'destroyed', {
|
|
105
|
-
message: reason !== null && reason !== void 0 ? reason : 'Channel has been destroyed',
|
|
106
|
-
});
|
|
107
|
-
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel(this.name);
|
|
108
|
-
__classPrivateFieldGet(this, _ChannelEngine_users, "f").forEach((_, userId) => __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_removeUser).call(this, userId));
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* @desc Updates a user's assigns
|
|
112
|
-
* @param userId - The id of the user to update
|
|
113
|
-
* @param assigns - The new assigns of the user
|
|
114
|
-
*/
|
|
115
|
-
updateAssigns(userId, assigns) {
|
|
116
|
-
const user = __classPrivateFieldGet(this, _ChannelEngine_users, "f").get(userId);
|
|
117
|
-
if (user) {
|
|
118
|
-
__classPrivateFieldGet(this, _ChannelEngine_users, "f").set(userId, Object.assign(Object.assign({}, user), assigns));
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* @desc Gets the data of a user
|
|
126
|
-
* @param userId - The id of the user to get
|
|
127
|
-
*/
|
|
128
|
-
getUserData(userId) {
|
|
129
|
-
const assigns = __classPrivateFieldGet(this, _ChannelEngine_users, "f").get(userId);
|
|
130
|
-
const presence = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f") ? __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f").getUserPresence(userId) : {};
|
|
131
|
-
if (assigns) {
|
|
132
|
-
return {
|
|
133
|
-
assigns,
|
|
134
|
-
id: userId,
|
|
135
|
-
presence: presence || {},
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
return undefined;
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* @desc Gets the assign data of all users
|
|
142
|
-
*/
|
|
143
|
-
getAssigns() {
|
|
144
|
-
const assigns = {};
|
|
145
|
-
__classPrivateFieldGet(this, _ChannelEngine_users, "f").forEach((value, key) => {
|
|
146
|
-
assigns[key] = value;
|
|
147
|
-
});
|
|
148
|
-
return assigns;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* @desc Sends a message to a specified set of users, from a specified sender
|
|
152
|
-
* @param requestId - The id of the request
|
|
153
|
-
* @param sender - The sender of the message
|
|
154
|
-
* @param recipient - The users to send the message to
|
|
155
|
-
* @param action - The action of the message
|
|
156
|
-
* @param event - The event name
|
|
157
|
-
* @param payload - The payload of the message
|
|
158
|
-
*/
|
|
159
|
-
sendMessage(sender, recipient, action, event, payload, requestId = (0, pondsocket_common_1.uuid)()) {
|
|
160
|
-
if (!__classPrivateFieldGet(this, _ChannelEngine_users, "f").has(sender) && sender !== pondsocket_common_1.SystemSender.CHANNEL) {
|
|
161
|
-
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${sender} does not exist in channel ${this.name}`, 404, this.name);
|
|
162
|
-
}
|
|
163
|
-
const channelEvent = {
|
|
164
|
-
channelName: this.name,
|
|
165
|
-
requestId,
|
|
166
|
-
action,
|
|
167
|
-
event,
|
|
168
|
-
payload,
|
|
169
|
-
};
|
|
170
|
-
const recipients = __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_getUsersFromRecipients).call(this, recipient, sender);
|
|
171
|
-
__classPrivateFieldGet(this, _ChannelEngine_receiver, "f").publish(Object.assign(Object.assign({}, channelEvent), { recipients }));
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* @desc Handles a message from a user
|
|
175
|
-
* @param userId - The id of the user who sent the message
|
|
176
|
-
* @param message - The message received
|
|
177
|
-
*/
|
|
178
|
-
broadcastMessage(userId, message) {
|
|
179
|
-
if (!__classPrivateFieldGet(this, _ChannelEngine_users, "f").has(userId)) {
|
|
180
|
-
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
181
|
-
}
|
|
182
|
-
const responseEvent = {
|
|
183
|
-
sender: userId,
|
|
184
|
-
event: message.event,
|
|
185
|
-
payload: message.payload,
|
|
186
|
-
action: pondsocket_common_1.ServerActions.BROADCAST,
|
|
187
|
-
channelName: this.name,
|
|
188
|
-
requestId: message.requestId,
|
|
189
|
-
};
|
|
190
|
-
const request = new eventRequest_1.EventRequest(responseEvent, this);
|
|
191
|
-
const response = new eventResponse_1.EventResponse(responseEvent, this);
|
|
192
|
-
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").middleware.run(request, response, () => {
|
|
193
|
-
this.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, [userId], pondsocket_common_1.ServerActions.ERROR, pondsocket_common_1.ErrorTypes.HANDLER_NOT_FOUND, {
|
|
194
|
-
message: 'A handler did not respond to the event',
|
|
195
|
-
code: 404,
|
|
196
|
-
}, message.requestId);
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* @desc Subscribes a user to a channel, Will join the channel if it exists or add to pending subscriptions
|
|
201
|
-
* @param userId - The id of the user to subscribe
|
|
202
|
-
* @param channel - The name of the channel to subscribe to
|
|
203
|
-
*/
|
|
204
|
-
subscribeTo(userId, channel) {
|
|
205
|
-
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").parent.subscribeTo(userId, channel);
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* @desc Unsubscribes a user from a channel
|
|
209
|
-
* @param userId - The id of the user to unsubscribe
|
|
210
|
-
* @param channel - The name of the channel to unsubscribe from
|
|
211
|
-
*/
|
|
212
|
-
unsubscribeFrom(userId, channel) {
|
|
213
|
-
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").parent.unsubscribeFrom(userId, channel);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
exports.ChannelEngine = ChannelEngine;
|
|
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) {
|
|
218
|
-
const unsubscribe = __classPrivateFieldGet(this, _ChannelEngine_receiver, "f").subscribe((_a) => {
|
|
219
|
-
var { recipients } = _a, event = __rest(_a, ["recipients"]);
|
|
220
|
-
if (recipients.includes(userId)) {
|
|
221
|
-
onMessage(event);
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
return __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_buildSubscription).bind(this, userId, unsubscribe);
|
|
225
|
-
}, _ChannelEngine_getUsersFromRecipients = function _ChannelEngine_getUsersFromRecipients(recipients, sender) {
|
|
226
|
-
const allUsers = Array.from(__classPrivateFieldGet(this, _ChannelEngine_users, "f").keys());
|
|
227
|
-
let users;
|
|
228
|
-
switch (recipients) {
|
|
229
|
-
case pondsocket_common_1.ChannelReceiver.ALL_USERS:
|
|
230
|
-
users = allUsers;
|
|
231
|
-
break;
|
|
232
|
-
case pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER:
|
|
233
|
-
if (sender === pondsocket_common_1.SystemSender.CHANNEL) {
|
|
234
|
-
throw new pondError_1.ChannelError(`ChannelEngine: Cannot use ${pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER} with ${pondsocket_common_1.SystemSender.CHANNEL}`, 500, this.name);
|
|
235
|
-
}
|
|
236
|
-
users = allUsers.filter((user) => user !== sender);
|
|
237
|
-
break;
|
|
238
|
-
default:
|
|
239
|
-
if (!Array.isArray(recipients)) {
|
|
240
|
-
throw new pondError_1.ChannelError(`ChannelEngine: Invalid recipients ${recipients}`, 500, this.name);
|
|
241
|
-
}
|
|
242
|
-
if (recipients.some((user) => !allUsers.includes(user))) {
|
|
243
|
-
throw new pondError_1.ChannelError(`ChannelEngine: Invalid recipients ${recipients} some users do not exist in channel ${this.name}`, 500, this.name);
|
|
244
|
-
}
|
|
245
|
-
users = recipients;
|
|
246
|
-
break;
|
|
247
|
-
}
|
|
248
|
-
return users;
|
|
249
|
-
}, _ChannelEngine_buildSubscription = function _ChannelEngine_buildSubscription(userId, unsubscribe) {
|
|
250
|
-
var _a;
|
|
251
|
-
const user = __classPrivateFieldGet(this, _ChannelEngine_users, "f").get(userId);
|
|
252
|
-
if (!user) {
|
|
253
|
-
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
254
|
-
}
|
|
255
|
-
unsubscribe();
|
|
256
|
-
__classPrivateFieldGet(this, _ChannelEngine_users, "f").delete(userId);
|
|
257
|
-
const userPresence = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId, true);
|
|
258
|
-
if (__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback) {
|
|
259
|
-
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback({
|
|
260
|
-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
261
|
-
channel: new Channel(this),
|
|
262
|
-
user: {
|
|
263
|
-
id: userId,
|
|
264
|
-
presence: userPresence !== null && userPresence !== void 0 ? userPresence : {},
|
|
265
|
-
assigns: user,
|
|
266
|
-
},
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
if (__classPrivateFieldGet(this, _ChannelEngine_users, "f").size === 0) {
|
|
270
|
-
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel(this.name);
|
|
271
|
-
}
|
|
272
|
-
}, _ChannelEngine_removeUser = function _ChannelEngine_removeUser(userId) {
|
|
273
|
-
const cachedUser = __classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").parent.getUser(userId);
|
|
274
|
-
const unsubscribe = cachedUser.subscriptions.get(this.name);
|
|
275
|
-
if (!unsubscribe) {
|
|
276
|
-
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
277
|
-
}
|
|
278
|
-
unsubscribe();
|
|
279
|
-
cachedUser.subscriptions.delete(this.name);
|
|
280
|
-
};
|
|
281
|
-
class Channel {
|
|
282
|
-
constructor(engine) {
|
|
283
|
-
_Channel_engine.set(this, void 0);
|
|
284
|
-
__classPrivateFieldSet(this, _Channel_engine, engine, "f");
|
|
285
|
-
}
|
|
286
|
-
get name() {
|
|
287
|
-
return __classPrivateFieldGet(this, _Channel_engine, "f").name;
|
|
288
|
-
}
|
|
289
|
-
getAssigns() {
|
|
290
|
-
return __classPrivateFieldGet(this, _Channel_engine, "f").getAssigns();
|
|
291
|
-
}
|
|
292
|
-
getPresences() {
|
|
293
|
-
return __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getPresence();
|
|
294
|
-
}
|
|
295
|
-
getUserData(userId) {
|
|
296
|
-
return __classPrivateFieldGet(this, _Channel_engine, "f").getUserData(userId);
|
|
297
|
-
}
|
|
298
|
-
broadcast(event, payload) {
|
|
299
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
|
|
300
|
-
return this;
|
|
301
|
-
}
|
|
302
|
-
broadcastFrom(userId, event, payload) {
|
|
303
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(userId, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
|
|
304
|
-
return this;
|
|
305
|
-
}
|
|
306
|
-
broadcastTo(userIds, event, payload) {
|
|
307
|
-
const users = Array.isArray(userIds) ? userIds : [userIds];
|
|
308
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, users, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
|
|
309
|
-
return this;
|
|
310
|
-
}
|
|
311
|
-
evictUser(userId, reason) {
|
|
312
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").kickUser(userId, reason !== null && reason !== void 0 ? reason : 'You have been banned from the channel');
|
|
313
|
-
return this;
|
|
314
|
-
}
|
|
315
|
-
trackPresence(userId, presence) {
|
|
316
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
|
|
317
|
-
return this;
|
|
318
|
-
}
|
|
319
|
-
removePresence(userId) {
|
|
320
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.removePresence(userId);
|
|
321
|
-
return this;
|
|
322
|
-
}
|
|
323
|
-
updatePresence(userId, presence) {
|
|
324
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.updatePresence(userId, presence);
|
|
325
|
-
return this;
|
|
326
|
-
}
|
|
327
|
-
updateAssigns(userId, assigns) {
|
|
328
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").updateAssigns(userId, assigns);
|
|
329
|
-
return this;
|
|
330
|
-
}
|
|
331
|
-
subscribeTo(userId, channel) {
|
|
332
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").subscribeTo(userId, channel);
|
|
333
|
-
return this;
|
|
334
|
-
}
|
|
335
|
-
unsubscribeFrom(userId, channel) {
|
|
336
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").unsubscribeFrom(userId, channel);
|
|
337
|
-
return this;
|
|
338
|
-
}
|
|
339
|
-
upsertPresence(userId, presence) {
|
|
340
|
-
const oldPresence = __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getPresence()[userId];
|
|
341
|
-
if (oldPresence) {
|
|
342
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.updatePresence(userId, presence);
|
|
343
|
-
}
|
|
344
|
-
else {
|
|
345
|
-
__classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
|
|
346
|
-
}
|
|
347
|
-
return this;
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
exports.Channel = Channel;
|
|
351
|
-
_Channel_engine = new WeakMap();
|