Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

normalize-url

Package Overview
Dependencies
Maintainers
2
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

normalize-url - npm Package Compare versions

Comparing version 3.2.0 to 3.3.0

48

index.js

@@ -5,12 +5,13 @@ 'use strict';

function testParameter(name, filters) {
const testParameter = (name, filters) => {
return filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);
}
};
module.exports = (urlString, opts) => {
opts = Object.assign({
defaultProtocol: 'http:',
normalizeProtocol: true,
normalizeHttps: false,
normalizeHttp: false,
stripFragment: true,
forceHttp: false,
forceHttps: false,
stripHash: true,
stripWWW: true,

@@ -23,2 +24,15 @@ removeQueryParameters: [/^utm_\w+/i],

// Backwards compatibility
if (Reflect.has(opts, 'normalizeHttps')) {
opts.forceHttp = opts.normalizeHttps;
}
if (Reflect.has(opts, 'normalizeHttp')) {
opts.forceHttps = opts.normalizeHttp;
}
if (Reflect.has(opts, 'stripFragment')) {
opts.stripHash = opts.stripFragment;
}
urlString = urlString.trim();

@@ -31,3 +45,3 @@

if (!isRelativeUrl) {
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, 'http://');
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, opts.defaultProtocol);
}

@@ -37,16 +51,16 @@

if (opts.normalizeHttps && opts.normalizeHttp) {
throw new Error('The `normalizeHttp` and `normalizeHttps` options cannot be used together');
if (opts.forceHttp && opts.forceHttps) {
throw new Error('The `forceHttp` and `forceHttps` options cannot be used together');
}
if (opts.normalizeHttp && urlObj.protocol === 'http:') {
urlObj.protocol = 'https:';
if (opts.forceHttp && urlObj.protocol === 'https:') {
urlObj.protocol = 'http:';
}
if (opts.normalizeHttps && urlObj.protocol === 'https:') {
urlObj.protocol = 'http:';
if (opts.forceHttps && urlObj.protocol === 'http:') {
urlObj.protocol = 'https:';
}
// Remove fragment
if (opts.stripFragment) {
// Remove hash
if (opts.stripHash) {
urlObj.hash = '';

@@ -92,3 +106,7 @@ }

// Remove `www.`
if (opts.stripWWW) {
// 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
urlObj.hostname = urlObj.hostname.replace(/^www\./, '');

@@ -95,0 +113,0 @@ }

{
"name": "normalize-url",
"version": "3.2.0",
"version": "3.3.0",
"description": "Normalize a URL",

@@ -16,3 +16,3 @@ "license": "MIT",

"scripts": {
"test": "xo && ava"
"test": "xo && nyc ava"
},

@@ -39,4 +39,6 @@ "files": [

"ava": "*",
"coveralls": "^3.0.0",
"nyc": "^12.0.2",
"xo": "*"
}
}

@@ -1,2 +0,2 @@

# normalize-url [![Build Status](https://travis-ci.org/sindresorhus/normalize-url.svg?branch=master)](https://travis-ci.org/sindresorhus/normalize-url)
# normalize-url [![Build Status](https://travis-ci.org/sindresorhus/normalize-url.svg?branch=master)](https://travis-ci.org/sindresorhus/normalize-url) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/normalize-url/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/normalize-url?branch=master)

@@ -42,2 +42,7 @@ > [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL

##### defaultProtocol
Type: `string`<br>
Default: `http:`
##### normalizeProtocol

@@ -48,3 +53,3 @@

Prepend `http:` to the URL if it's protocol-relative.
Prepends `defaultProtocol` to the URL if it's protocol-relative.

@@ -59,3 +64,3 @@ ```js

##### normalizeHttps
##### forceHttp

@@ -65,3 +70,3 @@ Type: `boolean`<br>

Normalize `https:` URLs to `http:`.
Normalizes `https:` URLs to `http:`.

@@ -76,3 +81,3 @@ ```js

##### normalizeHttp
##### forceHttps

@@ -82,3 +87,3 @@ Type: `boolean`<br>

Normalize `http:` URLs to `https:`.
Normalizes `http:` URLs to `https:`.

@@ -93,5 +98,5 @@ ```js

This option is mutually exclusive with the `normalizeHttps` option.
This option can't be used with the `forceHttp` option at the same time.
##### stripFragment
##### stripHash

@@ -101,3 +106,3 @@ Type: `boolean`<br>

Remove the fragment at the end of the URL.
Removes hash from the URL.

@@ -108,3 +113,3 @@ ```js

normalizeUrl('sindresorhus.com/about.html#contact', {stripFragment: false});
normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: false});
//=> 'http://sindresorhus.com/about.html#contact'

@@ -118,3 +123,3 @@ ```

Remove `www.` from the URL.
Removes `www.` from the URL.

@@ -134,3 +139,3 @@ ```js

Remove query parameters that matches any of the provided strings or regexes.
Removes query parameters that matches any of the provided strings or regexes.

@@ -149,3 +154,3 @@ ```js

Remove trailing slash.
Removes trailing slash.

@@ -170,3 +175,3 @@ **Note:** Trailing slash is always removed if the URL doesn't have a pathname.

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.
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.

@@ -185,3 +190,3 @@ ```js

Sort the query parameters alphabetically by key.
Sorts the query parameters alphabetically by key.

@@ -188,0 +193,0 @@ ```js

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc