|
| 1 | +import * as fs from 'fs'; |
1 | 2 | import * as path from 'path';
|
2 | 3 | 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'; |
4 | 5 | import { promisify } from 'util';
|
5 | 6 |
|
| 7 | +const readFile = promisify(fs.readFile); |
| 8 | +const deleteFile = promisify(fs.unlink); |
| 9 | + |
| 10 | +const browserConfigPath = (configPath: string) => path.join(configPath, 'browsers.json'); |
| 11 | + |
6 | 12 | export { BrowserInstance };
|
7 | 13 |
|
| 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 | + |
8 | 31 | async function getLauncher(configPath: string) {
|
9 |
| - return await promisify(getBrowserLauncher)(path.join(configPath, 'browsers.json')); |
| 32 | + return await promisify(getBrowserLauncher)(browserConfigPath(configPath)); |
10 | 33 | }
|
11 | 34 |
|
12 | 35 | export const getAvailableBrowsers = async (configPath: string) => {
|
|
0 commit comments