Ghost/core/server/web/middleware/api/version-match.js
Katharina Irrgang 341f719d92
Extended ghost version utility (#9278)
refs https://github.com/TryGhost/Ghost-Release/issues/24

- differentiate between
  1. original package.json version (can contain pre and build suffix)
  2. full package.json version X.X.X-{pre} (optional)
  3. safe package.json version X.X (major+minor)
2018-01-03 20:20:18 +01:00

27 lines
1001 B
JavaScript

var semver = require('semver'),
common = require('../../../lib/common');
function checkVersionMatch(req, res, next) {
var 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]
serverVersion = res.locals.version.match(/^(\d+\.)?(\d+\.)?(\d+)/)[0],
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 common.errors.VersionMismatchError({
message: common.i18n.t('errors.middleware.api.versionMismatch', {
clientVersion: clientVersion,
serverVersion: serverVersion
})
}));
}
next();
}
module.exports = checkVersionMatch;