This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathConnection.js
157 lines (130 loc) · 3.79 KB
/
Connection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
const EventEmitter = require('events'),
ConnectionDelegate = require('./ConnectionDelegate'),
ResourceRepository = require('./Data/ResourceRepository'),
Instruction = require('./Instruction'),
DataSerializer = require('./Data/Serializer'),
DataUnserializer = require('./Data/Unserializer'),
Logger = require('./Logger');
/**
* Handle a connection interacting with this process.
*/
class Connection extends EventEmitter
{
/**
* Constructor.
*
* @param {net.Socket} socket
* @param {ConnectionDelegate} delegate
*/
constructor(socket, delegate)
{
super();
this.socket = this.configureSocket(socket);
this.delegate = delegate;
this.resources = new ResourceRepository;
this.dataSerializer = new DataSerializer(this.resources);
this.dataUnserializer = new DataUnserializer(this.resources);
}
/**
* Configure the socket for communication.
*
* @param {net.Socket} socket
* @return {net.Socket}
*/
configureSocket(socket)
{
socket.setEncoding('utf8');
socket.on('data', data => {
this.emit('activity');
this.handleSocketData(data);
});
return socket;
}
/**
* Handle data received on the socket.
*
* @param {string} data
*/
handleSocketData(data)
{
const instruction = new Instruction(JSON.parse(data), this.resources, this.dataUnserializer),
{responseHandler, errorHandler} = this.createInstructionHandlers();
this.delegate.handleInstruction(instruction, responseHandler, errorHandler);
}
/**
* Generate response and errors handlers.
*
* @return {Object}
*/
createInstructionHandlers()
{
let handlerHasBeenCalled = false;
const handler = (serializingMethod, value) => {
if (handlerHasBeenCalled) {
throw new Error('You can call only once the response/error handler.');
}
handlerHasBeenCalled = true;
this.writeToSocket(JSON.stringify({
logs: Logger.logs(),
value: this[serializingMethod](value),
}));
};
return {
responseHandler: handler.bind(this, 'serializeValue'),
errorHandler: handler.bind(this, 'serializeError'),
};
}
/**
* Write a string to the socket by slitting it in packets of fixed length.
*
* @param {string} str
*/
writeToSocket(str)
{
const payload = Buffer.from(str).toString('base64');
const bodySize = Connection.SOCKET_PACKET_SIZE - Connection.SOCKET_HEADER_SIZE,
chunkCount = Math.ceil(payload.length / bodySize);
for (let i = 0 ; i < chunkCount ; i++) {
const chunk = payload.substr(i * bodySize, bodySize);
let chunksLeft = String(chunkCount - 1 - i);
chunksLeft = chunksLeft.padStart(Connection.SOCKET_HEADER_SIZE - 1, '0');
this.socket.write(`${chunksLeft}:${chunk}`);
}
}
/**
* Serialize a value to return to the client.
*
* @param {*} value
* @return {Object}
*/
serializeValue(value)
{
return this.dataSerializer.serialize(value);
}
/**
* Serialize an error to return to the client.
*
* @param {Error} error
* @return {Object}
*/
serializeError(error)
{
return DataSerializer.serializeError(error);
}
}
/**
* The size of a packet sent through the sockets.
*
* @constant
* @type {number}
*/
Connection.SOCKET_PACKET_SIZE = 1024;
/**
* The size of the header in each packet sent through the sockets.
*
* @constant
* @type {number}
*/
Connection.SOCKET_HEADER_SIZE = 5;
module.exports = Connection;