@suissa/universal-queues
Version:
Factory universal para mensageria (RabbitMQ, Kafka, SQS) para sistemas distribuĂdos.
28 lines (24 loc) • 720 B
text/typescript
import { v4 as uuidv4 } from 'uuid';
import { createHash } from 'crypto';
export type Envelope = {
headers: Record<string, any>;
eventId: string;
hash: string;
origin: string;
};
export function enrichHeaders(
exchangeOrTopic: string,
routingKeyOrSubject: string,
message: object,
headers: Record<string, any> = {}
): Envelope {
const eventId = headers['x-event-id'] || uuidv4();
const hash = createHash('sha256').update(JSON.stringify(message)).digest('hex');
const origin = `${exchangeOrTopic}.${routingKeyOrSubject}`;
return {
headers: { ...headers, 'x-event-id': eventId, 'x-event-hash': hash, 'x-origin': origin },
eventId,
hash,
origin,
};
}