forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 1
/
line_socket.js
56 lines (48 loc) · 1.41 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
53
54
55
56
"use strict";
// A subclass of Socket which reads data by line
var net = require('net');
var tls = require('./tls_socket');
var util = require('util');
var line_regexp = /^([^\n]*\n)/;
function Socket(options) {
if (!(this instanceof Socket)) return new Socket(options);
var self = this;
net.Socket.call(this, options);
setup_line_processor(this);
}
function setup_line_processor (self) {
var current_data = '';
self.process_data = function (data) {
current_data += data;
var results;
while (results = line_regexp.exec(current_data)) {
var this_line = results[1];
current_data = current_data.slice(this_line.length);
self.emit('line', this_line);
}
};
self.process_end = function () {
if (current_data.length)
self.emit('line', current_data)
current_data = '';
};
self.on('data', function (data) { self.process_data(data) });
self.on('end', function () { self.process_end() });
}
util.inherits(Socket, net.Socket);
exports.Socket = Socket;
// New interface - uses TLS
exports.connect = function (port, host, cb) {
var options = {};
if (typeof port === 'object') {
options = port;
cb = host;
}
else {
options.port = port;
options.host = host;
}
var sock = tls.connect(options, cb);
setup_line_processor(sock);
return sock;
}