This commit is contained in:
2022-07-18 02:50:52 +00:00
parent befd344ab0
commit 06181b34d6
8569 changed files with 818704 additions and 352705 deletions
+42
View File
@@ -0,0 +1,42 @@
const qs = require('querystring');
/**
* @typedef ParsedURL
* @type {import('.').ParsedURL}
*/
/**
* @typedef Request
* @property {string} url
* @property {ParsedURL} _parsedUrl
*/
/**
* @param {Request} req
* @returns {ParsedURL|void}
*/
function parse(req) {
let raw = req.url;
if (raw == null) return;
let prev = req._parsedUrl;
if (prev && prev.raw === raw) return prev;
let pathname=raw, search='', query;
if (raw.length > 1) {
let idx = raw.indexOf('?', 1);
if (idx !== -1) {
search = raw.substring(idx);
pathname = raw.substring(0, idx);
if (search.length > 1) {
query = qs.parse(search.substring(1));
}
}
}
return req._parsedUrl = { pathname, search, query, raw };
}
exports.parse = parse;
+40
View File
@@ -0,0 +1,40 @@
import * as qs from 'querystring';
/**
* @typedef ParsedURL
* @type {import('.').ParsedURL}
*/
/**
* @typedef Request
* @property {string} url
* @property {ParsedURL} _parsedUrl
*/
/**
* @param {Request} req
* @returns {ParsedURL|void}
*/
export function parse(req) {
let raw = req.url;
if (raw == null) return;
let prev = req._parsedUrl;
if (prev && prev.raw === raw) return prev;
let pathname=raw, search='', query;
if (raw.length > 1) {
let idx = raw.indexOf('?', 1);
if (idx !== -1) {
search = raw.substring(idx);
pathname = raw.substring(0, idx);
if (search.length > 1) {
query = qs.parse(search.substring(1));
}
}
}
return req._parsedUrl = { pathname, search, query, raw };
}
+10
View File
@@ -0,0 +1,10 @@
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;
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (https://lukeed.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
+66
View File
@@ -0,0 +1,66 @@
{
"_args": [
[
"@polka/url@1.0.0-next.21",
"/home/node/nuxt"
]
],
"_from": "@polka/url@1.0.0-next.21",
"_id": "@polka/url@1.0.0-next.21",
"_inBundle": false,
"_integrity": "sha512-a5Sab1C4/icpTZVzZc5Ghpz88yQtGOyNqYXcZgOssB2uuAr+wF/MvN6bgtW32q7HHrvBki+BsZ0OuNv6EV3K9g==",
"_location": "/@polka/url",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "@polka/url@1.0.0-next.21",
"name": "@polka/url",
"escapedName": "@polka%2furl",
"scope": "@polka",
"rawSpec": "1.0.0-next.21",
"saveSpec": null,
"fetchSpec": "1.0.0-next.21"
},
"_requiredBy": [
"/sirv"
],
"_resolved": "https://registry.npmjs.org/@polka/url/-/url-1.0.0-next.21.tgz",
"_spec": "1.0.0-next.21",
"_where": "/home/node/nuxt",
"author": {
"name": "Luke Edwards",
"email": "luke@lukeed.com",
"url": "https://lukeed.com"
},
"bugs": {
"url": "https://github.com/lukeed/polka/issues"
},
"description": "Super fast, memoized `req.url` parser",
"exports": {
".": {
"import": "./build.mjs",
"require": "./build.js"
},
"./package.json": "./package.json"
},
"files": [
"*.d.ts",
"build.*"
],
"gitHead": "8d6e31871225f4449e645ecba13de7014f772b34",
"homepage": "https://github.com/lukeed/polka#readme",
"license": "MIT",
"main": "build.js",
"module": "build.mjs",
"name": "@polka/url",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lukeed/polka.git"
},
"types": "index.d.ts",
"version": "1.0.0-next.21"
}
+68
View File
@@ -0,0 +1,68 @@
# @polka/url [![npm](https://badgen.now.sh/npm/v/@polka/url)](https://npmjs.org/package/@polka/url)
> 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`.
> **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.
## Install
```
$ npm install --save @polka/url
```
## Usage
```js
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
```
## API
### url(req)
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 }`
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`.
## Benchmarks
Check out the [`bench`](/bench) directory for in-depth benchmark results and comparisons.
## Support
Any issues or questions can be sent to the [Polka][polka] repository.<br>However, please specify that your inquiry is about `@polka/url` specifically.
## License
MIT © [Luke Edwards](https://lukeed.com)
[polka]: https://github.com/lukeed/polka