/* * Copyright (c) 2022-2023, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include namespace Web::Fetch::Infrastructure { // https://fetch.spec.whatwg.org/#concept-body class Body final : public JS::Cell { JS_CELL(Body, JS::Cell); JS_DECLARE_ALLOCATOR(Body); public: using SourceType = Variant>; // processBody must be an algorithm accepting a byte sequence. using ProcessBodyCallback = JS::SafeFunction; // processBodyError must be an algorithm optionally accepting an exception. using ProcessBodyErrorCallback = JS::SafeFunction)>; [[nodiscard]] static JS::NonnullGCPtr create(JS::VM&, JS::NonnullGCPtr); [[nodiscard]] static JS::NonnullGCPtr create(JS::VM&, JS::NonnullGCPtr, SourceType, Optional); [[nodiscard]] JS::NonnullGCPtr stream() const { return *m_stream; } [[nodiscard]] SourceType const& source() const { return m_source; } [[nodiscard]] Optional const& length() const { return m_length; } [[nodiscard]] JS::NonnullGCPtr clone(JS::Realm&); WebIDL::ExceptionOr fully_read(JS::Realm&, ProcessBodyCallback process_body, ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const; virtual void visit_edges(JS::Cell::Visitor&) override; private: explicit Body(JS::NonnullGCPtr); Body(JS::NonnullGCPtr, SourceType, Optional); // https://fetch.spec.whatwg.org/#concept-body-stream // A stream (a ReadableStream object). JS::NonnullGCPtr m_stream; // https://fetch.spec.whatwg.org/#concept-body-source // A source (null, a byte sequence, a Blob object, or a FormData object), initially null. SourceType m_source; // https://fetch.spec.whatwg.org/#concept-body-total-bytes // A length (null or an integer), initially null. Optional m_length; }; // https://fetch.spec.whatwg.org/#body-with-type // A body with type is a tuple that consists of a body (a body) and a type (a header value or null). struct BodyWithType { JS::NonnullGCPtr body; Optional type; }; WebIDL::ExceptionOr> byte_sequence_as_body(JS::Realm&, ReadonlyBytes); }