Skip to content

Commit 1865536

Browse files
committed
Create an existing-terminal interceptor
1 parent 64c4e58 commit 1865536

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

src/interceptors/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { HtkConfig } from '../config';
44

55
import { FreshChrome } from './fresh-chrome';
66
import { FreshFirefox } from './fresh-firefox';
7-
import { TerminalInterceptor } from './terminal/fresh-terminal-interceptor';
7+
import { FreshTerminalInterceptor } from './terminal/fresh-terminal-interceptor';
8+
import { ExistingTerminalInterceptor } from './terminal/existing-terminal-interceptor';
89
import { addShutdownHandler } from '../shutdown';
910

1011
export interface Interceptor {
@@ -23,7 +24,8 @@ export function buildInterceptors(config: HtkConfig): _.Dictionary<Interceptor>
2324
const interceptors = [
2425
new FreshChrome(config),
2526
new FreshFirefox(config),
26-
new TerminalInterceptor(config)
27+
new FreshTerminalInterceptor(config),
28+
new ExistingTerminalInterceptor(config)
2729
];
2830

2931
// When the server exits, try to shut down the interceptors too
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
import { Mockttp, getLocal } from 'mockttp';
3+
4+
import { Interceptor } from '..';
5+
import { HtkConfig } from '../../config';
6+
import { getTerminalEnvVars } from './terminal-env-overrides';
7+
import { getShellScript } from './terminal-scripts';
8+
9+
export class ExistingTerminalInterceptor implements Interceptor {
10+
11+
private servers: { [proxyPort: number]: Mockttp } = {};
12+
13+
id = 'existing-terminal';
14+
version = '1.0.0';
15+
16+
constructor(private config: HtkConfig) { }
17+
18+
isActivable(): Promise<boolean> {
19+
return Promise.resolve(true);
20+
}
21+
22+
isActive(proxyPort: number): boolean {
23+
return !!this.servers[proxyPort];
24+
}
25+
26+
async activate(proxyPort: number): Promise<{ port: number }> {
27+
const server = getLocal();
28+
await server.start();
29+
30+
const envVars = getTerminalEnvVars(proxyPort, this.config.https, 'runtime-inherit');
31+
const setupScript = getShellScript(envVars);
32+
server.get('/setup').thenReply(200, setupScript, { "content-type": "text/x-shellscript" })
33+
34+
this.servers[proxyPort] = server;
35+
36+
return { port: server.port };
37+
}
38+
39+
async deactivate(proxyPort: number): Promise<void> {
40+
if (this.isActive(proxyPort)) {
41+
await this.servers[proxyPort].stop();
42+
delete this.servers[proxyPort];
43+
}
44+
}
45+
46+
deactivateAll(): Promise<void> {
47+
return Promise.all(
48+
Object.keys(this.servers).map((port) =>
49+
this.deactivate(parseInt(port, 10))
50+
)
51+
).then(() => {});
52+
}
53+
54+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ const getXfceTerminalCommand = async (command = 'xfce4-terminal'): Promise<Spawn
214214

215215
const terminals: _.Dictionary<ChildProcess[] | undefined> = {}
216216

217-
export class TerminalInterceptor implements Interceptor {
217+
export class FreshTerminalInterceptor implements Interceptor {
218218

219219
id = 'fresh-terminal';
220220
version = '1.0.0';

src/interceptors/terminal/terminal-scripts.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ if [ -n "$HTTP_TOOLKIT_ACTIVE" ]
7171
end
7272
${END_CONFIG_SECTION}`;
7373

74+
// A source-able shell script. Should work for everything except fish, sadly.
75+
export const getShellScript = (env: { [name: string]: string }) => `${
76+
_.map(env, (value, key) => ` export ${key}="${value}"`).join('\n')
77+
}
78+
79+
if command -v winpty >/dev/null 2>&1; then
80+
# Work around for winpty's hijacking of certain commands
81+
alias php=php
82+
alias node=node
83+
fi
84+
85+
echo 'HTTP Toolkit interception enabled'
86+
`;
87+
7488
// Find the relevant user shell config file, add the above line to it, so that
7589
// shells launched with HTTP_TOOLKIT_ACTIVE set use the interception PATH.
7690
export const editShellStartupScripts = async () => {

test/integration-test.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ describe('Integration test', function () {
141141
"isActivable": true,
142142
"isActive": false,
143143
"version": "1.0.0"
144+
},
145+
{
146+
"id": "existing-terminal",
147+
"isActivable": true,
148+
"isActive": false,
149+
"version": "1.0.0"
144150
}
145151
]);
146152
});

0 commit comments

Comments
 (0)