@dumanarge/xssrv-shared-lib 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var OutboxRepository_1, OutboxWorker_1, RabbitMQEventPublisher_1;
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.RabbitMQEventPublisher = exports.OutboxWorker = exports.EventPublisher = exports.OutboxRepository = void 0;
14
+ const common_1 = require("@nestjs/common");
15
+ const span_decorator_1 = require("../tracing/span.decorator");
16
+ let OutboxRepository = OutboxRepository_1 = class OutboxRepository {
17
+ prisma;
18
+ logger = new common_1.Logger(OutboxRepository_1.name);
19
+ constructor(prisma) {
20
+ this.prisma = prisma;
21
+ }
22
+ async createInTransaction(tx, event) {
23
+ return tx.outboxEvent.create({
24
+ data: {
25
+ aggregateType: event.aggregateType,
26
+ aggregateId: event.aggregateId,
27
+ eventType: event.eventType,
28
+ payload: event.payload,
29
+ metadata: event.metadata || {},
30
+ retryCount: 0,
31
+ maxRetries: event.maxRetries || 5,
32
+ },
33
+ });
34
+ }
35
+ async fetchUnprocessed(batchSize = 50) {
36
+ return this.prisma.outboxEvent.findMany({
37
+ where: {
38
+ processedAt: null,
39
+ retryCount: { lt: this.prisma.outboxEvent.fields?.maxRetries || 5 },
40
+ },
41
+ orderBy: { createdAt: 'asc' },
42
+ take: batchSize,
43
+ });
44
+ }
45
+ async markProcessed(eventId) {
46
+ await this.prisma.outboxEvent.update({
47
+ where: { id: eventId },
48
+ data: { processedAt: new Date() },
49
+ });
50
+ }
51
+ async incrementRetry(eventId) {
52
+ await this.prisma.outboxEvent.update({
53
+ where: { id: eventId },
54
+ data: { retryCount: { increment: 1 } },
55
+ });
56
+ }
57
+ };
58
+ exports.OutboxRepository = OutboxRepository;
59
+ exports.OutboxRepository = OutboxRepository = OutboxRepository_1 = __decorate([
60
+ (0, common_1.Injectable)(),
61
+ __metadata("design:paramtypes", [Object])
62
+ ], OutboxRepository);
63
+ class EventPublisher {
64
+ }
65
+ exports.EventPublisher = EventPublisher;
66
+ let OutboxWorker = OutboxWorker_1 = class OutboxWorker {
67
+ outboxRepo;
68
+ eventPublisher;
69
+ logger = new common_1.Logger(OutboxWorker_1.name);
70
+ intervalId = null;
71
+ pollIntervalMs;
72
+ batchSize;
73
+ isProcessing = false;
74
+ constructor(outboxRepo, eventPublisher) {
75
+ this.outboxRepo = outboxRepo;
76
+ this.eventPublisher = eventPublisher;
77
+ this.pollIntervalMs = parseInt(process.env.OUTBOX_POLL_INTERVAL_MS || '1000', 10);
78
+ this.batchSize = parseInt(process.env.OUTBOX_BATCH_SIZE || '50', 10);
79
+ }
80
+ onModuleInit() {
81
+ this.logger.log(`Starting outbox worker (poll: ${this.pollIntervalMs}ms, batch: ${this.batchSize})`);
82
+ this.intervalId = setInterval(() => this.processOutbox(), this.pollIntervalMs);
83
+ }
84
+ onModuleDestroy() {
85
+ if (this.intervalId) {
86
+ clearInterval(this.intervalId);
87
+ this.logger.log('Outbox worker stopped');
88
+ }
89
+ }
90
+ async processOutbox() {
91
+ if (this.isProcessing)
92
+ return;
93
+ this.isProcessing = true;
94
+ try {
95
+ const events = await this.outboxRepo.fetchUnprocessed(this.batchSize);
96
+ if (events.length === 0)
97
+ return;
98
+ this.logger.debug(`Processing ${events.length} outbox events`);
99
+ for (const event of events) {
100
+ try {
101
+ await this.eventPublisher.publish(event.eventType, {
102
+ aggregateType: event.aggregateType,
103
+ aggregateId: event.aggregateId,
104
+ payload: event.payload,
105
+ metadata: event.metadata,
106
+ timestamp: event.createdAt,
107
+ });
108
+ await this.outboxRepo.markProcessed(event.id);
109
+ }
110
+ catch (error) {
111
+ this.logger.error(`Failed to process outbox event ${event.id}: ${error.message}`);
112
+ await this.outboxRepo.incrementRetry(event.id);
113
+ }
114
+ }
115
+ }
116
+ catch (error) {
117
+ this.logger.error(`Outbox processing error: ${error.message}`);
118
+ }
119
+ finally {
120
+ this.isProcessing = false;
121
+ }
122
+ }
123
+ };
124
+ exports.OutboxWorker = OutboxWorker;
125
+ __decorate([
126
+ (0, span_decorator_1.Span)('outbox.process'),
127
+ __metadata("design:type", Function),
128
+ __metadata("design:paramtypes", []),
129
+ __metadata("design:returntype", Promise)
130
+ ], OutboxWorker.prototype, "processOutbox", null);
131
+ exports.OutboxWorker = OutboxWorker = OutboxWorker_1 = __decorate([
132
+ (0, common_1.Injectable)(),
133
+ __metadata("design:paramtypes", [OutboxRepository,
134
+ EventPublisher])
135
+ ], OutboxWorker);
136
+ let RabbitMQEventPublisher = RabbitMQEventPublisher_1 = class RabbitMQEventPublisher extends EventPublisher {
137
+ amqpConnection;
138
+ logger = new common_1.Logger(RabbitMQEventPublisher_1.name);
139
+ constructor(amqpConnection) {
140
+ super();
141
+ this.amqpConnection = amqpConnection;
142
+ }
143
+ async publish(routingKey, payload) {
144
+ const exchange = process.env.RABBITMQ_EXCHANGE || 'xenonsmart.events';
145
+ await this.amqpConnection.publish(exchange, routingKey, Buffer.from(JSON.stringify({
146
+ ...payload,
147
+ publishedAt: new Date().toISOString(),
148
+ })), {
149
+ persistent: true,
150
+ contentType: 'application/json',
151
+ headers: {
152
+ 'x-event-type': routingKey,
153
+ 'x-source-service': process.env.SERVICE_NAME,
154
+ },
155
+ });
156
+ this.logger.debug(`Published event: ${routingKey}`);
157
+ }
158
+ };
159
+ exports.RabbitMQEventPublisher = RabbitMQEventPublisher;
160
+ __decorate([
161
+ (0, span_decorator_1.Span)('rabbitmq.publish'),
162
+ __metadata("design:type", Function),
163
+ __metadata("design:paramtypes", [String, Object]),
164
+ __metadata("design:returntype", Promise)
165
+ ], RabbitMQEventPublisher.prototype, "publish", null);
166
+ exports.RabbitMQEventPublisher = RabbitMQEventPublisher = RabbitMQEventPublisher_1 = __decorate([
167
+ (0, common_1.Injectable)(),
168
+ __metadata("design:paramtypes", [Object])
169
+ ], RabbitMQEventPublisher);
170
+ //# sourceMappingURL=outbox.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"outbox.module.js","sourceRoot":"","sources":["../../src/outbox/outbox.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAUA,2CAAmF;AACnF,8DAAiD;AAkB1C,IAAM,gBAAgB,wBAAtB,MAAM,gBAAgB;IAGE;IAFZ,MAAM,GAAG,IAAI,eAAM,CAAC,kBAAgB,CAAC,IAAI,CAAC,CAAC;IAE5D,YAA6B,MAAW;QAAX,WAAM,GAAN,MAAM,CAAK;IAAG,CAAC;IAgB5C,KAAK,CAAC,mBAAmB,CAAC,EAAO,EAAE,KAAkB;QACnD,OAAO,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC;YAC3B,IAAI,EAAE;gBACJ,aAAa,EAAE,KAAK,CAAC,aAAa;gBAClC,WAAW,EAAE,KAAK,CAAC,WAAW;gBAC9B,SAAS,EAAE,KAAK,CAAC,SAAS;gBAC1B,OAAO,EAAE,KAAK,CAAC,OAAc;gBAC7B,QAAQ,EAAE,KAAK,CAAC,QAAe,IAAI,EAAE;gBACrC,UAAU,EAAE,CAAC;gBACb,UAAU,EAAE,KAAK,CAAC,UAAU,IAAI,CAAC;aAClC;SACF,CAAC,CAAC;IACL,CAAC;IAKD,KAAK,CAAC,gBAAgB,CAAC,YAAoB,EAAE;QAC3C,OAAO,IAAI,CAAC,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I,mBAAmB,CAAC;QAEtE,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,QAAQ,EAAE,UAAU,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;YACjF,GAAG,OAAO;YACV,WAAW,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;SACtC,CAAC,CAAC,EAAE;YACH,UAAU,EAAE,IAAI;YAChB,WAAW,EAAE,kBAAkB;YAC/B,OAAO,EAAE;gBACP,cAAc,EAAE,UAAU;gBAC1B,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY;aAC7C;SACF,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,oBAAoB,UAAU,EAAE,CAAC,CAAC;IACtD,CAAC;CACF,CAAA;AAzBY,wDAAsB;AAQ3B;IADL,IAAA,qBAAI,EAAC,kBAAkB,CAAC;;;;qDAiBxB;iCAxBU,sBAAsB;IADlC,IAAA,mBAAU,GAAE;;GACA,sBAAsB,CAyBlC"}
@@ -0,0 +1,3 @@
1
+ import { trace, SpanStatusCode, context } from '@opentelemetry/api';
2
+ export declare function Span(name?: string): MethodDecorator;
3
+ export { trace, context, SpanStatusCode };
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SpanStatusCode = exports.context = exports.trace = void 0;
4
+ exports.Span = Span;
5
+ const api_1 = require("@opentelemetry/api");
6
+ Object.defineProperty(exports, "trace", { enumerable: true, get: function () { return api_1.trace; } });
7
+ Object.defineProperty(exports, "SpanStatusCode", { enumerable: true, get: function () { return api_1.SpanStatusCode; } });
8
+ Object.defineProperty(exports, "context", { enumerable: true, get: function () { return api_1.context; } });
9
+ function Span(name) {
10
+ return (target, propertyKey, descriptor) => {
11
+ const originalMethod = descriptor.value;
12
+ const spanName = name || `${target.constructor.name}.${String(propertyKey)}`;
13
+ descriptor.value = async function (...args) {
14
+ const tracer = api_1.trace.getTracer('xenonsmart-backend');
15
+ return tracer.startActiveSpan(spanName, async (span) => {
16
+ try {
17
+ span.setAttribute('code.function', String(propertyKey));
18
+ span.setAttribute('code.namespace', target.constructor.name);
19
+ const result = await originalMethod.apply(this, args);
20
+ span.setStatus({ code: api_1.SpanStatusCode.OK });
21
+ return result;
22
+ }
23
+ catch (error) {
24
+ span.setStatus({
25
+ code: api_1.SpanStatusCode.ERROR,
26
+ message: error instanceof Error ? error.message : 'Unknown error',
27
+ });
28
+ span.recordException(error);
29
+ throw error;
30
+ }
31
+ finally {
32
+ span.end();
33
+ }
34
+ });
35
+ };
36
+ return descriptor;
37
+ };
38
+ }
39
+ //# sourceMappingURL=span.decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"span.decorator.js","sourceRoot":"","sources":["../../src/tracing/span.decorator.ts"],"names":[],"mappings":";;;AAUA,oBA8BC;AAhCD,4CAAoE;AAkC3D,sFAlCA,WAAK,OAkCA;AAAW,+FAlCT,oBAAc,OAkCS;AAAvB,wFAlCgB,aAAO,OAkChB;AAhCvB,SAAgB,IAAI,CAAC,IAAa;IAChC,OAAO,CAAC,MAAW,EAAE,WAA4B,EAAE,UAA8B,EAAE,EAAE;QACnF,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAC;QACxC,MAAM,QAAQ,GAAG,IAAI,IAAI,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,IAAI,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC;QAE7E,UAAU,CAAC,KAAK,GAAG,KAAK,WAAW,GAAG,IAAW;YAC/C,MAAM,MAAM,GAAG,WAAK,CAAC,SAAS,CAAC,oBAAoB,CAAC,CAAC;YACrD,OAAO,MAAM,CAAC,eAAe,CAAC,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;gBACrD,IAAI,CAAC;oBACH,IAAI,CAAC,YAAY,CAAC,eAAe,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;oBACxD,IAAI,CAAC,YAAY,CAAC,gBAAgB,EAAE,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;oBAE7D,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;oBACtD,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,oBAAc,CAAC,EAAE,EAAE,CAAC,CAAC;oBAC5C,OAAO,MAAM,CAAC;gBAChB,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,IAAI,CAAC,SAAS,CAAC;wBACb,IAAI,EAAE,oBAAc,CAAC,KAAK;wBAC1B,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe;qBAClE,CAAC,CAAC;oBACH,IAAI,CAAC,eAAe,CAAC,KAAc,CAAC,CAAC;oBACrC,MAAM,KAAK,CAAC;gBACd,CAAC;wBAAS,CAAC;oBACT,IAAI,CAAC,GAAG,EAAE,CAAC;gBACb,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { NodeSDK } from '@opentelemetry/sdk-node';
2
+ declare const sdk: NodeSDK;
3
+ export { sdk };
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sdk = void 0;
4
+ const sdk_node_1 = require("@opentelemetry/sdk-node");
5
+ const exporter_trace_otlp_grpc_1 = require("@opentelemetry/exporter-trace-otlp-grpc");
6
+ const exporter_metrics_otlp_grpc_1 = require("@opentelemetry/exporter-metrics-otlp-grpc");
7
+ const auto_instrumentations_node_1 = require("@opentelemetry/auto-instrumentations-node");
8
+ const resources_1 = require("@opentelemetry/resources");
9
+ const sdk_metrics_1 = require("@opentelemetry/sdk-metrics");
10
+ const api_1 = require("@opentelemetry/api");
11
+ if (process.env.OTEL_DEBUG === 'true') {
12
+ api_1.diag.setLogger(new api_1.DiagConsoleLogger(), api_1.DiagLogLevel.DEBUG);
13
+ }
14
+ const serviceName = process.env.SERVICE_NAME || 'unknown-service';
15
+ const serviceVersion = process.env.SERVICE_VERSION || '1.0.0';
16
+ const otelEndpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT || 'http://localhost:4317';
17
+ const environment = process.env.NODE_ENV || 'development';
18
+ const resource = (0, resources_1.resourceFromAttributes)({
19
+ 'service.name': serviceName,
20
+ 'service.version': serviceVersion,
21
+ 'deployment.environment.name': environment,
22
+ 'service.namespace': 'xenonsmart-backend',
23
+ });
24
+ const traceExporter = new exporter_trace_otlp_grpc_1.OTLPTraceExporter({
25
+ url: otelEndpoint,
26
+ });
27
+ const metricReader = new sdk_metrics_1.PeriodicExportingMetricReader({
28
+ exporter: new exporter_metrics_otlp_grpc_1.OTLPMetricExporter({ url: otelEndpoint }),
29
+ exportIntervalMillis: 15000,
30
+ });
31
+ const sdk = new sdk_node_1.NodeSDK({
32
+ resource,
33
+ traceExporter,
34
+ metricReader: metricReader,
35
+ instrumentations: [
36
+ (0, auto_instrumentations_node_1.getNodeAutoInstrumentations)({
37
+ '@opentelemetry/instrumentation-fs': { enabled: false },
38
+ '@opentelemetry/instrumentation-dns': { enabled: false },
39
+ '@opentelemetry/instrumentation-net': { enabled: false },
40
+ '@opentelemetry/instrumentation-fastify': { enabled: true },
41
+ '@opentelemetry/instrumentation-http': { enabled: true },
42
+ '@opentelemetry/instrumentation-grpc': { enabled: true },
43
+ '@opentelemetry/instrumentation-ioredis': { enabled: true },
44
+ '@opentelemetry/instrumentation-pg': { enabled: true },
45
+ }),
46
+ ],
47
+ });
48
+ exports.sdk = sdk;
49
+ sdk.start();
50
+ const shutdown = async () => {
51
+ try {
52
+ await sdk.shutdown();
53
+ console.log('OpenTelemetry SDK shut down successfully');
54
+ }
55
+ catch (err) {
56
+ console.error('Error shutting down OpenTelemetry SDK', err);
57
+ }
58
+ };
59
+ process.on('SIGTERM', shutdown);
60
+ process.on('SIGINT', shutdown);
61
+ //# sourceMappingURL=tracing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracing.js","sourceRoot":"","sources":["../../src/tracing/tracing.ts"],"names":[],"mappings":";;;AASA,sDAAkD;AAClD,sFAA4E;AAC5E,0FAA+E;AAC/E,0FAAwF;AACxF,wDAAkE;AAClE,4DAA2E;AAC3E,4CAA2E;AAG3E,IAAI,OAAO,CAAC,GAAG,CAAC,UAAU,KAAK,MAAM,EAAE,CAAC;IACtC,UAAI,CAAC,SAAS,CAAC,IAAI,uBAAiB,EAAE,EAAE,kBAAY,CAAC,KAAK,CAAC,CAAC;AAC9D,CAAC;AAED,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,iBAAiB,CAAC;AAClE,MAAM,cAAc,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,OAAO,CAAC;AAC9D,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,2BAA2B,IAAI,uBAAuB,CAAC;AACxF,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,aAAa,CAAC;AAE1D,MAAM,QAAQ,GAAG,IAAA,kCAAsB,EAAC;IACtC,cAAc,EAAE,WAAW;IAC3B,iBAAiB,EAAE,cAAc;IACjC,6BAA6B,EAAE,WAAW;IAC1C,mBAAmB,EAAE,oBAAoB;CAC1C,CAAC,CAAC;AAEH,MAAM,aAAa,GAAG,IAAI,4CAAiB,CAAC;IAC1C,GAAG,EAAE,YAAY;CAClB,CAAC,CAAC;AAEH,MAAM,YAAY,GAAG,IAAI,2CAA6B,CAAC;IACrD,QAAQ,EAAE,IAAI,+CAAkB,CAAC,EAAE,GAAG,EAAE,YAAY,EAAE,CAAC;IACvD,oBAAoB,EAAE,KAAK;CAC5B,CAAC,CAAC;AAEH,MAAM,GAAG,GAAG,IAAI,kBAAO,CAAC;IACtB,QAAQ;IACR,aAAa;IACb,YAAY,EAAE,YAAmB;IACjC,gBAAgB,EAAE;QAChB,IAAA,wDAA2B,EAAC;YAC1B,mCAAmC,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE;YACvD,oCAAoC,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE;YACxD,oCAAoC,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE;YACxD,wCAAwC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;YAC3D,qCAAqC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;YACxD,qCAAqC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;YACxD,wCAAwC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;YAC3D,mCAAmC,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;SACvD,CAAC;KACH;CACF,CAAC,CAAC;AAiBM,kBAAG;AAfZ,GAAG,CAAC,KAAK,EAAE,CAAC;AAGZ,MAAM,QAAQ,GAAG,KAAK,IAAI,EAAE;IAC1B,IAAI,CAAC;QACH,MAAM,GAAG,CAAC,QAAQ,EAAE,CAAC;QACrB,OAAO,CAAC,GAAG,CAAC,0CAA0C,CAAC,CAAC;IAC1D,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,uCAAuC,EAAE,GAAG,CAAC,CAAC;IAC9D,CAAC;AACH,CAAC,CAAC;AAEF,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;AAChC,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,176 @@
1
+ {
2
+ "name": "@dumanarge/xssrv-shared-lib",
3
+ "version": "1.0.0",
4
+ "description": "Shared library for DumanArge IoT Platform services — tracing, logging, health, outbox, auth, config",
5
+ "author": "Oguzhan Karahan <oguzhan.karahan@xenonsmart.com>",
6
+ "license": "MIT",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ },
14
+ "./tracing": {
15
+ "types": "./dist/tracing/tracing.d.ts",
16
+ "default": "./dist/tracing/tracing.js"
17
+ },
18
+ "./logging": {
19
+ "types": "./dist/logging/logging.module.d.ts",
20
+ "default": "./dist/logging/logging.module.js"
21
+ },
22
+ "./health": {
23
+ "types": "./dist/health/health.module.d.ts",
24
+ "default": "./dist/health/health.module.js"
25
+ },
26
+ "./outbox": {
27
+ "types": "./dist/outbox/outbox.module.d.ts",
28
+ "default": "./dist/outbox/outbox.module.js"
29
+ },
30
+ "./auth": {
31
+ "types": "./dist/auth/tenant.middleware.d.ts",
32
+ "default": "./dist/auth/tenant.middleware.js"
33
+ },
34
+ "./config": {
35
+ "types": "./dist/config/graceful-shutdown.d.ts",
36
+ "default": "./dist/config/graceful-shutdown.js"
37
+ }
38
+ },
39
+ "files": [
40
+ "dist",
41
+ "README.md",
42
+ "CHANGELOG.md",
43
+ "LICENSE"
44
+ ],
45
+ "keywords": [
46
+ "nestjs",
47
+ "iot",
48
+ "shared-library",
49
+ "tracing",
50
+ "opentelemetry",
51
+ "logging",
52
+ "pino",
53
+ "health-check",
54
+ "outbox-pattern",
55
+ "multi-tenant",
56
+ "prisma",
57
+ "microservices",
58
+ "dumanarge",
59
+ "xenonsmart"
60
+ ],
61
+ "engines": {
62
+ "node": ">=20.0.0"
63
+ },
64
+ "publishConfig": {
65
+ "access": "public",
66
+ "registry": "https://registry.npmjs.org/"
67
+ },
68
+ "repository": {
69
+ "type": "git",
70
+ "url": "git+https://github.com/dumanarge/xssrv-shared-lib.git"
71
+ },
72
+ "bugs": {
73
+ "url": "https://github.com/dumanarge/xssrv-shared-lib/issues"
74
+ },
75
+ "homepage": "https://github.com/dumanarge/xssrv-shared-lib#readme",
76
+ "peerDependencies": {
77
+ "@nestjs/common": "^11.0.0",
78
+ "@nestjs/config": "^4.0.0",
79
+ "@nestjs/core": "^11.0.0",
80
+ "@nestjs/terminus": "^11.0.0",
81
+ "@opentelemetry/auto-instrumentations-node": "^0.52.0",
82
+ "@opentelemetry/exporter-metrics-otlp-grpc": "^0.55.0",
83
+ "@opentelemetry/exporter-trace-otlp-grpc": "^0.55.0",
84
+ "@opentelemetry/resources": "^1.28.0",
85
+ "@opentelemetry/sdk-metrics": "^1.28.0",
86
+ "@opentelemetry/sdk-node": "^0.55.0",
87
+ "@prisma/client": "^6.0.0",
88
+ "class-transformer": "^0.5.1",
89
+ "class-validator": "^0.14.0",
90
+ "express": "^4.21.0",
91
+ "ioredis": "^5.4.0",
92
+ "nestjs-pino": "^4.0.0",
93
+ "pino": "^9.0.0",
94
+ "pino-http": "^10.0.0",
95
+ "reflect-metadata": "^0.2.0",
96
+ "rxjs": "^7.8.0"
97
+ },
98
+ "peerDependenciesMeta": {
99
+ "@opentelemetry/auto-instrumentations-node": {
100
+ "optional": true
101
+ },
102
+ "@opentelemetry/exporter-metrics-otlp-grpc": {
103
+ "optional": true
104
+ },
105
+ "@opentelemetry/exporter-trace-otlp-grpc": {
106
+ "optional": true
107
+ },
108
+ "@opentelemetry/resources": {
109
+ "optional": true
110
+ },
111
+ "@opentelemetry/sdk-metrics": {
112
+ "optional": true
113
+ },
114
+ "@opentelemetry/sdk-node": {
115
+ "optional": true
116
+ },
117
+ "@prisma/client": {
118
+ "optional": true
119
+ },
120
+ "ioredis": {
121
+ "optional": true
122
+ },
123
+ "nestjs-pino": {
124
+ "optional": true
125
+ },
126
+ "pino": {
127
+ "optional": true
128
+ },
129
+ "pino-http": {
130
+ "optional": true
131
+ },
132
+ "@nestjs/terminus": {
133
+ "optional": true
134
+ },
135
+ "@nestjs/config": {
136
+ "optional": true
137
+ }
138
+ },
139
+ "devDependencies": {
140
+ "@nestjs/common": "^11.1.14",
141
+ "@nestjs/config": "^4.0.3",
142
+ "@nestjs/core": "^11.1.14",
143
+ "@nestjs/terminus": "^11.1.1",
144
+ "@opentelemetry/api": "^1.9.0",
145
+ "@opentelemetry/auto-instrumentations-node": "^0.70.0",
146
+ "@opentelemetry/exporter-metrics-otlp-grpc": "^0.212.0",
147
+ "@opentelemetry/exporter-trace-otlp-grpc": "^0.212.0",
148
+ "@opentelemetry/instrumentation-fastify": "^0.56.0",
149
+ "@opentelemetry/instrumentation-grpc": "^0.212.0",
150
+ "@opentelemetry/resources": "^2.5.1",
151
+ "@opentelemetry/sdk-metrics": "^2.5.1",
152
+ "@opentelemetry/sdk-node": "^0.212.0",
153
+ "@prisma/client": "^7.4.1",
154
+ "@types/express": "^5.0.0",
155
+ "@types/jest": "^29.5.0",
156
+ "@types/node": "^22.0.0",
157
+ "class-transformer": "^0.5.1",
158
+ "class-validator": "^0.14.3",
159
+ "express": "^5.2.1",
160
+ "ioredis": "^5.9.3",
161
+ "jest": "^29.7.0",
162
+ "nestjs-pino": "^4.6.0",
163
+ "pino": "^10.3.1",
164
+ "pino-http": "^11.0.0",
165
+ "reflect-metadata": "^0.2.2",
166
+ "rimraf": "^6.0.0",
167
+ "rxjs": "^7.8.2",
168
+ "ts-jest": "^29.2.0",
169
+ "typescript": "^5.7.0"
170
+ },
171
+ "scripts": {
172
+ "build": "rm -rf dist tsconfig.build.tsbuildinfo && tsc -p tsconfig.build.json",
173
+ "lint": "eslint 'src/**/*.ts'",
174
+ "test": "jest"
175
+ }
176
+ }