[79a0317] | 1 | # fast-uri
|
---|
| 2 |
|
---|
| 3 | <div align="center">
|
---|
| 4 |
|
---|
| 5 | [![NPM version](https://img.shields.io/npm/v/fast-uri.svg?style=flat)](https://www.npmjs.com/package/fast-uri)
|
---|
| 6 | [![CI](https://github.com/fastify/fast-uri/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fast-uri/actions/workflows/ci.yml)
|
---|
| 7 | [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)
|
---|
| 8 |
|
---|
| 9 | </div>
|
---|
| 10 |
|
---|
| 11 | Dependency-free RFC 3986 URI toolbox.
|
---|
| 12 |
|
---|
| 13 | ## Usage
|
---|
| 14 |
|
---|
| 15 | ## Options
|
---|
| 16 |
|
---|
| 17 | 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:
|
---|
| 18 |
|
---|
| 19 | * `scheme` (string)
|
---|
| 20 | Indicates the scheme that the URI should be treated as, overriding the URI's normal scheme parsing behavior.
|
---|
| 21 |
|
---|
| 22 | * `reference` (string)
|
---|
| 23 | 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.
|
---|
| 24 |
|
---|
| 25 | * `tolerant` (boolean, false)
|
---|
| 26 | If set to `true`, the parser will relax URI resolving rules.
|
---|
| 27 |
|
---|
| 28 | * `absolutePath` (boolean, false)
|
---|
| 29 | If set to `true`, the serializer will not resolve a relative `path` component.
|
---|
| 30 |
|
---|
| 31 | * `unicodeSupport` (boolean, false)
|
---|
| 32 | If set to `true`, the parser will unescape non-ASCII characters in the parsed output as per [RFC 3987](http://www.ietf.org/rfc/rfc3987.txt).
|
---|
| 33 |
|
---|
| 34 | * `domainHost` (boolean, false)
|
---|
| 35 | 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](http://www.ietf.org/rfc/rfc5891.txt).
|
---|
| 36 |
|
---|
| 37 | ### Parse
|
---|
| 38 |
|
---|
| 39 | ```js
|
---|
| 40 | const uri = require('fast-uri')
|
---|
| 41 | uri.parse('uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body')
|
---|
| 42 | // Output
|
---|
| 43 | {
|
---|
| 44 | scheme: "uri",
|
---|
| 45 | userinfo: "user:pass",
|
---|
| 46 | host: "example.com",
|
---|
| 47 | port: 123,
|
---|
| 48 | path: "/one/two.three",
|
---|
| 49 | query: "q1=a1&q2=a2",
|
---|
| 50 | fragment: "body"
|
---|
| 51 | }
|
---|
| 52 | ```
|
---|
| 53 |
|
---|
| 54 | ### Serialize
|
---|
| 55 |
|
---|
| 56 | ```js
|
---|
| 57 | const uri = require('fast-uri')
|
---|
| 58 | uri.serialize({scheme: "http", host: "example.com", fragment: "footer"})
|
---|
| 59 | // Output
|
---|
| 60 | "http://example.com/#footer"
|
---|
| 61 |
|
---|
| 62 | ```
|
---|
| 63 |
|
---|
| 64 | ### Resolve
|
---|
| 65 |
|
---|
| 66 | ```js
|
---|
| 67 | const uri = require('fast-uri')
|
---|
| 68 | uri.resolve("uri://a/b/c/d?q", "../../g")
|
---|
| 69 | // Output
|
---|
| 70 | "uri://a/g"
|
---|
| 71 | ```
|
---|
| 72 |
|
---|
| 73 | ### Equal
|
---|
| 74 |
|
---|
| 75 | ```js
|
---|
| 76 | const uri = require('fast-uri')
|
---|
| 77 | uri.equal("example://a/b/c/%7Bfoo%7D", "eXAMPLE://a/./b/../b/%63/%7bfoo%7d")
|
---|
| 78 | // Output
|
---|
| 79 | true
|
---|
| 80 | ```
|
---|
| 81 |
|
---|
| 82 | ## Scheme supports
|
---|
| 83 |
|
---|
| 84 | fast-uri supports inserting custom [scheme](http://en.wikipedia.org/wiki/URI_scheme)-dependent processing rules. Currently, fast-uri has built-in support for the following schemes:
|
---|
| 85 |
|
---|
| 86 | * http \[[RFC 2616](http://www.ietf.org/rfc/rfc2616.txt)\]
|
---|
| 87 | * https \[[RFC 2818](http://www.ietf.org/rfc/rfc2818.txt)\]
|
---|
| 88 | * ws \[[RFC 6455](http://www.ietf.org/rfc/rfc6455.txt)\]
|
---|
| 89 | * wss \[[RFC 6455](http://www.ietf.org/rfc/rfc6455.txt)\]
|
---|
| 90 | * urn \[[RFC 2141](http://www.ietf.org/rfc/rfc2141.txt)\]
|
---|
| 91 | * urn:uuid \[[RFC 4122](http://www.ietf.org/rfc/rfc4122.txt)\]
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | ## Benchmarks
|
---|
| 95 |
|
---|
| 96 | ```
|
---|
| 97 | fast-uri: parse domain x 1,306,864 ops/sec ±0.31% (100 runs sampled)
|
---|
| 98 | urijs: parse domain x 483,001 ops/sec ±0.09% (99 runs sampled)
|
---|
| 99 | WHATWG URL: parse domain x 862,461 ops/sec ±0.18% (97 runs sampled)
|
---|
| 100 | fast-uri: parse IPv4 x 2,381,452 ops/sec ±0.26% (96 runs sampled)
|
---|
| 101 | urijs: parse IPv4 x 384,705 ops/sec ±0.34% (99 runs sampled)
|
---|
| 102 | WHATWG URL: parse IPv4 NOT SUPPORTED
|
---|
| 103 | fast-uri: parse IPv6 x 923,519 ops/sec ±0.09% (100 runs sampled)
|
---|
| 104 | urijs: parse IPv6 x 289,070 ops/sec ±0.07% (95 runs sampled)
|
---|
| 105 | WHATWG URL: parse IPv6 NOT SUPPORTED
|
---|
| 106 | fast-uri: parse URN x 2,596,395 ops/sec ±0.42% (98 runs sampled)
|
---|
| 107 | urijs: parse URN x 1,152,412 ops/sec ±0.09% (97 runs sampled)
|
---|
| 108 | WHATWG URL: parse URN x 1,183,307 ops/sec ±0.38% (100 runs sampled)
|
---|
| 109 | fast-uri: parse URN uuid x 1,666,861 ops/sec ±0.10% (98 runs sampled)
|
---|
| 110 | urijs: parse URN uuid x 852,724 ops/sec ±0.17% (95 runs sampled)
|
---|
| 111 | WHATWG URL: parse URN uuid NOT SUPPORTED
|
---|
| 112 | fast-uri: serialize uri x 1,741,499 ops/sec ±0.57% (95 runs sampled)
|
---|
| 113 | urijs: serialize uri x 389,014 ops/sec ±0.28% (93 runs sampled)
|
---|
| 114 | fast-uri: serialize IPv6 x 441,095 ops/sec ±0.37% (97 runs sampled)
|
---|
| 115 | urijs: serialize IPv6 x 255,443 ops/sec ±0.58% (94 runs sampled)
|
---|
| 116 | fast-uri: serialize ws x 1,448,667 ops/sec ±0.25% (97 runs sampled)
|
---|
| 117 | urijs: serialize ws x 352,884 ops/sec ±0.08% (96 runs sampled)
|
---|
| 118 | fast-uri: resolve x 340,084 ops/sec ±0.98% (98 runs sampled)
|
---|
| 119 | urijs: resolve x 225,759 ops/sec ±0.37% (95 runs sampled)
|
---|
| 120 | ```
|
---|
| 121 |
|
---|
| 122 | ## TODO
|
---|
| 123 |
|
---|
| 124 | - [ ] Support MailTo
|
---|
| 125 | - [ ] Be 100% iso compatible with uri-js
|
---|
| 126 | - [ ] Add browser test stack
|
---|
| 127 |
|
---|
| 128 | ## License
|
---|
| 129 |
|
---|
| 130 | Licensed under [BSD-3-Clause](./LICENSE).
|
---|