DecompressionStream.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCompress/Deflate.h>
  7. #include <LibCompress/Gzip.h>
  8. #include <LibCompress/Zlib.h>
  9. #include <LibJS/Runtime/ArrayBuffer.h>
  10. #include <LibJS/Runtime/Realm.h>
  11. #include <LibJS/Runtime/TypedArray.h>
  12. #include <LibWeb/Bindings/DecompressionStreamPrototype.h>
  13. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  14. #include <LibWeb/Bindings/Intrinsics.h>
  15. #include <LibWeb/Compression/DecompressionStream.h>
  16. #include <LibWeb/Streams/AbstractOperations.h>
  17. #include <LibWeb/Streams/TransformStream.h>
  18. #include <LibWeb/WebIDL/AbstractOperations.h>
  19. namespace Web::Compression {
  20. GC_DEFINE_ALLOCATOR(DecompressionStream);
  21. // https://compression.spec.whatwg.org/#dom-decompressionstream-decompressionstream
  22. WebIDL::ExceptionOr<GC::Ref<DecompressionStream>> DecompressionStream::construct_impl(JS::Realm& realm, Bindings::CompressionFormat format)
  23. {
  24. // 1. If format is unsupported in DecompressionStream, then throw a TypeError.
  25. // 2. Set this's format to format.
  26. auto input_stream = make<AllocatingMemoryStream>();
  27. auto decompressor = [&, input_stream = MaybeOwned<Stream> { *input_stream }]() mutable -> ErrorOr<Decompressor> {
  28. switch (format) {
  29. case Bindings::CompressionFormat::Deflate:
  30. return TRY(Compress::ZlibDecompressor::create(move(input_stream)));
  31. case Bindings::CompressionFormat::DeflateRaw:
  32. return TRY(Compress::DeflateDecompressor::construct(make<LittleEndianInputBitStream>(move(input_stream))));
  33. case Bindings::CompressionFormat::Gzip:
  34. return make<Compress::GzipDecompressor>(move(input_stream));
  35. }
  36. VERIFY_NOT_REACHED();
  37. }();
  38. if (decompressor.is_error())
  39. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Unable to create decompressor: {}", decompressor.error())) };
  40. // 5. Set this's transform to a new TransformStream.
  41. // NOTE: We do this first so that we may store it as nonnull in the GenericTransformStream.
  42. auto stream = realm.create<DecompressionStream>(realm, realm.create<Streams::TransformStream>(realm), decompressor.release_value(), move(input_stream));
  43. // 3. Let transformAlgorithm be an algorithm which takes a chunk argument and runs the decompress and enqueue a chunk
  44. // algorithm with this and chunk.
  45. auto transform_algorithm = GC::create_function(realm.heap(), [stream](JS::Value chunk) -> GC::Ref<WebIDL::Promise> {
  46. auto& realm = stream->realm();
  47. auto& vm = realm.vm();
  48. if (auto result = stream->decompress_and_enqueue_chunk(chunk); result.is_error()) {
  49. auto throw_completion = Bindings::exception_to_throw_completion(vm, result.exception());
  50. return WebIDL::create_rejected_promise(realm, *throw_completion.release_value());
  51. }
  52. return WebIDL::create_resolved_promise(realm, JS::js_undefined());
  53. });
  54. // 4. Let flushAlgorithm be an algorithm which takes no argument and runs the decompress flush and enqueue algorithm with this.
  55. auto flush_algorithm = GC::create_function(realm.heap(), [stream]() -> GC::Ref<WebIDL::Promise> {
  56. auto& realm = stream->realm();
  57. auto& vm = realm.vm();
  58. if (auto result = stream->decompress_flush_and_enqueue(); result.is_error()) {
  59. auto throw_completion = Bindings::exception_to_throw_completion(vm, result.exception());
  60. return WebIDL::create_rejected_promise(realm, *throw_completion.release_value());
  61. }
  62. return WebIDL::create_resolved_promise(realm, JS::js_undefined());
  63. });
  64. // 6. Set up this's transform with transformAlgorithm set to transformAlgorithm and flushAlgorithm set to flushAlgorithm.
  65. stream->m_transform->set_up(transform_algorithm, flush_algorithm);
  66. return stream;
  67. }
  68. DecompressionStream::DecompressionStream(JS::Realm& realm, GC::Ref<Streams::TransformStream> transform, Decompressor decompressor, NonnullOwnPtr<AllocatingMemoryStream> input_stream)
  69. : Bindings::PlatformObject(realm)
  70. , Streams::GenericTransformStreamMixin(transform)
  71. , m_decompressor(move(decompressor))
  72. , m_input_stream(move(input_stream))
  73. {
  74. }
  75. DecompressionStream::~DecompressionStream() = default;
  76. void DecompressionStream::initialize(JS::Realm& realm)
  77. {
  78. Base::initialize(realm);
  79. WEB_SET_PROTOTYPE_FOR_INTERFACE(DecompressionStream);
  80. }
  81. void DecompressionStream::visit_edges(JS::Cell::Visitor& visitor)
  82. {
  83. Base::visit_edges(visitor);
  84. Streams::GenericTransformStreamMixin::visit_edges(visitor);
  85. }
  86. // https://compression.spec.whatwg.org/#decompress-and-enqueue-a-chunk
  87. WebIDL::ExceptionOr<void> DecompressionStream::decompress_and_enqueue_chunk(JS::Value chunk)
  88. {
  89. auto& realm = this->realm();
  90. // 1. If chunk is not a BufferSource type, then throw a TypeError.
  91. if (!WebIDL::is_buffer_source_type(chunk))
  92. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Chunk is not a BufferSource type"sv };
  93. // 2. Let buffer be the result of decompressing chunk with ds's format and context. If this results in an error,
  94. // then throw a TypeError.
  95. auto buffer = [&]() -> ErrorOr<ByteBuffer> {
  96. if (auto buffer = WebIDL::underlying_buffer_source(chunk.as_object()))
  97. return decompress(buffer->buffer());
  98. return ByteBuffer {};
  99. }();
  100. if (buffer.is_error())
  101. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Unable to decompress chunk: {}", buffer.error())) };
  102. // 3. If buffer is empty, return.
  103. if (buffer.value().is_empty())
  104. return {};
  105. // 4. Split buffer into one or more non-empty pieces and convert them into Uint8Arrays.
  106. auto array_buffer = JS::ArrayBuffer::create(realm, buffer.release_value());
  107. auto array = JS::Uint8Array::create(realm, array_buffer->byte_length(), *array_buffer);
  108. // 5. For each Uint8Array array, enqueue array in ds's transform.
  109. TRY(Streams::transform_stream_default_controller_enqueue(*m_transform->controller(), array));
  110. return {};
  111. }
  112. // https://compression.spec.whatwg.org/#decompress-flush-and-enqueue
  113. WebIDL::ExceptionOr<void> DecompressionStream::decompress_flush_and_enqueue()
  114. {
  115. auto& realm = this->realm();
  116. // 1. Let buffer be the result of decompressing an empty input with ds's format and context, with the finish flag.
  117. auto buffer = decompress({});
  118. if (buffer.is_error())
  119. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Unable to compress flush: {}", buffer.error())) };
  120. // FIXME: 2. If the end of the compressed input has not been reached, then throw a TypeError.
  121. // 3. If buffer is empty, return.
  122. if (buffer.value().is_empty())
  123. return {};
  124. // 4. Split buffer into one or more non-empty pieces and convert them into Uint8Arrays.
  125. auto array_buffer = JS::ArrayBuffer::create(realm, buffer.release_value());
  126. auto array = JS::Uint8Array::create(realm, array_buffer->byte_length(), *array_buffer);
  127. // 5. For each Uint8Array array, enqueue array in ds's transform.
  128. TRY(Streams::transform_stream_default_controller_enqueue(*m_transform->controller(), array));
  129. return {};
  130. }
  131. ErrorOr<ByteBuffer> DecompressionStream::decompress(ReadonlyBytes bytes)
  132. {
  133. TRY(m_input_stream->write_until_depleted(bytes));
  134. return TRY(m_decompressor.visit([&](auto const& decompressor) {
  135. return decompressor->read_until_eof();
  136. }));
  137. }
  138. }