Stories: Proper notifications and handling of out-of-order messages

This commit is contained in:
Scott Nonnenberg 2023-01-11 14:54:06 -08:00 committed by GitHub
parent 81fc9ff94d
commit 50a0110192
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 425 additions and 274 deletions

View file

@ -2,6 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
import { isPlainObject } from 'lodash';
import * as log from '../logging/log';
import { isIterable } from '../util/iterables';
@ -22,7 +23,7 @@ export function cleanDataForIpc(data: unknown): {
pathsChanged: Array<string>;
} {
const pathsChanged: Array<string> = [];
const cleaned = cleanDataInner(data, 'root', pathsChanged);
const cleaned = cleanDataInner(data, 'root', pathsChanged, 0);
return { cleaned, pathsChanged };
}
@ -49,8 +50,17 @@ interface CleanedArray extends Array<CleanedDataValue> {}
function cleanDataInner(
data: unknown,
path: string,
pathsChanged: Array<string>
pathsChanged: Array<string>,
depth: number
): CleanedDataValue {
if (depth > 10) {
log.error(
`cleanDataInner: Reached maximum depth ${depth}; path is ${path}`
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return { cleaned: data as any, pathsChanged };
}
switch (typeof data) {
case 'undefined':
case 'boolean':
@ -77,7 +87,9 @@ function cleanDataInner(
if (item == null) {
pathsChanged.push(indexPath);
} else {
result.push(cleanDataInner(item, indexPath, pathsChanged));
result.push(
cleanDataInner(item, indexPath, pathsChanged, depth + 1)
);
}
});
return result;
@ -91,7 +103,8 @@ function cleanDataInner(
result[key] = cleanDataInner(
value,
`${path}.<map value at ${key}>`,
pathsChanged
pathsChanged,
depth + 1
);
} else {
pathsChanged.push(`${path}.<map key ${String(key)}>`);
@ -121,7 +134,12 @@ function cleanDataInner(
typeof dataAsRecord.toNumber === 'function'
) {
// We clean this just in case `toNumber` returns something bogus.
return cleanDataInner(dataAsRecord.toNumber(), path, pathsChanged);
return cleanDataInner(
dataAsRecord.toNumber(),
path,
pathsChanged,
depth + 1
);
}
if (isIterable(dataAsRecord)) {
@ -133,7 +151,8 @@ function cleanDataInner(
cleanDataInner(
value,
`${path}.<iterator index ${index}>`,
pathsChanged
pathsChanged,
depth + 1
)
);
index += 1;
@ -151,7 +170,12 @@ function cleanDataInner(
// Conveniently, `Object.entries` removes symbol keys.
Object.entries(dataAsRecord).forEach(([key, value]) => {
result[key] = cleanDataInner(value, `${path}.${key}`, pathsChanged);
result[key] = cleanDataInner(
value,
`${path}.${key}`,
pathsChanged,
depth + 1
);
});
return result;