| 1 | import chalk from 'chalk'; |
| 2 | |
| 3 | import type { AbstractUIServer } from './AbstractUIServer'; |
| 4 | import { UIHttpServer } from './UIHttpServer'; |
| 5 | import { UIServerUtils } from './UIServerUtils'; |
| 6 | import { UIWebSocketServer } from './UIWebSocketServer'; |
| 7 | import { ApplicationProtocol, type UIServerConfiguration } from '../../types'; |
| 8 | import { Configuration } from '../../utils'; |
| 9 | |
| 10 | export class UIServerFactory { |
| 11 | private constructor() { |
| 12 | // This is intentional |
| 13 | } |
| 14 | |
| 15 | public static getUIServerImplementation( |
| 16 | uiServerConfiguration?: UIServerConfiguration |
| 17 | ): AbstractUIServer | null { |
| 18 | if (UIServerUtils.isLoopback(uiServerConfiguration.options?.host) === false) { |
| 19 | console.warn( |
| 20 | chalk.yellow( |
| 21 | 'Loopback address not detected in UI server configuration. This is not recommended.' |
| 22 | ) |
| 23 | ); |
| 24 | } |
| 25 | switch (uiServerConfiguration?.type ?? Configuration.getUIServer().type) { |
| 26 | case ApplicationProtocol.WS: |
| 27 | return new UIWebSocketServer(uiServerConfiguration ?? Configuration.getUIServer()); |
| 28 | case ApplicationProtocol.HTTP: |
| 29 | return new UIHttpServer(uiServerConfiguration ?? Configuration.getUIServer()); |
| 30 | default: |
| 31 | return null; |
| 32 | } |
| 33 | } |
| 34 | } |