@bakit/service 3.2.1 → 4.0.1
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/index.cjs +757 -0
- package/dist/index.d.cts +323 -0
- package/dist/index.d.ts +274 -207
- package/dist/index.js +653 -349
- package/dist/service.cjs +27 -0
- package/dist/service.js +25 -0
- package/package.json +10 -6
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import EventEmitter from 'node:events';
|
|
2
|
+
import * as _bakit_utils from '@bakit/utils';
|
|
3
|
+
import { Awaitable, Collection, FunctionLike, Promisify } from '@bakit/utils';
|
|
4
|
+
import { Socket } from 'node:net';
|
|
5
|
+
|
|
6
|
+
type Serializable =
|
|
7
|
+
| string
|
|
8
|
+
| number
|
|
9
|
+
| boolean
|
|
10
|
+
| null
|
|
11
|
+
| undefined
|
|
12
|
+
| Serializable[]
|
|
13
|
+
| { [key: string]: Serializable };
|
|
14
|
+
|
|
15
|
+
interface RPCResponseError {
|
|
16
|
+
message: string;
|
|
17
|
+
name: string;
|
|
18
|
+
constructorName: string;
|
|
19
|
+
stack?: string;
|
|
20
|
+
cause?: RPCResponseError;
|
|
21
|
+
errors?: RPCResponseError[]; // For AggregateError
|
|
22
|
+
[key: string]: unknown;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
declare const SUPPORTED_LENGTH_BYTES: readonly [1, 2, 4, 8];
|
|
26
|
+
interface FrameCodecOptions {
|
|
27
|
+
maxBufferSize?: number;
|
|
28
|
+
maxFrameSize?: number;
|
|
29
|
+
lengthBytes?: (typeof SUPPORTED_LENGTH_BYTES)[number];
|
|
30
|
+
endian?: "big" | "little";
|
|
31
|
+
lengthIncludesHeader?: boolean;
|
|
32
|
+
}
|
|
33
|
+
declare class FrameCodec {
|
|
34
|
+
private buffer;
|
|
35
|
+
readonly options: Required<FrameCodecOptions>;
|
|
36
|
+
stats: {
|
|
37
|
+
bytesReceived: number;
|
|
38
|
+
bytesDecoded: number;
|
|
39
|
+
framesDecoded: number;
|
|
40
|
+
bufferCopies: number;
|
|
41
|
+
};
|
|
42
|
+
constructor(options?: FrameCodecOptions);
|
|
43
|
+
push(chunk: Buffer): Buffer[];
|
|
44
|
+
get bufferedBytes(): number;
|
|
45
|
+
reset(): void;
|
|
46
|
+
private readLength;
|
|
47
|
+
encode(payload: Buffer): Buffer<ArrayBufferLike>;
|
|
48
|
+
static encode(payload: Buffer, options?: FrameCodecOptions): Buffer;
|
|
49
|
+
static serialize(obj: Serializable): Buffer;
|
|
50
|
+
static deserialize(buf: Buffer): Serializable;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type EventMap<T> = Record<keyof T, any[]> | DefaultEventMap;
|
|
54
|
+
type DefaultEventMap = [never];
|
|
55
|
+
interface BaseClientDriverEvents {
|
|
56
|
+
message: [message: Serializable];
|
|
57
|
+
connect: [];
|
|
58
|
+
disconnect: [];
|
|
59
|
+
error: [error: Error];
|
|
60
|
+
}
|
|
61
|
+
interface BaseServerDriverEvents<Connection = unknown> {
|
|
62
|
+
listen: [];
|
|
63
|
+
close: [];
|
|
64
|
+
error: [error: Error];
|
|
65
|
+
message: [connection: Connection, message: Serializable];
|
|
66
|
+
connectionAdd: [connection: Connection];
|
|
67
|
+
connectionRemove: [connection: Connection];
|
|
68
|
+
connectionError: [connection: Connection, error: Error];
|
|
69
|
+
}
|
|
70
|
+
declare abstract class BaseDriver<Options extends object, Events extends EventMap<Events>> extends EventEmitter<Events> {
|
|
71
|
+
options: Options;
|
|
72
|
+
constructor(options: Options);
|
|
73
|
+
}
|
|
74
|
+
declare abstract class BaseClientDriver<Options extends object = object, Events extends EventMap<Events> & BaseClientDriverEvents = BaseClientDriverEvents> extends BaseDriver<Options, Events> {
|
|
75
|
+
constructor(options: Options);
|
|
76
|
+
abstract readonly ready: boolean;
|
|
77
|
+
abstract send(message: Serializable): Awaitable<void>;
|
|
78
|
+
abstract connect(): Awaitable<void>;
|
|
79
|
+
abstract disconnect(): Awaitable<void>;
|
|
80
|
+
}
|
|
81
|
+
declare abstract class BaseServerDriver<Options extends object = object, Connection = unknown, Events extends EventMap<Events> & BaseServerDriverEvents<Connection> = BaseServerDriverEvents<Connection>> extends BaseDriver<Options, Events> {
|
|
82
|
+
constructor(options: Options);
|
|
83
|
+
readonly _connectionType: Connection;
|
|
84
|
+
abstract readonly connections: Set<Connection> | Map<unknown, Connection> | Connection[];
|
|
85
|
+
abstract listen(): Awaitable<void>;
|
|
86
|
+
abstract close(): Awaitable<void>;
|
|
87
|
+
abstract send(connection: Connection, message: Serializable): Awaitable<void>;
|
|
88
|
+
abstract broadcast(message: Serializable): Awaitable<unknown>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface IPCClientOptions {
|
|
92
|
+
id: string;
|
|
93
|
+
codec?: FrameCodecOptions;
|
|
94
|
+
reconnect?: IPCClientReconnectOptions;
|
|
95
|
+
}
|
|
96
|
+
interface IPCClientReconnectOptions {
|
|
97
|
+
/** Enable auto-reconnect (default: true) */
|
|
98
|
+
enabled?: boolean;
|
|
99
|
+
/** Max retry attempts (default: 5) */
|
|
100
|
+
maxRetries?: number;
|
|
101
|
+
/** Initial delay in ms (default: 1000) */
|
|
102
|
+
initialDelay?: number;
|
|
103
|
+
/** Backoff multiplier (default: 1.5) */
|
|
104
|
+
backoff?: number;
|
|
105
|
+
/** Max delay in ms (default: 30000) */
|
|
106
|
+
maxDelay?: number;
|
|
107
|
+
}
|
|
108
|
+
interface IPCClientEvents extends BaseClientDriverEvents {
|
|
109
|
+
reconnect: [];
|
|
110
|
+
reconnecting: [attempt: number, delay: number];
|
|
111
|
+
}
|
|
112
|
+
declare enum IPCClientState {
|
|
113
|
+
Idle = 0,
|
|
114
|
+
Connecting = 1,
|
|
115
|
+
Connected = 2,
|
|
116
|
+
Disconnected = 3
|
|
117
|
+
}
|
|
118
|
+
declare const DEFAULT_IPC_CLIENT_RECONNECT_OPTIONS: Required<IPCClientReconnectOptions>;
|
|
119
|
+
declare class IPCClient extends BaseClientDriver<IPCClientOptions, IPCClientEvents> {
|
|
120
|
+
private socket?;
|
|
121
|
+
private codec;
|
|
122
|
+
state: IPCClientState;
|
|
123
|
+
private _ready;
|
|
124
|
+
private reconnectTimer?;
|
|
125
|
+
private reconnectOptions;
|
|
126
|
+
private reconnectAttempt;
|
|
127
|
+
private isIntentionalClose;
|
|
128
|
+
private queue;
|
|
129
|
+
constructor(options: IPCClientOptions);
|
|
130
|
+
get path(): string;
|
|
131
|
+
get ready(): boolean;
|
|
132
|
+
connect(): Promise<void>;
|
|
133
|
+
send(message: Serializable): Promise<void>;
|
|
134
|
+
disconnect(): Promise<void>;
|
|
135
|
+
private init;
|
|
136
|
+
private cleanup;
|
|
137
|
+
private onSocketConnect;
|
|
138
|
+
private onSocketClose;
|
|
139
|
+
private onSocketError;
|
|
140
|
+
private onSocketData;
|
|
141
|
+
private scheduleReconnect;
|
|
142
|
+
private clearReconnectTimer;
|
|
143
|
+
private makeMessage;
|
|
144
|
+
private serialize;
|
|
145
|
+
private deserialize;
|
|
146
|
+
}
|
|
147
|
+
declare function createIPCClient(options: IPCClientOptions): IPCClient;
|
|
148
|
+
|
|
149
|
+
interface IPCConnectionEvents {
|
|
150
|
+
message: [message: Serializable];
|
|
151
|
+
close: [];
|
|
152
|
+
error: [error: Error];
|
|
153
|
+
}
|
|
154
|
+
declare class IPCConnection extends EventEmitter<IPCConnectionEvents> {
|
|
155
|
+
server: IPCServer;
|
|
156
|
+
socket: Socket;
|
|
157
|
+
private codec;
|
|
158
|
+
constructor(server: IPCServer, socket: Socket);
|
|
159
|
+
send(message: Serializable): Promise<void>;
|
|
160
|
+
destroy(): void;
|
|
161
|
+
private setupListeners;
|
|
162
|
+
private handleData;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
interface IPCServerOptions {
|
|
166
|
+
id: string;
|
|
167
|
+
codec?: FrameCodecOptions;
|
|
168
|
+
}
|
|
169
|
+
type IPCServerEvents = BaseServerDriverEvents<IPCConnection>;
|
|
170
|
+
declare class IPCServer extends BaseServerDriver<IPCServerOptions, IPCConnection, IPCServerEvents> {
|
|
171
|
+
private server?;
|
|
172
|
+
private codecOptions;
|
|
173
|
+
connections: Collection<Socket, IPCConnection>;
|
|
174
|
+
codec: FrameCodec;
|
|
175
|
+
constructor(options: IPCServerOptions);
|
|
176
|
+
get path(): string;
|
|
177
|
+
listen(): Promise<void>;
|
|
178
|
+
private handleConnection;
|
|
179
|
+
send(connection: IPCConnection, message: Serializable): Awaitable<void>;
|
|
180
|
+
/**
|
|
181
|
+
* Send message to a specific client
|
|
182
|
+
*/
|
|
183
|
+
sendSocket(socket: Socket, message: Serializable): Promise<void>;
|
|
184
|
+
/**
|
|
185
|
+
* Broadcast to all connected clients
|
|
186
|
+
* Returns count of successful sends (fire-and-forget, errors emitted via 'clientError')
|
|
187
|
+
*/
|
|
188
|
+
broadcast(message: Serializable): Promise<void>;
|
|
189
|
+
/**
|
|
190
|
+
* Close server and disconnect all clients
|
|
191
|
+
*/
|
|
192
|
+
close(): Promise<void>;
|
|
193
|
+
private sendSocketFrame;
|
|
194
|
+
}
|
|
195
|
+
declare function createIPCServer(options: IPCServerOptions): IPCServer;
|
|
196
|
+
|
|
197
|
+
interface TransportClientEvents {
|
|
198
|
+
connect: [];
|
|
199
|
+
disconnect: [];
|
|
200
|
+
error: [error: Error];
|
|
201
|
+
message: [message: Serializable];
|
|
202
|
+
}
|
|
203
|
+
declare class TransportClient<D extends BaseClientDriver> extends EventEmitter<TransportClientEvents> {
|
|
204
|
+
readonly driver: D;
|
|
205
|
+
private pending;
|
|
206
|
+
constructor(driver: D);
|
|
207
|
+
connect(): _bakit_utils.Awaitable<void>;
|
|
208
|
+
disconnect(): _bakit_utils.Awaitable<void>;
|
|
209
|
+
request<Result extends Serializable>(method: string, ...args: Serializable[]): Promise<Result>;
|
|
210
|
+
send(message: Serializable): _bakit_utils.Awaitable<void>;
|
|
211
|
+
private setupDriverListeners;
|
|
212
|
+
private handleMessage;
|
|
213
|
+
private hydrateError;
|
|
214
|
+
private captureCallStack;
|
|
215
|
+
private isResponseMessage;
|
|
216
|
+
}
|
|
217
|
+
declare function createTransportClient<D extends BaseClientDriver<object, any>>(driver: D): TransportClient<D>;
|
|
218
|
+
|
|
219
|
+
type RPCHandler = (...args: Serializable[]) => Promise<Serializable | Error>;
|
|
220
|
+
interface TransportServerEvents<D extends BaseServerDriver> {
|
|
221
|
+
listen: [];
|
|
222
|
+
close: [];
|
|
223
|
+
error: [error: Error];
|
|
224
|
+
connectionAdd: [connection: D["_connectionType"]];
|
|
225
|
+
connectionRemove: [connection: D["_connectionType"]];
|
|
226
|
+
connectionError: [connection: D["_connectionType"], error: Error];
|
|
227
|
+
message: [connection: D["_connectionType"], message: Serializable];
|
|
228
|
+
}
|
|
229
|
+
declare class TransportServer<D extends BaseServerDriver> extends EventEmitter<TransportServerEvents<D>> {
|
|
230
|
+
readonly driver: D;
|
|
231
|
+
private handlers;
|
|
232
|
+
constructor(driver: D);
|
|
233
|
+
get connections(): D["connections"];
|
|
234
|
+
handle(method: string, handler: RPCHandler): void;
|
|
235
|
+
listen(): ReturnType<D["listen"]>;
|
|
236
|
+
close(): ReturnType<D["close"]>;
|
|
237
|
+
broadcast(message: Serializable): ReturnType<D["broadcast"]>;
|
|
238
|
+
private setupDriverListeners;
|
|
239
|
+
private handleMessage;
|
|
240
|
+
private isRequestMessage;
|
|
241
|
+
}
|
|
242
|
+
declare function createTransportServer<D extends BaseServerDriver<object, any>>(driver: D): TransportServer<D>;
|
|
243
|
+
|
|
244
|
+
declare enum ServiceDriver {
|
|
245
|
+
IPC = "ipc"
|
|
246
|
+
}
|
|
247
|
+
interface DriverOptionsMap {
|
|
248
|
+
[ServiceDriver.IPC]: {
|
|
249
|
+
id: string;
|
|
250
|
+
server?: Omit<IPCServerOptions, "id">;
|
|
251
|
+
client?: Omit<IPCClientOptions, "id">;
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
type ServiceOptions = {
|
|
255
|
+
[D in keyof DriverOptionsMap]: {
|
|
256
|
+
driver: D;
|
|
257
|
+
} & DriverOptionsMap[D];
|
|
258
|
+
}[keyof DriverOptionsMap];
|
|
259
|
+
interface ServiceClientRuntime {
|
|
260
|
+
role: "client";
|
|
261
|
+
transport: TransportClient<BaseClientDriver>;
|
|
262
|
+
}
|
|
263
|
+
interface ServiceServerRuntime {
|
|
264
|
+
role: "server";
|
|
265
|
+
transport: TransportServer<BaseServerDriver>;
|
|
266
|
+
}
|
|
267
|
+
declare class Service {
|
|
268
|
+
readonly options: ServiceOptions;
|
|
269
|
+
private runtime?;
|
|
270
|
+
private handlers;
|
|
271
|
+
private binding?;
|
|
272
|
+
constructor(options: ServiceOptions);
|
|
273
|
+
get role(): "client" | "server";
|
|
274
|
+
define<F extends FunctionLike>(method: string, handler: F): Promisify<F>;
|
|
275
|
+
protected ensureServerBound(): Promise<void> | undefined;
|
|
276
|
+
protected ensureClientBound(): Promise<void> | undefined;
|
|
277
|
+
}
|
|
278
|
+
declare function createService(options: ServiceOptions): Service;
|
|
279
|
+
declare function getServices(entryDir?: string, cwd?: string): Promise<string[]>;
|
|
280
|
+
declare function getServiceName(servicePath: string, cwd?: string): string;
|
|
281
|
+
|
|
282
|
+
interface ServiceProcessEvents {
|
|
283
|
+
stdout: [chunk: Buffer];
|
|
284
|
+
stderr: [chunk: Buffer];
|
|
285
|
+
spawn: [];
|
|
286
|
+
error: [err: Error];
|
|
287
|
+
exit: [code: number | null, signal: NodeJS.Signals | null];
|
|
288
|
+
}
|
|
289
|
+
declare enum ServiceState {
|
|
290
|
+
Idle = 0,
|
|
291
|
+
Starting = 1,
|
|
292
|
+
Running = 2,
|
|
293
|
+
Restarting = 3,
|
|
294
|
+
Stopping = 4,
|
|
295
|
+
Stopped = 5
|
|
296
|
+
}
|
|
297
|
+
declare class ServiceProcess extends EventEmitter<ServiceProcessEvents> {
|
|
298
|
+
readonly path: string;
|
|
299
|
+
private child?;
|
|
300
|
+
private spawnTimestamp;
|
|
301
|
+
private _state;
|
|
302
|
+
constructor(path: string);
|
|
303
|
+
get name(): string;
|
|
304
|
+
get state(): ServiceState;
|
|
305
|
+
get ready(): boolean;
|
|
306
|
+
get uptime(): number;
|
|
307
|
+
start(): void;
|
|
308
|
+
stop(): Promise<void>;
|
|
309
|
+
restart(): Promise<void>;
|
|
310
|
+
private init;
|
|
311
|
+
private cleanup;
|
|
312
|
+
private onChildSpawn;
|
|
313
|
+
private onChildExit;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
declare function getIPCPath(id: string, platform?: NodeJS.Platform): string;
|
|
317
|
+
declare function isServerRunning(path: string): Promise<boolean>;
|
|
318
|
+
|
|
319
|
+
declare function serializeRPCError(err: Error): RPCResponseError;
|
|
320
|
+
declare function createDynamicRPCError(data: RPCResponseError): Error;
|
|
321
|
+
declare function isSerializedError(value: unknown): value is RPCResponseError;
|
|
322
|
+
|
|
323
|
+
export { BaseClientDriver, type BaseClientDriverEvents, BaseServerDriver, type BaseServerDriverEvents, DEFAULT_IPC_CLIENT_RECONNECT_OPTIONS, FrameCodec, type FrameCodecOptions, IPCClient, type IPCClientEvents, type IPCClientOptions, type IPCClientReconnectOptions, IPCClientState, IPCServer, type IPCServerEvents, type IPCServerOptions, type RPCHandler, Service, type ServiceClientRuntime, ServiceDriver, type ServiceOptions, ServiceProcess, type ServiceProcessEvents, type ServiceServerRuntime, ServiceState, TransportClient, type TransportClientEvents, TransportServer, type TransportServerEvents, createDynamicRPCError, createIPCClient, createIPCServer, createService, createTransportClient, createTransportServer, getIPCPath, getServiceName, getServices, isSerializedError, isServerRunning, serializeRPCError };
|