@eleven-am/pondsocket 0.1.199 → 0.1.201
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/abstracts/distributor.js +126 -0
- package/abstracts/middleware.js +29 -0
- package/abstracts/types.js +16 -0
- package/contexts/baseContext.js +86 -0
- package/contexts/connectionContext.js +169 -0
- package/contexts/eventContext.js +99 -0
- package/contexts/joinContext.js +156 -0
- package/contexts/outgoingContext.js +59 -0
- package/engines/channelEngine.js +285 -19
- package/engines/endpointEngine.js +10 -9
- package/engines/lobbyEngine.js +39 -60
- package/engines/presenceEngine.js +0 -1
- package/package.json +16 -16
- package/server/server.js +48 -48
- package/types.d.ts +273 -101
- package/wrappers/pondChannel.js +1 -1
- package/requests/eventRequest.js +0 -69
- package/requests/joinRequest.js +0 -56
- package/responses/connectionResponse.js +0 -92
- package/responses/eventResponse.js +0 -101
- package/responses/joinResponse.js +0 -134
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
12
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
13
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
14
|
+
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");
|
|
15
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
16
|
+
};
|
|
17
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
18
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
19
|
+
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");
|
|
20
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
21
|
+
};
|
|
22
|
+
var _RedisDistributedBackend_instances, _RedisDistributedBackend_publishClient, _RedisDistributedBackend_subscribeClient, _RedisDistributedBackend_keyPrefix, _RedisDistributedBackend_subject, _RedisDistributedBackend_isConnected, _RedisDistributedBackend_initialize, _RedisDistributedBackend_handleRedisMessage, _RedisDistributedBackend_isValidMessage, _RedisDistributedBackend_buildKey;
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.RedisDistributedBackend = void 0;
|
|
25
|
+
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
26
|
+
const redis_1 = require("redis");
|
|
27
|
+
const types_1 = require("../types");
|
|
28
|
+
class RedisDistributedBackend {
|
|
29
|
+
constructor(options = {}) {
|
|
30
|
+
_RedisDistributedBackend_instances.add(this);
|
|
31
|
+
_RedisDistributedBackend_publishClient.set(this, void 0);
|
|
32
|
+
_RedisDistributedBackend_subscribeClient.set(this, void 0);
|
|
33
|
+
_RedisDistributedBackend_keyPrefix.set(this, void 0);
|
|
34
|
+
_RedisDistributedBackend_subject.set(this, void 0);
|
|
35
|
+
_RedisDistributedBackend_isConnected.set(this, false);
|
|
36
|
+
const { host = 'localhost', port = 6379, password, database = 0, url, keyPrefix = 'pondsocket', } = options;
|
|
37
|
+
__classPrivateFieldSet(this, _RedisDistributedBackend_keyPrefix, keyPrefix, "f");
|
|
38
|
+
__classPrivateFieldSet(this, _RedisDistributedBackend_subject, new pondsocket_common_1.Subject(), "f");
|
|
39
|
+
// Create Redis clients
|
|
40
|
+
const clientConfig = url
|
|
41
|
+
? { url }
|
|
42
|
+
: { socket: { host,
|
|
43
|
+
port },
|
|
44
|
+
password,
|
|
45
|
+
database };
|
|
46
|
+
__classPrivateFieldSet(this, _RedisDistributedBackend_publishClient, (0, redis_1.createClient)(clientConfig), "f");
|
|
47
|
+
__classPrivateFieldSet(this, _RedisDistributedBackend_subscribeClient, __classPrivateFieldGet(this, _RedisDistributedBackend_publishClient, "f").duplicate(), "f");
|
|
48
|
+
void __classPrivateFieldGet(this, _RedisDistributedBackend_instances, "m", _RedisDistributedBackend_initialize).call(this);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Gets the subject for subscribing to distributed messages
|
|
52
|
+
*/
|
|
53
|
+
get subject() {
|
|
54
|
+
return __classPrivateFieldGet(this, _RedisDistributedBackend_subject, "f");
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Subscribe to messages for a specific endpoint and channel
|
|
58
|
+
*/
|
|
59
|
+
subscribe(endpointName, channelName, handler) {
|
|
60
|
+
return this.subject.subscribe((message) => {
|
|
61
|
+
if (message.endpointName === endpointName && message.channelName === channelName) {
|
|
62
|
+
handler(message);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Broadcasts a message to all nodes for a specific endpoint and channel
|
|
68
|
+
*/
|
|
69
|
+
broadcast(endpointName, channelName, message) {
|
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
if (!__classPrivateFieldGet(this, _RedisDistributedBackend_isConnected, "f")) {
|
|
72
|
+
throw new Error('Redis backend is not connected');
|
|
73
|
+
}
|
|
74
|
+
const key = __classPrivateFieldGet(this, _RedisDistributedBackend_instances, "m", _RedisDistributedBackend_buildKey).call(this, endpointName, channelName);
|
|
75
|
+
const serializedMessage = JSON.stringify(message);
|
|
76
|
+
yield __classPrivateFieldGet(this, _RedisDistributedBackend_publishClient, "f").publish(key, serializedMessage);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Cleanup and close all Redis connections
|
|
81
|
+
*/
|
|
82
|
+
cleanup() {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
this.subject.close();
|
|
85
|
+
if (__classPrivateFieldGet(this, _RedisDistributedBackend_subscribeClient, "f").isOpen) {
|
|
86
|
+
yield __classPrivateFieldGet(this, _RedisDistributedBackend_subscribeClient, "f").quit();
|
|
87
|
+
}
|
|
88
|
+
if (__classPrivateFieldGet(this, _RedisDistributedBackend_publishClient, "f").isOpen) {
|
|
89
|
+
yield __classPrivateFieldGet(this, _RedisDistributedBackend_publishClient, "f").quit();
|
|
90
|
+
}
|
|
91
|
+
__classPrivateFieldSet(this, _RedisDistributedBackend_isConnected, false, "f");
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.RedisDistributedBackend = RedisDistributedBackend;
|
|
96
|
+
_RedisDistributedBackend_publishClient = new WeakMap(), _RedisDistributedBackend_subscribeClient = new WeakMap(), _RedisDistributedBackend_keyPrefix = new WeakMap(), _RedisDistributedBackend_subject = new WeakMap(), _RedisDistributedBackend_isConnected = new WeakMap(), _RedisDistributedBackend_instances = new WeakSet(), _RedisDistributedBackend_initialize = function _RedisDistributedBackend_initialize() {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
yield Promise.all([
|
|
99
|
+
__classPrivateFieldGet(this, _RedisDistributedBackend_publishClient, "f").connect(),
|
|
100
|
+
__classPrivateFieldGet(this, _RedisDistributedBackend_subscribeClient, "f").connect(),
|
|
101
|
+
]);
|
|
102
|
+
const pattern = `${__classPrivateFieldGet(this, _RedisDistributedBackend_keyPrefix, "f")}:*`;
|
|
103
|
+
yield __classPrivateFieldGet(this, _RedisDistributedBackend_subscribeClient, "f").pSubscribe(pattern, (message) => {
|
|
104
|
+
__classPrivateFieldGet(this, _RedisDistributedBackend_instances, "m", _RedisDistributedBackend_handleRedisMessage).call(this, message);
|
|
105
|
+
});
|
|
106
|
+
__classPrivateFieldSet(this, _RedisDistributedBackend_isConnected, true, "f");
|
|
107
|
+
});
|
|
108
|
+
}, _RedisDistributedBackend_handleRedisMessage = function _RedisDistributedBackend_handleRedisMessage(message) {
|
|
109
|
+
try {
|
|
110
|
+
const parsedMessage = JSON.parse(message);
|
|
111
|
+
if (__classPrivateFieldGet(this, _RedisDistributedBackend_instances, "m", _RedisDistributedBackend_isValidMessage).call(this, parsedMessage)) {
|
|
112
|
+
this.subject.publish(parsedMessage);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (_a) {
|
|
116
|
+
// Silently ignore invalid messages
|
|
117
|
+
}
|
|
118
|
+
}, _RedisDistributedBackend_isValidMessage = function _RedisDistributedBackend_isValidMessage(message) {
|
|
119
|
+
return (typeof message === 'object' &&
|
|
120
|
+
typeof message.type === 'string' &&
|
|
121
|
+
Object.values(types_1.DistributedMessageType).includes(message.type) &&
|
|
122
|
+
typeof message.endpointName === 'string' &&
|
|
123
|
+
typeof message.channelName === 'string');
|
|
124
|
+
}, _RedisDistributedBackend_buildKey = function _RedisDistributedBackend_buildKey(endpointName, channelName) {
|
|
125
|
+
return `${__classPrivateFieldGet(this, _RedisDistributedBackend_keyPrefix, "f")}:${endpointName}:${channelName}`;
|
|
126
|
+
};
|
package/abstracts/middleware.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
12
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
13
|
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");
|
|
@@ -64,6 +73,26 @@ class Middleware {
|
|
|
64
73
|
};
|
|
65
74
|
next();
|
|
66
75
|
}
|
|
76
|
+
runAsync(req, res, final) {
|
|
77
|
+
let index = 0;
|
|
78
|
+
const next = (err) => __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
if (err) {
|
|
80
|
+
return final(__classPrivateFieldGet(this, _Middleware_instances, "m", _Middleware_handleError).call(this, err));
|
|
81
|
+
}
|
|
82
|
+
if (index >= __classPrivateFieldGet(this, _Middleware_stack, "f").length) {
|
|
83
|
+
return final();
|
|
84
|
+
}
|
|
85
|
+
const middleware = __classPrivateFieldGet(this, _Middleware_stack, "f")[index];
|
|
86
|
+
index++;
|
|
87
|
+
try {
|
|
88
|
+
yield middleware(req, res, next);
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
return final(__classPrivateFieldGet(this, _Middleware_instances, "m", _Middleware_handleError).call(this, error));
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return next();
|
|
95
|
+
}
|
|
67
96
|
}
|
|
68
97
|
exports.Middleware = Middleware;
|
|
69
98
|
_Middleware_stack = new WeakMap(), _Middleware_instances = new WeakSet(), _Middleware_handleError = function _Middleware_handleError(error) {
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DistributedMessageType = void 0;
|
|
4
|
+
var DistributedMessageType;
|
|
5
|
+
(function (DistributedMessageType) {
|
|
6
|
+
DistributedMessageType["STATE_REQUEST"] = "STATE_REQUEST";
|
|
7
|
+
DistributedMessageType["STATE_RESPONSE"] = "STATE_RESPONSE";
|
|
8
|
+
DistributedMessageType["USER_JOINED"] = "USER_JOINED";
|
|
9
|
+
DistributedMessageType["USER_LEFT"] = "USER_LEFT";
|
|
10
|
+
DistributedMessageType["USER_MESSAGE"] = "USER_MESSAGE";
|
|
11
|
+
DistributedMessageType["PRESENCE_UPDATE"] = "PRESENCE_UPDATE";
|
|
12
|
+
DistributedMessageType["PRESENCE_REMOVED"] = "PRESENCE_REMOVED";
|
|
13
|
+
DistributedMessageType["ASSIGNS_UPDATE"] = "ASSIGNS_UPDATE";
|
|
14
|
+
DistributedMessageType["ASSIGNS_REMOVED"] = "ASSIGNS_REMOVED";
|
|
15
|
+
DistributedMessageType["EVICT_USER"] = "EVICT_USER";
|
|
16
|
+
})(DistributedMessageType || (exports.DistributedMessageType = DistributedMessageType = {}));
|
|
@@ -0,0 +1,86 @@
|
|
|
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 _BaseContext_engine, _BaseContext_params, _BaseContext_event, _BaseContext_sender, _BaseContext_payload;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.BaseContext = void 0;
|
|
16
|
+
const channel_1 = require("../wrappers/channel");
|
|
17
|
+
class BaseContext {
|
|
18
|
+
constructor(engine, params, event, payload, sender) {
|
|
19
|
+
_BaseContext_engine.set(this, void 0);
|
|
20
|
+
_BaseContext_params.set(this, void 0);
|
|
21
|
+
_BaseContext_event.set(this, void 0);
|
|
22
|
+
_BaseContext_sender.set(this, void 0);
|
|
23
|
+
_BaseContext_payload.set(this, void 0);
|
|
24
|
+
__classPrivateFieldSet(this, _BaseContext_engine, engine, "f");
|
|
25
|
+
__classPrivateFieldSet(this, _BaseContext_params, params, "f");
|
|
26
|
+
__classPrivateFieldSet(this, _BaseContext_event, event, "f");
|
|
27
|
+
__classPrivateFieldSet(this, _BaseContext_payload, payload, "f");
|
|
28
|
+
__classPrivateFieldSet(this, _BaseContext_sender, sender, "f");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The event information
|
|
32
|
+
*/
|
|
33
|
+
get event() {
|
|
34
|
+
return {
|
|
35
|
+
event: __classPrivateFieldGet(this, _BaseContext_event, "f"),
|
|
36
|
+
query: __classPrivateFieldGet(this, _BaseContext_params, "f").query,
|
|
37
|
+
params: __classPrivateFieldGet(this, _BaseContext_params, "f").params,
|
|
38
|
+
payload: __classPrivateFieldGet(this, _BaseContext_payload, "f"),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The channel name
|
|
43
|
+
*/
|
|
44
|
+
get channelName() {
|
|
45
|
+
return __classPrivateFieldGet(this, _BaseContext_engine, "f").name;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The channel instance
|
|
49
|
+
*/
|
|
50
|
+
get channel() {
|
|
51
|
+
return new channel_1.Channel(__classPrivateFieldGet(this, _BaseContext_engine, "f"));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* All current presences in the channel
|
|
55
|
+
*/
|
|
56
|
+
get presences() {
|
|
57
|
+
return __classPrivateFieldGet(this, _BaseContext_engine, "f").getPresence();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* All current assigns in the channel
|
|
61
|
+
*/
|
|
62
|
+
get assigns() {
|
|
63
|
+
return __classPrivateFieldGet(this, _BaseContext_engine, "f").getAssigns();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The user who sent the request
|
|
67
|
+
*/
|
|
68
|
+
get user() {
|
|
69
|
+
return this.channel.getUserData(__classPrivateFieldGet(this, _BaseContext_sender, "f"));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Gets the channel engine instance
|
|
73
|
+
* @protected
|
|
74
|
+
*/
|
|
75
|
+
get engine() {
|
|
76
|
+
return __classPrivateFieldGet(this, _BaseContext_engine, "f");
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Updates the event parameters.
|
|
80
|
+
*/
|
|
81
|
+
updateParams(params) {
|
|
82
|
+
__classPrivateFieldSet(this, _BaseContext_params, params, "f");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.BaseContext = BaseContext;
|
|
86
|
+
_BaseContext_engine = new WeakMap(), _BaseContext_params = new WeakMap(), _BaseContext_event = new WeakMap(), _BaseContext_sender = new WeakMap(), _BaseContext_payload = new WeakMap();
|
|
@@ -0,0 +1,169 @@
|
|
|
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 _ConnectionContext_instances, _ConnectionContext_executed, _ConnectionContext_assigns, _ConnectionContext_clientId, _ConnectionContext_engine, _ConnectionContext_params, _ConnectionContext_webSocketServer, _ConnectionContext_connectionData, _ConnectionContext_performChecks;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.ConnectionContext = void 0;
|
|
16
|
+
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
17
|
+
const httpError_1 = require("../errors/httpError");
|
|
18
|
+
/**
|
|
19
|
+
* ConnectionContext combines IncomingConnection data with ConnectionResponse functionality.
|
|
20
|
+
*/
|
|
21
|
+
class ConnectionContext {
|
|
22
|
+
constructor(connectionData, { engine, params, webSocketServer }) {
|
|
23
|
+
_ConnectionContext_instances.add(this);
|
|
24
|
+
_ConnectionContext_executed.set(this, void 0);
|
|
25
|
+
_ConnectionContext_assigns.set(this, void 0);
|
|
26
|
+
_ConnectionContext_clientId.set(this, void 0);
|
|
27
|
+
_ConnectionContext_engine.set(this, void 0);
|
|
28
|
+
_ConnectionContext_params.set(this, void 0);
|
|
29
|
+
_ConnectionContext_webSocketServer.set(this, void 0);
|
|
30
|
+
_ConnectionContext_connectionData.set(this, void 0);
|
|
31
|
+
__classPrivateFieldSet(this, _ConnectionContext_webSocketServer, webSocketServer, "f");
|
|
32
|
+
__classPrivateFieldSet(this, _ConnectionContext_clientId, connectionData.id, "f");
|
|
33
|
+
__classPrivateFieldSet(this, _ConnectionContext_executed, false, "f");
|
|
34
|
+
__classPrivateFieldSet(this, _ConnectionContext_engine, engine, "f");
|
|
35
|
+
__classPrivateFieldSet(this, _ConnectionContext_params, params, "f");
|
|
36
|
+
__classPrivateFieldSet(this, _ConnectionContext_assigns, {}, "f");
|
|
37
|
+
__classPrivateFieldSet(this, _ConnectionContext_connectionData, connectionData, "f");
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Whether the connection has been handled
|
|
41
|
+
*/
|
|
42
|
+
get hasResponded() {
|
|
43
|
+
return __classPrivateFieldGet(this, _ConnectionContext_executed, "f");
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Connection details including headers, cookies, address, id, etc.
|
|
47
|
+
*/
|
|
48
|
+
get connection() {
|
|
49
|
+
return __classPrivateFieldGet(this, _ConnectionContext_connectionData, "f");
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The request parameters
|
|
53
|
+
*/
|
|
54
|
+
get requestId() {
|
|
55
|
+
return __classPrivateFieldGet(this, _ConnectionContext_params, "f").requestId;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The client ID
|
|
59
|
+
*/
|
|
60
|
+
get clientId() {
|
|
61
|
+
return __classPrivateFieldGet(this, _ConnectionContext_clientId, "f");
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The request headers
|
|
65
|
+
*/
|
|
66
|
+
get headers() {
|
|
67
|
+
return __classPrivateFieldGet(this, _ConnectionContext_connectionData, "f").headers;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The request cookies
|
|
71
|
+
*/
|
|
72
|
+
get cookies() {
|
|
73
|
+
return __classPrivateFieldGet(this, _ConnectionContext_connectionData, "f").cookies;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The client address
|
|
77
|
+
*/
|
|
78
|
+
get address() {
|
|
79
|
+
return __classPrivateFieldGet(this, _ConnectionContext_connectionData, "f").address;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The event parameters
|
|
83
|
+
*/
|
|
84
|
+
get event() {
|
|
85
|
+
return {
|
|
86
|
+
query: __classPrivateFieldGet(this, _ConnectionContext_connectionData, "f").query,
|
|
87
|
+
params: __classPrivateFieldGet(this, _ConnectionContext_connectionData, "f").params,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The query parameters
|
|
92
|
+
*/
|
|
93
|
+
get query() {
|
|
94
|
+
return __classPrivateFieldGet(this, _ConnectionContext_connectionData, "f").query;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The path parameters
|
|
98
|
+
*/
|
|
99
|
+
get params() {
|
|
100
|
+
return __classPrivateFieldGet(this, _ConnectionContext_connectionData, "f").params;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Assigns data to the client
|
|
104
|
+
*/
|
|
105
|
+
assign(assigns) {
|
|
106
|
+
if (!__classPrivateFieldGet(this, _ConnectionContext_executed, "f")) {
|
|
107
|
+
__classPrivateFieldSet(this, _ConnectionContext_assigns, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _ConnectionContext_assigns, "f")), assigns), "f");
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const user = __classPrivateFieldGet(this, _ConnectionContext_engine, "f").getUser(__classPrivateFieldGet(this, _ConnectionContext_clientId, "f"));
|
|
111
|
+
user.assigns = Object.assign(Object.assign({}, user.assigns), assigns);
|
|
112
|
+
}
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Accepts the connection request
|
|
117
|
+
*/
|
|
118
|
+
accept() {
|
|
119
|
+
__classPrivateFieldGet(this, _ConnectionContext_instances, "m", _ConnectionContext_performChecks).call(this);
|
|
120
|
+
__classPrivateFieldGet(this, _ConnectionContext_webSocketServer, "f").handleUpgrade(__classPrivateFieldGet(this, _ConnectionContext_params, "f").request, __classPrivateFieldGet(this, _ConnectionContext_params, "f").socket, __classPrivateFieldGet(this, _ConnectionContext_params, "f").head, (ws) => {
|
|
121
|
+
const cache = {
|
|
122
|
+
clientId: __classPrivateFieldGet(this, _ConnectionContext_clientId, "f"),
|
|
123
|
+
subscriptions: new Set(),
|
|
124
|
+
assigns: __classPrivateFieldGet(this, _ConnectionContext_assigns, "f"),
|
|
125
|
+
socket: ws,
|
|
126
|
+
};
|
|
127
|
+
__classPrivateFieldGet(this, _ConnectionContext_webSocketServer, "f").emit('connection', ws);
|
|
128
|
+
__classPrivateFieldGet(this, _ConnectionContext_engine, "f").manageSocket(cache);
|
|
129
|
+
});
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Declines the connection request
|
|
134
|
+
*/
|
|
135
|
+
decline(message, errorCode) {
|
|
136
|
+
__classPrivateFieldGet(this, _ConnectionContext_instances, "m", _ConnectionContext_performChecks).call(this);
|
|
137
|
+
const code = errorCode || 400;
|
|
138
|
+
const { socket } = __classPrivateFieldGet(this, _ConnectionContext_params, "f");
|
|
139
|
+
const newMessage = message || 'Unauthorized connection';
|
|
140
|
+
socket.write(`HTTP/1.1 ${code} ${newMessage}\r\n\r\n`);
|
|
141
|
+
socket.destroy();
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Sends a direct reply to the client
|
|
146
|
+
*/
|
|
147
|
+
reply(event, payload) {
|
|
148
|
+
if (!__classPrivateFieldGet(this, _ConnectionContext_executed, "f")) {
|
|
149
|
+
throw new httpError_1.HttpError(400, 'Connection has not been accepted');
|
|
150
|
+
}
|
|
151
|
+
const { socket } = __classPrivateFieldGet(this, _ConnectionContext_engine, "f").getUser(__classPrivateFieldGet(this, _ConnectionContext_clientId, "f"));
|
|
152
|
+
const message = {
|
|
153
|
+
event,
|
|
154
|
+
payload,
|
|
155
|
+
action: pondsocket_common_1.ServerActions.BROADCAST,
|
|
156
|
+
requestId: __classPrivateFieldGet(this, _ConnectionContext_params, "f").requestId,
|
|
157
|
+
channelName: pondsocket_common_1.SystemSender.ENDPOINT,
|
|
158
|
+
};
|
|
159
|
+
__classPrivateFieldGet(this, _ConnectionContext_engine, "f").sendMessage(socket, message);
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
exports.ConnectionContext = ConnectionContext;
|
|
164
|
+
_ConnectionContext_executed = new WeakMap(), _ConnectionContext_assigns = new WeakMap(), _ConnectionContext_clientId = new WeakMap(), _ConnectionContext_engine = new WeakMap(), _ConnectionContext_params = new WeakMap(), _ConnectionContext_webSocketServer = new WeakMap(), _ConnectionContext_connectionData = new WeakMap(), _ConnectionContext_instances = new WeakSet(), _ConnectionContext_performChecks = function _ConnectionContext_performChecks() {
|
|
165
|
+
if (__classPrivateFieldGet(this, _ConnectionContext_executed, "f")) {
|
|
166
|
+
throw new httpError_1.HttpError(400, 'Response has already been executed');
|
|
167
|
+
}
|
|
168
|
+
__classPrivateFieldSet(this, _ConnectionContext_executed, true, "f");
|
|
169
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
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 _EventContext_instances, _EventContext_event, _EventContext_requestId, _EventContext_sendMessage;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.EventContext = void 0;
|
|
16
|
+
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
17
|
+
const baseContext_1 = require("./baseContext");
|
|
18
|
+
/**
|
|
19
|
+
* EventContext combines the functionality of EventRequest and EventResponse
|
|
20
|
+
* to provide a unified interface for handling events in a channel.
|
|
21
|
+
*/
|
|
22
|
+
class EventContext extends baseContext_1.BaseContext {
|
|
23
|
+
constructor(event, params, engine) {
|
|
24
|
+
super(engine, params, event.event, event.payload, event.sender);
|
|
25
|
+
_EventContext_instances.add(this);
|
|
26
|
+
_EventContext_event.set(this, void 0);
|
|
27
|
+
_EventContext_requestId.set(this, void 0);
|
|
28
|
+
__classPrivateFieldSet(this, _EventContext_event, event, "f");
|
|
29
|
+
__classPrivateFieldSet(this, _EventContext_requestId, event.requestId, "f");
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Assigns data to the client
|
|
33
|
+
*/
|
|
34
|
+
assign(assigns) {
|
|
35
|
+
this.channel.updateAssigns(__classPrivateFieldGet(this, _EventContext_event, "f").sender, assigns);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Sends a direct reply to the client
|
|
40
|
+
*/
|
|
41
|
+
reply(event, payload) {
|
|
42
|
+
this.engine.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, [__classPrivateFieldGet(this, _EventContext_event, "f").sender], pondsocket_common_1.ServerActions.SYSTEM, event, payload, __classPrivateFieldGet(this, _EventContext_requestId, "f"));
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Broadcasts a message to all users in the channel
|
|
47
|
+
*/
|
|
48
|
+
broadcast(event, payload) {
|
|
49
|
+
__classPrivateFieldGet(this, _EventContext_instances, "m", _EventContext_sendMessage).call(this, pondsocket_common_1.ChannelReceiver.ALL_USERS, event, payload);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Broadcasts a message to all users except the sender
|
|
54
|
+
*/
|
|
55
|
+
broadcastFrom(event, payload) {
|
|
56
|
+
__classPrivateFieldGet(this, _EventContext_instances, "m", _EventContext_sendMessage).call(this, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, event, payload);
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Broadcasts a message to specific users
|
|
61
|
+
*/
|
|
62
|
+
broadcastTo(event, payload, userIds) {
|
|
63
|
+
const ids = Array.isArray(userIds) ? userIds : [userIds];
|
|
64
|
+
__classPrivateFieldGet(this, _EventContext_instances, "m", _EventContext_sendMessage).call(this, ids, event, payload);
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Tracks a user's presence
|
|
69
|
+
*/
|
|
70
|
+
trackPresence(presence, userId = __classPrivateFieldGet(this, _EventContext_event, "f").sender) {
|
|
71
|
+
this.channel.trackPresence(userId, presence);
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Updates a user's presence
|
|
76
|
+
*/
|
|
77
|
+
updatePresence(presence, userId = __classPrivateFieldGet(this, _EventContext_event, "f").sender) {
|
|
78
|
+
this.channel.updatePresence(userId, presence);
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Kicks a user from the channel
|
|
83
|
+
*/
|
|
84
|
+
evictUser(reason, userId = __classPrivateFieldGet(this, _EventContext_event, "f").sender) {
|
|
85
|
+
this.channel.evictUser(userId, reason);
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Removes a user's presence
|
|
90
|
+
*/
|
|
91
|
+
removePresence(userId = __classPrivateFieldGet(this, _EventContext_event, "f").sender) {
|
|
92
|
+
this.channel.removePresence(userId);
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.EventContext = EventContext;
|
|
97
|
+
_EventContext_event = new WeakMap(), _EventContext_requestId = new WeakMap(), _EventContext_instances = new WeakSet(), _EventContext_sendMessage = function _EventContext_sendMessage(recipient, event, payload) {
|
|
98
|
+
this.engine.sendMessage(__classPrivateFieldGet(this, _EventContext_event, "f").sender, recipient, pondsocket_common_1.ServerActions.BROADCAST, event, payload, __classPrivateFieldGet(this, _EventContext_requestId, "f"));
|
|
99
|
+
};
|