mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-16 21:56:25 -05:00
add md5 etags for json
This commit is contained in:
parent
27844cd358
commit
86f02bda7d
2 changed files with 30 additions and 0 deletions
|
@ -7,6 +7,7 @@ var UError = require('./error').UserError;
|
||||||
var basic_auth = require('./middleware').basic_auth;
|
var basic_auth = require('./middleware').basic_auth;
|
||||||
var validate_name = require('./middleware').validate_name;
|
var validate_name = require('./middleware').validate_name;
|
||||||
var media = require('./middleware').media;
|
var media = require('./middleware').media;
|
||||||
|
var etagify = require('./middleware').etagify;
|
||||||
var expect_json = require('./middleware').expect_json;
|
var expect_json = require('./middleware').expect_json;
|
||||||
|
|
||||||
module.exports = function(config_hash) {
|
module.exports = function(config_hash) {
|
||||||
|
@ -28,6 +29,7 @@ module.exports = function(config_hash) {
|
||||||
|
|
||||||
var app = express();
|
var app = express();
|
||||||
app.use(express.logger());
|
app.use(express.logger());
|
||||||
|
app.use(etagify);
|
||||||
app.use(basic_auth(function(user, pass) {
|
app.use(basic_auth(function(user, pass) {
|
||||||
return config.authenticate(user, pass);
|
return config.authenticate(user, pass);
|
||||||
}));
|
}));
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
var crypto = require('crypto');
|
||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
var UError = require('./error').UserError;
|
var UError = require('./error').UserError;
|
||||||
|
|
||||||
|
@ -77,3 +78,30 @@ module.exports.basic_auth = function basic_auth(callback) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// express doesn't do etags with requests <= 1024b
|
||||||
|
// we use md5 here, it works well on 1k+ bytes, but sucks with fewer data
|
||||||
|
// could improve performance using crc32 after benchmarks
|
||||||
|
function md5sum(data) {
|
||||||
|
return crypto.createHash('md5').update(data).digest('hex');
|
||||||
|
}
|
||||||
|
|
||||||
|
// using it for json only right now
|
||||||
|
module.exports.etagify = function(req, res, next) {
|
||||||
|
var _send = res.send;
|
||||||
|
res.send = function(body) {
|
||||||
|
if (typeof(body) === 'string' || typeof(body) === 'object') {
|
||||||
|
res.header('Content-type', 'application/json');
|
||||||
|
|
||||||
|
if (typeof(body) === 'object') {
|
||||||
|
body = JSON.stringify(body, undefined, '\t');
|
||||||
|
}
|
||||||
|
|
||||||
|
res.header('ETag', '"' + md5sum(body) + '"');
|
||||||
|
} else {
|
||||||
|
// send(null), send(204), etc.
|
||||||
|
}
|
||||||
|
_send.call(res, body);
|
||||||
|
};
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue