Skip to content

Incorrectly getting @ added on href #229

Closed
@gita-vahdatinia

Description

@gita-vahdatinia

Hi! 👋

After the change from #226 I was getting an incorrect evaluation of URLs when using toString().

Example: If I urlParseLibrary('/a/relative/path') I would get:

{
  auth: '',
  hash: '',
  host: '.',
  hostname: '.',
  href: 'https://./a/relative/path',
  origin: 'https://.',
  password: '',
  pathname: '/a/relative/path',
  port: '',
  protocol: 'https:',
  query: '',
  slashes: true,
  username: ''
}

If I set the hostname to '' I would get the href as 'https://@/a/relative/path',

{
  auth: '',
  hash: '',
  host: '',
  hostname: '',
  href: 'https://@/a/relative/path',
  origin: 'null',
  password: '',
  pathname: '/a/relative/path',
  port: '',
  protocol: 'https:',
  query: '',
  slashes: true,
  username: ''
}

Here is the diff that solved my problem:

diff --git a/node_modules/url-parse/index.js b/node_modules/url-parse/index.js
index b86c29f..f8e9252 100644
--- a/node_modules/url-parse/index.js
+++ b/node_modules/url-parse/index.js
@@ -547,7 +547,7 @@ function toString(stringify) {
     url.protocol !== 'file:' &&
     isSpecial(url.protocol) &&
     !host &&
-    url.pathname !== '/'
+    !url.pathname.startsWith('/')
   ) {
     //
     // Add back the empty userinfo, otherwise the original invalid URL
@@ -555,7 +555,6 @@ function toString(stringify) {
     //
     result += '@';
   }
-
   //
   // Trailing colon is removed from `url.host` when it is parsed. If it still
   // ends with a colon, then add back the trailing colon that was removed. This

Is this an intentional change? Please let me know Ill open PR

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