imean-service-engine 1.0.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 +585 -0
- package/dist/mod.cjs +1217 -0
- package/dist/mod.d.cts +259 -0
- package/dist/mod.d.ts +259 -0
- package/dist/mod.js +1192 -0
- package/package.json +74 -0
package/dist/mod.d.cts
ADDED
@@ -0,0 +1,259 @@
|
|
1
|
+
import { z } from 'zod';
|
2
|
+
export * from 'zod';
|
3
|
+
import { Lease, Etcd3 } from 'etcd3';
|
4
|
+
import { Hono } from 'hono';
|
5
|
+
import { LRUCache } from 'lru-cache';
|
6
|
+
export { default as dayjs } from 'dayjs';
|
7
|
+
import winston from 'winston';
|
8
|
+
|
9
|
+
interface ActionOptions {
|
10
|
+
params?: z.ZodType<any>[];
|
11
|
+
returns?: z.ZodType<any>;
|
12
|
+
idempotence?: boolean;
|
13
|
+
description?: string;
|
14
|
+
printError?: boolean;
|
15
|
+
cache?: boolean;
|
16
|
+
ttl?: number;
|
17
|
+
stream?: boolean;
|
18
|
+
}
|
19
|
+
interface ActionMetadata extends ActionOptions {
|
20
|
+
name: string;
|
21
|
+
idempotence: boolean;
|
22
|
+
cache?: boolean;
|
23
|
+
ttl?: number;
|
24
|
+
stream?: boolean;
|
25
|
+
}
|
26
|
+
interface ModuleOptions {
|
27
|
+
/** 模块名称 */
|
28
|
+
name?: string;
|
29
|
+
/** 模块版本 */
|
30
|
+
version?: string;
|
31
|
+
/** 模块描述 */
|
32
|
+
description?: string;
|
33
|
+
/** 打印错误 */
|
34
|
+
printError?: boolean;
|
35
|
+
}
|
36
|
+
interface ModuleMetadata {
|
37
|
+
name: string;
|
38
|
+
options: Required<ModuleOptions>;
|
39
|
+
}
|
40
|
+
declare enum ScheduleMode {
|
41
|
+
/** 固定间隔触发,不管上次任务是否完成 */
|
42
|
+
FIXED_RATE = "FIXED_RATE",
|
43
|
+
/** 任务完成后再等待间隔时间,再执行下一次 */
|
44
|
+
FIXED_DELAY = "FIXED_DELAY"
|
45
|
+
}
|
46
|
+
interface ScheduleOptions {
|
47
|
+
/** 执行间隔(毫秒) */
|
48
|
+
interval: number;
|
49
|
+
/** 调度模式 */
|
50
|
+
mode?: ScheduleMode;
|
51
|
+
}
|
52
|
+
interface ScheduleMetadata extends ScheduleOptions {
|
53
|
+
name: string;
|
54
|
+
electionKey: string;
|
55
|
+
}
|
56
|
+
interface EtcdConfig {
|
57
|
+
hosts: string[];
|
58
|
+
auth?: {
|
59
|
+
username: string;
|
60
|
+
password: string;
|
61
|
+
};
|
62
|
+
/** 服务注册的 TTL,默认 10 秒 */
|
63
|
+
ttl?: number;
|
64
|
+
/** etcd 的命名空间 */
|
65
|
+
namespace?: string;
|
66
|
+
}
|
67
|
+
interface ServiceStats {
|
68
|
+
totalCalls: number;
|
69
|
+
successCalls: number;
|
70
|
+
failureCalls: number;
|
71
|
+
avgResponseTime: number;
|
72
|
+
maxResponseTime: number;
|
73
|
+
minResponseTime: number;
|
74
|
+
lastUpdateTime: number;
|
75
|
+
cacheHit: number;
|
76
|
+
}
|
77
|
+
interface StatisticsEventOptions {
|
78
|
+
/** 统计事件触发间隔,默认 5 秒 */
|
79
|
+
interval?: number;
|
80
|
+
/** 是否在服务停止时强制触发未发送的统计事件 */
|
81
|
+
forceEmitOnShutdown?: boolean;
|
82
|
+
}
|
83
|
+
interface ServiceInfo {
|
84
|
+
id: string;
|
85
|
+
name: string;
|
86
|
+
version?: string;
|
87
|
+
prefix?: string;
|
88
|
+
env?: string;
|
89
|
+
modules: Record<string, ModuleInfo>;
|
90
|
+
}
|
91
|
+
interface EventServiceInfo {
|
92
|
+
id: string;
|
93
|
+
name: string;
|
94
|
+
version?: string;
|
95
|
+
env?: string;
|
96
|
+
}
|
97
|
+
interface StatisticsEvent {
|
98
|
+
service: EventServiceInfo;
|
99
|
+
module: string;
|
100
|
+
action: string;
|
101
|
+
stats: ServiceStats;
|
102
|
+
startTime: number;
|
103
|
+
endTime: number;
|
104
|
+
}
|
105
|
+
interface ActionErrorEvent {
|
106
|
+
service: EventServiceInfo;
|
107
|
+
module: string;
|
108
|
+
action: string;
|
109
|
+
requestHash: string;
|
110
|
+
params: any[];
|
111
|
+
time: number;
|
112
|
+
error: string;
|
113
|
+
}
|
114
|
+
interface MicroserviceOptions {
|
115
|
+
name?: string;
|
116
|
+
version?: string;
|
117
|
+
prefix?: string;
|
118
|
+
env?: string;
|
119
|
+
printError?: boolean;
|
120
|
+
disableCache?: boolean;
|
121
|
+
modules: (new () => any)[];
|
122
|
+
generateClient?: URL | boolean;
|
123
|
+
etcd?: EtcdConfig;
|
124
|
+
events?: StatisticsEventOptions & {
|
125
|
+
onStats?: (event: StatisticsEvent) => void | Promise<void>;
|
126
|
+
onRegister?: (serviceInfo: ServiceInfo) => void | Promise<void>;
|
127
|
+
onError?: (event: ActionErrorEvent) => void | Promise<void>;
|
128
|
+
};
|
129
|
+
websocket?: {
|
130
|
+
enabled?: boolean;
|
131
|
+
timeout?: number;
|
132
|
+
};
|
133
|
+
}
|
134
|
+
interface ModuleInfo extends ModuleOptions {
|
135
|
+
actions: Record<string, ActionMetadata>;
|
136
|
+
name: string;
|
137
|
+
}
|
138
|
+
interface StreamResponse<T = any> {
|
139
|
+
value?: T;
|
140
|
+
done: boolean;
|
141
|
+
error?: string;
|
142
|
+
}
|
143
|
+
|
144
|
+
declare class ActionHandler {
|
145
|
+
private moduleInstance;
|
146
|
+
private actionName;
|
147
|
+
metadata: ActionMetadata;
|
148
|
+
private microservice;
|
149
|
+
private moduleName;
|
150
|
+
constructor(moduleInstance: any, actionName: string, metadata: ActionMetadata, microservice: Microservice, moduleName: string);
|
151
|
+
handle(req: string | any[]): Promise<any>;
|
152
|
+
}
|
153
|
+
|
154
|
+
declare const ServiceContext: {
|
155
|
+
service?: Microservice;
|
156
|
+
lease?: Lease;
|
157
|
+
etcdClient?: Etcd3;
|
158
|
+
};
|
159
|
+
declare class Microservice {
|
160
|
+
private app;
|
161
|
+
private nodeWebSocket;
|
162
|
+
private codeCache?;
|
163
|
+
private waitingInitialization;
|
164
|
+
private etcdClient?;
|
165
|
+
private serviceKey?;
|
166
|
+
private statsMap;
|
167
|
+
private lease?;
|
168
|
+
private scheduler?;
|
169
|
+
private abortController?;
|
170
|
+
private isShuttingDown;
|
171
|
+
private statisticsTimer?;
|
172
|
+
private wsHandler?;
|
173
|
+
private actionHandlers;
|
174
|
+
modules: Map<string, ModuleInfo>;
|
175
|
+
readonly fetch: typeof fetch;
|
176
|
+
options: MicroserviceOptions;
|
177
|
+
cache: LRUCache<string, any>;
|
178
|
+
serviceId: string;
|
179
|
+
constructor(options: MicroserviceOptions);
|
180
|
+
private initialize;
|
181
|
+
private initModules;
|
182
|
+
private initRoutes;
|
183
|
+
private generateClientCode;
|
184
|
+
private clientCode;
|
185
|
+
private initEtcd;
|
186
|
+
/**
|
187
|
+
* 注册服务
|
188
|
+
*/
|
189
|
+
private registerService;
|
190
|
+
/**
|
191
|
+
* 更新方法统计信息
|
192
|
+
*/
|
193
|
+
updateMethodStats(moduleName: string, methodName: string, responseTime: number, success: boolean, cacheHit?: boolean): void;
|
194
|
+
/**
|
195
|
+
* 获取 Hono 应用实例
|
196
|
+
*/
|
197
|
+
getApp(): Hono;
|
198
|
+
/**
|
199
|
+
* 启动服务
|
200
|
+
*/
|
201
|
+
start(port?: number, silent?: boolean): Promise<void>;
|
202
|
+
/**
|
203
|
+
* 获取所有模块的元数据
|
204
|
+
*/
|
205
|
+
private getModules;
|
206
|
+
/**
|
207
|
+
* 停止服务
|
208
|
+
*/
|
209
|
+
stop(): Promise<void>;
|
210
|
+
/**
|
211
|
+
* 初始化停机处理
|
212
|
+
*/
|
213
|
+
private initShutdownHandlers;
|
214
|
+
/**
|
215
|
+
* 优雅停机
|
216
|
+
*/
|
217
|
+
private gracefulShutdown;
|
218
|
+
private initStatsEventManager;
|
219
|
+
private updateStats;
|
220
|
+
getActionHandler(moduleName: string, actionName: string): ActionHandler;
|
221
|
+
private handleRequest;
|
222
|
+
init(): Promise<void>;
|
223
|
+
/**
|
224
|
+
* 获取所有注册的服务
|
225
|
+
*/
|
226
|
+
getServices(): Promise<ServiceInfo[]>;
|
227
|
+
/**
|
228
|
+
* 获取指定服务的实例
|
229
|
+
*/
|
230
|
+
getServiceInstances(serviceName: string): Promise<ServiceInfo[]>;
|
231
|
+
}
|
232
|
+
|
233
|
+
interface PreStartChecker {
|
234
|
+
name: string;
|
235
|
+
check: () => Promise<void> | void;
|
236
|
+
skip?: boolean;
|
237
|
+
}
|
238
|
+
declare function startCheck(checkers: PreStartChecker[], pass?: () => void | Promise<void>): Promise<void>;
|
239
|
+
|
240
|
+
/**
|
241
|
+
* 用于给微服务模块方法添加的注解
|
242
|
+
*/
|
243
|
+
declare function Action(options: ActionOptions): Function;
|
244
|
+
|
245
|
+
/**
|
246
|
+
* 用于标注一个类是微服务模块的装饰器
|
247
|
+
* @param name 模块名称
|
248
|
+
* @param options 模块配置选项
|
249
|
+
*/
|
250
|
+
declare function Module(name: string, options?: ModuleOptions): Function;
|
251
|
+
|
252
|
+
/**
|
253
|
+
* 用于给微服务模块方法添加的调度注解
|
254
|
+
*/
|
255
|
+
declare function Schedule(options: ScheduleOptions): Function;
|
256
|
+
|
257
|
+
declare const logger: winston.Logger;
|
258
|
+
|
259
|
+
export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, type EtcdConfig, type EventServiceInfo, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, type PreStartChecker, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
|
package/dist/mod.d.ts
ADDED
@@ -0,0 +1,259 @@
|
|
1
|
+
import { z } from 'zod';
|
2
|
+
export * from 'zod';
|
3
|
+
import { Lease, Etcd3 } from 'etcd3';
|
4
|
+
import { Hono } from 'hono';
|
5
|
+
import { LRUCache } from 'lru-cache';
|
6
|
+
export { default as dayjs } from 'dayjs';
|
7
|
+
import winston from 'winston';
|
8
|
+
|
9
|
+
interface ActionOptions {
|
10
|
+
params?: z.ZodType<any>[];
|
11
|
+
returns?: z.ZodType<any>;
|
12
|
+
idempotence?: boolean;
|
13
|
+
description?: string;
|
14
|
+
printError?: boolean;
|
15
|
+
cache?: boolean;
|
16
|
+
ttl?: number;
|
17
|
+
stream?: boolean;
|
18
|
+
}
|
19
|
+
interface ActionMetadata extends ActionOptions {
|
20
|
+
name: string;
|
21
|
+
idempotence: boolean;
|
22
|
+
cache?: boolean;
|
23
|
+
ttl?: number;
|
24
|
+
stream?: boolean;
|
25
|
+
}
|
26
|
+
interface ModuleOptions {
|
27
|
+
/** 模块名称 */
|
28
|
+
name?: string;
|
29
|
+
/** 模块版本 */
|
30
|
+
version?: string;
|
31
|
+
/** 模块描述 */
|
32
|
+
description?: string;
|
33
|
+
/** 打印错误 */
|
34
|
+
printError?: boolean;
|
35
|
+
}
|
36
|
+
interface ModuleMetadata {
|
37
|
+
name: string;
|
38
|
+
options: Required<ModuleOptions>;
|
39
|
+
}
|
40
|
+
declare enum ScheduleMode {
|
41
|
+
/** 固定间隔触发,不管上次任务是否完成 */
|
42
|
+
FIXED_RATE = "FIXED_RATE",
|
43
|
+
/** 任务完成后再等待间隔时间,再执行下一次 */
|
44
|
+
FIXED_DELAY = "FIXED_DELAY"
|
45
|
+
}
|
46
|
+
interface ScheduleOptions {
|
47
|
+
/** 执行间隔(毫秒) */
|
48
|
+
interval: number;
|
49
|
+
/** 调度模式 */
|
50
|
+
mode?: ScheduleMode;
|
51
|
+
}
|
52
|
+
interface ScheduleMetadata extends ScheduleOptions {
|
53
|
+
name: string;
|
54
|
+
electionKey: string;
|
55
|
+
}
|
56
|
+
interface EtcdConfig {
|
57
|
+
hosts: string[];
|
58
|
+
auth?: {
|
59
|
+
username: string;
|
60
|
+
password: string;
|
61
|
+
};
|
62
|
+
/** 服务注册的 TTL,默认 10 秒 */
|
63
|
+
ttl?: number;
|
64
|
+
/** etcd 的命名空间 */
|
65
|
+
namespace?: string;
|
66
|
+
}
|
67
|
+
interface ServiceStats {
|
68
|
+
totalCalls: number;
|
69
|
+
successCalls: number;
|
70
|
+
failureCalls: number;
|
71
|
+
avgResponseTime: number;
|
72
|
+
maxResponseTime: number;
|
73
|
+
minResponseTime: number;
|
74
|
+
lastUpdateTime: number;
|
75
|
+
cacheHit: number;
|
76
|
+
}
|
77
|
+
interface StatisticsEventOptions {
|
78
|
+
/** 统计事件触发间隔,默认 5 秒 */
|
79
|
+
interval?: number;
|
80
|
+
/** 是否在服务停止时强制触发未发送的统计事件 */
|
81
|
+
forceEmitOnShutdown?: boolean;
|
82
|
+
}
|
83
|
+
interface ServiceInfo {
|
84
|
+
id: string;
|
85
|
+
name: string;
|
86
|
+
version?: string;
|
87
|
+
prefix?: string;
|
88
|
+
env?: string;
|
89
|
+
modules: Record<string, ModuleInfo>;
|
90
|
+
}
|
91
|
+
interface EventServiceInfo {
|
92
|
+
id: string;
|
93
|
+
name: string;
|
94
|
+
version?: string;
|
95
|
+
env?: string;
|
96
|
+
}
|
97
|
+
interface StatisticsEvent {
|
98
|
+
service: EventServiceInfo;
|
99
|
+
module: string;
|
100
|
+
action: string;
|
101
|
+
stats: ServiceStats;
|
102
|
+
startTime: number;
|
103
|
+
endTime: number;
|
104
|
+
}
|
105
|
+
interface ActionErrorEvent {
|
106
|
+
service: EventServiceInfo;
|
107
|
+
module: string;
|
108
|
+
action: string;
|
109
|
+
requestHash: string;
|
110
|
+
params: any[];
|
111
|
+
time: number;
|
112
|
+
error: string;
|
113
|
+
}
|
114
|
+
interface MicroserviceOptions {
|
115
|
+
name?: string;
|
116
|
+
version?: string;
|
117
|
+
prefix?: string;
|
118
|
+
env?: string;
|
119
|
+
printError?: boolean;
|
120
|
+
disableCache?: boolean;
|
121
|
+
modules: (new () => any)[];
|
122
|
+
generateClient?: URL | boolean;
|
123
|
+
etcd?: EtcdConfig;
|
124
|
+
events?: StatisticsEventOptions & {
|
125
|
+
onStats?: (event: StatisticsEvent) => void | Promise<void>;
|
126
|
+
onRegister?: (serviceInfo: ServiceInfo) => void | Promise<void>;
|
127
|
+
onError?: (event: ActionErrorEvent) => void | Promise<void>;
|
128
|
+
};
|
129
|
+
websocket?: {
|
130
|
+
enabled?: boolean;
|
131
|
+
timeout?: number;
|
132
|
+
};
|
133
|
+
}
|
134
|
+
interface ModuleInfo extends ModuleOptions {
|
135
|
+
actions: Record<string, ActionMetadata>;
|
136
|
+
name: string;
|
137
|
+
}
|
138
|
+
interface StreamResponse<T = any> {
|
139
|
+
value?: T;
|
140
|
+
done: boolean;
|
141
|
+
error?: string;
|
142
|
+
}
|
143
|
+
|
144
|
+
declare class ActionHandler {
|
145
|
+
private moduleInstance;
|
146
|
+
private actionName;
|
147
|
+
metadata: ActionMetadata;
|
148
|
+
private microservice;
|
149
|
+
private moduleName;
|
150
|
+
constructor(moduleInstance: any, actionName: string, metadata: ActionMetadata, microservice: Microservice, moduleName: string);
|
151
|
+
handle(req: string | any[]): Promise<any>;
|
152
|
+
}
|
153
|
+
|
154
|
+
declare const ServiceContext: {
|
155
|
+
service?: Microservice;
|
156
|
+
lease?: Lease;
|
157
|
+
etcdClient?: Etcd3;
|
158
|
+
};
|
159
|
+
declare class Microservice {
|
160
|
+
private app;
|
161
|
+
private nodeWebSocket;
|
162
|
+
private codeCache?;
|
163
|
+
private waitingInitialization;
|
164
|
+
private etcdClient?;
|
165
|
+
private serviceKey?;
|
166
|
+
private statsMap;
|
167
|
+
private lease?;
|
168
|
+
private scheduler?;
|
169
|
+
private abortController?;
|
170
|
+
private isShuttingDown;
|
171
|
+
private statisticsTimer?;
|
172
|
+
private wsHandler?;
|
173
|
+
private actionHandlers;
|
174
|
+
modules: Map<string, ModuleInfo>;
|
175
|
+
readonly fetch: typeof fetch;
|
176
|
+
options: MicroserviceOptions;
|
177
|
+
cache: LRUCache<string, any>;
|
178
|
+
serviceId: string;
|
179
|
+
constructor(options: MicroserviceOptions);
|
180
|
+
private initialize;
|
181
|
+
private initModules;
|
182
|
+
private initRoutes;
|
183
|
+
private generateClientCode;
|
184
|
+
private clientCode;
|
185
|
+
private initEtcd;
|
186
|
+
/**
|
187
|
+
* 注册服务
|
188
|
+
*/
|
189
|
+
private registerService;
|
190
|
+
/**
|
191
|
+
* 更新方法统计信息
|
192
|
+
*/
|
193
|
+
updateMethodStats(moduleName: string, methodName: string, responseTime: number, success: boolean, cacheHit?: boolean): void;
|
194
|
+
/**
|
195
|
+
* 获取 Hono 应用实例
|
196
|
+
*/
|
197
|
+
getApp(): Hono;
|
198
|
+
/**
|
199
|
+
* 启动服务
|
200
|
+
*/
|
201
|
+
start(port?: number, silent?: boolean): Promise<void>;
|
202
|
+
/**
|
203
|
+
* 获取所有模块的元数据
|
204
|
+
*/
|
205
|
+
private getModules;
|
206
|
+
/**
|
207
|
+
* 停止服务
|
208
|
+
*/
|
209
|
+
stop(): Promise<void>;
|
210
|
+
/**
|
211
|
+
* 初始化停机处理
|
212
|
+
*/
|
213
|
+
private initShutdownHandlers;
|
214
|
+
/**
|
215
|
+
* 优雅停机
|
216
|
+
*/
|
217
|
+
private gracefulShutdown;
|
218
|
+
private initStatsEventManager;
|
219
|
+
private updateStats;
|
220
|
+
getActionHandler(moduleName: string, actionName: string): ActionHandler;
|
221
|
+
private handleRequest;
|
222
|
+
init(): Promise<void>;
|
223
|
+
/**
|
224
|
+
* 获取所有注册的服务
|
225
|
+
*/
|
226
|
+
getServices(): Promise<ServiceInfo[]>;
|
227
|
+
/**
|
228
|
+
* 获取指定服务的实例
|
229
|
+
*/
|
230
|
+
getServiceInstances(serviceName: string): Promise<ServiceInfo[]>;
|
231
|
+
}
|
232
|
+
|
233
|
+
interface PreStartChecker {
|
234
|
+
name: string;
|
235
|
+
check: () => Promise<void> | void;
|
236
|
+
skip?: boolean;
|
237
|
+
}
|
238
|
+
declare function startCheck(checkers: PreStartChecker[], pass?: () => void | Promise<void>): Promise<void>;
|
239
|
+
|
240
|
+
/**
|
241
|
+
* 用于给微服务模块方法添加的注解
|
242
|
+
*/
|
243
|
+
declare function Action(options: ActionOptions): Function;
|
244
|
+
|
245
|
+
/**
|
246
|
+
* 用于标注一个类是微服务模块的装饰器
|
247
|
+
* @param name 模块名称
|
248
|
+
* @param options 模块配置选项
|
249
|
+
*/
|
250
|
+
declare function Module(name: string, options?: ModuleOptions): Function;
|
251
|
+
|
252
|
+
/**
|
253
|
+
* 用于给微服务模块方法添加的调度注解
|
254
|
+
*/
|
255
|
+
declare function Schedule(options: ScheduleOptions): Function;
|
256
|
+
|
257
|
+
declare const logger: winston.Logger;
|
258
|
+
|
259
|
+
export { Action, type ActionErrorEvent, type ActionMetadata, type ActionOptions, type EtcdConfig, type EventServiceInfo, Microservice, type MicroserviceOptions, Module, type ModuleInfo, type ModuleMetadata, type ModuleOptions, type PreStartChecker, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, ServiceContext, type ServiceInfo, type ServiceStats, type StatisticsEvent, type StreamResponse, logger, startCheck };
|