@@ -30,21 +30,25 @@ function resolve (baseURI, relativeURI, options) {
3030 parsed : baseParsed ,
3131 malformedAuthorityOrPort : baseMalformed ,
3232 malformedPercentEncoding : baseMalformedPercentEncoding ,
33- malformedSchemeSpecific : baseMalformedSchemeSpecific
33+ malformedSchemeSpecific : baseMalformedSchemeSpecific ,
34+ malformedHost : baseMalformedHost
3435 } = parseWithStatus ( baseURI , schemelessOptions )
3536 const {
3637 parsed : relativeParsed ,
3738 malformedAuthorityOrPort : relativeMalformed ,
3839 malformedPercentEncoding : relativeMalformedPercentEncoding ,
39- malformedSchemeSpecific : relativeMalformedSchemeSpecific
40+ malformedSchemeSpecific : relativeMalformedSchemeSpecific ,
41+ malformedHost : relativeMalformedHost
4042 } = parseWithStatus ( relativeURI , schemelessOptions )
4143 if (
4244 baseMalformed ||
4345 relativeMalformed ||
4446 baseMalformedPercentEncoding ||
4547 relativeMalformedPercentEncoding ||
4648 baseMalformedSchemeSpecific ||
47- relativeMalformedSchemeSpecific
49+ relativeMalformedSchemeSpecific ||
50+ baseMalformedHost ||
51+ relativeMalformedHost
4852 ) {
4953 throw new Error ( baseParsed . error || relativeParsed . error || 'URI is malformed.' )
5054 }
@@ -315,6 +319,7 @@ function hasMalformedComponentPercentEncoding (matches) {
315319 * @param {import('./types/index').Options } options
316320 * @param {{ domainHost?: boolean, unicodeSupport?: boolean }|undefined } schemeHandler
317321 * @param {boolean } isIP
322+ * @returns {boolean } whether host conversion failed
318323 */
319324function canonicalizeHost ( parsed , options , schemeHandler , isIP ) {
320325 if (
@@ -329,14 +334,16 @@ function canonicalizeHost (parsed, options, schemeHandler, isIP) {
329334 parsed . host = new URL ( 'http://' + parsed . host ) . hostname
330335 } catch ( e ) {
331336 parsed . error = parsed . error || "Host's domain name can not be converted to ASCII: " + e
337+ return true
332338 }
333339 }
340+ return false
334341}
335342
336343/**
337344 * @param {string } uri
338345 * @param {import('./types/index').Options } [opts]
339- * @returns {{ parsed: import('./types/index').URIComponent, malformedAuthorityOrPort: boolean, malformedPercentEncoding: boolean, malformedSchemeSpecific: boolean } }
346+ * @returns {{ parsed: import('./types/index').URIComponent, malformedAuthorityOrPort: boolean, malformedPercentEncoding: boolean, malformedSchemeSpecific: boolean, malformedHost: boolean } }
340347 */
341348function parseWithStatus ( uri , opts ) {
342349 const options = Object . assign ( { } , opts )
@@ -354,6 +361,7 @@ function parseWithStatus (uri, opts) {
354361 let malformedAuthorityOrPort = false
355362 let malformedPercentEncoding = false
356363 let malformedSchemeSpecific = false
364+ let malformedHost = false
357365
358366 let isIP = false
359367 if ( options . reference === 'suffix' ) {
@@ -456,15 +464,15 @@ function parseWithStatus (uri, opts) {
456464 const schemeHandler = getSchemeHandler ( options . scheme || parsed . scheme )
457465
458466 // convert Unicode IDN -> ASCII IDN when the effective scheme uses domain hosts
459- canonicalizeHost ( parsed , options , schemeHandler , isIP )
467+ malformedHost = canonicalizeHost ( parsed , options , schemeHandler , isIP )
460468
461469 if ( ! schemeHandler || ( schemeHandler && ! schemeHandler . skipNormalize ) ) {
462470 if ( uri . indexOf ( '%' ) !== - 1 ) {
463471 if ( parsed . scheme !== undefined ) {
464472 parsed . scheme = unescape ( parsed . scheme )
465473 }
466474 if ( parsed . host !== undefined ) {
467- parsed . host = reescapeHostDelimiters ( unescape ( parsed . host ) , isIP )
475+ parsed . host = reescapeHostDelimiters ( normalizePercentEncoding ( parsed . host , true ) , isIP )
468476 }
469477 }
470478 if ( parsed . path ) {
@@ -488,7 +496,7 @@ function parseWithStatus (uri, opts) {
488496 } else {
489497 parsed . error = parsed . error || 'URI can not be parsed.'
490498 }
491- return { parsed, malformedAuthorityOrPort, malformedPercentEncoding, malformedSchemeSpecific }
499+ return { parsed, malformedAuthorityOrPort, malformedPercentEncoding, malformedSchemeSpecific, malformedHost }
492500}
493501
494502/**
@@ -512,15 +520,16 @@ function normalizeString (uri, opts) {
512520/**
513521 * @param {string } uri
514522 * @param {import('./types/index').Options } [opts]
515- * @returns {{ normalized: string, malformedAuthorityOrPort: boolean, malformedPercentEncoding: boolean, malformedSchemeSpecific: boolean } }
523+ * @returns {{ normalized: string, malformedAuthorityOrPort: boolean, malformedPercentEncoding: boolean, malformedSchemeSpecific: boolean, malformedHost: boolean } }
516524 */
517525function normalizeStringWithStatus ( uri , opts ) {
518- const { parsed, malformedAuthorityOrPort, malformedPercentEncoding, malformedSchemeSpecific } = parseWithStatus ( uri , opts )
526+ const { parsed, malformedAuthorityOrPort, malformedPercentEncoding, malformedSchemeSpecific, malformedHost } = parseWithStatus ( uri , opts )
519527 return {
520- normalized : malformedAuthorityOrPort || malformedPercentEncoding || malformedSchemeSpecific ? uri : serialize ( parsed , opts ) ,
528+ normalized : malformedAuthorityOrPort || malformedPercentEncoding || malformedSchemeSpecific || malformedHost ? uri : serialize ( parsed , opts ) ,
521529 malformedAuthorityOrPort,
522530 malformedPercentEncoding,
523- malformedSchemeSpecific
531+ malformedSchemeSpecific,
532+ malformedHost
524533 }
525534}
526535
@@ -535,8 +544,8 @@ function normalizeComparableURI (uri, opts) {
535544 }
536545
537546 const value = typeof uri === 'string' ? uri : serialize ( uri , opts )
538- const { normalized, malformedAuthorityOrPort, malformedPercentEncoding, malformedSchemeSpecific } = normalizeStringWithStatus ( value , opts )
539- return malformedAuthorityOrPort || malformedPercentEncoding || malformedSchemeSpecific ? undefined : normalized
547+ const { normalized, malformedAuthorityOrPort, malformedPercentEncoding, malformedSchemeSpecific, malformedHost } = normalizeStringWithStatus ( value , opts )
548+ return malformedAuthorityOrPort || malformedPercentEncoding || malformedSchemeSpecific || malformedHost ? undefined : normalized
540549}
541550
542551const fastUri = {
0 commit comments