class Propshaft::Asset
Attributes
Public Class Methods
Source
# File lib/propshaft/asset.rb, line 9 def extract_path_and_digest(digested_path) digest = digested_path[/-([0-9a-zA-Z]{7,128})\.(?!digested)([^.]|.map)+\z/, 1] path = digest ? digested_path.sub("-#{digest}", "") : digested_path [path, digest] end
Source
# File lib/propshaft/asset.rb, line 17 def initialize(path, logical_path:, load_path:) @path, @logical_path, @load_path = path, Pathname.new(logical_path), load_path end
Public Instance Methods
Source
# File lib/propshaft/asset.rb, line 71 def ==(other_asset) logical_path.hash == other_asset.logical_path.hash end
Source
# File lib/propshaft/asset.rb, line 21 def compiled_content @compiled_content ||= load_path.compilers.compile(self) end
Source
# File lib/propshaft/asset.rb, line 25 def content(encoding: "ASCII-8BIT") File.read(path, encoding: encoding, mode: "rb") end
Source
# File lib/propshaft/asset.rb, line 29 def content_type Mime::Type.lookup_by_extension(logical_path.extname.from(1)) end
Source
# File lib/propshaft/asset.rb, line 37 def digest @digest ||= Digest::SHA1.hexdigest("#{content_with_compile_references}#{load_path.version}").first(8) end
Source
# File lib/propshaft/asset.rb, line 59 def digested_path if already_digested? logical_path else logical_path.sub(/\.(\w+(\.map)?)$/) { |ext| "-#{digest}#{ext}" } end end
Source
# File lib/propshaft/asset.rb, line 67 def fresh?(digest) self.digest == digest || already_digested? end
Source
# File lib/propshaft/asset.rb, line 41 def integrity(hash_algorithm:) # Following the Subresource Integrity spec draft # https://w3c.github.io/webappsec-subresource-integrity/ # allowing only sha256, sha384, and sha512 bitlen = case hash_algorithm when "sha256" 256 when "sha384" 384 when "sha512" 512 else raise(StandardError.new("Subresource Integrity hash algorithm must be one of SHA2 family (sha256, sha384, sha512)")) end [hash_algorithm, Digest::SHA2.new(bitlen).base64digest(compiled_content)].join("-") end
Private Instance Methods
Source
# File lib/propshaft/asset.rb, line 80 def already_digested? logical_path.to_s =~ /-([0-9a-zA-Z_-]{7,128})\.digested/ end
Source
# File lib/propshaft/asset.rb, line 76 def content_with_compile_references content + load_path.find_referenced_by(self).collect(&:content).join end