Skip to content

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

Description

@Archkon

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions