-
-
Notifications
You must be signed in to change notification settings - Fork 661
/
line_socket.js
52 lines (44 loc) · 1.22 KB
/
line_socket.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
'use strict';
// A subclass of Socket which reads data by line
const net = require('node:net');
const utils = require('haraka-utils');
const tls_socket = require('./tls_socket');
class Socket extends net.Socket {
constructor (options) {
super(options);
setup_line_processor(this);
}
}
function setup_line_processor (socket) {
let current_data = '';
socket.on('data', function on_socket_data (data) {
current_data += data;
let results;
while ((results = utils.line_regexp.exec(current_data))) {
const this_line = results[1];
current_data = current_data.slice(this_line.length);
socket.emit('line', this_line);
}
})
socket.on('end', function on_socket_end () {
if (current_data.length) {
socket.emit('line', current_data);
}
current_data = '';
})
}
exports.Socket = Socket;
// New interface - uses TLS
exports.connect = (port, host) => {
let options = {};
if (typeof port === 'object') {
options = port;
}
else {
options.port = port;
options.host = host;
}
const sock = tls_socket.connect(options);
setup_line_processor(sock);
return sock;
}