LibWeb: Implement FetchController::abort()

This commit is contained in:
Kenneth Myhra 2024-11-22 12:30:16 +01:00 committed by Andreas Kling
parent 4c5019f89c
commit 968c38e54f
Notes: github-actions[bot] 2024-11-24 10:12:37 +00:00
4 changed files with 34 additions and 4 deletions

View file

@ -85,9 +85,15 @@ void FetchController::abort(JS::Realm& realm, Optional<JS::Value> error)
if (!error.has_value())
error = fallback_error;
// FIXME: 4. Let serializedError be StructuredSerialize(error). If that threw an exception, catch it, and let serializedError be StructuredSerialize(fallbackError).
// FIXME: 5. Set controllers serialized abort reason to serializedError.
(void)error;
// 4. Let serializedError be StructuredSerialize(error). If that threw an exception, catch it, and let serializedError be StructuredSerialize(fallbackError).
// 5. Set controllers serialized abort reason to serializedError
auto structured_serialize = [](JS::VM& vm, JS::Value error, JS::Value fallback_error) {
auto serialized_value_or_error = HTML::structured_serialize(vm, error);
return serialized_value_or_error.is_error()
? HTML::structured_serialize(vm, fallback_error).value()
: serialized_value_or_error.value();
};
m_serialized_abort_reason = structured_serialize(realm.vm(), error.value(), fallback_error);
}
// FIXME: https://fetch.spec.whatwg.org/#deserialize-a-serialized-abort-reason

View file

@ -16,6 +16,7 @@
#include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/EventLoop/Task.h>
#include <LibWeb/HTML/StructuredSerialize.h>
namespace Web::Fetch::Infrastructure {
@ -74,8 +75,9 @@ private:
GC::Ptr<GC::Function<void(JS::Object const&)>> m_report_timing_steps;
// https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps
// FIXME: serialized abort reason (default null)
// serialized abort reason (default null)
// Null or a Record (result of StructuredSerialize).
HTML::SerializationRecord m_serialized_abort_reason;
// https://fetch.spec.whatwg.org/#fetch-controller-next-manual-redirect-steps
// next manual redirect steps (default null)

View file

@ -0,0 +1,2 @@
AbortError: Aborted without reason
Abort with a reason

View file

@ -0,0 +1,20 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
test(() => {
const runAbortController = ((reason) => {
let controller = new AbortController();
let signal = controller.signal;
fetch('data:text/plain,fetched from far', { signal })
.catch(err => {
println(err);
});
controller.abort(reason);
});
runAbortController(undefined);
runAbortController("Abort with a reason");
});
</script>