trimPathRegex is quadratic on a long run of slashes

Aperta Adatta ai principianti
#757 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
77/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, node.js, typescript

Direzione di ricerca

Inizia individuando trimPathRegex e normalizePath nel codice sorgente del router, quindi riproduci in Node il tempo di esecuzione segnalato usando l’input fornito con molte barre. Modifica l’approccio di troncamento in modo che il tempo di esecuzione cresca linearmente e conferma che il troncamento preserva il comportamento atteso del percorso e che il caso con 100.000 barre termina rapidamente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Describe the bug

In @solidjs/router 2.0.1, trimPathRegex = /^\/+|(\/)\/+$/g (used by normalizePath) backtracks quadratically when a path has a long run of slashes followed by another character. The regex engine rescans the run from every start position, then fails at $. A client-side navigation to such a path blocks the main thread.

Your Example Website or App

No app needed; the regex alone reproduces it in Node (see steps).

Steps to Reproduce the Bug or Issue
  1. Run in Node:
const s = "/streams" + "/".repeat(100000) + "x";
const t = performance.now();
s.replace(/^\/+|(\/)\/+$/g, "$1");
console.log(Math.round(performance.now() - t), "ms");
  1. It prints about 2650 ms.
  2. In an app using the router, navigate client-side (link or history.pushState) to /streams followed by 100,000 slashes and x: the page freezes for about 0.9 s in Chromium and Firefox and 2.9 s in WebKit. With 1,000,000 slashes it does not return within 30 s.
Expected behavior

Trimming slashes takes linear time; a long path does not freeze the page. A loop over charCodeAt from the end (or a regex without the backtracking \/+$ alternative) does it in well under 1 ms for 100,000 slashes.

Screenshots or Videos

No response

Platform
  • @solidjs/router 2.0.1, solid-js 2.0.4
  • Node 26.13.1
  • Browsers: Chromium, Firefox and WebKit from Playwright 1.65.1
  • OS: Linux
Additional context

Servers usually reject such long URLs on a direct load (e.g. HTTP 431 near 20,000 characters), so the reach is client-side links and history.pushState.

Lingua principale
TypeScript
Stelle
1.3k
Fork
193
Merge medio
8h 48m
PR unite (30g)
21

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di solidjs/solid-router

Tutte le issue di solidjs/solid-router

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.