File tree Expand file tree Collapse file tree 7 files changed +76
-8
lines changed Expand file tree Collapse file tree 7 files changed +76
-8
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import { HttpToolkitServer } from './httptoolkit-server';
1111import { checkBrowserConfig } from './browsers' ;
1212import { reportError } from './error-tracking' ;
1313import { delay , ALLOWED_ORIGINS } from './util' ;
14+ import { registerShutdownHandler } from './shutdown' ;
1415
1516const canAccess = util . promisify ( fs . access ) ;
1617const mkDir = util . promisify ( fs . mkdir ) ;
@@ -48,6 +49,7 @@ export async function runHTK(options: {
4849 configPath ?: string
4950} = { } ) {
5051 const startTime = Date . now ( ) ;
52+ registerShutdownHandler ( ) ;
5153
5254 const configPath = options . configPath || envPaths ( 'httptoolkit' , { suffix : '' } ) . config ;
5355
Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ export class FreshChrome implements Interceptor {
3131
3232 constructor ( private config : HtkConfig ) { }
3333
34- isActive ( proxyPort : number ) {
34+ isActive ( proxyPort : number | string ) {
3535 return browsers [ proxyPort ] != null && ! ! browsers [ proxyPort ] . pid ;
3636 }
3737
@@ -78,12 +78,18 @@ export class FreshChrome implements Interceptor {
7878 await delay ( 500 ) ;
7979 }
8080
81- async deactivate ( proxyPort : number ) {
81+ async deactivate ( proxyPort : number | string ) {
8282 if ( this . isActive ( proxyPort ) ) {
8383 const browser = browsers [ proxyPort ] ;
8484 const exitPromise = new Promise ( ( resolve ) => browser ! . process . once ( 'exit' , resolve ) ) ;
8585 browser ! . stop ( ) ;
8686 await exitPromise ;
8787 }
8888 }
89+
90+ async deactivateAll ( ) : Promise < void > {
91+ await Promise . all (
92+ Object . keys ( browsers ) . map ( ( proxyPort ) => this . deactivate ( proxyPort ) )
93+ ) ;
94+ }
8995} ;
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ export class FreshFirefox implements Interceptor {
2828
2929 constructor ( private config : HtkConfig ) { }
3030
31- isActive ( proxyPort : number ) {
31+ isActive ( proxyPort : number | string ) {
3232 return browsers [ proxyPort ] != null && ! ! browsers [ proxyPort ] . pid ;
3333 }
3434
@@ -133,12 +133,18 @@ export class FreshFirefox implements Interceptor {
133133 await delay ( 1000 ) ;
134134 }
135135
136- async deactivate ( proxyPort : number ) {
136+ async deactivate ( proxyPort : number | string ) {
137137 if ( this . isActive ( proxyPort ) ) {
138138 const browser = browsers [ proxyPort ] ;
139139 const exitPromise = new Promise ( ( resolve ) => browser ! . process . once ( 'exit' , resolve ) ) ;
140140 browser ! . stop ( ) ;
141141 await exitPromise ;
142142 }
143143 }
144+
145+ async deactivateAll ( ) : Promise < void > {
146+ await Promise . all (
147+ Object . keys ( browsers ) . map ( ( proxyPort ) => this . deactivate ( proxyPort ) )
148+ ) ;
149+ }
144150} ;
Original file line number Diff line number Diff line change @@ -223,7 +223,7 @@ export class TerminalInterceptor implements Interceptor {
223223 return ! ! ( await getTerminalCommand ( ) ) ;
224224 }
225225
226- isActive ( proxyPort : number ) : boolean {
226+ isActive ( proxyPort : number | string ) : boolean {
227227 return ! ! ( terminals [ proxyPort ] && terminals [ proxyPort ] ! . length ) ;
228228 }
229229
@@ -288,7 +288,7 @@ export class TerminalInterceptor implements Interceptor {
288288 } ) ;
289289 }
290290
291- async deactivate ( proxyPort : number ) : Promise < void > {
291+ async deactivate ( proxyPort : number | string ) : Promise < void > {
292292 if ( ! this . isActive ( proxyPort ) ) return ;
293293
294294 await Promise . all ( ( terminals [ proxyPort ] || [ ] ) . map ( ( proc ) => {
@@ -299,4 +299,10 @@ export class TerminalInterceptor implements Interceptor {
299299 } ) ) ;
300300 }
301301
302+ async deactivateAll ( ) : Promise < void > {
303+ await Promise . all (
304+ Object . keys ( terminals ) . map ( ( proxyPort ) => this . deactivate ( proxyPort ) )
305+ ) ;
306+ }
307+
302308}
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { HtkConfig } from '../config';
55import { FreshChrome } from './fresh-chrome' ;
66import { FreshFirefox } from './fresh-firefox' ;
77import { TerminalInterceptor } from './fresh-terminal' ;
8+ import { addShutdownHandler } from '../shutdown' ;
89
910export interface Interceptor {
1011 id : string ;
@@ -15,12 +16,22 @@ export interface Interceptor {
1516
1617 activate ( proxyPort : number , options ?: any ) : Promise < void > ;
1718 deactivate ( proxyPort : number , options ?: any ) : Promise < void > ;
19+ deactivateAll ( ) : Promise < void > ;
1820}
1921
2022export function buildInterceptors ( config : HtkConfig ) : _ . Dictionary < Interceptor > {
21- return _ . keyBy ( [
23+ const interceptors = [
2224 new FreshChrome ( config ) ,
2325 new FreshFirefox ( config ) ,
2426 new TerminalInterceptor ( config )
25- ] , ( interceptor ) => interceptor . id ) ;
27+ ] ;
28+
29+ // When the server exits, try to shut down the interceptors too
30+ addShutdownHandler ( ( ) => shutdownInterceptors ( interceptors ) ) ;
31+
32+ return _ . keyBy ( interceptors , ( interceptor ) => interceptor . id ) ;
33+ }
34+
35+ function shutdownInterceptors ( interceptors : Interceptor [ ] ) {
36+ return Promise . all ( interceptors . map ( i => i . deactivateAll ( ) ) ) ;
2637}
Original file line number Diff line number Diff line change 1+ import { reportError } from './error-tracking' ;
2+
3+ type ShutdownHandler = ( ) => Promise < unknown > ;
4+ const shutdownHandlers : Array < ShutdownHandler > = [ ] ;
5+
6+ export function registerShutdownHandler ( ) {
7+ process . on ( 'SIGTERM' , shutdown ) ;
8+ process . on ( 'SIGINT' , shutdown ) ;
9+ }
10+
11+ export function addShutdownHandler ( handler : ShutdownHandler ) {
12+ shutdownHandlers . push ( handler ) ;
13+ }
14+
15+ async function shutdown ( ) {
16+ console . log ( 'Shutting down...' ) ;
17+
18+ await Promise . all ( shutdownHandlers . map (
19+ ( handler ) => handler ( ) . catch ( reportError )
20+ ) ) ;
21+
22+ process . exit ( 0 ) ;
23+ }
24+
Original file line number Diff line number Diff line change @@ -59,4 +59,17 @@ export function itCanBeActivated(interceptorSetup: InterceptorSetup) {
5959 await interceptor . deactivate ( server . port ) ;
6060 expect ( interceptor . isActive ( server . port ) ) . to . equal ( false ) ;
6161 } ) ;
62+
63+ it ( 'can deactivate all' , async ( ) => {
64+ const { interceptor, server } = await interceptorSetup ;
65+
66+ expect ( interceptor . isActive ( server . port ) ) . to . equal ( false ) ;
67+
68+ await interceptor . activate ( server . port ) ;
69+ expect ( interceptor . isActive ( server . port ) ) . to . equal ( true ) ;
70+ expect ( interceptor . isActive ( server . port + 1 ) ) . to . equal ( false ) ;
71+
72+ await interceptor . deactivateAll ( ) ;
73+ expect ( interceptor . isActive ( server . port ) ) . to . equal ( false ) ;
74+ } ) ;
6275}
You can’t perform that action at this time.
0 commit comments