@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/websocket.ts
DELETED
|
@@ -1,191 +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 { ServerWebSocket } from 'bun';
|
|
16
|
-
import type { Context, ErrorContext, TSchema } from 'elysia';
|
|
17
|
-
import type { Class, ConditionalPick, JsonObject, Primitive } from 'type-fest';
|
|
18
|
-
import type { Route } from './http';
|
|
19
|
-
|
|
20
|
-
import { Elysia } from 'elysia';
|
|
21
|
-
|
|
22
|
-
import { Event } from './event';
|
|
23
|
-
import { Service } from './service';
|
|
24
|
-
import { nextTick, Symbols } from './utils';
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Properties required when declaring a websocket controller using the `@websocket()` decorator.
|
|
28
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
29
|
-
*/
|
|
30
|
-
export type WebsocketProps = {
|
|
31
|
-
/**
|
|
32
|
-
* The path of the websocket route.
|
|
33
|
-
*/
|
|
34
|
-
path: Route;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* The options for the websocket server.
|
|
38
|
-
*/
|
|
39
|
-
options?: ConditionalPick<Bun.WebSocketHandler, Primitive | JsonObject>;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The websocket connection instance.
|
|
44
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
45
|
-
* @template TData Additional data stored in the websocket connection.
|
|
46
|
-
*/
|
|
47
|
-
export type WS<TData = unknown> = ServerWebSocket<
|
|
48
|
-
{
|
|
49
|
-
/**
|
|
50
|
-
* Unique identifier for the websocket connection.
|
|
51
|
-
*/
|
|
52
|
-
id: string;
|
|
53
|
-
} & Context &
|
|
54
|
-
TData
|
|
55
|
-
>;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* The data generated when an error occurs in a websocket connection.
|
|
59
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
60
|
-
*/
|
|
61
|
-
export type WSError = ErrorContext & { error: Readonly<Error> };
|
|
62
|
-
|
|
63
|
-
export namespace Websocket {
|
|
64
|
-
/**
|
|
65
|
-
* Marks a class as a websocket controller.
|
|
66
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
67
|
-
* @param props The websocket route properties.
|
|
68
|
-
*/
|
|
69
|
-
export const controller = (props: WebsocketProps) => {
|
|
70
|
-
return function (target: Class<any>) {
|
|
71
|
-
async function handleWebsocket(): Promise<Elysia> {
|
|
72
|
-
// TODO: Use the logger service here
|
|
73
|
-
console.log(`Registering Websocket route for ${props.path} using ${target.name}`);
|
|
74
|
-
await nextTick();
|
|
75
|
-
|
|
76
|
-
const controller = Service.make(target);
|
|
77
|
-
|
|
78
|
-
const metadata = Reflect.getMetadata(Symbols.websocket, target) ?? {};
|
|
79
|
-
const open = metadata.open?.bind(controller);
|
|
80
|
-
const close = metadata.close?.bind(controller);
|
|
81
|
-
const message = metadata.message?.bind(controller);
|
|
82
|
-
const drain = metadata.drain?.bind(controller);
|
|
83
|
-
const error =
|
|
84
|
-
metadata.error?.bind(controller) ??
|
|
85
|
-
((e: WSError) => Event.emit('elysium:error', e.error));
|
|
86
|
-
|
|
87
|
-
const app = new Elysia();
|
|
88
|
-
|
|
89
|
-
// TODO: Add middlewares here
|
|
90
|
-
app.ws(props.path, {
|
|
91
|
-
// TODO: beforeHandle
|
|
92
|
-
// TODO: afterHandle
|
|
93
|
-
open,
|
|
94
|
-
close,
|
|
95
|
-
message,
|
|
96
|
-
drain,
|
|
97
|
-
error,
|
|
98
|
-
body: metadata.body,
|
|
99
|
-
...props.options
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
return app;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
Reflect.defineMetadata(Symbols.elysiaPlugin, handleWebsocket, target);
|
|
106
|
-
};
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Marks a method as the websocket "open" event handler.
|
|
111
|
-
*
|
|
112
|
-
* This decorator should be used on a websocket controller method. Only one "open" event handler
|
|
113
|
-
* can be defined per websocket controller.
|
|
114
|
-
*
|
|
115
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
116
|
-
*/
|
|
117
|
-
export const onOpen = (): MethodDecorator => {
|
|
118
|
-
return function (target, propertyKey, descriptor) {
|
|
119
|
-
const metadata = Reflect.getMetadata(Symbols.websocket, target.constructor) ?? {};
|
|
120
|
-
metadata.open = descriptor.value;
|
|
121
|
-
Reflect.defineMetadata(Symbols.websocket, metadata, target.constructor);
|
|
122
|
-
};
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Marks a method as the websocket "close" event handler.
|
|
127
|
-
*
|
|
128
|
-
* This decorator should be used on a websocket controller method. Only one "close" event handler
|
|
129
|
-
* can be defined per websocket controller.
|
|
130
|
-
*
|
|
131
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
132
|
-
*/
|
|
133
|
-
export const onClose = (): MethodDecorator => {
|
|
134
|
-
return function (target, propertyKey, descriptor) {
|
|
135
|
-
const metadata = Reflect.getMetadata(Symbols.websocket, target.constructor) ?? {};
|
|
136
|
-
metadata.close = descriptor.value;
|
|
137
|
-
Reflect.defineMetadata(Symbols.websocket, metadata, target.constructor);
|
|
138
|
-
};
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Marks a method as the websocket "message" event handler.
|
|
143
|
-
*
|
|
144
|
-
* This decorator should be used on a websocket controller method. Only one "message" event handler
|
|
145
|
-
* can be defined per websocket controller.
|
|
146
|
-
*
|
|
147
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
148
|
-
*
|
|
149
|
-
* @param schema The schema of the message body.
|
|
150
|
-
*/
|
|
151
|
-
export const onMessage = (schema?: TSchema): MethodDecorator => {
|
|
152
|
-
return function (target, propertyKey, descriptor) {
|
|
153
|
-
const metadata = Reflect.getMetadata(Symbols.websocket, target.constructor) ?? {};
|
|
154
|
-
metadata.message = descriptor.value;
|
|
155
|
-
metadata.body = schema;
|
|
156
|
-
Reflect.defineMetadata(Symbols.websocket, metadata, target.constructor);
|
|
157
|
-
};
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Marks a method as the websocket "drain" event handler.
|
|
162
|
-
*
|
|
163
|
-
* This decorator should be used on a websocket controller method. Only one "drain" event handler
|
|
164
|
-
* can be defined per websocket controller.
|
|
165
|
-
*
|
|
166
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
167
|
-
*/
|
|
168
|
-
export const onDrain = (): MethodDecorator => {
|
|
169
|
-
return function (target, propertyKey, descriptor) {
|
|
170
|
-
const metadata = Reflect.getMetadata(Symbols.websocket, target.constructor) ?? {};
|
|
171
|
-
metadata.drain = descriptor.value;
|
|
172
|
-
Reflect.defineMetadata(Symbols.websocket, metadata, target.constructor);
|
|
173
|
-
};
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Marks a method as the websocket "error" event handler.
|
|
178
|
-
*
|
|
179
|
-
* This decorator should be used on a websocket controller method. Only one "error" event handler
|
|
180
|
-
* can be defined per websocket controller.
|
|
181
|
-
*
|
|
182
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
183
|
-
*/
|
|
184
|
-
export const onError = (): MethodDecorator => {
|
|
185
|
-
return function (target, propertyKey, descriptor) {
|
|
186
|
-
const metadata = Reflect.getMetadata(Symbols.websocket, target.constructor) ?? {};
|
|
187
|
-
metadata.error = descriptor.value;
|
|
188
|
-
Reflect.defineMetadata(Symbols.websocket, metadata, target.constructor);
|
|
189
|
-
};
|
|
190
|
-
};
|
|
191
|
-
}
|