You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.4 KiB

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