|
| 1 | +import type { ContextTimed } from '@matrixai/contexts'; |
| 2 | +import type { DB } from '@matrixai/db'; |
| 3 | +import type { JSONValue } from '@matrixai/rpc'; |
| 4 | +import type { |
| 5 | + AgentRPCRequestParams, |
| 6 | + AgentRPCResponseResult, |
| 7 | + AuditIdMessage, |
| 8 | + AgentAuditMessage, |
| 9 | +} from '../types'; |
| 10 | +import type Audit from '../../../audit/Audit'; |
| 11 | +import type { AuditEvent } from '../../../audit/types'; |
| 12 | +import type { AuditEventId } from '../../../ids'; |
| 13 | +import { ServerHandler } from '@matrixai/rpc'; |
| 14 | +import * as auditUtils from '../../../audit/utils'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Gets audit events from a node |
| 18 | + */ |
| 19 | +class NodesAuditEventsGet extends ServerHandler< |
| 20 | + { |
| 21 | + audit: Audit; |
| 22 | + db: DB; |
| 23 | + }, |
| 24 | + AgentRPCRequestParams<AuditIdMessage>, |
| 25 | + AgentRPCResponseResult<AgentAuditMessage<AuditEvent>> |
| 26 | +> { |
| 27 | + public handle = async function* ( |
| 28 | + input: AgentRPCRequestParams<AuditIdMessage>, |
| 29 | + _cancel: (reason?: any) => void, |
| 30 | + _meta: Record<string, JSONValue> | undefined, |
| 31 | + ctx: ContextTimed, |
| 32 | + ): AsyncGenerator<AgentRPCResponseResult<AgentAuditMessage<AuditEvent>>> { |
| 33 | + let seek_: AuditEventId | number | undefined; |
| 34 | + let seekEnd_: AuditEventId | number | undefined; |
| 35 | + |
| 36 | + const { seek, seekEnd, limit } = input; |
| 37 | + |
| 38 | + if (typeof seek !== 'number') { |
| 39 | + seek_ = auditUtils.decodeAuditEventId(seek); |
| 40 | + } |
| 41 | + if (typeof seekEnd !== 'number') { |
| 42 | + seekEnd_ = auditUtils.decodeAuditEventId(seekEnd); |
| 43 | + } |
| 44 | + |
| 45 | + const { audit, db }: { audit: Audit; db: DB } = this.container; |
| 46 | + |
| 47 | + yield* db.withTransactionG(async function* (tran): AsyncGenerator< |
| 48 | + AgentRPCResponseResult<AgentAuditMessage<AuditEvent>> |
| 49 | + > { |
| 50 | + for await (const auditEvent of audit.getAuditEvents( |
| 51 | + [], |
| 52 | + { |
| 53 | + seek: seek_, |
| 54 | + seekEnd: seekEnd_, |
| 55 | + limit, |
| 56 | + }, |
| 57 | + tran, |
| 58 | + )) { |
| 59 | + ctx.signal.throwIfAborted(); |
| 60 | + yield { |
| 61 | + id: auditUtils.encodeAuditEventId(auditEvent.id), |
| 62 | + path: auditEvent.path, |
| 63 | + data: auditEvent.data, |
| 64 | + }; |
| 65 | + } |
| 66 | + }); |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +export default NodesAuditEventsGet; |
0 commit comments