All files / nodejs/src/daemon start-daemon.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 2/2
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                                                                                3x   3x             3x 3x 3x     3x            
import type { KernelDatabase } from '@metamask/kernel-store';
import type { Kernel } from '@metamask/ocap-kernel';
 
import { startRpcSocketServer } from './rpc-socket-server.ts';
 
/**
 * Options for starting the daemon.
 */
export type StartDaemonOptions = {
  /** UNIX socket path for the RPC server. */
  socketPath: string;
  /** A running kernel instance. */
  kernel: Kernel;
  /** The kernel database instance. */
  kernelDatabase: KernelDatabase;
  /** Optional callback invoked when a `shutdown` RPC is received. */
  onShutdown?: () => Promise<void>;
};
 
/**
 * Handle returned by {@link startDaemon}.
 */
export type DaemonHandle = {
  kernel: Kernel;
  socketPath: string;
  close: () => Promise<void>;
};
 
/**
 * Start the OCAP daemon.
 *
 * Starts a JSON-RPC socket server that exposes kernel control methods
 * on a UNIX domain socket.
 *
 * @param options - Configuration options.
 * @returns A daemon handle.
 */
export async function startDaemon(
  options: StartDaemonOptions,
): Promise<DaemonHandle> {
  const { socketPath, kernel, kernelDatabase, onShutdown } = options;
 
  const rpcServer = await startRpcSocketServer({
    socketPath,
    kernel,
    kernelDatabase,
    onShutdown,
  });
 
  const close = async (): Promise<void> => {
    await rpcServer.close();
    await kernel.stop();
  };
 
  return {
    kernel,
    socketPath,
    close,
  };
}