fast-uri
Dependency free RFC 3986 URI toolbox.
Usage
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:
Malformed authorities and out-of-range ports are reported through the parsed component's error field. normalize() leaves malformed string inputs unchanged, and equal() returns false when either string input is malformed.
Schemes are never percent-decoded. RFC 3986 does not allow percent-encoding in a scheme, so an input such as %6Aavascript:alert(1) is reported as malformed (error: 'URI scheme is malformed.') instead of being parsed as javascript:. normalize() leaves it unchanged, equal() returns false, and resolve() and serialize() throw.
For schemes with domain hosts (http, https, ws, wss, or any scheme with the domainHost option), a host containing an ASCII character that RFC 3986 does not allow in a reg-name (for example @, TAB, LF, CR, space, ", <, >, ^, `, {, |, }) is reported as malformed (error: 'URI host is malformed.'). It is never passed through a WHATWG URL parser that could treat @ as a delimiter or strip whitespace. normalize() leaves such input unchanged, equal() returns false, and resolve() throws.
-
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 parser will use the option's scheme property to determine the URI's scheme.
A suffix reference is written without its scheme and without the // authority marker (RFC 3986 section 4.5). Such a reference is inherently ambiguous: an authority component cannot always be distinguished from a path. When the input carries an authority of the form host:port (for example //javascript:80/x) or a userinfo containing a colon, the serialized suffix output can begin with text that reparses as a URI scheme (for example javascript:80/x). Do not treat suffix output as a trusted absolute URL, and do not render it or use it in a scheme allowlist without first validating it with parse(). If you need an unambiguous result, use the default (non-suffix) serialization.
-
tolerant (boolean, false)
If set to true, the parser will relax URI resolving rules.
-
absolutePath (boolean, false)
If set to true, the serializer will not resolve a relative path component.
-
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.
Parse
const uri = require('fast-uri')
uri.parse('uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body')
{
scheme : "uri",
userinfo : "user:pass",
host : "example.com",
port : 123,
path : "/one/two.three",
query : "q1=a1&q2=a2",
fragment : "body"
}
Serialize
const uri = require('fast-uri')
uri.serialize({scheme : "http", host : "example.com", fragment : "footer"})
"http://example.com/#footer"
Resolve
const uri = require('fast-uri')
uri.resolve("uri://a/b/c/d?q", "../../g")
"uri://a/g"
Normalize
const uri = require('fast-uri')
uri.normalize('http://example.com/a%2Fb')
"http://example.com/a%2Fb"
Reserved path escapes such as %2F and %2E are preserved as path data during normalization and comparison.
Dot segments in relative references are removed without changing what the reference resolves to: leading .. segments that cannot be collapsed are kept, a relative path is never turned into a root-relative one, and a first-segment colon exposed by the removal is kept behind a leading ./ so the reference is not reparsed as a scheme (a/../b → b, ../x → ../x, x/.. → ./, a/../a:b → ./a:b).
Equal
const uri = require('fast-uri')
uri.equal("example://a/b/c/%7Bfoo%7D", "eXAMPLE://a/./b/../b/%63/%7bfoo%7d")
true
Scheme supports
fast-uri supports inserting custom scheme dependent processing rules. Currently, fast-uri has built in support for the following schemes:
Benchmarks
fast-uri: parse domain x 1,306,864 ops/sec ±0.31% (100 runs sampled)
urijs: parse domain x 483,001 ops/sec ±0.09% (99 runs sampled)
WHATWG URL: parse domain x 862,461 ops/sec ±0.18% (97 runs sampled)
fast-uri: parse IPv4 x 2,381,452 ops/sec ±0.26% (96 runs sampled)
urijs: parse IPv4 x 384,705 ops/sec ±0.34% (99 runs sampled)
WHATWG URL: parse IPv4 NOT SUPPORTED
fast-uri: parse IPv6 x 923,519 ops/sec ±0.09% (100 runs sampled)
urijs: parse IPv6 x 289,070 ops/sec ±0.07% (95 runs sampled)
WHATWG URL: parse IPv6 NOT SUPPORTED
fast-uri: parse URN x 2,596,395 ops/sec ±0.42% (98 runs sampled)
urijs: parse URN x 1,152,412 ops/sec ±0.09% (97 runs sampled)
WHATWG URL: parse URN x 1,183,307 ops/sec ±0.38% (100 runs sampled)
fast-uri: parse URN uuid x 1,666,861 ops/sec ±0.10% (98 runs sampled)
urijs: parse URN uuid x 852,724 ops/sec ±0.17% (95 runs sampled)
WHATWG URL: parse URN uuid NOT SUPPORTED
fast-uri: serialize uri x 1,741,499 ops/sec ±0.57% (95 runs sampled)
urijs: serialize uri x 389,014 ops/sec ±0.28% (93 runs sampled)
fast-uri: serialize IPv6 x 441,095 ops/sec ±0.37% (97 runs sampled)
urijs: serialize IPv6 x 255,443 ops/sec ±0.58% (94 runs sampled)
fast-uri: serialize ws x 1,448,667 ops/sec ±0.25% (97 runs sampled)
urijs: serialize ws x 352,884 ops/sec ±0.08% (96 runs sampled)
fast-uri: resolve x 340,084 ops/sec ±0.98% (98 runs sampled)
urijs: resolve x 225,759 ops/sec ±0.37% (95 runs sampled)
TODO