-
Notifications
You must be signed in to change notification settings - Fork 3
/
marshal.ts
40 lines (33 loc) · 1.26 KB
/
marshal.ts
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
import { Far, makeMarshal } from '@endo/marshal';
const makeTranslationTable = (
makeSlot: (val: unknown, size: number) => unknown,
makeVal: (slot: unknown, iface: string | undefined) => unknown,
) => {
const valToSlot = new Map();
const slotToVal = new Map();
const convertValToSlot = (val: unknown) => {
if (valToSlot.has(val)) return valToSlot.get(val);
const slot = makeSlot(val, valToSlot.size);
valToSlot.set(val, slot);
slotToVal.set(slot, val);
return slot;
};
const convertSlotToVal = (slot: unknown, iface: string | undefined) => {
if (slot === null) return makeVal(slot, iface);
if (slotToVal.has(slot)) return slotToVal.get(slot);
const val = makeVal(slot, iface);
valToSlot.set(val, slot);
slotToVal.set(slot, val);
return val;
};
return harden({ convertValToSlot, convertSlotToVal });
};
const synthesizeRemotable = (slot: unknown, iface: string | undefined) =>
Far(`${(iface ?? '').replace(/^Alleged: /, '')}#${slot}`, {});
const { convertValToSlot, convertSlotToVal } = makeTranslationTable(slot => {
throw new Error(`unknown id: ${slot}`);
}, synthesizeRemotable);
export const makeClientMarshaller = () =>
makeMarshal(convertValToSlot, convertSlotToVal, {
serializeBodyFormat: 'smallcaps',
});