Module: Bootsnap::CompileCache::YAML
- Defined in:
- lib/bootsnap/compile_cache/yaml.rb
Defined Under Namespace
Modules: EncodingAwareSymbols, Psych3, Psych4
Constant Summary collapse
- Uncompilable =
Class.new(StandardError)
- UnsupportedTags =
Class.new(Uncompilable)
- SUPPORTED_INTERNAL_ENCODINGS =
[ nil, # UTF-8 Encoding::UTF_8, Encoding::ASCII, Encoding::BINARY, ].freeze
Class Attribute Summary collapse
-
.cache_dir ⇒ Object
Returns the value of attribute cache_dir.
-
.implementation ⇒ Object
readonly
Returns the value of attribute implementation.
-
.msgpack_factory ⇒ Object
Returns the value of attribute msgpack_factory.
-
.supported_options ⇒ Object
Returns the value of attribute supported_options.
Class Method Summary collapse
- .init! ⇒ Object
- .install!(cache_dir) ⇒ Object
- .patch ⇒ Object
- .precompile(path) ⇒ Object
- .strict_load(payload) ⇒ Object
-
.supported_internal_encoding? ⇒ Boolean
Psych coerce strings to ‘Encoding.default_internal` but Message Pack only support UTF-8, US-ASCII and BINARY.
Class Attribute Details
.cache_dir ⇒ Object
Returns the value of attribute cache_dir.
20 21 22 |
# File 'lib/bootsnap/compile_cache/yaml.rb', line 20 def cache_dir @cache_dir end |
.implementation ⇒ Object (readonly)
Returns the value of attribute implementation.
20 21 22 |
# File 'lib/bootsnap/compile_cache/yaml.rb', line 20 def implementation @implementation end |
.msgpack_factory ⇒ Object
Returns the value of attribute msgpack_factory.
19 20 21 |
# File 'lib/bootsnap/compile_cache/yaml.rb', line 19 def msgpack_factory @msgpack_factory end |
.supported_options ⇒ Object
Returns the value of attribute supported_options.
19 20 21 |
# File 'lib/bootsnap/compile_cache/yaml.rb', line 19 def @supported_options end |
Class Method Details
.init! ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/bootsnap/compile_cache/yaml.rb', line 57 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: EncodingAwareSymbols.method(:unpack).to_proc, ) if defined? MessagePack::Timestamp factory.register_type( MessagePack::Timestamp::TYPE, # or just -1 Time, packer: MessagePack::Time::Packer, unpacker: MessagePack::Time::Unpacker, ) marshal_fallback = { packer: ->(value) { Marshal.dump(value) }, unpacker: ->(payload) { Marshal.load(payload) }, } { Date => 0x01, Regexp => 0x02, }.each do |type, code| factory.register_type(code, type, marshal_fallback) end end self.msgpack_factory = factory self. = [] params = ::YAML.method(:load).parameters if params.include?([:key, :symbolize_names]) << :symbolize_names end if params.include?([:key, :freeze]) && factory.load(factory.dump("yaml"), freeze: true).frozen? << :freeze end .freeze end |
.install!(cache_dir) ⇒ Object
36 37 38 39 40 |
# File 'lib/bootsnap/compile_cache/yaml.rb', line 36 def install!(cache_dir) self.cache_dir = cache_dir init! ::YAML.singleton_class.prepend(@implementation::Patch) end |
.patch ⇒ Object
124 125 126 |
# File 'lib/bootsnap/compile_cache/yaml.rb', line 124 def patch @implementation::Patch end |
.precompile(path) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# 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, path.to_s, @implementation, ) end |
.strict_load(payload) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/bootsnap/compile_cache/yaml.rb', line 128 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? ⇒ Boolean
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
45 46 47 |
# File 'lib/bootsnap/compile_cache/yaml.rb', line 45 def supported_internal_encoding? SUPPORTED_INTERNAL_ENCODINGS.include?(Encoding.default_internal) end |