@haku-sci/utils 1.1.0 → 1.2.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/dist/health/health.controller.d.ts +6 -0
- package/dist/health/health.controller.js +31 -0
- package/dist/health/health.controller.js.map +1 -0
- package/dist/health/health.module.d.ts +4 -0
- package/dist/health/health.module.js +25 -0
- package/dist/health/health.module.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/microservice/microservice.libs.d.ts +6 -0
- package/dist/microservice/microservice.libs.js +43 -0
- package/dist/microservice/microservice.libs.js.map +1 -0
- package/dist/microservice/microservice.module.d.ts +7 -0
- package/dist/microservice/microservice.module.js +61 -0
- package/dist/microservice/microservice.module.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils.module.js +1 -5
- package/dist/utils.module.js.map +1 -1
- package/dist/utils.service.d.ts +5 -6
- package/dist/utils.service.js +33 -28
- package/dist/utils.service.js.map +1 -1
- package/dist/utils.service.spec.js +0 -14
- package/dist/utils.service.spec.js.map +1 -1
- package/package.json +30 -29
- package/utils/src/index.ts +1 -1
- package/utils/src/microservice/microservice.libs.ts +52 -0
- package/utils/src/utils.module.ts +0 -4
- package/utils/src/utils.service.spec.ts +2 -1
- package/utils/src/utils.service.ts +0 -54
package/dist/utils.module.js
CHANGED
|
@@ -8,14 +8,10 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.UtilsModule = void 0;
|
|
10
10
|
const common_1 = require("@nestjs/common");
|
|
11
|
-
const utils_service_1 = require("./utils.service");
|
|
12
11
|
let UtilsModule = class UtilsModule {
|
|
13
12
|
};
|
|
14
13
|
exports.UtilsModule = UtilsModule;
|
|
15
14
|
exports.UtilsModule = UtilsModule = __decorate([
|
|
16
|
-
(0, common_1.Module)({
|
|
17
|
-
providers: [utils_service_1.UtilsService],
|
|
18
|
-
exports: [utils_service_1.UtilsService],
|
|
19
|
-
})
|
|
15
|
+
(0, common_1.Module)({})
|
|
20
16
|
], UtilsModule);
|
|
21
17
|
//# sourceMappingURL=utils.module.js.map
|
package/dist/utils.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.module.js","sourceRoot":"","sources":["../utils/src/utils.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAwC;
|
|
1
|
+
{"version":3,"file":"utils.module.js","sourceRoot":"","sources":["../utils/src/utils.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAwC;AAGjC,IAAM,WAAW,GAAjB,MAAM,WAAW;CAAI,CAAA;AAAf,kCAAW;sBAAX,WAAW;IAFvB,IAAA,eAAM,EAAC,EACP,CAAC;GACW,WAAW,CAAI"}
|
package/dist/utils.service.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { INestMicroservice } from '@nestjs/common';
|
|
2
|
+
export declare class MicroserviceLibModule {
|
|
3
3
|
static projectName(): Promise<string>;
|
|
4
|
-
static
|
|
5
|
-
static
|
|
6
|
-
|
|
7
|
-
static bootstrap(appModule: any): Promise<void>;
|
|
4
|
+
private static registerService;
|
|
5
|
+
private static register;
|
|
6
|
+
static bootstrapMicroservice(appModule: any, customControllers?: any): Promise<INestMicroservice>;
|
|
8
7
|
}
|
package/dist/utils.service.js
CHANGED
|
@@ -5,52 +5,57 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
5
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
|
-
var
|
|
8
|
+
var MicroserviceLibModule_1;
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.
|
|
10
|
+
exports.MicroserviceLibModule = void 0;
|
|
11
11
|
const common_1 = require("@nestjs/common");
|
|
12
12
|
const promises_1 = require("fs/promises");
|
|
13
13
|
const path = require("path");
|
|
14
14
|
const core_1 = require("@nestjs/core");
|
|
15
15
|
const microservices_1 = require("@nestjs/microservices");
|
|
16
|
-
|
|
16
|
+
const axios_1 = require("axios");
|
|
17
|
+
const health_module_1 = require("./health/health.module");
|
|
18
|
+
let MicroserviceLibModule = MicroserviceLibModule_1 = class MicroserviceLibModule {
|
|
17
19
|
static async projectName() {
|
|
18
20
|
var data = await (0, promises_1.readFile)(path.join(process.cwd(), 'package.json'));
|
|
19
21
|
const { name } = JSON.parse(data.toString());
|
|
20
22
|
return name;
|
|
21
23
|
}
|
|
22
|
-
static async
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
static async registerService(address) {
|
|
25
|
+
const projectName = await this.projectName();
|
|
26
|
+
const serviceData = {
|
|
27
|
+
ID: projectName,
|
|
28
|
+
Name: projectName,
|
|
29
|
+
Address: address.address,
|
|
30
|
+
Port: address.port,
|
|
31
|
+
Check: {
|
|
32
|
+
TCP: `${address.address}:${address.port}/is-alive`,
|
|
33
|
+
Interval: "10s",
|
|
34
|
+
Timeout: "5s",
|
|
35
|
+
DeregisterCriticalServiceAfter: "1m"
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
await axios_1.default.put(process.env["CONSUL_URL"], serviceData);
|
|
39
|
+
console.log(`Service registered with Consul on ${address.address}:${address.port}`);
|
|
27
40
|
}
|
|
28
|
-
static
|
|
41
|
+
static register(appModule, additionalModules = []) {
|
|
29
42
|
return {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
options: {
|
|
33
|
-
host: `${process.env["CURRENT_BRANCH_SUBENDPOINT"]}.${service.toLowerCase()}.${process.env["DOMAIN_NAME"]}`,
|
|
34
|
-
port: port
|
|
35
|
-
}
|
|
43
|
+
module: MicroserviceLibModule_1,
|
|
44
|
+
imports: [appModule, health_module_1.HealthModule, ...additionalModules],
|
|
36
45
|
};
|
|
37
46
|
}
|
|
38
|
-
static async
|
|
39
|
-
const app = await core_1.NestFactory.createMicroservice(appModule, {
|
|
47
|
+
static async bootstrapMicroservice(appModule, customControllers) {
|
|
48
|
+
const app = await core_1.NestFactory.createMicroservice(MicroserviceLibModule_1.register(appModule, customControllers), {
|
|
40
49
|
transport: microservices_1.Transport.TCP,
|
|
41
|
-
options: {
|
|
42
|
-
host: process.env["DOMAIN_NAME"]
|
|
43
|
-
&& `${process.env["CURRENT_BRANCH_SUBENDPOINT"]}.${await UtilsService_1.projectName()}.${process.env["DOMAIN_NAME"]}`
|
|
44
|
-
|| "localhost",
|
|
45
|
-
port: await UtilsService_1.port()
|
|
46
|
-
}
|
|
47
50
|
});
|
|
48
51
|
await app.listen();
|
|
52
|
+
const tcpServer = app.server.server;
|
|
53
|
+
this.registerService(tcpServer.address());
|
|
54
|
+
return app;
|
|
49
55
|
}
|
|
50
56
|
};
|
|
51
|
-
exports.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
], UtilsService);
|
|
57
|
+
exports.MicroserviceLibModule = MicroserviceLibModule;
|
|
58
|
+
exports.MicroserviceLibModule = MicroserviceLibModule = MicroserviceLibModule_1 = __decorate([
|
|
59
|
+
(0, common_1.Module)({})
|
|
60
|
+
], MicroserviceLibModule);
|
|
56
61
|
//# sourceMappingURL=utils.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.service.js","sourceRoot":"","sources":["../utils/src/utils.service.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"utils.service.js","sourceRoot":"","sources":["../utils/src/utils.service.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAA0E;AAC1E,0CAAuC;AACvC,6BAA6B;AAC7B,uCAA2C;AAC3C,yDAAuE;AAEvE,iCAA0B;AAC1B,0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EAAE,iBAAkB;QAC9D,MAAM,GAAG,GAAG,MAAM,kBAAW,CAAC,kBAAkB,CAC9C,uBAAqB,CAAC,QAAQ,CAC5B,SAAS,EACT,iBAAiB,CAClB,EACD;YACE,SAAS,EAAE,yBAAS,CAAC,GAAG;SACzB,CACF,CAAC;QACF,MAAM,GAAG,CAAC,MAAM,EAAE,CAAC;QACnB,MAAM,SAAS,GAAgB,GAAW,CAAC,MAAM,CAAC,MAAM,CAAC;QACzD,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC,OAAO,EAAqB,CAAC,CAAA;QAC5D,OAAO,GAAG,CAAA;IACZ,CAAC;CACF,CAAA;AArDY,sDAAqB;gCAArB,qBAAqB;IADjC,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,qBAAqB,CAqDjC"}
|
|
@@ -1,17 +1,3 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const testing_1 = require("@nestjs/testing");
|
|
4
|
-
const utils_service_1 = require("./utils.service");
|
|
5
|
-
describe('UtilsService', () => {
|
|
6
|
-
let service;
|
|
7
|
-
beforeEach(async () => {
|
|
8
|
-
const module = await testing_1.Test.createTestingModule({
|
|
9
|
-
providers: [utils_service_1.UtilsService],
|
|
10
|
-
}).compile();
|
|
11
|
-
service = module.get(utils_service_1.UtilsService);
|
|
12
|
-
});
|
|
13
|
-
it('should be defined', () => {
|
|
14
|
-
expect(service).toBeDefined();
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
3
|
//# sourceMappingURL=utils.service.spec.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.service.spec.js","sourceRoot":"","sources":["../utils/src/utils.service.spec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.service.spec.js","sourceRoot":"","sources":["../utils/src/utils.service.spec.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@haku-sci/utils",
|
|
3
|
-
"dependencies": {
|
|
4
|
-
"@nestjs/common": "^10.4.1",
|
|
5
|
-
"@nestjs/microservices": "^10.4.1",
|
|
6
|
-
"utils": "^0.3.1"
|
|
7
|
-
},
|
|
8
|
-
"devDependencies": {
|
|
9
|
-
"@nestjs/testing": "^10.4.1",
|
|
10
|
-
"@types/jest": "^29.5.12"
|
|
11
|
-
},
|
|
12
|
-
"version": "1.
|
|
13
|
-
"description": "utils from haku-sci. Library only",
|
|
14
|
-
"main": "./dist/index.js",
|
|
15
|
-
"scripts": {
|
|
16
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@haku-sci/utils",
|
|
3
|
+
"dependencies": {
|
|
4
|
+
"@nestjs/common": "^10.4.1",
|
|
5
|
+
"@nestjs/microservices": "^10.4.1",
|
|
6
|
+
"utils": "^0.3.1"
|
|
7
|
+
},
|
|
8
|
+
"devDependencies": {
|
|
9
|
+
"@nestjs/testing": "^10.4.1",
|
|
10
|
+
"@types/jest": "^29.5.12"
|
|
11
|
+
},
|
|
12
|
+
"version": "1.2.0",
|
|
13
|
+
"description": "utils from haku-sci. Library only",
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
17
|
+
"build":"tsc"
|
|
18
|
+
},
|
|
19
|
+
"author": "Nicolas Blexmann",
|
|
20
|
+
"license": "ISC",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "Haku-Nicolas-Blexmann"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"microservices",
|
|
27
|
+
"ports",
|
|
28
|
+
"hosts"
|
|
29
|
+
]
|
|
30
|
+
}
|
package/utils/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export * from './utils.module';
|
|
2
|
-
export * from './
|
|
2
|
+
export * from './microservice/microservice.libs';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { DynamicModule, INestMicroservice, Module } from '@nestjs/common';
|
|
2
|
+
import { readFile } from 'fs/promises';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import { NestFactory } from '@nestjs/core';
|
|
5
|
+
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
|
|
6
|
+
import * as net from 'net';
|
|
7
|
+
import axios from 'axios';
|
|
8
|
+
|
|
9
|
+
export class MicroserviceLib {
|
|
10
|
+
static async projectName(): Promise<string> {
|
|
11
|
+
var data = await readFile(
|
|
12
|
+
path.join(
|
|
13
|
+
process.cwd(),
|
|
14
|
+
'package.json',
|
|
15
|
+
)
|
|
16
|
+
)
|
|
17
|
+
const { name } = JSON.parse(data.toString());
|
|
18
|
+
return name;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
private static async registerService(address: net.AddressInfo) {
|
|
22
|
+
const projectName=await this.projectName();
|
|
23
|
+
const serviceData = {
|
|
24
|
+
ID: projectName,
|
|
25
|
+
Name: projectName,
|
|
26
|
+
Address: address.address,
|
|
27
|
+
Port: address.port,
|
|
28
|
+
Check: {
|
|
29
|
+
TCP: `${address.address}:${address.port}`,
|
|
30
|
+
Interval: "10s",
|
|
31
|
+
Timeout: "5s",
|
|
32
|
+
DeregisterCriticalServiceAfter: "1m"
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
await axios.put(process.env["CONSUL_URL"], serviceData);
|
|
37
|
+
console.log(`Service registered with Consul on ${address.address}:${address.port}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static async bootstrapMicroservice(appModule):Promise<INestMicroservice> {
|
|
41
|
+
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
|
|
42
|
+
appModule,
|
|
43
|
+
{
|
|
44
|
+
transport: Transport.TCP,
|
|
45
|
+
},
|
|
46
|
+
);
|
|
47
|
+
await app.listen();
|
|
48
|
+
const tcpServer: net.Server = (app as any).server.server;
|
|
49
|
+
this.registerService(tcpServer.address() as net.AddressInfo)
|
|
50
|
+
return app
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
2
|
-
import { UtilsService } from './utils.service';
|
|
3
2
|
|
|
3
|
+
/*
|
|
4
4
|
describe('UtilsService', () => {
|
|
5
5
|
let service: UtilsService;
|
|
6
6
|
|
|
@@ -16,3 +16,4 @@ describe('UtilsService', () => {
|
|
|
16
16
|
expect(service).toBeDefined();
|
|
17
17
|
});
|
|
18
18
|
});
|
|
19
|
+
*/
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
|
2
|
-
import { readFile } from 'fs/promises';
|
|
3
|
-
import * as path from 'path';
|
|
4
|
-
import { NestFactory } from '@nestjs/core';
|
|
5
|
-
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
|
|
6
|
-
@Injectable()
|
|
7
|
-
export class UtilsService {
|
|
8
|
-
|
|
9
|
-
private static listServices: Record<string, number> = JSON.parse(process.env["SERVICES"].replaceAll("'", '"'))
|
|
10
|
-
static async projectName(): Promise<string> {
|
|
11
|
-
var data = await readFile(
|
|
12
|
-
path.join(
|
|
13
|
-
process.cwd(),
|
|
14
|
-
'package.json',
|
|
15
|
-
)
|
|
16
|
-
)
|
|
17
|
-
const { name } = JSON.parse(data.toString());
|
|
18
|
-
return name;
|
|
19
|
-
}
|
|
20
|
-
static async port(): Promise<number> {
|
|
21
|
-
return this.listServices[await this.projectName()]
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
static microservicesClients(): any[] {
|
|
25
|
-
return Object.keys(this.listServices).map(service => this.getServiceClient(service,this.listServices[service]));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
private static getServiceClient(service: string, port?: number): { name, transport, options } {
|
|
29
|
-
return {
|
|
30
|
-
name: service.toUpperCase(),
|
|
31
|
-
transport: Transport.TCP,
|
|
32
|
-
options: {
|
|
33
|
-
host: `${process.env["CURRENT_BRANCH_SUBENDPOINT"]}.${service.toLowerCase()}.${process.env["DOMAIN_NAME"]}`,
|
|
34
|
-
port: port
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
static async bootstrap(appModule) {
|
|
40
|
-
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
|
|
41
|
-
appModule,
|
|
42
|
-
{
|
|
43
|
-
transport: Transport.TCP,
|
|
44
|
-
options:{
|
|
45
|
-
host: process.env["DOMAIN_NAME"]
|
|
46
|
-
&& `${process.env["CURRENT_BRANCH_SUBENDPOINT"]}.${await UtilsService.projectName()}.${process.env["DOMAIN_NAME"]}`
|
|
47
|
-
||"localhost",
|
|
48
|
-
port:await UtilsService.port()
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
);
|
|
52
|
-
await app.listen();
|
|
53
|
-
}
|
|
54
|
-
}
|