@ecom-micro/common 2.0.7 → 2.0.9
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/baseListener.d.ts +8 -5
- package/build/events/baseListener.js +16 -54
- package/build/events/basePublisher.d.ts +3 -3
- package/build/events/basePublisher.js +6 -11
- package/build/index.d.ts +0 -2
- package/build/index.js +0 -2
- package/package.json +2 -2
- package/build/events/productCreatedListener.d.ts +0 -8
- package/build/events/productCreatedListener.js +0 -33
- package/build/events/productCreatedPublisher.d.ts +0 -6
- package/build/events/productCreatedPublisher.js +0 -30
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Message,
|
|
1
|
+
import { Message, Stan } from 'node-nats-streaming';
|
|
2
2
|
import { Subjects } from '../types/subjects';
|
|
3
3
|
interface Event {
|
|
4
4
|
subject: Subjects;
|
|
@@ -6,10 +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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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;
|
|
13
16
|
parseMessage(msg: Message): any;
|
|
14
17
|
}
|
|
15
18
|
export {};
|
|
@@ -1,68 +1,30 @@
|
|
|
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
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.Listener = void 0;
|
|
40
4
|
var Listener = /** @class */ (function () {
|
|
41
5
|
function Listener(client) {
|
|
6
|
+
this.ackWait = 5 * 1000;
|
|
42
7
|
this.client = client;
|
|
43
8
|
}
|
|
9
|
+
Listener.prototype.subscriptionOptions = function () {
|
|
10
|
+
return this.client
|
|
11
|
+
.subscriptionOptions()
|
|
12
|
+
.setDeliverAllAvailable()
|
|
13
|
+
.setManualAckMode(true)
|
|
14
|
+
.setAckWait(this.ackWait)
|
|
15
|
+
.setDurableName(this.queueGroupName);
|
|
16
|
+
};
|
|
44
17
|
Listener.prototype.listen = function () {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (message !== null) {
|
|
52
|
-
console.log("Message Received to: ".concat(_this.subject));
|
|
53
|
-
var parseMsg = _this.parseMessage(message);
|
|
54
|
-
_this.onMessage(parseMsg, message, channel);
|
|
55
|
-
}
|
|
56
|
-
}, {
|
|
57
|
-
noAck: true,
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
return [2 /*return*/];
|
|
61
|
-
});
|
|
18
|
+
var _this = this;
|
|
19
|
+
var subscription = this.client.subscribe(this.subject, this.queueGroupName, this.subscriptionOptions());
|
|
20
|
+
subscription.on('message', function (msg) {
|
|
21
|
+
console.log("Message received: ".concat(_this.subject, " / ").concat(_this.queueGroupName));
|
|
22
|
+
var parsedData = _this.parseMessage(msg);
|
|
23
|
+
_this.onMessage(parsedData, msg);
|
|
62
24
|
});
|
|
63
25
|
};
|
|
64
26
|
Listener.prototype.parseMessage = function (msg) {
|
|
65
|
-
var data = msg.
|
|
27
|
+
var data = msg.getData();
|
|
66
28
|
return typeof data === 'string' ? JSON.parse(data) : JSON.parse(data.toString('utf8'));
|
|
67
29
|
};
|
|
68
30
|
return Listener;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Stan } from 'node-nats-streaming';
|
|
2
2
|
import { Subjects } from '../types/subjects';
|
|
3
3
|
interface Event {
|
|
4
4
|
subject: Subjects;
|
|
@@ -6,8 +6,8 @@ interface Event {
|
|
|
6
6
|
}
|
|
7
7
|
export declare abstract class Publisher<T extends Event> {
|
|
8
8
|
abstract subject: T['subject'];
|
|
9
|
-
protected client:
|
|
10
|
-
constructor(client:
|
|
9
|
+
protected client: Stan;
|
|
10
|
+
constructor(client: Stan);
|
|
11
11
|
publish(data: T['data']): Promise<void>;
|
|
12
12
|
}
|
|
13
13
|
export {};
|
|
@@ -8,18 +8,13 @@ var Publisher = /** @class */ (function () {
|
|
|
8
8
|
Publisher.prototype.publish = function (data) {
|
|
9
9
|
var _this = this;
|
|
10
10
|
return new Promise(function (resolve, reject) {
|
|
11
|
-
_this.client
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
exclusive: true,
|
|
17
|
-
});
|
|
18
|
-
channel.sendToQueue(_this.subject, Buffer.from(JSON.stringify(data)));
|
|
19
|
-
console.log("Event published to subject: ".concat(_this.subject));
|
|
11
|
+
_this.client.publish(_this.subject, JSON.stringify(data), function (err) {
|
|
12
|
+
if (err) {
|
|
13
|
+
return reject(err);
|
|
14
|
+
}
|
|
15
|
+
console.log('Event published to subject', _this.subject);
|
|
20
16
|
resolve();
|
|
21
|
-
})
|
|
22
|
-
.catch(function (error) { return reject(error); });
|
|
17
|
+
});
|
|
23
18
|
});
|
|
24
19
|
};
|
|
25
20
|
return Publisher;
|
package/build/index.d.ts
CHANGED
|
@@ -9,6 +9,4 @@ export * from './middleware/requestValidation';
|
|
|
9
9
|
export * from './events/baseListener';
|
|
10
10
|
export * from './events/basePublisher';
|
|
11
11
|
export * from './events/productCreatedEvent';
|
|
12
|
-
export * from './events/productCreatedListener';
|
|
13
|
-
export * from './events/productCreatedPublisher';
|
|
14
12
|
export * from './types/subjects';
|
package/build/index.js
CHANGED
|
@@ -25,6 +25,4 @@ __exportStar(require("./middleware/requestValidation"), exports);
|
|
|
25
25
|
__exportStar(require("./events/baseListener"), exports);
|
|
26
26
|
__exportStar(require("./events/basePublisher"), exports);
|
|
27
27
|
__exportStar(require("./events/productCreatedEvent"), exports);
|
|
28
|
-
__exportStar(require("./events/productCreatedListener"), exports);
|
|
29
|
-
__exportStar(require("./events/productCreatedPublisher"), exports);
|
|
30
28
|
__exportStar(require("./types/subjects"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecom-micro/common",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.9",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
"@types/express": "^4.17.14",
|
|
22
22
|
"@types/express-validator": "^3.0.0",
|
|
23
23
|
"@types/jsonwebtoken": "^8.5.9",
|
|
24
|
-
"amqplib": "^0.10.3",
|
|
25
24
|
"cookie-session": "^2.0.0",
|
|
26
25
|
"express": "^4.18.2",
|
|
27
26
|
"express-validator": "^6.14.2",
|
|
28
27
|
"jsonwebtoken": "^8.5.1",
|
|
28
|
+
"node-nats-streaming": "^0.2.6",
|
|
29
29
|
"typescript": "^4.8.4"
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Message, Channel } from 'amqplib';
|
|
2
|
-
import { Subjects } from '../types/subjects';
|
|
3
|
-
import { Listener } from './baseListener';
|
|
4
|
-
import { ProductCreatedEvent } from './productCreatedEvent';
|
|
5
|
-
export declare class ProductCreatedListener extends Listener<ProductCreatedEvent> {
|
|
6
|
-
subject: Subjects.ProductCreated;
|
|
7
|
-
onMessage(data: ProductCreatedEvent['data'], message: Message, channel: Channel): void;
|
|
8
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.ProductCreatedListener = void 0;
|
|
19
|
-
var subjects_1 = require("../types/subjects");
|
|
20
|
-
var baseListener_1 = require("./baseListener");
|
|
21
|
-
var ProductCreatedListener = /** @class */ (function (_super) {
|
|
22
|
-
__extends(ProductCreatedListener, _super);
|
|
23
|
-
function ProductCreatedListener() {
|
|
24
|
-
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
25
|
-
_this.subject = subjects_1.Subjects.ProductCreated;
|
|
26
|
-
return _this;
|
|
27
|
-
}
|
|
28
|
-
ProductCreatedListener.prototype.onMessage = function (data, message, channel) {
|
|
29
|
-
channel.ack(message);
|
|
30
|
-
};
|
|
31
|
-
return ProductCreatedListener;
|
|
32
|
-
}(baseListener_1.Listener));
|
|
33
|
-
exports.ProductCreatedListener = ProductCreatedListener;
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Subjects } from '../types/subjects';
|
|
2
|
-
import { Publisher } from './basePublisher';
|
|
3
|
-
import { ProductCreatedEvent } from './productCreatedEvent';
|
|
4
|
-
export declare class ProductCreatedPublisher extends Publisher<ProductCreatedEvent> {
|
|
5
|
-
subject: Subjects.ProductCreated;
|
|
6
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.ProductCreatedPublisher = void 0;
|
|
19
|
-
var subjects_1 = require("../types/subjects");
|
|
20
|
-
var basePublisher_1 = require("./basePublisher");
|
|
21
|
-
var ProductCreatedPublisher = /** @class */ (function (_super) {
|
|
22
|
-
__extends(ProductCreatedPublisher, _super);
|
|
23
|
-
function ProductCreatedPublisher() {
|
|
24
|
-
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
25
|
-
_this.subject = subjects_1.Subjects.ProductCreated;
|
|
26
|
-
return _this;
|
|
27
|
-
}
|
|
28
|
-
return ProductCreatedPublisher;
|
|
29
|
-
}(basePublisher_1.Publisher));
|
|
30
|
-
exports.ProductCreatedPublisher = ProductCreatedPublisher;
|