-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
whatwg-url parses correctly, but is quite a big dependency. parseUrl() only handles Chrome+Firefox quirks regarding parsing of urls using non-special schemes.
- Loading branch information
Showing
10 changed files
with
101 additions
and
50 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { parseUrl } from '@/src/utils/url'; | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
describe('utils/url', () => { | ||
describe('parseUrl', () => { | ||
it('should parse a URL', () => { | ||
expect(parseUrl('https://example.com/path').pathname).to.equal('/path'); | ||
expect(parseUrl('gs://bucket/').protocol).to.equal('gs:'); | ||
expect(parseUrl('gs://bucket/path/object').pathname).to.equal( | ||
'/path/object' | ||
); | ||
expect(parseUrl('path/object', 'gs://bucket').protocol).to.equal('gs:'); | ||
}); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const SPECIAL_SCHEME_RE = /^(https?|ftp|file|wss?):$/; | ||
|
||
export interface URLParts { | ||
readonly hash: string; | ||
readonly hostname: string; | ||
readonly port: string; | ||
readonly password: string; | ||
readonly username: string; | ||
readonly pathname: string; | ||
readonly search: string; | ||
readonly protocol: string; | ||
} | ||
|
||
/** | ||
* Parses a URL into parts. | ||
* | ||
* Chrome, Firefox, and Safari parse URLS differently for | ||
* non-special schemes. parseUrl's goal is to have uniform | ||
* output across browsers for non-special schemes. | ||
* | ||
* This API intentionally restricts the return type to be | ||
* readonly, since it's not like the URL API where you can | ||
* reconstruct the URL from its parts. | ||
* | ||
* @param url | ||
* @param base | ||
* @returns | ||
*/ | ||
export function parseUrl(url: string, base?: string): URLParts { | ||
let parsed = new URL(url, base); | ||
if (SPECIAL_SCHEME_RE.test(parsed.protocol)) { | ||
return parsed; | ||
} | ||
|
||
// replace the scheme with a special scheme to get uniform parsing. | ||
const proto = parsed.protocol; | ||
const re = new RegExp(`^${proto}`); | ||
parsed = new URL(url.replace(re, 'ws:'), base?.replace(re, 'ws:')); | ||
|
||
return { | ||
hash: parsed.hash, | ||
hostname: parsed.hostname, | ||
port: parsed.port, | ||
password: parsed.password, | ||
username: parsed.username, | ||
pathname: parsed.pathname, | ||
search: parsed.search, | ||
protocol: proto, | ||
}; | ||
} |