npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

catberry-uri

v3.2.2

Published

URI parser strictly according to RFC 3986

Downloads

87

Readme

URI Parser for Catberry Framework

Build Status codecov.io

Installation

npm install catberry-uri

Description

This is well-tested URI parser implementation that has been developed strictly according to RFC 3986.

It supports percent-encoding for all URI components except scheme and port. Percent encoding/decoding happens automatically.

It validates scheme and port component values during URI recombination.

It implements relative reference resolution algorithm from RFC 3986 5.2.

Usage

Parse URI

const catberryURI = require('catberry-uri');
const URI = catberryURI.URI;
const uri = new URI('http://user:[email protected]:3000/some/path?some=value&some2=value&some2=value2&some3#fragment');
console.log(uri);

And you will get such object

 scheme: 'http',
  authority:
   { userInfo: { user: 'user', password: 'pass' },
     port: '3000',
     host: 'example.org' },
  path: '/some/path',
  query: { values: { some: 'value', some2: [ 'value', 'value2' ], some3: null } },
  fragment: 'fragment' }

This object is instance of URI constructor, authority field is instance of Authority, authority.userInfo is instance of UserInfo and query is instance of Query.

Get URI string

You can safely edit every component of parsed URI and then get URI string back. When you convert URI object to string scheme and port values are validated and could throw exception if values are not satisfy /^[a-z]+[a-z\d\+\.-]*$/i and /^\d+$/ respectively.

To get string representation of all these URI components just use toString()

uri.authority.userInfo.toString(); // user:pass
uri.authority.toString(); // user:[email protected]:3000
uri.query.toString(); // some=value&some2=value
console.log(uri.toString());
// http://user:[email protected]:3000/some/path?some=value&some2=value&some2=value2&some3#fragment

Building new URI

Also, every URI component including URI itself has clone method to create a cloned URI component for building another URI.

You can create new empty URI like this:

const uri = new URI();
uri.scheme = 'http';
// there is a static method in the URI class
uri.authority = URI.createAuthority();
// and also, an instance method
uri.authority.userInfo = uri.createUserInfo();
uri.authority.userInfo.user = 'user';
uri.authority.userInfo.password = 'pass';
uri.authority.host = 'example.org';
uri.authority.port = '3000';
uri.path = '/some/path';
uri.query = uri.createQuery();
uri.query.values = {
	some: 'value',
	some2: [ 'value', 'value2' ],
	some3: null
}
uri.fragment = 'fragment';

console.log(uri.toString());
// http://user:[email protected]:3000/some/path?some=value&some2=value&some2=value2&some3#fragment

All URI components are optional and null by default. null and undefined values of every component are ignored otherwise any value will be converted to string.

Resolve relative URI

Also, you can resolve any relative URI using base URI.

const uri = new URI('../../../../g');
const baseUri = new URI('http://a/b/c/d;p?q');

const absoluteUri = uri.resolveRelative(baseUri);
console.log(absoluteUri.toString()); // http://a/g

Interface

class URI {

	/**
	 * Creates a new URI authority component.
	 * @param {string?} Existing string.
	 * @return {Authority} The authority component.
	 */
	static createAuthority(string) {}

	/**
	 * Creates a new URI authority component.
	 * @param {string?} Existing string.
	 * @return {Authority} The authority component.
	 */
	createAuthority(string) {}

	/**
	 * Creates a new URI user info component.
	 * @param {string?} Existing string.
	 * @return {UserInfo} The user info component.
	 */
	static createUserInfo(string) {}

	/**
	 * Creates a new URI user info component.
	 * @param {string?} Existing string.
	 * @return {UserInfo} The user info component.
	 */
	createUserInfo(string) {}

	/**
	 * Creates a new URI query component.
	 * @param {string?} Existing string.
	 * @return {Query} The query component.
	 */
	static createQuery(string)}

	/**
	 * Creates a new URI query component.
	 * @param {string?} Existing string.
	 * @return {Query} The query component.
	 */
	createQuery(string) {}

	/**
	 * Creates new instance of URI according to RFC 3986.
	 * @param {string?} uriString URI string to parse components.
	 */
	constructor(uriString) {}

	/**
	 * Converts a URI reference that might be relative to a given base URI
	 * into the reference's target URI.
	 * https://tools.ietf.org/html/rfc3986#section-5.2
	 * @param {URI} baseUri Base URI.
	 * @returns {URI} Resolved URI.
	 */
	resolveRelative(baseUri) {}

	/**
	 * Clones current URI to a new object.
	 * @returns {URI} New clone of current object.
	 */
	clone() {}

	/**
	 * Recomposes URI components to URI string,
	 * https://tools.ietf.org/html/rfc3986#section-5.3
	 * @returns {string} URI string.
	 */
	toString() {}
}

Contributing

There are a lot of ways to contribute:

Denis Rechkunov [email protected]