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 { getUsernameAndPasswordFromAuthorizationToken
} from
'./UIServerUtils.js'
9 import { BaseError
} from
'../../exception/index.js'
11 ApplicationProtocolVersion
,
13 type ChargingStationData
,
16 type ProtocolResponse
,
20 type UIServerConfiguration
21 } from
'../../types/index.js'
23 export abstract class AbstractUIServer
{
24 public readonly chargingStations
: Map
<string, ChargingStationData
>
25 public readonly chargingStationTemplates
: Set
<string>
26 protected readonly httpServer
: Server
| Http2Server
27 protected readonly responseHandlers
: Map
<string, ServerResponse
| WebSocket
>
28 protected readonly uiServices
: Map
<ProtocolVersion
, AbstractUIService
>
30 public constructor (protected readonly uiServerConfiguration
: UIServerConfiguration
) {
31 this.chargingStations
= new Map
<string, ChargingStationData
>()
32 this.chargingStationTemplates
= new Set
<string>()
33 switch (this.uiServerConfiguration
.version
) {
34 case ApplicationProtocolVersion
.VERSION_11
:
35 this.httpServer
= new Server()
37 case ApplicationProtocolVersion
.VERSION_20
:
38 this.httpServer
= createServer()
42 `Unsupported application protocol version ${this.uiServerConfiguration.version}`
45 this.responseHandlers
= new Map
<string, ServerResponse
| WebSocket
>()
46 this.uiServices
= new Map
<ProtocolVersion
, AbstractUIService
>()
49 public buildProtocolRequest (
51 procedureName
: ProcedureName
,
52 requestPayload
: RequestPayload
54 return [id
, procedureName
, requestPayload
]
57 public buildProtocolResponse (id
: string, responsePayload
: ResponsePayload
): ProtocolResponse
{
58 return [id
, responsePayload
]
61 public stop (): void {
63 for (const uiService
of this.uiServices
.values()) {
66 this.chargingStations
.clear()
67 this.chargingStationTemplates
.clear()
70 public async sendInternalRequest (request
: ProtocolRequest
): Promise
<ProtocolResponse
> {
71 const protocolVersion
= ProtocolVersion
['0.0.1']
72 this.registerProtocolVersionUIService(protocolVersion
)
73 return await (this.uiServices
75 ?.requestHandler(request
) as Promise
<ProtocolResponse
>)
78 public hasResponseHandler (id
: string): boolean {
79 return this.responseHandlers
.has(id
)
82 protected startHttpServer (): void {
83 if (!this.httpServer
.listening
) {
84 this.httpServer
.listen(this.uiServerConfiguration
.options
)
88 protected registerProtocolVersionUIService (version
: ProtocolVersion
): void {
89 if (!this.uiServices
.has(version
)) {
90 this.uiServices
.set(version
, UIServiceFactory
.getUIServiceImplementation(version
, this))
94 protected authenticate (req
: IncomingMessage
, next
: (err
?: Error) => void): void {
95 const authorizationError
= new BaseError('Unauthorized')
96 if (this.isBasicAuthEnabled()) {
97 if (!this.isValidBasicAuth(req
, next
)) {
98 next(authorizationError
)
101 } else if (this.isProtocolBasicAuthEnabled()) {
102 if (!this.isValidProtocolBasicAuth(req
, next
)) {
103 next(authorizationError
)
106 } else if (this.uiServerConfiguration
.authentication
?.enabled
=== true) {
107 next(authorizationError
)
112 private stopHttpServer (): void {
113 if (this.httpServer
.listening
) {
114 this.httpServer
.close()
118 private isBasicAuthEnabled (): boolean {
120 this.uiServerConfiguration
.authentication
?.enabled
=== true &&
121 this.uiServerConfiguration
.authentication
.type === AuthenticationType
.BASIC_AUTH
125 private isProtocolBasicAuthEnabled (): boolean {
127 this.uiServerConfiguration
.authentication
?.enabled
=== true &&
128 this.uiServerConfiguration
.authentication
.type === AuthenticationType
.PROTOCOL_BASIC_AUTH
132 private isValidBasicAuth (req
: IncomingMessage
, next
: (err
?: Error) => void): boolean {
133 const [username
, password
] = getUsernameAndPasswordFromAuthorizationToken(
134 req
.headers
.authorization
?.split(/\s
+/).pop() ?? '',
137 return this.isValidUsernameAndPassword(username
, password
)
140 private isValidProtocolBasicAuth (req
: IncomingMessage
, next
: (err
?: Error) => void): boolean {
141 const authorizationProtocol
= req
.headers
['sec-websocket-protocol']?.split(/,\s
+/).pop()
142 const [username
, password
] = getUsernameAndPasswordFromAuthorizationToken(
143 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
144 `${authorizationProtocol}${Array(((4 - (authorizationProtocol!.length % 4)) % 4) + 1).join('=')}`
149 return this.isValidUsernameAndPassword(username
, password
)
152 private isValidUsernameAndPassword (username
: string, password
: string): boolean {
154 this.uiServerConfiguration
.authentication
?.username
=== username
&&
155 this.uiServerConfiguration
.authentication
.password
=== password
159 public abstract start (): void
160 public abstract sendRequest (request
: ProtocolRequest
): void
161 public abstract sendResponse (response
: ProtocolResponse
): void
162 public abstract logPrefix (moduleName
?: string, methodName
?: string, prefixSuffix
?: string): string