normalize-url 4.5.1 → 5.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 +17 -24
- package/index.js +15 -39
- package/package.json +6 -5
- package/readme.md +13 -17
package/index.d.ts
CHANGED
|
@@ -188,29 +188,22 @@ declare namespace normalizeUrl {
|
|
|
188
188
|
}
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
(url: string, options?: normalizeUrl.Options): string;
|
|
209
|
-
|
|
210
|
-
// TODO: Remove this for the next major release, refactor the whole definition to:
|
|
211
|
-
// declare function normalizeUrl(url: string, options?: normalizeUrl.Options): string;
|
|
212
|
-
// export = normalizeUrl;
|
|
213
|
-
default: typeof normalizeUrl;
|
|
214
|
-
};
|
|
191
|
+
/**
|
|
192
|
+
[Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL.
|
|
193
|
+
|
|
194
|
+
@param url - URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).
|
|
195
|
+
|
|
196
|
+
@example
|
|
197
|
+
```
|
|
198
|
+
import normalizeUrl = require('normalize-url');
|
|
199
|
+
|
|
200
|
+
normalizeUrl('sindresorhus.com');
|
|
201
|
+
//=> 'http://sindresorhus.com'
|
|
202
|
+
|
|
203
|
+
normalizeUrl('HTTP://xn--xample-hva.com:80/?b=bar&a=foo');
|
|
204
|
+
//=> 'http://êxample.com/?a=foo&b=bar'
|
|
205
|
+
```
|
|
206
|
+
*/
|
|
207
|
+
declare function normalizeUrl(url: string, options?: normalizeUrl.Options): string;
|
|
215
208
|
|
|
216
209
|
export = normalizeUrl;
|
package/index.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
// TODO: Use the `URL` global when targeting Node.js 10
|
|
3
|
-
const URLParser = typeof URL === 'undefined' ? require('url').URL : URL;
|
|
4
2
|
|
|
5
3
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
|
|
6
4
|
const DATA_URL_DEFAULT_MIME_TYPE = 'text/plain';
|
|
@@ -11,21 +9,20 @@ const testParameter = (name, filters) => {
|
|
|
11
9
|
};
|
|
12
10
|
|
|
13
11
|
const normalizeDataURL = (urlString, {stripHash}) => {
|
|
14
|
-
const
|
|
12
|
+
const match = /^data:(?<type>.*?),(?<data>.*?)(?:#(?<hash>.*))?$/.exec(urlString);
|
|
15
13
|
|
|
16
|
-
if (!
|
|
14
|
+
if (!match) {
|
|
17
15
|
throw new Error(`Invalid URL: ${urlString}`);
|
|
18
16
|
}
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
let base64 = false;
|
|
18
|
+
let {type, data, hash} = match.groups;
|
|
19
|
+
const mediaType = type.split(';');
|
|
20
|
+
hash = stripHash ? '' : hash;
|
|
25
21
|
|
|
22
|
+
let isBase64 = false;
|
|
26
23
|
if (mediaType[mediaType.length - 1] === 'base64') {
|
|
27
24
|
mediaType.pop();
|
|
28
|
-
|
|
25
|
+
isBase64 = true;
|
|
29
26
|
}
|
|
30
27
|
|
|
31
28
|
// Lowercase MIME type
|
|
@@ -51,7 +48,7 @@ const normalizeDataURL = (urlString, {stripHash}) => {
|
|
|
51
48
|
...attributes
|
|
52
49
|
];
|
|
53
50
|
|
|
54
|
-
if (
|
|
51
|
+
if (isBase64) {
|
|
55
52
|
normalizedMediaType.push('base64');
|
|
56
53
|
}
|
|
57
54
|
|
|
@@ -59,7 +56,7 @@ const normalizeDataURL = (urlString, {stripHash}) => {
|
|
|
59
56
|
normalizedMediaType.unshift(mimeType);
|
|
60
57
|
}
|
|
61
58
|
|
|
62
|
-
return `data:${normalizedMediaType.join(';')},${
|
|
59
|
+
return `data:${normalizedMediaType.join(';')},${isBase64 ? data.trim() : data}${hash ? `#${hash}` : ''}`;
|
|
63
60
|
};
|
|
64
61
|
|
|
65
62
|
const normalizeUrl = (urlString, options) => {
|
|
@@ -78,19 +75,6 @@ const normalizeUrl = (urlString, options) => {
|
|
|
78
75
|
...options
|
|
79
76
|
};
|
|
80
77
|
|
|
81
|
-
// TODO: Remove this at some point in the future
|
|
82
|
-
if (Reflect.has(options, 'normalizeHttps')) {
|
|
83
|
-
throw new Error('options.normalizeHttps is renamed to options.forceHttp');
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (Reflect.has(options, 'normalizeHttp')) {
|
|
87
|
-
throw new Error('options.normalizeHttp is renamed to options.forceHttps');
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (Reflect.has(options, 'stripFragment')) {
|
|
91
|
-
throw new Error('options.stripFragment is renamed to options.stripHash');
|
|
92
|
-
}
|
|
93
|
-
|
|
94
78
|
urlString = urlString.trim();
|
|
95
79
|
|
|
96
80
|
// Data URL
|
|
@@ -106,7 +90,7 @@ const normalizeUrl = (urlString, options) => {
|
|
|
106
90
|
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, options.defaultProtocol);
|
|
107
91
|
}
|
|
108
92
|
|
|
109
|
-
const urlObj = new
|
|
93
|
+
const urlObj = new URL(urlString);
|
|
110
94
|
|
|
111
95
|
if (options.forceHttp && options.forceHttps) {
|
|
112
96
|
throw new Error('The `forceHttp` and `forceHttps` options cannot be used together');
|
|
@@ -133,20 +117,14 @@ const normalizeUrl = (urlString, options) => {
|
|
|
133
117
|
|
|
134
118
|
// Remove duplicate slashes if not preceded by a protocol
|
|
135
119
|
if (urlObj.pathname) {
|
|
136
|
-
|
|
137
|
-
// `urlObj.pathname = urlObj.pathname.replace(/(?<!https?:)\/{2,}/g, '/');`
|
|
138
|
-
urlObj.pathname = urlObj.pathname.replace(/((?!:).|^)\/{2,}/g, (_, p1) => {
|
|
139
|
-
if (/^(?!\/)/g.test(p1)) {
|
|
140
|
-
return `${p1}/`;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return '/';
|
|
144
|
-
});
|
|
120
|
+
urlObj.pathname = urlObj.pathname.replace(/(?<!https?:)\/{2,}/g, '/');
|
|
145
121
|
}
|
|
146
122
|
|
|
147
123
|
// Decode URI octets
|
|
148
124
|
if (urlObj.pathname) {
|
|
149
|
-
|
|
125
|
+
try {
|
|
126
|
+
urlObj.pathname = decodeURI(urlObj.pathname);
|
|
127
|
+
} catch (_) {}
|
|
150
128
|
}
|
|
151
129
|
|
|
152
130
|
// Remove directory index
|
|
@@ -169,7 +147,7 @@ const normalizeUrl = (urlString, options) => {
|
|
|
169
147
|
urlObj.hostname = urlObj.hostname.replace(/\.$/, '');
|
|
170
148
|
|
|
171
149
|
// Remove `www.`
|
|
172
|
-
if (options.stripWWW && /^www\.([a-z\-\d]{2,63})\.([a-z.]{2,5})$/.test(urlObj.hostname)) {
|
|
150
|
+
if (options.stripWWW && /^www\.(?:[a-z\-\d]{2,63})\.(?:[a-z.]{2,5})$/.test(urlObj.hostname)) {
|
|
173
151
|
// Each label should be max 63 at length (min: 2).
|
|
174
152
|
// The extension should be max 5 at length (min: 2).
|
|
175
153
|
// Source: https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
|
|
@@ -217,5 +195,3 @@ const normalizeUrl = (urlString, options) => {
|
|
|
217
195
|
};
|
|
218
196
|
|
|
219
197
|
module.exports = normalizeUrl;
|
|
220
|
-
// TODO: Remove this for the next major release
|
|
221
|
-
module.exports.default = normalizeUrl;
|
package/package.json
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "normalize-url",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "Normalize a URL",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/normalize-url",
|
|
7
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
7
8
|
"author": {
|
|
8
9
|
"name": "Sindre Sorhus",
|
|
9
10
|
"email": "sindresorhus@gmail.com",
|
|
10
11
|
"url": "sindresorhus.com"
|
|
11
12
|
},
|
|
12
13
|
"engines": {
|
|
13
|
-
"node": ">=
|
|
14
|
+
"node": ">=10"
|
|
14
15
|
},
|
|
15
16
|
"scripts": {
|
|
16
17
|
"test": "xo && nyc ava && tsd"
|
|
@@ -37,8 +38,8 @@
|
|
|
37
38
|
"devDependencies": {
|
|
38
39
|
"ava": "^2.4.0",
|
|
39
40
|
"coveralls": "^3.0.6",
|
|
40
|
-
"nyc": "^
|
|
41
|
-
"tsd": "^0.
|
|
42
|
-
"xo": "^0.
|
|
41
|
+
"nyc": "^15.0.0",
|
|
42
|
+
"tsd": "^0.11.0",
|
|
43
|
+
"xo": "^0.25.3"
|
|
43
44
|
}
|
|
44
45
|
}
|
package/readme.md
CHANGED
|
@@ -4,14 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
## Install
|
|
9
8
|
|
|
10
9
|
```
|
|
11
10
|
$ npm install normalize-url
|
|
12
11
|
```
|
|
13
12
|
|
|
14
|
-
|
|
15
13
|
## Usage
|
|
16
14
|
|
|
17
15
|
```js
|
|
@@ -24,7 +22,6 @@ normalizeUrl('HTTP://xn--xample-hva.com:80/?b=bar&a=foo');
|
|
|
24
22
|
//=> 'http://êxample.com/?a=foo&b=bar'
|
|
25
23
|
```
|
|
26
24
|
|
|
27
|
-
|
|
28
25
|
## API
|
|
29
26
|
|
|
30
27
|
### normalizeUrl(url, options?)
|
|
@@ -41,12 +38,12 @@ Type: `object`
|
|
|
41
38
|
|
|
42
39
|
##### defaultProtocol
|
|
43
40
|
|
|
44
|
-
Type: `string
|
|
41
|
+
Type: `string`\
|
|
45
42
|
Default: `http:`
|
|
46
43
|
|
|
47
44
|
##### normalizeProtocol
|
|
48
45
|
|
|
49
|
-
Type: `boolean
|
|
46
|
+
Type: `boolean`\
|
|
50
47
|
Default: `true`
|
|
51
48
|
|
|
52
49
|
Prepend `defaultProtocol` to the URL if it's protocol-relative.
|
|
@@ -61,7 +58,7 @@ normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});
|
|
|
61
58
|
|
|
62
59
|
##### forceHttp
|
|
63
60
|
|
|
64
|
-
Type: `boolean
|
|
61
|
+
Type: `boolean`\
|
|
65
62
|
Default: `false`
|
|
66
63
|
|
|
67
64
|
Normalize `https:` to `http:`.
|
|
@@ -76,7 +73,7 @@ normalizeUrl('https://sindresorhus.com:80/', {forceHttp: true});
|
|
|
76
73
|
|
|
77
74
|
##### forceHttps
|
|
78
75
|
|
|
79
|
-
Type: `boolean
|
|
76
|
+
Type: `boolean`\
|
|
80
77
|
Default: `false`
|
|
81
78
|
|
|
82
79
|
Normalize `http:` to `https:`.
|
|
@@ -93,7 +90,7 @@ This option can't be used with the `forceHttp` option at the same time.
|
|
|
93
90
|
|
|
94
91
|
##### stripAuthentication
|
|
95
92
|
|
|
96
|
-
Type: `boolean
|
|
93
|
+
Type: `boolean`\
|
|
97
94
|
Default: `true`
|
|
98
95
|
|
|
99
96
|
Strip the [authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) part of the URL.
|
|
@@ -108,7 +105,7 @@ normalizeUrl('user:password@sindresorhus.com', {stripAuthentication: false});
|
|
|
108
105
|
|
|
109
106
|
##### stripHash
|
|
110
107
|
|
|
111
|
-
Type: `boolean
|
|
108
|
+
Type: `boolean`\
|
|
112
109
|
Default: `false`
|
|
113
110
|
|
|
114
111
|
Strip the hash part of the URL.
|
|
@@ -123,7 +120,7 @@ normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
|
|
|
123
120
|
|
|
124
121
|
##### stripProtocol
|
|
125
122
|
|
|
126
|
-
Type: `boolean
|
|
123
|
+
Type: `boolean`\
|
|
127
124
|
Default: `false`
|
|
128
125
|
|
|
129
126
|
Remove HTTP(S) protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`.
|
|
@@ -132,13 +129,13 @@ Remove HTTP(S) protocol from the URL: `http://sindresorhus.com` → `sindresorhu
|
|
|
132
129
|
normalizeUrl('https://sindresorhus.com');
|
|
133
130
|
//=> 'https://sindresorhus.com'
|
|
134
131
|
|
|
135
|
-
normalizeUrl('sindresorhus.com', {stripProtocol: true});
|
|
132
|
+
normalizeUrl('https://sindresorhus.com', {stripProtocol: true});
|
|
136
133
|
//=> 'sindresorhus.com'
|
|
137
134
|
```
|
|
138
135
|
|
|
139
136
|
##### stripWWW
|
|
140
137
|
|
|
141
|
-
Type: `boolean
|
|
138
|
+
Type: `boolean`\
|
|
142
139
|
Default: `true`
|
|
143
140
|
|
|
144
141
|
Remove `www.` from the URL.
|
|
@@ -153,7 +150,7 @@ normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});
|
|
|
153
150
|
|
|
154
151
|
##### removeQueryParameters
|
|
155
152
|
|
|
156
|
-
Type: `Array<RegExp | string
|
|
153
|
+
Type: `Array<RegExp | string>`\
|
|
157
154
|
Default: `[/^utm_\w+/i]`
|
|
158
155
|
|
|
159
156
|
Remove query parameters that matches any of the provided strings or regexes.
|
|
@@ -167,7 +164,7 @@ normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
|
|
|
167
164
|
|
|
168
165
|
##### removeTrailingSlash
|
|
169
166
|
|
|
170
|
-
Type: `boolean
|
|
167
|
+
Type: `boolean`\
|
|
171
168
|
Default: `true`
|
|
172
169
|
|
|
173
170
|
Remove trailing slash.
|
|
@@ -187,7 +184,7 @@ normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
|
|
|
187
184
|
|
|
188
185
|
##### removeDirectoryIndex
|
|
189
186
|
|
|
190
|
-
Type: `boolean | Array<RegExp | string
|
|
187
|
+
Type: `boolean | Array<RegExp | string>`\
|
|
191
188
|
Default: `false`
|
|
192
189
|
|
|
193
190
|
Removes the default directory index file from path that matches any of the provided strings or regexes. When `true`, the regex `/^index\.[a-z]+$/` is used.
|
|
@@ -201,7 +198,7 @@ normalizeUrl('www.sindresorhus.com/foo/default.php', {
|
|
|
201
198
|
|
|
202
199
|
##### sortQueryParameters
|
|
203
200
|
|
|
204
|
-
Type: `boolean
|
|
201
|
+
Type: `boolean`\
|
|
205
202
|
Default: `true`
|
|
206
203
|
|
|
207
204
|
Sorts the query parameters alphabetically by key.
|
|
@@ -213,7 +210,6 @@ normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
|
|
|
213
210
|
//=> 'http://sindresorhus.com/?b=two&a=one&c=three'
|
|
214
211
|
```
|
|
215
212
|
|
|
216
|
-
|
|
217
213
|
## Related
|
|
218
214
|
|
|
219
215
|
- [compare-urls](https://github.com/sindresorhus/compare-urls) - Compare URLs by first normalizing them
|