(input)
| 16 | } |
| 17 | |
| 18 | export function parseServerAddress(input) { |
| 19 | const raw = input.trim(); |
| 20 | if (!raw) return { ok: false, error: 'Please enter a server address' }; |
| 21 | |
| 22 | const hasScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(raw); |
| 23 | let url; |
| 24 | try { |
| 25 | url = new URL(hasScheme ? raw : `ws://${raw}`); |
| 26 | } catch { |
| 27 | return { ok: false, error: 'Invalid address' }; |
| 28 | } |
| 29 | |
| 30 | const protocol = url.protocol.toLowerCase(); |
| 31 | let wsProtocol; |
| 32 | if (protocol === 'ws:') wsProtocol = 'ws:'; |
| 33 | else if (protocol === 'wss:') wsProtocol = 'wss:'; |
| 34 | else if (protocol === 'http:') wsProtocol = 'ws:'; |
| 35 | else if (protocol === 'https:') wsProtocol = 'wss:'; |
| 36 | else return { ok: false, error: 'Use ws://, wss://, http://, https://, or host:port' }; |
| 37 | |
| 38 | if (!url.hostname) return { ok: false, error: 'Invalid address' }; |
| 39 | if (!hasScheme && !url.port) url.port = '3100'; |
| 40 | |
| 41 | const wsUrl = new URL(url.toString()); |
| 42 | wsUrl.protocol = wsProtocol; |
| 43 | |
| 44 | return { |
| 45 | ok: true, |
| 46 | displayAddr: formatUrlForDisplay(url, hasScheme), |
| 47 | wsUrl: wsUrl.toString(), |
| 48 | cacheAddr: wsUrl.toString(), |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | export function shortenPath(p) { |
| 53 | if (!p) return ''; |
no test coverage detected