@elysiumjs/core 0.7.0 → 0.8.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/dist/wamp.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- import type { Class, Primitive } from 'type-fest';
1
+ import type { Class } from 'type-fest';
2
+ import { Elysia } from 'elysia';
3
+ import Wampy from 'wampy';
2
4
  /**
3
5
  * Options used to register a WAMP RPC for invocation.
4
6
  * @author Axel Nana <axel.nana@workbud.com>
@@ -69,11 +71,11 @@ export type WampClientProps = {
69
71
  * @param method The authentication method.
70
72
  * @param extra Authentication metadata.
71
73
  */
72
- onChallenge?(method: string, extra: any): string | null;
74
+ onChallenge?(method: string, extra: Record<string, unknown>): string | Promise<string>;
73
75
  /**
74
- * Callback for handling the WAMP connection open event.
76
+ * Called once the first connection is established; not on wampy reconnects.
75
77
  */
76
- onOpen?(): void;
78
+ onOpen?(): void | Promise<void>;
77
79
  /**
78
80
  * Callback for handling the WAMP connection close event.
79
81
  */
@@ -81,7 +83,7 @@ export type WampClientProps = {
81
83
  /**
82
84
  * Callback for handling the WAMP connection error event.
83
85
  */
84
- onError?(): void;
86
+ onError?(error: Error): void;
85
87
  /**
86
88
  * Callback for handling the WAMP connection reconnect event.
87
89
  */
@@ -89,77 +91,13 @@ export type WampClientProps = {
89
91
  /**
90
92
  * Callback for handling the WAMP connection reconnect success event.
91
93
  */
92
- onReconnectSuccess?(): void;
93
- };
94
- export type WampClient = {
95
- /**
96
- * Disconnects from WAMP server. Clears all queues, subscriptions and calls.
97
- * @author Axel Nana <axel.nana@workbud.com>
98
- */
99
- disconnect(): Promise<void>;
100
- subscribe(topic: string, handler: WampSubscriptionHandler, options?: WampSubscriptionOptions): Promise<{
101
- topic: string;
102
- requestId: string;
103
- subscriptionId: string;
104
- subscriptionKey: string;
105
- }>;
106
- unsubscribe(subscriptionIdOrKey: string, handler?: WampSubscriptionHandler): Promise<{
107
- topic: string;
108
- requestId: string;
109
- }>;
110
- publish(topic: string, params?: Primitive | Array<any> | Record<string, Primitive | Array<any>> | {
111
- argsList: Array<any>;
112
- argsDict: Record<string, any>;
113
- }, options?: {
114
- exclude?: number | number[];
115
- exclude_authid?: string | string[];
116
- exclude_authrole?: string | string[];
117
- eligible?: number | number[];
118
- eligible_authid?: string | string[];
119
- eligible_authrole?: string | string[];
120
- exclude_me?: boolean;
121
- disclose_me?: boolean;
122
- ppt_scheme?: string;
123
- ppt_serializer?: string;
124
- ppt_cipher?: string;
125
- ppt_keyid?: string;
126
- }): Promise<{
127
- topic: string;
128
- requestId: string;
129
- publicationId: string;
130
- }>;
131
- call(procedure: string, params?: Primitive | Array<any> | Record<string, Primitive | Array<any>> | {
132
- argsList: Array<any>;
133
- argsDict: Record<string, any>;
134
- }, options?: {
135
- disclose_me?: boolean;
136
- progress_callback?(args: {
137
- argsList: Array<any>;
138
- argsDict: Record<string, any>;
139
- }): void;
140
- timeout?: number;
141
- ppt_scheme?: string;
142
- ppt_serializer?: string;
143
- ppt_cipher?: string;
144
- ppt_keyid?: string;
145
- }): Promise<{
146
- details: Record<string, any>;
147
- argsList?: Array<any>;
148
- argsDict?: Record<string, any>;
149
- }>;
150
- cancel(requestId: string, options?: {
151
- mode?: 'kill' | 'killnowait' | 'skip';
152
- }): true;
153
- register(topic: string, handler: WampRegistrationHandler, options?: WampRegistrationOptions): Promise<{
154
- topic: string;
155
- requestId: string;
156
- registrationId: string;
157
- }>;
158
- unregister(registrationId: string): Promise<{
159
- topic: string;
160
- requestId: string;
161
- }>;
94
+ onReconnectSuccess?(details: Record<string, unknown>): void;
162
95
  };
96
+ /**
97
+ * The WAMP client, as provided by Wampy.
98
+ * @author Axel Nana <axel.nana@workbud.com>
99
+ */
100
+ export type WampClient = Wampy;
163
101
  /**
164
102
  * Data sent to a WAMP RPC.
165
103
  * @author Axel Nana <axel.nana@workbud.com>
@@ -210,25 +148,20 @@ export type WampRegistrationHandler = <T = any>(data: WampRegistrationHandlerArg
210
148
  export type WampSubscriptionHandler = <T = any>(data: WampSubscriptionHandlerArgs) => T;
211
149
  export declare namespace Wamp {
212
150
  /**
213
- * Retrieves the client for a WAMP connection.
151
+ * Creates a service name for a WAMP connection.
214
152
  * @author Axel Nana <axel.nana@workbud.com>
215
153
  * @param name The name of the connection.
216
- * @returns The Wampy client for the connection with the given name.
154
+ * @returns A service name for the connection.
217
155
  */
218
- const getConnection: (name: string) => WampClient;
156
+ const getConnectionName: (name: string) => string;
219
157
  /**
220
- * Creates and registers a new WAMP client.
221
- *
222
- * This will make the registered connection available for dependency injection with
223
- * the key `wamp.connection.{name}`, where `{name}` is replaced with the given name.
224
- *
158
+ * Retrieves the client for a WAMP connection declared by the {@link WampModule}.
225
159
  * @author Axel Nana <axel.nana@workbud.com>
226
- *
227
160
  * @param name The name of the connection.
228
- * @param config The configuration for the WAMP client.
229
- * @returns The newly created and configured WAMP client for the given connection.
161
+ * @returns The Wampy client for the connection with the given name.
162
+ * @throws {ServiceNotFoundError} If no connection with that name is declared.
230
163
  */
231
- const registerConnection: (name: string, config: WampClientProps) => WampClient;
164
+ const getConnection: (name?: string) => Wampy;
232
165
  /**
233
166
  * Checks if a WAMP connection with the given name exists.
234
167
  * @author Axel Nana <axel.nana@workbud.com>
@@ -241,14 +174,12 @@ export declare namespace Wamp {
241
174
  * @author Axel Nana <axel.nana@workbud.com>
242
175
  * @returns The default WAMP connection.
243
176
  */
244
- const getDefaultConnection: () => WampClient;
177
+ const getDefaultConnection: () => Wampy;
245
178
  /**
246
- * Sets the default WAMP connection.
179
+ * Creates a WAMP client and starts connecting it, retrying on failure.
247
180
  * @author Axel Nana <axel.nana@workbud.com>
248
- * @param name The name of the connection to set as default.
249
- * @returns The default WAMP connection.
250
181
  */
251
- const setDefaultConnection: (name: string) => WampClient;
182
+ const createClient: (name: string, config: WampClientProps) => WampClient;
252
183
  /**
253
184
  * Marks a class as a WAMP controller.
254
185
  * @author Axel Nana <axel.nana@workbud.com>
@@ -270,3 +201,52 @@ export declare namespace Wamp {
270
201
  */
271
202
  const subscribe: (topic: string, options?: WampSubscriptionOptions) => MethodDecorator;
272
203
  }
