@konplit-services/common 1.0.320 → 1.0.321

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.
@@ -95,7 +95,6 @@ class Listener {
95
95
  const decodedRequest = this.jc.decode(msg.data);
96
96
  console.log("Received Request:", decodedRequest);
97
97
  const response = yield this.onMessageResponse(decodedRequest, msg);
98
- console.log("Respons comming here", response);
99
98
  // Send a reply back
100
99
  msg.respond(this.jc.encode({ data: response }));
101
100
  }
@@ -144,7 +143,7 @@ class Listener {
144
143
  retention: nats_1.RetentionPolicy.Limits,
145
144
  max_msgs: 10000,
146
145
  max_bytes: 1 * 1024 * 1024 * 1024,
147
- max_age: 14 * 24 * 3600 * 1000, // 2 weeks in milliseconds
146
+ max_age: (0, nats_1.nanos)(14 * 24 * 3600 * 1000), //2 weeks in milliseconds
148
147
  };
149
148
  yield jsm.streams.add(streamConfig);
150
149
  console.log(`Stream '${streamName}' created`);
@@ -188,7 +187,7 @@ class Listener {
188
187
  filter_subject: subject,
189
188
  deliver_policy: nats_1.DeliverPolicy.All,
190
189
  ack_wait: this.ackWait,
191
- max_deliver: 576, //5 minutes * 576 equals two days assuming the system is down
190
+ max_deliver: 576, //5 minutes * 576 equals two days assuming the systme
192
191
  };
193
192
  yield jsm.consumers.add(streamName, consumerConfig);
194
193
  console.log(`Consumer '${durableName}' created`);
@@ -121,7 +121,7 @@ class Publisher {
121
121
  max_consumers: -1,
122
122
  max_msgs: 10000,
123
123
  max_bytes: 5 * 1024 * 1024 * 1024,
124
- max_age: 14 * 24 * 3600 * 1000, // 2 weeks in milliseconds
124
+ max_age: (0, nats_1.nanos)(14 * 24 * 3600 * 1000), //2 weeks in milliseconds
125
125
  };
126
126
  yield jsm.streams.add(streamConfig);
127
127
  console.log(`Stream '${streamName}' created`);
