Sha256: fac10c7b59936da8fc29be8fd437f3aa6e44d3ca2e3cadb5fd3746fe658a6ef0

Contents?: true

Size: 1.66 KB

Versions: 36

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

require_relative "extensions"

module ActiveSupport
  module MessagePack
    module Serializer # :nodoc:
      SIGNATURE = "\xCC\x80".b.freeze # == 128.to_msgpack
      SIGNATURE_INT = 128

      def dump(object)
        message_pack_pool.packer do |packer|
          packer.write(SIGNATURE_INT)
          packer.write(object)
          packer.full_pack
        end
      end

      def load(dumped)
        message_pack_pool.unpacker do |unpacker|
          unpacker.feed_reference(dumped)
          raise "Invalid serialization format" unless unpacker.read == SIGNATURE_INT
          unpacker.full_unpack
        end
      end

      def signature?(dumped)
        dumped.getbyte(0) == SIGNATURE.getbyte(0) && dumped.getbyte(1) == SIGNATURE.getbyte(1)
      end

      def message_pack_factory
        @message_pack_factory ||= ::MessagePack::Factory.new
      end

      def message_pack_factory=(factory)
        @message_pack_pool = nil
        @message_pack_factory = factory
      end

      delegate :register_type, to: :message_pack_factory

      def warmup
        message_pack_pool # eagerly compute
      end

      private
        def message_pack_pool
          @message_pack_pool ||= begin
            unless message_pack_factory.frozen?
              Extensions.install(message_pack_factory)
              install_unregistered_type_handler
              message_pack_factory.freeze
            end
            message_pack_factory.pool(ENV.fetch("RAILS_MAX_THREADS", 5).to_i)
          end
        end

        def install_unregistered_type_handler
          Extensions.install_unregistered_type_error(message_pack_factory)
        end
    end
  end
end

Version data entries

36 entries across 36 versions & 6 rubygems

Version Path
activesupport-8.0.0 lib/active_support/message_pack/serializer.rb
activesupport-7.2.2 lib/active_support/message_pack/serializer.rb
activesupport-7.1.5 lib/active_support/message_pack/serializer.rb
activesupport-8.0.0.rc2 lib/active_support/message_pack/serializer.rb
activesupport-7.2.1.2 lib/active_support/message_pack/serializer.rb
activesupport-7.1.4.2 lib/active_support/message_pack/serializer.rb
activesupport-8.0.0.rc1 lib/active_support/message_pack/serializer.rb
activesupport-7.2.1.1 lib/active_support/message_pack/serializer.rb
activesupport-7.1.4.1 lib/active_support/message_pack/serializer.rb
activesupport-8.0.0.beta1 lib/active_support/message_pack/serializer.rb
omg-activesupport-8.0.0.alpha9 lib/active_support/message_pack/serializer.rb
omg-activesupport-8.0.0.alpha8 lib/active_support/message_pack/serializer.rb
omg-activesupport-8.0.0.alpha7 lib/active_support/message_pack/serializer.rb
omg-activesupport-8.0.0.alpha4 lib/active_support/message_pack/serializer.rb
omg-activesupport-8.0.0.alpha3 lib/active_support/message_pack/serializer.rb
omg-activesupport-8.0.0.alpha2 lib/active_support/message_pack/serializer.rb
omg-activesupport-8.0.0.alpha1 lib/active_support/message_pack/serializer.rb
activesupport-7.1.4 lib/active_support/message_pack/serializer.rb
activesupport-7.2.1 lib/active_support/message_pack/serializer.rb
activesupport-7.2.0 lib/active_support/message_pack/serializer.rb