Socket
Socket
Sign inDemoInstall

uri-js

Package Overview
Dependencies
0
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    uri-js

An RFC 3986/3987 compliant, scheme extendable URI/IRI parsing/validating/resolving library for JavaScript.


Version published
Weekly downloads
40M
decreased by-1.35%
Maintainers
1
Install size
199 kB
Created
Weekly downloads
 

Package description

What is uri-js?

The uri-js package is a utility for working with URIs (Uniform Resource Identifiers) in JavaScript. It provides functions for parsing, serializing, normalizing, and resolving URIs, as well as working with individual URI components.

What are uri-js's main functionalities?

Parsing URIs

This feature allows you to parse a URI string into its components, such as scheme, authority, path, query, and fragment.

const URI = require('uri-js');
const parsedURI = URI.parse('https://example.com:8080/path?query=string#fragment');

Serializing URIs

This feature allows you to build a URI string from its individual components.

const URI = require('uri-js');
const components = {
  scheme: 'https',
  userinfo: 'user:pass',
  host: 'example.com',
  port: 8080,
  path: '/path',
  query: 'query=string',
  fragment: 'fragment'
};
const serializedURI = URI.serialize(components);

Normalizing URIs

This feature allows you to normalize a URI by converting it to its canonical form, which is useful for URI comparison.

const URI = require('uri-js');
const normalizedURI = URI.normalize('HTTP://EXAMPLE.COM:80/a/../b/./c%2f?%61');

Resolving URIs

This feature allows you to resolve a relative URI against a base URI, resulting in an absolute URI.

const URI = require('uri-js');
const baseURI = 'http://example.com/dir/';
const relativeURI = '../other';
const resolvedURI = URI.resolve(baseURI, relativeURI);

Other packages similar to uri-js

Readme

Source

URI.js

URI.js is an RFC 3986 compliant, scheme extendable URI parsing/validating/resolving library for all JavaScript environments (browsers, Node.js, etc). It is also compliant with the IRI (RFC 3987) and IDNA (RFC 5890) specifications.

URI.js weighs in at only 2.3kb (gzipped, 7kb deflated). Need IRI support? It's only an extra 1.3kb (3.6kb gzipped, 9.8kb deflated).

API

Parsing & Validating

var components = URI.parse("uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body");
//returns:
//{
//  scheme : "uri",
//  userinfo : "user:pass",
//  host : "example.com",
//  port : 123,
//  path : "/one/two.three",
//  query : "q1=a1&q2=a2",
//  fragment : "body"
//}

Serializing

URI.serialize({scheme : "http", host : "example.com", fragment : "footer"}) === "http://example.com/#footer"

Resolving

URI.resolve("uri://a/b/c/d?q", "../../g") === "uri://a/g"

Normalizing

URI.normalize("HTTP://ABC.com:80/%7Esmith/home.html") === "http://abc.com/~smith/home.html"

Comparison

URI.equal("example://a/b/c/%7Bfoo%7D", "eXAMPLE://a/./b/../b/%63/%7bfoo%7d") === true

IRI Support

//convert IRI to URI
URI.serialize(URI.parse("http://exampl�.org/ros�")) === "http://xn--exampl-gva.org/ros%C3%A9"
//convert URI to IRI
URI.serialize(URI.parse("http://xn--exampl-gva.org/ros%C3%A9"), {iri:true}) === "http://exampl�.org/ros�"

Options

All of the above functions can accept an additional options argument that is an object that can contain one or more of the following properties:

  • scheme (string)

    Indicates the scheme that the URI should be treated as, overriding the URI's normal scheme parsing behavior.

  • reference (string)

    If set to "suffix", it indicates that the URI is in the suffix format, and the validator will use the option's scheme property to determine the URI's scheme.

  • tolerant (boolean, false)

    If set to true, the parser will not report invalid URIs. It will also relax URI resolving rules.

  • absolutePath (boolean, false)

    If set to true, the serializer will not resolve a relative path component.

  • iri (boolean, false)

    If set to true, the serializer will unescape non-ASCII characters as per RFC 3987.

  • unicodeSupport (boolean, false)

    If set to true, the parser will unescape non-ASCII characters in the parsed output as per RFC 3987.

  • domainHost (boolean, false)

    If set to true, the library will treat the host component as a domain name, and convert IDNs (International Domain Names) as per RFC 5891.

Scheme Extendable

URI.js supports inserting custom scheme dependent processing rules. Currently, URI.js has built in support for the following schemes:

Note: The minified version of URI.js only comes with http/https support compiled in.

HTTP Support

URI.equal("HTTP://ABC.COM:80", "http://abc.com/") === true

Mailto Support

URI.parse("mailto:alpha@example.com,bravo@example.com?subject=SUBSCRIBE&body=Sign%20me%20up!");
//returns:
//{
//	scheme : "mailto", 
//	to : ["alpha@example.com", "bravo@example.com"],
//	subject : "SUBSCRIBE",
//	body : "Sign me up!"
//}

URI.serialize({
	scheme : "mailto", 
	to : ["alpha@example.com"], 
	subject : "REMOVE", 
	body : "Please remove me",
	headers : {
		cc : "charlie@example.com"
	}
}) === "mailto:alpha@example.com?cc=charlie@example.com&subject=REMOVE&body=Please%20remove%20me"

Usage

To load in a browser, use the following tag:

<script type="text/javascript" src="uri-js/dist/uri.min.js"></script>

If you need IRI support, use the following tag instead:

<script type="text/javascript" src="uri-js/dist/uri-iri.min.js"></script>

To load in a CommonJS (Node.js/io.js) environment, first install with npm by running on the command line:

npm install uri-js

Then, in your code, load it using:

var URI = require("uri-js");

Breaking changes from 1.x

The errors array on parsed components as now been changed to an error string.

License

Copyright 2011 Gary Court. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY GARY COURT "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARY COURT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of Gary Court.

Keywords

FAQs

Last updated on 08 Jul 2015

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc