DecompressionStream.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // FIXME: Our zlib decompressor assumes the initial data contains the zlib header. We don't have any data yet,
  31. // so this will always fail.
  32. return TRY(Compress::ZlibDecompressor::create(move(input_stream)));
  33. case Bindings::CompressionFormat::DeflateRaw:
  34. return TRY(Compress::DeflateDecompressor::construct(make<LittleEndianInputBitStream>(move(input_stream))));
  35. case Bindings::CompressionFormat::Gzip:
  36. return make<Compress::GzipDecompressor>(move(input_stream));
  37. }
  38. VERIFY_NOT_REACHED();
  39. }();
  40. if (decompressor.is_error())
  41. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Unable to create decompressor: {}", decompressor.error())) };
  42. // 5. Set this's transform to a new TransformStream.
  43. // NOTE: We do this first so that we may store it as nonnull in the GenericTransformStream.
  44. auto stream = realm.create<DecompressionStream>(realm, realm.create<Streams::TransformStream>(realm), decompressor.release_value(), move(input_stream));
  45. // 3. Let transformAlgorithm be an algorithm which takes a chunk argument and runs the decompress and enqueue a chunk
  46. // algorithm with this and chunk.
  47. auto transform_algorithm = GC::create_function(realm.heap(), [stream](JS::Value chunk) -> GC::Ref<WebIDL::Promise> {
  48. auto& realm = stream->realm();
  49. auto& vm = realm.vm();
  50. if (auto result = stream->decompress_and_enqueue_chunk(chunk); result.is_error()) {
  51. auto throw_completion = Bindings::dom_exception_to_throw_completion(vm, result.exception());
  52. return WebIDL::create_rejected_promise(realm, *throw_completion.release_value());
  53. }
  54. return WebIDL::create_resolved_promise(realm, JS::js_undefined());
  55. });
  56. // 4. Let flushAlgorithm be an algorithm which takes no argument and runs the decompress flush and enqueue algorithm with this.
  57. auto flush_algorithm = GC::create_function(realm.heap(), [stream]() -> GC::Ref<WebIDL::Promise> {
  58. auto& realm = stream->realm();
  59. auto& vm = realm.vm();
  60. if (auto result = stream->decompress_flush_and_enqueue(); result.is_error()) {
  61. auto throw_completion = Bindings::dom_exception_to_throw_completion(vm, result.exception());
  62. return WebIDL::create_rejected_promise(realm, *throw_completion.release_value());
  63. }
  64. return WebIDL::create_resolved_promise(realm, JS::js_undefined());
  65. });
  66. // 6. Set up this's transform with transformAlgorithm set to transformAlgorithm and flushAlgorithm set to flushAlgorithm.
  67. Streams::transform_stream_set_up(stream->m_transform, transform_algorithm, flush_algorithm);
  68. return stream;
  69. }
  70. DecompressionStream::DecompressionStream(JS::Realm& realm, GC::Ref<Streams::TransformStream> transform, Decompressor decompressor, NonnullOwnPtr<AllocatingMemoryStream> input_stream)
  71. : Bindings::PlatformObject(realm)
  72. , Streams::GenericTransformStreamMixin(transform)
  73. , m_decompressor(move(decompressor))
  74. , m_input_stream(move(input_stream))
  75. {
  76. }
  77. DecompressionStream::~DecompressionStream() = default;
  78. void DecompressionStream::initialize(JS::Realm& realm)
  79. {
  80. Base::initialize(realm);
  81. WEB_SET_PROTOTYPE_FOR_INTERFACE(DecompressionStream);
  82. }
  83. void DecompressionStream::visit_edges(JS::Cell::Visitor& visitor)
  84. {
  85. Base::visit_edges(visitor);
  86. Streams::GenericTransformStreamMixin::visit_edges(visitor);
  87. }
  88. // https://compression.spec.whatwg.org/#decompress-and-enqueue-a-chunk
  89. WebIDL::ExceptionOr<void> DecompressionStream::decompress_and_enqueue_chunk(JS::Value chunk)
  90. {
  91. auto& realm = this->realm();
  92. // 1. If chunk is not a BufferSource type, then throw a TypeError.
  93. if (!WebIDL::is_buffer_source_type(chunk))
  94. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Chunk is not a BufferSource type"sv };
  95. // 2. Let buffer be the result of decompressing chunk with ds's format and context. If this results in an error,
  96. // then throw a TypeError.
  97. auto buffer = [&]() -> ErrorOr<ByteBuffer> {
  98. if (auto buffer = WebIDL::underlying_buffer_source(chunk.as_object()))
  99. return decompress(buffer->buffer());
  100. return ByteBuffer {};
  101. }();
  102. if (buffer.is_error())
  103. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Unable to decompress chunk: {}", buffer.error())) };
  104. // 3. If buffer is empty, return.
  105. if (buffer.value().is_empty())
  106. return {};
  107. // 4. Split buffer into one or more non-empty pieces and convert them into Uint8Arrays.
  108. auto array_buffer = JS::ArrayBuffer::create(realm, buffer.release_value());
  109. auto array = JS::Uint8Array::create(realm, array_buffer->byte_length(), *array_buffer);
  110. // 5. For each Uint8Array array, enqueue array in ds's transform.
  111. TRY(Streams::transform_stream_default_controller_enqueue(*m_transform->controller(), array));
  112. return {};
  113. }
  114. // https://compression.spec.whatwg.org/#decompress-flush-and-enqueue
  115. WebIDL::ExceptionOr<void> DecompressionStream::decompress_flush_and_enqueue()
  116. {
  117. auto& realm = this->realm();
  118. // 1. Let buffer be the result of decompressing an empty input with ds's format and context, with the finish flag.
  119. auto buffer = decompress({});
  120. if (buffer.is_error())
  121. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Unable to compress flush: {}", buffer.error())) };
  122. // FIXME: 2. If the end of the compressed input has not been reached, then throw a TypeError.
  123. // 3. If buffer is empty, return.
  124. if (buffer.value().is_empty())
  125. return {};
  126. // 4. Split buffer into one or more non-empty pieces and convert them into Uint8Arrays.
  127. auto array_buffer = JS::ArrayBuffer::create(realm, buffer.release_value());
  128. auto array = JS::Uint8Array::create(realm, array_buffer->byte_length(), *array_buffer);
  129. // 5. For each Uint8Array array, enqueue array in ds's transform.
  130. TRY(Streams::transform_stream_default_controller_enqueue(*m_transform->controller(), array));
  131. return {};
  132. }
  133. ErrorOr<ByteBuffer> DecompressionStream::decompress(ReadonlyBytes bytes)
  134. {
  135. TRY(m_input_stream->write_until_depleted(bytes));
  136. return TRY(m_decompressor.visit([&](auto const& decompressor) {
  137. return decompressor->read_until_eof();
  138. }));
  139. }
  140. }