@mjtickets981/common 1.0.4 → 1.0.6

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,18 @@
1
+ import { Message, Stan } from "node-nats-streaming";
2
+ import { Subjects } from "./subjects";
3
+ interface Event {
4
+ subject: Subjects;
5
+ data: any;
6
+ }
7
+ export declare abstract class Listener<T extends Event> {
8
+ abstract subject: T["subject"];
9
+ abstract queueGroupName: string;
10
+ abstract onMessage(data: T["data"], msg: Message): void;
11
+ private client;
12
+ protected ackWait: number;
13
+ constructor(client: Stan);
14
+ subscriptionOptions(): import("node-nats-streaming").SubscriptionOptions;
15
+ listen(): void;
16
+ parseMessage(msg: Message): any;
17
+ }
18
+ export {};
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Listener = void 0;
4
+ class Listener {
5
+ constructor(client) {
6
+ this.ackWait = 5 * 1000; // Default ack wait time of 5 seconds
7
+ this.client = client;
8
+ }
9
+ subscriptionOptions() {
10
+ return this.client
11
+ .subscriptionOptions()
12
+ .setDeliverAllAvailable()
13
+ .setManualAckMode(true)
14
+ .setAckWait(this.ackWait)
15
+ .setDurableName(this.queueGroupName);
16
+ } // Enable manual acknowledgment mode
17
+ // Then the none-nats streaming library is no longer going to automatically acknowledge (or) tell the NATS Streaming Server that we have successfully
18
+ // processed the message as soon as we receive it. Instead, we are going to have to manually acknowledge it by calling a function on the message object itself.
19
+ // In other words we can say that the NATS streaming library is no longer going to automatically acknowledge (or) tell the
20
+ // NATS streaming library that we have received the event but instead it will be upto you and I to run some processing on
21
+ // this event, possibly save some information to the database and then after that entire process is complete then only we can
22
+ // acknowledge the message and say okay everything has been processed successfully. If we donot acknowledge the incoming event,
23
+ // then the NATS streaming server is going to wait some amount of time which is 30 seconds by default and then after 30 seconds
24
+ // of not getting the acknowledgement then it is automatically decide to take this event and send it to some other member of
25
+ // the same queue group. This is really useful because if our service crashes (or) goes down right after receiving an event
26
+ // but before processing it and acknowledging it, then the NATS streaming server is going to automatically resend this event
27
+ // to some other instance of our service which is listening on the same queue group. This way we can be sure that no events
28
+ // are lost even if our service goes down at any point of time.
29
+ listen() {
30
+ const subscription = this.client.subscribe(this.subject, this.queueGroupName, this.subscriptionOptions());
31
+ subscription.on("message", (msg) => {
32
+ console.log(`Message received: ${this.subject} / ${this.queueGroupName}`);
33
+ const parsedData = this.parseMessage(msg);
34
+ this.onMessage(parsedData, msg);
35
+ });
36
+ }
37
+ parseMessage(msg) {
38
+ const data = msg.getData();
39
+ return typeof data === "string"
40
+ ? JSON.parse(data)
41
+ : JSON.parse(data.toString("utf8")); // If it is a buffer then will convert it to utf8 string and then parse it to json
42
+ }
43
+ }
44
+ exports.Listener = Listener;
@@ -0,0 +1,13 @@
1
+ import { Stan } from "node-nats-streaming";
2
+ import { Subjects } from "./subjects";
3
+ interface Event {
4
+ subject: Subjects;
5
+ data: any;
6
+ }
7
+ export declare abstract class Publisher<T extends Event> {
8
+ abstract subject: T["subject"];
9
+ private client;
10
+ constructor(client: Stan);
11
+ publish(data: T["data"]): Promise<void>;
12
+ }
13
+ export {};
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Publisher = void 0;
4
+ class Publisher {
5
+ constructor(client) {
6
+ this.client = client;
7
+ }
8
+ publish(data) {
9
+ return new Promise((resolve, reject) => {
10
+ this.client.publish(this.subject, JSON.stringify(data), (err) => {
11
+ if (err) {
12
+ return reject(err); // reject the promise if there is an error
13
+ }
14
+ console.log("Event published to subject", this.subject);
15
+ resolve(); // resolve the promise if the publish is successful
16
+ });
17
+ });
18
+ }
19
+ }
20
+ exports.Publisher = Publisher;
@@ -0,0 +1,4 @@
1
+ export declare enum Subjects {
2
+ TicketCreated = "ticket:created",
3
+ TicketUpdated = "ticket:updated"
4
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ // Subject in the world of NATS-streaming server is nothing but a channel name
3
+ // where messages are published to and subscribers listen to.
4
+ // Here we define all the subjects that our application will use.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Subjects = void 0;
7
+ var Subjects;
8
+ (function (Subjects) {
9
+ Subjects["TicketCreated"] = "ticket:created";
10
+ Subjects["TicketUpdated"] = "ticket:updated";
11
+ })(Subjects || (exports.Subjects = Subjects = {}));
12
+ const printSubject = (subject) => { };
@@ -0,0 +1,10 @@
1
+ import { Subjects } from "./subjects";
2
+ export interface TicketCreatedEvent {
3
+ subject: Subjects.TicketCreated;
4
+ data: {
5
+ id: string;
6
+ title: string;
7
+ price: number;
8
+ userId: string;
9
+ };
10
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { Subjects } from "./subjects";
2
+ export interface TicketUpdatedEvent {
3
+ subject: Subjects.TicketUpdated;
4
+ data: {
5
+ id: string;
6
+ title: string;
7
+ price: number;
8
+ userId: string;
9
+ };
10
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/build/index.d.ts CHANGED
@@ -8,3 +8,8 @@ export * from "./middlewares/error-handler";
8
8
  export * from "./middlewares/current-user";
9
9
  export * from "./middlewares/require-auth";
10
10
  export * from "./middlewares/validate-request";
11
+ export * from "./events/base-listener";
12
+ export * from "./events/base-publisher";
13
+ export * from "./events/subjects";
14
+ export * from "./events/ticket-created-event";
15
+ export * from "./events/ticket-updated-event";
package/build/index.js CHANGED
@@ -24,3 +24,8 @@ __exportStar(require("./middlewares/error-handler"), exports);
24
24
  __exportStar(require("./middlewares/current-user"), exports);
25
25
  __exportStar(require("./middlewares/require-auth"), exports);
26
26
  __exportStar(require("./middlewares/validate-request"), exports);
27
+ __exportStar(require("./events/base-listener"), exports);
28
+ __exportStar(require("./events/base-publisher"), exports);
29
+ __exportStar(require("./events/subjects"), exports);
30
+ __exportStar(require("./events/ticket-created-event"), exports);
31
+ __exportStar(require("./events/ticket-updated-event"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mjtickets981/common",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -26,6 +26,7 @@
26
26
  "cookie-session": "^2.1.1",
27
27
  "express": "^5.1.0",
28
28
  "express-validator": "^7.2.1",
29
- "jsonwebtoken": "^9.0.2"
29
+ "jsonwebtoken": "^9.0.2",
30
+ "node-nats-streaming": "^0.3.2"
30
31
  }
31
32
  }