@bakit/service 3.0.0 → 3.1.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 CHANGED
@@ -223,23 +223,26 @@ type ServiceFunction = FunctionLike<any[], Awaitable<any>>;
223
223
  declare const ServiceRole: {
224
224
  readonly Client: "client";
225
225
  readonly Server: "server";
226
- readonly Unknown: "unknown";
227
226
  };
228
227
  type ServiceRole = ValueOf<typeof ServiceRole>;
229
- type ServiceRuntime = {
230
- role: "unknown";
231
- } | {
232
- role: "client";
228
+ interface ServiceClientRuntime {
229
+ role: typeof ServiceRole.Client;
233
230
  transport: TransportClient;
234
- } | {
235
- role: "server";
231
+ }
232
+ interface ServiceServerRuntime {
233
+ role: typeof ServiceRole.Server;
236
234
  transport: TransportServer;
237
- };
235
+ }
236
+ type ServiceRuntime = ServiceClientRuntime | ServiceServerRuntime;
237
+ interface MutableRuntime {
238
+ role: ServiceRole | "unknown";
239
+ transport?: TransportClient | TransportServer;
240
+ }
238
241
  interface ServiceInteral {
239
242
  runtime: ServiceRuntime;
240
243
  bind(role: "client" | "server"): void;
241
244
  }
242
245
  declare function createService(options: ServiceOptions): Service;
243
- declare function getInteralService(service: Service): ServiceInteral;
246
+ declare function getInternalService(service: Service): ServiceInteral;
244
247
 
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 };
248
+ 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, type MutableRuntime, RPCError, type RPCErrorPayload, type RPCHandler, type RPCRequestMessage, type RPCResponseMessage, type Serializable, type Service, type ServiceClientRuntime, type ServiceFunction, type ServiceInteral, type ServiceOptions, ServiceRole, type ServiceRuntime, type ServiceServerRuntime, 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, getInternalService, serializeRPCError };
package/dist/index.js CHANGED
@@ -343,8 +343,7 @@ function createTransportServerProtocol(driver) {
343
343
  }
344
344
  var ServiceRole = {
345
345
  Client: "client",
346
- Server: "server",
347
- Unknown: "unknown"
346
+ Server: "server"
348
347
  }, SERVICE_INTERNAL = Symbol("service-internal");
349
348
  function createService(options) {
350
349
  let handlers = new Collection(), runtime = { role: "unknown" }, service = {
@@ -352,6 +351,8 @@ function createService(options) {
352
351
  return options.name;
353
352
  },
354
353
  get role() {
354
+ if (runtime.role === "unknown")
355
+ throw new Error(`Service "${options.name}" is not bound`);
355
356
  return runtime.role;
356
357
  },
357
358
  define
@@ -360,11 +361,18 @@ function createService(options) {
360
361
  if (handlers.has(method))
361
362
  throw new Error(`Service method "${method}" already defined`);
362
363
  return handlers.set(method, handler), (async (...args) => {
363
- if (runtime.role === "unknown")
364
+ if (ensureClientBound(), runtime.role === "unknown")
364
365
  throw new Error(`Service "${options.name}" is not bound (method "${method}")`);
365
- return runtime.role === "server" ? handler(...args) : runtime.transport.request(method, ...args);
366
+ return runtime.role === "server" ? handler(...args) : (assertClient(runtime), runtime.transport.request(method, ...args));
366
367
  });
367
368
  }
369
+ function ensureClientBound() {
370
+ runtime.role !== "unknown" || process.env.BAKIT_SERVICE_NAME === options.name || bind(ServiceRole.Client);
371
+ }
372
+ function assertClient(runtime2) {
373
+ if (runtime2.role !== "client")
374
+ throw new Error(`Service "${options.name}" is not a client`);
375
+ }
368
376
  function bind(role) {
369
377
  if (runtime.role !== "unknown")
370
378
  throw new Error(`Service "${options.name}" already bound`);
@@ -372,29 +380,25 @@ function createService(options) {
372
380
  let server = createTransportServer(options.transport);
373
381
  for (let [method, handler] of handlers)
374
382
  server.handle(method, handler);
375
- runtime = {
376
- role,
377
- transport: server
378
- }, server.listen();
383
+ runtime.role = "server", runtime.transport = server, server.listen();
379
384
  } else if (role === "client") {
380
385
  let client = createTransportClient(options.transport);
381
- runtime = {
382
- role,
383
- transport: client
384
- }, client.connect();
386
+ runtime.role = "client", runtime.transport = client, client.connect();
385
387
  }
386
388
  }
387
389
  return Object.defineProperty(service, SERVICE_INTERNAL, {
388
390
  value: {
389
391
  get runtime() {
392
+ if (runtime.role === "unknown")
393
+ throw new Error(`Service "${options.name}" is not bound`);
390
394
  return runtime;
391
395
  },
392
396
  bind
393
397
  }
394
398
  }), service;
395
399
  }
396
- function getInteralService(service) {
400
+ function getInternalService(service) {
397
401
  return service[SERVICE_INTERNAL];
398
402
  }
399
403
 
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 };
404
+ export { DEFAULT_IPC_SOCKET_CONNECTION_OPTIONS, IPCServerState, RPCError, ServiceRole, SocketState, TransportDriver, createIPCClient, createIPCServer, createIPCSocketConnection, createIPCSocketMessageHandler, createService, createTransportClient, createTransportClientProtocol, createTransportServer, createTransportServerProtocol, deserializeRPCError, getIPCPath, getInternalService, serializeRPCError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bakit/service",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Service manager for bakit framework",
5
5
  "type": "module",
6
6
  "exports": {