-
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.
Merge pull request #411 from Kitware/drop-whatwg-url
Drop whatwg-url
- Loading branch information
Showing
10 changed files
with
118 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,67 @@ | ||
const SPECIAL_SCHEME_RE = /^(https?|ftp|file|wss?):$/; | ||
|
||
/** | ||
* Represents a URL object with relaxed writing semantics. | ||
* | ||
* The protocol field is now unconditionally writeable, | ||
* regardless of whether the protocol is special. | ||
*/ | ||
export class WriteableURL extends URL { | ||
_proto: string; | ||
|
||
constructor(url: string, base?: string) { | ||
super(url, base); | ||
this._proto = this.protocol; | ||
} | ||
|
||
set protocol(proto: string) { | ||
this._proto = proto; | ||
} | ||
|
||
get protocol() { | ||
return this._proto; | ||
} | ||
|
||
private replaceProtocol(url: string) { | ||
const re = new RegExp(`^${super.protocol}`); | ||
return url.replace(re, this._proto); | ||
} | ||
|
||
get origin() { | ||
return this.replaceProtocol(super.origin); | ||
} | ||
|
||
get href() { | ||
return this.replaceProtocol(super.href); | ||
} | ||
|
||
toString() { | ||
return this.replaceProtocol(super.toString()); | ||
} | ||
} | ||
|
||
/** | ||
* Parses a URL. | ||
* | ||
* 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. | ||
* | ||
* @param url | ||
* @param base | ||
* @returns | ||
*/ | ||
export function parseUrl(url: string, base?: string) { | ||
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 WriteableURL(url.replace(re, 'ws:'), base?.replace(re, 'ws:')); | ||
// This line doesn't work with the standard URL API. | ||
parsed.protocol = proto; | ||
return parsed; | ||
} |