import { URL } from 'url' ; internal export function replaceSubdomain ( issuerUrl , xsuaaUrl ) { if ( ! isValidURL ( issuerUrl ) ) { throw new Error ( 'JWT issuer URL is not valid "' + issuerUrl + '".' ) ; } if ( ! isValidURL ( xsuaaUrl ) ) { throw new Error ( 'XSUAA URL is not valid "' + xsuaaUrl + '".' ) ; } const subdomain = parseSubdomain ( issuerUrl ) ; return replace ( xsuaaUrl , subdomain ) ; } function replace ( xsuaaUrl , subdomain ) { const parsedXsuaaUrl = new URL ( xsuaaUrl ) ; const scheme = parsedXsuaaUrl . protocol + '//' ; const xsuaaDomain = xsuaaUrl . slice ( xsuaaUrl . indexOf ( '.' ) ) ; return scheme + subdomain + xsuaaDomain ; } internal export function parseSubdomain ( issuerUrl ) { const url = new URL ( issuerUrl ) ; const host = url . host ; if ( ! host || host . indexOf ( '.' ) === - 1 ) { throw new Error ( 'Failed to determine sub-domain: invalid host in "' + issuerUrl + '".' ) ; } return host . split ( '.' ) [ 0 ] ; } function isValidURL ( url ) { try { new URL ( url ) ; return true ; } catch ( _ ) { return false ; } }