fix: maybe
This commit is contained in:
		
							
								
								
									
										65
									
								
								node_modules/@polka/url/build.js
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										65
									
								
								node_modules/@polka/url/build.js
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -1,42 +1,47 @@
 | 
			
		||||
const qs = require('querystring');
 | 
			
		||||
function parse(str) {
 | 
			
		||||
	let out={}, arr=str.split('&');
 | 
			
		||||
	for (let i=0, k, v; i < arr.length; i++) {
 | 
			
		||||
		[k, v=''] = arr[i].split('=');
 | 
			
		||||
		out[k] = out[k] !== void 0 ? [].concat(out[k], v) : v;
 | 
			
		||||
	}
 | 
			
		||||
	return out;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @typedef ParsedURL
 | 
			
		||||
 * @type {import('.').ParsedURL}
 | 
			
		||||
 */
 | 
			
		||||
module.exports = function (req, toDecode) {
 | 
			
		||||
	let url = req.url;
 | 
			
		||||
	if (url == null) return;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @typedef Request
 | 
			
		||||
 * @property {string} url
 | 
			
		||||
 * @property {ParsedURL} _parsedUrl
 | 
			
		||||
 */
 | 
			
		||||
	let obj = req._parsedUrl;
 | 
			
		||||
	if (obj && obj._raw === url) return obj;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @param {Request} req
 | 
			
		||||
 * @returns {ParsedURL|void}
 | 
			
		||||
 */
 | 
			
		||||
function parse(req) {
 | 
			
		||||
	let raw = req.url;
 | 
			
		||||
	if (raw == null) return;
 | 
			
		||||
	obj = {
 | 
			
		||||
		path: url,
 | 
			
		||||
		pathname: url,
 | 
			
		||||
		search: null,
 | 
			
		||||
		query: null,
 | 
			
		||||
		href: url,
 | 
			
		||||
		_raw: url
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	let prev = req._parsedUrl;
 | 
			
		||||
	if (prev && prev.raw === raw) return prev;
 | 
			
		||||
	if (url.length > 1) {
 | 
			
		||||
		if (toDecode && !req._decoded && !!~url.indexOf('%', 1)) {
 | 
			
		||||
			let nxt = url;
 | 
			
		||||
			try { nxt = decodeURIComponent(url) } catch (e) {/* bad */}
 | 
			
		||||
			url = req.url = obj.href = obj.path = obj.pathname = obj._raw = nxt;
 | 
			
		||||
			req._decoded = true;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	let pathname=raw, search='', query;
 | 
			
		||||
 | 
			
		||||
	if (raw.length > 1) {
 | 
			
		||||
		let idx = raw.indexOf('?', 1);
 | 
			
		||||
		let idx = url.indexOf('?', 1);
 | 
			
		||||
 | 
			
		||||
		if (idx !== -1) {
 | 
			
		||||
			search = raw.substring(idx);
 | 
			
		||||
			pathname = raw.substring(0, idx);
 | 
			
		||||
			if (search.length > 1) {
 | 
			
		||||
				query = qs.parse(search.substring(1));
 | 
			
		||||
			obj.search = url.substring(idx);
 | 
			
		||||
			obj.query = obj.search.substring(1);
 | 
			
		||||
			obj.pathname = url.substring(0, idx);
 | 
			
		||||
			if (toDecode && obj.query.length > 0) {
 | 
			
		||||
				obj.query = parse(obj.query);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return req._parsedUrl = { pathname, search, query, raw };
 | 
			
		||||
	return (req._parsedUrl = obj);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
exports.parse = parse;
 | 
			
		||||
							
								
								
									
										63
									
								
								node_modules/@polka/url/build.mjs
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										63
									
								
								node_modules/@polka/url/build.mjs
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -1,40 +1,47 @@
 | 
			
		||||
import * as qs from 'querystring';
 | 
			
		||||
function parse(str) {
 | 
			
		||||
	let out={}, arr=str.split('&');
 | 
			
		||||
	for (let i=0, k, v; i < arr.length; i++) {
 | 
			
		||||
		[k, v=''] = arr[i].split('=');
 | 
			
		||||
		out[k] = out[k] !== void 0 ? [].concat(out[k], v) : v;
 | 
			
		||||
	}
 | 
			
		||||
	return out;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @typedef ParsedURL
 | 
			
		||||
 * @type {import('.').ParsedURL}
 | 
			
		||||
 */
 | 
			
		||||
export default function (req, toDecode) {
 | 
			
		||||
	let url = req.url;
 | 
			
		||||
	if (url == null) return;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @typedef Request
 | 
			
		||||
 * @property {string} url
 | 
			
		||||
 * @property {ParsedURL} _parsedUrl
 | 
			
		||||
 */
 | 
			
		||||
	let obj = req._parsedUrl;
 | 
			
		||||
	if (obj && obj._raw === url) return obj;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @param {Request} req
 | 
			
		||||
 * @returns {ParsedURL|void}
 | 
			
		||||
 */
 | 
			
		||||
export function parse(req) {
 | 
			
		||||
	let raw = req.url;
 | 
			
		||||
	if (raw == null) return;
 | 
			
		||||
	obj = {
 | 
			
		||||
		path: url,
 | 
			
		||||
		pathname: url,
 | 
			
		||||
		search: null,
 | 
			
		||||
		query: null,
 | 
			
		||||
		href: url,
 | 
			
		||||
		_raw: url
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	let prev = req._parsedUrl;
 | 
			
		||||
	if (prev && prev.raw === raw) return prev;
 | 
			
		||||
	if (url.length > 1) {
 | 
			
		||||
		if (toDecode && !req._decoded && !!~url.indexOf('%', 1)) {
 | 
			
		||||
			let nxt = url;
 | 
			
		||||
			try { nxt = decodeURIComponent(url) } catch (e) {/* bad */}
 | 
			
		||||
			url = req.url = obj.href = obj.path = obj.pathname = obj._raw = nxt;
 | 
			
		||||
			req._decoded = true;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	let pathname=raw, search='', query;
 | 
			
		||||
 | 
			
		||||
	if (raw.length > 1) {
 | 
			
		||||
		let idx = raw.indexOf('?', 1);
 | 
			
		||||
		let idx = url.indexOf('?', 1);
 | 
			
		||||
 | 
			
		||||
		if (idx !== -1) {
 | 
			
		||||
			search = raw.substring(idx);
 | 
			
		||||
			pathname = raw.substring(0, idx);
 | 
			
		||||
			if (search.length > 1) {
 | 
			
		||||
				query = qs.parse(search.substring(1));
 | 
			
		||||
			obj.search = url.substring(idx);
 | 
			
		||||
			obj.query = obj.search.substring(1);
 | 
			
		||||
			obj.pathname = url.substring(0, idx);
 | 
			
		||||
			if (toDecode && obj.query.length > 0) {
 | 
			
		||||
				obj.query = parse(obj.query);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return req._parsedUrl = { pathname, search, query, raw };
 | 
			
		||||
	return (req._parsedUrl = obj);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										10
									
								
								node_modules/@polka/url/index.d.ts
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										10
									
								
								node_modules/@polka/url/index.d.ts
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -1,10 +0,0 @@
 | 
			
		||||
import type { IncomingMessage } from 'http';
 | 
			
		||||
 | 
			
		||||
export interface ParsedURL {
 | 
			
		||||
	pathname: string;
 | 
			
		||||
	search: string;
 | 
			
		||||
	query: Record<string, string | string[]> | void;
 | 
			
		||||
	raw: string;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function parse(req: IncomingMessage): ParsedURL;
 | 
			
		||||
							
								
								
									
										17
									
								
								node_modules/@polka/url/package.json
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										17
									
								
								node_modules/@polka/url/package.json
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -1,22 +1,12 @@
 | 
			
		||||
{
 | 
			
		||||
  "version": "1.0.0-next.23",
 | 
			
		||||
  "version": "1.0.0-next.9",
 | 
			
		||||
  "name": "@polka/url",
 | 
			
		||||
  "repository": "lukeed/polka",
 | 
			
		||||
  "description": "Super fast, memoized `req.url` parser",
 | 
			
		||||
  "module": "build.mjs",
 | 
			
		||||
  "types": "index.d.ts",
 | 
			
		||||
  "main": "build.js",
 | 
			
		||||
  "license": "MIT",
 | 
			
		||||
  "exports": {
 | 
			
		||||
    ".": {
 | 
			
		||||
      "types": "./index.d.ts",
 | 
			
		||||
      "import": "./build.mjs",
 | 
			
		||||
      "require": "./build.js"
 | 
			
		||||
    },
 | 
			
		||||
    "./package.json": "./package.json"
 | 
			
		||||
  },
 | 
			
		||||
  "files": [
 | 
			
		||||
    "*.d.ts",
 | 
			
		||||
    "build.*"
 | 
			
		||||
  ],
 | 
			
		||||
  "author": {
 | 
			
		||||
@@ -26,5 +16,6 @@
 | 
			
		||||
  },
 | 
			
		||||
  "publishConfig": {
 | 
			
		||||
    "access": "public"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
  },
 | 
			
		||||
  "gitHead": "5c8f314e57c1edb3132f2556ce13fb6c2b4e839b"
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										53
									
								
								node_modules/@polka/url/readme.md
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										53
									
								
								node_modules/@polka/url/readme.md
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -2,11 +2,11 @@
 | 
			
		||||
 | 
			
		||||
> Super fast, memoized `req.url` parser; _not_ limited to [Polka][polka]!
 | 
			
		||||
 | 
			
		||||
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`.
 | 
			
		||||
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`, `path`, `href`, and `_raw`.
 | 
			
		||||
 | 
			
		||||
> **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.
 | 
			
		||||
 | 
			
		||||
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.
 | 
			
		||||
Parsed requests will be mutated with a `_parsedUrl` key, containing the returned output. This is used for future memoization, so as to avoid parsing the same `url` value multiple times.
 | 
			
		||||
 | 
			
		||||
## Install
 | 
			
		||||
 | 
			
		||||
@@ -19,37 +19,58 @@ $ npm install --save @polka/url
 | 
			
		||||
```js
 | 
			
		||||
const parse = require('@polka/url');
 | 
			
		||||
 | 
			
		||||
let req = {
 | 
			
		||||
  url: '/foo/bar?fizz=buzz'
 | 
			
		||||
};
 | 
			
		||||
let output = parse(req);
 | 
			
		||||
//=> {
 | 
			
		||||
let req = { url: '/foo/bar?fizz=buzz' };
 | 
			
		||||
let foo = parse(req);
 | 
			
		||||
//=> { search: '?fizz=buzz',
 | 
			
		||||
//=>   query: 'fizz=buzz',
 | 
			
		||||
//=>   pathname: '/foo/bar',
 | 
			
		||||
//=>   raw: '/foo/bar?fizz=buzz',
 | 
			
		||||
//=>   search: '?fizz=buzz',
 | 
			
		||||
//=>   query: {
 | 
			
		||||
//=>     fizz: 'buzz'
 | 
			
		||||
//=>   },
 | 
			
		||||
//=> }
 | 
			
		||||
//=>   path: '/foo/bar?fizz=buzz',
 | 
			
		||||
//=>   href: '/foo/bar?fizz=buzz',
 | 
			
		||||
//=>   _raw: '/foo/bar?fizz=buzz' }
 | 
			
		||||
 | 
			
		||||
// Attaches result for future memoization
 | 
			
		||||
assert.deepEqual(output, req._parsedUrl); //=> true
 | 
			
		||||
assert.deepEqual(foo, req._parsedUrl); //=> true
 | 
			
		||||
 | 
			
		||||
// Example with `toDecode` param
 | 
			
		||||
req = { url: '/f%C3%B8%C3%B8%C3%9F%E2%88%82r?phone=%2b8675309' };
 | 
			
		||||
parse(req, true);
 | 
			
		||||
//=> { search: '?phone=+8675309',
 | 
			
		||||
//=>   query: { phone: '+8675309' },
 | 
			
		||||
//=>   pathname: '/føøß∂r',
 | 
			
		||||
//=>   path: '/føøß∂r?phone=+8675309',
 | 
			
		||||
//=>   href: '/føøß∂r?phone=+8675309',
 | 
			
		||||
//=>   _raw: '/føøß∂r?phone=+8675309' }
 | 
			
		||||
 | 
			
		||||
// Attaches awareness key
 | 
			
		||||
assert(req._decoded); //=> true
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## API
 | 
			
		||||
 | 
			
		||||
### url(req)
 | 
			
		||||
### url(req, toDecode)
 | 
			
		||||
Returns: `Object` or `undefined`
 | 
			
		||||
 | 
			
		||||
> **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.
 | 
			
		||||
 | 
			
		||||
#### req
 | 
			
		||||
Type: `IncomingMessage` or `{ url: string }`
 | 
			
		||||
Type: `IncomingMessage` or `Object`
 | 
			
		||||
 | 
			
		||||
The incoming HTTP request (`req`) or a plain `Object` with a `url` key.
 | 
			
		||||
 | 
			
		||||
> **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`.
 | 
			
		||||
 | 
			
		||||
#### toDecode
 | 
			
		||||
Type: `Boolean`<br>
 | 
			
		||||
Default: `false`
 | 
			
		||||
 | 
			
		||||
If enabled, the `url` will be fully decoded (via [`decodeURIComponent`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent)) and the output keys will be slightly different:
 | 
			
		||||
 | 
			
		||||
* `path`, `pathname`, `href`, `_raw` — will be the decoded strings
 | 
			
		||||
* `search` — if there is a value, will be decoded string, else remain `null`
 | 
			
		||||
* `query` — if there is a value, will be a decoded **object**, else remain `null`
 | 
			
		||||
 | 
			
		||||
Additionally, the `req` is mutated with `req._decoded = true` so as to prevent repetitive decoding.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## Benchmarks
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user