All files / omnium-gatherum/src/controllers index.ts

0% Statements 0/7
100% Branches 0/0
0% Functions 0/4
0% Lines 0/7

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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101                                                                                                                                                                                                         
import { E } from '@endo/eventual-send';
import type { KernelFacade } from '@metamask/kernel-browser-runtime';
import type { Logger } from '@metamask/logger';
import type { ClusterConfig } from '@metamask/ocap-kernel';
 
import { CapletController } from './caplet/caplet-controller.ts';
import type { CapletControllerFacet, LaunchResult } from './caplet/index.ts';
import { makeChromeStorageAdapter } from './storage/index.ts';
 
// Base controller
export { Controller } from './base-controller.ts';
export type { ControllerConfig, ControllerMethods, FacetOf } from './types.ts';
 
// Storage
export type {
  StorageAdapter,
  ControllerStorageConfig,
} from './storage/index.ts';
export {
  makeChromeStorageAdapter,
  ControllerStorage,
} from './storage/index.ts';
 
// Caplet
export type {
  CapletId,
  SemVer,
  CapletManifest,
  InstalledCaplet,
  InstallResult,
  LaunchResult,
  CapletControllerState,
  CapletControllerFacet,
  CapletControllerDeps,
} from './caplet/index.ts';
export {
  isCapletId,
  isSemVer,
  isCapletManifest,
  assertCapletManifest,
  CapletIdStruct,
  SemVerStruct,
  CapletManifestStruct,
  CapletController,
} from './caplet/index.ts';
 
type InitializeControllersOptions = {
  logger: Logger;
  kernel: KernelFacade | Promise<KernelFacade>;
};
 
/**
 * Initializes the controllers for the host application.
 *
 * @param options - The options for initializing the controllers.
 * @param options.logger - The logger to use.
 * @param options.kernel - The kernel to use.
 * @returns The controllers for the host application.
 */
export async function initializeControllers({
  logger,
  kernel,
}: InitializeControllersOptions): Promise<{
  caplet: CapletControllerFacet;
}> {
  const storageAdapter = makeChromeStorageAdapter();
 
  const capletController = await CapletController.make(
    { logger: logger.subLogger({ tags: ['caplet'] }) },
    {
      adapter: storageAdapter,
      /**
       * Launch a subcluster for a caplet. Not concurrency safe.
       *
       * @param config - The configuration for the subcluster.
       * @returns The subcluster ID.
       */
      launchSubcluster: async (
        config: ClusterConfig,
      ): Promise<LaunchResult> => {
        const result = await E(kernel).launchSubcluster(config);
        return {
          subclusterId: result.subclusterId,
          rootKref: result.rootKref,
        };
      },
      terminateSubcluster: async (subclusterId: string): Promise<void> => {
        await E(kernel).terminateSubcluster(subclusterId);
      },
      getVatRoot: async (krefString: string): Promise<unknown> => {
        // Convert kref string to presence via kernel facade
        return E(kernel).getVatRoot(krefString);
      },
    },
  );
 
  return {
    caplet: capletController,
  };
}