|
| 1 | +import { makePromiseKit } from '@endo/promise-kit'; |
| 2 | +import { waitUntilQuiescent } from '@metamask/kernel-utils'; |
| 3 | + |
| 4 | +import type { EndowmentContext } from '../types.ts'; |
| 5 | + |
| 6 | +/** |
| 7 | + * An ocap url. |
| 8 | + */ |
| 9 | +export type OcapUrl = `ocap://${string}`; |
| 10 | + |
| 11 | +/** |
| 12 | + * A function that makes an ocap url for a given object. |
| 13 | + */ |
| 14 | +export type MakeOcapUrl = (object: unknown) => Promise<OcapUrl>; |
| 15 | + |
| 16 | +/** |
| 17 | + * Make a function that makes an ocap url for a given object. Intended to be |
| 18 | + * used as a user code endowment. |
| 19 | + * |
| 20 | + * @param context - The context in which the endowment is created. |
| 21 | + * @returns A function that makes an ocap url for a given object. |
| 22 | + */ |
| 23 | +export function factory(context: EndowmentContext): MakeOcapUrl { |
| 24 | + const { toRef } = context; |
| 25 | + |
| 26 | + /** |
| 27 | + * Make an ocap url for a given object. |
| 28 | + * |
| 29 | + * @param object - The object to make an ocap url for. |
| 30 | + * @returns A promise that resolves with the ocap url. |
| 31 | + */ |
| 32 | + const makeOcapUrl = async (object: unknown): Promise<OcapUrl> => { |
| 33 | + const { promise, resolve } = makePromiseKit<OcapUrl>(); |
| 34 | + // This vpid can be sent to the kernel as the resolution target. |
| 35 | + const vpid = toRef(promise); |
| 36 | + const vref = toRef(object); |
| 37 | + console.log('makeOcapUrl', vpid, vref); |
| 38 | + // XXX this is a placeholder for the actual implementation which sends the |
| 39 | + // kernel a request to resolve the promise with an ocap url for the given |
| 40 | + // vref. |
| 41 | + waitUntilQuiescent() |
| 42 | + .catch((error) => console.error('wait until quiescent failed:', error)) |
| 43 | + .then(() => resolve(`ocap://${vref}`)) |
| 44 | + .catch((error) => console.error('resolve failed:', error)); |
| 45 | + return promise; |
| 46 | + }; |
| 47 | + |
| 48 | + return harden(makeOcapUrl); |
| 49 | +} |
0 commit comments