Ghost/core/server/middleware/api/version-match.js
Hannah Wolfe b1cfa6e342 Improved version match logic (#8922)
closes #8821

- Use semver to do constraint matching
- Use client to generate a caret constraint
- E.g. if the client is 1.1, then the constraint ^1.1.0 will match >=1.1.0 <2.0.0
- Updated tests
2017-08-22 10:59:01 +01:00

24 lines
818 B
JavaScript

var semver = require('semver'),
errors = require('../../errors'),
i18n = require('../../i18n');
function checkVersionMatch(req, res, next) {
var clientVersion = req.get('X-Ghost-Version'),
serverVersion = res.locals.version,
constraint = '^' + clientVersion + '.0';
// no error when client is on an earlier minor version than server
// error when client is on a later minor version than server
// always error when the major version is different
if (clientVersion && !semver.satisfies(serverVersion, constraint)) {
return next(new errors.VersionMismatchError({
message: i18n.t('errors.middleware.api.versionMismatch', {clientVersion: clientVersion, serverVersion: serverVersion})
}));
}
next();
}
module.exports = checkVersionMatch;