@eleven-am/pondsocket 0.1.66 → 0.1.68
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 +42 -18
- package/channel/channel.js +15 -15
- package/channel/eventRequest.js +2 -2
- package/lobby/joinRequest.js +2 -2
- package/package.json +3 -3
- package/types.d.ts +5 -6
package/README.md
CHANGED
|
@@ -129,7 +129,6 @@ To create a PondSocket server that accepts authenticated connections and checks
|
|
|
129
129
|
|
|
130
130
|
```javascript
|
|
131
131
|
import PondSocket from "@eleven-am/pondsocket";
|
|
132
|
-
import Filter from "bad-words";
|
|
133
132
|
|
|
134
133
|
// Helper functions for token validation
|
|
135
134
|
function isValidToken(token) {
|
|
@@ -144,8 +143,17 @@ function getRoleFromToken(token) {
|
|
|
144
143
|
return 'user';
|
|
145
144
|
}
|
|
146
145
|
|
|
147
|
-
|
|
148
|
-
|
|
146
|
+
function isTextProfane(text) {
|
|
147
|
+
// Implement your profanity check logic here
|
|
148
|
+
// Return true if the text is profane, false otherwise
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function getMessagesFromDatabase(channelId) {
|
|
153
|
+
// Implement your logic to retrieve messages from the database
|
|
154
|
+
// Return an array of messages
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
149
157
|
|
|
150
158
|
const pond = new PondSocket();
|
|
151
159
|
|
|
@@ -157,7 +165,7 @@ const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
|
|
|
157
165
|
// Perform token validation here
|
|
158
166
|
if (isValidToken(token)) {
|
|
159
167
|
// Extract the authenticated user's username
|
|
160
|
-
const role =
|
|
168
|
+
const role = getRoleFromToken(token);
|
|
161
169
|
|
|
162
170
|
// Handle socket connection and authentication for valid users
|
|
163
171
|
res.accept({ role }); // Assign the user's role to the socket
|
|
@@ -167,23 +175,39 @@ const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
|
|
|
167
175
|
}
|
|
168
176
|
});
|
|
169
177
|
|
|
170
|
-
|
|
178
|
+
// Create a channel, providing a callback that is called when a user attempts to join the channel
|
|
179
|
+
const profanityChannel = endpoint.createChannel('/channel/:id', async (req, res) => {
|
|
171
180
|
// When joining the channel, any joinParams passed from the client will be available in the request payload
|
|
172
181
|
// Also any previous assigns on the socket will be available in the request payload as well
|
|
173
182
|
const { role } = req.user.assigns;
|
|
174
183
|
const { username } = req.joinParams;
|
|
175
|
-
|
|
184
|
+
const { id } = req.event.params;
|
|
185
|
+
|
|
186
|
+
// maybe retrieve the channel from a database
|
|
187
|
+
const messages = await getMessagesFromDatabase(id);
|
|
188
|
+
|
|
176
189
|
// Check if the user has the required role to join the channel
|
|
177
190
|
if (role === 'admin') {
|
|
178
191
|
// Accept the join request
|
|
179
192
|
res.accept({ username, profanityCount: 0 })
|
|
180
|
-
|
|
193
|
+
// optionally you can track the presence of the user in the channel
|
|
181
194
|
.trackPresence({
|
|
182
195
|
username,
|
|
183
196
|
role,
|
|
184
197
|
status: 'online',
|
|
185
198
|
onlineSince: Date.now(),
|
|
186
|
-
})
|
|
199
|
+
})
|
|
200
|
+
// and send the user the channel history
|
|
201
|
+
.sendToUsers('history', { messages }, [req.user.id]);
|
|
202
|
+
|
|
203
|
+
// Alternatively, you can also send messages to the user, NOTE that the user would be automatically subscribed to the channel.
|
|
204
|
+
// res.send('history', { messages }, { username, profanityCount: 0 })
|
|
205
|
+
// .trackPresence({
|
|
206
|
+
// username,
|
|
207
|
+
// role,
|
|
208
|
+
// status: 'online',
|
|
209
|
+
// onlineSince: Date.now(),
|
|
210
|
+
// });
|
|
187
211
|
} else {
|
|
188
212
|
// Reject the join request
|
|
189
213
|
res.reject('You do not have the required role to join this channel', 403);
|
|
@@ -195,14 +219,14 @@ profanityChannel.onEvent('message', (req, res) => {
|
|
|
195
219
|
const { text } = req.event.payload;
|
|
196
220
|
|
|
197
221
|
// Check for profanity
|
|
198
|
-
if (
|
|
199
|
-
const profanityCount = req.user.assigns.profanityCount + 1
|
|
222
|
+
if (isTextProfane(text)) {
|
|
200
223
|
// Reject the message if it contains profanity
|
|
201
224
|
res.reject('Profanity is not allowed', 400, {
|
|
202
|
-
profanityCount
|
|
225
|
+
profanityCount: req.user.assigns.profanityCount + 1
|
|
203
226
|
});
|
|
204
|
-
|
|
205
|
-
|
|
227
|
+
|
|
228
|
+
// note that profanityCount is updated so req.user.assigns.profanityCount will be updated
|
|
229
|
+
if (req.user.assigns.profanityCount >= 3) {
|
|
206
230
|
// Kick the user from the channel if they have used profanity more than 3 times
|
|
207
231
|
res.evictUser('You have been kicked from the channel for using profanity');
|
|
208
232
|
} else {
|
|
@@ -215,15 +239,15 @@ profanityChannel.onEvent('message', (req, res) => {
|
|
|
215
239
|
// Accept the message to allow broadcasting to other clients in the channel
|
|
216
240
|
res.accept();
|
|
217
241
|
}
|
|
218
|
-
|
|
242
|
+
|
|
219
243
|
// for more complete access to the channel, you can use the client object
|
|
220
|
-
// const channel = req.
|
|
244
|
+
// const channel = req.channel;
|
|
221
245
|
});
|
|
222
246
|
|
|
223
247
|
profanityChannel.onEvent('presence/:presence', (req, res) => {
|
|
224
248
|
const { presence } = req.event.params;
|
|
225
249
|
const { username } = req.user.assigns;
|
|
226
|
-
|
|
250
|
+
|
|
227
251
|
// Handle presence events
|
|
228
252
|
res.updatePresence({
|
|
229
253
|
username,
|
|
@@ -235,9 +259,9 @@ profanityChannel.onEvent('presence/:presence', (req, res) => {
|
|
|
235
259
|
|
|
236
260
|
profanityChannel.onLeave((req, res) => {
|
|
237
261
|
const { username } = req.user.assigns;
|
|
238
|
-
|
|
262
|
+
|
|
239
263
|
// When a user leaves the channel, PondSocket will automatically remove the user from the presence list and inform other users in the channel
|
|
240
|
-
|
|
264
|
+
|
|
241
265
|
// perform a cleanup operation here
|
|
242
266
|
});
|
|
243
267
|
|
package/channel/channel.js
CHANGED
|
@@ -21,9 +21,9 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
21
21
|
}
|
|
22
22
|
return t;
|
|
23
23
|
};
|
|
24
|
-
var _ChannelEngine_instances, _ChannelEngine_receiver, _ChannelEngine_presenceEngine, _ChannelEngine_users, _ChannelEngine_parentEngine, _ChannelEngine_subscribe, _ChannelEngine_getUsersFromRecipients,
|
|
24
|
+
var _ChannelEngine_instances, _ChannelEngine_receiver, _ChannelEngine_presenceEngine, _ChannelEngine_users, _ChannelEngine_parentEngine, _ChannelEngine_subscribe, _ChannelEngine_getUsersFromRecipients, _Channel_engine;
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
26
|
+
exports.Channel = exports.ChannelEngine = void 0;
|
|
27
27
|
const eventRequest_1 = require("./eventRequest");
|
|
28
28
|
const eventResponse_1 = require("./eventResponse");
|
|
29
29
|
const enums_1 = require("../enums");
|
|
@@ -257,37 +257,37 @@ _ChannelEngine_receiver = new WeakMap(), _ChannelEngine_presenceEngine = new Wea
|
|
|
257
257
|
}
|
|
258
258
|
return users;
|
|
259
259
|
};
|
|
260
|
-
class
|
|
260
|
+
class Channel {
|
|
261
261
|
constructor(engine) {
|
|
262
|
-
|
|
263
|
-
__classPrivateFieldSet(this,
|
|
262
|
+
_Channel_engine.set(this, void 0);
|
|
263
|
+
__classPrivateFieldSet(this, _Channel_engine, engine, "f");
|
|
264
264
|
}
|
|
265
265
|
getAssigns() {
|
|
266
|
-
return __classPrivateFieldGet(this,
|
|
266
|
+
return __classPrivateFieldGet(this, _Channel_engine, "f").getAssigns();
|
|
267
267
|
}
|
|
268
268
|
getUserData(userId) {
|
|
269
|
-
return __classPrivateFieldGet(this,
|
|
269
|
+
return __classPrivateFieldGet(this, _Channel_engine, "f").getUserData(userId);
|
|
270
270
|
}
|
|
271
271
|
broadcastMessage(event, payload) {
|
|
272
|
-
__classPrivateFieldGet(this,
|
|
272
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(enums_1.SystemSender.CHANNEL, enums_1.ChannelReceiver.ALL_USERS, enums_1.ServerActions.BROADCAST, event, payload);
|
|
273
273
|
}
|
|
274
274
|
sendToUser(userId, event, payload) {
|
|
275
|
-
__classPrivateFieldGet(this,
|
|
275
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(enums_1.SystemSender.CHANNEL, [userId], enums_1.ServerActions.BROADCAST, event, payload);
|
|
276
276
|
}
|
|
277
277
|
banUser(userId, reason) {
|
|
278
|
-
__classPrivateFieldGet(this,
|
|
278
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").kickUser(userId, reason !== null && reason !== void 0 ? reason : 'You have been banned from the channel');
|
|
279
279
|
}
|
|
280
280
|
trackPresence(userId, presence) {
|
|
281
|
-
__classPrivateFieldGet(this,
|
|
281
|
+
__classPrivateFieldGet(this, _Channel_engine, "f").trackPresence(userId, presence);
|
|
282
282
|
}
|
|
283
283
|
removePresence(userId) {
|
|
284
284
|
var _a;
|
|
285
|
-
(_a = __classPrivateFieldGet(this,
|
|
285
|
+
(_a = __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine) === null || _a === void 0 ? void 0 : _a.removePresence(userId);
|
|
286
286
|
}
|
|
287
287
|
updatePresence(userId, presence) {
|
|
288
288
|
var _a;
|
|
289
|
-
(_a = __classPrivateFieldGet(this,
|
|
289
|
+
(_a = __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine) === null || _a === void 0 ? void 0 : _a.updatePresence(userId, presence);
|
|
290
290
|
}
|
|
291
291
|
}
|
|
292
|
-
exports.
|
|
293
|
-
|
|
292
|
+
exports.Channel = Channel;
|
|
293
|
+
_Channel_engine = new WeakMap();
|
package/channel/eventRequest.js
CHANGED
|
@@ -18,8 +18,8 @@ class EventRequest extends abstractRequest_1.AbstractRequest {
|
|
|
18
18
|
}
|
|
19
19
|
return assigns;
|
|
20
20
|
}
|
|
21
|
-
get
|
|
22
|
-
return new channel_1.
|
|
21
|
+
get channel() {
|
|
22
|
+
return new channel_1.Channel(this._engine);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
exports.EventRequest = EventRequest;
|
package/lobby/joinRequest.js
CHANGED
|
@@ -35,8 +35,8 @@ class JoinRequest extends abstractRequest_1.AbstractRequest {
|
|
|
35
35
|
presence: {},
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
|
-
get
|
|
39
|
-
return new channel_1.
|
|
38
|
+
get channel() {
|
|
39
|
+
return new channel_1.Channel(this._engine);
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
exports.JoinRequest = JoinRequest;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eleven-am/pondsocket",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.68",
|
|
4
4
|
"description": "PondSocket is a fast simple socket server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"socket",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
},
|
|
23
23
|
"author": "Roy OSSAI",
|
|
24
24
|
"license": "GPL-3.0",
|
|
25
|
-
"main": "
|
|
26
|
-
"types": "
|
|
25
|
+
"main": "index.js",
|
|
26
|
+
"types": "index.d.ts",
|
|
27
27
|
"repository": {
|
|
28
28
|
"type": "git",
|
|
29
29
|
"url": "git+https://github.com/Eleven-am/pondSocket.git"
|
package/types.d.ts
CHANGED
|
@@ -111,7 +111,7 @@ declare abstract class PondResponse {
|
|
|
111
111
|
declare class EventRequest<Path extends string> extends AbstractRequest<Path> {
|
|
112
112
|
user: UserData;
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
channel: Channel;
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
declare class EventResponse extends PondResponse {
|
|
@@ -193,7 +193,7 @@ declare class EventResponse extends PondResponse {
|
|
|
193
193
|
closeChannel (reason: string): void;
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
-
export declare class
|
|
196
|
+
export declare class ClientChannel {
|
|
197
197
|
channelState: ChannelState;
|
|
198
198
|
|
|
199
199
|
/**
|
|
@@ -319,7 +319,7 @@ declare class Endpoint {
|
|
|
319
319
|
closeConnection (clientIds: string | string[]): void;
|
|
320
320
|
}
|
|
321
321
|
|
|
322
|
-
export declare class
|
|
322
|
+
export declare class Channel {
|
|
323
323
|
/**
|
|
324
324
|
* @desc Gets the current assign data for the channel.
|
|
325
325
|
*/
|
|
@@ -379,7 +379,7 @@ declare class JoinRequest<Path extends string> extends AbstractRequest<Path> {
|
|
|
379
379
|
|
|
380
380
|
user: UserData;
|
|
381
381
|
|
|
382
|
-
|
|
382
|
+
channel: Channel;
|
|
383
383
|
}
|
|
384
384
|
|
|
385
385
|
declare class JoinResponse extends PondResponse {
|
|
@@ -571,7 +571,7 @@ declare class PondClient {
|
|
|
571
571
|
* @param name - The name of the channel.
|
|
572
572
|
* @param params - The params to send to the server.
|
|
573
573
|
*/
|
|
574
|
-
createChannel (name: string, params?: JoinParams):
|
|
574
|
+
createChannel (name: string, params?: JoinParams): ClientChannel;
|
|
575
575
|
|
|
576
576
|
/**
|
|
577
577
|
* @desc Subscribes to the connection state.
|
|
@@ -581,4 +581,3 @@ declare class PondClient {
|
|
|
581
581
|
}
|
|
582
582
|
|
|
583
583
|
declare const pondSocket: (app: Express) => PondSocketExpressApp;
|
|
584
|
-
|