-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (33 loc) · 937 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
const crypto = require('crypto');
module.exports = function(req, res, next){
var hash = crypto.createHash('sha1');
var write = res.write, end = res.end;
/**
* [function description]
* @param {[type]} chunk [description]
* @param {[type]} encoding [description]
* @return {[type]} [description]
*/
res.write = function(chunk, encoding){
hash.update(chunk, encoding);
write.apply(res, arguments);
};
/**
* [function description]
* @param {[type]} chunk [description]
* @param {[type]} encoding [description]
* @return {[type]} [description]
*/
res.end = function(chunk, encoding){
if(chunk) hash.update(chunk, encoding);
var str = hash.digest('hex');
if(str == req.headers[ 'if-none-match' ]){
res.writeHead(304);
end.call(res);
return res;
}
res.setHeader('ETag', str);
end.apply(res, arguments);
};
next();
};