/
fserver.js
505 lines (402 loc) · 13 KB
/
fserver.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*!
* liveview FileServer
*/
require('shelljs/global');
var http = require('http')
, net = require('net')
, url = require('url')
, path = require('path')
, join = path.join
, fs = require('fs')
, os = require('os')
, fsWatcher = require('chokidar')
, colors = require('coloring')
, debug = require('debug')('liveview:server')
, pkgConf = require('../package')
, util = require('util')
, detect = require('jsparse-tools').lexscope
, Hypar = require('hypar');
var OS_TYPE = ('' + os.type()).toLowerCase();
var HOME_DIR = process.env.HOME || process.env.USERPROFILE;
var TMP_DIR = join(HOME_DIR, '/.titanium/')
var PID = process.pid;
var PID_FILE = join(TMP_DIR, (PID + '-liveview'));
var noop = function() {};
var log = (process.env.SILENT) ? noop : console.log;
var logError = (process.env.SILENT) ? noop : console.error;
/**
* main export
*/
var FServer = module.exports = {};
/**
* [pids description]
* @return {[type]} [description]
*/
FServer.pids = function (env) {
var pids = [];
try {
pids = ls(join(TMP_DIR, '*-liveview'));
!!env && log('[LiveView]'.green, 'Current running servers pid files: ' + (pids.join(' ').trim() || 'No Active Servers'.grey) + '\n');
} catch (e) {
debug(e);
// do nothing
}
return pids;
};
/**
* [status description]
* @return {[type]} [description]
*/
FServer.status = function (env) {
var paths = cat(join(TMP_DIR, '*-liveview'));
!!env && log('[LiveView]'.green, 'Current running servers: ' + (paths.trim() || 'No Active Servers'.grey) + '\n');
return paths;
};
/**
* [restart description]
* @return {[type]} [description]
*/
FServer.restart = function () {
var paths = FServer.status().split('\n');
FServer.stop();
paths.forEach(function(path){
if (path.length) {
FServer.start({
path: path
});
}
});
};
/**
* [stop description]
* @return {[type]} [description]
*/
FServer.stop = function (env) {
var pids = ls(join(TMP_DIR, '*-liveview'));
if (!pids.length) {
!!env && logError('[LiveView]'.red, 'No active servers');
return;
}
pids.forEach(function (pidPath) {
try {
var _pid = pidPath.replace(TMP_DIR, '').split('-')[0];
rm(pidPath);
log('[LiveView]'.green, 'Closing file/event server process id: ' + _pid);
process.kill(_pid);
} catch (e) {
logError('[LiveView]'.red, 'Error closing server', e);
}
});
debug('Killed PIDS:', pids);
};
/**
* [start description]
* @param {[type]} opts [description]
* @return {[type]} [description]
*/
FServer.start = function(opts) {
opts = opts || {};
debug('Liveview File Server Opts', opts);
debug('Running PIDS:', FServer.pids());
log('[LiveView]'.green, 'version', pkgConf.version);
var RESOURCE_DIR = path.resolve(opts.path || opts.projectDir || process.cwd());
RESOURCE_DIR = (!~RESOURCE_DIR.indexOf('Resources'))
? RESOURCE_DIR + '/Resources'
: RESOURCE_DIR;
var PROJECT_DIR = join(RESOURCE_DIR, '../');
var LV_DIR = join(__dirname, '../');
var BIN_DIR = join(LV_DIR, '../bin');
var ALLOY_DIR = join(PROJECT_DIR, 'app');
var LOCALE_DIR = join(PROJECT_DIR, 'i18n');
var HOST = opts.host || opts.liveviewIp;
var FPORT = opts.fport || opts.liveviewFport || 8324;
var EPORT = opts.eport || opts.liveviewEport || 8323;
var isAlloy = fs.existsSync(ALLOY_DIR);
var hasLocale = fs.existsSync(LOCALE_DIR);
var isBuilding = false;
var fileCount = {};
var PLATFORM = opts.platform;
if (!fs.existsSync(RESOURCE_DIR) || !fs.existsSync(join(PROJECT_DIR, 'tiapp.xml'))) {
logError('[LiveView]'.red, 'Project directory "' + PROJECT_DIR + '" is not a valid Titanium project.\n');
process.exit(1);
return;
}
// create new pid file
fs.existsSync(PID_FILE) && fs.unlinkSync(PID_FILE);
fs.writeFileSync(PID_FILE, PROJECT_DIR);
// create static file server
var fServer = http.createServer(function(request, response) {
debug('[LiveView] File Server host: ', request.headers.host, '\n');
var uri = url.parse(request.url).pathname || '';
var requestPlatform = request.headers['x-platform'] || PLATFORM;
var platformFilepath = join(RESOURCE_DIR, requestPlatform, uri);
var filename = join(RESOURCE_DIR, uri);
var overridePath = join(__dirname, 'overrides', uri);
if (uri === '/') {
response.writeHead(200, {
'Content-Type': 'application/json'
});
var data = {
pid: process.pid,
path: PROJECT_DIR,
uptime: process.uptime(),
alloy: isAlloy
};
response.write(JSON.stringify(data));
response.end();
return;
}
if (uri === '/kill') {
response.end('');
fServer.close();
evtServer.close();
rm('-rf', PID_FILE);
process.exit(0);
}
debug('Request Headers:', '\n', util.inspect(request.headers), '\n');
filename = (fs.existsSync(overridePath))
? overridePath
: (fs.existsSync(platformFilepath))
? platformFilepath
: filename;
if (uri === '/localeStrings.js') {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Project-ID': 'GUID_XXXXXX'
});
if (!hasLocale) {
response.write('module.exports = {};');
response.end();
return;
}
var langs = fs.readdirSync(LOCALE_DIR).filter(function(name) {
return fs.statSync(path.resolve(LOCALE_DIR, name)).isDirectory();
});
var langData = {};
function decodeEntity(str) {
var names = {
'nbsp': 160,
'lt': 60,
'gt': 62,
'amp': 38,
'cent': 162,
'pound': 163,
'yen': 164,
'euro': 8364,
'copy': 169,
'reg:': 174
};
return ('' + str).replace(/&#?([\w\d]+);?/g, function(s, entity) {
entity = (isNaN(entity)) ? names[entity] : entity;
return String.fromCharCode(encodeURI(entity).replace('%'));
});
};
function next(i) {
if (!langs[i]) {
response.write('module.exports = ' + JSON.stringify(langData) + ';');
response.end();
return;
}
var tags = {};
var parser = Hypar();
parser.on('tag', function (e) {
if(e.name !== 'string') { return; }
tags[e.attr.name] = decodeEntity(this._text);
});
fs.createReadStream(join(LOCALE_DIR, langs[i], 'strings.xml'))
.pipe(parser)
.on('data', function (e) {
langData[langs[i]] = tags;
return next(i+1);
});
}
next(0);
} else {
fs.exists(filename, function(exists) {
fileCount[filename] = (fileCount[filename] || 0) + 1;
// return 404 if file does not exist
if (!exists) {
response.writeHead(404, {
'Content-Type': 'text/plain',
'Project-ID': 'todo'
});
response.write('404 Not Found\n');
response.end();
return;
}
// responsd with file string
fs.readFile(filename, function(err, file) {
var out = (err)
? { code: 500, data: err }
: { code: 200, data: file };
debug('File Server Response',
'\n' +
' requested: ' + fileCount[filename] + ' time(s)\n' +
' file: ' + uri.replace('/', '') + '\n' +
' response: ' + out.code + '\n' +
' length: ' + out.data.length + '\n'
);
response.writeHead(out.code, {
'Content-Type': 'text/plain',
'Project-ID': 'GUID_XXXXXX'
});
if (uri === '/app.js') {
var scope = detect(out.data);
// TODO probably a better way to do this, but this fix will work for now.
out.data = String(out.data).replace(/^#![^\n]*\n/, '').split('');
var v = Object.keys(scope.lexvars['']);
var shiftCount = 0;
v.forEach(function (vr) {
var line = scope.lexvars[''][vr],
start;
if (line.type === 'VariableDeclarator') {
start = line.parent.start + shiftCount;
out.data[start] = ' ';
out.data[start + 1] = ' ';
out.data[start + 2] = ' ';
if (line.init === null) {
out.data.splice(line.end + shiftCount, 0, ' = null');
shiftCount++;
}
}
});
out.data = out.data.join('');
debug('[LiveView] app.js global variables: ', scope.locals[''], '\n');
scope.locals[''].forEach(function(lexVar){
out.data = out.data + '\ntry { lvGlobal._globalCtx.' + lexVar + ' = ' + lexVar + '; } catch(e) {}';
});
response.write(out.data);
response.end();
} else {
response.write(out.data);
response.end();
}
});
});
}
});
// start listening for requests
fServer.listen(parseInt(FPORT, 10), HOST, function() {
log('[LiveView]'.green, 'File Server Started on', ((HOST ? HOST : '0.0.0.0') + ':' + FPORT).bold);
});
// TCP Server for two messaging between running app and server
var evtServer = net.createServer();
// connect clients
evtServer.clients = [];
// listen for new client connections
evtServer.on('connection', function (client) {
log('[LiveView]'.green, 'Client connected');
evtServer.clients.push(client);
client.on('end', function(e){
log('[LiveView]'.green, 'Client disconnected');
//evtServer.clients.splice(client);
});
client.on('data', function(e){
log('data:'+e);
});
});
evtServer.on('close', function (client) {
var clients = evtServer.clients;
var clientLen = clients.length;
for (var i = 0; i < clientLen; i++) {
log(client[i]);
//clients.splice(i);
}
});
evtServer.on('error', function (e) {
if (e.code === 'EADDRINUSE') {
log('Address in use, retrying...');
setTimeout(function () {
evtServer.close();
evtServer.listen(parseInt(EPORT));
}, 1000);
}
});
evtServer.on('change', function(file){
var clients = evtServer.clients.slice(0);
var clientLen = clients.length;
logFsChange(file, 'Changed');
for (var i = 0; i < clientLen; i++) {
try {
clients[i].write(JSON.stringify({type:'event', name:'reload'}));
evtServer.clients.shift();
} catch(e){
//clients.splice(i);
}
}
});
// start up event server
evtServer.listen(parseInt(EPORT, 10), HOST, function() {
log('[LiveView]'.green, 'Event Server Started on', ((HOST ? HOST : '0.0.0.0') + ':' + EPORT).bold);
});
/**
* watch Resources dir for file changes
*/
var resourceWatcher = evtServer.watcher = fsWatcher.watch(RESOURCE_DIR, { persistent: true, ignoreInitial: true });
resourceWatcher.on('change', function (file) {
if (isBuilding) { return; }
evtServer.emit('change', file);
});
resourceWatcher.on('error', function (path) {
logError('[LiveView]'.red, 'Error loading watching file', path);
});
/**
* watch i18n dir for file changes
*/
if (hasLocale) {
var localeWatcher = evtServer.watcher = fsWatcher.watch(LOCALE_DIR, { persistent: true, ignoreInitial: true });
localeWatcher.on('change', function (file) {
if (isBuilding) { return; }
evtServer.emit('change', file);
});
localeWatcher.on('error', function (path) {
logError('[LiveView]'.red, 'Error loading watching file', path);
});
}
/**
* check if alloy project folder should be watched
*/
if (fs.existsSync(ALLOY_DIR)) {
var alloyWatcher = fsWatcher.watch(ALLOY_DIR, { persistent: true, ignoreInitial: true });
log('[LiveView]'.green, 'Alloy project monitor started');
alloyWatcher.on('change', function (file) {
if (isBuilding) { return log('[LiveView]'.green, 'File changes ignored while Alloy compiling'); }
isBuilding = true;
log('[LiveView]'.green, 'Alloy recompile initiated for', PLATFORM);
exec('appc alloy compile \"' + ALLOY_DIR + '\" --no-colors --config platform=' + PLATFORM, {silent: true}, function (code, error) {
isBuilding = false;
if(code) { return logError('[LiveView]'.red, error); }
evtServer.emit('change', file);
});
});
}
process.on('SIGINT', function() {
fServer.close();
evtServer.close();
rm('-rf', PID_FILE);
process.exit(0);
});
};
process.on('uncaughtException', function (e) {
logError('[LiveView]'.red, e);
});
/**
* [escape description]
* @param {[type]} str [description]
* @return {[type]} [description]
*/
function escape() {
var args = join.apply(this, [].slice.call(arguments));
return '"' + args + '"';
}
/**
* [logFsChange description]
* @param {[type]} file [description]
* @param {[type]} event [description]
* @return {[type]} [description]
*/
function logFsChange(file, event){
log('[LiveView]'.green, 'Reload Triggered');
log(' REASON:'.grey, 'File', event);
log(' FILE:'.grey, file);
}