Skip to content

Commit 04e8b94

Browse files
refactor(wasm): remove extra component implementations
Signed-off-by: Brooks Townsend <[email protected]>
1 parent ff8a364 commit 04e8b94

File tree

8 files changed

+35
-755
lines changed

8 files changed

+35
-755
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,6 @@ if_wasm! {
372372
mod util;
373373

374374
pub use self::wasm::{Body, Client, ClientBuilder, Request, RequestBuilder, Response};
375-
#[cfg(feature = "multipart")]
375+
#[cfg(all(not(all(target_os = "wasi", target_env = "p2")), feature = "multipart"))]
376376
pub use self::wasm::multipart;
377377
}

src/wasm/component/body.rs

Lines changed: 1 addition & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
1-
#[cfg(feature = "multipart")]
2-
use super::multipart::Form;
3-
/// dox
41
use bytes::Bytes;
52
use std::{borrow::Cow, fmt};
63

7-
/// The body of a `Request`.
8-
///
9-
/// In most cases, this is not needed directly, as the
10-
/// [`RequestBuilder.body`][builder] method uses `Into<Body>`, which allows
11-
/// passing many things (like a string or vector of bytes).
12-
///
13-
/// [builder]: ./struct.RequestBuilder.html#method.body
4+
/// The body of a [`super::Request`].
145
pub struct Body {
156
inner: Inner,
167
}
178

189
enum Inner {
1910
Single(Single),
20-
/// MultipartForm holds a multipart/form-data body.
21-
#[cfg(feature = "multipart")]
22-
MultipartForm(Form),
2311
}
2412

