mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 02:11:44 +03:00
273e220327
refs 829e8ed010
- i18n is used everywhere but only requires shared or external packages, therefore it's a good candidate for living in shared
- this reduces invalid requires across frontend and server, and lets us use it everywhere until we come up with a better option
28 lines
1.0 KiB
JavaScript
28 lines
1.0 KiB
JavaScript
const semver = require('semver');
|
|
const errors = require('@tryghost/errors');
|
|
const i18n = require('../../../../shared/i18n');
|
|
|
|
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;
|