/
githubmirror
/
nativefier
Обзор
Документация
Войти
/
githubmirror
/
nativefier
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/options/normalizeUrl.ts
32 строки
961 B
Adam Weeden
Enable TypeScript strict:true, and more typescript-eslint rules (#1223)
16 июн 2021, 05:20
Не верифицирован
16 июн 2021, 05:20
7a08a2d
Код
Авторство
О чём код?
import * as url from 'url'; import * as log from 'loglevel'; function appendProtocol(inputUrl: string): string { const parsed = url.parse(inputUrl); if (!parsed.protocol) { const urlWithProtocol = `https://${inputUrl}`; log.warn( `URL "${inputUrl}" lacks a protocol.`, `Will try to parse it as HTTPS: "${urlWithProtocol}".`, `Please pass "http://${inputUrl}" if this is what you meant.`, ); return urlWithProtocol; } return inputUrl; } export function normalizeUrl(urlToNormalize: string): string { const urlWithProtocol = appendProtocol(urlToNormalize); let parsedUrl: url.URL; try { parsedUrl = new url.URL(urlWithProtocol); } catch (err: unknown) { log.error('normalizeUrl ERROR', err); throw new Error(`Your url "${urlWithProtocol}" is invalid`); } const normalizedUrl = parsedUrl.toString(); log.debug(`Normalized URL ${urlToNormalize} to:`, normalizedUrl); return normalizedUrl; }