-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAuditEventsGet.ts
More file actions
116 lines (110 loc) · 3.52 KB
/
AuditEventsGet.ts
File metadata and controls
116 lines (110 loc) · 3.52 KB
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
import type { ContextTimed } from '@matrixai/contexts';
import type { JSONValue } from '@matrixai/rpc';
import type { ClientRPCRequestParams, ClientRPCResponseResult } from '../types';
import type {
AuditEvent,
AuditEventSerialized,
TopicPath,
TopicSubPath,
} from '../../audit/types';
import type { Audit } from '../../audit';
import type { AuditEventId, AuditEventIdEncoded } from '../../ids';
import { ServerHandler } from '@matrixai/rpc';
import * as auditUtils from '../../audit/utils';
class AuditEventsGet extends ServerHandler<
{
audit: Audit;
},
ClientRPCRequestParams<{
paths: Array<TopicSubPath>;
seek?: AuditEventIdEncoded | number;
seekEnd?: AuditEventIdEncoded | number;
order?: 'asc' | 'desc';
limit?: number;
awaitFutureEvents?: boolean;
}>,
ClientRPCResponseResult<AuditEventSerialized>
> {
public handle = async function* (
{
paths,
seek,
seekEnd,
order = 'asc',
limit,
awaitFutureEvents = false,
}: ClientRPCRequestParams<{
seek?: AuditEventIdEncoded | number;
seekEnd?: AuditEventIdEncoded | number;
order?: 'asc' | 'desc';
limit?: number;
awaitFutureEvents?: boolean;
}> & {
paths: Array<TopicSubPath>;
},
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue>,
ctx: ContextTimed,
): AsyncGenerator<ClientRPCResponseResult<AuditEventSerialized>> {
const { audit }: { audit: Audit } = this.container;
const iterators: Array<AsyncGenerator<AuditEvent>> = [];
let seek_: AuditEventId | number | undefined;
if (seek != null) {
seek_ =
typeof seek === 'string' ? auditUtils.decodeAuditEventId(seek) : seek;
}
let seekEnd_: AuditEventId | number | undefined;
if (seekEnd != null) {
seekEnd_ =
typeof seekEnd === 'string'
? auditUtils.decodeAuditEventId(seekEnd)
: seekEnd;
}
// Convert the paths
const topicPaths: Array<TopicPath> = [];
for (const topicPath of auditUtils.filterSubPaths(paths)) {
if (auditUtils.isTopicPath(topicPath)) topicPaths.push(topicPath);
}
// Creating iterators for each `topicPath`
for (const topicPath of topicPaths) {
if (awaitFutureEvents) {
// If we're awaiting future events then we call `getAuditEventsLongRunning`, order is forced to `asc` in this case
const iterator = audit.getAuditEventsLongRunning(topicPath, {
seek: seek_,
seekEnd: seekEnd_,
limit: limit,
});
iterators.push(iterator);
} else {
// Otherwise we use the normal `getAuditEvents`
const iterator = audit.getAuditEvents(topicPath, {
seek: seek_,
seekEnd: seekEnd_,
order: order,
limit: limit,
});
iterators.push(iterator);
}
}
// We need to reverse the compare if we are descending in time
const orderSwitchMultiplier = awaitFutureEvents || order === 'asc' ? 1 : -1;
function sortFn(a: AuditEvent, b: AuditEvent) {
return Buffer.compare(a.id, b.id) * orderSwitchMultiplier;
}
const combinedIterator = auditUtils.genSort<AuditEvent>(
sortFn,
...iterators,
);
ctx.signal.addEventListener('abort', async () => {
await combinedIterator.return(ctx.signal.reason);
});
for await (const auditEvent of combinedIterator) {
yield {
id: auditUtils.encodeAuditEventId(auditEvent.id),
path: auditEvent.path,
data: auditEvent.data,
};
}
};
}
export default AuditEventsGet;