@@ -141,36 +141,36 @@ bool process_body_read(JSContext *cx, host_api::HttpBody::Handle handle, JS::Han
141141 return true ;
142142}
143143
144+ enum StreamState { Complete, Wait, Error };
145+
144146struct ReadResult {
145147 JS::UniqueChars buffer;
146148 size_t length;
147- bool end_of_stream ;
149+ StreamState state ;
148150};
149151
150152// Returns a UniqueChars and the length of that string. The UniqueChars value is not
151153// null-terminated.
152- template < bool read_async> ReadResult read_from_handle_all (JSContext *cx, host_api::HttpBody body) {
154+ ReadResult read_from_handle_all (JSContext *cx, host_api::HttpBody body) {
153155 std::vector<host_api::HostString> chunks;
154156 size_t bytes_read = 0 ;
155157 bool end_of_stream = true ;
156158 while (true ) {
157159 DEBUG_LOG (" read from handle all loop" )
158- if (read_async) {
159- auto ready_res = body.is_ready ();
160- if (auto *err = ready_res.to_err ()) {
161- HANDLE_ERROR (cx, *err);
162- return {nullptr , 0 , true };
163- }
164- if (!ready_res.unwrap ()) {
165- DEBUG_LOG (" async break" )
166- end_of_stream = false ;
167- break ;
168- }
160+ auto ready_res = body.is_ready ();
161+ if (auto *err = ready_res.to_err ()) {
162+ HANDLE_ERROR (cx, *err);
163+ return {nullptr , 0 , StreamState::Error};
164+ }
165+ if (!ready_res.unwrap ()) {
166+ DEBUG_LOG (" async break" )
167+ end_of_stream = false ;
168+ break ;
169169 }
170170 auto res = body.read (HANDLE_READ_CHUNK_SIZE);
171171 if (auto *err = res.to_err ()) {
172172 HANDLE_ERROR (cx, *err);
173- return {nullptr , 0 , true };
173+ return {nullptr , 0 , StreamState::Error };
174174 }
175175
176176 auto &chunk = res.unwrap ();
@@ -184,7 +184,7 @@ template <bool read_async> ReadResult read_from_handle_all(JSContext *cx, host_a
184184
185185 JS::UniqueChars buf;
186186 if (chunks.size () == 0 ) {
187- return {nullptr , 0 , end_of_stream};
187+ return {nullptr , 0 , end_of_stream ? StreamState::Complete : StreamState::Wait };
188188 } else if (chunks.size () == 1 ) {
189189 // If there was only one chunk read, reuse that allocation.
190190 auto &chunk = chunks.back ();
@@ -194,7 +194,7 @@ template <bool read_async> ReadResult read_from_handle_all(JSContext *cx, host_a
194194 buf.reset (static_cast <char *>(JS_string_malloc (cx, bytes_read)));
195195 if (!buf) {
196196 JS_ReportOutOfMemory (cx);
197- return {nullptr , 0 };
197+ return {nullptr , 0 , StreamState::Error };
198198 }
199199
200200 char *end = buf.get ();
@@ -203,7 +203,7 @@ template <bool read_async> ReadResult read_from_handle_all(JSContext *cx, host_a
203203 }
204204 }
205205
206- return {std::move (buf), bytes_read, end_of_stream};
206+ return {std::move (buf), bytes_read, end_of_stream ? StreamState::Complete : StreamState::Wait };
207207}
208208
209209} // namespace
@@ -1383,9 +1383,9 @@ bool async_process_body_handle_for_bodyAll(JSContext *cx, uint32_t handle, JS::H
13831383 auto body = RequestOrResponse::body_handle (self);
13841384 auto *parse_body = reinterpret_cast <RequestOrResponse::ParseBodyCB *>(body_parser.toPrivate ());
13851385 DEBUG_LOG (" Consume read from handle all task" )
1386- auto [buf, bytes_read, end_of_stream ] = read_from_handle_all< true > (cx, body);
1386+ auto [buf, bytes_read, state ] = read_from_handle_all (cx, body);
13871387 DEBUG_LOG (" Got handle all task" )
1388- if (!buf && end_of_stream ) {
1388+ if (state == StreamState::Error ) {
13891389 DEBUG_LOG (" stream error" )
13901390 JS::RootedObject result_promise (cx);
13911391 result_promise =
@@ -1396,7 +1396,7 @@ bool async_process_body_handle_for_bodyAll(JSContext *cx, uint32_t handle, JS::H
13961396 return RejectPromiseWithPendingError (cx, result_promise);
13971397 }
13981398
1399- if (end_of_stream ) {
1399+ if (state == StreamState::Complete ) {
14001400 DEBUG_LOG (" stream complete" )
14011401 return parse_body (cx, self, std::move (buf), bytes_read);
14021402 }
@@ -1414,9 +1414,9 @@ bool RequestOrResponse::consume_body_handle_for_bodyAll(JSContext *cx, JS::Handl
14141414 auto body = body_handle (self);
14151415 auto *parse_body = reinterpret_cast <ParseBodyCB *>(body_parser.toPrivate ());
14161416 DEBUG_LOG (" Consume read from handle all" )
1417- auto [buf, bytes_read, end_of_stream ] = read_from_handle_all< true > (cx, body);
1417+ auto [buf, bytes_read, state ] = read_from_handle_all (cx, body);
14181418 DEBUG_LOG (" Got handle all" )
1419- if (!buf && end_of_stream ) {
1419+ if (state == StreamState::Error ) {
14201420 DEBUG_LOG (" stream error" )
14211421 JS::RootedObject result_promise (cx);
14221422 result_promise =
@@ -1425,7 +1425,7 @@ bool RequestOrResponse::consume_body_handle_for_bodyAll(JSContext *cx, JS::Handl
14251425 return RejectPromiseWithPendingError (cx, result_promise);
14261426 }
14271427
1428- if (end_of_stream ) {
1428+ if (state == StreamState::Complete ) {
14291429 DEBUG_LOG (" stream complete" )
14301430 return parse_body (cx, self, std::move (buf), bytes_read);
14311431 }
0 commit comments