@logistically/events
Version:
A production-ready event-driven architecture library for NestJS with Redis Streams, comprehensive batching, reliable consumption, and enterprise-grade features.
47 lines (46 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisStreamsClientProxy = void 0;
const microservices_1 = require("@nestjs/microservices");
const ioredis_1 = require("ioredis");
class RedisStreamsClientProxy extends microservices_1.ClientProxy {
constructor(options) {
var _a;
super();
this.options = options;
this.client = new ioredis_1.default({
host: options.host,
port: options.port,
password: options.password,
tls: options.tls,
connectTimeout: 10000, // Add connection timeout
commandTimeout: 5000, // Add command timeout
maxRetriesPerRequest: 3,
});
this.verbose = (_a = options.verbose) !== null && _a !== void 0 ? _a : false;
}
async connect() {
return this.client;
}
async close() {
await this.client.quit();
}
async dispatchEvent(packet, options) {
try {
const { pattern, data } = packet;
const stream = (options === null || options === void 0 ? void 0 : options.stream) || this.options.stream;
await this.client.xadd(stream, '*', 'data', JSON.stringify({ pattern, data }));
if (this.verbose)
console.log(`[RedisStreamsClientProxy] Published event to stream ${stream} with pattern ${pattern}`);
}
catch (err) {
if (this.verbose)
console.error('[RedisStreamsClientProxy] Error publishing event:', err);
}
}
publish(packet, callback) {
// Not implemented: request-response pattern for streams
return () => { };
}
}
exports.RedisStreamsClientProxy = RedisStreamsClientProxy;