Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polka does not support unicode in routing patterns. #187

Open
aral opened this issue Dec 14, 2022 · 2 comments
Open

Polka does not support unicode in routing patterns. #187

aral opened this issue Dec 14, 2022 · 2 comments

Comments

@aral
Copy link
Contributor

aral commented Dec 14, 2022

The problem

Polka does not support unicode in routing patterns.

(Routing fails when unicode – e.g., emoji or an ü, etc. – is included in the routing pattern.)

Basic reproduction

import polka from 'polka'

polka()
  .get('/kitten', (_request, response) => response.end('Meow!'))
  .get('/😸', (_request, response) => response.end('Meow!'))
  .listen(3000, _ => {
    console.log('Visit http://localhost:3000/kitten (works)')
    console.log('and http://localhost:3000/😸 (doesn’t work).')
  })

I’m assuming this is actually an issue in trouter, so going to try and reproduce there now but opening the issue here since it’s the main project that’s impacted.

Nope, the bug is in Polka, not trouter:

import Trouter from 'trouter'

const router = new Trouter()

router
  .get('/kitten', _ => {
    console.log('Meow from plain text route path!')
  })
  .get('/😸', _ => {
    console.log('Meow from emoji route path!')
  })

// Find a route definition
const kittenText = router.find('GET', '/kitten')
const kittenEmoji = router.find('GET', '/😸')

// Both handlers execute as expected.
kittenText.handlers[0]()
kittenEmoji.handlers[0]()

You can replace the Kitten emoji with the letter ü and observe the same behaviour.

Workaround

You must manually URI encode every component of the file path before adding your route to the router. Unless I’m mistaken, this is basically what SvelteKit does also (see sveltejs/kit#2171).

So here’s the above example, with the workaround implemented:

import path from 'node:path'
import polka from 'polka'

const encodeFilePath = filePath => filePath
  .split(path.sep)
  .map(value => encodeURIComponent(value))
  .join(path.sep)

polka()
  .get(encodeFilePath('/kitten'), (_request, response) => response.end('Meow!'))
  .get(encodeFilePath('/😸'), (_request, response) => response.end('Meow!'))
  .listen(3000, _ => {
    console.log('Visit http://localhost:3000/kitten (works)')
    console.log('and http://localhost:3000/😸 (now also works).')
  })

Suggested solution

Based on the above workaround, Polka itself should apply the equivalent of the encodeFilePath() function internally and transparently when .get(), etc., is called.

Please let me know if you’d like a pull request for this; I’d be happy to prepare one.

@aral
Copy link
Contributor Author

aral commented Dec 15, 2022

Possibly the same issue as sveltejs/kit#2166

(I’m seeing this on Polka 1.0.0-next.22.)

@aral
Copy link
Contributor Author

aral commented Dec 15, 2022

More observations:

If I add the route like this, it works:

.get('/%F0%9F%98%B8', (_request, response) => response.end('Meow!'))

Even if I use middleware to rewrite manually decode the URI component, it doesn’t work.

e.g.,

.use((request, _response, next) => {
  console.log(request.path)
  request.path = decodeURIComponent(request.path)
  next()
})

You also cannot just encodeUriComponent() the path you’re adding to the router as that will encode the slashes too and it still doesn’t work.

@aral aral changed the title Router fails when emoji is included in path Router fails when emoji/unicode is included in path Dec 15, 2022
@aral aral changed the title Router fails when emoji/unicode is included in path Routing fails when unicode (e.g., emoji) is included in request path Dec 15, 2022
@aral aral changed the title Routing fails when unicode (e.g., emoji) is included in request path Polka does not support unicode in URIs. Dec 15, 2022
@aral aral changed the title Polka does not support unicode in URIs. Polka does not support unicode in routing patterns. Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant