/
impress.application.js
796 lines (748 loc) · 29.5 KB
/
impress.application.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
'use strict';
// Directories for application
//
impress.applicationDirs = [ 'server', 'static', 'config', 'tasks', 'init', 'setup', 'model', 'lib', 'files', 'templates' ];
impress.applicationPlaces = [ 'tasks', 'init', 'setup', 'model', 'lib' ];
impress.defaultTemplates = [ 'error', 'index', 'introspection' ];
// Mixin application methods to given object
// Application should have:
// .dir - application root
//
impress.mixinApplication = function(application) {
application.backend = impress.eventEmitter();
application.frontend = impress.eventEmitter();
// Class factory for this application
//
application.createApplicationClass = function(className) {
var BaseClass = impress[className];
if (BaseClass) {
var ChildClass = function() {
this.constructor.super_.apply(this, arguments);
};
api.util.inherits(ChildClass, BaseClass);
application[className] = ChildClass;
ChildClass.prototype.application = application;
}
};
if (application !== impress) application.createApplicationClass('Client');
application.config = {}; // key is file name, value is object, exported from file
application.tasks = {}; // key is task name, value is Function with parameters
application.model = {}; // key is model name, value is object exported from model definition file
application.users = {}; // key is login, value is instance of User class
application.ips = {}; // key is ip in integer form, value is instance of IP class
application.sessions = {}; // key is sid, value is instance of Session class
application.channels = {};
application.workers = {};
application.longWorkers = {};
// RPS Limits (request per second)
// Arrays of structure { rps, time }
// rps - request counter
// sec - time in secconds
//
application.limit = {
ips: [], // keyed by IP
sid: [] // keyed by SID
};
// RPS Limits (request per second)
// Arrays of structure { rps, time }
// rps - request counter
// sec - time in secconds
//
application.deny = {
ips: [], // keyed by IP
sid: [] // keyed by SID
};
application.isInitialized = false;
// Refactor: remove .domain from application
application.domain = api.domain.create();
// Create execution domain
//
application.catchException = function(err) {
if (application.domain.client) {
// Refactor: remove .client from domain
application.domain.client.error(500, err);
application.domain.client = null;
}
application.logException(err);
};
application.domain.on('error', application.catchException);
// Log application error with stack trace
//
application.logException = function(err) {
var separator = impress.isWin ? '\\' : '/',
path = __dirname + separator,
rxPath1 = new RegExp(api.impress.escapeRegExp(api.path.dirname(__dirname) + separator + 'node_modules'), 'g'),
rxPath2 = new RegExp(api.impress.escapeRegExp(path), 'g'),
rxPath3 = new RegExp(api.impress.escapeRegExp(process.cwd() + separator + 'node_modules'), 'g'),
rxPath4 = new RegExp(api.impress.escapeRegExp(process.cwd()), 'g'),
stack = err.stack;
if (!stack) stack = err.toString();
stack = stack.replace(rxPath1, '').replace(rxPath2, '').replace(rxPath3, '').replace(rxPath4, '').replace(/\n\s{4,}at/g, ';');
if (application.log && application.log.error) application.log.error(stack);
else if (impress.log && impress.log.error) impress.log.error(stack);
else console.log(stack);
};
// Check system templates
//
function checkTemplate(templateName, templatePath) {
api.fs.exists(templatePath, function(exists) {
if (exists) application.systemTemplates[templateName] = templatePath;
});
}
application.systemTemplates = {};
var templateName, templatePath;
for (var j = 0, jlen = impress.defaultTemplates.length; j < jlen; j++) {
templateName = impress.defaultTemplates[j];
templatePath = application.dir + '/templates/' + templateName + '.template';
application.systemTemplates[templateName] = impress.moduleDir + '/templates/' + templateName + '.template';
checkTemplate(templateName, templatePath);
}
// Convert absolute path to relative
//
application.relative = function(path) {
return path.substr(application.dir.length);
};
// Compile, execute and save script exports to cache or get exports from cache,
// callback(err, exports), where exports - function or object exported from script
//
application.createScript = function(fileName, callback) {
var exports = application.cache.scripts[application.relative(fileName)];
if (exports && callback) callback(null, exports);
else api.fs.readFile(fileName, function(err, code) {
if (err) {
var logger = application.log || impress.log;
logger.error(impress.CANT_READ_FILE + fileName);
callback(err);
} else {
var scriptName = fileName.replace(application.isMainApplication ? impress.dir : impress.applicationsDir, '');
try {
var script = api.vm.createScript(code, scriptName);
application.sandbox.module = {};
script.runInNewContext(application.sandbox);
exports = application.sandbox.module.exports;
application.sandbox.module = {};
} catch(err) {
err.stack = err.toString() + ' in '+scriptName;
application.logException(err);
callback(err);
return;
}
application.cache.scripts[application.relative(fileName)] = exports;
}
callback(null, exports);
});
};
// Create application sandbox
//
application.createSandbox = function(callback) {
var sandbox = { module: {}, api: {}, callInContext: api.impress.callInContextMethod };
sandbox.global = sandbox;
sandbox.application = application;
application.sandbox = api.vm.createContext(sandbox);
application.loadConfigFile('sandbox.js', function() {
if (application.config.sandbox) {
var module, moduleName, moduleAlias;
var globals = application.config.sandbox.global || impress.DEFAULT_SANDBOX_MODULES;
for (var i = 0, len = globals.length; i < len; i++) {
moduleName = globals[i];
if (moduleName === 'require') module = require;
else module = global[moduleName];
if (module) application.sandbox[moduleName] = module;
}
var apis = application.config.sandbox.api || [];
for (var j = 0, jlen = apis.length; j < jlen; j++) {
moduleName = apis[j];
if (impress.API_ALIASES[moduleName]) moduleAlias = impress.API_ALIASES[moduleName];
else {
moduleAlias = moduleName;
moduleName = api.impress.spinalToCamel(moduleAlias);
}
module = api[moduleName];
if (!module) module = impress.require(moduleAlias);
if (module) application.sandbox.api[moduleName] = module;
}
}
callback();
});
};
// Call given function in application sandbox context
// Context will be single parameter of the call
//
application.callInContext = function(fn) {
application.sandbox.__callInContext = fn;
api.impress.callInContextScript.runInNewContext(application.sandbox);
delete application.sandbox.__callInContext;
};
// Preload all handlers in directory
// relPath - relative path from /server
// depth - recursion depth, 0 - maximum, 1 - one level (no recursion), etc.
// callback(err, directories) - preload finish
//
application.preloadDirectory = function(relPath, depth, callback) {
if (!callback) callback = api.impress.emptyness;
if (depth === undefined) depth = 0;
var absPath = application.dir + '/server' + relPath;
api.fs.readdir(absPath, function(err, files) {
var directories = [];
if (err) {
application.log.error(impress.CANT_READ_DIR + absPath);
callback(err);
} else if (files.length > 0) {
api.async.each(files, function(fileName, cb) {
var fileExt = api.impress.fileExt(fileName),
filePath = api.impress.addTrailingSlash(absPath) + fileName;
api.fs.stat(filePath, function(err, stats) {
if (!err) {
if (stats.isDirectory() && (depth === 0 || depth > 1)) {
directories.push(fileName);
application.preloadDirectory(
api.impress.addTrailingSlash(relPath) + fileName, depth - 1, cb
);
} else if (fileExt === 'js') application.createScript(filePath, cb);
else cb();
} else cb();
});
}, function() {
callback(null, directories);
});
application.watchCache(relPath);
} else callback(null, directories);
});
};
// Load configuration files
//
application.loadConfig = function(callback) {
api.fs.readdir(application.dir + '/config', function(err, files) {
if (err) {
application.log.error(impress.CANT_READ_DIR + application.dir + '/config');
callback();
} else {
files.sort(api.impress.sortCompareConfig);
api.async.filterSeries(files, function(file, cb) {
var fileExt = api.path.extname(file),
fileName = api.path.basename(file, fileExt);
if (!impress.mode) cb(!api.impress.contains(fileName, '.'));
else {
var modeName = api.path.extname(fileName);
cb(
!api.impress.inArray(files, fileName + '.' + impress.mode + fileExt) &&
(modeName === '' || modeName === '.' + impress.mode)
);
}
}, function(files) {
api.async.eachSeries(files, application.loadConfigFile, function() {
callback();
});
});
}
});
application.watchCache('/config');
};
// Load single configuration file
//
application.loadConfigFile = function(file, callback) {
var configFile = application.dir + '/config/' + file,
configDefinition = (application === impress) ? impress.serverConfigDefinition : impress.applicationConfigDefinition,
sectionName, validationResult,
fileExt = api.path.extname(file),
fileName = api.path.basename(file, fileExt);
if (impress.mode) sectionName = api.path.basename(fileName, '.' + impress.mode);
else sectionName = fileName;
if (fileExt === '.js') {
if (!application.config[sectionName]) {
application.createScript(configFile, function(err, exports) {
application.config[sectionName] = exports;
// Validate configuration
if (configDefinition[sectionName]) {
validationResult = api.definition.validate(exports, configDefinition, sectionName, true);
if (api.cluster.isMaster) api.definition.printErrors(
'Error(s) in configuration found:\n'.red.bold +
'Application: ' + application.name.yellow.bold + ' Config file: ' + (sectionName + '.js').yellow.bold,
validationResult
);
}
if (sectionName === 'databases' && global.db) db.openApplicationDatabases(application, callback);
else callback();
});
} else callback();
} else if (fileExt === '.json') {
application.config[sectionName] = require(configFile);
callback();
} else callback();
};
// Preprocess application configuration
//
application.preprocessConfig = function() {
var config = application.config;
if (config.hosts) application.hostsRx = api.impress.arrayRegExp(config.hosts);
else if (application.log && application !== impress) {
application.log.error('Configuration error: empty or wrong hosts.js');
}
if (!application.isInitialized && application !== impress) {
application.isInitialized = true;
// Prepare plugins
if (config.sandbox && config.sandbox.plugins) {
var pluginName, plugin;
for (var i = 0, ilen = config.sandbox.plugins.length; i < ilen; i++) {
pluginName = config.sandbox.plugins[i];
plugin = api.impress.getByPath(global, pluginName);
if (plugin && plugin !== impress && plugin.mixinApplication) {
plugin.mixinApplication(application);
}
}
}
// Prepare application routes
if (config.routes) {
var route, rx, routes = config.routes;
for (var j = 0, jlen = routes.length; j < jlen; j++) {
route = routes[j];
if (route.escaping === false) rx = route.url;
else rx = '^' + route.url.replace(/(\/|\?|\.)/g, '\\$1').replace(/\(\\\.\*\)/, '(.*)') + '$';
route.urlRx = new RegExp(rx);
}
}
}
};
// Load placs
//
application.loadPlaces = function(callback) {
api.async.each(impress.applicationPlaces, function(placeName, cb) {
application.loadPlaceScripts(placeName, cb);
}, callback);
application.watchCache('/model');
};
// Load single place scripts
//
application.loadPlaceScripts = function(placeName, callback) {
var placeDir = application.dir + '/' + placeName;
api.fs.exists(placeDir, function(exists) {
if (exists) {
api.fs.readdir(placeDir, function(err, files) {
if (err) {
application.log.error(impress.CANT_READ_DIR + application.dir + '/config');
callback();
} else {
api.async.each(files, function(file, cb) {
if (api.impress.endsWith(file, '.js')) {
if (placeName === 'setup') {
var sectionName = api.path.basename(file, '.js');
if (api.impress.inArray(files, sectionName + '.done')) return cb();
}
application.loadPlaceFile(placeName, placeDir, file, cb);
} else cb();
}, function(/*err*/) {
callback();
});
}
});
if (placeName !== 'setup') application.watchCache('/' + placeName);
} else callback();
});
};
// Load place file
// placeName - place name (subdirectory name in application base directory)
// placeDir - directory path
// file - file name in place directory
// callback - call after file loaded
//
application.loadPlaceFile = function(placeName, placeDir, file, callback) {
var sectionName = api.path.basename(file, '.js');
application.createScript(placeDir + '/' + file, function(err, exports) {
if (placeName === 'tasks') application.setTask(sectionName, exports);
else if (placeName === 'model') application.model[sectionName] = exports;
else if (placeName === 'setup') api.fs.writeFile(placeDir + '/' + sectionName + '.done', new Date().toISOString());
callback();
});
};
// Start or restart application tasks
//
application.setTask = function(taskName, exports) {
application.stopTask(taskName);
application.tasks[taskName] = exports;
var task = application.tasks[taskName];
if (task) {
task.name = taskName;
task.success = null;
task.error = null;
task.lastStart = null;
task.lastEnd = null;
task.executing = false;
task.active = false;
task.count = 0;
application.startTask(taskName);
}
};
// Start task
// taskName - task name in application.tasks hash
//
application.startTask = function(taskName) {
var task = application.tasks[taskName];
if (task && !task.active) {
if ((api.cluster.isMaster && task.place === 'master') || (api.cluster.isWorker && task.place === 'worker')) {
task.active = true;
task.interval = api.impress.duration(task.interval);
task.timer = setInterval(function() {
if (!task.executing) {
task.lastStart = Date.now();
task.executing = true;
task.run(task, function(taskResult) {
task.error = taskResult;
task.success = taskResult === null;
task.lastEnd = Date.now();
task.executing = false;
task.count++;
});
}
}, task.interval);
}
}
};
// Stop task
// taskName - task name in application.tasks hash
//
application.stopTask = function(taskName) {
var task = application.tasks[taskName];
if (task && task.timer) clearInterval(task.timer);
delete application.tasks[taskName];
};
// Stop application tasks
//
application.stopTasks = function() {
var tasks = application.tasks,
taskNames = Object.keys(tasks);
taskNames.map(application.stopTask);
};
// Dispatch requests
// req - request is an instance of http.IncomingMessage
// res - rsponse is an instance of http.ServerResponse
//
application.dispatch = function(req, res) {
var route, match, form,
client = new application.Client(application, req, res);
if (application.config.application.slowTime) client.slowTime = application.config.application.slowTime;
if (application.config.routes) {
for (var iRoute = 0, len = application.config.routes.length; iRoute < len; iRoute++) {
route = application.config.routes[iRoute];
match = req.url.match(route.urlRx);
if (match) return application.dispatchRoute(client, route, match, req, res, iRoute);
}
}
client.static(function() {
if (impress.HTTP_VEBS.indexOf(client.method) > 0) { // get, post, put, delete
var contentType = req.headers['content-type'];
if (contentType && api.impress.startsWith(contentType, 'multipart')) {
form = new api.multiparty.Form();
form.parse(req, function(err, fields, files) {
if (err) client.error(400);
else {
client.files = files;
client.fields = fields;
api.impress.extend(client.parameters, client.fields);
client.dispatch();
}
});
} else {
req.on('data', function(chunk) {
client.chunks.push(chunk);
});
req.on('end', function() {
client.data = Buffer.concat(client.chunks).toString();
if (contentType && api.impress.startsWith(contentType, 'application/json')) {
client.fields = JSON.parse(client.data);
} else client.fields = api.querystring.parse(client.data);
api.impress.extend(client.parameters, client.fields);
client.dispatch();
});
}
} else client.dispatch();
});
};
// Dispatch route
// client - instance of Client
// route - route name
// match - Array of URL elements mached route.urlRx
// req - request is an instance of http.IncomingMessage
// res - rsponse is an instance of http.ServerResponse
// iRoute - route index in application.config.routes
//
application.dispatchRoute = function(client, route, match, req, res, iRoute) {
if (route.slowTime) client.slowTime = route.slowTime;
var urlRoute = req.url;
if (route.rewrite && match.length > 1) {
urlRoute = route.rewrite.replace(/\[([0-9]+)\]/g, function(s, key) {
return match[key] || '';
});
} else urlRoute = route.rewrite;
req.usedRoutes = req.usedRoutes || [];
if (route.host) client.proxy(route.host, route.port || 80, urlRoute);
else if (api.impress.inArray(req.usedRoutes, iRoute)) client.error(508);
else {
req.url = urlRoute;
req.usedRoutes.push(iRoute);
impress.dispatcher(req, res);
}
};
// Create or clear application cache
//
application.clearCache = function() {
application.config = {};
if (application.cache) {
var watcher;
if (application.cache.timer) clearTimeout(application.cache.timer);
for (var watcherPath in application.cache.watchers) {
watcher = application.cache.watchers[watcherPath];
watcher.close();
}
}
application.cache = {
templates: [], // template body cache indexed by file name
files: [], // file override/inherited cache indexed by file name
folders: [], // folder existence cache indexed by folder name
scripts: [], // compiled vm scripts
watchers: [], // directory watchers indexed by directory name
timer: null, // timer to consolidate watch changes
updates: [], // array of changes to update on next application.cache.timer event
static: [], // static files cache
pages: [], // rendered pages cache
size: 0 // cache size
};
};
// Purge application cache
//
application.purgeCache = function() {
if (application.cache.size > application.config.files.cacheSize) {
for (var name in application.cache.static) {
if (application.cache.static[name].data) {
application.cache.size -= application.cache.static[name].data.length;
delete application.cache.static[name];
if (application.cache.size < application.config.files.cacheSize) return;
}
}
}
};
// Refresh static in memory cache with compression and minification
// filePath - compressing file path
// stats - instance of fs.Stats
// callback - function(err, data, compressed)
//
application.compress = function(filePath, stats, callback) {
api.fs.readFile(filePath, function(error, data) {
if (error) {
if (callback) callback(error);
} else {
var relPath = application.relative(filePath),
ext = api.impress.fileExt(filePath),
cfg = api.impress.inArray(application.config.files.preprocess, ext);
if (cfg) {
var pre = impress.preprocess[ext];
if (pre) {
data = pre(data);
if (data) stats.size = data.length;
else {
if (callback) callback(new Error(ext + ' parse/preprocess error'));
application.cache.static[relPath] = impress.FILE_PARSE_ERROR;
application.watchCache(api.path.dirname(relPath));
return;
}
}
}
if (
application.config.files.gzip &&
!api.impress.inArray(impress.COMPRESSED_EXT, ext) &&
stats.size > impress.COMPRESS_ABOVE
) {
api.zlib.gzip(data, function(err, data) {
if (!err) stats.size = data.length;
if (callback) callback(err, data, true);
if (!err) application.addToCache(relPath, stats, true, data);
});
} else {
if (callback) callback(null, data, false);
application.addToCache(relPath, stats, false, data);
}
application.watchCache(api.path.dirname(relPath));
}
});
};
// Add static to cache
// relPath - file path relative to application base directory
// stats - instance of fs.Stats
// compressed - compression boolean flag
// data - buffer
//
application.addToCache = function(relPath, stats, compressed, data) {
if (data) {
application.cache.static[relPath] = { data: data, stats: stats, compressed: compressed };
application.cache.size += data.length;
}
application.purgeCache();
};
// Shutdown application
//
application.shutdownLongWorkers = function() {
for (var workerId in application.longWorkers) {
application.longWorkers[workerId].kill();
}
};
// Programmatically create handler
// method - http verb (get, post...)
// path - path for handler
// handler - impress 2 parameter functon or 3 parameter middleware
// meta - metadata to be set as handler.handler (optional)
//
application.handler = function(method, path, handler, meta) {
var dirPath = application.dir + '/server' + api.impress.addTrailingSlash(path),
filePath = dirPath + method + '.js',
relPath = application.relative(filePath);
if (meta) handler.meta = meta;
application.cache.scripts[relPath] = handler;
application.cache.files[relPath] = impress.FILE_EXISTS;
application.cache.folders[application.relative(relPath)] = impress.DIR_EXISTS;
};
// Programmatically create handlers for http verbs
// application.get(path, handler, meta)
// application.post(path, handler, meta)
// application.put(path, handler, meta)
// application.delete(path, handler, meta)
//
impress.HTTP_VEBS.forEach(function(verb) {
application[verb] = function(path, handler, meta) {
application.handler(verb, path, handler, meta);
};
});
// Update changed file in cache
// filePath - file path relative to application base directory
// stats - instance of fs.Stats
//
application.updateFileCache = function(filePath, stats) {
api.fs.exists(filePath, function(exists) {
if (!exists) application.cliearDirectoryCache(filePath);
else {
var ext = api.impress.fileExt(filePath),
relPath = application.relative(filePath);
api.impress.clearCacheStartingWith(application.cache.pages, relPath);
if (relPath in application.cache.static) {
// Replace static files memory cache
application.compress(filePath, stats);
} else if (ext === 'js' && (relPath in application.cache.scripts)) {
// Replace changed js file in cache
application.cache.scripts[relPath] = null;
application.createScript(filePath, function(err, exports) {
application.cache.scripts[relPath] = exports;
var sectionName = api.path.basename(filePath, '.js');
if (api.impress.startsWith(filePath, application.dir + '/model')) {
//
} else if (api.impress.startsWith(filePath, application.dir + '/config')) {
// Reload config
application.config[sectionName] = exports;
application.preprocessConfig();
} else if (api.impress.startsWith(filePath, application.dir + '/tasks')) {
// Reload task
application.setTask(sectionName, exports);
}
});
} else if (ext === 'template') {
// Replace changed template file in cache
delete application.cache.templates[relPath];
delete application.cache.files[relPath];
api.fs.readFile(filePath, 'utf8', function(err, tpl) {
if (!err) {
if (!tpl) tpl = impress.FILE_IS_EMPTY;
else tpl = api.impress.removeBOM(tpl);
application.cache.templates[relPath] = tpl;
}
});
}
}
});
};
// Clear cache for all changed folders (created or deleted files)
// filePath - file path relative to application base directory
//
application.cliearDirectoryCache = function(filePath) {
var relPath = application.relative(filePath);
api.impress.clearCacheStartingWith(application.cache.static, relPath);
api.impress.clearCacheStartingWith(application.cache.folders, relPath);
api.impress.clearCacheStartingWith(application.cache.pages, relPath);
api.impress.clearCacheStartingWith(application.cache.files, relPath, function(used) {
var ext = api.impress.fileExt(used);
if (ext === 'js' && (used in application.cache.scripts)) {
delete application.cache.scripts[used];
} else if (ext === 'template' && (used in application.cache.templates)) {
delete application.cache.templates[used];
}
});
};
// Update watched cache: process application.cache.updates array
//
application.updateCache = function() {
var updates = application.cache.updates,
files = {};
api.async.each(updates, function(relPath, cb) {
var filePath = application.dir + relPath;
api.fs.exists(filePath, function(exists) {
if (exists) {
api.fs.stat(filePath, function(err, stats) {
if (err) return cb();
if (stats.isFile()) files[filePath] = stats;
else {
// Refresh all cached files in directory
for (var key in application.cache.files) {
if (key !== filePath && api.impress.startsWith(key, filePath) && !files[key]) {
files[key] = stats;
}
}
}
cb();
});
} else {
// Remove from cache
application.cliearDirectoryCache(filePath);
cb();
}
});
}, function() {
var stats;
for (var filePath in files) {
stats = files[filePath];
application.updateFileCache(filePath, stats);
}
});
application.cache.timer = null;
application.cache.updates = [];
};
// Cache watchers
// filePath - absolute path to file or directory to watch
//
application.watchCache = function(relPath) {
var filePath = application.dir + relPath,
watchInterval = api.impress.getByPath(impress.config, 'scale.watchInterval') || 2000,
watcher, path = filePath;
if (application) {
watcher = application.cache.watchers[relPath];
if (!watcher) {
api.fs.exists(path, function(exists) {
if (exists) {
watcher = api.fs.watch(path, function(event, fileName) {
var filePath, relPath, watcher;
if (fileName) filePath = path + '/' + fileName;
else filePath = path;
relPath = application.relative(filePath);
watcher = application.cache.watchers[relPath];
if (application.cache.timer) clearTimeout(application.cache.timer);
if (application.cache.updates.indexOf(relPath) === -1) {
application.cache.updates.push(relPath);
}
application.cache.timer = setTimeout(application.updateCache, watchInterval);
});
watcher.on('error', function() {
watcher.close();
});
application.cache.watchers[relPath] = watcher;
}
});
}
}
};
application.clearCache();
};