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-10-04 14:19:48 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
|
|
|
|
const messages = {
|
|
|
|
versionMismatch: 'Client request for {clientVersion} does not match server version {serverVersion}.'
|
|
|
|
};
|
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({
|
2021-10-04 14:19:48 +03:00
|
|
|
message: tpl(messages.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;
|