File size: 11,726 Bytes
a5d718a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | # fast-uri
[](https://www.npmjs.com/package/fast-uri)
[](https://github.com/fastify/fast-uri/actions/workflows/ci.yml)
[](https://github.com/neostandard/neostandard)
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.
* `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.
* `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](http://www.ietf.org/rfc/rfc3987.txt).
* `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](http://www.ietf.org/rfc/rfc5891.txt).
### Parse
```js
const uri = require('fast-uri')
uri.parse('uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body')
// Output
{
scheme: "uri",
userinfo: "user:pass",
host: "example.com",
port: 123,
path: "/one/two.three",
query: "q1=a1&q2=a2",
fragment: "body"
}
```
### Serialize
```js
const uri = require('fast-uri')
uri.serialize({scheme: "http", host: "example.com", fragment: "footer"})
// Output
"http://example.com/#footer"
```
### Resolve
```js
const uri = require('fast-uri')
uri.resolve("uri://a/b/c/d?q", "../../g")
// Output
"uri://a/g"
```
### Normalize
```js
const uri = require('fast-uri')
uri.normalize('http://example.com/a%2Fb')
// Output
"http://example.com/a%2Fb"
```
Reserved path escapes such as `%2F` and `%2E` are preserved as path data during normalization and comparison.
### Equal
```js
const uri = require('fast-uri')
uri.equal("example://a/b/c/%7Bfoo%7D", "eXAMPLE://a/./b/../b/%63/%7bfoo%7d")
// Output
true
```
## Scheme supports
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:
* http \[[RFC 2616](http://www.ietf.org/rfc/rfc2616.txt)\]
* https \[[RFC 2818](http://www.ietf.org/rfc/rfc2818.txt)\]
* ws \[[RFC 6455](http://www.ietf.org/rfc/rfc6455.txt)\]
* wss \[[RFC 6455](http://www.ietf.org/rfc/rfc6455.txt)\]
* urn \[[RFC 2141](http://www.ietf.org/rfc/rfc2141.txt)\]
* urn:uuid \[[RFC 4122](http://www.ietf.org/rfc/rfc4122.txt)\]
## Benchmarks
```
fast-uri benchmark
βββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββ¬βββββββββββββββββββ¬βββββββββββββββββββββββββ¬βββββββββββββββββββββββββ¬ββββββββββ
β (index) β Task name β Latency avg (ns) β Latency med (ns) β Throughput avg (ops/s) β Throughput med (ops/s) β Samples β
βββββββββββΌβββββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββ€
β 0 β 'fast-uri: parse domain' β '951.31 Β± 0.75%' β '875.00 Β± 11.00' β '1122538 Β± 0.01%' β '1142857 Β± 14550' β 1051187 β
β 1 β 'fast-uri: parse IPv4' β '443.44 Β± 0.22%' β '406.00 Β± 3.00' β '2422762 Β± 0.01%' β '2463054 Β± 18335' β 2255105 β
β 2 β 'fast-uri: parse IPv6' β '1241.6 Β± 1.74%' β '1131.0 Β± 30.00' β '875177 Β± 0.02%' β '884173 Β± 24092' β 805399 β
β 3 β 'fast-uri: parse URN' β '689.19 Β± 4.29%' β '618.00 Β± 9.00' β '1598373 Β± 0.01%' β '1618123 Β± 23913' β 1450972 β
β 4 β 'fast-uri: parse URN uuid' β '1025.4 Β± 2.02%' β '921.00 Β± 19.00' β '1072419 Β± 0.02%' β '1085776 Β± 22871' β 975236 β
β 5 β 'fast-uri: serialize uri' β '1028.5 Β± 0.53%' β '933.00 Β± 43.00' β '1063310 Β± 0.02%' β '1071811 Β± 50523' β 972249 β
β 6 β 'fast-uri: serialize long uri with dots' β '1805.1 Β± 0.52%' β '1627.0 Β± 17.00' β '602620 Β± 0.02%' β '614628 Β± 6490' β 553997 β
β 7 β 'fast-uri: serialize IPv6' β '2569.4 Β± 2.69%' β '2302.0 Β± 21.00' β '426080 Β± 0.03%' β '434405 Β± 3999' β 389194 β
β 8 β 'fast-uri: serialize ws' β '979.39 Β± 0.43%' β '882.00 Β± 8.00' β '1111665 Β± 0.02%' β '1133787 Β± 10378' β 1021045 β
β 9 β 'fast-uri: resolve' β '2208.2 Β± 1.08%' β '1980.0 Β± 24.00' β '495001 Β± 0.03%' β '505051 Β± 6049' β 452848 β
βββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββ΄βββββββββββββββββββ΄βββββββββββββββββββββββββ΄βββββββββββββββββββββββββ΄ββββββββββ
uri-js benchmark
βββββββββββ¬ββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββ¬βββββββββββββββββββ¬βββββββββββββββββββββββββ¬βββββββββββββββββββββββββ¬ββββββββββ
β (index) β Task name β Latency avg (ns) β Latency med (ns) β Throughput avg (ops/s) β Throughput med (ops/s) β Samples β
βββββββββββΌββββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββ€
β 0 β 'urijs: parse domain' β '3618.3 Β± 0.43%' β '3314.0 Β± 33.00' β '294875 Β± 0.04%' β '301750 Β± 2975' β 276375 β
β 1 β 'urijs: parse IPv4' β '4024.1 Β± 0.41%' β '3751.0 Β± 25.00' β '261981 Β± 0.04%' β '266596 Β± 1789' β 248506 β
β 2 β 'urijs: parse IPv6' β '5417.2 Β± 0.46%' β '4968.0 Β± 43.00' β '196023 Β± 0.05%' β '201288 Β± 1727' β 184598 β
β 3 β 'urijs: parse URN' β '1324.2 Β± 0.23%' β '1229.0 Β± 17.00' β '801535 Β± 0.02%' β '813670 Β± 11413' β 755185 β
β 4 β 'urijs: parse URN uuid' β '1822.0 Β± 3.08%' β '1655.0 Β± 15.00' β '594433 Β± 0.02%' β '604230 Β± 5427' β 548843 β
β 5 β 'urijs: serialize uri' β '4196.8 Β± 0.36%' β '3908.0 Β± 27.00' β '251146 Β± 0.04%' β '255885 Β± 1756' β 238276 β
β 6 β 'urijs: serialize long uri with dots' β '8331.0 Β± 1.30%' β '7658.0 Β± 72.00' β '126440 Β± 0.07%' β '130582 Β± 1239' β 120034 β
β 7 β 'urijs: serialize IPv6' β '5685.5 Β± 0.30%' β '5366.0 Β± 33.00' β '182632 Β± 0.05%' β '186359 Β± 1153' β 175886 β
β 8 β 'urijs: serialize ws' β '4159.3 Β± 0.20%' β '3899.0 Β± 28.00' β '250459 Β± 0.04%' β '256476 Β± 1855' β 240423 β
β 9 β 'urijs: resolve' β '6729.9 Β± 0.39%' β '6261.0 Β± 37.00' β '156361 Β± 0.06%' β '159719 Β± 949' β 148591 β
βββββββββββ΄ββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββ΄βββββββββββββββββββ΄βββββββββββββββββββββββββ΄βββββββββββββββββββββββββ΄ββββββββββ
WHATWG URL benchmark
βββββββββββ¬βββββββββββββββββββββββββββββ¬βββββββββββββββββββ¬βββββββββββββββββββ¬βββββββββββββββββββββββββ¬βββββββββββββββββββββββββ¬ββββββββββ
β (index) β Task name β Latency avg (ns) β Latency med (ns) β Throughput avg (ops/s) β Throughput med (ops/s) β Samples β
βββββββββββΌβββββββββββββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββ€
β 0 β 'WHATWG URL: parse domain' β '475.22 Β± 0.20%' β '444.00 Β± 5.00' β '2217599 Β± 0.01%' β '2252252 Β± 25652' β 2104289 β
β 1 β 'WHATWG URL: parse URN' β '384.78 Β± 0.85%' β '350.00 Β± 5.00' β '2809071 Β± 0.01%' β '2857143 Β± 41408' β 2598885 β
βββββββββββ΄βββββββββββββββββββββββββββββ΄βββββββββββββββββββ΄βββββββββββββββββββ΄βββββββββββββββββββββββββ΄βββββββββββββββββββββββββ΄ββββββββββ
```
## TODO
- [ ] Support MailTo
- [ ] Be 100% iso compatible with uri-js
## License
Licensed under [BSD-3-Clause](./LICENSE).
|