class Minipack::Manifest

Attributes

cache[W]
path[R]

Public Class Methods

new(path, cache: false) click to toggle source
# File lib/minipack/manifest.rb, line 45
def initialize(path, cache: false)
  @path = path.to_s
  @cache = cache
end

Public Instance Methods

assets() click to toggle source
# File lib/minipack/manifest.rb, line 79
def assets
  data.values
end
cache_enabled?() click to toggle source
# File lib/minipack/manifest.rb, line 83
def cache_enabled?
  @cache
end
find(name) click to toggle source

Find an entry by it's name

@param [Symbol] name entry name @return [Minipack::Entry]

# File lib/minipack/manifest.rb, line 70
def find(name)
  path = data[name.to_s] || return
  if path.is_a? Hash
    integrity = path['integrity']
    path = path['src']
  end
  Entry.new(path, integrity: integrity)
end
lookup!(name) click to toggle source
# File lib/minipack/manifest.rb, line 62
def lookup!(name)
  find(name) || handle_missing_entry(name)
end
lookup_pack_with_chunks!(name, type: nil) click to toggle source
# File lib/minipack/manifest.rb, line 50
def lookup_pack_with_chunks!(name, type: nil)
  manifest_pack_type = manifest_type(name, type)
  manifest_pack_name = manifest_name(name, manifest_pack_type)
  paths = data['entrypoints']&.dig(manifest_pack_name, manifest_pack_type) || handle_missing_entry(name)

  entries = data['entrypoints']&.dig(manifest_pack_name, manifest_pack_type).map do |source|
    entry_from_source(source) || handle_missing_entry(name)
  end

  ChunkGroup.new(entries)
end

Private Instance Methods

data() click to toggle source
# File lib/minipack/manifest.rb, line 89
def data
  if cache_enabled?
    @data ||= load_data
  else
    load_data
  end
end
entry_from_source(source) click to toggle source

Find an entry by its source

@param [String] source @return [Minipack::Entry,nil]

# File lib/minipack/manifest.rb, line 135
def entry_from_source(source)
  entry = assets.find { |entry| entry.is_a?(String) ? entry == source : entry['src'] == source }
  return unless entry
  entry.is_a?(String) ? Entry.new(entry) : Entry.new(entry['src'], integrity: entry['integrity'])
end
handle_missing_entry(name) click to toggle source
# File lib/minipack/manifest.rb, line 123
    def handle_missing_entry(name)
      raise MissingEntryError, <<~MSG
        Can not find #{name} in #{@path}.
        Your manifest contains:
        #{JSON.pretty_generate(@data)}
      MSG
    end
load_data() click to toggle source
# File lib/minipack/manifest.rb, line 97
def load_data
  u = URI.parse(@path)
  data = nil
  if u.scheme == 'file' || u.path == @path  # file path
    raise(FileNotFoundError, "#{@path}: no such manifest found") unless File.exist?(@path)
    data = File.read(@path)
  else
    # http url
    data = u.read
  end
  JSON.parse(data)
end
manifest_name(name, pack_type) click to toggle source

The `manifest_name` method strips of the file extension of the name, because in the manifest hash the entrypoints are defined by their pack name without the extension. When the user provides a name with a file extension, we want to try to strip it off.

# File lib/minipack/manifest.rb, line 113
def manifest_name(name, pack_type)
  return name if File.extname(name.to_s).empty?
  File.basename(name, '.' + pack_type)
end
manifest_type(name, pack_type) click to toggle source
# File lib/minipack/manifest.rb, line 118
def manifest_type(name, pack_type)
  return File.extname(name)[1..-1] if pack_type.nil?
  pack_type.to_s
end