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

100% Statements 5/5
100% Branches 0/0
100% Functions 1/1
100% Lines 5/5

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 62 63 64 65 66 67 68 69                                                                                                            5x     5x     5x   5x         1x  
import { makeCapTP } from '@endo/captp';
import type { Kernel } from '@metamask/ocap-kernel';
 
import { makeKernelFacade } from './kernel-facade.ts';
import type { CapTPMessage } from '../../types.ts';
 
/**
 * Options for creating a kernel CapTP endpoint.
 */
export type KernelCapTPOptions = {
  /**
   * The kernel instance to expose via CapTP.
   */
  kernel: Kernel;
 
  /**
   * Function to send CapTP messages to the background.
   *
   * @param message - The CapTP message to send.
   */
  send: (message: CapTPMessage) => void;
};
 
/**
 * The kernel's CapTP endpoint.
 */
export type KernelCapTP = {
  /**
   * Dispatch an incoming CapTP message from the background.
   *
   * @param message - The CapTP message to dispatch.
   * @returns True if the message was handled.
   */
  dispatch: (message: CapTPMessage) => boolean;
 
  /**
   * Abort the CapTP connection.
   *
   * @param reason - The reason for aborting.
   */
  abort: (reason?: unknown) => void;
};
 
/**
 * Create a CapTP endpoint for the kernel.
 *
 * This sets up a CapTP connection that exposes the kernel facade as the
 * bootstrap object. The background can then use `E(kernel).method()` to
 * call kernel methods.
 *
 * @param options - The options for creating the CapTP endpoint.
 * @returns The kernel CapTP endpoint.
 */
export function makeKernelCapTP(options: KernelCapTPOptions): KernelCapTP {
  const { kernel, send } = options;
 
  // Create the kernel facade that will be exposed to the background
  const kernelFacade = makeKernelFacade(kernel);
 
  // Create the CapTP endpoint
  const { dispatch, abort } = makeCapTP('kernel', send, kernelFacade);
 
  return harden({
    dispatch,
    abort,
  });
}
harden(makeKernelCapTP);