import { DeviceInfo } from '../types/device'; import { UnionOmit } from './common'; export type MessageBodies = { report: { installIds: string[]; }; reportRequest: Record; synchronize: | { syncType: 'redis'; } | { syncType: 'list'; installs: DeviceInfo[]; }; keyRequest: { obnizId?: string; requestId: string; key: string; }; keyRequestResponse: { results: { [key: string]: string }; requestId: string; }; /** * Worker-to-worker request. Sent from a Slave and received by other Slaves. * When obnizId is set, only the Slave hosting that obnizId should respond. */ workerKeyRequest: { obnizId?: string; requestId: string; key: string; }; /** * Worker-to-worker response. Sent from a receiving Slave back to the * requesting Slave (direct, toManager:false). */ workerKeyRequestResponse: { results: { [key: string]: string }; requestId: string; }; }; export type MessageKeys = keyof MessageBodies; const MessageKeysArray = [ 'report', 'reportRequest', 'synchronize', 'keyRequest', 'keyRequestResponse', 'workerKeyRequest', 'workerKeyRequestResponse', ] satisfies (keyof MessageBodies)[]; export type MessageInfo = | { to: string; toManager: boolean; sendMode: 'direct'; from: string; } | { toManager: boolean; sendMode: 'broadcast'; from: string; }; export type MessageInfoOmitFrom = UnionOmit['info'], 'from'>; export type Message = { action: ActionName; info: MessageInfo; } & { body: MessageBodies[ActionName] }; export type Messages = T extends MessageKeys ? Message : never; export type MessagesUnion = Messages; export const isValidMessage = (mes: any): mes is Messages => { return mes.action !== undefined && MessageKeysArray.includes(mes.action); };