@cjser/normalize-url__v7_2_0 7.2.0-cjser.2

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.
@@ -0,0 +1,200 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // packages/@cjser/normalize-url__v7_2_0.tmp-26-1778150474382/index.js
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ default: () => normalizeUrl
23
+ });
24
+ module.exports = __toCommonJS(index_exports);
25
+ var DATA_URL_DEFAULT_MIME_TYPE = "text/plain";
26
+ var DATA_URL_DEFAULT_CHARSET = "us-ascii";
27
+ var testParameter = (name, filters) => filters.some((filter) => filter instanceof RegExp ? filter.test(name) : filter === name);
28
+ var normalizeDataURL = (urlString, { stripHash }) => {
29
+ const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(urlString);
30
+ if (!match) {
31
+ throw new Error(`Invalid URL: ${urlString}`);
32
+ }
33
+ let { type, data, hash } = match.groups;
34
+ const mediaType = type.split(";");
35
+ hash = stripHash ? "" : hash;
36
+ let isBase64 = false;
37
+ if (mediaType[mediaType.length - 1] === "base64") {
38
+ mediaType.pop();
39
+ isBase64 = true;
40
+ }
41
+ const mimeType = (mediaType.shift() || "").toLowerCase();
42
+ const attributes = mediaType.map((attribute) => {
43
+ let [key, value = ""] = attribute.split("=").map((string) => string.trim());
44
+ if (key === "charset") {
45
+ value = value.toLowerCase();
46
+ if (value === DATA_URL_DEFAULT_CHARSET) {
47
+ return "";
48
+ }
49
+ }
50
+ return `${key}${value ? `=${value}` : ""}`;
51
+ }).filter(Boolean);
52
+ const normalizedMediaType = [
53
+ ...attributes
54
+ ];
55
+ if (isBase64) {
56
+ normalizedMediaType.push("base64");
57
+ }
58
+ if (normalizedMediaType.length > 0 || mimeType && mimeType !== DATA_URL_DEFAULT_MIME_TYPE) {
59
+ normalizedMediaType.unshift(mimeType);
60
+ }
61
+ return `data:${normalizedMediaType.join(";")},${isBase64 ? data.trim() : data}${hash ? `#${hash}` : ""}`;
62
+ };
63
+ function normalizeUrl(urlString, options) {
64
+ options = {
65
+ defaultProtocol: "http:",
66
+ normalizeProtocol: true,
67
+ forceHttp: false,
68
+ forceHttps: false,
69
+ stripAuthentication: true,
70
+ stripHash: false,
71
+ stripTextFragment: true,
72
+ stripWWW: true,
73
+ removeQueryParameters: [/^utm_\w+/i],
74
+ removeTrailingSlash: true,
75
+ removeSingleSlash: true,
76
+ removeDirectoryIndex: false,
77
+ removeExplicitPort: false,
78
+ sortQueryParameters: true,
79
+ ...options
80
+ };
81
+ urlString = urlString.trim();
82
+ if (/^data:/i.test(urlString)) {
83
+ return normalizeDataURL(urlString, options);
84
+ }
85
+ if (/^view-source:/i.test(urlString)) {
86
+ throw new Error("`view-source:` is not supported as it is a non-standard protocol");
87
+ }
88
+ const hasRelativeProtocol = urlString.startsWith("//");
89
+ const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
90
+ if (!isRelativeUrl) {
91
+ urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, options.defaultProtocol);
92
+ }
93
+ const urlObject = new URL(urlString);
94
+ if (options.forceHttp && options.forceHttps) {
95
+ throw new Error("The `forceHttp` and `forceHttps` options cannot be used together");
96
+ }
97
+ if (options.forceHttp && urlObject.protocol === "https:") {
98
+ urlObject.protocol = "http:";
99
+ }
100
+ if (options.forceHttps && urlObject.protocol === "http:") {
101
+ urlObject.protocol = "https:";
102
+ }
103
+ if (options.stripAuthentication) {
104
+ urlObject.username = "";
105
+ urlObject.password = "";
106
+ }
107
+ if (options.stripHash) {
108
+ urlObject.hash = "";
109
+ } else if (options.stripTextFragment) {
110
+ urlObject.hash = urlObject.hash.replace(/#?:~:text.*?$/i, "");
111
+ }
112
+ if (urlObject.pathname) {
113
+ const protocolRegex = /\b[a-z][a-z\d+\-.]{1,50}:\/\//g;
114
+ let lastIndex = 0;
115
+ let result = "";
116
+ for (; ; ) {
117
+ const match = protocolRegex.exec(urlObject.pathname);
118
+ if (!match) {
119
+ break;
120
+ }
121
+ const protocol = match[0];
122
+ const protocolAtIndex = match.index;
123
+ const intermediate = urlObject.pathname.slice(lastIndex, protocolAtIndex);
124
+ result += intermediate.replace(/\/{2,}/g, "/");
125
+ result += protocol;
126
+ lastIndex = protocolAtIndex + protocol.length;
127
+ }
128
+ const remnant = urlObject.pathname.slice(lastIndex, urlObject.pathname.length);
129
+ result += remnant.replace(/\/{2,}/g, "/");
130
+ urlObject.pathname = result;
131
+ }
132
+ if (urlObject.pathname) {
133
+ try {
134
+ urlObject.pathname = decodeURI(urlObject.pathname);
135
+ } catch {
136
+ }
137
+ }
138
+ if (options.removeDirectoryIndex === true) {
139
+ options.removeDirectoryIndex = [/^index\.[a-z]+$/];
140
+ }
141
+ if (Array.isArray(options.removeDirectoryIndex) && options.removeDirectoryIndex.length > 0) {
142
+ let pathComponents = urlObject.pathname.split("/");
143
+ const lastComponent = pathComponents[pathComponents.length - 1];
144
+ if (testParameter(lastComponent, options.removeDirectoryIndex)) {
145
+ pathComponents = pathComponents.slice(0, -1);
146
+ urlObject.pathname = pathComponents.slice(1).join("/") + "/";
147
+ }
148
+ }
149
+ if (urlObject.hostname) {
150
+ urlObject.hostname = urlObject.hostname.replace(/\.$/, "");
151
+ if (options.stripWWW && /^www\.(?!www\.)[a-z\-\d]{1,63}\.[a-z.\-\d]{2,63}$/.test(urlObject.hostname)) {
152
+ urlObject.hostname = urlObject.hostname.replace(/^www\./, "");
153
+ }
154
+ }
155
+ if (Array.isArray(options.removeQueryParameters)) {
156
+ for (const key of [...urlObject.searchParams.keys()]) {
157
+ if (testParameter(key, options.removeQueryParameters)) {
158
+ urlObject.searchParams.delete(key);
159
+ }
160
+ }
161
+ }
162
+ if (!Array.isArray(options.keepQueryParameters) && options.removeQueryParameters === true) {
163
+ urlObject.search = "";
164
+ }
165
+ if (Array.isArray(options.keepQueryParameters) && options.keepQueryParameters.length > 0) {
166
+ for (const key of [...urlObject.searchParams.keys()]) {
167
+ if (!testParameter(key, options.keepQueryParameters)) {
168
+ urlObject.searchParams.delete(key);
169
+ }
170
+ }
171
+ }
172
+ if (options.sortQueryParameters) {
173
+ urlObject.searchParams.sort();
174
+ try {
175
+ urlObject.search = decodeURIComponent(urlObject.search);
176
+ } catch {
177
+ }
178
+ }
179
+ if (options.removeTrailingSlash) {
180
+ urlObject.pathname = urlObject.pathname.replace(/\/$/, "");
181
+ }
182
+ if (options.removeExplicitPort && urlObject.port) {
183
+ urlObject.port = "";
184
+ }
185
+ const oldUrlString = urlString;
186
+ urlString = urlObject.toString();
187
+ if (!options.removeSingleSlash && urlObject.pathname === "/" && !oldUrlString.endsWith("/") && urlObject.hash === "") {
188
+ urlString = urlString.replace(/\/$/, "");
189
+ }
190
+ if ((options.removeTrailingSlash || urlObject.pathname === "/") && urlObject.hash === "" && options.removeSingleSlash) {
191
+ urlString = urlString.replace(/\/$/, "");
192
+ }
193
+ if (hasRelativeProtocol && !options.normalizeProtocol) {
194
+ urlString = urlString.replace(/^http:\/\//, "//");
195
+ }
196
+ if (options.stripProtocol) {
197
+ urlString = urlString.replace(/^(?:https?:)?\/\//, "");
198
+ }
199
+ return urlString;
200
+ }
package/index.d.ts ADDED
@@ -0,0 +1,301 @@
1
+ export interface Options {
2
+ /**
3
+ @default 'http:'
4
+
5
+ Values: `'https:' | 'http:'`
6
+ */
7
+ readonly defaultProtocol?: string; // TODO: Make this `'https:' | 'http:'` in the next major version.
8
+
9
+ /**
10
+ Prepends `defaultProtocol` to the URL if it's protocol-relative.
11
+
12
+ @default true
13
+
14
+ @example
15
+ ```
16
+ normalizeUrl('//sindresorhus.com');
17
+ //=> 'http://sindresorhus.com'
18
+
19
+ normalizeUrl('//sindresorhus.com', {normalizeProtocol: false});
20
+ //=> '//sindresorhus.com'
21
+ ```
22
+ */
23
+ readonly normalizeProtocol?: boolean;
24
+
25
+ /**
26
+ Normalizes `https:` URLs to `http:`.
27
+
28
+ @default false
29
+
30
+ @example
31
+ ```
32
+ normalizeUrl('https://sindresorhus.com');
33
+ //=> 'https://sindresorhus.com'
34
+
35
+ normalizeUrl('https://sindresorhus.com', {forceHttp: true});
36
+ //=> 'http://sindresorhus.com'
37
+ ```
38
+ */
39
+ readonly forceHttp?: boolean;
40
+
41
+ /**
42
+ Normalizes `http:` URLs to `https:`.
43
+
44
+ This option can't be used with the `forceHttp` option at the same time.
45
+
46
+ @default false
47
+
48
+ @example
49
+ ```
50
+ normalizeUrl('http://sindresorhus.com');
51
+ //=> 'http://sindresorhus.com'
52
+
53
+ normalizeUrl('http://sindresorhus.com', {forceHttps: true});
54
+ //=> 'https://sindresorhus.com'
55
+ ```
56
+ */
57
+ readonly forceHttps?: boolean;
58
+
59
+ /**
60
+ Strip the [authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) part of a URL.
61
+
62
+ @default true
63
+
64
+ @example
65
+ ```
66
+ normalizeUrl('user:password@sindresorhus.com');
67
+ //=> 'https://sindresorhus.com'
68
+
69
+ normalizeUrl('user:password@sindresorhus.com', {stripAuthentication: false});
70
+ //=> 'https://user:password@sindresorhus.com'
71
+ ```
72
+ */
73
+ readonly stripAuthentication?: boolean;
74
+
75
+ /**
76
+ Removes hash from the URL.
77
+
78
+ @default false
79
+
80
+ @example
81
+ ```
82
+ normalizeUrl('sindresorhus.com/about.html#contact');
83
+ //=> 'http://sindresorhus.com/about.html#contact'
84
+
85
+ normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
86
+ //=> 'http://sindresorhus.com/about.html'
87
+ ```
88
+ */
89
+ readonly stripHash?: boolean;
90
+
91
+ /**
92
+ Remove the protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`.
93
+
94
+ It will only remove `https://` and `http://` protocols.
95
+
96
+ @default false
97
+
98
+ @example
99
+ ```
100
+ normalizeUrl('https://sindresorhus.com');
101
+ //=> 'https://sindresorhus.com'
102
+
103
+ normalizeUrl('sindresorhus.com', {stripProtocol: true});
104
+ //=> 'sindresorhus.com'
105
+ ```
106
+ */
107
+ readonly stripProtocol?: boolean;
108
+
109
+ /**
110
+ Strip the [text fragment](https://web.dev/text-fragments/) part of the URL
111
+
112
+ __Note:__ The text fragment will always be removed if the `stripHash` option is set to `true`, as the hash contains the text fragment.
113
+
114
+ @default true
115
+
116
+ @example
117
+ ```
118
+ normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello');
119
+ //=> 'http://sindresorhus.com/about.html#'
120
+
121
+ normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello');
122
+ //=> 'http://sindresorhus.com/about.html#section'
123
+
124
+ normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello', {stripTextFragment: false});
125
+ //=> 'http://sindresorhus.com/about.html#:~:text=hello'
126
+
127
+ normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello', {stripTextFragment: false});
128
+ //=> 'http://sindresorhus.com/about.html#section:~:text=hello'
129
+ ```
130
+ */
131
+ readonly stripTextFragment?: boolean;
132
+
133
+ /**
134
+ Removes `www.` from the URL.
135
+
136
+ @default true
137
+
138
+ @example
139
+ ```
140
+ normalizeUrl('http://www.sindresorhus.com');
141
+ //=> 'http://sindresorhus.com'
142
+
143
+ normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});
144
+ //=> 'http://www.sindresorhus.com'
145
+ ```
146
+ */
147
+ readonly stripWWW?: boolean;
148
+
149
+ /**
150
+ Removes query parameters that matches any of the provided strings or regexes.
151
+
152
+ @default [/^utm_\w+/i]
153
+
154
+ @example
155
+ ```
156
+ normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
157
+ removeQueryParameters: ['ref']
158
+ });
159
+ //=> 'http://sindresorhus.com/?foo=bar'
160
+ ```
161
+
162
+ If a boolean is provided, `true` will remove all the query parameters.
163
+
164
+ ```
165
+ normalizeUrl('www.sindresorhus.com?foo=bar', {
166
+ removeQueryParameters: true
167
+ });
168
+ //=> 'http://sindresorhus.com'
169
+ ```
170
+
171
+ `false` will not remove any query parameter.
172
+
173
+ ```
174
+ normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
175
+ removeQueryParameters: false
176
+ });
177
+ //=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'
178
+ ```
179
+ */
180
+ readonly removeQueryParameters?: ReadonlyArray<RegExp | string> | boolean;
181
+
182
+ /**
183
+ Keeps only query parameters that matches any of the provided strings or regexes.
184
+
185
+ __Note__: It overrides the `removeQueryParameters` option.
186
+
187
+ @default undefined
188
+
189
+ @example
190
+ ```
191
+ normalizeUrl('https://sindresorhus.com?foo=bar&ref=unicorn', {
192
+ keepQueryParameters: ['ref']
193
+ });
194
+ //=> 'https://sindresorhus.com/?ref=unicorn'
195
+ ```
196
+ */
197
+ readonly keepQueryParameters?: ReadonlyArray<RegExp | string>;
198
+
199
+ /**
200
+ Removes trailing slash.
201
+
202
+ __Note__: Trailing slash is always removed if the URL doesn't have a pathname unless the `removeSingleSlash` option is set to `false`.
203
+
204
+ @default true
205
+
206
+ @example
207
+ ```
208
+ normalizeUrl('http://sindresorhus.com/redirect/');
209
+ //=> 'http://sindresorhus.com/redirect'
210
+
211
+ normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
212
+ //=> 'http://sindresorhus.com/redirect/'
213
+
214
+ normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
215
+ //=> 'http://sindresorhus.com'
216
+ ```
217
+ */
218
+ readonly removeTrailingSlash?: boolean;
219
+
220
+ /**
221
+ Remove a sole `/` pathname in the output. This option is independent of `removeTrailingSlash`.
222
+
223
+ @default true
224
+
225
+ @example
226
+ ```
227
+ normalizeUrl('https://sindresorhus.com/');
228
+ //=> 'https://sindresorhus.com'
229
+
230
+ normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
231
+ //=> 'https://sindresorhus.com/'
232
+ ```
233
+ */
234
+ readonly removeSingleSlash?: boolean;
235
+
236
+ /**
237
+ Removes the default directory index file from path that matches any of the provided strings or regexes.
238
+ When `true`, the regex `/^index\.[a-z]+$/` is used.
239
+
240
+ @default false
241
+
242
+ @example
243
+ ```
244
+ normalizeUrl('www.sindresorhus.com/foo/default.php', {
245
+ removeDirectoryIndex: [/^default\.[a-z]+$/]
246
+ });
247
+ //=> 'http://sindresorhus.com/foo'
248
+ ```
249
+ */
250
+ readonly removeDirectoryIndex?: boolean | ReadonlyArray<RegExp | string>;
251
+
252
+ /**
253
+ Removes an explicit port number from the URL.
254
+
255
+ Port 443 is always removed from HTTPS URLs and 80 is always removed from HTTP URLs regardless of this option.
256
+
257
+ @default false
258
+
259
+ @example
260
+ ```
261
+ normalizeUrl('sindresorhus.com:123', {
262
+ removeExplicitPort: true
263
+ });
264
+ //=> 'http://sindresorhus.com'
265
+ ```
266
+ */
267
+ readonly removeExplicitPort?: boolean;
268
+
269
+ /**
270
+ Sorts the query parameters alphabetically by key.
271
+
272
+ @default true
273
+
274
+ @example
275
+ ```
276
+ normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
277
+ sortQueryParameters: false
278
+ });
279
+ //=> 'http://sindresorhus.com/?b=two&a=one&c=three'
280
+ ```
281
+ */
282
+ readonly sortQueryParameters?: boolean;
283
+ }
284
+
285
+ /**
286
+ [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL.
287
+
288
+ @param url - URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).
289
+
290
+ @example
291
+ ```
292
+ import normalizeUrl from '@cjser/normalize-url__v7_2_0';
293
+
294
+ normalizeUrl('sindresorhus.com');
295
+ //=> 'http://sindresorhus.com'
296
+
297
+ normalizeUrl('//www.sindresorhus.com:80/../baz?b=bar&a=foo');
298
+ //=> 'http://sindresorhus.com/baz?a=foo&b=bar'
299
+ ```
300
+ */
301
+ export default function normalizeUrl(url: string, options?: Options): string;
package/index.js ADDED
@@ -0,0 +1,262 @@
1
+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
2
+ const DATA_URL_DEFAULT_MIME_TYPE = 'text/plain';
3
+ const DATA_URL_DEFAULT_CHARSET = 'us-ascii';
4
+
5
+ const testParameter = (name, filters) => filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);
6
+
7
+ const normalizeDataURL = (urlString, {stripHash}) => {
8
+ const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(urlString);
9
+
10
+ if (!match) {
11
+ throw new Error(`Invalid URL: ${urlString}`);
12
+ }
13
+
14
+ let {type, data, hash} = match.groups;
15
+ const mediaType = type.split(';');
16
+ hash = stripHash ? '' : hash;
17
+
18
+ let isBase64 = false;
19
+ if (mediaType[mediaType.length - 1] === 'base64') {
20
+ mediaType.pop();
21
+ isBase64 = true;
22
+ }
23
+
24
+ // Lowercase MIME type
25
+ const mimeType = (mediaType.shift() || '').toLowerCase();
26
+ const attributes = mediaType
27
+ .map(attribute => {
28
+ let [key, value = ''] = attribute.split('=').map(string => string.trim());
29
+
30
+ // Lowercase `charset`
31
+ if (key === 'charset') {
32
+ value = value.toLowerCase();
33
+
34
+ if (value === DATA_URL_DEFAULT_CHARSET) {
35
+ return '';
36
+ }
37
+ }
38
+
39
+ return `${key}${value ? `=${value}` : ''}`;
40
+ })
41
+ .filter(Boolean);
42
+
43
+ const normalizedMediaType = [
44
+ ...attributes,
45
+ ];
46
+
47
+ if (isBase64) {
48
+ normalizedMediaType.push('base64');
49
+ }
50
+
51
+ if (normalizedMediaType.length > 0 || (mimeType && mimeType !== DATA_URL_DEFAULT_MIME_TYPE)) {
52
+ normalizedMediaType.unshift(mimeType);
53
+ }
54
+
55
+ return `data:${normalizedMediaType.join(';')},${isBase64 ? data.trim() : data}${hash ? `#${hash}` : ''}`;
56
+ };
57
+
58
+ export default function normalizeUrl(urlString, options) {
59
+ options = {
60
+ defaultProtocol: 'http:',
61
+ normalizeProtocol: true,
62
+ forceHttp: false,
63
+ forceHttps: false,
64
+ stripAuthentication: true,
65
+ stripHash: false,
66
+ stripTextFragment: true,
67
+ stripWWW: true,
68
+ removeQueryParameters: [/^utm_\w+/i],
69
+ removeTrailingSlash: true,
70
+ removeSingleSlash: true,
71
+ removeDirectoryIndex: false,
72
+ removeExplicitPort: false,
73
+ sortQueryParameters: true,
74
+ ...options,
75
+ };
76
+
77
+ urlString = urlString.trim();
78
+
79
+ // Data URL
80
+ if (/^data:/i.test(urlString)) {
81
+ return normalizeDataURL(urlString, options);
82
+ }
83
+
84
+ if (/^view-source:/i.test(urlString)) {
85
+ throw new Error('`view-source:` is not supported as it is a non-standard protocol');
86
+ }
87
+
88
+ const hasRelativeProtocol = urlString.startsWith('//');
89
+ const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
90
+
91
+ // Prepend protocol
92
+ if (!isRelativeUrl) {
93
+ urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, options.defaultProtocol);
94
+ }
95
+
96
+ const urlObject = new URL(urlString);
97
+
98
+ if (options.forceHttp && options.forceHttps) {
99
+ throw new Error('The `forceHttp` and `forceHttps` options cannot be used together');
100
+ }
101
+
102
+ if (options.forceHttp && urlObject.protocol === 'https:') {
103
+ urlObject.protocol = 'http:';
104
+ }
105
+
106
+ if (options.forceHttps && urlObject.protocol === 'http:') {
107
+ urlObject.protocol = 'https:';
108
+ }
109
+
110
+ // Remove auth
111
+ if (options.stripAuthentication) {
112
+ urlObject.username = '';
113
+ urlObject.password = '';
114
+ }
115
+
116
+ // Remove hash
117
+ if (options.stripHash) {
118
+ urlObject.hash = '';
119
+ } else if (options.stripTextFragment) {
120
+ urlObject.hash = urlObject.hash.replace(/#?:~:text.*?$/i, '');
121
+ }
122
+
123
+ // Remove duplicate slashes if not preceded by a protocol
124
+ // NOTE: This could be implemented using a single negative lookbehind
125
+ // regex, but we avoid that to maintain compatibility with older js engines
126
+ // which do not have support for that feature.
127
+ if (urlObject.pathname) {
128
+ // TODO: Replace everything below with `urlObject.pathname = urlObject.pathname.replace(/(?<!\b[a-z][a-z\d+\-.]{1,50}:)\/{2,}/g, '/');` when Safari supports negative lookbehind.
129
+
130
+ // Split the string by occurrences of this protocol regex, and perform
131
+ // duplicate-slash replacement on the strings between those occurrences
132
+ // (if any).
133
+ const protocolRegex = /\b[a-z][a-z\d+\-.]{1,50}:\/\//g;
134
+
135
+ let lastIndex = 0;
136
+ let result = '';
137
+ for (;;) {
138
+ const match = protocolRegex.exec(urlObject.pathname);
139
+ if (!match) {
140
+ break;
141
+ }
142
+
143
+ const protocol = match[0];
144
+ const protocolAtIndex = match.index;
145
+ const intermediate = urlObject.pathname.slice(lastIndex, protocolAtIndex);
146
+
147
+ result += intermediate.replace(/\/{2,}/g, '/');
148
+ result += protocol;
149
+ lastIndex = protocolAtIndex + protocol.length;
150
+ }
151
+
152
+ const remnant = urlObject.pathname.slice(lastIndex, urlObject.pathname.length);
153
+ result += remnant.replace(/\/{2,}/g, '/');
154
+
155
+ urlObject.pathname = result;
156
+ }
157
+
158
+ // Decode URI octets
159
+ if (urlObject.pathname) {
160
+ try {
161
+ urlObject.pathname = decodeURI(urlObject.pathname);
162
+ } catch {}
163
+ }
164
+
165
+ // Remove directory index
166
+ if (options.removeDirectoryIndex === true) {
167
+ options.removeDirectoryIndex = [/^index\.[a-z]+$/];
168
+ }
169
+
170
+ if (Array.isArray(options.removeDirectoryIndex) && options.removeDirectoryIndex.length > 0) {
171
+ let pathComponents = urlObject.pathname.split('/');
172
+ const lastComponent = pathComponents[pathComponents.length - 1];
173
+
174
+ if (testParameter(lastComponent, options.removeDirectoryIndex)) {
175
+ pathComponents = pathComponents.slice(0, -1);
176
+ urlObject.pathname = pathComponents.slice(1).join('/') + '/';
177
+ }
178
+ }
179
+
180
+ if (urlObject.hostname) {
181
+ // Remove trailing dot
182
+ urlObject.hostname = urlObject.hostname.replace(/\.$/, '');
183
+
184
+ // Remove `www.`
185
+ if (options.stripWWW && /^www\.(?!www\.)[a-z\-\d]{1,63}\.[a-z.\-\d]{2,63}$/.test(urlObject.hostname)) {
186
+ // Each label should be max 63 at length (min: 1).
187
+ // Source: https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
188
+ // Each TLD should be up to 63 characters long (min: 2).
189
+ // It is technically possible to have a single character TLD, but none currently exist.
190
+ urlObject.hostname = urlObject.hostname.replace(/^www\./, '');
191
+ }
192
+ }
193
+
194
+ // Remove query unwanted parameters
195
+ if (Array.isArray(options.removeQueryParameters)) {
196
+ // eslint-disable-next-line unicorn/no-useless-spread -- We are intentionally spreading to get a copy.
197
+ for (const key of [...urlObject.searchParams.keys()]) {
198
+ if (testParameter(key, options.removeQueryParameters)) {
199
+ urlObject.searchParams.delete(key);
200
+ }
201
+ }
202
+ }
203
+
204
+ if (!Array.isArray(options.keepQueryParameters) && options.removeQueryParameters === true) {
205
+ urlObject.search = '';
206
+ }
207
+
208
+ // Keep wanted query parameters
209
+ if (Array.isArray(options.keepQueryParameters) && options.keepQueryParameters.length > 0) {
210
+ // eslint-disable-next-line unicorn/no-useless-spread -- We are intentionally spreading to get a copy.
211
+ for (const key of [...urlObject.searchParams.keys()]) {
212
+ if (!testParameter(key, options.keepQueryParameters)) {
213
+ urlObject.searchParams.delete(key);
214
+ }
215
+ }
216
+ }
217
+
218
+ // Sort query parameters
219
+ if (options.sortQueryParameters) {
220
+ urlObject.searchParams.sort();
221
+
222
+ // Calling `.sort()` encodes the search parameters, so we need to decode them again.
223
+ try {
224
+ urlObject.search = decodeURIComponent(urlObject.search);
225
+ } catch {}
226
+ }
227
+
228
+ if (options.removeTrailingSlash) {
229
+ urlObject.pathname = urlObject.pathname.replace(/\/$/, '');
230
+ }
231
+
232
+ // Remove an explicit port number, excluding a default port number, if applicable
233
+ if (options.removeExplicitPort && urlObject.port) {
234
+ urlObject.port = '';
235
+ }
236
+
237
+ const oldUrlString = urlString;
238
+
239
+ // Take advantage of many of the Node `url` normalizations
240
+ urlString = urlObject.toString();
241
+
242
+ if (!options.removeSingleSlash && urlObject.pathname === '/' && !oldUrlString.endsWith('/') && urlObject.hash === '') {
243
+ urlString = urlString.replace(/\/$/, '');
244
+ }
245
+
246
+ // Remove ending `/` unless removeSingleSlash is false
247
+ if ((options.removeTrailingSlash || urlObject.pathname === '/') && urlObject.hash === '' && options.removeSingleSlash) {
248
+ urlString = urlString.replace(/\/$/, '');
249
+ }
250
+
251
+ // Restore relative protocol, if applicable
252
+ if (hasRelativeProtocol && !options.normalizeProtocol) {
253
+ urlString = urlString.replace(/^http:\/\//, '//');
254
+ }
255
+
256
+ // Remove http/https
257
+ if (options.stripProtocol) {
258
+ urlString = urlString.replace(/^(?:https?:)?\/\//, '');
259
+ }
260
+
261
+ return urlString;
262
+ }
package/license ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "@cjser/normalize-url__v7_2_0",
3
+ "version": "7.2.0-cjser.2",
4
+ "description": "Normalize a URL",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://code.moenext.com/3rdeye/cjser.git"
9
+ },
10
+ "funding": "https://github.com/sponsors/sindresorhus",
11
+ "author": {
12
+ "name": "Sindre Sorhus",
13
+ "email": "sindresorhus@gmail.com",
14
+ "url": "https://sindresorhus.com"
15
+ },
16
+ "type": "module",
17
+ "exports": {
18
+ "require": "./dist-cjser/index.cjs",
19
+ "default": "./index.js"
20
+ },
21
+ "engines": {
22
+ "node": ">=12.20"
23
+ },
24
+ "scripts": {
25
+ "test": "xo && c8 ava && tsd"
26
+ },
27
+ "files": [
28
+ "index.js",
29
+ "index.d.ts",
30
+ "dist-cjser"
31
+ ],
32
+ "keywords": [
33
+ "normalize",
34
+ "url",
35
+ "uri",
36
+ "address",
37
+ "string",
38
+ "normalization",
39
+ "normalisation",
40
+ "query",
41
+ "querystring",
42
+ "simplify",
43
+ "strip",
44
+ "trim",
45
+ "canonical"
46
+ ],
47
+ "devDependencies": {
48
+ "ava": "^4.0.1",
49
+ "c8": "^7.11.0",
50
+ "tsd": "^0.19.1",
51
+ "xo": "^0.47.0"
52
+ },
53
+ "c8": {
54
+ "reporter": [
55
+ "text",
56
+ "lcov"
57
+ ]
58
+ },
59
+ "main": "./dist-cjser/index.cjs",
60
+ "cjser": {
61
+ "sourceVersion": "7.2.0",
62
+ "cjserVersion": 2,
63
+ "original": {
64
+ "name": "normalize-url",
65
+ "version": "7.2.0",
66
+ "exports": "./index.js",
67
+ "repository": "sindresorhus/normalize-url",
68
+ "files": [
69
+ "index.js",
70
+ "index.d.ts"
71
+ ],
72
+ "scripts": {
73
+ "test": "xo && c8 ava && tsd"
74
+ }
75
+ }
76
+ }
77
+ }
package/readme.md ADDED
@@ -0,0 +1,327 @@
1
+ # normalize-url [![Coverage Status](https://codecov.io/gh/sindresorhus/normalize-url/branch/main/graph/badge.svg)](https://codecov.io/gh/sindresorhus/normalize-url)
2
+
3
+ > [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL
4
+
5
+ Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.
6
+
7
+ **Note:** This package does **not** do URL sanitization. [Garbage in, garbage out.](https://en.wikipedia.org/wiki/Garbage_in,_garbage_out) If you use this in a server context and accept URLs as user input, it's up to you to protect against invalid URLs, [path traversal attacks](https://owasp.org/www-community/attacks/Path_Traversal), etc.
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ npm install normalize-url
13
+ ```
14
+
15
+ *If you need Safari support, use version 4: `npm i normalize-url@4`*
16
+
17
+ ## Usage
18
+
19
+ ```js
20
+ import normalizeUrl from 'normalize-url';
21
+
22
+ normalizeUrl('sindresorhus.com');
23
+ //=> 'http://sindresorhus.com'
24
+
25
+ normalizeUrl('//www.sindresorhus.com:80/../baz?b=bar&a=foo');
26
+ //=> 'http://sindresorhus.com/baz?a=foo&b=bar'
27
+ ```
28
+
29
+ ## API
30
+
31
+ ### normalizeUrl(url, options?)
32
+
33
+ #### url
34
+
35
+ Type: `string`
36
+
37
+ URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs).
38
+
39
+ #### options
40
+
41
+ Type: `object`
42
+
43
+ ##### defaultProtocol
44
+
45
+ Type: `string`\
46
+ Default: `http:`\
47
+ Values: `'https:' | 'http:'`
48
+
49
+ ##### normalizeProtocol
50
+
51
+ Type: `boolean`\
52
+ Default: `true`
53
+
54
+ Prepend `defaultProtocol` to the URL if it's protocol-relative.
55
+
56
+ ```js
57
+ normalizeUrl('//sindresorhus.com');
58
+ //=> 'http://sindresorhus.com'
59
+
60
+ normalizeUrl('//sindresorhus.com', {normalizeProtocol: false});
61
+ //=> '//sindresorhus.com'
62
+ ```
63
+
64
+ ##### forceHttp
65
+
66
+ Type: `boolean`\
67
+ Default: `false`
68
+
69
+ Normalize `https:` to `http:`.
70
+
71
+ ```js
72
+ normalizeUrl('https://sindresorhus.com');
73
+ //=> 'https://sindresorhus.com'
74
+
75
+ normalizeUrl('https://sindresorhus.com', {forceHttp: true});
76
+ //=> 'http://sindresorhus.com'
77
+ ```
78
+
79
+ ##### forceHttps
80
+
81
+ Type: `boolean`\
82
+ Default: `false`
83
+
84
+ Normalize `http:` to `https:`.
85
+
86
+ ```js
87
+ normalizeUrl('http://sindresorhus.com');
88
+ //=> 'http://sindresorhus.com'
89
+
90
+ normalizeUrl('http://sindresorhus.com', {forceHttps: true});
91
+ //=> 'https://sindresorhus.com'
92
+ ```
93
+
94
+ This option can't be used with the `forceHttp` option at the same time.
95
+
96
+ ##### stripAuthentication
97
+
98
+ Type: `boolean`\
99
+ Default: `true`
100
+
101
+ Strip the [authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) part of the URL.
102
+
103
+ ```js
104
+ normalizeUrl('user:password@sindresorhus.com');
105
+ //=> 'https://sindresorhus.com'
106
+
107
+ normalizeUrl('user:password@sindresorhus.com', {stripAuthentication: false});
108
+ //=> 'https://user:password@sindresorhus.com'
109
+ ```
110
+
111
+ ##### stripHash
112
+
113
+ Type: `boolean`\
114
+ Default: `false`
115
+
116
+ Strip the hash part of the URL.
117
+
118
+ ```js
119
+ normalizeUrl('sindresorhus.com/about.html#contact');
120
+ //=> 'http://sindresorhus.com/about.html#contact'
121
+
122
+ normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
123
+ //=> 'http://sindresorhus.com/about.html'
124
+ ```
125
+
126
+ ##### stripProtocol
127
+
128
+ Type: `boolean`\
129
+ Default: `false`
130
+
131
+ Remove the protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`.
132
+
133
+ It will only remove `https://` and `http://` protocols.
134
+
135
+ ```js
136
+ normalizeUrl('https://sindresorhus.com');
137
+ //=> 'https://sindresorhus.com'
138
+
139
+ normalizeUrl('https://sindresorhus.com', {stripProtocol: true});
140
+ //=> 'sindresorhus.com'
141
+ ```
142
+
143
+ ##### stripTextFragment
144
+
145
+ Type: `boolean`\
146
+ Default: `true`
147
+
148
+ Strip the [text fragment](https://web.dev/text-fragments/) part of the URL.
149
+
150
+ **Note:** The text fragment will always be removed if the `stripHash` option is set to `true`, as the hash contains the text fragment.
151
+
152
+ ```js
153
+ normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello');
154
+ //=> 'http://sindresorhus.com/about.html#'
155
+
156
+ normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello');
157
+ //=> 'http://sindresorhus.com/about.html#section'
158
+
159
+ normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello', {stripTextFragment: false});
160
+ //=> 'http://sindresorhus.com/about.html#:~:text=hello'
161
+
162
+ normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello', {stripTextFragment: false});
163
+ //=> 'http://sindresorhus.com/about.html#section:~:text=hello'
164
+ ```
165
+
166
+ ##### stripWWW
167
+
168
+ Type: `boolean`\
169
+ Default: `true`
170
+
171
+ Remove `www.` from the URL.
172
+
173
+ ```js
174
+ normalizeUrl('http://www.sindresorhus.com');
175
+ //=> 'http://sindresorhus.com'
176
+
177
+ normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});
178
+ //=> 'http://www.sindresorhus.com'
179
+ ```
180
+
181
+ ##### removeQueryParameters
182
+
183
+ Type: `Array<RegExp | string> | boolean`\
184
+ Default: `[/^utm_\w+/i]`
185
+
186
+ Remove query parameters that matches any of the provided strings or regexes.
187
+
188
+ ```js
189
+ normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
190
+ removeQueryParameters: ['ref']
191
+ });
192
+ //=> 'http://sindresorhus.com/?foo=bar'
193
+ ```
194
+
195
+ If a boolean is provided, `true` will remove all the query parameters.
196
+
197
+ ```js
198
+ normalizeUrl('www.sindresorhus.com?foo=bar', {
199
+ removeQueryParameters: true
200
+ });
201
+ //=> 'http://sindresorhus.com'
202
+ ```
203
+
204
+ `false` will not remove any query parameter.
205
+
206
+ ```js
207
+ normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
208
+ removeQueryParameters: false
209
+ });
210
+ //=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'
211
+ ```
212
+
213
+ ##### keepQueryParameters
214
+
215
+ Type: `Array<RegExp | string>`\
216
+ Default: `undefined`
217
+
218
+ Keeps only query parameters that matches any of the provided strings or regexes.
219
+
220
+ **Note:** It overrides the `removeQueryParameters` option.
221
+
222
+ ```js
223
+ normalizeUrl('https://sindresorhus.com?foo=bar&ref=unicorn', {
224
+ keepQueryParameters: ['ref']
225
+ });
226
+ //=> 'https://sindresorhus.com/?ref=unicorn'
227
+ ```
228
+
229
+ ##### removeTrailingSlash
230
+
231
+ Type: `boolean`\
232
+ Default: `true`
233
+
234
+ Remove trailing slash.
235
+
236
+ **Note:** Trailing slash is always removed if the URL doesn't have a pathname unless the `removeSingleSlash` option is set to `false`.
237
+
238
+ ```js
239
+ normalizeUrl('http://sindresorhus.com/redirect/');
240
+ //=> 'http://sindresorhus.com/redirect'
241
+
242
+ normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
243
+ //=> 'http://sindresorhus.com/redirect/'
244
+
245
+ normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
246
+ //=> 'http://sindresorhus.com'
247
+ ```
248
+
249
+ ##### removeSingleSlash
250
+
251
+ Type: `boolean`\
252
+ Default: `true`
253
+
254
+ Remove a sole `/` pathname in the output. This option is independent of `removeTrailingSlash`.
255
+
256
+ ```js
257
+ normalizeUrl('https://sindresorhus.com/');
258
+ //=> 'https://sindresorhus.com'
259
+
260
+ normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
261
+ //=> 'https://sindresorhus.com/'
262
+ ```
263
+
264
+ ##### removeDirectoryIndex
265
+
266
+ Type: `boolean | Array<RegExp | string>`\
267
+ Default: `false`
268
+
269
+ 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.
270
+
271
+ ```js
272
+ normalizeUrl('www.sindresorhus.com/foo/default.php', {
273
+ removeDirectoryIndex: [/^default\.[a-z]+$/]
274
+ });
275
+ //=> 'http://sindresorhus.com/foo'
276
+ ```
277
+
278
+ ##### removeExplicitPort
279
+
280
+ Type: `boolean`\
281
+ Default: `false`
282
+
283
+ Removes an explicit port number from the URL.
284
+
285
+ Port 443 is always removed from HTTPS URLs and 80 is always removed from HTTP URLs regardless of this option.
286
+
287
+ ```js
288
+ normalizeUrl('sindresorhus.com:123', {
289
+ removeExplicitPort: true
290
+ });
291
+ //=> 'http://sindresorhus.com'
292
+ ```
293
+
294
+ ##### sortQueryParameters
295
+
296
+ Type: `boolean`\
297
+ Default: `true`
298
+
299
+ Sorts the query parameters alphabetically by key.
300
+
301
+ ```js
302
+ normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
303
+ sortQueryParameters: false
304
+ });
305
+ //=> 'http://sindresorhus.com/?b=two&a=one&c=three'
306
+ ```
307
+
308
+ ## Related
309
+
310
+ - [compare-urls](https://github.com/sindresorhus/compare-urls) - Compare URLs by first normalizing them
311
+
312
+ ---
313
+
314
+ <div align="center">
315
+ <b>
316
+ <a href="https://tidelift.com/subscription/pkg/npm-normalize-url?utm_source=npm-normalize-url&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
317
+ </b>
318
+ <br>
319
+ <sub>
320
+ Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
321
+ </sub>
322
+ </div>
323
+
324
+ ## cjser
325
+
326
+ This package is a CommonJS-compatible build generated by cjser for projects that still need `require()` support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build.
327
+ Original repository: https://github.com/sindresorhus/normalize-url