@@ -14,9 +14,13 @@ const CHECK_TIMEOUT = 1000 * 60 * 60 * 24 // 1 day
1414
1515const rp = promisify ( request )
1616
17- exports . versionCheckMiddleware = async function ( req , res , next ) {
17+ exports . checkVersion = checkVersion
18+ /**
19+ * @param {Express.Application|Express.Request } ctx
20+ */
21+ async function checkVersion ( ctx ) {
1822 if ( lastCheckAt && ( lastCheckAt + CHECK_TIMEOUT > Date . now ( ) ) ) {
19- return next ( )
23+ return
2024 }
2125
2226 // update lastCheckAt whether the check would fail or not
@@ -31,17 +35,36 @@ exports.versionCheckMiddleware = async function (req, res, next) {
3135
3236 if ( statusCode !== 200 || data . status === 'error' ) {
3337 logger . error ( 'Version check failed.' )
34- return next ( )
38+ return
3539 }
3640
37- req . app . locals . versionInfo . latest = data . latest
38- req . app . locals . versionInfo . versionItem = req . app . locals . latest ? null : data . versionItem
41+ let locals = ctx . locals ? ctx . locals : ctx . app . locals
3942
40- return next ( )
43+ locals . versionInfo . latest = data . latest
44+ locals . versionInfo . versionItem = data . latest ? null : data . versionItem
45+
46+ if ( ! data . latest ) {
47+ const { version, link } = data . versionItem
48+
49+ logger . warn ( `Your CodiMD version is out of date! The latest version is ${ version } . Please see what's new on ${ link } .` )
50+ }
51+
52+ return
4153 } catch ( err ) {
4254 // ignore and skip version check
4355 logger . error ( 'Version check failed.' )
4456 logger . error ( err )
45- return next ( )
57+
58+ return
4659 }
4760}
61+
62+ exports . versionCheckMiddleware = function ( req , res , next ) {
63+ checkVersion ( req )
64+ . then ( ( ) => {
65+ next ( )
66+ } )
67+ . catch ( ( err ) => {
68+ next ( )
69+ } )
70+ }
0 commit comments