normalize-url 7.2.0 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,10 +1,8 @@
1
- export interface Options {
1
+ export type Options = {
2
2
  /**
3
- @default 'http:'
4
-
5
- Values: `'https:' | 'http:'`
3
+ @default 'http'
6
4
  */
7
- readonly defaultProtocol?: string; // TODO: Make this `'https:' | 'http:'` in the next major version.
5
+ readonly defaultProtocol?: 'https' | 'http';
8
6
 
9
7
  /**
10
8
  Prepends `defaultProtocol` to the URL if it's protocol-relative.
@@ -23,7 +21,7 @@ export interface Options {
23
21
  readonly normalizeProtocol?: boolean;
24
22
 
25
23
  /**
26
- Normalizes `https:` URLs to `http:`.
24
+ Normalizes HTTPS URLs to HTTP.
27
25
 
28
26
  @default false
29
27
 
@@ -39,9 +37,9 @@ export interface Options {
39
37
  readonly forceHttp?: boolean;
40
38
 
41
39
  /**
42
- Normalizes `http:` URLs to `https:`.
40
+ Normalizes HTTP URLs to HTTPS.
43
41
 
44
- This option can't be used with the `forceHttp` option at the same time.
42
+ This option cannot be used with the `forceHttp` option at the same time.
45
43
 
46
44
  @default false
47
45
 
@@ -280,11 +278,13 @@ export interface Options {
280
278
  ```
281
279
  */
282
280
  readonly sortQueryParameters?: boolean;
283
- }
281
+ };
284
282
 
285
283
  /**
286
284
  [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL.
287
285
 
286
+ URLs with custom protocols are not normalized and just passed through by default. Supported protocols are: `https`, `http`, `file`, and `data`.
287
+
288
288
  @param url - URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).
289
289
 
290
290
  @example
package/index.js CHANGED
@@ -4,6 +4,21 @@ const DATA_URL_DEFAULT_CHARSET = 'us-ascii';
4
4
 
5
5
  const testParameter = (name, filters) => filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);
6
6
 
7
+ const supportedProtocols = new Set([
8
+ 'https:',
9
+ 'http:',
10
+ 'file:',
11
+ ]);
12
+
13
+ const hasCustomProtocol = urlString => {
14
+ try {
15
+ const {protocol} = new URL(urlString);
16
+ return protocol.endsWith(':') && !supportedProtocols.has(protocol);
17
+ } catch {
18
+ return false;
19
+ }
20
+ };
21
+
7
22
  const normalizeDataURL = (urlString, {stripHash}) => {
8
23
  const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(urlString);
9
24
 
@@ -22,7 +37,7 @@ const normalizeDataURL = (urlString, {stripHash}) => {
22
37
  }
23
38
 
24
39
  // Lowercase MIME type
25
- const mimeType = (mediaType.shift() || '').toLowerCase();
40
+ const mimeType = mediaType.shift()?.toLowerCase() ?? '';
26
41
  const attributes = mediaType
27
42
  .map(attribute => {
28
43
  let [key, value = ''] = attribute.split('=').map(string => string.trim());
@@ -57,7 +72,7 @@ const normalizeDataURL = (urlString, {stripHash}) => {
57
72
 
58
73
  export default function normalizeUrl(urlString, options) {
59
74
  options = {
60
- defaultProtocol: 'http:',
75
+ defaultProtocol: 'http',
61
76
  normalizeProtocol: true,
62
77
  forceHttp: false,
63
78
  forceHttps: false,
@@ -74,6 +89,11 @@ export default function normalizeUrl(urlString, options) {
74
89
  ...options,
75
90
  };
76
91
 
92
+ // Legacy: Append `:` to the protocol if missing.
93
+ if (typeof options.defaultProtocol === 'string' && !options.defaultProtocol.endsWith(':')) {
94
+ options.defaultProtocol = `${options.defaultProtocol}:`;
95
+ }
96
+
77
97
  urlString = urlString.trim();
78
98
 
79
99
  // Data URL
@@ -81,8 +101,8 @@ export default function normalizeUrl(urlString, options) {
81
101
  return normalizeDataURL(urlString, options);
82
102
  }
83
103
 
84
- if (/^view-source:/i.test(urlString)) {
85
- throw new Error('`view-source:` is not supported as it is a non-standard protocol');
104
+ if (hasCustomProtocol(urlString)) {
105
+ return urlString;
86
106
  }
87
107
 
88
108
  const hasRelativeProtocol = urlString.startsWith('//');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "normalize-url",
3
- "version": "7.2.0",
3
+ "version": "8.0.0",
4
4
  "description": "Normalize a URL",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/normalize-url",
@@ -11,9 +11,12 @@
11
11
  "url": "https://sindresorhus.com"
12
12
  },
13
13
  "type": "module",
14
- "exports": "./index.js",
14
+ "exports": {
15
+ "types": "./index.d.ts",
16
+ "default": "./index.js"
17
+ },
15
18
  "engines": {
16
- "node": ">=12.20"
19
+ "node": ">=14.16"
17
20
  },
18
21
  "scripts": {
19
22
  "test": "xo && c8 ava && tsd"
@@ -38,10 +41,10 @@
38
41
  "canonical"
39
42
  ],
40
43
  "devDependencies": {
41
- "ava": "^4.0.1",
42
- "c8": "^7.11.0",
43
- "tsd": "^0.19.1",
44
- "xo": "^0.47.0"
44
+ "ava": "^5.0.1",
45
+ "c8": "^7.12.0",
46
+ "tsd": "^0.24.1",
47
+ "xo": "^0.52.4"
45
48
  },
46
49
  "c8": {
47
50
  "reporter": [
package/readme.md CHANGED
@@ -30,6 +30,8 @@ normalizeUrl('//www.sindresorhus.com:80/../baz?b=bar&a=foo');
30
30
 
31
31
  ### normalizeUrl(url, options?)
32
32
 
33
+ URLs with custom protocols are not normalized and just passed through by default. Supported protocols are: `https`, `http`, `file`, and `data`.
34
+
33
35
  #### url
34
36
 
35
37
  Type: `string`
@@ -43,8 +45,8 @@ Type: `object`
43
45
  ##### defaultProtocol
44
46
 
45
47
  Type: `string`\
46
- Default: `http:`\
47
- Values: `'https:' | 'http:'`
48
+ Default: `'http'`\
49
+ Values: `'https' | 'http'`
48
50
 
49
51
  ##### normalizeProtocol
50
52
 
@@ -66,7 +68,7 @@ normalizeUrl('//sindresorhus.com', {normalizeProtocol: false});
66
68
  Type: `boolean`\
67
69
  Default: `false`
68
70
 
69
- Normalize `https:` to `http:`.
71
+ Normalize HTTPS to HTTP.
70
72
 
71
73
  ```js
72
74
  normalizeUrl('https://sindresorhus.com');
@@ -81,7 +83,7 @@ normalizeUrl('https://sindresorhus.com', {forceHttp: true});
81
83
  Type: `boolean`\
82
84
  Default: `false`
83
85
 
84
- Normalize `http:` to `https:`.
86
+ Normalize HTTP to HTTPS.
85
87
 
86
88
  ```js
87
89
  normalizeUrl('http://sindresorhus.com');
@@ -91,7 +93,7 @@ normalizeUrl('http://sindresorhus.com', {forceHttps: true});
91
93
  //=> 'https://sindresorhus.com'
92
94
  ```
93
95
 
94
- This option can't be used with the `forceHttp` option at the same time.
96
+ This option cannot be used with the `forceHttp` option at the same time.
95
97
 
96
98
  ##### stripAuthentication
97
99