diff --git a/lib/event_source/operations/mime_encode.rb b/lib/event_source/operations/mime_encode.rb index f1a4e1c..7e28290 100644 --- a/lib/event_source/operations/mime_encode.rb +++ b/lib/event_source/operations/mime_encode.rb @@ -8,7 +8,8 @@ module Operations # A class for handling payload compression. class MimeEncode include Dry::Monads[:result, :do] - + include EventSource::Logging + MIME_TYPES = %w[application/zlib application/json].freeze # Compresses the payload into a compressed string using Zlib. @@ -42,11 +43,21 @@ def validate_payload(payload, mime_type) def encode(json_payload, mime_type) encoded_data = Zlib.deflate(json_payload) if mime_type.to_s == 'application/zlib' - + + logger.debug "*" * 80 + logger.debug "Starting payload encoding for MIME type: '#{mime_type}'" + logger.debug "Original payload size: #{data_size_in_kb(json_payload)} KB" + logger.debug "Encoded payload size: #{data_size_in_kb(encoded_data)} KB" if encoded_data + logger.debug "*" * 80 + Success(encoded_data || json_payload) rescue Zlib::Error => e Failure("Failed to compress payload using Zlib: #{e.message}") end + + def data_size_in_kb(data) + (data.bytesize / 1024.0).round(2) + end end end end diff --git a/lib/event_source/publish_operation.rb b/lib/event_source/publish_operation.rb index b841272..7eb6735 100644 --- a/lib/event_source/publish_operation.rb +++ b/lib/event_source/publish_operation.rb @@ -3,6 +3,8 @@ module EventSource # Publish {EventSource::Event} messages class PublishOperation + include EventSource::Logging + # @attr_reader [EventSource::Channel] channel the channel instance used by # this PublishOperation # @attr_reader [Object] subject instance of the protocol's publish class @@ -39,7 +41,7 @@ def compress_payload_if_required(payload) message_bindings = @async_api_publish_operation.message['bindings'] encoding = message_bindings.first[1]['contentEncoding'] if message_bindings - return payload if encoding.nil? + return payload unless encoding output = EventSource::Operations::MimeEncode.new.call(encoding, payload) if output.success?