2018-09-20 16:03:33 +03:00
|
|
|
const semver = require('semver');
|
2020-04-09 21:40:00 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-05-03 19:29:44 +03:00
|
|
|
const i18n = require('../../../../shared/i18n');
|
2016-06-09 18:14:36 +03:00
|
|
|
|
|
|
|
function checkVersionMatch(req, res, next) {
|
2018-09-20 16:03:33 +03:00
|
|
|
const clientVersion = req.get('X-Ghost-Version');
|
|
|
|
// can contain pre-release suffix, you should be able to use e.g. 1.19.0-pre [server] with 1.18.0 [client]
|
|
|
|
const serverVersion = res.locals.version.match(/^(\d+\.)?(\d+\.)?(\d+)/)[0];
|
|
|
|
const 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)) {
|
2020-04-09 21:40:00 +03:00
|
|
|
return next(new errors.VersionMismatchError({
|
|
|
|
message: i18n.t('errors.middleware.api.versionMismatch', {
|
2017-12-12 00:47:46 +03:00
|
|
|
clientVersion: clientVersion,
|
|
|
|
serverVersion: serverVersion
|
|
|
|
})
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2016-06-09 18:14:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = checkVersionMatch;
|