nodejs / nodejs/node

url: URLPattern rejects values requiring WebIDL USVString conversion for input and baseURL

Offen
#64,785 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
JavaScript
Sterne
122k
Forks
37.3k
Ø Merge
4 T. 2 Std.
Gemergte PRs (30 T.)
283

Beschreibung

Version

latest main branch

Platform
7.1.4-arch1-1
Subsystem

url

What steps will reproduce the bug?

  const { URL, URLPattern } = require('node:url');

  const base = 'https://example.com/';
  const numberPattern =
    new URLPattern('https://example.com/123');
  const fooPattern =
    new URLPattern('https://example.com/foo');

  const baseObject = {
    toString() {
      return base;
    },
  };

  const baseURLObject = new URL(base);

  function observe(label, callback) {
    try {
      console.log(label, callback());
    } catch (error) {
      console.log(label, error.code, error.name);
    }
  }

  // URLPatternInput conversion.
  observe(
    'constructor input:',
    () => new URLPattern(123, base).pathname,
  );

  observe(
    'test input:',
    () => numberPattern.test(123, base),
  );

  observe(
    'exec input:',
    () => numberPattern.exec(123, base) !== null,
  );

  // baseURL conversion. The three-argument constructor is intentional:
  // it unambiguously selects the baseURL overload.
  observe(
    'constructor baseURL:',
    () => new URLPattern('foo', baseObject, {}).pathname,
  );

  observe(
    'test baseURL:',
    () => fooPattern.test('foo', baseURLObject),
  );

  observe(
    'exec baseURL:',
    () => fooPattern.exec('foo', baseURLObject) !== null,
  );
How often does it reproduce? Is there a required condition?

Every

What is the expected behavior? Why is that the expected behavior?
  constructor input: /123
  test input: true
  exec input: true
  constructor baseURL: /foo
  test baseURL: true
  exec baseURL: true
What do you see instead?

Each case throws a TypeError with code === 'ERR_INVALID_ARG_TYPE'.

Additional information

The URL Pattern IDL (https://urlpattern.spec.whatwg.org/#urlpattern) defines:

typedef (USVString or URLPatternInit) URLPatternInput;

constructor(
URLPatternInput input,
USVString baseURL,
optional URLPatternOptions options = {}
);

constructor(
optional URLPatternInput input = {},
optional URLPatternOptions options = {}
);

boolean test(
optional URLPatternInput input = {},
optional USVString baseURL
);

URLPatternResult? exec(
optional URLPatternInput input = {},
optional USVString baseURL
);

WebIDL requires JavaScript values assigned to USVString to undergo USVString conversion
(https://webidl.spec.whatwg.org/#es-USVString), rather than already being JavaScript String values.

The constructor must first perform WebIDL overload resolution (https://webidl.spec.whatwg.org/#dfn-overload-resolution-algorithm).

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit den Einstiegspunkten node:url URLPattern-Konstruktor, test() und exec() und führe die Reproduktion anschließend gegen den neuesten main-Branch aus. Vergleiche die Argumentbehandlung mit den zitierten Regeln zur WebIDL-Überladungsauflösung und zu USVString. Erledigt ist die Aufgabe, wenn numerische Eingaben und object baseURL-Werte die erwarteten Ergebnisse statt ERR_INVALID_ARG_TYPE liefern, mit Regressionstests für die gezeigten Fälle.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, nodejs
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.