@eleven-am/pondsocket-nest 0.0.60 → 0.0.62
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 +11 -2
- package/helpers/createParamDecorator.js +1 -3
- package/index.d.ts +5 -6
- package/package.json +7 -7
- package/performers/action.js +11 -16
- package/performers/errors.js +1 -3
- package/performers/response.js +8 -5
- package/services/pondSocket.js +17 -34
package/README.md
CHANGED
|
@@ -190,7 +190,7 @@ const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
|
|
|
190
190
|
res.accept({role}); // Assign the user's role to the socket
|
|
191
191
|
} else {
|
|
192
192
|
// Reject the connection for invalid users or without a token
|
|
193
|
-
res.
|
|
193
|
+
res.reject('Invalid token', 401);
|
|
194
194
|
}
|
|
195
195
|
});
|
|
196
196
|
|
|
@@ -217,7 +217,16 @@ const profanityChannel = endpoint.createChannel('/channel/:id', async (req, res)
|
|
|
217
217
|
onlineSince: Date.now(),
|
|
218
218
|
})
|
|
219
219
|
// and send the user the channel history
|
|
220
|
-
.
|
|
220
|
+
.sendToUsers('history', {messages}, [req.user.id]);
|
|
221
|
+
|
|
222
|
+
// Alternatively, you can also send messages to the user, NOTE that the user would be automatically subscribed to the channel.
|
|
223
|
+
// res.send('history', { messages }, { username, profanityCount: 0 })
|
|
224
|
+
// .trackPresence({
|
|
225
|
+
// username,
|
|
226
|
+
// role,
|
|
227
|
+
// status: 'online',
|
|
228
|
+
// onlineSince: Date.now(),
|
|
229
|
+
// });
|
|
221
230
|
} else {
|
|
222
231
|
// Reject the join request
|
|
223
232
|
res.decline('You do not have the required role to join this channel', 403);
|
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createParamDecorator = void 0;
|
|
4
|
-
require("reflect-metadata");
|
|
5
4
|
const parametres_1 = require("../managers/parametres");
|
|
6
5
|
function createParamDecorator(callback) {
|
|
7
6
|
return (data) => (target, propertyKey, index) => {
|
|
8
7
|
const { set } = (0, parametres_1.manageParameters)(target, propertyKey);
|
|
9
|
-
|
|
10
|
-
set(index, (context) => callback(data, context, type));
|
|
8
|
+
set(index, (context) => callback(data, context));
|
|
11
9
|
};
|
|
12
10
|
}
|
|
13
11
|
exports.createParamDecorator = createParamDecorator;
|
package/index.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ import type { DynamicModule, ModuleMetadata } from '@nestjs/common';
|
|
|
18
18
|
|
|
19
19
|
type Constructor<T> = new (...args: any[]) => T;
|
|
20
20
|
|
|
21
|
-
type ParamDecoratorCallback<Input
|
|
21
|
+
type ParamDecoratorCallback<Input> = (data: Input, context: Context) => unknown | Promise<unknown>;
|
|
22
22
|
|
|
23
23
|
interface CanActivate {
|
|
24
24
|
|
|
@@ -38,8 +38,7 @@ interface Metadata extends Omit<ModuleMetadata, 'controllers'> {
|
|
|
38
38
|
type NestFuncType<Event extends string, Payload extends PondMessage, Presence extends PondPresence, Assigns extends PondAssigns = PondAssigns> = {
|
|
39
39
|
event?: Event;
|
|
40
40
|
broadcast?: Event;
|
|
41
|
-
|
|
42
|
-
assigns?: Partial<Assigns>;
|
|
41
|
+
assigns?: Assigns;
|
|
43
42
|
presence?: Presence;
|
|
44
43
|
subscribeTo?: string[];
|
|
45
44
|
unsubscribeFrom?: string[];
|
|
@@ -276,7 +275,7 @@ declare function OnConnectionRequest(): MethodDecorator;
|
|
|
276
275
|
/**
|
|
277
276
|
* @desc Method decorator that marks a method as an onEvent handler
|
|
278
277
|
*/
|
|
279
|
-
declare function OnEvent
|
|
278
|
+
declare function OnEvent(path?: string): MethodDecorator;
|
|
280
279
|
|
|
281
280
|
/**
|
|
282
281
|
* @desc Method decorator that marks a method as an onJoin handler
|
|
@@ -292,13 +291,13 @@ declare function OnLeave(): MethodDecorator;
|
|
|
292
291
|
* @desc Decorator that adds guards to a channel's or endpoint's class or method
|
|
293
292
|
* @param guards - The guards to add. It is important to add the guards to a providers array in any module (only necessary if the guards inject dependencies)
|
|
294
293
|
*/
|
|
295
|
-
declare function PondGuards (...guards: Constructor<CanActivate>[]): ClassDecorator
|
|
294
|
+
declare function PondGuards (...guards: Constructor<CanActivate>[]): ClassDecorator & MethodDecorator;
|
|
296
295
|
|
|
297
296
|
/**
|
|
298
297
|
* @desc Helper function that creates a parameter decorator
|
|
299
298
|
* @param callback - The callback to run when the parameter is being retrieved
|
|
300
299
|
*/
|
|
301
|
-
declare function createParamDecorator<Input
|
|
300
|
+
declare function createParamDecorator<Input>(callback: ParamDecoratorCallback<Input>): (data: Input) => ParameterDecorator;
|
|
302
301
|
|
|
303
302
|
declare class PondSocketModule {
|
|
304
303
|
static forRoot({ guards, providers, imports, exports, isGlobal, appModuleName }: Metadata): DynamicModule;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eleven-am/pondsocket-nest",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.62",
|
|
4
4
|
"description": "PondSocket is a fast simple socket server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"socket",
|
|
@@ -28,14 +28,14 @@
|
|
|
28
28
|
"pipeline": "npm run lint && npm run build && npm run push"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@eleven-am/pondsocket": "^0.1.
|
|
32
|
-
"@golevelup/nestjs-discovery": "^4.0.
|
|
31
|
+
"@eleven-am/pondsocket": "^0.1.158",
|
|
32
|
+
"@golevelup/nestjs-discovery": "^4.0.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@nestjs/common": "^10.3.
|
|
36
|
-
"@nestjs/core": "^10.3.
|
|
35
|
+
"@nestjs/common": "^10.3.3",
|
|
36
|
+
"@nestjs/core": "^10.3.3",
|
|
37
37
|
"@types/jest": "^29.5.12",
|
|
38
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
38
|
+
"@typescript-eslint/eslint-plugin": "^7.1.0",
|
|
39
39
|
"eslint-plugin-file-progress": "^1.3.0",
|
|
40
40
|
"eslint-plugin-import": "^2.29.1",
|
|
41
41
|
"jest": "^29.7.0",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"ts-jest": "^29.1.2",
|
|
44
44
|
"ts-loader": "^9.5.1",
|
|
45
45
|
"ts-node": "^10.9.2",
|
|
46
|
-
"typescript": "^5.
|
|
46
|
+
"typescript": "^5.3.3"
|
|
47
47
|
},
|
|
48
48
|
"jest": {
|
|
49
49
|
"moduleFileExtensions": [
|
package/performers/action.js
CHANGED
|
@@ -16,21 +16,16 @@ const response_1 = require("./response");
|
|
|
16
16
|
const context_1 = require("../context/context");
|
|
17
17
|
const parametres_1 = require("../managers/parametres");
|
|
18
18
|
function retrieveParameters(context) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}));
|
|
29
|
-
const values = yield Promise.all(promises);
|
|
30
|
-
return values
|
|
31
|
-
.sort((a, b) => a.index - b.index)
|
|
32
|
-
.map(({ value }) => value);
|
|
33
|
-
});
|
|
19
|
+
var _a;
|
|
20
|
+
const gottenValues = (_a = (0, parametres_1.manageParameters)(context.getInstance(), context.getMethod()).get()) !== null && _a !== void 0 ? _a : [];
|
|
21
|
+
const values = gottenValues
|
|
22
|
+
.map(({ callback, index }) => ({
|
|
23
|
+
value: callback(context),
|
|
24
|
+
index,
|
|
25
|
+
}));
|
|
26
|
+
return values
|
|
27
|
+
.sort((a, b) => a.index - b.index)
|
|
28
|
+
.map(({ value }) => value);
|
|
34
29
|
}
|
|
35
30
|
function performAction(instance, moduleRef, originalMethod, propertyKey, leaveEvent, request, response) {
|
|
36
31
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -58,7 +53,7 @@ function performAction(instance, moduleRef, originalMethod, propertyKey, leaveEv
|
|
|
58
53
|
const socketId = context.user.id;
|
|
59
54
|
const canProceed = yield (0, guards_1.performGuards)(moduleRef, context);
|
|
60
55
|
if (canProceed) {
|
|
61
|
-
const data = yield originalMethod.apply(instance,
|
|
56
|
+
const data = yield originalMethod.apply(instance, retrieveParameters(context));
|
|
62
57
|
(0, response_1.performResponse)(socketId, channel, data, response);
|
|
63
58
|
}
|
|
64
59
|
else if (response && ((0, narrow_1.isJoinResponse)(response) || (0, narrow_1.isConnectionResponse)(response))) {
|
package/performers/errors.js
CHANGED
|
@@ -28,8 +28,6 @@ function performErrors(error, response) {
|
|
|
28
28
|
if (!response.hasResponded) {
|
|
29
29
|
return response.decline(message, status);
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
console.error(error);
|
|
33
|
-
}
|
|
31
|
+
throw error;
|
|
34
32
|
}
|
|
35
33
|
exports.performErrors = performErrors;
|
package/performers/response.js
CHANGED
|
@@ -23,7 +23,7 @@ function performResponse(socketId, channel, data, response) {
|
|
|
23
23
|
if (response && response.hasResponded || !isNotEmpty(data)) {
|
|
24
24
|
return;
|
|
25
25
|
}
|
|
26
|
-
const { event, presence, assigns, broadcast,
|
|
26
|
+
const { event, presence, assigns, broadcast, subscribeTo, unsubscribeFrom } = data, rest = __rest(data, ["event", "presence", "assigns", "broadcast", "subscribeTo", "unsubscribeFrom"]);
|
|
27
27
|
if (response) {
|
|
28
28
|
if ((0, narrow_1.isConnectionResponse)(response) || (0, narrow_1.isJoinResponse)(response)) {
|
|
29
29
|
response
|
|
@@ -41,9 +41,6 @@ function performResponse(socketId, channel, data, response) {
|
|
|
41
41
|
if (broadcast && ((0, narrow_1.isJoinResponse)(response) || (0, narrow_1.isEventResponse)(response))) {
|
|
42
42
|
response.broadcast(broadcast, rest);
|
|
43
43
|
}
|
|
44
|
-
if (broadcastFrom && (0, narrow_1.isEventResponse)(response)) {
|
|
45
|
-
response.broadcastFrom(broadcastFrom, rest);
|
|
46
|
-
}
|
|
47
44
|
}
|
|
48
45
|
if (subscribeTo) {
|
|
49
46
|
subscribeTo.forEach((channelName) => {
|
|
@@ -61,7 +58,13 @@ function performResponse(socketId, channel, data, response) {
|
|
|
61
58
|
channel.broadcast(broadcast, rest);
|
|
62
59
|
}
|
|
63
60
|
if (isNotEmpty(presence)) {
|
|
64
|
-
channel.
|
|
61
|
+
const existingPresence = channel.getPresences()[socketId];
|
|
62
|
+
if (existingPresence) {
|
|
63
|
+
channel.updatePresence(socketId, presence);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
channel.trackPresence(socketId, presence);
|
|
67
|
+
}
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
}
|
package/services/pondSocket.js
CHANGED
|
@@ -15,8 +15,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.PondSocketService = void 0;
|
|
16
16
|
const http_1 = require("http");
|
|
17
17
|
const pondsocket_1 = __importDefault(require("@eleven-am/pondsocket"));
|
|
18
|
-
// eslint-disable-next-line import/no-unresolved
|
|
19
|
-
const common_1 = require("@nestjs/common");
|
|
20
18
|
const constants_1 = require("../constants");
|
|
21
19
|
const channel_1 = require("../managers/channel");
|
|
22
20
|
const channelInstance_1 = require("../managers/channelInstance");
|
|
@@ -33,9 +31,11 @@ class PondSocketService {
|
|
|
33
31
|
this.discovery = discovery;
|
|
34
32
|
this.adapterHost = adapterHost;
|
|
35
33
|
this.externalGuards = externalGuards;
|
|
36
|
-
this.logger = new common_1.Logger(PondSocketService.name);
|
|
37
34
|
const httpAdapter = this.adapterHost.httpAdapter;
|
|
38
|
-
void
|
|
35
|
+
httpAdapter.listen = (...args) => __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
const socket = yield this.init(httpAdapter);
|
|
37
|
+
return socket.listen(...args);
|
|
38
|
+
});
|
|
39
39
|
}
|
|
40
40
|
init(httpAdapter) {
|
|
41
41
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -46,7 +46,7 @@ class PondSocketService {
|
|
|
46
46
|
groupedInstances.forEach((groupedInstance) => {
|
|
47
47
|
this.manageEndpoint(socket, groupedInstance);
|
|
48
48
|
});
|
|
49
|
-
|
|
49
|
+
return socket;
|
|
50
50
|
});
|
|
51
51
|
}
|
|
52
52
|
manageEndpoint(socket, groupedInstance) {
|
|
@@ -59,10 +59,9 @@ class PondSocketService {
|
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
61
|
setGuards(this.externalGuards);
|
|
62
|
-
const { get } = (0, connection_1.manageConnection)(instance);
|
|
63
|
-
const channels = [...new Set([...groupedInstance.channels.map((channel) => channel)])];
|
|
64
|
-
const [handler] = get();
|
|
65
62
|
const endpoint = socket.createEndpoint(metadata, (request, response) => __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
const { get } = (0, connection_1.manageConnection)(instance);
|
|
64
|
+
const [handler] = get();
|
|
66
65
|
if (handler) {
|
|
67
66
|
yield handler.value(instance, this.moduleRef, request, response);
|
|
68
67
|
}
|
|
@@ -70,14 +69,13 @@ class PondSocketService {
|
|
|
70
69
|
response.accept();
|
|
71
70
|
}
|
|
72
71
|
}));
|
|
73
|
-
this.logger.log(`Mapped {${metadata}} endpoint`);
|
|
74
|
-
if (handler) {
|
|
75
|
-
this.logger.log(`Mapped {${metadata}} connection handler`);
|
|
76
|
-
}
|
|
77
72
|
setEndpoint(endpoint);
|
|
78
|
-
channels.
|
|
73
|
+
const channels = [...new Set([...groupedInstance.channels.map((channel) => channel)])];
|
|
74
|
+
channels.forEach((channel) => {
|
|
75
|
+
this.manageChannel(channel, endpoint);
|
|
76
|
+
});
|
|
79
77
|
}
|
|
80
|
-
manageChannel(channel, endpoint
|
|
78
|
+
manageChannel(channel, endpoint) {
|
|
81
79
|
const instance = channel.instance;
|
|
82
80
|
const constructor = instance.constructor;
|
|
83
81
|
const path = (0, channel_1.manageChannel)(constructor).get();
|
|
@@ -86,10 +84,10 @@ class PondSocketService {
|
|
|
86
84
|
}
|
|
87
85
|
const { set: setGuards } = (0, guards_1.manageGuards)(constructor);
|
|
88
86
|
const { set: setChannel } = (0, channelInstance_1.manageChannelInstance)(instance);
|
|
89
|
-
const { get } = (0, join_1.manageJoin)(instance);
|
|
90
|
-
const [handler] = get();
|
|
91
87
|
setGuards(this.externalGuards);
|
|
92
88
|
const channelInstance = endpoint.createChannel(path, (request, response) => __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
const { get } = (0, join_1.manageJoin)(instance);
|
|
90
|
+
const [handler] = get();
|
|
93
91
|
if (handler) {
|
|
94
92
|
yield handler.value(instance, this.moduleRef, request, response);
|
|
95
93
|
}
|
|
@@ -97,10 +95,6 @@ class PondSocketService {
|
|
|
97
95
|
response.accept();
|
|
98
96
|
}
|
|
99
97
|
}));
|
|
100
|
-
this.logger.log(`Mapped {${endpointPath}:${path}} channel`);
|
|
101
|
-
if (handler) {
|
|
102
|
-
this.logger.log(`Mapped {${endpointPath}:${path}} join handler`);
|
|
103
|
-
}
|
|
104
98
|
setChannel(channelInstance);
|
|
105
99
|
const { get: getEventHandlers } = (0, event_1.manageEvent)(instance);
|
|
106
100
|
const { get: getLeaveHandlers } = (0, leave_1.manageLeave)(instance);
|
|
@@ -108,19 +102,16 @@ class PondSocketService {
|
|
|
108
102
|
channelInstance.onEvent(handler.path, (request, response) => __awaiter(this, void 0, void 0, function* () {
|
|
109
103
|
yield handler.value(instance, this.moduleRef, request, response);
|
|
110
104
|
}));
|
|
111
|
-
this.logger.log(`Mapped {${endpointPath}:${path}} event {${handler.path}}`);
|
|
112
105
|
});
|
|
113
106
|
const [leaveHandler] = getLeaveHandlers();
|
|
114
107
|
if (leaveHandler) {
|
|
115
108
|
channelInstance.onLeave((event) => __awaiter(this, void 0, void 0, function* () {
|
|
116
109
|
yield leaveHandler.value(instance, this.moduleRef, event);
|
|
117
110
|
}));
|
|
118
|
-
this.logger.log(`Mapped {${endpointPath}:${path}} leave handler`);
|
|
119
111
|
}
|
|
120
112
|
}
|
|
121
113
|
getGroupedInstances() {
|
|
122
114
|
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
-
var _a;
|
|
124
115
|
const endpoints = yield this.discovery.providersWithMetaAtKey(constants_1.endpointKey);
|
|
125
116
|
const channels = yield this.discovery.providersWithMetaAtKey(constants_1.channelKey);
|
|
126
117
|
const modules = new Map();
|
|
@@ -151,20 +142,12 @@ class PondSocketService {
|
|
|
151
142
|
endpoint,
|
|
152
143
|
channels: instance.channels,
|
|
153
144
|
}))).flat();
|
|
154
|
-
const baseEndpoint = endpoints.
|
|
155
|
-
endpoints[0].discoveredClass :
|
|
156
|
-
(_a = endpoints
|
|
157
|
-
.find((endpoint) => endpoint.discoveredClass.parentModule.name === 'AppModule')) === null || _a === void 0 ? void 0 : _a.discoveredClass;
|
|
158
|
-
const groupedInstanceWithBaseEndpoint = groupedInstances.find((groupedInstance) => groupedInstance.endpoint.instance === (baseEndpoint === null || baseEndpoint === void 0 ? void 0 : baseEndpoint.instance));
|
|
145
|
+
const baseEndpoint = endpoints.find((endpoint) => endpoint.discoveredClass.parentModule.name === 'AppModule');
|
|
159
146
|
if (channelsWithNoEndpoints.length > 0) {
|
|
160
|
-
if (
|
|
161
|
-
const channels = channelsWithNoEndpoints.map((instance) => instance.channels).flat();
|
|
162
|
-
groupedInstanceWithBaseEndpoint.channels = [...new Set([...groupedInstanceWithBaseEndpoint.channels, ...channels])];
|
|
163
|
-
}
|
|
164
|
-
else if (baseEndpoint) {
|
|
147
|
+
if (baseEndpoint) {
|
|
165
148
|
const channels = channelsWithNoEndpoints.map((instance) => instance.channels).flat();
|
|
166
149
|
groupedInstances.push({
|
|
167
|
-
endpoint: baseEndpoint,
|
|
150
|
+
endpoint: baseEndpoint.discoveredClass,
|
|
168
151
|
channels,
|
|
169
152
|
});
|
|
170
153
|
}
|