Skip to content

Commit

Permalink
fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jan 31, 2025
1 parent b97d40c commit 580eb17
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
2 changes: 1 addition & 1 deletion runtime/fastly/builtins/fetch/fetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ bool apply_body_transform(JSContext *cx, JS::HandleValue response, host_api::Htt

JS::Value array_buffer_ret;
JS::CallArgs args = JS::CallArgsFromVp(0, &array_buffer_ret);
if (!RequestOrResponse::bodyAll<RequestOrResponse::BodyReadResult::ArrayBuffer, true>(
if (!RequestOrResponse::bodyAll<RequestOrResponse::BodyReadResult::ArrayBuffer, false>(
cx, args, response_obj)) {
return false;
}
Expand Down
65 changes: 33 additions & 32 deletions runtime/fastly/builtins/fetch/request-response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1384,32 +1384,33 @@ bool RequestOrResponse::consume_content_stream_for_bodyAll(JSContext *cx, JS::Ha
return JS::AddPromiseReactions(cx, promise, then_handler, catch_handler);
}

bool async_process_body_handle_for_bodyAll(JSContext *cx, uint32_t handle, JS::HandleObject self,
JS::HandleValue body_parser) {
auto body = RequestOrResponse::body_handle(self);
auto *parse_body = reinterpret_cast<RequestOrResponse::ParseBodyCB *>(body_parser.toPrivate());
auto [buf, bytes_read, state] = read_from_handle_all<true>(cx, body);
if (state == StreamState::Error) {

JS::RootedObject result_promise(cx);
result_promise =
&JS::GetReservedSlot(self, static_cast<uint32_t>(RequestOrResponse::Slots::BodyAllPromise))
.toObject();
JS::SetReservedSlot(self, static_cast<uint32_t>(RequestOrResponse::Slots::BodyAllPromise),
JS::UndefinedValue());
return RejectPromiseWithPendingError(cx, result_promise);
}

if (state == StreamState::Complete) {

return parse_body(cx, self, std::move(buf), bytes_read);
}

// still have to wait for the stream to complete, queue an async task
ENGINE->queue_async_task(new FastlyAsyncTask(body.async_handle(), self, JS::UndefinedHandleValue,
async_process_body_handle_for_bodyAll));
return true;
}
// bool async_process_body_handle_for_bodyAll(JSContext *cx, uint32_t handle, JS::HandleObject self,
// JS::HandleValue body_parser) {
// auto body = RequestOrResponse::body_handle(self);
// auto *parse_body = reinterpret_cast<RequestOrResponse::ParseBodyCB *>(body_parser.toPrivate());
// auto [buf, bytes_read, state] = read_from_handle_all<true>(cx, body);
// if (state == StreamState::Error) {

// JS::RootedObject result_promise(cx);
// result_promise =
// &JS::GetReservedSlot(self,
// static_cast<uint32_t>(RequestOrResponse::Slots::BodyAllPromise))
// .toObject();
// JS::SetReservedSlot(self, static_cast<uint32_t>(RequestOrResponse::Slots::BodyAllPromise),
// JS::UndefinedValue());
// return RejectPromiseWithPendingError(cx, result_promise);
// }

// if (state == StreamState::Complete) {
// return parse_body(cx, self, std::move(buf), bytes_read);
// }

// // still have to wait for the stream to complete, queue an async task
// ENGINE->queue_async_task(new FastlyAsyncTask(body.async_handle(), self,
// JS::UndefinedHandleValue,
// async_process_body_handle_for_bodyAll));
// return true;
// }

template <bool async>
bool RequestOrResponse::consume_body_handle_for_bodyAll(JSContext *cx, JS::HandleObject self,
Expand All @@ -1431,16 +1432,16 @@ bool RequestOrResponse::consume_body_handle_for_bodyAll(JSContext *cx, JS::Handl
return parse_body(cx, self, std::move(buf), bytes_read);
}

// TODO: the async path isn't working because we don't yet store a chunk buffer along with
// the body parser / on the Response slot. This would be a nice addition in future.

// still have to wait for the stream to complete, queue an async task
ENGINE->queue_async_task(new FastlyAsyncTask(body.async_handle(), self, JS::UndefinedHandleValue,
async_process_body_handle_for_bodyAll));
// ENGINE->queue_async_task(new FastlyAsyncTask(body.async_handle(), self,
// JS::UndefinedHandleValue,
// async_process_body_handle_for_bodyAll));
return true;
}

// Async case only used from fetch.cpp currently
template bool RequestOrResponse::bodyAll<RequestOrResponse::BodyReadResult::ArrayBuffer, true>(
JSContext *, JS::CallArgs, JS::HandleObject);

template <RequestOrResponse::BodyReadResult result_type, bool async>
bool RequestOrResponse::bodyAll(JSContext *cx, JS::CallArgs args, JS::HandleObject self) {
// TODO: mark body as consumed when operating on stream, too.
Expand Down

0 comments on commit 580eb17

Please sign in to comment.