Skip to content

Commit f6e13f9

Browse files
committed
Persist the firefox profile, as long as setup was successful.
This depends on https://github.com/httptoolkit/james-browser-launcher/commit/02c8a1f4ad9e6fad8c9dfa1ed0a962c77f29985b which adds this functionality to james-browser-launcher. In the meantime, this will continue to work as previously.
1 parent 6c684e1 commit f6e13f9

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ declare module '@james-proxy/james-browser-launcher' {
1616
noProxy?: string | string[];
1717
headless?: boolean;
1818
prefs?: { [key: string]: any };
19+
profile?: string;
1920
}
2021

2122
function Launch(

package-lock.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
"@oclif/plugin-help": "^2.1.3",
2727
"@types/env-paths": "^1.0.2",
2828
"@types/lodash": "^4.14.117",
29+
"@types/rimraf": "^2.0.2",
2930
"env-paths": "^1.0.0",
3031
"graphql-yoga": "^1.16.7",
3132
"lodash": "^4.17.11",
3233
"mockttp": "^0.11.0",
34+
"rimraf": "^2.6.2",
3335
"tslib": "^1.9.3"
3436
},
3537
"devDependencies": {

src/cert-check-server.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@ let installingCert: boolean;
1717
function ensureCertificateIsInstalled() {
1818
const testUrl = window.location.href.replace('http://', 'https://').replace('check-cert', 'test-https');
1919
const downloadUrl = window.location.href.replace('check-cert', 'download-cert');
20+
const reportSuccessUrl = window.location.href.replace('check-cert', 'report-success');
2021

2122
fetch(testUrl)
2223
.then(() => true)
2324
.catch(() => false)
2425
.then((certificateIsTrusted) => {
2526
if (certificateIsTrusted) {
26-
window.location.replace(targetUrl);
27+
// Report success (ignoring errors) then continue.
28+
fetch(reportSuccessUrl).catch(() => {}).then(() => {
29+
window.location.replace(targetUrl);
30+
});
2731
} else {
32+
// Start trying to prompt the user to install the cert
2833
if (!installingCert) {
2934
installingCert = true;
3035
const iframe = document.createElement('iframe');
@@ -136,6 +141,15 @@ export class CertCheckServer {
136141
.replace(/\/?$/, '/check-cert');
137142
}
138143

144+
async waitForSuccess(): Promise<void> {
145+
return new Promise<void>((resolve) =>
146+
this.server!.get('/report-success').thenCallback(() => {
147+
resolve();
148+
return { status: 200 };
149+
})
150+
);
151+
}
152+
139153
async stop() {
140154
if (this.server) {
141155
await this.server.stop();

src/interceptors/fresh-firefox.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import { promisify } from 'util';
12
import * as _ from 'lodash';
3+
import * as rimraf from 'rimraf';
4+
import * as path from 'path';
25

36
import { HtkConfig } from '../config';
47

58
import { getAvailableBrowsers, launchBrowser, BrowserInstance } from '../browsers';
69
import { CertCheckServer } from '../cert-check-server';
710

11+
const deleteFolder = promisify(rimraf);
12+
813
let browsers: _.Dictionary<BrowserInstance> = {};
914

1015
export class FreshFirefox {
@@ -32,11 +37,11 @@ export class FreshFirefox {
3237
const certCheckServer = new CertCheckServer(this.config);
3338
await certCheckServer.start('https://amiusing.httptoolkit.tech');
3439

35-
// TODO: Change james launcher so I can pass a profile here,
36-
// so things persist across launches. Permanently? Yes, probably.
40+
const firefoxProfile = path.join(this.config.configPath, 'firefox-profile');
3741

3842
const browser = await launchBrowser(certCheckServer.checkCertUrl, {
3943
browser: 'firefox',
44+
profile: firefoxProfile,
4045
proxy: `localhost:${proxyPort}`,
4146
// Don't intercept our cert testing requests
4247
noProxy: certCheckServer.host,
@@ -60,8 +65,6 @@ export class FreshFirefox {
6065
"browser.tabs.warnOnClose": false,
6166
"browser.tabs.warnOnCloseOtherTabs": false,
6267

63-
// Disable 'new FF available' messages
64-
6568
// Disable various first-run things:
6669
"browser.uitour.enabled": false,
6770
'browser.usedOnWindows10': true,
@@ -75,10 +78,18 @@ export class FreshFirefox {
7578
}
7679
}, this.config.configPath);
7780

81+
let success = false;
82+
certCheckServer.waitForSuccess().then(() => {
83+
success = true;
84+
}).catch(console.warn);
85+
7886
browsers[proxyPort] = browser;
7987
browser.process.once('exit', () => {
8088
certCheckServer.stop();
8189
delete browsers[proxyPort];
90+
if (!success) {
91+
deleteFolder(firefoxProfile).catch(console.warn);
92+
}
8293
});
8394
}
8495

0 commit comments

Comments
 (0)