From 867a6d303285cdffd060e6bb4b0e97de73925cfe Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Wed, 17 May 2023 01:20:32 +0200 Subject: refactor(node): reimplement http client (#19122) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit reimplements most of "node:http" client APIs using "ext/fetch". There is some duplicated code and two removed Node compat tests that will be fixed in follow up PRs. --------- Co-authored-by: Bartek IwaƄczuk --- ext/fetch/lib.rs | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'ext/fetch/lib.rs') diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index 51688a6fc..e41d85ea4 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -66,7 +66,7 @@ pub use reqwest; pub use fs_fetch_handler::FsFetchHandler; -use crate::byte_stream::MpscByteStream; +pub use crate::byte_stream::MpscByteStream; #[derive(Clone)] pub struct Options { @@ -186,9 +186,9 @@ pub fn get_declaration() -> PathBuf { #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct FetchReturn { - request_rid: ResourceId, - request_body_rid: Option, - cancel_handle_rid: Option, + pub request_rid: ResourceId, + pub request_body_rid: Option, + pub cancel_handle_rid: Option, } pub fn get_or_create_client_from_state( @@ -302,7 +302,7 @@ where } Some(data) => { // If a body is passed, we use it, and don't return a body for streaming. - request = request.body(Vec::from(&*data)); + request = request.body(data.to_vec()); None } } @@ -400,12 +400,12 @@ where #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct FetchResponse { - status: u16, - status_text: String, - headers: Vec<(ByteString, ByteString)>, - url: String, - response_rid: ResourceId, - content_length: Option, + pub status: u16, + pub status_text: String, + pub headers: Vec<(ByteString, ByteString)>, + pub url: String, + pub response_rid: ResourceId, + pub content_length: Option, } #[op] @@ -462,8 +462,8 @@ pub async fn op_fetch_send( type CancelableResponseResult = Result, Canceled>; -struct FetchRequestResource( - Pin>>, +pub struct FetchRequestResource( + pub Pin>>, ); impl Resource for FetchRequestResource { @@ -472,7 +472,7 @@ impl Resource for FetchRequestResource { } } -struct FetchCancelHandle(Rc); +pub struct FetchCancelHandle(pub Rc); impl Resource for FetchCancelHandle { fn name(&self) -> Cow { @@ -485,8 +485,8 @@ impl Resource for FetchCancelHandle { } pub struct FetchRequestBodyResource { - body: AsyncRefCell>>, - cancel: CancelHandle, + pub body: AsyncRefCell>>, + pub cancel: CancelHandle, } impl Resource for FetchRequestBodyResource { @@ -537,10 +537,10 @@ impl Resource for FetchRequestBodyResource { type BytesStream = Pin> + Unpin>>; -struct FetchResponseBodyResource { - reader: AsyncRefCell>, - cancel: CancelHandle, - size: Option, +pub struct FetchResponseBodyResource { + pub reader: AsyncRefCell>, + pub cancel: CancelHandle, + pub size: Option, } impl Resource for FetchResponseBodyResource { @@ -590,8 +590,8 @@ impl Resource for FetchResponseBodyResource { } } -struct HttpClientResource { - client: Client, +pub struct HttpClientResource { + pub client: Client, } impl Resource for HttpClientResource { -- cgit v1.2.3