2019-12-26 08:56:31 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const { promisify } = require('util')
|
|
|
|
|
|
|
|
const request = require('request')
|
|
|
|
|
|
|
|
const logger = require('../../logger')
|
|
|
|
const config = require('../../config')
|
|
|
|
|
|
|
|
let lastCheckAt
|
|
|
|
|
|
|
|
const VERSION_CHECK_ENDPOINT = 'https://evangelion.codimd.dev/'
|
|
|
|
const CHECK_TIMEOUT = 1000 * 60 * 60 * 24 // 1 day
|
|
|
|
|
|
|
|
const rp = promisify(request)
|
|
|
|
|
|
|
|
exports.versionCheckMiddleware = async function (req, res, next) {
|
|
|
|
if (lastCheckAt && (lastCheckAt + CHECK_TIMEOUT > Date.now())) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
|
|
|
// update lastCheckAt whether the check would fail or not
|
|
|
|
lastCheckAt = Date.now()
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { statusCode, body: data } = await rp({
|
|
|
|
url: `${VERSION_CHECK_ENDPOINT}?v=${config.version}`,
|
|
|
|
method: 'GET',
|
|
|
|
json: true
|
|
|
|
})
|
|
|
|
|
|
|
|
if (statusCode !== 200 || data.status === 'error') {
|
|
|
|
logger.error('Version check failed.')
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
2019-12-27 07:55:00 +00:00
|
|
|
req.app.locals.versionInfo.latest = data.latest
|
|
|
|
req.app.locals.versionInfo.versionItem = req.app.locals.latest ? null : data.versionItem
|
2019-12-26 08:56:31 +00:00
|
|
|
|
|
|
|
return next()
|
|
|
|
} catch (err) {
|
|
|
|
// ignore and skip version check
|
|
|
|
logger.error('Version check failed.')
|
|
|
|
logger.error(err)
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
}
|