2513
#[derive(Clone)]
@@ -52,46 +40,13 @@ impl Body {
5240
pub fn as_bytes(&self) -> Option<&[u8]> {
5341
match &self.inner {
5442
Inner::Single(single) => Some(single.as_bytes()),
55-
#[cfg(feature = "multipart")]
56-
Inner::MultipartForm(_) => None,
57-
}
58-
}
59-
60-
#[cfg(feature = "multipart")]
61-
pub(crate) fn as_single(&self) -> Option<&Single> {
62-
match &self.inner {
63-
Inner::Single(single) => Some(single),
64-
Inner::MultipartForm(_) => None,
65-
}
66-
}
67-
68-
#[inline]
69-
#[cfg(feature = "multipart")]
70-
pub(crate) fn from_form(f: Form) -> Body {
71-
Self {
72-
inner: Inner::MultipartForm(f),
73-
}
74-
}
75-
76-
/// into_part turns a regular body into the body of a multipart/form-data part.
77-
#[cfg(feature = "multipart")]
78-
pub(crate) fn into_part(self) -> Body {
79-
match self.inner {
80-
Inner::Single(single) => Self {
81-
inner: Inner::Single(single),
82-
},
83-
Inner::MultipartForm(form) => Self {
84-
inner: Inner::MultipartForm(form),
85-
},
8643
}
8744
}
8845

8946
#[allow(unused)]
9047
pub(crate) fn is_empty(&self) -> bool {
9148
match &self.inner {
9249
Inner::Single(single) => single.is_empty(),
93-
#[cfg(feature = "multipart")]
94-
Inner::MultipartForm(form) => form.is_empty(),
9550
}
9651
}
9752

@@ -100,8 +55,6 @@ impl Body {
10055
Inner::Single(single) => Some(Self {
10156
inner: Inner::Single(single.clone()),
10257
}),
103-
#[cfg(feature = "multipart")]
104-
Inner::MultipartForm(_) => None,
10558
}
10659
}
10760
}
@@ -156,128 +109,3 @@ impl fmt::Debug for Body {
156109
f.debug_struct("Body").finish()
157110
}
158111
}
159-
160-
#[cfg(test)]
161-
mod tests {
162-
// use crate::Body;
163-
// use js_sys::Uint8Array;
164-
// use wasm_bindgen::prelude::*;
165-
// use wasm_bindgen_test::*;
166-
167-
// wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
168-
169-
// #[wasm_bindgen]
170-
// extern "C" {
171-
// // Use `js_namespace` here to bind `console.log(..)` instead of just
172-
// // `log(..)`
173-
// #[wasm_bindgen(js_namespace = console)]
174-
// fn log(s: String);
175-
// }
176-
177-
// #[wasm_bindgen_test]
178-
// async fn test_body() {
179-
// let body = Body::from("TEST");
180-
// assert_eq!([84, 69, 83, 84], body.as_bytes().unwrap());
181-
// }
182-
183-
// #[wasm_bindgen_test]
184-
// async fn test_body_js_static_str() {
185-
// let body_value = "TEST";
186-
// let body = Body::from(body_value);
187-
188-
// let mut init = web_sys::RequestInit::new();
189-
// init.method("POST");
190-
// init.body(Some(
191-
// body.to_js_value()
192-
// .expect("could not convert body to JsValue")
193-
// .as_ref(),
194-
// ));
195-
196-
// let js_req = web_sys::Request::new_with_str_and_init("", &init)
197-
// .expect("could not create JS request");
198-
// let text_promise = js_req.text().expect("could not get text promise");
199-
// let text = crate::wasm::promise::<JsValue>(text_promise)
200-
// .await
201-
// .expect("could not get request body as text");
202-
203-
// assert_eq!(text.as_string().expect("text is not a string"), body_value);
204-
// }
205-
// #[wasm_bindgen_test]
206-
// async fn test_body_js_string() {
207-
// let body_value = "TEST".to_string();
208-
// let body = Body::from(body_value.clone());
209-
210-
// let mut init = web_sys::RequestInit::new();
211-
// init.method("POST");
212-
// init.body(Some(
213-
// body.to_js_value()
214-
// .expect("could not convert body to JsValue")
215-
// .as_ref(),
216-
// ));
217-
218-
// let js_req = web_sys::Request::new_with_str_and_init("", &init)
219-
// .expect("could not create JS request");
220-
// let text_promise = js_req.text().expect("could not get text promise");
221-
// let text = crate::wasm::promise::<JsValue>(text_promise)
222-
// .await
223-
// .expect("could not get request body as text");
224-
225-
// assert_eq!(text.as_string().expect("text is not a string"), body_value);
226-
// }
227-
228-
// #[wasm_bindgen_test]
229-
// async fn test_body_js_static_u8_slice() {
230-
// let body_value: &'static [u8] = b"\x00\x42";
231-
// let body = Body::from(body_value);
232-
233-
// let mut init = web_sys::RequestInit::new();
234-
// init.method("POST");
235-
// init.body(Some(
236-
// body.to_js_value()
237-
// .expect("could not convert body to JsValue")
238-
// .as_ref(),
239-
// ));
240-
241-
// let js_req = web_sys::Request::new_with_str_and_init("", &init)
242-
// .expect("could not create JS request");
243-
244-
// let array_buffer_promise = js_req
245-
// .array_buffer()
246-
// .expect("could not get array_buffer promise");
247-
// let array_buffer = crate::wasm::promise::<JsValue>(array_buffer_promise)
248-
// .await
249-
// .expect("could not get request body as array buffer");
250-
251-
// let v = Uint8Array::new(&array_buffer).to_vec();
252-
253-
// assert_eq!(v, body_value);
254-
// }
255-
256-
// #[wasm_bindgen_test]
257-
// async fn test_body_js_vec_u8() {
258-
// let body_value = vec![0u8, 42];
259-
// let body = Body::from(body_value.clone());
260-
261-
// let mut init = web_sys::RequestInit::new();
262-
// init.method("POST");
263-
// init.body(Some(
264-
// body.to_js_value()
265-
// .expect("could not convert body to JsValue")
266-
// .as_ref(),
267-
// ));
268-
269-
// let js_req = web_sys::Request::new_with_str_and_init("", &init)
270-
// .expect("could not create JS request");
271-
272-
// let array_buffer_promise = js_req
273-
// .array_buffer()
274-
// .expect("could not get array_buffer promise");
275-
// let array_buffer = crate::wasm::promise::<JsValue>(array_buffer_promise)
276-
// .await
277-
// .expect("could not get request body as array buffer");
278-
279-
// let v = Uint8Array::new(&array_buffer).to_vec();
280-
281-
// assert_eq!(v, body_value);
282-
// }
283-
}

src/wasm/component/client/future.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ use std::{
44
};
55

