-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmarshal.js
510 lines (485 loc) · 15.9 KB
/
marshal.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// @ts-check
/// <reference types="ses"/>
import { Nat } from '@endo/nat';
import { assertPassable, passStyleOf } from './passStyleOf.js';
import './types.js';
import { getInterfaceOf } from './helpers/remotable.js';
import { ErrorHelper, getErrorConstructor } from './helpers/error.js';
import { makeTagged } from './makeTagged.js';
import { isObject, getTag } from './helpers/passStyle-helpers.js';
import {
assertPassableSymbol,
nameForPassableSymbol,
passableSymbolForName,
} from './helpers/symbol.js';
const { ownKeys } = Reflect;
const { isArray } = Array;
const {
getOwnPropertyDescriptors,
defineProperties,
is,
fromEntries,
freeze,
} = Object;
const { details: X, quote: q } = assert;
/**
* Special property name that indicates an encoding that needs special
* decoding.
*/
const QCLASS = '@qclass';
export { QCLASS };
const defaultValToSlotFn = x => x;
const defaultSlotToValFn = (x, _) => x;
/**
* @template Slot
* @type {MakeMarshal<Slot>}
*/
export function makeMarshal(
convertValToSlot = defaultValToSlotFn,
convertSlotToVal = defaultSlotToValFn,
{
errorTagging = 'on',
marshalName = 'anon-marshal',
// TODO Temporary hack.
// See https://github.com/Agoric/agoric-sdk/issues/2780
errorIdNum = 10000,
// We prefer that the caller instead log to somewhere hidden
// to be revealed when correlating with the received error.
marshalSaveError = err =>
console.log('Temporary logging of sent error', err),
} = {},
) {
assert.typeof(marshalName, 'string');
assert(
errorTagging === 'on' || errorTagging === 'off',
X`The errorTagging option can only be "on" or "off" ${errorTagging}`,
);
const nextErrorId = () => {
errorIdNum += 1;
return `error:${marshalName}#${errorIdNum}`;
};
/**
* @template Slot
* @type {Serialize<Slot>}
*/
const serialize = root => {
const slots = [];
// maps val (promise or remotable) to index of slots[]
const slotMap = new Map();
/**
* @param {Passable} val
* @param {InterfaceSpec=} iface
* @returns {Encoding}
*/
function serializeSlot(val, iface = undefined) {
let slotIndex;
if (slotMap.has(val)) {
// TODO assert that it's the same iface as before
slotIndex = slotMap.get(val);
assert.typeof(slotIndex, 'number');
iface = undefined;
} else {
const slot = convertValToSlot(val);
slotIndex = slots.length;
slots.push(slot);
slotMap.set(val, slotIndex);
}
// TODO explore removing this special case
if (iface === undefined) {
return harden({
[QCLASS]: 'slot',
index: slotIndex,
});
}
return harden({
[QCLASS]: 'slot',
iface,
index: slotIndex,
});
}
/**
* Even if an Error is not actually passable, we'd rather send
* it anyway because the diagnostic info carried by the error
* is more valuable than diagnosing why the error isn't
* passable. See comments in ErrorHelper.
*
* @param {Error} err
* @returns {Encoding}
*/
const encodeError = err => {
// Must encode `cause`, `errors`.
// nested non-passable errors must be ok from here.
if (errorTagging === 'on') {
// We deliberately do not share the stack, but it would
// be useful to log the stack locally so someone who has
// privileged access to the throwing Vat can correlate
// the problem with the remote Vat that gets this
// summary. If we do that, we could allocate some random
// identifier and include it in the message, to help
// with the correlation.
const errorId = nextErrorId();
assert.note(err, X`Sent as ${errorId}`);
marshalSaveError(err);
return harden({
[QCLASS]: 'error',
errorId,
message: `${err.message}`,
name: `${err.name}`,
});
} else {
return harden({
[QCLASS]: 'error',
message: `${err.message}`,
name: `${err.name}`,
});
}
};
/**
* Must encode `val` into plain JSON data *canonically*, such that
* `JSON.stringify(encode(v1)) === JSON.stringify(encode(v1))`
* For each copyRecord, we only accept string property names,
* not symbols. The encoded form the sort
* order of these names must be the same as their enumeration
* order, so a `JSON.stringify` of the encoded form agrees with
* a canonical-json stringify of the encoded form.
*
* @param {Passable} val
* @returns {Encoding}
*/
const encode = val => {
if (ErrorHelper.canBeValid(val)) {
return encodeError(val);
}
// First we handle all primitives. Some can be represented directly as
// JSON, and some must be encoded as [QCLASS] composites.
const passStyle = passStyleOf(val);
switch (passStyle) {
case 'null': {
return null;
}
case 'undefined': {
return harden({ [QCLASS]: 'undefined' });
}
case 'string':
case 'boolean': {
return val;
}
case 'number': {
if (Number.isNaN(val)) {
return harden({ [QCLASS]: 'NaN' });
}
if (is(val, -0)) {
return 0;
}
if (val === Infinity) {
return harden({ [QCLASS]: 'Infinity' });
}
if (val === -Infinity) {
return harden({ [QCLASS]: '-Infinity' });
}
return val;
}
case 'bigint': {
return harden({
[QCLASS]: 'bigint',
digits: String(val),
});
}
case 'symbol': {
assertPassableSymbol(val);
const name = /** @type {string} */ (nameForPassableSymbol(val));
if (name === '@@asyncIterator') {
// Deprectated qclass. TODO make conditional
// on environment variable. Eventually remove, but only after
// confident that all supported receivers understand
// `[QCLASS]: 'symbol'`.
return harden({
[QCLASS]: '@@asyncIterator',
});
}
return harden({
[QCLASS]: 'symbol',
name,
});
}
case 'copyRecord': {
if (QCLASS in val) {
// Hilbert hotel
const { [QCLASS]: qclassValue, ...rest } = val;
if (ownKeys(rest).length === 0) {
/** @type {Encoding} */
const result = harden({
[QCLASS]: 'hilbert',
original: encode(qclassValue),
});
return result;
} else {
/** @type {Encoding} */
const result = harden({
[QCLASS]: 'hilbert',
original: encode(qclassValue),
// See https://github.com/Agoric/agoric-sdk/issues/4313
rest: encode(freeze(rest)),
});
return result;
}
}
// Currently copyRecord allows only string keys so this will
// work. If we allow sortable symbol keys, this will need to
// become more interesting.
const names = ownKeys(val).sort();
return fromEntries(names.map(name => [name, encode(val[name])]));
}
case 'copyArray': {
return val.map(encode);
}
case 'tagged': {
/** @type {Encoding} */
const result = harden({
[QCLASS]: 'tagged',
tag: getTag(val),
payload: encode(val.payload),
});
return result;
}
case 'remotable': {
const iface = getInterfaceOf(val);
// console.log(`serializeSlot: ${val}`);
return serializeSlot(val, iface);
}
case 'error': {
return encodeError(val);
}
case 'promise': {
// console.log(`serializeSlot: ${val}`);
return serializeSlot(val);
}
default: {
assert.fail(X`unrecognized passStyle ${q(passStyle)}`, TypeError);
}
}
};
const encoded = encode(root);
return harden({
body: JSON.stringify(encoded),
slots,
});
};
const makeFullRevive = slots => {
/** @type {Map<number, Passable>} */
const valMap = new Map();
function unserializeSlot(index, iface) {
if (valMap.has(index)) {
return valMap.get(index);
}
// TODO SECURITY HAZARD: must enfoce that remotable vs promise
// is according to the encoded string.
const slot = slots[Number(Nat(index))];
const val = convertSlotToVal(slot, iface);
valMap.set(index, val);
return val;
}
/**
* We stay close to the algorithm at
* https://tc39.github.io/ecma262/#sec-json.parse , where
* fullRevive(harden(JSON.parse(str))) is like JSON.parse(str, revive))
* for a similar reviver. But with the following differences:
*
* Rather than pass a reviver to JSON.parse, we first call a plain
* (one argument) JSON.parse to get rawTree, and then post-process
* the rawTree with fullRevive. The kind of revive function
* handled by JSON.parse only does one step in post-order, with
* JSON.parse doing the recursion. By contrast, fullParse does its
* own recursion in the same pre-order in which the replacer visited them.
*
* In order to break cycles, the potentially cyclic objects are
* not frozen during the recursion. Rather, the whole graph is
* hardened before being returned. Error objects are not
* potentially recursive, and so may be harmlessly hardened when
* they are produced.
*
* fullRevive can produce properties whose value is undefined,
* which a JSON.parse on a reviver cannot do. If a reviver returns
* undefined to JSON.parse, JSON.parse will delete the property
* instead.
*
* fullRevive creates and returns a new graph, rather than
* modifying the original tree in place.
*
* fullRevive may rely on rawTree being the result of a plain call
* to JSON.parse. However, it *cannot* rely on it having been
* produced by JSON.stringify on the replacer above, i.e., it
* cannot rely on it being a valid marshalled
* representation. Rather, fullRevive must validate that.
*
* @param {Encoding} rawTree must be hardened
*/
function fullRevive(rawTree) {
if (!isObject(rawTree)) {
// primitives pass through
return rawTree;
}
// Assertions of the above to narrow the type.
assert.typeof(rawTree, 'object');
assert(rawTree !== null);
if (QCLASS in rawTree) {
const qclass = rawTree[QCLASS];
assert.typeof(
qclass,
'string',
X`invalid qclass typeof ${q(typeof qclass)}`,
);
assert(!isArray(rawTree));
// Switching on `rawTree[QCLASS]` (or anything less direct, like
// `qclass`) does not discriminate rawTree in [email protected] and
// earlier.
switch (rawTree['@qclass']) {
// Encoding of primitives not handled by JSON
case 'undefined': {
return undefined;
}
case 'NaN': {
return NaN;
}
case 'Infinity': {
return Infinity;
}
case '-Infinity': {
return -Infinity;
}
case 'bigint': {
const { digits } = rawTree;
assert.typeof(
digits,
'string',
X`invalid digits typeof ${q(typeof digits)}`,
);
return BigInt(digits);
}
case '@@asyncIterator': {
// Deprectated qclass. TODO make conditional
// on environment variable. Eventually remove, but after confident
// that there are no more supported senders.
return Symbol.asyncIterator;
}
case 'symbol': {
const { name } = rawTree;
return passableSymbolForName(name);
}
case 'tagged': {
const { tag, payload } = rawTree;
return makeTagged(tag, fullRevive(payload));
}
case 'error': {
// Must decode `cause` and `errors` properties
const { name, message, errorId } = rawTree;
assert.typeof(
name,
'string',
X`invalid error name typeof ${q(typeof name)}`,
);
assert.typeof(
message,
'string',
X`invalid error message typeof ${q(typeof message)}`,
);
const EC = getErrorConstructor(`${name}`) || Error;
// errorId is a late addition so be tolerant of its absence.
const errorName =
errorId === undefined
? `Remote${EC.name}`
: `Remote${EC.name}(${errorId})`;
// Due to a defect in the SES type definition, the next line is
// fails a type check.
// Pending https://github.com/endojs/endo/issues/977
// @ts-ignore-next-line
const error = assert.error(`${message}`, EC, { errorName });
return error;
}
case 'slot': {
const { index, iface } = rawTree;
const val = unserializeSlot(index, iface);
return val;
}
case 'hilbert': {
const { original, rest } = rawTree;
assert(
'original' in rawTree,
X`Invalid Hilbert Hotel encoding ${rawTree}`,
);
// Don't harden since we're not done mutating it
const result = { [QCLASS]: fullRevive(original) };
if ('rest' in rawTree) {
assert(
rest !== undefined,
X`Rest encoding must not be undefined`,
);
const restObj = fullRevive(rest);
// TODO really should assert that `passStyleOf(rest)` is
// `'copyRecord'` but we'd have to harden it and it is too
// early to do that.
assert(
!(QCLASS in restObj),
X`Rest must not contain its own definition of ${q(QCLASS)}`,
);
defineProperties(result, getOwnPropertyDescriptors(restObj));
}
return result;
}
default: {
assert(
// @ts-expect-error exhaustive check should make condition true
qclass !== 'ibid',
X`The protocol no longer supports ibid encoding: ${rawTree}.`,
);
assert.fail(X`unrecognized ${q(QCLASS)} ${q(qclass)}`, TypeError);
}
}
} else if (isArray(rawTree)) {
const result = [];
const { length } = rawTree;
for (let i = 0; i < length; i += 1) {
result[i] = fullRevive(rawTree[i]);
}
return result;
} else {
const result = {};
for (const name of ownKeys(rawTree)) {
assert.typeof(
name,
'string',
X`Property ${name} of ${rawTree} must be a string`,
);
result[name] = fullRevive(rawTree[name]);
}
return result;
}
}
return fullRevive;
};
/**
* @template Slot
* @type {Unserialize<Slot>}
*/
const unserialize = data => {
assert.typeof(
data.body,
'string',
X`unserialize() given non-capdata (.body is ${data.body}, not string)`,
);
assert(
isArray(data.slots),
X`unserialize() given non-capdata (.slots are not Array)`,
);
const rawTree = harden(JSON.parse(data.body));
const fullRevive = makeFullRevive(data.slots);
const result = harden(fullRevive(rawTree));
// See https://github.com/Agoric/agoric-sdk/issues/4337
assertPassable(result);
return result;
};
return harden({
serialize,
unserialize,
});
}