/* * Copyright (c) 2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include #include #include #include namespace Web::Compression { GC_DEFINE_ALLOCATOR(CompressionStream); // https://compression.spec.whatwg.org/#dom-compressionstream-compressionstream WebIDL::ExceptionOr> CompressionStream::construct_impl(JS::Realm& realm, Bindings::CompressionFormat format) { // 1. If format is unsupported in CompressionStream, then throw a TypeError. // 2. Set this's format to format. auto input_stream = make(); auto compressor = [&, input_stream = MaybeOwned { *input_stream }]() mutable -> ErrorOr { switch (format) { case Bindings::CompressionFormat::Deflate: return TRY(Compress::ZlibCompressor::construct(move(input_stream))); case Bindings::CompressionFormat::DeflateRaw: return TRY(Compress::DeflateCompressor::construct(make(move(input_stream)))); case Bindings::CompressionFormat::Gzip: return TRY(Compress::GzipCompressor::create(move(input_stream))); } VERIFY_NOT_REACHED(); }(); if (compressor.is_error()) return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Unable to create compressor: {}", compressor.error())) }; // 5. Set this's transform to a new TransformStream. // NOTE: We do this first so that we may store it as nonnull in the GenericTransformStream. auto stream = realm.create(realm, realm.create(realm), compressor.release_value(), move(input_stream)); // 3. Let transformAlgorithm be an algorithm which takes a chunk argument and runs the compress and enqueue a chunk // algorithm with this and chunk. auto transform_algorithm = GC::create_function(realm.heap(), [stream](JS::Value chunk) -> GC::Ref { auto& realm = stream->realm(); auto& vm = realm.vm(); if (auto result = stream->compress_and_enqueue_chunk(chunk); result.is_error()) { auto throw_completion = Bindings::dom_exception_to_throw_completion(vm, result.exception()); return WebIDL::create_rejected_promise(realm, *throw_completion.release_value()); } return WebIDL::create_resolved_promise(realm, JS::js_undefined()); }); // 4. Let flushAlgorithm be an algorithm which takes no argument and runs the compress flush and enqueue algorithm with this. auto flush_algorithm = GC::create_function(realm.heap(), [stream]() -> GC::Ref { auto& realm = stream->realm(); auto& vm = realm.vm(); if (auto result = stream->compress_flush_and_enqueue(); result.is_error()) { auto throw_completion = Bindings::dom_exception_to_throw_completion(vm, result.exception()); return WebIDL::create_rejected_promise(realm, *throw_completion.release_value()); } return WebIDL::create_resolved_promise(realm, JS::js_undefined()); }); // 6. Set up this's transform with transformAlgorithm set to transformAlgorithm and flushAlgorithm set to flushAlgorithm. Streams::transform_stream_set_up(stream->m_transform, transform_algorithm, flush_algorithm); return stream; } CompressionStream::CompressionStream(JS::Realm& realm, GC::Ref transform, Compressor compressor, NonnullOwnPtr input_stream) : Bindings::PlatformObject(realm) , Streams::GenericTransformStreamMixin(transform) , m_compressor(move(compressor)) , m_output_stream(move(input_stream)) { } CompressionStream::~CompressionStream() = default; void CompressionStream::initialize(JS::Realm& realm) { Base::initialize(realm); WEB_SET_PROTOTYPE_FOR_INTERFACE(CompressionStream); } void CompressionStream::visit_edges(JS::Cell::Visitor& visitor) { Base::visit_edges(visitor); Streams::GenericTransformStreamMixin::visit_edges(visitor); } // https://compression.spec.whatwg.org/#compress-and-enqueue-a-chunk WebIDL::ExceptionOr CompressionStream::compress_and_enqueue_chunk(JS::Value chunk) { auto& realm = this->realm(); // 1. If chunk is not a BufferSource type, then throw a TypeError. if (!WebIDL::is_buffer_source_type(chunk)) return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Chunk is not a BufferSource type"sv }; // 2. Let buffer be the result of compressing chunk with cs's format and context. auto buffer = [&]() -> ErrorOr { if (auto buffer = WebIDL::underlying_buffer_source(chunk.as_object())) return compress(buffer->buffer(), Finish::No); return ByteBuffer {}; }(); if (buffer.is_error()) return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Unable to compress chunk: {}", buffer.error())) }; // 3. If buffer is empty, return. if (buffer.value().is_empty()) return {}; // 4. Split buffer into one or more non-empty pieces and convert them into Uint8Arrays. auto array_buffer = JS::ArrayBuffer::create(realm, buffer.release_value()); auto array = JS::Uint8Array::create(realm, array_buffer->byte_length(), *array_buffer); // 5. For each Uint8Array array, enqueue array in cs's transform. TRY(Streams::transform_stream_default_controller_enqueue(*m_transform->controller(), array)); return {}; } // https://compression.spec.whatwg.org/#compress-flush-and-enqueue WebIDL::ExceptionOr CompressionStream::compress_flush_and_enqueue() { auto& realm = this->realm(); // 1. Let buffer be the result of compressing an empty input with cs's format and context, with the finish flag. auto buffer = compress({}, Finish::Yes); if (buffer.is_error()) return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Unable to compress flush: {}", buffer.error())) }; // 2. If buffer is empty, return. if (buffer.value().is_empty()) return {}; // 3. Split buffer into one or more non-empty pieces and convert them into Uint8Arrays. auto array_buffer = JS::ArrayBuffer::create(realm, buffer.release_value()); auto array = JS::Uint8Array::create(realm, array_buffer->byte_length(), *array_buffer); // 4. For each Uint8Array array, enqueue array in cs's transform. TRY(Streams::transform_stream_default_controller_enqueue(*m_transform->controller(), array)); return {}; } ErrorOr CompressionStream::compress(ReadonlyBytes bytes, Finish finish) { TRY(m_compressor.visit([&](auto const& compressor) { return compressor->write_until_depleted(bytes); })); if (finish == Finish::Yes) { TRY(m_compressor.visit( [&](NonnullOwnPtr const& compressor) { return compressor->finish(); }, [&](NonnullOwnPtr const& compressor) { return compressor->final_flush(); }, [&](NonnullOwnPtr const& compressor) { return compressor->finish(); })); } auto buffer = TRY(ByteBuffer::create_uninitialized(m_output_stream->used_buffer_size())); TRY(m_output_stream->read_until_filled(buffer.bytes())); return buffer; } }