Skip to content

Commit

Permalink
break: do not decode req.path by default (#172)
Browse files Browse the repository at this point in the history
* [fix] properly parse path

* Update packages/polka/index.js

Co-authored-by: Luke Edwards <[email protected]>

* spaces to tabs

* update readme

Co-authored-by: Luke Edwards <[email protected]>
  • Loading branch information
benmccann and lukeed authored Aug 13, 2021
1 parent 4885177 commit 6ef32a6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
7 changes: 5 additions & 2 deletions packages/polka/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ class Polka extends Router {
}

handler(req, res, next) {
let info = this.parse(req, true);
let info = this.parse(req);
let obj = this.find(req.method, req.path=info.pathname);

req.params = obj.params;
req.params = {};
for (let key in obj.params) {
req.params[key] = decodeURIComponent(obj.params[key]);
}
req.originalUrl = req.originalUrl || req.url;
req.url = info.pathname + info.search;
req.query = info.query || {};
Expand Down
8 changes: 4 additions & 4 deletions packages/polka/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1425,16 +1425,16 @@ test('HEAD', async () => {
});


test('decode url', async () => {
test('encoded url', async () => {
// t.plan(8);

let sub = (
polka()
.get('/:foo', (req, res) => {
assert.ok('~> inside "GET /sub/:foo" handler')
assert.ok(req._decoded, '~> marked as decoded');
assert.is(req.path, '/føøß∂r', '~> decoded "path" value');
assert.is(req.url, '/føøß∂r?phone=%2b8675309', '~> decoded "url" value partially');
assert.ok(!req._decoded, '~> not marked as decoded');
assert.is(req.path, '/f%C3%B8%C3%B8%C3%9F%E2%88%82r', '~> "path" value');
assert.is(req.url, '/f%C3%B8%C3%B8%C3%9F%E2%88%82r?phone=%2b8675309', '~> "url" value');
assert.is(req.params.foo, 'føøß∂r', '~> decoded "params.foo" segment');
assert.is(req.query.phone, '+8675309', '~~> does NOT decode "req.query" keys twice');
res.end('done');
Expand Down
12 changes: 1 addition & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,12 @@ You may also pass a sub-application, which _must_ be accompanied by a `base` pat
Please see [`Middleware`](#middleware) and [Express' middleware examples](http://expressjs.com/en/4x/api.html#middleware-callback-function-examples) for more info.


### parse(req, toDecode=true)
### parse(req, toDecode=false)

Returns: `Object` or `undefined`

As of `v0.5.0`, this is an alias of the [`@polka/url`](/packages/url) module. For nearly all cases, you'll notice no changes.

One important difference is that URL decoding is enabled by default in polka but it is disabled by default in the `@polka/url` module. You can disable the URL decoding if required,

```js
const app = polka();
const { parse } = require('@polka/url');
app.parse = (req, toDecode) => {
return parse(req, false); // or simply, return parse(req);
}
```

But, for whatever reason, you can quickly swap in [`parseurl`](https://github.com/pillarjs/parseurl) again:

```js
Expand Down

0 comments on commit 6ef32a6

Please sign in to comment.