UNPKG

4.12 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ServerTCP = void 0;
4const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
5const net = require("net");
6const constants_1 = require("../constants");
7const tcp_context_1 = require("../ctx-host/tcp.context");
8const enums_1 = require("../enums");
9const helpers_1 = require("../helpers");
10const tls_1 = require("tls");
11const server_1 = require("./server");
12class ServerTCP extends server_1.Server {
13 constructor(options) {
14 super();
15 this.options = options;
16 this.transportId = enums_1.Transport.TCP;
17 this.isExplicitlyTerminated = false;
18 this.retryAttemptsCount = 0;
19 this.port = this.getOptionsProp(options, 'port') || constants_1.TCP_DEFAULT_PORT;
20 this.host = this.getOptionsProp(options, 'host') || constants_1.TCP_DEFAULT_HOST;
21 this.socketClass =
22 this.getOptionsProp(options, 'socketClass') || helpers_1.JsonSocket;
23 this.tlsOptions = this.getOptionsProp(options, 'tlsOptions');
24 this.init();
25 this.initializeSerializer(options);
26 this.initializeDeserializer(options);
27 }
28 listen(callback) {
29 this.server.once(constants_1.ERROR_EVENT, (err) => {
30 if (err?.code === constants_1.EADDRINUSE || err?.code === constants_1.ECONNREFUSED) {
31 return callback(err);
32 }
33 });
34 this.server.listen(this.port, this.host, callback);
35 }
36 close() {
37 this.isExplicitlyTerminated = true;
38 this.server.close();
39 }
40 bindHandler(socket) {
41 const readSocket = this.getSocketInstance(socket);
42 readSocket.on(constants_1.MESSAGE_EVENT, async (msg) => this.handleMessage(readSocket, msg));
43 readSocket.on(constants_1.ERROR_EVENT, this.handleError.bind(this));
44 }
45 async handleMessage(socket, rawMessage) {
46 const packet = await this.deserializer.deserialize(rawMessage);
47 const pattern = !(0, shared_utils_1.isString)(packet.pattern)
48 ? JSON.stringify(packet.pattern)
49 : packet.pattern;
50 const tcpContext = new tcp_context_1.TcpContext([socket, pattern]);
51 if ((0, shared_utils_1.isUndefined)(packet.id)) {
52 return this.handleEvent(pattern, packet, tcpContext);
53 }
54 const handler = this.getHandlerByPattern(pattern);
55 if (!handler) {
56 const status = 'error';
57 const noHandlerPacket = this.serializer.serialize({
58 id: packet.id,
59 status,
60 err: constants_1.NO_MESSAGE_HANDLER,
61 });
62 return socket.sendMessage(noHandlerPacket);
63 }
64 const response$ = this.transformToObservable(await handler(packet.data, tcpContext));
65 response$ &&
66 this.send(response$, data => {
67 Object.assign(data, { id: packet.id });
68 const outgoingResponse = this.serializer.serialize(data);
69 socket.sendMessage(outgoingResponse);
70 });
71 }
72 handleClose() {
73 if (this.isExplicitlyTerminated ||
74 !this.getOptionsProp(this.options, 'retryAttempts') ||
75 this.retryAttemptsCount >=
76 this.getOptionsProp(this.options, 'retryAttempts')) {
77 return undefined;
78 }
79 ++this.retryAttemptsCount;
80 return setTimeout(() => this.server.listen(this.port, this.host), this.getOptionsProp(this.options, 'retryDelay') || 0);
81 }
82 init() {
83 if (this.tlsOptions) {
84 // TLS enabled, use tls server
85 this.server = (0, tls_1.createServer)(this.tlsOptions, this.bindHandler.bind(this));
86 }
87 else {
88 // TLS disabled, use net server
89 this.server = net.createServer(this.bindHandler.bind(this));
90 }
91 this.server.on(constants_1.ERROR_EVENT, this.handleError.bind(this));
92 this.server.on(constants_1.CLOSE_EVENT, this.handleClose.bind(this));
93 }
94 getSocketInstance(socket) {
95 return new this.socketClass(socket);
96 }
97}
98exports.ServerTCP = ServerTCP;