Skip to content

Commit 8d58432

Browse files
committed
Ensure browsers.json is in a good start before starting
1 parent 858fb17 commit 8d58432

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/browsers.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1+
import * as fs from 'fs';
12
import * as path from 'path';
23
import * as getBrowserLauncher from '@james-proxy/james-browser-launcher';
3-
import { Launch, LaunchOptions, BrowserInstance } from '@james-proxy/james-browser-launcher';
4+
import { LaunchOptions, BrowserInstance } from '@james-proxy/james-browser-launcher';
45
import { promisify } from 'util';
56

7+
const readFile = promisify(fs.readFile);
8+
const deleteFile = promisify(fs.unlink);
9+
10+
const browserConfigPath = (configPath: string) => path.join(configPath, 'browsers.json');
11+
612
export { BrowserInstance };
713

14+
export async function checkBrowserConfig(configPath: string) {
15+
// It's not clear why, but sometimes the browser config can become corrupted, so it's not valid JSON
16+
// If that happens JBL doesn't catch it, so we crash. To avoid that, we check it here on startup.
17+
18+
const browserConfig = browserConfigPath(configPath);
19+
return readFile(browserConfig, 'utf8')
20+
.then((contents) => JSON.parse(contents))
21+
.catch((error) => {
22+
if (error.code === 'ENOENT') return;
23+
24+
console.warn('Failed to read browser config on startup', error);
25+
return deleteFile(browserConfig).catch((err) => {
26+
console.error('Failed to clear broken config file:', err);
27+
});
28+
});
29+
}
30+
831
async function getLauncher(configPath: string) {
9-
return await promisify(getBrowserLauncher)(path.join(configPath, 'browsers.json'));
32+
return await promisify(getBrowserLauncher)(browserConfigPath(configPath));
1033
}
1134

1235
export const getAvailableBrowsers = async (configPath: string) => {

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getStandalone, generateCACertificate } from 'mockttp';
77
import updateCommand from '@oclif/plugin-update/lib/commands/update';
88

99
import { HttpToolkitServer } from './httptoolkit-server';
10+
import { checkBrowserConfig } from './browsers';
1011

1112
const canAccess = util.promisify(fs.access);
1213
const mkDir = util.promisify(fs.mkdir);
@@ -46,6 +47,7 @@ export async function runHTK(options: {
4647
const configPath = options.configPath || envPaths('httptoolkit', { suffix: '' }).config;
4748

4849
await ensureDirectoryExists(configPath);
50+
await checkBrowserConfig(configPath);
4951

5052
const httpsConfig = await generateHTTPSConfig(configPath);
5153

0 commit comments

Comments
 (0)