@eleven-am/pondsocket 0.1.61 → 0.1.63
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 +6 -0
- package/lobby/lobby.js +18 -2
- package/matcher/matcher.js +54 -73
- package/package.json +1 -1
package/channel/channel.js
CHANGED
|
@@ -70,6 +70,12 @@ class ChannelEngine {
|
|
|
70
70
|
__classPrivateFieldGet(this, _ChannelEngine_users, "f").delete(userId);
|
|
71
71
|
__classPrivateFieldGet(this, _ChannelEngine_receiver, "f").unsubscribe(userId);
|
|
72
72
|
(_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId, graceful);
|
|
73
|
+
if (__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback) {
|
|
74
|
+
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback({
|
|
75
|
+
userId,
|
|
76
|
+
assigns: user,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
73
79
|
if (__classPrivateFieldGet(this, _ChannelEngine_users, "f").size === 0) {
|
|
74
80
|
__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel();
|
|
75
81
|
}
|
package/lobby/lobby.js
CHANGED
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
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
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _LobbyEngine_channels, _LobbyEngine_middleware, _PondChannel_lobby;
|
|
13
|
+
var _LobbyEngine_channels, _LobbyEngine_middleware, _LobbyEngine_leaveCallback, _PondChannel_lobby;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.PondChannel = exports.LobbyEngine = void 0;
|
|
16
16
|
const middleware_1 = require("../abstracts/middleware");
|
|
@@ -20,6 +20,7 @@ class LobbyEngine {
|
|
|
20
20
|
constructor() {
|
|
21
21
|
_LobbyEngine_channels.set(this, void 0);
|
|
22
22
|
_LobbyEngine_middleware.set(this, void 0);
|
|
23
|
+
_LobbyEngine_leaveCallback.set(this, void 0);
|
|
23
24
|
__classPrivateFieldSet(this, _LobbyEngine_channels, new Set(), "f");
|
|
24
25
|
__classPrivateFieldSet(this, _LobbyEngine_middleware, new middleware_1.Middleware(), "f");
|
|
25
26
|
}
|
|
@@ -42,6 +43,13 @@ class LobbyEngine {
|
|
|
42
43
|
next();
|
|
43
44
|
});
|
|
44
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* @desc Handles the leave event for a user, can occur when a user disconnects or leaves a channel, use this to clean up any resources
|
|
48
|
+
* @param callback - The callback to execute when a user leaves
|
|
49
|
+
*/
|
|
50
|
+
onLeave(callback) {
|
|
51
|
+
__classPrivateFieldSet(this, _LobbyEngine_leaveCallback, callback, "f");
|
|
52
|
+
}
|
|
45
53
|
/**
|
|
46
54
|
* @desc Broadcasts a message to all users in a channel
|
|
47
55
|
* @param event - The event to broadcast
|
|
@@ -127,6 +135,7 @@ class LobbyEngine {
|
|
|
127
135
|
const parentEngine = {
|
|
128
136
|
execute,
|
|
129
137
|
destroyChannel,
|
|
138
|
+
leaveCallback: __classPrivateFieldGet(this, _LobbyEngine_leaveCallback, "f"),
|
|
130
139
|
};
|
|
131
140
|
const newChannel = new channel_1.ChannelEngine(channelName, parentEngine);
|
|
132
141
|
__classPrivateFieldGet(this, _LobbyEngine_channels, "f").add(newChannel);
|
|
@@ -134,7 +143,7 @@ class LobbyEngine {
|
|
|
134
143
|
}
|
|
135
144
|
}
|
|
136
145
|
exports.LobbyEngine = LobbyEngine;
|
|
137
|
-
_LobbyEngine_channels = new WeakMap(), _LobbyEngine_middleware = new WeakMap();
|
|
146
|
+
_LobbyEngine_channels = new WeakMap(), _LobbyEngine_middleware = new WeakMap(), _LobbyEngine_leaveCallback = new WeakMap();
|
|
138
147
|
class PondChannel {
|
|
139
148
|
constructor(lobby) {
|
|
140
149
|
_PondChannel_lobby.set(this, void 0);
|
|
@@ -169,6 +178,13 @@ class PondChannel {
|
|
|
169
178
|
broadcast(event, payload, channelName) {
|
|
170
179
|
__classPrivateFieldGet(this, _PondChannel_lobby, "f").broadcast(event, payload, channelName);
|
|
171
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* @desc Handles the leave event for a user, can occur when a user disconnects or leaves a channel, use this to clean up any resources
|
|
183
|
+
* @param callback - The callback to execute when a user leaves
|
|
184
|
+
*/
|
|
185
|
+
onLeave(callback) {
|
|
186
|
+
__classPrivateFieldGet(this, _PondChannel_lobby, "f").onLeave(callback);
|
|
187
|
+
}
|
|
172
188
|
}
|
|
173
189
|
exports.PondChannel = PondChannel;
|
|
174
190
|
_PondChannel_lobby = new WeakMap();
|
package/matcher/matcher.js
CHANGED
|
@@ -1,91 +1,72 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseAddress = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* @example
|
|
10
|
-
* /api/:id should match /api/123 and return { id: 123 }
|
|
11
|
-
* /api/:id/:name should match /api/123/abc and return { id: 123, name: abc }
|
|
12
|
-
* hello:id should match hello:123 and return { id: 123 }
|
|
13
|
-
* @private
|
|
14
|
-
*/
|
|
15
|
-
function matchPath(path, address) {
|
|
16
|
-
const pathParts = path.split('/');
|
|
17
|
-
const addressParts = address.split('/');
|
|
18
|
-
const params = {};
|
|
19
|
-
const length = Math.max(pathParts.length, addressParts.length);
|
|
20
|
-
for (let i = 0; i < length; i++) {
|
|
21
|
-
const pathPart = pathParts[i];
|
|
22
|
-
const addressPart = addressParts[i];
|
|
23
|
-
if (pathPart === undefined || addressPart === undefined) {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
else if (pathPart === '*') {
|
|
27
|
-
return params;
|
|
4
|
+
function pathToRegexp(path) {
|
|
5
|
+
const parts = path.split('/');
|
|
6
|
+
const regexpParts = parts.map((part) => {
|
|
7
|
+
if (part.startsWith(':')) {
|
|
8
|
+
return '([^/]+)';
|
|
28
9
|
}
|
|
29
|
-
else if (
|
|
30
|
-
|
|
10
|
+
else if (part === '*') {
|
|
11
|
+
return '(.*)';
|
|
31
12
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
return params;
|
|
13
|
+
return part;
|
|
14
|
+
});
|
|
15
|
+
return new RegExp(`^${regexpParts.join('/')}$`);
|
|
37
16
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
17
|
+
function getParams(path, route) {
|
|
18
|
+
const regexp = pathToRegexp(route);
|
|
19
|
+
const match = path.match(regexp);
|
|
20
|
+
if (!match) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
const cleanRoute = route.replace(/:/g, '');
|
|
24
|
+
const cleanMatch = cleanRoute.match(regexp);
|
|
25
|
+
if (!cleanMatch) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const keys = cleanMatch.slice(1).filter((s) => s !== '');
|
|
29
|
+
const values = match.slice(1).filter((s) => s !== '');
|
|
30
|
+
if (keys.length !== values.length) {
|
|
45
31
|
return {};
|
|
46
32
|
}
|
|
47
|
-
return
|
|
33
|
+
return keys.reduce((params, key, index) => {
|
|
34
|
+
if (key === '*') {
|
|
35
|
+
params[key] = `/${values.slice(index).join('/')}`.replace(/\/+/g, '/');
|
|
36
|
+
return params;
|
|
37
|
+
}
|
|
38
|
+
params[key] = values[index];
|
|
39
|
+
return params;
|
|
40
|
+
}, {});
|
|
48
41
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* /api/id?name=abc should return { name: 'abc' }
|
|
55
|
-
* /api/id?name=abc&age=123 should return { name: 'abc', age: '123' }
|
|
56
|
-
*/
|
|
57
|
-
function getQuery(address) {
|
|
58
|
-
const obj = {};
|
|
59
|
-
const params = address.split('?')[1];
|
|
60
|
-
if (params) {
|
|
61
|
-
params.split('&').forEach((param) => {
|
|
62
|
-
const [key, value] = param.split('=');
|
|
63
|
-
obj[key] = value;
|
|
64
|
-
});
|
|
42
|
+
function getQuery(query) {
|
|
43
|
+
if (!query) {
|
|
44
|
+
return {};
|
|
65
45
|
}
|
|
66
|
-
|
|
46
|
+
const parts = query.split('&');
|
|
47
|
+
return parts.reduce((params, part) => {
|
|
48
|
+
const [key, value] = part.split('=');
|
|
49
|
+
params[key] = value;
|
|
50
|
+
return params;
|
|
51
|
+
}, {});
|
|
67
52
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
*/
|
|
73
|
-
function parseAddress(path, address) {
|
|
74
|
-
let params;
|
|
75
|
-
const [paramsPath] = address.split('?');
|
|
76
|
-
if (typeof path === 'string') {
|
|
77
|
-
params = matchPath(path, paramsPath);
|
|
78
|
-
if (params === null) {
|
|
53
|
+
function parseAddress(route, address) {
|
|
54
|
+
if (route instanceof RegExp) {
|
|
55
|
+
const match = address.match(route);
|
|
56
|
+
if (!match) {
|
|
79
57
|
return null;
|
|
80
58
|
}
|
|
59
|
+
return {
|
|
60
|
+
params: {},
|
|
61
|
+
query: {},
|
|
62
|
+
};
|
|
81
63
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
64
|
+
const [path, queryParams] = address.split('?');
|
|
65
|
+
const params = getParams(path, route);
|
|
66
|
+
if (!params) {
|
|
67
|
+
return null;
|
|
87
68
|
}
|
|
88
|
-
const query = getQuery(
|
|
69
|
+
const query = getQuery(queryParams);
|
|
89
70
|
return {
|
|
90
71
|
params,
|
|
91
72
|
query,
|