@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.
@@ -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
@@ -1 +1 @@
1
- {"version":3,"file":"utils.module.js","sourceRoot":"","sources":["../utils/src/utils.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAwC;AACxC,mDAA+C;AAMxC,IAAM,WAAW,GAAjB,MAAM,WAAW;CAAI,CAAA;AAAf,kCAAW;sBAAX,WAAW;IAJvB,IAAA,eAAM,EAAC;QACN,SAAS,EAAE,CAAC,4BAAY,CAAC;QACzB,OAAO,EAAE,CAAC,4BAAY,CAAC;KACxB,CAAC;GACW,WAAW,CAAI"}
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"}
@@ -1,8 +1,7 @@
1
- export declare class UtilsService {
2
- private static listServices;
1
+ import { INestMicroservice } from '@nestjs/common';
2
+ export declare class MicroserviceLibModule {
3
3
  static projectName(): Promise<string>;
4
- static port(): Promise<number>;
5
- static microservicesClients(): any[];
6
- private static getServiceClient;
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
  }
@@ -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 UtilsService_1;
8
+ var MicroserviceLibModule_1;
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.UtilsService = void 0;
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
- let UtilsService = UtilsService_1 = class UtilsService {
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 port() {
23
- return this.listServices[await this.projectName()];
24
- }
25
- static microservicesClients() {
26
- return Object.keys(this.listServices).map(service => this.getServiceClient(service, this.listServices[service]));
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 getServiceClient(service, port) {
41
+ static register(appModule, additionalModules = []) {
29
42
  return {
30
- name: service.toUpperCase(),
31
- transport: microservices_1.Transport.TCP,
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 bootstrap(appModule) {
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.UtilsService = UtilsService;
52
- UtilsService.listServices = JSON.parse(process.env["SERVICES"].replaceAll("'", '"'));
53
- exports.UtilsService = UtilsService = UtilsService_1 = __decorate([
54
- (0, common_1.Injectable)()
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,2CAA4C;AAC5C,0CAAuC;AACvC,6BAA6B;AAC7B,uCAA2C;AAC3C,yDAAuE;AAEhE,I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  "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":";;AAAA,6CAAsD;AACtD,mDAA+C;AAE/C,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,IAAI,OAAqB,CAAC;IAE1B,UAAU,CAAC,KAAK,IAAI,EAAE;QACpB,MAAM,MAAM,GAAkB,MAAM,cAAI,CAAC,mBAAmB,CAAC;YAC3D,SAAS,EAAE,CAAC,4BAAY,CAAC;SAC1B,CAAC,CAAC,OAAO,EAAE,CAAC;QAEb,OAAO,GAAG,MAAM,CAAC,GAAG,CAAe,4BAAY,CAAC,CAAC;IACnD,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mBAAmB,EAAE,GAAG,EAAE;QAC3B,MAAM,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,CAAC;IAChC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
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.1.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
- },
18
- "author": "Nicolas Blexmann",
19
- "license": "ISC",
20
- "repository": {
21
- "type": "git",
22
- "url": "Haku-Nicolas-Blexmann"
23
- },
24
- "keywords": [
25
- "microservices",
26
- "ports",
27
- "hosts"
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
+ }
@@ -1,2 +1,2 @@
1
1
  export * from './utils.module';
2
- export * from './utils.service';
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,8 +1,4 @@
1
1
  import { Module } from '@nestjs/common';
2
- import { UtilsService } from './utils.service';
3
-
4
2
  @Module({
5
- providers: [UtilsService],
6
- exports: [UtilsService],
7
3
  })
8
4
  export class UtilsModule { }
@@ -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
- }