UNPKG

3.44 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 json_socket_1 = require("../helpers/json-socket");
10const server_1 = require("./server");
11class ServerTCP extends server_1.Server {
12 constructor(options) {
13 super();
14 this.options = options;
15 this.transportId = enums_1.Transport.TCP;
16 this.isExplicitlyTerminated = false;
17 this.retryAttemptsCount = 0;
18 this.port = this.getOptionsProp(options, 'port') || constants_1.TCP_DEFAULT_PORT;
19 this.host = this.getOptionsProp(options, 'host') || constants_1.TCP_DEFAULT_HOST;
20 this.init();
21 this.initializeSerializer(options);
22 this.initializeDeserializer(options);
23 }
24 listen(callback) {
25 this.server.listen(this.port, this.host, callback);
26 }
27 close() {
28 this.isExplicitlyTerminated = true;
29 this.server.close();
30 }
31 bindHandler(socket) {
32 const readSocket = this.getSocketInstance(socket);
33 readSocket.on(constants_1.MESSAGE_EVENT, async (msg) => this.handleMessage(readSocket, msg));
34 readSocket.on(constants_1.ERROR_EVENT, this.handleError.bind(this));
35 }
36 async handleMessage(socket, rawMessage) {
37 const packet = this.deserializer.deserialize(rawMessage);
38 const pattern = !shared_utils_1.isString(packet.pattern)
39 ? JSON.stringify(packet.pattern)
40 : packet.pattern;
41 const tcpContext = new tcp_context_1.TcpContext([socket, pattern]);
42 if (shared_utils_1.isUndefined(packet.id)) {
43 return this.handleEvent(pattern, packet, tcpContext);
44 }
45 const handler = this.getHandlerByPattern(pattern);
46 if (!handler) {
47 const status = 'error';
48 const noHandlerPacket = this.serializer.serialize({
49 id: packet.id,
50 status,
51 err: constants_1.NO_MESSAGE_HANDLER,
52 });
53 return socket.sendMessage(noHandlerPacket);
54 }
55 const response$ = this.transformToObservable(await handler(packet.data, tcpContext));
56 response$ &&
57 this.send(response$, data => {
58 Object.assign(data, { id: packet.id });
59 const outgoingResponse = this.serializer.serialize(data);
60 socket.sendMessage(outgoingResponse);
61 });
62 }
63 handleClose() {
64 if (this.isExplicitlyTerminated ||
65 !this.getOptionsProp(this.options, 'retryAttempts') ||
66 this.retryAttemptsCount >=
67 this.getOptionsProp(this.options, 'retryAttempts')) {
68 return undefined;
69 }
70 ++this.retryAttemptsCount;
71 return setTimeout(() => this.server.listen(this.port), this.getOptionsProp(this.options, 'retryDelay') || 0);
72 }
73 init() {
74 this.server = net.createServer(this.bindHandler.bind(this));
75 this.server.on(constants_1.ERROR_EVENT, this.handleError.bind(this));
76 this.server.on(constants_1.CLOSE_EVENT, this.handleClose.bind(this));
77 }
78 getSocketInstance(socket) {
79 return new json_socket_1.JsonSocket(socket);
80 }
81}
82exports.ServerTCP = ServerTCP;