Module: Bootsnap::CompileCache::YAML
- Defined in:
- lib/bootsnap/compile_cache/yaml.rb
Defined Under Namespace
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
- .msgpack_unpacker_options(kwargs) ⇒ Object
- .patch ⇒ Object
- .precompile(path) ⇒ Object
- .strict_load(payload) ⇒ Object
-
.supported_internal_encoding? ⇒ Boolean
Psych coerce strings to
Encoding.default_internalbut 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
50 51 52 53 54 55 56 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# 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. = [] params = ::YAML.method(:load).parameters if params.include?([:key, :symbolize_names]) << :symbolize_names end if params.include?([:key, :freeze]) << :freeze end .freeze end |
.install!(cache_dir) ⇒ Object
37 38 39 40 41 |
# 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) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/bootsnap/compile_cache/yaml.rb', line 181 def (kwargs) return kwargs unless kwargs&.key?(:symbolize_names) kwargs = kwargs.dup kwargs[:symbolize_keys] = kwargs.delete(:symbolize_names) kwargs end |
.patch ⇒ Object
167 168 169 |
# File 'lib/bootsnap/compile_cache/yaml.rb', line 167 def patch @implementation::Patch end |
.precompile(path) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# 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) ⇒ Object
171 172 173 174 175 176 177 178 179 |
# 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? ⇒ 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
46 47 48 |
# File 'lib/bootsnap/compile_cache/yaml.rb', line 46 def supported_internal_encoding? SUPPORTED_INTERNAL_ENCODINGS.include?(Encoding.default_internal) end |