Skip to content

Commit

Permalink
Split Implementation Blocks for Native Classes
Browse files Browse the repository at this point in the history
Fixed URLSearchParams `getAll` Method
  • Loading branch information
Redfire75369 committed Nov 3, 2023
1 parent c94151c commit 88ac4f7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 68 deletions.
10 changes: 6 additions & 4 deletions runtime/src/globals/fetch/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HeaderEntry>, mut headers: HeaderMap, kind: HeadersKind) -> Result<Headers> {
pub fn from_array(vec: Vec<HeaderEntry>, mut headers: HeaderMap, kind: HeadersKind) -> Result<Headers> {
for entry in vec {
let mut name = entry.name;
let value = entry.value;
Expand All @@ -177,7 +176,10 @@ impl Headers {
kind,
})
}
}

#[js_class]
impl Headers {
#[ion(constructor)]
pub fn constructor(init: Option<HeadersInit>) -> Result<Headers> {
init.unwrap_or_default().into_headers(HeaderMap::default(), HeadersKind::None)
Expand Down Expand Up @@ -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<Header> {
fn get_header(headers: &HeaderMap, name: &HeaderName) -> Option<Header> {
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 {
Expand Down
25 changes: 8 additions & 17 deletions runtime/src/globals/fetch/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -348,16 +352,3 @@ impl<'cx> FromValue<'cx> for &'cx Request {
}
}
}

pub(crate) fn clone_request(request: &hyper::Request<Body>) -> hyper::Request<Body> {
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()
}
94 changes: 48 additions & 46 deletions runtime/src/globals/fetch/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FetchBody>, init: Option<ResponseInit>) -> Result<Response> {
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<Body>, url: Url) -> Response {
pub fn new(response: hyper::Response<Body>, url: Url) -> Response {
let status = response.status();
let status_text = if let Some(reason) = response.extensions().get::<ReasonPhrase>() {
Some(String::from_utf8(reason.as_bytes().to_vec()).unwrap())
Expand All @@ -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(),
Expand All @@ -140,6 +96,52 @@ impl Response {
range_requested: false,
}
}
}

#[js_class]
impl Response {
#[ion(constructor)]
pub fn constructor(cx: &Context, body: Option<FetchBody>, init: Option<ResponseInit>) -> Result<Response> {
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 {
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/globals/url/search_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
self.pairs.iter().filter(|(k, _)| k == &key).map(|(_, v)| v.clone()).collect()
}
Expand Down

0 comments on commit 88ac4f7

Please sign in to comment.