diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/normalize-url | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/normalize-url')
| -rw-r--r-- | node_modules/normalize-url/index.js | 161 | ||||
| -rw-r--r-- | node_modules/normalize-url/license | 21 | ||||
| -rw-r--r-- | node_modules/normalize-url/package.json | 50 | ||||
| -rw-r--r-- | node_modules/normalize-url/readme.md | 156 |
4 files changed, 388 insertions, 0 deletions
diff --git a/node_modules/normalize-url/index.js b/node_modules/normalize-url/index.js new file mode 100644 index 00000000..f4aa1814 --- /dev/null +++ b/node_modules/normalize-url/index.js @@ -0,0 +1,161 @@ +'use strict'; +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'); + +var DEFAULT_PORTS = { + 'http:': 80, + 'https:': 443, + 'ftp:': 21 +}; + +// 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, + normalizeHttps: false, + stripFragment: true, + stripWWW: true, + removeQueryParameters: [/^utm_\w+/i], + removeTrailingSlash: true, + removeDirectoryIndex: false + }, opts); + + if (typeof str !== 'string') { + throw new TypeError('Expected a string'); + } + + var hasRelativeProtocol = str.indexOf('//') === 0; + + // prepend protocol + str = prependHttp(str.trim()).replace(/^\/\//, 'http://'); + + var urlObj = url.parse(str); + + if (opts.normalizeHttps && urlObj.protocol === 'https:') { + urlObj.protocol = 'http:'; + } + + if (!urlObj.hostname && !urlObj.pathname) { + throw new Error('Invalid URL'); + } + + // prevent these from being used by `url.format` + delete urlObj.host; + delete urlObj.query; + + // remove fragment + if (opts.stripFragment) { + delete urlObj.hash; + } + + // remove default port + var port = DEFAULT_PORTS[urlObj.protocol]; + if (Number(urlObj.port) === port) { + delete urlObj.port; + } + + // remove duplicate slashes + if (urlObj.pathname) { + urlObj.pathname = urlObj.pathname.replace(/\/{2,}/g, '/'); + } + + // decode URI octets + if (urlObj.pathname) { + urlObj.pathname = decodeURI(urlObj.pathname); + } + + // remove directory index + if (opts.removeDirectoryIndex === true) { + opts.removeDirectoryIndex = [/^index\.[a-z]+$/]; + } + + 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); + urlObj.pathname = pathComponents.slice(1).join('/') + '/'; + } + } + + // 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) { + // IDN to Unicode + urlObj.hostname = punycode.toUnicode(urlObj.hostname).toLowerCase(); + + // remove trailing dot + urlObj.hostname = urlObj.hostname.replace(/\.$/, ''); + + // remove `www.` + if (opts.stripWWW) { + urlObj.hostname = urlObj.hostname.replace(/^www\./, ''); + } + } + + // 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 (var key in queryParameters) { + if (testParameter(key, opts.removeQueryParameters)) { + delete queryParameters[key]; + } + } + } + + // sort query parameters + urlObj.search = queryString.stringify(sortKeys(queryParameters)); + + // decode query parameters + urlObj.search = decodeURIComponent(urlObj.search); + + // take advantage of many of the Node `url` normalizations + str = url.format(urlObj); + + // remove ending `/` + if (opts.removeTrailingSlash || urlObj.pathname === '/') { + str = str.replace(/\/$/, ''); + } + + // restore relative protocol, if applicable + if (hasRelativeProtocol && !opts.normalizeProtocol) { + str = str.replace(/^http:\/\//, '//'); + } + + return str; +}; diff --git a/node_modules/normalize-url/license b/node_modules/normalize-url/license new file mode 100644 index 00000000..654d0bfe --- /dev/null +++ b/node_modules/normalize-url/license @@ -0,0 +1,21 @@ +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: + +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. diff --git a/node_modules/normalize-url/package.json b/node_modules/normalize-url/package.json new file mode 100644 index 00000000..b255039b --- /dev/null +++ b/node_modules/normalize-url/package.json @@ -0,0 +1,50 @@ +{ + "name": "normalize-url", + "version": "1.9.1", + "description": "Normalize a URL", + "license": "MIT", + "repository": "sindresorhus/normalize-url", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=4" + }, + "scripts": { + "test": "xo && ava" + }, + "files": [ + "index.js" + ], + "keywords": [ + "normalize", + "url", + "uri", + "address", + "string", + "str", + "normalise", + "normalization", + "normalisation", + "query", + "string", + "querystring", + "unicode", + "simplify", + "strip", + "trim", + "canonical" + ], + "dependencies": { + "object-assign": "^4.0.1", + "prepend-http": "^1.0.0", + "query-string": "^4.1.0", + "sort-keys": "^1.0.0" + }, + "devDependencies": { + "ava": "*", + "xo": "^0.16.0" + } +} diff --git a/node_modules/normalize-url/readme.md b/node_modules/normalize-url/readme.md new file mode 100644 index 00000000..51f624aa --- /dev/null +++ b/node_modules/normalize-url/readme.md @@ -0,0 +1,156 @@ +# normalize-url [](https://travis-ci.org/sindresorhus/normalize-url) + +> [Normalize](http://en.wikipedia.org/wiki/URL_normalization) a URL + +Useful when you need to display, store, deduplicate, sort, compare, etc, URLs. + + +## Install + +``` +$ npm install --save normalize-url +``` + + +## Usage + +```js +const normalizeUrl = require('normalize-url'); + +normalizeUrl('sindresorhus.com'); +//=> 'http://sindresorhus.com' + +normalizeUrl('HTTP://xn--xample-hva.com:80/?b=bar&a=foo'); +//=> 'http://êxample.com/?a=foo&b=bar' +``` + + +## API + +### normalizeUrl(url, [options]) + +#### url + +Type: `string` + +URL to normalize. + +#### options + +##### normalizeProtocol + +Type: `boolean`<br> +Default: `true` + +Prepend `http:` to the URL if it's protocol-relative. + +```js +normalizeUrl('//sindresorhus.com:80/'); +//=> 'http://sindresorhus.com' + +normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false}); +//=> '//sindresorhus.com' +``` + +##### normalizeHttps + +Type: `boolean`<br> +Default: `false` + +Normalize `https:` URLs to `http:`. + +```js +normalizeUrl('https://sindresorhus.com:80/'); +//=> 'https://sindresorhus.com' + +normalizeUrl('https://sindresorhus.com:80/', {normalizeHttps: true}); +//=> 'http://sindresorhus.com' +``` + +##### stripFragment + +Type: `boolean`<br> +Default: `true` + +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', {stripFragment: false}); +//=> 'http://sindresorhus.com/about.html#contact' +``` + +##### stripWWW + +Type: `boolean`<br> +Default: `true` + +Remove `www.` from the URL. + +```js +normalizeUrl('http://www.sindresorhus.com/about.html#contact'); +//=> 'http://sindresorhus.com/about.html#contact' + +normalizeUrl('http://www.sindresorhus.com/about.html#contact', {stripWWW: false}); +//=> 'http://www.sindresorhus.com/about.html#contact' +``` + +##### removeQueryParameters + +Type: `Array<RegExp|string>`<br> +Default: `[/^utm_\w+/i]` + +Remove query parameters that matches any of the provided strings or regexes. + +```js +normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', { + removeQueryParameters: ['ref'] +}); +//=> 'http://sindresorhus.com/?foo=bar' +``` + +##### removeTrailingSlash + +Type: `boolean`<br> +Default: `true` + +Remove trailing slash. + +**Note:** Trailing slash is always removed if the URL doesn't have a pathname. + +```js +normalizeUrl('http://sindresorhus.com/redirect/'); +//=> 'http://sindresorhus.com/redirect' + +normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false}); +//=> 'http://sindresorhus.com/redirect/' + +normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false}); +//=> 'http://sindresorhus.com' +``` + +##### removeDirectoryIndex + +Type: `boolean` `Array<RegExp|string>`<br> +Default: `false` + +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', { + removeDirectoryIndex: [/^default\.[a-z]+$/] +}); +//=> 'http://sindresorhus.com/foo' +``` + + +## Related + +- [compare-urls](https://github.com/sindresorhus/compare-urls) - Compare URLs by first normalizing them + + +## License + +MIT © [Sindre Sorhus](https://sindresorhus.com) |
