1
1
import * as fs from 'fs' ;
2
2
import * as path from 'path' ;
3
3
import { promisify } from 'util' ;
4
-
5
4
import { Mutex } from 'async-mutex' ;
6
5
7
6
import * as getBrowserLauncherCb from '@james-proxy/james-browser-launcher' ;
8
7
import { LaunchOptions , BrowserInstance } from '@james-proxy/james-browser-launcher' ;
9
8
9
+ import { reportError } from './error-tracking' ;
10
+
11
+ const statFile = promisify ( fs . stat ) ;
10
12
const readFile = promisify ( fs . readFile ) ;
11
13
const deleteFile = promisify ( fs . unlink ) ;
12
14
const getBrowserLauncher = promisify ( getBrowserLauncherCb ) ;
@@ -15,21 +17,38 @@ const browserConfigPath = (configPath: string) => path.join(configPath, 'browser
15
17
16
18
export { BrowserInstance } ;
17
19
20
+ const updateBrowserConfig = promisify ( getBrowserLauncherCb . update ) ;
21
+
18
22
export async function checkBrowserConfig ( configPath : string ) {
19
23
// It's not clear why, but sometimes the browser config can become corrupted, so it's not valid JSON
20
24
// If that happens JBL doesn't catch it, so we crash. To avoid that, we check it here on startup.
21
25
22
26
const browserConfig = browserConfigPath ( configPath ) ;
23
- return readFile ( browserConfig , 'utf8' )
24
- . then ( ( contents ) => JSON . parse ( contents ) )
27
+ return Promise . all ( [
28
+ // Check the file is readable and parseable
29
+ readFile ( browserConfig , 'utf8' )
30
+ . then ( ( contents ) => JSON . parse ( contents ) ) ,
31
+
32
+ // Check the file is relatively recent
33
+ statFile ( browserConfig )
34
+ . then ( ( stats ) => {
35
+ if ( Date . now ( ) - stats . mtime . valueOf ( ) > 1000 * 60 * 60 * 24 ) {
36
+ return deleteFile ( browserConfig ) . catch ( ( err ) => {
37
+ console . error ( 'Failed to clear outdated config file' ) ;
38
+ reportError ( err ) ;
39
+ } ) ;
40
+ } ;
41
+ } )
42
+ ] )
25
43
. catch ( ( error ) => {
26
44
if ( error . code === 'ENOENT' ) return ;
27
45
28
46
console . warn ( 'Failed to read browser config on startup' , error ) ;
29
47
return deleteFile ( browserConfig ) . catch ( ( err ) => {
30
48
console . error ( 'Failed to clear broken config file:' , err ) ;
49
+ reportError ( err ) ;
31
50
} ) ;
32
- } ) ;
51
+ } )
33
52
}
34
53
35
54
// It's not safe to call getBrowserLauncher in parallel, as config files can
0 commit comments