2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
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
|
|
|
|
2021-09-08 05:14:29 -04:00
|
|
|
mod urlpattern;
|
|
|
|
|
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::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;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
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;
|
2022-03-16 20:25:44 -04:00
|
|
|
use std::path::PathBuf;
|
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
|
|
|
|
2021-09-08 05:14:29 -04:00
|
|
|
use crate::urlpattern::op_urlpattern_parse;
|
|
|
|
use crate::urlpattern::op_urlpattern_process_match_input;
|
|
|
|
|
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-09-08 05:14:29 -04:00
|
|
|
"01_urlpattern.js",
|
2021-04-28 18:16:45 -04:00
|
|
|
))
|
|
|
|
.ops(vec![
|
2022-03-14 13:44:15 -04:00
|
|
|
op_url_parse::decl(),
|
|
|
|
op_url_reparse::decl(),
|
|
|
|
op_url_parse_search_params::decl(),
|
|
|
|
op_url_stringify_search_params::decl(),
|
|
|
|
op_urlpattern_parse::decl(),
|
|
|
|
op_urlpattern_process_match_input::decl(),
|
2021-04-28 18:16:45 -04:00
|
|
|
])
|
|
|
|
.build()
|
2021-04-28 12:41:50 -04:00
|
|
|
}
|
|
|
|
|
2021-08-19 07:41:47 -04:00
|
|
|
// UrlParts is a \n joined string of the following parts:
|
|
|
|
// #[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,
|
|
|
|
// }
|
|
|
|
// TODO: implement cleaner & faster serialization
|
|
|
|
type UrlParts = String;
|
2021-04-05 12:40:24 -04:00
|
|
|
|
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`.
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
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(
|
2021-08-18 17:21:33 -04:00
|
|
|
href: String,
|
|
|
|
base_href: Option<String>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<UrlParts, AnyError> {
|
2021-08-18 17:21:33 -04:00
|
|
|
let base_url = base_href
|
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
|
|
|
.as_ref()
|
|
|
|
.map(|b| Url::parse(b).map_err(|_| type_error("Invalid base URL")))
|
|
|
|
.transpose()?;
|
2021-08-18 17:21:33 -04:00
|
|
|
let url = Url::options()
|
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
|
|
|
.base_url(base_url.as_ref())
|
2021-08-18 17:21:33 -04:00
|
|
|
.parse(&href)
|
|
|
|
.map_err(|_| type_error("Invalid URL"))?;
|
|
|
|
|
2022-04-03 08:42:38 -04:00
|
|
|
Ok(url_parts(url))
|
2021-08-18 17:21:33 -04:00
|
|
|
}
|
|
|
|
|
2022-04-03 08:42:38 -04:00
|
|
|
#[derive(PartialEq, Debug)]
|
2021-08-18 17:21:33 -04:00
|
|
|
#[repr(u8)]
|
|
|
|
pub enum UrlSetter {
|
2022-04-03 08:42:38 -04:00
|
|
|
Hash = 0,
|
|
|
|
Host = 1,
|
|
|
|
Hostname = 2,
|
|
|
|
Password = 3,
|
|
|
|
Pathname = 4,
|
|
|
|
Port = 5,
|
|
|
|
Protocol = 6,
|
|
|
|
Search = 7,
|
|
|
|
Username = 8,
|
2021-08-18 17:21:33 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-08-18 17:21:33 -04:00
|
|
|
pub fn op_url_reparse(
|
|
|
|
href: String,
|
2022-04-03 08:42:38 -04:00
|
|
|
setter_opts: (u8, String),
|
2021-08-18 17:21:33 -04:00
|
|
|
) -> Result<UrlParts, AnyError> {
|
|
|
|
let mut url = Url::options()
|
|
|
|
.parse(&href)
|
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
|
|
|
.map_err(|_| type_error("Invalid URL"))?;
|
|
|
|
|
2021-08-18 17:21:33 -04:00
|
|
|
let (setter, setter_value) = setter_opts;
|
2022-04-03 08:42:38 -04:00
|
|
|
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) };
|
2021-08-18 17:21:33 -04:00
|
|
|
let value = setter_value.as_ref();
|
|
|
|
match setter {
|
|
|
|
UrlSetter::Hash => quirks::set_hash(&mut url, value),
|
|
|
|
UrlSetter::Host => quirks::set_host(&mut url, value)
|
|
|
|
.map_err(|_| uri_error("Invalid host"))?,
|
|
|
|
UrlSetter::Hostname => quirks::set_hostname(&mut url, value)
|
|
|
|
.map_err(|_| uri_error("Invalid hostname"))?,
|
|
|
|
UrlSetter::Password => quirks::set_password(&mut url, value)
|
|
|
|
.map_err(|_| uri_error("Invalid password"))?,
|
|
|
|
UrlSetter::Pathname => quirks::set_pathname(&mut url, value),
|
|
|
|
UrlSetter::Port => quirks::set_port(&mut url, value)
|
|
|
|
.map_err(|_| uri_error("Invalid port"))?,
|
|
|
|
UrlSetter::Protocol => quirks::set_protocol(&mut url, value)
|
|
|
|
.map_err(|_| uri_error("Invalid protocol"))?,
|
|
|
|
UrlSetter::Search => quirks::set_search(&mut url, value),
|
|
|
|
UrlSetter::Username => quirks::set_username(&mut url, value)
|
|
|
|
.map_err(|_| uri_error("Invalid username"))?,
|
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
|
|
|
}
|
|
|
|
|
2022-04-03 08:42:38 -04:00
|
|
|
Ok(url_parts(url))
|
2021-08-18 17:21:33 -04:00
|
|
|
}
|
|
|
|
|
2022-04-03 08:42:38 -04:00
|
|
|
fn url_parts(url: Url) -> UrlParts {
|
|
|
|
[
|
|
|
|
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),
|
|
|
|
quirks::username(&url),
|
|
|
|
]
|
|
|
|
.join("\n")
|
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
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
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(
|
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
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-05-13 04:36:31 -04:00
|
|
|
pub fn op_url_stringify_search_params(args: Vec<(String, String)>) -> 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
|
|
|
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();
|
2022-05-13 04:36:31 -04:00
|
|
|
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
|
|
|
}
|
2022-03-16 20:25:44 -04:00
|
|
|
|
|
|
|
pub fn get_declaration() -> PathBuf {
|
|
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_url.d.ts")
|
|
|
|
}
|