1 import { type IncomingMessage
, Server
, type ServerResponse
} from
'node:http'
2 import { type Http2Server
, createServer
} from
'node:http2'
4 import type { WebSocket
} from
'ws'
6 import type { AbstractUIService
} from
'./ui-services/AbstractUIService.js'
7 import { UIServiceFactory
} from
'./ui-services/UIServiceFactory.js'
8 import { BaseError
} from
'../../exception/index.js'
10 ApplicationProtocolVersion
,
12 type ChargingStationData
,
15 type ProtocolResponse
,
19 type UIServerConfiguration
20 } from
'../../types/index.js'
22 export abstract class AbstractUIServer
{
23 public readonly chargingStations
: Map
<string, ChargingStationData
>
24 public readonly chargingStationTemplates
: Set
<string>
25 protected readonly httpServer
: Server
| Http2Server
26 protected readonly responseHandlers
: Map
<string, ServerResponse
| WebSocket
>
27 protected readonly uiServices
: Map
<ProtocolVersion
, AbstractUIService
>
29 public constructor (protected readonly uiServerConfiguration
: UIServerConfiguration
) {
30 this.chargingStations
= new Map
<string, ChargingStationData
>()
31 this.chargingStationTemplates
= new Set
<string>()
32 switch (this.uiServerConfiguration
.version
) {
33 case ApplicationProtocolVersion
.VERSION_11
:
34 this.httpServer
= new Server()
36 case ApplicationProtocolVersion
.VERSION_20
:
37 this.httpServer
= createServer()
41 `Unsupported application protocol version ${this.uiServerConfiguration.version}`
44 this.responseHandlers
= new Map
<string, ServerResponse
| WebSocket
>()
45 this.uiServices
= new Map
<ProtocolVersion
, AbstractUIService
>()
48 public buildProtocolRequest (
50 procedureName
: ProcedureName
,
51 requestPayload
: RequestPayload
53 return [id
, procedureName
, requestPayload
]
56 public buildProtocolResponse (id
: string, responsePayload
: ResponsePayload
): ProtocolResponse
{
57 return [id
, responsePayload
]
60 public stop (): void {
62 for (const uiService
of this.uiServices
.values()) {
65 this.chargingStations
.clear()
66 this.chargingStationTemplates
.clear()
69 public async sendInternalRequest (request
: ProtocolRequest
): Promise
<ProtocolResponse
> {
70 const protocolVersion
= ProtocolVersion
['0.0.1']
71 this.registerProtocolVersionUIService(protocolVersion
)
72 return await (this.uiServices
74 ?.requestHandler(request
) as Promise
<ProtocolResponse
>)
77 public hasResponseHandler (id
: string): boolean {
78 return this.responseHandlers
.has(id
)
81 protected startHttpServer (): void {
82 if (!this.httpServer
.listening
) {
83 this.httpServer
.listen(this.uiServerConfiguration
.options
)
87 protected registerProtocolVersionUIService (version
: ProtocolVersion
): void {
88 if (!this.uiServices
.has(version
)) {
89 this.uiServices
.set(version
, UIServiceFactory
.getUIServiceImplementation(version
, this))
93 protected authenticate (req
: IncomingMessage
, next
: (err
?: Error) => void): void {
94 if (this.isBasicAuthEnabled()) {
95 if (!this.isValidBasicAuth(req
)) {
96 next(new BaseError('Unauthorized'))
103 private stopHttpServer (): void {
104 if (this.httpServer
.listening
) {
105 this.httpServer
.close()
109 private isBasicAuthEnabled (): boolean {
111 this.uiServerConfiguration
.authentication
?.enabled
=== true &&
112 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
113 this.uiServerConfiguration
.authentication
.type === AuthenticationType
.BASIC_AUTH
117 private isValidBasicAuth (req
: IncomingMessage
): boolean {
118 const authorizationHeader
= req
.headers
.authorization
?? ''
119 const authorizationToken
= authorizationHeader
.split(/\s
+/).pop() ?? ''
120 const authentication
= Buffer
.from(authorizationToken
, 'base64').toString()
121 const authenticationParts
= authentication
.split(/:/)
122 const username
= authenticationParts
.shift()
123 const password
= authenticationParts
.join(':')
125 this.uiServerConfiguration
.authentication
?.username
=== username
&&
126 this.uiServerConfiguration
.authentication
?.password
=== password
130 public abstract start (): void
131 public abstract sendRequest (request
: ProtocolRequest
): void
132 public abstract sendResponse (response
: ProtocolResponse
): void
133 public abstract logPrefix (moduleName
?: string, methodName
?: string, prefixSuffix
?: string): string