Commit | Line | Data |
---|---|---|
8114d10e JB |
1 | import chalk from 'chalk'; |
2 | ||
4c3c0d59 JB |
3 | import type { AbstractUIServer } from './AbstractUIServer'; |
4 | import { UIHttpServer } from './UIHttpServer'; | |
5 | import { UIServerUtils } from './UIServerUtils'; | |
6 | import { UIWebSocketServer } from './UIWebSocketServer'; | |
a6080904 JB |
7 | import { |
8 | ApplicationProtocol, | |
9 | ApplicationProtocolVersion, | |
10 | type UIServerConfiguration, | |
11 | } from '../../types'; | |
fe94fce0 | 12 | |
268a74bb | 13 | export class UIServerFactory { |
fe94fce0 JB |
14 | private constructor() { |
15 | // This is intentional | |
16 | } | |
17 | ||
18 | public static getUIServerImplementation( | |
864e5f8d | 19 | uiServerConfiguration: UIServerConfiguration, |
fe94fce0 | 20 | ): AbstractUIServer | null { |
864e5f8d | 21 | if (UIServerUtils.isLoopback(uiServerConfiguration.options!.host!) === false) { |
d5bd1c00 | 22 | console.warn( |
9c5d9fa4 | 23 | chalk.yellow( |
5edd8ba0 JB |
24 | 'Loopback address not detected in UI server configuration. This is not recommended.', |
25 | ), | |
d5bd1c00 JB |
26 | ); |
27 | } | |
a6080904 | 28 | uiServerConfiguration = { |
7aba23e1 | 29 | version: ApplicationProtocolVersion.VERSION_11, |
a6080904 JB |
30 | ...uiServerConfiguration, |
31 | }; | |
7aba23e1 JB |
32 | if ( |
33 | uiServerConfiguration.type === ApplicationProtocol.WS && | |
34 | uiServerConfiguration.version !== ApplicationProtocolVersion.VERSION_11 | |
35 | ) { | |
36 | console.warn( | |
37 | chalk.yellow( | |
38 | `Only version ${ApplicationProtocolVersion.VERSION_11} is supported for WebSocket UI server. Falling back to version ${ApplicationProtocolVersion.VERSION_11}.`, | |
39 | ), | |
40 | ); | |
41 | uiServerConfiguration.version = ApplicationProtocolVersion.VERSION_11; | |
42 | } | |
864e5f8d | 43 | switch (uiServerConfiguration.type) { |
fe94fce0 | 44 | case ApplicationProtocol.WS: |
864e5f8d | 45 | return new UIWebSocketServer(uiServerConfiguration); |
1f7fa4de | 46 | case ApplicationProtocol.HTTP: |
864e5f8d | 47 | return new UIHttpServer(uiServerConfiguration); |
fe94fce0 JB |
48 | default: |
49 | return null; | |
50 | } | |
51 | } | |
52 | } |