204
+ /**
205
+ * Configuration of the {@link WampModule}.
206
+ * @author Axel Nana <axel.nana@workbud.com>
207
+ */
208
+ export type WampModuleConfig = {
209
+ /** The name of the connection that `default` resolves to. */
210
+ default: string;
211
+ /** The connections to declare, by name. */
212
+ connections: Record<string, WampClientProps>;
213
+ };
214
+ declare const WampModule_base: (abstract new () => {
215
+ get config(): WampModuleConfig;
216
+ onInit(): Promise<void> | void;
217
+ onDestroy(): Promise<void> | void;
218
+ onServe(_app: Elysia<any>): Promise<void> | void;
219
+ }) & {
220
+ get config(): string;
221
+ with(config: Partial<WampModuleConfig> | (() => Partial<WampModuleConfig> | Promise<Partial<WampModuleConfig>>)): import("./module").ConfiguredModule<WampModuleConfig>;
222
+ resolveConfig(input?: Partial<WampModuleConfig>): WampModuleConfig;
223
+ register<TConfig = any>(props?: import("./module").ModuleProps<TConfig>): (target: import("./module").ModuleClass) => void;
224
+ configurable<TConfig extends object>(options?: {
225
+ defaults?: Partial<TConfig>;
226
+ required?: (keyof TConfig)[];
227
+ }): (abstract new () => {
228
+ get config(): TConfig;
229
+ onInit(): Promise<void> | void;
230
+ onDestroy(): Promise<void> | void;
231
+ onServe(_app: Elysia<any>): Promise<void> | void;
232
+ }) & {
233
+ get config(): string;
234
+ with(config: Partial<TConfig> | (() => Partial<TConfig> | Promise<Partial<TConfig>>)): import("./module").ConfiguredModule<TConfig>;
235
+ resolveConfig(input?: Partial<TConfig>): TConfig;
236
+ register<TConfig_1 = any>(props?: import("./module").ModuleProps<TConfig_1>): (target: import("./module").ModuleClass) => void;
237
+ configurable<TConfig extends object>(options?: {
238
+ defaults?: Partial<TConfig>;
239
+ required?: (keyof TConfig)[];
240
+ }): /*elided*/ any;
241
+ };
242
+ };
243
+ /**
244
+ * Declares WAMP connections as global services named `wamp.connection.{name}`, plus
245
+ * `wamp.connection.default` aliasing the configured default. Clients are created on first use.
246
+ * @author Axel Nana <axel.nana@workbud.com>
247
+ */
248
+ export declare class WampModule extends WampModule_base {
249
+ /** Disconnects the clients that were created. */
250
+ onDestroy(): Promise<void>;
251
+ }
252
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elysiumjs/core",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
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,
@@ -13,7 +13,7 @@
13
13
  "scripts": {
14
14
  "format": "prettier --write \"./src/**/*.ts\"",
15
15
  "clean": "rimraf dist",
16
- "build:js": "bun build ./src/index.ts --outdir ./dist --target bun --packages external --root ./src --minify",
16
+ "build:js": "bun build ./src/index.ts --outdir ./dist --target bun --packages external --root ./src --minify-syntax --minify-whitespace",
17
17
  "build:ts": "tsc",
18
18
  "build": "bun run clean && bun run build:js && bun run build:ts",
19
19
  "typecheck": "tsc --noEmit",
@@ -33,11 +33,10 @@
33
33
  "./src/index.ts"
34
34
  ],
