1
1
#!/usr/bin/env ts-node
2
2
3
- /* eslint-disable @typescript-eslint/no-misused-promises */
4
3
/* eslint-disable no-console */
5
4
import express from 'express'
6
5
import { MockServer } from './MockServer'
7
6
import Router from 'express-promise-router'
8
7
import cors from 'cors'
9
8
10
- // const startMockServerController = async () => {
11
- // /**
12
- // * @type {MockServer }
13
- // */
14
- // const mockServer = null
15
-
16
- // app.get('/stop', (req, res) => {
17
- // res.send('Hello World!')
18
- // })
19
-
20
- // app.listen(port, () => {
21
- // console.log(`Example app listening on port ${port}`)
22
- // })
23
- // }
24
-
25
- // type ExpressGetHandler = Parameters<IRouterMatcher<'get'>>[1]
26
-
27
9
class MockServerController {
28
10
private readonly mockServers : MockServer [ ] = [ ]
29
11
private readonly app = express ( )
@@ -32,7 +14,7 @@ class MockServerController {
32
14
private readonly port = 3000
33
15
server : import ( 'http' ) . Server
34
16
constructor ( ) {
35
- this . router . get < '/start' , { port ?: string } > ( '/start' , async ( req , res , next ) => {
17
+ this . router . get < '/start' , { port ?: string } > ( '/start' , async ( req , res , next ) => { // eslint-disable-line @typescript-eslint/no-misused-promises
36
18
const { port } = req . params
37
19
38
20
let mockServer : MockServer | null = null
@@ -57,7 +39,7 @@ class MockServerController {
57
39
/**
58
40
* A client will request to shut down it's mockServer by port, which it should have received upon calling '/start'
59
41
*/
60
- this . router . get < '/stop/:port' , { port : string } > ( '/stop/:port' , async ( req , res , next ) => {
42
+ this . router . get < '/stop/:port' , { port : string } > ( '/stop/:port' , async ( req , res , next ) => { // eslint-disable-line @typescript-eslint/no-misused-promises
61
43
const { port } = req . params
62
44
63
45
const mockServer = this . mockServers . find ( ( mockS ) => mockS . basePath . includes ( port ) )
@@ -84,21 +66,27 @@ class MockServerController {
84
66
} )
85
67
86
68
// And you'll want to make sure you close the server when your process exits
87
- process . on ( 'beforeExit' , this . shutdown )
88
- process . on ( 'SIGTERM' , this . shutdown )
89
- process . on ( 'SIGINT' , this . shutdown )
90
- process . on ( 'SIGHUP' , this . shutdown )
69
+ process . on ( 'beforeExit' , this . shutdownSync )
70
+ process . on ( 'SIGTERM' , this . shutdownSync )
71
+ process . on ( 'SIGINT' , this . shutdownSync )
72
+ process . on ( 'SIGHUP' , this . shutdownSync )
91
73
92
74
// To prevent duplicated cleanup, remove the process listeners on server close.
93
75
this . server . on ( 'close' , ( ) => {
94
76
} )
95
77
}
96
78
79
+ private shutdownSync ( ) {
80
+ this . shutdown ( ) . catch ( ( err ) => {
81
+ console . error ( err )
82
+ } )
83
+ }
84
+
97
85
async shutdown ( ) {
98
- process . off ( 'beforeExit' , this . shutdown )
99
- process . off ( 'SIGTERM' , this . shutdown )
100
- process . off ( 'SIGINT' , this . shutdown )
101
- process . off ( 'SIGHUP' , this . shutdown )
86
+ process . off ( 'beforeExit' , this . shutdownSync )
87
+ process . off ( 'SIGTERM' , this . shutdownSync )
88
+ process . off ( 'SIGINT' , this . shutdownSync )
89
+ process . off ( 'SIGHUP' , this . shutdownSync )
102
90
await new Promise < void > ( ( resolve , reject ) => {
103
91
this . server . close ( ( err ) => {
104
92
if ( err != null ) {
0 commit comments