UNPKG

3.92 kBJavaScriptView Raw
1'use strict';
2// TODO: Use the `URL` global when targeting Node.js 10
3const URLParser = typeof URL === 'undefined' ? require('url').URL : URL;
4
5const testParameter = (name, filters) => {
6 return filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);
7};
8
9module.exports = (urlString, opts) => {
10 opts = Object.assign({
11 defaultProtocol: 'http:',
12 normalizeProtocol: true,
13 forceHttp: false,
14 forceHttps: false,
15 stripHash: true,
16 stripWWW: true,
17 removeQueryParameters: [/^utm_\w+/i],
18 removeTrailingSlash: true,
19 removeDirectoryIndex: false,
20 sortQueryParameters: true
21 }, opts);
22
23 // Backwards compatibility
24 if (Reflect.has(opts, 'normalizeHttps')) {
25 opts.forceHttp = opts.normalizeHttps;
26 }
27
28 if (Reflect.has(opts, 'normalizeHttp')) {
29 opts.forceHttps = opts.normalizeHttp;
30 }
31
32 if (Reflect.has(opts, 'stripFragment')) {
33 opts.stripHash = opts.stripFragment;
34 }
35
36 urlString = urlString.trim();
37
38 const hasRelativeProtocol = urlString.startsWith('//');
39 const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
40
41 // Prepend protocol
42 if (!isRelativeUrl) {
43 urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, opts.defaultProtocol);
44 }
45
46 const urlObj = new URLParser(urlString);
47
48 if (opts.forceHttp && opts.forceHttps) {
49 throw new Error('The `forceHttp` and `forceHttps` options cannot be used together');
50 }
51
52 if (opts.forceHttp && urlObj.protocol === 'https:') {
53 urlObj.protocol = 'http:';
54 }
55
56 if (opts.forceHttps && urlObj.protocol === 'http:') {
57 urlObj.protocol = 'https:';
58 }
59
60 // Remove hash
61 if (opts.stripHash) {
62 urlObj.hash = '';
63 }
64
65 // Remove duplicate slashes if not preceded by a protocol
66 if (urlObj.pathname) {
67 // TODO: Use the following instead when targeting Node.js 10
68 // `urlObj.pathname = urlObj.pathname.replace(/(?<!https?:)\/{2,}/g, '/');`
69 urlObj.pathname = urlObj.pathname.replace(/((?![https?:]).)\/{2,}/g, (_, p1) => {
70 if (/^(?!\/)/g.test(p1)) {
71 return `${p1}/`;
72 }
73 return '/';
74 });
75 }
76
77 // Decode URI octets
78 if (urlObj.pathname) {
79 urlObj.pathname = decodeURI(urlObj.pathname);
80 }
81
82 // Remove directory index
83 if (opts.removeDirectoryIndex === true) {
84 opts.removeDirectoryIndex = [/^index\.[a-z]+$/];
85 }
86
87 if (Array.isArray(opts.removeDirectoryIndex) && opts.removeDirectoryIndex.length > 0) {
88 let pathComponents = urlObj.pathname.split('/');
89 const lastComponent = pathComponents[pathComponents.length - 1];
90
91 if (testParameter(lastComponent, opts.removeDirectoryIndex)) {
92 pathComponents = pathComponents.slice(0, pathComponents.length - 1);
93 urlObj.pathname = pathComponents.slice(1).join('/') + '/';
94 }
95 }
96
97 if (urlObj.hostname) {
98 // Remove trailing dot
99 urlObj.hostname = urlObj.hostname.replace(/\.$/, '');
100
101 // Remove `www.`
102 // eslint-disable-next-line no-useless-escape
103 if (opts.stripWWW && /^www\.([a-z\-\d]{2,63})\.([a-z\.]{2,5})$/.test(urlObj.hostname)) {
104 // Each label should be max 63 at length (min: 2).
105 // The extension should be max 5 at length (min: 2).
106 // Source: https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
107 urlObj.hostname = urlObj.hostname.replace(/^www\./, '');
108 }
109 }
110
111 // Remove query unwanted parameters
112 if (Array.isArray(opts.removeQueryParameters)) {
113 for (const key of [...urlObj.searchParams.keys()]) {
114 if (testParameter(key, opts.removeQueryParameters)) {
115 urlObj.searchParams.delete(key);
116 }
117 }
118 }
119
120 // Sort query parameters
121 if (opts.sortQueryParameters) {
122 urlObj.searchParams.sort();
123 }
124
125 // Take advantage of many of the Node `url` normalizations
126 urlString = urlObj.toString();
127
128 // Remove ending `/`
129 if (opts.removeTrailingSlash || urlObj.pathname === '/') {
130 urlString = urlString.replace(/\/$/, '');
131 }
132
133 // Restore relative protocol, if applicable
134 if (hasRelativeProtocol && !opts.normalizeProtocol) {
135 urlString = urlString.replace(/^http:\/\//, '//');
136 }
137
138 return urlString;
139};