-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
46 lines (37 loc) · 996 Bytes
/
index.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
var path = require('path');
var fs;
try {
fs = require('graceful-fs');
} catch (err) {
fs = require('fs');
}
module.exports = read;
function read(filename, encoding, done) {
filename = path.resolve(filename);
if (typeof encoding === 'function' || !Buffer.isEncoding(encoding)) {
done = encoding;
encoding = 'utf8';
}
fs.stat(filename, function (err, stats) {
if (err) return done(err);
var cache = read.cache[filename];
// only return the cached value if the mtime is exactly the same
if (cache && cache.mtime.getTime() === stats.mtime.getTime())
return done(null, cache.value);
fs.readFile(filename, {'encoding':encoding}, function (err, body) {
if (err) return done(err);
cache = read.cache[filename] = {
mtime: stats.mtime,
value: body
};
done(null, body);
});
});
return function (fn) {
done = fn;
};
}
read.clear = function () {
read.cache = Object.create(null);
};
read.clear();