@@ -43,10 +43,7 @@ impl HttpClient for WasmClient {
43
43
response. set_body ( Body :: from ( body) ) ;
44
44
for ( name, value) in res. headers ( ) {
45
45
let name: http_types:: headers:: HeaderName = name. parse ( ) . unwrap ( ) ;
46
- response. insert_header (
47
- & name,
48
- value. parse :: < http_types:: headers:: HeaderValue > ( ) . unwrap ( ) ,
49
- ) ;
46
+ response. insert_header ( & name, value) ;
50
47
}
51
48
52
49
Ok ( response)
@@ -76,7 +73,6 @@ impl Future for InnerFuture {
76
73
}
77
74
78
75
mod fetch {
79
- use futures_util:: io:: AsyncReadExt ;
80
76
use js_sys:: { Array , ArrayBuffer , Reflect , Uint8Array } ;
81
77
use wasm_bindgen:: JsCast ;
82
78
use wasm_bindgen_futures:: JsFuture ;
@@ -91,35 +87,34 @@ mod fetch {
91
87
/// An HTTP Fetch Request.
92
88
pub ( crate ) struct Request {
93
89
request : web_sys:: Request ,
94
- _body_buf : Pin < Vec < u8 > > ,
90
+ /// This field stores the body of the request to ensure it stays allocated as long as the request needs it.
91
+ #[ allow( dead_code) ]
92
+ body_buf : Pin < Vec < u8 > > ,
95
93
}
96
94
97
95
impl Request {
98
96
/// Create a new instance.
99
97
pub ( crate ) async fn new ( mut req : super :: Request ) -> Result < Self , io:: Error > {
100
- //create a fetch request initaliser
98
+ // create a fetch request initaliser
101
99
let mut init = RequestInit :: new ( ) ;
102
100
103
- //set the fetch method
101
+ // set the fetch method
104
102
init. method ( req. method ( ) . as_ref ( ) ) ;
105
103
106
104
let uri = req. url ( ) . to_string ( ) ;
107
- let mut body = req. take_body ( ) ;
105
+ let body = req. take_body ( ) ;
108
106
109
- //convert the body into a uint8 array
107
+ // convert the body into a uint8 array
110
108
// needs to be pinned and retained inside the Request because the Uint8Array passed to
111
109
// js is just a portal into WASM linear memory, and if the underlying data is moved the
112
110
// js ref will become silently invalid
113
- let mut body_buf = Vec :: with_capacity ( 1024 ) ;
114
- body. read_to_end ( & mut body_buf) . await . map_err ( |_| {
111
+ let body_buf = body. into_bytes ( ) . await . map_err ( |_| {
115
112
io:: Error :: new ( io:: ErrorKind :: Other , "could not read body into a buffer" )
116
113
} ) ?;
117
114
let body_pinned = Pin :: new ( body_buf) ;
118
115
if body_pinned. len ( ) > 0 {
119
- unsafe {
120
- let uint_8_array = js_sys:: Uint8Array :: view ( & body_pinned) ;
121
- init. body ( Some ( & uint_8_array) ) ;
122
- }
116
+ let uint_8_array = unsafe { js_sys:: Uint8Array :: view ( & body_pinned) } ;
117
+ init. body ( Some ( & uint_8_array) ) ;
123
118
}
124
119
125
120
let request = web_sys:: Request :: new_with_str_and_init ( & uri, & init) . map_err ( |e| {
@@ -129,7 +124,7 @@ mod fetch {
129
124
)
130
125
} ) ?;
131
126
132
- //add any fetch headers
127
+ // add any fetch headers
133
128
let headers: & mut super :: Headers = req. as_mut ( ) ;
134
129
for ( name, value) in headers. iter ( ) {
135
130
let name = name. as_str ( ) ;
@@ -145,7 +140,7 @@ mod fetch {
145
140
146
141
Ok ( Self {
147
142
request,
148
- _body_buf : body_pinned,
143
+ body_buf : body_pinned,
149
144
} )
150
145
}
151
146
0 commit comments