Socket
Socket
Sign inDemoInstall

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 2.0.1 to 3.0.0

93

index.js
'use strict';
const url = require('url');
const punycode = require('punycode');
const queryString = require('query-string');
const prependHttp = require('prepend-http');
const sortKeys = require('sort-keys');
const {URL} = require('url');
const DEFAULT_PORTS = {
'http:': 80,
'https:': 443,
'ftp:': 21
};
// Protocols that always contain a `//`` bit
const 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) {

@@ -32,3 +8,3 @@ return filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name);

module.exports = (str, opts) => {
module.exports = (urlString, opts) => {
opts = Object.assign({

@@ -45,12 +21,13 @@ normalizeProtocol: true,

if (typeof str !== 'string') {
throw new TypeError('Expected a string');
}
urlString = urlString.trim();
const hasRelativeProtocol = str.startsWith('//');
const hasRelativeProtocol = urlString.startsWith('//');
const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString);
// Prepend protocol
str = prependHttp(str.trim()).replace(/^\/\//, 'http://');
if (!isRelativeUrl) {
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, 'http://');
}
const urlObj = url.parse(str);
const urlObj = new URL(urlString);

@@ -61,21 +38,7 @@ if (opts.normalizeHttps && urlObj.protocol === 'https:') {

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;
urlObj.hash = '';
}
// Remove default port
const port = DEFAULT_PORTS[urlObj.protocol];
if (Number(urlObj.port) === port) {
delete urlObj.port;
}
// Remove duplicate slashes

@@ -106,13 +69,3 @@ if (urlObj.pathname) {

// Resolve relative paths, but only for slashed protocols
if (slashedProtocol[urlObj.protocol]) {
const domain = urlObj.protocol + '//' + urlObj.hostname;
const 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

@@ -127,14 +80,7 @@ urlObj.hostname = urlObj.hostname.replace(/\.$/, '');

// Remove URL with empty query string
if (urlObj.search === '?') {
delete urlObj.search;
}
const queryParameters = queryString.parse(urlObj.search);
// Remove query unwanted parameters
if (Array.isArray(opts.removeQueryParameters)) {
for (const key in queryParameters) {
for (const key of [...urlObj.searchParams.keys()]) {
if (testParameter(key, opts.removeQueryParameters)) {
delete queryParameters[key];
urlObj.searchParams.delete(key);
}

@@ -146,16 +92,11 @@ }

if (opts.sortQueryParameters) {
urlObj.search = queryString.stringify(sortKeys(queryParameters));
urlObj.searchParams.sort();
}
// Decode query parameters
if (urlObj.search !== null) {
urlObj.search = decodeURIComponent(urlObj.search);
}
// Take advantage of many of the Node `url` normalizations
str = url.format(urlObj);
urlString = urlObj.toString();
// Remove ending `/`
if (opts.removeTrailingSlash || urlObj.pathname === '/') {
str = str.replace(/\/$/, '');
urlString = urlString.replace(/\/$/, '');
}

@@ -165,6 +106,6 @@

if (hasRelativeProtocol && !opts.normalizeProtocol) {
str = str.replace(/^http:\/\//, '//');
urlString = urlString.replace(/^http:\/\//, '//');
}
return str;
return urlString;
};

82

package.json
{
"name": "normalize-url",
"version": "2.0.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",
"normalization",
"normalisation",
"query",
"querystring",
"unicode",
"simplify",
"strip",
"trim",
"canonical"
],
"dependencies": {
"prepend-http": "^2.0.0",
"query-string": "^5.0.1",
"sort-keys": "^2.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "normalize-url",
"version": "3.0.0",
"description": "Normalize a URL",
"license": "MIT",
"repository": "sindresorhus/normalize-url",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"normalize",
"url",
"uri",
"address",
"string",
"normalization",
"normalisation",
"query",
"querystring",
"simplify",
"strip",
"trim",
"canonical"
],
"devDependencies": {
"ava": "*",
"xo": "*"
}
}
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