Ghost/core/server/web/api/middleware/version-match.js
Hannah Wolfe 41c3b4e92b Moved version-match mw into api app
- Moved version-match from shared to api as it is not shared (except within the API)
- This file is only used in one part of the app, this updates the code structure to reflect this
- This is one of many similar changes needed to make it easier to refactor to the existing setup
2020-04-22 07:12:25 +01:00

28 lines
1.0 KiB
JavaScript

const semver = require('semver');
const errors = require('@tryghost/errors');
const {i18n} = require('../../../lib/common');
function checkVersionMatch(req, res, next) {
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';
// 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;