-
-
Notifications
You must be signed in to change notification settings - Fork 122
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
Add support for custom protocols #153
Conversation
@@ -394,3 +394,10 @@ test('does not have exponential performance for data URLs', t => { | |||
t.true(difference < 100, `Execution time: ${difference}`); | |||
} | |||
}); | |||
|
|||
test('supports custom protocol', t => { | |||
t.is(normalizeUrl('custom://sindresorhus.com'), 'custom://sindresorhus.com'); |
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.
Can you also add a test with custom:sindresorhus.com
(no slashes)?
|
||
// Prepend protocol | ||
if (!isRelativeUrl) { | ||
if (!isRelativeUrl & !hasCustomProtocol) { |
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.
if (!isRelativeUrl & !hasCustomProtocol) { | |
if (!isRelativeUrl && !hasCustomProtocol) { |
From the issue:
Did you? |
Friendly bump :) |
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.
Nice thanks :)!
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.
Approve
@@ -86,9 +86,10 @@ export default function normalizeUrl(urlString, options) { | |||
|
|||
const hasRelativeProtocol = urlString.startsWith('//'); | |||
const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString); | |||
const hasCustomProtocol = /^[a-z.+-]*[.+-][a-z.+-]*:\/\//i.test(urlString); |
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.
const hasCustomProtocol = /^[a-z.+-]*[.+-][a-z.+-]*:\/\//i.test(urlString); | |
const hasCustomProtocol = /^[a-z][a-z.+-]*?[.+-][a-z.+-]*?:/i.test(urlString); |
a protocol does not need //
and it has to start with a-z
: https://stackoverflow.com/questions/3641722/valid-characters-for-uri-schemes
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.
Also add a sensible length limit to prevent ReDoS abuse.
I may be oversimplifying here, but this feels like a bug fix to not apply the protocol prefix when a custom prefix is present. Retains existing functionality and adds support for custom protocols. Closes #152.