* @param {Express.Application|Express.Request} ctx
(ctx)
| 19 | * @param {Express.Application|Express.Request} ctx |
| 20 | */ |
| 21 | async function checkVersion (ctx) { |
| 22 | if (lastCheckAt && (lastCheckAt + CHECK_TIMEOUT > Date.now())) { |
| 23 | return |
| 24 | } |
| 25 | |
| 26 | // update lastCheckAt whether the check would fail or not |
| 27 | lastCheckAt = Date.now() |
| 28 | |
| 29 | try { |
| 30 | const { statusCode, body: data } = await rp({ |
| 31 | url: `${VERSION_CHECK_ENDPOINT}?v=${config.version}`, |
| 32 | method: 'GET', |
| 33 | json: true, |
| 34 | timeout: 3000 |
| 35 | }) |
| 36 | |
| 37 | if (statusCode !== 200 || data.status === 'error') { |
| 38 | logger.warn('Version check failed.') |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | const locals = ctx.locals ? ctx.locals : ctx.app.locals |
| 43 | |
| 44 | locals.versionInfo.latest = data.latest |
| 45 | locals.versionInfo.versionItem = data.latest ? null : data.versionItem |
| 46 | |
| 47 | if (!data.latest) { |
| 48 | const { version, link } = data.versionItem |
| 49 | |
| 50 | logger.info(`Your CodiMD version is out of date! The latest version is ${version}. Please see what's new on ${link}.`) |
| 51 | } |
| 52 | } catch (err) { |
| 53 | // ignore and skip version check |
| 54 | logger.warn('Version check failed.') |
| 55 | logger.warn(err) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | exports.versionCheckMiddleware = function (req, res, next) { |
| 60 | checkVersion(req) |
no outgoing calls
no test coverage detected