@car-parts/common 1.0.5 → 1.0.7
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.
- package/build/events/base-listener.d.ts +9 -12
- package/build/events/base-listener.js +24 -67
- package/build/events/base-publisher.d.ts +4 -7
- package/build/events/base-publisher.js +10 -81
- package/build/events/subjects.d.ts +1 -1
- package/build/events/subjects.js +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.js +1 -1
- package/build/middlewares/restrict-to.d.ts +3 -0
- package/build/middlewares/restrict-to.js +12 -0
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Message, Stan } from 'node-nats-streaming';
|
|
2
2
|
import { Subjects } from './subjects';
|
|
3
3
|
interface Event {
|
|
4
4
|
subject: Subjects;
|
|
@@ -6,16 +6,13 @@ interface Event {
|
|
|
6
6
|
}
|
|
7
7
|
export declare abstract class Listener<T extends Event> {
|
|
8
8
|
abstract subject: T['subject'];
|
|
9
|
-
abstract
|
|
10
|
-
abstract onMessage(data: T['data'],
|
|
11
|
-
protected
|
|
12
|
-
protected
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
parseMessage(message: KafkaMessage): any;
|
|
18
|
-
disconnect(): Promise<void>;
|
|
19
|
-
commitOffsets(offsets: Array<TopicPartitionOffsetAndMetadata>): Promise<void>;
|
|
9
|
+
abstract queueGroupName: string;
|
|
10
|
+
abstract onMessage(data: T['data'], msg: Message): void;
|
|
11
|
+
protected client: Stan;
|
|
12
|
+
protected ackWait: number;
|
|
13
|
+
constructor(client: Stan);
|
|
14
|
+
subscriptionOptions(): import("node-nats-streaming").SubscriptionOptions;
|
|
15
|
+
listen(): void;
|
|
16
|
+
parseMessage(msg: Message): any;
|
|
20
17
|
}
|
|
21
18
|
export {};
|
|
@@ -1,78 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.Listener = void 0;
|
|
13
4
|
class Listener {
|
|
14
|
-
constructor(
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
this.consumerConfig = consumerConfig;
|
|
5
|
+
constructor(client) {
|
|
6
|
+
this.ackWait = 5 * 1000;
|
|
7
|
+
this.client = client;
|
|
18
8
|
}
|
|
19
|
-
|
|
20
|
-
return
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
topic: this.subject,
|
|
27
|
-
fromBeginning: true, // Similar to setDeliverAllAvailable in NATS
|
|
28
|
-
});
|
|
29
|
-
yield this.consumer.run({
|
|
30
|
-
eachMessage: (_a) => __awaiter(this, [_a], void 0, function* ({ topic, partition, message, }) {
|
|
31
|
-
console.log(`Message received: ${topic} / ${this.groupId} / Partition: ${partition}`);
|
|
32
|
-
try {
|
|
33
|
-
const parsedData = this.parseMessage(message);
|
|
34
|
-
yield this.onMessage(parsedData, message);
|
|
35
|
-
// Kafka automatically commits offsets by default (autoCommit: true)
|
|
36
|
-
// If you want manual commit, set autoCommit to false in run() options
|
|
37
|
-
}
|
|
38
|
-
catch (err) {
|
|
39
|
-
console.error('Error processing message:', err);
|
|
40
|
-
// Implement your error handling strategy here
|
|
41
|
-
// You might want to:
|
|
42
|
-
// 1. Skip the message and continue
|
|
43
|
-
// 2. Send to dead letter queue
|
|
44
|
-
// 3. Retry with backoff
|
|
45
|
-
throw err; // This will pause the consumer
|
|
46
|
-
}
|
|
47
|
-
}),
|
|
48
|
-
});
|
|
49
|
-
// Handle errors
|
|
50
|
-
this.consumer.on('consumer.crash', (event) => {
|
|
51
|
-
console.error('Consumer crashed:', event.payload.error);
|
|
52
|
-
});
|
|
53
|
-
this.consumer.on('consumer.disconnect', () => {
|
|
54
|
-
console.log('Consumer disconnected');
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
parseMessage(message) {
|
|
59
|
-
if (!message.value) {
|
|
60
|
-
throw new Error('Message value is null');
|
|
61
|
-
}
|
|
62
|
-
const data = message.value.toString('utf8');
|
|
63
|
-
return JSON.parse(data);
|
|
9
|
+
subscriptionOptions() {
|
|
10
|
+
return this.client
|
|
11
|
+
.subscriptionOptions()
|
|
12
|
+
.setDeliverAllAvailable()
|
|
13
|
+
.setManualAckMode(true)
|
|
14
|
+
.setAckWait(this.ackWait)
|
|
15
|
+
.setDurableName(this.queueGroupName);
|
|
64
16
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
17
|
+
// subscribe for event
|
|
18
|
+
listen() {
|
|
19
|
+
const subscription = this.client.subscribe(this.subject, this.queueGroupName, this.subscriptionOptions());
|
|
20
|
+
// msg.getSequence(); // returns event number
|
|
21
|
+
// msg.getData() // returns data | Buffer
|
|
22
|
+
subscription.on('message', (msg) => {
|
|
23
|
+
console.log(`Message received: ${this.subject} / ${this.queueGroupName}`);
|
|
24
|
+
const parsedData = this.parseMessage(msg);
|
|
25
|
+
this.onMessage(parsedData, msg);
|
|
69
26
|
});
|
|
70
27
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return
|
|
74
|
-
|
|
75
|
-
|
|
28
|
+
parseMessage(msg) {
|
|
29
|
+
const data = msg.getData(); // string | buffer
|
|
30
|
+
return typeof data === 'string'
|
|
31
|
+
? JSON.parse(data)
|
|
32
|
+
: JSON.parse(data.toString('utf8'));
|
|
76
33
|
}
|
|
77
34
|
}
|
|
78
35
|
exports.Listener = Listener;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Stan } from 'node-nats-streaming';
|
|
2
2
|
import { Subjects } from './subjects';
|
|
3
3
|
interface Event {
|
|
4
4
|
subject: Subjects;
|
|
@@ -6,11 +6,8 @@ interface Event {
|
|
|
6
6
|
}
|
|
7
7
|
export declare abstract class Publisher<T extends Event> {
|
|
8
8
|
abstract subject: T['subject'];
|
|
9
|
-
protected
|
|
10
|
-
constructor(
|
|
11
|
-
publish(data: T['data']): Promise<
|
|
12
|
-
publishBatch(dataArray: T['data'][]): Promise<RecordMetadata[]>;
|
|
13
|
-
publishWithKey(data: T['data'], key: string): Promise<RecordMetadata[]>;
|
|
14
|
-
disconnect(): Promise<void>;
|
|
9
|
+
protected client: Stan;
|
|
10
|
+
constructor(client: Stan);
|
|
11
|
+
publish(data: T['data']): Promise<void>;
|
|
15
12
|
}
|
|
16
13
|
export {};
|
|
@@ -1,90 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.Publisher = void 0;
|
|
13
4
|
class Publisher {
|
|
14
|
-
constructor(
|
|
15
|
-
this.
|
|
5
|
+
constructor(client) {
|
|
6
|
+
this.client = client;
|
|
16
7
|
}
|
|
17
8
|
publish(data) {
|
|
18
|
-
return
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// key: data.id,
|
|
27
|
-
// Optional: add headers
|
|
28
|
-
// headers: {
|
|
29
|
-
// 'correlation-id': 'some-id',
|
|
30
|
-
// },
|
|
31
|
-
},
|
|
32
|
-
],
|
|
33
|
-
});
|
|
34
|
-
console.log('Event published to topic:', this.subject);
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
37
|
-
catch (err) {
|
|
38
|
-
console.error('Error publishing event:', err);
|
|
39
|
-
throw err;
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
// Publish multiple messages at once (batch)
|
|
44
|
-
publishBatch(dataArray) {
|
|
45
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
try {
|
|
47
|
-
const result = yield this.producer.send({
|
|
48
|
-
topic: this.subject,
|
|
49
|
-
messages: dataArray.map((data) => ({
|
|
50
|
-
value: JSON.stringify(data),
|
|
51
|
-
})),
|
|
52
|
-
});
|
|
53
|
-
console.log(`${dataArray.length} events published to topic:`, this.subject);
|
|
54
|
-
return result;
|
|
55
|
-
}
|
|
56
|
-
catch (err) {
|
|
57
|
-
console.error('Error publishing batch events:', err);
|
|
58
|
-
throw err;
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
// Publish with custom partition key
|
|
63
|
-
publishWithKey(data, key) {
|
|
64
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
-
try {
|
|
66
|
-
const result = yield this.producer.send({
|
|
67
|
-
topic: this.subject,
|
|
68
|
-
messages: [
|
|
69
|
-
{
|
|
70
|
-
key: key,
|
|
71
|
-
value: JSON.stringify(data),
|
|
72
|
-
},
|
|
73
|
-
],
|
|
74
|
-
});
|
|
75
|
-
console.log('Event published to topic:', this.subject, 'with key:', key);
|
|
76
|
-
return result;
|
|
77
|
-
}
|
|
78
|
-
catch (err) {
|
|
79
|
-
console.error('Error publishing event:', err);
|
|
80
|
-
throw err;
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
disconnect() {
|
|
85
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
-
yield this.producer.disconnect();
|
|
87
|
-
console.log('Kafka producer disconnected');
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
this.client.publish(this.subject, JSON.stringify(data), (err) => {
|
|
11
|
+
if (err) {
|
|
12
|
+
return reject(err);
|
|
13
|
+
}
|
|
14
|
+
console.log('Event published to subject', this.subject);
|
|
15
|
+
resolve();
|
|
16
|
+
});
|
|
88
17
|
});
|
|
89
18
|
}
|
|
90
19
|
}
|
package/build/events/subjects.js
CHANGED
|
@@ -3,6 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Subjects = void 0;
|
|
4
4
|
var Subjects;
|
|
5
5
|
(function (Subjects) {
|
|
6
|
-
Subjects["UserCreated"] = "
|
|
6
|
+
Subjects["UserCreated"] = "user:created";
|
|
7
7
|
})(Subjects || (exports.Subjects = Subjects = {}));
|
|
8
8
|
// interface to make sure event names are consistent
|
package/build/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export * from './middlewares/current-user';
|
|
|
9
9
|
export * from './middlewares/error-handler';
|
|
10
10
|
export * from './middlewares/require-auth';
|
|
11
11
|
export * from './middlewares/validate-request';
|
|
12
|
-
export * from './middlewares/
|
|
12
|
+
export * from './middlewares/restrict-to';
|
|
13
13
|
export * from './events/base-listener';
|
|
14
14
|
export * from './events/base-publisher';
|
|
15
15
|
export * from './events/subjects';
|
package/build/index.js
CHANGED
|
@@ -25,7 +25,7 @@ __exportStar(require("./middlewares/current-user"), exports);
|
|
|
25
25
|
__exportStar(require("./middlewares/error-handler"), exports);
|
|
26
26
|
__exportStar(require("./middlewares/require-auth"), exports);
|
|
27
27
|
__exportStar(require("./middlewares/validate-request"), exports);
|
|
28
|
-
__exportStar(require("./middlewares/
|
|
28
|
+
__exportStar(require("./middlewares/restrict-to"), exports);
|
|
29
29
|
__exportStar(require("./events/base-listener"), exports);
|
|
30
30
|
__exportStar(require("./events/base-publisher"), exports);
|
|
31
31
|
__exportStar(require("./events/subjects"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.restrictTo = void 0;
|
|
4
|
+
const forbidden_error_1 = require("../errors/forbidden-error");
|
|
5
|
+
const restrictTo = (...roles) => (req, res, next) => {
|
|
6
|
+
// roles is an array ['admin']
|
|
7
|
+
if (!roles.includes(req.currentUser.role)) {
|
|
8
|
+
throw new forbidden_error_1.ForbiddenError(); // forbidden
|
|
9
|
+
}
|
|
10
|
+
next();
|
|
11
|
+
};
|
|
12
|
+
exports.restrictTo = restrictTo;
|