This repository has been archived by the owner on Sep 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.js
67 lines (64 loc) · 1.68 KB
/
resources.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
var fs = require('fs');
var resourcefiles = {
'_lucos': { filename: __dirname+"/lucos.js", type: 'js'}
};
/**
* Adds a resource file to the list
*
* @param String key - A unqiue key to identify the file on the client
* @param String type - tells the client how to parse the file
* @param String filename - Where in the filesystem the file is stored
*/
exports.add = function (key, type, filename) {
resourcefiles[key] = {filename: filename, type: type};
}
/**
* Loads the relevant files
*
* @param ServerResponse res
* @param String version - The version sent to the client the last time it requested resources
*/
exports.load = function (res, version) {
var ii;
var output = {};
var filenum = 0;
var totalfiles = 0;
var vtime = new Date(parseInt(version)*1000);
var newvtime = vtime;
var resources = {};
var isReady = function _isReady() {
filenum++;
if (filenum < totalfiles) return;
output.v = newvtime.getTime() / 1000;
output.r = resources;
res.writeHead(200, {'Content-Type': "application/json"});
res.write(JSON.stringify(output));
res.end();
}
for (ii in resourcefiles) {
(function _addFileToOutput(key, filename, type) {
resources[key] = type;
fs.stat(filename, function _gotstat(err, stats) {
if (err) {
isReady();
return;
}
var mtime = new Date(stats.mtime);
if (mtime > vtime) {
if (mtime > newvtime) newvtime = mtime;
fs.readFile(filename, function _gotfile(err, data) {
if (err) {
isReady();
return;
}
output[key] = data.toString();
isReady();
});
} else {
isReady();
}
});
})(ii, resourcefiles[ii].filename, resourcefiles[ii].type);
totalfiles++;
}
}