normalize-url
Advanced tools
Comparing version 7.2.0 to 8.0.0
@@ -1,8 +0,6 @@ | ||
export interface Options { | ||
export type Options = { | ||
/** | ||
@default 'http:' | ||
Values: `'https:' | 'http:'` | ||
@default 'http' | ||
*/ | ||
readonly defaultProtocol?: string; // TODO: Make this `'https:' | 'http:'` in the next major version. | ||
readonly defaultProtocol?: 'https' | 'http'; | ||
@@ -26,3 +24,3 @@ /** | ||
/** | ||
Normalizes `https:` URLs to `http:`. | ||
Normalizes HTTPS URLs to HTTP. | ||
@@ -43,5 +41,5 @@ @default false | ||
/** | ||
Normalizes `http:` URLs to `https:`. | ||
Normalizes HTTP URLs to HTTPS. | ||
This option can't be used with the `forceHttp` option at the same time. | ||
This option cannot be used with the `forceHttp` option at the same time. | ||
@@ -285,3 +283,3 @@ @default false | ||
readonly sortQueryParameters?: boolean; | ||
} | ||
}; | ||
@@ -291,2 +289,4 @@ /** | ||
URLs with custom protocols are not normalized and just passed through by default. Supported protocols are: `https`, `http`, `file`, and `data`. | ||
@param url - URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs). | ||
@@ -293,0 +293,0 @@ |
28
index.js
@@ -7,2 +7,17 @@ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | ||
const supportedProtocols = new Set([ | ||
'https:', | ||
'http:', | ||
'file:', | ||
]); | ||
const hasCustomProtocol = urlString => { | ||
try { | ||
const {protocol} = new URL(urlString); | ||
return protocol.endsWith(':') && !supportedProtocols.has(protocol); | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
const normalizeDataURL = (urlString, {stripHash}) => { | ||
@@ -26,3 +41,3 @@ const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(urlString); | ||
// Lowercase MIME type | ||
const mimeType = (mediaType.shift() || '').toLowerCase(); | ||
const mimeType = mediaType.shift()?.toLowerCase() ?? ''; | ||
const attributes = mediaType | ||
@@ -62,3 +77,3 @@ .map(attribute => { | ||
options = { | ||
defaultProtocol: 'http:', | ||
defaultProtocol: 'http', | ||
normalizeProtocol: true, | ||
@@ -80,2 +95,7 @@ forceHttp: false, | ||
// Legacy: Append `:` to the protocol if missing. | ||
if (typeof options.defaultProtocol === 'string' && !options.defaultProtocol.endsWith(':')) { | ||
options.defaultProtocol = `${options.defaultProtocol}:`; | ||
} | ||
urlString = urlString.trim(); | ||
@@ -88,4 +108,4 @@ | ||
if (/^view-source:/i.test(urlString)) { | ||
throw new Error('`view-source:` is not supported as it is a non-standard protocol'); | ||
if (hasCustomProtocol(urlString)) { | ||
return urlString; | ||
} | ||
@@ -92,0 +112,0 @@ |
{ | ||
"name": "normalize-url", | ||
"version": "7.2.0", | ||
"version": "8.0.0", | ||
"description": "Normalize a URL", | ||
@@ -14,5 +14,8 @@ "license": "MIT", | ||
"type": "module", | ||
"exports": "./index.js", | ||
"exports": { | ||
"types": "./index.d.ts", | ||
"default": "./index.js" | ||
}, | ||
"engines": { | ||
"node": ">=12.20" | ||
"node": ">=14.16" | ||
}, | ||
@@ -42,6 +45,6 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^4.0.1", | ||
"c8": "^7.11.0", | ||
"tsd": "^0.19.1", | ||
"xo": "^0.47.0" | ||
"ava": "^5.0.1", | ||
"c8": "^7.12.0", | ||
"tsd": "^0.24.1", | ||
"xo": "^0.52.4" | ||
}, | ||
@@ -48,0 +51,0 @@ "c8": { |
@@ -33,2 +33,4 @@ # normalize-url [![Coverage Status](https://codecov.io/gh/sindresorhus/normalize-url/branch/main/graph/badge.svg)](https://codecov.io/gh/sindresorhus/normalize-url) | ||
URLs with custom protocols are not normalized and just passed through by default. Supported protocols are: `https`, `http`, `file`, and `data`. | ||
#### url | ||
@@ -47,4 +49,4 @@ | ||
Type: `string`\ | ||
Default: `http:`\ | ||
Values: `'https:' | 'http:'` | ||
Default: `'http'`\ | ||
Values: `'https' | 'http'` | ||
@@ -71,3 +73,3 @@ ##### normalizeProtocol | ||
Normalize `https:` to `http:`. | ||
Normalize HTTPS to HTTP. | ||
@@ -87,3 +89,3 @@ ```js | ||
Normalize `http:` to `https:`. | ||
Normalize HTTP to HTTPS. | ||
@@ -98,3 +100,3 @@ ```js | ||
This option can't be used with the `forceHttp` option at the same time. | ||
This option cannot be used with the `forceHttp` option at the same time. | ||
@@ -101,0 +103,0 @@ ##### stripAuthentication |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25662
454
325