@eleven-am/pondsocket 0.1.134 → 0.1.136
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/channel/channel.js +10 -8
- package/endpoint/endpoint.js +70 -56
- package/index.d.ts +1 -1
- package/lobby/lobby.js +2 -3
- package/package.json +3 -3
- package/presence/presence.js +3 -3
- package/types.d.ts +20 -54
- package/types.js +0 -2
package/channel/channel.js
CHANGED
|
@@ -104,27 +104,29 @@ class ChannelEngine {
|
|
|
104
104
|
/**
|
|
105
105
|
* @desc Removes a user from the channel
|
|
106
106
|
* @param userId - The id of the user to remove
|
|
107
|
-
* @param graceful - Whether to remove the user gracefully or not
|
|
108
107
|
*/
|
|
109
|
-
removeUser(userId
|
|
108
|
+
removeUser(userId) {
|
|
110
109
|
var _a;
|
|
111
110
|
const user = __classPrivateFieldGet(this, _ChannelEngine_users, "f").get(userId);
|
|
112
111
|
if (user) {
|
|
113
112
|
__classPrivateFieldGet(this, _ChannelEngine_users, "f").delete(userId);
|
|
114
113
|
__classPrivateFieldGet(this, _ChannelEngine_receiver, "f").unsubscribe(userId);
|
|
115
|
-
(_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId
|
|
114
|
+
const userPresence = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId);
|
|
116
115
|
if (__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback) {
|
|
117
116
|
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback({
|
|
118
|
-
id: userId,
|
|
119
|
-
assigns: user,
|
|
120
117
|
channel: new Channel(this),
|
|
118
|
+
user: {
|
|
119
|
+
id: userId,
|
|
120
|
+
presence: userPresence !== null && userPresence !== void 0 ? userPresence : {},
|
|
121
|
+
assigns: user,
|
|
122
|
+
},
|
|
121
123
|
});
|
|
122
124
|
}
|
|
123
125
|
if (__classPrivateFieldGet(this, _ChannelEngine_users, "f").size === 0) {
|
|
124
126
|
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel();
|
|
125
127
|
}
|
|
126
128
|
}
|
|
127
|
-
else
|
|
129
|
+
else {
|
|
128
130
|
throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
|
|
129
131
|
}
|
|
130
132
|
}
|
|
@@ -153,7 +155,7 @@ class ChannelEngine {
|
|
|
153
155
|
message: reason !== null && reason !== void 0 ? reason : 'Channel has been destroyed',
|
|
154
156
|
});
|
|
155
157
|
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel();
|
|
156
|
-
__classPrivateFieldGet(this, _ChannelEngine_users, "f").forEach((_, userId) =>
|
|
158
|
+
__classPrivateFieldGet(this, _ChannelEngine_users, "f").forEach((_, userId) => this.removeUser(userId));
|
|
157
159
|
}
|
|
158
160
|
/**
|
|
159
161
|
* @desc Updates a user's assigns
|
|
@@ -263,7 +265,7 @@ class ChannelEngine {
|
|
|
263
265
|
}
|
|
264
266
|
exports.ChannelEngine = ChannelEngine;
|
|
265
267
|
_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) {
|
|
266
|
-
__classPrivateFieldGet(this, _ChannelEngine_receiver, "f").subscribeWith(userId, (_a) => {
|
|
268
|
+
return __classPrivateFieldGet(this, _ChannelEngine_receiver, "f").subscribeWith(userId, (_a) => {
|
|
267
269
|
var { recipients } = _a, event = __rest(_a, ["recipients"]);
|
|
268
270
|
if (recipients.includes(userId)) {
|
|
269
271
|
onMessage(event);
|
package/endpoint/endpoint.js
CHANGED
|
@@ -117,13 +117,80 @@ class Endpoint {
|
|
|
117
117
|
});
|
|
118
118
|
socket.addEventListener('close', () => {
|
|
119
119
|
__classPrivateFieldGet(this, _Endpoint_channels, "f")
|
|
120
|
-
.forEach((manager) => manager.removeUser(cache.clientId
|
|
120
|
+
.forEach((manager) => manager.removeUser(cache.clientId));
|
|
121
121
|
});
|
|
122
122
|
socket.addEventListener('error', () => {
|
|
123
123
|
__classPrivateFieldGet(this, _Endpoint_channels, "f")
|
|
124
|
-
.forEach((manager) => manager.removeUser(cache.clientId
|
|
124
|
+
.forEach((manager) => manager.removeUser(cache.clientId));
|
|
125
125
|
});
|
|
126
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
|
+
}
|
|
127
194
|
}
|
|
128
195
|
exports.Endpoint = Endpoint;
|
|
129
196
|
_Endpoint_middleware = new WeakMap(), _Endpoint_channels = new WeakMap(), _Endpoint_sockets = new WeakMap(), _Endpoint_instances = new WeakSet(), _Endpoint_sendMessage = function _Endpoint_sendMessage(socket, message) {
|
|
@@ -161,65 +228,12 @@ _Endpoint_middleware = new WeakMap(), _Endpoint_channels = new WeakMap(), _Endpo
|
|
|
161
228
|
throw new Error(`GatewayEngine: Action ${message.action} does not exist`);
|
|
162
229
|
}
|
|
163
230
|
}, _Endpoint_readMessage = function _Endpoint_readMessage(cache, message) {
|
|
164
|
-
const errorMessage = {
|
|
165
|
-
event: pondsocket_common_1.ErrorTypes.INVALID_MESSAGE,
|
|
166
|
-
action: pondsocket_common_1.ServerActions.ERROR,
|
|
167
|
-
channelName: pondsocket_common_1.SystemSender.ENDPOINT,
|
|
168
|
-
requestId: (0, pondsocket_common_1.uuid)(),
|
|
169
|
-
payload: {},
|
|
170
|
-
};
|
|
171
231
|
try {
|
|
172
232
|
const data = JSON.parse(message);
|
|
173
233
|
const result = schema_1.clientMessageSchema.parse(data);
|
|
174
234
|
__classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_handleMessage).call(this, cache, result);
|
|
175
235
|
}
|
|
176
236
|
catch (e) {
|
|
177
|
-
|
|
178
|
-
errorMessage.payload = {
|
|
179
|
-
message: e.message,
|
|
180
|
-
code: 400,
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
else if (e instanceof SyntaxError) {
|
|
184
|
-
errorMessage.payload = {
|
|
185
|
-
message: 'Invalid JSON',
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
else if (e instanceof Error) {
|
|
189
|
-
errorMessage.event = pondsocket_common_1.ErrorTypes.INTERNAL_SERVER_ERROR;
|
|
190
|
-
errorMessage.payload = {
|
|
191
|
-
message: e.message,
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
else if (e instanceof pondError_1.PresenceError) {
|
|
195
|
-
errorMessage.event = pondsocket_common_1.ErrorTypes.PRESENCE_ERROR;
|
|
196
|
-
errorMessage.channelName = e.channel;
|
|
197
|
-
errorMessage.payload = {
|
|
198
|
-
message: e.message,
|
|
199
|
-
code: e.code,
|
|
200
|
-
action: e.event,
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
else if (e instanceof pondError_1.ChannelError) {
|
|
204
|
-
errorMessage.event = pondsocket_common_1.ErrorTypes.CHANNEL_ERROR;
|
|
205
|
-
errorMessage.channelName = e.channel;
|
|
206
|
-
errorMessage.payload = {
|
|
207
|
-
message: e.message,
|
|
208
|
-
code: e.code,
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
else if (e instanceof pondError_1.EndpointError) {
|
|
212
|
-
errorMessage.event = pondsocket_common_1.ErrorTypes.ENDPOINT_ERROR;
|
|
213
|
-
errorMessage.payload = {
|
|
214
|
-
message: e.message,
|
|
215
|
-
code: e.code,
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
errorMessage.payload = {
|
|
220
|
-
message: 'Unknown error',
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
__classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_sendMessage).call(this, cache.socket, errorMessage);
|
|
237
|
+
__classPrivateFieldGet(this, _Endpoint_instances, "m", _Endpoint_sendMessage).call(this, cache.socket, this.buildError(e));
|
|
224
238
|
}
|
|
225
239
|
};
|
package/index.d.ts
CHANGED
package/lobby/lobby.js
CHANGED
|
@@ -79,11 +79,10 @@ class LobbyEngine {
|
|
|
79
79
|
/**
|
|
80
80
|
* @desc Removes a user from all channels
|
|
81
81
|
* @param clientId - The client id of the user to remove
|
|
82
|
-
* @param graceful - Whether to gracefully remove the user or not
|
|
83
82
|
*/
|
|
84
|
-
removeUser(clientId
|
|
83
|
+
removeUser(clientId) {
|
|
85
84
|
__classPrivateFieldGet(this, _LobbyEngine_channels, "f").forEach((channel) => {
|
|
86
|
-
channel.removeUser(clientId
|
|
85
|
+
channel.removeUser(clientId);
|
|
87
86
|
});
|
|
88
87
|
}
|
|
89
88
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eleven-am/pondsocket",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.136",
|
|
4
4
|
"description": "PondSocket is a fast simple socket server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"socket",
|
|
@@ -34,13 +34,13 @@
|
|
|
34
34
|
"url": "git+https://github.com/Eleven-am/pondSocket.git"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@eleven-am/pondsocket-common": "^0.0.
|
|
37
|
+
"@eleven-am/pondsocket-common": "^0.0.10",
|
|
38
38
|
"ws": "^8.16.0",
|
|
39
39
|
"zod": "^3.22.4"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/jest": "^29.5.11",
|
|
43
|
-
"@types/node": "^20.11.
|
|
43
|
+
"@types/node": "^20.11.5",
|
|
44
44
|
"@types/websocket": "^1.0.10",
|
|
45
45
|
"@types/ws": "^8.5.10",
|
|
46
46
|
"@typescript-eslint/eslint-plugin": "^6.19.0",
|
package/presence/presence.js
CHANGED
|
@@ -62,9 +62,8 @@ class PresenceEngine {
|
|
|
62
62
|
/**
|
|
63
63
|
* @desc Removes a presence from the presence engine
|
|
64
64
|
* @param presenceKey - The key of the presence
|
|
65
|
-
* @param graceful - Whether to gracefully remove the presence
|
|
66
65
|
*/
|
|
67
|
-
removePresence(presenceKey
|
|
66
|
+
removePresence(presenceKey) {
|
|
68
67
|
const presence = __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").get(presenceKey);
|
|
69
68
|
if (presence) {
|
|
70
69
|
__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").delete(presenceKey);
|
|
@@ -73,11 +72,12 @@ class PresenceEngine {
|
|
|
73
72
|
presence: Array.from(__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").values()),
|
|
74
73
|
});
|
|
75
74
|
}
|
|
76
|
-
else
|
|
75
|
+
else {
|
|
77
76
|
const code = 404;
|
|
78
77
|
const message = `PresenceEngine: Presence with key ${presenceKey} does not exist`;
|
|
79
78
|
throw new pondError_1.PresenceError(message, code, __classPrivateFieldGet(this, _PresenceEngine_channel, "f").name, pondsocket_common_1.PresenceEventTypes.LEAVE);
|
|
80
79
|
}
|
|
80
|
+
return presence;
|
|
81
81
|
}
|
|
82
82
|
/**
|
|
83
83
|
* @desc Updates a presence
|
package/types.d.ts
CHANGED
|
@@ -1,57 +1,23 @@
|
|
|
1
|
-
import { Server as HTTPServer
|
|
2
|
-
|
|
1
|
+
import { Server as HTTPServer } from 'http';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
PondMessage,
|
|
5
|
+
PondPresence,
|
|
6
|
+
PondAssigns,
|
|
7
|
+
PondEvent,
|
|
8
|
+
PondPath,
|
|
9
|
+
JoinParams,
|
|
10
|
+
PondEventMap,
|
|
11
|
+
IncomingConnection, UserData,
|
|
12
|
+
} from '@eleven-am/pondsocket-common';
|
|
3
13
|
import { WebSocketServer } from 'ws';
|
|
4
14
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
type FilteredParams<Path> = Path extends `${infer First}/${infer Second}`
|
|
9
|
-
? IsParam<First> | FilteredParams<Second>
|
|
10
|
-
: IsParam<Path>
|
|
11
|
-
|
|
12
|
-
type Params<Path> = {
|
|
13
|
-
[Key in FilteredParams<Path>]: string
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
type PondPath<Path extends string> = Path | RegExp;
|
|
17
|
-
|
|
18
|
-
type EventParams<Path> = {
|
|
19
|
-
query: Record<string, string>;
|
|
20
|
-
params: Params<Path>;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
type PondObject = default_t;
|
|
24
|
-
type PondPresence = PondObject;
|
|
25
|
-
type PondMessage = PondObject;
|
|
26
|
-
type PondAssigns = PondObject;
|
|
27
|
-
type JoinParams = PondObject;
|
|
28
|
-
|
|
29
|
-
type PondEvent<Path> = EventParams<Path> & {
|
|
30
|
-
payload: PondMessage;
|
|
31
|
-
event: string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
type IncomingConnection<Path> = EventParams<Path> & {
|
|
35
|
-
id: string;
|
|
36
|
-
headers: IncomingHttpHeaders;
|
|
37
|
-
address: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
interface LeaveEvent<EventTypes extends PondEvenType = PondEvenType, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
41
|
-
id: string;
|
|
42
|
-
assigns: AssignType;
|
|
15
|
+
interface LeaveEvent<EventTypes extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
16
|
+
user: UserData<PresenceType, AssignType>;
|
|
43
17
|
channel: Channel<EventTypes, PresenceType, AssignType>;
|
|
44
18
|
}
|
|
45
19
|
|
|
46
|
-
type LeaveCallback<EventTypes extends
|
|
47
|
-
|
|
48
|
-
type PondEvenType = { [key: string]: PondMessage };
|
|
49
|
-
|
|
50
|
-
interface UserData<PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
51
|
-
assigns: AssignType;
|
|
52
|
-
presence: PresenceType;
|
|
53
|
-
id: string;
|
|
54
|
-
}
|
|
20
|
+
type LeaveCallback<EventTypes extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> = (event: LeaveEvent<EventTypes, PresenceType, AssignType>) => void;
|
|
55
21
|
|
|
56
22
|
declare class AbstractRequest<Path extends string, PresenceType extends PondPresence, AssignType extends PondAssigns> {
|
|
57
23
|
event: PondEvent<Path>;
|
|
@@ -69,7 +35,7 @@ declare class EventRequest<Path extends string, PresenceType extends PondPresenc
|
|
|
69
35
|
channel: Channel;
|
|
70
36
|
}
|
|
71
37
|
|
|
72
|
-
declare class EventResponse<EventType extends
|
|
38
|
+
declare class EventResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
73
39
|
/**
|
|
74
40
|
* @desc Whether the server has responded to the request
|
|
75
41
|
*/
|
|
@@ -176,7 +142,7 @@ declare class Endpoint {
|
|
|
176
142
|
* response.reject('You are not an admin', 403);
|
|
177
143
|
* });
|
|
178
144
|
*/
|
|
179
|
-
createChannel<Path extends string, EventType extends
|
|
145
|
+
createChannel<Path extends string, EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> (path: PondPath<Path>, handler: (request: JoinRequest<Path, PresenceType, AssignType>, response: JoinResponse<EventType, PresenceType, AssignType>) => void | Promise<void>): PondChannel<EventType, PresenceType, AssignType>;
|
|
180
146
|
|
|
181
147
|
/**
|
|
182
148
|
* @desc Broadcasts a message to all clients connected to this endpoint
|
|
@@ -192,7 +158,7 @@ declare class Endpoint {
|
|
|
192
158
|
closeConnection (clientIds: string | string[]): void;
|
|
193
159
|
}
|
|
194
160
|
|
|
195
|
-
export declare class Channel<EventType extends
|
|
161
|
+
export declare class Channel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
196
162
|
/**
|
|
197
163
|
* The name of the channel.
|
|
198
164
|
*/
|
|
@@ -281,7 +247,7 @@ declare class JoinRequest<Path extends string, PresenceType extends PondPresence
|
|
|
281
247
|
channel: Channel;
|
|
282
248
|
}
|
|
283
249
|
|
|
284
|
-
declare class JoinResponse<EventType extends
|
|
250
|
+
declare class JoinResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
285
251
|
/**
|
|
286
252
|
* @desc Whether the server has responded to the request
|
|
287
253
|
*/
|
|
@@ -365,7 +331,7 @@ declare class ConnectionResponse {
|
|
|
365
331
|
send (event: string, payload: PondMessage, assigns?: PondAssigns): void;
|
|
366
332
|
}
|
|
367
333
|
|
|
368
|
-
export declare class PondChannel <EventType extends
|
|
334
|
+
export declare class PondChannel <EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
369
335
|
/**
|
|
370
336
|
* @desc Handles an event request made by a user
|
|
371
337
|
* @param event - The event to listen for
|
package/types.js
DELETED