@@ -14,9 +14,13 @@ const CHECK_TIMEOUT = 1000 * 60 * 60 * 24 // 1 day
14
14
15
15
const rp = promisify ( request )
16
16
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 ) {
18
22
if ( lastCheckAt && ( lastCheckAt + CHECK_TIMEOUT > Date . now ( ) ) ) {
19
- return next ( )
23
+ return
20
24
}
21
25
22
26
// update lastCheckAt whether the check would fail or not
@@ -31,17 +35,36 @@ exports.versionCheckMiddleware = async function (req, res, next) {
31
35
32
36
if ( statusCode !== 200 || data . status === 'error' ) {
33
37
logger . error ( 'Version check failed.' )
34
- return next ( )
38
+ return
35
39
}
36
40
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
39
42
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
41
53
} catch ( err ) {
42
54
// ignore and skip version check
43
55
logger . error ( 'Version check failed.' )
44
56
logger . error ( err )
45
- return next ( )
57
+
58
+ return
46
59
}
47
60
}
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