module Bootsnap::CompileCache::YAML
Constants
- SUPPORTED_INTERNAL_ENCODINGS
- Uncompilable
- UnsupportedTags
Attributes
cache_dir[R]
implementation[R]
msgpack_factory[RW]
supported_options[RW]
Public Class Methods
cache_dir=(cache_dir)
click to toggle source
# File lib/bootsnap/compile_cache/yaml.rb, line 22 def cache_dir=(cache_dir) @cache_dir = cache_dir.end_with?("/") ? "#{cache_dir}yaml" : "#{cache_dir}-yaml" end
init!()
click to toggle source
# File lib/bootsnap/compile_cache/yaml.rb, line 50 def init! require "yaml" require "msgpack" require "date" @implementation = ::YAML::VERSION >= "4" ? Psych4 : Psych3 if @implementation::Patch.method_defined?(:unsafe_load_file) && !::YAML.respond_to?(:unsafe_load_file) @implementation::Patch.send(:remove_method, :unsafe_load_file) end unless const_defined?(:NoTagsVisitor) visitor = Class.new(Psych::Visitors::NoAliasRuby) do def visit(target) if target.tag raise UnsupportedTags, "YAML tags are not supported: #{target.tag}" end super end end const_set(:NoTagsVisitor, visitor) 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/122 factory = MessagePack::Factory.new factory.register_type( 0x00, Symbol, packer: :to_msgpack_ext, unpacker: :from_msgpack_ext, optimized_symbol_parsing: true, ) factory.register_type( MessagePack::Timestamp::TYPE, # or just -1 Time, packer: MessagePack::Time::Packer, unpacker: MessagePack::Time::Unpacker, ) factory.register_type( 0x01, Date, packer: lambda { |date, packer| packer.write(date.year) packer.write(date.month) packer.write(date.day) }, unpacker: lambda { |unpacker| ::Date.new(unpacker.read, unpacker.read, unpacker.read) }, recursive: true, ) factory.register_type( 0x02, Regexp, packer: ->(value) { Marshal.dump(value) }, unpacker: ->(payload) { Marshal.load(payload) }, ) factory.register_type( 0x03, DateTime, packer: lambda { |dt, packer| packer.write(dt.year) packer.write(dt.month) packer.write(dt.day) packer.write(dt.hour) packer.write(dt.minute) sec = dt.sec + dt.sec_fraction packer.write(sec.numerator) packer.write(sec.denominator) offset = dt.offset packer.write(offset.numerator) packer.write(offset.denominator) }, unpacker: lambda { |unpacker| ::DateTime.new( unpacker.read, # year unpacker.read, # month unpacker.read, # day unpacker.read, # hour unpacker.read, # minute Rational(unpacker.read, unpacker.read), # sec fraction Rational(unpacker.read, unpacker.read), # offset fraction ) }, recursive: true, ) require "msgpack/bigint" factory.register_type( 0x04, Integer, packer: MessagePack::Bigint.method(:to_msgpack_ext), unpacker: MessagePack::Bigint.method(:from_msgpack_ext), oversized_integer_extension: true, ) self.msgpack_factory = factory self.supported_options = [] params = ::YAML.method(:load).parameters if params.include?([:key, :symbolize_names]) supported_options << :symbolize_names end if params.include?([:key, :freeze]) supported_options << :freeze end supported_options.freeze end
install!(cache_dir)
click to toggle source
# File lib/bootsnap/compile_cache/yaml.rb, line 37 def install!(cache_dir) self.cache_dir = cache_dir init! ::YAML.singleton_class.prepend(@implementation::Patch) end
msgpack_unpacker_options(kwargs)
click to toggle source
# File lib/bootsnap/compile_cache/yaml.rb, line 181 def msgpack_unpacker_options(kwargs) return kwargs unless kwargs&.key?(:symbolize_names) kwargs = kwargs.dup kwargs[:symbolize_keys] = kwargs.delete(:symbolize_names) kwargs end
patch()
click to toggle source
# File lib/bootsnap/compile_cache/yaml.rb, line 167 def patch @implementation::Patch end
precompile(path)
click to toggle source
# File lib/bootsnap/compile_cache/yaml.rb, line 26 def precompile(path) return false unless CompileCache::YAML.supported_internal_encoding? CompileCache::Native.precompile( cache_dir, nil, path.to_s, @implementation, ) end
strict_load(payload)
click to toggle source
# File lib/bootsnap/compile_cache/yaml.rb, line 171 def strict_load(payload) ast = ::YAML.parse(payload) return ast unless ast loader = ::Psych::ClassLoader::Restricted.new(["Symbol"], []) scanner = ::Psych::ScalarScanner.new(loader) NoTagsVisitor.new(scanner, loader).visit(ast) end
supported_internal_encoding?()
click to toggle source
Psych coerce strings to ‘Encoding.default_internal` but Message Pack only support UTF-8, US-ASCII and BINARY. So if Encoding.default_internal is set to anything else we can’t safely use the cache
# File lib/bootsnap/compile_cache/yaml.rb, line 46 def supported_internal_encoding? SUPPORTED_INTERNAL_ENCODINGS.include?(Encoding.default_internal) end
visit(target)
click to toggle source
Calls superclass method
# File lib/bootsnap/compile_cache/yaml.rb, line 62 def visit(target) if target.tag raise UnsupportedTags, "YAML tags are not supported: #{target.tag}" end super end