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

url-relation

v1.0.0-alpha10

Published

Determine the relation between two URLs.

Downloads

11

Readme

url-relation NPM Version File Size Build Status Coverage Status Dependency Monitor

Determine the relation between two URLs.

Installation

Node.js >= 6 is required. To install, type this at the command line:

npm install url-relation

Usage

const urlRelation = require('url-relation');

const url1 = new URL('http://domain.com/');
const url2 = new URL('http://domain.com/#hash');

const relation = urlRelation(url1, url2, options);
//-> 14

if (relation >= urlRelation.HOST) {
  console.log('same server!');
}

Options

It is simplest to use an option profile, but custom configurations are still possible.

defaultPorts

Type: Object
Default value: {}
A map of protocol default ports for ignoreDefaultPort. Be sure to include the suffixed ":" in the key. Common protocols already have their ports removed.

directoryIndexes

Type: Array<RegExp|string>
Default value: ['index.html']
A list of file names for ignoreDirectoryIndex.

ignoreDefaultPort

Type: Boolean or Function
Default value: true
When set to true or a function that returns true, a URL's port that matches any found in defaultPorts will be ignored during comparison.

ignoreDirectoryIndex

Type: Boolean or Function
Default value: Function
When set to true or a function that returns true, a URL's file name that matches any found in directoryIndexes will be ignored during comparison.

ignoreEmptyDirectoryNames

Type: Boolean or Function
Default value: false
When set to true or a function that returns true, empty directory names within a URL's path (such as the "//" in "/path//to/") will be ignored during comparison.

ignoreEmptyQueries

Type: Boolean or Function
Default value: Function
When set to true or a function that returns true, a URL's empty query parameters (such as "?=") will not distinguish one URL from another. This option will be silently skipped if the input URLs do not support URLSearchParams.

ignoreQueryNames

Type: Boolean or Function
Default value: false
When set to true or a function that returns true, a URL's query parameters matching queryNames will not distinguish one URL from another. This option will be silently skipped if the input URLs do not support URLSearchParams.

ignoreQueryOrder

Type: Boolean or Function
Default value: Function
When set to true or a function that returns true, the order of unique query parameters will not distinguish one URL from another. This option will be silently skipped if the input URLs do not support URLSearchParams.

ignoreWWW

Type: Boolean or Function
Default value: Function
When set to true or a function that returns true, a URL's "www" subdomain will be ignored during comparison.

queryNames

Type: Array<RegExp|string>
Default value: []
A list of query parameters for ignoreQueryNames.

Function as an Option

When an option is defined as a Function, it must return true to be included in the custom filter:

const options = {
  ignoreDirectoryIndex: function(url1, url2) {
    // Only URLs with these protocols will have their directory indexes ignored
    return url1.protocol === 'http:' && url1.protocol === 'https:';
  }
};

Option Profiles

CAREFUL_PROFILE is useful for a URL to an unknown or third-party server that could be incorrectly configured according to specifications and common best practices.

COMMON_PROFILE, the default profile, is useful for a URL to a known server that you trust and expect to be correctly configured according to specifications and common best practices.

An example of checking for a trusted hostname:

const url1 = new URL('http://domain.com/');
const url2 = new URL('http://domain.com/#hash');

const trustedHosts = ['domain.com'];

const isTrusted = trustedHosts
  .reduce(function(results, trustedHost) {
    results[0] = results[0] || url1.hostname.endsWith(trustedHost);
    results[1] = results[1] || url2.hostname.endsWith(trustedHost);
    return results;
  }, [false,false])
  .every(result => result);

const options = urlRelation[`${isTrusted ? 'COMMON' : 'CAREFUL'}_PROFILE`];

urlRelation(url1, url2, options);

Customizing Profiles

const custom = Object.assign({}, urlRelation.CAREFUL_PROFILE, { ignoreTrailingSlash:true });

Or:

const extend = require('extend');

const custom = extend(true, {}, urlRelation.COMMON_PROFILE, { directoryIndexes:['index.php'] });

Relation Constants

Returned values can be compared with: NONE, PROTOCOL, TLD, DOMAIN, SUBDOMAIN, HOSTNAME, PORT, HOST, USERNAME, PASSWORD, AUTH, DIRECTORY, FILENAME, PATHNAME, SEARCH, PATH, HASH, ALL.

               AUTH                 HOST                         PATH
              __|__                ___|___                 _______|______
             /     \              /       \               /              \
        USERNAME PASSWORD     HOSTNAME    PORT        PATHNAME        SEARCH  HASH
         ___|__   __|___   ______|______   |   __________|_________   ___|___   |
        /      \ /      \ /             \ / \ /                    \ /       \ / \
  foo://username:[email protected]:123/hello/world/there.html?var=value#foo
  \_/                     \_/ \_____/ \_/     \_________/ \________/
   |                       |     |     |           |           |
PROTOCOL               SUBDOMAIN |    TLD      DIRECTORY   FILENAME
                                 |
                              DOMAIN

Note: there are a few breaks in the linearity of these values:

  • AUTH is prioritized after HOST because matching authentication on a different domain is pointless.
  • TLD is prioritized before DOMAIN because matching a domain on a different top-level domain is pointless.
  • SUBDOMAIN is prioritized after DOMAIN.

Browserify/etc

Due to extreme file size in correctly parsing domains, browser builds will not include such functionality by default. As a result, output of this library within a web browser will never exactly equal TLD, DOMAIN nor SUBDOMAIN.