Skip to content

fix question mark stripped from url when no params are given (#776)#777

Closed
dzek69 wants to merge 2 commits intonode-fetch:masterfrom
dzek69:fix-776
Closed

fix question mark stripped from url when no params are given (#776)#777
dzek69 wants to merge 2 commits intonode-fetch:masterfrom
dzek69:fix-776

Conversation

@dzek69
Copy link
Copy Markdown
Contributor

@dzek69 dzek69 commented Apr 13, 2020

note: new URL() will return both search and hash with their starting character (? or #) but not when these parameters are empty, including when only their starting characters are given in url

however href keeps them both and this fix relies on that

hrefWithoutHash is technically incorrect when on that edge case with hash, but that doesn't matter here so I haven't wrote a code that doesn't matter in the final result

Fixes #776

@@ -0,0 +1,12 @@
export function getSearch(parsedURL) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure a helper function is needed for this. It can be written concisely without any function calls or allocations like so:

u.search || u.href[u.href.length - u.hash.length - 1] === '?' ? '?' : ''

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that, but I usually avoid "magic looking code" (or just unreadable at first sight) even if I personally like that. Decision is up to node-fetch authors.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and your code will fail, it should look for ? with includes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... What input will cause it to fail? Using includes will perform a linear search from the beginning of the string. Given that we know the input must have been valid to have been parsed by URL, we should be able to safely check the end of the href, minus the hash.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will fail for http://example.com/?# but if you include some hash check too it should be okay to test only the last character I think

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good spotting 😄 I suppose unit tests would be useful for covering such edge cases. How about this?

function getSearch(u) {
  if (u.search) {
    return u.search;
  }
  const lastOffset = u.href.length - 1;
  const hash = u.hash || u.href[lastOffset] === '#' ? '#' : '';
  return u.href[lastOffset - hash.length] === '?' ? '?' : '';
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me. I have added an unit test for the search string already, so updating my solution with yours should pass the tests

I don't know if just the hash alone causes issues anywhere, as it is not send to the server anyway

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pushed an update with your suggestion (and fixed missing parentheses). I have replaced u with parsedURL for readability (build job should minify the code, seems like builds are kind of work in progress so I guess this will come later).

Thank you for your suggestions

@xxczaki xxczaki mentioned this pull request Apr 14, 2020
35 tasks
@xxczaki
Copy link
Copy Markdown
Member

xxczaki commented Apr 20, 2020

See #782

@xxczaki xxczaki closed this Apr 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v3 incorrectly strips out question mark from URL when no query params after question mark

3 participants