-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
dc1088a
commit c721b8c
Showing
2 changed files
with
18 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 { | ||
// remove all leading slashes | ||
const escapedAssetPrefix = assetPrefix?.replace(/^\/+/, '') || 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 { hostname, port, protocol, pathname } = new URL(escapedAssetPrefix) | ||
return `${protocol}//${hostname}${port ? `:${port}` : ''}${pathname}` | ||
} | ||
|
||
// assuming assetPrefix here is a pathname-style, | ||
// restore the leading slash | ||
return `/${escapedAssetPrefix}` | ||
} |