@avleon/core 0.0.28 → 0.0.29
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/dist/application.js +1 -1
- package/dist/cache.d.ts +1 -1
- package/dist/cache.js +2 -2
- package/dist/collection.d.ts +25 -32
- package/dist/collection.js +50 -6
- package/dist/collection.test.d.ts +1 -0
- package/dist/collection.test.js +59 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.js +30 -5
- package/dist/config.test.d.ts +1 -0
- package/dist/config.test.js +40 -0
- package/dist/controller.js +2 -2
- package/dist/environment-variables.js +42 -5
- package/dist/event-dispatcher.d.ts +23 -0
- package/dist/event-dispatcher.js +102 -0
- package/dist/event-subscriber.d.ts +15 -0
- package/dist/event-subscriber.js +96 -0
- package/dist/exceptions/http-exceptions.js +1 -1
- package/dist/exceptions/index.d.ts +1 -1
- package/dist/exceptions/system-exception.js +3 -1
- package/dist/file-storage.js +1 -1
- package/dist/helpers.js +1 -1
- package/dist/icore.d.ts +5 -0
- package/dist/icore.js +53 -18
- package/dist/index.d.ts +2 -0
- package/dist/index.js +6 -1
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/optional-require.js +2 -2
- package/dist/validation.d.ts +1 -1
- package/dist/websocket.d.ts +7 -0
- package/dist/websocket.js +20 -0
- package/dist/websocket.test.d.ts +0 -0
- package/dist/websocket.test.js +1 -0
- package/package.json +3 -1
- package/src/config.test.ts +2 -2
- package/src/config.ts +2 -2
- package/src/event-dispatcher.ts +100 -0
- package/src/event-subscriber.ts +79 -0
- package/src/icore.ts +1106 -1060
- package/src/index.ts +3 -1
- package/src/middleware.ts +6 -4
- package/src/websocket.ts +47 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Container, Service } from "typedi";
|
|
2
|
+
import { Socket, Server } from "socket.io";
|
|
3
|
+
import { SocketContextService } from "./event-dispatcher";
|
|
4
|
+
import "reflect-metadata";
|
|
5
|
+
|
|
6
|
+
const PRIVATE_META_KEY = "avleon:private";
|
|
7
|
+
|
|
8
|
+
export function Private(channelResolver?: (socket: any) => string) {
|
|
9
|
+
return function (target: any, propertyKey: string) {
|
|
10
|
+
Reflect.defineMetadata(PRIVATE_META_KEY, true, target, propertyKey);
|
|
11
|
+
Reflect.defineMetadata(`private:channel:${propertyKey}`, channelResolver, target);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function isPrivate(target: any, propertyKey: string): boolean {
|
|
16
|
+
return Reflect.getMetadata(PRIVATE_META_KEY, target, propertyKey) || false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getPrivateChannelResolver(target: any, propertyKey: string): ((socket: any) => string) | undefined {
|
|
20
|
+
return Reflect.getMetadata(`private:channel:${propertyKey}`, target);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const socketSubscriberClasses = new Set<Function>();
|
|
24
|
+
|
|
25
|
+
export function registerSocketSubscriber(target: Function) {
|
|
26
|
+
socketSubscriberClasses.add(target);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function getSocketSubscribers(): Function[] {
|
|
30
|
+
return Array.from(socketSubscriberClasses);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
export function Subscribe(event: string): MethodDecorator {
|
|
35
|
+
return (target, propertyKey) => {
|
|
36
|
+
Reflect.defineMetadata("socket:event", event, target, propertyKey);
|
|
37
|
+
registerSocketSubscriber(target.constructor);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@Service()
|
|
44
|
+
export class EventSubscriberRegistry {
|
|
45
|
+
constructor(private readonly socketContext: SocketContextService) { }
|
|
46
|
+
|
|
47
|
+
register(socket: Socket) {
|
|
48
|
+
const subscriberClasses = getSocketSubscribers();
|
|
49
|
+
|
|
50
|
+
for (const SubscriberClass of subscriberClasses) {
|
|
51
|
+
const instance: any = Container.get(SubscriberClass);
|
|
52
|
+
const prototype = Object.getPrototypeOf(instance);
|
|
53
|
+
|
|
54
|
+
const methodNames = Object.getOwnPropertyNames(prototype)
|
|
55
|
+
.filter(name => typeof prototype[name] === "function");
|
|
56
|
+
|
|
57
|
+
for (const methodName of methodNames) {
|
|
58
|
+
const event = Reflect.getMetadata("socket:event", prototype, methodName);
|
|
59
|
+
const isPrivateListener = isPrivate(instance, methodName);
|
|
60
|
+
const channelResolver = getPrivateChannelResolver(instance, methodName);
|
|
61
|
+
|
|
62
|
+
if (event) {
|
|
63
|
+
const channel = isPrivateListener && channelResolver ? channelResolver(socket) : event;
|
|
64
|
+
console.log("Channel", channel);
|
|
65
|
+
socket.on(channel, (payload: any) => {
|
|
66
|
+
this.socketContext.run(socket, async () => {
|
|
67
|
+
if (isPrivateListener) {
|
|
68
|
+
const user = socket.data.user;
|
|
69
|
+
if (!user) return; // unauthorized
|
|
70
|
+
// optionally add more validation here
|
|
71
|
+
}
|
|
72
|
+
await instance[methodName](payload, socket.data);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|