35
35
  "dependencies": {
36
- "@elysiajs/swagger": "^1.3.1",
37
- "elysia": "^1.4.9",
36
+ "@elysiajs/openapi": "^1.4.16",
37
+ "elysia": "^1.4.30",
38
38
  "radash": "^12.1.1",
39
- "reflect-metadata": "^0.2.2",
40
- "wampy": "^7.2.0"
39
+ "wampy": "^8.0.2"
41
40
  },
42
41
  "trustedDependencies": [
43
42
  "es5-ext"
package/dist/job.d.ts DELETED
@@ -1,105 +0,0 @@
1
- import 'reflect-metadata';
2
- import type { Class } from 'type-fest';
3
- import { InteractsWithConsole } from './console';
4
- /**
5
- * Properties required when declaring a job using the `@job()` decorator.
6
- * @author Axel Nana <axel.nana@workbud.com>
7
- */
8
- export type JobProps = {
9
- /**
10
- * The name of the job.
11
- */
12
- name?: string;
13
- /**
14
- * The queue on which the job should be processed.
15
- */
16
- queue?: string;
17
- };
18
- /**
19
- * Base class for background jobs.
20
- *
21
- * Jobs are classes that can be executed in the background. They provide
22
- * an abstract execute method that serves as the entry point for the job.
23
- *
24
- * @author Axel Nana <axel.nana@workbud.com>
25
- */
26
- export declare abstract class Job extends InteractsWithConsole {
27
- #private;
28
- /**
29
- * Marks a class as a job.
30
- * @author Axel Nana <axel.nana@workbud.com>
31
- * @param options The decorator options.
32
- */
33
- static register(options?: JobProps): (target: Class<Job>) => void;
34
- /**
35
- * Unique identifier for the job.
36
- */
37
- readonly id: string;
38
- /**
39
- * The timestamp when the job was created.
40
- */
41
- readonly createdAt: Date;
42
- /**
43
- * The timestamp when the job started execution.
44
- */
45
- get startedAt(): Date | undefined;
46
- /**
47
- * The timestamp when the job completed execution.
48
- */
49
- get completedAt(): Date | undefined;
50
- /**
51
- * The current status of the job.
52
- */
53
- get status(): JobStatus;
54
- /**
55
- * Creates a new job instance.
56
- *
57
- * @param id Optional job ID. If not provided, a random ID will be generated.
58
- */
59
- constructor(id?: string);
60
- /**
61
- * Run the job.
62
- * This method handles the job lifecycle and calls the execute method.
63
- */
64
- run(): Promise<void>;
65
- /**
66
- * Cancels the job.
67
- */
68
- cancel(): void;
69
- /**
70
- * The main execution method for the job.
71
- * This must be implemented by all job classes.
72
- */
73
- protected abstract execute(): Promise<void>;
74
- /**
75
- * Generate a random job ID.
76
- * @returns A random job ID.
77
- */
78
- private generateJobId;
79
- }
80
- /**
81
- * Possible statuses for a job.
82
- * @author Axel Nana <axel.nana@workbud.com>
83
- */
84
- export declare enum JobStatus {
85
- /**
86
- * The job is in pending state, meaning it has been added to the queue but has not yet been processed.
87
- */
88
- PENDING = "pending",
89
- /**
90
- * The job is in running state, meaning it is currently being processed.
91
- */
92
- RUNNING = "running",
93
- /**
94
- * The job has completed successfully.
95
- */
96
- COMPLETED = "completed",
97
- /**
98
- * The job has failed to complete successfully. If there are enough retries left, the job will be restarted.
99
- */
100
- FAILED = "failed",
101
- /**
102
- * The job has been cancelled by the user or another job.
103
- */
104
- CANCELLED = "cancelled"
105
- }
package/dist/worker.d.ts DELETED
@@ -1,219 +0,0 @@
1
- import type { Class } from 'type-fest';
2
- import type { Job } from './job';
3
- import { InteractsWithConsole } from './console';
4
- declare const BunWorker: {
5
- new (scriptURL: string | URL, options?: WorkerOptions): globalThis.Worker;
6
- prototype: globalThis.Worker;
7
- };
8
- type BunWorker = InstanceType<typeof BunWorker>;
9
- /**
10
- * Configuration options for a job queue.
11
- * @author Axel Nana <axel.nana@workbud.com>
12
- */
13
- export interface QueueOptions {
14
- /**
15
- * The name of the queue.
16
- */
17
- name: string;
18
- /**
19
- * The maximum number of concurrent jobs to process in this queue.
20
- * @default 1
21
- */
22
- concurrency?: number;
23
- /**
24
- * The maximum number of retries for failed jobs.
25
- * @default 0
26
- */
27
- maxRetries?: number;
28
- /**
29
- * The delay in milliseconds between retries.
30
- * @default 1000
31
- */
32
- retryDelay?: number;
33
- /**
34
- * Whether to pause processing when an error occurs.
35
- * @default false
36
- */
37
- pauseOnError?: boolean;
38
- }
39
- /**
40
- * Worker class that manages multiple job queues and processes them in parallel.
41
- * Each queue processes its jobs sequentially by default, but can be configured for concurrency.
42
- * @author Axel Nana <axel.nana@workbud.com>
43
- */
44
- export declare class Worker extends InteractsWithConsole {
45
- #private;
46
- /**
47
- * The queues managed by this worker.
48
- */
49
- private queues;
50
- /**
51
- * The default queue name.
52
- */
53
- private defaultQueueName;
54
- static spawn(thread: BunWorker, queues?: string[], options?: Omit<QueueOptions, 'name'>): Worker;
55
- /**
56
- * Creates a new Worker instance.
57
- */
58
- constructor();
59
- /**
60
- * Gets the worker ID.
61
- * @returns The worker ID.
62
- */
63
- get id(): string;
64
- /**
65
- * Creates a new queue with the specified options.
66
- *
67
- * @param options The queue configuration options.
68
- * @returns The Worker instance for method chaining.
69
- */
70
- createQueue(options: QueueOptions): Worker;
71
- /**
72
- * Adds a job to the specified queue or the default queue if not specified.
73
- *
74
- * @param job The job to add to the queue.
75
- * @param queueName Optional name of the queue to add the job to. Defaults to the default queue.
76
- * @returns The Worker instance for method chaining.
77
- * @throws Error if the specified queue does not exist.
78
- */
79
- addJob(job: Job, queueName?: string): Worker;
80
- /**
81
- * Pauses a queue, preventing it from processing new jobs.
82
- *
83
- * @param queueName The name of the queue to pause. If not specified, pauses all queues.
84
- * @returns The Worker instance for method chaining.
85
- */
86
- pause(queueName?: string): Worker;
87
- /**
88
- * Resumes a paused queue.
89
- *
90
- * @param queueName The name of the queue to resume. If not specified, resumes all queues.
91
- * @returns The Worker instance for method chaining.
92
- */
93
- resume(queueName?: string): Worker;
94
- /**
95
- * Drains a queue, completing all current jobs but not accepting new ones.
96
- *
97
- * @param queueName The name of the queue to drain. If not specified, drains all queues.
98
- * @returns The Worker instance for method chaining.
99
- */
100
- drain(queueName?: string): Worker;
101
- /**
102
- * Clears all jobs from a queue.
103
- *
104
- * @param queueName The name of the queue to clear. If not specified, clears all queues.
105
- * @returns The Worker instance for method chaining.
106
- */
107
- clear(queueName?: string): Worker;
108
- /**
109
- * Gets the number of jobs in a queue.
110
- *
111
- * @param queueName The name of the queue to get the size of.
112
- * @returns The number of jobs in the queue.
113
- * @throws Error if the specified queue does not exist.
114
- */
115
- size(queueName?: string): number;
116
- /**
117
- * Gets the total number of jobs across all queues.
118
- *
119
- * @returns The total number of jobs.
120
- */
121
- totalSize(): number;
122
- /**
123
- * Processes the next job in the specified queue.
124
- *
125
- * @param queueName The name of the queue to process.
126
- */
127
- private processNextJob;
128
- /**
129
- * Processes jobs in a queue up to the concurrency limit.
130
- *
131
- * @param queueName The name of the queue to process.
132
- */
133
- private processQueueJobs;
134
- /**
135
- * Gets statistics about all queues.
136
- *
137
- * @returns An object containing statistics about each queue.
138
- */
139
- getStats(): Record<string, {
140
- waiting: number;
141
- processing: number;
142
- paused: boolean;
143
- draining: boolean;
144
- }>;
145
- /**
146
- * Cancels a job by its ID.
147
- *
148
- * @param jobId The ID of the job to cancel.
149
- * @param queueName Optional queue name to search in. If not provided, searches all queues.
150
- * @returns `true` if the job was found and canceled, false otherwise.
151
- */
152
- cancelJob(jobId: string, queueName?: string): boolean;
153
- /**
154
- * Cancels a job in a specific queue.
155
- *
156
- * @param jobId The ID of the job to cancel.
157
- * @param queueName The name of the queue to search in.
158
- * @returns `true` if the job was found and canceled, false otherwise.
159
- */
160
- private cancelJobInQueue;
161
- /**
162
- * Cancels all jobs in a queue.
163
- *
164
- * @param queueName The name of the queue to cancel jobs from. If not provided, cancels jobs in all queues.
165
- * @returns The number of jobs canceled.
166
- */
167
- cancelAllJobs(queueName?: string): number;
168
- }
169
- /**
170
- * A pool of worker threads.
171
- * @author Axel Nana <axel.nana@workbud.com>
172
- */
173
- export declare class WorkerPool extends InteractsWithConsole {
174
- private workers;
175
- private currentWorkerIndex;
176
- private queueWorkerMap;
177
- static readonly instance: WorkerPool;
178
- private constructor();
179
- /**
180
- * Initialize the worker pool.
181
- */
182
- init(): Promise<void>;
183
- /**
184
- * Add a new worker to the pool.
185
- * @param queues The queues to listen from the worker.
186
- */
187
- addWorker(queues?: string[]): Promise<void>;
188
- /**
189
- * Removes a worker from the pool.
190
- * @param w The worker to be removed. If not provided, the last worker will be removed.
191
- */
192
- removeWorker(w?: BunWorker): void;
193
- /**
194
- * Terminates all workers in the pool.
195
- */
196
- terminate(): void;
197
- /**
198
- * Runs a job in a worker.
199
- * @param job The job class to run.
200
- * @param args The class constructor arguments to pass to the job.
201
- */
202
- runJob<T extends Job, TClass extends Class<T>>(job: TClass, ...args: ConstructorParameters<TClass>): void;
203
- /**
204
- * Cancels a job in the worker pool.
205
- *
206
- * @param jobId The ID of the job to cancel.
207
- * @param queue Optional queue name to search in.
208
- * @returns `true` if a cancellation message was sent to workers.
209
- */
210
- cancelJob(jobId: string, queue?: string): boolean;
211
- /**
212
- * Cancels all jobs in a queue.
213
- *
214
- * @param queue Optional queue name to cancel jobs from. If not provided, cancels jobs in all queues.
215
- * @returns `true` if a cancellation message was sent to workers.
216
- */
217
- cancelAllJobs(queue?: string): boolean;
218
- }
219
- export {};