@elysiumjs/core 0.4.0 → 0.4.1
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/package.json +5 -5
- package/src/app.ts +31 -1
- package/src/database.ts +0 -1
- package/src/http.ts +3 -3
- package/src/module.ts +10 -4
- package/src/redis.ts +4 -3
- package/src/wamp.ts +294 -147
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elysiumjs/core",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "A super-charged web framework for Bun, built on top of ElysiaJS.",
|
|
6
6
|
"private": false,
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
"module": "index.ts",
|
|
27
27
|
"type": "module",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@elysiajs/swagger": "^1.
|
|
30
|
-
"cache-manager": "^6.4.
|
|
31
|
-
"cacheable": "^1.
|
|
29
|
+
"@elysiajs/swagger": "^1.3.0",
|
|
30
|
+
"cache-manager": "^6.4.3",
|
|
31
|
+
"cacheable": "^1.9.0",
|
|
32
32
|
"drizzle-orm": "0.43.1-4aa795a",
|
|
33
33
|
"elysia": "latest",
|
|
34
34
|
"radash": "^12.1.0",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@ianvs/prettier-plugin-sort-imports": "^4.4.1",
|
|
40
|
-
"type-fest": "^4.
|
|
40
|
+
"type-fest": "^4.41.0",
|
|
41
41
|
"@types/bun": "latest"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
package/src/app.ts
CHANGED
|
@@ -19,6 +19,7 @@ import type { DatabaseConnectionProps } from './database';
|
|
|
19
19
|
import type { Route } from './http';
|
|
20
20
|
import type { ModuleClass } from './module';
|
|
21
21
|
import type { RedisConnectionProps } from './redis';
|
|
22
|
+
import type { WampClientProps } from './wamp';
|
|
22
23
|
|
|
23
24
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
24
25
|
import { parseArgs } from 'node:util';
|
|
@@ -35,6 +36,7 @@ import { applyMiddlewares } from './middleware';
|
|
|
35
36
|
import { Redis } from './redis';
|
|
36
37
|
import { Service } from './service';
|
|
37
38
|
import { deepMerge, Symbols } from './utils';
|
|
39
|
+
import { Wamp } from './wamp';
|
|
38
40
|
|
|
39
41
|
/**
|
|
40
42
|
* An application plugin, consisting of a function that returns an Elysia instance.
|
|
@@ -65,6 +67,21 @@ export type AppProps = {
|
|
|
65
67
|
port?: number;
|
|
66
68
|
};
|
|
67
69
|
|
|
70
|
+
/**
|
|
71
|
+
* The wamp client configuration for the app.
|
|
72
|
+
*/
|
|
73
|
+
wamp?: {
|
|
74
|
+
/**
|
|
75
|
+
* The default connection name.
|
|
76
|
+
*/
|
|
77
|
+
default: string;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The list of connections.
|
|
81
|
+
*/
|
|
82
|
+
connections: Record<string, WampClientProps>;
|
|
83
|
+
};
|
|
84
|
+
|
|
68
85
|
/**
|
|
69
86
|
* The list of modules provided the app.
|
|
70
87
|
*/
|
|
@@ -217,7 +234,10 @@ export abstract class Application extends InteractsWithConsole {
|
|
|
217
234
|
|
|
218
235
|
Service.instance('elysium.app', this);
|
|
219
236
|
|
|
220
|
-
const { database, redis } = Reflect.getMetadata(
|
|
237
|
+
const { database, redis, wamp } = Reflect.getMetadata(
|
|
238
|
+
Symbols.app,
|
|
239
|
+
this.constructor
|
|
240
|
+
) as AppProps;
|
|
221
241
|
|
|
222
242
|
if (redis) {
|
|
223
243
|
for (const connectionName in redis.connections) {
|
|
@@ -239,6 +259,16 @@ export abstract class Application extends InteractsWithConsole {
|
|
|
239
259
|
}
|
|
240
260
|
}
|
|
241
261
|
|
|
262
|
+
if (wamp) {
|
|
263
|
+
for (const connectionName in wamp.connections) {
|
|
264
|
+
Wamp.registerConnection(connectionName, wamp.connections[connectionName]);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (Wamp.connectionExists(wamp.default)) {
|
|
268
|
+
Wamp.setDefaultConnection(wamp.default);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
242
272
|
// Run the application
|
|
243
273
|
this.run()
|
|
244
274
|
.then(() => Event.emit('elysium:app:launched', this, this))
|
package/src/database.ts
CHANGED
|
@@ -119,7 +119,6 @@ export class DatabaseCache extends DrizzleCache {
|
|
|
119
119
|
isTag: boolean,
|
|
120
120
|
config?: CacheConfig
|
|
121
121
|
): Promise<void> {
|
|
122
|
-
console.log({ key, response, tables, isTag, config });
|
|
123
122
|
await this.#cache.set(key, response, (config?.ex ?? this.globalTtl) * 1000);
|
|
124
123
|
for (const table of tables) {
|
|
125
124
|
const keys = this.usedTablesPerKey[table];
|
package/src/http.ts
CHANGED
|
@@ -334,7 +334,7 @@ export namespace Http {
|
|
|
334
334
|
return function (target: Class<any>) {
|
|
335
335
|
async function handleHttp(): Promise<ElysiaApp> {
|
|
336
336
|
// TODO: Use the logger service here
|
|
337
|
-
console.log(`Registering HTTP
|
|
337
|
+
console.log(`Registering HTTP controller for ${props.path} using ${target.name}`);
|
|
338
338
|
await nextTick();
|
|
339
339
|
|
|
340
340
|
props = assign({ path: '/', scope: HttpControllerScope.SERVER, tags: [] }, props);
|
|
@@ -480,7 +480,7 @@ export namespace Http {
|
|
|
480
480
|
return await handler(...(await getParameters(c)));
|
|
481
481
|
}
|
|
482
482
|
|
|
483
|
-
throw c.
|
|
483
|
+
throw c.status(400, 'Invalid request body');
|
|
484
484
|
};
|
|
485
485
|
}
|
|
486
486
|
|
|
@@ -503,7 +503,7 @@ export namespace Http {
|
|
|
503
503
|
config: {},
|
|
504
504
|
async transform(c) {
|
|
505
505
|
if (isValidator && !(await route.body!.validator!.authorize(c as Context))) {
|
|
506
|
-
throw c.
|
|
506
|
+
throw c.status(401, 'Unauthorized');
|
|
507
507
|
}
|
|
508
508
|
|
|
509
509
|
return executeMiddlewareChain(mi, c, 'onTransform');
|
package/src/module.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
|
|
15
15
|
import type { Class } from 'type-fest';
|
|
16
|
-
import type { Route } from './http';
|
|
16
|
+
import type { ElysiaApp, Route } from './http';
|
|
17
17
|
|
|
18
18
|
import { Elysia } from 'elysia';
|
|
19
19
|
import { assign } from 'radash';
|
|
@@ -30,6 +30,12 @@ export type ModuleProps = {
|
|
|
30
30
|
* The list of controllers provided by the module.
|
|
31
31
|
*/
|
|
32
32
|
controllers?: Class<any>[];
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The base path for all the controllers registered by this module.
|
|
36
|
+
* This value will be prefixed to each controllers path.
|
|
37
|
+
*/
|
|
38
|
+
prefix?: Route;
|
|
33
39
|
};
|
|
34
40
|
|
|
35
41
|
/**
|
|
@@ -52,14 +58,14 @@ export abstract class Module {
|
|
|
52
58
|
*/
|
|
53
59
|
public static register(options: ModuleProps = {}) {
|
|
54
60
|
return function (target: ModuleClass) {
|
|
55
|
-
async function handleModule(m: Module): Promise<
|
|
61
|
+
async function handleModule(m: Module): Promise<ElysiaApp> {
|
|
56
62
|
// TODO: Use the logger service here
|
|
57
63
|
console.log(`Registering module ${target.name}`);
|
|
58
64
|
await nextTick();
|
|
59
65
|
|
|
60
66
|
const props = assign({ controllers: [] }, options) as Required<ModuleProps>;
|
|
61
67
|
|
|
62
|
-
const plugin:
|
|
68
|
+
const plugin: ElysiaApp = new Elysia({ name: target.name, prefix: options.prefix });
|
|
63
69
|
plugin.decorate('module', m);
|
|
64
70
|
|
|
65
71
|
const middlewares = Reflect.getMetadata(Symbols.middlewares, target) ?? [];
|
|
@@ -70,7 +76,7 @@ export abstract class Module {
|
|
|
70
76
|
if (app === undefined) {
|
|
71
77
|
// TODO: Use the logger service here
|
|
72
78
|
console.error(
|
|
73
|
-
`Invalid controller class ${controller.name} registered in module ${target.name}. Ensure that you either used the @
|
|
79
|
+
`Invalid controller class ${controller.name} registered in module ${target.name}. Ensure that you either used the @Http.controller(), the @Websocket.controller(), or the @Wamp.controller() decorators on the class.`
|
|
74
80
|
);
|
|
75
81
|
process.exit(1);
|
|
76
82
|
} else {
|
package/src/redis.ts
CHANGED
|
@@ -342,7 +342,7 @@ export namespace Redis {
|
|
|
342
342
|
if (!Service.exists(getConnectionName(name))) {
|
|
343
343
|
// TODO: Use logger service here
|
|
344
344
|
console.error(
|
|
345
|
-
`No connection with name ${name} found. Please make sure to register the connection before using it.`
|
|
345
|
+
`No Redis connection with name ${name} found. Please make sure to register the connection before using it.`
|
|
346
346
|
);
|
|
347
347
|
process.exit(1);
|
|
348
348
|
}
|
|
@@ -352,11 +352,12 @@ export namespace Redis {
|
|
|
352
352
|
|
|
353
353
|
/**
|
|
354
354
|
* Creates and registers a new redis connection.
|
|
355
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
356
355
|
*
|
|
357
356
|
* This will make the registered connection available for dependency injection with
|
|
358
357
|
* the key `redis.connection.{name}`, where `{name}` is replaced with the given name.
|
|
359
358
|
*
|
|
359
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
360
|
+
*
|
|
360
361
|
* @param name The name of the connection.
|
|
361
362
|
* @param config The configuration for the redis connection.
|
|
362
363
|
* @returns The newly created and registered redis connection.
|
|
@@ -364,7 +365,7 @@ export namespace Redis {
|
|
|
364
365
|
export const registerConnection = (name: string, config: RedisConnectionProps) => {
|
|
365
366
|
if (Service.exists(getConnectionName(name))) {
|
|
366
367
|
// TODO: Use logger service here
|
|
367
|
-
console.error(`A connection with the name ${name} has already been registered.`);
|
|
368
|
+
console.error(`A Redis connection with the name ${name} has already been registered.`);
|
|
368
369
|
process.exit(1);
|
|
369
370
|
}
|
|
370
371
|
|
package/src/wamp.ts
CHANGED
|
@@ -12,7 +12,9 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
|
|
15
|
-
import type { Class } from 'type-fest';
|
|
15
|
+
import type { Class, Primitive } from 'type-fest';
|
|
16
|
+
|
|
17
|
+
import { setInterval } from 'node:timers/promises';
|
|
16
18
|
|
|
17
19
|
import { Elysia } from 'elysia';
|
|
18
20
|
import { omit } from 'radash';
|
|
@@ -66,6 +68,18 @@ export type WampSubscriptionOptions = Omit<WampRegistrationOptions, 'invoke'>;
|
|
|
66
68
|
* @author Axel Nana <axel.nana@workbud.com>
|
|
67
69
|
*/
|
|
68
70
|
export type WampProps = {
|
|
71
|
+
/**
|
|
72
|
+
* The name of the connection to use. This connection must be registered in the application properties.
|
|
73
|
+
* @default 'default'
|
|
74
|
+
*/
|
|
75
|
+
connection?: string;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Properties required when declaring a WAMP client in the application properties.
|
|
80
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
81
|
+
*/
|
|
82
|
+
export type WampClientProps = {
|
|
69
83
|
/**
|
|
70
84
|
* The URL to the WAMP server.
|
|
71
85
|
*/
|
|
@@ -100,6 +114,131 @@ export type WampProps = {
|
|
|
100
114
|
* An array of supported authentication methods.
|
|
101
115
|
*/
|
|
102
116
|
authmethods?: string[];
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Callback for handling WAMP authentication challenges.
|
|
120
|
+
* @param method The authentication method.
|
|
121
|
+
* @param extra Authentication metadata.
|
|
122
|
+
*/
|
|
123
|
+
onChallenge?(method: string, extra: any): string | null;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Callback for handling the WAMP connection open event.
|
|
127
|
+
*/
|
|
128
|
+
onOpen?(): void;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Callback for handling the WAMP connection close event.
|
|
132
|
+
*/
|
|
133
|
+
onClose?(): void;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Callback for handling the WAMP connection error event.
|
|
137
|
+
*/
|
|
138
|
+
onError?(): void;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Callback for handling the WAMP connection reconnect event.
|
|
142
|
+
*/
|
|
143
|
+
onReconnect?(): void;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Callback for handling the WAMP connection reconnect success event.
|
|
147
|
+
*/
|
|
148
|
+
onReconnectSuccess?(): void;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export type WampClient = {
|
|
152
|
+
/**
|
|
153
|
+
* Disconnects from WAMP server. Clears all queues, subscriptions and calls.
|
|
154
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
155
|
+
*/
|
|
156
|
+
disconnect(): Promise<void>;
|
|
157
|
+
|
|
158
|
+
subscribe(
|
|
159
|
+
topic: string,
|
|
160
|
+
handler: WampSubscriptionHandler,
|
|
161
|
+
options?: WampSubscriptionOptions
|
|
162
|
+
): Promise<{
|
|
163
|
+
topic: string;
|
|
164
|
+
requestId: string;
|
|
165
|
+
subscriptionId: string;
|
|
166
|
+
subscriptionKey: string;
|
|
167
|
+
}>;
|
|
168
|
+
|
|
169
|
+
unsubscribe(
|
|
170
|
+
subscriptionIdOrKey: string,
|
|
171
|
+
handler?: WampSubscriptionHandler
|
|
172
|
+
): Promise<{
|
|
173
|
+
topic: string;
|
|
174
|
+
requestId: string;
|
|
175
|
+
}>;
|
|
176
|
+
|
|
177
|
+
publish(
|
|
178
|
+
topic: string,
|
|
179
|
+
params?:
|
|
180
|
+
| Primitive
|
|
181
|
+
| Array<any>
|
|
182
|
+
| Record<string, Primitive | Array<any>>
|
|
183
|
+
| { argsList: Array<any>; argsDict: Record<string, any> },
|
|
184
|
+
options?: {
|
|
185
|
+
exclude?: number | number[];
|
|
186
|
+
exclude_authid?: string | string[];
|
|
187
|
+
exclude_authrole?: string | string[];
|
|
188
|
+
eligible?: number | number[];
|
|
189
|
+
eligible_authid?: string | string[];
|
|
190
|
+
eligible_authrole?: string | string[];
|
|
191
|
+
exclude_me?: boolean;
|
|
192
|
+
disclose_me?: boolean;
|
|
193
|
+
ppt_scheme?: string;
|
|
194
|
+
ppt_serializer?: string;
|
|
195
|
+
ppt_cipher?: string;
|
|
196
|
+
ppt_keyid?: string;
|
|
197
|
+
}
|
|
198
|
+
): Promise<{
|
|
199
|
+
topic: string;
|
|
200
|
+
requestId: string;
|
|
201
|
+
publicationId: string;
|
|
202
|
+
}>;
|
|
203
|
+
|
|
204
|
+
call(
|
|
205
|
+
procedure: string,
|
|
206
|
+
params?:
|
|
207
|
+
| Primitive
|
|
208
|
+
| Array<any>
|
|
209
|
+
| Record<string, Primitive | Array<any>>
|
|
210
|
+
| { argsList: Array<any>; argsDict: Record<string, any> },
|
|
211
|
+
options?: {
|
|
212
|
+
disclose_me?: boolean;
|
|
213
|
+
progress_callback?(args: { argsList: Array<any>; argsDict: Record<string, any> }): void;
|
|
214
|
+
timeout?: number;
|
|
215
|
+
ppt_scheme?: string;
|
|
216
|
+
ppt_serializer?: string;
|
|
217
|
+
ppt_cipher?: string;
|
|
218
|
+
ppt_keyid?: string;
|
|
219
|
+
}
|
|
220
|
+
): Promise<{
|
|
221
|
+
details: Record<string, any>;
|
|
222
|
+
argsList?: Array<any>;
|
|
223
|
+
argsDict?: Record<string, any>;
|
|
224
|
+
}>;
|
|
225
|
+
|
|
226
|
+
cancel(requestId: string, options?: { mode?: 'kill' | 'killnowait' | 'skip' }): true;
|
|
227
|
+
|
|
228
|
+
register(
|
|
229
|
+
topic: string,
|
|
230
|
+
handler: WampRegistrationHandler,
|
|
231
|
+
options?: WampRegistrationOptions
|
|
232
|
+
): Promise<{
|
|
233
|
+
topic: string;
|
|
234
|
+
requestId: string;
|
|
235
|
+
registrationId: string;
|
|
236
|
+
}>;
|
|
237
|
+
|
|
238
|
+
unregister(registrationId: string): Promise<{
|
|
239
|
+
topic: string;
|
|
240
|
+
requestId: string;
|
|
241
|
+
}>;
|
|
103
242
|
};
|
|
104
243
|
|
|
105
244
|
/**
|
|
@@ -204,6 +343,127 @@ type WampSubscription = {
|
|
|
204
343
|
};
|
|
205
344
|
|
|
206
345
|
export namespace Wamp {
|
|
346
|
+
/**
|
|
347
|
+
* Creates a service name for a WAMP connection.
|
|
348
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
349
|
+
* @param name The name of the connection.
|
|
350
|
+
* @returns A service name for the connection.
|
|
351
|
+
*/
|
|
352
|
+
const getConnectionName = (name: string) => {
|
|
353
|
+
return `wamp.connection.${name}`;
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Retrieves the client for a WAMP connection.
|
|
358
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
359
|
+
* @param name The name of the connection.
|
|
360
|
+
* @returns The Wampy client for the connection with the given name.
|
|
361
|
+
*/
|
|
362
|
+
export const getConnection = (name: string) => {
|
|
363
|
+
if (!Service.exists(getConnectionName(name))) {
|
|
364
|
+
// TODO: Use logger service here
|
|
365
|
+
console.error(
|
|
366
|
+
`No WAMP connection with name ${name} found. Please make sure to register the connection before using it.`
|
|
367
|
+
);
|
|
368
|
+
process.exit(1);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return Service.get<WampClient>(getConnectionName(name))!;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Creates and registers a new WAMP client.
|
|
376
|
+
*
|
|
377
|
+
* This will make the registered connection available for dependency injection with
|
|
378
|
+
* the key `wamp.connection.{name}`, where `{name}` is replaced with the given name.
|
|
379
|
+
*
|
|
380
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
381
|
+
*
|
|
382
|
+
* @param name The name of the connection.
|
|
383
|
+
* @param config The configuration for the WAMP client.
|
|
384
|
+
* @returns The newly created and configured WAMP client for the given connection.
|
|
385
|
+
*/
|
|
386
|
+
export const registerConnection = (name: string, config: WampClientProps) => {
|
|
387
|
+
if (Service.exists(getConnectionName(name))) {
|
|
388
|
+
// TODO: Use logger service here
|
|
389
|
+
console.error(
|
|
390
|
+
`A WAMP connection with name ${name} already exists. Please ensure to register a new connection before overwriting it.`
|
|
391
|
+
);
|
|
392
|
+
process.exit(1);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const client = new Wampy(config.url, {
|
|
396
|
+
ws: WampWebsocket,
|
|
397
|
+
...omit(config, ['url']),
|
|
398
|
+
onError() {
|
|
399
|
+
config.onError?.();
|
|
400
|
+
// TODO: Add more details like controller name and url
|
|
401
|
+
Event.emit('elysium:error', new Error(`An error occurred in WAMP connection ${name}`));
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
const maxRetries = config.maxRetries ?? 5;
|
|
406
|
+
const retryInterval = config.retryInterval ?? 5000;
|
|
407
|
+
let retryCount = 0;
|
|
408
|
+
|
|
409
|
+
const tryConnect = async () => {
|
|
410
|
+
try {
|
|
411
|
+
await client.connect();
|
|
412
|
+
} catch (e) {
|
|
413
|
+
console.error(
|
|
414
|
+
`Failed to connect to WAMP server (attempt ${retryCount + 1}/${maxRetries}):`,
|
|
415
|
+
e
|
|
416
|
+
);
|
|
417
|
+
|
|
418
|
+
if (retryCount < maxRetries) {
|
|
419
|
+
retryCount++;
|
|
420
|
+
setTimeout(tryConnect, retryInterval);
|
|
421
|
+
} else {
|
|
422
|
+
console.error(`Max retry attempts (${maxRetries}) reached. Giving up.`);
|
|
423
|
+
Event.emit(
|
|
424
|
+
'elysium:error',
|
|
425
|
+
new Error(`Failed to establish WAMP connection ${name} after ${maxRetries} attempts`)
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
tryConnect();
|
|
432
|
+
|
|
433
|
+
return Service.instance<WampClient>(getConnectionName(name), client);
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Checks if a WAMP connection with the given name exists.
|
|
438
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
439
|
+
* @param name The name of the connection.
|
|
440
|
+
* @returns `true` if the connection exists, `false` otherwise.
|
|
441
|
+
*/
|
|
442
|
+
export const connectionExists = (name: string) => {
|
|
443
|
+
return Service.exists(getConnectionName(name));
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Retrieves the default WAMP connection.
|
|
448
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
449
|
+
* @returns The default WAMP connection.
|
|
450
|
+
*/
|
|
451
|
+
export const getDefaultConnection = () => {
|
|
452
|
+
return getConnection('default');
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Sets the default WAMP connection.
|
|
457
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
458
|
+
* @param name The name of the connection to set as default.
|
|
459
|
+
* @returns The default WAMP connection.
|
|
460
|
+
*/
|
|
461
|
+
export const setDefaultConnection = (name: string) => {
|
|
462
|
+
const serviceName = getConnectionName('default');
|
|
463
|
+
Service.remove(serviceName);
|
|
464
|
+
return Service.instance<WampClient>(serviceName, getConnection(name));
|
|
465
|
+
};
|
|
466
|
+
|
|
207
467
|
/**
|
|
208
468
|
* Marks a class as a WAMP controller.
|
|
209
469
|
* @author Axel Nana <axel.nana@workbud.com>
|
|
@@ -213,67 +473,50 @@ export namespace Wamp {
|
|
|
213
473
|
return function (target: Class<any>) {
|
|
214
474
|
async function handleWamp(): Promise<Elysia> {
|
|
215
475
|
// TODO: Use the logger service here
|
|
216
|
-
console.log(
|
|
476
|
+
console.log(
|
|
477
|
+
`Registering Wamp controller for connection ${options.connection} using ${target.name}`
|
|
478
|
+
);
|
|
217
479
|
await nextTick();
|
|
218
480
|
|
|
219
481
|
const controller = Service.make(target);
|
|
220
482
|
|
|
221
483
|
const metadata = Reflect.getMetadata(Symbols.wamp, target) ?? {};
|
|
222
484
|
|
|
223
|
-
const w =
|
|
224
|
-
ws: WampWebsocket,
|
|
225
|
-
...omit(options, ['url']),
|
|
226
|
-
onChallenge: metadata.challenge?.bind(controller),
|
|
227
|
-
onClose: metadata.close?.bind(controller),
|
|
228
|
-
onError() {
|
|
229
|
-
metadata.error?.call(controller);
|
|
230
|
-
// TODO: Add more details like controller name and url
|
|
231
|
-
Event.emit('elysium:error', new Error('Wamp connection error'));
|
|
232
|
-
},
|
|
233
|
-
onReconnect: metadata.reconnect?.bind(controller),
|
|
234
|
-
onReconnectSuccess: metadata.reconnectSuccess?.bind(controller)
|
|
235
|
-
});
|
|
485
|
+
const w = Wamp.getConnection(options.connection ?? 'default');
|
|
236
486
|
|
|
237
487
|
const app = new Elysia();
|
|
238
488
|
|
|
239
489
|
app.onStart(async (_) => {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
490
|
+
const registrations: WampRegistration[] = metadata.registrations ?? [];
|
|
491
|
+
const subscriptions: WampSubscription[] = metadata.subscriptions ?? [];
|
|
492
|
+
|
|
493
|
+
for (const registration of registrations) {
|
|
494
|
+
w.register(
|
|
495
|
+
registration.topic,
|
|
496
|
+
registration.handler.bind(controller),
|
|
497
|
+
registration.options
|
|
498
|
+
).then((r: { topic: string; requestId: string; registrationId: string }) => {
|
|
499
|
+
// TODO: Use the logger service here
|
|
500
|
+
console.log(`Registered ${registration.topic} with id ${r.registrationId}`);
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
for (const subscription of subscriptions) {
|
|
505
|
+
w.subscribe(
|
|
506
|
+
subscription.topic,
|
|
507
|
+
subscription.handler.bind(controller),
|
|
508
|
+
subscription.options
|
|
509
|
+
).then(
|
|
510
|
+
(s: {
|
|
511
|
+
topic: string;
|
|
512
|
+
requestId: string;
|
|
513
|
+
subscriptionId: string;
|
|
514
|
+
subscriptionKey: string;
|
|
515
|
+
}) => {
|
|
253
516
|
// TODO: Use the logger service here
|
|
254
|
-
console.log(`
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
for (const subscription of subscriptions) {
|
|
259
|
-
w.subscribe(
|
|
260
|
-
subscription.topic,
|
|
261
|
-
subscription.handler.bind(controller),
|
|
262
|
-
subscription.options
|
|
263
|
-
).then(
|
|
264
|
-
(s: {
|
|
265
|
-
topic: string;
|
|
266
|
-
requestId: string;
|
|
267
|
-
subscriptionId: string;
|
|
268
|
-
subscriptionKey: string;
|
|
269
|
-
}) => {
|
|
270
|
-
// TODO: Use the logger service here
|
|
271
|
-
console.log(`Subscribed ${subscription.topic} with id ${s.subscriptionId}`);
|
|
272
|
-
}
|
|
273
|
-
);
|
|
274
|
-
}
|
|
275
|
-
} catch (error) {
|
|
276
|
-
// noop
|
|
517
|
+
console.log(`Subscribed ${subscription.topic} with id ${s.subscriptionId}`);
|
|
518
|
+
}
|
|
519
|
+
);
|
|
277
520
|
}
|
|
278
521
|
});
|
|
279
522
|
|
|
@@ -325,100 +568,4 @@ export namespace Wamp {
|
|
|
325
568
|
Reflect.defineMetadata(Symbols.wamp, metadata, target.constructor);
|
|
326
569
|
};
|
|
327
570
|
};
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Marks a method as the WAMP "challenge" event handler.
|
|
331
|
-
*
|
|
332
|
-
* This decorator should be used on a WAMP controller method. Only one "challenge" event handler
|
|
333
|
-
* can be defined per WAMP controller.
|
|
334
|
-
*
|
|
335
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
336
|
-
*/
|
|
337
|
-
export const onChallenge = (): MethodDecorator => {
|
|
338
|
-
return function (target, _propertyKey, descriptor) {
|
|
339
|
-
const metadata = Reflect.getMetadata(Symbols.wamp, target.constructor) ?? {};
|
|
340
|
-
metadata.challenge = descriptor.value;
|
|
341
|
-
Reflect.defineMetadata(Symbols.wamp, metadata, target.constructor);
|
|
342
|
-
};
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Marks a method as the WAMP "open" event handler.
|
|
347
|
-
*
|
|
348
|
-
* This decorator should be used on a WAMP controller method. Only one "open" event handler
|
|
349
|
-
* can be defined per WAMP controller.
|
|
350
|
-
*
|
|
351
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
352
|
-
*/
|
|
353
|
-
export const onOpen = (): MethodDecorator => {
|
|
354
|
-
return function (target, _propertyKey, descriptor) {
|
|
355
|
-
const metadata = Reflect.getMetadata(Symbols.wamp, target.constructor) ?? {};
|
|
356
|
-
metadata.open = descriptor.value;
|
|
357
|
-
Reflect.defineMetadata(Symbols.wamp, metadata, target.constructor);
|
|
358
|
-
};
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* Marks a method as the WAMP "close" event handler.
|
|
363
|
-
*
|
|
364
|
-
* This decorator should be used on a WAMP controller method. Only one "close" event handler
|
|
365
|
-
* can be defined per WAMP controller.
|
|
366
|
-
*
|
|
367
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
368
|
-
*/
|
|
369
|
-
export const onClose = (): MethodDecorator => {
|
|
370
|
-
return function (target, _propertyKey, descriptor) {
|
|
371
|
-
const metadata = Reflect.getMetadata(Symbols.wamp, target.constructor) ?? {};
|
|
372
|
-
metadata.close = descriptor.value;
|
|
373
|
-
Reflect.defineMetadata(Symbols.wamp, metadata, target.constructor);
|
|
374
|
-
};
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* Marks a method as the WAMP "error" event handler.
|
|
379
|
-
*
|
|
380
|
-
* This decorator should be used on a WAMP controller method. Only one "error" event handler
|
|
381
|
-
* can be defined per WAMP controller.
|
|
382
|
-
*
|
|
383
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
384
|
-
*/
|
|
385
|
-
export const onError = (): MethodDecorator => {
|
|
386
|
-
return function (target, _propertyKey, descriptor) {
|
|
387
|
-
const metadata = Reflect.getMetadata(Symbols.wamp, target.constructor) ?? {};
|
|
388
|
-
metadata.error = descriptor.value;
|
|
389
|
-
Reflect.defineMetadata(Symbols.wamp, metadata, target.constructor);
|
|
390
|
-
};
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* Marks a method as the WAMP "reconnect" event handler.
|
|
395
|
-
*
|
|
396
|
-
* This decorator should be used on a WAMP controller method. Only one "reconnect" event handler
|
|
397
|
-
* can be defined per WAMP controller.
|
|
398
|
-
*
|
|
399
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
400
|
-
*/
|
|
401
|
-
export const onReconnect = (): MethodDecorator => {
|
|
402
|
-
return function (target, _propertyKey, descriptor) {
|
|
403
|
-
const metadata = Reflect.getMetadata(Symbols.wamp, target.constructor) ?? {};
|
|
404
|
-
metadata.reconnect = descriptor.value;
|
|
405
|
-
Reflect.defineMetadata(Symbols.wamp, metadata, target.constructor);
|
|
406
|
-
};
|
|
407
|
-
};
|
|
408
|
-
|
|
409
|
-
/**
|
|
410
|
-
* Marks a method as the WAMP "reconnectSuccess" event handler.
|
|
411
|
-
*
|
|
412
|
-
* This decorator should be used on a WAMP controller method. Only one "reconnectSuccess" event handler
|
|
413
|
-
* can be defined per WAMP controller.
|
|
414
|
-
*
|
|
415
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
416
|
-
*/
|
|
417
|
-
export const onReconnectSuccess = (): MethodDecorator => {
|
|
418
|
-
return function (target, _propertyKey, descriptor) {
|
|
419
|
-
const metadata = Reflect.getMetadata(Symbols.wamp, target.constructor) ?? {};
|
|
420
|
-
metadata.reconnectSuccess = descriptor.value;
|
|
421
|
-
Reflect.defineMetadata(Symbols.wamp, metadata, target.constructor);
|
|
422
|
-
};
|
|
423
|
-
};
|
|
424
571
|
}
|