Skip to content

Commit 35cd129

Browse files
committed
Fix profile cleanup for Snap-backed browsers
1 parent b125ead commit 35cd129

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/interceptors/chromium-based-interceptors.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ abstract class FreshChromiumBasedInterceptor implements Interceptor {
106106

107107
const browserDetails = await getBrowserDetails(this.config.configPath, this.variantName);
108108

109-
const profilePath = browserDetails && await isSnap(browserDetails.command)
109+
const snapProfilePath = browserDetails && await isSnap(browserDetails.command)
110110
? path.join(await getSnapConfigPath(this.variantName), 'profile')
111111
: undefined;
112112

113113
const browser = await launchBrowser(hideWarningServer.hideWarningUrl,
114114
await getChromiumLaunchOptions(
115115
browserDetails ? browserDetails.name : this.variantName,
116116
this.config,
117-
profilePath,
117+
snapProfilePath,
118118
proxyPort,
119119
hideWarningServer,
120120
!!options.webExtensionEnabled
@@ -139,22 +139,27 @@ abstract class FreshChromiumBasedInterceptor implements Interceptor {
139139
if (process.platform === 'win32' && this.variantName === 'opera') return;
140140
await delay(1000); // No hurry, make sure the browser & related processes have all cleaned up
141141

142-
if (Object.keys(this.activeBrowsers).length === 0 && typeof browserDetails?.profile === 'string') {
143-
// If we were the last browser, and we have a profile path, and it's in our config
144-
// (just in case something's gone wrong) -> delete the profile to reset everything.
145-
146-
const profilePath = browserDetails.profile;
147-
if (!profilePath.startsWith(this.config.configPath)) {
142+
const activeBrowserCount = Object.keys(this.activeBrowsers).length;
143+
const profilePath = snapProfilePath ?? browserDetails?.profile;
144+
if (activeBrowserCount === 0 && typeof profilePath === 'string') {
145+
// If we were the last browser and we have a profile path, and it's definitely part of our
146+
// local/snap config (just in case something's gone wrong) -> delete it & reset everything.
147+
148+
if (
149+
// Part of our local config path:
150+
!profilePath.startsWith(this.config.configPath) &&
151+
// Or part of the .httptoolkit dir within a Snap's data dir:
152+
!profilePath.includes(path.join('snap', this.variantName, 'current', '.httptoolkit'))
153+
) {
148154
logError(
149155
`Unexpected ${this.variantName} profile location, not deleting: ${profilePath}`
150156
);
151157
} else {
152-
const profileFolder = browserDetails.profile;
153-
deleteFolder(profileFolder)
158+
deleteFolder(profilePath)
154159
.catch(async() => {
155160
// After 1 error, wait a little and retry
156161
await delay(1000);
157-
await deleteFolder(profileFolder);
162+
await deleteFolder(profilePath);
158163
}).catch(console.warn); // If it still fails, just give up, not a big deal
159164
}
160165
}

0 commit comments

Comments
 (0)