Class: Propshaft::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/propshaft/asset.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, logical_path:, load_path:) ⇒ Asset

Returns a new instance of Asset.



17
18
19
# 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

Instance Attribute Details

#load_pathObject (readonly)

Returns the value of attribute load_path.



6
7
8
# File 'lib/propshaft/asset.rb', line 6

def load_path
  @load_path
end

#logical_pathObject (readonly)

Returns the value of attribute logical_path.



6
7
8
# File 'lib/propshaft/asset.rb', line 6

def logical_path
  @logical_path
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/propshaft/asset.rb', line 6

def path
  @path
end

Class Method Details

.extract_path_and_digest(digested_path) ⇒ Object



9
10
11
12
13
14
# 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

Instance Method Details

#==(other_asset) ⇒ Object



71
72
73
# File 'lib/propshaft/asset.rb', line 71

def ==(other_asset)
  logical_path.hash == other_asset.logical_path.hash
end

#compiled_contentObject



21
22
23
# File 'lib/propshaft/asset.rb', line 21

def compiled_content
  @compiled_content ||= load_path.compilers.compile(self)
end

#content(encoding: "ASCII-8BIT") ⇒ Object



25
26
27
# File 'lib/propshaft/asset.rb', line 25

def content(encoding: "ASCII-8BIT")
  File.read(path, encoding: encoding, mode: "rb")
end

#content_typeObject



29
30
31
# File 'lib/propshaft/asset.rb', line 29

def content_type
  Mime::Type.lookup_by_extension(logical_path.extname.from(1))
end

#digestObject



37
38
39
# File 'lib/propshaft/asset.rb', line 37

def digest
  @digest ||= Digest::SHA1.hexdigest("#{content_with_compile_references}#{load_path.version}").first(8)
end

#digested_pathObject



59
60
61
62
63
64
65
# 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

#fresh?(digest) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/propshaft/asset.rb', line 67

def fresh?(digest)
  self.digest == digest || already_digested?
end

#integrity(hash_algorithm:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

#lengthObject



33
34
35
# File 'lib/propshaft/asset.rb', line 33

def length
  content.size
end