chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::uri_error;
|
|
|
|
use deno_core::error::AnyError;
|
2021-04-28 12:41:50 -04:00
|
|
|
use deno_core::include_js_files;
|
|
|
|
use deno_core::op_sync;
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
use deno_core::url::form_urlencoded;
|
|
|
|
use deno_core::url::quirks;
|
|
|
|
use deno_core::url::Url;
|
2021-04-28 12:41:50 -04:00
|
|
|
use deno_core::Extension;
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
use deno_core::ZeroCopyBuf;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
|
|
|
use std::panic::catch_unwind;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2021-04-28 12:41:50 -04:00
|
|
|
pub fn init() -> Extension {
|
2021-04-28 18:16:45 -04:00
|
|
|
Extension::builder()
|
|
|
|
.js(include_js_files!(
|
2021-08-11 06:27:05 -04:00
|
|
|
prefix "deno:ext/url",
|
2021-04-28 12:41:50 -04:00
|
|
|
"00_url.js",
|
2021-04-28 18:16:45 -04:00
|
|
|
))
|
|
|
|
.ops(vec![
|
2021-04-28 12:41:50 -04:00
|
|
|
("op_url_parse", op_sync(op_url_parse)),
|
|
|
|
(
|
|
|
|
"op_url_parse_search_params",
|
|
|
|
op_sync(op_url_parse_search_params),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"op_url_stringify_search_params",
|
|
|
|
op_sync(op_url_stringify_search_params),
|
|
|
|
),
|
2021-04-28 18:16:45 -04:00
|
|
|
])
|
|
|
|
.build()
|
2021-04-28 12:41:50 -04:00
|
|
|
}
|
|
|
|
|
2021-03-17 17:33:29 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct UrlParseArgs {
|
|
|
|
href: String,
|
|
|
|
base_href: Option<String>,
|
|
|
|
// If one of the following are present, this is a setter call. Apply the
|
|
|
|
// proper `Url::set_*()` method after (re)parsing `href`.
|
|
|
|
set_hash: Option<String>,
|
|
|
|
set_host: Option<String>,
|
|
|
|
set_hostname: Option<String>,
|
|
|
|
set_password: Option<String>,
|
|
|
|
set_pathname: Option<String>,
|
|
|
|
set_port: Option<String>,
|
|
|
|
set_protocol: Option<String>,
|
|
|
|
set_search: Option<String>,
|
|
|
|
set_username: Option<String>,
|
|
|
|
}
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct UrlParts {
|
|
|
|
href: String,
|
|
|
|
hash: String,
|
|
|
|
host: String,
|
|
|
|
hostname: String,
|
|
|
|
origin: String,
|
|
|
|
password: String,
|
|
|
|
pathname: String,
|
|
|
|
port: String,
|
|
|
|
protocol: String,
|
|
|
|
search: String,
|
|
|
|
username: String,
|
|
|
|
}
|
|
|
|
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
/// Parse `UrlParseArgs::href` with an optional `UrlParseArgs::base_href`, or an
|
|
|
|
/// optional part to "set" after parsing. Return `UrlParts`.
|
|
|
|
pub fn op_url_parse(
|
|
|
|
_state: &mut deno_core::OpState,
|
2021-03-17 17:33:29 -04:00
|
|
|
args: UrlParseArgs,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<UrlParts, AnyError> {
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
let base_url = args
|
|
|
|
.base_href
|
|
|
|
.as_ref()
|
|
|
|
.map(|b| Url::parse(b).map_err(|_| type_error("Invalid base URL")))
|
|
|
|
.transpose()?;
|
|
|
|
let mut url = Url::options()
|
|
|
|
.base_url(base_url.as_ref())
|
|
|
|
.parse(&args.href)
|
|
|
|
.map_err(|_| type_error("Invalid URL"))?;
|
|
|
|
|
|
|
|
if let Some(hash) = args.set_hash.as_ref() {
|
|
|
|
quirks::set_hash(&mut url, hash);
|
|
|
|
} else if let Some(host) = args.set_host.as_ref() {
|
|
|
|
quirks::set_host(&mut url, host).map_err(|_| uri_error("Invalid host"))?;
|
|
|
|
} else if let Some(hostname) = args.set_hostname.as_ref() {
|
|
|
|
quirks::set_hostname(&mut url, hostname)
|
|
|
|
.map_err(|_| uri_error("Invalid hostname"))?;
|
|
|
|
} else if let Some(password) = args.set_password.as_ref() {
|
|
|
|
quirks::set_password(&mut url, password)
|
|
|
|
.map_err(|_| uri_error("Invalid password"))?;
|
|
|
|
} else if let Some(pathname) = args.set_pathname.as_ref() {
|
|
|
|
quirks::set_pathname(&mut url, pathname);
|
|
|
|
} else if let Some(port) = args.set_port.as_ref() {
|
|
|
|
quirks::set_port(&mut url, port).map_err(|_| uri_error("Invalid port"))?;
|
|
|
|
} else if let Some(protocol) = args.set_protocol.as_ref() {
|
|
|
|
quirks::set_protocol(&mut url, protocol)
|
|
|
|
.map_err(|_| uri_error("Invalid protocol"))?;
|
|
|
|
} else if let Some(search) = args.set_search.as_ref() {
|
|
|
|
quirks::set_search(&mut url, search);
|
|
|
|
} else if let Some(username) = args.set_username.as_ref() {
|
|
|
|
quirks::set_username(&mut url, username)
|
|
|
|
.map_err(|_| uri_error("Invalid username"))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(nayeemrmn): Panic that occurs in rust-url for the `non-spec:`
|
|
|
|
// url-constructor wpt tests: https://github.com/servo/rust-url/issues/670.
|
|
|
|
let username = catch_unwind(|| quirks::username(&url)).map_err(|_| {
|
|
|
|
generic_error(format!(
|
|
|
|
"Internal error while parsing \"{}\"{}, \
|
|
|
|
see https://github.com/servo/rust-url/issues/670",
|
|
|
|
args.href,
|
|
|
|
args
|
|
|
|
.base_href
|
|
|
|
.map(|b| format!(" against \"{}\"", b))
|
|
|
|
.unwrap_or_default()
|
|
|
|
))
|
|
|
|
})?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(UrlParts {
|
|
|
|
href: quirks::href(&url).to_string(),
|
|
|
|
hash: quirks::hash(&url).to_string(),
|
|
|
|
host: quirks::host(&url).to_string(),
|
|
|
|
hostname: quirks::hostname(&url).to_string(),
|
|
|
|
origin: quirks::origin(&url),
|
|
|
|
password: quirks::password(&url).to_string(),
|
|
|
|
pathname: quirks::pathname(&url).to_string(),
|
|
|
|
port: quirks::port(&url).to_string(),
|
|
|
|
protocol: quirks::protocol(&url).to_string(),
|
|
|
|
search: quirks::search(&url).to_string(),
|
|
|
|
username: username.to_string(),
|
|
|
|
})
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_url_parse_search_params(
|
|
|
|
_state: &mut deno_core::OpState,
|
2021-04-20 08:47:22 -04:00
|
|
|
args: Option<String>,
|
|
|
|
zero_copy: Option<ZeroCopyBuf>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<Vec<(String, String)>, AnyError> {
|
2021-04-20 08:47:22 -04:00
|
|
|
let params = match (args, zero_copy) {
|
|
|
|
(None, Some(zero_copy)) => form_urlencoded::parse(&zero_copy)
|
|
|
|
.into_iter()
|
|
|
|
.map(|(k, v)| (k.as_ref().to_owned(), v.as_ref().to_owned()))
|
|
|
|
.collect(),
|
|
|
|
(Some(args), None) => form_urlencoded::parse(args.as_bytes())
|
|
|
|
.into_iter()
|
|
|
|
.map(|(k, v)| (k.as_ref().to_owned(), v.as_ref().to_owned()))
|
|
|
|
.collect(),
|
|
|
|
_ => return Err(type_error("invalid parameters")),
|
|
|
|
};
|
|
|
|
Ok(params)
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_url_stringify_search_params(
|
|
|
|
_state: &mut deno_core::OpState,
|
2021-03-17 17:33:29 -04:00
|
|
|
args: Vec<(String, String)>,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<String, AnyError> {
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
let search = form_urlencoded::Serializer::new(String::new())
|
2021-03-17 17:33:29 -04:00
|
|
|
.extend_pairs(args)
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
.finish();
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(search)
|
chore: split web op crate (#9635)
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
2021-03-12 10:17:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_declaration() -> PathBuf {
|
|
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_url.d.ts")
|
|
|
|
}
|