@eleven-am/pondsocket 0.1.141 → 0.1.142
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/README.md +15 -15
- package/channel/channel.js +81 -61
- package/channel/channel.test.js +86 -61
- package/channel/eventRequest.test.js +3 -3
- package/channel/eventResponse.js +36 -61
- package/channel/eventResponse.test.js +31 -53
- package/endpoint/endpoint.test.js +30 -13
- package/endpoint/endpointEngine.js +321 -0
- package/endpoint/response.js +48 -14
- package/lobby/JoinRequest.test.js +17 -6
- package/lobby/JoinResponse.test.js +18 -23
- package/lobby/joinResponse.js +70 -31
- package/lobby/lobby.js +18 -22
- package/package.json +8 -8
- package/presence/presence.js +1 -1
- package/presence/presenceEngine.test.js +1 -4
- package/schema.js +0 -1
- package/server/pondSocket.js +9 -6
- package/types.d.ts +200 -138
- package/abstracts/abstractResponse.js +0 -9
- package/endpoint/endpoint.js +0 -239
package/endpoint/endpoint.js
DELETED
|
@@ -1,239 +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 _Endpoint_instances, _Endpoint_middleware, _Endpoint_channels, _Endpoint_sockets, _Endpoint_sendMessage, _Endpoint_joinChannel, _Endpoint_execute, _Endpoint_handleMessage, _Endpoint_readMessage;
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.Endpoint = void 0;
|
|
16
|
-
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
17
|
-
const zod_1 = require("zod");
|
|
18
|
-
const middleware_1 = require("../abstracts/middleware");
|
|
19
|
-
const pondError_1 = require("../errors/pondError");
|
|
20
|
-
const joinRequest_1 = require("../lobby/joinRequest");
|
|
21
|
-
const joinResponse_1 = require("../lobby/joinResponse");
|
|
22
|
-
const lobby_1 = require("../lobby/lobby");
|
|
23
|
-
const matcher_1 = require("../matcher/matcher");
|
|
24
|
-
const schema_1 = require("../schema");
|
|
25
|
-
class Endpoint {
|
|
26
|
-
constructor() {
|
|
27
|
-
_Endpoint_instances.add(this);
|
|
28
|
-
_Endpoint_middleware.set(this, void 0);
|
|
29
|
-
_Endpoint_channels.set(this, void 0);
|
|
30
|
-
_Endpoint_sockets.set(this, void 0);
|
|
31
|
-
__classPrivateFieldSet(this, _Endpoint_sockets, new Set(), "f");
|
|
32
|
-
__classPrivateFieldSet(this, _Endpoint_middleware, new middleware_1.Middleware(), "f");
|
|
33
|
-
__classPrivateFieldSet(this, _Endpoint_channels, new Set(), "f");
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* @desc Adds a new PondChannel to this path on this endpoint
|
|
37
|
-
* @param path - The path to add the channel to
|
|
38
|
-
* @param handler - The handler to use to authenticate the client
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* const channel = endpoint.createChannel('/chat', (request, response) => {
|
|
42
|
-
* if (request.user.assigns.admin)
|
|
43
|
-
* response.accept();
|
|
44
|
-
*
|
|
45
|
-
* else
|
|
46
|
-
* response.reject('You are not an admin', 403);
|
|
47
|
-
* });
|
|
48
|
-
*/
|
|
49
|
-
createChannel(path, handler) {
|
|
50
|
-
const pondChannel = new lobby_1.LobbyEngine();
|
|
51
|
-
__classPrivateFieldGet(this, _Endpoint_middleware, "f").use((user, joinParams, next) => {
|
|
52
|
-
const event = (0, matcher_1.parseAddress)(path, user.channelName);
|
|
53
|
-
if (event) {
|
|
54
|
-
const newChannel = pondChannel.getChannel(user.channelName) || pondChannel.createChannel(user.channelName);
|
|
55
|
-
const request = new joinRequest_1.JoinRequest(user, joinParams, newChannel);
|
|
56
|
-
const response = new joinResponse_1.JoinResponse(user, newChannel);
|
|
57
|
-
if (request._parseQueries(path)) {
|
|
58
|
-
return handler(request, response);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
next();
|
|
62
|
-
});
|
|
63
|
-
__classPrivateFieldGet(this, _Endpoint_channels, "f").add(pondChannel);
|
|
64
|
-
return new lobby_1.PondChannel(pondChannel);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* @desc List all clients connected to this endpoint
|
|
68
|
-
*/
|
|
69
|
-
listConnections() {
|
|
70
|
-
return [...__classPrivateFieldGet(this, _Endpoint_sockets, "f")].map(({ clientId }) => clientId);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* @desc Gets all clients connected to this endpoint
|
|
74
|
-
*/
|
|
75
|
-
getClients() {
|
|
76
|
-
return [...__classPrivateFieldGet(this, _Endpoint_sockets, "f")];
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* @desc Broadcasts a message to all clients connected to this endpoint
|
|
80
|
-
* @param event - The event to broadcast
|
|
81
|
-
* @param payload - The payload to broadcast
|
|
82
|
-
*/
|
|
83
|
-
broadcast(event, payload) {
|
|
84
|
-
__classPrivateFieldGet(this, _Endpoint_sockets, "f").forEach(({ socket }) => {
|
|
85
|
-
const message = {
|
|
86
|
-
event,
|
|
87
|
-
payload,
|
|
88
|
-
requestId: (0, pondsocket_common_1.uuid)(),
|
|
89
|
-
action: pondsocket_common_1.ServerActions.BROADCAST,
|
|
90
|
-
channelName: pondsocket_common_1.SystemSender.ENDPOINT,
|
|
91
|
-
};
|
|
92
|
-
__classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_sendMessage).call(this, socket, message);
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* @desc Closes specific clients connected to this endpoint
|
|
97
|
-
* @param clientIds - The id for the client / clients to close
|
|
98
|
-
*/
|
|
99
|
-
closeConnection(clientIds) {
|
|
100
|
-
const clients = typeof clientIds === 'string' ? [clientIds] : clientIds;
|
|
101
|
-
this.getClients()
|
|
102
|
-
.forEach(({ clientId, socket }) => {
|
|
103
|
-
if (clients.includes(clientId)) {
|
|
104
|
-
socket.close();
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* @desc Manages a new socket connection
|
|
110
|
-
* @param cache - The socket cache
|
|
111
|
-
*/
|
|
112
|
-
manageSocket(cache) {
|
|
113
|
-
__classPrivateFieldGet(this, _Endpoint_sockets, "f").add(cache);
|
|
114
|
-
const socket = cache.socket;
|
|
115
|
-
socket.addEventListener('message', (message) => {
|
|
116
|
-
__classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_readMessage).call(this, cache, message.data);
|
|
117
|
-
});
|
|
118
|
-
socket.addEventListener('close', () => {
|
|
119
|
-
__classPrivateFieldGet(this, _Endpoint_channels, "f")
|
|
120
|
-
.forEach((manager) => manager.removeUser(cache.clientId));
|
|
121
|
-
});
|
|
122
|
-
socket.addEventListener('error', () => {
|
|
123
|
-
__classPrivateFieldGet(this, _Endpoint_channels, "f")
|
|
124
|
-
.forEach((manager) => manager.removeUser(cache.clientId));
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* @desc Builds an error message
|
|
129
|
-
* @param error - The error to build
|
|
130
|
-
* @private
|
|
131
|
-
*/
|
|
132
|
-
buildError(error) {
|
|
133
|
-
const event = {
|
|
134
|
-
event: pondsocket_common_1.ErrorTypes.INVALID_MESSAGE,
|
|
135
|
-
action: pondsocket_common_1.ServerActions.ERROR,
|
|
136
|
-
channelName: pondsocket_common_1.SystemSender.ENDPOINT,
|
|
137
|
-
requestId: (0, pondsocket_common_1.uuid)(),
|
|
138
|
-
payload: {},
|
|
139
|
-
};
|
|
140
|
-
if (error instanceof SyntaxError) {
|
|
141
|
-
event.payload = {
|
|
142
|
-
message: 'Invalid JSON',
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
else if (error instanceof Error) {
|
|
146
|
-
event.event = pondsocket_common_1.ErrorTypes.INTERNAL_SERVER_ERROR;
|
|
147
|
-
event.payload = {
|
|
148
|
-
message: error.message,
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
else if (error instanceof pondError_1.PresenceError) {
|
|
152
|
-
event.event = pondsocket_common_1.ErrorTypes.PRESENCE_ERROR;
|
|
153
|
-
event.channelName = error.channel;
|
|
154
|
-
event.payload = {
|
|
155
|
-
message: error.message,
|
|
156
|
-
code: error.code,
|
|
157
|
-
action: error.event,
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
else if (error instanceof pondError_1.ChannelError) {
|
|
161
|
-
event.event = pondsocket_common_1.ErrorTypes.CHANNEL_ERROR;
|
|
162
|
-
event.channelName = error.channel;
|
|
163
|
-
event.payload = {
|
|
164
|
-
message: error.message,
|
|
165
|
-
code: error.code,
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
else if (error instanceof pondError_1.EndpointError) {
|
|
169
|
-
event.event = pondsocket_common_1.ErrorTypes.ENDPOINT_ERROR;
|
|
170
|
-
event.payload = {
|
|
171
|
-
message: error.message,
|
|
172
|
-
code: error.code,
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
else if (error instanceof zod_1.ZodError) {
|
|
176
|
-
const message = error.errors.map((error) => {
|
|
177
|
-
if ('expected' in error && 'received' in error) {
|
|
178
|
-
return `Expected ${error.path.join('.')} to be ${error.expected}, but received ${error.received}`;
|
|
179
|
-
}
|
|
180
|
-
return `${error.path.join('.')} ${error.message}`;
|
|
181
|
-
}).join(', ');
|
|
182
|
-
event.payload = {
|
|
183
|
-
message,
|
|
184
|
-
code: 400,
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
else {
|
|
188
|
-
event.payload = {
|
|
189
|
-
message: 'Unknown error',
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
return event;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
exports.Endpoint = Endpoint;
|
|
196
|
-
_Endpoint_middleware = new WeakMap(), _Endpoint_channels = new WeakMap(), _Endpoint_sockets = new WeakMap(), _Endpoint_instances = new WeakSet(), _Endpoint_sendMessage = function _Endpoint_sendMessage(socket, message) {
|
|
197
|
-
socket.send(JSON.stringify(message));
|
|
198
|
-
}, _Endpoint_joinChannel = function _Endpoint_joinChannel(channel, socket, joinParams, requestId) {
|
|
199
|
-
const cache = Object.assign(Object.assign({}, socket), { requestId, channelName: channel });
|
|
200
|
-
__classPrivateFieldGet(this, _Endpoint_middleware, "f").run(cache, joinParams, () => {
|
|
201
|
-
throw new pondError_1.EndpointError(`GatewayEngine: Channel ${channel} does not exist`, 404);
|
|
202
|
-
});
|
|
203
|
-
}, _Endpoint_execute = function _Endpoint_execute(channel, handler) {
|
|
204
|
-
for (const manager of __classPrivateFieldGet(this, _Endpoint_channels, "f")) {
|
|
205
|
-
const isPresent = manager.listChannels()
|
|
206
|
-
.includes(channel);
|
|
207
|
-
if (isPresent) {
|
|
208
|
-
return manager.execute(channel, handler);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
throw new Error(`GatewayEngine: Channel ${channel} does not exist`);
|
|
212
|
-
}, _Endpoint_handleMessage = function _Endpoint_handleMessage(cache, message) {
|
|
213
|
-
switch (message.action) {
|
|
214
|
-
case pondsocket_common_1.ClientActions.JOIN_CHANNEL:
|
|
215
|
-
__classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_joinChannel).call(this, message.channelName, cache, message.payload, message.requestId);
|
|
216
|
-
break;
|
|
217
|
-
case pondsocket_common_1.ClientActions.LEAVE_CHANNEL:
|
|
218
|
-
__classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_execute).call(this, message.channelName, (channel) => {
|
|
219
|
-
channel.removeUser(cache.clientId, true);
|
|
220
|
-
});
|
|
221
|
-
break;
|
|
222
|
-
case pondsocket_common_1.ClientActions.BROADCAST:
|
|
223
|
-
__classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_execute).call(this, message.channelName, (channel) => {
|
|
224
|
-
channel.broadcastMessage(cache.clientId, message);
|
|
225
|
-
});
|
|
226
|
-
break;
|
|
227
|
-
default:
|
|
228
|
-
throw new Error(`GatewayEngine: Action ${message.action} does not exist`);
|
|
229
|
-
}
|
|
230
|
-
}, _Endpoint_readMessage = function _Endpoint_readMessage(cache, message) {
|
|
231
|
-
try {
|
|
232
|
-
const data = JSON.parse(message);
|
|
233
|
-
const result = schema_1.clientMessageSchema.parse(data);
|
|
234
|
-
__classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_handleMessage).call(this, cache, result);
|
|
235
|
-
}
|
|
236
|
-
catch (e) {
|
|
237
|
-
__classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_sendMessage).call(this, cache.socket, this.buildError(e));
|
|
238
|
-
}
|
|
239
|
-
};
|