fix(core): URL normalization that survives non-ASCII and malformed input

Replace nanorouter's regex+decodeURI normalization with the WHATWG URL
parser. Path segments are percent-decoded exactly once in the trie, with
a keep-raw fallback instead of the URIError that crashed v7 on a literal
'%'. Route definitions and locations are NFC-normalized so 'café' matches
regardless of composition. Hash-to-slash rewriting now handles every hash,
not just the first, and state.href is decoded for humans (raw on failure).

Behavior changes from v7: params are no longer double-decoded (%2540 →
'%40', not '@'), and unmatchable encodings fall back to raw segments
instead of silently 404ing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014NgfSjHE11oFpoSnLVKLXd
This commit is contained in:
Bendik Aagaard Lynghaug
2026-09-08 16:50:34 +02:00
co-authored by Claude Fable 5
parent 4ad3d02207
commit 501e8f5927
3 changed files with 181 additions and 43 deletions
+11 -1
View File
@@ -14,6 +14,16 @@ import { ok, equal, notEqual } from './lib/assert.js'
const HISTORY_OBJECT = {}
// state.href is for reading: decode it for humans ('/caf%C3%A9' → '/café'),
// but never throw on malformed input — keep the raw string instead.
function safeDecode (location) {
try {
return decodeURI(location).normalize('NFC')
} catch (e) {
return location
}
}
export class Choo {
constructor (opts) {
const timing = nanotiming('choo.constructor')
@@ -247,7 +257,7 @@ export class Choo {
}
const matched = this.router.match(location)
this._handler = matched.cb
state.href = location
state.href = safeDecode(location)
state.query = nanoquery(queryString)
state.route = matched.route
state.params = matched.params