-
Notifications
You must be signed in to change notification settings - Fork 61
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
82 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,31 @@ | ||
const SPECIAL_SCHEME_RE = /^(https?|ftp|file|wss?):$/; | ||
|
||
function parseWithNonSpecialScheme(url: string, scheme: string) { | ||
const re = new RegExp(`^${scheme}`); | ||
// Parse with a special scheme. Chrome + Firefox conform to | ||
// whatwg-url on special schemes. | ||
const src = new URL(url.replace(re, 'ws:')); | ||
|
||
// We cannot change a special scheme to a non-special scheme | ||
// and vice-versa, so we use a placeholder non-special scheme | ||
// and copy the components over. | ||
const dst = new URL('tmp://'); | ||
|
||
dst.hash = src.hash; | ||
dst.hostname = src.hostname; | ||
dst.port = src.port; | ||
dst.password = src.password; | ||
dst.username = src.username; | ||
dst.pathname = src.pathname; | ||
dst.search = src.search; | ||
dst.protocol = scheme; | ||
return dst; | ||
} | ||
|
||
export function parseUrl(url: string, base?: string) { | ||
const parsed = new URL(url, base); | ||
if (SPECIAL_SCHEME_RE.test(parsed.protocol)) { | ||
return parsed; | ||
} | ||
return parseWithNonSpecialScheme(parsed.toString(), parsed.protocol); | ||
} |