export function parseUrl()

in tools/awps-tunnel/server/util.ts [33:52]


export function parseUrl(url: string, optionalScheme: false | "http" | "https" = false): URL | undefined {
  // only http and https schemes are supported
  const regex = /^(http|https):\/\/[^ "]+$/i;
  try {
    if (regex.test(url)) {
      return new URL(url);
    } else {
      if (optionalScheme) {
        // check if it is the ommitted scheme case
        if (!url.includes("://")) {
          return parseUrl(`${optionalScheme}://${url}`);
        }
      }

      return undefined;
    }
  } catch {
    return undefined;
  }
}