rust-urlpattern icon indicating copy to clipboard operation
rust-urlpattern copied to clipboard

`URLPattern.exec(string)` implementation differs from the node polyfill

Open adriannsemb opened this issue 10 months ago • 0 comments

Hi,

I'm linking issue here from:

  • https://github.com/denoland/deno/issues/28247

as this library contains the implementation of URLPattern for deno.


Deno's URLPattern.exec(string) implementation differs from the node polyfill, which is causing some libraries to behave inconsistently or even break when running in Deno.

const pattern = ":protocol://:domain(.*)::port?/:locale(de)?/:path(.*)?";
const url = "https://localhost:5173/de";

const urlpattern = new URLPattern(pattern);
const match = urlpattern.exec(url);

console.log(match);

expected

{
  "inputs": ["https://localhost:5173/de"],
  "protocol": { "input": "https", "groups": { "protocol": "https" } },
  "username": { "input": "", "groups": { "0": "" } },
  "password": { "input": "", "groups": { "0": "" } },
  "hostname": { "input": "localhost", "groups": { "domain": "localhost" } },   //<----------
  "port": { "input": "5173", "groups": { "port": "5173" } },
  "pathname": { "input": "/de", "groups": { "locale": "de", "path": undefined } },
  "search": { "input": "", "groups": { "0": "" } },
  "hash": { "input": "", "groups": { "0": "" } }
}

actual

{
  "inputs": ["https://localhost:5173/de"],
  "protocol": { "input": "https", "groups": { "protocol": "https" } },
  "username": { "input": "", "groups": { "0": "" } },
  "password": { "input": "", "groups": { "0": "" } },
  "hostname": { "input": "localhost", "groups": { "0": "localhost" } },        //<----------
  "port": { "input": "5173", "groups": { "port": "5173" } },
  "pathname": { "input": "/de", "groups": { "locale": "de", "path": undefined } },
  "search": { "input": "", "groups": { "0": "" } },
  "hash": { "input": "", "groups": { "0": "" } }
}

difference

hostname.groups object does not contain the expected named group (domain), while in Chrome/Node's polyfill, it does ("groups": { "domain": "localhost" }).

adriannsemb avatar Mar 01 '25 17:03 adriannsemb