* @param {string|URL|Record } url * @returns {URL}
(url)
| 159 | * @returns {URL} |
| 160 | */ |
| 161 | function parseURL (url) { |
| 162 | if (typeof url === 'string') { |
| 163 | /** |
| 164 | * @type {URL} |
| 165 | */ |
| 166 | url = new URL(url) |
| 167 | |
| 168 | if (!isHttpOrHttpsPrefixed(url.origin || url.protocol)) { |
| 169 | throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.') |
| 170 | } |
| 171 | |
| 172 | return url |
| 173 | } |
| 174 | |
| 175 | if (!url || typeof url !== 'object') { |
| 176 | throw new InvalidArgumentError('Invalid URL: The URL argument must be a non-null object.') |
| 177 | } |
| 178 | |
| 179 | if (!(url instanceof URL)) { |
| 180 | if (url.port != null && url.port !== '' && isValidPort(url.port) === false) { |
| 181 | throw new InvalidArgumentError('Invalid URL: port must be a valid integer or a string representation of an integer.') |
| 182 | } |
| 183 | |
| 184 | if (url.path != null && typeof url.path !== 'string') { |
| 185 | throw new InvalidArgumentError('Invalid URL path: the path must be a string or null/undefined.') |
| 186 | } |
| 187 | |
| 188 | if (url.pathname != null && typeof url.pathname !== 'string') { |
| 189 | throw new InvalidArgumentError('Invalid URL pathname: the pathname must be a string or null/undefined.') |
| 190 | } |
| 191 | |
| 192 | if (url.hostname != null && typeof url.hostname !== 'string') { |
| 193 | throw new InvalidArgumentError('Invalid URL hostname: the hostname must be a string or null/undefined.') |
| 194 | } |
| 195 | |
| 196 | if (url.origin != null && typeof url.origin !== 'string') { |
| 197 | throw new InvalidArgumentError('Invalid URL origin: the origin must be a string or null/undefined.') |
| 198 | } |
| 199 | |
| 200 | if (!isHttpOrHttpsPrefixed(url.origin || url.protocol)) { |
| 201 | throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.') |
| 202 | } |
| 203 | |
| 204 | const port = url.port != null |
| 205 | ? url.port |
| 206 | : (url.protocol === 'https:' ? 443 : 80) |
| 207 | let origin = url.origin != null |
| 208 | ? url.origin |
| 209 | : `${url.protocol || ''}//${url.hostname || ''}:${port}` |
| 210 | let path = url.path != null |
| 211 | ? url.path |
| 212 | : `${url.pathname || ''}${url.search || ''}` |
| 213 | |
| 214 | if (origin[origin.length - 1] === '/') { |
| 215 | origin = origin.slice(0, origin.length - 1) |
| 216 | } |
| 217 | |
| 218 | if (path && path[0] !== '/') { |
no test coverage detected