2022-01-13 05:45:50 +00:00
|
|
|
import is from '@sindresorhus/is';
|
|
|
|
// eslint-disable-next-line no-restricted-imports
|
|
|
|
import _parseLinkHeader from 'parse-link-header';
|
2020-12-09 12:05:31 +00:00
|
|
|
import urlJoin from 'url-join';
|
2022-01-13 05:45:50 +00:00
|
|
|
import { logger } from '../logger';
|
2021-10-27 14:37:11 +00:00
|
|
|
import { regEx } from './regex';
|
2020-12-09 12:05:31 +00:00
|
|
|
|
2021-08-30 16:28:32 +00:00
|
|
|
export function joinUrlParts(...parts: string[]): string {
|
|
|
|
return urlJoin(...parts);
|
|
|
|
}
|
|
|
|
|
2021-07-28 06:07:20 +00:00
|
|
|
export function ensurePathPrefix(url: string, prefix: string): string {
|
|
|
|
const parsed = new URL(url);
|
2021-09-24 12:13:54 +00:00
|
|
|
const fullPath = parsed.pathname + parsed.search;
|
2021-07-28 06:07:20 +00:00
|
|
|
if (fullPath.startsWith(prefix)) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
return parsed.origin + prefix + fullPath;
|
|
|
|
}
|
|
|
|
|
2020-04-14 05:05:30 +00:00
|
|
|
export function ensureTrailingSlash(url: string): string {
|
2021-12-06 15:05:37 +00:00
|
|
|
return url.replace(/\/?$/, '/'); // TODO #12875 adds slash at the front when re2 is used
|
2020-04-14 05:05:30 +00:00
|
|
|
}
|
2020-12-09 12:05:31 +00:00
|
|
|
|
2021-03-08 13:12:19 +00:00
|
|
|
export function trimTrailingSlash(url: string): string {
|
2021-11-29 19:16:05 +00:00
|
|
|
return url.replace(regEx(/\/+$/), '');
|
2021-03-08 13:12:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 08:16:46 +00:00
|
|
|
export function trimLeadingSlash(path: string): string {
|
2022-08-14 15:57:37 +00:00
|
|
|
return path.replace(/^\/+/, '');
|
2022-04-08 08:16:46 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 12:42:25 +00:00
|
|
|
export function trimSlashes(path: string): string {
|
|
|
|
return trimLeadingSlash(trimTrailingSlash(path));
|
|
|
|
}
|
|
|
|
|
2023-03-02 14:19:55 +00:00
|
|
|
/**
|
|
|
|
* Resolves an input path against a base URL
|
|
|
|
*
|
|
|
|
* @param baseUrl - base URL to resolve against
|
|
|
|
* @param input - input path (if this is a full URL, it will be returned)
|
|
|
|
*/
|
2020-12-09 12:05:31 +00:00
|
|
|
export function resolveBaseUrl(baseUrl: string, input: string | URL): string {
|
|
|
|
const inputString = input.toString();
|
|
|
|
|
|
|
|
let host;
|
|
|
|
let pathname;
|
|
|
|
try {
|
|
|
|
({ host, pathname } = new URL(inputString));
|
|
|
|
} catch (e) {
|
|
|
|
pathname = inputString;
|
|
|
|
}
|
|
|
|
|
|
|
|
return host ? inputString : urlJoin(baseUrl, pathname || '');
|
|
|
|
}
|
2021-02-24 08:58:21 +00:00
|
|
|
|
2023-03-02 14:19:55 +00:00
|
|
|
/**
|
|
|
|
* Replaces the path of a URL with a new path
|
|
|
|
*
|
|
|
|
* @param baseUrl - source URL
|
|
|
|
* @param path - replacement path (if this is a full URL, it will be returned)
|
|
|
|
*/
|
|
|
|
export function replaceUrlPath(baseUrl: string | URL, path: string): string {
|
|
|
|
if (parseUrl(path)) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { origin } = is.string(baseUrl) ? new URL(baseUrl) : baseUrl;
|
|
|
|
return urlJoin(origin, path);
|
|
|
|
}
|
|
|
|
|
2021-02-24 08:58:21 +00:00
|
|
|
export function getQueryString(params: Record<string, any>): string {
|
|
|
|
const usp = new URLSearchParams();
|
|
|
|
for (const [k, v] of Object.entries(params)) {
|
2023-03-12 05:53:06 +00:00
|
|
|
if (is.array<object>(v)) {
|
2021-02-24 08:58:21 +00:00
|
|
|
for (const item of v) {
|
2023-08-02 15:07:49 +00:00
|
|
|
// TODO: fix me?
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
2021-02-24 08:58:21 +00:00
|
|
|
usp.append(k, item.toString());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
usp.append(k, v.toString());
|
|
|
|
}
|
|
|
|
}
|
2023-03-12 05:53:06 +00:00
|
|
|
return usp.toString();
|
2021-02-24 08:58:21 +00:00
|
|
|
}
|
2021-03-04 05:40:08 +00:00
|
|
|
|
2023-10-04 04:48:58 +00:00
|
|
|
export function validateUrl(
|
|
|
|
url: string | null | undefined,
|
2023-11-07 15:50:29 +00:00
|
|
|
httpOnly = true,
|
2023-10-04 04:48:58 +00:00
|
|
|
): boolean {
|
|
|
|
if (!is.nonEmptyString(url)) {
|
2021-03-04 05:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const { protocol } = new URL(url);
|
|
|
|
return httpOnly ? !!protocol.startsWith('http') : !!protocol;
|
|
|
|
} catch (err) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-03-13 08:41:51 +00:00
|
|
|
|
2022-02-07 06:14:23 +00:00
|
|
|
export function parseUrl(url: string | undefined | null): URL | null {
|
|
|
|
if (!url) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:41:51 +00:00
|
|
|
try {
|
|
|
|
return new URL(url);
|
|
|
|
} catch (err) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2021-10-31 07:06:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to create an URL object from either a full URL string or a hostname
|
|
|
|
* @param url either the full url or a hostname
|
|
|
|
* @returns an URL object or null
|
|
|
|
*/
|
|
|
|
export function createURLFromHostOrURL(url: string): URL | null {
|
2021-11-28 15:31:11 +00:00
|
|
|
return parseUrl(url) ?? parseUrl(`https://${url}`);
|
2021-10-31 07:06:59 +00:00
|
|
|
}
|
2022-01-13 05:45:50 +00:00
|
|
|
|
|
|
|
export type LinkHeaderLinks = _parseLinkHeader.Links;
|
|
|
|
|
|
|
|
export function parseLinkHeader(
|
2023-11-07 15:50:29 +00:00
|
|
|
linkHeader: string | null | undefined,
|
2022-01-13 05:45:50 +00:00
|
|
|
): LinkHeaderLinks | null {
|
|
|
|
if (!is.nonEmptyString(linkHeader)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (linkHeader.length > 2000) {
|
|
|
|
logger.warn({ linkHeader }, 'Link header too long.');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return _parseLinkHeader(linkHeader);
|
|
|
|
}
|