@@ -4,53 +4,74 @@ module.exports = class HttpServer {
4
4
#response;
5
5
#endpoints;
6
6
#options;
7
+ #server;
7
8
8
- constructor ( http , request , response , endpoints , options ) {
9
+ constructor ( http , request , response , endpoints , options , server ) {
9
10
this . #http = http ;
10
11
this . #request = request ;
11
12
this . #response = response ;
12
13
this . #endpoints = endpoints ;
13
14
this . #options = options ;
15
+ this . #server = server ;
14
16
}
15
17
16
18
start ( ) {
17
- return new Promise ( resolve => {
18
- this . #http
19
- . createServer ( async ( requestStream , responseStream ) => {
20
- try {
21
- return await ( this . #response
22
- . copy ( responseStream , await this . #endpoints
23
- . handle ( await ( this . #request
24
- . copy ( requestStream ) )
25
- . flush ( ) ) ) )
26
- . flush ( ) ;
19
+ const server = this . #http. createServer ( async ( requestStream , responseStream ) => {
20
+ try {
21
+ return await ( this . #response
22
+ . copy ( responseStream , await this . #endpoints
23
+ . handle ( await ( this . #request
24
+ . copy ( requestStream ) )
25
+ . flush ( ) ) ) )
26
+ . flush ( ) ;
27
+
28
+ } catch ( e ) {
29
+ if ( e . cause === 'INVALID_REQUEST' ) {
30
+ return await ( this . #response
31
+ . copy ( responseStream , {
32
+ statusCode : 400 ,
33
+ body : e . message
34
+ } ) )
35
+ . flush ( ) ;
36
+ }
37
+
38
+ return await ( this . #response
39
+ . copy ( responseStream , {
40
+ statusCode : 500 ,
41
+ body : 'Unexpected server error.'
42
+ } ) )
43
+ . flush ( ) ;
44
+ }
45
+ } ) ;
27
46
28
- } catch ( e ) {
29
- if ( e . cause === 'INVALID_REQUEST' ) {
30
- return this . #response
31
- . copy ( responseStream , {
32
- statusCode : 400 ,
33
- body : e . message
34
- } )
35
- . flush ( ) ;
36
- }
47
+ return new Promise ( resolve => {
48
+ server . listen (
49
+ this . #options,
50
+ ( ) => resolve ( new HttpServer (
51
+ this . #http,
52
+ this . #request,
53
+ this . #response,
54
+ this . #endpoints,
55
+ this . #options,
56
+ server ) )
57
+ ) ;
58
+ } ) ;
59
+ }
37
60
38
- return this . #response
39
- . copy ( responseStream , {
40
- statusCode : 500 ,
41
- body : 'Unexpected server error.'
42
- } )
43
- . flush ( ) ;
44
- }
45
- } )
46
- . listen (
47
- { port : this . #options. port } ,
48
- ( ) => resolve ( this )
49
- ) ;
61
+ stop ( ) {
62
+ return new Promise ( resolve => {
63
+ this . #server. close (
64
+ ( ) => resolve ( new HttpServer (
65
+ this . #http,
66
+ this . #request,
67
+ this . #response,
68
+ this . #endpoints,
69
+ this . #options) )
70
+ ) ;
50
71
} ) ;
51
72
}
52
73
53
74
options ( ) {
54
75
return this . #options;
55
76
}
56
- }
77
+ } ;
0 commit comments