@hkdigital/lib-sveltekit 0.2.11 → 0.2.13

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.
@@ -1,350 +1,179 @@
1
1
  /**
2
- * @typedef {Object} ServiceEntry
3
- * @property {Object} instance - Service instance
4
- * @property {Object} config - Service configuration
5
- * @property {string[]} dependencies - List of service dependencies
6
- * @property {Function} stateChangedUnsubscribe - Unsubscribe function for state events
7
- * @property {Function} errorUnsubscribe - Unsubscribe function for error events
2
+ * @typedef {import('./typedef.js').ServiceConstructor} ServiceConstructor
3
+ * @typedef {import('./typedef.js').ServiceConfig} ServiceConfig
4
+ * @typedef {import('./typedef.js').ServiceRegistrationOptions} ServiceRegistrationOptions
5
+ * @typedef {import('./typedef.js').ServiceManagerConfig} ServiceManagerConfig
6
+ * @typedef {import('./typedef.js').StopOptions} StopOptions
7
+ * @typedef {import('./typedef.js').ServiceEntry} ServiceEntry
8
+ * @typedef {import('./typedef.js').HealthCheckResult} HealthCheckResult
8
9
  */
9
10
  /**
10
- * Manager for coordinating services lifecycle
11
+ * Service Manager for lifecycle and dependency management
12
+ * @extends EventEmitter
11
13
  */
