All files / kernel-browser-runtime/src/kernel-worker/captp kernel-facade.ts

90% Statements 9/10
100% Branches 0/0
87.5% Functions 7/8
90% Lines 9/10

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                            20x 1x       3x 2x       2x       3x       3x       3x                   2x  
import { makeDefaultExo } from '@metamask/kernel-utils/exo';
import type { Kernel, ClusterConfig, KRef, VatId } from '@metamask/ocap-kernel';
 
import type { KernelFacade, LaunchResult } from '../../types.ts';
 
export type { KernelFacade } from '../../types.ts';
 
/**
 * Create the kernel facade exo that exposes kernel methods via CapTP.
 *
 * @param kernel - The kernel instance to wrap.
 * @returns The kernel facade exo.
 */
export function makeKernelFacade(kernel: Kernel): KernelFacade {
  return makeDefaultExo('KernelFacade', {
    ping: async () => 'pong' as const,
 
    launchSubcluster: async (config: ClusterConfig): Promise<LaunchResult> => {
      const { subclusterId, bootstrapRootKref } =
        await kernel.launchSubcluster(config);
      return { subclusterId, rootKref: bootstrapRootKref };
    },
 
    terminateSubcluster: async (subclusterId: string) => {
      return kernel.terminateSubcluster(subclusterId);
    },
 
    queueMessage: async (target: KRef, method: string, args: unknown[]) => {
      return kernel.queueMessage(target, method, args);
    },
 
    getStatus: async () => {
      return kernel.getStatus();
    },
 
    pingVat: async (vatId: VatId) => {
      return kernel.pingVat(vatId);
    },
 
    getVatRoot: async (krefString: string) => {
      // Return wrapped kref for future CapTP marshalling to presence
      // TODO: Enable custom CapTP marshalling tables to convert this to a presence
      return { kref: krefString };
    },
  });
}
harden(makeKernelFacade);