# frozen_string_literal: truerequire('bootsnap/bootsnap')moduleBootsnapmoduleCompileCachemoduleYAMLclass<<selfattr_accessor(:msgpack_factory,:cache_dir,:supported_options)definput_to_storage(contents,_)obj=strict_load(contents)msgpack_factory.dump(obj)rescueNoMethodError,RangeError# The object included things that we can't serializeraise(Uncompilable)enddefstorage_to_output(data,kwargs)ifkwargs&&kwargs.key?(:symbolize_names)kwargs[:symbolize_keys]=kwargs.delete(:symbolize_names)endmsgpack_factory.load(data,kwargs)enddefinput_to_output(data,kwargs)::YAML.unsafe_load(data,**(kwargs||{}))enddefstrict_load(payload,*args)ast=::YAML.parse(payload)returnastunlessaststrict_visitor.create(*args).visit(ast)endruby2_keywords:strict_loadifrespond_to?(:ruby2_keywords,true)defprecompile(path,cache_dir: YAML.cache_dir)Bootsnap::CompileCache::Native.precompile(cache_dir,path.to_s,Bootsnap::CompileCache::YAML,)enddefinstall!(cache_dir)self.cache_dir=cache_dirinit!::YAML.singleton_class.prepend(Patch)enddefinit!require('yaml')require('msgpack')require('date')ifPatch.method_defined?(:unsafe_load_file)&&!::YAML.respond_to?(:unsafe_load_file)Patch.send(:remove_method,:unsafe_load_file)endifPatch.method_defined?(:load_file)&&::YAML::VERSION>='4'Patch.send(:remove_method,:load_file)end# MessagePack serializes symbols as strings by default.# We want them to roundtrip cleanly, so we use a custom factory.# see: https://github.com/msgpack/msgpack-ruby/pull/122factory=MessagePack::Factory.newfactory.register_type(0x00,Symbol)ifdefined?MessagePack::Timestampfactory.register_type(MessagePack::Timestamp::TYPE,# or just -1Time,packer: MessagePack::Time::Packer,unpacker: MessagePack::Time::Unpacker)marshal_fallback={packer: ->(value){Marshal.dump(value)},unpacker: ->(payload){Marshal.load(payload)},}{Date=>0x01,Regexp=>0x02,}.eachdo|type,code|factory.register_type(code,type,marshal_fallback)endendself.msgpack_factory=factoryself.supported_options=[]params=::YAML.method(:load).parametersifparams.include?([:key,:symbolize_names])self.supported_options<<:symbolize_namesendifparams.include?([:key,:freeze])iffactory.load(factory.dump('yaml'),freeze: true).frozen?self.supported_options<<:freezeendendself.supported_options.freezeenddefstrict_visitorself::NoTagsVisitor||=Class.new(Psych::Visitors::ToRuby)dodefvisit(target)iftarget.tagraiseUncompilable,"YAML tags are not supported: #{target.tag}"endsuperendendendendmodulePatchdefload_file(path,*args)returnsuperifargs.size>1ifkwargs=args.firstreturnsuperunlesskwargs.is_a?(Hash)returnsuperunless(kwargs.keys-::Bootsnap::CompileCache::YAML.supported_options).empty?endbegin::Bootsnap::CompileCache::Native.fetch(Bootsnap::CompileCache::YAML.cache_dir,File.realpath(path),::Bootsnap::CompileCache::YAML,kwargs,)rescueErrno::EACCES::Bootsnap::CompileCache.permission_error(path)endendruby2_keywords:load_fileifrespond_to?(:ruby2_keywords,true)defunsafe_load_file(path,*args)returnsuperifargs.size>1ifkwargs=args.firstreturnsuperunlesskwargs.is_a?(Hash)returnsuperunless(kwargs.keys-::Bootsnap::CompileCache::YAML.supported_options).empty?endbegin::Bootsnap::CompileCache::Native.fetch(Bootsnap::CompileCache::YAML.cache_dir,File.realpath(path),::Bootsnap::CompileCache::YAML,kwargs,)rescueErrno::EACCES::Bootsnap::CompileCache.permission_error(path)endendruby2_keywords:unsafe_load_fileifrespond_to?(:ruby2_keywords,true)endendendend