Skip to content

Commit bf5a5c2

Browse files
committed
Ensure that the detected browser config gets updated intermittently
1 parent 04f7c53 commit bf5a5c2

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

custom-typings/@james-proxy/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ declare module '@james-proxy/james-browser-launcher' {
3131
export const browsers: Launcher.Browser[];
3232
}
3333

34+
export function update(callback: (error: Error | null, config: object) => void): void;
35+
export function update(configFile: string, callback: (error: Error | null, config: object) => void): void;
36+
3437
export interface Browser {
3538
name: string;
3639
version: string;

src/browsers.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33
import { promisify } from 'util';
4-
54
import { Mutex } from 'async-mutex';
65

76
import * as getBrowserLauncherCb from '@james-proxy/james-browser-launcher';
87
import { LaunchOptions, BrowserInstance } from '@james-proxy/james-browser-launcher';
98

9+
import { reportError } from './error-tracking';
10+
11+
const statFile = promisify(fs.stat);
1012
const readFile = promisify(fs.readFile);
1113
const deleteFile = promisify(fs.unlink);
1214
const getBrowserLauncher = promisify(getBrowserLauncherCb);
@@ -15,21 +17,38 @@ const browserConfigPath = (configPath: string) => path.join(configPath, 'browser
1517

1618
export { BrowserInstance };
1719

20+
const updateBrowserConfig = promisify(getBrowserLauncherCb.update);
21+
1822
export async function checkBrowserConfig(configPath: string) {
1923
// It's not clear why, but sometimes the browser config can become corrupted, so it's not valid JSON
2024
// If that happens JBL doesn't catch it, so we crash. To avoid that, we check it here on startup.
2125

2226
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+
])
2543
.catch((error) => {
2644
if (error.code === 'ENOENT') return;
2745

2846
console.warn('Failed to read browser config on startup', error);
2947
return deleteFile(browserConfig).catch((err) => {
3048
console.error('Failed to clear broken config file:', err);
49+
reportError(err);
3150
});
32-
});
51+
})
3352
}
3453

3554
// It's not safe to call getBrowserLauncher in parallel, as config files can

src/error-tracking.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let sentryInitialized = false;
44

55
export function initErrorTracking() {
66
const packageJson = require('../package.json');
7-
7+
88
let { SENTRY_DSN } = process.env;
99
if (!SENTRY_DSN && process.env.HTTPTOOLKIT_SERVER_BINPATH) {
1010
// If we're a built binary, use the standard DSN automatically

0 commit comments

Comments
 (0)