@@ -0,0 +1,9 @@
1
+ import { StreamEvent, StreamName, Subjects } from "./";
2
+ export interface TestEvent {
3
+ subject: Subjects.AccountCreated;
4
+ streamName: StreamName.name;
5
+ streamEvents: StreamEvent.Event;
6
+ data: {
7
+ transactionId: string;
8
+ };
9
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
1
+ import { TestEvent } from "./interfact";
2
+ import { JsMsg, Msg } from "nats";
3
+ import { Listener, StreamEvent, StreamName, Subjects } from "./";
4
+ export declare class AccountCreatedListener extends Listener<TestEvent> {
5
+ streamEvents: StreamEvent.Event;
6
+ stream: StreamName.name;
7
+ readonly subject: Subjects.AccountCreated;
8
+ readonly durableName = "test_group";
9
+ onMessage(data: TestEvent["data"], oldMessageObject: JsMsg): Promise<void>;
10
+ onMessageResponse(data: TestEvent["data"], msg: Msg): void;
11
+ }
12
+ export declare const startup: () => Promise<void>;
@@ -0,0 +1,60 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.startup = exports.AccountCreatedListener = void 0;
16
+ const nats_cons_1 = __importDefault(require("./nats-cons"));
17
+ const _1 = require("./");
18
+ class AccountCreatedListener extends _1.Listener {
19
+ constructor() {
20
+ super(...arguments);
21
+ this.streamEvents = _1.StreamEvent.Event;
22
+ this.stream = _1.StreamName.name;
23
+ this.subject = _1.Subjects.AccountCreated;
24
+ this.durableName = "test_group";
25
+ }
26
+ onMessage(data, oldMessageObject) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ const accountData = data;
29
+ try {
30
+ //Resolve BankService from container
31
+ console.log("data", data);
32
+ oldMessageObject.ack();
33
+ }
34
+ catch (error) {
35
+ console.error("Failed to create an account", error);
36
+ }
37
+ });
38
+ }
39
+ onMessageResponse(data, msg) {
40
+ throw new Error("Method not implemented.");
41
+ }
42
+ }
43
+ exports.AccountCreatedListener = AccountCreatedListener;
44
+ const startup = () => __awaiter(void 0, void 0, void 0, function* () {
45
+ try {
46
+ yield nats_cons_1.default.getInstance().connect({
47
+ servers: "localhost:4222",
48
+ });
49
+ //assign the client to global object
50
+ const client = nats_cons_1.default.getInstance().getJetStream;
51
+ new AccountCreatedListener(client).listen();
52
+ yield new Promise(() => { }); // Infinite promise to keep running
53
+ }
54
+ catch (error) {
55
+ console.error("Unhandled error:", error);
56
+ process.exit(1);
57
+ }
58
+ });
59
+ exports.startup = startup;
60
+ (0, exports.startup)();
@@ -0,0 +1,12 @@
1
+ import { JetStreamClient, NatsConnection, ConnectionOptions } from "nats";
2
+ declare class NatsWrapper {
3
+ private static instance;
4
+ private nc;
5
+ private js;
6
+ private constructor();
7
+ static getInstance(): NatsWrapper;
8
+ connect(options: ConnectionOptions): Promise<JetStreamClient>;
9
+ get getJetStream(): JetStreamClient;
10
+ get getConnection(): NatsConnection;
11
+ }
12
+ export default NatsWrapper;
@@ -0,0 +1,54 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const nats_1 = require("nats");
13
+ class NatsWrapper {
14
+ constructor() {
15
+ this.nc = null;
16
+ this.js = null;
17
+ }
18
+ static getInstance() {
19
+ if (!NatsWrapper.instance) {
20
+ NatsWrapper.instance = new NatsWrapper();
21
+ }
22
+ return NatsWrapper.instance;
23
+ }
24
+ connect(options) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ if (this.nc && this.js) {
27
+ return this.js;
28
+ }
29
+ try {
30
+ this.nc = yield (0, nats_1.connect)(options);
31
+ console.log("Connected to NATS JetStream Server");
32
+ this.js = this.nc.jetstream();
33
+ return this.js;
34
+ }
35
+ catch (error) {
36
+ console.error("NATS JetStream connection error:", error);
37
+ throw error;
38
+ }
39
+ });
40
+ }
41
+ get getJetStream() {
42
+ if (!this.js) {
43
+ throw new Error("JetStream not connected. Call connect() first.");
44
+ }
45
+ return this.js;
46
+ }
47
+ get getConnection() {
48
+ if (!this.nc) {
49
+ throw new Error("NATS client not connected. Call connect() first.");
50
+ }
51
+ return this.nc;
52
+ }
53
+ }
54
+ exports.default = NatsWrapper;
@@ -0,0 +1,8 @@
1
+ import { TestEvent } from "./interfact";
2
+ import { Publisher, StreamEvent, StreamName, Subjects } from "./";
3
+ export declare class TestPublisher extends Publisher<TestEvent> {
4
+ streamEvents: StreamEvent.Event;
5
+ stream: StreamName.name;
6
+ readonly subject = Subjects.AccountCreated;
7
+ }
8
+ export declare const startup: () => Promise<void>;
@@ -0,0 +1,46 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.startup = exports.TestPublisher = void 0;
16
+ const nats_cons_1 = __importDefault(require("./nats-cons"));
17
+ const _1 = require("./");
18
+ class TestPublisher extends _1.Publisher {
19
+ constructor() {
20
+ super(...arguments);
21
+ this.streamEvents = _1.StreamEvent.Event;
22
+ this.stream = _1.StreamName.name;
23
+ this.subject = _1.Subjects.AccountCreated;
24
+ }
25
+ }
26
+ exports.TestPublisher = TestPublisher;
27
+ const startup = () => __awaiter(void 0, void 0, void 0, function* () {
28
+ try {
29
+ yield nats_cons_1.default.getInstance().connect({
30
+ servers: "localhost:4222",
31
+ });
32
+ //assign the client to global object
33
+ const client = nats_cons_1.default.getInstance().getJetStream;
34
+ [1, 2].forEach(() => {
35
+ new TestPublisher(client).publish({
36
+ transactionId: "transa tion",
37
+ });
38
+ });
39
+ }
40
+ catch (error) {
41
+ console.error("Unhandled error:", error);
42
+ process.exit(1);
43
+ }
44
+ });
45
+ exports.startup = startup;
46
+ (0, exports.startup)();
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@konplit-services/common",
3
- "version": "1.0.320",
3
+ "version": "1.0.321",
4
4
  "description": "",
5
- "main": "./build/index.js",
5
+ "main": "index.js",
6
6
  "types": "./build/index.d.ts",
7
7
  "private": false,
8
8
  "files": [
@@ -10,6 +10,8 @@
10
10
  ],
11
11
  "scripts": {
12
12
  "build": "tsc",
13
+ "publisher": "ts-node-dev --notify false src/publisher.ts",
14
+ "listener": "ts-node-dev --notify false src/listener.ts",
13
15
  "pub": "git add . && git commit -m \"updates\" && npm version patch && npm run build && npm publish --access public"
14
16
  },
15
17
  "keywords": [],