mirror of
https://github.com/denoland/deno.git
synced 2025-08-03 18:38:33 +00:00
refactor(node): reimplement http client (#19122)
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 <biwanczuk@gmail.com>
This commit is contained in:
parent
a22388bbd1
commit
867a6d3032
14 changed files with 1702 additions and 1163 deletions
|
@ -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<ResourceId>,
|
||||
cancel_handle_rid: Option<ResourceId>,
|
||||
pub request_rid: ResourceId,
|
||||
pub request_body_rid: Option<ResourceId>,
|
||||
pub cancel_handle_rid: Option<ResourceId>,
|
||||
}
|
||||
|
||||
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<u64>,
|
||||
pub status: u16,
|
||||
pub status_text: String,
|
||||
pub headers: Vec<(ByteString, ByteString)>,
|
||||
pub url: String,
|
||||
pub response_rid: ResourceId,
|
||||
pub content_length: Option<u64>,
|
||||
}
|
||||
|
||||
#[op]
|
||||
|
@ -462,8 +462,8 @@ pub async fn op_fetch_send(
|
|||
|
||||
type CancelableResponseResult = Result<Result<Response, AnyError>, Canceled>;
|
||||
|
||||
struct FetchRequestResource(
|
||||
Pin<Box<dyn Future<Output = CancelableResponseResult>>>,
|
||||
pub struct FetchRequestResource(
|
||||
pub Pin<Box<dyn Future<Output = CancelableResponseResult>>>,
|
||||
);
|
||||
|
||||
impl Resource for FetchRequestResource {
|
||||
|
@ -472,7 +472,7 @@ impl Resource for FetchRequestResource {
|
|||
}
|
||||
}
|
||||
|
||||
struct FetchCancelHandle(Rc<CancelHandle>);
|
||||
pub struct FetchCancelHandle(pub Rc<CancelHandle>);
|
||||
|
||||
impl Resource for FetchCancelHandle {
|
||||
fn name(&self) -> Cow<str> {
|
||||
|
@ -485,8 +485,8 @@ impl Resource for FetchCancelHandle {
|
|||
}
|
||||
|
||||
pub struct FetchRequestBodyResource {
|
||||
body: AsyncRefCell<mpsc::Sender<Option<bytes::Bytes>>>,
|
||||
cancel: CancelHandle,
|
||||
pub body: AsyncRefCell<mpsc::Sender<Option<bytes::Bytes>>>,
|
||||
pub cancel: CancelHandle,
|
||||
}
|
||||
|
||||
impl Resource for FetchRequestBodyResource {
|
||||
|
@ -537,10 +537,10 @@ impl Resource for FetchRequestBodyResource {
|
|||
type BytesStream =
|
||||
Pin<Box<dyn Stream<Item = Result<bytes::Bytes, std::io::Error>> + Unpin>>;
|
||||
|
||||
struct FetchResponseBodyResource {
|
||||
reader: AsyncRefCell<Peekable<BytesStream>>,
|
||||
cancel: CancelHandle,
|
||||
size: Option<u64>,
|
||||
pub struct FetchResponseBodyResource {
|
||||
pub reader: AsyncRefCell<Peekable<BytesStream>>,
|
||||
pub cancel: CancelHandle,
|
||||
pub size: Option<u64>,
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue