@@ -6,62 +6,91 @@ import { TcpAddress } from "../shared/TcpTypes.js";
6
6
7
7
export class HttpServerElectron {
8
8
readonly id : number ;
9
- private _server : http . Server ;
10
- private _messagePort : MessagePort ;
11
- private _nextRequestId = 0 ;
12
- private _requests = new Map < number , ( response : HttpResponse ) => Promise < void > > ( ) ;
13
- private _api = new Map < string , RpcHandler > ( [
14
- [ "address" , ( callId ) => this . _apiResponse ( [ callId , this . address ( ) ] ) ] ,
9
+ #server: http . Server ;
10
+ #messagePort: MessagePort ;
11
+ #nextRequestId = 0 ;
12
+ #requests = new Map < number , ( response : HttpResponse ) => Promise < void > > ( ) ;
13
+ #api = new Map < string , RpcHandler > ( [
14
+ [
15
+ "address" ,
16
+ ( callId ) => {
17
+ this . #apiResponse( [ callId , this . address ( ) ] ) ;
18
+ } ,
19
+ ] ,
15
20
[
16
21
"listen" ,
17
22
( callId , args ) => {
18
23
const port = args [ 0 ] as number | undefined ;
19
24
const hostname = args [ 1 ] as string | undefined ;
20
25
const backlog = args [ 2 ] as number | undefined ;
21
26
this . listen ( port , hostname , backlog )
22
- . then ( ( ) => this . _apiResponse ( [ callId , undefined ] ) )
23
- . catch ( ( err : Error ) => this . _apiResponse ( [ callId , String ( err . stack ?? err ) ] ) ) ;
27
+ . then ( ( ) => {
28
+ this . #apiResponse( [ callId , undefined ] ) ;
29
+ } )
30
+ . catch ( ( err : Error ) => {
31
+ this . #apiResponse( [ callId , String ( err . stack ?? err ) ] ) ;
32
+ } ) ;
24
33
} ,
25
34
] ,
26
35
[
27
36
"response" ,
28
37
( callId , args ) => {
29
38
const requestId = args [ 0 ] as number ;
30
39
const response = args [ 1 ] as HttpResponse ;
31
- const handler = this . _requests . get ( requestId ) ;
40
+ const handler = this . #requests . get ( requestId ) ;
32
41
if ( handler == undefined ) {
33
- this . _apiResponse ( [ callId , `unknown requestId ${ requestId } ` ] ) ;
42
+ this . #apiResponse ( [ callId , `unknown requestId ${ requestId } ` ] ) ;
34
43
return ;
35
44
}
36
- this . _requests . delete ( requestId ) ;
45
+ this . #requests . delete ( requestId ) ;
37
46
handler ( response )
38
- . then ( ( ) => this . _apiResponse ( [ callId , undefined ] ) )
39
- . catch ( ( err : Error ) => this . _apiResponse ( [ callId , String ( err . stack ?? err ) ] ) ) ;
47
+ . then ( ( ) => {
48
+ this . #apiResponse( [ callId , undefined ] ) ;
49
+ } )
50
+ . catch ( ( err : Error ) => {
51
+ this . #apiResponse( [ callId , String ( err . stack ?? err ) ] ) ;
52
+ } ) ;
53
+ } ,
54
+ ] ,
55
+ [
56
+ "close" ,
57
+ ( callId ) => {
58
+ //eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
59
+ this . #apiResponse( [ callId , this . close ( ) ] ) ;
60
+ } ,
61
+ ] ,
62
+ [
63
+ "dispose" ,
64
+ ( callId ) => {
65
+ //eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
66
+ this . #apiResponse( [ callId , this . dispose ( ) ] ) ;
40
67
} ,
41
68
] ,
42
- [ "close" , ( callId ) => this . _apiResponse ( [ callId , this . close ( ) ] ) ] ,
43
- [ "dispose" , ( callId ) => this . _apiResponse ( [ callId , this . dispose ( ) ] ) ] ,
44
69
] ) ;
45
70
46
71
constructor ( id : number , messagePort : MessagePort ) {
47
72
this . id = id ;
48
- this . _server = http . createServer ( this . _handleRequest ) ;
49
- this . _messagePort = messagePort ;
73
+ this . #server = http . createServer ( this . #handleRequest ) ;
74
+ this . #messagePort = messagePort ;
50
75
51
- this . _server . on ( "close" , ( ) => this . _emit ( "close" ) ) ;
52
- this . _server . on ( "error" , ( err ) => this . _emit ( "error" , String ( err . stack ?? err ) ) ) ;
76
+ this . #server. on ( "close" , ( ) => {
77
+ this . #emit( "close" ) ;
78
+ } ) ;
79
+ this . #server. on ( "error" , ( err ) => {
80
+ this . #emit( "error" , String ( err . stack ?? err ) ) ;
81
+ } ) ;
53
82
54
83
messagePort . onmessage = ( ev : MessageEvent < RpcCall > ) => {
55
84
const [ methodName , callId ] = ev . data ;
56
85
const args = ev . data . slice ( 2 ) ;
57
- const handler = this . _api . get ( methodName ) ;
86
+ const handler = this . #api . get ( methodName ) ;
58
87
handler ?.( callId , args ) ;
59
88
} ;
60
89
messagePort . start ( ) ;
61
90
}
62
91
63
92
address ( ) : TcpAddress | undefined {
64
- const addr = this . _server . address ( ) ;
93
+ const addr = this . #server . address ( ) ;
65
94
if ( addr == undefined || typeof addr === "string" ) {
66
95
// Address will only be a string for an IPC (named pipe) server, which
67
96
// should never happen here
@@ -71,45 +100,45 @@ export class HttpServerElectron {
71
100
}
72
101
73
102
async listen ( port ?: number , hostname ?: string , backlog ?: number ) : Promise < void > {
74
- return await new Promise ( ( resolve , reject ) => {
75
- this . _server . listen ( port , hostname , backlog , ( ) => {
76
- this . _server . removeListener ( "error" , reject ) ;
103
+ await new Promise < void > ( ( resolve , reject ) => {
104
+ this . #server . listen ( port , hostname , backlog , ( ) => {
105
+ this . #server . removeListener ( "error" , reject ) ;
77
106
resolve ( ) ;
78
107
} ) ;
79
108
} ) ;
80
109
}
81
110
82
111
close ( ) : void {
83
- this . _server . close ( ) ;
112
+ this . #server . close ( ) ;
84
113
}
85
114
86
115
dispose ( ) : void {
87
- this . _server . removeAllListeners ( ) ;
116
+ this . #server . removeAllListeners ( ) ;
88
117
this . close ( ) ;
89
- this . _messagePort . close ( ) ;
118
+ this . #messagePort . close ( ) ;
90
119
}
91
120
92
- private _apiResponse ( message : RpcResponse , transfer ?: Transferable [ ] ) : void {
121
+ #apiResponse ( message : RpcResponse , transfer ?: Transferable [ ] ) : void {
93
122
if ( transfer != undefined ) {
94
- this . _messagePort . postMessage ( message , transfer ) ;
123
+ this . #messagePort . postMessage ( message , transfer ) ;
95
124
} else {
96
- this . _messagePort . postMessage ( message ) ;
125
+ this . #messagePort . postMessage ( message ) ;
97
126
}
98
127
}
99
128
100
- private _emit ( eventName : string , ...args : Cloneable [ ] ) : void {
129
+ #emit ( eventName : string , ...args : Cloneable [ ] ) : void {
101
130
const msg = [ eventName , ...args ] ;
102
- this . _messagePort . postMessage ( msg ) ;
131
+ this . #messagePort . postMessage ( msg ) ;
103
132
}
104
133
105
- private _handleRequest = ( req : http . IncomingMessage , res : http . ServerResponse ) : void => {
134
+ #handleRequest = ( req : http . IncomingMessage , res : http . ServerResponse ) : void => {
106
135
const chunks : Uint8Array [ ] = [ ] ;
107
136
req . on ( "data" , ( chunk : Uint8Array ) => chunks . push ( chunk ) ) ;
108
137
req . on ( "end" , ( ) => {
109
138
const body = Buffer . concat ( chunks ) . toString ( ) ;
110
139
111
- const requestId = this . _nextRequestId ++ ;
112
- this . _requests . set ( requestId , async ( out ) : Promise < void > => {
140
+ const requestId = this . #nextRequestId ++ ;
141
+ this . #requests . set ( requestId , async ( out ) : Promise < void > => {
113
142
res . shouldKeepAlive = out . shouldKeepAlive ?? res . shouldKeepAlive ;
114
143
res . statusCode = out . statusCode ;
115
144
res . statusMessage = out . statusMessage ?? "" ;
@@ -147,7 +176,7 @@ export class HttpServerElectron {
147
176
remotePort : req . socket . remotePort ,
148
177
} ,
149
178
} ;
150
- this . _emit ( "request" , requestId , request ) ;
179
+ this . #emit ( "request" , requestId , request ) ;
151
180
} ) ;
152
181
} ;
153
182
}
0 commit comments