/* * 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 { GC_CELL(Body, JS::Cell); GC_DECLARE_ALLOCATOR(Body); public: using SourceType = Variant>; // processBody must be an algorithm accepting a byte sequence. using ProcessBodyCallback = GC::Ref>; // processBodyError must be an algorithm optionally accepting an exception. using ProcessBodyErrorCallback = GC::Ref>; // processBodyChunk must be an algorithm accepting a byte sequence. using ProcessBodyChunkCallback = GC::Ref>; // processEndOfBody must be an algorithm accepting no arguments using ProcessEndOfBodyCallback = GC::Ref>; [[nodiscard]] static GC::Ref create(JS::VM&, GC::Ref); [[nodiscard]] static GC::Ref create(JS::VM&, GC::Ref, SourceType, Optional); [[nodiscard]] GC::Ref stream() const { return *m_stream; } void set_stream(GC::Ref value) { m_stream = value; } [[nodiscard]] SourceType const& source() const { return m_source; } [[nodiscard]] Optional const& length() const { return m_length; } [[nodiscard]] GC::Ref clone(JS::Realm&); void fully_read(JS::Realm&, ProcessBodyCallback process_body, ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const; void incrementally_read(ProcessBodyChunkCallback process_body_chunk, ProcessEndOfBodyCallback process_end_of_body, ProcessBodyErrorCallback process_body_error, TaskDestination task_destination); void incrementally_read_loop(Streams::ReadableStreamDefaultReader& reader, GC::Ref task_destination, ProcessBodyChunkCallback process_body_chunk, ProcessEndOfBodyCallback process_end_of_body, ProcessBodyErrorCallback process_body_error); virtual void visit_edges(JS::Cell::Visitor&) override; private: explicit Body(GC::Ref); Body(GC::Ref, SourceType, Optional); // https://fetch.spec.whatwg.org/#concept-body-stream // A stream (a ReadableStream object). GC::Ref 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 { GC::Ref body; Optional type; }; GC::Ref byte_sequence_as_body(JS::Realm&, ReadonlyBytes); }