alwinb / spec-url

URL library that implements a reference resolution algorithm for WHATWG URLs
MIT License
5 stars 0 forks source link

NPM badge

URL Implementation

🌲

An URL manipulation library that supports URL records, relative URLs, reference resolution and a number of other elementary operations on URLs in a way that is compatible with the WHATWG URL Standard.

This library serves as a reference implementation for this URL Specification, which is an alternative URL specification that rephrases and generalises the WHATWG URL Standard to add support for relative URLs, reference resolution and a number of other elementary operations, as wel as restoring a formal grammar.

Always feel free to ask questions. If you wish, you may file an issue for a question.

People are encouraged to experiment with creating more high level APIs around this library. One example is my reurl library, which wraps around spec-url to provide a high level API for immutable URL objects.

API

The library exposes a concise, low-level API for working with URL strings and URL records. It models URLs as plain javascript objects and it exposes a number of functions for working with them.

URL

In this implementation an URL is modeled as a plain JavaScript object with the following optional attributes:

If present, dirs is an non-empty array of strings; host is a Host (see below) and all other attributes are strings. The string valued attributes are subject to the constraints as described in my URL Specification.

A Host is either an ipv6 address, a domain, an ipv4 address, or an opaque host. In this implementation these are modeled as an array of numbers, an array of strings, a number, or a string, respectively.

Note The [URL Specification] models URLs as [ordered sequences of components][URL Model], "with at most one component per type, except for **dir** componens, of which it may have any amount". Futhermore, the **username**, **password**, **host** and **port** are nested inside an **authority** component. In this this library URLs are modeled as plain JavaScript objects. The **dir** componens, if present, are collected into a single **dirs** _array_, and the **authority**, if present, is expanded by setting any of its **user**, **pass**, **host** and **port** constituents directly on the url object itself. There is a one-to-one correspondence between this representation and sequences of components as defined in the URL specification.

Bsics

Rebase and Resolve

The rebase function is the preferred method for composing URLs. It can be thought of as a resolve function for relative URLs. The rebase function does not attempt to parse opaque hosts as a domain, and does not enforce additional requirements on the authority.

The resolve function is similar to rebase but it always produces an absolute URL, or throws an error if it is unable to do so. It coerces special URLs to have an authority, and parses their hosts as a domain. It enforces that file URLs do not have a user, pass nor port. NB this converts the first non-empty path segment of a web-URL to an authority if this is needed.

Normalisation

Parsing

Printing

Host Parsing

Changelog

Forthcoming

Work has started on a simple API wrapper.

Version 2.4.0-dev

Version 2.3.3-dev

Version 2.3.2-dev

Version 2.3.1-dev

Version 2.3.0-dev

Towards a simple API without modes; towards loosening the constraints on the model a bit, and enforcing them in the resolution operation:

Version 2.2.0-dev

Version 2.1.0-dev

Version 2.0.0-dev.1

Version 1.5.0

Version 1.4.0

Version 1.3.0

Version 1.2.0

Licence