123456789_123456789_123456789_123456789_123456789_

Module: ActiveSupport::Messages::Metadata

Do not use. This module is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: activesupport/lib/active_support/messages/metadata.rb

Constant Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#use_message_serializer_for_metadata (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 10

singleton_class.attr_accessor :

#use_message_serializer_for_metadata?Boolean (rw, private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 60

def use_message_serializer_for_metadata?
  Metadata. && Metadata::ENVELOPE_SERIALIZERS.include?(serializer)
end

Instance Method Details

#deserialize_from_json(serialized) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 128

def deserialize_from_json(serialized)
  ActiveSupport::JSON.decode(serialized)
rescue ::JSON::ParserError => error
  # Throw :invalid_message_format instead of :invalid_message_serialization
  # because here a parse error is due to a bad message rather than an
  # incompatible `self.serializer`.
  throw :invalid_message_format, error
end

#deserialize_from_json_safe_string(string) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 141

def deserialize_from_json_safe_string(string)
  deserialize(decode(string, url_safe: false))
end

#deserialize_with_metadata(message, **expected_metadata) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 43

def (message, **)
  if (message)
    envelope = deserialize_from_json(message)
    extracted = (envelope, **)
    deserialize_from_json_safe_string(extracted["message"])
  else
    deserialized = deserialize(message)
    if (deserialized)
      (deserialized, **)["data"]
    elsif .none? { |k, v| v }
      deserialized
    else
      throw :invalid_message_content, "missing metadata"
    end
  end
end

#dual_serialized_metadata_envelope_json?(string) ⇒ Boolean (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 96

def (string)
  string.start_with?('{"_rails":{"message":')
end

#extract_from_metadata_envelope(envelope, purpose: nil) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 78

def (envelope, purpose: nil)
  hash = envelope["_rails"]

  if hash["exp"] && Time.now.utc >= parse_expiry(hash["exp"])
    throw :invalid_message_content, "expired"
  end

  if hash["pur"].to_s != purpose.to_s
    throw :invalid_message_content, "mismatched purpose"
  end

  hash
end

#metadata_envelope?(object) ⇒ Boolean (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 92

def (object)
  object.is_a?(Hash) && object.key?("_rails")
end

#parse_expiry(expires_at) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 114

def parse_expiry(expires_at)
  if !expires_at.is_a?(String)
    expires_at
  elsif ActiveSupport.use_standard_json_time_format
    Time.iso8601(expires_at)
  else
    Time.parse(expires_at)
  end
end

#pick_expiry(expires_at, expires_in) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 100

def pick_expiry(expires_at, expires_in)
  expiry = if expires_at
    expires_at.utc
  elsif expires_in
    Time.now.utc.advance(seconds: expires_in)
  end

  unless Metadata::TIMESTAMP_SERIALIZERS.include?(serializer)
    expiry = expiry&.iso8601(3)
  end

  expiry
end

#serialize_to_json(data) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 124

def serialize_to_json(data)
  ActiveSupport::JSON.encode(data)
end

#serialize_to_json_safe_string(data) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 137

def serialize_to_json_safe_string(data)
  encode(serialize(data), url_safe: false)
end

#serialize_with_metadata(data, **metadata) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 30

def (data, **)
   = .any? { |k, v| v }

  if  && !use_message_serializer_for_metadata?
    data_string = serialize_to_json_safe_string(data)
    envelope = ({ "message" => data_string }, **)
    serialize_to_json(envelope)
  else
    data = ({ "data" => data }, **) if 
    serialize(data)
  end
end

#wrap_in_metadata_envelope(hash, expires_at: nil, expires_in: nil, purpose: nil) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 64

def (hash, expires_at: nil, expires_in: nil, purpose: nil)
  expiry = pick_expiry(expires_at, expires_in)
  hash["exp"] = expiry if expiry
  hash["pur"] = purpose.to_s if purpose
  { "_rails" => hash }
end

#wrap_in_metadata_legacy_envelope(hash, expires_at: nil, expires_in: nil, purpose: nil) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/metadata.rb', line 71

def (hash, expires_at: nil, expires_in: nil, purpose: nil)
  expiry = pick_expiry(expires_at, expires_in)
  hash["exp"] = expiry
  hash["pur"] = purpose
  { "_rails" => hash }
end