@@ -20,39 +20,39 @@ import {
2020} from './actions' ;
2121import { ServerUrlResolutionStatus , Server , ServerUrlResolutionResult } from './common' ;
2222
23- export const normalizeServerUrl = ( input : string ) : string => {
24- if ( typeof input !== 'string' ) {
25- throw new TypeError ( 'server URL is not a string' ) ;
26- }
23+ const REQUIRED_SERVER_VERSION_RANGE = '>=2.4.0' ;
2724
28- let parsedUrl : URL ;
25+ export const convertToURL = ( input : string ) : URL => {
26+ let url : URL ;
2927
30- try {
31- parsedUrl = new URL ( input ) ;
32- } catch ( error ) {
33- parsedUrl = new URL ( `https://${ input } ` ) ;
28+ if ( / ^ h t t p s ? : \/ \/ / . test ( input ) ) {
29+ url = new URL ( input ) ;
30+ } else {
31+ url = new URL ( `https://${ input } ` ) ;
3432 }
3533
36- const { protocol, username, password, hostname, port, pathname } = parsedUrl ;
34+ const { protocol, username, password, hostname, port, pathname } = url ;
3735 return Object . assign ( new URL ( 'https://0.0.0.0' ) , {
3836 protocol,
3937 username,
4038 password,
4139 hostname,
42- port,
43- pathname,
44- } ) . href ;
40+ port : ( protocol === 'http' && port === '80' && undefined )
41+ || ( protocol === 'https' && port === '443' && undefined )
42+ || port ,
43+ pathname : / \/ $ / . test ( pathname ) ? pathname : `${ pathname } /` ,
44+ } ) ;
4545} ;
4646
47- export const getServerVersion = async ( serverUrl : string ) : Promise < string > => {
48- const { username, password, href } = new URL ( serverUrl ) ;
47+ const fetchServerInformation = async ( url : URL ) : Promise < [ finalURL : URL , version : string ] > => {
48+ const { username, password } = url ;
4949 const headers : HeadersInit = [ ] ;
5050
5151 if ( username && password ) {
5252 headers . push ( [ 'Authorization' , `Basic ${ Buffer . from ( `${ username } :${ password } ` ) . toString ( 'base64' ) } ` ] ) ;
5353 }
5454
55- const endpoint = new URL ( 'api/info' , href ) ;
55+ const endpoint = new URL ( 'api/info' , url ) ;
5656
5757 const controller = new AbortController ( ) ;
5858
@@ -80,37 +80,43 @@ export const getServerVersion = async (serverUrl: string): Promise<string> => {
8080 throw new Error ( ) ;
8181 }
8282
83- return responseBody . version ;
83+ return [ new URL ( '/' , convertToURL ( response . url ) ) , responseBody . version ] ;
8484} ;
8585
86- export const resolveServerUrl = async ( serverUrl : string ) : Promise < ServerUrlResolutionResult > => {
87- let normalizedServerUrl : string ;
86+ export const resolveServerUrl = async ( input : string ) : Promise < ServerUrlResolutionResult > => {
87+ let url : URL ;
8888
8989 try {
90- normalizedServerUrl = normalizeServerUrl ( serverUrl ) ;
90+ url = convertToURL ( input ) ;
9191 } catch ( error ) {
92- return [ serverUrl , ServerUrlResolutionStatus . INVALID_URL , error ] ;
92+ return [ input , ServerUrlResolutionStatus . INVALID_URL , error ] ;
9393 }
9494
95- try {
96- const version = await getServerVersion ( serverUrl ) ;
95+ let version : string ;
9796
98- if ( ! satisfies ( coerce ( version ) , '>=3.0.x' ) ) {
99- throw new Error ( `incompatible server version (${ version } , expected >=3.0.x)` ) ;
100- }
97+ try {
98+ [ url , version ] = await fetchServerInformation ( url ) ;
10199 } catch ( error ) {
102- if ( ! / ( ^ h t t p s ? : \/ \/ ) | ( \. ) | ( ^ ( [ ^ : ] + : [ ^ @ ] + @ ) ? l o c a l h o s t ( : \d + ) ? $ ) / . test ( serverUrl ) ) {
103- return resolveServerUrl ( `https://${ serverUrl } .rocket.chat` ) ;
100+ if ( ! / ( ^ h t t p s ? : \/ \/ ) | ( \. ) | ( ^ ( [ ^ : ] + : [ ^ @ ] + @ ) ? l o c a l h o s t ( : \d + ) ? $ ) / . test ( input ) ) {
101+ return resolveServerUrl ( `https://${ input } .rocket.chat` ) ;
104102 }
105103
106104 if ( error . name === 'AbortError' ) {
107- return [ normalizedServerUrl , ServerUrlResolutionStatus . TIMEOUT , error ] ;
105+ return [ url . href , ServerUrlResolutionStatus . TIMEOUT , error ] ;
108106 }
109107
110- return [ normalizedServerUrl , ServerUrlResolutionStatus . INVALID , error ] ;
108+ return [ url . href , ServerUrlResolutionStatus . INVALID , error ] ;
109+ }
110+
111+ if ( ! satisfies ( coerce ( version ) , REQUIRED_SERVER_VERSION_RANGE ) ) {
112+ return [
113+ url . href ,
114+ ServerUrlResolutionStatus . INVALID ,
115+ new Error ( `incompatible server version (${ version } , expected ${ REQUIRED_SERVER_VERSION_RANGE } )` ) ,
116+ ] ;
111117 }
112118
113- return [ normalizedServerUrl , ServerUrlResolutionStatus . OK ] ;
119+ return [ url . href , ServerUrlResolutionStatus . OK ] ;
114120} ;
115121
116122const loadAppServers = async ( ) : Promise < Record < string , string > > => {
0 commit comments