All files / kernel-browser-runtime/src/rpc-handlers queue-message.ts

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

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                              5x                             5x             2x      
import type { CapData } from '@endo/marshal';
import type { MethodSpec, Handler } from '@metamask/kernel-rpc-methods';
import type { Kernel } from '@metamask/ocap-kernel';
import { CapDataStruct } from '@metamask/ocap-kernel';
import { tuple, string, array } from '@metamask/superstruct';
import { UnsafeJsonStruct } from '@metamask/utils';
import type { Json } from '@metamask/utils';
 
/**
 * Enqueue a message to a vat via the kernel's crank queue.
 */
export const queueMessageSpec: MethodSpec<
  'queueMessage',
  [string, string, Json[]],
  CapData<string>
> = {
  method: 'queueMessage',
  params: tuple([string(), string(), array(UnsafeJsonStruct)]),
  result: CapDataStruct,
};
 
export type QueueMessageHooks = {
  kernel: Pick<Kernel, 'queueMessage'>;
};
 
export const queueMessageHandler: Handler<
  'queueMessage',
  [string, string, Json[]],
  Promise<CapData<string>>,
  QueueMessageHooks
> = {
  ...queueMessageSpec,
  hooks: { kernel: true },
  implementation: async (
    { kernel }: QueueMessageHooks,
    [target, method, args],
  ): Promise<CapData<string>> => {
    return kernel.queueMessage(target, method, args);
  },
};