66
use futures_core::Future;
7-
use wasi::{
8-
self,
9-
http::{
10-
outgoing_handler::{FutureIncomingResponse, OutgoingRequest},
11-
types::{OutgoingBody, OutputStream},
12-
},
7+
use wasi::http::{
8+
outgoing_handler::{FutureIncomingResponse, OutgoingRequest},
9+
types::{OutgoingBody, OutputStream},
1310
};
1411

1512
use crate::{Body, Request, Response};
1613

14+
/// A [`Future`] implementation for a [`Response`] that uses the [`wasi::io::poll`]
15+
/// primitives to poll receipt of the HTTP response.
1716
#[derive(Debug)]
1817
pub struct ResponseFuture {
1918
request: Request,

src/wasm/component/client.rs renamed to src/wasm/component/client/mod.rs

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
11
#![allow(warnings)]
22

3-
use http::header::{CONTENT_LENGTH, USER_AGENT};
3+
use http::header::{Entry, CONTENT_LENGTH, USER_AGENT};
44
use http::{HeaderMap, HeaderValue, Method};
55
use std::any::Any;
66
use std::convert::TryInto;
77
use std::pin::Pin;
88
use std::task::{ready, Context, Poll};
99
use std::{fmt, future::Future, sync::Arc};
1010

11-
use self::future::ResponseFuture;
12-
13-
use super::{Request, RequestBuilder, Response};
14-
use crate::Body;
15-
use crate::IntoUrl;
16-
use wasi::http::outgoing_handler::{self, OutgoingRequest};
11+
use crate::wasm::component::{Request, RequestBuilder, Response};
12+
use crate::{Body, IntoUrl};
13+
use wasi::http::outgoing_handler::OutgoingRequest;
1714
use wasi::http::types::{FutureIncomingResponse, OutgoingBody, OutputStream, Pollable};
1815

1916
mod future;
17+
use future::ResponseFuture;
2018

21-
/// dox
22-
#[derive(Clone)]
19+
/// A client for making HTTP requests.
20+
#[derive(Default, Debug, Clone)]
2321
pub struct Client {
2422
config: Arc<Config>,
2523
}
2624

27-
/// dox
25+
/// A builder to configure a [`Client`].
26+
#[derive(Default, Debug)]
2827
pub struct ClientBuilder {
2928
config: Config,
3029
}
3130

3231
impl Client {
33-
/// Constructs a new `Client`.
32+
/// Constructs a new [`Client`].
3433
pub fn new() -> Self {
3534
Client::builder().build().expect("Client::new()")
3635
}
3736

38-
/// dox
37+
/// Constructs a new [`ClientBuilder`].
3938
pub fn builder() -> ClientBuilder {
4039
ClientBuilder::new()
4140
}
@@ -123,12 +122,10 @@ impl Client {
123122
self.execute_request(request)
124123
}
125124

126-
// merge request headers with Client default_headers, prior to external http fetch
127-
fn merge_headers(&self, req: &mut Request) {
128-
use http::header::Entry;
125+
/// Merge [`Request`] headers with default headers set in [`Config`]
126+
fn merge_default_headers(&self, req: &mut Request) {
129127
let headers: &mut HeaderMap = req.headers_mut();
130-
// insert default headers in the request headers
131-
// without overwriting already appended headers.
128+
// Insert without overwriting existing headers
132129
for (key, value) in self.config.headers.iter() {
133130
if let Entry::Vacant(entry) = headers.entry(key) {
134131
entry.insert(value.clone());
@@ -137,37 +134,14 @@ impl Client {
137134
}
138135

139136
pub(super) fn execute_request(&self, mut req: Request) -> crate::Result<ResponseFuture> {
140-
self.merge_headers(&mut req);
137+
self.merge_default_headers(&mut req);
141138
fetch(req)
142139
}
143140
}
144141

145-
impl Default for Client {
146-
fn default() -> Self {
147-
Self::new()
148-
}
149-
}
150-
151-
impl fmt::Debug for Client {
152-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
153-
let mut builder = f.debug_struct("Client");
154-
self.config.fmt_fields(&mut builder);
155-
builder.finish()
156-
}
157-
}
158-
159-
impl fmt::Debug for ClientBuilder {
160-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
161-
let mut builder = f.debug_struct("ClientBuilder");
162-
self.config.fmt_fields(&mut builder);
163-
builder.finish()
164-
}
165-
}
166-
167142
fn fetch(req: Request) -> crate::Result<ResponseFuture> {
168143
let headers = wasi::http::types::Fields::new();
169144
for (name, value) in req.headers() {
170-
// TODO: see if we can avoid the extra allocation
171145
headers
172146
.append(&name.to_string(), &value.as_bytes().to_vec())
173147
.map_err(crate::error::builder)?;
@@ -232,8 +206,6 @@ fn fetch(req: Request) -> crate::Result<ResponseFuture> {
232206
ResponseFuture::new(req, outgoing_request)
233207
}
234208

235-
// ===== impl ClientBuilder =====
236-
237209
impl ClientBuilder {
238210
/// Return a new `ClientBuilder`.
239211
pub fn new() -> Self {
@@ -280,27 +252,12 @@ impl ClientBuilder {
280252
}
281253
}
282254

283-
impl Default for ClientBuilder {
284-
fn default() -> Self {
285-
Self::new()
286-
}
287-
}
288-
289-
#[derive(Debug)]
255+
#[derive(Default, Debug)]
290256
struct Config {
291257
headers: HeaderMap,
292258
error: Option<crate::Error>,
293259
}
294260

295-
impl Default for Config {
296-
fn default() -> Config {
297-
Config {
298-
headers: HeaderMap::new(),
299-
error: None,
300-
}
301-
}
302-
}
303-
304261
impl Config {
305262
fn fmt_fields(&self, f: &mut fmt::DebugStruct<'_, '_>) {
306263
f.field("default_headers", &self.headers);

src/wasm/component/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
mod body;
22
mod client;
3-
#[cfg(feature = "multipart")]
4-
pub mod multipart;
53
mod request;
64
mod response;
75

0 commit comments

Comments
 (0)