0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

chore(ext/url): cleanup url ops (#14177)

This commit is contained in:
Divy Srivastava 2022-04-03 18:12:38 +05:30 committed by GitHub
parent a6e4b4297d
commit 1f7dd5eda9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 61 deletions

View file

@ -30,15 +30,15 @@
const _urlObject = Symbol("url object"); const _urlObject = Symbol("url object");
// WARNING: must match rust code's UrlSetter::* // WARNING: must match rust code's UrlSetter::*
const SET_HASH = 1; const SET_HASH = 0;
const SET_HOST = 2; const SET_HOST = 1;
const SET_HOSTNAME = 3; const SET_HOSTNAME = 2;
const SET_PASSWORD = 4; const SET_PASSWORD = 3;
const SET_PATHNAME = 5; const SET_PATHNAME = 4;
const SET_PORT = 6; const SET_PORT = 5;
const SET_PROTOCOL = 7; const SET_PROTOCOL = 6;
const SET_SEARCH = 8; const SET_SEARCH = 7;
const SET_USERNAME = 9; const SET_USERNAME = 8;
// Helper functions // Helper functions
function opUrlReparse(href, setter, value) { function opUrlReparse(href, setter, value) {

View file

@ -2,7 +2,6 @@
mod urlpattern; mod urlpattern;
use deno_core::error::generic_error;
use deno_core::error::type_error; use deno_core::error::type_error;
use deno_core::error::uri_error; use deno_core::error::uri_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
@ -13,7 +12,6 @@ use deno_core::url::quirks;
use deno_core::url::Url; use deno_core::url::Url;
use deno_core::Extension; use deno_core::Extension;
use deno_core::ZeroCopyBuf; use deno_core::ZeroCopyBuf;
use std::panic::catch_unwind;
use std::path::PathBuf; use std::path::PathBuf;
use crate::urlpattern::op_urlpattern_parse; use crate::urlpattern::op_urlpattern_parse;
@ -71,37 +69,39 @@ pub fn op_url_parse(
.parse(&href) .parse(&href)
.map_err(|_| type_error("Invalid URL"))?; .map_err(|_| type_error("Invalid URL"))?;
url_result(url, href, base_href) Ok(url_parts(url))
} }
#[derive( #[derive(PartialEq, Debug)]
serde_repr::Serialize_repr, serde_repr::Deserialize_repr, PartialEq, Debug,
)]
#[repr(u8)] #[repr(u8)]
pub enum UrlSetter { pub enum UrlSetter {
Hash = 1, Hash = 0,
Host = 2, Host = 1,
Hostname = 3, Hostname = 2,
Password = 4, Password = 3,
Pathname = 5, Pathname = 4,
Port = 6, Port = 5,
Protocol = 7, Protocol = 6,
Search = 8, Search = 7,
Username = 9, Username = 8,
} }
#[op] #[op]
pub fn op_url_reparse( pub fn op_url_reparse(
href: String, href: String,
setter_opts: (UrlSetter, String), setter_opts: (u8, String),
) -> Result<UrlParts, AnyError> { ) -> Result<UrlParts, AnyError> {
let mut url = Url::options() let mut url = Url::options()
.parse(&href) .parse(&href)
.map_err(|_| type_error("Invalid URL"))?; .map_err(|_| type_error("Invalid URL"))?;
let (setter, setter_value) = setter_opts; let (setter, setter_value) = setter_opts;
if setter > 8 {
return Err(type_error("Invalid URL setter"));
}
// SAFETY: checked to be less than 9.
let setter = unsafe { std::mem::transmute::<u8, UrlSetter>(setter) };
let value = setter_value.as_ref(); let value = setter_value.as_ref();
match setter { match setter {
UrlSetter::Hash => quirks::set_hash(&mut url, value), UrlSetter::Hash => quirks::set_hash(&mut url, value),
UrlSetter::Host => quirks::set_host(&mut url, value) UrlSetter::Host => quirks::set_host(&mut url, value)
@ -120,43 +120,24 @@ pub fn op_url_reparse(
.map_err(|_| uri_error("Invalid username"))?, .map_err(|_| uri_error("Invalid username"))?,
} }
url_result(url, href, None) Ok(url_parts(url))
} }
fn url_result( fn url_parts(url: Url) -> UrlParts {
url: Url, [
href: String, quirks::href(&url),
base_href: Option<String>, quirks::hash(&url),
) -> Result<UrlParts, AnyError> { quirks::host(&url),
// TODO(nayeemrmn): Panic that occurs in rust-url for the `non-spec:` quirks::hostname(&url),
// url-constructor wpt tests: https://github.com/servo/rust-url/issues/670. &quirks::origin(&url),
let username = catch_unwind(|| quirks::username(&url)).map_err(|_| { quirks::password(&url),
generic_error(format!( quirks::pathname(&url),
"Internal error while parsing \"{}\"{}, \ quirks::port(&url),
see https://github.com/servo/rust-url/issues/670", quirks::protocol(&url),
href, quirks::search(&url),
base_href quirks::username(&url),
.map(|b| format!(" against \"{}\"", b)) ]
.unwrap_or_default() .join("\n")
))
})?;
Ok(
[
quirks::href(&url),
quirks::hash(&url),
quirks::host(&url),
quirks::hostname(&url),
&quirks::origin(&url),
quirks::password(&url),
quirks::pathname(&url),
quirks::port(&url),
quirks::protocol(&url),
quirks::search(&url),
username,
]
.join("\n"),
)
} }
#[op] #[op]