-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.cache.stat.js
51 lines (48 loc) · 2.37 KB
/
server.cache.stat.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
var fs = require('fs');
fs.statCacheExists = false;
fs.statCache = [];
fs.statExceptionCache = [];
// plug in our own version of lstatSync that caches the results
var fslstatSync = fs.lstatSync;
fs.lstatSync = function(path) {
try {
var stats = fslstatSync(path);
if (!fs.statCache[path]) {
fs.statCache[path] = 'new fs.Stats(' + stats.dev + ',' + stats.mode + ',' + stats.nlink + ',' + stats.uid + ',' + stats.gid + ',' + stats.rdev + ',' + stats.blksize + ',' + stats.ino + ',' + stats.size + ',' + stats.blocks + ',' + stats.atime.getTime() + ',' + stats.mtime.getTime() + ',' + stats.ctime.getTime() + ',' + stats.birthtime.getTime() + ')';
}
return stats;
} catch(ex) {
if (!fs.statExceptionCache[path]) {
fs.statExceptionCache[path] = '{Error:new Error(\'' + convertStringToCode(ex.message) + '\'),errno:' + ex.errno + ',code:\'' + convertStringToCode(ex.code) + '\',syscall:\'' + convertStringToCode(ex.syscall) + '\',path:\'' + convertStringToCode(ex.path) + '\'}';
}
throw ex;
}
}
// plug in our own version of statSync that caches the results
var fsstatSync = fs.statSync;
fs.statSync = function(path) {
try {
var stats = fsstatSync(path);
if (!fs.statCache[path]) {
fs.statCache[path] = 'new fs.Stats(' + stats.dev + ',' + stats.mode + ',' + stats.nlink + ',' + stats.uid + ',' + stats.gid + ',' + stats.rdev + ',' + stats.blksize + ',' + stats.ino + ',' + stats.size + ',' + stats.blocks + ',' + stats.atime.getTime() + ',' + stats.mtime.getTime() + ',' + stats.ctime.getTime() + ',' + stats.birthtime.getTime() + ')';
}
return stats;
} catch(ex) {
if (!fs.statExceptionCache[path]) {
fs.statExceptionCache[path] = '{Error:new Error(\'' + convertStringToCode(ex.message) + '\'),errno:' + ex.errno + ',code:\'' + convertStringToCode(ex.code) + '\',syscall:\'' + convertStringToCode(ex.syscall) + '\',path:\'' + convertStringToCode(ex.path) + '\'}';
}
throw ex;
}
}
// converts a string into an escaped javascript string
function convertStringToCode(content) {
var strReplaceAll = require('str-replace-all');
content = strReplaceAll('\\', '\\\\', content);
content = strReplaceAll('\f', '\\f', content);
content = strReplaceAll('\n', '\\n', content);
content = strReplaceAll('\r', '\\r', content);
content = strReplaceAll('\t', '\\t', content);
content = strReplaceAll('\v', '\\v', content);
content = strReplaceAll('\'', '\\\'', content);
return content;
};