@mjtickets981/common 1.0.4 → 1.0.8
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 +18 -0
- package/build/events/base-listener.js +44 -0
- package/build/events/base-publisher.d.ts +13 -0
- package/build/events/base-publisher.js +20 -0
- package/build/events/subjects.d.ts +4 -0
- package/build/events/subjects.js +12 -0
- package/build/events/ticket-created-event.d.ts +10 -0
- package/build/events/ticket-created-event.js +2 -0
- package/build/events/ticket-updated-event.d.ts +10 -0
- package/build/events/ticket-updated-event.js +2 -0
- package/build/events/types/order-status.d.ts +6 -0
- package/build/events/types/order-status.js +10 -0
- package/build/index.d.ts +6 -0
- package/build/index.js +6 -0
- package/package.json +3 -2
|
@@ -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,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,6 @@
|
|
|
1
|
+
export declare enum OrderStatus {
|
|
2
|
+
Created = "created",// When an order is created but not yet reserved. The ticket is still available for purchase.
|
|
3
|
+
Cancelled = "cancelled",// When an order is cancelled. The ticket becomes available for purchase again.
|
|
4
|
+
AwaitingPayment = "awaiting:payment",// When an order has been reserved and is waiting for payment. The ticket is no longer available for purchase.
|
|
5
|
+
Complete = "complete"
|
|
6
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OrderStatus = void 0;
|
|
4
|
+
var OrderStatus;
|
|
5
|
+
(function (OrderStatus) {
|
|
6
|
+
OrderStatus["Created"] = "created";
|
|
7
|
+
OrderStatus["Cancelled"] = "cancelled";
|
|
8
|
+
OrderStatus["AwaitingPayment"] = "awaiting:payment";
|
|
9
|
+
OrderStatus["Complete"] = "complete";
|
|
10
|
+
})(OrderStatus || (exports.OrderStatus = OrderStatus = {}));
|
package/build/index.d.ts
CHANGED
|
@@ -8,3 +8,9 @@ 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";
|
|
16
|
+
export * from "./events/types/order-status";
|
package/build/index.js
CHANGED
|
@@ -24,3 +24,9 @@ __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);
|
|
32
|
+
__exportStar(require("./events/types/order-status"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mjtickets981/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
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
|
}
|