@agoric/client-utils 0.1.1-dev-70182f8.0 → 0.1.1-dev-5761f40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/client-utils",
3
- "version": "0.1.1-dev-70182f8.0+70182f8",
3
+ "version": "0.1.1-dev-5761f40.0+5761f40",
4
4
  "description": "Utilities for building Agoric clients",
5
5
  "license": "Apache-2.0",
6
6
  "publishConfig": {
@@ -62,5 +62,5 @@
62
62
  ],
63
63
  "timeout": "20m"
64
64
  },
65
- "gitHead": "70182f867f9b2091d383a2023655cc4748f9ad68"
65
+ "gitHead": "5761f4094a6458a9d2b379f231e6f07d7b757ab3"
66
66
  }
package/src/main.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { VBankAccount };
2
2
  export * from "./cli.js";
3
+ export * from "./marshalTables.js";
3
4
  export * from "./network-config.js";
4
5
  export * from "./rpc.js";
5
6
  export * from "./smart-wallet-kit.js";
package/src/main.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["main.js"],"names":[],"mappings":";;;;;;;;;;6BAA6B,gCAAgC"}
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["main.js"],"names":[],"mappings":";;;;;;;;;;;6BAA6B,gCAAgC"}
package/src/main.js CHANGED
@@ -2,6 +2,7 @@ import { VBankAccount } from '@agoric/internal/src/config.js';
2
2
 
3
3
  export { VBankAccount };
4
4
  export * from './cli.js';
5
+ export * from './marshalTables.js';
5
6
  export * from './network-config.js';
6
7
  export * from './rpc.js';
7
8
  export * from './smart-wallet-kit.js';
@@ -0,0 +1,11 @@
1
+ export function makeClientMarshaller(valToSlot?: (v: unknown) => string): {
2
+ toCapData: import("@endo/marshal").ToCapData<string | null>;
3
+ fromCapData: import("@endo/marshal").FromCapData<string | null>;
4
+ serialize: import("@endo/marshal").ToCapData<string | null>;
5
+ unserialize: import("@endo/marshal").FromCapData<string | null>;
6
+ };
7
+ /**
8
+ * The null slot indicates that identity is not intended to be preserved.
9
+ */
10
+ export type WildSlot = string | null;
11
+ //# sourceMappingURL=marshalTables.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"marshalTables.d.ts","sourceRoot":"","sources":["marshalTables.js"],"names":[],"mappings":"AA4EO,iDAFI,CAAC,CAAC,EAAE,OAAO,KAAK,MAAM;;;;;EAchC;;;;uBAzEa,MAAM,GAAG,IAAI"}
@@ -0,0 +1,89 @@
1
+ // @ts-check
2
+ /**
3
+ * @file marshal tools for vstorage clients
4
+ *
5
+ * TODO: integrate back into @agoric/rpc
6
+ * - fixes: calls to makeClientMarshaller share static mutable state
7
+ * https://github.com/Agoric/ui-kit/issues/73
8
+ * - fits in this plain .js project
9
+ */
10
+ /** global harden */
11
+ import { Far, makeMarshal } from '@endo/marshal';
12
+
13
+ /**
14
+ * The null slot indicates that identity is not intended to be preserved.
15
+ *
16
+ * @typedef { string | null } WildSlot
17
+ */
18
+
19
+ /**
20
+ * Implement conventional parts of convertValToSlot, convertSlotToVal functions
21
+ * for use with makeMarshal based on a slot <-> value translation table,
22
+ * indexed in both directions. Caller supplies functions for making
23
+ * slots, values when not present in the table.
24
+ *
25
+ * @template Val
26
+ * @param {(val: Val, size: number) => string} makeSlot
27
+ * @param {(slot: WildSlot, iface: string | undefined) => Val} makeVal
28
+ */
29
+ const makeTranslationTable = (makeSlot, makeVal) => {
30
+ /** @type {Map<Val, string>} */
31
+ const valToSlot = new Map();
32
+ /** @type {Map<string, Val>} */
33
+ const slotToVal = new Map();
34
+
35
+ /** @type {(val: Val) => string} */
36
+ const convertValToSlot = val => {
37
+ if (valToSlot.has(val)) {
38
+ // @ts-expect-error https://github.com/microsoft/TypeScript/issues/13086
39
+ return valToSlot.get(val);
40
+ }
41
+ const slot = makeSlot(val, valToSlot.size);
42
+ valToSlot.set(val, slot);
43
+ slotToVal.set(slot, val);
44
+ return slot;
45
+ };
46
+
47
+ /** @type {(slot: WildSlot, iface: string | undefined) => Val} */
48
+ const convertSlotToVal = (slot, iface) => {
49
+ if (slot === null) return makeVal(slot, iface);
50
+ if (slotToVal.has(slot)) {
51
+ // @ts-expect-error https://github.com/microsoft/TypeScript/issues/13086
52
+ return slotToVal.get(slot);
53
+ }
54
+ const val = makeVal(slot, iface);
55
+ valToSlot.set(val, slot);
56
+ slotToVal.set(slot, val);
57
+ return val;
58
+ };
59
+ // eslint-disable-next-line no-undef
60
+ return harden({ convertValToSlot, convertSlotToVal });
61
+ };
62
+
63
+ /** @type {(slot: string, iface: string | undefined) => any} */
64
+ const synthesizeRemotable = (slot, iface) => {
65
+ const ifaceStr = iface ?? '';
66
+ const suffix = ifaceStr.endsWith(`#${slot}`) ? '' : `#${slot}`;
67
+ return Far(`${ifaceStr.replace(/^Alleged: /, '')}${suffix}`, {});
68
+ };
69
+
70
+ /**
71
+ * Make a marshaller that synthesizes a remotable the first
72
+ * time it sees a slot identifier, allowing clients to recognize
73
+ * object identity for brands, instances, etc.
74
+ *
75
+ * @param {(v: unknown) => string} [valToSlot]
76
+ */
77
+ export const makeClientMarshaller = valToSlot => {
78
+ const noNewSlots = val => {
79
+ throw Error(`unknown value: ${val}`);
80
+ };
81
+ const { convertValToSlot, convertSlotToVal } = makeTranslationTable(
82
+ valToSlot || noNewSlots,
83
+ synthesizeRemotable,
84
+ );
85
+
86
+ return makeMarshal(convertValToSlot, convertSlotToVal, {
87
+ serializeBodyFormat: 'smallcaps',
88
+ });
89
+ };