@haku-sci/utils 1.2.9 → 1.3.0
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/README.md
CHANGED
|
@@ -1,9 +1,35 @@
|
|
|
1
1
|
# Description of the utils inside this package
|
|
2
2
|
|
|
3
|
-
This package is used by Haku-Sci, to handle
|
|
3
|
+
This package is used by Haku-Sci, to handle different contents.
|
|
4
|
+
|
|
5
|
+
## Microservices
|
|
6
|
+
|
|
4
7
|
Loading the class MicroserviceLib, it allows to fetch:
|
|
5
8
|
|
|
6
9
|
* The name of the current microservice
|
|
7
10
|
* default bootstrap for microservices
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
### Consul
|
|
13
|
+
|
|
14
|
+
if consul is used, and env variabe CONSUL_URI is defined, then the lib allows as well:
|
|
15
|
+
|
|
16
|
+
* to send messages to other services via TCP, and fetch the response if any
|
|
17
|
+
* to emit message for all services via RabbitMQ
|
|
18
|
+
|
|
19
|
+
## Docker
|
|
20
|
+
|
|
21
|
+
the start_docker_services.bat method allows to run all required services from docker:
|
|
22
|
+
|
|
23
|
+
* Consul
|
|
24
|
+
* Neo4j
|
|
25
|
+
* Postgresql
|
|
26
|
+
* RabbitMQ
|
|
27
|
+
|
|
28
|
+
## launch.json
|
|
29
|
+
|
|
30
|
+
The launch.json file allows to run all services (the project libs must be in the same directory as "services", which contains all services)
|
|
31
|
+
|
|
32
|
+
## other
|
|
33
|
+
|
|
34
|
+
Finally, the method executeFunction into the utils library lets call dynamically the function of an object, with parameters
|
|
35
|
+
(Format: object as class, function name as string, parameters as dictionary)
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { INestApplication } from '@nestjs/common';
|
|
2
2
|
export declare class MicroserviceService {
|
|
3
|
+
private static serviceAddress;
|
|
4
|
+
private static msName;
|
|
3
5
|
static projectName(): Promise<string>;
|
|
4
6
|
private static getServiceURI;
|
|
5
7
|
static sendTCPMessage(service: any, messagePattern: any, payload?: any): Promise<any>;
|
|
8
|
+
static emitTCPEvent(messagePattern: any, queue?: any, payload?: any): Promise<void>;
|
|
6
9
|
private static registerService;
|
|
7
|
-
static bootstrapMicroservice(appModule: any): Promise<
|
|
10
|
+
static bootstrapMicroservice(appModule: any): Promise<INestApplication>;
|
|
8
11
|
}
|
|
@@ -16,11 +16,15 @@ const microservices_1 = require("@nestjs/microservices");
|
|
|
16
16
|
const net = require("net");
|
|
17
17
|
const axios_1 = require("axios");
|
|
18
18
|
const rxjs_1 = require("rxjs");
|
|
19
|
+
const HAKU_SCI_EXCHANGE = 'Haku-SciExchange';
|
|
19
20
|
let MicroserviceService = MicroserviceService_1 = class MicroserviceService {
|
|
20
21
|
static async projectName() {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
if (!this.msName) {
|
|
23
|
+
var data = await (0, promises_1.readFile)(path.join(process.cwd(), 'package.json'));
|
|
24
|
+
const { name } = JSON.parse(data.toString());
|
|
25
|
+
this.msName = name;
|
|
26
|
+
}
|
|
27
|
+
return this.msName;
|
|
24
28
|
}
|
|
25
29
|
static async getServiceURI(serviceName) {
|
|
26
30
|
const response = await axios_1.default.get(`${process.env["CONSUL_URL"]}/v1/catalog/service/${serviceName}`);
|
|
@@ -47,8 +51,23 @@ let MicroserviceService = MicroserviceService_1 = class MicroserviceService {
|
|
|
47
51
|
const result = await (0, rxjs_1.lastValueFrom)(await client.send(messagePattern, payload));
|
|
48
52
|
return result;
|
|
49
53
|
}
|
|
50
|
-
|
|
51
|
-
|
|
54
|
+
finally {
|
|
55
|
+
client.close();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
static async emitTCPEvent(messagePattern, queue, payload) {
|
|
59
|
+
const client = microservices_1.ClientProxyFactory.create({
|
|
60
|
+
options: {
|
|
61
|
+
urls: [`amqp://${process.env["RABBITMQ_URL"]}`],
|
|
62
|
+
queue: queue,
|
|
63
|
+
queueOptions: {
|
|
64
|
+
durable: false,
|
|
65
|
+
},
|
|
66
|
+
exchange: HAKU_SCI_EXCHANGE,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
try {
|
|
70
|
+
client.emit(messagePattern, payload);
|
|
52
71
|
}
|
|
53
72
|
finally {
|
|
54
73
|
client.close();
|
|
@@ -75,14 +94,28 @@ let MicroserviceService = MicroserviceService_1 = class MicroserviceService {
|
|
|
75
94
|
const server = net.createServer();
|
|
76
95
|
server.listen(0);
|
|
77
96
|
await new Promise((resolve) => server.once('listening', resolve));
|
|
78
|
-
|
|
79
|
-
const app = await core_1.NestFactory.
|
|
97
|
+
this.serviceAddress = server.address();
|
|
98
|
+
const app = await core_1.NestFactory.create(appModule);
|
|
99
|
+
app.connectMicroservice({
|
|
80
100
|
transport: microservices_1.Transport.TCP,
|
|
81
101
|
options: {
|
|
82
|
-
port:
|
|
102
|
+
port: this.serviceAddress.port
|
|
83
103
|
}
|
|
84
104
|
});
|
|
85
|
-
|
|
105
|
+
if (process.env["RABBITMQ_URL"]) {
|
|
106
|
+
app.connectMicroservice({
|
|
107
|
+
transport: microservices_1.Transport.RMQ,
|
|
108
|
+
options: {
|
|
109
|
+
urls: [`amqp://${process.env["RABBITMQ_URL"]}`],
|
|
110
|
+
queue: await this.projectName(),
|
|
111
|
+
queueOptions: {
|
|
112
|
+
durable: false,
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
await app.startAllMicroservices();
|
|
118
|
+
await app.listen(this.serviceAddress.port);
|
|
86
119
|
if (process.env["CONSUL_URL"]) {
|
|
87
120
|
const tcpServer = app.server.server;
|
|
88
121
|
this.registerService(tcpServer.address());
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"microservice.service.js","sourceRoot":"","sources":["../../src/microservice/microservice.service.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"microservice.service.js","sourceRoot":"","sources":["../../src/microservice/microservice.service.ts"],"names":[],"m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AC,cAAc,CAAC,EAAE,CAAC;oBAC/C,KAAK,EAAE,MAAM,IAAI,CAAC,WAAW,EAAE;oBAC/B,YAAY,EAAE;wBACZ,OAAO,EAAE,KAAK;qBACf;iBACF;aACF,CAAC,CAAC;QACL,CAAC;QACD,MAAM,GAAG,CAAC,qBAAqB,EAAE,CAAA;QACjC,MAAM,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;QAC3C,IAAI,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC;YAC9B,MAAM,SAAS,GAAgB,GAAW,CAAC,MAAM,CAAC,MAAM,CAAC;YACzD,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC,OAAO,EAAqB,CAAC,CAAA;QAC9D,CAAC;QACD,OAAO,GAAG,CAAA;IACZ,CAAC;CACF,CAAA;AA7HY,kDAAmB;8BAAnB,mBAAmB;IAD/B,IAAA,mBAAU,GAAE;GACA,mBAAmB,CA6H/B"}
|
package/package.json
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@haku-sci/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "utils from haku-sci. Library only",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"peerdependencies": {
|
|
8
8
|
"@nestjs/common": "^10.4.1",
|
|
9
9
|
"@nestjs/microservices": "^10.4.1",
|
|
10
|
-
"
|
|
11
|
-
"utils": "^0.3.1"
|
|
12
|
-
},
|
|
13
|
-
"dependencies": {
|
|
10
|
+
"utils": "^0.3.1",
|
|
14
11
|
"@nestjs/typeorm": "^10.0.2",
|
|
15
12
|
"axios": "^1.7.9"
|
|
16
13
|
},
|
|
@@ -18,12 +15,12 @@
|
|
|
18
15
|
"@nestjs/testing": "^10.4.1",
|
|
19
16
|
"@types/jest": "^29.5.12",
|
|
20
17
|
"rimraf": "^6.0.1",
|
|
21
|
-
"typescript": "^5.7.
|
|
18
|
+
"typescript": "^5.7.3"
|
|
22
19
|
},
|
|
23
20
|
"scripts": {
|
|
24
21
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
25
|
-
"build": "tsc",
|
|
26
|
-
"clean": "
|
|
22
|
+
"build": "tsc --build",
|
|
23
|
+
"clean": "tsc --build --clean"
|
|
27
24
|
},
|
|
28
25
|
"author": "Nicolas Blexmann",
|
|
29
26
|
"license": "ISC",
|
|
@@ -38,5 +35,10 @@
|
|
|
38
35
|
"microservices",
|
|
39
36
|
"ports",
|
|
40
37
|
"hosts"
|
|
41
|
-
]
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@nestjs/microservices": "^10.4.15",
|
|
41
|
+
"@nestjs/platform-express": "^10.4.15",
|
|
42
|
+
"axios": "^1.7.9"
|
|
43
|
+
}
|
|
42
44
|
}
|