UNPKG

@jellyfin/sdk

Version:
64 lines (61 loc) 2.13 kB
import { HTTP_PROTOCOL, HTTP_PORT, HTTPS_PROTOCOL, HTTPS_PORT } from './constants.js'; import normalizeUrl from './normalize-url.js'; /** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /** * Copies a URL instance. * @param original The original URL. * @returns A copy of the original URL. */ function copyUrl(original) { return new URL(original.toString()); } /** * Returns the default port number for a given protocol. * @param protocol The protocol string. * @returns The default port number of the protocol. */ function getDefaultPort(protocol) { if (protocol === HTTP_PROTOCOL) return HTTP_PORT; if (protocol === HTTPS_PROTOCOL) return HTTPS_PORT; throw new Error('Unsupported protocol'); } /** * Checks if the URL string specifies the protocol and port. * @param urlString The URL string to test. * @param url The parsed URL object. * @returns True if the the URL string includes the protocol and port. */ function hasProtocolAndPort(urlString, url) { // The parsed URL object drops the default port const port = url.port || getDefaultPort(url.protocol); return urlString .toLowerCase() .startsWith(`${url.protocol}//${url.hostname.toLowerCase()}:${port}`); } /** * Checks if the URL is using the protocol's default port. * @param url The URL object to check. * @returns True if the URL port is the protocol default. */ function isDefaultPort(url) { return !url.port || (url.port === getDefaultPort(url.protocol).toString()); } /** * Parses a string to a URL object. * @param input A string representing a URL. * @returns The URL object. */ function parseUrl(input) { if (!(input === null || input === void 0 ? void 0 : input.trim())) { throw new Error('Input is required'); } return new URL(normalizeUrl(input)); } export { HTTPS_PORT, HTTPS_PROTOCOL, HTTP_PORT, HTTP_PROTOCOL, copyUrl, getDefaultPort, hasProtocolAndPort, isDefaultPort, parseUrl };