Bodies.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. [[nodiscard]] static JS::NonnullGCPtr<Body> create(JS::VM&, JS::NonnullGCPtr<Streams::ReadableStream>);
  30. [[nodiscard]] static JS::NonnullGCPtr<Body> create(JS::VM&, JS::NonnullGCPtr<Streams::ReadableStream>, SourceType, Optional<u64>);
  31. [[nodiscard]] JS::NonnullGCPtr<Streams::ReadableStream> stream() const { return *m_stream; }
  32. void set_stream(JS::NonnullGCPtr<Streams::ReadableStream> value) { m_stream = value; }
  33. [[nodiscard]] SourceType const& source() const { return m_source; }
  34. [[nodiscard]] Optional<u64> const& length() const { return m_length; }
  35. [[nodiscard]] JS::NonnullGCPtr<Body> clone(JS::Realm&);
  36. void fully_read(JS::Realm&, ProcessBodyCallback process_body, ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const;
  37. virtual void visit_edges(JS::Cell::Visitor&) override;
  38. private:
  39. explicit Body(JS::NonnullGCPtr<Streams::ReadableStream>);
  40. Body(JS::NonnullGCPtr<Streams::ReadableStream>, SourceType, Optional<u64>);
  41. // https://fetch.spec.whatwg.org/#concept-body-stream
  42. // A stream (a ReadableStream object).
  43. JS::NonnullGCPtr<Streams::ReadableStream> m_stream;
  44. // https://fetch.spec.whatwg.org/#concept-body-source
  45. // A source (null, a byte sequence, a Blob object, or a FormData object), initially null.
  46. SourceType m_source;
  47. // https://fetch.spec.whatwg.org/#concept-body-total-bytes
  48. // A length (null or an integer), initially null.
  49. Optional<u64> m_length;
  50. };
  51. // https://fetch.spec.whatwg.org/#body-with-type
  52. // A body with type is a tuple that consists of a body (a body) and a type (a header value or null).
  53. struct BodyWithType {
  54. JS::NonnullGCPtr<Body> body;
  55. Optional<ByteBuffer> type;
  56. };
  57. WebIDL::ExceptionOr<JS::NonnullGCPtr<Body>> byte_sequence_as_body(JS::Realm&, ReadonlyBytes);
  58. }