![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
url-toolkit
Advanced tools
Build an absolute URL from a base URL and a relative URL (RFC 1808). No dependencies!
The url-toolkit npm package provides utilities for parsing, resolving, and normalizing URLs. It is particularly useful for handling URL manipulations in web applications, ensuring that URLs are correctly formatted and resolved.
Parsing URLs
This feature allows you to parse a URL into its components such as protocol, hostname, pathname, search, and hash. The code sample demonstrates how to parse a URL and log its components.
const URLToolkit = require('url-toolkit');
const parsedURL = URLToolkit.parseURL('https://example.com/path?query=123#hash');
console.log(parsedURL);
Resolving URLs
This feature allows you to resolve a relative URL against a base URL to get an absolute URL. The code sample shows how to resolve a relative URL to an absolute URL.
const URLToolkit = require('url-toolkit');
const resolvedURL = URLToolkit.buildAbsoluteURL('https://example.com/path/', '../newpath');
console.log(resolvedURL);
Normalizing URLs
This feature allows you to normalize a URL path, resolving any '..' or '.' segments. The code sample demonstrates how to normalize a URL path.
const URLToolkit = require('url-toolkit');
const normalizedURL = URLToolkit.normalizePath('https://example.com/path/../newpath');
console.log(normalizedURL);
The 'url' package is a core Node.js module that provides utilities for URL resolution and parsing. It offers similar functionalities to url-toolkit, such as parsing URLs and resolving relative URLs. However, url-toolkit provides additional features like URL normalization.
The 'whatwg-url' package is a full implementation of the WHATWG URL Standard. It provides comprehensive URL parsing, serialization, and manipulation functionalities. Compared to url-toolkit, whatwg-url is more standards-compliant and offers a broader range of URL manipulation features.
The 'url-parse' package is a lightweight URL parser that works in both Node.js and the browser. It provides similar functionalities to url-toolkit, such as parsing and resolving URLs. However, url-parse is designed to be more lightweight and faster.
Lightweight library to build an absolute URL from a base URL and a relative URL, written from the spec (RFC 1808). Initially part of HLS.JS.
URL()
The JS URL() function also lets you calculate a new URL from a base and relative one.
That uses the URL Living Standard which is slightly different to RFC 1808 that this library implements.
One of the key differences is that the URL Living Standard has the concept of a 'special url' and 'special scheme'. For these special URL's, such as a URL with the http
scheme, they normalise them in a way that results in http:///example.com/something
becoming http://example.com/something
. This library does not do that and parseURL()
would give you //
as the netLoc
and /example.com
as the path.
buildAbsoluteURL(baseURL, relativeURL, opts={})
Build an absolute URL from a relative and base one.
URLToolkit.buildAbsoluteURL('http://a.com/b/cd', 'e/f/../g'); // => http://a.com/b/e/g
If you want to ensure that the URL is treated as a relative one you should prefix it with ./
.
URLToolkit.buildAbsoluteURL('http://a.com/b/cd', 'a:b'); // => a:b
URLToolkit.buildAbsoluteURL('http://a.com/b/cd', './a:b'); // => http://a.com/b/a:b
By default the paths will not be normalized unless necessary, according to the spec. However you can ensure paths are always normalized by setting the opts.alwaysNormalize
option to true
.
URLToolkit.buildAbsoluteURL('http://a.com/b/cd', '/e/f/../g'); // => http://a.com/e/f/../g
URLToolkit.buildAbsoluteURL('http://a.com/b/cd', '/e/f/../g', {
alwaysNormalize: true,
}); // => http://a.com/e/g
normalizePath(url)
Normalizes a path.
URLToolkit.normalizePath('a/b/../c'); // => a/c
parseURL(url)
Parse a URL into its separate components.
URLToolkit.parseURL('http://a/b/c/d;p?q#f'); // =>
/* {
scheme: 'http:',
netLoc: '//a',
path: '/b/c/d',
params: ';p',
query: '?q',
fragment: '#f'
} */
buildURLFromParts(parts)
Puts all the parts from parseURL()
back together into a string.
var URLToolkit = require('url-toolkit');
var url = URLToolkit.buildAbsoluteURL(
'https://a.com/b/cd/e.m3u8?test=1#something',
'../z.ts?abc=1#test'
);
console.log(url); // 'https://a.com/b/z.ts?abc=1#test'
This can also be used in the browser thanks to jsDelivr:
<head>
<script
type="text/javascript"
src="https://cdn.jsdelivr.net/npm/url-toolkit@2"
></script>
<script type="text/javascript">
var url = URLToolkit.buildAbsoluteURL(
'https://a.com/b/cd/e.m3u8?test=1#something',
'../z.ts?abc=1#test'
);
console.log(url); // 'https://a.com/b/z.ts?abc=1#test'
</script>
</head>
FAQs
Build an absolute URL from a base URL and a relative URL (RFC 1808). No dependencies!
We found that url-toolkit demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.