@eleven-am/pondsocket 0.1.49 → 0.1.51
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/abstractRequest.js +58 -0
- package/abstracts/middleware.js +51 -0
- package/channel/channel.js +253 -0
- package/{server/channel → channel}/eventRequest.js +4 -1
- package/channel/eventResponse.js +150 -0
- package/client/channel.js +120 -97
- package/client.d.ts +2 -3
- package/client.js +34 -20
- package/endpoint/endpoint.js +233 -0
- package/endpoint/response.js +86 -0
- package/enums.js +14 -10
- package/errors/pondError.js +27 -0
- package/express.d.ts +2 -2
- package/express.js +3 -3
- package/index.d.ts +1 -2
- package/index.js +1 -4
- package/lobby/joinRequest.js +39 -0
- package/lobby/joinResponse.js +125 -0
- package/lobby/lobby.js +174 -0
- package/matcher/matcher.js +94 -0
- package/node.d.ts +2 -3
- package/node.js +3 -4
- package/package.json +3 -2
- package/presence/presence.js +113 -0
- package/server/pondSocket.js +123 -0
- package/subjects/subject.js +93 -0
- package/types.d.ts +274 -323
- package/client/channel.test.js +0 -546
- package/server/abstracts/abstractRequest.js +0 -40
- package/server/abstracts/abstractRequest.test.js +0 -41
- package/server/abstracts/middleware.js +0 -38
- package/server/abstracts/middleware.test.js +0 -70
- package/server/channel/channelEngine.js +0 -280
- package/server/channel/channelEngine.test.js +0 -377
- package/server/channel/channelRequest.test.js +0 -29
- package/server/channel/channelResponse.test.js +0 -164
- package/server/channel/eventResponse.js +0 -153
- package/server/endpoint/connectionResponse.js +0 -64
- package/server/endpoint/endpoint.js +0 -253
- package/server/endpoint/endpoint.test.js +0 -428
- package/server/endpoint/endpointResponse.test.js +0 -43
- package/server/pondChannel/joinRequest.js +0 -29
- package/server/pondChannel/joinResponse.js +0 -103
- package/server/pondChannel/pondChannel.js +0 -185
- package/server/pondChannel/pondChannelResponse.test.js +0 -109
- package/server/presence/presenceEngine.js +0 -107
- package/server/presence/presenceEngine.test.js +0 -105
- package/server/server/pondSocket.js +0 -121
- package/server/server/server.test.js +0 -121
- package/server/utils/matchPattern.js +0 -108
- package/server/utils/matchPattern.test.js +0 -76
- package/server/utils/subjectUtils.js +0 -68
- package/server/utils/subjectUtils.test.js +0 -128
- /package/{server/abstracts → abstracts}/abstractResponse.js +0 -0
|
@@ -0,0 +1,58 @@
|
|
|
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 _AbstractRequest_eventObject, _AbstractRequest_event, _AbstractRequest_payload;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.AbstractRequest = void 0;
|
|
16
|
+
const matcher_1 = require("../matcher/matcher");
|
|
17
|
+
class AbstractRequest {
|
|
18
|
+
constructor(event, engine, payload) {
|
|
19
|
+
_AbstractRequest_eventObject.set(this, void 0);
|
|
20
|
+
_AbstractRequest_event.set(this, void 0);
|
|
21
|
+
_AbstractRequest_payload.set(this, void 0);
|
|
22
|
+
this._engine = engine;
|
|
23
|
+
__classPrivateFieldSet(this, _AbstractRequest_event, event, "f");
|
|
24
|
+
__classPrivateFieldSet(this, _AbstractRequest_eventObject, null, "f");
|
|
25
|
+
__classPrivateFieldSet(this, _AbstractRequest_payload, payload, "f");
|
|
26
|
+
}
|
|
27
|
+
get event() {
|
|
28
|
+
if (__classPrivateFieldGet(this, _AbstractRequest_eventObject, "f") === null) {
|
|
29
|
+
throw new Error('Event was not parsed');
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
event: __classPrivateFieldGet(this, _AbstractRequest_event, "f"),
|
|
33
|
+
params: __classPrivateFieldGet(this, _AbstractRequest_eventObject, "f").params || {},
|
|
34
|
+
query: __classPrivateFieldGet(this, _AbstractRequest_eventObject, "f").query || {},
|
|
35
|
+
payload: __classPrivateFieldGet(this, _AbstractRequest_payload, "f"),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
get channelNme() {
|
|
39
|
+
return this._engine.name;
|
|
40
|
+
}
|
|
41
|
+
get assigns() {
|
|
42
|
+
return this._engine.getAssigns();
|
|
43
|
+
}
|
|
44
|
+
get presence() {
|
|
45
|
+
var _a;
|
|
46
|
+
return ((_a = this._engine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()) || {};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @desc Parses the event and returns true if the event matches the path
|
|
50
|
+
* @param path - the path to match
|
|
51
|
+
*/
|
|
52
|
+
_parseQueries(path) {
|
|
53
|
+
__classPrivateFieldSet(this, _AbstractRequest_eventObject, (0, matcher_1.parseAddress)(path, __classPrivateFieldGet(this, _AbstractRequest_event, "f")), "f");
|
|
54
|
+
return __classPrivateFieldGet(this, _AbstractRequest_eventObject, "f") !== null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.AbstractRequest = AbstractRequest;
|
|
58
|
+
_AbstractRequest_eventObject = new WeakMap(), _AbstractRequest_event = new WeakMap(), _AbstractRequest_payload = new WeakMap();
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
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");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var _Middleware_stack;
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.Middleware = void 0;
|
|
10
|
+
class Middleware {
|
|
11
|
+
constructor(second) {
|
|
12
|
+
_Middleware_stack.set(this, []);
|
|
13
|
+
if (second) {
|
|
14
|
+
__classPrivateFieldGet(this, _Middleware_stack, "f").push(...__classPrivateFieldGet(second, _Middleware_stack, "f"));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* @desc Adds a middleware function to the middleware stack
|
|
19
|
+
* @param middleware - the middleware function to add
|
|
20
|
+
*/
|
|
21
|
+
use(middleware) {
|
|
22
|
+
__classPrivateFieldGet(this, _Middleware_stack, "f").push(middleware);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @desc Runs the middleware stack
|
|
26
|
+
* @param req - the request object
|
|
27
|
+
* @param res - the response object
|
|
28
|
+
* @param final - the final function to call
|
|
29
|
+
*/
|
|
30
|
+
run(req, res, final) {
|
|
31
|
+
const temp = __classPrivateFieldGet(this, _Middleware_stack, "f").concat();
|
|
32
|
+
const nextFunction = () => {
|
|
33
|
+
const middleware = temp.shift();
|
|
34
|
+
if (middleware) {
|
|
35
|
+
middleware(req, res, nextFunction);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
final();
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
nextFunction();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* @desc Returns the middleware stack length
|
|
45
|
+
*/
|
|
46
|
+
get length() {
|
|
47
|
+
return __classPrivateFieldGet(this, _Middleware_stack, "f").length;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.Middleware = Middleware;
|
|
51
|
+
_Middleware_stack = new WeakMap();
|
|
@@ -0,0 +1,253 @@
|
|
|
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;
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.ChannelEngine = void 0;
|
|
27
|
+
const eventRequest_1 = require("./eventRequest");
|
|
28
|
+
const eventResponse_1 = require("./eventResponse");
|
|
29
|
+
const enums_1 = require("../enums");
|
|
30
|
+
const pondError_1 = require("../errors/pondError");
|
|
31
|
+
const presence_1 = require("../presence/presence");
|
|
32
|
+
const subject_1 = require("../subjects/subject");
|
|
33
|
+
class ChannelEngine {
|
|
34
|
+
constructor(name, parent) {
|
|
35
|
+
_ChannelEngine_instances.add(this);
|
|
36
|
+
_ChannelEngine_receiver.set(this, void 0);
|
|
37
|
+
_ChannelEngine_presenceEngine.set(this, void 0);
|
|
38
|
+
_ChannelEngine_users.set(this, void 0);
|
|
39
|
+
_ChannelEngine_parentEngine.set(this, void 0);
|
|
40
|
+
this.name = name;
|
|
41
|
+
__classPrivateFieldSet(this, _ChannelEngine_receiver, new subject_1.Subject(), "f");
|
|
42
|
+
__classPrivateFieldSet(this, _ChannelEngine_users, new Map(), "f");
|
|
43
|
+
__classPrivateFieldSet(this, _ChannelEngine_parentEngine, parent, "f");
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* @desc Adds a user to the channel
|
|
47
|
+
* @param userId - The id of the user to add
|
|
48
|
+
* @param assigns - The assigns to add to the user
|
|
49
|
+
* @param onMessage - The callback to call when a message is received
|
|
50
|
+
*/
|
|
51
|
+
addUser(userId, assigns, onMessage) {
|
|
52
|
+
const oldUser = __classPrivateFieldGet(this, _ChannelEngine_users, "f").get(userId);
|
|
53
|
+
if (oldUser) {
|
|
54
|
+
const message = `ChannelEngine: User with id ${userId} already exists in channel ${this.name}`;
|
|
55
|
+
const code = 404;
|
|
56
|
+
throw new pondError_1.ChannelError(message, code, this.name);
|
|
57
|
+
}
|
|
58
|
+
__classPrivateFieldGet(this, _ChannelEngine_users, "f").set(userId, assigns);
|
|
59
|
+
return __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_subscribe).call(this, userId, onMessage);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @desc Removes a user from the channel
|
|
63
|
+
* @param userId - The id of the user to remove
|
|
64
|
+
* @param graceful - Whether to remove the user gracefully or not
|
|
65
|
+
*/
|
|
66
|
+
removeUser(userId, graceful = false) {
|
|
67
|
+
var _a;
|
|
68
|
+
const user = __classPrivateFieldGet(this, _ChannelEngine_users, "f").get(userId);
|
|
69
|
+
if (user) {
|
|
70
|
+
__classPrivateFieldGet(this, _ChannelEngine_users, "f").delete(userId);
|
|
71
|
+
__classPrivateFieldGet(this, _ChannelEngine_receiver, "f").unsubscribe(userId);
|
|
72
|
+
(_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId, graceful);
|
|
73
|
+
if (__classPrivateFieldGet(this, _ChannelEngine_users, "f").size === 0) {
|
|
74
|
+
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else if (!graceful) {
|
|
78
|
+
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* @desc Kicks a user from the channel
|
|
83
|
+
* @param userId - The id of the user to kick
|
|
84
|
+
* @param reason - The reason for kicking the user
|
|
85
|
+
*/
|
|
86
|
+
kickUser(userId, reason) {
|
|
87
|
+
this.sendMessage(enums_1.SystemSender.CHANNEL, [userId], enums_1.ServerActions.SYSTEM, 'kicked_out', {
|
|
88
|
+
message: reason !== null && reason !== void 0 ? reason : 'You have been kicked out of the channel',
|
|
89
|
+
});
|
|
90
|
+
this.removeUser(userId);
|
|
91
|
+
this.sendMessage(enums_1.SystemSender.CHANNEL, enums_1.ChannelReceiver.ALL_USERS, enums_1.ServerActions.SYSTEM, 'kicked', {
|
|
92
|
+
userId,
|
|
93
|
+
reason,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* @desc Self destructs the channel
|
|
98
|
+
* @param reason - The reason for self-destructing the channel
|
|
99
|
+
*/
|
|
100
|
+
destroy(reason) {
|
|
101
|
+
this.sendMessage(enums_1.SystemSender.CHANNEL, enums_1.ChannelReceiver.ALL_USERS, enums_1.ServerActions.ERROR, 'destroyed', {
|
|
102
|
+
message: reason !== null && reason !== void 0 ? reason : 'Channel has been destroyed',
|
|
103
|
+
});
|
|
104
|
+
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel();
|
|
105
|
+
__classPrivateFieldGet(this, _ChannelEngine_users, "f").forEach((_, userId) => __classPrivateFieldGet(this, _ChannelEngine_receiver, "f").unsubscribe(userId));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* @desc Updates a user's assigns
|
|
109
|
+
* @param userId - The id of the user to update
|
|
110
|
+
* @param assigns - The new assigns of the user
|
|
111
|
+
*/
|
|
112
|
+
updateAssigns(userId, assigns) {
|
|
113
|
+
const user = __classPrivateFieldGet(this, _ChannelEngine_users, "f").get(userId);
|
|
114
|
+
if (user) {
|
|
115
|
+
__classPrivateFieldGet(this, _ChannelEngine_users, "f").set(userId, Object.assign(Object.assign({}, user), assigns));
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* @desc Gets the data of a user
|
|
123
|
+
* @param userId - The id of the user to get
|
|
124
|
+
*/
|
|
125
|
+
getUserData(userId) {
|
|
126
|
+
const presence = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f") ? __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f").getUserPresence(userId) : {};
|
|
127
|
+
if (__classPrivateFieldGet(this, _ChannelEngine_users, "f").has(userId)) {
|
|
128
|
+
return {
|
|
129
|
+
id: userId,
|
|
130
|
+
assigns: __classPrivateFieldGet(this, _ChannelEngine_users, "f").get(userId),
|
|
131
|
+
presence: presence || {},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* @desc Gets the assign data of all users
|
|
138
|
+
*/
|
|
139
|
+
getAssigns() {
|
|
140
|
+
const assigns = {};
|
|
141
|
+
__classPrivateFieldGet(this, _ChannelEngine_users, "f").forEach((value, key) => {
|
|
142
|
+
assigns[key] = value;
|
|
143
|
+
});
|
|
144
|
+
return assigns;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* @desc Sends a message to a specified set of users, from a specified sender
|
|
148
|
+
* @param sender - The sender of the message
|
|
149
|
+
* @param recipient - The users to send the message to
|
|
150
|
+
* @param action - The action of the message
|
|
151
|
+
* @param event - The event name
|
|
152
|
+
* @param payload - The payload of the message
|
|
153
|
+
* @private
|
|
154
|
+
*/
|
|
155
|
+
sendMessage(sender, recipient, action, event, payload) {
|
|
156
|
+
if (!__classPrivateFieldGet(this, _ChannelEngine_users, "f").has(sender) && sender !== enums_1.SystemSender.CHANNEL) {
|
|
157
|
+
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${sender} does not exist in channel ${this.name}`, 404, this.name);
|
|
158
|
+
}
|
|
159
|
+
const eventMessage = {
|
|
160
|
+
recipients: __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_getUsersFromRecipients).call(this, recipient, sender),
|
|
161
|
+
channelName: this.name,
|
|
162
|
+
action,
|
|
163
|
+
payload,
|
|
164
|
+
event,
|
|
165
|
+
};
|
|
166
|
+
__classPrivateFieldGet(this, _ChannelEngine_receiver, "f").publish(eventMessage);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* @desc Handles a message from a user
|
|
170
|
+
* @param userId - The id of the user who sent the message
|
|
171
|
+
* @param message - The message received
|
|
172
|
+
*/
|
|
173
|
+
broadcastMessage(userId, message) {
|
|
174
|
+
if (!__classPrivateFieldGet(this, _ChannelEngine_users, "f").has(userId)) {
|
|
175
|
+
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
176
|
+
}
|
|
177
|
+
const responseEvent = {
|
|
178
|
+
sender: userId,
|
|
179
|
+
event: message.event,
|
|
180
|
+
payload: message.payload,
|
|
181
|
+
action: enums_1.ServerActions.BROADCAST,
|
|
182
|
+
channelName: this.name,
|
|
183
|
+
recipients: __classPrivateFieldGet(this, _ChannelEngine_instances, "m", _ChannelEngine_getUsersFromRecipients).call(this, message.addresses || enums_1.ChannelReceiver.ALL_USERS, userId),
|
|
184
|
+
};
|
|
185
|
+
const request = new eventRequest_1.EventRequest(responseEvent, this);
|
|
186
|
+
const response = new eventResponse_1.EventResponse(responseEvent, this);
|
|
187
|
+
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").execute(request, response, () => {
|
|
188
|
+
this.sendMessage(enums_1.SystemSender.CHANNEL, [userId], enums_1.ServerActions.ERROR, enums_1.ErrorTypes.HANDLER_NOT_FOUND, {
|
|
189
|
+
message: 'A handler did not respond to the event',
|
|
190
|
+
code: 404,
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* @desc Begins tracking a user's presence
|
|
196
|
+
* @param userId - The id of the user to track
|
|
197
|
+
* @param presence - The initial presence of the user
|
|
198
|
+
*/
|
|
199
|
+
trackPresence(userId, presence) {
|
|
200
|
+
var _a;
|
|
201
|
+
if (!__classPrivateFieldGet(this, _ChannelEngine_users, "f").has(userId)) {
|
|
202
|
+
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
203
|
+
}
|
|
204
|
+
__classPrivateFieldSet(this, _ChannelEngine_presenceEngine, (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) !== null && _a !== void 0 ? _a : new presence_1.PresenceEngine(this.name), "f");
|
|
205
|
+
__classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f").trackPresence(userId, presence, (change) => {
|
|
206
|
+
const { type } = change, rest = __rest(change, ["type"]);
|
|
207
|
+
this.sendMessage(enums_1.SystemSender.CHANNEL, [userId], enums_1.ServerActions.PRESENCE, type, rest);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* @desc Gets the presence engine for the channel
|
|
212
|
+
*/
|
|
213
|
+
get presenceEngine() {
|
|
214
|
+
return __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f");
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* @desc Gets the number of users in the channel
|
|
218
|
+
*/
|
|
219
|
+
get size() {
|
|
220
|
+
return __classPrivateFieldGet(this, _ChannelEngine_users, "f").size;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
exports.ChannelEngine = ChannelEngine;
|
|
224
|
+
_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) {
|
|
225
|
+
__classPrivateFieldGet(this, _ChannelEngine_receiver, "f").subscribeWith(userId, (_a) => {
|
|
226
|
+
var { recipients } = _a, event = __rest(_a, ["recipients"]);
|
|
227
|
+
if (recipients.includes(userId)) {
|
|
228
|
+
onMessage(event);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
}, _ChannelEngine_getUsersFromRecipients = function _ChannelEngine_getUsersFromRecipients(recipients, sender) {
|
|
232
|
+
const allUsers = Array.from(__classPrivateFieldGet(this, _ChannelEngine_users, "f").keys());
|
|
233
|
+
let users;
|
|
234
|
+
switch (recipients) {
|
|
235
|
+
case enums_1.ChannelReceiver.ALL_USERS:
|
|
236
|
+
users = allUsers;
|
|
237
|
+
break;
|
|
238
|
+
case enums_1.ChannelReceiver.ALL_EXCEPT_SENDER:
|
|
239
|
+
if (sender === enums_1.SystemSender.CHANNEL) {
|
|
240
|
+
throw new pondError_1.ChannelError(`ChannelEngine: Cannot use ${enums_1.ChannelReceiver.ALL_EXCEPT_SENDER} with ${enums_1.SystemSender.CHANNEL}`, 500, this.name);
|
|
241
|
+
}
|
|
242
|
+
users = allUsers.filter((user) => user !== sender);
|
|
243
|
+
break;
|
|
244
|
+
default:
|
|
245
|
+
const absentUsers = recipients.filter((user) => !allUsers.includes(user));
|
|
246
|
+
if (absentUsers.length > 0) {
|
|
247
|
+
throw new pondError_1.ChannelError(`ChannelEngine: Users ${absentUsers.join(', ')} are not in channel ${this.name}`, 400, this.name);
|
|
248
|
+
}
|
|
249
|
+
users = recipients;
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
return users;
|
|
253
|
+
};
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EventRequest = void 0;
|
|
4
4
|
const abstractRequest_1 = require("../abstracts/abstractRequest");
|
|
5
|
+
const pondError_1 = require("../errors/pondError");
|
|
5
6
|
class EventRequest extends abstractRequest_1.AbstractRequest {
|
|
6
7
|
constructor(event, engine) {
|
|
7
8
|
super(event.event, engine, event.payload);
|
|
@@ -10,7 +11,9 @@ class EventRequest extends abstractRequest_1.AbstractRequest {
|
|
|
10
11
|
get user() {
|
|
11
12
|
const assigns = this._engine.getUserData(this._internalEvent.sender);
|
|
12
13
|
if (!assigns) {
|
|
13
|
-
|
|
14
|
+
const message = `ChannelRequest: User with id ${this._internalEvent.sender} does not exist in channel ${this._engine.name}`;
|
|
15
|
+
const code = 404;
|
|
16
|
+
throw new pondError_1.ChannelError(message, code, this._engine.name);
|
|
14
17
|
}
|
|
15
18
|
return assigns;
|
|
16
19
|
}
|
|
@@ -0,0 +1,150 @@
|
|
|
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 _EventResponse_instances, _EventResponse_event, _EventResponse_engine, _EventResponse_executed, _EventResponse_manageAssigns, _EventResponse_performChecks;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.EventResponse = void 0;
|
|
16
|
+
const abstractResponse_1 = require("../abstracts/abstractResponse");
|
|
17
|
+
const enums_1 = require("../enums");
|
|
18
|
+
const pondError_1 = require("../errors/pondError");
|
|
19
|
+
class EventResponse extends abstractResponse_1.PondResponse {
|
|
20
|
+
constructor(event, engine) {
|
|
21
|
+
super();
|
|
22
|
+
_EventResponse_instances.add(this);
|
|
23
|
+
_EventResponse_event.set(this, void 0);
|
|
24
|
+
_EventResponse_engine.set(this, void 0);
|
|
25
|
+
_EventResponse_executed.set(this, void 0);
|
|
26
|
+
__classPrivateFieldSet(this, _EventResponse_event, event, "f");
|
|
27
|
+
__classPrivateFieldSet(this, _EventResponse_engine, engine, "f");
|
|
28
|
+
__classPrivateFieldSet(this, _EventResponse_executed, false, "f");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @desc Accepts the request and optionally assigns data to the client
|
|
32
|
+
* @param assigns - the data to assign to the client
|
|
33
|
+
*/
|
|
34
|
+
accept(assigns) {
|
|
35
|
+
__classPrivateFieldGet(this, _EventResponse_instances, "m", _EventResponse_manageAssigns).call(this, assigns);
|
|
36
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(__classPrivateFieldGet(this, _EventResponse_event, "f").sender, __classPrivateFieldGet(this, _EventResponse_event, "f").recipients, __classPrivateFieldGet(this, _EventResponse_event, "f").action, __classPrivateFieldGet(this, _EventResponse_event, "f").event, __classPrivateFieldGet(this, _EventResponse_event, "f").payload);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @desc Rejects the request and optionally assigns data to the client
|
|
41
|
+
* @param message - the error message
|
|
42
|
+
* @param errorCode - the error code
|
|
43
|
+
* @param assigns - the data to assign to the client
|
|
44
|
+
*/
|
|
45
|
+
reject(message, errorCode, assigns) {
|
|
46
|
+
__classPrivateFieldGet(this, _EventResponse_instances, "m", _EventResponse_manageAssigns).call(this, assigns);
|
|
47
|
+
const text = message || 'Unauthorized request';
|
|
48
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(enums_1.SystemSender.CHANNEL, [__classPrivateFieldGet(this, _EventResponse_event, "f").sender], enums_1.ServerActions.ERROR, enums_1.ErrorTypes.UNAUTHORIZED_BROADCAST, {
|
|
49
|
+
message: text,
|
|
50
|
+
code: errorCode || 403,
|
|
51
|
+
});
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @desc Emits a direct message to the client
|
|
56
|
+
* @param event - the event name
|
|
57
|
+
* @param payload - the payload to send
|
|
58
|
+
* @param assigns - the data to assign to the client
|
|
59
|
+
*/
|
|
60
|
+
send(event, payload, assigns) {
|
|
61
|
+
this.accept(assigns);
|
|
62
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(enums_1.SystemSender.CHANNEL, [__classPrivateFieldGet(this, _EventResponse_event, "f").sender], enums_1.ServerActions.SYSTEM, event, payload);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* @desc Sends a message to all clients in the channel
|
|
66
|
+
* @param event - the event to send
|
|
67
|
+
* @param payload - the payload to send
|
|
68
|
+
*/
|
|
69
|
+
broadcast(event, payload) {
|
|
70
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(__classPrivateFieldGet(this, _EventResponse_event, "f").sender, enums_1.ChannelReceiver.ALL_USERS, enums_1.ServerActions.BROADCAST, event, payload);
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @desc Sends a message to all clients in the channel except the client making the request
|
|
75
|
+
* @param event - the event to send
|
|
76
|
+
* @param payload - the payload to send
|
|
77
|
+
*/
|
|
78
|
+
broadcastFromUser(event, payload) {
|
|
79
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(__classPrivateFieldGet(this, _EventResponse_event, "f").sender, enums_1.ChannelReceiver.ALL_EXCEPT_SENDER, enums_1.ServerActions.BROADCAST, event, payload);
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* @desc Sends a message to a set of clients in the channel
|
|
84
|
+
* @param event - the event to send
|
|
85
|
+
* @param payload - the payload to send
|
|
86
|
+
* @param userIds - the ids of the clients to send the message to
|
|
87
|
+
*/
|
|
88
|
+
sendToUsers(event, payload, userIds) {
|
|
89
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").sendMessage(__classPrivateFieldGet(this, _EventResponse_event, "f").sender, userIds, enums_1.ServerActions.BROADCAST, event, payload);
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* @desc Tracks a user's presence in the channel
|
|
94
|
+
* @param presence - the initial presence data
|
|
95
|
+
* @param userId - the id of the user to track
|
|
96
|
+
*/
|
|
97
|
+
trackPresence(presence, userId) {
|
|
98
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").trackPresence(userId || __classPrivateFieldGet(this, _EventResponse_event, "f").sender, presence);
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* @desc Updates a user's presence in the channel
|
|
103
|
+
* @param presence - the updated presence data
|
|
104
|
+
* @param userId - the id of the user to update
|
|
105
|
+
*/
|
|
106
|
+
updatePresence(presence, userId) {
|
|
107
|
+
var _a;
|
|
108
|
+
(_a = __classPrivateFieldGet(this, _EventResponse_engine, "f").presenceEngine) === null || _a === void 0 ? void 0 : _a.updatePresence(userId || __classPrivateFieldGet(this, _EventResponse_event, "f").sender, presence);
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* @desc Removes a user's presence from the channel
|
|
113
|
+
* @param userId - the id of the user to remove
|
|
114
|
+
*/
|
|
115
|
+
unTrackPresence(userId) {
|
|
116
|
+
var _a;
|
|
117
|
+
userId = userId || __classPrivateFieldGet(this, _EventResponse_event, "f").sender;
|
|
118
|
+
(_a = __classPrivateFieldGet(this, _EventResponse_engine, "f").presenceEngine) === null || _a === void 0 ? void 0 : _a.removePresence(userId);
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* @desc Evicts a user from the channel
|
|
123
|
+
* @param reason - the reason for the eviction
|
|
124
|
+
* @param userId - the id of the user to evict,
|
|
125
|
+
*/
|
|
126
|
+
evictUser(reason, userId) {
|
|
127
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").kickUser(userId || __classPrivateFieldGet(this, _EventResponse_event, "f").sender, reason);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* @desc Closes the channel from the server side for all clients
|
|
131
|
+
* @param reason - the reason for closing the channel
|
|
132
|
+
*/
|
|
133
|
+
closeChannel(reason) {
|
|
134
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").destroy(reason);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.EventResponse = EventResponse;
|
|
138
|
+
_EventResponse_event = new WeakMap(), _EventResponse_engine = new WeakMap(), _EventResponse_executed = new WeakMap(), _EventResponse_instances = new WeakSet(), _EventResponse_manageAssigns = function _EventResponse_manageAssigns(assigns) {
|
|
139
|
+
__classPrivateFieldGet(this, _EventResponse_instances, "m", _EventResponse_performChecks).call(this);
|
|
140
|
+
if (assigns) {
|
|
141
|
+
__classPrivateFieldGet(this, _EventResponse_engine, "f").updateAssigns(__classPrivateFieldGet(this, _EventResponse_event, "f").sender, assigns);
|
|
142
|
+
}
|
|
143
|
+
}, _EventResponse_performChecks = function _EventResponse_performChecks() {
|
|
144
|
+
if (__classPrivateFieldGet(this, _EventResponse_executed, "f")) {
|
|
145
|
+
const message = 'Event response has already been executed';
|
|
146
|
+
const code = 403;
|
|
147
|
+
throw new pondError_1.ChannelError(message, code, __classPrivateFieldGet(this, _EventResponse_engine, "f").name);
|
|
148
|
+
}
|
|
149
|
+
__classPrivateFieldSet(this, _EventResponse_executed, true, "f");
|
|
150
|
+
};
|