1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.JsonSocket = void 0;
|
4 | const string_decoder_1 = require("string_decoder");
|
5 | const constants_1 = require("../constants");
|
6 | const corrupted_packet_length_exception_1 = require("../errors/corrupted-packet-length.exception");
|
7 | const invalid_json_format_exception_1 = require("../errors/invalid-json-format.exception");
|
8 | const net_socket_closed_exception_1 = require("../errors/net-socket-closed.exception");
|
9 | class JsonSocket {
|
10 | constructor(socket) {
|
11 | this.socket = socket;
|
12 | this.contentLength = null;
|
13 | this.isClosed = false;
|
14 | this.buffer = '';
|
15 | this.stringDecoder = new string_decoder_1.StringDecoder();
|
16 | this.delimeter = '#';
|
17 | this.socket.on(constants_1.DATA_EVENT, this.onData.bind(this));
|
18 | this.socket.on(constants_1.CONNECT_EVENT, () => (this.isClosed = false));
|
19 | this.socket.on(constants_1.CLOSE_EVENT, () => (this.isClosed = true));
|
20 | this.socket.on(constants_1.ERROR_EVENT, () => (this.isClosed = true));
|
21 | }
|
22 | get netSocket() {
|
23 | return this.socket;
|
24 | }
|
25 | connect(port, host) {
|
26 | this.socket.connect(port, host);
|
27 | return this;
|
28 | }
|
29 | on(event, callback) {
|
30 | this.socket.on(event, callback);
|
31 | return this;
|
32 | }
|
33 | once(event, callback) {
|
34 | this.socket.once(event, callback);
|
35 | return this;
|
36 | }
|
37 | end() {
|
38 | this.socket.end();
|
39 | return this;
|
40 | }
|
41 | sendMessage(message, callback) {
|
42 | if (this.isClosed) {
|
43 | callback && callback(new net_socket_closed_exception_1.NetSocketClosedException());
|
44 | return;
|
45 | }
|
46 | this.socket.write(this.formatMessageData(message), 'utf-8', callback);
|
47 | }
|
48 | onData(dataRaw) {
|
49 | const data = Buffer.isBuffer(dataRaw)
|
50 | ? this.stringDecoder.write(dataRaw)
|
51 | : dataRaw;
|
52 | try {
|
53 | this.handleData(data);
|
54 | }
|
55 | catch (e) {
|
56 | this.socket.emit(constants_1.ERROR_EVENT, e.message);
|
57 | this.socket.end();
|
58 | }
|
59 | }
|
60 | handleData(data) {
|
61 | this.buffer += data;
|
62 | if (this.contentLength == null) {
|
63 | const i = this.buffer.indexOf(this.delimeter);
|
64 | |
65 |
|
66 |
|
67 |
|
68 | if (i !== -1) {
|
69 | const rawContentLength = this.buffer.substring(0, i);
|
70 | this.contentLength = parseInt(rawContentLength, 10);
|
71 | if (isNaN(this.contentLength)) {
|
72 | this.contentLength = null;
|
73 | this.buffer = '';
|
74 | throw new corrupted_packet_length_exception_1.CorruptedPacketLengthException(rawContentLength);
|
75 | }
|
76 | this.buffer = this.buffer.substring(i + 1);
|
77 | }
|
78 | }
|
79 | if (this.contentLength !== null) {
|
80 | const length = this.buffer.length;
|
81 | if (length === this.contentLength) {
|
82 | this.handleMessage(this.buffer);
|
83 | }
|
84 | else if (length > this.contentLength) {
|
85 | const message = this.buffer.substring(0, this.contentLength);
|
86 | const rest = this.buffer.substring(this.contentLength);
|
87 | this.handleMessage(message);
|
88 | this.onData(rest);
|
89 | }
|
90 | }
|
91 | }
|
92 | handleMessage(data) {
|
93 | this.contentLength = null;
|
94 | this.buffer = '';
|
95 | let message;
|
96 | try {
|
97 | message = JSON.parse(data);
|
98 | }
|
99 | catch (e) {
|
100 | throw new invalid_json_format_exception_1.InvalidJSONFormatException(e, data);
|
101 | }
|
102 | message = message || {};
|
103 | this.socket.emit(constants_1.MESSAGE_EVENT, message);
|
104 | }
|
105 | formatMessageData(message) {
|
106 | const messageData = JSON.stringify(message);
|
107 | const length = messageData.length;
|
108 | const data = length + this.delimeter + messageData;
|
109 | return data;
|
110 | }
|
111 | }
|
112 | exports.JsonSocket = JsonSocket;
|