All files / kernel-test/src/vats monotonous-vat.ts

0% Statements 0/23
0% Branches 0/6
0% Functions 0/4
0% Lines 0/23

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                                                                                                                                             
import { E } from '@endo/eventual-send';
import { makeDefaultExo } from '@metamask/kernel-utils/exo';
import type { Baggage } from '@metamask/ocap-kernel';
 
/**
 * Vat providing Monotony As A Service (MaaS). It just keeps relentlessly counting up. It's very boring.
 *
 * Monotonicity: a consistent pattern of always increasing.
 * Monotony: a lack of variety or a tedious sameness.
 * It really could be either.
 *
 * @param _vatPowers - Special powers granted to this vat (not used here).
 * @param parameters - Initialization parameters from the vat's config object.
 * @param parameters.name - The name of the vat.
 * @param baggage - Root of vat's persistent state.
 * @returns The root object for the new vat.
 */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function buildRootObject(
  _vatPowers: unknown,
  parameters: { name?: string },
  baggage: Baggage,
) {
  const name = parameters?.name ?? 'anonymous';
  // eslint-disable-next-line no-console
  console.log(`buildRootObject "${name}"`);
  if (baggage.has('url')) {
    const url = baggage.get('url') as string;
    // eslint-disable-next-line no-console
    console.log(`URL for MaaS: ${url}`);
  } else {
    // eslint-disable-next-line no-console
    console.log(`URL for MasS not yet initialized`);
  }
 
  const myself = makeDefaultExo('root', {
    async bootstrap(
      _vats: unknown,
      services: { ocapURLIssuerService?: unknown },
    ) {
      // eslint-disable-next-line no-console
      console.log(`vat ${name} is bootstrap`);
      const issuer = services.ocapURLIssuerService;
      if (!issuer) {
        // eslint-disable-next-line no-console
        console.log(`no ocapURLIssuerService found`);
        throw Error(`MaaS requires an ocap URL issuer`);
      }
      const url = await E(issuer).issue(myself);
      // eslint-disable-next-line no-console
      console.log(`URL for MaaS: ${url}`);
      baggage.init('url', url);
      baggage.init('count', 1);
      return url;
    },
    getUrl() {
      return baggage.get('url');
    },
    next(from: string) {
      const count = baggage.get('count') as number;
      baggage.set('count', count + 1);
      // eslint-disable-next-line no-console
      console.log(
        `vat ${name} got 'next' request from ${from}, returning ${count}`,
      );
      return count;
    },
  });
  return myself;
}