update
This commit is contained in:
+99
-77
@@ -1,92 +1,100 @@
|
||||
'use strict';
|
||||
// TODO: Use the `URL` global when targeting Node.js 10
|
||||
const URLParser = typeof URL === 'undefined' ? require('url').URL : URL;
|
||||
var url = require('url');
|
||||
var punycode = require('punycode');
|
||||
var queryString = require('query-string');
|
||||
var prependHttp = require('prepend-http');
|
||||
var sortKeys = require('sort-keys');
|
||||
var objectAssign = require('object-assign');
|
||||
|
||||
const testParameter = (name, filters) => {
|
||||
return filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);
|
||||
var DEFAULT_PORTS = {
|
||||
'http:': 80,
|
||||
'https:': 443,
|
||||
'ftp:': 21
|
||||
};
|
||||
|
||||
module.exports = (urlString, opts) => {
|
||||
opts = Object.assign({
|
||||
defaultProtocol: 'http:',
|
||||
// protocols that always contain a `//`` bit
|
||||
var slashedProtocol = {
|
||||
'http': true,
|
||||
'https': true,
|
||||
'ftp': true,
|
||||
'gopher': true,
|
||||
'file': true,
|
||||
'http:': true,
|
||||
'https:': true,
|
||||
'ftp:': true,
|
||||
'gopher:': true,
|
||||
'file:': true
|
||||
};
|
||||
|
||||
function testParameter(name, filters) {
|
||||
return filters.some(function (filter) {
|
||||
return filter instanceof RegExp ? filter.test(name) : filter === name;
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = function (str, opts) {
|
||||
opts = objectAssign({
|
||||
normalizeProtocol: true,
|
||||
forceHttp: false,
|
||||
forceHttps: false,
|
||||
stripHash: true,
|
||||
normalizeHttps: false,
|
||||
stripFragment: true,
|
||||
stripWWW: true,
|
||||
removeQueryParameters: [/^utm_\w+/i],
|
||||
removeTrailingSlash: true,
|
||||
removeDirectoryIndex: false,
|
||||
sortQueryParameters: true
|
||||
removeDirectoryIndex: false
|
||||
}, opts);
|
||||
|
||||
// Backwards compatibility
|
||||
if (Reflect.has(opts, 'normalizeHttps')) {
|
||||
opts.forceHttp = opts.normalizeHttps;
|
||||
if (typeof str !== 'string') {
|
||||
throw new TypeError('Expected a string');
|
||||
}
|
||||
|
||||
if (Reflect.has(opts, 'normalizeHttp')) {
|
||||
opts.forceHttps = opts.normalizeHttp;
|
||||
}
|
||||
var hasRelativeProtocol = str.indexOf('//') === 0;
|
||||
|
||||
if (Reflect.has(opts, 'stripFragment')) {
|
||||
opts.stripHash = opts.stripFragment;
|
||||
}
|
||||
// prepend protocol
|
||||
str = prependHttp(str.trim()).replace(/^\/\//, 'http://');
|
||||
|
||||
urlString = urlString.trim();
|
||||
var urlObj = url.parse(str);
|
||||
|
||||
const hasRelativeProtocol = urlString.startsWith('//');
|
||||
const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
|
||||
|
||||
// Prepend protocol
|
||||
if (!isRelativeUrl) {
|
||||
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, opts.defaultProtocol);
|
||||
}
|
||||
|
||||
const urlObj = new URLParser(urlString);
|
||||
|
||||
if (opts.forceHttp && opts.forceHttps) {
|
||||
throw new Error('The `forceHttp` and `forceHttps` options cannot be used together');
|
||||
}
|
||||
|
||||
if (opts.forceHttp && urlObj.protocol === 'https:') {
|
||||
if (opts.normalizeHttps && urlObj.protocol === 'https:') {
|
||||
urlObj.protocol = 'http:';
|
||||
}
|
||||
|
||||
if (opts.forceHttps && urlObj.protocol === 'http:') {
|
||||
urlObj.protocol = 'https:';
|
||||
if (!urlObj.hostname && !urlObj.pathname) {
|
||||
throw new Error('Invalid URL');
|
||||
}
|
||||
|
||||
// Remove hash
|
||||
if (opts.stripHash) {
|
||||
urlObj.hash = '';
|
||||
// prevent these from being used by `url.format`
|
||||
delete urlObj.host;
|
||||
delete urlObj.query;
|
||||
|
||||
// remove fragment
|
||||
if (opts.stripFragment) {
|
||||
delete urlObj.hash;
|
||||
}
|
||||
|
||||
// Remove duplicate slashes if not preceded by a protocol
|
||||
// remove default port
|
||||
var port = DEFAULT_PORTS[urlObj.protocol];
|
||||
if (Number(urlObj.port) === port) {
|
||||
delete urlObj.port;
|
||||
}
|
||||
|
||||
// remove duplicate slashes
|
||||
if (urlObj.pathname) {
|
||||
// TODO: Use the following instead when targeting Node.js 10
|
||||
// `urlObj.pathname = urlObj.pathname.replace(/(?<!https?:)\/{2,}/g, '/');`
|
||||
urlObj.pathname = urlObj.pathname.replace(/((?![https?:]).)\/{2,}/g, (_, p1) => {
|
||||
if (/^(?!\/)/g.test(p1)) {
|
||||
return `${p1}/`;
|
||||
}
|
||||
return '/';
|
||||
});
|
||||
urlObj.pathname = urlObj.pathname.replace(/\/{2,}/g, '/');
|
||||
}
|
||||
|
||||
// Decode URI octets
|
||||
// decode URI octets
|
||||
if (urlObj.pathname) {
|
||||
urlObj.pathname = decodeURI(urlObj.pathname);
|
||||
}
|
||||
|
||||
// Remove directory index
|
||||
// remove directory index
|
||||
if (opts.removeDirectoryIndex === true) {
|
||||
opts.removeDirectoryIndex = [/^index\.[a-z]+$/];
|
||||
}
|
||||
|
||||
if (Array.isArray(opts.removeDirectoryIndex) && opts.removeDirectoryIndex.length > 0) {
|
||||
let pathComponents = urlObj.pathname.split('/');
|
||||
const lastComponent = pathComponents[pathComponents.length - 1];
|
||||
if (Array.isArray(opts.removeDirectoryIndex) && opts.removeDirectoryIndex.length) {
|
||||
var pathComponents = urlObj.pathname.split('/');
|
||||
var lastComponent = pathComponents[pathComponents.length - 1];
|
||||
|
||||
if (testParameter(lastComponent, opts.removeDirectoryIndex)) {
|
||||
pathComponents = pathComponents.slice(0, pathComponents.length - 1);
|
||||
@@ -94,46 +102,60 @@ module.exports = (urlString, opts) => {
|
||||
}
|
||||
}
|
||||
|
||||
// resolve relative paths, but only for slashed protocols
|
||||
if (slashedProtocol[urlObj.protocol]) {
|
||||
var domain = urlObj.protocol + '//' + urlObj.hostname;
|
||||
var relative = url.resolve(domain, urlObj.pathname);
|
||||
urlObj.pathname = relative.replace(domain, '');
|
||||
}
|
||||
|
||||
if (urlObj.hostname) {
|
||||
// Remove trailing dot
|
||||
// IDN to Unicode
|
||||
urlObj.hostname = punycode.toUnicode(urlObj.hostname).toLowerCase();
|
||||
|
||||
// remove trailing dot
|
||||
urlObj.hostname = urlObj.hostname.replace(/\.$/, '');
|
||||
|
||||
// Remove `www.`
|
||||
// eslint-disable-next-line no-useless-escape
|
||||
if (opts.stripWWW && /^www\.([a-z\-\d]{2,63})\.([a-z\.]{2,5})$/.test(urlObj.hostname)) {
|
||||
// Each label should be max 63 at length (min: 2).
|
||||
// The extension should be max 5 at length (min: 2).
|
||||
// Source: https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
|
||||
// remove `www.`
|
||||
if (opts.stripWWW) {
|
||||
urlObj.hostname = urlObj.hostname.replace(/^www\./, '');
|
||||
}
|
||||
}
|
||||
|
||||
// Remove query unwanted parameters
|
||||
// remove URL with empty query string
|
||||
if (urlObj.search === '?') {
|
||||
delete urlObj.search;
|
||||
}
|
||||
|
||||
var queryParameters = queryString.parse(urlObj.search);
|
||||
|
||||
// remove query unwanted parameters
|
||||
if (Array.isArray(opts.removeQueryParameters)) {
|
||||
for (const key of [...urlObj.searchParams.keys()]) {
|
||||
for (var key in queryParameters) {
|
||||
if (testParameter(key, opts.removeQueryParameters)) {
|
||||
urlObj.searchParams.delete(key);
|
||||
delete queryParameters[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort query parameters
|
||||
if (opts.sortQueryParameters) {
|
||||
urlObj.searchParams.sort();
|
||||
}
|
||||
// sort query parameters
|
||||
urlObj.search = queryString.stringify(sortKeys(queryParameters));
|
||||
|
||||
// Take advantage of many of the Node `url` normalizations
|
||||
urlString = urlObj.toString();
|
||||
// decode query parameters
|
||||
urlObj.search = decodeURIComponent(urlObj.search);
|
||||
|
||||
// Remove ending `/`
|
||||
// take advantage of many of the Node `url` normalizations
|
||||
str = url.format(urlObj);
|
||||
|
||||
// remove ending `/`
|
||||
if (opts.removeTrailingSlash || urlObj.pathname === '/') {
|
||||
urlString = urlString.replace(/\/$/, '');
|
||||
str = str.replace(/\/$/, '');
|
||||
}
|
||||
|
||||
// Restore relative protocol, if applicable
|
||||
// restore relative protocol, if applicable
|
||||
if (hasRelativeProtocol && !opts.normalizeProtocol) {
|
||||
urlString = urlString.replace(/^http:\/\//, '//');
|
||||
str = str.replace(/^http:\/\//, '//');
|
||||
}
|
||||
|
||||
return urlString;
|
||||
return str;
|
||||
};
|
||||
|
||||
+16
-4
@@ -1,9 +1,21 @@
|
||||
MIT License
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
|
||||
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:
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
+26
-19
@@ -1,33 +1,32 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"normalize-url@3.3.0",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"normalize-url@1.9.1",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "normalize-url@3.3.0",
|
||||
"_id": "normalize-url@3.3.0",
|
||||
"_from": "normalize-url@1.9.1",
|
||||
"_id": "normalize-url@1.9.1",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-U+JJi7duF1o+u2pynbp2zXDW2/PADgC30f0GsHZtRh+HOcXHnw137TrNlyxxRvWW5fjKd3bcLHPxofWuCjaeZg==",
|
||||
"_integrity": "sha512-A48My/mtCklowHBlI8Fq2jFWK4tX4lJ5E6ytFsSOq1fzpvT0SQSgKhSg7lN5c2uYFOrUAOQp6zhhJnpp1eMloQ==",
|
||||
"_location": "/normalize-url",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "normalize-url@3.3.0",
|
||||
"raw": "normalize-url@1.9.1",
|
||||
"name": "normalize-url",
|
||||
"escapedName": "normalize-url",
|
||||
"rawSpec": "3.3.0",
|
||||
"rawSpec": "1.9.1",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "3.3.0"
|
||||
"fetchSpec": "1.9.1"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/parse-url",
|
||||
"/postcss-normalize-url"
|
||||
"/extract-css-chunks-webpack-plugin"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-3.3.0.tgz",
|
||||
"_spec": "3.3.0",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-1.9.1.tgz",
|
||||
"_spec": "1.9.1",
|
||||
"_where": "/home/node/nuxt",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
@@ -36,15 +35,19 @@
|
||||
"bugs": {
|
||||
"url": "https://github.com/sindresorhus/normalize-url/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"object-assign": "^4.0.1",
|
||||
"prepend-http": "^1.0.0",
|
||||
"query-string": "^4.1.0",
|
||||
"sort-keys": "^1.0.0"
|
||||
},
|
||||
"description": "Normalize a URL",
|
||||
"devDependencies": {
|
||||
"ava": "*",
|
||||
"coveralls": "^3.0.0",
|
||||
"nyc": "^12.0.2",
|
||||
"xo": "*"
|
||||
"xo": "^0.16.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
"node": ">=4"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
@@ -56,10 +59,14 @@
|
||||
"uri",
|
||||
"address",
|
||||
"string",
|
||||
"str",
|
||||
"normalise",
|
||||
"normalization",
|
||||
"normalisation",
|
||||
"query",
|
||||
"string",
|
||||
"querystring",
|
||||
"unicode",
|
||||
"simplify",
|
||||
"strip",
|
||||
"trim",
|
||||
@@ -72,7 +79,7 @@
|
||||
"url": "git+https://github.com/sindresorhus/normalize-url.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && nyc ava"
|
||||
"test": "xo && ava"
|
||||
},
|
||||
"version": "3.3.0"
|
||||
"version": "1.9.1"
|
||||
}
|
||||
|
||||
+13
-51
@@ -1,6 +1,6 @@
|
||||
# normalize-url [](https://travis-ci.org/sindresorhus/normalize-url) [](https://coveralls.io/github/sindresorhus/normalize-url?branch=master)
|
||||
# normalize-url [](https://travis-ci.org/sindresorhus/normalize-url)
|
||||
|
||||
> [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL
|
||||
> [Normalize](http://en.wikipedia.org/wiki/URL_normalization) a URL
|
||||
|
||||
Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.
|
||||
|
||||
@@ -8,7 +8,7 @@ Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install normalize-url
|
||||
$ npm install --save normalize-url
|
||||
```
|
||||
|
||||
|
||||
@@ -37,19 +37,12 @@ URL to normalize.
|
||||
|
||||
#### options
|
||||
|
||||
Type: `Object`
|
||||
|
||||
##### defaultProtocol
|
||||
|
||||
Type: `string`<br>
|
||||
Default: `http:`
|
||||
|
||||
##### normalizeProtocol
|
||||
|
||||
Type: `boolean`<br>
|
||||
Default: `true`
|
||||
|
||||
Prepends `defaultProtocol` to the URL if it's protocol-relative.
|
||||
Prepend `http:` to the URL if it's protocol-relative.
|
||||
|
||||
```js
|
||||
normalizeUrl('//sindresorhus.com:80/');
|
||||
@@ -59,12 +52,12 @@ normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});
|
||||
//=> '//sindresorhus.com'
|
||||
```
|
||||
|
||||
##### forceHttp
|
||||
##### normalizeHttps
|
||||
|
||||
Type: `boolean`<br>
|
||||
Default: `false`
|
||||
|
||||
Normalizes `https:` URLs to `http:`.
|
||||
Normalize `https:` URLs to `http:`.
|
||||
|
||||
```js
|
||||
normalizeUrl('https://sindresorhus.com:80/');
|
||||
@@ -74,35 +67,18 @@ normalizeUrl('https://sindresorhus.com:80/', {normalizeHttps: true});
|
||||
//=> 'http://sindresorhus.com'
|
||||
```
|
||||
|
||||
##### forceHttps
|
||||
|
||||
Type: `boolean`<br>
|
||||
Default: `false`
|
||||
|
||||
Normalizes `http:` URLs to `https:`.
|
||||
|
||||
```js
|
||||
normalizeUrl('https://sindresorhus.com:80/');
|
||||
//=> 'https://sindresorhus.com'
|
||||
|
||||
normalizeUrl('http://sindresorhus.com:80/', {normalizeHttp: true});
|
||||
//=> 'https://sindresorhus.com'
|
||||
```
|
||||
|
||||
This option can't be used with the `forceHttp` option at the same time.
|
||||
|
||||
##### stripHash
|
||||
##### stripFragment
|
||||
|
||||
Type: `boolean`<br>
|
||||
Default: `true`
|
||||
|
||||
Removes hash from the URL.
|
||||
Remove the fragment at the end of the URL.
|
||||
|
||||
```js
|
||||
normalizeUrl('sindresorhus.com/about.html#contact');
|
||||
//=> 'http://sindresorhus.com/about.html'
|
||||
|
||||
normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: false});
|
||||
normalizeUrl('sindresorhus.com/about.html#contact', {stripFragment: false});
|
||||
//=> 'http://sindresorhus.com/about.html#contact'
|
||||
```
|
||||
|
||||
@@ -111,7 +87,7 @@ normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: false});
|
||||
Type: `boolean`<br>
|
||||
Default: `true`
|
||||
|
||||
Removes `www.` from the URL.
|
||||
Remove `www.` from the URL.
|
||||
|
||||
```js
|
||||
normalizeUrl('http://www.sindresorhus.com/about.html#contact');
|
||||
@@ -126,7 +102,7 @@ normalizeUrl('http://www.sindresorhus.com/about.html#contact', {stripWWW: false}
|
||||
Type: `Array<RegExp|string>`<br>
|
||||
Default: `[/^utm_\w+/i]`
|
||||
|
||||
Removes query parameters that matches any of the provided strings or regexes.
|
||||
Remove query parameters that matches any of the provided strings or regexes.
|
||||
|
||||
```js
|
||||
normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
|
||||
@@ -140,7 +116,7 @@ normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
|
||||
Type: `boolean`<br>
|
||||
Default: `true`
|
||||
|
||||
Removes trailing slash.
|
||||
Remove trailing slash.
|
||||
|
||||
**Note:** Trailing slash is always removed if the URL doesn't have a pathname.
|
||||
|
||||
@@ -160,7 +136,7 @@ normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
|
||||
Type: `boolean` `Array<RegExp|string>`<br>
|
||||
Default: `false`
|
||||
|
||||
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.
|
||||
Remove 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.
|
||||
|
||||
```js
|
||||
normalizeUrl('www.sindresorhus.com/foo/default.php', {
|
||||
@@ -169,20 +145,6 @@ normalizeUrl('www.sindresorhus.com/foo/default.php', {
|
||||
//=> 'http://sindresorhus.com/foo'
|
||||
```
|
||||
|
||||
##### sortQueryParameters
|
||||
|
||||
Type: `boolean`<br>
|
||||
Default: `true`
|
||||
|
||||
Sorts the query parameters alphabetically by key.
|
||||
|
||||
```js
|
||||
normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
|
||||
sortQueryParameters: false
|
||||
});
|
||||
//=> 'http://sindresorhus.com/?b=two&a=one&c=three'
|
||||
```
|
||||
|
||||
|
||||
## Related
|
||||
|
||||
|
||||
Reference in New Issue
Block a user