-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtcpserver.js
140 lines (131 loc) · 4.47 KB
/
tcpserver.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
'use strict';
var mjml = require('mjml'),
mjml_maj_ver = parseInt(require('mjml/package.json').version.split('.')[0]),
net = require('net'),
fs = require('fs'),
argv = process.argv.slice(2),
server = null,
conf = {
host: '127.0.0.1',
port: '28101',
touchstop: null,
mjml: {}
};
function terminate(exit_code) {
if (server && server.listening) {
server.close(function () {
process.exit(exit_code);
});
} else {
process.exit(exit_code);
}
}
process.on('SIGINT', function() {
terminate(0);
});
process.on('SIGTERM', function() {
terminate(0);
});
for (var i = 0; i < argv.length; i++) {
var kv, key, val,
arg = argv[i];
try {
if (!arg.startsWith('--')) {
throw {message: 'unknown arg'};
} else {
arg = arg.slice(2);
}
if (arg === 'help') {
if (mjml_maj_ver >= 4) {
// more options: https://github.com/mjmlio/mjml/blob/master/packages/mjml-core/src/index.js#L34
console.log('Run command: NODE_PATH=node_modules node tcpserver.js ' +
'--port=28101 --host=127.0.0.1 --touchstop=/tmp/mjmltcpserver.stop ' +
'--mjml.minify=false --mjml.validationLevel=soft');
} else {
// more options: https://github.com/mjmlio/mjml/blob/3.3.x/packages/mjml-core/src/MJMLRenderer.js#L78
console.log('Run command: NODE_PATH=node_modules node tcpserver.js ' +
'--port=28101 --host=127.0.0.1 --touchstop=/tmp/mjmltcpserver.stop ' +
'--mjml.disableMinify=false --mjml.level=soft');
}
terminate(0);
}
kv = arg.split('=', 2);
key = kv[0];
val = kv[1];
if (!key || !val) throw {message: 'wrong syntax'};
if (conf.hasOwnProperty(key) && key !== 'mjml') {
conf[key] = val;
} else if (key.startsWith('mjml.')) {
if (val === 'true') {
val = true;
} else if (val === 'false') {
val = false;
}
conf.mjml[key.slice(5)] = val;
} else {
throw {message: 'unknown arg'};
}
} catch (err) {
console.log('Invalid parsing arg "%s": %s', argv[i], err.message);
terminate(1);
}
}
function handleConnection(conn) {
var total_data = '',
header_size = 9,
total_data_size, data_size, result;
conn.setEncoding('utf8');
conn.on('data', function(d) {
total_data += d;
total_data_size = Buffer.byteLength(total_data);
if (total_data.length < header_size) return;
if (data_size === undefined) data_size = parseInt(total_data.slice(0, header_size)) + header_size;
if (total_data_size < data_size) {
return;
} else if (total_data_size > data_size) {
result = 'MJML server received too many data';
conn.write('1');
} else {
try {
total_data = total_data.slice(header_size).toString();
if (mjml_maj_ver >= 4) {
result = mjml(total_data, conf.mjml);
} else {
result = mjml.mjml2html(total_data, conf.mjml);
}
if (typeof result === 'object') {
if (result.errors.length) throw {message: JSON.stringify(result.errors, null, 2)};
result = result.html;
}
conn.write('0');
} catch (err) {
result = err.message;
conn.write('1');
}
}
conn.write((Array(header_size + 1).join('0') + Buffer.byteLength(result).toString()).slice(-9));
conn.write(result);
total_data = '';
data_size = undefined;
result = undefined;
});
conn.on('close', function() {});
conn.on('error', function(err) {});
conn.on('end', function() {});
}
server = net.createServer();
server.on('connection', handleConnection);
server.listen(conf.port, conf.host, function () {
console.log('RUN SERVER %s:%s', conf.host, conf.port);
});
if (conf.touchstop) {
try {
fs.statSync(conf.touchstop);
} catch (e) {
fs.closeSync(fs.openSync(conf.touchstop, 'w'));
}
fs.watchFile(conf.touchstop, function() {
console.log('STOP SERVER (cause touchstop)');
terminate(0);
});
}