@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.
- package/dist/classes/services/ServiceBase.d.ts +83 -73
- package/dist/classes/services/ServiceBase.js +237 -181
- package/dist/classes/services/ServiceManager.d.ts +96 -267
- package/dist/classes/services/ServiceManager.js +368 -874
- package/dist/classes/services/_old/ServiceBase.d.ts +153 -0
- package/dist/classes/services/_old/ServiceBase.js +409 -0
- package/dist/classes/services/_old/ServiceManager.d.ts +350 -0
- package/dist/classes/services/_old/ServiceManager.js +1114 -0
- package/dist/classes/services/service-states.d.ts +154 -0
- package/dist/classes/services/service-states.js +199 -0
- package/dist/classes/services/typedef.d.ts +206 -0
- package/dist/classes/services/typedef.js +169 -0
- package/package.json +1 -1
- /package/dist/classes/services/{constants.d.ts → _old/constants.d.ts} +0 -0
- /package/dist/classes/services/{constants.js → _old/constants.js} +0 -0
- /package/dist/classes/services/{index.d.ts → _old/index.d.ts} +0 -0
- /package/dist/classes/services/{index.js → _old/index.js} +0 -0
@@ -1,350 +1,179 @@
|
|
1
1
|
/**
|
2
|
-
* @typedef {
|
3
|
-
* @
|
4
|
-
* @
|
5
|
-
* @
|
6
|
-
* @
|
7
|
-
* @
|
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
|
11
|
+
* Service Manager for lifecycle and dependency management
|
12
|
+
* @extends EventEmitter
|
11
13
|
*/
|
12
|
-
export
|
14
|
+
export class ServiceManager extends EventEmitter {
|
13
15
|
/**
|
14
|
-
* Create a new
|
16
|
+
* Create a new ServiceManager instance
|
15
17
|
*
|
16
|
-
* @param {
|
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
|
-
*
|
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}
|
56
|
-
* @param {
|
57
|
-
* @
|
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
|
-
* @
|
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
|
-
|
37
|
+
register(name: string, ServiceClass: ServiceConstructor, config?: ServiceConfig, options?: ServiceRegistrationOptions): void;
|
69
38
|
/**
|
70
|
-
*
|
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
|
-
* @
|
80
|
-
*
|
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
|
-
|
87
|
-
dependencies?: string[];
|
88
|
-
config?: any;
|
89
|
-
}): boolean;
|
46
|
+
get(name: string): import("./typedef.js").ServiceBase | null;
|
90
47
|
/**
|
91
|
-
*
|
48
|
+
* Initialize a service
|
92
49
|
*
|
93
50
|
* @param {string} name - Service name
|
94
|
-
* @returns {boolean} True if unregistration was successful
|
95
51
|
*
|
96
|
-
* @
|
97
|
-
* manager.unregister('api');
|
52
|
+
* @returns {Promise<boolean>} True if initialization succeeded
|
98
53
|
*/
|
99
|
-
|
54
|
+
initService(name: string): Promise<boolean>;
|
100
55
|
/**
|
101
|
-
*
|
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
|
-
* @
|
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
|
-
|
62
|
+
startService(name: string): Promise<boolean>;
|
111
63
|
/**
|
112
|
-
*
|
64
|
+
* Stop a service
|
113
65
|
*
|
114
66
|
* @param {string} name - Service name
|
115
|
-
* @param {
|
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
|
-
* @
|
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
|
-
|
71
|
+
stopService(name: string, options?: StopOptions): Promise<boolean>;
|
138
72
|
/**
|
139
|
-
*
|
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
|
-
* @
|
145
|
-
* await manager.startService('api');
|
77
|
+
* @returns {Promise<boolean>} True if recovery succeeded
|
146
78
|
*/
|
147
|
-
|
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
|
152
|
-
*
|
153
|
-
* @example
|
154
|
-
* await manager.startAll();
|
83
|
+
* @returns {Promise<Object<string, boolean>>} Map of service results
|
155
84
|
*/
|
156
|
-
startAll(): Promise<
|
157
|
-
|
158
|
-
|
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 {
|
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
|
-
* @
|
179
|
-
* await manager.stopAll();
|
93
|
+
* @returns {Promise<Object<string, boolean>>} Map of service results
|
180
94
|
*/
|
181
|
-
stopAll(options?: {
|
182
|
-
|
183
|
-
}
|
95
|
+
stopAll(options?: StopOptions): Promise<{
|
96
|
+
[x: string]: boolean;
|
97
|
+
}>;
|
184
98
|
/**
|
185
|
-
*
|
99
|
+
* Stop services sequentially
|
186
100
|
*
|
187
|
-
* @
|
188
|
-
* @param {
|
189
|
-
* @param {boolean}
|
190
|
-
* @
|
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
|
-
|
196
|
-
force?: boolean;
|
197
|
-
}): Promise<boolean>;
|
106
|
+
private _stopAllSequentially;
|
198
107
|
/**
|
199
|
-
*
|
108
|
+
* Get health status for all services
|
200
109
|
*
|
201
|
-
* @
|
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
|
-
|
209
|
-
force?: boolean;
|
210
|
-
}): Promise<boolean>;
|
112
|
+
checkHealth(): Promise<HealthCheckResult>;
|
211
113
|
/**
|
212
|
-
*
|
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
|
-
*
|
225
|
-
* await manager.recoverService('database', {
|
226
|
-
* recursive: false,
|
227
|
-
* autoStart: false
|
228
|
-
* });
|
118
|
+
* @returns {Promise<boolean>} True if service is running
|
229
119
|
*/
|
230
|
-
|
231
|
-
recursive?: boolean;
|
232
|
-
autoStart?: boolean;
|
233
|
-
}): Promise<boolean>;
|
120
|
+
isRunning(name: string): Promise<boolean>;
|
234
121
|
/**
|
235
|
-
*
|
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
|
-
*
|
246
|
-
*
|
124
|
+
* @param {string} name - Service name or '*' for global
|
125
|
+
* @param {string} level - Log level to set
|
247
126
|
*/
|
248
|
-
|
249
|
-
autoStart?: boolean;
|
250
|
-
}): Promise<boolean>;
|
127
|
+
setLogLevel(name: string, level: string): void;
|
251
128
|
/**
|
252
|
-
*
|
129
|
+
* Get all services with a specific tag
|
253
130
|
*
|
254
|
-
* @param {string}
|
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
|
-
* @
|
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
|
-
|
135
|
+
getServicesByTag(tag: string): string[];
|
266
136
|
/**
|
267
|
-
*
|
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
|
141
|
+
private _setupLogging;
|
299
142
|
/**
|
300
|
-
*
|
143
|
+
* Get the appropriate log level for a service
|
301
144
|
*
|
302
145
|
* @private
|
303
|
-
* @param {string}
|
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
|
-
* @
|
148
|
+
* @returns {string|undefined} Log level or undefined
|
311
149
|
*/
|
312
|
-
private
|
150
|
+
private _getServiceLogLevel;
|
313
151
|
/**
|
314
|
-
*
|
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
|
159
|
+
private _attachServiceEvents;
|
319
160
|
/**
|
320
|
-
*
|
161
|
+
* Sort services by dependencies using topological sort
|
321
162
|
*
|
322
163
|
* @private
|
323
|
-
*
|
164
|
+
*
|
165
|
+
* @returns {string[]} Service names in dependency order
|
166
|
+
* @throws {Error} If circular dependencies are detected
|
324
167
|
*/
|
325
|
-
private
|
168
|
+
private _topologicalSort;
|
326
169
|
}
|
327
|
-
export
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
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';
|