@elysiumjs/core 0.4.2 → 0.5.0
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 +26 -17
- package/dist/app.d.ts +180 -0
- package/dist/cache.d.ts +114 -0
- package/dist/command.d.ts +273 -0
- package/dist/console.d.ts +167 -0
- package/dist/database.d.ts +113 -0
- package/dist/env.d.ts +30 -0
- package/dist/event.d.ts +94 -0
- package/dist/http.d.ts +242 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +24 -0
- package/dist/job.d.ts +106 -0
- package/dist/macros.d.ts +2 -0
- package/dist/middleware.d.ts +70 -0
- package/dist/model.d.ts +4803 -0
- package/dist/module.d.ts +45 -0
- package/dist/redis.d.ts +134 -0
- package/dist/repository.d.ts +164 -0
- package/dist/service.d.ts +132 -0
- package/dist/utils.d.ts +26 -0
- package/dist/wamp.d.ts +273 -0
- package/dist/websocket.d.ts +92 -0
- package/dist/worker.d.ts +220 -0
- package/package.json +15 -8
- package/tsconfig.json +34 -33
- package/index.ts +0 -54
- package/macros.ts +0 -16
- package/src/app.ts +0 -647
- package/src/cache.ts +0 -218
- package/src/command.ts +0 -908
- package/src/console.ts +0 -493
- package/src/database.ts +0 -257
- package/src/env.ts +0 -62
- package/src/event.ts +0 -227
- package/src/http.ts +0 -736
- package/src/job.ts +0 -196
- package/src/middleware.ts +0 -164
- package/src/model.ts +0 -386
- package/src/module.ts +0 -104
- package/src/redis.ts +0 -408
- package/src/repository.ts +0 -295
- package/src/service.ts +0 -351
- package/src/utils.ts +0 -95
- package/src/wamp.ts +0 -569
- package/src/websocket.ts +0 -191
- package/src/worker.ts +0 -849
- package/types.d.ts +0 -35
package/src/wamp.ts
DELETED
|
@@ -1,569 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import type { Class, Primitive } from 'type-fest';
|
|
16
|
-
|
|
17
|
-
import { Elysia } from 'elysia';
|
|
18
|
-
import { omit } from 'radash';
|
|
19
|
-
// @ts-expect-error The Wampy type definitions are not up to date
|
|
20
|
-
import Wampy from 'wampy';
|
|
21
|
-
|
|
22
|
-
import { Event } from './event';
|
|
23
|
-
import { Service } from './service';
|
|
24
|
-
import { nextTick, Symbols } from './utils';
|
|
25
|
-
|
|
26
|
-
// FIXME: Temporary solution for Bun to properly fill the protocol field. Remove this once oven-sh/bun#18744 is fixed.
|
|
27
|
-
class WampWebsocket extends WebSocket {
|
|
28
|
-
public constructor(
|
|
29
|
-
url: string,
|
|
30
|
-
private readonly protocols?: string | string[]
|
|
31
|
-
) {
|
|
32
|
-
super(url, protocols);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public get protocol(): string {
|
|
36
|
-
return this.protocols?.[0] ?? 'wamp.2.json';
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Options used to register a WAMP RPC for invocation.
|
|
42
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
43
|
-
*/
|
|
44
|
-
export type WampRegistrationOptions = {
|
|
45
|
-
/**
|
|
46
|
-
* The match policy to use for the registration.
|
|
47
|
-
* @default 'exact'
|
|
48
|
-
*/
|
|
49
|
-
match?: 'prefix' | 'exact' | 'wildcard';
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* The invocation policy to use for the registration.
|
|
53
|
-
* @default 'single'
|
|
54
|
-
*/
|
|
55
|
-
invoke?: 'single' | 'first' | 'last' | 'roundrobin' | 'random';
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Options used to subscribe to a WAMP topic.
|
|
60
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
61
|
-
*/
|
|
62
|
-
export type WampSubscriptionOptions = Omit<WampRegistrationOptions, 'invoke'>;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Properties required when declaring a WAMP controller using the `@wamp()` decorator.
|
|
66
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
67
|
-
*/
|
|
68
|
-
export type WampProps = {
|
|
69
|
-
/**
|
|
70
|
-
* The name of the connection to use. This connection must be registered in the application properties.
|
|
71
|
-
* @default 'default'
|
|
72
|
-
*/
|
|
73
|
-
connection?: string;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Properties required when declaring a WAMP client in the application properties.
|
|
78
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
79
|
-
*/
|
|
80
|
-
export type WampClientProps = {
|
|
81
|
-
/**
|
|
82
|
-
* The URL to the WAMP server.
|
|
83
|
-
*/
|
|
84
|
-
url: string;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* The name of the realm to connect to.
|
|
88
|
-
*/
|
|
89
|
-
realm: string;
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Whether to automatically reconnect to the WAMP server on connection loss.
|
|
93
|
-
*/
|
|
94
|
-
autoReconnect?: boolean;
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* The maximum number of times to retry connecting to the WAMP server.
|
|
98
|
-
*/
|
|
99
|
-
maxRetries?: number;
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* The interval to wait between retries when connecting to the WAMP server.
|
|
103
|
-
*/
|
|
104
|
-
retryInterval?: number;
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* The authentication ID to use in challenges.
|
|
108
|
-
*/
|
|
109
|
-
authid?: string;
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* An array of supported authentication methods.
|
|
113
|
-
*/
|
|
114
|
-
authmethods?: string[];
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Callback for handling WAMP authentication challenges.
|
|
118
|
-
* @param method The authentication method.
|
|
119
|
-
* @param extra Authentication metadata.
|
|
120
|
-
*/
|
|
121
|
-
onChallenge?(method: string, extra: any): string | null;
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Callback for handling the WAMP connection open event.
|
|
125
|
-
*/
|
|
126
|
-
onOpen?(): void;
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Callback for handling the WAMP connection close event.
|
|
130
|
-
*/
|
|
131
|
-
onClose?(): void;
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Callback for handling the WAMP connection error event.
|
|
135
|
-
*/
|
|
136
|
-
onError?(): void;
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Callback for handling the WAMP connection reconnect event.
|
|
140
|
-
*/
|
|
141
|
-
onReconnect?(): void;
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Callback for handling the WAMP connection reconnect success event.
|
|
145
|
-
*/
|
|
146
|
-
onReconnectSuccess?(): void;
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
export type WampClient = {
|
|
150
|
-
/**
|
|
151
|
-
* Disconnects from WAMP server. Clears all queues, subscriptions and calls.
|
|
152
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
153
|
-
*/
|
|
154
|
-
disconnect(): Promise<void>;
|
|
155
|
-
|
|
156
|
-
subscribe(
|
|
157
|
-
topic: string,
|
|
158
|
-
handler: WampSubscriptionHandler,
|
|
159
|
-
options?: WampSubscriptionOptions
|
|
160
|
-
): Promise<{
|
|
161
|
-
topic: string;
|
|
162
|
-
requestId: string;
|
|
163
|
-
subscriptionId: string;
|
|
164
|
-
subscriptionKey: string;
|
|
165
|
-
}>;
|
|
166
|
-
|
|
167
|
-
unsubscribe(
|
|
168
|
-
subscriptionIdOrKey: string,
|
|
169
|
-
handler?: WampSubscriptionHandler
|
|
170
|
-
): Promise<{
|
|
171
|
-
topic: string;
|
|
172
|
-
requestId: string;
|
|
173
|
-
}>;
|
|
174
|
-
|
|
175
|
-
publish(
|
|
176
|
-
topic: string,
|
|
177
|
-
params?:
|
|
178
|
-
| Primitive
|
|
179
|
-
| Array<any>
|
|
180
|
-
| Record<string, Primitive | Array<any>>
|
|
181
|
-
| { argsList: Array<any>; argsDict: Record<string, any> },
|
|
182
|
-
options?: {
|
|
183
|
-
exclude?: number | number[];
|
|
184
|
-
exclude_authid?: string | string[];
|
|
185
|
-
exclude_authrole?: string | string[];
|
|
186
|
-
eligible?: number | number[];
|
|
187
|
-
eligible_authid?: string | string[];
|
|
188
|
-
eligible_authrole?: string | string[];
|
|
189
|
-
exclude_me?: boolean;
|
|
190
|
-
disclose_me?: boolean;
|
|
191
|
-
ppt_scheme?: string;
|
|
192
|
-
ppt_serializer?: string;
|
|
193
|
-
ppt_cipher?: string;
|
|
194
|
-
ppt_keyid?: string;
|
|
195
|
-
}
|
|
196
|
-
): Promise<{
|
|
197
|
-
topic: string;
|
|
198
|
-
requestId: string;
|
|
199
|
-
publicationId: string;
|
|
200
|
-
}>;
|
|
201
|
-
|
|
202
|
-
call(
|
|
203
|
-
procedure: string,
|
|
204
|
-
params?:
|
|
205
|
-
| Primitive
|
|
206
|
-
| Array<any>
|
|
207
|
-
| Record<string, Primitive | Array<any>>
|
|
208
|
-
| { argsList: Array<any>; argsDict: Record<string, any> },
|
|
209
|
-
options?: {
|
|
210
|
-
disclose_me?: boolean;
|
|
211
|
-
progress_callback?(args: { argsList: Array<any>; argsDict: Record<string, any> }): void;
|
|
212
|
-
timeout?: number;
|
|
213
|
-
ppt_scheme?: string;
|
|
214
|
-
ppt_serializer?: string;
|
|
215
|
-
ppt_cipher?: string;
|
|
216
|
-
ppt_keyid?: string;
|
|
217
|
-
}
|
|
218
|
-
): Promise<{
|
|
219
|
-
details: Record<string, any>;
|
|
220
|
-
argsList?: Array<any>;
|
|
221
|
-
argsDict?: Record<string, any>;
|
|
222
|
-
}>;
|
|
223
|
-
|
|
224
|
-
cancel(requestId: string, options?: { mode?: 'kill' | 'killnowait' | 'skip' }): true;
|
|
225
|
-
|
|
226
|
-
register(
|
|
227
|
-
topic: string,
|
|
228
|
-
handler: WampRegistrationHandler,
|
|
229
|
-
options?: WampRegistrationOptions
|
|
230
|
-
): Promise<{
|
|
231
|
-
topic: string;
|
|
232
|
-
requestId: string;
|
|
233
|
-
registrationId: string;
|
|
234
|
-
}>;
|
|
235
|
-
|
|
236
|
-
unregister(registrationId: string): Promise<{
|
|
237
|
-
topic: string;
|
|
238
|
-
requestId: string;
|
|
239
|
-
}>;
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Data sent to a WAMP RPC.
|
|
244
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
245
|
-
*/
|
|
246
|
-
export type WampRegistrationHandlerArgs<TList = any[], TDict = any> = {
|
|
247
|
-
/**
|
|
248
|
-
* The arguments passed to the RPC or subscription.
|
|
249
|
-
*/
|
|
250
|
-
argsList: TList;
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* The keyword arguments passed to the RPC or subscription.
|
|
254
|
-
*/
|
|
255
|
-
argsDict: TDict;
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* The WAMP details object.
|
|
259
|
-
*/
|
|
260
|
-
details: {
|
|
261
|
-
receive_progress?: boolean;
|
|
262
|
-
trustlevel?: number;
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* The result handler for the RPC. Use this to send progressive results.
|
|
267
|
-
* @param value
|
|
268
|
-
*/
|
|
269
|
-
result_handler: (value: any) => void;
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* The error handler for the RPC when working with progressive results.
|
|
273
|
-
* @param error The thrown error.
|
|
274
|
-
*/
|
|
275
|
-
error_handler: (error: any) => void;
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Data sent to a WAMP subscription.
|
|
280
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
281
|
-
*/
|
|
282
|
-
export type WampSubscriptionHandlerArgs = Omit<
|
|
283
|
-
WampRegistrationHandlerArgs,
|
|
284
|
-
'result_handler' | 'error_handler'
|
|
285
|
-
>;
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* The WAMP RPC handler function.
|
|
289
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
290
|
-
* @param data The data sent to the handler.
|
|
291
|
-
*/
|
|
292
|
-
export type WampRegistrationHandler = <T = any>(data: WampRegistrationHandlerArgs) => T;
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* The WAMP subscription handler function.
|
|
296
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
297
|
-
* @param data The data sent to the handler.
|
|
298
|
-
*/
|
|
299
|
-
export type WampSubscriptionHandler = <T = any>(data: WampSubscriptionHandlerArgs) => T;
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* Metadata for a WAMP RPC.
|
|
303
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
304
|
-
*/
|
|
305
|
-
type WampRegistration = {
|
|
306
|
-
/**
|
|
307
|
-
* The topic to register the RPC for.
|
|
308
|
-
*/
|
|
309
|
-
topic: string;
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* The registration options for the RPC.
|
|
313
|
-
*/
|
|
314
|
-
options?: WampRegistrationOptions;
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* The RPC handler function.
|
|
318
|
-
*/
|
|
319
|
-
handler: WampRegistrationHandler;
|
|
320
|
-
};
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
* Metadata for a WAMP subscription.
|
|
324
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
325
|
-
*/
|
|
326
|
-
type WampSubscription = {
|
|
327
|
-
/**
|
|
328
|
-
* The topic to subscribe to.
|
|
329
|
-
*/
|
|
330
|
-
topic: string;
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* The subscription options for the subscription.
|
|
334
|
-
*/
|
|
335
|
-
options?: WampSubscriptionOptions;
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* The subscription handler function.
|
|
339
|
-
*/
|
|
340
|
-
handler: WampSubscriptionHandler;
|
|
341
|
-
};
|
|
342
|
-
|
|
343
|
-
export namespace Wamp {
|
|
344
|
-
/**
|
|
345
|
-
* Creates a service name for a WAMP connection.
|
|
346
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
347
|
-
* @param name The name of the connection.
|
|
348
|
-
* @returns A service name for the connection.
|
|
349
|
-
*/
|
|
350
|
-
const getConnectionName = (name: string) => {
|
|
351
|
-
return `wamp.connection.${name}`;
|
|
352
|
-
};
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* Retrieves the client for a WAMP connection.
|
|
356
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
357
|
-
* @param name The name of the connection.
|
|
358
|
-
* @returns The Wampy client for the connection with the given name.
|
|
359
|
-
*/
|
|
360
|
-
export const getConnection = (name: string) => {
|
|
361
|
-
if (!Service.exists(getConnectionName(name))) {
|
|
362
|
-
// TODO: Use logger service here
|
|
363
|
-
console.error(
|
|
364
|
-
`No WAMP connection with name ${name} found. Please make sure to register the connection before using it.`
|
|
365
|
-
);
|
|
366
|
-
process.exit(1);
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
return Service.get<WampClient>(getConnectionName(name))!;
|
|
370
|
-
};
|
|
371
|
-
|
|
372
|
-
/**
|
|
373
|
-
* Creates and registers a new WAMP client.
|
|
374
|
-
*
|
|
375
|
-
* This will make the registered connection available for dependency injection with
|
|
376
|
-
* the key `wamp.connection.{name}`, where `{name}` is replaced with the given name.
|
|
377
|
-
*
|
|
378
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
379
|
-
*
|
|
380
|
-
* @param name The name of the connection.
|
|
381
|
-
* @param config The configuration for the WAMP client.
|
|
382
|
-
* @returns The newly created and configured WAMP client for the given connection.
|
|
383
|
-
*/
|
|
384
|
-
export const registerConnection = (name: string, config: WampClientProps) => {
|
|
385
|
-
if (Service.exists(getConnectionName(name))) {
|
|
386
|
-
// TODO: Use logger service here
|
|
387
|
-
console.error(
|
|
388
|
-
`A WAMP connection with name ${name} already exists. Please ensure to register a new connection before overwriting it.`
|
|
389
|
-
);
|
|
390
|
-
process.exit(1);
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
const client = new Wampy(config.url, {
|
|
394
|
-
ws: WampWebsocket,
|
|
395
|
-
...omit(config, ['url']),
|
|
396
|
-
onError() {
|
|
397
|
-
config.onError?.();
|
|
398
|
-
// TODO: Add more details like controller name and url
|
|
399
|
-
Event.emit('elysium:error', new Error(`An error occurred in WAMP connection ${name}`));
|
|
400
|
-
}
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
const maxRetries = config.maxRetries ?? 5;
|
|
404
|
-
const retryInterval = config.retryInterval ?? 5000;
|
|
405
|
-
let retryCount = 0;
|
|
406
|
-
|
|
407
|
-
const tryConnect = async () => {
|
|
408
|
-
try {
|
|
409
|
-
await client.connect();
|
|
410
|
-
} catch (e) {
|
|
411
|
-
console.error(
|
|
412
|
-
`Failed to connect to WAMP server (attempt ${retryCount + 1}/${maxRetries}):`,
|
|
413
|
-
e
|
|
414
|
-
);
|
|
415
|
-
|
|
416
|
-
if (retryCount < maxRetries) {
|
|
417
|
-
retryCount++;
|
|
418
|
-
setTimeout(tryConnect, retryInterval);
|
|
419
|
-
} else {
|
|
420
|
-
console.error(`Max retry attempts (${maxRetries}) reached. Giving up.`);
|
|
421
|
-
Event.emit(
|
|
422
|
-
'elysium:error',
|
|
423
|
-
new Error(`Failed to establish WAMP connection ${name} after ${maxRetries} attempts`)
|
|
424
|
-
);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
};
|
|
428
|
-
|
|
429
|
-
tryConnect();
|
|
430
|
-
|
|
431
|
-
return Service.instance<WampClient>(getConnectionName(name), client);
|
|
432
|
-
};
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* Checks if a WAMP connection with the given name exists.
|
|
436
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
437
|
-
* @param name The name of the connection.
|
|
438
|
-
* @returns `true` if the connection exists, `false` otherwise.
|
|
439
|
-
*/
|
|
440
|
-
export const connectionExists = (name: string) => {
|
|
441
|
-
return Service.exists(getConnectionName(name));
|
|
442
|
-
};
|
|
443
|
-
|
|
444
|
-
/**
|
|
445
|
-
* Retrieves the default WAMP connection.
|
|
446
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
447
|
-
* @returns The default WAMP connection.
|
|
448
|
-
*/
|
|
449
|
-
export const getDefaultConnection = () => {
|
|
450
|
-
return getConnection('default');
|
|
451
|
-
};
|
|
452
|
-
|
|
453
|
-
/**
|
|
454
|
-
* Sets the default WAMP connection.
|
|
455
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
456
|
-
* @param name The name of the connection to set as default.
|
|
457
|
-
* @returns The default WAMP connection.
|
|
458
|
-
*/
|
|
459
|
-
export const setDefaultConnection = (name: string) => {
|
|
460
|
-
const serviceName = getConnectionName('default');
|
|
461
|
-
Service.remove(serviceName);
|
|
462
|
-
return Service.instance<WampClient>(serviceName, getConnection(name));
|
|
463
|
-
};
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
* Marks a class as a WAMP controller.
|
|
467
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
468
|
-
* @param options The decorator options.
|
|
469
|
-
*/
|
|
470
|
-
export const controller = (options: WampProps) => {
|
|
471
|
-
return function (target: Class<any>) {
|
|
472
|
-
async function handleWamp(): Promise<Elysia> {
|
|
473
|
-
// TODO: Use the logger service here
|
|
474
|
-
console.log(
|
|
475
|
-
`Registering Wamp controller for connection ${options.connection} using ${target.name}`
|
|
476
|
-
);
|
|
477
|
-
await nextTick();
|
|
478
|
-
|
|
479
|
-
const controller = Service.make(target);
|
|
480
|
-
|
|
481
|
-
const metadata = Reflect.getMetadata(Symbols.wamp, target) ?? {};
|
|
482
|
-
|
|
483
|
-
const w = Wamp.getConnection(options.connection ?? 'default');
|
|
484
|
-
|
|
485
|
-
const app = new Elysia();
|
|
486
|
-
|
|
487
|
-
app.onStart(async (_) => {
|
|
488
|
-
const registrations: WampRegistration[] = metadata.registrations ?? [];
|
|
489
|
-
const subscriptions: WampSubscription[] = metadata.subscriptions ?? [];
|
|
490
|
-
|
|
491
|
-
for (const registration of registrations) {
|
|
492
|
-
w.register(
|
|
493
|
-
registration.topic,
|
|
494
|
-
registration.handler.bind(controller),
|
|
495
|
-
registration.options
|
|
496
|
-
).then((r: { topic: string; requestId: string; registrationId: string }) => {
|
|
497
|
-
// TODO: Use the logger service here
|
|
498
|
-
console.log(`Registered ${registration.topic} with id ${r.registrationId}`);
|
|
499
|
-
});
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
for (const subscription of subscriptions) {
|
|
503
|
-
w.subscribe(
|
|
504
|
-
subscription.topic,
|
|
505
|
-
subscription.handler.bind(controller),
|
|
506
|
-
subscription.options
|
|
507
|
-
).then(
|
|
508
|
-
(s: {
|
|
509
|
-
topic: string;
|
|
510
|
-
requestId: string;
|
|
511
|
-
subscriptionId: string;
|
|
512
|
-
subscriptionKey: string;
|
|
513
|
-
}) => {
|
|
514
|
-
// TODO: Use the logger service here
|
|
515
|
-
console.log(`Subscribed ${subscription.topic} with id ${s.subscriptionId}`);
|
|
516
|
-
}
|
|
517
|
-
);
|
|
518
|
-
}
|
|
519
|
-
});
|
|
520
|
-
|
|
521
|
-
return app;
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
Reflect.defineMetadata(Symbols.elysiaPlugin, handleWamp, target);
|
|
525
|
-
};
|
|
526
|
-
};
|
|
527
|
-
|
|
528
|
-
/**
|
|
529
|
-
* Registers a method as a WAMP RPC.
|
|
530
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
531
|
-
* @param topic The RPC topic to register.
|
|
532
|
-
* @param options The registration options.
|
|
533
|
-
*/
|
|
534
|
-
export const register = (topic: string, options?: WampRegistrationOptions): MethodDecorator => {
|
|
535
|
-
return function (target, _propertyKey, descriptor) {
|
|
536
|
-
const metadata = Reflect.getMetadata(Symbols.wamp, target.constructor) ?? {};
|
|
537
|
-
|
|
538
|
-
metadata.registrations ??= [];
|
|
539
|
-
metadata.registrations.push({
|
|
540
|
-
topic,
|
|
541
|
-
options,
|
|
542
|
-
handler: descriptor.value as WampRegistrationHandler
|
|
543
|
-
});
|
|
544
|
-
|
|
545
|
-
Reflect.defineMetadata(Symbols.wamp, metadata, target.constructor);
|
|
546
|
-
};
|
|
547
|
-
};
|
|
548
|
-
|
|
549
|
-
/**
|
|
550
|
-
* Subscribes to a WAMP topic.
|
|
551
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
552
|
-
* @param topic The topic to subscribe to.
|
|
553
|
-
* @param options The subscription options.
|
|
554
|
-
*/
|
|
555
|
-
export const subscribe = (topic: string, options?: WampSubscriptionOptions): MethodDecorator => {
|
|
556
|
-
return function (target, _propertyKey, descriptor) {
|
|
557
|
-
const metadata = Reflect.getMetadata(Symbols.wamp, target.constructor) ?? {};
|
|
558
|
-
|
|
559
|
-
metadata.subscriptions ??= [];
|
|
560
|
-
metadata.subscriptions.push({
|
|
561
|
-
topic,
|
|
562
|
-
options,
|
|
563
|
-
handler: descriptor.value as WampRegistrationHandler
|
|
564
|
-
});
|
|
565
|
-
|
|
566
|
-
Reflect.defineMetadata(Symbols.wamp, metadata, target.constructor);
|
|
567
|
-
};
|
|
568
|
-
};
|
|
569
|
-
}
|