Commit | Line | Data |
---|---|---|
66a7748d | 1 | import chalk from 'chalk' |
8114d10e | 2 | |
66a7748d JB |
3 | import type { AbstractUIServer } from './AbstractUIServer.js' |
4 | import { UIHttpServer } from './UIHttpServer.js' | |
75adc3d8 | 5 | import { isLoopback } from './UIServerUtils.js' |
66a7748d | 6 | import { UIWebSocketServer } from './UIWebSocketServer.js' |
53a9f111 | 7 | import { BaseError } from '../../exception/index.js' |
a6080904 JB |
8 | import { |
9 | ApplicationProtocol, | |
10 | ApplicationProtocolVersion, | |
329eab0e | 11 | AuthenticationType, |
66a7748d JB |
12 | type UIServerConfiguration |
13 | } from '../../types/index.js' | |
fe94fce0 | 14 | |
66a7748d | 15 | // eslint-disable-next-line @typescript-eslint/no-extraneous-class |
268a74bb | 16 | export class UIServerFactory { |
66a7748d | 17 | private constructor () { |
fe94fce0 JB |
18 | // This is intentional |
19 | } | |
20 | ||
66a7748d JB |
21 | public static getUIServerImplementation ( |
22 | uiServerConfiguration: UIServerConfiguration | |
6d2b7d01 | 23 | ): AbstractUIServer | undefined { |
329eab0e JB |
24 | if ( |
25 | uiServerConfiguration.authentication?.enabled === true && | |
26 | !Object.values(AuthenticationType).includes(uiServerConfiguration.authentication.type) | |
27 | ) { | |
28 | throw new BaseError( | |
29 | `Unknown authentication type '${uiServerConfiguration.authentication.type}' for UI server` | |
30 | ) | |
31 | } | |
32 | if ( | |
33 | uiServerConfiguration.type === ApplicationProtocol.HTTP && | |
34 | uiServerConfiguration.authentication?.enabled === true && | |
35 | uiServerConfiguration.authentication.type === AuthenticationType.PROTOCOL_BASIC_AUTH | |
36 | ) { | |
37 | throw new BaseError('Protocol basic authentication is not supported for HTTP UI server') | |
38 | } | |
b35a06e0 JB |
39 | if ( |
40 | uiServerConfiguration.authentication?.enabled !== true && | |
41 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
75adc3d8 | 42 | !isLoopback(uiServerConfiguration.options!.host!) |
b35a06e0 | 43 | ) { |
d5bd1c00 | 44 | console.warn( |
9c5d9fa4 | 45 | chalk.yellow( |
b35a06e0 | 46 | 'Non loopback address in UI server configuration without authentication enabled. This is not recommended' |
66a7748d JB |
47 | ) |
48 | ) | |
d5bd1c00 | 49 | } |
7aba23e1 JB |
50 | if ( |
51 | uiServerConfiguration.type === ApplicationProtocol.WS && | |
52 | uiServerConfiguration.version !== ApplicationProtocolVersion.VERSION_11 | |
53 | ) { | |
54 | console.warn( | |
55 | chalk.yellow( | |
412cece8 | 56 | `Only version ${ApplicationProtocolVersion.VERSION_11} is supported for WebSocket UI server. Falling back to version ${ApplicationProtocolVersion.VERSION_11}` |
66a7748d JB |
57 | ) |
58 | ) | |
59 | uiServerConfiguration.version = ApplicationProtocolVersion.VERSION_11 | |
7aba23e1 | 60 | } |
864e5f8d | 61 | switch (uiServerConfiguration.type) { |
fe94fce0 | 62 | case ApplicationProtocol.WS: |
66a7748d | 63 | return new UIWebSocketServer(uiServerConfiguration) |
1f7fa4de | 64 | case ApplicationProtocol.HTTP: |
66a7748d | 65 | return new UIHttpServer(uiServerConfiguration) |
fe94fce0 JB |
66 | } |
67 | } | |
68 | } |