-
Notifications
You must be signed in to change notification settings - Fork 29.8k
fix: normalize-asset-prefix adding leading slash when URL assetPrefix is provided
#68518
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
devjiwonchoi
merged 3 commits into
canary
from
08-05-fix_normalize_asset_prefix_for_socket_if_start_with_http
Aug 7, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 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
fix: normalize asset prefix for socket if start with http
refactor: use URL obj refactor: use URL canParse Co-authored-by: Sebastian Silbermann <[email protected]> refactor
- Loading branch information
commit 38d17daf4ad14511e82a29c7fdb2bf2259bfecb4
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,19 +8,19 @@ function getSocketProtocol(assetPrefix: string): string { | |
| protocol = new URL(assetPrefix).protocol | ||
| } catch {} | ||
|
|
||
| return protocol === 'http:' ? 'ws' : 'wss' | ||
| return protocol === 'http:' ? 'ws:' : 'wss:' | ||
| } | ||
|
|
||
| export function getSocketUrl(assetPrefix: string | undefined): string { | ||
| const { hostname, port } = window.location | ||
| const protocol = getSocketProtocol(assetPrefix || '') | ||
| const prefix = normalizedAssetPrefix(assetPrefix) | ||
| const protocol = getSocketProtocol(assetPrefix || '') | ||
|
|
||
| // if original assetPrefix is a full URL with protocol | ||
| // we just update to use the correct `ws` protocol | ||
| if (assetPrefix?.replace(/^\/+/, '').includes('://')) { | ||
| return `${protocol}://${prefix}` | ||
| if (URL.canParse(prefix)) { | ||
| // since normalized asset prefix is ensured to be a URL format, | ||
| // we can safely replace the protocol | ||
devjiwonchoi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return prefix.replace(/^http/, 'ws') | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could be extra safe and use the |
||
| } | ||
|
|
||
| return `${protocol}://${hostname}:${port}${prefix}` | ||
| const { hostname, port } = window.location | ||
| return `${protocol}//${hostname}${port ? `:${port}` : ''}${prefix}` | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
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 |
|---|---|---|
| @@ -1,16 +1,19 @@ | ||
| export function normalizedAssetPrefix(assetPrefix: string | undefined): string { | ||
| const escapedAssetPrefix = assetPrefix?.replace(/^\/+/, '') || false | ||
| // remove all leading slashes and trailing slashes | ||
| const escapedAssetPrefix = assetPrefix?.replace(/^\/+|\/+$/g, '') || false | ||
|
|
||
| // assetPrefix as a url | ||
| if (escapedAssetPrefix && escapedAssetPrefix.startsWith('://')) { | ||
| return escapedAssetPrefix.split('://', 2)[1] | ||
| } | ||
|
|
||
| // assetPrefix is set to `undefined` or '/' | ||
| // if an assetPrefix was '/', we return empty string | ||
| // because it could be an unnecessary trailing slash | ||
| if (!escapedAssetPrefix) { | ||
| return '' | ||
| } | ||
|
|
||
| // assetPrefix is a common path but escaped so let's add one leading slash | ||
| if (URL.canParse(escapedAssetPrefix)) { | ||
| const url = new URL(escapedAssetPrefix).toString() | ||
| return url.endsWith('/') ? url.slice(0, -1) : url | ||
| } | ||
|
|
||
| // assuming assetPrefix here is a pathname-style, | ||
| // restore the leading slash | ||
| return `/${escapedAssetPrefix}` | ||
| } |
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.
added
:for consistency