Bodies.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/Forward.h>
  9. #include <AK/NonnullRefPtr.h>
  10. #include <AK/Optional.h>
  11. #include <AK/Variant.h>
  12. #include <LibJS/Heap/GCPtr.h>
  13. #include <LibJS/Heap/Handle.h>
  14. #include <LibWeb/Fetch/Infrastructure/Task.h>
  15. #include <LibWeb/FileAPI/Blob.h>
  16. #include <LibWeb/Streams/ReadableStream.h>
  17. #include <LibWeb/WebIDL/Promise.h>
  18. namespace Web::Fetch::Infrastructure {
  19. // https://fetch.spec.whatwg.org/#concept-body
  20. class Body final : public JS::Cell {
  21. JS_CELL(Body, JS::Cell);
  22. JS_DECLARE_ALLOCATOR(Body);
  23. public:
  24. using SourceType = Variant<Empty, ByteBuffer, JS::Handle<FileAPI::Blob>>;
  25. // processBody must be an algorithm accepting a byte sequence.
  26. using ProcessBodyCallback = JS::NonnullGCPtr<JS::HeapFunction<void(ByteBuffer)>>;
  27. // processBodyError must be an algorithm optionally accepting an exception.
  28. using ProcessBodyErrorCallback = JS::NonnullGCPtr<JS::HeapFunction<void(JS::Value)>>;
  29. // processBodyChunk must be an algorithm accepting a byte sequence.
  30. using ProcessBodyChunkCallback = JS::NonnullGCPtr<JS::HeapFunction<void(ByteBuffer)>>;
  31. // processEndOfBody must be an algorithm accepting no arguments
  32. using ProcessEndOfBodyCallback = JS::NonnullGCPtr<JS::HeapFunction<void()>>;
  33. [[nodiscard]] static JS::NonnullGCPtr<Body> create(JS::VM&, JS::NonnullGCPtr<Streams::ReadableStream>);
  34. [[nodiscard]] static JS::NonnullGCPtr<Body> create(JS::VM&, JS::NonnullGCPtr<Streams::ReadableStream>, SourceType, Optional<u64>);
  35. [[nodiscard]] JS::NonnullGCPtr<Streams::ReadableStream> stream() const { return *m_stream; }
  36. void set_stream(JS::NonnullGCPtr<Streams::ReadableStream> value) { m_stream = value; }
  37. [[nodiscard]] SourceType const& source() const { return m_source; }
  38. [[nodiscard]] Optional<u64> const& length() const { return m_length; }
  39. [[nodiscard]] JS::NonnullGCPtr<Body> clone(JS::Realm&);
  40. void fully_read(JS::Realm&, ProcessBodyCallback process_body, ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const;
  41. void incrementally_read(ProcessBodyChunkCallback process_body_chunk, ProcessEndOfBodyCallback process_end_of_body, ProcessBodyErrorCallback process_body_error, TaskDestination task_destination);
  42. void incrementally_read_loop(Streams::ReadableStreamDefaultReader& reader, JS::NonnullGCPtr<JS::Object> task_destination, ProcessBodyChunkCallback process_body_chunk, ProcessEndOfBodyCallback process_end_of_body, ProcessBodyErrorCallback process_body_error);
  43. virtual void visit_edges(JS::Cell::Visitor&) override;
  44. private:
  45. explicit Body(JS::NonnullGCPtr<Streams::ReadableStream>);
  46. Body(JS::NonnullGCPtr<Streams::ReadableStream>, SourceType, Optional<u64>);
  47. // https://fetch.spec.whatwg.org/#concept-body-stream
  48. // A stream (a ReadableStream object).
  49. JS::NonnullGCPtr<Streams::ReadableStream> m_stream;
  50. // https://fetch.spec.whatwg.org/#concept-body-source
  51. // A source (null, a byte sequence, a Blob object, or a FormData object), initially null.
  52. SourceType m_source;
  53. // https://fetch.spec.whatwg.org/#concept-body-total-bytes
  54. // A length (null or an integer), initially null.
  55. Optional<u64> m_length;
  56. };
  57. // https://fetch.spec.whatwg.org/#body-with-type
  58. // A body with type is a tuple that consists of a body (a body) and a type (a header value or null).
  59. struct BodyWithType {
  60. JS::NonnullGCPtr<Body> body;
  61. Optional<ByteBuffer> type;
  62. };
  63. WebIDL::ExceptionOr<JS::NonnullGCPtr<Body>> byte_sequence_as_body(JS::Realm&, ReadonlyBytes);
  64. }