@eleven-am/pondsocket 0.1.72 → 0.1.73
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/nest.d.ts +31 -0
- package/nest.js +507 -0
- package/package.json +6 -3
- package/types.d.ts +161 -0
package/nest.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {
|
|
2
|
+
OnJoinRequest,
|
|
3
|
+
DChannel as Channel,
|
|
4
|
+
Channels, Endpoints,
|
|
5
|
+
OnConnectionRequest,
|
|
6
|
+
DEndpoint as Endpoint,
|
|
7
|
+
OnEvent, PondSocketModule,
|
|
8
|
+
GetEventParams, GetEventQuery,
|
|
9
|
+
GetEventRequest, GetEventResponse,
|
|
10
|
+
GetJoinParams, GetJoinRequest,
|
|
11
|
+
GetJoinResponse, GetConnectionParams,
|
|
12
|
+
GetConnectionRequest, GetConnectionResponse,
|
|
13
|
+
GetConnectionQuery, GetConnectionHeaders,
|
|
14
|
+
GetConnectionRequestId, GetEventPayload,
|
|
15
|
+
GetUserData, GetUserPresence, GetInternalChannel,
|
|
16
|
+
} from './types';
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
OnJoinRequest,
|
|
20
|
+
Endpoint, PondSocketModule,
|
|
21
|
+
OnConnectionRequest, OnEvent,
|
|
22
|
+
Channel, Channels, Endpoints,
|
|
23
|
+
GetEventQuery, GetJoinParams,
|
|
24
|
+
GetEventParams, GetJoinResponse,
|
|
25
|
+
GetEventRequest, GetJoinRequest,
|
|
26
|
+
GetEventResponse, GetConnectionQuery,
|
|
27
|
+
GetConnectionRequestId, GetEventPayload,
|
|
28
|
+
GetConnectionParams, GetConnectionRequest,
|
|
29
|
+
GetConnectionResponse, GetConnectionHeaders,
|
|
30
|
+
GetUserData, GetUserPresence, GetInternalChannel,
|
|
31
|
+
}
|
package/nest.js
ADDED
|
@@ -0,0 +1,507 @@
|
|
|
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 __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
exports.PondSocketModule = exports.Endpoints = exports.Endpoint = exports.Channels = exports.Channel = exports.OnConnectionRequest = exports.OnEvent = exports.OnJoinRequest = exports.GetConnectionQuery = exports.GetConnectionHeaders = exports.GetConnectionParams = exports.GetConnectionRequestId = exports.GetConnectionResponse = exports.GetConnectionRequest = exports.GetEventRequest = exports.GetEventResponse = exports.GetEventQuery = exports.GetEventParams = exports.GetEventPayload = exports.GetUserPresence = exports.GetInternalChannel = exports.GetUserData = exports.GetJoinParams = exports.GetJoinResponse = exports.GetJoinRequest = void 0;
|
|
24
|
+
require("reflect-metadata");
|
|
25
|
+
const common_1 = require("@nestjs/common");
|
|
26
|
+
const pondSocket_1 = require("./server/pondSocket");
|
|
27
|
+
const joinRequestKey = Symbol('joinRequestKey');
|
|
28
|
+
const joinResponseKey = Symbol('joinResponseKey');
|
|
29
|
+
const joinParamsKey = Symbol('joinParamsKey');
|
|
30
|
+
const userDataKey = Symbol('userDataKey');
|
|
31
|
+
const internalChannelKey = Symbol('internalChannelKey');
|
|
32
|
+
const userPresenceKey = Symbol('userPresenceKey');
|
|
33
|
+
const eventRequestKey = Symbol('eventRequestKey');
|
|
34
|
+
const eventPayloadKey = Symbol('eventPayloadKey');
|
|
35
|
+
const eventParamsKey = Symbol('eventParamsKey');
|
|
36
|
+
const eventQueryKey = Symbol('eventQueryKey');
|
|
37
|
+
const eventResponseKey = Symbol('eventResponseKey');
|
|
38
|
+
const connectionRequestKey = Symbol('connectionRequestKey');
|
|
39
|
+
const connectionResponseKey = Symbol('connectionResponseKey');
|
|
40
|
+
const connectionRequestIdKey = Symbol('connectionRequestIdKey');
|
|
41
|
+
const connectionParamsKey = Symbol('connectionParamsKey');
|
|
42
|
+
const connectionQueryKey = Symbol('connectionQueryKey');
|
|
43
|
+
const connectionHeadersKey = Symbol('connectionHeadersKey');
|
|
44
|
+
const onJoinHandlerKey = Symbol('onJoinHandlerKey');
|
|
45
|
+
const onEventHandlerKey = Symbol('onEventHandlerKey');
|
|
46
|
+
const onConnectionHandlerKey = Symbol('onConnectionHandlerKey');
|
|
47
|
+
const channelsKey = Symbol('channelsKey');
|
|
48
|
+
const endpointsKey = Symbol('endpointsKey');
|
|
49
|
+
function createParamDecorator(key, error) {
|
|
50
|
+
return (target, propertyKey, parameterIndex) => {
|
|
51
|
+
const existingParams = Reflect.getMetadata(key, target, propertyKey);
|
|
52
|
+
if (existingParams) {
|
|
53
|
+
throw new Error(error);
|
|
54
|
+
}
|
|
55
|
+
Reflect.defineMetadata(key, parameterIndex, target, propertyKey);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function resolveParamDecorator(key, target, propertyKey) {
|
|
59
|
+
const index = Reflect.getMetadata(key, target, propertyKey);
|
|
60
|
+
if (typeof index !== 'number') {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
return index;
|
|
64
|
+
}
|
|
65
|
+
function createClassDecorator(key, target) {
|
|
66
|
+
return {
|
|
67
|
+
get() {
|
|
68
|
+
return (Reflect.getMetadata(key, target) || []);
|
|
69
|
+
},
|
|
70
|
+
set(value) {
|
|
71
|
+
Reflect.defineMetadata(key, value, target);
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function manageHandlers(key, target) {
|
|
76
|
+
return {
|
|
77
|
+
get() {
|
|
78
|
+
return (Reflect.getMetadata(key, target) || []);
|
|
79
|
+
},
|
|
80
|
+
set(path, value) {
|
|
81
|
+
const handlers = Reflect.getMetadata(key, target) || [];
|
|
82
|
+
Reflect.defineMetadata(key, [
|
|
83
|
+
...handlers, { path,
|
|
84
|
+
value },
|
|
85
|
+
], target);
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function manageJoinHandlers(target) {
|
|
90
|
+
return manageHandlers(onJoinHandlerKey, target);
|
|
91
|
+
}
|
|
92
|
+
function manageEventHandlers(target) {
|
|
93
|
+
return manageHandlers(onEventHandlerKey, target);
|
|
94
|
+
}
|
|
95
|
+
function manageConnectionHandlers(target) {
|
|
96
|
+
return manageHandlers(onConnectionHandlerKey, target);
|
|
97
|
+
}
|
|
98
|
+
function manageChannels(target) {
|
|
99
|
+
return createClassDecorator(channelsKey, target);
|
|
100
|
+
}
|
|
101
|
+
function manageEndpoints(target) {
|
|
102
|
+
return createClassDecorator(endpointsKey, target);
|
|
103
|
+
}
|
|
104
|
+
function GetJoinRequest() {
|
|
105
|
+
return createParamDecorator(joinRequestKey, 'JoinRequest decorator already applied');
|
|
106
|
+
}
|
|
107
|
+
exports.GetJoinRequest = GetJoinRequest;
|
|
108
|
+
function GetJoinResponse() {
|
|
109
|
+
return createParamDecorator(joinResponseKey, 'JoinResponse decorator already applied');
|
|
110
|
+
}
|
|
111
|
+
exports.GetJoinResponse = GetJoinResponse;
|
|
112
|
+
function GetJoinParams() {
|
|
113
|
+
return createParamDecorator(joinParamsKey, 'JoinParams decorator already applied');
|
|
114
|
+
}
|
|
115
|
+
exports.GetJoinParams = GetJoinParams;
|
|
116
|
+
function GetUserData() {
|
|
117
|
+
return createParamDecorator(userDataKey, 'UserData decorator already applied');
|
|
118
|
+
}
|
|
119
|
+
exports.GetUserData = GetUserData;
|
|
120
|
+
function GetInternalChannel() {
|
|
121
|
+
return createParamDecorator(internalChannelKey, 'InternalChannel decorator already applied');
|
|
122
|
+
}
|
|
123
|
+
exports.GetInternalChannel = GetInternalChannel;
|
|
124
|
+
function GetUserPresence() {
|
|
125
|
+
return createParamDecorator(userPresenceKey, 'UserPresence decorator already applied');
|
|
126
|
+
}
|
|
127
|
+
exports.GetUserPresence = GetUserPresence;
|
|
128
|
+
function GetEventPayload() {
|
|
129
|
+
return createParamDecorator(eventPayloadKey, 'EventPayload decorator already applied');
|
|
130
|
+
}
|
|
131
|
+
exports.GetEventPayload = GetEventPayload;
|
|
132
|
+
function GetEventParams() {
|
|
133
|
+
return createParamDecorator(eventParamsKey, 'EventParams decorator already applied');
|
|
134
|
+
}
|
|
135
|
+
exports.GetEventParams = GetEventParams;
|
|
136
|
+
function GetEventQuery() {
|
|
137
|
+
return createParamDecorator(eventQueryKey, 'EventQuery decorator already applied');
|
|
138
|
+
}
|
|
139
|
+
exports.GetEventQuery = GetEventQuery;
|
|
140
|
+
function GetEventResponse() {
|
|
141
|
+
return createParamDecorator(eventResponseKey, 'EventResponse decorator already applied');
|
|
142
|
+
}
|
|
143
|
+
exports.GetEventResponse = GetEventResponse;
|
|
144
|
+
function GetEventRequest() {
|
|
145
|
+
return createParamDecorator(eventRequestKey, 'EventRequest decorator already applied');
|
|
146
|
+
}
|
|
147
|
+
exports.GetEventRequest = GetEventRequest;
|
|
148
|
+
function GetConnectionRequest() {
|
|
149
|
+
return createParamDecorator(connectionRequestKey, 'ConnectionRequest decorator already applied');
|
|
150
|
+
}
|
|
151
|
+
exports.GetConnectionRequest = GetConnectionRequest;
|
|
152
|
+
function GetConnectionResponse() {
|
|
153
|
+
return createParamDecorator(connectionResponseKey, 'ConnectionResponse decorator already applied');
|
|
154
|
+
}
|
|
155
|
+
exports.GetConnectionResponse = GetConnectionResponse;
|
|
156
|
+
function GetConnectionRequestId() {
|
|
157
|
+
return createParamDecorator(connectionRequestIdKey, 'ConnectionRequestId decorator already applied');
|
|
158
|
+
}
|
|
159
|
+
exports.GetConnectionRequestId = GetConnectionRequestId;
|
|
160
|
+
function GetConnectionParams() {
|
|
161
|
+
return createParamDecorator(connectionParamsKey, 'ConnectionParams decorator already applied');
|
|
162
|
+
}
|
|
163
|
+
exports.GetConnectionParams = GetConnectionParams;
|
|
164
|
+
function GetConnectionHeaders() {
|
|
165
|
+
return createParamDecorator(connectionHeadersKey, 'ConnectionHeaders decorator already applied');
|
|
166
|
+
}
|
|
167
|
+
exports.GetConnectionHeaders = GetConnectionHeaders;
|
|
168
|
+
function GetConnectionQuery() {
|
|
169
|
+
return createParamDecorator(connectionQueryKey, 'ConnectionQuery decorator already applied');
|
|
170
|
+
}
|
|
171
|
+
exports.GetConnectionQuery = GetConnectionQuery;
|
|
172
|
+
function resolveJoinParameters(request, response, target, propertyKey) {
|
|
173
|
+
const joinRequestIndex = resolveParamDecorator(joinRequestKey, target, propertyKey);
|
|
174
|
+
const joinParamsIndex = resolveParamDecorator(joinParamsKey, target, propertyKey);
|
|
175
|
+
const userDataIndex = resolveParamDecorator(userDataKey, target, propertyKey);
|
|
176
|
+
const internalChannelIndex = resolveParamDecorator(internalChannelKey, target, propertyKey);
|
|
177
|
+
const eventParamsIndex = resolveParamDecorator(eventParamsKey, target, propertyKey);
|
|
178
|
+
const eventQueryIndex = resolveParamDecorator(eventQueryKey, target, propertyKey);
|
|
179
|
+
const JoinResponseIndex = resolveParamDecorator(joinResponseKey, target, propertyKey);
|
|
180
|
+
const array = [
|
|
181
|
+
joinRequestIndex,
|
|
182
|
+
joinParamsIndex,
|
|
183
|
+
userDataIndex,
|
|
184
|
+
internalChannelIndex,
|
|
185
|
+
eventParamsIndex,
|
|
186
|
+
eventQueryIndex,
|
|
187
|
+
JoinResponseIndex,
|
|
188
|
+
].filter((index) => typeof index === 'number');
|
|
189
|
+
const rejectedKeys = [
|
|
190
|
+
eventPayloadKey,
|
|
191
|
+
eventRequestKey,
|
|
192
|
+
eventResponseKey,
|
|
193
|
+
userPresenceKey,
|
|
194
|
+
connectionRequestKey,
|
|
195
|
+
connectionResponseKey,
|
|
196
|
+
connectionRequestIdKey,
|
|
197
|
+
connectionParamsKey,
|
|
198
|
+
connectionQueryKey,
|
|
199
|
+
]
|
|
200
|
+
.map((key) => resolveParamDecorator(key, target, propertyKey))
|
|
201
|
+
.filter((index) => typeof index === 'number');
|
|
202
|
+
if (rejectedKeys.length) {
|
|
203
|
+
throw new Error(`Invalid parameter decorators: ${rejectedKeys.join(', ')}`);
|
|
204
|
+
}
|
|
205
|
+
return array
|
|
206
|
+
.sort((a, b) => a - b)
|
|
207
|
+
.map((index) => {
|
|
208
|
+
switch (index) {
|
|
209
|
+
case joinRequestIndex:
|
|
210
|
+
return request;
|
|
211
|
+
case joinParamsIndex:
|
|
212
|
+
return request.joinParams;
|
|
213
|
+
case userDataIndex:
|
|
214
|
+
return request.user;
|
|
215
|
+
case internalChannelIndex:
|
|
216
|
+
return request.channel;
|
|
217
|
+
case eventParamsIndex:
|
|
218
|
+
return request.event.params;
|
|
219
|
+
case eventQueryIndex:
|
|
220
|
+
return request.event.query;
|
|
221
|
+
case JoinResponseIndex:
|
|
222
|
+
return response;
|
|
223
|
+
default:
|
|
224
|
+
throw new Error('Invalid parameter decorator');
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
function resolveEventParameters(request, response, target, propertyKey) {
|
|
229
|
+
const userDataIndex = resolveParamDecorator(userDataKey, target, propertyKey);
|
|
230
|
+
const internalChannelIndex = resolveParamDecorator(internalChannelKey, target, propertyKey);
|
|
231
|
+
const eventParamsIndex = resolveParamDecorator(eventParamsKey, target, propertyKey);
|
|
232
|
+
const eventQueryIndex = resolveParamDecorator(eventQueryKey, target, propertyKey);
|
|
233
|
+
const eventPayloadIndex = resolveParamDecorator(eventPayloadKey, target, propertyKey);
|
|
234
|
+
const eventResponseIndex = resolveParamDecorator(eventResponseKey, target, propertyKey);
|
|
235
|
+
const eventRequestIndex = resolveParamDecorator(eventRequestKey, target, propertyKey);
|
|
236
|
+
const array = [
|
|
237
|
+
userDataIndex,
|
|
238
|
+
internalChannelIndex,
|
|
239
|
+
eventParamsIndex,
|
|
240
|
+
eventQueryIndex,
|
|
241
|
+
eventPayloadIndex,
|
|
242
|
+
eventResponseIndex,
|
|
243
|
+
eventRequestIndex,
|
|
244
|
+
].filter((index) => typeof index === 'number');
|
|
245
|
+
const rejectedKeys = [
|
|
246
|
+
joinRequestKey,
|
|
247
|
+
joinResponseKey,
|
|
248
|
+
joinParamsKey,
|
|
249
|
+
userPresenceKey,
|
|
250
|
+
connectionRequestKey,
|
|
251
|
+
connectionResponseKey,
|
|
252
|
+
connectionRequestIdKey,
|
|
253
|
+
connectionParamsKey,
|
|
254
|
+
connectionQueryKey,
|
|
255
|
+
]
|
|
256
|
+
.map((key) => resolveParamDecorator(key, target, propertyKey))
|
|
257
|
+
.filter((index) => typeof index === 'number');
|
|
258
|
+
if (rejectedKeys.length) {
|
|
259
|
+
throw new Error(`Invalid parameter decorators: ${rejectedKeys.join(', ')}`);
|
|
260
|
+
}
|
|
261
|
+
return array
|
|
262
|
+
.sort((a, b) => a - b)
|
|
263
|
+
.map((index) => {
|
|
264
|
+
switch (index) {
|
|
265
|
+
case userDataIndex:
|
|
266
|
+
return request.user;
|
|
267
|
+
case internalChannelIndex:
|
|
268
|
+
return request.channel;
|
|
269
|
+
case eventParamsIndex:
|
|
270
|
+
return request.event.params;
|
|
271
|
+
case eventQueryIndex:
|
|
272
|
+
return request.event.query;
|
|
273
|
+
case eventPayloadIndex:
|
|
274
|
+
return request.event.payload;
|
|
275
|
+
case eventResponseIndex:
|
|
276
|
+
return response;
|
|
277
|
+
case eventRequestIndex:
|
|
278
|
+
return request;
|
|
279
|
+
default:
|
|
280
|
+
throw new Error('Invalid parameter decorator');
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
function resolveConnectionParameters(request, response, target, propertyKey) {
|
|
285
|
+
const connectionRequestIndex = resolveParamDecorator(connectionRequestKey, target, propertyKey);
|
|
286
|
+
const connectionResponseIndex = resolveParamDecorator(connectionResponseKey, target, propertyKey);
|
|
287
|
+
const connectionRequestIdIndex = resolveParamDecorator(connectionRequestIdKey, target, propertyKey);
|
|
288
|
+
const connectionParamsIndex = resolveParamDecorator(connectionParamsKey, target, propertyKey);
|
|
289
|
+
const connectionQueryIndex = resolveParamDecorator(connectionQueryKey, target, propertyKey);
|
|
290
|
+
const connectionHeadersIndex = resolveParamDecorator(connectionHeadersKey, target, propertyKey);
|
|
291
|
+
const array = [
|
|
292
|
+
connectionRequestIndex,
|
|
293
|
+
connectionResponseIndex,
|
|
294
|
+
connectionRequestIdIndex,
|
|
295
|
+
connectionParamsIndex,
|
|
296
|
+
connectionQueryIndex,
|
|
297
|
+
connectionHeadersIndex,
|
|
298
|
+
].filter((index) => typeof index === 'number');
|
|
299
|
+
const rejectedKeys = [
|
|
300
|
+
joinRequestKey,
|
|
301
|
+
joinResponseKey,
|
|
302
|
+
joinParamsKey,
|
|
303
|
+
userDataKey,
|
|
304
|
+
internalChannelKey,
|
|
305
|
+
eventParamsKey,
|
|
306
|
+
eventQueryKey,
|
|
307
|
+
eventPayloadKey,
|
|
308
|
+
eventResponseKey,
|
|
309
|
+
userPresenceKey,
|
|
310
|
+
eventRequestKey,
|
|
311
|
+
]
|
|
312
|
+
.map((key) => resolveParamDecorator(key, target, propertyKey))
|
|
313
|
+
.filter((index) => typeof index === 'number');
|
|
314
|
+
if (rejectedKeys.length) {
|
|
315
|
+
throw new Error(`Invalid parameter decorators: ${rejectedKeys.join(', ')}`);
|
|
316
|
+
}
|
|
317
|
+
return array
|
|
318
|
+
.sort((a, b) => a - b)
|
|
319
|
+
.map((index) => {
|
|
320
|
+
switch (index) {
|
|
321
|
+
case connectionRequestIndex:
|
|
322
|
+
return request;
|
|
323
|
+
case connectionResponseIndex:
|
|
324
|
+
return response;
|
|
325
|
+
case connectionRequestIdIndex:
|
|
326
|
+
return request.id;
|
|
327
|
+
case connectionParamsIndex:
|
|
328
|
+
return request.params;
|
|
329
|
+
case connectionQueryIndex:
|
|
330
|
+
return request.query;
|
|
331
|
+
case connectionHeadersIndex:
|
|
332
|
+
return request.headers;
|
|
333
|
+
default:
|
|
334
|
+
throw new Error('Invalid parameter decorator');
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
function OnJoinRequest() {
|
|
339
|
+
return (target, propertyKey, descriptor) => {
|
|
340
|
+
const originalMethod = descriptor.value;
|
|
341
|
+
const { set } = manageJoinHandlers(target);
|
|
342
|
+
set('', (request, response) => __awaiter(this, void 0, void 0, function* () {
|
|
343
|
+
try {
|
|
344
|
+
const data = yield originalMethod.apply(target, resolveJoinParameters(request, response, target, propertyKey));
|
|
345
|
+
if (!response.hasResponded) {
|
|
346
|
+
if (data) {
|
|
347
|
+
const { event } = data, rest = __rest(data, ["event"]);
|
|
348
|
+
response.send(event !== null && event !== void 0 ? event : 'response', rest);
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
response.accept();
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
response.reject(String(error));
|
|
357
|
+
}
|
|
358
|
+
}));
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
exports.OnJoinRequest = OnJoinRequest;
|
|
362
|
+
function OnEvent(event = '*') {
|
|
363
|
+
return (target, propertyKey, descriptor) => {
|
|
364
|
+
const originalMethod = descriptor.value;
|
|
365
|
+
const { set } = manageEventHandlers(target);
|
|
366
|
+
set(event, (request, response) => __awaiter(this, void 0, void 0, function* () {
|
|
367
|
+
try {
|
|
368
|
+
const data = yield originalMethod.apply(target, resolveEventParameters(request, response, target, propertyKey));
|
|
369
|
+
if (!response.hasResponded) {
|
|
370
|
+
if (data) {
|
|
371
|
+
const { event } = data, rest = __rest(data, ["event"]);
|
|
372
|
+
response.send(event !== null && event !== void 0 ? event : 'response', rest);
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
response.accept();
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
catch (error) {
|
|
380
|
+
response.reject(String(error));
|
|
381
|
+
}
|
|
382
|
+
}));
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
exports.OnEvent = OnEvent;
|
|
386
|
+
function OnConnectionRequest() {
|
|
387
|
+
return (target, propertyKey, descriptor) => {
|
|
388
|
+
const originalMethod = descriptor.value;
|
|
389
|
+
const { set } = manageConnectionHandlers(target);
|
|
390
|
+
set('', (request, response) => __awaiter(this, void 0, void 0, function* () {
|
|
391
|
+
try {
|
|
392
|
+
const data = yield originalMethod.apply(target, resolveConnectionParameters(request, response, target, propertyKey));
|
|
393
|
+
if (!response.hasResponded) {
|
|
394
|
+
if (data) {
|
|
395
|
+
const { event } = data, rest = __rest(data, ["event"]);
|
|
396
|
+
response.send(event !== null && event !== void 0 ? event : 'response', rest);
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
response.accept();
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
catch (error) {
|
|
404
|
+
response.reject(String(error));
|
|
405
|
+
}
|
|
406
|
+
}));
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
exports.OnConnectionRequest = OnConnectionRequest;
|
|
410
|
+
function Channel(path = '*') {
|
|
411
|
+
return (constructor) => class extends constructor {
|
|
412
|
+
_setEndpoint(endpoint) {
|
|
413
|
+
const channel = endpoint.createChannel(path, (request, response) => __awaiter(this, void 0, void 0, function* () {
|
|
414
|
+
const { get } = manageJoinHandlers(this);
|
|
415
|
+
const [handler] = get();
|
|
416
|
+
if (handler) {
|
|
417
|
+
yield handler.value(request, response);
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
response.accept();
|
|
421
|
+
}
|
|
422
|
+
}));
|
|
423
|
+
const { get } = manageEventHandlers(this);
|
|
424
|
+
const handlers = get();
|
|
425
|
+
handlers.forEach((handler) => {
|
|
426
|
+
channel.onEvent(handler.path, (request, response) => __awaiter(this, void 0, void 0, function* () {
|
|
427
|
+
yield handler.value(request, response);
|
|
428
|
+
}));
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
exports.Channel = Channel;
|
|
434
|
+
function Channels(channels) {
|
|
435
|
+
return (constructor) => {
|
|
436
|
+
const { set } = manageChannels(constructor.prototype);
|
|
437
|
+
set(channels);
|
|
438
|
+
return class extends constructor {
|
|
439
|
+
constructor() {
|
|
440
|
+
super(...arguments);
|
|
441
|
+
this._channels = channels;
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
exports.Channels = Channels;
|
|
447
|
+
function Endpoint(path = '*') {
|
|
448
|
+
return (constructor) => class extends constructor {
|
|
449
|
+
_setSocket(moduleRef, socket) {
|
|
450
|
+
var _a;
|
|
451
|
+
const { get } = manageConnectionHandlers(this);
|
|
452
|
+
const [handler] = get();
|
|
453
|
+
const endpoint = socket.createEndpoint(path, (request, response) => __awaiter(this, void 0, void 0, function* () {
|
|
454
|
+
if (handler) {
|
|
455
|
+
yield handler.value(request, response);
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
response.accept();
|
|
459
|
+
}
|
|
460
|
+
}));
|
|
461
|
+
const channels = (_a = this._channels) !== null && _a !== void 0 ? _a : [];
|
|
462
|
+
channels.forEach((channel) => {
|
|
463
|
+
const chan = moduleRef.get(channel, {
|
|
464
|
+
strict: false,
|
|
465
|
+
});
|
|
466
|
+
chan._setEndpoint(endpoint);
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
exports.Endpoint = Endpoint;
|
|
472
|
+
function Endpoints(endpoints) {
|
|
473
|
+
return (constructor) => {
|
|
474
|
+
const channels = endpoints.reduce((acc, endpoint) => {
|
|
475
|
+
const { get } = manageChannels(endpoint.prototype);
|
|
476
|
+
const channels = get();
|
|
477
|
+
return [...acc, ...channels];
|
|
478
|
+
}, []);
|
|
479
|
+
// eslint-disable-next-line new-cap
|
|
480
|
+
const NestJSModule = (0, common_1.Module)({
|
|
481
|
+
providers: [...endpoints, ...channels],
|
|
482
|
+
});
|
|
483
|
+
const { set } = manageEndpoints(constructor.prototype);
|
|
484
|
+
set(endpoints);
|
|
485
|
+
// eslint-disable-next-line new-cap
|
|
486
|
+
return NestJSModule(constructor);
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
exports.Endpoints = Endpoints;
|
|
490
|
+
class PondSocketModule {
|
|
491
|
+
constructor(moduleRef, adapterHost) {
|
|
492
|
+
this.moduleRef = moduleRef;
|
|
493
|
+
this.adapterHost = adapterHost;
|
|
494
|
+
const expressInstance = this.adapterHost.httpAdapter;
|
|
495
|
+
const server = expressInstance.getHttpServer();
|
|
496
|
+
this.socket = new pondSocket_1.PondSocket(server);
|
|
497
|
+
expressInstance.listen = (...args) => {
|
|
498
|
+
const { get } = manageEndpoints(this);
|
|
499
|
+
get().forEach((endpoint) => {
|
|
500
|
+
const instance = this.moduleRef.get(endpoint, { strict: false });
|
|
501
|
+
instance._setSocket(this.moduleRef, this.socket);
|
|
502
|
+
});
|
|
503
|
+
this.socket.listen(...args);
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
exports.PondSocketModule = PondSocketModule;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eleven-am/pondsocket",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.73",
|
|
4
4
|
"description": "PondSocket is a fast simple socket server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"socket",
|
|
@@ -29,8 +29,11 @@
|
|
|
29
29
|
"url": "git+https://github.com/Eleven-am/pondSocket.git"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
+
"@nestjs/common": "^10.2.10",
|
|
33
|
+
"@nestjs/core": "^10.2.10",
|
|
34
|
+
"reflect-metadata": "^0.1.14",
|
|
32
35
|
"websocket": "^1.0.34",
|
|
33
|
-
"ws": "^8.15.
|
|
36
|
+
"ws": "^8.15.1"
|
|
34
37
|
},
|
|
35
38
|
"devDependencies": {
|
|
36
39
|
"@types/express": "^4.17.21",
|
|
@@ -38,7 +41,7 @@
|
|
|
38
41
|
"@types/node": "^20.10.4",
|
|
39
42
|
"@types/websocket": "^1.0.10",
|
|
40
43
|
"@types/ws": "^8.5.10",
|
|
41
|
-
"@typescript-eslint/eslint-plugin": "^6.
|
|
44
|
+
"@typescript-eslint/eslint-plugin": "^6.14.0",
|
|
42
45
|
"eslint": "^8.55.0",
|
|
43
46
|
"eslint-plugin-file-progress": "^1.3.0",
|
|
44
47
|
"eslint-plugin-import": "^2.29.0",
|
package/types.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { Server as HTTPServer, IncomingHttpHeaders } from 'http';
|
|
2
2
|
|
|
3
|
+
import type { ModuleRef, HttpAdapterHost } from '@nestjs/core';
|
|
3
4
|
import type { Express } from 'express';
|
|
4
5
|
import { WebSocketServer } from 'ws';
|
|
5
6
|
|
|
6
7
|
type Unsubscribe = () => void;
|
|
7
8
|
|
|
9
|
+
type Constructor<T> = new (...args: any[]) => T;
|
|
10
|
+
type MethodDecorator = (target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<unknown>) => TypedPropertyDescriptor<unknown> | void;
|
|
8
11
|
export type default_t<T = any> = Record<string, T>;
|
|
9
12
|
type IsParam<Path> = Path extends `:${infer Param}` ? Param : never;
|
|
10
13
|
|
|
@@ -615,3 +618,161 @@ declare class PondClient {
|
|
|
615
618
|
|
|
616
619
|
declare const pondSocket: (app: Express) => PondSocketExpressApp;
|
|
617
620
|
|
|
621
|
+
/**
|
|
622
|
+
* @desc The Decorator for retrieving the JoinRequest object from the request in a handler
|
|
623
|
+
* @returns {JoinRequest}
|
|
624
|
+
*/
|
|
625
|
+
declare function GetJoinRequest(): MethodDecorator;
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* @desc The Decorator for retrieving the JoinResponse object from the response in a handler
|
|
629
|
+
* @returns {JoinResponse}
|
|
630
|
+
*/
|
|
631
|
+
declare function GetJoinResponse(): MethodDecorator;
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* @desc The Decorator for retrieving the JoinParams object from the request in a handler
|
|
635
|
+
* @returns {JoinParams}
|
|
636
|
+
*/
|
|
637
|
+
declare function GetJoinParams(): MethodDecorator;
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* @desc The Decorator for retrieving the UserData object from the request in a handler
|
|
641
|
+
* @returns {UserData}
|
|
642
|
+
*/
|
|
643
|
+
declare function GetUserData(): MethodDecorator;
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* @desc The Decorator for retrieving the Channel object from the request in a handler
|
|
647
|
+
* @returns {Channel}
|
|
648
|
+
*/
|
|
649
|
+
declare function GetInternalChannel(): MethodDecorator;
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* @desc The Decorator for retrieving the UserPresence object from the request in a handler
|
|
653
|
+
* @returns {PondPresence}
|
|
654
|
+
*/
|
|
655
|
+
declare function GetUserPresence(): MethodDecorator;
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* @desc The Decorator for retrieving the event payload from the request in a handler
|
|
659
|
+
* @returns {PondMessage}
|
|
660
|
+
*/
|
|
661
|
+
declare function GetEventPayload(): MethodDecorator;
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* @desc The Decorator for retrieving the EventRequest Params object from the request in a handler
|
|
665
|
+
* @returns {EventParams}
|
|
666
|
+
*/
|
|
667
|
+
declare function GetEventParams(): MethodDecorator;
|
|
668
|
+
|
|
669
|
+
/**
|
|
670
|
+
* @desc The Decorator for retrieving the EventRequest Query object from the request in a handler
|
|
671
|
+
* @returns {EventParams}
|
|
672
|
+
*/
|
|
673
|
+
declare function GetEventQuery(): MethodDecorator;
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* @desc The Decorator for retrieving the EventResponse in a handler
|
|
677
|
+
* @returns {EventResponse}
|
|
678
|
+
*/
|
|
679
|
+
declare function GetEventResponse(): MethodDecorator;
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* @desc The Decorator for retrieving the EventRequest in a handler
|
|
683
|
+
* @returns {EventRequest}
|
|
684
|
+
*/
|
|
685
|
+
declare function GetEventRequest(): MethodDecorator;
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* @desc The Decorator for retrieving the ConnectionReQuest in a handler
|
|
689
|
+
* @returns {IncomingConnection}
|
|
690
|
+
*/
|
|
691
|
+
declare function GetConnectionRequest(): MethodDecorator;
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* @desc The Decorator for retrieving the ConnectionResponse in a handler
|
|
695
|
+
* @returns {ConnectionResponse}
|
|
696
|
+
*/
|
|
697
|
+
declare function GetConnectionResponse(): MethodDecorator;
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* @desc The Decorator for retrieving the ConnectionRequestId in a handler
|
|
701
|
+
* @returns {string}
|
|
702
|
+
*/
|
|
703
|
+
declare function GetConnectionRequestId(): MethodDecorator;
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* @desc The Decorator for retrieving the ConnectionParams in a handler
|
|
707
|
+
* @returns {EventParams}
|
|
708
|
+
*/
|
|
709
|
+
declare function GetConnectionParams(): MethodDecorator;
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* @desc The Decorator for retrieving the ConnectionHeaders from the request in a handler
|
|
713
|
+
* @returns {EventParams}
|
|
714
|
+
*/
|
|
715
|
+
declare function GetConnectionHeaders(): MethodDecorator;
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* @desc The Decorator for retrieving the ConnectionQuery in a handler
|
|
719
|
+
* @returns {EventParams}
|
|
720
|
+
*/
|
|
721
|
+
declare function GetConnectionQuery(): MethodDecorator;
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
* @desc Marks a method as a handler for JoinRequest events.
|
|
725
|
+
*/
|
|
726
|
+
declare function OnJoinRequest(): MethodDecorator;
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* @desc Marks a method as a handler for events with the specified name.
|
|
730
|
+
* @param event - The name of the event to handle.
|
|
731
|
+
*/
|
|
732
|
+
declare function OnEvent(event?: string): MethodDecorator;
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* @desc Marks a method as a handler for ConnectionRequest events.
|
|
736
|
+
*/
|
|
737
|
+
declare function OnConnectionRequest(): MethodDecorator;
|
|
738
|
+
|
|
739
|
+
/**
|
|
740
|
+
* Decorator to mark a class as a channel.
|
|
741
|
+
* @param path - The path for the channel (default is '*').
|
|
742
|
+
*/
|
|
743
|
+
declare function DChannel<T extends Constructor<NonNullable<unknown>>>(
|
|
744
|
+
path?: string
|
|
745
|
+
): (constructor: T) => void;
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* Decorator to mark a class as having multiple channels.
|
|
749
|
+
* @param channels - The array of channels.
|
|
750
|
+
*/
|
|
751
|
+
declare function Channels<T extends Constructor<NonNullable<unknown>>>(
|
|
752
|
+
channels: Constructor<NonNullable<unknown>>[]
|
|
753
|
+
): (constructor: T) => void;
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* Decorator to mark a class as an endpoint.
|
|
757
|
+
* @param path - The path for the endpoint (default is '*').
|
|
758
|
+
*/
|
|
759
|
+
declare function DEndpoint<T extends Constructor<NonNullable<unknown>>>(
|
|
760
|
+
path?: string
|
|
761
|
+
): (constructor: T) => void;
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Decorator to mark a class as having multiple endpoints.
|
|
765
|
+
* @param endpoints - The array of endpoints.
|
|
766
|
+
*/
|
|
767
|
+
declare function Endpoints<T extends Constructor<NonNullable<unknown>>>(
|
|
768
|
+
endpoints: Constructor<NonNullable<unknown>>[]
|
|
769
|
+
): (constructor: T) => void;
|
|
770
|
+
|
|
771
|
+
declare class PondSocketModule {
|
|
772
|
+
/**
|
|
773
|
+
* @desc The NestJS module for the PondSocketModule
|
|
774
|
+
* @param moduleRef - The NestJS moduleRef
|
|
775
|
+
* @param adapterHost - The NestJS adapterHost
|
|
776
|
+
*/
|
|
777
|
+
constructor (moduleRef: ModuleRef, adapterHost: HttpAdapterHost);
|
|
778
|
+
}
|