1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.NestMicroservice = void 0;
|
4 | const logger_service_1 = require("@nestjs/common/services/logger.service");
|
5 | const constants_1 = require("@nestjs/core/constants");
|
6 | const optional_require_1 = require("@nestjs/core/helpers/optional-require");
|
7 | const nest_application_context_1 = require("@nestjs/core/nest-application-context");
|
8 | const transport_enum_1 = require("./enums/transport.enum");
|
9 | const microservices_module_1 = require("./microservices-module");
|
10 | const server_factory_1 = require("./server/server-factory");
|
11 | const { SocketModule } = optional_require_1.optionalRequire('@nestjs/websockets/socket-module', () => require('@nestjs/websockets/socket-module'));
|
12 | class NestMicroservice extends nest_application_context_1.NestApplicationContext {
|
13 | constructor(container, config = {}, applicationConfig) {
|
14 | super(container);
|
15 | this.applicationConfig = applicationConfig;
|
16 | this.logger = new logger_service_1.Logger(NestMicroservice.name, {
|
17 | timestamp: true,
|
18 | });
|
19 | this.microservicesModule = new microservices_module_1.MicroservicesModule();
|
20 | this.socketModule = SocketModule ? new SocketModule() : null;
|
21 | this.isTerminated = false;
|
22 | this.isInitHookCalled = false;
|
23 | this.microservicesModule.register(container, this.applicationConfig);
|
24 | this.createServer(config);
|
25 | this.selectContextModule();
|
26 | }
|
27 | createServer(config) {
|
28 | try {
|
29 | this.microserviceConfig = Object.assign({ transport: transport_enum_1.Transport.TCP }, config);
|
30 | const { strategy } = config;
|
31 | this.server = strategy
|
32 | ? strategy
|
33 | : server_factory_1.ServerFactory.create(this.microserviceConfig);
|
34 | }
|
35 | catch (e) {
|
36 | this.logger.error(e);
|
37 | throw e;
|
38 | }
|
39 | }
|
40 | async registerModules() {
|
41 | this.socketModule &&
|
42 | this.socketModule.register(this.container, this.applicationConfig);
|
43 | this.microservicesModule.setupClients(this.container);
|
44 | this.registerListeners();
|
45 | this.setIsInitialized(true);
|
46 | if (!this.isInitHookCalled) {
|
47 | await this.callInitHook();
|
48 | await this.callBootstrapHook();
|
49 | }
|
50 | }
|
51 | registerListeners() {
|
52 | this.microservicesModule.setupListeners(this.container, this.server);
|
53 | }
|
54 | useWebSocketAdapter(adapter) {
|
55 | this.applicationConfig.setIoAdapter(adapter);
|
56 | return this;
|
57 | }
|
58 | useGlobalFilters(...filters) {
|
59 | this.applicationConfig.useGlobalFilters(...filters);
|
60 | return this;
|
61 | }
|
62 | useGlobalPipes(...pipes) {
|
63 | this.applicationConfig.useGlobalPipes(...pipes);
|
64 | return this;
|
65 | }
|
66 | useGlobalInterceptors(...interceptors) {
|
67 | this.applicationConfig.useGlobalInterceptors(...interceptors);
|
68 | return this;
|
69 | }
|
70 | useGlobalGuards(...guards) {
|
71 | this.applicationConfig.useGlobalGuards(...guards);
|
72 | return this;
|
73 | }
|
74 | async init() {
|
75 | if (this.isInitialized) {
|
76 | return this;
|
77 | }
|
78 | await super.init();
|
79 | await this.registerModules();
|
80 | return this;
|
81 | }
|
82 | async listen() {
|
83 | !this.isInitialized && (await this.registerModules());
|
84 | return new Promise((resolve, reject) => {
|
85 | this.server.listen((err, info) => {
|
86 | var _a, _b;
|
87 | if ((_b = (_a = this.microserviceConfig) === null || _a === void 0 ? void 0 : _a.autoFlushLogs) !== null && _b !== void 0 ? _b : true) {
|
88 | this.flushLogs();
|
89 | }
|
90 | if (err) {
|
91 | return reject(err);
|
92 | }
|
93 | this.logger.log(constants_1.MESSAGES.MICROSERVICE_READY);
|
94 | resolve(info);
|
95 | });
|
96 | });
|
97 | }
|
98 | async listenAsync() {
|
99 | this.logger.warn('DEPRECATED! "listenAsync" method is deprecated and will be removed in the next major release. Please, use "listen" instead.');
|
100 | return this.listen();
|
101 | }
|
102 | async close() {
|
103 | await this.server.close();
|
104 | if (this.isTerminated) {
|
105 | return;
|
106 | }
|
107 | this.setIsTerminated(true);
|
108 | await this.closeApplication();
|
109 | }
|
110 | setIsInitialized(isInitialized) {
|
111 | this.isInitialized = isInitialized;
|
112 | }
|
113 | setIsTerminated(isTerminated) {
|
114 | this.isTerminated = isTerminated;
|
115 | }
|
116 | setIsInitHookCalled(isInitHookCalled) {
|
117 | this.isInitHookCalled = isInitHookCalled;
|
118 | }
|
119 | async closeApplication() {
|
120 | this.socketModule && (await this.socketModule.close());
|
121 | this.microservicesModule && (await this.microservicesModule.close());
|
122 | await super.close();
|
123 | this.setIsTerminated(true);
|
124 | }
|
125 | async dispose() {
|
126 | await this.server.close();
|
127 | if (this.isTerminated) {
|
128 | return;
|
129 | }
|
130 | this.socketModule && (await this.socketModule.close());
|
131 | this.microservicesModule && (await this.microservicesModule.close());
|
132 | }
|
133 | }
|
134 | exports.NestMicroservice = NestMicroservice;
|