-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
Closed
Labels
Description
Current express calculates etag via utils.etag(), which is a private function, and could be overrided by:
res.set('etag', 'etag value')beforeres.send(), or- override by wrapping
res.send()by settingres.set('etag', 'etag value')before forward arguments to the oldres.send().
These two method works, but all come with catches:
- require to call
res.set()explicitly in every route, which is very inconvenient; - new
res.send()must handle all supported method signatures, to generateetagcorrectly based on modifiedbody. Which comes with performance penalty and may lead to forward compatibility issue.
So a more clean solution is to expose etag on application and response level, which allows override by doing:
app.etag = function(buf) {
return '"' + crc32c.calculate(buf) + '"';
};or
function strongEtag(buf) {
return '"' + crypto.createHash('sha1').update(buf).digest('hex') + '"';
}
function strongEtagMiddleware(req, res, next) {
res.etag = strongEtag;
next();
}
app.get('/critical', strongEtagMiddleware, function(req, res) {
res.send('critical data');
});