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.

68 lines
2.1 KiB

6 months ago
  1. # @polka/url [![npm](https://badgen.now.sh/npm/v/@polka/url)](https://npmjs.org/package/@polka/url) [![licenses](https://licenses.dev/b/npm/%40polka%2Furl)](https://licenses.dev/npm/%40polka%2Furl)
  2. > Super fast, memoized `req.url` parser; _not_ limited to [Polka][polka]!
  3. Parses the `url` from a [`IncomingMessage`](https://nodejs.org/api/http.html#http_class_http_incomingmessage) request. The returned object will always only contain the following keys: `search`, `query`, `pathname`, and `raw`.
  4. > **Note:** This library does not process `protocol`, `hostname`, `port`, etc.<br>This is because the incoming `req.url` value only begins with the path information.
  5. Parsed requests will be mutated with a `_parsedUrl` key, containing the returned output. This is used for future memoization, avoiding the need to fully parse the same `url` value multiple times.
  6. ## Install
  7. ```
  8. $ npm install --save @polka/url
  9. ```
  10. ## Usage
  11. ```js
  12. const parse = require('@polka/url');
  13. let req = {
  14. url: '/foo/bar?fizz=buzz'
  15. };
  16. let output = parse(req);
  17. //=> {
  18. //=> pathname: '/foo/bar',
  19. //=> raw: '/foo/bar?fizz=buzz',
  20. //=> search: '?fizz=buzz',
  21. //=> query: {
  22. //=> fizz: 'buzz'
  23. //=> },
  24. //=> }
  25. // Attaches result for future memoization
  26. assert.deepEqual(output, req._parsedUrl); //=> true
  27. ```
  28. ## API
  29. ### url(req)
  30. Returns: `Object` or `undefined`
  31. > **Important:** The `req` must have a `url` key, otherwise `undefined` will be returned.<br>If no input is provided at all, a `TypeError` will be thrown.
  32. #### req
  33. Type: `IncomingMessage` or `{ url: string }`
  34. The incoming HTTP request (`req`) or a plain `Object` with a `url` key.
  35. > **Note:** In Node.js servers, the [`req.url`](https://nodejs.org/api/http.html#http_message_url) begins with a pathname & does not include a `hash`.
  36. ## Benchmarks
  37. Check out the [`bench`](/bench) directory for in-depth benchmark results and comparisons.
  38. ## Support
  39. Any issues or questions can be sent to the [Polka][polka] repository.<br>However, please specify that your inquiry is about `@polka/url` specifically.
  40. ## License
  41. MIT © [Luke Edwards](https://lukeed.com)
  42. [polka]: https://github.com/lukeed/polka