Socket
Socket
Sign inDemoInstall

@polka/url

Package Overview
Dependencies
0
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@polka/url


Version published
Weekly downloads
6.6M
decreased by-5.5%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

@polka/url npm

Super fast, memoized req.url parser; not limited to Polka!

Parses the url from a IncomingMessage request. The returned object will always only contain the following keys: search, query, pathname, and raw.

Note: This library does not process protocol, hostname, port, etc.
This is because the incoming req.url value only begins with the path information.

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.

Install

$ npm install --save @polka/url

Usage

const parse = require('@polka/url');

let req = {
  url: '/foo/bar?fizz=buzz'
};
let output = parse(req);
//=> {
//=>   pathname: '/foo/bar',
//=>   raw: '/foo/bar?fizz=buzz',
//=>   search: '?fizz=buzz',
//=>   query: {
//=>     fizz: 'buzz'
//=>   },
//=> }

// Attaches result for future memoization
assert.deepEqual(output, req._parsedUrl); //=> true

// Example with `toDecode` param
req = {
  url: '/f%C3%B8%C3%B8%C3%9F%E2%88%82r?phone=%2b8675309'
};
parse(req, true);
//=> {
//=>   pathname: '/føøß∂r',
//=>   raw: '/f%C3%B8%C3%B8%C3%9F%E2%88%82r?phone=%2b8675309',
//=>   search: '?phone=%2b8675309',
//=>   query: {
//=>     phone: '+8675309'
//=>   }
//=> }

// Attaches awareness key
assert(req._decoded); //=> true

API

url(req, toDecode?)

Returns: Object or undefined

Important: The req must have a url key, otherwise undefined will be returned.
If no input is provided at all, a TypeError will be thrown.

req

Type: IncomingMessage or { url: string }

The incoming HTTP request (req) or a plain Object with a url key.

Note: In Node.js servers, the req.url begins with a pathname & does not include a hash.

toDecode

Type: Boolean
Default: false

If enabled, the pathname will be fully decoded, via decodeURIComponent.

Important: Please note the following behaviors:

  • raw is never decoded; this key reflects your original value
  • pathname is decoded only when toDecode is enabled
  • search is never decoded; this key reflects your original querystring value
  • query is always decoded; even when toDecode is disabled

Additionally, the req is mutated with req._decoded = true so as to prevent repetitive decoding.

Benchmarks

Check out the bench directory for in-depth benchmark results and comparisons.

Support

Any issues or questions can be sent to the Polka repository.
However, please specify that your inquiry is about @polka/url specifically.

License

MIT © Luke Edwards

FAQs

Last updated on 24 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc