From 88ac4f709c909f3a7154ff9e16e6aed4d7dec06e Mon Sep 17 00:00:00 2001 From: Redfire Date: Sat, 4 Nov 2023 00:32:42 +0800 Subject: [PATCH] Split Implementation Blocks for Native Classes Fixed URLSearchParams `getAll` Method --- runtime/src/globals/fetch/header.rs | 10 ++- runtime/src/globals/fetch/request/mod.rs | 25 ++---- runtime/src/globals/fetch/response/mod.rs | 94 ++++++++++++----------- runtime/src/globals/url/search_params.rs | 2 +- 4 files changed, 63 insertions(+), 68 deletions(-) diff --git a/runtime/src/globals/fetch/header.rs b/runtime/src/globals/fetch/header.rs index 379c4798..81697715 100644 --- a/runtime/src/globals/fetch/header.rs +++ b/runtime/src/globals/fetch/header.rs @@ -155,13 +155,12 @@ pub struct Headers { pub(crate) kind: HeadersKind, } -#[js_class] impl Headers { - pub(crate) fn new(kind: HeadersKind) -> Headers { + pub fn new(kind: HeadersKind) -> Headers { Headers { kind, ..Headers::default() } } - pub(crate) fn from_array(vec: Vec, mut headers: HeaderMap, kind: HeadersKind) -> Result { + pub fn from_array(vec: Vec, mut headers: HeaderMap, kind: HeadersKind) -> Result { for entry in vec { let mut name = entry.name; let value = entry.value; @@ -177,7 +176,10 @@ impl Headers { kind, }) } +} +#[js_class] +impl Headers { #[ion(constructor)] pub fn constructor(init: Option) -> Result { init.unwrap_or_default().into_headers(HeaderMap::default(), HeadersKind::None) @@ -497,7 +499,7 @@ pub(crate) fn remove_all_header_entries(headers: &mut HeaderMap, name: &HeaderNa } } -pub fn get_header(headers: &HeaderMap, name: &HeaderName) -> Option
{ +fn get_header(headers: &HeaderMap, name: &HeaderName) -> Option
{ let split = headers.get_all(name).into_iter().map(split_value); let mut values = Vec::with_capacity(split.size_hint().0); for value in split { diff --git a/runtime/src/globals/fetch/request/mod.rs b/runtime/src/globals/fetch/request/mod.rs index bc6acaf3..c3c0743b 100644 --- a/runtime/src/globals/fetch/request/mod.rs +++ b/runtime/src/globals/fetch/request/mod.rs @@ -300,11 +300,15 @@ impl Request { pub fn get_duplex(&self) -> String { String::from("half") } +} + +impl Clone for Request { + fn clone(&self) -> Request { + let method = self.method().clone(); + let uri = self.uri().clone(); - #[allow(clippy::should_implement_trait)] - #[ion(skip)] - pub fn clone(&self) -> Request { - let request = clone_request(&self.request); + let request = hyper::Request::builder().method(method).uri(uri); + let request = request.body(Body::empty()).unwrap(); let url = self.locations.last().unwrap().clone(); Request { @@ -348,16 +352,3 @@ impl<'cx> FromValue<'cx> for &'cx Request { } } } - -pub(crate) fn clone_request(request: &hyper::Request) -> hyper::Request { - let method = request.method().clone(); - let uri = request.uri().clone(); - let headers = request.headers().clone(); - - let mut request = hyper::Request::builder().method(method).uri(uri); - if let Some(head) = request.headers_mut() { - *head = headers; - } - - request.body(Body::empty()).unwrap() -} diff --git a/runtime/src/globals/fetch/response/mod.rs b/runtime/src/globals/fetch/response/mod.rs index ae6d06e5..48d6a93a 100644 --- a/runtime/src/globals/fetch/response/mod.rs +++ b/runtime/src/globals/fetch/response/mod.rs @@ -48,52 +48,8 @@ pub struct Response { pub(crate) range_requested: bool, } -#[js_class] impl Response { - #[ion(constructor)] - pub fn constructor(cx: &Context, body: Option, init: Option) -> Result { - let init = init.unwrap_or_default(); - - let response = hyper::Response::builder().status(init.status).body(Body::empty())?; - let mut response = Response { - reflector: Reflector::default(), - - response: Some(response), - headers: Box::default(), - body: None, - body_used: false, - - kind: ResponseKind::default(), - url: None, - redirected: false, - - status: Some(init.status), - status_text: init.status_text, - - range_requested: false, - }; - - let mut headers = init.headers.into_headers(HeaderMap::new(), HeadersKind::Response)?; - - if let Some(body) = body { - if init.status == StatusCode::NO_CONTENT || init.status == StatusCode::RESET_CONTENT || init.status == StatusCode::NOT_MODIFIED { - return Err(Error::new("Received non-null body with null body status.", ErrorKind::Type)); - } - - if let Some(kind) = body.kind { - if !headers.headers.contains_key(CONTENT_TYPE) { - headers.headers.append(CONTENT_TYPE, HeaderValue::from_str(&kind.to_string()).unwrap()); - } - } - response.body = Some(body); - } - - response.headers.set(Headers::new_object(cx, Box::new(headers))); - - Ok(response) - } - - pub(crate) fn new(response: hyper::Response, url: Url) -> Response { + pub fn new(response: hyper::Response, url: Url) -> Response { let status = response.status(); let status_text = if let Some(reason) = response.extensions().get::() { Some(String::from_utf8(reason.as_bytes().to_vec()).unwrap()) @@ -120,7 +76,7 @@ impl Response { } } - pub(crate) fn new_from_bytes(bytes: Bytes, url: Url) -> Response { + pub fn new_from_bytes(bytes: Bytes, url: Url) -> Response { let response = hyper::Response::builder().body(Body::from(bytes)).unwrap(); Response { reflector: Reflector::default(), @@ -140,6 +96,52 @@ impl Response { range_requested: false, } } +} + +#[js_class] +impl Response { + #[ion(constructor)] + pub fn constructor(cx: &Context, body: Option, init: Option) -> Result { + let init = init.unwrap_or_default(); + + let response = hyper::Response::builder().status(init.status).body(Body::empty())?; + let mut response = Response { + reflector: Reflector::default(), + + response: Some(response), + headers: Box::default(), + body: None, + body_used: false, + + kind: ResponseKind::default(), + url: None, + redirected: false, + + status: Some(init.status), + status_text: init.status_text, + + range_requested: false, + }; + + let mut headers = init.headers.into_headers(HeaderMap::new(), HeadersKind::Response)?; + + if let Some(body) = body { + if init.status == StatusCode::NO_CONTENT || init.status == StatusCode::RESET_CONTENT || init.status == StatusCode::NOT_MODIFIED { + return Err(Error::new("Received non-null body with null body status.", ErrorKind::Type)); + } + + if let Some(kind) = body.kind { + if !headers.headers.contains_key(CONTENT_TYPE) { + headers.headers.append(CONTENT_TYPE, HeaderValue::from_str(&kind.to_string()).unwrap()); + } + } + response.body = Some(body); + } + + response.headers.set(Headers::new_object(cx, Box::new(headers))); + + Ok(response) + } #[ion(get)] pub fn get_type(&self) -> String { diff --git a/runtime/src/globals/url/search_params.rs b/runtime/src/globals/url/search_params.rs index c3ae0a4b..735657a7 100644 --- a/runtime/src/globals/url/search_params.rs +++ b/runtime/src/globals/url/search_params.rs @@ -106,7 +106,7 @@ impl URLSearchParams { self.pairs.iter().find(|(k, _)| k == &key).map(|(_, v)| v.clone()) } - #[ion(name = "get_all")] + #[ion(name = "getAll")] pub fn get_all(&self, key: String) -> Vec { self.pairs.iter().filter(|(k, _)| k == &key).map(|(_, v)| v.clone()).collect() }