Skip to content

Commit 44a5757

Browse files
committed
package: add polka/url
1 parent 47e886e commit 44a5757

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

packages/url/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const { parse } = require('url');
2+
3+
module.exports = function (req) {
4+
let url = req.url;
5+
if (url === void 0) return url;
6+
7+
let obj = req._parsedUrl;
8+
if (obj && obj._raw === url) return obj;
9+
10+
obj = {};
11+
let c, len=url.length;
12+
obj.query = obj.search = null;
13+
obj.href = obj.path = obj.pathname = url;
14+
15+
while (--len) {
16+
c = url.charCodeAt(len);
17+
if (c === 35) { // #
18+
obj = parse(url);
19+
break;
20+
} else if (c === 63) { // ?
21+
obj.search = url.substring(len);
22+
obj.query = obj.search.substring(1);
23+
obj.pathname = url.substring(0, len);
24+
break;
25+
}
26+
}
27+
28+
obj._raw = url;
29+
30+
return (req._parsedUrl = obj);
31+
}

packages/url/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "@polka/url",
3+
"version": "0.0.0",
4+
"description": "Super fast, memoized `req.url` parser",
5+
"repository": "lukeed/polka",
6+
"license": "MIT",
7+
"files": [
8+
"*.js"
9+
],
10+
"author": {
11+
"name": "Luke Edwards",
12+
"email": "[email protected]",
13+
"url": "https://lukeed.com"
14+
},
15+
"publishConfig": {
16+
"access": "public"
17+
}
18+
}

packages/url/readme.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# @polka/url [![npm](https://badgen.now.sh/npm/v/@polka/url)](https://npmjs.org/package/@polka/url)
2+
3+
> Super fast, memoized `req.url` parser; _not_ limited to [Polka][polka]!
4+
5+
Parses the `url` from a [`IncomingMessage`](https://nodejs.org/api/http.html#http_class_http_incomingmessage) request.
6+
7+
If the `req.url` contains a hash (`#`) then the native [`url.parse`](https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost) will be used, returning a complete [`Url`](https://nodejs.org/api/url.html#url_class_url).<br>Otherwise, the return object will only contain the following keys: `search`, `query`, `pathname`, `path`, `href`, and `_raw`.
8+
9+
> **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.
10+
11+
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.
12+
13+
## Install
14+
15+
```
16+
$ npm install --save @polka/url
17+
```
18+
19+
## Usage
20+
21+
```js
22+
const parse = require('@polka/url');
23+
24+
let req = { url: '/foo/bar?fizz=buzz' };
25+
let foo = parse(req);
26+
//=> { search: '?fizz=buzz',
27+
//=> query: 'fizz=buzz',
28+
//=> pathname: '/foo/bar',
29+
//=> path: '/foo/bar?fizz=buzz',
30+
//=> href: '/foo/bar?fizz=buzz',
31+
//=> _raw: '/foo/bar?fizz=buzz' }
32+
33+
// Attaches result for future memoization
34+
assert.deepEqual(foo, req._parsedUrl); //=> true
35+
36+
// Process a request w/ a hashtag
37+
req = { url: '/foo/bar?fizz=buzz#hello' };
38+
let bar = parse(req);
39+
//=> Url {
40+
//=> protocol: null,
41+
//=> slashes: null,
42+
//=> auth: null,
43+
//=> host: null,
44+
//=> port: null,
45+
//=> hostname: null,
46+
//=> hash: '#hello',
47+
//=> search: '?fizz=buzz',
48+
//=> query: 'fizz=buzz',
49+
//=> pathname: '/foo/bar',
50+
//=> path: '/foo/bar?fizz=buzz',
51+
//=> href: '/foo/bar?fizz=buzz#hello',
52+
//=> _raw: '/foo/bar?fizz=buzz#hello' }
53+
54+
// Also attaches result for future memoization
55+
assert.deepEqual(bar, req._parsedUrl); //=> true
56+
```
57+
58+
## API
59+
60+
### url(req)
61+
Returns: `Object` or `undefined`
62+
63+
> **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.
64+
65+
#### req
66+
Type: `IncomingMessage`
67+
68+
The incoming HTTP request (`req`).
69+
70+
71+
72+
## Support
73+
74+
Any issues or questions can be sent to the [Polka][polka] repo, but please specify that your inquiry is about `@polka/url` specifically.
75+
76+
77+
## License
78+
79+
MIT © [Luke Edwards](https://lukeed.com)
80+
81+
[polka]: https://github.com/lukeed/polka

0 commit comments

Comments
 (0)