Skip to content

Commit 109a2bf

Browse files
committed
Improve interceptor activation tracking & reporting
This now reports failed startups only for actual timeouts or errors, and stops treating isActive as a strictly required result for activate(). For existing terminals for example, activate starts a server so they can be started, and isActive returns true only once somebody has used that used.
1 parent 4b30112 commit 109a2bf

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

src/httptoolkit-server.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,20 @@ const buildResolvers = (
7474
const interceptor = interceptors[id];
7575
if (!interceptor) throw new Error(`Unknown interceptor ${id}`);
7676

77-
let metadata: any = null;
78-
await Promise.race([
79-
interceptor.activate(proxyPort, options)
80-
.then((result) => metadata = result)
81-
.catch(reportError),
82-
delay(30000) // After 30s, we don't stop activating, but we do report failure
77+
const result = await Promise.race([
78+
// Activate, and either return metadata, or some error
79+
interceptor.activate(proxyPort, options).catch((e) => e),
80+
// After 30s, we don't stop activating, but we do return an error
81+
delay(30000).then(() => new Error(`Timeout activating ${id}`))
8382
]);
8483

85-
const isActive = interceptor.isActive(proxyPort);
86-
87-
if (isActive) {
88-
addBreadcrumb(`Successfully activated ${id}`, { category: 'interceptor' });
84+
if (_.isError(result)) {
85+
reportError(result);
86+
return { success: false };
8987
} else {
90-
reportError(new Error(`Failed to activate ${id}`));
88+
addBreadcrumb(`Successfully activated ${id}`, { category: 'interceptor' });
89+
return { success: true, metadata: result };
9190
}
92-
93-
return { success: isActive, metadata };
9491
},
9592
deactivateInterceptor: async (__: void, args: _.Dictionary<any>) => {
9693
const { id, proxyPort, options } = args;

src/interceptors/fresh-firefox.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { getAvailableBrowsers, launchBrowser, BrowserInstance } from '../browser
1010
import { CertCheckServer } from '../cert-check-server';
1111
import { delay } from '../util';
1212
import { Interceptor } from '.';
13+
import { reportError } from '../error-tracking';
1314

1415
const deleteFolder = promisify(rimraf);
1516
const readFile = promisify(fs.readFile);
@@ -132,6 +133,7 @@ export class FreshFirefox implements Interceptor {
132133
certCheckServer.stop();
133134
delete browsers[proxyPort];
134135
if (!success) {
136+
reportError('Firefox certificate check failed');
135137
deleteFolder(firefoxProfile).catch(console.warn);
136138
}
137139
});

src/interceptors/terminal/existing-terminal-interceptor.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ import { HtkConfig } from '../../config';
66
import { getTerminalEnvVars } from './terminal-env-overrides';
77
import { getShellScript } from './terminal-scripts';
88

9+
interface ServerState {
10+
server: Mockttp;
11+
isActive: boolean;
12+
}
13+
914
export class ExistingTerminalInterceptor implements Interceptor {
1015

11-
private servers: { [proxyPort: number]: Mockttp } = {};
16+
private servers: {
17+
[proxyPort: number]: ServerState
18+
} = {};
1219

1320
id = 'existing-terminal';
1421
version = '1.0.0';
@@ -20,29 +27,40 @@ export class ExistingTerminalInterceptor implements Interceptor {
2027
}
2128

2229
isActive(proxyPort: number): boolean {
23-
return !!this.servers[proxyPort];
30+
const serverState = this.servers[proxyPort];
31+
return !!serverState && serverState.isActive;
2432
}
2533

2634
async activate(proxyPort: number): Promise<{ port: number }> {
2735
if (this.servers[proxyPort]) {
28-
return { port: this.servers[proxyPort].port };
36+
return { port: this.servers[proxyPort].server.port };
2937
}
3038

3139
const server = getLocal();
3240
await server.start({ startPort: proxyPort + 1, endPort: 65535 });
3341

3442
const envVars = getTerminalEnvVars(proxyPort, this.config.https, 'runtime-inherit');
3543
const setupScript = getShellScript(envVars);
36-
server.get('/setup').thenReply(200, setupScript, { "content-type": "text/x-shellscript" });
37-
38-
this.servers[proxyPort] = server;
3944

45+
const serverState = { server, isActive: false };
46+
await server
47+
.get('/setup')
48+
.thenCallback(() => {
49+
serverState.isActive = true;
50+
return {
51+
status: 200,
52+
headers: { "content-type": "text/x-shellscript" },
53+
body: setupScript
54+
};
55+
});
56+
57+
this.servers[proxyPort] = serverState;
4058
return { port: server.port };
4159
}
4260

4361
async deactivate(proxyPort: number): Promise<void> {
44-
if (this.isActive(proxyPort)) {
45-
await this.servers[proxyPort].stop();
62+
if (this.servers[proxyPort]) {
63+
await this.servers[proxyPort].server.stop();
4664
delete this.servers[proxyPort];
4765
}
4866
}

0 commit comments

Comments
 (0)