@bakit/service 2.0.0 → 3.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/dist/index.d.ts +33 -27
- package/dist/index.js +53 -35
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -39,6 +39,8 @@ interface BaseClientDriverEvents {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
interface BaseServerDriverEvents {
|
|
42
|
+
listen: [];
|
|
43
|
+
close: [];
|
|
42
44
|
message: [connection: unknown, message: Serializable];
|
|
43
45
|
clientConnect: [connection: unknown];
|
|
44
46
|
clientDisconnect: [connection: unknown];
|
|
@@ -66,8 +68,6 @@ interface IPCServerEvents extends BaseServerDriverEvents {
|
|
|
66
68
|
clientDisconnect: [socket: Socket];
|
|
67
69
|
clientError: [socket: Socket, error: Error];
|
|
68
70
|
drain: [socket: Socket];
|
|
69
|
-
listen: [];
|
|
70
|
-
close: [];
|
|
71
71
|
}
|
|
72
72
|
interface IPCClientEvents extends BaseClientDriverEvents {
|
|
73
73
|
}
|
|
@@ -162,12 +162,12 @@ type TransportServerOptions = {
|
|
|
162
162
|
driver: D;
|
|
163
163
|
} & TransportServerDriverSpecificOptions[D];
|
|
164
164
|
}[TransportDriver];
|
|
165
|
-
interface TransportClient extends TransportClientProtocol, EventBus<
|
|
165
|
+
interface TransportClient extends TransportClientProtocol, EventBus<TransportClientEvents> {
|
|
166
166
|
send: BaseServerDriver["send"];
|
|
167
167
|
connect: BaseClientDriver["connect"];
|
|
168
168
|
disconnect: BaseClientDriver["disconnect"];
|
|
169
169
|
}
|
|
170
|
-
interface TransportServer extends TransportServerProtocol, EventBus<
|
|
170
|
+
interface TransportServer extends TransportServerProtocol, EventBus<TransportServerEvents> {
|
|
171
171
|
broadcast: BaseServerDriver["broadcast"];
|
|
172
172
|
listen: BaseServerDriver["listen"];
|
|
173
173
|
close: BaseServerDriver["close"];
|
|
@@ -193,13 +193,15 @@ type RPCResponseMessage = {
|
|
|
193
193
|
}, {
|
|
194
194
|
error: RPCErrorPayload;
|
|
195
195
|
}>;
|
|
196
|
-
interface
|
|
196
|
+
interface TransportClientEvents {
|
|
197
197
|
connect: [];
|
|
198
198
|
disconnect: [];
|
|
199
199
|
error: [error: Error];
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
200
|
+
}
|
|
201
|
+
interface TransportServerEvents {
|
|
202
|
+
listen: [];
|
|
203
|
+
close: [];
|
|
204
|
+
error: [error: Error];
|
|
203
205
|
clientConnect: [connection: unknown];
|
|
204
206
|
clientDisconnect: [connection: unknown];
|
|
205
207
|
}
|
|
@@ -212,28 +214,32 @@ interface ServiceOptions {
|
|
|
212
214
|
name: string;
|
|
213
215
|
transport: TransportClientOptions & TransportServerOptions;
|
|
214
216
|
}
|
|
215
|
-
interface ServiceServer {
|
|
216
|
-
define<F extends ServiceFunction>(method: string, handler: F): Promisify<F>;
|
|
217
|
-
transport: TransportServer;
|
|
218
|
-
}
|
|
219
|
-
interface ServiceClient {
|
|
220
|
-
define<F extends ServiceFunction>(method: string, handler: F): Promisify<F>;
|
|
221
|
-
transport: TransportClient;
|
|
222
|
-
}
|
|
223
217
|
interface Service {
|
|
218
|
+
readonly name: string;
|
|
219
|
+
readonly role: ServiceRole;
|
|
224
220
|
define<F extends ServiceFunction>(method: string, handler: F): Promisify<F>;
|
|
225
|
-
start(): void;
|
|
226
|
-
stop(): void;
|
|
227
221
|
}
|
|
228
222
|
type ServiceFunction = FunctionLike<any[], Awaitable<any>>;
|
|
223
|
+
declare const ServiceRole: {
|
|
224
|
+
readonly Client: "client";
|
|
225
|
+
readonly Server: "server";
|
|
226
|
+
readonly Unknown: "unknown";
|
|
227
|
+
};
|
|
228
|
+
type ServiceRole = ValueOf<typeof ServiceRole>;
|
|
229
|
+
type ServiceRuntime = {
|
|
230
|
+
role: "unknown";
|
|
231
|
+
} | {
|
|
232
|
+
role: "client";
|
|
233
|
+
transport: TransportClient;
|
|
234
|
+
} | {
|
|
235
|
+
role: "server";
|
|
236
|
+
transport: TransportServer;
|
|
237
|
+
};
|
|
238
|
+
interface ServiceInteral {
|
|
239
|
+
runtime: ServiceRuntime;
|
|
240
|
+
bind(role: "client" | "server"): void;
|
|
241
|
+
}
|
|
229
242
|
declare function createService(options: ServiceOptions): Service;
|
|
230
|
-
|
|
231
|
-
* Create a service client instance.
|
|
232
|
-
*
|
|
233
|
-
* @param {options} options.
|
|
234
|
-
* @return {ServiceClient} Service client instance.
|
|
235
|
-
*/
|
|
236
|
-
declare function createServiceClient(options: ServiceOptions): ServiceClient;
|
|
237
|
-
declare function createServiceServer(options: ServiceOptions): ServiceServer;
|
|
243
|
+
declare function getInteralService(service: Service): ServiceInteral;
|
|
238
244
|
|
|
239
|
-
export { type BaseClientDriver, type BaseClientDriverEvents, type BaseServerDriver, type BaseServerDriverEvents, DEFAULT_IPC_SOCKET_CONNECTION_OPTIONS, type IPCClientEvents, type IPCClientOptions, type IPCServer, type IPCServerEvents, type IPCServerOptions, IPCServerState, type IPCSocketConnection, type IPCSocketConnectionOptions, type IPCSocketMessageHandler, type IPCSocketMessageHandlerOptions, RPCError, type RPCErrorPayload, type RPCHandler, type RPCRequestMessage, type RPCResponseMessage, type Serializable, type Service, type
|
|
245
|
+
export { type BaseClientDriver, type BaseClientDriverEvents, type BaseServerDriver, type BaseServerDriverEvents, DEFAULT_IPC_SOCKET_CONNECTION_OPTIONS, type IPCClientEvents, type IPCClientOptions, type IPCServer, type IPCServerEvents, type IPCServerOptions, IPCServerState, type IPCSocketConnection, type IPCSocketConnectionOptions, type IPCSocketMessageHandler, type IPCSocketMessageHandlerOptions, RPCError, type RPCErrorPayload, type RPCHandler, type RPCRequestMessage, type RPCResponseMessage, type Serializable, type Service, type ServiceFunction, type ServiceInteral, type ServiceOptions, ServiceRole, type ServiceRuntime, SocketState, type TransportClient, type TransportClientDriverSpecificOptions, type TransportClientEvents, type TransportClientOptions, type TransportClientProtocol, TransportDriver, type TransportServer, type TransportServerDriverSpecificOptions, type TransportServerEvents, type TransportServerOptions, type TransportServerProtocol, createIPCClient, createIPCServer, createIPCSocketConnection, createIPCSocketMessageHandler, createService, createTransportClient, createTransportClientProtocol, createTransportServer, createTransportServerProtocol, deserializeRPCError, getIPCPath, getInteralService, serializeRPCError };
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { join } from 'path';
|
|
|
3
3
|
import { existsSync, rmSync } from 'fs';
|
|
4
4
|
import { pack, unpack } from 'msgpackr';
|
|
5
5
|
import PQueue from 'p-queue';
|
|
6
|
-
import { attachEventBus, Collection, isPlainObject
|
|
6
|
+
import { attachEventBus, Collection, isPlainObject } from '@bakit/utils';
|
|
7
7
|
import { randomUUID } from 'crypto';
|
|
8
8
|
|
|
9
9
|
// src/lib/driver/ipc.ts
|
|
@@ -255,7 +255,7 @@ function createTransportServer(options) {
|
|
|
255
255
|
listen: driver.listen,
|
|
256
256
|
close: driver.close
|
|
257
257
|
}, self = attachEventBus(base);
|
|
258
|
-
return driver.on("clientConnect", (conn) => self.emit("clientConnect", conn)), driver.on("clientDisconnect", (conn) => self.emit("clientDisconnect", conn)), driver.on("clientError", (_, error) => self.emit("error", error)), self;
|
|
258
|
+
return driver.on("listen", () => self.emit("listen")), driver.on("clientConnect", (conn) => self.emit("clientConnect", conn)), driver.on("clientDisconnect", (conn) => self.emit("clientDisconnect", conn)), driver.on("clientError", (_, error) => self.emit("error", error)), self;
|
|
259
259
|
}
|
|
260
260
|
function createTransportClientProtocol(driver) {
|
|
261
261
|
let requests = new Collection();
|
|
@@ -341,42 +341,60 @@ function createTransportServerProtocol(driver) {
|
|
|
341
341
|
handle
|
|
342
342
|
};
|
|
343
343
|
}
|
|
344
|
+
var ServiceRole = {
|
|
345
|
+
Client: "client",
|
|
346
|
+
Server: "server",
|
|
347
|
+
Unknown: "unknown"
|
|
348
|
+
}, SERVICE_INTERNAL = Symbol("service-internal");
|
|
344
349
|
function createService(options) {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
let client = createServiceClient(options);
|
|
354
|
-
return {
|
|
355
|
-
define: client.define,
|
|
356
|
-
start: client.transport.connect,
|
|
357
|
-
stop: client.transport.disconnect
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
function createServiceClient(options) {
|
|
362
|
-
let transport = createTransportClient(options.transport);
|
|
363
|
-
function define(method, _handler) {
|
|
364
|
-
return (...args) => transport.request(method, ...args);
|
|
365
|
-
}
|
|
366
|
-
return {
|
|
367
|
-
define,
|
|
368
|
-
transport
|
|
350
|
+
let handlers = new Collection(), runtime = { role: "unknown" }, service = {
|
|
351
|
+
get name() {
|
|
352
|
+
return options.name;
|
|
353
|
+
},
|
|
354
|
+
get role() {
|
|
355
|
+
return runtime.role;
|
|
356
|
+
},
|
|
357
|
+
define
|
|
369
358
|
};
|
|
370
|
-
}
|
|
371
|
-
function createServiceServer(options) {
|
|
372
|
-
let transport = createTransportServer(options.transport);
|
|
373
359
|
function define(method, handler) {
|
|
374
|
-
|
|
360
|
+
if (handlers.has(method))
|
|
361
|
+
throw new Error(`Service method "${method}" already defined`);
|
|
362
|
+
return handlers.set(method, handler), (async (...args) => {
|
|
363
|
+
if (runtime.role === "unknown")
|
|
364
|
+
throw new Error(`Service "${options.name}" is not bound (method "${method}")`);
|
|
365
|
+
return runtime.role === "server" ? handler(...args) : runtime.transport.request(method, ...args);
|
|
366
|
+
});
|
|
375
367
|
}
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
368
|
+
function bind(role) {
|
|
369
|
+
if (runtime.role !== "unknown")
|
|
370
|
+
throw new Error(`Service "${options.name}" already bound`);
|
|
371
|
+
if (role === "server") {
|
|
372
|
+
let server = createTransportServer(options.transport);
|
|
373
|
+
for (let [method, handler] of handlers)
|
|
374
|
+
server.handle(method, handler);
|
|
375
|
+
runtime = {
|
|
376
|
+
role,
|
|
377
|
+
transport: server
|
|
378
|
+
}, server.listen();
|
|
379
|
+
} else if (role === "client") {
|
|
380
|
+
let client = createTransportClient(options.transport);
|
|
381
|
+
runtime = {
|
|
382
|
+
role,
|
|
383
|
+
transport: client
|
|
384
|
+
}, client.connect();
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return Object.defineProperty(service, SERVICE_INTERNAL, {
|
|
388
|
+
value: {
|
|
389
|
+
get runtime() {
|
|
390
|
+
return runtime;
|
|
391
|
+
},
|
|
392
|
+
bind
|
|
393
|
+
}
|
|
394
|
+
}), service;
|
|
395
|
+
}
|
|
396
|
+
function getInteralService(service) {
|
|
397
|
+
return service[SERVICE_INTERNAL];
|
|
380
398
|
}
|
|
381
399
|
|
|
382
|
-
export { DEFAULT_IPC_SOCKET_CONNECTION_OPTIONS, IPCServerState, RPCError, SocketState, TransportDriver, createIPCClient, createIPCServer, createIPCSocketConnection, createIPCSocketMessageHandler, createService,
|
|
400
|
+
export { DEFAULT_IPC_SOCKET_CONNECTION_OPTIONS, IPCServerState, RPCError, ServiceRole, SocketState, TransportDriver, createIPCClient, createIPCServer, createIPCSocketConnection, createIPCSocketMessageHandler, createService, createTransportClient, createTransportClientProtocol, createTransportServer, createTransportServerProtocol, deserializeRPCError, getIPCPath, getInteralService, serializeRPCError };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bakit/service",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Service manager for bakit framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -28,14 +28,14 @@
|
|
|
28
28
|
"author": "louiszn",
|
|
29
29
|
"license": "MIT",
|
|
30
30
|
"dependencies": {
|
|
31
|
+
"p-queue": "^9.1.0",
|
|
31
32
|
"msgpackr": "^1.11.8",
|
|
32
|
-
"p-queue": "^9.0.1",
|
|
33
33
|
"type-fest": "^4.41.0",
|
|
34
|
-
"@bakit/utils": "^2.0.0
|
|
34
|
+
"@bakit/utils": "^2.0.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/ws": "^8.18.1",
|
|
38
|
-
"ws": "^8.
|
|
38
|
+
"ws": "^8.19.0"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "tsup",
|