Installations
npm install normalize-url
Score
99.5
Supply Chain
99.5
Quality
77
Maintenance
100
Vulnerability
100
License
Developer
sindresorhus
Developer Guide
Module System
ESM
Min. Node Version
>=14.16
Typescript Support
Yes
Node Version
18.19.0
NPM Version
9.2.0
Statistics
840 Stars
166 Commits
122 Forks
16 Watching
1 Branches
43 Contributors
Updated on 15 Oct 2024
Bundle Size
3.63 kB
Minified
1.57 kB
Minified + Gzipped
Languages
JavaScript (95.02%)
TypeScript (4.98%)
Total Downloads
Cumulative downloads
Total Downloads
7,445,896,679
Last day
-7.4%
5,195,466
Compared to previous day
Last week
2.1%
30,936,972
Compared to previous week
Last month
10.6%
127,100,748
Compared to previous month
Last year
-6.3%
1,382,052,449
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
normalize-url
Normalize a URL
Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.
Note: This package does not do URL sanitization. Garbage in, garbage out. If you use this in a server context and accept URLs as user input, it's up to you to protect against invalid URLs, path traversal attacks, etc.
Install
1npm install normalize-url
Usage
1import normalizeUrl from 'normalize-url';
2
3normalizeUrl('sindresorhus.com');
4//=> 'http://sindresorhus.com'
5
6normalizeUrl('//www.sindresorhus.com:80/../baz?b=bar&a=foo');
7//=> 'http://sindresorhus.com/baz?a=foo&b=bar'
API
normalizeUrl(url, options?)
URLs with custom protocols are not normalized and just passed through by default. Supported protocols are: https
, http
, file
, and data
.
Human-friendly URLs with basic auth (for example, user:password@sindresorhus.com
) are not handled because basic auth conflicts with custom protocols. Basic auth URLs are also deprecated.
url
Type: string
URL to normalize, including data URL.
options
Type: object
defaultProtocol
Type: string
Default: 'http'
Values: 'https' | 'http'
normalizeProtocol
Type: boolean
Default: true
Prepend defaultProtocol
to the URL if it's protocol-relative.
1normalizeUrl('//sindresorhus.com');
2//=> 'http://sindresorhus.com'
3
4normalizeUrl('//sindresorhus.com', {normalizeProtocol: false});
5//=> '//sindresorhus.com'
forceHttp
Type: boolean
Default: false
Normalize HTTPS to HTTP.
1normalizeUrl('https://sindresorhus.com');
2//=> 'https://sindresorhus.com'
3
4normalizeUrl('https://sindresorhus.com', {forceHttp: true});
5//=> 'http://sindresorhus.com'
forceHttps
Type: boolean
Default: false
Normalize HTTP to HTTPS.
1normalizeUrl('http://sindresorhus.com');
2//=> 'http://sindresorhus.com'
3
4normalizeUrl('http://sindresorhus.com', {forceHttps: true});
5//=> 'https://sindresorhus.com'
This option cannot be used with the forceHttp
option at the same time.
stripAuthentication
Type: boolean
Default: true
Strip the authentication part of the URL.
1normalizeUrl('https://user:password@sindresorhus.com');
2//=> 'https://sindresorhus.com'
3
4normalizeUrl('https://user:password@sindresorhus.com', {stripAuthentication: false});
5//=> 'https://user:password@sindresorhus.com'
stripHash
Type: boolean
Default: false
Strip the hash part of the URL.
1normalizeUrl('sindresorhus.com/about.html#contact');
2//=> 'http://sindresorhus.com/about.html#contact'
3
4normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
5//=> 'http://sindresorhus.com/about.html'
stripProtocol
Type: boolean
Default: false
Remove the protocol from the URL: http://sindresorhus.com
→ sindresorhus.com
.
It will only remove https://
and http://
protocols.
1normalizeUrl('https://sindresorhus.com');
2//=> 'https://sindresorhus.com'
3
4normalizeUrl('https://sindresorhus.com', {stripProtocol: true});
5//=> 'sindresorhus.com'
stripTextFragment
Type: boolean
Default: true
Strip the text fragment part of the URL.
Note: The text fragment will always be removed if the stripHash
option is set to true
, as the hash contains the text fragment.
1normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello');
2//=> 'http://sindresorhus.com/about.html#'
3
4normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello');
5//=> 'http://sindresorhus.com/about.html#section'
6
7normalizeUrl('http://sindresorhus.com/about.html#:~:text=hello', {stripTextFragment: false});
8//=> 'http://sindresorhus.com/about.html#:~:text=hello'
9
10normalizeUrl('http://sindresorhus.com/about.html#section:~:text=hello', {stripTextFragment: false});
11//=> 'http://sindresorhus.com/about.html#section:~:text=hello'
stripWWW
Type: boolean
Default: true
Remove www.
from the URL.
1normalizeUrl('http://www.sindresorhus.com');
2//=> 'http://sindresorhus.com'
3
4normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});
5//=> 'http://www.sindresorhus.com'
removeQueryParameters
Type: Array<RegExp | string> | boolean
Default: [/^utm_\w+/i]
Remove query parameters that matches any of the provided strings or regexes.
1normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
2 removeQueryParameters: ['ref']
3});
4//=> 'http://sindresorhus.com/?foo=bar'
If a boolean is provided, true
will remove all the query parameters.
1normalizeUrl('www.sindresorhus.com?foo=bar', { 2 removeQueryParameters: true 3}); 4//=> 'http://sindresorhus.com'
false
will not remove any query parameter.
1normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', {
2 removeQueryParameters: false
3});
4//=> 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test'
keepQueryParameters
Type: Array<RegExp | string>
Default: undefined
Keeps only query parameters that matches any of the provided strings or regexes.
Note: It overrides the removeQueryParameters
option.
1normalizeUrl('https://sindresorhus.com?foo=bar&ref=unicorn', {
2 keepQueryParameters: ['ref']
3});
4//=> 'https://sindresorhus.com/?ref=unicorn'
removeTrailingSlash
Type: boolean
Default: true
Remove trailing slash.
Note: Trailing slash is always removed if the URL doesn't have a pathname unless the removeSingleSlash
option is set to false
.
1normalizeUrl('http://sindresorhus.com/redirect/');
2//=> 'http://sindresorhus.com/redirect'
3
4normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
5//=> 'http://sindresorhus.com/redirect/'
6
7normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
8//=> 'http://sindresorhus.com'
removeSingleSlash
Type: boolean
Default: true
Remove a sole /
pathname in the output. This option is independent of removeTrailingSlash
.
1normalizeUrl('https://sindresorhus.com/');
2//=> 'https://sindresorhus.com'
3
4normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
5//=> 'https://sindresorhus.com/'
removeDirectoryIndex
Type: boolean | Array<RegExp | string>
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.
1normalizeUrl('www.sindresorhus.com/foo/default.php', {
2 removeDirectoryIndex: [/^default\.[a-z]+$/]
3});
4//=> 'http://sindresorhus.com/foo'
removeExplicitPort
Type: boolean
Default: false
Removes an explicit port number from the URL.
Port 443 is always removed from HTTPS URLs and 80 is always removed from HTTP URLs regardless of this option.
1normalizeUrl('sindresorhus.com:123', { 2 removeExplicitPort: true 3}); 4//=> 'http://sindresorhus.com'
sortQueryParameters
Type: boolean
Default: true
Sorts the query parameters alphabetically by key.
1normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
2 sortQueryParameters: false
3});
4//=> 'http://sindresorhus.com/?b=two&a=one&c=three'
Related
- compare-urls - Compare URLs by first normalizing them
Stable Version
The latest stable version of the package.
Stable Version
8.0.1
HIGH
3
7.5/10
Summary
ReDoS in normalize-url
Affected Versions
>= 4.3.0, < 4.5.1
Patched Versions
4.5.1
7.5/10
Summary
ReDoS in normalize-url
Affected Versions
>= 6.0.0, < 6.0.1
Patched Versions
6.0.1
7.5/10
Summary
ReDoS in normalize-url
Affected Versions
>= 5.0.0, < 5.3.1
Patched Versions
5.3.1
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
- Info: security policy file detected: .github/security.md:1
- Info: Found linked content: .github/security.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: .github/security.md:1
- Info: Found text in security policy: .github/security.md:1
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
0 existing vulnerabilities detected
Reason
Found 7/30 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/normalize-url/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/normalize-url/main.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/normalize-url/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:22
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 8 are checked with a SAST tool
Score
4.2
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to normalize-url
@esm2cjs/normalize-url
Normalize a URL. This is a fork of sindresorhus/normalize-url, but with CommonJS support.
postcss-normalize-url
Normalize URLs with PostCSS
normalize-url-plus
normalize-url plus additional features to supercharge link normalization!
cssnano-preset-default
Safe defaults for cssnano which require minimal configuration.