2017-08-22 12:59:01 +03:00
|
|
|
var semver = require('semver'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../../lib/common');
|
2016-06-09 18:14:36 +03:00
|
|
|
|
|
|
|
function checkVersionMatch(req, res, next) {
|
2017-08-22 12:59:01 +03:00
|
|
|
var clientVersion = req.get('X-Ghost-Version'),
|
2018-01-03 22:20:18 +03:00
|
|
|
// can contain pre-release suffix, you should be able to use e.g. 1.19.0-pre [server] with 1.18.0 [client]
|
|
|
|
serverVersion = res.locals.version.match(/^(\d+\.)?(\d+\.)?(\d+)/)[0],
|
2017-08-22 12:59:01 +03:00
|
|
|
constraint = '^' + clientVersion + '.0';
|
2016-06-09 18:14:36 +03:00
|
|
|
|
2017-08-22 12:59:01 +03:00
|
|
|
// 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)) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return next(new common.errors.VersionMismatchError({
|
|
|
|
message: common.i18n.t('errors.middleware.api.versionMismatch', {
|
|
|
|
clientVersion: clientVersion,
|
|
|
|
serverVersion: serverVersion
|
|
|
|
})
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2016-06-09 18:14:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = checkVersionMatch;
|