From f37f8323a60cdaa3cc753fe1e9bed9a613170110 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Wed, 17 Nov 2021 11:37:03 +0200 Subject: [PATCH] Switch to WHATWG URL Fixes #4 --- index.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index df75004..52bf616 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,5 @@ 'use strict'; -var parseUrl = require('url').parse; - var DEFAULT_PORTS = { ftp: 21, gopher: 70, @@ -17,12 +15,20 @@ var stringEndsWith = String.prototype.endsWith || function(s) { }; /** - * @param {string|object} url - The URL, or the result from url.parse. + * @param {string|URL} url - The URL, as a string or as URL object. * @return {string} The URL of the proxy that should handle the request to the * given URL. If no proxy is set, this will be an empty string. */ +// eslint-disable-next-line max-statements function getProxyForUrl(url) { - var parsedUrl = typeof url === 'string' ? parseUrl(url) : url || {}; + var parsedUrl = url || {}; + try { + if (typeof url === 'string') { + parsedUrl = new URL(url); + } + } catch (err) { + return ''; + } var proto = parsedUrl.protocol; var hostname = parsedUrl.host; var port = parsedUrl.port;