sindresorhus / normalize-url

Normalize a URL
MIT License
840 stars 122 forks source link

No support for "tel:xxxx", "mailto:xxx" like urls #177

Closed daKmoR closed 1 year ago

daKmoR commented 2 years ago

I run all < href="x"> url through normalize... but it does fail on standard protocols like mailto:, tel:.

should that be supported or do I need to "filter" non-http-like URLs before?

Code to reproduce

import normalizeUrl from 'normalize-url';

// 1.
console.log(normalizeUrl('tel:004346382763'));

// 2.
console.log(normalizeUrl('mailto:office@foo.com'));

Expected behavior

Do not touch the URLs e.g. return tel:004346382763 and mailto:office@foo.com

Actual behavior

  1. it throws

    TypeError [ERR_INVALID_URL]: Invalid URL
      at new NodeError (node:internal/errors:387:5)
      at URL.onParseError (node:internal/url:564:9)
      at new URL (node:internal/url:644:5)
      at normalizeUrl (file:///Users/html/rocket/node_modules/normalize-url/index.js:97:20)
      at file:///Users/html/rocket/packages/check-html-links/foo.js:3:13
      at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
      at async Promise.all (index 0)
      at async ESMLoader.import (node:internal/modules/esm/loader:541:24)
      at async loadESM (node:internal/process/esm_loader:91:5)
      at async handleMainPromise (node:internal/modules/run_main:65:12) {
    input: 'http:tel:004346382763',
    code: 'ERR_INVALID_URL'
    }
  2. Results in http://foo.com

Investigation

it always seems to prepend http: which leads to the invalid http:tel:004346382763 URL which then throws on the new URL here https://github.com/sindresorhus/normalize-url/blob/main/index.js#L96