@elysiumjs/core 0.1.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/.prettierignore +5 -0
- package/.prettierrc +25 -0
- package/README.md +112 -0
- package/bun.lock +262 -0
- package/index.ts +50 -0
- package/package.json +49 -0
- package/src/app.ts +503 -0
- package/src/cache.ts +218 -0
- package/src/command.ts +908 -0
- package/src/console.ts +487 -0
- package/src/database.ts +130 -0
- package/src/event.ts +225 -0
- package/src/http.ts +669 -0
- package/src/job.ts +196 -0
- package/src/middleware.ts +122 -0
- package/src/model.ts +312 -0
- package/src/module.ts +98 -0
- package/src/redis.ts +407 -0
- package/src/repository.ts +261 -0
- package/src/service.ts +349 -0
- package/src/utils.ts +41 -0
- package/src/wamp.ts +388 -0
- package/src/websocket.ts +186 -0
- package/src/worker.ts +842 -0
package/src/event.ts
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
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 { EventEmitter } from 'node:events';
|
|
16
|
+
|
|
17
|
+
import { isString } from 'radash';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Internal event bus used by the framework.
|
|
21
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
22
|
+
*/
|
|
23
|
+
class EventBus extends EventEmitter {
|
|
24
|
+
static readonly instance = new EventBus();
|
|
25
|
+
|
|
26
|
+
private constructor() {
|
|
27
|
+
super();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public emitError(error: unknown) {
|
|
31
|
+
this.emit('elysium:error', { data: error, source: null });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public emitEvent(event: string, data: unknown, source: unknown = null) {
|
|
35
|
+
this.emit(event, { data, source });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Stores the event data with the source.
|
|
41
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
42
|
+
*/
|
|
43
|
+
export type EventData<TData, TSource = null> = {
|
|
44
|
+
/**
|
|
45
|
+
* The emitted data.
|
|
46
|
+
*/
|
|
47
|
+
data: TData;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The source of the event.
|
|
51
|
+
*/
|
|
52
|
+
source: TSource | null;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Event handler function.
|
|
57
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
58
|
+
*/
|
|
59
|
+
export type EventHandler<TData, TSource = any> = (
|
|
60
|
+
event: EventData<TData, TSource>
|
|
61
|
+
) => void | Promise<void>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Properties required when declaring a method as an event listener.
|
|
65
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
66
|
+
*/
|
|
67
|
+
export type ListenProps = {
|
|
68
|
+
/**
|
|
69
|
+
* The listener mode.
|
|
70
|
+
*
|
|
71
|
+
* Set it to `on` to listen for the event every time it is emitted.
|
|
72
|
+
* Set it to `once` to listen for the event only once.
|
|
73
|
+
*/
|
|
74
|
+
mode: 'on' | 'once';
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The event name.
|
|
78
|
+
*/
|
|
79
|
+
event: string;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Whether to prepend the listener to the event. Defaults to `false`.
|
|
83
|
+
* @default false
|
|
84
|
+
*/
|
|
85
|
+
prepend?: boolean;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export namespace Event {
|
|
89
|
+
/**
|
|
90
|
+
* Emits an event with the provided data and source.
|
|
91
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
92
|
+
* @param event The event name.
|
|
93
|
+
* @param data The emitted data.
|
|
94
|
+
* @param source The source of the event. Defaults to `null`.
|
|
95
|
+
*/
|
|
96
|
+
export const emit = <TData, TSource = any>(
|
|
97
|
+
event: string,
|
|
98
|
+
data: TData,
|
|
99
|
+
source: TSource | null = null
|
|
100
|
+
): void => {
|
|
101
|
+
EventBus.instance.emitEvent(event, data, source);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Marks a method as an event listener.
|
|
106
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
107
|
+
* @param options The decorator options.
|
|
108
|
+
*/
|
|
109
|
+
export function on(options: Omit<ListenProps, 'mode'>): MethodDecorator;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Registers an event listener for the specified event.
|
|
113
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
114
|
+
* @param event The event name.
|
|
115
|
+
* @param handler The event handler function.
|
|
116
|
+
*/
|
|
117
|
+
export function on<TData, TSource = any>(
|
|
118
|
+
event: string,
|
|
119
|
+
handler: EventHandler<TData, TSource>
|
|
120
|
+
): void;
|
|
121
|
+
|
|
122
|
+
export function on<TData, TSource = any>(
|
|
123
|
+
event: string | Omit<ListenProps, 'mode'>,
|
|
124
|
+
handler?: EventHandler<TData, TSource> | never
|
|
125
|
+
): MethodDecorator | void {
|
|
126
|
+
if (isString(event)) {
|
|
127
|
+
EventBus.instance.on(event, handler!);
|
|
128
|
+
} else {
|
|
129
|
+
return function (_target, _propertyKey, descriptor) {
|
|
130
|
+
const listener = async function (eventData: EventData<any>) {
|
|
131
|
+
try {
|
|
132
|
+
await (descriptor.value as EventHandler<any>)(eventData);
|
|
133
|
+
} catch (e: unknown) {
|
|
134
|
+
EventBus.instance.emitError(e);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const prepend = event.prepend ?? false;
|
|
139
|
+
if (prepend) {
|
|
140
|
+
EventBus.instance.prependListener(event.event, listener);
|
|
141
|
+
} else {
|
|
142
|
+
EventBus.instance.on(event.event, listener);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Marks a method as an event listener that listens for the event only once.
|
|
150
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
151
|
+
* @param options The decorator options.
|
|
152
|
+
*/
|
|
153
|
+
export function once(options: Omit<ListenProps, 'mode'>): MethodDecorator;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Registers an event listener for the specified event that listens for the event only once.
|
|
157
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
158
|
+
* @param event The event name.
|
|
159
|
+
* @param handler The event handler function.
|
|
160
|
+
*/
|
|
161
|
+
export function once<TData, TSource = any>(
|
|
162
|
+
event: string,
|
|
163
|
+
handler: EventHandler<TData, TSource>
|
|
164
|
+
): void;
|
|
165
|
+
|
|
166
|
+
export function once<TData, TSource = any>(
|
|
167
|
+
event: string | Omit<ListenProps, 'mode'>,
|
|
168
|
+
handler?: EventHandler<TData, TSource> | never
|
|
169
|
+
): MethodDecorator | void {
|
|
170
|
+
if (isString(event)) {
|
|
171
|
+
EventBus.instance.once(event, handler!);
|
|
172
|
+
} else {
|
|
173
|
+
return function (_target, _propertyKey, descriptor) {
|
|
174
|
+
const listener = async function (eventData: EventData<any>) {
|
|
175
|
+
try {
|
|
176
|
+
await (descriptor.value as EventHandler<any>)(eventData);
|
|
177
|
+
} catch (e: unknown) {
|
|
178
|
+
EventBus.instance.emitError(e);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const prepend = event.prepend ?? false;
|
|
183
|
+
if (prepend) {
|
|
184
|
+
EventBus.instance.prependOnceListener(event.event, listener);
|
|
185
|
+
} else {
|
|
186
|
+
EventBus.instance.once(event.event, listener);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Removes all event listeners for the specified event.
|
|
194
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
195
|
+
* @param event The event name.
|
|
196
|
+
*/
|
|
197
|
+
export const off = (event: string): void => {
|
|
198
|
+
EventBus.instance.removeAllListeners(event);
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Removes all event listeners.
|
|
203
|
+
* @author Axel Nana <axel.nana@workbud.com>
|
|
204
|
+
*/
|
|
205
|
+
export const clear = () => {
|
|
206
|
+
EventBus.instance.removeAllListeners();
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Marks a method as an event listener.
|
|
211
|
+
* @param options The decorator options.
|
|
212
|
+
*/
|
|
213
|
+
export const listen = (options: ListenProps): MethodDecorator => {
|
|
214
|
+
if (options.mode === 'on') {
|
|
215
|
+
return on(options);
|
|
216
|
+
} else if (options.mode === 'once') {
|
|
217
|
+
return once(options);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return function (_target, _propertyKey, _descriptor) {
|
|
221
|
+
// TODO: Use logger service
|
|
222
|
+
console.error("Unknown mode provided to @listen. Use either 'on' or 'once'.");
|
|
223
|
+
};
|
|
224
|
+
};
|
|
225
|
+
}
|