@hkdigital/lib-sveltekit 0.2.11 → 0.2.12
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 +367 -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
@@ -0,0 +1,154 @@
|
|
1
|
+
/**
|
2
|
+
* Check if a state transition is valid
|
3
|
+
*
|
4
|
+
* @param {string} fromState - Current state
|
5
|
+
* @param {string} toState - Desired state
|
6
|
+
*
|
7
|
+
* @returns {boolean} True if transition is valid
|
8
|
+
*/
|
9
|
+
export function isValidTransition(fromState: string, toState: string): boolean;
|
10
|
+
/**
|
11
|
+
* Check if a service is in an operational state
|
12
|
+
*
|
13
|
+
* @param {string} state - Current state
|
14
|
+
*
|
15
|
+
* @returns {boolean} True if operational
|
16
|
+
*/
|
17
|
+
export function isOperational(state: string): boolean;
|
18
|
+
/**
|
19
|
+
* Check if a service is in a transitional state
|
20
|
+
*
|
21
|
+
* @param {string} state - Current state
|
22
|
+
*
|
23
|
+
* @returns {boolean} True if transitioning
|
24
|
+
*/
|
25
|
+
export function isTransitioning(state: string): boolean;
|
26
|
+
/**
|
27
|
+
* Check if a service is in an inactive state
|
28
|
+
*
|
29
|
+
* @param {string} state - Current state
|
30
|
+
*
|
31
|
+
* @returns {boolean} True if inactive
|
32
|
+
*/
|
33
|
+
export function isInactive(state: string): boolean;
|
34
|
+
/**
|
35
|
+
* @fileoverview Service state constants for the service management system.
|
36
|
+
*
|
37
|
+
* Defines all possible states a service can be in during its lifecycle.
|
38
|
+
* Services transition through these states as they are initialized, started,
|
39
|
+
* stopped, and handle errors or recovery.
|
40
|
+
*
|
41
|
+
* @example
|
42
|
+
* // Using state constants in a service
|
43
|
+
* import { RUNNING, ERROR } from './service-states.js';
|
44
|
+
*
|
45
|
+
* class MyService extends ServiceBase {
|
46
|
+
* async doWork() {
|
47
|
+
* if (this.state !== RUNNING) {
|
48
|
+
* throw new Error('Service not running');
|
49
|
+
* }
|
50
|
+
* // ... do work
|
51
|
+
* }
|
52
|
+
* }
|
53
|
+
*
|
54
|
+
* @example
|
55
|
+
* // Checking service states
|
56
|
+
* import { RUNNING, ERROR, STOPPED } from './service-states.js';
|
57
|
+
*
|
58
|
+
* const service = serviceManager.get('database');
|
59
|
+
*
|
60
|
+
* switch (service.state) {
|
61
|
+
* case RUNNING:
|
62
|
+
* console.log('Service is operational');
|
63
|
+
* break;
|
64
|
+
* case ERROR:
|
65
|
+
* console.log('Service has failed');
|
66
|
+
* break;
|
67
|
+
* case STOPPED:
|
68
|
+
* console.log('Service is stopped');
|
69
|
+
* break;
|
70
|
+
* }
|
71
|
+
*/
|
72
|
+
/**
|
73
|
+
* Service has been created but not initialized
|
74
|
+
* @const {string}
|
75
|
+
*/
|
76
|
+
export const CREATED: "created";
|
77
|
+
/**
|
78
|
+
* Service is currently initializing
|
79
|
+
* @const {string}
|
80
|
+
*/
|
81
|
+
export const INITIALIZING: "initializing";
|
82
|
+
/**
|
83
|
+
* Service has been initialized and is ready to start
|
84
|
+
* @const {string}
|
85
|
+
*/
|
86
|
+
export const INITIALIZED: "initialized";
|
87
|
+
/**
|
88
|
+
* Service is currently starting up
|
89
|
+
* @const {string}
|
90
|
+
*/
|
91
|
+
export const STARTING: "starting";
|
92
|
+
/**
|
93
|
+
* Service is running and operational
|
94
|
+
* @const {string}
|
95
|
+
*/
|
96
|
+
export const RUNNING: "running";
|
97
|
+
/**
|
98
|
+
* Service is currently shutting down
|
99
|
+
* @const {string}
|
100
|
+
*/
|
101
|
+
export const STOPPING: "stopping";
|
102
|
+
/**
|
103
|
+
* Service has been stopped
|
104
|
+
* @const {string}
|
105
|
+
*/
|
106
|
+
export const STOPPED: "stopped";
|
107
|
+
/**
|
108
|
+
* Service is being destroyed and cleaned up
|
109
|
+
* @const {string}
|
110
|
+
*/
|
111
|
+
export const DESTROYING: "destroying";
|
112
|
+
/**
|
113
|
+
* Service has been destroyed and cannot be used
|
114
|
+
* @const {string}
|
115
|
+
*/
|
116
|
+
export const DESTROYED: "destroyed";
|
117
|
+
/**
|
118
|
+
* Service encountered an error and is not operational
|
119
|
+
* @const {string}
|
120
|
+
*/
|
121
|
+
export const ERROR: "error";
|
122
|
+
/**
|
123
|
+
* Service is attempting to recover from an error
|
124
|
+
* @const {string}
|
125
|
+
*/
|
126
|
+
export const RECOVERING: "recovering";
|
127
|
+
export namespace VALID_TRANSITIONS {
|
128
|
+
let created: string[];
|
129
|
+
let initializing: string[];
|
130
|
+
let initialized: string[];
|
131
|
+
let starting: string[];
|
132
|
+
let running: string[];
|
133
|
+
let stopping: string[];
|
134
|
+
let stopped: string[];
|
135
|
+
let destroying: string[];
|
136
|
+
let destroyed: string[];
|
137
|
+
let error: string[];
|
138
|
+
let recovering: string[];
|
139
|
+
}
|
140
|
+
/**
|
141
|
+
* States that indicate the service is operational
|
142
|
+
* @const {string[]}
|
143
|
+
*/
|
144
|
+
export const OPERATIONAL_STATES: string[];
|
145
|
+
/**
|
146
|
+
* States that indicate the service is transitioning
|
147
|
+
* @const {string[]}
|
148
|
+
*/
|
149
|
+
export const TRANSITIONAL_STATES: string[];
|
150
|
+
/**
|
151
|
+
* States that indicate the service is not operational
|
152
|
+
* @const {string[]}
|
153
|
+
*/
|
154
|
+
export const INACTIVE_STATES: string[];
|
@@ -0,0 +1,199 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview Service state constants for the service management system.
|
3
|
+
*
|
4
|
+
* Defines all possible states a service can be in during its lifecycle.
|
5
|
+
* Services transition through these states as they are initialized, started,
|
6
|
+
* stopped, and handle errors or recovery.
|
7
|
+
*
|
8
|
+
* @example
|
9
|
+
* // Using state constants in a service
|
10
|
+
* import { RUNNING, ERROR } from './service-states.js';
|
11
|
+
*
|
12
|
+
* class MyService extends ServiceBase {
|
13
|
+
* async doWork() {
|
14
|
+
* if (this.state !== RUNNING) {
|
15
|
+
* throw new Error('Service not running');
|
16
|
+
* }
|
17
|
+
* // ... do work
|
18
|
+
* }
|
19
|
+
* }
|
20
|
+
*
|
21
|
+
* @example
|
22
|
+
* // Checking service states
|
23
|
+
* import { RUNNING, ERROR, STOPPED } from './service-states.js';
|
24
|
+
*
|
25
|
+
* const service = serviceManager.get('database');
|
26
|
+
*
|
27
|
+
* switch (service.state) {
|
28
|
+
* case RUNNING:
|
29
|
+
* console.log('Service is operational');
|
30
|
+
* break;
|
31
|
+
* case ERROR:
|
32
|
+
* console.log('Service has failed');
|
33
|
+
* break;
|
34
|
+
* case STOPPED:
|
35
|
+
* console.log('Service is stopped');
|
36
|
+
* break;
|
37
|
+
* }
|
38
|
+
*/
|
39
|
+
|
40
|
+
/**
|
41
|
+
* Service has been created but not initialized
|
42
|
+
* @const {string}
|
43
|
+
*/
|
44
|
+
export const CREATED = 'created';
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Service is currently initializing
|
48
|
+
* @const {string}
|
49
|
+
*/
|
50
|
+
export const INITIALIZING = 'initializing';
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Service has been initialized and is ready to start
|
54
|
+
* @const {string}
|
55
|
+
*/
|
56
|
+
export const INITIALIZED = 'initialized';
|
57
|
+
|
58
|
+
/**
|
59
|
+
* Service is currently starting up
|
60
|
+
* @const {string}
|
61
|
+
*/
|
62
|
+
export const STARTING = 'starting';
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Service is running and operational
|
66
|
+
* @const {string}
|
67
|
+
*/
|
68
|
+
export const RUNNING = 'running';
|
69
|
+
|
70
|
+
/**
|
71
|
+
* Service is currently shutting down
|
72
|
+
* @const {string}
|
73
|
+
*/
|
74
|
+
export const STOPPING = 'stopping';
|
75
|
+
|
76
|
+
/**
|
77
|
+
* Service has been stopped
|
78
|
+
* @const {string}
|
79
|
+
*/
|
80
|
+
export const STOPPED = 'stopped';
|
81
|
+
|
82
|
+
/**
|
83
|
+
* Service is being destroyed and cleaned up
|
84
|
+
* @const {string}
|
85
|
+
*/
|
86
|
+
export const DESTROYING = 'destroying';
|
87
|
+
|
88
|
+
/**
|
89
|
+
* Service has been destroyed and cannot be used
|
90
|
+
* @const {string}
|
91
|
+
*/
|
92
|
+
export const DESTROYED = 'destroyed';
|
93
|
+
|
94
|
+
/**
|
95
|
+
* Service encountered an error and is not operational
|
96
|
+
* @const {string}
|
97
|
+
*/
|
98
|
+
export const ERROR = 'error';
|
99
|
+
|
100
|
+
/**
|
101
|
+
* Service is attempting to recover from an error
|
102
|
+
* @const {string}
|
103
|
+
*/
|
104
|
+
export const RECOVERING = 'recovering';
|
105
|
+
|
106
|
+
/**
|
107
|
+
* Valid state transitions map
|
108
|
+
* Maps each state to the states it can transition to
|
109
|
+
* @const {Object<string, string[]>}
|
110
|
+
*/
|
111
|
+
export const VALID_TRANSITIONS = {
|
112
|
+
[CREATED]: [INITIALIZING, DESTROYING],
|
113
|
+
[INITIALIZING]: [INITIALIZED, ERROR, DESTROYING],
|
114
|
+
[INITIALIZED]: [STARTING, DESTROYING],
|
115
|
+
[STARTING]: [RUNNING, ERROR, STOPPING],
|
116
|
+
[RUNNING]: [STOPPING, ERROR],
|
117
|
+
[STOPPING]: [STOPPED, ERROR],
|
118
|
+
[STOPPED]: [STARTING, DESTROYING, INITIALIZING],
|
119
|
+
[DESTROYING]: [DESTROYED, ERROR],
|
120
|
+
[DESTROYED]: [CREATED], // Can be recreated
|
121
|
+
[ERROR]: [RECOVERING, STOPPING, DESTROYING],
|
122
|
+
[RECOVERING]: [RUNNING, STOPPED, ERROR]
|
123
|
+
};
|
124
|
+
|
125
|
+
/**
|
126
|
+
* States that indicate the service is operational
|
127
|
+
* @const {string[]}
|
128
|
+
*/
|
129
|
+
export const OPERATIONAL_STATES = [RUNNING];
|
130
|
+
|
131
|
+
/**
|
132
|
+
* States that indicate the service is transitioning
|
133
|
+
* @const {string[]}
|
134
|
+
*/
|
135
|
+
export const TRANSITIONAL_STATES = [
|
136
|
+
INITIALIZING,
|
137
|
+
STARTING,
|
138
|
+
STOPPING,
|
139
|
+
DESTROYING,
|
140
|
+
RECOVERING
|
141
|
+
];
|
142
|
+
|
143
|
+
/**
|
144
|
+
* States that indicate the service is not operational
|
145
|
+
* @const {string[]}
|
146
|
+
*/
|
147
|
+
export const INACTIVE_STATES = [
|
148
|
+
CREATED,
|
149
|
+
INITIALIZED,
|
150
|
+
STOPPED,
|
151
|
+
DESTROYED,
|
152
|
+
ERROR
|
153
|
+
];
|
154
|
+
|
155
|
+
/**
|
156
|
+
* Check if a state transition is valid
|
157
|
+
*
|
158
|
+
* @param {string} fromState - Current state
|
159
|
+
* @param {string} toState - Desired state
|
160
|
+
*
|
161
|
+
* @returns {boolean} True if transition is valid
|
162
|
+
*/
|
163
|
+
export function isValidTransition(fromState, toState) {
|
164
|
+
const validStates = VALID_TRANSITIONS[fromState];
|
165
|
+
return validStates ? validStates.includes(toState) : false;
|
166
|
+
}
|
167
|
+
|
168
|
+
/**
|
169
|
+
* Check if a service is in an operational state
|
170
|
+
*
|
171
|
+
* @param {string} state - Current state
|
172
|
+
*
|
173
|
+
* @returns {boolean} True if operational
|
174
|
+
*/
|
175
|
+
export function isOperational(state) {
|
176
|
+
return OPERATIONAL_STATES.includes(state);
|
177
|
+
}
|
178
|
+
|
179
|
+
/**
|
180
|
+
* Check if a service is in a transitional state
|
181
|
+
*
|
182
|
+
* @param {string} state - Current state
|
183
|
+
*
|
184
|
+
* @returns {boolean} True if transitioning
|
185
|
+
*/
|
186
|
+
export function isTransitioning(state) {
|
187
|
+
return TRANSITIONAL_STATES.includes(state);
|
188
|
+
}
|
189
|
+
|
190
|
+
/**
|
191
|
+
* Check if a service is in an inactive state
|
192
|
+
*
|
193
|
+
* @param {string} state - Current state
|
194
|
+
*
|
195
|
+
* @returns {boolean} True if inactive
|
196
|
+
*/
|
197
|
+
export function isInactive(state) {
|
198
|
+
return INACTIVE_STATES.includes(state);
|
199
|
+
}
|
@@ -0,0 +1,206 @@
|
|
1
|
+
/**
|
2
|
+
* //
|
3
|
+
*/
|
4
|
+
export type ServiceConfig = import("./typedef.js").ServiceConfig;
|
5
|
+
/**
|
6
|
+
* class MyService extends ServiceBase {
|
7
|
+
* async _init(config) {
|
8
|
+
* // config is typed as ServiceConfig
|
9
|
+
* }
|
10
|
+
*
|
11
|
+
* async _healthCheck() {
|
12
|
+
* // Return type is HealthStatus
|
13
|
+
* return { latency: 10 };
|
14
|
+
* }
|
15
|
+
* }
|
16
|
+
*/
|
17
|
+
export type HealthStatus = import("./typedef.js").HealthStatus;
|
18
|
+
/**
|
19
|
+
* //
|
20
|
+
*/
|
21
|
+
export type ServiceManagerConfig = import("./typedef.js").ServiceManagerConfig;
|
22
|
+
/**
|
23
|
+
* const config = {
|
24
|
+
* environment: 'development',
|
25
|
+
* stopTimeout: 5000
|
26
|
+
* };
|
27
|
+
*
|
28
|
+
* const manager = new ServiceManager(config);
|
29
|
+
*
|
30
|
+
* const options = {
|
31
|
+
* dependencies: ['database'],
|
32
|
+
* tags: ['critical']
|
33
|
+
* };
|
34
|
+
*
|
35
|
+
* manager.register('auth', AuthService, {}, options);
|
36
|
+
*/
|
37
|
+
export type ServiceRegistrationOptions = import("./typedef.js").ServiceRegistrationOptions;
|
38
|
+
/**
|
39
|
+
* Options for creating a service instance
|
40
|
+
*/
|
41
|
+
export type ServiceOptions = {
|
42
|
+
/**
|
43
|
+
* - Initial log level for the service
|
44
|
+
*/
|
45
|
+
logLevel?: string;
|
46
|
+
/**
|
47
|
+
* - Timeout for graceful shutdown
|
48
|
+
*/
|
49
|
+
shutdownTimeout?: number;
|
50
|
+
};
|
51
|
+
/**
|
52
|
+
* Options for stopping a service
|
53
|
+
*/
|
54
|
+
export type StopOptions = {
|
55
|
+
/**
|
56
|
+
* - Override shutdown timeout
|
57
|
+
*/
|
58
|
+
timeout?: number;
|
59
|
+
/**
|
60
|
+
* - Force stop even if timeout exceeded
|
61
|
+
*/
|
62
|
+
force?: boolean;
|
63
|
+
};
|
64
|
+
/**
|
65
|
+
* Event emitted when service state changes
|
66
|
+
*/
|
67
|
+
export type StateChangeEvent = {
|
68
|
+
/**
|
69
|
+
* - Service name
|
70
|
+
*/
|
71
|
+
service: string;
|
72
|
+
/**
|
73
|
+
* - Previous state
|
74
|
+
*/
|
75
|
+
oldState: string;
|
76
|
+
/**
|
77
|
+
* - New state
|
78
|
+
*/
|
79
|
+
newState: string;
|
80
|
+
};
|
81
|
+
/**
|
82
|
+
* Event emitted when service health changes
|
83
|
+
*/
|
84
|
+
export type HealthChangeEvent = {
|
85
|
+
/**
|
86
|
+
* - Service name
|
87
|
+
*/
|
88
|
+
service: string;
|
89
|
+
/**
|
90
|
+
* - New health status
|
91
|
+
*/
|
92
|
+
healthy: boolean;
|
93
|
+
};
|
94
|
+
/**
|
95
|
+
* Event emitted when service encounters an error
|
96
|
+
*/
|
97
|
+
export type ServiceErrorEvent = {
|
98
|
+
/**
|
99
|
+
* - Service name
|
100
|
+
*/
|
101
|
+
service: string;
|
102
|
+
/**
|
103
|
+
* - Operation that failed
|
104
|
+
*/
|
105
|
+
operation: string;
|
106
|
+
/**
|
107
|
+
* - Error that occurred
|
108
|
+
*/
|
109
|
+
error: Error;
|
110
|
+
};
|
111
|
+
/**
|
112
|
+
* Service class constructor type
|
113
|
+
*/
|
114
|
+
export type ServiceConstructor = new (name: string, options?: ServiceOptions) => ServiceBase;
|
115
|
+
/**
|
116
|
+
* Logging configuration
|
117
|
+
*/
|
118
|
+
export type LogConfig = {
|
119
|
+
/**
|
120
|
+
* - Default log level for services
|
121
|
+
*/
|
122
|
+
defaultLevel?: string;
|
123
|
+
/**
|
124
|
+
* - Override level for all services
|
125
|
+
*/
|
126
|
+
globalLevel?: string;
|
127
|
+
/**
|
128
|
+
* - Per-service log levels
|
129
|
+
*/
|
130
|
+
serviceLevels?: {
|
131
|
+
[x: string]: string;
|
132
|
+
};
|
133
|
+
};
|
134
|
+
/**
|
135
|
+
* Internal service registry entry
|
136
|
+
*/
|
137
|
+
export type ServiceEntry = {
|
138
|
+
/**
|
139
|
+
* - Service class constructor
|
140
|
+
*/
|
141
|
+
ServiceClass: ServiceConstructor;
|
142
|
+
/**
|
143
|
+
* - Service instance (lazy-created)
|
144
|
+
*/
|
145
|
+
instance: ServiceBase | null;
|
146
|
+
/**
|
147
|
+
* - Service configuration
|
148
|
+
*/
|
149
|
+
config: ServiceConfig;
|
150
|
+
/**
|
151
|
+
* - Service dependencies
|
152
|
+
*/
|
153
|
+
dependencies: string[];
|
154
|
+
/**
|
155
|
+
* - Services that depend on this one
|
156
|
+
*/
|
157
|
+
dependents: Set<string>;
|
158
|
+
/**
|
159
|
+
* - Service tags
|
160
|
+
*/
|
161
|
+
tags: string[];
|
162
|
+
/**
|
163
|
+
* - Startup priority
|
164
|
+
*/
|
165
|
+
priority: number;
|
166
|
+
};
|
167
|
+
/**
|
168
|
+
* Result of health check for all services
|
169
|
+
*/
|
170
|
+
export type HealthCheckResult = {
|
171
|
+
[x: string]: any;
|
172
|
+
};
|
173
|
+
/**
|
174
|
+
* Base class interface that services must implement
|
175
|
+
*/
|
176
|
+
export type ServiceBase = {
|
177
|
+
/**
|
178
|
+
* - Service name
|
179
|
+
*/
|
180
|
+
name: string;
|
181
|
+
/**
|
182
|
+
* - Current state
|
183
|
+
*/
|
184
|
+
state: string;
|
185
|
+
/**
|
186
|
+
* - Health status
|
187
|
+
*/
|
188
|
+
healthy: boolean;
|
189
|
+
/**
|
190
|
+
* - Last error
|
191
|
+
*/
|
192
|
+
error: Error | null;
|
193
|
+
/**
|
194
|
+
* - Service logger
|
195
|
+
*/
|
196
|
+
logger: Logger;
|
197
|
+
initialize: (config?: ServiceConfig) => Promise<boolean>;
|
198
|
+
start: () => Promise<boolean>;
|
199
|
+
stop: (options?: StopOptions) => Promise<boolean>;
|
200
|
+
recover: () => Promise<boolean>;
|
201
|
+
destroy: () => Promise<boolean>;
|
202
|
+
getHealth: () => Promise<HealthStatus>;
|
203
|
+
setLogLevel: (level: string) => boolean;
|
204
|
+
on: (event: string, handler: Function) => Function;
|
205
|
+
emit: (event: string, data: any) => boolean;
|
206
|
+
};
|