12
- export default class ServiceManager {
14
+ export class ServiceManager extends EventEmitter {
13
15
  /**
14
- * Create a new service manager
16
+ * Create a new ServiceManager instance
15
17
  *
16
- * @param {Object} [options] - Manager options
17
- * @param {string} [options.logLevel=INFO] - Log level for the manager
18
- */
19
- constructor(options?: {
20
- logLevel?: string;
21
- });
22
- /**
23
- * Map of registered services
24
- * @type {Map<string, ServiceEntry>}
25
- * @private
26
- */
27
- private services;
28
- /**
29
- * Event emitter for service events
30
- * @type {EventEmitter}
31
- */
32
- events: EventEmitter;
33
- /**
34
- * Service manager logger
35
- * @type {Logger}
18
+ * @param {ServiceManagerConfig} [config={}] - Manager configuration
36
19
  */
20
+ constructor(config?: ServiceManagerConfig);
21
+ /** @type {Map<string, ServiceEntry>} */
22
+ services: Map<string, ServiceEntry>;
23
+ /** @type {Logger} */
37
24
  logger: Logger;
25
+ /** @type {ServiceManagerConfig} */
26
+ config: ServiceManagerConfig;
38
27
  /**
39
- * Service dependency graph
40
- * @type {Map<string, Set<string>>}
41
- * @private
42
- */
43
- private dependencyGraph;
44
- /**
45
- * Register an event handler
46
- *
47
- * @param {string} eventName - Event name
48
- * @param {Function} handler - Event handler
49
- * @returns {Function} Unsubscribe function
50
- */
51
- on(eventName: string, handler: Function): Function;
52
- /**
53
- * Remove an event handler
28
+ * Register a service class with the manager
54
29
  *
55
- * @param {string} eventName - Event name
56
- * @param {Function} handler - Event handler
57
- * @returns {boolean} True if handler was removed
58
- */
59
- off(eventName: string, handler: Function): boolean;
60
- /**
61
- * Emit an event
30
+ * @param {string} name - Unique service identifier
31
+ * @param {ServiceConstructor} ServiceClass - Service class constructor
32
+ * @param {ServiceConfig} [config={}] - Service configuration
33
+ * @param {ServiceRegistrationOptions} [options={}] - Registration options
62
34
  *
63
- * @param {string} eventName - Event name
64
- * @param {*} data - Event data
65
- * @returns {boolean} True if event had listeners
66
- * @private
35
+ * @throws {Error} If service name is already registered
67
36
  */
68
- private _emit;
37
+ register(name: string, ServiceClass: ServiceConstructor, config?: ServiceConfig, options?: ServiceRegistrationOptions): void;
69
38
  /**
70
- * Register a service
39
+ * Get or create a service instance
71
40
  *
72
41
  * @param {string} name - Service name
73
- * @param {Object} instance - Service instance
74
- * @param {Object} [options] - Registration options
75
- * @param {string[]} [options.dependencies=[]] - Service dependencies
76
- * @param {Object} [options.config={}] - Service configuration
77
- * @returns {boolean} True if registration was successful
78
42
  *
79
- * @example
80
- * manager.register('database', new DatabaseService());
81
- * manager.register('api', new ApiService(), {
82
- * dependencies: ['database'],
83
- * config: { port: 3000 }
84
- * });
43
+ * @returns {import('./typedef.js').ServiceBase|null}
44
+ * Service instance or null if not found
85
45
  */
86
- register(name: string, instance: any, options?: {
87
- dependencies?: string[];
88
- config?: any;
89
- }): boolean;
46
+ get(name: string): import("./typedef.js").ServiceBase | null;
90
47
  /**
91
- * Unregister a service
48
+ * Initialize a service
92
49
  *
93
50
  * @param {string} name - Service name
94
- * @returns {boolean} True if unregistration was successful
95
51
  *
96
- * @example
97
- * manager.unregister('api');
52
+ * @returns {Promise<boolean>} True if initialization succeeded
98
53
  */
99
- unregister(name: string): boolean;
54
+ initService(name: string): Promise<boolean>;
100
55
  /**
101
- * Get a service instance
56
+ * Start a service and its dependencies
102
57
  *
103
58
  * @param {string} name - Service name
104
- * @returns {Object|null} Service instance or null if not found
105
59
  *
106
- * @example
107
- * const db = manager.getService('database');
108
- * await db.query('SELECT * FROM users');
60
+ * @returns {Promise<boolean>} True if service started successfully
109
61
  */
110
- getService(name: string): any | null;
62
+ startService(name: string): Promise<boolean>;
111
63
  /**
112
- * Initialize a specific service
64
+ * Stop a service
113
65
  *
114
66
  * @param {string} name - Service name
115
- * @param {Object} [config] - Service configuration (overrides config from
116
- * registration)
117
- * @returns {Promise<boolean>} True if initialization was successful
118
- *
119
- * @example
120
- * await manager.initializeService('database', {
121
- * connectionString: 'mongodb://localhost:27017'
122
- * });
123
- */
124
- initializeService(name: string, config?: any): Promise<boolean>;
125
- /**
126
- * Initialize all registered services
67
+ * @param {StopOptions} [options={}] - Stop options
127
68
  *
128
- * @param {Object} [configs] - Configuration map for services
129
- * @returns {Promise<boolean>} True if all services initialized successfully
130
- *
131
- * @example
132
- * await manager.initializeAll({
133
- * database: { connectionString: 'mongodb://localhost:27017' },
134
- * api: { port: 3000 }
135
- * });
69
+ * @returns {Promise<boolean>} True if service stopped successfully
136
70
  */
137
- initializeAll(configs?: any): Promise<boolean>;
71
+ stopService(name: string, options?: StopOptions): Promise<boolean>;
138
72
  /**
139
- * Start a specific service and its dependencies
73
+ * Recover a service from error state
140
74
  *
141
75
  * @param {string} name - Service name
142
- * @returns {Promise<boolean>} True if start was successful
143
76
  *
144
- * @example
145
- * await manager.startService('api');
77
+ * @returns {Promise<boolean>} True if recovery succeeded
146
78
  */
147
- startService(name: string): Promise<boolean>;
79
+ recoverService(name: string): Promise<boolean>;
148
80
  /**
149
- * Start all services in dependency order
81
+ * Start all registered services in dependency order
150
82
  *
151
- * @returns {Promise<boolean>} True if all services started successfully
152
- *
153
- * @example
154
- * await manager.startAll();
83
+ * @returns {Promise<Object<string, boolean>>} Map of service results
155
84
  */
156
- startAll(): Promise<boolean>;
157
- /**
158
- * Stop a specific service and services that depend on it
159
- *
160
- * @param {string} name - Service name
161
- * @param {Object} [options] - Stop options
162
- * @param {boolean} [options.force=false] - Force stop even with dependents
163
- * @returns {Promise<boolean>} True if stop was successful
164
- *
165
- * @example
166
- * await manager.stopService('database');
167
- */
168
- stopService(name: string, options?: {
169
- force?: boolean;
170
- }): Promise<boolean>;
85
+ startAll(): Promise<{
86
+ [x: string]: boolean;
87
+ }>;
171
88
  /**
172
89
  * Stop all services in reverse dependency order
173
90
  *
174
- * @param {Object} [options] - Stop options
175
- * @param {boolean} [options.force=false] - Force stop even with errors
176
- * @returns {Promise<boolean>} True if all services stopped successfully
91
+ * @param {StopOptions} [options={}] - Stop options
177
92
  *
178
- * @example
179
- * await manager.stopAll();
93
+ * @returns {Promise<Object<string, boolean>>} Map of service results
180
94
  */
181
- stopAll(options?: {
182
- force?: boolean;
183
- }): Promise<boolean>;
95
+ stopAll(options?: StopOptions): Promise<{
96
+ [x: string]: boolean;
97
+ }>;
184
98
  /**
185
- * Destroy a service and remove it from the manager
99
+ * Stop services sequentially
186
100
  *
187
- * @param {string} name - Service name
188
- * @param {Object} [options] - Destroy options
189
- * @param {boolean} [options.force=false] - Force destroy even with dependents
190
- * @returns {Promise<boolean>} True if service was destroyed
191
- *
192
- * @example
193
- * await manager.destroyService('api');
101
+ * @private
102
+ * @param {string[]} serviceNames - Ordered list of service names
103
+ * @param {Map<string, boolean>} results - Results map to populate
104
+ * @param {StopOptions} options - Stop options
194
105
  */
195
- destroyService(name: string, options?: {
196
- force?: boolean;
197
- }): Promise<boolean>;
106
+ private _stopAllSequentially;
198
107
  /**
199
- * Destroy all services and shutdown the manager
108
+ * Get health status for all services
200
109
  *
201
- * @param {Object} [options] - Destroy options
202
- * @param {boolean} [options.force=false] - Force destroy even with errors
203
- * @returns {Promise<boolean>} True if all services were destroyed
204
- *
205
- * @example
206
- * await manager.destroyAll();
110
+ * @returns {Promise<HealthCheckResult>} Health status for all services
207
111
  */
208
- destroyAll(options?: {
209
- force?: boolean;
210
- }): Promise<boolean>;
112
+ checkHealth(): Promise<HealthCheckResult>;
211
113
  /**
212
- * Recover a service and its dependencies from error state
114
+ * Check if a service is currently running
213
115
  *
214
116
  * @param {string} name - Service name
215
- * @param {Object} [options] - Recovery options
216
- * @param {boolean} [options.recursive=true] - Recursively recover dependencies
217
- * @param {boolean} [options.autoStart=true] - Auto-start service after recovery
218
- * @returns {Promise<boolean>} True if recovery was successful
219
- *
220
- * @example
221
- * // Recover a service and its dependencies
222
- * await manager.recoverService('api');
223
117
  *
224
- * // Recover just this service without auto-starting
225
- * await manager.recoverService('database', {
226
- * recursive: false,
227
- * autoStart: false
228
- * });
118
+ * @returns {Promise<boolean>} True if service is running
229
119
  */
230
- recoverService(name: string, options?: {
231
- recursive?: boolean;
232
- autoStart?: boolean;
233
- }): Promise<boolean>;
120
+ isRunning(name: string): Promise<boolean>;
234
121
  /**
235
- * Recover all services in dependency order
236
- *
237
- * @param {Object} [options] - Recovery options
238
- * @param {boolean} [options.autoStart=true] - Auto-start services after recovery
239
- * @returns {Promise<boolean>} True if all recoveries were successful
240
- *
241
- * @example
242
- * // Recover all services and auto-start them
243
- * await manager.recoverAll();
122
+ * Set log level for a service or globally
244
123
  *
245
- * // Recover all services without auto-starting
246
- * await manager.recoverAll({ autoStart: false });
124
+ * @param {string} name - Service name or '*' for global
125
+ * @param {string} level - Log level to set
247
126
  */
248
- recoverAll(options?: {
249
- autoStart?: boolean;
250
- }): Promise<boolean>;
127
+ setLogLevel(name: string, level: string): void;
251
128
  /**
252
- * Set log level for a specific service or all services
129
+ * Get all services with a specific tag
253
130
  *
254
- * @param {string} level - New log level
255
- * @param {string} [serviceName] - Service to set level for, or all if omitted
256
- * @returns {boolean} True if level was set successfully
131
+ * @param {string} tag - Tag to filter by
257
132
  *
258
- * @example
259
- * // Set level for specific service
260
- * manager.setLogLevel(DEBUG, 'database');
261
- *
262
- * // Set level for all services including manager
263
- * manager.setLogLevel(INFO);
133
+ * @returns {string[]} Array of service names
264
134
  */
265
- setLogLevel(level: string, serviceName?: string): boolean;
135
+ getServicesByTag(tag: string): string[];
266
136
  /**
267
- * Get the names of all registered services
268
- *
269
- * @returns {string[]} List of service names
270
- *
271
- * @example
272
- * const services = manager.getServiceNames();
273
- * console.log(`Registered services: ${services.join(', ')}`);
274
- */
275
- getServiceNames(): string[];
276
- /**
277
- * Get service status information
278
- *
279
- * @param {string} [name] - Service name, or all if omitted
280
- * @returns {Object|Array|null} Service status or null if not found
281
- *
282
- * @example
283
- * // Get status for all services
284
- * const allStatus = manager.getServiceStatus();
285
- *
286
- * // Get status for specific service
287
- * const dbStatus = manager.getServiceStatus('database');
288
- * console.log(`Database state: ${dbStatus.state}`);
289
- */
290
- getServiceStatus(name?: string): any | any[] | null;
291
- /**
292
- * Handle state change events from services
137
+ * Setup logging configuration based on config.dev
293
138
  *
294
139
  * @private
295
- * @param {string} serviceName - Service name
296
- * @param {Object} event - State change event
297
140
  */
298
- private _handleStateChanged;
141
+ private _setupLogging;
299
142
  /**
300
- * Handle error events from services
143
+ * Get the appropriate log level for a service
301
144
  *
302
145
  * @private
303
- * @param {string} serviceName - Service name
304
- * @param {Object} event - Error event
305
- */
306
- private _handleError;
307
- /**
308
- * Update the dependency graph
146
+ * @param {string} name - Service name
309
147
  *
310
- * @private
148
+ * @returns {string|undefined} Log level or undefined
311
149
  */
312
- private _updateDependencyGraph;
150
+ private _getServiceLogLevel;
313
151
  /**
314
- * Check for circular dependencies in the graph
152
+ * Attach event listeners to forward service events
315
153
  *
316
154
  * @private
155
+ * @param {string} name - Service name
156
+ * @param {import('./typedef.js').ServiceBase} instance
157
+ * Service instance
317
158
  */
318
- private _checkForCircularDependencies;
159
+ private _attachServiceEvents;
319
160
  /**
320
- * Get optimal service start order based on dependencies
161
+ * Sort services by dependencies using topological sort
321
162
  *
322
163
  * @private
323
- * @returns {string[]} Services in dependency order
164
+ *
165
+ * @returns {string[]} Service names in dependency order
166
+ * @throws {Error} If circular dependencies are detected
324
167
  */
325
- private _getStartOrder;
168
+ private _topologicalSort;
326
169
  }
327
- export type ServiceEntry = {
328
- /**
329
- * - Service instance
330
- */
331
- instance: any;
332
- /**
333
- * - Service configuration
334
- */
335
- config: any;
336
- /**
337
- * - List of service dependencies
338
- */
339
- dependencies: string[];
340
- /**
341
- * - Unsubscribe function for state events
342
- */
343
- stateChangedUnsubscribe: Function;
344
- /**
345
- * - Unsubscribe function for error events
346
- */
347
- errorUnsubscribe: Function;
348
- };
170
+ export default ServiceManager;
171
+ export type ServiceConstructor = import("./typedef.js").ServiceConstructor;
172
+ export type ServiceConfig = import("./typedef.js").ServiceConfig;
173
+ export type ServiceRegistrationOptions = import("./typedef.js").ServiceRegistrationOptions;
174
+ export type ServiceManagerConfig = import("./typedef.js").ServiceManagerConfig;
175
+ export type StopOptions = import("./typedef.js").StopOptions;
176
+ export type ServiceEntry = import("./typedef.js").ServiceEntry;
177
+ export type HealthCheckResult = import("./typedef.js").HealthCheckResult;
349
178
  import { EventEmitter } from '../events';
350
179
  import { Logger } from '../logging';