-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(react): Fix React Router v6 paramaterization #5515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Parses string form of URL into an object | ||
* // borrowed from https://tools.ietf.org/html/rfc3986#appendix-B | ||
* // intentionally using regex and not <a/> href parsing trick because React Native and other | ||
* // environments where DOM might not be available | ||
* @returns parsed URL object | ||
*/ | ||
export function parseUrl(url: string): { | ||
host?: string; | ||
path?: string; | ||
protocol?: string; | ||
relative?: string; | ||
} { | ||
if (!url) { | ||
return {}; | ||
} | ||
|
||
const match = url.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/); | ||
Check failureCode scanning / CodeQL Polynomial regular expression used on uncontrolled data
This [regular expression](1) that depends on [library input](2) may run slow on strings with many repetitions of '"'.
|
||
|
||
if (!match) { | ||
return {}; | ||
} | ||
|
||
// coerce to undefined values to empty string so we don't get 'undefined' | ||
const query = match[6] || ''; | ||
const fragment = match[8] || ''; | ||
return { | ||
host: match[4], | ||
path: match[5], | ||
protocol: match[2], | ||
relative: match[5] + query + fragment, // everything minus origin | ||
}; | ||
} | ||
|
||
/** | ||
* Strip the query string and fragment off of a given URL or path (if present) | ||
* | ||
* @param urlPath Full URL or path, including possible query string and/or fragment | ||
* @returns URL or path without query string or fragment | ||
*/ | ||
export function stripUrlQueryAndFragment(urlPath: string): string { | ||
// eslint-disable-next-line no-useless-escape | ||
return urlPath.split(/[\?#]/, 1)[0]; | ||
} | ||
|
||
/** | ||
* Returns number of URL segments of a passed string URL. | ||
*/ | ||
export function getNumberOfUrlSegments(url: string): number { | ||
// split at '/' or at '\/' to split regex urls correctly | ||
return url.split(/\\?\//).filter(s => s.length > 0 && s !== ',').length; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { getNumberOfUrlSegments, stripUrlQueryAndFragment } from '../src/url'; | ||
|
||
describe('stripQueryStringAndFragment', () => { | ||
const urlString = 'http://dogs.are.great:1231/yay/'; | ||
const queryString = '?furry=yes&funny=very'; | ||
const fragment = '#adoptnotbuy'; | ||
|
||
it('strips query string from url', () => { | ||
const urlWithQueryString = `${urlString}${queryString}`; | ||
expect(stripUrlQueryAndFragment(urlWithQueryString)).toBe(urlString); | ||
}); | ||
|
||
it('strips fragment from url', () => { | ||
const urlWithFragment = `${urlString}${fragment}`; | ||
expect(stripUrlQueryAndFragment(urlWithFragment)).toBe(urlString); | ||
}); | ||
|
||
it('strips query string and fragment from url', () => { | ||
const urlWithQueryStringAndFragment = `${urlString}${queryString}${fragment}`; | ||
expect(stripUrlQueryAndFragment(urlWithQueryStringAndFragment)).toBe(urlString); | ||
}); | ||
}); | ||
|
||
describe('getNumberOfUrlSegments', () => { | ||
test.each([ | ||
['regular path', '/projects/123/views/234', 4], | ||
['single param paramaterized path', '/users/:id/details', 3], | ||
['multi param paramaterized path', '/stores/:storeId/products/:productId', 4], | ||
['regex path', String(/\/api\/post[0-9]/), 2], | ||
])('%s', (_: string, input, output) => { | ||
expect(getNumberOfUrlSegments(input)).toEqual(output); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just out of curiosity: why the index for loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's to stay consistent with RR5 integration. Not sure if there's any specific reason there though.
sentry-javascript/packages/react/src/reactrouter.tsx
Lines 79 to 84 in 9b7131f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll just keep the explicit for loop, though technically it is extra bytes