Bodies.h 2.8 KB

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